OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/webrtc_transport.h" | 5 #include "remoting/protocol/webrtc_transport.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 private: | 95 private: |
96 ResultCallback result_callback_; | 96 ResultCallback result_callback_; |
97 | 97 |
98 DISALLOW_COPY_AND_ASSIGN(SetSessionDescriptionObserver); | 98 DISALLOW_COPY_AND_ASSIGN(SetSessionDescriptionObserver); |
99 }; | 99 }; |
100 | 100 |
101 } // namespace | 101 } // namespace |
102 | 102 |
103 WebrtcTransport::WebrtcTransport( | 103 WebrtcTransport::WebrtcTransport( |
104 rtc::Thread* worker_thread, | 104 rtc::Thread* worker_thread, |
105 scoped_refptr<TransportContext> transport_context) | 105 scoped_refptr<TransportContext> transport_context, |
106 : transport_context_(transport_context), | 106 EventHandler* event_handler) |
107 worker_thread_(worker_thread), | 107 : worker_thread_(worker_thread), |
| 108 transport_context_(transport_context), |
| 109 event_handler_(event_handler), |
108 weak_factory_(this) {} | 110 weak_factory_(this) {} |
109 | 111 |
110 WebrtcTransport::~WebrtcTransport() {} | 112 WebrtcTransport::~WebrtcTransport() {} |
111 | 113 |
112 void WebrtcTransport::Start(EventHandler* event_handler, | 114 void WebrtcTransport::Start( |
113 Authenticator* authenticator) { | 115 Authenticator* authenticator, |
| 116 SendTransportInfoCallback send_transport_info_callback) { |
114 DCHECK(thread_checker_.CalledOnValidThread()); | 117 DCHECK(thread_checker_.CalledOnValidThread()); |
| 118 DCHECK(send_transport_info_callback_.is_null()); |
115 | 119 |
116 event_handler_ = event_handler; | 120 send_transport_info_callback_ = std::move(send_transport_info_callback); |
| 121 |
117 // TODO(sergeyu): Use the |authenticator| to authenticate PeerConnection. | 122 // TODO(sergeyu): Use the |authenticator| to authenticate PeerConnection. |
118 | 123 |
119 transport_context_->CreatePortAllocator(base::Bind( | 124 transport_context_->CreatePortAllocator(base::Bind( |
120 &WebrtcTransport::OnPortAllocatorCreated, weak_factory_.GetWeakPtr())); | 125 &WebrtcTransport::OnPortAllocatorCreated, weak_factory_.GetWeakPtr())); |
121 } | 126 } |
122 | 127 |
123 void WebrtcTransport::OnPortAllocatorCreated( | 128 void WebrtcTransport::OnPortAllocatorCreated( |
124 scoped_ptr<cricket::PortAllocator> port_allocator) { | 129 scoped_ptr<cricket::PortAllocator> port_allocator) { |
125 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 130 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
126 | 131 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 } | 243 } |
239 | 244 |
240 return true; | 245 return true; |
241 } | 246 } |
242 | 247 |
243 StreamChannelFactory* WebrtcTransport::GetStreamChannelFactory() { | 248 StreamChannelFactory* WebrtcTransport::GetStreamChannelFactory() { |
244 DCHECK(thread_checker_.CalledOnValidThread()); | 249 DCHECK(thread_checker_.CalledOnValidThread()); |
245 return &data_stream_adapter_; | 250 return &data_stream_adapter_; |
246 } | 251 } |
247 | 252 |
248 StreamChannelFactory* WebrtcTransport::GetMultiplexedChannelFactory() { | |
249 DCHECK(thread_checker_.CalledOnValidThread()); | |
250 return GetStreamChannelFactory(); | |
251 } | |
252 | |
253 WebrtcTransport* WebrtcTransport::AsWebrtcTransport() { | |
254 return this; | |
255 } | |
256 | |
257 void WebrtcTransport::OnLocalSessionDescriptionCreated( | 253 void WebrtcTransport::OnLocalSessionDescriptionCreated( |
258 scoped_ptr<webrtc::SessionDescriptionInterface> description, | 254 scoped_ptr<webrtc::SessionDescriptionInterface> description, |
259 const std::string& error) { | 255 const std::string& error) { |
260 DCHECK(thread_checker_.CalledOnValidThread()); | 256 DCHECK(thread_checker_.CalledOnValidThread()); |
261 | 257 |
262 if (!peer_connection_) | 258 if (!peer_connection_) |
263 return; | 259 return; |
264 | 260 |
265 if (!description) { | 261 if (!description) { |
266 LOG(ERROR) << "PeerConnection offer creation failed: " << error; | 262 LOG(ERROR) << "PeerConnection offer creation failed: " << error; |
(...skipping 10 matching lines...) Expand all Loading... |
277 | 273 |
278 // Format and send the session description to the peer. | 274 // Format and send the session description to the peer. |
279 scoped_ptr<XmlElement> transport_info( | 275 scoped_ptr<XmlElement> transport_info( |
280 new XmlElement(QName(kTransportNamespace, "transport"), true)); | 276 new XmlElement(QName(kTransportNamespace, "transport"), true)); |
281 XmlElement* offer_tag = | 277 XmlElement* offer_tag = |
282 new XmlElement(QName(kTransportNamespace, "session-description")); | 278 new XmlElement(QName(kTransportNamespace, "session-description")); |
283 transport_info->AddElement(offer_tag); | 279 transport_info->AddElement(offer_tag); |
284 offer_tag->SetAttr(QName(std::string(), "type"), description->type()); | 280 offer_tag->SetAttr(QName(std::string(), "type"), description->type()); |
285 offer_tag->SetBodyText(description_sdp); | 281 offer_tag->SetBodyText(description_sdp); |
286 | 282 |
287 event_handler_->OnOutgoingTransportInfo(std::move(transport_info)); | 283 send_transport_info_callback_.Run(std::move(transport_info)); |
288 | 284 |
289 peer_connection_->SetLocalDescription( | 285 peer_connection_->SetLocalDescription( |
290 SetSessionDescriptionObserver::Create(base::Bind( | 286 SetSessionDescriptionObserver::Create(base::Bind( |
291 &WebrtcTransport::OnLocalDescriptionSet, weak_factory_.GetWeakPtr())), | 287 &WebrtcTransport::OnLocalDescriptionSet, weak_factory_.GetWeakPtr())), |
292 description.release()); | 288 description.release()); |
293 } | 289 } |
294 | 290 |
295 void WebrtcTransport::OnLocalDescriptionSet(bool success, | 291 void WebrtcTransport::OnLocalDescriptionSet(bool success, |
296 const std::string& error) { | 292 const std::string& error) { |
297 DCHECK(thread_checker_.CalledOnValidThread()); | 293 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 | 332 |
337 void WebrtcTransport::Close(ErrorCode error) { | 333 void WebrtcTransport::Close(ErrorCode error) { |
338 DCHECK(thread_checker_.CalledOnValidThread()); | 334 DCHECK(thread_checker_.CalledOnValidThread()); |
339 | 335 |
340 weak_factory_.InvalidateWeakPtrs(); | 336 weak_factory_.InvalidateWeakPtrs(); |
341 peer_connection_->Close(); | 337 peer_connection_->Close(); |
342 peer_connection_ = nullptr; | 338 peer_connection_ = nullptr; |
343 peer_connection_factory_ = nullptr; | 339 peer_connection_factory_ = nullptr; |
344 | 340 |
345 if (error != OK) | 341 if (error != OK) |
346 event_handler_->OnTransportError(error); | 342 event_handler_->OnWebrtcTransportError(error); |
347 } | 343 } |
348 | 344 |
349 void WebrtcTransport::OnSignalingChange( | 345 void WebrtcTransport::OnSignalingChange( |
350 webrtc::PeerConnectionInterface::SignalingState new_state) { | 346 webrtc::PeerConnectionInterface::SignalingState new_state) { |
351 DCHECK(thread_checker_.CalledOnValidThread()); | 347 DCHECK(thread_checker_.CalledOnValidThread()); |
352 } | 348 } |
353 | 349 |
354 void WebrtcTransport::OnAddStream(webrtc::MediaStreamInterface* stream) { | 350 void WebrtcTransport::OnAddStream(webrtc::MediaStreamInterface* stream) { |
355 DCHECK(thread_checker_.CalledOnValidThread()); | 351 DCHECK(thread_checker_.CalledOnValidThread()); |
356 LOG(ERROR) << "Stream added " << stream->label(); | 352 LOG(ERROR) << "Stream added " << stream->label(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 FROM_HERE, | 384 FROM_HERE, |
389 base::Bind(&WebrtcTransport::SendOffer, weak_factory_.GetWeakPtr())); | 385 base::Bind(&WebrtcTransport::SendOffer, weak_factory_.GetWeakPtr())); |
390 } | 386 } |
391 } | 387 } |
392 | 388 |
393 void WebrtcTransport::OnIceConnectionChange( | 389 void WebrtcTransport::OnIceConnectionChange( |
394 webrtc::PeerConnectionInterface::IceConnectionState new_state) { | 390 webrtc::PeerConnectionInterface::IceConnectionState new_state) { |
395 DCHECK(thread_checker_.CalledOnValidThread()); | 391 DCHECK(thread_checker_.CalledOnValidThread()); |
396 | 392 |
397 if (new_state == webrtc::PeerConnectionInterface::kIceConnectionConnected) | 393 if (new_state == webrtc::PeerConnectionInterface::kIceConnectionConnected) |
398 event_handler_->OnTransportConnected(); | 394 event_handler_->OnWebrtcTransportConnected(); |
399 } | 395 } |
400 | 396 |
401 void WebrtcTransport::OnIceGatheringChange( | 397 void WebrtcTransport::OnIceGatheringChange( |
402 webrtc::PeerConnectionInterface::IceGatheringState new_state) { | 398 webrtc::PeerConnectionInterface::IceGatheringState new_state) { |
403 DCHECK(thread_checker_.CalledOnValidThread()); | 399 DCHECK(thread_checker_.CalledOnValidThread()); |
404 } | 400 } |
405 | 401 |
406 void WebrtcTransport::OnIceCandidate( | 402 void WebrtcTransport::OnIceCandidate( |
407 const webrtc::IceCandidateInterface* candidate) { | 403 const webrtc::IceCandidateInterface* candidate) { |
408 DCHECK(thread_checker_.CalledOnValidThread()); | 404 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 CreateSessionDescriptionObserver::Create( | 459 CreateSessionDescriptionObserver::Create( |
464 base::Bind(&WebrtcTransport::OnLocalSessionDescriptionCreated, | 460 base::Bind(&WebrtcTransport::OnLocalSessionDescriptionCreated, |
465 weak_factory_.GetWeakPtr())), | 461 weak_factory_.GetWeakPtr())), |
466 &offer_config); | 462 &offer_config); |
467 } | 463 } |
468 | 464 |
469 void WebrtcTransport::SendTransportInfo() { | 465 void WebrtcTransport::SendTransportInfo() { |
470 DCHECK(thread_checker_.CalledOnValidThread()); | 466 DCHECK(thread_checker_.CalledOnValidThread()); |
471 DCHECK(pending_transport_info_message_); | 467 DCHECK(pending_transport_info_message_); |
472 | 468 |
473 event_handler_->OnOutgoingTransportInfo( | 469 send_transport_info_callback_.Run(std::move(pending_transport_info_message_)); |
474 std::move(pending_transport_info_message_)); | |
475 pending_transport_info_message_.reset(); | |
476 } | 470 } |
477 | 471 |
478 void WebrtcTransport::AddPendingCandidatesIfPossible() { | 472 void WebrtcTransport::AddPendingCandidatesIfPossible() { |
479 DCHECK(thread_checker_.CalledOnValidThread()); | 473 DCHECK(thread_checker_.CalledOnValidThread()); |
480 | 474 |
481 if (peer_connection_->signaling_state() == | 475 if (peer_connection_->signaling_state() == |
482 webrtc::PeerConnectionInterface::kStable) { | 476 webrtc::PeerConnectionInterface::kStable) { |
483 for (auto candidate : pending_incoming_candidates_) { | 477 for (auto candidate : pending_incoming_candidates_) { |
484 if (!peer_connection_->AddIceCandidate(candidate)) { | 478 if (!peer_connection_->AddIceCandidate(candidate)) { |
485 LOG(ERROR) << "Failed to add incoming candidate"; | 479 LOG(ERROR) << "Failed to add incoming candidate"; |
486 Close(INCOMPATIBLE_PROTOCOL); | 480 Close(INCOMPATIBLE_PROTOCOL); |
487 return; | 481 return; |
488 } | 482 } |
489 } | 483 } |
490 pending_incoming_candidates_.clear(); | 484 pending_incoming_candidates_.clear(); |
491 } | 485 } |
492 } | 486 } |
493 | 487 |
494 WebrtcTransportFactory::WebrtcTransportFactory( | |
495 rtc::Thread* worker_thread, | |
496 scoped_refptr<TransportContext> transport_context) | |
497 : worker_thread_(worker_thread), | |
498 transport_context_(transport_context) {} | |
499 | |
500 WebrtcTransportFactory::~WebrtcTransportFactory() {} | |
501 | |
502 scoped_ptr<Transport> WebrtcTransportFactory::CreateTransport() { | |
503 return make_scoped_ptr( | |
504 new WebrtcTransport(worker_thread_, transport_context_.get())); | |
505 } | |
506 | |
507 } // namespace protocol | 488 } // namespace protocol |
508 } // namespace remoting | 489 } // namespace remoting |
OLD | NEW |