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
3 // found in the LICENSE file.
5 #include "media/audio/mac/audio_input_mac.h"
7 #include <CoreServices/CoreServices.h>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/mac/mac_logging.h"
12 #include "media/audio/audio_manager_base.h"
17 PCMQueueInAudioInputStream::PCMQueueInAudioInputStream(
18 AudioManagerBase
* manager
, const AudioParameters
& params
)
22 buffer_size_bytes_(0),
24 // We must have a manager.
26 // A frame is one sample across all channels. In interleaved audio the per
27 // frame fields identify the set of n |channels|. In uncompressed audio, a
28 // packet is always one frame.
29 format_
.mSampleRate
= params
.sample_rate();
30 format_
.mFormatID
= kAudioFormatLinearPCM
;
31 format_
.mFormatFlags
= kLinearPCMFormatFlagIsPacked
|
32 kLinearPCMFormatFlagIsSignedInteger
;
33 format_
.mBitsPerChannel
= params
.bits_per_sample();
34 format_
.mChannelsPerFrame
= params
.channels();
35 format_
.mFramesPerPacket
= 1;
36 format_
.mBytesPerPacket
= (params
.bits_per_sample() * params
.channels()) / 8;
37 format_
.mBytesPerFrame
= format_
.mBytesPerPacket
;
38 format_
.mReserved
= 0;
40 buffer_size_bytes_
= params
.GetBytesPerBuffer();
43 PCMQueueInAudioInputStream::~PCMQueueInAudioInputStream() {
45 DCHECK(!audio_queue_
);
48 bool PCMQueueInAudioInputStream::Open() {
49 OSStatus err
= AudioQueueNewInput(&format_
,
50 &HandleInputBufferStatic
,
52 NULL
, // Use OS CFRunLoop for |callback|
53 kCFRunLoopCommonModes
,
60 return SetupBuffers();
63 void PCMQueueInAudioInputStream::Start(AudioInputCallback
* callback
) {
65 DLOG_IF(ERROR
, !audio_queue_
) << "Open() has not been called successfully";
66 if (callback_
|| !audio_queue_
)
69 OSStatus err
= AudioQueueStart(audio_queue_
, NULL
);
77 void PCMQueueInAudioInputStream::Stop() {
78 if (!audio_queue_
|| !started_
)
81 // We request a synchronous stop, so the next call can take some time. In
82 // the windows implementation we block here as well.
83 OSStatus err
= AudioQueueStop(audio_queue_
, true);
90 void PCMQueueInAudioInputStream::Close() {
91 // It is valid to call Close() before calling Open() or Start(), thus
92 // |audio_queue_| and |callback_| might be NULL.
94 OSStatus err
= AudioQueueDispose(audio_queue_
, true);
100 callback_
->OnClose(this);
103 manager_
->ReleaseInputStream(this);
104 // CARE: This object may now be destroyed.
107 double PCMQueueInAudioInputStream::GetMaxVolume() {
108 NOTREACHED() << "Only supported for low-latency mode.";
112 void PCMQueueInAudioInputStream::SetVolume(double volume
) {
113 NOTREACHED() << "Only supported for low-latency mode.";
116 double PCMQueueInAudioInputStream::GetVolume() {
117 NOTREACHED() << "Only supported for low-latency mode.";
121 void PCMQueueInAudioInputStream::SetAutomaticGainControl(bool enabled
) {
122 NOTREACHED() << "Only supported for low-latency mode.";
125 bool PCMQueueInAudioInputStream::GetAutomaticGainControl() {
126 NOTREACHED() << "Only supported for low-latency mode.";
130 void PCMQueueInAudioInputStream::HandleError(OSStatus err
) {
132 callback_
->OnError(this);
133 // This point should never be reached.
134 OSSTATUS_DCHECK(0, err
);
137 bool PCMQueueInAudioInputStream::SetupBuffers() {
138 DCHECK(buffer_size_bytes_
);
139 for (int i
= 0; i
< kNumberBuffers
; ++i
) {
140 AudioQueueBufferRef buffer
;
141 OSStatus err
= AudioQueueAllocateBuffer(audio_queue_
,
145 err
= QueueNextBuffer(buffer
);
150 // |buffer| will automatically be freed when |audio_queue_| is released.
155 OSStatus
PCMQueueInAudioInputStream::QueueNextBuffer(
156 AudioQueueBufferRef audio_buffer
) {
157 // Only the first 2 params are needed for recording.
158 return AudioQueueEnqueueBuffer(audio_queue_
, audio_buffer
, 0, NULL
);
162 void PCMQueueInAudioInputStream::HandleInputBufferStatic(
164 AudioQueueRef audio_queue
,
165 AudioQueueBufferRef audio_buffer
,
166 const AudioTimeStamp
* start_time
,
168 const AudioStreamPacketDescription
* desc
) {
169 reinterpret_cast<PCMQueueInAudioInputStream
*>(data
)->
170 HandleInputBuffer(audio_queue
, audio_buffer
, start_time
,
174 void PCMQueueInAudioInputStream::HandleInputBuffer(
175 AudioQueueRef audio_queue
,
176 AudioQueueBufferRef audio_buffer
,
177 const AudioTimeStamp
* start_time
,
179 const AudioStreamPacketDescription
* packet_desc
) {
180 DCHECK_EQ(audio_queue_
, audio_queue
);
181 DCHECK(audio_buffer
->mAudioData
);
183 // This can happen if Stop() was called without start.
184 DCHECK_EQ(0U, audio_buffer
->mAudioDataByteSize
);
188 if (audio_buffer
->mAudioDataByteSize
) {
189 // The AudioQueue API may use a large internal buffer and repeatedly call us
190 // back to back once that internal buffer is filled. When this happens the
191 // renderer client does not have enough time to read data back from the
192 // shared memory before the next write comes along. If HandleInputBuffer()
193 // is called too frequently, Sleep() at least 5ms to ensure the shared
194 // memory doesn't get trampled.
195 // TODO(dalecurtis): This is a HACK. Long term the AudioQueue path is going
196 // away in favor of the AudioUnit based AUAudioInputStream(). Tracked by
197 // http://crbug.com/161383.
198 base::TimeDelta elapsed
= base::TimeTicks::Now() - last_fill_
;
199 const base::TimeDelta kMinDelay
= base::TimeDelta::FromMilliseconds(5);
200 if (elapsed
< kMinDelay
)
201 base::PlatformThread::Sleep(kMinDelay
- elapsed
);
203 callback_
->OnData(this,
204 reinterpret_cast<const uint8
*>(audio_buffer
->mAudioData
),
205 audio_buffer
->mAudioDataByteSize
,
206 audio_buffer
->mAudioDataByteSize
,
209 last_fill_
= base::TimeTicks::Now();
211 // Recycle the buffer.
212 OSStatus err
= QueueNextBuffer(audio_buffer
);
214 if (err
== kAudioQueueErr_EnqueueDuringReset
) {
215 // This is the error you get if you try to enqueue a buffer and the
216 // queue has been closed. Not really a problem if indeed the queue
218 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an
219 // extra guard for this situation, but it seems to introduce more
220 // complications than it solves (memory barrier issues accessing it from
221 // multiple threads, looses the means to indicate OnClosed to client).
222 // Should determine if we need to do something equivalent here.