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. 4 5 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_ 6 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_ 7 8 #include <map> 9 #include <utility> 10 11 #include "base/synchronization/lock.h" 12 #include "content/common/content_export.h" 13 #include "media/audio/audio_parameters.h" 14 15 namespace media { 16 class AudioHardwareConfig; 17 class AudioRendererMixer; 18 class AudioRendererMixerInput; 19 class AudioRendererSink; 20 } 21 22 namespace content { 23 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. 28 // 29 // There should only be one instance of AudioRendererMixerManager per render 30 // thread. 31 // 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 { 38 public: 39 // Construct an instance using the given audio hardware configuration. The 40 // provided |hardware_config| is not owned by AudioRendererMixerManager and 41 // must outlive it. 42 explicit AudioRendererMixerManager( 43 media::AudioHardwareConfig* hardware_config); 44 ~AudioRendererMixerManager(); 45 46 // Creates an AudioRendererMixerInput with the proper callbacks necessary to 47 // retrieve an AudioRendererMixer instance from AudioRendererMixerManager. 48 // |source_render_view_id| refers to the RenderView containing the entity 49 // rendering the audio. |source_render_frame_id| refers to the RenderFrame 50 // containing the entity rendering the audio. Caller must ensure 51 // AudioRendererMixerManager outlives the returned input. 52 media::AudioRendererMixerInput* CreateInput(int source_render_view_id, 53 int source_render_frame_id); 54 55 // Returns a mixer instance based on AudioParameters; an existing one if one 56 // with the provided AudioParameters exists or a new one if not. 57 media::AudioRendererMixer* GetMixer(int source_render_view_id, 58 int source_render_frame_id, 59 const media::AudioParameters& params); 60 61 // Remove a mixer instance given a mixer if the only other reference is held 62 // by AudioRendererMixerManager. Every AudioRendererMixer owner must call 63 // this method when it's done with a mixer. 64 void RemoveMixer(int source_render_view_id, 65 const media::AudioParameters& params); 66 67 private: 68 friend class AudioRendererMixerManagerTest; 69 70 // Define a key so that only those AudioRendererMixerInputs from the same 71 // RenderView and with the same AudioParameters can be mixed together. 72 typedef std::pair<int, media::AudioParameters> MixerKey; 73 74 // Map of MixerKey to <AudioRendererMixer, Count>. Count allows 75 // AudioRendererMixerManager to keep track explicitly (v.s. RefCounted which 76 // is implicit) of the number of outstanding AudioRendererMixers. 77 struct AudioRendererMixerReference { 78 media::AudioRendererMixer* mixer; 79 int ref_count; 80 }; 81 typedef std::map<MixerKey, AudioRendererMixerReference> AudioRendererMixerMap; 82 83 // Overrides the AudioRendererSink implementation for unit testing. 84 void SetAudioRendererSinkForTesting(media::AudioRendererSink* sink); 85 86 // Active mixers. 87 AudioRendererMixerMap mixers_; 88 base::Lock mixers_lock_; 89 90 // Audio hardware configuration. Used to construct output AudioParameters for 91 // each AudioRendererMixer instance. 92 media::AudioHardwareConfig* const hardware_config_; 93 94 media::AudioRendererSink* sink_for_testing_; 95 96 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerManager); 97 }; 98 99 } // namespace content 100 101 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_ 102