OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_ |
6 #define MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_ | 6 #define MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_ |
7 | 7 |
8 #include <map> | |
9 #include <string> | 8 #include <string> |
10 #include <utility> | 9 #include <utility> |
11 | 10 |
12 #include "base/atomic_ref_count.h" | 11 #include "base/atomic_ref_count.h" |
13 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
14 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/scoped_vector.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "media/audio/audio_manager.h" | 17 #include "media/audio/audio_manager.h" |
18 | 18 |
| 19 #include "media/audio/audio_output_dispatcher.h" |
| 20 |
19 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
20 #include "base/win/scoped_com_initializer.h" | 22 #include "base/win/scoped_com_initializer.h" |
21 #endif | 23 #endif |
22 | 24 |
23 namespace base { | 25 namespace base { |
24 class Thread; | 26 class Thread; |
25 } | 27 } |
26 | 28 |
27 namespace media { | 29 namespace media { |
28 | 30 |
(...skipping 12 matching lines...) Expand all Loading... |
41 virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE; | 43 virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE; |
42 | 44 |
43 virtual string16 GetAudioInputDeviceModel() OVERRIDE; | 45 virtual string16 GetAudioInputDeviceModel() OVERRIDE; |
44 | 46 |
45 virtual void ShowAudioInputSettings() OVERRIDE; | 47 virtual void ShowAudioInputSettings() OVERRIDE; |
46 | 48 |
47 virtual void GetAudioInputDeviceNames( | 49 virtual void GetAudioInputDeviceNames( |
48 media::AudioDeviceNames* device_names) OVERRIDE; | 50 media::AudioDeviceNames* device_names) OVERRIDE; |
49 | 51 |
50 virtual AudioOutputStream* MakeAudioOutputStream( | 52 virtual AudioOutputStream* MakeAudioOutputStream( |
51 const AudioParameters& params) OVERRIDE; | 53 const AudioParameters& params, |
| 54 const std::string& input_device_id) OVERRIDE; |
52 | 55 |
53 virtual AudioInputStream* MakeAudioInputStream( | 56 virtual AudioInputStream* MakeAudioInputStream( |
54 const AudioParameters& params, const std::string& device_id) OVERRIDE; | 57 const AudioParameters& params, const std::string& device_id) OVERRIDE; |
55 | 58 |
56 virtual AudioOutputStream* MakeAudioOutputStreamProxy( | 59 virtual AudioOutputStream* MakeAudioOutputStreamProxy( |
57 const AudioParameters& params) OVERRIDE; | 60 const AudioParameters& params, |
| 61 const std::string& input_device_id) OVERRIDE; |
58 | 62 |
59 virtual bool IsRecordingInProcess() OVERRIDE; | 63 virtual bool IsRecordingInProcess() OVERRIDE; |
60 | 64 |
61 // Called internally by the audio stream when it has been closed. | 65 // Called internally by the audio stream when it has been closed. |
62 virtual void ReleaseOutputStream(AudioOutputStream* stream); | 66 virtual void ReleaseOutputStream(AudioOutputStream* stream); |
63 virtual void ReleaseInputStream(AudioInputStream* stream); | 67 virtual void ReleaseInputStream(AudioInputStream* stream); |
64 | 68 |
65 void IncreaseActiveInputStreamCount(); | 69 void IncreaseActiveInputStreamCount(); |
66 void DecreaseActiveInputStreamCount(); | 70 void DecreaseActiveInputStreamCount(); |
67 | 71 |
68 // Creates the output stream for the |AUDIO_PCM_LINEAR| format. The legacy | 72 // Creates the output stream for the |AUDIO_PCM_LINEAR| format. The legacy |
69 // name is also from |AUDIO_PCM_LINEAR|. | 73 // name is also from |AUDIO_PCM_LINEAR|. |
70 virtual AudioOutputStream* MakeLinearOutputStream( | 74 virtual AudioOutputStream* MakeLinearOutputStream( |
71 const AudioParameters& params) = 0; | 75 const AudioParameters& params) = 0; |
72 | 76 |
73 // Creates the output stream for the |AUDIO_PCM_LOW_LATENCY| format. | 77 // Creates the output stream for the |AUDIO_PCM_LOW_LATENCY| format. |
| 78 // |input_device_id| is used by unified IO to open the correct input device. |
74 virtual AudioOutputStream* MakeLowLatencyOutputStream( | 79 virtual AudioOutputStream* MakeLowLatencyOutputStream( |
75 const AudioParameters& params) = 0; | 80 const AudioParameters& params, const std::string& input_device_id) = 0; |
76 | 81 |
77 // Creates the input stream for the |AUDIO_PCM_LINEAR| format. The legacy | 82 // Creates the input stream for the |AUDIO_PCM_LINEAR| format. The legacy |
78 // name is also from |AUDIO_PCM_LINEAR|. | 83 // name is also from |AUDIO_PCM_LINEAR|. |
79 virtual AudioInputStream* MakeLinearInputStream( | 84 virtual AudioInputStream* MakeLinearInputStream( |
80 const AudioParameters& params, const std::string& device_id) = 0; | 85 const AudioParameters& params, const std::string& device_id) = 0; |
81 | 86 |
82 // Creates the input stream for the |AUDIO_PCM_LOW_LATENCY| format. | 87 // Creates the input stream for the |AUDIO_PCM_LOW_LATENCY| format. |
83 virtual AudioInputStream* MakeLowLatencyInputStream( | 88 virtual AudioInputStream* MakeLowLatencyInputStream( |
84 const AudioParameters& params, const std::string& device_id) = 0; | 89 const AudioParameters& params, const std::string& device_id) = 0; |
85 | 90 |
86 // Listeners will be notified on the AudioManager::GetMessageLoop() loop. | 91 // Listeners will be notified on the AudioManager::GetMessageLoop() loop. |
87 virtual void AddOutputDeviceChangeListener( | 92 virtual void AddOutputDeviceChangeListener( |
88 AudioDeviceListener* listener) OVERRIDE; | 93 AudioDeviceListener* listener) OVERRIDE; |
89 virtual void RemoveOutputDeviceChangeListener( | 94 virtual void RemoveOutputDeviceChangeListener( |
90 AudioDeviceListener* listener) OVERRIDE; | 95 AudioDeviceListener* listener) OVERRIDE; |
91 | 96 |
92 virtual AudioParameters GetDefaultOutputStreamParameters() OVERRIDE; | 97 virtual AudioParameters GetDefaultOutputStreamParameters() OVERRIDE; |
93 virtual AudioParameters GetInputStreamParameters( | 98 virtual AudioParameters GetInputStreamParameters( |
94 const std::string& device_id) OVERRIDE; | 99 const std::string& device_id) OVERRIDE; |
95 | 100 |
96 protected: | 101 protected: |
97 AudioManagerBase(); | 102 AudioManagerBase(); |
98 | 103 |
99 // TODO(dalecurtis): This must change to map both input and output parameters | |
100 // to a single dispatcher, otherwise on a device state change we'll just get | |
101 // the exact same invalid dispatcher. | |
102 typedef std::map<std::pair<AudioParameters, AudioParameters>, | |
103 scoped_refptr<AudioOutputDispatcher> > | |
104 AudioOutputDispatchersMap; | |
105 | 104 |
106 // Shuts down the audio thread and releases all the audio output dispatchers | 105 // Shuts down the audio thread and releases all the audio output dispatchers |
107 // on the audio thread. All audio streams should be freed before Shutdown() | 106 // on the audio thread. All audio streams should be freed before Shutdown() |
108 // is called. This must be called in the destructor of every AudioManagerBase | 107 // is called. This must be called in the destructor of every AudioManagerBase |
109 // implementation. | 108 // implementation. |
110 void Shutdown(); | 109 void Shutdown(); |
111 | 110 |
112 void SetMaxOutputStreamsAllowed(int max) { max_num_output_streams_ = max; } | 111 void SetMaxOutputStreamsAllowed(int max) { max_num_output_streams_ = max; } |
113 | 112 |
114 // Called by each platform specific AudioManager to notify output state change | 113 // Called by each platform specific AudioManager to notify output state change |
115 // listeners that a state change has occurred. Must be called from the audio | 114 // listeners that a state change has occurred. Must be called from the audio |
116 // thread. | 115 // thread. |
117 void NotifyAllOutputDeviceChangeListeners(); | 116 void NotifyAllOutputDeviceChangeListeners(); |
118 | 117 |
119 // Returns the preferred hardware audio output parameters for opening output | 118 // Returns the preferred hardware audio output parameters for opening output |
120 // streams. If the users inject a valid |input_params|, each AudioManager | 119 // streams. If the users inject a valid |input_params|, each AudioManager |
121 // will decide if they should return the values from |input_params| or the | 120 // will decide if they should return the values from |input_params| or the |
122 // default hardware values. If the |input_params| is invalid, it will return | 121 // default hardware values. If the |input_params| is invalid, it will return |
123 // the default hardware audio parameters. | 122 // the default hardware audio parameters. |
124 virtual AudioParameters GetPreferredOutputStreamParameters( | 123 virtual AudioParameters GetPreferredOutputStreamParameters( |
125 const AudioParameters& input_params) = 0; | 124 const AudioParameters& input_params) = 0; |
126 | 125 |
127 // Map of cached AudioOutputDispatcher instances. Must only be touched | |
128 // from the audio thread (no locking). | |
129 AudioOutputDispatchersMap output_dispatchers_; | |
130 | |
131 // Get number of input or output streams. | 126 // Get number of input or output streams. |
132 int input_stream_count() { return num_input_streams_; } | 127 int input_stream_count() { return num_input_streams_; } |
133 int output_stream_count() { return num_output_streams_; } | 128 int output_stream_count() { return num_output_streams_; } |
134 | 129 |
135 private: | 130 private: |
| 131 struct DispatcherParams; |
| 132 typedef ScopedVector<DispatcherParams> AudioOutputDispatchers; |
| 133 |
| 134 class CompareByParams; |
| 135 |
136 // Called by Shutdown(). | 136 // Called by Shutdown(). |
137 void ShutdownOnAudioThread(); | 137 void ShutdownOnAudioThread(); |
138 | 138 |
139 // Counts the number of active input streams to find out if something else | 139 // Counts the number of active input streams to find out if something else |
140 // is currently recording in Chrome. | 140 // is currently recording in Chrome. |
141 base::AtomicRefCount num_active_input_streams_; | 141 base::AtomicRefCount num_active_input_streams_; |
142 | 142 |
143 // Max number of open output streams, modified by | 143 // Max number of open output streams, modified by |
144 // SetMaxOutputStreamsAllowed(). | 144 // SetMaxOutputStreamsAllowed(). |
145 int max_num_output_streams_; | 145 int max_num_output_streams_; |
(...skipping 12 matching lines...) Expand all Loading... |
158 | 158 |
159 // Thread used to interact with audio streams created by this audio manager. | 159 // Thread used to interact with audio streams created by this audio manager. |
160 scoped_ptr<base::Thread> audio_thread_; | 160 scoped_ptr<base::Thread> audio_thread_; |
161 mutable base::Lock audio_thread_lock_; | 161 mutable base::Lock audio_thread_lock_; |
162 | 162 |
163 // The message loop of the audio thread this object runs on. Used for internal | 163 // The message loop of the audio thread this object runs on. Used for internal |
164 // tasks which run on the audio thread even after Shutdown() has been started | 164 // tasks which run on the audio thread even after Shutdown() has been started |
165 // and GetMessageLoop() starts returning NULL. | 165 // and GetMessageLoop() starts returning NULL. |
166 scoped_refptr<base::MessageLoopProxy> message_loop_; | 166 scoped_refptr<base::MessageLoopProxy> message_loop_; |
167 | 167 |
| 168 // Map of cached AudioOutputDispatcher instances. Must only be touched |
| 169 // from the audio thread (no locking). |
| 170 AudioOutputDispatchers output_dispatchers_; |
| 171 |
168 DISALLOW_COPY_AND_ASSIGN(AudioManagerBase); | 172 DISALLOW_COPY_AND_ASSIGN(AudioManagerBase); |
169 }; | 173 }; |
170 | 174 |
171 } // namespace media | 175 } // namespace media |
172 | 176 |
173 #endif // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_ | 177 #endif // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_ |
OLD | NEW |