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"
10 #include "base/bind_helpers.h"
11 #include "content/renderer/media/audio_device_factory.h"
12 #include "media/audio/audio_output_device.h"
13 #include "media/base/audio_hardware_config.h"
14 #include "media/base/audio_renderer_mixer.h"
15 #include "media/base/audio_renderer_mixer_input.h"
19 AudioRendererMixerManager::AudioRendererMixerManager(
20 media::AudioHardwareConfig
* hardware_config
)
21 : hardware_config_(hardware_config
),
22 sink_for_testing_(NULL
) {
25 AudioRendererMixerManager::~AudioRendererMixerManager() {
26 // References to AudioRendererMixers may be owned by garbage collected
27 // objects. During process shutdown they may be leaked, so, transitively,
28 // |mixers_| may leak (i.e., may be non-empty at this time) as well.
31 media::AudioRendererMixerInput
* AudioRendererMixerManager::CreateInput(
32 int source_render_frame_id
) {
33 return new media::AudioRendererMixerInput(
34 base::Bind(&AudioRendererMixerManager::GetMixer
, base::Unretained(this),
35 source_render_frame_id
),
36 base::Bind(&AudioRendererMixerManager::RemoveMixer
,
37 base::Unretained(this), source_render_frame_id
));
40 void AudioRendererMixerManager::SetAudioRendererSinkForTesting(
41 media::AudioRendererSink
* sink
) {
42 sink_for_testing_
= sink
;
45 media::AudioRendererMixer
* AudioRendererMixerManager::GetMixer(
46 int source_render_frame_id
,
47 const media::AudioParameters
& params
) {
48 // Effects are not passed through to output creation, so ensure none are set.
49 DCHECK_EQ(params
.effects(), media::AudioParameters::NO_EFFECTS
);
51 const MixerKey
key(source_render_frame_id
, params
);
52 base::AutoLock
auto_lock(mixers_lock_
);
54 AudioRendererMixerMap::iterator it
= mixers_
.find(key
);
55 if (it
!= mixers_
.end()) {
56 it
->second
.ref_count
++;
57 return it
->second
.mixer
;
60 // On ChromeOS we can rely on the playback device to handle resampling, so
61 // don't waste cycles on it here.
62 #if defined(OS_CHROMEOS)
63 int sample_rate
= params
.sample_rate();
65 int sample_rate
= hardware_config_
->GetOutputSampleRate();
68 // Create output parameters based on the audio hardware configuration for
69 // passing on to the output sink. Force to 16-bit output for now since we
70 // know that works everywhere; ChromeOS does not support other bit depths.
71 media::AudioParameters
output_params(
72 media::AudioParameters::AUDIO_PCM_LOW_LATENCY
, params
.channel_layout(),
73 sample_rate
, 16, hardware_config_
->GetHighLatencyBufferSize());
75 // If we've created invalid output parameters, simply pass on the input params
76 // and let the browser side handle automatic fallback.
77 if (!output_params
.IsValid())
78 output_params
= params
;
80 media::AudioRendererMixer
* mixer
;
81 if (sink_for_testing_
) {
82 mixer
= new media::AudioRendererMixer(
83 params
, output_params
, sink_for_testing_
);
85 mixer
= new media::AudioRendererMixer(
86 params
, output_params
,
87 AudioDeviceFactory::NewOutputDevice(source_render_frame_id
, 0,
88 std::string(), url::Origin()));
91 AudioRendererMixerReference mixer_reference
= { mixer
, 1 };
92 mixers_
[key
] = mixer_reference
;
96 void AudioRendererMixerManager::RemoveMixer(
97 int source_render_frame_id
,
98 const media::AudioParameters
& params
) {
99 const MixerKey
key(source_render_frame_id
, params
);
100 base::AutoLock
auto_lock(mixers_lock_
);
102 AudioRendererMixerMap::iterator it
= mixers_
.find(key
);
103 DCHECK(it
!= mixers_
.end());
105 // Only remove the mixer if AudioRendererMixerManager is the last owner.
106 it
->second
.ref_count
--;
107 if (it
->second
.ref_count
== 0) {
108 delete it
->second
.mixer
;
113 } // namespace content