Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / audio_renderer_mixer_manager.cc
blob498fbfa1292aaa392e7ac2fa65e958f133a1324e
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/media/audio_renderer_mixer_manager.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "content/renderer/media/audio_device_factory.h"
10 #include "media/audio/audio_output_device.h"
11 #include "media/base/audio_hardware_config.h"
12 #include "media/base/audio_renderer_mixer.h"
13 #include "media/base/audio_renderer_mixer_input.h"
15 namespace content {
17 AudioRendererMixerManager::AudioRendererMixerManager(
18 media::AudioHardwareConfig* hardware_config)
19 : hardware_config_(hardware_config),
20 sink_for_testing_(NULL) {
23 AudioRendererMixerManager::~AudioRendererMixerManager() {
24 // References to AudioRendererMixers may be owned by garbage collected
25 // objects. During process shutdown they may be leaked, so, transitively,
26 // |mixers_| may leak (i.e., may be non-empty at this time) as well.
29 media::AudioRendererMixerInput* AudioRendererMixerManager::CreateInput(
30 int source_render_frame_id) {
31 return new media::AudioRendererMixerInput(
32 base::Bind(&AudioRendererMixerManager::GetMixer, base::Unretained(this),
33 source_render_frame_id),
34 base::Bind(&AudioRendererMixerManager::RemoveMixer,
35 base::Unretained(this), source_render_frame_id));
38 void AudioRendererMixerManager::SetAudioRendererSinkForTesting(
39 media::AudioRendererSink* sink) {
40 sink_for_testing_ = sink;
43 media::AudioRendererMixer* AudioRendererMixerManager::GetMixer(
44 int source_render_frame_id,
45 const media::AudioParameters& params) {
46 // Effects are not passed through to output creation, so ensure none are set.
47 DCHECK_EQ(params.effects(), media::AudioParameters::NO_EFFECTS);
49 const MixerKey key(source_render_frame_id, params);
50 base::AutoLock auto_lock(mixers_lock_);
52 AudioRendererMixerMap::iterator it = mixers_.find(key);
53 if (it != mixers_.end()) {
54 it->second.ref_count++;
55 return it->second.mixer;
58 // On ChromeOS we can rely on the playback device to handle resampling, so
59 // don't waste cycles on it here.
60 #if defined(OS_CHROMEOS)
61 int sample_rate = params.sample_rate();
62 #else
63 int sample_rate = hardware_config_->GetOutputSampleRate();
64 #endif
66 // Create output parameters based on the audio hardware configuration for
67 // passing on to the output sink. Force to 16-bit output for now since we
68 // know that works everywhere; ChromeOS does not support other bit depths.
69 media::AudioParameters output_params(
70 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, params.channel_layout(),
71 sample_rate, 16, hardware_config_->GetHighLatencyBufferSize());
73 // If we've created invalid output parameters, simply pass on the input params
74 // and let the browser side handle automatic fallback.
75 if (!output_params.IsValid())
76 output_params = params;
78 media::AudioRendererMixer* mixer;
79 if (sink_for_testing_) {
80 mixer = new media::AudioRendererMixer(
81 params, output_params, sink_for_testing_);
82 } else {
83 mixer = new media::AudioRendererMixer(
84 params, output_params,
85 AudioDeviceFactory::NewOutputDevice(source_render_frame_id));
88 AudioRendererMixerReference mixer_reference = { mixer, 1 };
89 mixers_[key] = mixer_reference;
90 return mixer;
93 void AudioRendererMixerManager::RemoveMixer(
94 int source_render_frame_id,
95 const media::AudioParameters& params) {
96 const MixerKey key(source_render_frame_id, params);
97 base::AutoLock auto_lock(mixers_lock_);
99 AudioRendererMixerMap::iterator it = mixers_.find(key);
100 DCHECK(it != mixers_.end());
102 // Only remove the mixer if AudioRendererMixerManager is the last owner.
103 it->second.ref_count--;
104 if (it->second.ref_count == 0) {
105 delete it->second.mixer;
106 mixers_.erase(it);
110 } // namespace content