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
|
// Copyright (c) 2012 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 "content/browser/renderer_host/media/media_stream_manager.h"
#include <list>
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/rand_util.h"
#include "content/browser/renderer_host/media/audio_input_device_manager.h"
#include "content/browser/renderer_host/media/media_stream_device_settings.h"
#include "content/browser/renderer_host/media/media_stream_requester.h"
#include "content/browser/renderer_host/media/video_capture_manager.h"
#include "content/browser/resource_context_impl.h"
#include "content/common/media/media_stream_options.h"
#include "content/public/browser/browser_thread.h"
#include "media/audio/audio_manager.h"
using content::BrowserThread;
static const char* kMediaStreamManagerKeyName = "content_media_stream_manager";
namespace media_stream {
// Creates a random label used to identify requests.
static std::string RandomLabel() {
// Alphabet according to WhatWG standard, i.e. containing 36 characters from
// range: U+0021, U+0023 to U+0027, U+002A to U+002B, U+002D to U+002E,
// U+0030 to U+0039, U+0041 to U+005A, U+005E to U+007E.
static const char alphabet[] = "!#$%&\'*+-.0123456789"
"abcdefghijklmnopqrstuvwxyz^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~";
std::string label(36, ' ');
for (size_t i = 0; i < label.size(); ++i) {
int random_char = base::RandGenerator(sizeof(alphabet) - 1);
label[i] = alphabet[random_char];
}
return label;
}
// Helper to verify if a media stream type is part of options or not.
static bool Requested(const StreamOptions& options,
MediaStreamType stream_type) {
if (stream_type == content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE &&
(options.video_option != StreamOptions::kNoCamera)) {
return true;
} else if (stream_type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE &&
options.audio == true) {
return true;
}
return false;
}
struct MediaStreamManager::DeviceRequest {
enum RequestState {
kNotRequested = 0,
kRequested,
kPendingApproval,
kOpening,
kDone,
kError
};
enum RequestType {
kGenerateStream = 0,
kEnumerateDevices,
kOpenDevice
};
DeviceRequest()
: requester(NULL),
state(content::NUM_MEDIA_STREAM_DEVICE_TYPES, kNotRequested),
type(kGenerateStream) {
options.audio = false;
options.video_option = StreamOptions::kNoCamera;
}
DeviceRequest(MediaStreamRequester* requester,
const StreamOptions& request_options)
: requester(requester),
options(request_options),
state(content::NUM_MEDIA_STREAM_DEVICE_TYPES, kNotRequested),
type(kGenerateStream) {
DCHECK(requester);
}
~DeviceRequest() {}
MediaStreamRequester* requester;
StreamOptions options;
std::vector<RequestState> state;
RequestType type;
std::string requested_device_id;
StreamDeviceInfoArray audio_devices;
StreamDeviceInfoArray video_devices;
};
// static
MediaStreamManager* MediaStreamManager::GetForResourceContext(
content::ResourceContext* resource_context,
AudioManager* audio_manager) {
MediaStreamManager* rv = static_cast<MediaStreamManager*>(
resource_context->GetUserData(kMediaStreamManagerKeyName));
if (!rv) {
rv = new MediaStreamManager(audio_manager);
resource_context->SetUserData(kMediaStreamManagerKeyName, rv);
}
return rv;
}
MediaStreamManager::MediaStreamManager(AudioManager* audio_manager)
: ALLOW_THIS_IN_INITIALIZER_LIST(
device_settings_(new MediaStreamDeviceSettings(this))),
enumeration_in_progress_(content::NUM_MEDIA_STREAM_DEVICE_TYPES, false),
audio_manager_(audio_manager) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
}
MediaStreamManager::~MediaStreamManager() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (video_capture_manager_.get())
video_capture_manager_->Unregister();
if (audio_input_device_manager_.get())
audio_input_device_manager_->Unregister();
}
VideoCaptureManager* MediaStreamManager::video_capture_manager() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!video_capture_manager_.get()) {
video_capture_manager_ = new VideoCaptureManager();
video_capture_manager_->Register(this);
}
return video_capture_manager_.get();
}
AudioInputDeviceManager* MediaStreamManager::audio_input_device_manager() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!audio_input_device_manager_.get()) {
audio_input_device_manager_ = new AudioInputDeviceManager(audio_manager_);
audio_input_device_manager_->Register(this);
}
return audio_input_device_manager_.get();
}
void MediaStreamManager::GenerateStream(MediaStreamRequester* requester,
int render_process_id,
int render_view_id,
const StreamOptions& options,
const std::string& security_origin,
std::string* label) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Create a new request based on options.
DeviceRequest new_request = DeviceRequest(requester, options);
StartEnumeration(&new_request, render_process_id, render_view_id,
security_origin, label);
}
void MediaStreamManager::CancelRequests(MediaStreamRequester* requester) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DeviceRequests::iterator it = requests_.begin();
while (it != requests_.end()) {
if (it->second.requester == requester && !RequestDone(it->second)) {
// The request isn't complete, but there might be some devices already
// opened -> close them.
DeviceRequest* request = &(it->second);
if (request->state[content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE] ==
DeviceRequest::kOpening) {
for (StreamDeviceInfoArray::iterator it =
request->audio_devices.begin(); it != request->audio_devices.end();
++it) {
if (it->in_use == true) {
audio_input_device_manager()->Close(it->session_id);
}
}
}
if (request->state[content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE] ==
DeviceRequest::kOpening) {
for (StreamDeviceInfoArray::iterator it =
request->video_devices.begin(); it != request->video_devices.end();
++it) {
if (it->in_use == true) {
video_capture_manager()->Close(it->session_id);
}
}
}
requests_.erase(it++);
} else {
++it;
}
}
}
void MediaStreamManager::StopGeneratedStream(const std::string& label) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Find the request and close all open devices for the request.
DeviceRequests::iterator it = requests_.find(label);
if (it != requests_.end()) {
for (StreamDeviceInfoArray::iterator audio_it =
it->second.audio_devices.begin();
audio_it != it->second.audio_devices.end(); ++audio_it) {
audio_input_device_manager()->Close(audio_it->session_id);
}
for (StreamDeviceInfoArray::iterator video_it =
it->second.video_devices.begin();
video_it != it->second.video_devices.end(); ++video_it) {
video_capture_manager()->Close(video_it->session_id);
}
requests_.erase(it);
return;
}
}
void MediaStreamManager::EnumerateDevices(
MediaStreamRequester* requester,
int render_process_id,
int render_view_id,
MediaStreamType type,
const std::string& security_origin,
std::string* label) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Create a new request.
StreamOptions options;
if (type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE)
options.audio = true;
else
options.video_option = StreamOptions::kFacingUser;
DeviceRequest new_request = DeviceRequest(requester, options);
new_request.type = DeviceRequest::kEnumerateDevices;
StartEnumeration(&new_request, render_process_id, render_view_id,
security_origin, label);
}
void MediaStreamManager::OpenDevice(
MediaStreamRequester* requester,
int render_process_id,
int render_view_id,
const std::string& device_id,
MediaStreamType type,
const std::string& security_origin,
std::string* label) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Create a new request.
StreamOptions options;
if (type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE)
options.audio = true;
else
options.video_option = StreamOptions::kFacingUser;
DeviceRequest new_request = DeviceRequest(requester, options);
new_request.type = DeviceRequest::kOpenDevice;
new_request.requested_device_id = device_id;
StartEnumeration(&new_request, render_process_id, render_view_id,
security_origin, label);
}
void MediaStreamManager::StartEnumeration(
DeviceRequest* new_request,
int render_process_id,
int render_view_id,
const std::string& security_origin,
std::string* label) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
MediaStreamType stream_type = content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE;
if (Requested(new_request->options, stream_type)) {
new_request->state[stream_type] = DeviceRequest::kRequested;
if (!enumeration_in_progress_[stream_type]) {
enumeration_in_progress_[stream_type] = true;
GetDeviceManager(stream_type)->EnumerateDevices();
}
}
stream_type = content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE;
if (Requested(new_request->options, stream_type)) {
new_request->state[stream_type] = DeviceRequest::kRequested;
if (!enumeration_in_progress_[stream_type]) {
enumeration_in_progress_[stream_type] = true;
GetDeviceManager(stream_type)->EnumerateDevices();
}
}
// Create a label for this request and verify it is unique.
std::string request_label;
do {
request_label = RandomLabel();
} while (requests_.find(request_label) != requests_.end());
requests_.insert(std::make_pair(request_label, *new_request));
// Get user confirmation to use capture devices.
// Need to make an asynchronous call to make sure the |requester| gets the
// |label| before it would receive any event.
if (new_request->type == DeviceRequest::kGenerateStream) {
BrowserThread::PostTask(BrowserThread::IO,
FROM_HERE,
base::Bind(&MediaStreamDeviceSettings::RequestCaptureDeviceUsage,
base::Unretained(device_settings_.get()),
request_label, render_process_id,
render_view_id, new_request->options,
security_origin));
}
(*label) = request_label;
}
void MediaStreamManager::Opened(MediaStreamType stream_type,
int capture_session_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Find the request containing this device and mark it as used.
DeviceRequest* request = NULL;
StreamDeviceInfoArray* devices = NULL;
std::string label;
for (DeviceRequests::iterator request_it = requests_.begin();
request_it != requests_.end() && request == NULL; ++request_it) {
if (stream_type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE) {
devices = &(request_it->second.audio_devices);
} else if (stream_type == content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE) {
devices = &(request_it->second.video_devices);
} else {
NOTREACHED();
}
for (StreamDeviceInfoArray::iterator device_it = devices->begin();
device_it != devices->end(); ++device_it) {
if (device_it->session_id == capture_session_id) {
// We've found the request.
device_it->in_use = true;
label = request_it->first;
request = &(request_it->second);
break;
}
}
}
if (request == NULL) {
// The request doesn't exist.
return;
}
DCHECK_NE(request->state[stream_type], DeviceRequest::kRequested);
// Check if all devices for this stream type are opened. Update the state if
// they are.
for (StreamDeviceInfoArray::iterator device_it = devices->begin();
device_it != devices->end(); ++device_it) {
if (device_it->in_use == false) {
// Wait for more devices to be opened before we're done.
return;
}
}
request->state[stream_type] = DeviceRequest::kDone;
if (!RequestDone(*request)) {
// This stream_type is done, but not the other type.
return;
}
switch (request->type) {
case DeviceRequest::kOpenDevice:
request->requester->DeviceOpened(label, (*devices)[0]);
break;
case DeviceRequest::kGenerateStream:
request->requester->StreamGenerated(label, request->audio_devices,
request->video_devices);
break;
default:
NOTREACHED();
}
}
void MediaStreamManager::Closed(MediaStreamType stream_type,
int capture_session_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
}
void MediaStreamManager::DevicesEnumerated(
MediaStreamType stream_type, const StreamDeviceInfoArray& devices) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Publish the result for all requests waiting for device list(s).
// Find the requests waiting for this device list, store their labels and
// release the iterator before calling device settings. We might get a call
// back from device_settings that will need to iterate through devices.
std::list<std::string> label_list;
for (DeviceRequests::iterator it = requests_.begin(); it != requests_.end();
++it) {
if (it->second.state[stream_type] == DeviceRequest::kRequested &&
Requested(it->second.options, stream_type)) {
it->second.state[stream_type] = DeviceRequest::kPendingApproval;
label_list.push_back(it->first);
}
}
for (std::list<std::string>::iterator it = label_list.begin();
it != label_list.end(); ++it) {
DeviceRequest& request = requests_[*it];
switch (request.type) {
case DeviceRequest::kEnumerateDevices:
request.requester->DevicesEnumerated(*it, devices);
requests_.erase(*it);
break;
case DeviceRequest::kOpenDevice:
for (StreamDeviceInfoArray::const_iterator device_it = devices.begin();
device_it != devices.end(); device_it++) {
if (request.requested_device_id == device_it->device_id) {
StreamDeviceInfo device = *device_it;
device.in_use = false;
device.session_id =
GetDeviceManager(device_it->stream_type)->Open(device);
request.state[device_it->stream_type] = DeviceRequest::kOpening;
if (stream_type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE)
request.audio_devices.push_back(device);
else
request.video_devices.push_back(device);
break;
}
}
break;
default:
BrowserThread::PostTask(BrowserThread::IO,
FROM_HERE,
base::Bind(&MediaStreamDeviceSettings::AvailableDevices,
base::Unretained(device_settings_.get()),
*it, stream_type, devices));
}
}
label_list.clear();
enumeration_in_progress_[stream_type] = false;
}
void MediaStreamManager::Error(MediaStreamType stream_type,
int capture_session_id,
MediaStreamProviderError error) {
// Find the device for the error call.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
for (DeviceRequests::iterator it = requests_.begin(); it != requests_.end();
++it) {
StreamDeviceInfoArray* devices = NULL;
if (stream_type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE) {
devices = &(it->second.audio_devices);
} else if (stream_type == content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE) {
devices = &(it->second.video_devices);
} else {
NOTREACHED();
}
int device_idx = 0;
for (StreamDeviceInfoArray::iterator device_it = devices->begin();
device_it != devices->end(); ++device_it, ++device_idx) {
if (device_it->session_id == capture_session_id) {
// We've found the failing device. Find the error case:
if (it->second.state[stream_type] == DeviceRequest::kDone) {
// 1. Already opened -> signal device failure and close device.
// Use device_idx to signal which of the devices encountered an
// error.
if (stream_type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE) {
it->second.requester->AudioDeviceFailed(it->first, device_idx);
} else if (stream_type ==
content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE) {
it->second.requester->VideoDeviceFailed(it->first, device_idx);
}
GetDeviceManager(stream_type)->Close(capture_session_id);
devices->erase(device_it);
} else if (it->second.audio_devices.size()
+ it->second.video_devices.size() <= 1) {
// 2. Device not opened and no other devices for this request ->
// signal stream error and remove the request.
it->second.requester->StreamGenerationFailed(it->first);
requests_.erase(it);
} else {
// 3. Not opened but other devices exists for this request -> remove
// device from list, but don't signal an error.
devices->erase(device_it);
}
return;
}
}
}
}
void MediaStreamManager::DevicesAccepted(const std::string& label,
const StreamDeviceInfoArray& devices) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DeviceRequests::iterator request_it = requests_.find(label);
if (request_it != requests_.end()) {
if (devices.empty()) {
// No available devices or user didn't accept device usage.
request_it->second.requester->StreamGenerationFailed(request_it->first);
requests_.erase(request_it);
return;
}
// Loop through all device types for this request.
for (StreamDeviceInfoArray::const_iterator device_it = devices.begin();
device_it != devices.end(); ++device_it) {
StreamDeviceInfo device_info = *device_it;
// Set in_use to false to be able to track if this device has been
// opened. in_use might be true if the device type can be used in more
// than one session.
DCHECK_EQ(request_it->second.state[device_it->stream_type],
DeviceRequest::kPendingApproval);
device_info.in_use = false;
device_info.session_id =
GetDeviceManager(device_info.stream_type)->Open(device_info);
request_it->second.state[device_it->stream_type] =
DeviceRequest::kOpening;
if (device_info.stream_type ==
content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE) {
request_it->second.audio_devices.push_back(device_info);
} else if (device_info.stream_type ==
content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE) {
request_it->second.video_devices.push_back(device_info);
} else {
NOTREACHED();
}
}
// Check if we received all stream types requested.
MediaStreamType stream_type =
content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE;
if (Requested(request_it->second.options, stream_type) &&
request_it->second.audio_devices.size() == 0) {
request_it->second.state[stream_type] = DeviceRequest::kError;
}
stream_type = content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE;
if (Requested(request_it->second.options, stream_type) &&
request_it->second.video_devices.size() == 0) {
request_it->second.state[stream_type] = DeviceRequest::kError;
}
return;
}
}
void MediaStreamManager::SettingsError(const std::string& label) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Erase this request and report an error.
DeviceRequests::iterator it = requests_.find(label);
if (it != requests_.end()) {
DCHECK_EQ(it->second.type, DeviceRequest::kGenerateStream);
it->second.requester->StreamGenerationFailed(label);
requests_.erase(it);
return;
}
}
void MediaStreamManager::UseFakeDevice() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
video_capture_manager()->UseFakeDevice();
device_settings_->UseFakeUI();
}
bool MediaStreamManager::RequestDone(const DeviceRequest& request) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Check if all devices are opened.
if (Requested(request.options,
content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE)) {
for (StreamDeviceInfoArray::const_iterator it =
request.audio_devices.begin(); it != request.audio_devices.end();
++it) {
if (it->in_use == false) {
return false;
}
}
}
if (Requested(request.options,
content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE)) {
for (StreamDeviceInfoArray::const_iterator it =
request.video_devices.begin(); it != request.video_devices.end();
++it) {
if (it->in_use == false) {
return false;
}
}
}
return true;
}
// Called to get media capture device manager of specified type.
MediaStreamProvider* MediaStreamManager::GetDeviceManager(
MediaStreamType stream_type) {
if (stream_type == content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE) {
return video_capture_manager();
} else if (stream_type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE) {
return audio_input_device_manager();
}
NOTREACHED();
return NULL;
}
} // namespace media_stream
|