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/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"
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 rtc::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 // Called when a remote stream is added
141 void Conductor::OnAddStream(webrtc::MediaStreamInterface* stream) {
142 LOG(INFO) << __FUNCTION__ << " " << stream->label();
145 main_wnd_->QueueUIThreadCallback(NEW_STREAM_ADDED,
149 void Conductor::OnRemoveStream(webrtc::MediaStreamInterface* stream) {
150 LOG(INFO) << __FUNCTION__ << " " << stream->label();
152 main_wnd_->QueueUIThreadCallback(STREAM_REMOVED,
156 void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
157 LOG(INFO) << __FUNCTION__ << " " << candidate->sdp_mline_index();
158 Json::StyledWriter writer;
159 Json::Value jmessage;
161 jmessage[kCandidateSdpMidName] = candidate->sdp_mid();
162 jmessage[kCandidateSdpMlineIndexName] = candidate->sdp_mline_index();
164 if (!candidate->ToString(&sdp)) {
165 LOG(LS_ERROR) << "Failed to serialize candidate";
168 jmessage[kCandidateSdpName] = sdp;
169 SendMessage(writer.write(jmessage));
173 // PeerConnectionClientObserver implementation.
176 void Conductor::OnSignedIn() {
177 LOG(INFO) << __FUNCTION__;
178 main_wnd_->SwitchToPeerList(client_->peers());
181 void Conductor::OnDisconnected() {
182 LOG(INFO) << __FUNCTION__;
184 DeletePeerConnection();
186 if (main_wnd_->IsWindow())
187 main_wnd_->SwitchToConnectUI();
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());
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);
203 // Refresh the list if we're showing it.
204 if (main_wnd_->current_ui() == MainWindow::LIST_PEERS)
205 main_wnd_->SwitchToPeerList(client_->peers());
209 void Conductor::OnMessageFromPeer(int peer_id, const std::string& message) {
210 ASSERT(peer_id_ == peer_id || peer_id_ == -1);
211 ASSERT(!message.empty());
213 if (!peer_connection_.get()) {
214 ASSERT(peer_id_ == -1);
217 if (!InitializePeerConnection()) {
218 LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
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.";
230 Json::Value jmessage;
231 if (!reader.parse(message, jmessage)) {
232 LOG(WARNING) << "Received unknown message. " << message;
236 std::string json_object;
238 GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName, &type);
241 if (!GetStringFromJsonObject(jmessage, kSessionDescriptionSdpName, &sdp)) {
242 LOG(WARNING) << "Can't parse received session description message.";
245 webrtc::SessionDescriptionInterface* session_description(
246 webrtc::CreateSessionDescription(type, sdp));
247 if (!session_description) {
248 LOG(WARNING) << "Can't parse received session description message.";
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);
261 int sdp_mlineindex = 0;
263 if (!GetStringFromJsonObject(jmessage, kCandidateSdpMidName, &sdp_mid) ||
264 !GetIntFromJsonObject(jmessage, kCandidateSdpMlineIndexName,
266 !GetStringFromJsonObject(jmessage, kCandidateSdpName, &sdp)) {
267 LOG(WARNING) << "Can't parse received message.";
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.";
276 if (!peer_connection_->AddIceCandidate(candidate.get())) {
277 LOG(WARNING) << "Failed to apply the received candidate";
280 LOG(INFO) << " Received candidate :" << message;
285 void Conductor::OnMessageSent(int err) {
286 // Process the next pending message if any.
287 main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, NULL);
290 void Conductor::OnServerConnectionFailure() {
291 main_wnd_->MessageBox("Error", ("Failed to connect to " + server_).c_str(),
296 // MainWndCallback implementation.
299 void Conductor::StartLogin(const std::string& server, int port) {
300 if (client_->is_connected())
303 client_->Connect(server, port, GetPeerName());
306 void Conductor::DisconnectFromServer() {
307 if (client_->is_connected())
311 void Conductor::ConnectToPeer(int peer_id) {
312 ASSERT(peer_id_ == -1);
313 ASSERT(peer_id != -1);
315 if (peer_connection_.get()) {
316 main_wnd_->MessageBox("Error",
317 "We only support connecting to one peer at a time", true);
321 if (InitializePeerConnection()) {
323 peer_connection_->CreateOffer(this, NULL);
325 main_wnd_->MessageBox("Error", "Failed to initialize PeerConnection", true);
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";
336 std::vector<cricket::Device> devs;
337 if (!dev_manager->GetVideoCaptureDevices(&devs)) {
338 LOG(LS_ERROR) << "Can't enumerate video devices";
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)
351 void Conductor::AddStreams() {
352 if (active_streams_.find(kStreamLabel) != active_streams_.end())
353 return; // Already added.
355 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
356 peer_connection_factory_->CreateAudioTrack(
357 kAudioLabel, peer_connection_factory_->CreateAudioSource(NULL)));
359 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
360 peer_connection_factory_->CreateVideoTrack(
362 peer_connection_factory_->CreateVideoSource(OpenVideoCaptureDevice(),
364 main_wnd_->StartLocalRenderer(video_track);
366 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
367 peer_connection_factory_->CreateLocalMediaStream(kStreamLabel);
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";
374 typedef std::pair<std::string,
375 rtc::scoped_refptr<webrtc::MediaStreamInterface> >
377 active_streams_.insert(MediaStreamPair(stream->label(), stream));
378 main_wnd_->SwitchToStreamingUI();
381 void Conductor::DisconnectFromCurrentPeer() {
382 LOG(INFO) << __FUNCTION__;
383 if (peer_connection_.get()) {
384 client_->SendHangUp(peer_id_);
385 DeletePeerConnection();
388 if (main_wnd_->IsWindow())
389 main_wnd_->SwitchToPeerList(client_->peers());
392 void Conductor::UIThreadCallback(int msg_id, void* data) {
394 case PEER_CONNECTION_CLOSED:
395 LOG(INFO) << "PEER_CONNECTION_CLOSED";
396 DeletePeerConnection();
398 ASSERT(active_streams_.empty());
400 if (main_wnd_->IsWindow()) {
401 if (client_->is_connected()) {
402 main_wnd_->SwitchToPeerList(client_->peers());
404 main_wnd_->SwitchToConnectUI();
407 DisconnectFromServer();
411 case SEND_MESSAGE_TO_PEER: {
412 LOG(INFO) << "SEND_MESSAGE_TO_PEER";
413 std::string* msg = reinterpret_cast<std::string*>(data);
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);
421 if (!pending_messages_.empty() && !client_->IsSendingMessage()) {
422 msg = pending_messages_.front();
423 pending_messages_.pop_front();
425 if (!client_->SendToPeer(peer_id_, *msg) && peer_id_ != -1) {
426 LOG(LS_ERROR) << "SendToPeer failed";
427 DisconnectFromServer();
432 if (!peer_connection_.get())
438 case NEW_STREAM_ADDED: {
439 webrtc::MediaStreamInterface* stream =
440 reinterpret_cast<webrtc::MediaStreamInterface*>(
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);
452 case STREAM_REMOVED: {
453 // Remote peer stopped sending a stream.
454 webrtc::MediaStreamInterface* stream =
455 reinterpret_cast<webrtc::MediaStreamInterface*>(
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();
474 desc->ToString(&sdp);
475 jmessage[kSessionDescriptionSdpName] = sdp;
476 SendMessage(writer.write(jmessage));
479 void Conductor::OnFailure(const std::string& error) {
480 LOG(LERROR) << error;
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);