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/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/mac/mac_logging.h" | 11 #include "base/mac/mac_logging.h" |
12 #include "media/audio/mac/audio_manager_mac.h" | 12 #include "media/audio/mac/audio_manager_mac.h" |
13 #include "media/base/audio_bus.h" | |
14 | 13 |
15 namespace media { | 14 namespace media { |
16 | 15 |
17 PCMQueueInAudioInputStream::PCMQueueInAudioInputStream( | 16 PCMQueueInAudioInputStream::PCMQueueInAudioInputStream( |
18 AudioManagerMac* manager, | 17 AudioManagerMac* manager, const AudioParameters& params) |
19 const AudioParameters& params) | |
20 : manager_(manager), | 18 : manager_(manager), |
21 callback_(NULL), | 19 callback_(NULL), |
22 audio_queue_(NULL), | 20 audio_queue_(NULL), |
23 buffer_size_bytes_(0), | 21 buffer_size_bytes_(0), |
24 started_(false), | 22 started_(false) { |
25 audio_bus_(media::AudioBus::Create(params)) { | |
26 // We must have a manager. | 23 // We must have a manager. |
27 DCHECK(manager_); | 24 DCHECK(manager_); |
28 // A frame is one sample across all channels. In interleaved audio the per | 25 // A frame is one sample across all channels. In interleaved audio the per |
29 // frame fields identify the set of n |channels|. In uncompressed audio, a | 26 // frame fields identify the set of n |channels|. In uncompressed audio, a |
30 // packet is always one frame. | 27 // packet is always one frame. |
31 format_.mSampleRate = params.sample_rate(); | 28 format_.mSampleRate = params.sample_rate(); |
32 format_.mFormatID = kAudioFormatLinearPCM; | 29 format_.mFormatID = kAudioFormatLinearPCM; |
33 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | | 30 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | |
34 kLinearPCMFormatFlagIsSignedInteger; | 31 kLinearPCMFormatFlagIsSignedInteger; |
35 format_.mBitsPerChannel = params.bits_per_sample(); | 32 format_.mBitsPerChannel = params.bits_per_sample(); |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 // is called too frequently, Sleep() at least 5ms to ensure the shared | 208 // is called too frequently, Sleep() at least 5ms to ensure the shared |
212 // memory doesn't get trampled. | 209 // memory doesn't get trampled. |
213 // TODO(dalecurtis): This is a HACK. Long term the AudioQueue path is going | 210 // TODO(dalecurtis): This is a HACK. Long term the AudioQueue path is going |
214 // away in favor of the AudioUnit based AUAudioInputStream(). Tracked by | 211 // away in favor of the AudioUnit based AUAudioInputStream(). Tracked by |
215 // http://crbug.com/161383. | 212 // http://crbug.com/161383. |
216 base::TimeDelta elapsed = base::TimeTicks::Now() - last_fill_; | 213 base::TimeDelta elapsed = base::TimeTicks::Now() - last_fill_; |
217 const base::TimeDelta kMinDelay = base::TimeDelta::FromMilliseconds(5); | 214 const base::TimeDelta kMinDelay = base::TimeDelta::FromMilliseconds(5); |
218 if (elapsed < kMinDelay) | 215 if (elapsed < kMinDelay) |
219 base::PlatformThread::Sleep(kMinDelay - elapsed); | 216 base::PlatformThread::Sleep(kMinDelay - elapsed); |
220 | 217 |
221 uint8* audio_data = reinterpret_cast<uint8*>(audio_buffer->mAudioData); | 218 callback_->OnData(this, |
222 audio_bus_->FromInterleaved( | 219 reinterpret_cast<const uint8*>(audio_buffer->mAudioData), |
223 audio_data, audio_bus_->frames(), format_.mBitsPerChannel / 8); | 220 audio_buffer->mAudioDataByteSize, |
224 callback_->OnData( | 221 audio_buffer->mAudioDataByteSize, |
225 this, audio_bus_.get(), audio_buffer->mAudioDataByteSize, 0.0); | 222 0.0); |
226 | 223 |
227 last_fill_ = base::TimeTicks::Now(); | 224 last_fill_ = base::TimeTicks::Now(); |
228 } | 225 } |
229 // Recycle the buffer. | 226 // Recycle the buffer. |
230 OSStatus err = QueueNextBuffer(audio_buffer); | 227 OSStatus err = QueueNextBuffer(audio_buffer); |
231 if (err != noErr) { | 228 if (err != noErr) { |
232 if (err == kAudioQueueErr_EnqueueDuringReset) { | 229 if (err == kAudioQueueErr_EnqueueDuringReset) { |
233 // This is the error you get if you try to enqueue a buffer and the | 230 // This is the error you get if you try to enqueue a buffer and the |
234 // queue has been closed. Not really a problem if indeed the queue | 231 // queue has been closed. Not really a problem if indeed the queue |
235 // has been closed. | 232 // has been closed. |
236 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an | 233 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an |
237 // extra guard for this situation, but it seems to introduce more | 234 // extra guard for this situation, but it seems to introduce more |
238 // complications than it solves (memory barrier issues accessing it from | 235 // complications than it solves (memory barrier issues accessing it from |
239 // multiple threads, looses the means to indicate OnClosed to client). | 236 // multiple threads, looses the means to indicate OnClosed to client). |
240 // Should determine if we need to do something equivalent here. | 237 // Should determine if we need to do something equivalent here. |
241 return; | 238 return; |
242 } | 239 } |
243 HandleError(err); | 240 HandleError(err); |
244 } | 241 } |
245 } | 242 } |
246 | 243 |
247 } // namespace media | 244 } // namespace media |
OLD | NEW |