3 * Copyright 2012, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/examples/peerconnection/client/conductor.h"
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"
39 // Names used for a IceCandidate JSON object.
40 const char kCandidateSdpMidName[] = "sdpMid";
41 const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex";
42 const char kCandidateSdpName[] = "candidate";
44 // Names used for a SessionDescription JSON object.
45 const char kSessionDescriptionTypeName[] = "type";
46 const char kSessionDescriptionSdpName[] = "sdp";
48 class DummySetSessionDescriptionObserver
49 : public webrtc::SetSessionDescriptionObserver {
51 static DummySetSessionDescriptionObserver* Create() {
53 new talk_base::RefCountedObject<DummySetSessionDescriptionObserver>();
55 virtual void OnSuccess() {
56 LOG(INFO) << __FUNCTION__;
58 virtual void OnFailure(const std::string& error) {
59 LOG(INFO) << __FUNCTION__ << " " << error;
63 DummySetSessionDescriptionObserver() {}
64 ~DummySetSessionDescriptionObserver() {}
67 Conductor::Conductor(PeerConnectionClient* client, MainWindow* main_wnd)
71 client_->RegisterObserver(this);
72 main_wnd->RegisterObserver(this);
75 Conductor::~Conductor() {
76 ASSERT(peer_connection_.get() == NULL);
79 bool Conductor::connection_active() const {
80 return peer_connection_.get() != NULL;
83 void Conductor::Close() {
85 DeletePeerConnection();
88 bool Conductor::InitializePeerConnection() {
89 ASSERT(peer_connection_factory_.get() == NULL);
90 ASSERT(peer_connection_.get() == NULL);
92 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory();
94 if (!peer_connection_factory_.get()) {
95 main_wnd_->MessageBox("Error",
96 "Failed to initialize PeerConnectionFactory", true);
97 DeletePeerConnection();
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,
110 if (!peer_connection_.get()) {
111 main_wnd_->MessageBox("Error",
112 "CreatePeerConnection failed", true);
113 DeletePeerConnection();
116 return peer_connection_.get() != NULL;
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;
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();
137 // PeerConnectionObserver implementation.
140 void Conductor::OnError() {
141 LOG(LS_ERROR) << __FUNCTION__;
142 main_wnd_->QueueUIThreadCallback(PEER_CONNECTION_ERROR, NULL);
145 // Called when a remote stream is added
146 void Conductor::OnAddStream(webrtc::MediaStreamInterface* stream) {
147 LOG(INFO) << __FUNCTION__ << " " << stream->label();
150 main_wnd_->QueueUIThreadCallback(NEW_STREAM_ADDED,
154 void Conductor::OnRemoveStream(webrtc::MediaStreamInterface* stream) {
155 LOG(INFO) << __FUNCTION__ << " " << stream->label();
157 main_wnd_->QueueUIThreadCallback(STREAM_REMOVED,
161 void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
162 LOG(INFO) << __FUNCTION__ << " " << candidate->sdp_mline_index();
163 Json::StyledWriter writer;
164 Json::Value jmessage;
166 jmessage[kCandidateSdpMidName] = candidate->sdp_mid();
167 jmessage[kCandidateSdpMlineIndexName] = candidate->sdp_mline_index();
169 if (!candidate->ToString(&sdp)) {
170 LOG(LS_ERROR) << "Failed to serialize candidate";
173 jmessage[kCandidateSdpName] = sdp;
174 SendMessage(writer.write(jmessage));
178 // PeerConnectionClientObserver implementation.
181 void Conductor::OnSignedIn() {
182 LOG(INFO) << __FUNCTION__;
183 main_wnd_->SwitchToPeerList(client_->peers());
186 void Conductor::OnDisconnected() {
187 LOG(INFO) << __FUNCTION__;
189 DeletePeerConnection();
191 if (main_wnd_->IsWindow())
192 main_wnd_->SwitchToConnectUI();
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());
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);
208 // Refresh the list if we're showing it.
209 if (main_wnd_->current_ui() == MainWindow::LIST_PEERS)
210 main_wnd_->SwitchToPeerList(client_->peers());
214 void Conductor::OnMessageFromPeer(int peer_id, const std::string& message) {
215 ASSERT(peer_id_ == peer_id || peer_id_ == -1);
216 ASSERT(!message.empty());
218 if (!peer_connection_.get()) {
219 ASSERT(peer_id_ == -1);
222 if (!InitializePeerConnection()) {
223 LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
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.";
235 Json::Value jmessage;
236 if (!reader.parse(message, jmessage)) {
237 LOG(WARNING) << "Received unknown message. " << message;
241 std::string json_object;
243 GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName, &type);
246 if (!GetStringFromJsonObject(jmessage, kSessionDescriptionSdpName, &sdp)) {
247 LOG(WARNING) << "Can't parse received session description message.";
250 webrtc::SessionDescriptionInterface* session_description(
251 webrtc::CreateSessionDescription(type, sdp));
252 if (!session_description) {
253 LOG(WARNING) << "Can't parse received session description message.";
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);
266 int sdp_mlineindex = 0;
268 if (!GetStringFromJsonObject(jmessage, kCandidateSdpMidName, &sdp_mid) ||
269 !GetIntFromJsonObject(jmessage, kCandidateSdpMlineIndexName,
271 !GetStringFromJsonObject(jmessage, kCandidateSdpName, &sdp)) {
272 LOG(WARNING) << "Can't parse received message.";
275 talk_base::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.";
281 if (!peer_connection_->AddIceCandidate(candidate.get())) {
282 LOG(WARNING) << "Failed to apply the received candidate";
285 LOG(INFO) << " Received candidate :" << message;
290 void Conductor::OnMessageSent(int err) {
291 // Process the next pending message if any.
292 main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, NULL);
295 void Conductor::OnServerConnectionFailure() {
296 main_wnd_->MessageBox("Error", ("Failed to connect to " + server_).c_str(),
301 // MainWndCallback implementation.
304 void Conductor::StartLogin(const std::string& server, int port) {
305 if (client_->is_connected())
308 client_->Connect(server, port, GetPeerName());
311 void Conductor::DisconnectFromServer() {
312 if (client_->is_connected())
316 void Conductor::ConnectToPeer(int peer_id) {
317 ASSERT(peer_id_ == -1);
318 ASSERT(peer_id != -1);
320 if (peer_connection_.get()) {
321 main_wnd_->MessageBox("Error",
322 "We only support connecting to one peer at a time", true);
326 if (InitializePeerConnection()) {
328 peer_connection_->CreateOffer(this, NULL);
330 main_wnd_->MessageBox("Error", "Failed to initialize PeerConnection", true);
334 cricket::VideoCapturer* Conductor::OpenVideoCaptureDevice() {
335 talk_base::scoped_ptr<cricket::DeviceManagerInterface> dev_manager(
336 cricket::DeviceManagerFactory::Create());
337 if (!dev_manager->Init()) {
338 LOG(LS_ERROR) << "Can't create device manager";
341 std::vector<cricket::Device> devs;
342 if (!dev_manager->GetVideoCaptureDevices(&devs)) {
343 LOG(LS_ERROR) << "Can't enumerate video devices";
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)
356 void Conductor::AddStreams() {
357 if (active_streams_.find(kStreamLabel) != active_streams_.end())
358 return; // Already added.
360 talk_base::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
361 peer_connection_factory_->CreateAudioTrack(
362 kAudioLabel, peer_connection_factory_->CreateAudioSource(NULL)));
364 talk_base::scoped_refptr<webrtc::VideoTrackInterface> video_track(
365 peer_connection_factory_->CreateVideoTrack(
367 peer_connection_factory_->CreateVideoSource(OpenVideoCaptureDevice(),
369 main_wnd_->StartLocalRenderer(video_track);
371 talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream =
372 peer_connection_factory_->CreateLocalMediaStream(kStreamLabel);
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";
379 typedef std::pair<std::string,
380 talk_base::scoped_refptr<webrtc::MediaStreamInterface> >
382 active_streams_.insert(MediaStreamPair(stream->label(), stream));
383 main_wnd_->SwitchToStreamingUI();
386 void Conductor::DisconnectFromCurrentPeer() {
387 LOG(INFO) << __FUNCTION__;
388 if (peer_connection_.get()) {
389 client_->SendHangUp(peer_id_);
390 DeletePeerConnection();
393 if (main_wnd_->IsWindow())
394 main_wnd_->SwitchToPeerList(client_->peers());
397 void Conductor::UIThreadCallback(int msg_id, void* data) {
399 case PEER_CONNECTION_CLOSED:
400 LOG(INFO) << "PEER_CONNECTION_CLOSED";
401 DeletePeerConnection();
403 ASSERT(active_streams_.empty());
405 if (main_wnd_->IsWindow()) {
406 if (client_->is_connected()) {
407 main_wnd_->SwitchToPeerList(client_->peers());
409 main_wnd_->SwitchToConnectUI();
412 DisconnectFromServer();
416 case SEND_MESSAGE_TO_PEER: {
417 LOG(INFO) << "SEND_MESSAGE_TO_PEER";
418 std::string* msg = reinterpret_cast<std::string*>(data);
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);
426 if (!pending_messages_.empty() && !client_->IsSendingMessage()) {
427 msg = pending_messages_.front();
428 pending_messages_.pop_front();
430 if (!client_->SendToPeer(peer_id_, *msg) && peer_id_ != -1) {
431 LOG(LS_ERROR) << "SendToPeer failed";
432 DisconnectFromServer();
437 if (!peer_connection_.get())
443 case PEER_CONNECTION_ERROR:
444 main_wnd_->MessageBox("Error", "an unknown error occurred", true);
447 case NEW_STREAM_ADDED: {
448 webrtc::MediaStreamInterface* stream =
449 reinterpret_cast<webrtc::MediaStreamInterface*>(
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);
461 case STREAM_REMOVED: {
462 // Remote peer stopped sending a stream.
463 webrtc::MediaStreamInterface* stream =
464 reinterpret_cast<webrtc::MediaStreamInterface*>(
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();
483 desc->ToString(&sdp);
484 jmessage[kSessionDescriptionSdpName] = sdp;
485 SendMessage(writer.write(jmessage));
488 void Conductor::OnFailure(const std::string& error) {
489 LOG(LERROR) << error;
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);