This source file includes following definitions.
- sink_for_testing_
- CreateInput
- SetAudioRendererSinkForTesting
- GetMixer
- RemoveMixer
#include "content/renderer/media/audio_renderer_mixer_manager.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "content/renderer/media/audio_device_factory.h"
#include "media/audio/audio_output_device.h"
#include "media/base/audio_hardware_config.h"
#include "media/base/audio_renderer_mixer.h"
#include "media/base/audio_renderer_mixer_input.h"
namespace content {
AudioRendererMixerManager::AudioRendererMixerManager(
media::AudioHardwareConfig* hardware_config)
: hardware_config_(hardware_config),
sink_for_testing_(NULL) {
}
AudioRendererMixerManager::~AudioRendererMixerManager() {
DCHECK(mixers_.empty());
}
media::AudioRendererMixerInput* AudioRendererMixerManager::CreateInput(
int source_render_view_id, int source_render_frame_id) {
return new media::AudioRendererMixerInput(
base::Bind(
&AudioRendererMixerManager::GetMixer, base::Unretained(this),
source_render_view_id,
source_render_frame_id),
base::Bind(
&AudioRendererMixerManager::RemoveMixer, base::Unretained(this),
source_render_view_id));
}
void AudioRendererMixerManager::SetAudioRendererSinkForTesting(
media::AudioRendererSink* sink) {
sink_for_testing_ = sink;
}
media::AudioRendererMixer* AudioRendererMixerManager::GetMixer(
int source_render_view_id,
int source_render_frame_id,
const media::AudioParameters& params) {
const MixerKey key(source_render_view_id, params);
base::AutoLock auto_lock(mixers_lock_);
AudioRendererMixerMap::iterator it = mixers_.find(key);
if (it != mixers_.end()) {
it->second.ref_count++;
return it->second.mixer;
}
#if defined(OS_CHROMEOS) || defined(OS_LINUX)
int sample_rate = params.sample_rate();
#else
int sample_rate = hardware_config_->GetOutputSampleRate();
#endif
media::AudioParameters output_params(
media::AudioParameters::AUDIO_PCM_LOW_LATENCY, params.channel_layout(),
sample_rate, 16, hardware_config_->GetOutputBufferSize());
if (!output_params.IsValid())
output_params = params;
media::AudioRendererMixer* mixer;
if (sink_for_testing_) {
mixer = new media::AudioRendererMixer(
params, output_params, sink_for_testing_);
} else {
mixer = new media::AudioRendererMixer(
params, output_params, AudioDeviceFactory::NewOutputDevice(
source_render_view_id, source_render_frame_id));
}
AudioRendererMixerReference mixer_reference = { mixer, 1 };
mixers_[key] = mixer_reference;
return mixer;
}
void AudioRendererMixerManager::RemoveMixer(
int source_render_view_id,
const media::AudioParameters& params) {
const MixerKey key(source_render_view_id, params);
base::AutoLock auto_lock(mixers_lock_);
AudioRendererMixerMap::iterator it = mixers_.find(key);
DCHECK(it != mixers_.end());
it->second.ref_count--;
if (it->second.ref_count == 0) {
delete it->second.mixer;
mixers_.erase(it);
}
}
}