This source file includes following definitions.
- started_
- Open
- Start
- Stop
- Close
- GetMaxVolume
- SetVolume
- GetVolume
- SetAutomaticGainControl
- GetAutomaticGainControl
- HandleError
- SetupBuffers
- QueueNextBuffer
- HandleInputBufferStatic
- HandleInputBuffer
#include "media/audio/mac/audio_input_mac.h"
#include <CoreServices/CoreServices.h>
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/mac/mac_logging.h"
#include "media/audio/audio_manager_base.h"
namespace media {
PCMQueueInAudioInputStream::PCMQueueInAudioInputStream(
AudioManagerBase* manager, const AudioParameters& params)
: manager_(manager),
callback_(NULL),
audio_queue_(NULL),
buffer_size_bytes_(0),
started_(false) {
DCHECK(manager_);
format_.mSampleRate = params.sample_rate();
format_.mFormatID = kAudioFormatLinearPCM;
format_.mFormatFlags = kLinearPCMFormatFlagIsPacked |
kLinearPCMFormatFlagIsSignedInteger;
format_.mBitsPerChannel = params.bits_per_sample();
format_.mChannelsPerFrame = params.channels();
format_.mFramesPerPacket = 1;
format_.mBytesPerPacket = (params.bits_per_sample() * params.channels()) / 8;
format_.mBytesPerFrame = format_.mBytesPerPacket;
format_.mReserved = 0;
buffer_size_bytes_ = params.GetBytesPerBuffer();
}
PCMQueueInAudioInputStream::~PCMQueueInAudioInputStream() {
DCHECK(!callback_);
DCHECK(!audio_queue_);
}
bool PCMQueueInAudioInputStream::Open() {
OSStatus err = AudioQueueNewInput(&format_,
&HandleInputBufferStatic,
this,
NULL,
kCFRunLoopCommonModes,
0,
&audio_queue_);
if (err != noErr) {
HandleError(err);
return false;
}
return SetupBuffers();
}
void PCMQueueInAudioInputStream::Start(AudioInputCallback* callback) {
DCHECK(callback);
DLOG_IF(ERROR, !audio_queue_) << "Open() has not been called successfully";
if (callback_ || !audio_queue_)
return;
callback_ = callback;
OSStatus err = AudioQueueStart(audio_queue_, NULL);
if (err != noErr) {
HandleError(err);
} else {
started_ = true;
}
}
void PCMQueueInAudioInputStream::Stop() {
if (!audio_queue_ || !started_)
return;
OSStatus err = AudioQueueStop(audio_queue_, true);
if (err != noErr)
HandleError(err);
started_ = false;
callback_ = NULL;
}
void PCMQueueInAudioInputStream::Close() {
Stop();
if (audio_queue_) {
OSStatus err = AudioQueueDispose(audio_queue_, true);
audio_queue_ = NULL;
if (err != noErr)
HandleError(err);
}
manager_->ReleaseInputStream(this);
}
double PCMQueueInAudioInputStream::GetMaxVolume() {
NOTREACHED() << "Only supported for low-latency mode.";
return 0.0;
}
void PCMQueueInAudioInputStream::SetVolume(double volume) {
NOTREACHED() << "Only supported for low-latency mode.";
}
double PCMQueueInAudioInputStream::GetVolume() {
NOTREACHED() << "Only supported for low-latency mode.";
return 0.0;
}
void PCMQueueInAudioInputStream::SetAutomaticGainControl(bool enabled) {
NOTREACHED() << "Only supported for low-latency mode.";
}
bool PCMQueueInAudioInputStream::GetAutomaticGainControl() {
NOTREACHED() << "Only supported for low-latency mode.";
return false;
}
void PCMQueueInAudioInputStream::HandleError(OSStatus err) {
if (callback_)
callback_->OnError(this);
OSSTATUS_DCHECK(0, err);
}
bool PCMQueueInAudioInputStream::SetupBuffers() {
DCHECK(buffer_size_bytes_);
for (int i = 0; i < kNumberBuffers; ++i) {
AudioQueueBufferRef buffer;
OSStatus err = AudioQueueAllocateBuffer(audio_queue_,
buffer_size_bytes_,
&buffer);
if (err == noErr)
err = QueueNextBuffer(buffer);
if (err != noErr) {
HandleError(err);
return false;
}
}
return true;
}
OSStatus PCMQueueInAudioInputStream::QueueNextBuffer(
AudioQueueBufferRef audio_buffer) {
return AudioQueueEnqueueBuffer(audio_queue_, audio_buffer, 0, NULL);
}
void PCMQueueInAudioInputStream::HandleInputBufferStatic(
void* data,
AudioQueueRef audio_queue,
AudioQueueBufferRef audio_buffer,
const AudioTimeStamp* start_time,
UInt32 num_packets,
const AudioStreamPacketDescription* desc) {
reinterpret_cast<PCMQueueInAudioInputStream*>(data)->
HandleInputBuffer(audio_queue, audio_buffer, start_time,
num_packets, desc);
}
void PCMQueueInAudioInputStream::HandleInputBuffer(
AudioQueueRef audio_queue,
AudioQueueBufferRef audio_buffer,
const AudioTimeStamp* start_time,
UInt32 num_packets,
const AudioStreamPacketDescription* packet_desc) {
DCHECK_EQ(audio_queue_, audio_queue);
DCHECK(audio_buffer->mAudioData);
if (!callback_) {
DCHECK_EQ(0U, audio_buffer->mAudioDataByteSize);
return;
}
if (audio_buffer->mAudioDataByteSize) {
base::TimeDelta elapsed = base::TimeTicks::Now() - last_fill_;
const base::TimeDelta kMinDelay = base::TimeDelta::FromMilliseconds(5);
if (elapsed < kMinDelay)
base::PlatformThread::Sleep(kMinDelay - elapsed);
callback_->OnData(this,
reinterpret_cast<const uint8*>(audio_buffer->mAudioData),
audio_buffer->mAudioDataByteSize,
audio_buffer->mAudioDataByteSize,
0.0);
last_fill_ = base::TimeTicks::Now();
}
OSStatus err = QueueNextBuffer(audio_buffer);
if (err != noErr) {
if (err == kAudioQueueErr_EnqueueDuringReset) {
return;
}
HandleError(err);
}
}
}