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/threading/thread.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 media {
     25 
     26 class AudioOutputDispatcher;
     27 
     28 // AudioManagerBase provides AudioManager functions common for all platforms.
     29 class MEDIA_EXPORT AudioManagerBase : public AudioManager {
     30  public:
     31   // TODO(sergeyu): The constants below belong to AudioManager interface, not
     32   // to the base implementation.
     33 
     34   // Name of the generic "default" device.
     35   static const char kDefaultDeviceName[];
     36   // Unique Id of the generic "default" device.
     37   static const char kDefaultDeviceId[];
     38 
     39   // Input device ID used to capture the default system playback stream. When
     40   // this device ID is passed to MakeAudioInputStream() the returned
     41   // AudioInputStream will be capturing audio currently being played on the
     42   // default playback device. At the moment this feature is supported only on
     43   // some platforms. AudioInputStream::Intialize() will return an error on
     44   // platforms that don't support it. GetInputStreamParameters() must be used
     45   // to get the parameters of the loopback device before creating a loopback
     46   // stream, otherwise stream initialization may fail.
     47   static const char kLoopbackInputDeviceId[];
     48 
     49   virtual ~AudioManagerBase();
     50 
     51   virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE;
     52   virtual scoped_refptr<base::MessageLoopProxy> GetWorkerLoop() OVERRIDE;
     53 
     54   virtual base::string16 GetAudioInputDeviceModel() OVERRIDE;
     55 
     56   virtual void ShowAudioInputSettings() OVERRIDE;
     57 
     58   virtual void GetAudioInputDeviceNames(
     59       AudioDeviceNames* device_names) OVERRIDE;
     60 
     61   virtual void GetAudioOutputDeviceNames(
     62       AudioDeviceNames* device_names) OVERRIDE;
     63 
     64   virtual AudioOutputStream* MakeAudioOutputStream(
     65       const AudioParameters& params,
     66       const std::string& device_id,
     67       const std::string& input_device_id) OVERRIDE;
     68 
     69   virtual AudioInputStream* MakeAudioInputStream(
     70       const AudioParameters& params, const std::string& device_id) OVERRIDE;
     71 
     72   virtual AudioOutputStream* MakeAudioOutputStreamProxy(
     73       const AudioParameters& params,
     74       const std::string& device_id,
     75       const std::string& input_device_id) OVERRIDE;
     76 
     77   // Called internally by the audio stream when it has been closed.
     78   virtual void ReleaseOutputStream(AudioOutputStream* stream);
     79   virtual void ReleaseInputStream(AudioInputStream* stream);
     80 
     81   // Creates the output stream for the |AUDIO_PCM_LINEAR| format. The legacy
     82   // name is also from |AUDIO_PCM_LINEAR|.
     83   virtual AudioOutputStream* MakeLinearOutputStream(
     84       const AudioParameters& params) = 0;
     85 
     86   // Creates the output stream for the |AUDIO_PCM_LOW_LATENCY| format.
     87   // |input_device_id| is used by unified IO to open the correct input device.
     88   virtual AudioOutputStream* MakeLowLatencyOutputStream(
     89       const AudioParameters& params,
     90       const std::string& device_id,
     91       const std::string& input_device_id) = 0;
     92 
     93   // Creates the input stream for the |AUDIO_PCM_LINEAR| format. The legacy
     94   // name is also from |AUDIO_PCM_LINEAR|.
     95   virtual AudioInputStream* MakeLinearInputStream(
     96       const AudioParameters& params, const std::string& device_id) = 0;
     97 
     98   // Creates the input stream for the |AUDIO_PCM_LOW_LATENCY| format.
     99   virtual AudioInputStream* MakeLowLatencyInputStream(
    100       const AudioParameters& params, const std::string& device_id) = 0;
    101 
    102   // Listeners will be notified on the AudioManager::GetMessageLoop() loop.
    103   virtual void AddOutputDeviceChangeListener(
    104       AudioDeviceListener* listener) OVERRIDE;
    105   virtual void RemoveOutputDeviceChangeListener(
    106       AudioDeviceListener* listener) OVERRIDE;
    107 
    108   virtual AudioParameters GetDefaultOutputStreamParameters() OVERRIDE;
    109   virtual AudioParameters GetOutputStreamParameters(
    110       const std::string& device_id) OVERRIDE;
    111 
    112   virtual AudioParameters GetInputStreamParameters(
    113       const std::string& device_id) OVERRIDE;
    114 
    115   virtual std::string GetAssociatedOutputDeviceID(
    116       const std::string& input_device_id) OVERRIDE;
    117 
    118   virtual scoped_ptr<AudioLog> CreateAudioLog(
    119       AudioLogFactory::AudioComponent component) OVERRIDE;
    120 
    121   virtual void FixWedgedAudio() OVERRIDE;
    122 
    123  protected:
    124   AudioManagerBase(AudioLogFactory* audio_log_factory);
    125 
    126   // Shuts down the audio thread and releases all the audio output dispatchers
    127   // on the audio thread.  All audio streams should be freed before Shutdown()
    128   // is called.  This must be called in the destructor of every AudioManagerBase
    129   // implementation.
    130   void Shutdown();
    131 
    132   void SetMaxOutputStreamsAllowed(int max) { max_num_output_streams_ = max; }
    133 
    134   // Called by each platform specific AudioManager to notify output state change
    135   // listeners that a state change has occurred.  Must be called from the audio
    136   // thread.
    137   void NotifyAllOutputDeviceChangeListeners();
    138 
    139   // Returns user buffer size as specified on the command line or 0 if no buffer
    140   // size has been specified.
    141   int GetUserBufferSize();
    142 
    143   // Returns the preferred hardware audio output parameters for opening output
    144   // streams. If the users inject a valid |input_params|, each AudioManager
    145   // will decide if they should return the values from |input_params| or the
    146   // default hardware values. If the |input_params| is invalid, it will return
    147   // the default hardware audio parameters.
    148   // If |output_device_id| is empty, the implementation must treat that as
    149   // a request for the default output device.
    150   virtual AudioParameters GetPreferredOutputStreamParameters(
    151       const std::string& output_device_id,
    152       const AudioParameters& input_params) = 0;
    153 
    154   // Returns the ID of the default audio output device.
    155   // Implementations that don't yet support this should return an empty string.
    156   virtual std::string GetDefaultOutputDeviceID();
    157 
    158   // Get number of input or output streams.
    159   int input_stream_count() { return num_input_streams_; }
    160   int output_stream_count() { return num_output_streams_; }
    161 
    162  private:
    163   struct DispatcherParams;
    164   typedef ScopedVector<DispatcherParams> AudioOutputDispatchers;
    165 
    166   class CompareByParams;
    167 
    168   // Called by Shutdown().
    169   void ShutdownOnAudioThread();
    170 
    171   // Max number of open output streams, modified by
    172   // SetMaxOutputStreamsAllowed().
    173   int max_num_output_streams_;
    174 
    175   // Max number of open input streams.
    176   int max_num_input_streams_;
    177 
    178   // Number of currently open output streams.
    179   int num_output_streams_;
    180 
    181   // Number of currently open input streams.
    182   int num_input_streams_;
    183 
    184   // Track output state change listeners.
    185   ObserverList<AudioDeviceListener> output_listeners_;
    186 
    187   // Thread used to interact with audio streams created by this audio manager.
    188   base::Thread audio_thread_;
    189 
    190   // The message loop of the audio thread this object runs on. Used for internal
    191   // tasks which run on the audio thread even after Shutdown() has been started
    192   // and GetMessageLoop() starts returning NULL.
    193   scoped_refptr<base::MessageLoopProxy> message_loop_;
    194 
    195   // Map of cached AudioOutputDispatcher instances.  Must only be touched
    196   // from the audio thread (no locking).
    197   AudioOutputDispatchers output_dispatchers_;
    198 
    199   // Proxy for creating AudioLog objects.
    200   AudioLogFactory* const audio_log_factory_;
    201 
    202   DISALLOW_COPY_AND_ASSIGN(AudioManagerBase);
    203 };
    204 
    205 }  // namespace media
    206 
    207 #endif  // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
    208