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 #include "media/audio/mac/audio_input_mac.h" | 5 #include "media/audio/mac/audio_input_mac.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/mac/mac_logging.h" | 9 #include "base/mac/mac_logging.h" |
10 #include "media/audio/audio_manager_base.h" | 10 #include "media/audio/audio_manager_base.h" |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 // This can happen if Stop() was called without start. | 190 // This can happen if Stop() was called without start. |
191 DCHECK_EQ(0U, audio_buffer->mAudioDataByteSize); | 191 DCHECK_EQ(0U, audio_buffer->mAudioDataByteSize); |
192 return; | 192 return; |
193 } | 193 } |
194 | 194 |
195 if (audio_buffer->mAudioDataByteSize) { | 195 if (audio_buffer->mAudioDataByteSize) { |
196 // The AudioQueue API may use a large internal buffer and repeatedly call us | 196 // The AudioQueue API may use a large internal buffer and repeatedly call us |
197 // back to back once that internal buffer is filled. When this happens the | 197 // back to back once that internal buffer is filled. When this happens the |
198 // renderer client does not have enough time to read data back from the | 198 // renderer client does not have enough time to read data back from the |
199 // shared memory before the next write comes along. If HandleInputBuffer() | 199 // shared memory before the next write comes along. If HandleInputBuffer() |
200 // is called too frequently, Sleep() to simulate realtime input and ensure | 200 // is called too frequently, Sleep() at least 5ms to ensure the shared |
201 // the shared memory doesn't get trampled. | 201 // memory doesn't get trampled. |
202 // TODO(dalecurtis): This is a HACK. Long term the AudioQueue path is going | 202 // TODO(dalecurtis): This is a HACK. Long term the AudioQueue path is going |
203 // away in favor of the AudioUnit based AUAudioInputStream(). Tracked by | 203 // away in favor of the AudioUnit based AUAudioInputStream(). Tracked by |
204 // http://crbug.com/161383 | 204 // http://crbug.com/161383. |
205 base::TimeDelta elapsed = base::Time::Now() - last_fill_; | 205 base::TimeDelta elapsed = base::Time::Now() - last_fill_; |
206 base::TimeDelta buffer_length = base::TimeDelta::FromMilliseconds( | 206 const base::TimeDelta kMinDelay = base::TimeDelta::FromMilliseconds(5); |
207 audio_buffer->mAudioDataByteSize * base::Time::kMillisecondsPerSecond / | 207 if (elapsed < kMinDelay) |
208 static_cast<float>(format_.mBytesPerFrame * format_.mSampleRate)); | 208 base::PlatformThread::Sleep(kMinDelay - elapsed); |
209 if (elapsed < buffer_length) | |
210 base::PlatformThread::Sleep(buffer_length - elapsed); | |
211 | 209 |
212 callback_->OnData(this, | 210 callback_->OnData(this, |
213 reinterpret_cast<const uint8*>(audio_buffer->mAudioData), | 211 reinterpret_cast<const uint8*>(audio_buffer->mAudioData), |
214 audio_buffer->mAudioDataByteSize, | 212 audio_buffer->mAudioDataByteSize, |
215 audio_buffer->mAudioDataByteSize, | 213 audio_buffer->mAudioDataByteSize, |
216 0.0); | 214 0.0); |
217 | 215 |
218 last_fill_ = base::Time::Now(); | 216 last_fill_ = base::Time::Now(); |
219 } | 217 } |
220 // Recycle the buffer. | 218 // Recycle the buffer. |
221 OSStatus err = QueueNextBuffer(audio_buffer); | 219 OSStatus err = QueueNextBuffer(audio_buffer); |
222 if (err != noErr) { | 220 if (err != noErr) { |
223 if (err == kAudioQueueErr_EnqueueDuringReset) { | 221 if (err == kAudioQueueErr_EnqueueDuringReset) { |
224 // This is the error you get if you try to enqueue a buffer and the | 222 // This is the error you get if you try to enqueue a buffer and the |
225 // queue has been closed. Not really a problem if indeed the queue | 223 // queue has been closed. Not really a problem if indeed the queue |
226 // has been closed. | 224 // has been closed. |
227 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an | 225 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an |
228 // extra guard for this situation, but it seems to introduce more | 226 // extra guard for this situation, but it seems to introduce more |
229 // complications than it solves (memory barrier issues accessing it from | 227 // complications than it solves (memory barrier issues accessing it from |
230 // multiple threads, looses the means to indicate OnClosed to client). | 228 // multiple threads, looses the means to indicate OnClosed to client). |
231 // Should determine if we need to do something equivalent here. | 229 // Should determine if we need to do something equivalent here. |
232 return; | 230 return; |
233 } | 231 } |
234 HandleError(err); | 232 HandleError(err); |
235 } | 233 } |
236 } | 234 } |
237 | 235 |
238 } // namespace media | 236 } // namespace media |
OLD | NEW |