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/logging.h" | |
| 7 #include "base/threading/thread_restrictions.h" | 8 #include "base/threading/thread_restrictions.h" |
| 8 #include "media/base/limits.h" | 9 #include "media/base/limits.h" |
| 9 | 10 |
| 10 namespace media { | 11 namespace media { |
| 11 | 12 |
| 12 static const int kMaxInputChannels = 2; | 13 static const int kMaxInputChannels = 2; |
| 14 const int kDataCountCheckInterval = 1000; // One second. | |
| 13 | 15 |
| 14 // static | 16 // static |
| 15 AudioInputController::Factory* AudioInputController::factory_ = NULL; | 17 AudioInputController::Factory* AudioInputController::factory_ = NULL; |
| 16 | 18 |
| 17 AudioInputController::AudioInputController(EventHandler* handler, | 19 AudioInputController::AudioInputController(EventHandler* handler, |
| 18 SyncWriter* sync_writer) | 20 SyncWriter* sync_writer) |
| 19 : handler_(handler), | 21 : handler_(handler), |
| 20 stream_(NULL), | 22 stream_(NULL), |
| 23 on_data_call_count_(1), | |
| 24 previous_on_data_count_(0), | |
| 21 state_(kEmpty), | 25 state_(kEmpty), |
| 22 thread_("AudioInputControllerThread"), | 26 thread_("AudioInputControllerThread"), |
| 23 sync_writer_(sync_writer) { | 27 sync_writer_(sync_writer) { |
| 24 } | 28 } |
| 25 | 29 |
| 26 AudioInputController::~AudioInputController() { | 30 AudioInputController::~AudioInputController() { |
| 27 DCHECK(kClosed == state_ || kCreated == state_ || kEmpty == state_); | 31 DCHECK(kClosed == state_ || kCreated == state_ || kEmpty == state_); |
| 28 } | 32 } |
| 29 | 33 |
| 30 // static | 34 // static |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 } | 120 } |
| 117 | 121 |
| 118 if (stream_ && !stream_->Open()) { | 122 if (stream_ && !stream_->Open()) { |
| 119 stream_->Close(); | 123 stream_->Close(); |
| 120 stream_ = NULL; | 124 stream_ = NULL; |
| 121 // TODO(satish): Define error types. | 125 // TODO(satish): Define error types. |
| 122 handler_->OnError(this, 0); | 126 handler_->OnError(this, 0); |
| 123 return; | 127 return; |
| 124 } | 128 } |
| 125 | 129 |
| 130 ScheduleDataCountCheck(); | |
| 131 | |
| 126 state_ = kCreated; | 132 state_ = kCreated; |
| 127 handler_->OnCreated(this); | 133 handler_->OnCreated(this); |
| 128 } | 134 } |
| 129 | 135 |
| 130 void AudioInputController::DoRecord() { | 136 void AudioInputController::DoRecord() { |
| 131 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); | 137 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); |
| 132 | 138 |
| 133 if (state_ != kCreated) | 139 if (state_ != kCreated) |
| 134 return; | 140 return; |
| 135 | 141 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 161 // Since the stream is closed at this point there's no other threads reading | 167 // Since the stream is closed at this point there's no other threads reading |
| 162 // |state_| so we don't need to lock. | 168 // |state_| so we don't need to lock. |
| 163 state_ = kClosed; | 169 state_ = kClosed; |
| 164 } | 170 } |
| 165 | 171 |
| 166 void AudioInputController::DoReportError(int code) { | 172 void AudioInputController::DoReportError(int code) { |
| 167 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); | 173 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); |
| 168 handler_->OnError(this, code); | 174 handler_->OnError(this, code); |
| 169 } | 175 } |
| 170 | 176 |
| 177 void AudioInputController::DoDataCountCheck() { | |
| 178 DCHECK_EQ(thread_.message_loop(), MessageLoop::current()); | |
| 179 | |
| 180 if (previous_on_data_count_ < on_data_call_count_) { | |
| 181 // More data has arrived since last check. | |
| 182 previous_on_data_count_ = on_data_call_count_; | |
| 183 ScheduleDataCountCheck(); | |
| 184 return; | |
| 185 } | |
| 186 | |
| 187 // Have not received new data since last check so report an error. | |
|
Satish
2011/06/14 14:28:04
It would be useful to explain this more and even a
allanwoj
2011/06/14 15:03:12
Done.
| |
| 188 handler_->OnError(this, 0); | |
| 189 } | |
| 190 | |
| 191 void AudioInputController::ScheduleDataCountCheck() { | |
|
scherkus (not reviewing)
2011/06/13 19:04:56
consider using DelayTimer from base/timer.h
allanwoj
2011/06/14 15:03:12
Decided to keep it the way it is, this way it make
scherkus (not reviewing)
2011/06/14 21:22:02
Not sure I agree... right now I find it more confu
allanwoj
2011/06/15 10:11:40
Oh right, I see what you mean. I've gone ahead and
| |
| 192 thread_.message_loop()->PostDelayedTask( | |
| 193 FROM_HERE, | |
| 194 NewRunnableMethod(this, &AudioInputController::DoDataCountCheck), | |
| 195 kDataCountCheckInterval); | |
| 196 } | |
| 197 | |
| 171 void AudioInputController::OnData(AudioInputStream* stream, const uint8* data, | 198 void AudioInputController::OnData(AudioInputStream* stream, const uint8* data, |
| 172 uint32 size) { | 199 uint32 size) { |
| 173 { | 200 { |
| 174 base::AutoLock auto_lock(lock_); | 201 base::AutoLock auto_lock(lock_); |
| 175 if (state_ != kRecording) | 202 if (state_ != kRecording) |
| 176 return; | 203 return; |
| 177 } | 204 } |
| 178 | 205 |
| 206 ++on_data_call_count_; | |
| 207 | |
| 179 // Use SyncSocket if we are in a low-latency mode. | 208 // Use SyncSocket if we are in a low-latency mode. |
| 180 if (LowLatencyMode()) { | 209 if (LowLatencyMode()) { |
| 181 sync_writer_->Write(data, size); | 210 sync_writer_->Write(data, size); |
| 182 sync_writer_->UpdateRecordedBytes(size); | 211 sync_writer_->UpdateRecordedBytes(size); |
| 183 return; | 212 return; |
| 184 } | 213 } |
| 185 | 214 |
| 186 handler_->OnData(this, data, size); | 215 handler_->OnData(this, data, size); |
| 187 } | 216 } |
| 188 | 217 |
| 189 void AudioInputController::OnClose(AudioInputStream* stream) { | 218 void AudioInputController::OnClose(AudioInputStream* stream) { |
| 190 // TODO(satish): Sometimes the device driver closes the input stream without | 219 // 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 | 220 // us asking for it (may be if the device was unplugged?). Check how to handle |
| 192 // such cases here. | 221 // such cases here. |
| 193 } | 222 } |
| 194 | 223 |
| 195 void AudioInputController::OnError(AudioInputStream* stream, int code) { | 224 void AudioInputController::OnError(AudioInputStream* stream, int code) { |
| 196 // Handle error on the audio controller thread. | 225 // Handle error on the audio controller thread. |
| 197 thread_.message_loop()->PostTask( | 226 thread_.message_loop()->PostTask( |
| 198 FROM_HERE, | 227 FROM_HERE, |
| 199 NewRunnableMethod(this, &AudioInputController::DoReportError, code)); | 228 NewRunnableMethod(this, &AudioInputController::DoReportError, code)); |
| 200 } | 229 } |
| 201 | 230 |
| 202 } // namespace media | 231 } // namespace media |
| OLD | NEW |