Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / remoting / host / cast_extension_session.cc
blob9bceaa6e40b444fc9645b6246fc8ba911b85d0b1
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/host/cast_extension_session.h"
7 #include "base/bind.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/logging.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "net/url_request/url_request_context_getter.h"
13 #include "remoting/host/cast_video_capturer_adapter.h"
14 #include "remoting/host/chromium_port_allocator_factory.h"
15 #include "remoting/host/client_session.h"
16 #include "remoting/proto/control.pb.h"
17 #include "remoting/protocol/client_stub.h"
18 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
19 #include "third_party/libjingle/source/talk/app/webrtc/test/fakeconstraints.h"
20 #include "third_party/libjingle/source/talk/app/webrtc/videosourceinterface.h"
22 namespace remoting {
24 // Used as the type attribute of all Cast protocol::ExtensionMessages.
25 const char kExtensionMessageType[] = "cast_message";
27 // Top-level keys used in all extension messages between host and client.
28 // Must keep synced with webapp.
29 const char kTopLevelData[] = "chromoting_data";
30 const char kTopLevelSubject[] = "subject";
32 // Keys used to describe the subject of a cast extension message. WebRTC-related
33 // message subjects are prepended with "webrtc_".
34 // Must keep synced with webapp.
35 const char kSubjectReady[] = "ready";
36 const char kSubjectTest[] = "test";
37 const char kSubjectNewCandidate[] = "webrtc_candidate";
38 const char kSubjectOffer[] = "webrtc_offer";
39 const char kSubjectAnswer[] = "webrtc_answer";
41 // WebRTC headers used inside messages with subject = "webrtc_*".
42 const char kWebRtcCandidate[] = "candidate";
43 const char kWebRtcSessionDescType[] = "type";
44 const char kWebRtcSessionDescSDP[] = "sdp";
45 const char kWebRtcSDPMid[] = "sdpMid";
46 const char kWebRtcSDPMLineIndex[] = "sdpMLineIndex";
48 // Media labels used over the PeerConnection.
49 const char kVideoLabel[] = "cast_video_label";
50 const char kStreamLabel[] = "stream_label";
52 // Default STUN server used to construct
53 // webrtc::PeerConnectionInterface::RTCConfiguration for the PeerConnection.
54 const char kDefaultStunURI[] = "stun:stun.l.google.com:19302";
56 const char kWorkerThreadName[] = "CastExtensionSessionWorkerThread";
58 // Interval between each call to PollPeerConnectionStats().
59 const int kStatsLogIntervalSec = 10;
61 // Minimum frame rate for video streaming over the PeerConnection in frames per
62 // second, added as a media constraint when constructing the video source for
63 // the Peer Connection.
64 const int kMinFramesPerSecond = 5;
66 // A webrtc::SetSessionDescriptionObserver implementation used to receive the
67 // results of setting local and remote descriptions of the PeerConnection.
68 class CastSetSessionDescriptionObserver
69 : public webrtc::SetSessionDescriptionObserver {
70 public:
71 static CastSetSessionDescriptionObserver* Create() {
72 return new rtc::RefCountedObject<CastSetSessionDescriptionObserver>();
74 void OnSuccess() override {
75 VLOG(1) << "Setting session description succeeded.";
77 void OnFailure(const std::string& error) override {
78 LOG(ERROR) << "Setting session description failed: " << error;
81 protected:
82 CastSetSessionDescriptionObserver() {}
83 ~CastSetSessionDescriptionObserver() override {}
85 DISALLOW_COPY_AND_ASSIGN(CastSetSessionDescriptionObserver);
88 // A webrtc::CreateSessionDescriptionObserver implementation used to receive the
89 // results of creating descriptions for this end of the PeerConnection.
90 class CastCreateSessionDescriptionObserver
91 : public webrtc::CreateSessionDescriptionObserver {
92 public:
93 static CastCreateSessionDescriptionObserver* Create(
94 CastExtensionSession* session) {
95 return new rtc::RefCountedObject<CastCreateSessionDescriptionObserver>(
96 session);
98 void OnSuccess(webrtc::SessionDescriptionInterface* desc) override {
99 if (cast_extension_session_ == nullptr) {
100 LOG(ERROR)
101 << "No CastExtensionSession. Creating session description succeeded.";
102 return;
104 cast_extension_session_->OnCreateSessionDescription(desc);
106 void OnFailure(const std::string& error) override {
107 if (cast_extension_session_ == nullptr) {
108 LOG(ERROR)
109 << "No CastExtensionSession. Creating session description failed.";
110 return;
112 cast_extension_session_->OnCreateSessionDescriptionFailure(error);
114 void SetCastExtensionSession(CastExtensionSession* cast_extension_session) {
115 cast_extension_session_ = cast_extension_session;
118 protected:
119 explicit CastCreateSessionDescriptionObserver(CastExtensionSession* session)
120 : cast_extension_session_(session) {}
121 ~CastCreateSessionDescriptionObserver() override {}
123 private:
124 CastExtensionSession* cast_extension_session_;
126 DISALLOW_COPY_AND_ASSIGN(CastCreateSessionDescriptionObserver);
129 // A webrtc::StatsObserver implementation used to receive statistics about the
130 // current PeerConnection.
131 class CastStatsObserver : public webrtc::StatsObserver {
132 public:
133 static CastStatsObserver* Create() {
134 return new rtc::RefCountedObject<CastStatsObserver>();
137 void OnComplete(const webrtc::StatsReports& reports) override {
138 VLOG(1) << "Received " << reports.size() << " new StatsReports.";
140 int index = 0;
141 for (const auto* report : reports) {
142 VLOG(1) << "Report " << index++ << ":";
143 for (const auto& v : report->values()) {
144 VLOG(1) << "Stat: " << v.second->display_name() << "="
145 << v.second->ToString() << ".";
150 protected:
151 CastStatsObserver() {}
152 ~CastStatsObserver() override {}
154 DISALLOW_COPY_AND_ASSIGN(CastStatsObserver);
157 // TODO(aiguha): Fix PeerConnnection-related tear down crash caused by premature
158 // destruction of cricket::CaptureManager (which occurs on releasing
159 // |peer_conn_factory_|). See crbug.com/403840.
160 CastExtensionSession::~CastExtensionSession() {
161 DCHECK(caller_task_runner_->BelongsToCurrentThread());
163 // Explicitly clear |create_session_desc_observer_|'s pointer to |this|,
164 // since the CastExtensionSession is destructing. Otherwise,
165 // |create_session_desc_observer_| would be left with a dangling pointer.
166 create_session_desc_observer_->SetCastExtensionSession(nullptr);
168 CleanupPeerConnection();
171 // static
172 scoped_ptr<CastExtensionSession> CastExtensionSession::Create(
173 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
174 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
175 const protocol::NetworkSettings& network_settings,
176 ClientSessionControl* client_session_control,
177 protocol::ClientStub* client_stub) {
178 scoped_ptr<CastExtensionSession> cast_extension_session(
179 new CastExtensionSession(caller_task_runner,
180 url_request_context_getter,
181 network_settings,
182 client_session_control,
183 client_stub));
184 if (!cast_extension_session->WrapTasksAndSave() ||
185 !cast_extension_session->InitializePeerConnection()) {
186 return nullptr;
188 return cast_extension_session.Pass();
191 void CastExtensionSession::OnCreateSessionDescription(
192 webrtc::SessionDescriptionInterface* desc) {
193 if (!caller_task_runner_->BelongsToCurrentThread()) {
194 caller_task_runner_->PostTask(
195 FROM_HERE,
196 base::Bind(&CastExtensionSession::OnCreateSessionDescription,
197 base::Unretained(this),
198 desc));
199 return;
202 peer_connection_->SetLocalDescription(
203 CastSetSessionDescriptionObserver::Create(), desc);
205 scoped_ptr<base::DictionaryValue> json(new base::DictionaryValue());
206 json->SetString(kWebRtcSessionDescType, desc->type());
207 std::string subject =
208 (desc->type() == "offer") ? kSubjectOffer : kSubjectAnswer;
209 std::string desc_str;
210 desc->ToString(&desc_str);
211 json->SetString(kWebRtcSessionDescSDP, desc_str);
212 std::string json_str;
213 if (!base::JSONWriter::Write(json.get(), &json_str)) {
214 LOG(ERROR) << "Failed to serialize sdp message.";
215 return;
218 SendMessageToClient(subject.c_str(), json_str);
221 void CastExtensionSession::OnCreateSessionDescriptionFailure(
222 const std::string& error) {
223 VLOG(1) << "Creating Session Description failed: " << error;
226 // TODO(aiguha): Support the case(s) where we've grabbed the capturer already,
227 // but another extension reset the video pipeline. We should remove the
228 // stream from the peer connection here, and then attempt to re-setup the
229 // peer connection in the OnRenegotiationNeeded() callback.
230 // See crbug.com/403843.
231 void CastExtensionSession::OnCreateVideoCapturer(
232 scoped_ptr<webrtc::DesktopCapturer>* capturer) {
233 if (has_grabbed_capturer_) {
234 LOG(ERROR) << "The video pipeline was reset unexpectedly.";
235 has_grabbed_capturer_ = false;
236 peer_connection_->RemoveStream(stream_.release());
237 return;
240 if (received_offer_) {
241 has_grabbed_capturer_ = true;
242 if (SetupVideoStream(capturer->Pass())) {
243 peer_connection_->CreateAnswer(create_session_desc_observer_, nullptr);
244 } else {
245 has_grabbed_capturer_ = false;
246 // Ignore the received offer, since we failed to setup a video stream.
247 received_offer_ = false;
249 return;
253 bool CastExtensionSession::ModifiesVideoPipeline() const {
254 return true;
257 // Returns true if the |message| is a Cast ExtensionMessage, even if
258 // it was badly formed or a resulting action failed. This is done so that
259 // the host does not continue to attempt to pass |message| to other
260 // HostExtensionSessions.
261 bool CastExtensionSession::OnExtensionMessage(
262 ClientSessionControl* client_session_control,
263 protocol::ClientStub* client_stub,
264 const protocol::ExtensionMessage& message) {
265 if (message.type() != kExtensionMessageType) {
266 return false;
269 scoped_ptr<base::Value> value(base::JSONReader::Read(message.data()));
270 base::DictionaryValue* client_message;
271 if (!(value && value->GetAsDictionary(&client_message))) {
272 LOG(ERROR) << "Could not read cast extension message.";
273 return true;
276 std::string subject;
277 if (!client_message->GetString(kTopLevelSubject, &subject)) {
278 LOG(ERROR) << "Invalid Cast Extension Message (missing subject header).";
279 return true;
282 if (subject == kSubjectOffer && !received_offer_) {
283 // Reset the video pipeline so we can grab the screen capturer and setup
284 // a video stream.
285 if (ParseAndSetRemoteDescription(client_message)) {
286 received_offer_ = true;
287 LOG(INFO) << "About to ResetVideoPipeline.";
288 client_session_control_->ResetVideoPipeline();
291 } else if (subject == kSubjectAnswer) {
292 ParseAndSetRemoteDescription(client_message);
293 } else if (subject == kSubjectNewCandidate) {
294 ParseAndAddICECandidate(client_message);
295 } else {
296 VLOG(1) << "Unexpected CastExtension Message: " << message.data();
298 return true;
301 // Private methods ------------------------------------------------------------
303 CastExtensionSession::CastExtensionSession(
304 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
305 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
306 const protocol::NetworkSettings& network_settings,
307 ClientSessionControl* client_session_control,
308 protocol::ClientStub* client_stub)
309 : caller_task_runner_(caller_task_runner),
310 url_request_context_getter_(url_request_context_getter),
311 network_settings_(network_settings),
312 client_session_control_(client_session_control),
313 client_stub_(client_stub),
314 stats_observer_(CastStatsObserver::Create()),
315 received_offer_(false),
316 has_grabbed_capturer_(false),
317 signaling_thread_wrapper_(nullptr),
318 worker_thread_wrapper_(nullptr),
319 worker_thread_(kWorkerThreadName) {
320 DCHECK(caller_task_runner_->BelongsToCurrentThread());
321 DCHECK(url_request_context_getter_.get());
322 DCHECK(client_session_control_);
323 DCHECK(client_stub_);
325 // The worker thread is created with base::MessageLoop::TYPE_IO because
326 // the PeerConnection performs some port allocation operations on this thread
327 // that require it. See crbug.com/404013.
328 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0);
329 worker_thread_.StartWithOptions(options);
330 worker_task_runner_ = worker_thread_.task_runner();
333 bool CastExtensionSession::ParseAndSetRemoteDescription(
334 base::DictionaryValue* message) {
335 DCHECK(peer_connection_.get() != nullptr);
337 base::DictionaryValue* message_data;
338 if (!message->GetDictionary(kTopLevelData, &message_data)) {
339 LOG(ERROR) << "Invalid Cast Extension Message (missing data).";
340 return false;
343 std::string webrtc_type;
344 if (!message_data->GetString(kWebRtcSessionDescType, &webrtc_type)) {
345 LOG(ERROR)
346 << "Invalid Cast Extension Message (missing webrtc type header).";
347 return false;
350 std::string sdp;
351 if (!message_data->GetString(kWebRtcSessionDescSDP, &sdp)) {
352 LOG(ERROR) << "Invalid Cast Extension Message (missing webrtc sdp header).";
353 return false;
356 webrtc::SdpParseError error;
357 webrtc::SessionDescriptionInterface* session_description(
358 webrtc::CreateSessionDescription(webrtc_type, sdp, &error));
360 if (!session_description) {
361 LOG(ERROR) << "Invalid Cast Extension Message (could not parse sdp).";
362 VLOG(1) << "SdpParseError was: " << error.description;
363 return false;
366 peer_connection_->SetRemoteDescription(
367 CastSetSessionDescriptionObserver::Create(), session_description);
368 return true;
371 bool CastExtensionSession::ParseAndAddICECandidate(
372 base::DictionaryValue* message) {
373 DCHECK(peer_connection_.get() != nullptr);
375 base::DictionaryValue* message_data;
376 if (!message->GetDictionary(kTopLevelData, &message_data)) {
377 LOG(ERROR) << "Invalid Cast Extension Message (missing data).";
378 return false;
381 std::string candidate_str;
382 std::string sdp_mid;
383 int sdp_mlineindex = 0;
384 if (!message_data->GetString(kWebRtcSDPMid, &sdp_mid) ||
385 !message_data->GetInteger(kWebRtcSDPMLineIndex, &sdp_mlineindex) ||
386 !message_data->GetString(kWebRtcCandidate, &candidate_str)) {
387 LOG(ERROR) << "Invalid Cast Extension Message (could not parse).";
388 return false;
391 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
392 webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, candidate_str));
393 if (!candidate.get()) {
394 LOG(ERROR)
395 << "Invalid Cast Extension Message (could not create candidate).";
396 return false;
399 if (!peer_connection_->AddIceCandidate(candidate.get())) {
400 LOG(ERROR) << "Failed to apply received ICE Candidate to PeerConnection.";
401 return false;
404 VLOG(1) << "Received and Added ICE Candidate: " << candidate_str;
406 return true;
409 bool CastExtensionSession::SendMessageToClient(const std::string& subject,
410 const std::string& data) {
411 DCHECK(caller_task_runner_->BelongsToCurrentThread());
413 if (client_stub_ == nullptr) {
414 LOG(ERROR) << "No Client Stub. Cannot send message to client.";
415 return false;
418 base::DictionaryValue message_dict;
419 message_dict.SetString(kTopLevelSubject, subject);
420 message_dict.SetString(kTopLevelData, data);
421 std::string message_json;
423 if (!base::JSONWriter::Write(&message_dict, &message_json)) {
424 LOG(ERROR) << "Failed to serialize JSON message.";
425 return false;
428 protocol::ExtensionMessage message;
429 message.set_type(kExtensionMessageType);
430 message.set_data(message_json);
431 client_stub_->DeliverHostMessage(message);
432 return true;
435 void CastExtensionSession::EnsureTaskAndSetSend(rtc::Thread** ptr,
436 base::WaitableEvent* event) {
437 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
438 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
439 *ptr = jingle_glue::JingleThreadWrapper::current();
441 if (event != nullptr) {
442 event->Signal();
446 bool CastExtensionSession::WrapTasksAndSave() {
447 DCHECK(caller_task_runner_->BelongsToCurrentThread());
449 EnsureTaskAndSetSend(&signaling_thread_wrapper_);
450 if (signaling_thread_wrapper_ == nullptr)
451 return false;
453 base::WaitableEvent wrap_worker_thread_event(true, false);
454 worker_task_runner_->PostTask(
455 FROM_HERE,
456 base::Bind(&CastExtensionSession::EnsureTaskAndSetSend,
457 base::Unretained(this),
458 &worker_thread_wrapper_,
459 &wrap_worker_thread_event));
460 wrap_worker_thread_event.Wait();
462 return (worker_thread_wrapper_ != nullptr);
465 bool CastExtensionSession::InitializePeerConnection() {
466 DCHECK(caller_task_runner_->BelongsToCurrentThread());
467 DCHECK(!peer_conn_factory_);
468 DCHECK(!peer_connection_);
469 DCHECK(worker_thread_wrapper_ != nullptr);
470 DCHECK(signaling_thread_wrapper_ != nullptr);
472 peer_conn_factory_ = webrtc::CreatePeerConnectionFactory(
473 worker_thread_wrapper_, signaling_thread_wrapper_, nullptr, nullptr,
474 nullptr);
476 if (!peer_conn_factory_.get()) {
477 CleanupPeerConnection();
478 return false;
481 VLOG(1) << "Created PeerConnectionFactory successfully.";
483 webrtc::PeerConnectionInterface::IceServers servers;
484 webrtc::PeerConnectionInterface::IceServer server;
485 server.uri = kDefaultStunURI;
486 servers.push_back(server);
487 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
488 rtc_config.servers = servers;
490 // DTLS-SRTP is the preferred encryption method. If set to kValueFalse, the
491 // peer connection uses SDES. Disabling SDES as well will cause the peer
492 // connection to fail to connect.
493 // Note: For protection and unprotection of SRTP packets, the libjingle
494 // ENABLE_EXTERNAL_AUTH flag must not be set.
495 webrtc::FakeConstraints constraints;
496 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
497 webrtc::MediaConstraintsInterface::kValueTrue);
499 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
500 port_allocator_factory = ChromiumPortAllocatorFactory::Create(
501 network_settings_, url_request_context_getter_);
503 peer_connection_ = peer_conn_factory_->CreatePeerConnection(
504 rtc_config, &constraints, port_allocator_factory, nullptr, this);
506 if (!peer_connection_.get()) {
507 CleanupPeerConnection();
508 return false;
511 VLOG(1) << "Created PeerConnection successfully.";
513 create_session_desc_observer_ =
514 CastCreateSessionDescriptionObserver::Create(this);
516 // Send a test message to the client. Then, notify the client to start
517 // webrtc offer/answer negotiation.
518 if (!SendMessageToClient(kSubjectTest, "Hello, client.") ||
519 !SendMessageToClient(kSubjectReady, "Host ready to receive offers.")) {
520 LOG(ERROR) << "Failed to send messages to client.";
521 return false;
524 return true;
527 bool CastExtensionSession::SetupVideoStream(
528 scoped_ptr<webrtc::DesktopCapturer> desktop_capturer) {
529 DCHECK(caller_task_runner_->BelongsToCurrentThread());
530 DCHECK(desktop_capturer);
532 if (stream_) {
533 VLOG(1) << "Already added MediaStream. Aborting Setup.";
534 return false;
537 scoped_ptr<CastVideoCapturerAdapter> cast_video_capturer_adapter(
538 new CastVideoCapturerAdapter(desktop_capturer.Pass()));
540 // Set video stream constraints.
541 webrtc::FakeConstraints video_constraints;
542 video_constraints.AddMandatory(
543 webrtc::MediaConstraintsInterface::kMinFrameRate, kMinFramesPerSecond);
545 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track =
546 peer_conn_factory_->CreateVideoTrack(
547 kVideoLabel,
548 peer_conn_factory_->CreateVideoSource(
549 cast_video_capturer_adapter.release(), &video_constraints));
551 stream_ = peer_conn_factory_->CreateLocalMediaStream(kStreamLabel);
553 if (!stream_->AddTrack(video_track) ||
554 !peer_connection_->AddStream(stream_)) {
555 return false;
558 VLOG(1) << "Setup video stream successfully.";
560 return true;
563 void CastExtensionSession::PollPeerConnectionStats() {
564 if (!connection_active()) {
565 VLOG(1) << "Cannot poll stats while PeerConnection is inactive.";
567 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> video_track =
568 stream_->FindVideoTrack(kVideoLabel);
569 peer_connection_->GetStats(
570 stats_observer_,
571 video_track.release(),
572 webrtc::PeerConnectionInterface::kStatsOutputLevelStandard);
575 void CastExtensionSession::CleanupPeerConnection() {
576 peer_connection_->Close();
577 peer_connection_ = nullptr;
578 stream_ = nullptr;
579 peer_conn_factory_ = nullptr;
580 worker_thread_.Stop();
583 bool CastExtensionSession::connection_active() const {
584 return peer_connection_.get() != nullptr;
587 // webrtc::PeerConnectionObserver implementation -------------------------------
588 void CastExtensionSession::OnSignalingChange(
589 webrtc::PeerConnectionInterface::SignalingState new_state) {
590 VLOG(1) << "PeerConnectionObserver: SignalingState changed to:" << new_state;
593 void CastExtensionSession::OnStateChange(
594 webrtc::PeerConnectionObserver::StateType state_changed) {
595 VLOG(1) << "PeerConnectionObserver: StateType changed to: " << state_changed;
598 void CastExtensionSession::OnAddStream(webrtc::MediaStreamInterface* stream) {
599 VLOG(1) << "PeerConnectionObserver: stream added: " << stream->label();
602 void CastExtensionSession::OnRemoveStream(
603 webrtc::MediaStreamInterface* stream) {
604 VLOG(1) << "PeerConnectionObserver: stream removed: " << stream->label();
607 void CastExtensionSession::OnDataChannel(
608 webrtc::DataChannelInterface* data_channel) {
609 VLOG(1) << "PeerConnectionObserver: data channel: " << data_channel->label();
612 void CastExtensionSession::OnRenegotiationNeeded() {
613 VLOG(1) << "PeerConnectionObserver: renegotiation needed.";
616 void CastExtensionSession::OnIceConnectionChange(
617 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
618 VLOG(1) << "PeerConnectionObserver: IceConnectionState changed to: "
619 << new_state;
621 // TODO(aiguha): Maybe start timer only if enabled by command-line flag or
622 // at a particular verbosity level.
623 if (!stats_polling_timer_.IsRunning() &&
624 new_state == webrtc::PeerConnectionInterface::kIceConnectionConnected) {
625 stats_polling_timer_.Start(
626 FROM_HERE,
627 base::TimeDelta::FromSeconds(kStatsLogIntervalSec),
628 this,
629 &CastExtensionSession::PollPeerConnectionStats);
633 void CastExtensionSession::OnIceGatheringChange(
634 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
635 VLOG(1) << "PeerConnectionObserver: IceGatheringState changed to: "
636 << new_state;
639 void CastExtensionSession::OnIceComplete() {
640 VLOG(1) << "PeerConnectionObserver: all ICE candidates found.";
643 void CastExtensionSession::OnIceCandidate(
644 const webrtc::IceCandidateInterface* candidate) {
645 std::string candidate_str;
646 if (!candidate->ToString(&candidate_str)) {
647 LOG(ERROR) << "PeerConnectionObserver: failed to serialize candidate.";
648 return;
650 scoped_ptr<base::DictionaryValue> json(new base::DictionaryValue());
651 json->SetString(kWebRtcSDPMid, candidate->sdp_mid());
652 json->SetInteger(kWebRtcSDPMLineIndex, candidate->sdp_mline_index());
653 json->SetString(kWebRtcCandidate, candidate_str);
654 std::string json_str;
655 if (!base::JSONWriter::Write(json.get(), &json_str)) {
656 LOG(ERROR) << "Failed to serialize candidate message.";
657 return;
659 SendMessageToClient(kSubjectNewCandidate, json_str);
662 } // namespace remoting