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