Skip to content

Commit e7d3a58

Browse files
committed
Support media enumerable API from upstream 30
sync with upstream c129874dc579801d403c521fe0ebe38fbc1cf2f2 Fix nwjs#632
1 parent cc23d50 commit e7d3a58

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

src/media/media_capture_devices_dispatcher.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include "base/memory/singleton.h"
6+
57
#include "content/nw/src/media/media_capture_devices_dispatcher.h"
68

79
#include "content/public/browser/browser_thread.h"
@@ -11,6 +13,23 @@
1113
using content::BrowserThread;
1214
using content::MediaStreamDevices;
1315

16+
namespace {
17+
18+
// Finds a device in |devices| that has |device_id|, or NULL if not found.
19+
const content::MediaStreamDevice* FindDeviceWithId(
20+
const content::MediaStreamDevices& devices,
21+
const std::string& device_id) {
22+
content::MediaStreamDevices::const_iterator iter = devices.begin();
23+
for (; iter != devices.end(); ++iter) {
24+
if (iter->id == device_id) {
25+
return &(*iter);
26+
}
27+
}
28+
return NULL;
29+
};
30+
31+
}
32+
1433
MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher()
1534
: devices_enumerated_(false) {}
1635

@@ -107,3 +126,45 @@ void MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread(
107126
FOR_EACH_OBSERVER(Observer, observers_,
108127
OnCreatingAudioStream(render_process_id, render_view_id));
109128
}
129+
130+
const content::MediaStreamDevice*
131+
MediaCaptureDevicesDispatcher::GetRequestedAudioDevice(
132+
const std::string& requested_audio_device_id) {
133+
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
134+
const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices();
135+
const content::MediaStreamDevice* const device =
136+
FindDeviceWithId(audio_devices, requested_audio_device_id);
137+
return device;
138+
}
139+
140+
const content::MediaStreamDevice*
141+
MediaCaptureDevicesDispatcher::GetFirstAvailableAudioDevice() {
142+
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
143+
const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices();
144+
if (audio_devices.empty())
145+
return NULL;
146+
return &(*audio_devices.begin());
147+
}
148+
149+
const content::MediaStreamDevice*
150+
MediaCaptureDevicesDispatcher::GetRequestedVideoDevice(
151+
const std::string& requested_video_device_id) {
152+
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
153+
const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices();
154+
const content::MediaStreamDevice* const device =
155+
FindDeviceWithId(video_devices, requested_video_device_id);
156+
return device;
157+
}
158+
159+
const content::MediaStreamDevice*
160+
MediaCaptureDevicesDispatcher::GetFirstAvailableVideoDevice() {
161+
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
162+
const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices();
163+
if (video_devices.empty())
164+
return NULL;
165+
return &(*video_devices.begin());
166+
}
167+
168+
MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() {
169+
return Singleton<MediaCaptureDevicesDispatcher>::get();
170+
}

src/media/media_capture_devices_dispatcher.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ class MediaCaptureDevicesDispatcher
3333
virtual ~Observer() {}
3434
};
3535

36+
static MediaCaptureDevicesDispatcher* GetInstance();
37+
3638
MediaCaptureDevicesDispatcher();
39+
virtual ~MediaCaptureDevicesDispatcher();
3740

3841
// Called on IO thread when one audio device is plugged in or unplugged.
3942
void AudioCaptureDevicesChanged(const content::MediaStreamDevices& devices);
@@ -52,9 +55,21 @@ class MediaCaptureDevicesDispatcher
5255
const content::MediaStreamDevices& GetAudioCaptureDevices();
5356
const content::MediaStreamDevices& GetVideoCaptureDevices();
5457

58+
// Helpers for picking particular requested devices, identified by raw id.
59+
// If the device requested is not available it will return NULL.
60+
const content::MediaStreamDevice*
61+
GetRequestedAudioDevice(const std::string& requested_audio_device_id);
62+
const content::MediaStreamDevice*
63+
GetRequestedVideoDevice(const std::string& requested_video_device_id);
64+
65+
// Returns the first available audio or video device, or NULL if no devices
66+
// are available.
67+
const content::MediaStreamDevice* GetFirstAvailableAudioDevice();
68+
const content::MediaStreamDevice* GetFirstAvailableVideoDevice();
69+
70+
5571
private:
5672
friend class base::RefCountedThreadSafe<MediaCaptureDevicesDispatcher>;
57-
virtual ~MediaCaptureDevicesDispatcher();
5873

