| 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 <CoreServices/CoreServices.h> | 7 #include <CoreServices/CoreServices.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/mac/mac_logging.h" | 10 #include "base/mac/mac_logging.h" |
| 11 #include "base/trace_event/trace_event.h" |
| 11 #include "media/audio/mac/audio_manager_mac.h" | 12 #include "media/audio/mac/audio_manager_mac.h" |
| 12 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
| 13 | 14 |
| 14 namespace media { | 15 namespace media { |
| 15 | 16 |
| 16 PCMQueueInAudioInputStream::PCMQueueInAudioInputStream( | 17 PCMQueueInAudioInputStream::PCMQueueInAudioInputStream( |
| 17 AudioManagerMac* manager, | 18 AudioManagerMac* manager, |
| 18 const AudioParameters& params) | 19 const AudioParameters& params) |
| 19 : manager_(manager), | 20 : manager_(manager), |
| 20 callback_(NULL), | 21 callback_(NULL), |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 } | 196 } |
| 196 | 197 |
| 197 void PCMQueueInAudioInputStream::HandleInputBuffer( | 198 void PCMQueueInAudioInputStream::HandleInputBuffer( |
| 198 AudioQueueRef audio_queue, | 199 AudioQueueRef audio_queue, |
| 199 AudioQueueBufferRef audio_buffer, | 200 AudioQueueBufferRef audio_buffer, |
| 200 const AudioTimeStamp* start_time, | 201 const AudioTimeStamp* start_time, |
| 201 UInt32 num_packets, | 202 UInt32 num_packets, |
| 202 const AudioStreamPacketDescription* packet_desc) { | 203 const AudioStreamPacketDescription* packet_desc) { |
| 203 DCHECK_EQ(audio_queue_, audio_queue); | 204 DCHECK_EQ(audio_queue_, audio_queue); |
| 204 DCHECK(audio_buffer->mAudioData); | 205 DCHECK(audio_buffer->mAudioData); |
| 206 TRACE_EVENT0("audio", "PCMQueueInAudioInputStream::HandleInputBuffer"); |
| 205 if (!callback_) { | 207 if (!callback_) { |
| 206 // This can happen if Stop() was called without start. | 208 // This can happen if Stop() was called without start. |
| 207 DCHECK_EQ(0U, audio_buffer->mAudioDataByteSize); | 209 DCHECK_EQ(0U, audio_buffer->mAudioDataByteSize); |
| 208 return; | 210 return; |
| 209 } | 211 } |
| 210 | 212 |
| 211 if (audio_buffer->mAudioDataByteSize) { | 213 if (audio_buffer->mAudioDataByteSize) { |
| 212 // The AudioQueue API may use a large internal buffer and repeatedly call us | 214 // The AudioQueue API may use a large internal buffer and repeatedly call us |
| 213 // back to back once that internal buffer is filled. When this happens the | 215 // back to back once that internal buffer is filled. When this happens the |
| 214 // renderer client does not have enough time to read data back from the | 216 // renderer client does not have enough time to read data back from the |
| 215 // shared memory before the next write comes along. If HandleInputBuffer() | 217 // shared memory before the next write comes along. If HandleInputBuffer() |
| 216 // is called too frequently, Sleep() at least 5ms to ensure the shared | 218 // is called too frequently, Sleep() at least 5ms to ensure the shared |
| 217 // memory doesn't get trampled. | 219 // memory doesn't get trampled. |
| 218 // TODO(dalecurtis): This is a HACK. Long term the AudioQueue path is going | 220 // TODO(dalecurtis): This is a HACK. Long term the AudioQueue path is going |
| 219 // away in favor of the AudioUnit based AUAudioInputStream(). Tracked by | 221 // away in favor of the AudioUnit based AUAudioInputStream(). Tracked by |
| 220 // http://crbug.com/161383. | 222 // http://crbug.com/161383. |
| 221 base::TimeDelta elapsed = base::TimeTicks::Now() - last_fill_; | 223 base::TimeDelta elapsed = base::TimeTicks::Now() - last_fill_; |
| 222 const base::TimeDelta kMinDelay = base::TimeDelta::FromMilliseconds(5); | 224 const base::TimeDelta kMinDelay = base::TimeDelta::FromMilliseconds(5); |
| 223 if (elapsed < kMinDelay) | 225 if (elapsed < kMinDelay) { |
| 226 TRACE_EVENT0("audio", |
| 227 "PCMQueueInAudioInputStream::HandleInputBuffer sleep"); |
| 224 base::PlatformThread::Sleep(kMinDelay - elapsed); | 228 base::PlatformThread::Sleep(kMinDelay - elapsed); |
| 229 } |
| 225 | 230 |
| 226 uint8_t* audio_data = reinterpret_cast<uint8_t*>(audio_buffer->mAudioData); | 231 uint8_t* audio_data = reinterpret_cast<uint8_t*>(audio_buffer->mAudioData); |
| 227 audio_bus_->FromInterleaved( | 232 audio_bus_->FromInterleaved( |
| 228 audio_data, audio_bus_->frames(), format_.mBitsPerChannel / 8); | 233 audio_data, audio_bus_->frames(), format_.mBitsPerChannel / 8); |
| 229 callback_->OnData( | 234 callback_->OnData( |
| 230 this, audio_bus_.get(), audio_buffer->mAudioDataByteSize, 0.0); | 235 this, audio_bus_.get(), audio_buffer->mAudioDataByteSize, 0.0); |
| 231 | 236 |
| 232 last_fill_ = base::TimeTicks::Now(); | 237 last_fill_ = base::TimeTicks::Now(); |
| 233 } | 238 } |
| 234 // Recycle the buffer. | 239 // Recycle the buffer. |
| 235 OSStatus err = QueueNextBuffer(audio_buffer); | 240 OSStatus err = QueueNextBuffer(audio_buffer); |
| 236 if (err != noErr) { | 241 if (err != noErr) { |
| 237 if (err == kAudioQueueErr_EnqueueDuringReset) { | 242 if (err == kAudioQueueErr_EnqueueDuringReset) { |
| 238 // This is the error you get if you try to enqueue a buffer and the | 243 // This is the error you get if you try to enqueue a buffer and the |
| 239 // queue has been closed. Not really a problem if indeed the queue | 244 // queue has been closed. Not really a problem if indeed the queue |
| 240 // has been closed. | 245 // has been closed. |
| 241 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an | 246 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an |
| 242 // extra guard for this situation, but it seems to introduce more | 247 // extra guard for this situation, but it seems to introduce more |
| 243 // complications than it solves (memory barrier issues accessing it from | 248 // complications than it solves (memory barrier issues accessing it from |
| 244 // multiple threads, looses the means to indicate OnClosed to client). | 249 // multiple threads, looses the means to indicate OnClosed to client). |
| 245 // Should determine if we need to do something equivalent here. | 250 // Should determine if we need to do something equivalent here. |
| 246 return; | 251 return; |
| 247 } | 252 } |
| 248 HandleError(err); | 253 HandleError(err); |
| 249 } | 254 } |
| 250 } | 255 } |
| 251 | 256 |
| 252 } // namespace media | 257 } // namespace media |
| OLD | NEW |