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/callback.h" | 10 #include "base/callback.h" |
10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
13 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
15 #include "base/timer.h" | 16 #include "base/timer.h" |
16 #include "media/audio/audio_io.h" | 17 #include "media/audio/audio_io.h" |
17 #include "media/audio/audio_manager_base.h" | 18 #include "media/audio/audio_manager_base.h" |
18 | 19 |
(...skipping 16 matching lines...) Expand all Loading... |
35 // | 36 // |
36 // * Initial state | 37 // * Initial state |
37 // | 38 // |
38 // State sequences (assuming low-latency): | 39 // State sequences (assuming low-latency): |
39 // | 40 // |
40 // [Creating Thread] [Audio Thread] | 41 // [Creating Thread] [Audio Thread] |
41 // | 42 // |
42 // User AudioInputController EventHandler | 43 // User AudioInputController EventHandler |
43 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 44 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
44 // CrateLowLatency() ==> DoCreate() | 45 // CrateLowLatency() ==> DoCreate() |
45 // AudioManager::MakeAudioInputStream() | 46 // AudioManager::MakeAudioInputStream() |
46 // AudioInputStream::Open() | 47 // AudioInputStream::Open() |
47 // .- - - - - - - - - - - - -> OnError() | 48 // .- - - - - - - - - - - - -> OnError() |
48 // DoResetNoDataTimer (posted on creating tread) | 49 // create the data timer |
49 // .-------------------------> OnCreated() | 50 // .-------------------------> OnCreated() |
50 // kCreated | 51 // kCreated |
51 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 52 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
52 // Record() ==> DoRecord() | 53 // Record() ==> DoRecord() |
53 // AudioInputStream::Start() | 54 // AudioInputStream::Start() |
54 // .-------------------------> OnRecording() | 55 // .-------------------------> OnRecording() |
| 56 // start the data timer |
55 // kRecording | 57 // kRecording |
56 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 58 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
57 // Close() ==> DoClose() | 59 // Close() ==> DoClose() |
58 // state_ = kClosed | 60 // delete the data timer |
59 // AudioInputStream::Stop() | 61 // state_ = kClosed |
60 // AudioInputStream::Close() | 62 // AudioInputStream::Stop() |
61 // SyncWriter::Close() | 63 // AudioInputStream::Close() |
62 // Closure::Run() <--------------. | 64 // SyncWriter::Close() |
| 65 // Closure::Run() <-----------------. |
63 // (closure-task) | 66 // (closure-task) |
64 // | 67 // |
65 // The audio thread itself is owned by the AudioManager that the | 68 // The audio thread itself is owned by the AudioManager that the |
66 // AudioInputController holds a reference to. When performing tasks on the | 69 // AudioInputController holds a reference to. When performing tasks on the |
67 // 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 |
68 // 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). |
69 // | 72 // |
70 namespace media { | 73 namespace media { |
71 | 74 |
72 class MEDIA_EXPORT AudioInputController | 75 class MEDIA_EXPORT AudioInputController |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 | 194 |
192 // Methods called on the audio thread (owned by the AudioManager). | 195 // Methods called on the audio thread (owned by the AudioManager). |
193 void DoCreate(AudioManager* audio_manager, const AudioParameters& params, | 196 void DoCreate(AudioManager* audio_manager, const AudioParameters& params, |
194 const std::string& device_id); | 197 const std::string& device_id); |
195 void DoRecord(); | 198 void DoRecord(); |
196 void DoClose(); | 199 void DoClose(); |
197 void DoReportError(int code); | 200 void DoReportError(int code); |
198 void DoSetVolume(double volume); | 201 void DoSetVolume(double volume); |
199 void DoSetAutomaticGainControl(bool enabled); | 202 void DoSetAutomaticGainControl(bool enabled); |
200 | 203 |
201 // Methods which ensures that OnError() is triggered when data recording | 204 // Method which ensures that OnError() is triggered when data recording |
202 // times out. Both are called on the creating thread. | 205 // times out. Called on the audio thread. |
203 void DoReportNoDataError(); | 206 void DoCheckForNoData(); |
204 void DoResetNoDataTimer(); | |
205 | 207 |
206 // Helper method that stops, closes, and NULL:s |*stream_|. | 208 // Helper method that stops, closes, and NULL:s |*stream_|. |
207 // Signals event when done if the event is not NULL. | 209 // Signals event when done if the event is not NULL. |
208 void DoStopCloseAndClearStream(base::WaitableEvent* done); | 210 void DoStopCloseAndClearStream(base::WaitableEvent* done); |
209 | 211 |
| 212 void SetDataIsActive(bool enabled); |
| 213 bool GetDataIsActive(); |
| 214 |
210 // Gives access to the message loop of the creating thread. | 215 // Gives access to the message loop of the creating thread. |
211 scoped_refptr<base::MessageLoopProxy> creator_loop_; | 216 scoped_refptr<base::MessageLoopProxy> creator_loop_; |
212 | 217 |
213 // The message loop of audio-manager thread that this object runs on. | 218 // The message loop of audio-manager thread that this object runs on. |
214 scoped_refptr<base::MessageLoopProxy> message_loop_; | 219 scoped_refptr<base::MessageLoopProxy> message_loop_; |
215 | 220 |
216 // Contains the AudioInputController::EventHandler which receives state | 221 // Contains the AudioInputController::EventHandler which receives state |
217 // notifications from this class. | 222 // notifications from this class. |
218 EventHandler* handler_; | 223 EventHandler* handler_; |
219 | 224 |
220 // Pointer to the audio input stream object. | 225 // Pointer to the audio input stream object. |
221 AudioInputStream* stream_; | 226 AudioInputStream* stream_; |
222 | 227 |
223 // |no_data_timer_| is used to call DoReportNoDataError() when we stop | 228 // |no_data_timer_| is used to call OnError() when we stop receiving |
224 // receiving OnData() calls without an OnClose() call. This can occur | 229 // OnData() calls without an OnClose() call. This can occur |
225 // when an audio input device is unplugged whilst recording on Windows. | 230 // when an audio input device is unplugged whilst recording on Windows. |
226 // See http://crbug.com/79936 for details. | 231 // See http://crbug.com/79936 for details. |
227 // This member is only touched by the creating thread. | 232 // This member is only touched by the audio thread. |
228 scoped_ptr<base::DelayTimer<AudioInputController> > no_data_timer_; | 233 scoped_ptr<base::DelayTimer<AudioInputController> > no_data_timer_; |
229 | 234 |
| 235 // This flag is used to signal that we are receiving OnData() calls, i.e, |
| 236 // that data is active. It can be touched by the audio thread and by the |
| 237 // low-level audio thread which calls OnData(). E.g. on Windows, the |
| 238 // low-level audio thread is called wasapi_capture_thread. |
| 239 base::subtle::Atomic32 data_is_active_; |
| 240 |
230 // |state_| is written on the audio thread and is read on the hardware audio | 241 // |state_| is written on the audio thread and is read on the hardware audio |
231 // thread. These operations need to be locked. But lock is not required for | 242 // thread. These operations need to be locked. But lock is not required for |
232 // reading on the audio input controller thread. | 243 // reading on the audio input controller thread. |
233 State state_; | 244 State state_; |
234 | 245 |
235 base::Lock lock_; | 246 base::Lock lock_; |
236 | 247 |
237 // SyncWriter is used only in low-latency mode for synchronous writing. | 248 // SyncWriter is used only in low-latency mode for synchronous writing. |
238 SyncWriter* sync_writer_; | 249 SyncWriter* sync_writer_; |
239 | 250 |
240 static Factory* factory_; | 251 static Factory* factory_; |
241 | 252 |
242 double max_volume_; | 253 double max_volume_; |
243 | 254 |
244 DISALLOW_COPY_AND_ASSIGN(AudioInputController); | 255 DISALLOW_COPY_AND_ASSIGN(AudioInputController); |
245 }; | 256 }; |
246 | 257 |
247 } // namespace media | 258 } // namespace media |
248 | 259 |
249 #endif // MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_ | 260 #endif // MEDIA_AUDIO_AUDIO_INPUT_CONTROLLER_H_ |
OLD | NEW |