forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcast_remoting_connector.cc
514 lines (450 loc) · 19.6 KB
/
cast_remoting_connector.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// Copyright 2016 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/browser/media/cast_remoting_connector.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/media/cast_remoting_connector_messaging.h"
#include "chrome/browser/media/cast_remoting_sender.h"
#include "chrome/browser/media/router/media_router.h"
#include "chrome/browser/media/router/media_router_factory.h"
#include "chrome/browser/media/router/route_message_observer.h"
#include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/media_router/media_source_helper.h"
#include "chrome/common/media_router/route_message.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
using content::BrowserThread;
using media::mojom::RemotingSinkCapabilities;
using media::mojom::RemotingStartFailReason;
using media::mojom::RemotingStopReason;
using Messaging = CastRemotingConnectorMessaging;
class CastRemotingConnector::RemotingBridge : public media::mojom::Remoter {
public:
// Constructs a "bridge" to delegate calls between the given |source| and
// |connector|. |connector| must be valid at the time of construction, but is
// otherwise a weak pointer that can become invalid during the lifetime of a
// RemotingBridge.
RemotingBridge(media::mojom::RemotingSourcePtr source,
CastRemotingConnector* connector)
: source_(std::move(source)), connector_(connector) {
DCHECK(connector_);
source_.set_connection_error_handler(base::Bind(
&RemotingBridge::Stop, base::Unretained(this),
RemotingStopReason::SOURCE_GONE));
connector_->RegisterBridge(this);
}
~RemotingBridge() final {
if (connector_)
connector_->DeregisterBridge(this, RemotingStopReason::SOURCE_GONE);
}
// The CastRemotingConnector calls these to call back to the RemotingSource.
void OnSinkAvailable(RemotingSinkCapabilities capabilities) {
source_->OnSinkAvailable(capabilities);
}
void OnSinkGone() { source_->OnSinkGone(); }
void OnStarted() { source_->OnStarted(); }
void OnStartFailed(RemotingStartFailReason reason) {
source_->OnStartFailed(reason);
}
void OnMessageFromSink(const std::vector<uint8_t>& message) {
source_->OnMessageFromSink(message);
}
void OnStopped(RemotingStopReason reason) { source_->OnStopped(reason); }
// The CastRemotingConnector calls this when it is no longer valid.
void OnCastRemotingConnectorDestroyed() {
connector_ = nullptr;
}
// media::mojom::Remoter implementation. The source calls these to start/stop
// media remoting and send messages to the sink. These simply delegate to the
// CastRemotingConnector, which mediates to establish only one remoting
// session among possibly multiple requests. The connector will respond to
// this request by calling one of: OnStarted() or OnStartFailed().
void Start() final {
if (connector_)
connector_->StartRemoting(this);
}
void StartDataStreams(
mojo::ScopedDataPipeConsumerHandle audio_pipe,
mojo::ScopedDataPipeConsumerHandle video_pipe,
media::mojom::RemotingDataStreamSenderRequest audio_sender_request,
media::mojom::RemotingDataStreamSenderRequest video_sender_request)
final {
if (connector_) {
connector_->StartRemotingDataStreams(
this, std::move(audio_pipe), std::move(video_pipe),
std::move(audio_sender_request), std::move(video_sender_request));
}
}
void Stop(RemotingStopReason reason) final {
if (connector_)
connector_->StopRemoting(this, reason);
}
void SendMessageToSink(const std::vector<uint8_t>& message) final {
if (connector_)
connector_->SendMessageToSink(this, message);
}
private:
media::mojom::RemotingSourcePtr source_;
// Weak pointer. Will be set to nullptr if the CastRemotingConnector is
// destroyed before this RemotingBridge.
CastRemotingConnector* connector_;
DISALLOW_COPY_AND_ASSIGN(RemotingBridge);
};
class CastRemotingConnector::MessageObserver
: public media_router::RouteMessageObserver {
public:
MessageObserver(media_router::MediaRouter* router,
const media_router::MediaRoute::Id& route_id,
CastRemotingConnector* connector)
: RouteMessageObserver(router, route_id), connector_(connector) {}
~MessageObserver() final {}
private:
void OnMessagesReceived(
const std::vector<media_router::RouteMessage>& messages) final {
connector_->ProcessMessagesFromRoute(messages);
}
CastRemotingConnector* const connector_;
};
// static
const void* const CastRemotingConnector::kUserDataKey = &kUserDataKey;
// static
CastRemotingConnector* CastRemotingConnector::Get(
content::WebContents* contents) {
DCHECK(contents);
CastRemotingConnector* connector =
static_cast<CastRemotingConnector*>(contents->GetUserData(kUserDataKey));
if (!connector) {
connector = new CastRemotingConnector(
media_router::MediaRouterFactory::GetApiForBrowserContext(
contents->GetBrowserContext()),
media_router::MediaSourceForTabContentRemoting(
SessionTabHelper::IdForTab(contents))
.id());
contents->SetUserData(kUserDataKey, base::WrapUnique(connector));
}
return connector;
}
// static
void CastRemotingConnector::CreateMediaRemoter(
content::RenderFrameHost* host,
media::mojom::RemotingSourcePtr source,
media::mojom::RemoterRequest request) {
DCHECK(host);
auto* const contents = content::WebContents::FromRenderFrameHost(host);
if (!contents)
return;
CastRemotingConnector::Get(contents)->CreateBridge(std::move(source),
std::move(request));
}
namespace {
RemotingSinkCapabilities GetFeatureEnabledCapabilities() {
#if !defined(OS_ANDROID) && !defined(OS_IOS)
if (base::FeatureList::IsEnabled(features::kMediaRemoting)) {
return RemotingSinkCapabilities::RENDERING_ONLY;
}
#endif // !defined(OS_ANDROID) && !defined(OS_IOS)
return RemotingSinkCapabilities::NONE;
}
} // namespace
CastRemotingConnector::CastRemotingConnector(
media_router::MediaRouter* router,
const media_router::MediaSource::Id& media_source_id)
: media_router::MediaRoutesObserver(router),
media_source_id_(media_source_id),
enabled_features_(GetFeatureEnabledCapabilities()),
session_counter_(0),
active_bridge_(nullptr),
weak_factory_(this) {}
CastRemotingConnector::~CastRemotingConnector() {
// Assume nothing about destruction/shutdown sequence of a tab. For example,
// it's possible the owning WebContents will be destroyed before the Mojo
// message pipes to the RemotingBridges have been closed.
if (active_bridge_)
StopRemoting(active_bridge_, RemotingStopReason::ROUTE_TERMINATED);
for (RemotingBridge* notifyee : bridges_) {
notifyee->OnSinkGone();
notifyee->OnCastRemotingConnectorDestroyed();
}
}
void CastRemotingConnector::CreateBridge(media::mojom::RemotingSourcePtr source,
media::mojom::RemoterRequest request) {
mojo::MakeStrongBinding(
base::MakeUnique<RemotingBridge>(std::move(source), this),
std::move(request));
}
void CastRemotingConnector::RegisterBridge(RemotingBridge* bridge) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(bridges_.find(bridge) == bridges_.end());
bridges_.insert(bridge);
if (message_observer_ && !active_bridge_)
bridge->OnSinkAvailable(enabled_features_);
}
void CastRemotingConnector::DeregisterBridge(RemotingBridge* bridge,
RemotingStopReason reason) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(bridges_.find(bridge) != bridges_.end());
if (bridge == active_bridge_)
StopRemoting(bridge, reason);
bridges_.erase(bridge);
}
void CastRemotingConnector::StartRemoting(RemotingBridge* bridge) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(bridges_.find(bridge) != bridges_.end());
// Refuse to start if there is no remoting route available, or if remoting is
// already active.
if (!message_observer_) {
bridge->OnStartFailed(RemotingStartFailReason::ROUTE_TERMINATED);
return;
}
if (active_bridge_) {
bridge->OnStartFailed(RemotingStartFailReason::CANNOT_START_MULTIPLE);
return;
}
// Notify all other sources that the sink is no longer available for remoting.
// A race condition is possible, where one of the other sources will try to
// start remoting before receiving this notification; but that attempt will
// just fail later on.
for (RemotingBridge* notifyee : bridges_) {
if (notifyee == bridge)
continue;
notifyee->OnSinkGone();
}
active_bridge_ = bridge;
// Send a start message to the Cast Provider.
++session_counter_; // New remoting session ID.
SendMessageToProvider(base::StringPrintf(
Messaging::kStartRemotingMessageFormat, session_counter_));
bridge->OnStarted();
}
void CastRemotingConnector::StartRemotingDataStreams(
RemotingBridge* bridge,
mojo::ScopedDataPipeConsumerHandle audio_pipe,
mojo::ScopedDataPipeConsumerHandle video_pipe,
media::mojom::RemotingDataStreamSenderRequest audio_sender_request,
media::mojom::RemotingDataStreamSenderRequest video_sender_request) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Refuse to start if there is no remoting route available, or if remoting is
// not active for this |bridge|.
if (!message_observer_ || active_bridge_ != bridge)
return;
// Also, if neither audio nor video pipe was provided, or if a request for a
// RemotingDataStreamSender was not provided for a data pipe, error-out early.
if ((!audio_pipe.is_valid() && !video_pipe.is_valid()) ||
(audio_pipe.is_valid() && !audio_sender_request.is_pending()) ||
(video_pipe.is_valid() && !video_sender_request.is_pending())) {
StopRemoting(active_bridge_, RemotingStopReason::DATA_SEND_FAILED);
return;
}
// Hold on to the data pipe handles and interface requests until one/both
// CastRemotingSenders are created and ready for use.
pending_audio_pipe_ = std::move(audio_pipe);
pending_video_pipe_ = std::move(video_pipe);
pending_audio_sender_request_ = std::move(audio_sender_request);
pending_video_sender_request_ = std::move(video_sender_request);
// Send a "start streams" message to the Cast Provider. The provider is
// responsible for creating and setting up a remoting Cast Streaming session
// that will result in new CastRemotingSender instances being created here in
// the browser process.
SendMessageToProvider(base::StringPrintf(
Messaging::kStartStreamsMessageFormat, session_counter_,
pending_audio_sender_request_.is_pending() ? 'Y' : 'N',
pending_video_sender_request_.is_pending() ? 'Y' : 'N'));
}
void CastRemotingConnector::StopRemoting(RemotingBridge* bridge,
RemotingStopReason reason) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (active_bridge_ != bridge)
return;
active_bridge_ = nullptr;
// Explicitly close the data pipes (and related requests) just in case the
// "start streams" operation was interrupted.
pending_audio_pipe_.reset();
pending_video_pipe_.reset();
pending_audio_sender_request_ = nullptr;
pending_video_sender_request_ = nullptr;
// Cancel all outstanding callbacks related to the remoting session.
weak_factory_.InvalidateWeakPtrs();
// Prevent the source from trying to start again until the Cast Provider has
// indicated the stop operation has completed.
bridge->OnSinkGone();
// Note: At this point, all sources should think the sink is gone.
SendMessageToProvider(base::StringPrintf(
Messaging::kStopRemotingMessageFormat, session_counter_));
// Note: Once the Cast Provider sends back an acknowledgement message, all
// sources will be notified that the remoting sink is available again.
bridge->OnStopped(reason);
}
void CastRemotingConnector::SendMessageToSink(
RemotingBridge* bridge, const std::vector<uint8_t>& message) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// During an active remoting session, simply pass all binary messages through
// to the sink.
if (!message_observer_ || active_bridge_ != bridge)
return;
media_router::MediaRoutesObserver::router()->SendRouteBinaryMessage(
message_observer_->route_id(),
base::MakeUnique<std::vector<uint8_t>>(message),
base::Bind(&CastRemotingConnector::HandleSendMessageResult,
weak_factory_.GetWeakPtr()));
}
void CastRemotingConnector::SendMessageToProvider(const std::string& message) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!message_observer_)
return;
if (active_bridge_) {
media_router::MediaRoutesObserver::router()->SendRouteMessage(
message_observer_->route_id(), message,
base::Bind(&CastRemotingConnector::HandleSendMessageResult,
weak_factory_.GetWeakPtr()));
} else {
struct Helper {
static void IgnoreSendMessageResult(bool ignored) {}
};
media_router::MediaRoutesObserver::router()->SendRouteMessage(
message_observer_->route_id(), message,
base::Bind(&Helper::IgnoreSendMessageResult));
}
}
void CastRemotingConnector::ProcessMessagesFromRoute(
const std::vector<media_router::RouteMessage>& messages) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Note: If any calls to message parsing functions are added/changed here,
// please update cast_remoting_connector_fuzzertest.cc as well!
for (const media_router::RouteMessage& message : messages) {
switch (message.type) {
case media_router::RouteMessage::TEXT:
// This is a notification message from the Cast Provider, about the
// execution state of the media remoting session between Chrome and the
// remote device.
DCHECK(message.text);
// If this is a "start streams" acknowledgement message, the
// CastRemotingSenders should now be available to begin consuming from
// the data pipes.
if (active_bridge_ &&
Messaging::IsMessageForSession(
*message.text,
Messaging::kStartedStreamsMessageFormatPartial,
session_counter_)) {
if (pending_audio_sender_request_.is_pending()) {
cast::CastRemotingSender::FindAndBind(
Messaging::GetStreamIdFromStartedMessage(
*message.text,
Messaging::kStartedStreamsMessageAudioIdSpecifier),
std::move(pending_audio_pipe_),
std::move(pending_audio_sender_request_),
base::Bind(&CastRemotingConnector::OnDataSendFailed,
weak_factory_.GetWeakPtr()));
}
if (pending_video_sender_request_.is_pending()) {
cast::CastRemotingSender::FindAndBind(
Messaging::GetStreamIdFromStartedMessage(
*message.text,
Messaging::kStartedStreamsMessageVideoIdSpecifier),
std::move(pending_video_pipe_),
std::move(pending_video_sender_request_),
base::Bind(&CastRemotingConnector::OnDataSendFailed,
weak_factory_.GetWeakPtr()));
}
break;
}
// If this is a failure message, call StopRemoting().
if (active_bridge_ &&
Messaging::IsMessageForSession(*message.text,
Messaging::kFailedMessageFormat,
session_counter_)) {
StopRemoting(active_bridge_, RemotingStopReason::UNEXPECTED_FAILURE);
break;
}
// If this is a stop acknowledgement message, indicating that the last
// session was stopped, notify all sources that the sink is once again
// available.
if (Messaging::IsMessageForSession(*message.text,
Messaging::kStoppedMessageFormat,
session_counter_)) {
if (active_bridge_) {
// Hmm...The Cast Provider was in a state that disagrees with this
// connector. Attempt to resolve this by shutting everything down to
// effectively reset to a known state.
LOG(WARNING) << "BUG: Cast Provider sent 'stopped' message during "
"an active remoting session.";
StopRemoting(active_bridge_,
RemotingStopReason::UNEXPECTED_FAILURE);
}
for (RemotingBridge* notifyee : bridges_)
notifyee->OnSinkAvailable(enabled_features_);
break;
}
LOG(WARNING) << "BUG: Unexpected message from Cast Provider: "
<< *message.text;
break;
case media_router::RouteMessage::BINARY: // This is for the source.
DCHECK(message.binary);
// All binary messages are passed through to the source during an active
// remoting session.
if (active_bridge_)
active_bridge_->OnMessageFromSink(*message.binary);
break;
}
}
}
void CastRemotingConnector::HandleSendMessageResult(bool success) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// A single message send failure is treated as fatal to an active remoting
// session.
if (!success && active_bridge_)
StopRemoting(active_bridge_, RemotingStopReason::MESSAGE_SEND_FAILED);
}
void CastRemotingConnector::OnDataSendFailed() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// A single data send failure is treated as fatal to an active remoting
// session.
if (active_bridge_)
StopRemoting(active_bridge_, RemotingStopReason::DATA_SEND_FAILED);
}
void CastRemotingConnector::OnRoutesUpdated(
const std::vector<media_router::MediaRoute>& routes,
const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// If a remoting route has already been identified, check that it still
// exists. Otherwise, shut down messaging and any active remoting, and notify
// the sources that remoting is no longer available.
if (message_observer_) {
for (const media_router::MediaRoute& route : routes) {
if (message_observer_->route_id() == route.media_route_id())
return; // Remoting route still exists. Take no further action.
}
message_observer_.reset();
if (active_bridge_)
StopRemoting(active_bridge_, RemotingStopReason::ROUTE_TERMINATED);
for (RemotingBridge* notifyee : bridges_)
notifyee->OnSinkGone();
}
// There shouldn't be an active RemotingBridge at this point, since there is
// currently no known remoting route.
DCHECK(!active_bridge_);
// Scan |routes| for a new remoting route. If one is found, begin processing
// messages on the route, and notify the sources that remoting is now
// available.
for (const media_router::MediaRoute& route : routes) {
if (route.media_source().id() != media_source_id_)
continue;
message_observer_.reset(new MessageObserver(
media_router::MediaRoutesObserver::router(), route.media_route_id(),
this));
// TODO(miu): In the future, scan the route ID for sink capabilities
// properties and pass these to the source in the OnSinkAvailable()
// notification.
for (RemotingBridge* notifyee : bridges_)
notifyee->OnSinkAvailable(enabled_features_);
break;
}
}