OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "media/audio/audio_input_controller.h" | 5 #include "media/audio/audio_input_controller.h" |
6 | 6 |
7 #include "base/threading/thread_restrictions.h" | 7 #include "base/threading/thread_restrictions.h" |
8 #include "media/base/limits.h" | 8 #include "media/base/limits.h" |
9 | 9 |
| 10 namespace { |
| 11 const int kMaxInputChannels = 2; |
| 12 const int kTimerResetInterval = 1; // One second. |
| 13 } |
| 14 |
10 namespace media { | 15 namespace media { |
11 | 16 |
12 static const int kMaxInputChannels = 2; | |
13 | |
14 // static | 17 // static |
15 AudioInputController::Factory* AudioInputController::factory_ = NULL; | 18 AudioInputController::Factory* AudioInputController::factory_ = NULL; |
16 | 19 |
17 AudioInputController::AudioInputController(EventHandler* handler, | 20 AudioInputController::AudioInputController(EventHandler* handler, |
18 SyncWriter* sync_writer) | 21 SyncWriter* sync_writer) |
19 : handler_(handler), | 22 : handler_(handler), |
20 stream_(NULL), | 23 stream_(NULL), |
| 24 ALLOW_THIS_IN_INITIALIZER_LIST(no_data_timer_( |
| 25 base::TimeDelta::FromSeconds(kTimerResetInterval), |
| 26 this, |
| 27 &AudioInputController::DoReportNoDataError)), |
21 state_(kEmpty), | 28 state_(kEmpty), |
22 thread_("AudioInputControllerThread"), | 29 thread_("AudioInputControllerThread"), |
23 sync_writer_(sync_writer) { | 30 sync_writer_(sync_writer) { |
24 } | 31 } |
25 | 32 |
26 AudioInputController::~AudioInputController() { | 33 AudioInputController::~AudioInputController() { |
27 DCHECK(kClosed == state_ || kCreated == state_ || kEmpty == state_); | 34 DCHECK(kClosed == state_ || kCreated == state_ || kEmpty == state_); |
28 } | 35 } |
29 | 36 |
30 // static | 37 // static |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 } | 123 } |
117 | 124 |
118 if (stream_ && !stream_->Open()) { | 125 if (stream_ && !stream_->Open()) { |
119 stream_->Close(); | 126 stream_->Close(); |
120 stream_ = NULL; | 127 stream_ = NULL; |
121 // TODO(satish): Define error types. | 128 // TODO(satish): Define error types. |
122 handler_->OnError(this, 0); | 129 handler_->OnError(this, 0); |
123 return; | 130 return; |
124 } | 131 } |
125 | 132 |
| 133 thread_.message_loop()->PostTask( |
| 134 FROM_HERE, |
| 135 NewRunnableMethod(this, &AudioInputController::DoResetNoDataTimer)); |
126 state_ = kCreated; | 136 state_ = kCreated; |
127 handler_->OnCreated(this); | 137 handler_->OnCreated(this); |
128 } | 138 } |
129 | 139 |
130 void AudioInputController::DoRecord() { | 140 void AudioInputController::DoRecord() { |
131 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); | 141 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); |
132 | 142 |
133 if (state_ != kCreated) | 143 if (state_ != kCreated) |
134 return; | 144 return; |
135 | 145 |
(...skipping 25 matching lines...) Expand all Loading... |
161 // Since the stream is closed at this point there's no other threads reading | 171 // Since the stream is closed at this point there's no other threads reading |
162 // |state_| so we don't need to lock. | 172 // |state_| so we don't need to lock. |
163 state_ = kClosed; | 173 state_ = kClosed; |
164 } | 174 } |
165 | 175 |
166 void AudioInputController::DoReportError(int code) { | 176 void AudioInputController::DoReportError(int code) { |
167 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); | 177 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); |
168 handler_->OnError(this, code); | 178 handler_->OnError(this, code); |
169 } | 179 } |
170 | 180 |
| 181 void AudioInputController::DoReportNoDataError() { |
| 182 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); |
| 183 handler_->OnError(this, 0); |
| 184 } |
| 185 |
| 186 void AudioInputController::DoResetNoDataTimer() { |
| 187 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); |
| 188 no_data_timer_.Reset(); |
| 189 } |
| 190 |
171 void AudioInputController::OnData(AudioInputStream* stream, const uint8* data, | 191 void AudioInputController::OnData(AudioInputStream* stream, const uint8* data, |
172 uint32 size) { | 192 uint32 size) { |
173 { | 193 { |
174 base::AutoLock auto_lock(lock_); | 194 base::AutoLock auto_lock(lock_); |
175 if (state_ != kRecording) | 195 if (state_ != kRecording) |
176 return; | 196 return; |
177 } | 197 } |
178 | 198 |
| 199 thread_.message_loop()->PostTask( |
| 200 FROM_HERE, |
| 201 NewRunnableMethod(this, &AudioInputController::DoResetNoDataTimer)); |
| 202 |
179 // Use SyncSocket if we are in a low-latency mode. | 203 // Use SyncSocket if we are in a low-latency mode. |
180 if (LowLatencyMode()) { | 204 if (LowLatencyMode()) { |
181 sync_writer_->Write(data, size); | 205 sync_writer_->Write(data, size); |
182 sync_writer_->UpdateRecordedBytes(size); | 206 sync_writer_->UpdateRecordedBytes(size); |
183 return; | 207 return; |
184 } | 208 } |
185 | 209 |
186 handler_->OnData(this, data, size); | 210 handler_->OnData(this, data, size); |
187 } | 211 } |
188 | 212 |
189 void AudioInputController::OnClose(AudioInputStream* stream) { | 213 void AudioInputController::OnClose(AudioInputStream* stream) { |
190 // TODO(satish): Sometimes the device driver closes the input stream without | 214 // TODO(satish): Sometimes the device driver closes the input stream without |
191 // us asking for it (may be if the device was unplugged?). Check how to handle | 215 // us asking for it (may be if the device was unplugged?). Check how to handle |
192 // such cases here. | 216 // such cases here. |
193 } | 217 } |
194 | 218 |
195 void AudioInputController::OnError(AudioInputStream* stream, int code) { | 219 void AudioInputController::OnError(AudioInputStream* stream, int code) { |
196 // Handle error on the audio controller thread. | 220 // Handle error on the audio controller thread. |
197 thread_.message_loop()->PostTask( | 221 thread_.message_loop()->PostTask( |
198 FROM_HERE, | 222 FROM_HERE, |
199 NewRunnableMethod(this, &AudioInputController::DoReportError, code)); | 223 NewRunnableMethod(this, &AudioInputController::DoReportError, code)); |
200 } | 224 } |
201 | 225 |
202 } // namespace media | 226 } // namespace media |
OLD | NEW |