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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 UInt32 num_packets, | 185 UInt32 num_packets, |
186 const AudioStreamPacketDescription* packet_desc) { | 186 const AudioStreamPacketDescription* packet_desc) { |
187 DCHECK_EQ(audio_queue_, audio_queue); | 187 DCHECK_EQ(audio_queue_, audio_queue); |
188 DCHECK(audio_buffer->mAudioData); | 188 DCHECK(audio_buffer->mAudioData); |
189 if (!callback_) { | 189 if (!callback_) { |
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 |
| 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 |
| 199 // shared memory before the next write comes along. If HandleInputBuffer() |
| 200 // is called too frequently, Sleep() to simulate realtime input and ensure |
| 201 // the shared memory doesn't get trampled. |
| 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 |
| 204 // http://crbug.com/161383 |
| 205 base::TimeDelta elapsed = base::Time::Now() - last_fill_; |
| 206 base::TimeDelta buffer_length = base::TimeDelta::FromMilliseconds( |
| 207 audio_buffer->mAudioDataByteSize * base::Time::kMillisecondsPerSecond / |
| 208 static_cast<float>(format_.mBytesPerFrame * format_.mSampleRate)); |
| 209 if (elapsed < buffer_length) |
| 210 base::PlatformThread::Sleep(buffer_length - elapsed); |
| 211 |
196 callback_->OnData(this, | 212 callback_->OnData(this, |
197 reinterpret_cast<const uint8*>(audio_buffer->mAudioData), | 213 reinterpret_cast<const uint8*>(audio_buffer->mAudioData), |
198 audio_buffer->mAudioDataByteSize, | 214 audio_buffer->mAudioDataByteSize, |
199 audio_buffer->mAudioDataByteSize, | 215 audio_buffer->mAudioDataByteSize, |
200 0.0); | 216 0.0); |
| 217 |
| 218 last_fill_ = base::Time::Now(); |
| 219 } |
201 // Recycle the buffer. | 220 // Recycle the buffer. |
202 OSStatus err = QueueNextBuffer(audio_buffer); | 221 OSStatus err = QueueNextBuffer(audio_buffer); |
203 if (err != noErr) { | 222 if (err != noErr) { |
204 if (err == kAudioQueueErr_EnqueueDuringReset) { | 223 if (err == kAudioQueueErr_EnqueueDuringReset) { |
205 // This is the error you get if you try to enqueue a buffer and the | 224 // This is the error you get if you try to enqueue a buffer and the |
206 // queue has been closed. Not really a problem if indeed the queue | 225 // queue has been closed. Not really a problem if indeed the queue |
207 // has been closed. | 226 // has been closed. |
208 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an | 227 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an |
209 // extra guard for this situation, but it seems to introduce more | 228 // extra guard for this situation, but it seems to introduce more |
210 // complications than it solves (memory barrier issues accessing it from | 229 // complications than it solves (memory barrier issues accessing it from |
211 // multiple threads, looses the means to indicate OnClosed to client). | 230 // multiple threads, looses the means to indicate OnClosed to client). |
212 // Should determine if we need to do something equivalent here. | 231 // Should determine if we need to do something equivalent here. |
213 return; | 232 return; |
214 } | 233 } |
215 HandleError(err); | 234 HandleError(err); |
216 } | 235 } |
217 } | 236 } |
218 | 237 |
219 } // namespace media | 238 } // namespace media |
OLD | NEW |