Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / media / audio / mac / audio_input_mac.cc
blob514b0215f15d0f979c3f8f1739e3d0aa65565078
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 "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/mac/mac_logging.h"
10 #include "media/audio/audio_manager_base.h"
11 #include "media/audio/audio_util.h"
13 #if !defined(OS_IOS)
14 #include <CoreServices/CoreServices.h>
15 #endif
17 namespace media {
19 PCMQueueInAudioInputStream::PCMQueueInAudioInputStream(
20 AudioManagerBase* manager, const AudioParameters& params)
21 : manager_(manager),
22 callback_(NULL),
23 audio_queue_(NULL),
24 buffer_size_bytes_(0),
25 started_(false) {
26 // We must have a manager.
27 DCHECK(manager_);
28 // 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
30 // packet is always one frame.
31 format_.mSampleRate = params.sample_rate();
32 format_.mFormatID = kAudioFormatLinearPCM;
33 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked |
34 kLinearPCMFormatFlagIsSignedInteger;
35 format_.mBitsPerChannel = params.bits_per_sample();
36 format_.mChannelsPerFrame = params.channels();
37 format_.mFramesPerPacket = 1;
38 format_.mBytesPerPacket = (params.bits_per_sample() * params.channels()) / 8;
39 format_.mBytesPerFrame = format_.mBytesPerPacket;
40 format_.mReserved = 0;
42 buffer_size_bytes_ = params.GetBytesPerBuffer();
45 PCMQueueInAudioInputStream::~PCMQueueInAudioInputStream() {
46 DCHECK(!callback_);
47 DCHECK(!audio_queue_);
50 bool PCMQueueInAudioInputStream::Open() {
51 OSStatus err = AudioQueueNewInput(&format_,
52 &HandleInputBufferStatic,
53 this,
54 NULL, // Use OS CFRunLoop for |callback|
55 kCFRunLoopCommonModes,
56 0, // Reserved
57 &audio_queue_);
58 if (err != noErr) {
59 HandleError(err);
60 return false;
62 return SetupBuffers();
65 void PCMQueueInAudioInputStream::Start(AudioInputCallback* callback) {
66 DCHECK(callback);
67 DLOG_IF(ERROR, !audio_queue_) << "Open() has not been called successfully";
68 if (callback_ || !audio_queue_)
69 return;
70 callback_ = callback;
71 OSStatus err = AudioQueueStart(audio_queue_, NULL);
72 if (err != noErr) {
73 HandleError(err);
74 } else {
75 started_ = true;
76 manager_->IncreaseActiveInputStreamCount();
80 void PCMQueueInAudioInputStream::Stop() {
81 if (!audio_queue_ || !started_)
82 return;
84 // Stop is always called before Close. In case of error, this will be
85 // also called when closing the input controller.
86 manager_->DecreaseActiveInputStreamCount();
88 // We request a synchronous stop, so the next call can take some time. In
89 // the windows implementation we block here as well.
90 OSStatus err = AudioQueueStop(audio_queue_, true);
91 if (err != noErr)
92 HandleError(err);
94 started_ = false;
97 void PCMQueueInAudioInputStream::Close() {
98 // It is valid to call Close() before calling Open() or Start(), thus
99 // |audio_queue_| and |callback_| might be NULL.
100 if (audio_queue_) {
101 OSStatus err = AudioQueueDispose(audio_queue_, true);
102 audio_queue_ = NULL;
103 if (err != noErr)
104 HandleError(err);
106 if (callback_) {
107 callback_->OnClose(this);
108 callback_ = NULL;
110 manager_->ReleaseInputStream(this);
111 // CARE: This object may now be destroyed.
114 double PCMQueueInAudioInputStream::GetMaxVolume() {
115 NOTREACHED() << "Only supported for low-latency mode.";
116 return 0.0;
119 void PCMQueueInAudioInputStream::SetVolume(double volume) {
120 NOTREACHED() << "Only supported for low-latency mode.";
123 double PCMQueueInAudioInputStream::GetVolume() {
124 NOTREACHED() << "Only supported for low-latency mode.";
125 return 0.0;
128 void PCMQueueInAudioInputStream::SetAutomaticGainControl(bool enabled) {
129 NOTREACHED() << "Only supported for low-latency mode.";
132 bool PCMQueueInAudioInputStream::GetAutomaticGainControl() {
133 NOTREACHED() << "Only supported for low-latency mode.";
134 return false;
137 void PCMQueueInAudioInputStream::HandleError(OSStatus err) {
138 if (callback_)
139 callback_->OnError(this);
140 // This point should never be reached.
141 OSSTATUS_DCHECK(0, err);
144 bool PCMQueueInAudioInputStream::SetupBuffers() {
145 DCHECK(buffer_size_bytes_);
146 for (int i = 0; i < kNumberBuffers; ++i) {
147 AudioQueueBufferRef buffer;
148 OSStatus err = AudioQueueAllocateBuffer(audio_queue_,
149 buffer_size_bytes_,
150 &buffer);
151 if (err == noErr)
152 err = QueueNextBuffer(buffer);
153 if (err != noErr) {
154 HandleError(err);
155 return false;
157 // |buffer| will automatically be freed when |audio_queue_| is released.
159 return true;
162 OSStatus PCMQueueInAudioInputStream::QueueNextBuffer(
163 AudioQueueBufferRef audio_buffer) {
164 // Only the first 2 params are needed for recording.
165 return AudioQueueEnqueueBuffer(audio_queue_, audio_buffer, 0, NULL);
168 // static
169 void PCMQueueInAudioInputStream::HandleInputBufferStatic(
170 void* data,
171 AudioQueueRef audio_queue,
172 AudioQueueBufferRef audio_buffer,
173 const AudioTimeStamp* start_time,
174 UInt32 num_packets,
175 const AudioStreamPacketDescription* desc) {
176 reinterpret_cast<PCMQueueInAudioInputStream*>(data)->
177 HandleInputBuffer(audio_queue, audio_buffer, start_time,
178 num_packets, desc);
181 void PCMQueueInAudioInputStream::HandleInputBuffer(
182 AudioQueueRef audio_queue,
183 AudioQueueBufferRef audio_buffer,
184 const AudioTimeStamp* start_time,
185 UInt32 num_packets,
186 const AudioStreamPacketDescription* packet_desc) {
187 DCHECK_EQ(audio_queue_, audio_queue);
188 DCHECK(audio_buffer->mAudioData);
189 if (!callback_) {
190 // This can happen if Stop() was called without start.
191 DCHECK_EQ(0U, audio_buffer->mAudioDataByteSize);
192 return;
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() at least 5ms to ensure the shared
201 // memory doesn't get trampled.
202 // TODO(dalecurtis): This is a HACK. Long term the AudioQueue path is going
203 // away in favor of the AudioUnit based AUAudioInputStream(). Tracked by
204 // http://crbug.com/161383.
205 base::TimeDelta elapsed = base::Time::Now() - last_fill_;
206 const base::TimeDelta kMinDelay = base::TimeDelta::FromMilliseconds(5);
207 if (elapsed < kMinDelay)
208 base::PlatformThread::Sleep(kMinDelay - elapsed);
210 callback_->OnData(this,
211 reinterpret_cast<const uint8*>(audio_buffer->mAudioData),
212 audio_buffer->mAudioDataByteSize,
213 audio_buffer->mAudioDataByteSize,
214 0.0);
216 last_fill_ = base::Time::Now();
218 // Recycle the buffer.
219 OSStatus err = QueueNextBuffer(audio_buffer);
220 if (err != noErr) {
221 if (err == kAudioQueueErr_EnqueueDuringReset) {
222 // This is the error you get if you try to enqueue a buffer and the
223 // queue has been closed. Not really a problem if indeed the queue
224 // has been closed.
225 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an
226 // extra guard for this situation, but it seems to introduce more
227 // complications than it solves (memory barrier issues accessing it from
228 // multiple threads, looses the means to indicate OnClosed to client).
229 // Should determine if we need to do something equivalent here.
230 return;
232 HandleError(err);
236 } // namespace media