This source file includes following definitions.
- audio_manager_
- GetOpenedDeviceInfoById
- Register
- Unregister
- EnumerateDevices
- Open
- Close
- UseFakeDevice
- ShouldUseFakeDevice
- EnumerateOnDeviceThread
- OpenOnDeviceThread
- DevicesEnumeratedOnIOThread
- OpenedOnIOThread
- ClosedOnIOThread
- IsOnDeviceThread
- GetDevice
#include "content/browser/renderer_host/media/audio_input_device_manager.h"
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/media_stream_request.h"
#include "media/audio/audio_device_name.h"
#include "media/audio/audio_input_ipc.h"
#include "media/audio/audio_manager_base.h"
#include "media/audio/audio_parameters.h"
#include "media/base/channel_layout.h"
#include "media/base/scoped_histogram_timer.h"
namespace content {
const int AudioInputDeviceManager::kFakeOpenSessionId = 1;
namespace {
const int kFirstSessionId = AudioInputDeviceManager::kFakeOpenSessionId + 1;
}
AudioInputDeviceManager::AudioInputDeviceManager(
media::AudioManager* audio_manager)
: listener_(NULL),
next_capture_session_id_(kFirstSessionId),
use_fake_device_(false),
audio_manager_(audio_manager) {
StreamDeviceInfo fake_device(MEDIA_DEVICE_AUDIO_CAPTURE,
media::AudioManagerBase::kDefaultDeviceName,
media::AudioManagerBase::kDefaultDeviceId,
44100, media::CHANNEL_LAYOUT_STEREO,
0);
fake_device.session_id = kFakeOpenSessionId;
devices_.push_back(fake_device);
}
AudioInputDeviceManager::~AudioInputDeviceManager() {
}
const StreamDeviceInfo* AudioInputDeviceManager::GetOpenedDeviceInfoById(
int session_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
StreamDeviceList::iterator device = GetDevice(session_id);
if (device == devices_.end())
return NULL;
return &(*device);
}
void AudioInputDeviceManager::Register(
MediaStreamProviderListener* listener,
const scoped_refptr<base::SingleThreadTaskRunner>& device_task_runner) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(!listener_);
DCHECK(!device_task_runner_);
listener_ = listener;
device_task_runner_ = device_task_runner;
}
void AudioInputDeviceManager::Unregister() {
DCHECK(listener_);
listener_ = NULL;
}
void AudioInputDeviceManager::EnumerateDevices(MediaStreamType stream_type) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(listener_);
device_task_runner_->PostTask(
FROM_HERE,
base::Bind(&AudioInputDeviceManager::EnumerateOnDeviceThread,
this, stream_type));
}
int AudioInputDeviceManager::Open(const StreamDeviceInfo& device) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
int session_id = next_capture_session_id_++;
device_task_runner_->PostTask(
FROM_HERE,
base::Bind(&AudioInputDeviceManager::OpenOnDeviceThread,
this, session_id, device));
return session_id;
}
void AudioInputDeviceManager::Close(int session_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(listener_);
StreamDeviceList::iterator device = GetDevice(session_id);
if (device == devices_.end())
return;
const MediaStreamType stream_type = device->device.type;
if (session_id != kFakeOpenSessionId)
devices_.erase(device);
BrowserThread::PostTask(BrowserThread::IO,
FROM_HERE,
base::Bind(&AudioInputDeviceManager::ClosedOnIOThread,
this, stream_type, session_id));
}
void AudioInputDeviceManager::UseFakeDevice() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
use_fake_device_ = true;
}
bool AudioInputDeviceManager::ShouldUseFakeDevice() const {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return use_fake_device_;
}
void AudioInputDeviceManager::EnumerateOnDeviceThread(
MediaStreamType stream_type) {
SCOPED_UMA_HISTOGRAM_TIMER(
"Media.AudioInputDeviceManager.EnumerateOnDeviceThreadTime");
DCHECK(IsOnDeviceThread());
media::AudioDeviceNames device_names;
switch (stream_type) {
case MEDIA_DEVICE_AUDIO_CAPTURE:
audio_manager_->GetAudioInputDeviceNames(&device_names);
break;
default:
NOTREACHED();
break;
}
scoped_ptr<StreamDeviceInfoArray> devices(new StreamDeviceInfoArray());
for (media::AudioDeviceNames::iterator it = device_names.begin();
it != device_names.end(); ++it) {
devices->push_back(StreamDeviceInfo(
stream_type, it->device_name, it->unique_id));
}
if (use_fake_device_ && devices->empty()) {
devices->push_back(StreamDeviceInfo(
stream_type, media::AudioManagerBase::kDefaultDeviceName,
media::AudioManagerBase::kDefaultDeviceId));
}
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&AudioInputDeviceManager::DevicesEnumeratedOnIOThread,
this, stream_type, base::Passed(&devices)));
}
void AudioInputDeviceManager::OpenOnDeviceThread(
int session_id, const StreamDeviceInfo& info) {
SCOPED_UMA_HISTOGRAM_TIMER(
"Media.AudioInputDeviceManager.OpenOnDeviceThreadTime");
DCHECK(IsOnDeviceThread());
StreamDeviceInfo out(info.device.type, info.device.name, info.device.id,
0, 0, 0);
out.session_id = session_id;
MediaStreamDevice::AudioDeviceParameters& input_params = out.device.input;
if (use_fake_device_) {
input_params.sample_rate = 44100;
input_params.channel_layout = media::CHANNEL_LAYOUT_STEREO;
} else {
media::AudioParameters params =
audio_manager_->GetInputStreamParameters(info.device.id);
input_params.sample_rate = params.sample_rate();
input_params.channel_layout = params.channel_layout();
input_params.frames_per_buffer = params.frames_per_buffer();
input_params.effects = params.effects();
out.device.matched_output_device_id =
audio_manager_->GetAssociatedOutputDeviceID(info.device.id);
if (!out.device.matched_output_device_id.empty()) {
params = audio_manager_->GetOutputStreamParameters(
out.device.matched_output_device_id);
MediaStreamDevice::AudioDeviceParameters& matched_output_params =
out.device.matched_output;
matched_output_params.sample_rate = params.sample_rate();
matched_output_params.channel_layout = params.channel_layout();
matched_output_params.frames_per_buffer = params.frames_per_buffer();
}
}
BrowserThread::PostTask(BrowserThread::IO,
FROM_HERE,
base::Bind(&AudioInputDeviceManager::OpenedOnIOThread,
this, session_id, out));
}
void AudioInputDeviceManager::DevicesEnumeratedOnIOThread(
MediaStreamType stream_type,
scoped_ptr<StreamDeviceInfoArray> devices) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (listener_)
listener_->DevicesEnumerated(stream_type, *devices);
}
void AudioInputDeviceManager::OpenedOnIOThread(int session_id,
const StreamDeviceInfo& info) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_EQ(session_id, info.session_id);
DCHECK(GetDevice(session_id) == devices_.end());
devices_.push_back(info);
if (listener_)
listener_->Opened(info.device.type, session_id);
}
void AudioInputDeviceManager::ClosedOnIOThread(MediaStreamType stream_type,
int session_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (listener_)
listener_->Closed(stream_type, session_id);
}
bool AudioInputDeviceManager::IsOnDeviceThread() const {
return device_task_runner_->BelongsToCurrentThread();
}
AudioInputDeviceManager::StreamDeviceList::iterator
AudioInputDeviceManager::GetDevice(int session_id) {
for (StreamDeviceList::iterator i(devices_.begin()); i != devices_.end();
++i) {
if (i->session_id == session_id)
return i;
}
return devices_.end();
}
}