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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
content / browser / renderer_host / media / media_stream_ui_proxy.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "content/browser/renderer_host/render_frame_host_delegate.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/desktop_media_id.h"
#include "content/public/browser/media_stream_request.h"
#include "content/public/common/content_switches.h"
#include "media/capture/video/fake_video_capture_device.h"
#include "third_party/blink/public/common/mediastream/media_stream_request.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
#include "third_party/blink/public/mojom/permissions_policy/permissions_policy.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
bool IsFeatureEnabled(RenderFrameHost* rfh,
bool tests_use_fake_render_frame_hosts,
blink::mojom::PermissionsPolicyFeature feature) {
// Some tests don't (or can't) set up the RenderFrameHost. In these cases we
// just ignore permissions policy checks (there is no permissions policy to
// test).
if (!rfh && tests_use_fake_render_frame_hosts)
return true;
if (!rfh)
return false;
return rfh->IsFeatureEnabled(feature);
}
class MediaStreamUIProxy::Core {
public:
explicit Core(const base::WeakPtr<MediaStreamUIProxy>& proxy,
RenderFrameHostDelegate* test_render_delegate);
Core(const Core&) = delete;
Core& operator=(const Core&) = delete;
~Core();
void RequestAccess(std::unique_ptr<MediaStreamRequest> request);
void OnStarted(gfx::NativeViewId* window_id,
bool has_source_callback,
const std::string& label,
std::vector<DesktopMediaID> screen_capture_ids);
void OnDeviceStopped(const std::string& label,
const DesktopMediaID& media_id);
void OnDeviceStoppedForSourceChange(const std::string& label,
const DesktopMediaID& old_media_id,
const DesktopMediaID& new_media_id,
bool captured_surface_control_active);
void OnRegionCaptureRectChanged(
const std::optional<gfx::Rect>& region_capture_rect);
#if !BUILDFLAG(IS_ANDROID)
void SetFocus(const DesktopMediaID& media_id,
bool focus,
bool is_from_microtask,
bool is_from_timer);
#endif
// The type blink::mojom::StreamDevices is not movable, therefore stream
// devices cannot be captured for usage with PostTask.
void ProcessAccessRequestResponseForPostTask(
int render_process_id,
int render_frame_id,
blink::mojom::StreamDevicesSetPtr stream_devices_set_ptr,
blink::mojom::MediaStreamRequestResult result,
std::unique_ptr<MediaStreamUI> stream_ui);
void ProcessAccessRequestResponse(
int render_process_id,
int render_frame_id,
const blink::mojom::StreamDevicesSet& stream_devices_set,
blink::mojom::MediaStreamRequestResult result,
std::unique_ptr<MediaStreamUI> stream_ui);
base::WeakPtr<Core> GetWeakPtr() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// This weak pointer is created in the ctor, which runs on the IO thread.
// This pointer is always posted from the IO thread to the UI thread,
// meaning reading |weak_this_| happens on the IO thead, but dereferencing
// the actual pointer happens in the UI thread. Invalidation happens in the
// destructor, which runs on the UI thread, so this is safe.
return weak_this_;
}
private:
friend class FakeMediaStreamUIProxy;
void ProcessStopRequestFromUI();
void ProcessChangeSourceRequestFromUI(const DesktopMediaID& media_id,
bool captured_surface_control_active);
void ProcessStateChangeFromUI(const DesktopMediaID& media,
blink::mojom::MediaStreamStateChange);
RenderFrameHostDelegate* GetRenderFrameHostDelegate(int render_process_id,
int render_frame_id);
base::WeakPtr<MediaStreamUIProxy> proxy_;
std::unique_ptr<MediaStreamUI> ui_;
bool tests_use_fake_render_frame_hosts_;
const raw_ptr<RenderFrameHostDelegate> test_render_delegate_;
base::WeakPtr<Core> weak_this_;
base::WeakPtrFactory<Core> weak_factory_{this};
// Used for calls supplied to `ui_`. Invalidated every time a new UI is
// created.
base::WeakPtrFactory<Core> weak_factory_for_ui_{this};
};
MediaStreamUIProxy::Core::Core(const base::WeakPtr<MediaStreamUIProxy>& proxy,
RenderFrameHostDelegate* test_render_delegate)
: proxy_(proxy),
tests_use_fake_render_frame_hosts_(test_render_delegate != nullptr),
test_render_delegate_(test_render_delegate) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
weak_this_ = weak_factory_.GetWeakPtr();
}
MediaStreamUIProxy::Core::~Core() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
void MediaStreamUIProxy::Core::RequestAccess(
std::unique_ptr<MediaStreamRequest> request) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RenderFrameHostDelegate* render_delegate = GetRenderFrameHostDelegate(
request->render_process_id, request->render_frame_id);
// Tab may have gone away, or has no delegate from which to request access.
if (!render_delegate) {
ProcessAccessRequestResponse(
request->render_process_id, request->render_frame_id,
blink::mojom::StreamDevicesSet(),
blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
std::unique_ptr<MediaStreamUI>());
return;
}
render_delegate->RequestMediaAccessPermission(
*request,
base::BindOnce(&Core::ProcessAccessRequestResponse, weak_this_,
request->render_process_id, request->render_frame_id));
}
void MediaStreamUIProxy::Core::OnStarted(
gfx::NativeViewId* window_id,
bool has_source_callback,
const std::string& label,
std::vector<DesktopMediaID> screen_share_ids) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!ui_)
return;
MediaStreamUI::SourceCallback device_change_cb;
if (has_source_callback) {
device_change_cb =
base::BindRepeating(&Core::ProcessChangeSourceRequestFromUI,
weak_factory_for_ui_.GetWeakPtr());
}
*window_id =
ui_->OnStarted(base::BindRepeating(&Core::ProcessStopRequestFromUI,
weak_factory_for_ui_.GetWeakPtr()),
device_change_cb, label, screen_share_ids,
base::BindRepeating(&Core::ProcessStateChangeFromUI,
weak_factory_for_ui_.GetWeakPtr()));
}
void MediaStreamUIProxy::Core::OnDeviceStopped(const std::string& label,
const DesktopMediaID& media_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (ui_) {
ui_->OnDeviceStopped(label, media_id);
}
}
void MediaStreamUIProxy::Core::OnDeviceStoppedForSourceChange(
const std::string& label,
const DesktopMediaID& old_media_id,
const DesktopMediaID& new_media_id,
bool captured_surface_control_active) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (ui_) {
ui_->OnDeviceStoppedForSourceChange(label, old_media_id, new_media_id,
captured_surface_control_active);
ui_->OnDeviceStopped(label, old_media_id);
}
}
void MediaStreamUIProxy::Core::OnRegionCaptureRectChanged(
const std::optional<gfx::Rect>& region_capture_rec) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (ui_) {
ui_->OnRegionCaptureRectChanged(region_capture_rec);
}
}
#if !BUILDFLAG(IS_ANDROID)
void MediaStreamUIProxy::Core::SetFocus(const DesktopMediaID& media_id,
bool focus,
bool is_from_microtask,
bool is_from_timer) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (ui_) {
ui_->SetFocus(media_id, focus, is_from_microtask, is_from_timer);
}
}
#endif
void MediaStreamUIProxy::Core::ProcessAccessRequestResponseForPostTask(
int render_process_id,
int render_frame_id,
blink::mojom::StreamDevicesSetPtr stream_devices_set_ptr,
blink::mojom::MediaStreamRequestResult result,
std::unique_ptr<MediaStreamUI> stream_ui) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(stream_devices_set_ptr);
ProcessAccessRequestResponse(render_process_id, render_frame_id,
*stream_devices_set_ptr, result,
std::move(stream_ui));
}
void MediaStreamUIProxy::Core::ProcessAccessRequestResponse(
int render_process_id,
int render_frame_id,
const blink::mojom::StreamDevicesSet& stream_devices_set,
blink::mojom::MediaStreamRequestResult result,
std::unique_ptr<MediaStreamUI> stream_ui) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK((result != blink::mojom::MediaStreamRequestResult::OK &&
stream_devices_set.stream_devices.empty()) ||
(result == blink::mojom::MediaStreamRequestResult::OK &&
stream_devices_set.stream_devices.size() == 1u));
blink::mojom::StreamDevicesSetPtr filtered_devices_set =
blink::mojom::StreamDevicesSet::New();
blink::mojom::StreamDevices devices;
if (!stream_devices_set.stream_devices.empty()) {
devices = *stream_devices_set.stream_devices[0];
filtered_devices_set->stream_devices.emplace_back(
blink::mojom::StreamDevices::New());
}
auto* host = RenderFrameHostImpl::FromID(render_process_id, render_frame_id);
if (devices.audio_device.has_value()) {
const blink::MediaStreamDevice& audio_device = devices.audio_device.value();
if (audio_device.type !=
blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE ||
IsFeatureEnabled(host, tests_use_fake_render_frame_hosts_,
blink::mojom::PermissionsPolicyFeature::kMicrophone)) {
filtered_devices_set->stream_devices[0]->audio_device = audio_device;
}
}
if (devices.video_device.has_value()) {
const blink::MediaStreamDevice& video_device = devices.video_device.value();
if (video_device.type !=
blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE ||
IsFeatureEnabled(host, tests_use_fake_render_frame_hosts_,
blink::mojom::PermissionsPolicyFeature::kCamera)) {
filtered_devices_set->stream_devices[0]->video_device = video_device;
}
}
if ((filtered_devices_set->stream_devices.empty() ||
(!filtered_devices_set->stream_devices[0]->audio_device.has_value() &&
!filtered_devices_set->stream_devices[0]->video_device.has_value())) &&
result == blink::mojom::MediaStreamRequestResult::OK) {
result = blink::mojom::MediaStreamRequestResult::PERMISSION_DENIED;
filtered_devices_set->stream_devices.clear();
}
if (stream_ui) {
// Callbacks that were supplied to the existing `ui_` are no longer
// applicable. This is important as some implementions (TabSharingUIViews)
// always run the callback when destroyed. However at the point the UI is
// replaced while the screencast is ongoing. Invalidating ensures the
// screencast is not terminated. See crbug.com/1155426 for details.
weak_factory_for_ui_.InvalidateWeakPtrs();
ui_ = std::move(stream_ui);
}
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&MediaStreamUIProxy::ProcessAccessRequestResponse, proxy_,
std::move(filtered_devices_set), result));
}
void MediaStreamUIProxy::Core::ProcessStopRequestFromUI() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&MediaStreamUIProxy::ProcessStopRequestFromUI, proxy_));
}
void MediaStreamUIProxy::Core::ProcessChangeSourceRequestFromUI(
const DesktopMediaID& media_id,
bool captured_surface_control_active) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&MediaStreamUIProxy::ProcessChangeSourceRequestFromUI,
proxy_, media_id, captured_surface_control_active));
}
void MediaStreamUIProxy::Core::ProcessStateChangeFromUI(
const DesktopMediaID& media_id,
blink::mojom::MediaStreamStateChange new_state) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&MediaStreamUIProxy::ProcessStateChangeFromUI,
proxy_, media_id, new_state));
}
RenderFrameHostDelegate* MediaStreamUIProxy::Core::GetRenderFrameHostDelegate(
int render_process_id,
int render_frame_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (test_render_delegate_)
return test_render_delegate_;
RenderFrameHostImpl* host =
RenderFrameHostImpl::FromID(render_process_id, render_frame_id);
return host ? host->delegate() : nullptr;
}
// static
std::unique_ptr<MediaStreamUIProxy> MediaStreamUIProxy::Create() {
return std::unique_ptr<MediaStreamUIProxy>(new MediaStreamUIProxy(nullptr));
}
// static
std::unique_ptr<MediaStreamUIProxy> MediaStreamUIProxy::CreateForTests(
RenderFrameHostDelegate* render_delegate) {
return std::unique_ptr<MediaStreamUIProxy>(
new MediaStreamUIProxy(render_delegate));
}
MediaStreamUIProxy::MediaStreamUIProxy(
RenderFrameHostDelegate* test_render_delegate) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
core_.reset(new Core(weak_factory_.GetWeakPtr(), test_render_delegate));
}
MediaStreamUIProxy::~MediaStreamUIProxy() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
}
void MediaStreamUIProxy::RequestAccess(
std::unique_ptr<MediaStreamRequest> request,
ResponseCallback response_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
response_callback_ = std::move(response_callback);
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&Core::RequestAccess, core_->GetWeakPtr(),
std::move(request)));
}
void MediaStreamUIProxy::OnStarted(
base::OnceClosure stop_callback,
MediaStreamUI::SourceCallback source_callback,
WindowIdCallback window_id_callback,
const std::string& label,
std::vector<DesktopMediaID> screen_share_ids,
MediaStreamUI::StateChangeCallback state_change_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
stop_callback_ = std::move(stop_callback);
source_callback_ = std::move(source_callback);
state_change_callback_ = std::move(state_change_callback);
// Owned by the PostTaskAndReply callback.
gfx::NativeViewId* window_id = new gfx::NativeViewId(0);
GetUIThreadTaskRunner({})->PostTaskAndReply(
FROM_HERE,
base::BindOnce(&Core::OnStarted, core_->GetWeakPtr(), window_id,
!!source_callback_, label, screen_share_ids),
base::BindOnce(&MediaStreamUIProxy::OnWindowId,
weak_factory_.GetWeakPtr(), std::move(window_id_callback),
base::Owned(window_id)));
}
void MediaStreamUIProxy::OnDeviceStopped(const std::string& label,
const DesktopMediaID& media_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&Core::OnDeviceStopped, core_->GetWeakPtr(),
label, media_id));
}
void MediaStreamUIProxy::OnDeviceStoppedForSourceChange(
const std::string& label,
const DesktopMediaID& old_media_id,
const DesktopMediaID& new_media_id,
bool captured_surface_control_active) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&Core::OnDeviceStoppedForSourceChange,
core_->GetWeakPtr(), label, old_media_id,
new_media_id, captured_surface_control_active));
}
void MediaStreamUIProxy::OnRegionCaptureRectChanged(
const std::optional<gfx::Rect>& region_capture_rec) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&Core::OnRegionCaptureRectChanged,
core_->GetWeakPtr(), region_capture_rec));
}
#if !BUILDFLAG(IS_ANDROID)
void MediaStreamUIProxy::SetFocus(const DesktopMediaID& media_id,
bool focus,
bool is_from_microtask,
bool is_from_timer) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&Core::SetFocus, core_->GetWeakPtr(), media_id,
focus, is_from_microtask, is_from_timer));
}
#endif
void MediaStreamUIProxy::ProcessAccessRequestResponse(
blink::mojom::StreamDevicesSetPtr stream_devices_set,
blink::mojom::MediaStreamRequestResult result) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(!response_callback_.is_null());
std::move(response_callback_).Run(*stream_devices_set, result);
}
void MediaStreamUIProxy::ProcessStopRequestFromUI() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// Careful when changing the following lines: upstream, this function is
// wrapped into a RepeatingClosure, which allows duplicating it and enabling
// multiple potentital sources to stop the stream; however only the first
// invocation should actually stop the stream.
if (stop_callback_)
std::move(stop_callback_).Run();
}
void MediaStreamUIProxy::ProcessChangeSourceRequestFromUI(
const DesktopMediaID& media_id,
bool captured_surface_control_active) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (source_callback_)
source_callback_.Run(media_id, captured_surface_control_active);
}
void MediaStreamUIProxy::ProcessStateChangeFromUI(
const DesktopMediaID& media_id,
blink::mojom::MediaStreamStateChange new_state) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (state_change_callback_)
state_change_callback_.Run(media_id, new_state);
}
void MediaStreamUIProxy::OnWindowId(WindowIdCallback window_id_callback,
gfx::NativeViewId* window_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!window_id_callback.is_null())
std::move(window_id_callback).Run(*window_id);
}
FakeMediaStreamUIProxy::FakeMediaStreamUIProxy(
bool tests_use_fake_render_frame_hosts)
: MediaStreamUIProxy(nullptr) {
core_->tests_use_fake_render_frame_hosts_ = tests_use_fake_render_frame_hosts;
}
FakeMediaStreamUIProxy::~FakeMediaStreamUIProxy() = default;
void FakeMediaStreamUIProxy::SetAvailableDevices(
const blink::MediaStreamDevices& devices) {
devices_ = devices;
}
void FakeMediaStreamUIProxy::SetMicAccess(bool access) {
mic_access_ = access;
}
void FakeMediaStreamUIProxy::SetCameraAccess(bool access) {
camera_access_ = access;
}
void FakeMediaStreamUIProxy::SetAudioShare(bool audio_share) {
audio_share_ = audio_share;
}
void FakeMediaStreamUIProxy::RequestAccess(
std::unique_ptr<MediaStreamRequest> request,
ResponseCallback response_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
response_callback_ = std::move(response_callback);
if (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kUseFakeUIForMediaStream) == "deny") {
// Immediately deny the request.
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
&MediaStreamUIProxy::Core::ProcessAccessRequestResponseForPostTask,
core_->GetWeakPtr(), request->render_process_id,
request->render_frame_id, blink::mojom::StreamDevicesSet::New(),
blink::mojom::MediaStreamRequestResult::PERMISSION_DENIED,
std::unique_ptr<MediaStreamUI>()));
return;
}
// Use the first capture device of the same media type in the list for the
// fake UI.
blink::mojom::StreamDevicesSetPtr devices_set_to_use =
blink::mojom::StreamDevicesSet::New();
devices_set_to_use->stream_devices.emplace_back(
blink::mojom::StreamDevices::New());
blink::mojom::StreamDevices& devices_to_use =
*devices_set_to_use->stream_devices[0];
for (const blink::MediaStreamDevice& device : devices_) {
if (!devices_to_use.audio_device.has_value() &&
blink::IsAudioInputMediaType(request->audio_type) &&
blink::IsAudioInputMediaType(device.type) &&
(request->requested_audio_device_ids.empty() ||
request->requested_audio_device_ids.front().empty() ||
request->requested_audio_device_ids.front() == device.id)) {
devices_to_use.audio_device = device;
} else if (!devices_to_use.video_device.has_value() &&
blink::IsVideoInputMediaType(request->video_type) &&
blink::IsVideoInputMediaType(device.type) &&
(request->requested_video_device_ids.empty() ||
request->requested_video_device_ids.front().empty() ||
request->requested_video_device_ids.front() == device.id)) {
devices_to_use.video_device = device;
}
}
// Fail the request if a device doesn't exist for the requested type.
if ((request->audio_type != blink::mojom::MediaStreamType::NO_SERVICE &&
!devices_to_use.audio_device.has_value()) ||
(request->video_type != blink::mojom::MediaStreamType::NO_SERVICE &&
!devices_to_use.video_device.has_value())) {
devices_to_use = blink::mojom::StreamDevices();
}
if (!audio_share_) {
devices_to_use.audio_device = std::nullopt;
}
const bool is_devices_empty = !devices_to_use.audio_device.has_value() &&
!devices_to_use.video_device.has_value();
if (is_devices_empty) {
devices_set_to_use->stream_devices.clear();
}
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
&MediaStreamUIProxy::Core::ProcessAccessRequestResponseForPostTask,
core_->GetWeakPtr(), request->render_process_id,
request->render_frame_id, std::move(devices_set_to_use),
is_devices_empty ? blink::mojom::MediaStreamRequestResult::NO_HARDWARE
: blink::mojom::MediaStreamRequestResult::OK,
std::unique_ptr<MediaStreamUI>()));
}
void FakeMediaStreamUIProxy::OnStarted(
base::OnceClosure stop_callback,
MediaStreamUI::SourceCallback source_callback,
WindowIdCallback window_id_callback,
const std::string& label,
std::vector<DesktopMediaID> screen_share_ids,
MediaStreamUI::StateChangeCallback state_change_callback) {}
void FakeMediaStreamUIProxy::OnDeviceStopped(const std::string& label,
const DesktopMediaID& media_id) {}
void FakeMediaStreamUIProxy::OnDeviceStoppedForSourceChange(
const std::string& label,
const DesktopMediaID& old_media_id,
const DesktopMediaID& new_media_id,
bool captured_surface_control_active) {}
} // namespace content