-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
webrtc.cc
383 lines (342 loc) · 15.9 KB
/
webrtc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/services/sharing/nearby/platform/webrtc.h"
#include "base/i18n/timezone.h"
#include "chrome/services/sharing/webrtc/ipc_network_manager.h"
#include "chrome/services/sharing/webrtc/ipc_packet_socket_factory.h"
#include "chrome/services/sharing/webrtc/mdns_responder_adapter.h"
#include "chrome/services/sharing/webrtc/p2p_port_allocator.h"
#include "chromeos/services/nearby/public/mojom/webrtc_signaling_messenger.mojom-shared.h"
#include "jingle/glue/thread_wrapper.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "third_party/nearby/src/cpp/platform/public/future.h"
#include "third_party/nearby/src/cpp/platform/public/logging.h"
#include "third_party/webrtc/api/jsep.h"
#include "third_party/webrtc/api/peer_connection_interface.h"
#include "third_party/webrtc_overrides/task_queue_factory.h"
namespace location {
namespace nearby {
namespace chrome {
namespace {
net::NetworkTrafficAnnotationTag kTrafficAnnotation =
net::DefineNetworkTrafficAnnotation("nearby_webrtc_connection", R"(
semantics {
sender: "Chrome Nearby Share via WebRTC"
description:
"Chrome Nearby Share allows users to send data securely between "
"devices. WebRTC allows Chrome to establish a secure session with "
"another Nearby instance running on a different device and to "
"transmit and receive data that users want to share across "
"devices."
trigger:
"User uses the Nearby Share feature and selects a peer device to"
" send the data to."
data:
"Text and media encrypted via AES-256-CBC. Protocol-level messages "
"for the various subprotocols employed by WebRTC (including ICE, "
"DTLS, RTCP, etc.) are encrypted via DTLS-SRTP. Note that ICE "
"connectivity checks may leak the user's IP address(es), subject "
"to the restrictions/guidance in "
"https://datatracker.ietf.org/doc/draft-ietf-rtcweb-ip-handling."
destination: OTHER
destination_other:
"A peer Nearby device that receives this data"
}
policy {
cookies_allowed: NO
setting:
"This feature is only enabled for signed-in users who enable "
"Nearby Share"
chrome_policy {
BrowserSignin {
policy_options {mode: MANDATORY}
BrowserSignin: 0
}
}
}
)");
class ProxyAsyncResolverFactory final : public webrtc::AsyncResolverFactory {
public:
explicit ProxyAsyncResolverFactory(
sharing::IpcPacketSocketFactory* socket_factory)
: socket_factory_(socket_factory) {
DCHECK(socket_factory_);
}
rtc::AsyncResolverInterface* Create() override {
return socket_factory_->CreateAsyncResolver();
}
private:
sharing::IpcPacketSocketFactory* socket_factory_;
};
// This object only exists to forward incoming mojo messages. It will be created
// as a SelfOwnedReceiver on a separate sequence and will be cleaned up when the
// connection goes down. This is necessary to keep it pumping messages while the
// the main WebRtc thread is blocked on a future.
class IncomingMessageListener
: public sharing::mojom::IncomingMessagesListener {
public:
explicit IncomingMessageListener(
api::WebRtcSignalingMessenger::OnSignalingMessageCallback
signaling_message_callback,
api::WebRtcSignalingMessenger::OnSignalingCompleteCallback
signaling_complete_callback)
: signaling_message_callback_(std::move(signaling_message_callback)),
signaling_complete_callback_(std::move(signaling_complete_callback)) {
DCHECK(signaling_message_callback_);
DCHECK(signaling_complete_callback_);
}
~IncomingMessageListener() override = default;
// mojom::IncomingMessagesListener:
void OnMessage(const std::string& message) override {
signaling_message_callback_(ByteArray(message));
}
// mojom::IncomingMessagesListener:
void OnComplete(bool success) override {
signaling_complete_callback_(success);
}
private:
api::WebRtcSignalingMessenger::OnSignalingMessageCallback
signaling_message_callback_;
api::WebRtcSignalingMessenger::OnSignalingCompleteCallback
signaling_complete_callback_;
};
// Used as a messenger in sending and receiving WebRTC messages between devices.
// The messages sent and received are considered untrusted since they
// originate in an untrusted sandboxed process on device.
class WebRtcSignalingMessengerImpl : public api::WebRtcSignalingMessenger {
public:
WebRtcSignalingMessengerImpl(
const std::string& self_id,
const connections::LocationHint& location_hint,
const mojo::SharedRemote<sharing::mojom::WebRtcSignalingMessenger>&
messenger)
: self_id_(self_id),
location_hint_(location_hint),
messenger_(messenger),
task_runner_(
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()})) {}
~WebRtcSignalingMessengerImpl() override { StopReceivingMessages(); }
WebRtcSignalingMessengerImpl(const WebRtcSignalingMessengerImpl& other) =
delete;
WebRtcSignalingMessengerImpl& operator=(
const WebRtcSignalingMessengerImpl& other) = delete;
sharing::mojom::LocationHintPtr CreateLocationHint() {
sharing::mojom::LocationHintPtr location_hint_ptr =
sharing::mojom::LocationHint::New();
location_hint_ptr->location = location_hint_.location();
switch (location_hint_.format()) {
case location::nearby::connections::LocationStandard_Format::
LocationStandard_Format_E164_CALLING:
location_hint_ptr->format =
sharing::mojom::LocationStandardFormat::E164_CALLING;
break;
case location::nearby::connections::LocationStandard_Format::
LocationStandard_Format_ISO_3166_1_ALPHA_2:
location_hint_ptr->format =
sharing::mojom::LocationStandardFormat::ISO_3166_1_ALPHA_2;
break;
case location::nearby::connections::LocationStandard_Format::
LocationStandard_Format_UNKNOWN:
// Here we default to the current default country code before sending.
location_hint_ptr->location = base::CountryCodeForCurrentTimezone();
location_hint_ptr->format =
sharing::mojom::LocationStandardFormat::ISO_3166_1_ALPHA_2;
break;
}
return location_hint_ptr;
}
// api::WebRtcSignalingMessenger:
bool SendMessage(absl::string_view peer_id,
const ByteArray& message) override {
bool success = false;
if (!messenger_->SendMessage(self_id_, std::string(peer_id),
CreateLocationHint(), std::string(message),
&success)) {
return false;
}
return success;
}
void BindIncomingReceiver(
mojo::PendingReceiver<sharing::mojom::IncomingMessagesListener>
pending_receiver,
api::WebRtcSignalingMessenger::OnSignalingMessageCallback
message_callback,
api::WebRtcSignalingMessenger::OnSignalingCompleteCallback
complete_callback) {
mojo::MakeSelfOwnedReceiver(
std::make_unique<IncomingMessageListener>(std::move(message_callback),
std::move(complete_callback)),
std::move(pending_receiver), task_runner_);
}
// api::WebRtcSignalingMessenger:
bool StartReceivingMessages(
OnSignalingMessageCallback message_callback,
OnSignalingCompleteCallback complete_callback) override {
bool success = false;
mojo::PendingRemote<sharing::mojom::IncomingMessagesListener>
pending_remote;
mojo::PendingReceiver<sharing::mojom::IncomingMessagesListener>
pending_receiver = pending_remote.InitWithNewPipeAndPassReceiver();
// NOTE: this is a Sync mojo call that waits until Fast-Path ready is
// received on the Instant Messaging (Tachyon) stream before returning.
if (!messenger_->StartReceivingMessages(self_id_, CreateLocationHint(),
std::move(pending_remote), &success,
&pending_session_remote_) ||
!success) {
receiving_messages_ = false;
return false;
}
// Do the pending_receiver Bind call on the task runner itself so it can
// receive messages while the WebRtc thread is waiting. Any incoming
// messages will be queued until the Bind happens.
task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&WebRtcSignalingMessengerImpl::BindIncomingReceiver,
weak_ptr_factory_.GetWeakPtr(),
std::move(pending_receiver), std::move(message_callback),
std::move(complete_callback)));
receiving_messages_ = true;
return true;
}
// api::WebRtcSignalingMessenger:
void StopReceivingMessages() override {
if (receiving_messages_) {
receiving_messages_ = false;
if (pending_session_remote_) {
mojo::Remote<sharing::mojom::ReceiveMessagesSession> session(
std::move(pending_session_remote_));
// This is a one-way message so it is safe to bind, send, and forget.
// When the Remote goes out of scope it will close the pipe and cause
// the other side to clean up the ReceiveMessagesExpress instance.
// If the receiver/pipe is already down, this does nothing.
session->StopReceivingMessages();
}
}
}
private:
bool receiving_messages_ = false;
std::string self_id_;
connections::LocationHint location_hint_;
// This is received and stored on a successful StartReceiveMessages(). We
// choose to not bind right away because multiple threads end up
// creating/calling/destroying WebRtcSignalingMessengerImpl by the design
// of NearbyConnections. We need to ensure the thread that
// binds/calls/destroys the remote is the same sequence, so we do all three at
// once in StopReceivingMessages(). If the other side of the pipe is already
// down, binding, calling, and destorying will be a no-op.
mojo::PendingRemote<sharing::mojom::ReceiveMessagesSession>
pending_session_remote_;
mojo::SharedRemote<sharing::mojom::WebRtcSignalingMessenger> messenger_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::WeakPtrFactory<WebRtcSignalingMessengerImpl> weak_ptr_factory_{this};
};
} // namespace
WebRtcMedium::WebRtcMedium(
const mojo::SharedRemote<network::mojom::P2PSocketManager>& socket_manager,
const mojo::SharedRemote<network::mojom::MdnsResponder>& mdns_responder,
const mojo::SharedRemote<sharing::mojom::IceConfigFetcher>&
ice_config_fetcher,
const mojo::SharedRemote<sharing::mojom::WebRtcSignalingMessenger>&
webrtc_signaling_messenger,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: p2p_socket_manager_(socket_manager),
mdns_responder_(mdns_responder),
ice_config_fetcher_(ice_config_fetcher),
webrtc_signaling_messenger_(webrtc_signaling_messenger),
task_runner_(std::move(task_runner)) {
DCHECK(p2p_socket_manager_.is_bound());
DCHECK(mdns_responder_.is_bound());
DCHECK(ice_config_fetcher_.is_bound());
DCHECK(webrtc_signaling_messenger_.is_bound());
}
WebRtcMedium::~WebRtcMedium() = default;
const std::string WebRtcMedium::GetDefaultCountryCode() {
return base::CountryCodeForCurrentTimezone();
}
void WebRtcMedium::CreatePeerConnection(
webrtc::PeerConnectionObserver* observer,
PeerConnectionCallback callback) {
task_runner_->PostTask(
FROM_HERE, base::BindOnce(&WebRtcMedium::FetchIceServers,
weak_ptr_factory_.GetWeakPtr(), observer,
std::move(callback)));
}
void WebRtcMedium::FetchIceServers(webrtc::PeerConnectionObserver* observer,
PeerConnectionCallback callback) {
ice_config_fetcher_->GetIceServers(base::BindOnce(
&WebRtcMedium::OnIceServersFetched, weak_ptr_factory_.GetWeakPtr(),
observer, std::move(callback)));
}
void WebRtcMedium::OnIceServersFetched(
webrtc::PeerConnectionObserver* observer,
PeerConnectionCallback callback,
std::vector<sharing::mojom::IceServerPtr> ice_servers) {
// WebRTC is using the current thread instead of creating new threads since
// otherwise the |network_manager| is created on current thread and destroyed
// on network thread, and so the mojo Receiver stored in it is not called on
// the same sequence. The long terms correct fix is to resolve
// http://crbug.com/1044522 and reuse the code in blink layer which ensures
// that the objects are created on the same thread they would be destroyed in.
jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
webrtc::PeerConnectionFactoryDependencies factory_dependencies;
factory_dependencies.task_queue_factory = CreateWebRtcTaskQueueFactory();
factory_dependencies.network_thread = rtc::Thread::Current();
factory_dependencies.worker_thread = rtc::Thread::Current();
factory_dependencies.signaling_thread = rtc::Thread::Current();
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory =
webrtc::CreateModularPeerConnectionFactory(
std::move(factory_dependencies));
webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
for (const auto& ice_server : ice_servers) {
webrtc::PeerConnectionInterface::IceServer ice_turn_server;
for (const auto& url : ice_server->urls)
ice_turn_server.urls.push_back(url.spec());
if (ice_server->username)
ice_turn_server.username = *ice_server->username;
if (ice_server->credential)
ice_turn_server.password = *ice_server->credential;
rtc_config.servers.push_back(ice_turn_server);
}
if (!socket_factory_) {
socket_factory_ = std::make_unique<sharing::IpcPacketSocketFactory>(
p2p_socket_manager_, kTrafficAnnotation);
}
if (!network_manager_) {
// NOTE: |network_manager_| is only created once and shared for every peer
// connection due to the use of the shared |p2p_socket_manager_|. See
// https://crbug.com/1142717 for more details.
network_manager_ = std::make_unique<sharing::IpcNetworkManager>(
p2p_socket_manager_,
std::make_unique<sharing::MdnsResponderAdapter>(mdns_responder_));
// NOTE: IpcNetworkManager::Initialize() does not override the empty default
// implementation so this doesn't actually do anything right now. However
// the contract of rtc::NetworkManagerBase states that it should be called
// before using and explicitly on the network thread (which right now is the
// current thread). Previously this was handled by P2PPortAllocator.
network_manager_->Initialize();
}
webrtc::PeerConnectionDependencies dependencies(observer);
sharing::P2PPortAllocator::Config port_config;
port_config.enable_multiple_routes = true;
port_config.enable_nonproxied_udp = true;
dependencies.allocator = std::make_unique<sharing::P2PPortAllocator>(
network_manager_.get(), socket_factory_.get(), port_config);
dependencies.async_resolver_factory =
std::make_unique<ProxyAsyncResolverFactory>(socket_factory_.get());
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection =
pc_factory->CreatePeerConnection(rtc_config, std::move(dependencies));
callback(std::move(peer_connection));
}
std::unique_ptr<api::WebRtcSignalingMessenger>
WebRtcMedium::GetSignalingMessenger(
absl::string_view self_id,
const connections::LocationHint& location_hint) {
return std::make_unique<WebRtcSignalingMessengerImpl>(
std::string(self_id), location_hint, webrtc_signaling_messenger_);
}
} // namespace chrome
} // namespace nearby
} // namespace location