1 /* 2 * libjingle 3 * Copyright 2012, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "talk/examples/peerconnection/client/conductor.h" 29 30 #include <utility> 31 32 #include "talk/app/webrtc/videosourceinterface.h" 33 #include "talk/base/common.h" 34 #include "talk/base/json.h" 35 #include "talk/base/logging.h" 36 #include "talk/examples/peerconnection/client/defaults.h" 37 #include "talk/media/devices/devicemanager.h" 38 39 // Names used for a IceCandidate JSON object. 40 const char kCandidateSdpMidName[] = "sdpMid"; 41 const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex"; 42 const char kCandidateSdpName[] = "candidate"; 43 44 // Names used for a SessionDescription JSON object. 45 const char kSessionDescriptionTypeName[] = "type"; 46 const char kSessionDescriptionSdpName[] = "sdp"; 47 48 class DummySetSessionDescriptionObserver 49 : public webrtc::SetSessionDescriptionObserver { 50 public: 51 static DummySetSessionDescriptionObserver* Create() { 52 return 53 new talk_base::RefCountedObject<DummySetSessionDescriptionObserver>(); 54 } 55 virtual void OnSuccess() { 56 LOG(INFO) << __FUNCTION__; 57 } 58 virtual void OnFailure(const std::string& error) { 59 LOG(INFO) << __FUNCTION__ << " " << error; 60 } 61 62 protected: 63 DummySetSessionDescriptionObserver() {} 64 ~DummySetSessionDescriptionObserver() {} 65 }; 66 67 Conductor::Conductor(PeerConnectionClient* client, MainWindow* main_wnd) 68 : peer_id_(-1), 69 client_(client), 70 main_wnd_(main_wnd) { 71 client_->RegisterObserver(this); 72 main_wnd->RegisterObserver(this); 73 } 74 75 Conductor::~Conductor() { 76 ASSERT(peer_connection_.get() == NULL); 77 } 78 79 bool Conductor::connection_active() const { 80 return peer_connection_.get() != NULL; 81 } 82 83 void Conductor::Close() { 84 client_->SignOut(); 85 DeletePeerConnection(); 86 } 87 88 bool Conductor::InitializePeerConnection() { 89 ASSERT(peer_connection_factory_.get() == NULL); 90 ASSERT(peer_connection_.get() == NULL); 91 92 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(); 93 94 if (!peer_connection_factory_.get()) { 95 main_wnd_->MessageBox("Error", 96 "Failed to initialize PeerConnectionFactory", true); 97 DeletePeerConnection(); 98 return false; 99 } 100 101 webrtc::PeerConnectionInterface::IceServers servers; 102 webrtc::PeerConnectionInterface::IceServer server; 103 server.uri = GetPeerConnectionString(); 104 servers.push_back(server); 105 peer_connection_ = peer_connection_factory_->CreatePeerConnection(servers, 106 NULL, 107 NULL, 108 this); 109 if (!peer_connection_.get()) { 110 main_wnd_->MessageBox("Error", 111 "CreatePeerConnection failed", true); 112 DeletePeerConnection(); 113 } 114 AddStreams(); 115 return peer_connection_.get() != NULL; 116 } 117 118 void Conductor::DeletePeerConnection() { 119 peer_connection_ = NULL; 120 active_streams_.clear(); 121 main_wnd_->StopLocalRenderer(); 122 main_wnd_->StopRemoteRenderer(); 123 peer_connection_factory_ = NULL; 124 peer_id_ = -1; 125 } 126 127 void Conductor::EnsureStreamingUI() { 128 ASSERT(peer_connection_.get() != NULL); 129 if (main_wnd_->IsWindow()) { 130 if (main_wnd_->current_ui() != MainWindow::STREAMING) 131 main_wnd_->SwitchToStreamingUI(); 132 } 133 } 134 135 // 136 // PeerConnectionObserver implementation. 137 // 138 139 void Conductor::OnError() { 140 LOG(LS_ERROR) << __FUNCTION__; 141 main_wnd_->QueueUIThreadCallback(PEER_CONNECTION_ERROR, NULL); 142 } 143 144 // Called when a remote stream is added 145 void Conductor::OnAddStream(webrtc::MediaStreamInterface* stream) { 146 LOG(INFO) << __FUNCTION__ << " " << stream->label(); 147 148 stream->AddRef(); 149 main_wnd_->QueueUIThreadCallback(NEW_STREAM_ADDED, 150 stream); 151 } 152 153 void Conductor::OnRemoveStream(webrtc::MediaStreamInterface* stream) { 154 LOG(INFO) << __FUNCTION__ << " " << stream->label(); 155 stream->AddRef(); 156 main_wnd_->QueueUIThreadCallback(STREAM_REMOVED, 157 stream); 158 } 159 160 void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) { 161 LOG(INFO) << __FUNCTION__ << " " << candidate->sdp_mline_index(); 162 Json::StyledWriter writer; 163 Json::Value jmessage; 164 165 jmessage[kCandidateSdpMidName] = candidate->sdp_mid(); 166 jmessage[kCandidateSdpMlineIndexName] = candidate->sdp_mline_index(); 167 std::string sdp; 168 if (!candidate->ToString(&sdp)) { 169 LOG(LS_ERROR) << "Failed to serialize candidate"; 170 return; 171 } 172 jmessage[kCandidateSdpName] = sdp; 173 SendMessage(writer.write(jmessage)); 174 } 175 176 // 177 // PeerConnectionClientObserver implementation. 178 // 179 180 void Conductor::OnSignedIn() { 181 LOG(INFO) << __FUNCTION__; 182 main_wnd_->SwitchToPeerList(client_->peers()); 183 } 184 185 void Conductor::OnDisconnected() { 186 LOG(INFO) << __FUNCTION__; 187 188 DeletePeerConnection(); 189 190 if (main_wnd_->IsWindow()) 191 main_wnd_->SwitchToConnectUI(); 192 } 193 194 void Conductor::OnPeerConnected(int id, const std::string& name) { 195 LOG(INFO) << __FUNCTION__; 196 // Refresh the list if we're showing it. 197 if (main_wnd_->current_ui() == MainWindow::LIST_PEERS) 198 main_wnd_->SwitchToPeerList(client_->peers()); 199 } 200 201 void Conductor::OnPeerDisconnected(int id) { 202 LOG(INFO) << __FUNCTION__; 203 if (id == peer_id_) { 204 LOG(INFO) << "Our peer disconnected"; 205 main_wnd_->QueueUIThreadCallback(PEER_CONNECTION_CLOSED, NULL); 206 } else { 207 // Refresh the list if we're showing it. 208 if (main_wnd_->current_ui() == MainWindow::LIST_PEERS) 209 main_wnd_->SwitchToPeerList(client_->peers()); 210 } 211 } 212 213 void Conductor::OnMessageFromPeer(int peer_id, const std::string& message) { 214 ASSERT(peer_id_ == peer_id || peer_id_ == -1); 215 ASSERT(!message.empty()); 216 217 if (!peer_connection_.get()) { 218 ASSERT(peer_id_ == -1); 219 peer_id_ = peer_id; 220 221 if (!InitializePeerConnection()) { 222 LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance"; 223 client_->SignOut(); 224 return; 225 } 226 } else if (peer_id != peer_id_) { 227 ASSERT(peer_id_ != -1); 228 LOG(WARNING) << "Received a message from unknown peer while already in a " 229 "conversation with a different peer."; 230 return; 231 } 232 233 Json::Reader reader; 234 Json::Value jmessage; 235 if (!reader.parse(message, jmessage)) { 236 LOG(WARNING) << "Received unknown message. " << message; 237 return; 238 } 239 std::string type; 240 std::string json_object; 241 242 GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName, &type); 243 if (!type.empty()) { 244 std::string sdp; 245 if (!GetStringFromJsonObject(jmessage, kSessionDescriptionSdpName, &sdp)) { 246 LOG(WARNING) << "Can't parse received session description message."; 247 return; 248 } 249 webrtc::SessionDescriptionInterface* session_description( 250 webrtc::CreateSessionDescription(type, sdp)); 251 if (!session_description) { 252 LOG(WARNING) << "Can't parse received session description message."; 253 return; 254 } 255 LOG(INFO) << " Received session description :" << message; 256 peer_connection_->SetRemoteDescription( 257 DummySetSessionDescriptionObserver::Create(), session_description); 258 if (session_description->type() == 259 webrtc::SessionDescriptionInterface::kOffer) { 260 peer_connection_->CreateAnswer(this, NULL); 261 } 262 return; 263 } else { 264 std::string sdp_mid; 265 int sdp_mlineindex = 0; 266 std::string sdp; 267 if (!GetStringFromJsonObject(jmessage, kCandidateSdpMidName, &sdp_mid) || 268 !GetIntFromJsonObject(jmessage, kCandidateSdpMlineIndexName, 269 &sdp_mlineindex) || 270 !GetStringFromJsonObject(jmessage, kCandidateSdpName, &sdp)) { 271 LOG(WARNING) << "Can't parse received message."; 272 return; 273 } 274 talk_base::scoped_ptr<webrtc::IceCandidateInterface> candidate( 275 webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, sdp)); 276 if (!candidate.get()) { 277 LOG(WARNING) << "Can't parse received candidate message."; 278 return; 279 } 280 if (!peer_connection_->AddIceCandidate(candidate.get())) { 281 LOG(WARNING) << "Failed to apply the received candidate"; 282 return; 283 } 284 LOG(INFO) << " Received candidate :" << message; 285 return; 286 } 287 } 288 289 void Conductor::OnMessageSent(int err) { 290 // Process the next pending message if any. 291 main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, NULL); 292 } 293 294 void Conductor::OnServerConnectionFailure() { 295 main_wnd_->MessageBox("Error", ("Failed to connect to " + server_).c_str(), 296 true); 297 } 298 299 // 300 // MainWndCallback implementation. 301 // 302 303 void Conductor::StartLogin(const std::string& server, int port) { 304 if (client_->is_connected()) 305 return; 306 server_ = server; 307 client_->Connect(server, port, GetPeerName()); 308 } 309 310 void Conductor::DisconnectFromServer() { 311 if (client_->is_connected()) 312 client_->SignOut(); 313 } 314 315 void Conductor::ConnectToPeer(int peer_id) { 316 ASSERT(peer_id_ == -1); 317 ASSERT(peer_id != -1); 318 319 if (peer_connection_.get()) { 320 main_wnd_->MessageBox("Error", 321 "We only support connecting to one peer at a time", true); 322 return; 323 } 324 325 if (InitializePeerConnection()) { 326 peer_id_ = peer_id; 327 peer_connection_->CreateOffer(this, NULL); 328 } else { 329 main_wnd_->MessageBox("Error", "Failed to initialize PeerConnection", true); 330 } 331 } 332 333 cricket::VideoCapturer* Conductor::OpenVideoCaptureDevice() { 334 talk_base::scoped_ptr<cricket::DeviceManagerInterface> dev_manager( 335 cricket::DeviceManagerFactory::Create()); 336 if (!dev_manager->Init()) { 337 LOG(LS_ERROR) << "Can't create device manager"; 338 return NULL; 339 } 340 std::vector<cricket::Device> devs; 341 if (!dev_manager->GetVideoCaptureDevices(&devs)) { 342 LOG(LS_ERROR) << "Can't enumerate video devices"; 343 return NULL; 344 } 345 std::vector<cricket::Device>::iterator dev_it = devs.begin(); 346 cricket::VideoCapturer* capturer = NULL; 347 for (; dev_it != devs.end(); ++dev_it) { 348 capturer = dev_manager->CreateVideoCapturer(*dev_it); 349 if (capturer != NULL) 350 break; 351 } 352 return capturer; 353 } 354 355 void Conductor::AddStreams() { 356 if (active_streams_.find(kStreamLabel) != active_streams_.end()) 357 return; // Already added. 358 359 talk_base::scoped_refptr<webrtc::AudioTrackInterface> audio_track( 360 peer_connection_factory_->CreateAudioTrack( 361 kAudioLabel, peer_connection_factory_->CreateAudioSource(NULL))); 362 363 talk_base::scoped_refptr<webrtc::VideoTrackInterface> video_track( 364 peer_connection_factory_->CreateVideoTrack( 365 kVideoLabel, 366 peer_connection_factory_->CreateVideoSource(OpenVideoCaptureDevice(), 367 NULL))); 368 main_wnd_->StartLocalRenderer(video_track); 369 370 talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream = 371 peer_connection_factory_->CreateLocalMediaStream(kStreamLabel); 372 373 stream->AddTrack(audio_track); 374 stream->AddTrack(video_track); 375 if (!peer_connection_->AddStream(stream, NULL)) { 376 LOG(LS_ERROR) << "Adding stream to PeerConnection failed"; 377 } 378 typedef std::pair<std::string, 379 talk_base::scoped_refptr<webrtc::MediaStreamInterface> > 380 MediaStreamPair; 381 active_streams_.insert(MediaStreamPair(stream->label(), stream)); 382 main_wnd_->SwitchToStreamingUI(); 383 } 384 385 void Conductor::DisconnectFromCurrentPeer() { 386 LOG(INFO) << __FUNCTION__; 387 if (peer_connection_.get()) { 388 client_->SendHangUp(peer_id_); 389 DeletePeerConnection(); 390 } 391 392 if (main_wnd_->IsWindow()) 393 main_wnd_->SwitchToPeerList(client_->peers()); 394 } 395 396 void Conductor::UIThreadCallback(int msg_id, void* data) { 397 switch (msg_id) { 398 case PEER_CONNECTION_CLOSED: 399 LOG(INFO) << "PEER_CONNECTION_CLOSED"; 400 DeletePeerConnection(); 401 402 ASSERT(active_streams_.empty()); 403 404 if (main_wnd_->IsWindow()) { 405 if (client_->is_connected()) { 406 main_wnd_->SwitchToPeerList(client_->peers()); 407 } else { 408 main_wnd_->SwitchToConnectUI(); 409 } 410 } else { 411 DisconnectFromServer(); 412 } 413 break; 414 415 case SEND_MESSAGE_TO_PEER: { 416 LOG(INFO) << "SEND_MESSAGE_TO_PEER"; 417 std::string* msg = reinterpret_cast<std::string*>(data); 418 if (msg) { 419 // For convenience, we always run the message through the queue. 420 // This way we can be sure that messages are sent to the server 421 // in the same order they were signaled without much hassle. 422 pending_messages_.push_back(msg); 423 } 424 425 if (!pending_messages_.empty() && !client_->IsSendingMessage()) { 426 msg = pending_messages_.front(); 427 pending_messages_.pop_front(); 428 429 if (!client_->SendToPeer(peer_id_, *msg) && peer_id_ != -1) { 430 LOG(LS_ERROR) << "SendToPeer failed"; 431 DisconnectFromServer(); 432 } 433 delete msg; 434 } 435 436 if (!peer_connection_.get()) 437 peer_id_ = -1; 438 439 break; 440 } 441 442 case PEER_CONNECTION_ERROR: 443 main_wnd_->MessageBox("Error", "an unknown error occurred", true); 444 break; 445 446 case NEW_STREAM_ADDED: { 447 webrtc::MediaStreamInterface* stream = 448 reinterpret_cast<webrtc::MediaStreamInterface*>( 449 data); 450 webrtc::VideoTrackVector tracks = stream->GetVideoTracks(); 451 // Only render the first track. 452 if (!tracks.empty()) { 453 webrtc::VideoTrackInterface* track = tracks[0]; 454 main_wnd_->StartRemoteRenderer(track); 455 } 456 stream->Release(); 457 break; 458 } 459 460 case STREAM_REMOVED: { 461 // Remote peer stopped sending a stream. 462 webrtc::MediaStreamInterface* stream = 463 reinterpret_cast<webrtc::MediaStreamInterface*>( 464 data); 465 stream->Release(); 466 break; 467 } 468 469 default: 470 ASSERT(false); 471 break; 472 } 473 } 474 475 void Conductor::OnSuccess(webrtc::SessionDescriptionInterface* desc) { 476 peer_connection_->SetLocalDescription( 477 DummySetSessionDescriptionObserver::Create(), desc); 478 Json::StyledWriter writer; 479 Json::Value jmessage; 480 jmessage[kSessionDescriptionTypeName] = desc->type(); 481 std::string sdp; 482 desc->ToString(&sdp); 483 jmessage[kSessionDescriptionSdpName] = sdp; 484 SendMessage(writer.write(jmessage)); 485 } 486 487 void Conductor::OnFailure(const std::string& error) { 488 LOG(LERROR) << error; 489 } 490 491 void Conductor::SendMessage(const std::string& json_object) { 492 std::string* msg = new std::string(json_object); 493 main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, msg); 494 } 495