Chromium Code Reviews| 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 base::TimeDelta elapsed = base::Time::Now() - last_fill_; | |
| 203 base::TimeDelta buffer_length = base::TimeDelta::FromMilliseconds( | |
| 204 audio_buffer->mAudioDataByteSize * base::Time::kMillisecondsPerSecond / | |
| 205 static_cast<float>(format_.mBytesPerFrame * format_.mSampleRate)); | |
| 206 if (elapsed < buffer_length) | |
| 207 base::PlatformThread::Sleep(buffer_length - elapsed); | |
|
Chris Rogers
2012/12/06 23:19:55
As we discussed in offline, this is really a stop-
DaleCurtis
2012/12/06 23:41:07
Added a comment. As discussed offline, the calling
| |
| 208 | |
| 196 callback_->OnData(this, | 209 callback_->OnData(this, |
| 197 reinterpret_cast<const uint8*>(audio_buffer->mAudioData), | 210 reinterpret_cast<const uint8*>(audio_buffer->mAudioData), |
| 198 audio_buffer->mAudioDataByteSize, | 211 audio_buffer->mAudioDataByteSize, |
| 199 audio_buffer->mAudioDataByteSize, | 212 audio_buffer->mAudioDataByteSize, |
| 200 0.0); | 213 0.0); |
| 214 | |
| 215 last_fill_ = base::Time::Now(); | |
| 216 } | |
| 201 // Recycle the buffer. | 217 // Recycle the buffer. |
| 202 OSStatus err = QueueNextBuffer(audio_buffer); | 218 OSStatus err = QueueNextBuffer(audio_buffer); |
| 203 if (err != noErr) { | 219 if (err != noErr) { |
| 204 if (err == kAudioQueueErr_EnqueueDuringReset) { | 220 if (err == kAudioQueueErr_EnqueueDuringReset) { |
| 205 // This is the error you get if you try to enqueue a buffer and the | 221 // 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 | 222 // queue has been closed. Not really a problem if indeed the queue |
| 207 // has been closed. | 223 // has been closed. |
| 208 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an | 224 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an |
| 209 // extra guard for this situation, but it seems to introduce more | 225 // extra guard for this situation, but it seems to introduce more |
| 210 // complications than it solves (memory barrier issues accessing it from | 226 // complications than it solves (memory barrier issues accessing it from |
| 211 // multiple threads, looses the means to indicate OnClosed to client). | 227 // multiple threads, looses the means to indicate OnClosed to client). |
| 212 // Should determine if we need to do something equivalent here. | 228 // Should determine if we need to do something equivalent here. |
| 213 return; | 229 return; |
| 214 } | 230 } |
| 215 HandleError(err); | 231 HandleError(err); |
| 216 } | 232 } |
| 217 } | 233 } |
| 218 | 234 |
| 219 } // namespace media | 235 } // namespace media |
| OLD | NEW |