Home | History | Annotate | Download | only in audio
      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 MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
      6 #define MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
      7 
      8 #include <string>
      9 #include <utility>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/scoped_vector.h"
     14 #include "base/observer_list.h"
     15 #include "base/synchronization/lock.h"
     16 #include "media/audio/audio_manager.h"
     17 
     18 #include "media/audio/audio_output_dispatcher.h"
     19 
     20 #if defined(OS_WIN)
     21 #include "base/win/scoped_com_initializer.h"
     22 #endif
     23 
     24 namespace base {
     25 class Thread;
     26 }
     27 
     28 namespace media {
     29 
     30 class AudioOutputDispatcher;
     31 
     32 // AudioManagerBase provides AudioManager functions common for all platforms.
     33 class MEDIA_EXPORT AudioManagerBase : public AudioManager {
     34  public:
     35   // Name of the generic "default" device.
     36   static const char kDefaultDeviceName[];
     37   // Unique Id of the generic "default" device.
     38   static const char kDefaultDeviceId[];
     39 
     40   virtual ~AudioManagerBase();
     41 
     42   virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE;
     43   virtual scoped_refptr<base::MessageLoopProxy> GetWorkerLoop() OVERRIDE;
     44 
     45   virtual string16 GetAudioInputDeviceModel() OVERRIDE;
     46 
     47   virtual void ShowAudioInputSettings() OVERRIDE;
     48 
     49   virtual void GetAudioInputDeviceNames(
     50       media::AudioDeviceNames* device_names) OVERRIDE;
     51 
     52   virtual AudioOutputStream* MakeAudioOutputStream(
     53       const AudioParameters& params,
     54       const std::string& input_device_id) OVERRIDE;
     55 
     56   virtual AudioInputStream* MakeAudioInputStream(
     57       const AudioParameters& params, const std::string& device_id) OVERRIDE;
     58 
     59   virtual AudioOutputStream* MakeAudioOutputStreamProxy(
     60       const AudioParameters& params,
     61       const std::string& input_device_id) OVERRIDE;
     62 
     63   // Called internally by the audio stream when it has been closed.
     64   virtual void ReleaseOutputStream(AudioOutputStream* stream);
     65   virtual void ReleaseInputStream(AudioInputStream* stream);
     66 
     67   // Creates the output stream for the |AUDIO_PCM_LINEAR| format. The legacy
     68   // name is also from |AUDIO_PCM_LINEAR|.
     69   virtual AudioOutputStream* MakeLinearOutputStream(
     70       const AudioParameters& params) = 0;
     71 
     72   // Creates the output stream for the |AUDIO_PCM_LOW_LATENCY| format.
     73   // |input_device_id| is used by unified IO to open the correct input device.
     74   virtual AudioOutputStream* MakeLowLatencyOutputStream(
     75       const AudioParameters& params, const std::string& input_device_id) = 0;
     76 
     77   // Creates the input stream for the |AUDIO_PCM_LINEAR| format. The legacy
     78   // name is also from |AUDIO_PCM_LINEAR|.
     79   virtual AudioInputStream* MakeLinearInputStream(
     80       const AudioParameters& params, const std::string& device_id) = 0;
     81 
     82   // Creates the input stream for the |AUDIO_PCM_LOW_LATENCY| format.
     83   virtual AudioInputStream* MakeLowLatencyInputStream(
     84       const AudioParameters& params, const std::string& device_id) = 0;
     85 
     86   // Listeners will be notified on the AudioManager::GetMessageLoop() loop.
     87   virtual void AddOutputDeviceChangeListener(
     88       AudioDeviceListener* listener) OVERRIDE;
     89   virtual void RemoveOutputDeviceChangeListener(
     90       AudioDeviceListener* listener) OVERRIDE;
     91 
     92   virtual AudioParameters GetDefaultOutputStreamParameters() OVERRIDE;
     93   virtual AudioParameters GetInputStreamParameters(
     94       const std::string& device_id) OVERRIDE;
     95 
     96  protected:
     97   AudioManagerBase();
     98 
     99 
    100   // Shuts down the audio thread and releases all the audio output dispatchers
    101   // on the audio thread.  All audio streams should be freed before Shutdown()
    102   // is called.  This must be called in the destructor of every AudioManagerBase
    103   // implementation.
    104   void Shutdown();
    105 
    106   void SetMaxOutputStreamsAllowed(int max) { max_num_output_streams_ = max; }
    107 
    108   // Called by each platform specific AudioManager to notify output state change
    109   // listeners that a state change has occurred.  Must be called from the audio
    110   // thread.
    111   void NotifyAllOutputDeviceChangeListeners();
    112 
    113   // Returns the preferred hardware audio output parameters for opening output
    114   // streams. If the users inject a valid |input_params|, each AudioManager
    115   // will decide if they should return the values from |input_params| or the
    116   // default hardware values. If the |input_params| is invalid, it will return
    117   // the default hardware audio parameters.
    118   virtual AudioParameters GetPreferredOutputStreamParameters(
    119       const AudioParameters& input_params) = 0;
    120 
    121   // Get number of input or output streams.
    122   int input_stream_count() { return num_input_streams_; }
    123   int output_stream_count() { return num_output_streams_; }
    124 
    125  private:
    126   struct DispatcherParams;
    127   typedef ScopedVector<DispatcherParams> AudioOutputDispatchers;
    128 
    129   class CompareByParams;
    130 
    131   // Called by Shutdown().
    132   void ShutdownOnAudioThread();
    133 
    134   // Max number of open output streams, modified by
    135   // SetMaxOutputStreamsAllowed().
    136   int max_num_output_streams_;
    137 
    138   // Max number of open input streams.
    139   int max_num_input_streams_;
    140 
    141   // Number of currently open output streams.
    142   int num_output_streams_;
    143 
    144   // Number of currently open input streams.
    145   int num_input_streams_;
    146 
    147   // Track output state change listeners.
    148   ObserverList<AudioDeviceListener> output_listeners_;
    149 
    150   // Thread used to interact with audio streams created by this audio manager.
    151   scoped_ptr<base::Thread> audio_thread_;
    152   mutable base::Lock audio_thread_lock_;
    153 
    154   // The message loop of the audio thread this object runs on. Used for internal
    155   // tasks which run on the audio thread even after Shutdown() has been started
    156   // and GetMessageLoop() starts returning NULL.
    157   scoped_refptr<base::MessageLoopProxy> message_loop_;
    158 
    159   // Map of cached AudioOutputDispatcher instances.  Must only be touched
    160   // from the audio thread (no locking).
    161   AudioOutputDispatchers output_dispatchers_;
    162 
    163   DISALLOW_COPY_AND_ASSIGN(AudioManagerBase);
    164 };
    165 
    166 }  // namespace media
    167 
    168 #endif  // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
    169