electron/atom/browser/media/media_capture_devices_dispatcher.cc

153 lines
4.9 KiB
C++
Raw Normal View History

// 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-CHROMIUM file.
#include "atom/browser/media/media_capture_devices_dispatcher.h"
#include "base/logging.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/media_capture_devices.h"
#include "content/public/common/media_stream_request.h"
namespace atom {
using content::BrowserThread;
using content::MediaStreamDevices;
namespace {
// Finds a device in |devices| that has |device_id|, or NULL if not found.
const content::MediaStreamDevice* FindDeviceWithId(
const content::MediaStreamDevices& devices,
const std::string& device_id) {
2016-07-10 11:12:33 +00:00
auto iter = devices.begin();
for (; iter != devices.end(); ++iter) {
if (iter->id == device_id) {
return &(*iter);
}
}
2016-07-10 09:56:02 +00:00
return nullptr;
}
const MediaStreamDevices& EmptyDevices() {
static MediaStreamDevices* devices = new MediaStreamDevices;
return *devices;
}
} // namespace
MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() {
2015-12-07 11:55:01 +00:00
return base::Singleton<MediaCaptureDevicesDispatcher>::get();
}
MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher()
: is_device_enumeration_disabled_(false) {
// MediaCaptureDevicesDispatcher is a singleton. It should be created on
// UI thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
MediaCaptureDevicesDispatcher::~MediaCaptureDevicesDispatcher() {}
const MediaStreamDevices&
MediaCaptureDevicesDispatcher::GetAudioCaptureDevices() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (is_device_enumeration_disabled_)
return EmptyDevices();
return content::MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices();
}
const MediaStreamDevices&
MediaCaptureDevicesDispatcher::GetVideoCaptureDevices() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (is_device_enumeration_disabled_)
return EmptyDevices();
return content::MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices();
}
void MediaCaptureDevicesDispatcher::GetDefaultDevices(
bool audio,
bool video,
content::MediaStreamDevices* devices) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(audio || video);
if (audio) {
const content::MediaStreamDevice* device = GetFirstAvailableAudioDevice();
if (device)
devices->push_back(*device);
}
if (video) {
const content::MediaStreamDevice* device = GetFirstAvailableVideoDevice();
if (device)
devices->push_back(*device);
}
}
const content::MediaStreamDevice*
MediaCaptureDevicesDispatcher::GetRequestedAudioDevice(
const std::string& requested_audio_device_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices();
const content::MediaStreamDevice* const device =
FindDeviceWithId(audio_devices, requested_audio_device_id);
return device;
}
const content::MediaStreamDevice*
MediaCaptureDevicesDispatcher::GetFirstAvailableAudioDevice() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices();
if (audio_devices.empty())
2016-07-10 09:56:02 +00:00
return nullptr;
return &(*audio_devices.begin());
}
const content::MediaStreamDevice*
MediaCaptureDevicesDispatcher::GetRequestedVideoDevice(
const std::string& requested_video_device_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices();
const content::MediaStreamDevice* const device =
FindDeviceWithId(video_devices, requested_video_device_id);
return device;
}
const content::MediaStreamDevice*
MediaCaptureDevicesDispatcher::GetFirstAvailableVideoDevice() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices();
if (video_devices.empty())
2016-07-10 09:56:02 +00:00
return nullptr;
return &(*video_devices.begin());
}
void MediaCaptureDevicesDispatcher::DisableDeviceEnumerationForTesting() {
is_device_enumeration_disabled_ = true;
}
2018-04-18 01:56:12 +00:00
void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged() {}
2018-04-18 01:56:12 +00:00
void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged() {}
void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged(
int render_process_id,
int render_view_id,
int page_request_id,
const GURL& security_origin,
2014-10-11 08:38:27 +00:00
content::MediaStreamType stream_type,
2018-04-18 01:56:12 +00:00
content::MediaRequestState state) {}
2018-04-18 01:56:12 +00:00
void MediaCaptureDevicesDispatcher::OnCreatingAudioStream(int render_process_id,
int render_view_id) {}
2016-07-04 06:06:05 +00:00
void MediaCaptureDevicesDispatcher::OnSetCapturingLinkSecured(
int render_process_id,
int render_frame_id,
int page_request_id,
content::MediaStreamType stream_type,
2018-04-18 01:56:12 +00:00
bool is_secure) {}
2016-07-04 06:06:05 +00:00
} // namespace atom