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/examples/peerconnection/client/defaults.h"
34 #include "talk/media/devices/devicemanager.h"
35 #include "webrtc/base/common.h"
36 #include "webrtc/base/json.h"
37 #include "webrtc/base/logging.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:
Create()51 static DummySetSessionDescriptionObserver* Create() {
52 return
53 new rtc::RefCountedObject<DummySetSessionDescriptionObserver>();
54 }
OnSuccess()55 virtual void OnSuccess() {
56 LOG(INFO) << __FUNCTION__;
57 }
OnFailure(const std::string & error)58 virtual void OnFailure(const std::string& error) {
59 LOG(INFO) << __FUNCTION__ << " " << error;
60 }
61
62 protected:
DummySetSessionDescriptionObserver()63 DummySetSessionDescriptionObserver() {}
~DummySetSessionDescriptionObserver()64 ~DummySetSessionDescriptionObserver() {}
65 };
66
Conductor(PeerConnectionClient * client,MainWindow * main_wnd)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
~Conductor()75 Conductor::~Conductor() {
76 ASSERT(peer_connection_.get() == NULL);
77 }
78
connection_active() const79 bool Conductor::connection_active() const {
80 return peer_connection_.get() != NULL;
81 }
82
Close()83 void Conductor::Close() {
84 client_->SignOut();
85 DeletePeerConnection();
86 }
87
InitializePeerConnection()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 NULL,
109 this);
110 if (!peer_connection_.get()) {
111 main_wnd_->MessageBox("Error",
112 "CreatePeerConnection failed", true);
113 DeletePeerConnection();
114 }
115 AddStreams();
116 return peer_connection_.get() != NULL;
117 }
118
DeletePeerConnection()119 void Conductor::DeletePeerConnection() {
120 peer_connection_ = NULL;
121 active_streams_.clear();
122 main_wnd_->StopLocalRenderer();
123 main_wnd_->StopRemoteRenderer();
124 peer_connection_factory_ = NULL;
125 peer_id_ = -1;
126 }
127
EnsureStreamingUI()128 void Conductor::EnsureStreamingUI() {
129 ASSERT(peer_connection_.get() != NULL);
130 if (main_wnd_->IsWindow()) {
131 if (main_wnd_->current_ui() != MainWindow::STREAMING)
132 main_wnd_->SwitchToStreamingUI();
133 }
134 }
135
136 //
137 // PeerConnectionObserver implementation.
138 //
139
OnError()140 void Conductor::OnError() {
141 LOG(LS_ERROR) << __FUNCTION__;
142 main_wnd_->QueueUIThreadCallback(PEER_CONNECTION_ERROR, NULL);
143 }
144
145 // Called when a remote stream is added
OnAddStream(webrtc::MediaStreamInterface * stream)146 void Conductor::OnAddStream(webrtc::MediaStreamInterface* stream) {
147 LOG(INFO) << __FUNCTION__ << " " << stream->label();
148
149 stream->AddRef();
150 main_wnd_->QueueUIThreadCallback(NEW_STREAM_ADDED,
151 stream);
152 }
153
OnRemoveStream(webrtc::MediaStreamInterface * stream)154 void Conductor::OnRemoveStream(webrtc::MediaStreamInterface* stream) {
155 LOG(INFO) << __FUNCTION__ << " " << stream->label();
156 stream->AddRef();
157 main_wnd_->QueueUIThreadCallback(STREAM_REMOVED,
158 stream);
159 }
160
OnIceCandidate(const webrtc::IceCandidateInterface * candidate)161 void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
162 LOG(INFO) << __FUNCTION__ << " " << candidate->sdp_mline_index();
163 Json::StyledWriter writer;
164 Json::Value jmessage;
165
166 jmessage[kCandidateSdpMidName] = candidate->sdp_mid();
167 jmessage[kCandidateSdpMlineIndexName] = candidate->sdp_mline_index();
168 std::string sdp;
169 if (!candidate->ToString(&sdp)) {
170 LOG(LS_ERROR) << "Failed to serialize candidate";
171 return;
172 }
173 jmessage[kCandidateSdpName] = sdp;
174 SendMessage(writer.write(jmessage));
175 }
176
177 //
178 // PeerConnectionClientObserver implementation.
179 //
180
OnSignedIn()181 void Conductor::OnSignedIn() {
182 LOG(INFO) << __FUNCTION__;
183 main_wnd_->SwitchToPeerList(client_->peers());
184 }
185
OnDisconnected()186 void Conductor::OnDisconnected() {
187 LOG(INFO) << __FUNCTION__;
188
189 DeletePeerConnection();
190
191 if (main_wnd_->IsWindow())
192 main_wnd_->SwitchToConnectUI();
193 }
194
OnPeerConnected(int id,const std::string & name)195 void Conductor::OnPeerConnected(int id, const std::string& name) {
196 LOG(INFO) << __FUNCTION__;
197 // Refresh the list if we're showing it.
198 if (main_wnd_->current_ui() == MainWindow::LIST_PEERS)
199 main_wnd_->SwitchToPeerList(client_->peers());
200 }
201
OnPeerDisconnected(int id)202 void Conductor::OnPeerDisconnected(int id) {
203 LOG(INFO) << __FUNCTION__;
204 if (id == peer_id_) {
205 LOG(INFO) << "Our peer disconnected";
206 main_wnd_->QueueUIThreadCallback(PEER_CONNECTION_CLOSED, NULL);
207 } else {
208 // Refresh the list if we're showing it.
209 if (main_wnd_->current_ui() == MainWindow::LIST_PEERS)
210 main_wnd_->SwitchToPeerList(client_->peers());
211 }
212 }
213
OnMessageFromPeer(int peer_id,const std::string & message)214 void Conductor::OnMessageFromPeer(int peer_id, const std::string& message) {
215 ASSERT(peer_id_ == peer_id || peer_id_ == -1);
216 ASSERT(!message.empty());
217
218 if (!peer_connection_.get()) {
219 ASSERT(peer_id_ == -1);
220 peer_id_ = peer_id;
221
222 if (!InitializePeerConnection()) {
223 LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
224 client_->SignOut();
225 return;
226 }
227 } else if (peer_id != peer_id_) {
228 ASSERT(peer_id_ != -1);
229 LOG(WARNING) << "Received a message from unknown peer while already in a "
230 "conversation with a different peer.";
231 return;
232 }
233
234 Json::Reader reader;
235 Json::Value jmessage;
236 if (!reader.parse(message, jmessage)) {
237 LOG(WARNING) << "Received unknown message. " << message;
238 return;
239 }
240 std::string type;
241 std::string json_object;
242
243 GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName, &type);
244 if (!type.empty()) {
245 std::string sdp;
246 if (!GetStringFromJsonObject(jmessage, kSessionDescriptionSdpName, &sdp)) {
247 LOG(WARNING) << "Can't parse received session description message.";
248 return;
249 }
250 webrtc::SessionDescriptionInterface* session_description(
251 webrtc::CreateSessionDescription(type, sdp));
252 if (!session_description) {
253 LOG(WARNING) << "Can't parse received session description message.";
254 return;
255 }
256 LOG(INFO) << " Received session description :" << message;
257 peer_connection_->SetRemoteDescription(
258 DummySetSessionDescriptionObserver::Create(), session_description);
259 if (session_description->type() ==
260 webrtc::SessionDescriptionInterface::kOffer) {
261 peer_connection_->CreateAnswer(this, NULL);
262 }
263 return;
264 } else {
265 std::string sdp_mid;
266 int sdp_mlineindex = 0;
267 std::string sdp;
268 if (!GetStringFromJsonObject(jmessage, kCandidateSdpMidName, &sdp_mid) ||
269 !GetIntFromJsonObject(jmessage, kCandidateSdpMlineIndexName,
270 &sdp_mlineindex) ||
271 !GetStringFromJsonObject(jmessage, kCandidateSdpName, &sdp)) {
272 LOG(WARNING) << "Can't parse received message.";
273 return;
274 }
275 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
276 webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, sdp));
277 if (!candidate.get()) {
278 LOG(WARNING) << "Can't parse received candidate message.";
279 return;
280 }
281 if (!peer_connection_->AddIceCandidate(candidate.get())) {
282 LOG(WARNING) << "Failed to apply the received candidate";
283 return;
284 }
285 LOG(INFO) << " Received candidate :" << message;
286 return;
287 }
288 }
289
OnMessageSent(int err)290 void Conductor::OnMessageSent(int err) {
291 // Process the next pending message if any.
292 main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, NULL);
293 }
294
OnServerConnectionFailure()295 void Conductor::OnServerConnectionFailure() {
296 main_wnd_->MessageBox("Error", ("Failed to connect to " + server_).c_str(),
297 true);
298 }
299
300 //
301 // MainWndCallback implementation.
302 //
303
StartLogin(const std::string & server,int port)304 void Conductor::StartLogin(const std::string& server, int port) {
305 if (client_->is_connected())
306 return;
307 server_ = server;
308 client_->Connect(server, port, GetPeerName());
309 }
310
DisconnectFromServer()311 void Conductor::DisconnectFromServer() {
312 if (client_->is_connected())
313 client_->SignOut();
314 }
315
ConnectToPeer(int peer_id)316 void Conductor::ConnectToPeer(int peer_id) {
317 ASSERT(peer_id_ == -1);
318 ASSERT(peer_id != -1);
319
320 if (peer_connection_.get()) {
321 main_wnd_->MessageBox("Error",
322 "We only support connecting to one peer at a time", true);
323 return;
324 }
325
326 if (InitializePeerConnection()) {
327 peer_id_ = peer_id;
328 peer_connection_->CreateOffer(this, NULL);
329 } else {
330 main_wnd_->MessageBox("Error", "Failed to initialize PeerConnection", true);
331 }
332 }
333
OpenVideoCaptureDevice()334 cricket::VideoCapturer* Conductor::OpenVideoCaptureDevice() {
335 rtc::scoped_ptr<cricket::DeviceManagerInterface> dev_manager(
336 cricket::DeviceManagerFactory::Create());
337 if (!dev_manager->Init()) {
338 LOG(LS_ERROR) << "Can't create device manager";
339 return NULL;
340 }
341 std::vector<cricket::Device> devs;
342 if (!dev_manager->GetVideoCaptureDevices(&devs)) {
343 LOG(LS_ERROR) << "Can't enumerate video devices";
344 return NULL;
345 }
346 std::vector<cricket::Device>::iterator dev_it = devs.begin();
347 cricket::VideoCapturer* capturer = NULL;
348 for (; dev_it != devs.end(); ++dev_it) {
349 capturer = dev_manager->CreateVideoCapturer(*dev_it);
350 if (capturer != NULL)
351 break;
352 }
353 return capturer;
354 }
355
AddStreams()356 void Conductor::AddStreams() {
357 if (active_streams_.find(kStreamLabel) != active_streams_.end())
358 return; // Already added.
359
360 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
361 peer_connection_factory_->CreateAudioTrack(
362 kAudioLabel, peer_connection_factory_->CreateAudioSource(NULL)));
363
364 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
365 peer_connection_factory_->CreateVideoTrack(
366 kVideoLabel,
367 peer_connection_factory_->CreateVideoSource(OpenVideoCaptureDevice(),
368 NULL)));
369 main_wnd_->StartLocalRenderer(video_track);
370
371 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
372 peer_connection_factory_->CreateLocalMediaStream(kStreamLabel);
373
374 stream->AddTrack(audio_track);
375 stream->AddTrack(video_track);
376 if (!peer_connection_->AddStream(stream, NULL)) {
377 LOG(LS_ERROR) << "Adding stream to PeerConnection failed";
378 }
379 typedef std::pair<std::string,
380 rtc::scoped_refptr<webrtc::MediaStreamInterface> >
381 MediaStreamPair;
382 active_streams_.insert(MediaStreamPair(stream->label(), stream));
383 main_wnd_->SwitchToStreamingUI();
384 }
385
DisconnectFromCurrentPeer()386 void Conductor::DisconnectFromCurrentPeer() {
387 LOG(INFO) << __FUNCTION__;
388 if (peer_connection_.get()) {
389 client_->SendHangUp(peer_id_);
390 DeletePeerConnection();
391 }
392
393 if (main_wnd_->IsWindow())
394 main_wnd_->SwitchToPeerList(client_->peers());
395 }
396
UIThreadCallback(int msg_id,void * data)397 void Conductor::UIThreadCallback(int msg_id, void* data) {
398 switch (msg_id) {
399 case PEER_CONNECTION_CLOSED:
400 LOG(INFO) << "PEER_CONNECTION_CLOSED";
401 DeletePeerConnection();
402
403 ASSERT(active_streams_.empty());
404
405 if (main_wnd_->IsWindow()) {
406 if (client_->is_connected()) {
407 main_wnd_->SwitchToPeerList(client_->peers());
408 } else {
409 main_wnd_->SwitchToConnectUI();
410 }
411 } else {
412 DisconnectFromServer();
413 }
414 break;
415
416 case SEND_MESSAGE_TO_PEER: {
417 LOG(INFO) << "SEND_MESSAGE_TO_PEER";
418 std::string* msg = reinterpret_cast<std::string*>(data);
419 if (msg) {
420 // For convenience, we always run the message through the queue.
421 // This way we can be sure that messages are sent to the server
422 // in the same order they were signaled without much hassle.
423 pending_messages_.push_back(msg);
424 }
425
426 if (!pending_messages_.empty() && !client_->IsSendingMessage()) {
427 msg = pending_messages_.front();
428 pending_messages_.pop_front();
429
430 if (!client_->SendToPeer(peer_id_, *msg) && peer_id_ != -1) {
431 LOG(LS_ERROR) << "SendToPeer failed";
432 DisconnectFromServer();
433 }
434 delete msg;
435 }
436
437 if (!peer_connection_.get())
438 peer_id_ = -1;
439
440 break;
441 }
442
443 case PEER_CONNECTION_ERROR:
444 main_wnd_->MessageBox("Error", "an unknown error occurred", true);
445 break;
446
447 case NEW_STREAM_ADDED: {
448 webrtc::MediaStreamInterface* stream =
449 reinterpret_cast<webrtc::MediaStreamInterface*>(
450 data);
451 webrtc::VideoTrackVector tracks = stream->GetVideoTracks();
452 // Only render the first track.
453 if (!tracks.empty()) {
454 webrtc::VideoTrackInterface* track = tracks[0];
455 main_wnd_->StartRemoteRenderer(track);
456 }
457 stream->Release();
458 break;
459 }
460
461 case STREAM_REMOVED: {
462 // Remote peer stopped sending a stream.
463 webrtc::MediaStreamInterface* stream =
464 reinterpret_cast<webrtc::MediaStreamInterface*>(
465 data);
466 stream->Release();
467 break;
468 }
469
470 default:
471 ASSERT(false);
472 break;
473 }
474 }
475
OnSuccess(webrtc::SessionDescriptionInterface * desc)476 void Conductor::OnSuccess(webrtc::SessionDescriptionInterface* desc) {
477 peer_connection_->SetLocalDescription(
478 DummySetSessionDescriptionObserver::Create(), desc);
479 Json::StyledWriter writer;
480 Json::Value jmessage;
481 jmessage[kSessionDescriptionTypeName] = desc->type();
482 std::string sdp;
483 desc->ToString(&sdp);
484 jmessage[kSessionDescriptionSdpName] = sdp;
485 SendMessage(writer.write(jmessage));
486 }
487
OnFailure(const std::string & error)488 void Conductor::OnFailure(const std::string& error) {
489 LOG(LERROR) << error;
490 }
491
SendMessage(const std::string & json_object)492 void Conductor::SendMessage(const std::string& json_object) {
493 std::string* msg = new std::string(json_object);
494 main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, msg);
495 }
496