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 MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
6 #define MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_
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"
18 #include "media/audio/audio_output_dispatcher.h"
21 #include "base/win/scoped_com_initializer.h"
30 class AudioOutputDispatcher
;
32 // AudioManagerBase provides AudioManager functions common for all platforms.
33 class MEDIA_EXPORT AudioManagerBase
: public AudioManager
{
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
[];
40 virtual ~AudioManagerBase();
42 virtual scoped_refptr
<base::MessageLoopProxy
> GetMessageLoop() OVERRIDE
;
43 virtual scoped_refptr
<base::MessageLoopProxy
> GetWorkerLoop() OVERRIDE
;
45 virtual string16
GetAudioInputDeviceModel() OVERRIDE
;
47 virtual void ShowAudioInputSettings() OVERRIDE
;
49 virtual void GetAudioInputDeviceNames(
50 media::AudioDeviceNames
* device_names
) OVERRIDE
;
52 virtual AudioOutputStream
* MakeAudioOutputStream(
53 const AudioParameters
& params
,
54 const std::string
& input_device_id
) OVERRIDE
;
56 virtual AudioInputStream
* MakeAudioInputStream(
57 const AudioParameters
& params
, const std::string
& device_id
) OVERRIDE
;
59 virtual AudioOutputStream
* MakeAudioOutputStreamProxy(
60 const AudioParameters
& params
,
61 const std::string
& input_device_id
) OVERRIDE
;
63 // Called internally by the audio stream when it has been closed.
64 virtual void ReleaseOutputStream(AudioOutputStream
* stream
);
65 virtual void ReleaseInputStream(AudioInputStream
* stream
);
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;
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;
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;
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;
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
;
92 virtual AudioParameters
GetDefaultOutputStreamParameters() OVERRIDE
;
93 virtual AudioParameters
GetInputStreamParameters(
94 const std::string
& device_id
) OVERRIDE
;
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
106 void SetMaxOutputStreamsAllowed(int max
) { max_num_output_streams_
= max
; }
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
111 void NotifyAllOutputDeviceChangeListeners();
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;
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_
; }
126 struct DispatcherParams
;
127 typedef ScopedVector
<DispatcherParams
> AudioOutputDispatchers
;
129 class CompareByParams
;
131 // Called by Shutdown().
132 void ShutdownOnAudioThread();
134 // Max number of open output streams, modified by
135 // SetMaxOutputStreamsAllowed().
136 int max_num_output_streams_
;
138 // Max number of open input streams.
139 int max_num_input_streams_
;
141 // Number of currently open output streams.
142 int num_output_streams_
;
144 // Number of currently open input streams.
145 int num_input_streams_
;
147 // Track output state change listeners.
148 ObserverList
<AudioDeviceListener
> output_listeners_
;
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_
;
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_
;
159 // Map of cached AudioOutputDispatcher instances. Must only be touched
160 // from the audio thread (no locking).
161 AudioOutputDispatchers output_dispatchers_
;
163 DISALLOW_COPY_AND_ASSIGN(AudioManagerBase
);
168 #endif // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_