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