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_INPUT_CONTROLLER_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_ |
6 #define MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_ | 6 #define MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include "base/atomicops.h" | 9 #include "base/atomicops.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
17 #include "media/audio/audio_io.h" | 17 #include "media/audio/audio_io.h" |
18 #include "media/audio/audio_manager_base.h" | 18 #include "media/audio/audio_manager_base.h" |
19 #include "media/base/user_input_monitor.h" | |
20 | 19 |
21 // An AudioInputController controls an AudioInputStream and records data | 20 // An AudioInputController controls an AudioInputStream and records data |
22 // from this input stream. The two main methods are Record() and Close() and | 21 // from this input stream. The two main methods are Record() and Close() and |
23 // they are both executed on the audio thread which is injected by the two | 22 // they are both executed on the audio thread which is injected by the two |
24 // alternative factory methods, Create() or CreateLowLatency(). | 23 // alternative factory methods, Create() or CreateLowLatency(). |
25 // | 24 // |
26 // All public methods of AudioInputController are non-blocking. | 25 // All public methods of AudioInputController are non-blocking. |
27 // | 26 // |
28 // Here is a state diagram for the AudioInputController: | 27 // Here is a state diagram for the AudioInputController: |
29 // | 28 // |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 // | 67 // |
69 // The audio thread itself is owned by the AudioManager that the | 68 // The audio thread itself is owned by the AudioManager that the |
70 // AudioInputController holds a reference to. When performing tasks on the | 69 // AudioInputController holds a reference to. When performing tasks on the |
71 // audio thread, the controller must not add or release references to the | 70 // audio thread, the controller must not add or release references to the |
72 // AudioManager or itself (since it in turn holds a reference to the manager). | 71 // AudioManager or itself (since it in turn holds a reference to the manager). |
73 // | 72 // |
74 namespace media { | 73 namespace media { |
75 | 74 |
76 class MEDIA_EXPORT AudioInputController | 75 class MEDIA_EXPORT AudioInputController |
77 : public base::RefCountedThreadSafe<AudioInputController>, | 76 : public base::RefCountedThreadSafe<AudioInputController>, |
78 public AudioInputStream::AudioInputCallback, | 77 public AudioInputStream::AudioInputCallback { |
79 public UserInputMonitor::KeyStrokeListener { | |
80 public: | 78 public: |
81 // An event handler that receives events from the AudioInputController. The | 79 // An event handler that receives events from the AudioInputController. The |
82 // following methods are all called on the audio thread. | 80 // following methods are all called on the audio thread. |
83 class MEDIA_EXPORT EventHandler { | 81 class MEDIA_EXPORT EventHandler { |
84 public: | 82 public: |
85 virtual void OnCreated(AudioInputController* controller) = 0; | 83 virtual void OnCreated(AudioInputController* controller) = 0; |
86 virtual void OnRecording(AudioInputController* controller) = 0; | 84 virtual void OnRecording(AudioInputController* controller) = 0; |
87 virtual void OnError(AudioInputController* controller) = 0; | 85 virtual void OnError(AudioInputController* controller) = 0; |
88 virtual void OnData(AudioInputController* controller, const uint8* data, | 86 virtual void OnData(AudioInputController* controller, const uint8* data, |
89 uint32 size) = 0; | 87 uint32 size) = 0; |
90 | 88 |
91 protected: | 89 protected: |
92 virtual ~EventHandler() {} | 90 virtual ~EventHandler() {} |
93 }; | 91 }; |
94 | 92 |
95 // A synchronous writer interface used by AudioInputController for | 93 // A synchronous writer interface used by AudioInputController for |
96 // synchronous writing. | 94 // synchronous writing. |
97 class SyncWriter { | 95 class SyncWriter { |
98 public: | 96 public: |
99 virtual ~SyncWriter() {} | 97 virtual ~SyncWriter() {} |
100 | 98 |
101 // Notify the synchronous writer about the number of bytes in the | 99 // Notify the synchronous writer about the number of bytes in the |
102 // soundcard which has been recorded. | 100 // soundcard which has been recorded. |
103 virtual void UpdateRecordedBytes(uint32 bytes) = 0; | 101 virtual void UpdateRecordedBytes(uint32 bytes) = 0; |
104 | 102 |
105 // Write certain amount of data from |data|. This method returns | 103 // Write certain amount of data from |data|. This method returns |
106 // number of written bytes. | 104 // number of written bytes. |
107 virtual uint32 Write(const void* data, | 105 virtual uint32 Write(const void* data, uint32 size, double volume) = 0; |
108 uint32 size, | |
109 double volume, | |
110 bool key_pressed) = 0; | |
111 | 106 |
112 // Close this synchronous writer. | 107 // Close this synchronous writer. |
113 virtual void Close() = 0; | 108 virtual void Close() = 0; |
114 }; | 109 }; |
115 | 110 |
116 // AudioInputController::Create() can use the currently registered Factory | 111 // AudioInputController::Create() can use the currently registered Factory |
117 // to create the AudioInputController. Factory is intended for testing only. | 112 // to create the AudioInputController. Factory is intended for testing only. |
118 // |user_input_monitor| is used for typing detection and can be NULL. | |
119 class Factory { | 113 class Factory { |
120 public: | 114 public: |
121 virtual AudioInputController* Create( | 115 virtual AudioInputController* Create(AudioManager* audio_manager, |
122 AudioManager* audio_manager, | 116 EventHandler* event_handler, |
123 EventHandler* event_handler, | 117 AudioParameters params) = 0; |
124 AudioParameters params, | |
125 UserInputMonitor* user_input_monitor) = 0; | |
126 | |
127 protected: | 118 protected: |
128 virtual ~Factory() {} | 119 virtual ~Factory() {} |
129 }; | 120 }; |
130 | 121 |
131 // Factory method for creating an AudioInputController. | 122 // Factory method for creating an AudioInputController. |
132 // The audio device will be created on the audio thread, and when that is | 123 // The audio device will be created on the audio thread, and when that is |
133 // done, the event handler will receive an OnCreated() call from that same | 124 // done, the event handler will receive an OnCreated() call from that same |
134 // thread. |device_id| is the unique ID of the audio device to be opened. | 125 // thread. |device_id| is the unique ID of the audio device to be opened. |
135 // |user_input_monitor| is used for typing detection and can be NULL. | |
136 static scoped_refptr<AudioInputController> Create( | 126 static scoped_refptr<AudioInputController> Create( |
137 AudioManager* audio_manager, | 127 AudioManager* audio_manager, |
138 EventHandler* event_handler, | 128 EventHandler* event_handler, |
139 const AudioParameters& params, | 129 const AudioParameters& params, |
140 const std::string& device_id, | 130 const std::string& device_id); |
141 UserInputMonitor* user_input_monitor); | |
142 | 131 |
143 // Sets the factory used by the static method Create(). AudioInputController | 132 // Sets the factory used by the static method Create(). AudioInputController |
144 // does not take ownership of |factory|. A value of NULL results in an | 133 // does not take ownership of |factory|. A value of NULL results in an |
145 // AudioInputController being created directly. | 134 // AudioInputController being created directly. |
146 static void set_factory_for_testing(Factory* factory) { factory_ = factory; } | 135 static void set_factory_for_testing(Factory* factory) { factory_ = factory; } |
147 AudioInputStream* stream_for_testing() { return stream_; } | 136 AudioInputStream* stream_for_testing() { return stream_; } |
148 | 137 |
149 // Factory method for creating an AudioInputController for low-latency mode. | 138 // Factory method for creating an AudioInputController for low-latency mode. |
150 // The audio device will be created on the audio thread, and when that is | 139 // The audio device will be created on the audio thread, and when that is |
151 // done, the event handler will receive an OnCreated() call from that same | 140 // done, the event handler will receive an OnCreated() call from that same |
152 // thread. |user_input_monitor| is used for typing detection and can be NULL. | 141 // thread. |
153 static scoped_refptr<AudioInputController> CreateLowLatency( | 142 static scoped_refptr<AudioInputController> CreateLowLatency( |
154 AudioManager* audio_manager, | 143 AudioManager* audio_manager, |
155 EventHandler* event_handler, | 144 EventHandler* event_handler, |
156 const AudioParameters& params, | 145 const AudioParameters& params, |
157 const std::string& device_id, | 146 const std::string& device_id, |
158 // External synchronous writer for audio controller. | 147 // External synchronous writer for audio controller. |
159 SyncWriter* sync_writer, | 148 SyncWriter* sync_writer); |
160 UserInputMonitor* user_input_monitor); | |
161 | 149 |
162 // Factory method for creating an AudioInputController for low-latency mode, | 150 // Factory method for creating an AudioInputController for low-latency mode, |
163 // taking ownership of |stream|. The stream will be opened on the audio | 151 // taking ownership of |stream|. The stream will be opened on the audio |
164 // thread, and when that is done, the event handler will receive an | 152 // thread, and when that is done, the event handler will receive an |
165 // OnCreated() call from that same thread. |user_input_monitor| is used for | 153 // OnCreated() call from that same thread. |
166 // typing detection and can be NULL. | |
167 static scoped_refptr<AudioInputController> CreateForStream( | 154 static scoped_refptr<AudioInputController> CreateForStream( |
168 const scoped_refptr<base::MessageLoopProxy>& message_loop, | 155 const scoped_refptr<base::MessageLoopProxy>& message_loop, |
169 EventHandler* event_handler, | 156 EventHandler* event_handler, |
170 AudioInputStream* stream, | 157 AudioInputStream* stream, |
171 // External synchronous writer for audio controller. | 158 // External synchronous writer for audio controller. |
172 SyncWriter* sync_writer, | 159 SyncWriter* sync_writer); |
173 UserInputMonitor* user_input_monitor); | |
174 | 160 |
175 // Starts recording using the created audio input stream. | 161 // Starts recording using the created audio input stream. |
176 // This method is called on the creator thread. | 162 // This method is called on the creator thread. |
177 virtual void Record(); | 163 virtual void Record(); |
178 | 164 |
179 // Closes the audio input stream. The state is changed and the resources | 165 // Closes the audio input stream. The state is changed and the resources |
180 // are freed on the audio thread. |closed_task| is then executed on the thread | 166 // are freed on the audio thread. |closed_task| is then executed on the thread |
181 // that called Close(). | 167 // that called Close(). |
182 // Callbacks (EventHandler and SyncWriter) must exist until |closed_task| | 168 // Callbacks (EventHandler and SyncWriter) must exist until |closed_task| |
183 // is called. | 169 // is called. |
(...skipping 12 matching lines...) Expand all Loading... |
196 | 182 |
197 // AudioInputCallback implementation. Threading details depends on the | 183 // AudioInputCallback implementation. Threading details depends on the |
198 // device-specific implementation. | 184 // device-specific implementation. |
199 virtual void OnData(AudioInputStream* stream, const uint8* src, uint32 size, | 185 virtual void OnData(AudioInputStream* stream, const uint8* src, uint32 size, |
200 uint32 hardware_delay_bytes, double volume) OVERRIDE; | 186 uint32 hardware_delay_bytes, double volume) OVERRIDE; |
201 virtual void OnClose(AudioInputStream* stream) OVERRIDE; | 187 virtual void OnClose(AudioInputStream* stream) OVERRIDE; |
202 virtual void OnError(AudioInputStream* stream) OVERRIDE; | 188 virtual void OnError(AudioInputStream* stream) OVERRIDE; |
203 | 189 |
204 bool LowLatencyMode() const { return sync_writer_ != NULL; } | 190 bool LowLatencyMode() const { return sync_writer_ != NULL; } |
205 | 191 |
206 // Impl of KeyStrokeListener. | |
207 virtual void OnKeyStroke() OVERRIDE; | |
208 | |
209 protected: | 192 protected: |
210 friend class base::RefCountedThreadSafe<AudioInputController>; | 193 friend class base::RefCountedThreadSafe<AudioInputController>; |
211 | 194 |
212 // Internal state of the source. | 195 // Internal state of the source. |
213 enum State { | 196 enum State { |
214 kEmpty, | 197 kEmpty, |
215 kCreated, | 198 kCreated, |
216 kRecording, | 199 kRecording, |
217 kClosed, | 200 kClosed, |
218 kError | 201 kError |
219 }; | 202 }; |
220 | 203 |
221 AudioInputController(EventHandler* handler, | 204 AudioInputController(EventHandler* handler, SyncWriter* sync_writer); |
222 SyncWriter* sync_writer, | |
223 UserInputMonitor* user_input_monitor); | |
224 virtual ~AudioInputController(); | 205 virtual ~AudioInputController(); |
225 | 206 |
226 // Methods called on the audio thread (owned by the AudioManager). | 207 // Methods called on the audio thread (owned by the AudioManager). |
227 void DoCreate(AudioManager* audio_manager, const AudioParameters& params, | 208 void DoCreate(AudioManager* audio_manager, const AudioParameters& params, |
228 const std::string& device_id); | 209 const std::string& device_id); |
229 void DoCreateForStream(AudioInputStream* stream_to_control, | 210 void DoCreateForStream(AudioInputStream* stream_to_control, |
230 bool enable_nodata_timer); | 211 bool enable_nodata_timer); |
231 void DoRecord(); | 212 void DoRecord(); |
232 void DoClose(); | 213 void DoClose(); |
233 void DoReportError(); | 214 void DoReportError(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 | 259 |
279 base::Lock lock_; | 260 base::Lock lock_; |
280 | 261 |
281 // SyncWriter is used only in low-latency mode for synchronous writing. | 262 // SyncWriter is used only in low-latency mode for synchronous writing. |
282 SyncWriter* sync_writer_; | 263 SyncWriter* sync_writer_; |
283 | 264 |
284 static Factory* factory_; | 265 static Factory* factory_; |
285 | 266 |
286 double max_volume_; | 267 double max_volume_; |
287 | 268 |
288 UserInputMonitor* user_input_monitor_; | |
289 | |
290 // True if any key has been pressed after the last OnData call. | |
291 bool key_pressed_; | |
292 | |
293 DISALLOW_COPY_AND_ASSIGN(AudioInputController); | 269 DISALLOW_COPY_AND_ASSIGN(AudioInputController); |
294 }; | 270 }; |
295 | 271 |
296 } // namespace media | 272 } // namespace media |
297 | 273 |
298 #endif // MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_ | 274 #endif // MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_ |
OLD | NEW |