You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

577 lines
18 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2012 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * 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.
  9. */
  10. #include "examples/peerconnection/client/conductor.h"
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <memory>
  14. #include <utility>
  15. #include <vector>
  16. #include "absl/memory/memory.h"
  17. #include "absl/types/optional.h"
  18. #include "api/audio/audio_mixer.h"
  19. #include "api/audio_codecs/audio_decoder_factory.h"
  20. #include "api/audio_codecs/audio_encoder_factory.h"
  21. #include "api/audio_codecs/builtin_audio_decoder_factory.h"
  22. #include "api/audio_codecs/builtin_audio_encoder_factory.h"
  23. #include "api/audio_options.h"
  24. #include "api/create_peerconnection_factory.h"
  25. #include "api/rtp_sender_interface.h"
  26. #include "api/video_codecs/builtin_video_decoder_factory.h"
  27. #include "api/video_codecs/builtin_video_encoder_factory.h"
  28. #include "api/video_codecs/video_decoder_factory.h"
  29. #include "api/video_codecs/video_encoder_factory.h"
  30. #include "examples/peerconnection/client/defaults.h"
  31. #include "modules/audio_device/include/audio_device.h"
  32. #include "modules/audio_processing/include/audio_processing.h"
  33. #include "modules/video_capture/video_capture.h"
  34. #include "modules/video_capture/video_capture_factory.h"
  35. #include "p2p/base/port_allocator.h"
  36. #include "pc/video_track_source.h"
  37. #include "rtc_base/checks.h"
  38. #include "rtc_base/logging.h"
  39. #include "rtc_base/ref_counted_object.h"
  40. #include "rtc_base/rtc_certificate_generator.h"
  41. #include "rtc_base/strings/json.h"
  42. #include "test/vcm_capturer.h"
  43. namespace {
  44. // Names used for a IceCandidate JSON object.
  45. const char kCandidateSdpMidName[] = "sdpMid";
  46. const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex";
  47. const char kCandidateSdpName[] = "candidate";
  48. // Names used for a SessionDescription JSON object.
  49. const char kSessionDescriptionTypeName[] = "type";
  50. const char kSessionDescriptionSdpName[] = "sdp";
  51. class DummySetSessionDescriptionObserver
  52. : public webrtc::SetSessionDescriptionObserver {
  53. public:
  54. static DummySetSessionDescriptionObserver* Create() {
  55. return new rtc::RefCountedObject<DummySetSessionDescriptionObserver>();
  56. }
  57. virtual void OnSuccess() { RTC_LOG(INFO) << __FUNCTION__; }
  58. virtual void OnFailure(webrtc::RTCError error) {
  59. RTC_LOG(INFO) << __FUNCTION__ << " " << ToString(error.type()) << ": "
  60. << error.message();
  61. }
  62. };
  63. class CapturerTrackSource : public webrtc::VideoTrackSource {
  64. public:
  65. static rtc::scoped_refptr<CapturerTrackSource> Create() {
  66. const size_t kWidth = 640;
  67. const size_t kHeight = 480;
  68. const size_t kFps = 30;
  69. std::unique_ptr<webrtc::test::VcmCapturer> capturer;
  70. std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(
  71. webrtc::VideoCaptureFactory::CreateDeviceInfo());
  72. if (!info) {
  73. return nullptr;
  74. }
  75. int num_devices = info->NumberOfDevices();
  76. for (int i = 0; i < num_devices; ++i) {
  77. capturer = absl::WrapUnique(
  78. webrtc::test::VcmCapturer::Create(kWidth, kHeight, kFps, i));
  79. if (capturer) {
  80. return new rtc::RefCountedObject<CapturerTrackSource>(
  81. std::move(capturer));
  82. }
  83. }
  84. return nullptr;
  85. }
  86. protected:
  87. explicit CapturerTrackSource(
  88. std::unique_ptr<webrtc::test::VcmCapturer> capturer)
  89. : VideoTrackSource(/*remote=*/false), capturer_(std::move(capturer)) {}
  90. private:
  91. rtc::VideoSourceInterface<webrtc::VideoFrame>* source() override {
  92. return capturer_.get();
  93. }
  94. std::unique_ptr<webrtc::test::VcmCapturer> capturer_;
  95. };
  96. } // namespace
  97. Conductor::Conductor(PeerConnectionClient* client, MainWindow* main_wnd)
  98. : peer_id_(-1), loopback_(false), client_(client), main_wnd_(main_wnd) {
  99. client_->RegisterObserver(this);
  100. main_wnd->RegisterObserver(this);
  101. }
  102. Conductor::~Conductor() {
  103. RTC_DCHECK(!peer_connection_);
  104. }
  105. bool Conductor::connection_active() const {
  106. return peer_connection_ != nullptr;
  107. }
  108. void Conductor::Close() {
  109. client_->SignOut();
  110. DeletePeerConnection();
  111. }
  112. bool Conductor::InitializePeerConnection() {
  113. RTC_DCHECK(!peer_connection_factory_);
  114. RTC_DCHECK(!peer_connection_);
  115. peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
  116. nullptr /* network_thread */, nullptr /* worker_thread */,
  117. nullptr /* signaling_thread */, nullptr /* default_adm */,
  118. webrtc::CreateBuiltinAudioEncoderFactory(),
  119. webrtc::CreateBuiltinAudioDecoderFactory(),
  120. webrtc::CreateBuiltinVideoEncoderFactory(),
  121. webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
  122. nullptr /* audio_processing */);
  123. if (!peer_connection_factory_) {
  124. main_wnd_->MessageBox("Error", "Failed to initialize PeerConnectionFactory",
  125. true);
  126. DeletePeerConnection();
  127. return false;
  128. }
  129. if (!CreatePeerConnection(/*dtls=*/true)) {
  130. main_wnd_->MessageBox("Error", "CreatePeerConnection failed", true);
  131. DeletePeerConnection();
  132. }
  133. AddTracks();
  134. return peer_connection_ != nullptr;
  135. }
  136. bool Conductor::ReinitializePeerConnectionForLoopback() {
  137. loopback_ = true;
  138. std::vector<rtc::scoped_refptr<webrtc::RtpSenderInterface>> senders =
  139. peer_connection_->GetSenders();
  140. peer_connection_ = nullptr;
  141. if (CreatePeerConnection(/*dtls=*/false)) {
  142. for (const auto& sender : senders) {
  143. peer_connection_->AddTrack(sender->track(), sender->stream_ids());
  144. }
  145. peer_connection_->CreateOffer(
  146. this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
  147. }
  148. return peer_connection_ != nullptr;
  149. }
  150. bool Conductor::CreatePeerConnection(bool dtls) {
  151. RTC_DCHECK(peer_connection_factory_);
  152. RTC_DCHECK(!peer_connection_);
  153. webrtc::PeerConnectionInterface::RTCConfiguration config;
  154. config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
  155. config.enable_dtls_srtp = dtls;
  156. webrtc::PeerConnectionInterface::IceServer server;
  157. server.uri = GetPeerConnectionString();
  158. config.servers.push_back(server);
  159. peer_connection_ = peer_connection_factory_->CreatePeerConnection(
  160. config, nullptr, nullptr, this);
  161. return peer_connection_ != nullptr;
  162. }
  163. void Conductor::DeletePeerConnection() {
  164. main_wnd_->StopLocalRenderer();
  165. main_wnd_->StopRemoteRenderer();
  166. peer_connection_ = nullptr;
  167. peer_connection_factory_ = nullptr;
  168. peer_id_ = -1;
  169. loopback_ = false;
  170. }
  171. void Conductor::EnsureStreamingUI() {
  172. RTC_DCHECK(peer_connection_);
  173. if (main_wnd_->IsWindow()) {
  174. if (main_wnd_->current_ui() != MainWindow::STREAMING)
  175. main_wnd_->SwitchToStreamingUI();
  176. }
  177. }
  178. //
  179. // PeerConnectionObserver implementation.
  180. //
  181. void Conductor::OnAddTrack(
  182. rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
  183. const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
  184. streams) {
  185. RTC_LOG(INFO) << __FUNCTION__ << " " << receiver->id();
  186. main_wnd_->QueueUIThreadCallback(NEW_TRACK_ADDED,
  187. receiver->track().release());
  188. }
  189. void Conductor::OnRemoveTrack(
  190. rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) {
  191. RTC_LOG(INFO) << __FUNCTION__ << " " << receiver->id();
  192. main_wnd_->QueueUIThreadCallback(TRACK_REMOVED, receiver->track().release());
  193. }
  194. void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
  195. RTC_LOG(INFO) << __FUNCTION__ << " " << candidate->sdp_mline_index();
  196. // For loopback test. To save some connecting delay.
  197. if (loopback_) {
  198. if (!peer_connection_->AddIceCandidate(candidate)) {
  199. RTC_LOG(WARNING) << "Failed to apply the received candidate";
  200. }
  201. return;
  202. }
  203. Json::StyledWriter writer;
  204. Json::Value jmessage;
  205. jmessage[kCandidateSdpMidName] = candidate->sdp_mid();
  206. jmessage[kCandidateSdpMlineIndexName] = candidate->sdp_mline_index();
  207. std::string sdp;
  208. if (!candidate->ToString(&sdp)) {
  209. RTC_LOG(LS_ERROR) << "Failed to serialize candidate";
  210. return;
  211. }
  212. jmessage[kCandidateSdpName] = sdp;
  213. SendMessage(writer.write(jmessage));
  214. }
  215. //
  216. // PeerConnectionClientObserver implementation.
  217. //
  218. void Conductor::OnSignedIn() {
  219. RTC_LOG(INFO) << __FUNCTION__;
  220. main_wnd_->SwitchToPeerList(client_->peers());
  221. }
  222. void Conductor::OnDisconnected() {
  223. RTC_LOG(INFO) << __FUNCTION__;
  224. DeletePeerConnection();
  225. if (main_wnd_->IsWindow())
  226. main_wnd_->SwitchToConnectUI();
  227. }
  228. void Conductor::OnPeerConnected(int id, const std::string& name) {
  229. RTC_LOG(INFO) << __FUNCTION__;
  230. // Refresh the list if we're showing it.
  231. if (main_wnd_->current_ui() == MainWindow::LIST_PEERS)
  232. main_wnd_->SwitchToPeerList(client_->peers());
  233. }
  234. void Conductor::OnPeerDisconnected(int id) {
  235. RTC_LOG(INFO) << __FUNCTION__;
  236. if (id == peer_id_) {
  237. RTC_LOG(INFO) << "Our peer disconnected";
  238. main_wnd_->QueueUIThreadCallback(PEER_CONNECTION_CLOSED, NULL);
  239. } else {
  240. // Refresh the list if we're showing it.
  241. if (main_wnd_->current_ui() == MainWindow::LIST_PEERS)
  242. main_wnd_->SwitchToPeerList(client_->peers());
  243. }
  244. }
  245. void Conductor::OnMessageFromPeer(int peer_id, const std::string& message) {
  246. RTC_DCHECK(peer_id_ == peer_id || peer_id_ == -1);
  247. RTC_DCHECK(!message.empty());
  248. if (!peer_connection_.get()) {
  249. RTC_DCHECK(peer_id_ == -1);
  250. peer_id_ = peer_id;
  251. if (!InitializePeerConnection()) {
  252. RTC_LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
  253. client_->SignOut();
  254. return;
  255. }
  256. } else if (peer_id != peer_id_) {
  257. RTC_DCHECK(peer_id_ != -1);
  258. RTC_LOG(WARNING)
  259. << "Received a message from unknown peer while already in a "
  260. "conversation with a different peer.";
  261. return;
  262. }
  263. Json::Reader reader;
  264. Json::Value jmessage;
  265. if (!reader.parse(message, jmessage)) {
  266. RTC_LOG(WARNING) << "Received unknown message. " << message;
  267. return;
  268. }
  269. std::string type_str;
  270. std::string json_object;
  271. rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName,
  272. &type_str);
  273. if (!type_str.empty()) {
  274. if (type_str == "offer-loopback") {
  275. // This is a loopback call.
  276. // Recreate the peerconnection with DTLS disabled.
  277. if (!ReinitializePeerConnectionForLoopback()) {
  278. RTC_LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
  279. DeletePeerConnection();
  280. client_->SignOut();
  281. }
  282. return;
  283. }
  284. absl::optional<webrtc::SdpType> type_maybe =
  285. webrtc::SdpTypeFromString(type_str);
  286. if (!type_maybe) {
  287. RTC_LOG(LS_ERROR) << "Unknown SDP type: " << type_str;
  288. return;
  289. }
  290. webrtc::SdpType type = *type_maybe;
  291. std::string sdp;
  292. if (!rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionSdpName,
  293. &sdp)) {
  294. RTC_LOG(WARNING) << "Can't parse received session description message.";
  295. return;
  296. }
  297. webrtc::SdpParseError error;
  298. std::unique_ptr<webrtc::SessionDescriptionInterface> session_description =
  299. webrtc::CreateSessionDescription(type, sdp, &error);
  300. if (!session_description) {
  301. RTC_LOG(WARNING) << "Can't parse received session description message. "
  302. << "SdpParseError was: " << error.description;
  303. return;
  304. }
  305. RTC_LOG(INFO) << " Received session description :" << message;
  306. peer_connection_->SetRemoteDescription(
  307. DummySetSessionDescriptionObserver::Create(),
  308. session_description.release());
  309. if (type == webrtc::SdpType::kOffer) {
  310. peer_connection_->CreateAnswer(
  311. this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
  312. }
  313. } else {
  314. std::string sdp_mid;
  315. int sdp_mlineindex = 0;
  316. std::string sdp;
  317. if (!rtc::GetStringFromJsonObject(jmessage, kCandidateSdpMidName,
  318. &sdp_mid) ||
  319. !rtc::GetIntFromJsonObject(jmessage, kCandidateSdpMlineIndexName,
  320. &sdp_mlineindex) ||
  321. !rtc::GetStringFromJsonObject(jmessage, kCandidateSdpName, &sdp)) {
  322. RTC_LOG(WARNING) << "Can't parse received message.";
  323. return;
  324. }
  325. webrtc::SdpParseError error;
  326. std::unique_ptr<webrtc::IceCandidateInterface> candidate(
  327. webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, sdp, &error));
  328. if (!candidate.get()) {
  329. RTC_LOG(WARNING) << "Can't parse received candidate message. "
  330. << "SdpParseError was: " << error.description;
  331. return;
  332. }
  333. if (!peer_connection_->AddIceCandidate(candidate.get())) {
  334. RTC_LOG(WARNING) << "Failed to apply the received candidate";
  335. return;
  336. }
  337. RTC_LOG(INFO) << " Received candidate :" << message;
  338. }
  339. }
  340. void Conductor::OnMessageSent(int err) {
  341. // Process the next pending message if any.
  342. main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, NULL);
  343. }
  344. void Conductor::OnServerConnectionFailure() {
  345. main_wnd_->MessageBox("Error", ("Failed to connect to " + server_).c_str(),
  346. true);
  347. }
  348. //
  349. // MainWndCallback implementation.
  350. //
  351. void Conductor::StartLogin(const std::string& server, int port) {
  352. if (client_->is_connected())
  353. return;
  354. server_ = server;
  355. client_->Connect(server, port, GetPeerName());
  356. }
  357. void Conductor::DisconnectFromServer() {
  358. if (client_->is_connected())
  359. client_->SignOut();
  360. }
  361. void Conductor::ConnectToPeer(int peer_id) {
  362. RTC_DCHECK(peer_id_ == -1);
  363. RTC_DCHECK(peer_id != -1);
  364. if (peer_connection_.get()) {
  365. main_wnd_->MessageBox(
  366. "Error", "We only support connecting to one peer at a time", true);
  367. return;
  368. }
  369. if (InitializePeerConnection()) {
  370. peer_id_ = peer_id;
  371. peer_connection_->CreateOffer(
  372. this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
  373. } else {
  374. main_wnd_->MessageBox("Error", "Failed to initialize PeerConnection", true);
  375. }
  376. }
  377. void Conductor::AddTracks() {
  378. if (!peer_connection_->GetSenders().empty()) {
  379. return; // Already added tracks.
  380. }
  381. rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
  382. peer_connection_factory_->CreateAudioTrack(
  383. kAudioLabel, peer_connection_factory_->CreateAudioSource(
  384. cricket::AudioOptions())));
  385. auto result_or_error = peer_connection_->AddTrack(audio_track, {kStreamId});
  386. if (!result_or_error.ok()) {
  387. RTC_LOG(LS_ERROR) << "Failed to add audio track to PeerConnection: "
  388. << result_or_error.error().message();
  389. }
  390. rtc::scoped_refptr<CapturerTrackSource> video_device =
  391. CapturerTrackSource::Create();
  392. if (video_device) {
  393. rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track_(
  394. peer_connection_factory_->CreateVideoTrack(kVideoLabel, video_device));
  395. main_wnd_->StartLocalRenderer(video_track_);
  396. result_or_error = peer_connection_->AddTrack(video_track_, {kStreamId});
  397. if (!result_or_error.ok()) {
  398. RTC_LOG(LS_ERROR) << "Failed to add video track to PeerConnection: "
  399. << result_or_error.error().message();
  400. }
  401. } else {
  402. RTC_LOG(LS_ERROR) << "OpenVideoCaptureDevice failed";
  403. }
  404. main_wnd_->SwitchToStreamingUI();
  405. }
  406. void Conductor::DisconnectFromCurrentPeer() {
  407. RTC_LOG(INFO) << __FUNCTION__;
  408. if (peer_connection_.get()) {
  409. client_->SendHangUp(peer_id_);
  410. DeletePeerConnection();
  411. }
  412. if (main_wnd_->IsWindow())
  413. main_wnd_->SwitchToPeerList(client_->peers());
  414. }
  415. void Conductor::UIThreadCallback(int msg_id, void* data) {
  416. switch (msg_id) {
  417. case PEER_CONNECTION_CLOSED:
  418. RTC_LOG(INFO) << "PEER_CONNECTION_CLOSED";
  419. DeletePeerConnection();
  420. if (main_wnd_->IsWindow()) {
  421. if (client_->is_connected()) {
  422. main_wnd_->SwitchToPeerList(client_->peers());
  423. } else {
  424. main_wnd_->SwitchToConnectUI();
  425. }
  426. } else {
  427. DisconnectFromServer();
  428. }
  429. break;
  430. case SEND_MESSAGE_TO_PEER: {
  431. RTC_LOG(INFO) << "SEND_MESSAGE_TO_PEER";
  432. std::string* msg = reinterpret_cast<std::string*>(data);
  433. if (msg) {
  434. // For convenience, we always run the message through the queue.
  435. // This way we can be sure that messages are sent to the server
  436. // in the same order they were signaled without much hassle.
  437. pending_messages_.push_back(msg);
  438. }
  439. if (!pending_messages_.empty() && !client_->IsSendingMessage()) {
  440. msg = pending_messages_.front();
  441. pending_messages_.pop_front();
  442. if (!client_->SendToPeer(peer_id_, *msg) && peer_id_ != -1) {
  443. RTC_LOG(LS_ERROR) << "SendToPeer failed";
  444. DisconnectFromServer();
  445. }
  446. delete msg;
  447. }
  448. if (!peer_connection_.get())
  449. peer_id_ = -1;
  450. break;
  451. }
  452. case NEW_TRACK_ADDED: {
  453. auto* track = reinterpret_cast<webrtc::MediaStreamTrackInterface*>(data);
  454. if (track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
  455. auto* video_track = static_cast<webrtc::VideoTrackInterface*>(track);
  456. main_wnd_->StartRemoteRenderer(video_track);
  457. }
  458. track->Release();
  459. break;
  460. }
  461. case TRACK_REMOVED: {
  462. // Remote peer stopped sending a track.
  463. auto* track = reinterpret_cast<webrtc::MediaStreamTrackInterface*>(data);
  464. track->Release();
  465. break;
  466. }
  467. default:
  468. RTC_NOTREACHED();
  469. break;
  470. }
  471. }
  472. void Conductor::OnSuccess(webrtc::SessionDescriptionInterface* desc) {
  473. peer_connection_->SetLocalDescription(
  474. DummySetSessionDescriptionObserver::Create(), desc);
  475. std::string sdp;
  476. desc->ToString(&sdp);
  477. // For loopback test. To save some connecting delay.
  478. if (loopback_) {
  479. // Replace message type from "offer" to "answer"
  480. std::unique_ptr<webrtc::SessionDescriptionInterface> session_description =
  481. webrtc::CreateSessionDescription(webrtc::SdpType::kAnswer, sdp);
  482. peer_connection_->SetRemoteDescription(
  483. DummySetSessionDescriptionObserver::Create(),
  484. session_description.release());
  485. return;
  486. }
  487. Json::StyledWriter writer;
  488. Json::Value jmessage;
  489. jmessage[kSessionDescriptionTypeName] =
  490. webrtc::SdpTypeToString(desc->GetType());
  491. jmessage[kSessionDescriptionSdpName] = sdp;
  492. SendMessage(writer.write(jmessage));
  493. }
  494. void Conductor::OnFailure(webrtc::RTCError error) {
  495. RTC_LOG(LERROR) << ToString(error.type()) << ": " << error.message();
  496. }
  497. void Conductor::SendMessage(const std::string& json_object) {
  498. std::string* msg = new std::string(json_object);
  499. main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, msg);
  500. }