Home | History | Annotate | Download | only in media
      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.  Caller must ensure AudioRendererMixerManager outlives
     50   // the returned input.
     51   media::AudioRendererMixerInput* CreateInput(int source_render_view_id);
     52 
     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_view_id,
     56                                       const media::AudioParameters& params);
     57 
     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_view_id,
     62                    const media::AudioParameters& params);
     63 
     64  private:
     65   friend class AudioRendererMixerManagerTest;
     66 
     67   // Define a key so that only those AudioRendererMixerInputs from the same
     68   // RenderView and with the same AudioParameters can be mixed together.
     69   typedef std::pair<int, media::AudioParameters> MixerKey;
     70 
     71   // Map of MixerKey to <AudioRendererMixer, Count>.  Count allows
     72   // AudioRendererMixerManager to keep track explicitly (v.s. RefCounted which
     73   // is implicit) of the number of outstanding AudioRendererMixers.
     74   struct AudioRendererMixerReference {
     75     media::AudioRendererMixer* mixer;
     76     int ref_count;
     77   };
     78   typedef std::map<MixerKey, AudioRendererMixerReference> AudioRendererMixerMap;
     79 
     80   // Overrides the AudioRendererSink implementation for unit testing.
     81   void SetAudioRendererSinkForTesting(media::AudioRendererSink* sink);
     82 
     83   // Active mixers.
     84   AudioRendererMixerMap mixers_;
     85   base::Lock mixers_lock_;
     86 
     87   // Audio hardware configuration.  Used to construct output AudioParameters for
     88   // each AudioRendererMixer instance.
     89   media::AudioHardwareConfig* const hardware_config_;
     90 
     91   media::AudioRendererSink* sink_for_testing_;
     92 
     93   DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerManager);
     94 };
     95 
     96 }  // namespace content
     97 
     98 #endif  // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_
     99