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