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