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 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_
6 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_
11 #include "base/synchronization/lock.h"
12 #include "content/common/content_export.h"
13 #include "media/audio/audio_parameters.h"
16 class AudioHardwareConfig
;
17 class AudioRendererMixer
;
18 class AudioRendererMixerInput
;
19 class AudioRendererSink
;
24 // Manages sharing of an AudioRendererMixer among AudioRendererMixerInputs based
25 // on their AudioParameters configuration. Inputs with the same AudioParameters
26 // configuration will share a mixer while a new AudioRendererMixer will be
27 // lazily created if one with the exact AudioParameters does not exist.
29 // There should only be one instance of AudioRendererMixerManager per render
32 // TODO(dalecurtis): Right now we require AudioParameters to be an exact match
33 // when we should be able to ignore bits per channel since we're only dealing
34 // with floats. However, bits per channel is currently used to interleave the
35 // audio data by AudioOutputDevice::AudioThreadCallback::Process for consumption
36 // via the shared memory. See http://crbug.com/114700.
37 class CONTENT_EXPORT AudioRendererMixerManager
{
39 // Construct an instance using the given audio hardware configuration. The
40 // provided |hardware_config| is not owned by AudioRendererMixerManager and
42 explicit AudioRendererMixerManager(
43 media::AudioHardwareConfig
* hardware_config
);
44 ~AudioRendererMixerManager();
46 // Creates an AudioRendererMixerInput with the proper callbacks necessary to
47 // retrieve an AudioRendererMixer instance from AudioRendererMixerManager.
48 // |source_render_frame_id| refers to the RenderFrame containing the entity
49 // rendering the audio. Caller must ensure AudioRendererMixerManager outlives
50 // the returned input.
51 media::AudioRendererMixerInput
* CreateInput(int source_render_frame_id
);
53 // Returns a mixer instance based on AudioParameters; an existing one if one
54 // with the provided AudioParameters exists or a new one if not.
55 media::AudioRendererMixer
* GetMixer(int source_render_frame_id
,
56 const media::AudioParameters
& params
);
58 // Remove a mixer instance given a mixer if the only other reference is held
59 // by AudioRendererMixerManager. Every AudioRendererMixer owner must call
60 // this method when it's done with a mixer.
61 void RemoveMixer(int source_render_frame_id
,
62 const media::AudioParameters
& params
);
65 friend class AudioRendererMixerManagerTest
;
67 // Define a key so that only those AudioRendererMixerInputs from the same
68 // RenderView and with the same AudioParameters can be mixed together. The
69 // first value is the RenderViewId.
70 typedef std::pair
<int, media::AudioParameters
> MixerKey
;
72 // Custom compare operator for the AudioRendererMixerMap. Allows reuse of
73 // mixers where only irrelevant keys mismatch; e.g., effects, bits per sample.
74 struct MixerKeyCompare
{
75 bool operator()(const MixerKey
& a
, const MixerKey
& b
) const {
76 if (a
.first
!= b
.first
)
77 return a
.first
< b
.first
;
78 if (a
.second
.sample_rate() != b
.second
.sample_rate())
79 return a
.second
.sample_rate() < b
.second
.sample_rate();
80 if (a
.second
.channels() != b
.second
.channels())
81 return a
.second
.channels() < b
.second
.channels();
83 // Ignore effects(), bits_per_sample(), format(), and frames_per_buffer(),
84 // these parameters do not affect mixer reuse. All AudioRendererMixer
85 // units disable FIFO, so frames_per_buffer() can be safely ignored.
86 return a
.second
.channel_layout() < b
.second
.channel_layout();
90 // Map of MixerKey to <AudioRendererMixer, Count>. Count allows
91 // AudioRendererMixerManager to keep track explicitly (v.s. RefCounted which
92 // is implicit) of the number of outstanding AudioRendererMixers.
93 struct AudioRendererMixerReference
{
94 media::AudioRendererMixer
* mixer
;
97 typedef std::map
<MixerKey
, AudioRendererMixerReference
, MixerKeyCompare
>
98 AudioRendererMixerMap
;
100 // Overrides the AudioRendererSink implementation for unit testing.
101 void SetAudioRendererSinkForTesting(media::AudioRendererSink
* sink
);
104 AudioRendererMixerMap mixers_
;
105 base::Lock mixers_lock_
;
107 // Audio hardware configuration. Used to construct output AudioParameters for
108 // each AudioRendererMixer instance.
109 media::AudioHardwareConfig
* const hardware_config_
;
111 media::AudioRendererSink
* sink_for_testing_
;
113 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerManager
);
116 } // namespace content
118 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_