blob: 423c35ff83251fdb5e215f3858ff30124d16e83b [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Donald E Curtisa8736442015-08-05 15:48:13 -070011#include "webrtc/examples/peerconnection/client/conductor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kwibergbfefb032016-05-01 14:53:46 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <utility>
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +000015#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
Henrik Kjellander15583c12016-02-10 10:53:12 +010017#include "webrtc/api/test/fakeconstraints.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/common.h"
19#include "webrtc/base/json.h"
20#include "webrtc/base/logging.h"
kjellandera96e2d72016-02-04 23:52:28 -080021#include "webrtc/examples/peerconnection/client/defaults.h"
solenberg8ad582d2016-03-16 09:34:56 -070022#include "webrtc/media/engine/webrtcvideocapturerfactory.h"
23#include "webrtc/modules/video_capture/video_capture_factory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
25// Names used for a IceCandidate JSON object.
26const char kCandidateSdpMidName[] = "sdpMid";
27const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex";
28const char kCandidateSdpName[] = "candidate";
29
30// Names used for a SessionDescription JSON object.
31const char kSessionDescriptionTypeName[] = "type";
32const char kSessionDescriptionSdpName[] = "sdp";
33
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +000034#define DTLS_ON true
35#define DTLS_OFF false
36
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037class DummySetSessionDescriptionObserver
38 : public webrtc::SetSessionDescriptionObserver {
39 public:
40 static DummySetSessionDescriptionObserver* Create() {
41 return
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000042 new rtc::RefCountedObject<DummySetSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043 }
44 virtual void OnSuccess() {
45 LOG(INFO) << __FUNCTION__;
46 }
47 virtual void OnFailure(const std::string& error) {
48 LOG(INFO) << __FUNCTION__ << " " << error;
49 }
50
51 protected:
52 DummySetSessionDescriptionObserver() {}
53 ~DummySetSessionDescriptionObserver() {}
54};
55
56Conductor::Conductor(PeerConnectionClient* client, MainWindow* main_wnd)
57 : peer_id_(-1),
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +000058 loopback_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 client_(client),
60 main_wnd_(main_wnd) {
61 client_->RegisterObserver(this);
62 main_wnd->RegisterObserver(this);
63}
64
65Conductor::~Conductor() {
66 ASSERT(peer_connection_.get() == NULL);
67}
68
69bool Conductor::connection_active() const {
70 return peer_connection_.get() != NULL;
71}
72
73void Conductor::Close() {
74 client_->SignOut();
75 DeletePeerConnection();
76}
77
78bool Conductor::InitializePeerConnection() {
79 ASSERT(peer_connection_factory_.get() == NULL);
80 ASSERT(peer_connection_.get() == NULL);
81
82 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory();
83
84 if (!peer_connection_factory_.get()) {
85 main_wnd_->MessageBox("Error",
86 "Failed to initialize PeerConnectionFactory", true);
87 DeletePeerConnection();
88 return false;
89 }
90
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +000091 if (!CreatePeerConnection(DTLS_ON)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 main_wnd_->MessageBox("Error",
93 "CreatePeerConnection failed", true);
94 DeletePeerConnection();
95 }
96 AddStreams();
97 return peer_connection_.get() != NULL;
98}
99
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000100bool Conductor::ReinitializePeerConnectionForLoopback() {
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000101 loopback_ = true;
102 rtc::scoped_refptr<webrtc::StreamCollectionInterface> streams(
103 peer_connection_->local_streams());
104 peer_connection_ = NULL;
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000105 if (CreatePeerConnection(DTLS_OFF)) {
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000106 for (size_t i = 0; i < streams->count(); ++i)
107 peer_connection_->AddStream(streams->at(i));
108 peer_connection_->CreateOffer(this, NULL);
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000109 }
110 return peer_connection_.get() != NULL;
111}
112
113bool Conductor::CreatePeerConnection(bool dtls) {
114 ASSERT(peer_connection_factory_.get() != NULL);
115 ASSERT(peer_connection_.get() == NULL);
116
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800117 webrtc::PeerConnectionInterface::RTCConfiguration config;
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000118 webrtc::PeerConnectionInterface::IceServer server;
119 server.uri = GetPeerConnectionString();
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800120 config.servers.push_back(server);
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000121
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000122 webrtc::FakeConstraints constraints;
123 if (dtls) {
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000124 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
125 "true");
jbauch70625e52015-12-09 14:18:14 -0800126 } else {
braveyao@webrtc.org9bfa5f02015-03-11 03:20:59 +0000127 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
128 "false");
129 }
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000130
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800131 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
Henrik Boström400781a2016-05-27 14:51:55 +0200132 config,
133 &constraints,
134 nullptr,
135 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>(),
136 this);
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000137 return peer_connection_.get() != NULL;
138}
139
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140void Conductor::DeletePeerConnection() {
141 peer_connection_ = NULL;
142 active_streams_.clear();
143 main_wnd_->StopLocalRenderer();
144 main_wnd_->StopRemoteRenderer();
145 peer_connection_factory_ = NULL;
146 peer_id_ = -1;
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000147 loopback_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148}
149
150void Conductor::EnsureStreamingUI() {
151 ASSERT(peer_connection_.get() != NULL);
152 if (main_wnd_->IsWindow()) {
153 if (main_wnd_->current_ui() != MainWindow::STREAMING)
154 main_wnd_->SwitchToStreamingUI();
155 }
156}
157
158//
159// PeerConnectionObserver implementation.
160//
161
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162// Called when a remote stream is added
163void Conductor::OnAddStream(webrtc::MediaStreamInterface* stream) {
164 LOG(INFO) << __FUNCTION__ << " " << stream->label();
165
166 stream->AddRef();
167 main_wnd_->QueueUIThreadCallback(NEW_STREAM_ADDED,
168 stream);
169}
170
171void Conductor::OnRemoveStream(webrtc::MediaStreamInterface* stream) {
172 LOG(INFO) << __FUNCTION__ << " " << stream->label();
173 stream->AddRef();
174 main_wnd_->QueueUIThreadCallback(STREAM_REMOVED,
175 stream);
176}
177
178void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
179 LOG(INFO) << __FUNCTION__ << " " << candidate->sdp_mline_index();
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000180 // For loopback test. To save some connecting delay.
181 if (loopback_) {
182 if (!peer_connection_->AddIceCandidate(candidate)) {
183 LOG(WARNING) << "Failed to apply the received candidate";
184 }
185 return;
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000186 }
187
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 Json::StyledWriter writer;
189 Json::Value jmessage;
190
191 jmessage[kCandidateSdpMidName] = candidate->sdp_mid();
192 jmessage[kCandidateSdpMlineIndexName] = candidate->sdp_mline_index();
193 std::string sdp;
194 if (!candidate->ToString(&sdp)) {
195 LOG(LS_ERROR) << "Failed to serialize candidate";
196 return;
197 }
198 jmessage[kCandidateSdpName] = sdp;
199 SendMessage(writer.write(jmessage));
200}
201
202//
203// PeerConnectionClientObserver implementation.
204//
205
206void Conductor::OnSignedIn() {
207 LOG(INFO) << __FUNCTION__;
208 main_wnd_->SwitchToPeerList(client_->peers());
209}
210
211void Conductor::OnDisconnected() {
212 LOG(INFO) << __FUNCTION__;
213
214 DeletePeerConnection();
215
216 if (main_wnd_->IsWindow())
217 main_wnd_->SwitchToConnectUI();
218}
219
220void Conductor::OnPeerConnected(int id, const std::string& name) {
221 LOG(INFO) << __FUNCTION__;
222 // Refresh the list if we're showing it.
223 if (main_wnd_->current_ui() == MainWindow::LIST_PEERS)
224 main_wnd_->SwitchToPeerList(client_->peers());
225}
226
227void Conductor::OnPeerDisconnected(int id) {
228 LOG(INFO) << __FUNCTION__;
229 if (id == peer_id_) {
230 LOG(INFO) << "Our peer disconnected";
231 main_wnd_->QueueUIThreadCallback(PEER_CONNECTION_CLOSED, NULL);
232 } else {
233 // Refresh the list if we're showing it.
234 if (main_wnd_->current_ui() == MainWindow::LIST_PEERS)
235 main_wnd_->SwitchToPeerList(client_->peers());
236 }
237}
238
239void Conductor::OnMessageFromPeer(int peer_id, const std::string& message) {
240 ASSERT(peer_id_ == peer_id || peer_id_ == -1);
241 ASSERT(!message.empty());
242
243 if (!peer_connection_.get()) {
244 ASSERT(peer_id_ == -1);
245 peer_id_ = peer_id;
246
247 if (!InitializePeerConnection()) {
248 LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
249 client_->SignOut();
250 return;
251 }
252 } else if (peer_id != peer_id_) {
253 ASSERT(peer_id_ != -1);
254 LOG(WARNING) << "Received a message from unknown peer while already in a "
255 "conversation with a different peer.";
256 return;
257 }
258
259 Json::Reader reader;
260 Json::Value jmessage;
261 if (!reader.parse(message, jmessage)) {
262 LOG(WARNING) << "Received unknown message. " << message;
263 return;
264 }
265 std::string type;
266 std::string json_object;
267
Thiago Farinacb76b892015-04-02 09:59:15 +0000268 rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName, &type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 if (!type.empty()) {
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000270 if (type == "offer-loopback") {
271 // This is a loopback call.
272 // Recreate the peerconnection with DTLS disabled.
273 if (!ReinitializePeerConnectionForLoopback()) {
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000274 LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
275 DeletePeerConnection();
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000276 client_->SignOut();
277 }
278 return;
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000279 }
280
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 std::string sdp;
Thiago Farinacb76b892015-04-02 09:59:15 +0000282 if (!rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionSdpName,
283 &sdp)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 LOG(WARNING) << "Can't parse received session description message.";
285 return;
286 }
jbauchfabe2c92015-07-16 13:43:14 -0700287 webrtc::SdpParseError error;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 webrtc::SessionDescriptionInterface* session_description(
jbauchfabe2c92015-07-16 13:43:14 -0700289 webrtc::CreateSessionDescription(type, sdp, &error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 if (!session_description) {
jbauchfabe2c92015-07-16 13:43:14 -0700291 LOG(WARNING) << "Can't parse received session description message. "
292 << "SdpParseError was: " << error.description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 return;
294 }
295 LOG(INFO) << " Received session description :" << message;
296 peer_connection_->SetRemoteDescription(
297 DummySetSessionDescriptionObserver::Create(), session_description);
298 if (session_description->type() ==
299 webrtc::SessionDescriptionInterface::kOffer) {
300 peer_connection_->CreateAnswer(this, NULL);
301 }
302 return;
303 } else {
304 std::string sdp_mid;
305 int sdp_mlineindex = 0;
306 std::string sdp;
Thiago Farinacb76b892015-04-02 09:59:15 +0000307 if (!rtc::GetStringFromJsonObject(jmessage, kCandidateSdpMidName,
308 &sdp_mid) ||
309 !rtc::GetIntFromJsonObject(jmessage, kCandidateSdpMlineIndexName,
310 &sdp_mlineindex) ||
311 !rtc::GetStringFromJsonObject(jmessage, kCandidateSdpName, &sdp)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 LOG(WARNING) << "Can't parse received message.";
313 return;
314 }
jbauchfabe2c92015-07-16 13:43:14 -0700315 webrtc::SdpParseError error;
kwibergbfefb032016-05-01 14:53:46 -0700316 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
jbauchfabe2c92015-07-16 13:43:14 -0700317 webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, sdp, &error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 if (!candidate.get()) {
jbauchfabe2c92015-07-16 13:43:14 -0700319 LOG(WARNING) << "Can't parse received candidate message. "
320 << "SdpParseError was: " << error.description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 return;
322 }
323 if (!peer_connection_->AddIceCandidate(candidate.get())) {
324 LOG(WARNING) << "Failed to apply the received candidate";
325 return;
326 }
327 LOG(INFO) << " Received candidate :" << message;
328 return;
329 }
330}
331
332void Conductor::OnMessageSent(int err) {
333 // Process the next pending message if any.
334 main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, NULL);
335}
336
337void Conductor::OnServerConnectionFailure() {
338 main_wnd_->MessageBox("Error", ("Failed to connect to " + server_).c_str(),
339 true);
340}
341
342//
343// MainWndCallback implementation.
344//
345
346void Conductor::StartLogin(const std::string& server, int port) {
347 if (client_->is_connected())
348 return;
349 server_ = server;
350 client_->Connect(server, port, GetPeerName());
351}
352
353void Conductor::DisconnectFromServer() {
354 if (client_->is_connected())
355 client_->SignOut();
356}
357
358void Conductor::ConnectToPeer(int peer_id) {
359 ASSERT(peer_id_ == -1);
360 ASSERT(peer_id != -1);
361
362 if (peer_connection_.get()) {
363 main_wnd_->MessageBox("Error",
364 "We only support connecting to one peer at a time", true);
365 return;
366 }
367
368 if (InitializePeerConnection()) {
369 peer_id_ = peer_id;
370 peer_connection_->CreateOffer(this, NULL);
371 } else {
372 main_wnd_->MessageBox("Error", "Failed to initialize PeerConnection", true);
373 }
374}
375
376cricket::VideoCapturer* Conductor::OpenVideoCaptureDevice() {
solenberg8ad582d2016-03-16 09:34:56 -0700377 std::vector<std::string> device_names;
378 {
379 std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(
380 webrtc::VideoCaptureFactory::CreateDeviceInfo(0));
381 if (!info) {
382 return nullptr;
383 }
384 int num_devices = info->NumberOfDevices();
385 for (int i = 0; i < num_devices; ++i) {
386 const uint32_t kSize = 256;
387 char name[kSize] = {0};
388 char id[kSize] = {0};
389 if (info->GetDeviceName(i, name, kSize, id, kSize) != -1) {
390 device_names.push_back(name);
391 }
392 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 }
solenberg8ad582d2016-03-16 09:34:56 -0700394
395 cricket::WebRtcVideoDeviceCapturerFactory factory;
396 cricket::VideoCapturer* capturer = nullptr;
397 for (const auto& name : device_names) {
398 capturer = factory.Create(cricket::Device(name, 0));
399 if (capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400 break;
solenberg8ad582d2016-03-16 09:34:56 -0700401 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 }
403 return capturer;
404}
405
406void Conductor::AddStreams() {
407 if (active_streams_.find(kStreamLabel) != active_streams_.end())
408 return; // Already added.
409
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000410 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 peer_connection_factory_->CreateAudioTrack(
412 kAudioLabel, peer_connection_factory_->CreateAudioSource(NULL)));
413
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000414 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 peer_connection_factory_->CreateVideoTrack(
416 kVideoLabel,
417 peer_connection_factory_->CreateVideoSource(OpenVideoCaptureDevice(),
418 NULL)));
419 main_wnd_->StartLocalRenderer(video_track);
420
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000421 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 peer_connection_factory_->CreateLocalMediaStream(kStreamLabel);
423
424 stream->AddTrack(audio_track);
425 stream->AddTrack(video_track);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000426 if (!peer_connection_->AddStream(stream)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 LOG(LS_ERROR) << "Adding stream to PeerConnection failed";
428 }
429 typedef std::pair<std::string,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430 rtc::scoped_refptr<webrtc::MediaStreamInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 MediaStreamPair;
432 active_streams_.insert(MediaStreamPair(stream->label(), stream));
433 main_wnd_->SwitchToStreamingUI();
434}
435
436void Conductor::DisconnectFromCurrentPeer() {
437 LOG(INFO) << __FUNCTION__;
438 if (peer_connection_.get()) {
439 client_->SendHangUp(peer_id_);
440 DeletePeerConnection();
441 }
442
443 if (main_wnd_->IsWindow())
444 main_wnd_->SwitchToPeerList(client_->peers());
445}
446
447void Conductor::UIThreadCallback(int msg_id, void* data) {
448 switch (msg_id) {
449 case PEER_CONNECTION_CLOSED:
450 LOG(INFO) << "PEER_CONNECTION_CLOSED";
451 DeletePeerConnection();
452
453 ASSERT(active_streams_.empty());
454
455 if (main_wnd_->IsWindow()) {
456 if (client_->is_connected()) {
457 main_wnd_->SwitchToPeerList(client_->peers());
458 } else {
459 main_wnd_->SwitchToConnectUI();
460 }
461 } else {
462 DisconnectFromServer();
463 }
464 break;
465
466 case SEND_MESSAGE_TO_PEER: {
467 LOG(INFO) << "SEND_MESSAGE_TO_PEER";
468 std::string* msg = reinterpret_cast<std::string*>(data);
469 if (msg) {
470 // For convenience, we always run the message through the queue.
471 // This way we can be sure that messages are sent to the server
472 // in the same order they were signaled without much hassle.
473 pending_messages_.push_back(msg);
474 }
475
476 if (!pending_messages_.empty() && !client_->IsSendingMessage()) {
477 msg = pending_messages_.front();
478 pending_messages_.pop_front();
479
480 if (!client_->SendToPeer(peer_id_, *msg) && peer_id_ != -1) {
481 LOG(LS_ERROR) << "SendToPeer failed";
482 DisconnectFromServer();
483 }
484 delete msg;
485 }
486
487 if (!peer_connection_.get())
488 peer_id_ = -1;
489
490 break;
491 }
492
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 case NEW_STREAM_ADDED: {
494 webrtc::MediaStreamInterface* stream =
495 reinterpret_cast<webrtc::MediaStreamInterface*>(
496 data);
497 webrtc::VideoTrackVector tracks = stream->GetVideoTracks();
498 // Only render the first track.
499 if (!tracks.empty()) {
500 webrtc::VideoTrackInterface* track = tracks[0];
501 main_wnd_->StartRemoteRenderer(track);
502 }
503 stream->Release();
504 break;
505 }
506
507 case STREAM_REMOVED: {
508 // Remote peer stopped sending a stream.
509 webrtc::MediaStreamInterface* stream =
510 reinterpret_cast<webrtc::MediaStreamInterface*>(
511 data);
512 stream->Release();
513 break;
514 }
515
516 default:
517 ASSERT(false);
518 break;
519 }
520}
521
522void Conductor::OnSuccess(webrtc::SessionDescriptionInterface* desc) {
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000523 peer_connection_->SetLocalDescription(
524 DummySetSessionDescriptionObserver::Create(), desc);
525
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000526 std::string sdp;
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000527 desc->ToString(&sdp);
528
529 // For loopback test. To save some connecting delay.
530 if (loopback_) {
531 // Replace message type from "offer" to "answer"
532 webrtc::SessionDescriptionInterface* session_description(
jbauchfabe2c92015-07-16 13:43:14 -0700533 webrtc::CreateSessionDescription("answer", sdp, nullptr));
braveyao@webrtc.orgc68e0c92015-02-27 09:51:25 +0000534 peer_connection_->SetRemoteDescription(
535 DummySetSessionDescriptionObserver::Create(), session_description);
536 return;
537 }
braveyao@webrtc.orga742cb12015-01-29 04:23:01 +0000538
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 Json::StyledWriter writer;
540 Json::Value jmessage;
541 jmessage[kSessionDescriptionTypeName] = desc->type();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 jmessage[kSessionDescriptionSdpName] = sdp;
543 SendMessage(writer.write(jmessage));
544}
545
546void Conductor::OnFailure(const std::string& error) {
547 LOG(LERROR) << error;
548}
549
550void Conductor::SendMessage(const std::string& json_object) {
551 std::string* msg = new std::string(json_object);
552 main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, msg);
553}