5974
// Called by the public Audio/VideoCaptureDevicesChanged() functions,
6075
// executed on UI thread.

src/media/media_internals.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,5 @@ MediaInternals::GetMediaCaptureDevicesDispatcher() {
125125
}
126126

127127
MediaInternals::MediaInternals()
128-
: media_devices_dispatcher_(new MediaCaptureDevicesDispatcher()) {
128+
: media_devices_dispatcher_(MediaCaptureDevicesDispatcher::GetInstance()) {
129129
}

src/media/media_stream_devices_controller.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,78 @@ const std::string& MediaStreamDevicesController::GetSecurityOriginSpec() const {
115115
void MediaStreamDevicesController::Accept(bool update_content_setting) {
116116
// Get the default devices for the request.
117117
content::MediaStreamDevices devices;
118+
switch (request_.request_type) {
119+
case content::MEDIA_OPEN_DEVICE: {
120+
const content::MediaStreamDevice* device = NULL;
121+
// For open device request pick the desired device or fall back to the
122+
// first available of the given type.
123+
if (request_.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) {
124+
device = MediaCaptureDevicesDispatcher::GetInstance()->
125+
GetRequestedAudioDevice(request_.requested_audio_device_id);
126+
// TODO(wjia): Confirm this is the intended behavior.
127+
if (!device) {
128+
device = MediaCaptureDevicesDispatcher::GetInstance()->
129+
GetFirstAvailableAudioDevice();
130+
}
131+
} else if (request_.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE) {
132+
// Pepper API opens only one device at a time.
133+
device = MediaCaptureDevicesDispatcher::GetInstance()->
134+
GetRequestedVideoDevice(request_.requested_video_device_id);
135+
// TODO(wjia): Confirm this is the intended behavior.
136+
if (!device) {
137+
device = MediaCaptureDevicesDispatcher::GetInstance()->
138+
GetFirstAvailableVideoDevice();
139+
}
140+
}
141+
if (device)
142+
devices.push_back(*device);
143+
break;
144+
} case content::MEDIA_GENERATE_STREAM: {
145+
bool needs_audio_device = has_audio_;
146+
bool needs_video_device = has_video_;
147+
148+
// Get the exact audio or video device if an id is specified.
149+
if (!request_.requested_audio_device_id.empty()) {
150+
const content::MediaStreamDevice* audio_device =
151+
MediaCaptureDevicesDispatcher::GetInstance()->
152+
GetRequestedAudioDevice(request_.requested_audio_device_id);
153+
if (audio_device) {
154+
devices.push_back(*audio_device);
155+
needs_audio_device = false;
156+
}
157+
}
158+
if (!request_.requested_video_device_id.empty()) {
159+
const content::MediaStreamDevice* video_device =
160+
MediaCaptureDevicesDispatcher::GetInstance()->
161+
GetRequestedVideoDevice(request_.requested_video_device_id);
162+
if (video_device) {
163+
devices.push_back(*video_device);
164+
needs_video_device = false;
165+
}
166+
}
167+
168+
// If either or both audio and video devices were requested but not
169+
// specified by id, get the default devices.
170+
if (needs_audio_device || needs_video_device) {
171+
media::GetDefaultDevicesForProfile(
172+
needs_audio_device,
173+
needs_video_device,
174+
&devices);
175+
}
176+
break;
177+
} case content::MEDIA_DEVICE_ACCESS:
178+
// Get the default devices for the request.
179+
media::GetDefaultDevicesForProfile(
180+
has_audio_,
181+
has_video_,
182+
&devices);
183+
break;
184+
case content::MEDIA_ENUMERATE_DEVICES:
185+
// Do nothing.
186+
NOTREACHED();
187+
break;
188+
}
189+
118190
media::GetDefaultDevicesForProfile(
119191
has_audio_,
120192
has_video_,

0 commit comments

Comments
 (0)