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" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "jingle/glue/thread_wrapper.h" | 12 #include "jingle/glue/thread_wrapper.h" |
| 13 #include "remoting/protocol/transport_context.h" |
13 #include "third_party/libjingle/source/talk/app/webrtc/test/fakeconstraints.h" | 14 #include "third_party/libjingle/source/talk/app/webrtc/test/fakeconstraints.h" |
14 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 15 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
15 #include "third_party/webrtc/modules/audio_device/include/fake_audio_device.h" | 16 #include "third_party/webrtc/modules/audio_device/include/fake_audio_device.h" |
16 | 17 |
17 using buzz::QName; | 18 using buzz::QName; |
18 using buzz::XmlElement; | 19 using buzz::XmlElement; |
19 | 20 |
20 namespace remoting { | 21 namespace remoting { |
21 namespace protocol { | 22 namespace protocol { |
22 | 23 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 ~SetSessionDescriptionObserver() override {} | 93 ~SetSessionDescriptionObserver() override {} |
93 | 94 |
94 private: | 95 private: |
95 ResultCallback result_callback_; | 96 ResultCallback result_callback_; |
96 | 97 |
97 DISALLOW_COPY_AND_ASSIGN(SetSessionDescriptionObserver); | 98 DISALLOW_COPY_AND_ASSIGN(SetSessionDescriptionObserver); |
98 }; | 99 }; |
99 | 100 |
100 } // namespace | 101 } // namespace |
101 | 102 |
102 WebrtcTransport::WebrtcTransport(rtc::Thread* worker_thread, | 103 WebrtcTransport::WebrtcTransport( |
103 PortAllocatorFactory* port_allocator_factory, | 104 rtc::Thread* worker_thread, |
104 TransportRole role) | 105 scoped_refptr<TransportContext> transport_context) |
105 : port_allocator_factory_(port_allocator_factory), | 106 : transport_context_(transport_context), |
106 role_(role), | |
107 worker_thread_(worker_thread), | 107 worker_thread_(worker_thread), |
108 weak_factory_(this) {} | 108 weak_factory_(this) {} |
109 | 109 |
110 WebrtcTransport::~WebrtcTransport() {} | 110 WebrtcTransport::~WebrtcTransport() {} |
111 | 111 |
112 void WebrtcTransport::Start(EventHandler* event_handler, | 112 void WebrtcTransport::Start(EventHandler* event_handler, |
113 Authenticator* authenticator) { | 113 Authenticator* authenticator) { |
114 DCHECK(thread_checker_.CalledOnValidThread()); | 114 DCHECK(thread_checker_.CalledOnValidThread()); |
115 | 115 |
116 event_handler_ = event_handler; | 116 event_handler_ = event_handler; |
| 117 // TODO(sergeyu): Use the |authenticator| to authenticate PeerConnection. |
117 | 118 |
118 // TODO(sergeyu): Use the |authenticator| to authenticate PeerConnection. | 119 transport_context_->CreatePortAllocator(base::Bind( |
| 120 &WebrtcTransport::OnPortAllocatorCreated, weak_factory_.GetWeakPtr())); |
| 121 } |
| 122 |
| 123 void WebrtcTransport::OnPortAllocatorCreated( |
| 124 scoped_ptr<cricket::PortAllocator> port_allocator) { |
119 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 125 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
120 | 126 |
121 // TODO(sergeyu): Investigate if it's possible to avoid Send(). | 127 // TODO(sergeyu): Investigate if it's possible to avoid Send(). |
122 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); | 128 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); |
123 | 129 |
124 fake_audio_device_module_.reset(new webrtc::FakeAudioDeviceModule()); | 130 fake_audio_device_module_.reset(new webrtc::FakeAudioDeviceModule()); |
125 | 131 |
126 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( | 132 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( |
127 worker_thread_, rtc::Thread::Current(), | 133 worker_thread_, rtc::Thread::Current(), |
128 fake_audio_device_module_.get(), nullptr, nullptr); | 134 fake_audio_device_module_.get(), nullptr, nullptr); |
129 | 135 |
130 webrtc::PeerConnectionInterface::IceServer stun_server; | 136 webrtc::PeerConnectionInterface::IceServer stun_server; |
131 stun_server.urls.push_back("stun:stun.l.google.com:19302"); | 137 stun_server.urls.push_back("stun:stun.l.google.com:19302"); |
132 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config; | 138 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config; |
133 rtc_config.servers.push_back(stun_server); | 139 rtc_config.servers.push_back(stun_server); |
134 | 140 |
135 webrtc::FakeConstraints constraints; | 141 webrtc::FakeConstraints constraints; |
136 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, | 142 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, |
137 webrtc::MediaConstraintsInterface::kValueTrue); | 143 webrtc::MediaConstraintsInterface::kValueTrue); |
138 | 144 |
139 rtc::scoped_ptr<cricket::PortAllocator> port_allocator( | 145 peer_connection_ = peer_connection_factory_->CreatePeerConnection( |
140 port_allocator_factory_->CreatePortAllocator()); | 146 rtc_config, &constraints, |
| 147 rtc::scoped_ptr<cricket::PortAllocator>(port_allocator.release()), |
| 148 nullptr, this); |
141 | 149 |
142 peer_connection_ = peer_connection_factory_->CreatePeerConnection( | 150 data_stream_adapter_.Initialize( |
143 rtc_config, &constraints, std::move(port_allocator), nullptr, this); | 151 peer_connection_, transport_context_->role() == TransportRole::SERVER); |
144 | 152 |
145 data_stream_adapter_.Initialize(peer_connection_, | 153 if (transport_context_->role() == TransportRole::SERVER) |
146 role_ == TransportRole::SERVER); | |
147 | |
148 if (role_ == TransportRole::SERVER) | |
149 RequestNegotiation(); | 154 RequestNegotiation(); |
150 } | 155 } |
151 | 156 |
152 bool WebrtcTransport::ProcessTransportInfo(XmlElement* transport_info) { | 157 bool WebrtcTransport::ProcessTransportInfo(XmlElement* transport_info) { |
153 DCHECK(thread_checker_.CalledOnValidThread()); | 158 DCHECK(thread_checker_.CalledOnValidThread()); |
154 | 159 |
155 if (transport_info->Name() != QName(kTransportNamespace, "transport")) | 160 if (transport_info->Name() != QName(kTransportNamespace, "transport")) |
156 return false; | 161 return false; |
157 | 162 |
158 if (!peer_connection_) | 163 if (!peer_connection_) |
159 return false; | 164 return false; |
160 | 165 |
161 XmlElement* session_description = transport_info->FirstNamed( | 166 XmlElement* session_description = transport_info->FirstNamed( |
162 QName(kTransportNamespace, "session-description")); | 167 QName(kTransportNamespace, "session-description")); |
163 if (session_description) { | 168 if (session_description) { |
164 webrtc::PeerConnectionInterface::SignalingState expected_state = | 169 webrtc::PeerConnectionInterface::SignalingState expected_state = |
165 role_ == TransportRole::CLIENT | 170 transport_context_->role() == TransportRole::CLIENT |
166 ? webrtc::PeerConnectionInterface::kStable | 171 ? webrtc::PeerConnectionInterface::kStable |
167 : webrtc::PeerConnectionInterface::kHaveLocalOffer; | 172 : webrtc::PeerConnectionInterface::kHaveLocalOffer; |
168 if (peer_connection_->signaling_state() != expected_state) { | 173 if (peer_connection_->signaling_state() != expected_state) { |
169 LOG(ERROR) << "Received unexpected WebRTC session_description. "; | 174 LOG(ERROR) << "Received unexpected WebRTC session_description. "; |
170 return false; | 175 return false; |
171 } | 176 } |
172 | 177 |
173 std::string type = session_description->Attr(QName(std::string(), "type")); | 178 std::string type = session_description->Attr(QName(std::string(), "type")); |
174 std::string sdp = session_description->BodyText(); | 179 std::string sdp = session_description->BodyText(); |
175 if (type.empty() || sdp.empty()) { | 180 if (type.empty() || sdp.empty()) { |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 363 |
359 void WebrtcTransport::OnDataChannel( | 364 void WebrtcTransport::OnDataChannel( |
360 webrtc::DataChannelInterface* data_channel) { | 365 webrtc::DataChannelInterface* data_channel) { |
361 DCHECK(thread_checker_.CalledOnValidThread()); | 366 DCHECK(thread_checker_.CalledOnValidThread()); |
362 data_stream_adapter_.OnIncomingDataChannel(data_channel); | 367 data_stream_adapter_.OnIncomingDataChannel(data_channel); |
363 } | 368 } |
364 | 369 |
365 void WebrtcTransport::OnRenegotiationNeeded() { | 370 void WebrtcTransport::OnRenegotiationNeeded() { |
366 DCHECK(thread_checker_.CalledOnValidThread()); | 371 DCHECK(thread_checker_.CalledOnValidThread()); |
367 | 372 |
368 if (role_ == TransportRole::SERVER) { | 373 if (transport_context_->role() == TransportRole::SERVER) { |
369 RequestNegotiation(); | 374 RequestNegotiation(); |
370 } else { | 375 } else { |
371 // TODO(sergeyu): Is it necessary to support renegotiation initiated by the | 376 // TODO(sergeyu): Is it necessary to support renegotiation initiated by the |
372 // client? | 377 // client? |
373 NOTIMPLEMENTED(); | 378 NOTIMPLEMENTED(); |
374 } | 379 } |
375 } | 380 } |
376 | 381 |
377 void WebrtcTransport::RequestNegotiation() { | 382 void WebrtcTransport::RequestNegotiation() { |
378 DCHECK(role_ == TransportRole::SERVER); | 383 DCHECK(transport_context_->role() == TransportRole::SERVER); |
379 | 384 |
380 if (!negotiation_pending_) { | 385 if (!negotiation_pending_) { |
381 negotiation_pending_ = true; | 386 negotiation_pending_ = true; |
382 base::ThreadTaskRunnerHandle::Get()->PostTask( | 387 base::ThreadTaskRunnerHandle::Get()->PostTask( |
383 FROM_HERE, | 388 FROM_HERE, |
384 base::Bind(&WebrtcTransport::SendOffer, weak_factory_.GetWeakPtr())); | 389 base::Bind(&WebrtcTransport::SendOffer, weak_factory_.GetWeakPtr())); |
385 } | 390 } |
386 } | 391 } |
387 | 392 |
388 void WebrtcTransport::OnIceConnectionChange( | 393 void WebrtcTransport::OnIceConnectionChange( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 | 438 |
434 // Delay sending the new candidates in case we get more candidates | 439 // Delay sending the new candidates in case we get more candidates |
435 // that we can send in one message. | 440 // that we can send in one message. |
436 transport_info_timer_.Start( | 441 transport_info_timer_.Start( |
437 FROM_HERE, base::TimeDelta::FromMilliseconds(kTransportInfoSendDelayMs), | 442 FROM_HERE, base::TimeDelta::FromMilliseconds(kTransportInfoSendDelayMs), |
438 this, &WebrtcTransport::SendTransportInfo); | 443 this, &WebrtcTransport::SendTransportInfo); |
439 } | 444 } |
440 } | 445 } |
441 | 446 |
442 void WebrtcTransport::SendOffer() { | 447 void WebrtcTransport::SendOffer() { |
443 DCHECK(role_ == TransportRole::SERVER); | 448 DCHECK(transport_context_->role() == TransportRole::SERVER); |
444 | 449 |
445 DCHECK(negotiation_pending_); | 450 DCHECK(negotiation_pending_); |
446 negotiation_pending_ = false; | 451 negotiation_pending_ = false; |
447 | 452 |
448 webrtc::FakeConstraints offer_config; | 453 webrtc::FakeConstraints offer_config; |
449 offer_config.AddMandatory( | 454 offer_config.AddMandatory( |
450 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, | 455 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, |
451 webrtc::MediaConstraintsInterface::kValueTrue); | 456 webrtc::MediaConstraintsInterface::kValueTrue); |
452 offer_config.AddMandatory( | 457 offer_config.AddMandatory( |
453 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, | 458 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, |
(...skipping 27 matching lines...) Expand all Loading... |
481 Close(INCOMPATIBLE_PROTOCOL); | 486 Close(INCOMPATIBLE_PROTOCOL); |
482 return; | 487 return; |
483 } | 488 } |
484 } | 489 } |
485 pending_incoming_candidates_.clear(); | 490 pending_incoming_candidates_.clear(); |
486 } | 491 } |
487 } | 492 } |
488 | 493 |
489 WebrtcTransportFactory::WebrtcTransportFactory( | 494 WebrtcTransportFactory::WebrtcTransportFactory( |
490 rtc::Thread* worker_thread, | 495 rtc::Thread* worker_thread, |
491 SignalStrategy* signal_strategy, | 496 scoped_refptr<TransportContext> transport_context) |
492 scoped_ptr<PortAllocatorFactory> port_allocator_factory, | |
493 TransportRole role) | |
494 : worker_thread_(worker_thread), | 497 : worker_thread_(worker_thread), |
495 signal_strategy_(signal_strategy), | 498 transport_context_(transport_context) {} |
496 port_allocator_factory_(std::move(port_allocator_factory)), | |
497 role_(role) {} | |
498 | 499 |
499 WebrtcTransportFactory::~WebrtcTransportFactory() {} | 500 WebrtcTransportFactory::~WebrtcTransportFactory() {} |
500 | 501 |
501 scoped_ptr<Transport> WebrtcTransportFactory::CreateTransport() { | 502 scoped_ptr<Transport> WebrtcTransportFactory::CreateTransport() { |
502 return make_scoped_ptr(new WebrtcTransport( | 503 return make_scoped_ptr( |
503 worker_thread_, port_allocator_factory_.get(), role_)); | 504 new WebrtcTransport(worker_thread_, transport_context_.get())); |
504 } | 505 } |
505 | 506 |
506 } // namespace protocol | 507 } // namespace protocol |
507 } // namespace remoting | 508 } // namespace remoting |
OLD | NEW |