2003-05-21 21:15:46 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* ao_macosx.c
|
|
|
|
*
|
|
|
|
* Original Copyright (C) Timothy J. Wood - Aug 2000
|
|
|
|
*
|
|
|
|
* This file is part of libao, a cross-platform library. See
|
|
|
|
* README for a history of this source code.
|
|
|
|
*
|
|
|
|
* libao is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* libao is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2007-01-22 08:02:06 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with libao; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2003-05-21 21:15:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The MacOS X CoreAudio framework doesn't mesh as simply as some
|
|
|
|
* simpler frameworks do. This is due to the fact that CoreAudio pulls
|
|
|
|
* audio samples rather than having them pushed at it (which is nice
|
|
|
|
* when you are wanting to do good buffering of audio).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Change log:
|
|
|
|
*
|
|
|
|
* 14/5-2003: Ported to MPlayer libao2 by Dan Christiansen
|
|
|
|
*
|
|
|
|
* AC-3 and MPEG audio passthrough is possible, but I don't have
|
|
|
|
* access to a sound card that supports it.
|
|
|
|
*/
|
|
|
|
|
2005-06-05 14:47:26 +00:00
|
|
|
#include <CoreServices/CoreServices.h>
|
|
|
|
#include <AudioUnit/AudioUnit.h>
|
|
|
|
#include <AudioToolbox/AudioToolbox.h>
|
2003-05-21 21:15:46 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2005-03-03 13:46:54 +00:00
|
|
|
#include <stdlib.h>
|
2003-05-21 21:15:46 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2005-01-12 22:00:02 +00:00
|
|
|
#include "config.h"
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "mp_msg.h"
|
2003-05-21 21:15:46 +00:00
|
|
|
|
|
|
|
#include "audio_out.h"
|
|
|
|
#include "audio_out_internal.h"
|
2004-12-27 17:30:15 +00:00
|
|
|
#include "libaf/af_format.h"
|
2003-05-21 21:15:46 +00:00
|
|
|
|
|
|
|
static ao_info_t info =
|
|
|
|
{
|
|
|
|
"Darwin/Mac OS X native audio output",
|
|
|
|
"macosx",
|
2005-06-05 14:47:26 +00:00
|
|
|
"Timothy J. Wood & Dan Christiansen & Chris Roccati",
|
2003-05-21 21:15:46 +00:00
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
LIBAO_EXTERN(macosx)
|
|
|
|
|
2003-05-22 16:12:54 +00:00
|
|
|
/* Prefix for all mp_msg() calls */
|
|
|
|
#define ao_msg(a, b, c...) mp_msg(a, b, "AO: [macosx] " c)
|
|
|
|
|
2003-05-21 21:15:46 +00:00
|
|
|
/* This is large, but best (maybe it should be even larger).
|
|
|
|
* CoreAudio supposedly has an internal latency in the order of 2ms */
|
2005-01-01 03:51:08 +00:00
|
|
|
#define NUM_BUFS 32
|
2003-05-21 21:15:46 +00:00
|
|
|
|
|
|
|
typedef struct ao_macosx_s
|
|
|
|
{
|
2005-06-05 14:47:26 +00:00
|
|
|
/* AudioUnit */
|
|
|
|
AudioUnit theOutputUnit;
|
|
|
|
int packetSize;
|
2006-08-18 01:19:19 +00:00
|
|
|
int paused;
|
2003-05-21 21:15:46 +00:00
|
|
|
|
|
|
|
/* Ring-buffer */
|
2005-02-22 20:54:31 +00:00
|
|
|
/* does not need explicit synchronization, but needs to allocate
|
|
|
|
* (num_chunks + 1) * chunk_size memory to store num_chunks * chunk_size
|
|
|
|
* data */
|
|
|
|
unsigned char *buffer;
|
|
|
|
unsigned int buffer_len; ///< must always be (num_chunks + 1) * chunk_size
|
|
|
|
unsigned int num_chunks;
|
|
|
|
unsigned int chunk_size;
|
2003-05-21 21:15:46 +00:00
|
|
|
|
|
|
|
unsigned int buf_read_pos;
|
|
|
|
unsigned int buf_write_pos;
|
|
|
|
} ao_macosx_t;
|
|
|
|
|
2005-06-29 18:17:19 +00:00
|
|
|
static ao_macosx_t *ao = NULL;
|
2003-05-21 21:15:46 +00:00
|
|
|
|
2005-02-22 20:54:31 +00:00
|
|
|
/**
|
|
|
|
* \brief return number of free bytes in the buffer
|
|
|
|
* may only be called by mplayer's thread
|
|
|
|
* \return minimum number of free bytes in buffer, value may change between
|
|
|
|
* two immediately following calls, and the real number of free bytes
|
|
|
|
* might actually be larger!
|
|
|
|
*/
|
2006-07-06 04:30:19 +00:00
|
|
|
static int buf_free(void) {
|
2005-02-22 20:54:31 +00:00
|
|
|
int free = ao->buf_read_pos - ao->buf_write_pos - ao->chunk_size;
|
|
|
|
if (free < 0) free += ao->buffer_len;
|
|
|
|
return free;
|
|
|
|
}
|
2003-05-21 21:15:46 +00:00
|
|
|
|
2005-02-22 20:54:31 +00:00
|
|
|
/**
|
|
|
|
* \brief return number of buffered bytes
|
|
|
|
* may only be called by playback thread
|
|
|
|
* \return minimum number of buffered bytes, value may change between
|
|
|
|
* two immediately following calls, and the real number of buffered bytes
|
|
|
|
* might actually be larger!
|
|
|
|
*/
|
2006-07-06 04:30:19 +00:00
|
|
|
static int buf_used(void) {
|
2005-02-22 20:54:31 +00:00
|
|
|
int used = ao->buf_write_pos - ao->buf_read_pos;
|
|
|
|
if (used < 0) used += ao->buffer_len;
|
|
|
|
return used;
|
|
|
|
}
|
2003-05-21 21:15:46 +00:00
|
|
|
|
2005-02-22 20:54:31 +00:00
|
|
|
/**
|
|
|
|
* \brief add data to ringbuffer
|
|
|
|
*/
|
|
|
|
static int write_buffer(unsigned char* data, int len){
|
|
|
|
int first_len = ao->buffer_len - ao->buf_write_pos;
|
|
|
|
int free = buf_free();
|
|
|
|
if (len > free) len = free;
|
|
|
|
if (first_len > len) first_len = len;
|
|
|
|
// till end of buffer
|
|
|
|
memcpy (&ao->buffer[ao->buf_write_pos], data, first_len);
|
|
|
|
if (len > first_len) { // we have to wrap around
|
|
|
|
// remaining part from beginning of buffer
|
|
|
|
memcpy (ao->buffer, &data[first_len], len - first_len);
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
2005-02-22 20:54:31 +00:00
|
|
|
ao->buf_write_pos = (ao->buf_write_pos + len) % ao->buffer_len;
|
|
|
|
return len;
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
2005-02-22 20:54:31 +00:00
|
|
|
/**
|
|
|
|
* \brief remove data from ringbuffer
|
|
|
|
*/
|
2003-05-21 21:15:46 +00:00
|
|
|
static int read_buffer(unsigned char* data,int len){
|
2005-02-22 20:54:31 +00:00
|
|
|
int first_len = ao->buffer_len - ao->buf_read_pos;
|
|
|
|
int buffered = buf_used();
|
|
|
|
if (len > buffered) len = buffered;
|
|
|
|
if (first_len > len) first_len = len;
|
|
|
|
// till end of buffer
|
|
|
|
memcpy (data, &ao->buffer[ao->buf_read_pos], first_len);
|
|
|
|
if (len > first_len) { // we have to wrap around
|
|
|
|
// remaining part from beginning of buffer
|
|
|
|
memcpy (&data[first_len], ao->buffer, len - first_len);
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
2005-02-22 20:54:31 +00:00
|
|
|
ao->buf_read_pos = (ao->buf_read_pos + len) % ao->buffer_len;
|
|
|
|
return len;
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
2005-06-14 12:41:42 +00:00
|
|
|
OSStatus theRenderProc(void *inRefCon, AudioUnitRenderActionFlags *inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumFrames, AudioBufferList *ioData)
|
2003-05-21 21:15:46 +00:00
|
|
|
{
|
2005-06-05 14:47:26 +00:00
|
|
|
int amt=buf_used();
|
2005-06-14 12:41:42 +00:00
|
|
|
int req=(inNumFrames)*ao->packetSize;
|
2003-05-21 21:15:46 +00:00
|
|
|
|
2005-06-05 14:47:26 +00:00
|
|
|
if(amt>req)
|
|
|
|
amt=req;
|
|
|
|
|
|
|
|
if(amt)
|
|
|
|
read_buffer((unsigned char *)ioData->mBuffers[0].mData, amt);
|
2005-06-29 18:17:19 +00:00
|
|
|
else audio_pause();
|
2005-06-14 12:41:42 +00:00
|
|
|
ioData->mBuffers[0].mDataByteSize = amt;
|
2005-06-05 14:47:26 +00:00
|
|
|
|
|
|
|
return noErr;
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int control(int cmd,void *arg){
|
2006-11-28 23:05:45 +00:00
|
|
|
ao_control_vol_t *control_vol;
|
|
|
|
OSStatus err;
|
|
|
|
Float32 pan, vol;
|
2003-05-21 21:15:46 +00:00
|
|
|
switch (cmd) {
|
2005-01-01 03:51:08 +00:00
|
|
|
case AOCONTROL_GET_VOLUME:
|
2006-11-28 23:05:45 +00:00
|
|
|
control_vol = (ao_control_vol_t*)arg;
|
|
|
|
err = AudioUnitGetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &vol);
|
|
|
|
|
|
|
|
if(err==0) {
|
|
|
|
// printf("GET VOL=%f\n", vol);
|
|
|
|
control_vol->left=control_vol->right=vol*100.0/4.0;
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
|
2005-01-01 03:51:08 +00:00
|
|
|
case AOCONTROL_SET_VOLUME:
|
2006-11-28 23:05:45 +00:00
|
|
|
control_vol = (ao_control_vol_t*)arg;
|
|
|
|
|
|
|
|
vol=(control_vol->left+control_vol->right)*4.0/200.0;
|
|
|
|
err = AudioUnitSetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0);
|
|
|
|
if(err==0) {
|
|
|
|
// printf("SET VOL=%f\n", vol);
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
2005-06-05 14:47:26 +00:00
|
|
|
/* Everything is currently unimplemented */
|
2003-05-21 21:15:46 +00:00
|
|
|
default:
|
2003-05-22 16:12:54 +00:00
|
|
|
return CONTROL_FALSE;
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-03 03:34:18 +00:00
|
|
|
static void print_format(const char* str,AudioStreamBasicDescription *f){
|
2005-01-01 03:51:08 +00:00
|
|
|
uint32_t flags=(uint32_t) f->mFormatFlags;
|
2005-01-03 03:34:18 +00:00
|
|
|
ao_msg(MSGT_AO,MSGL_V, "%s %7.1fHz %dbit [%c%c%c%c] %s %s %s%s%s%s\n",
|
|
|
|
str, f->mSampleRate, f->mBitsPerChannel,
|
2005-01-01 03:51:08 +00:00
|
|
|
(int)(f->mFormatID & 0xff000000) >> 24,
|
|
|
|
(int)(f->mFormatID & 0x00ff0000) >> 16,
|
|
|
|
(int)(f->mFormatID & 0x0000ff00) >> 8,
|
|
|
|
(int)(f->mFormatID & 0x000000ff) >> 0,
|
|
|
|
(flags&kAudioFormatFlagIsFloat) ? "float" : "int",
|
|
|
|
(flags&kAudioFormatFlagIsBigEndian) ? "BE" : "LE",
|
|
|
|
(flags&kAudioFormatFlagIsSignedInteger) ? "S" : "U",
|
|
|
|
(flags&kAudioFormatFlagIsPacked) ? " packed" : "",
|
|
|
|
(flags&kAudioFormatFlagIsAlignedHigh) ? " aligned" : "",
|
|
|
|
(flags&kAudioFormatFlagIsNonInterleaved) ? " ni" : "" );
|
|
|
|
|
2005-01-03 03:34:18 +00:00
|
|
|
ao_msg(MSGT_AO,MSGL_DBG2, "%5d mBytesPerPacket\n",
|
2005-01-01 03:51:08 +00:00
|
|
|
(int)f->mBytesPerPacket);
|
2005-01-03 03:34:18 +00:00
|
|
|
ao_msg(MSGT_AO,MSGL_DBG2, "%5d mFramesPerPacket\n",
|
2005-01-01 03:51:08 +00:00
|
|
|
(int)f->mFramesPerPacket);
|
2005-01-03 03:34:18 +00:00
|
|
|
ao_msg(MSGT_AO,MSGL_DBG2, "%5d mBytesPerFrame\n",
|
2005-01-01 03:51:08 +00:00
|
|
|
(int)f->mBytesPerFrame);
|
2005-01-03 03:34:18 +00:00
|
|
|
ao_msg(MSGT_AO,MSGL_DBG2, "%5d mChannelsPerFrame\n",
|
2005-01-01 03:51:08 +00:00
|
|
|
(int)f->mChannelsPerFrame);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-21 21:15:46 +00:00
|
|
|
static int init(int rate,int channels,int format,int flags)
|
|
|
|
{
|
2005-06-05 14:47:26 +00:00
|
|
|
AudioStreamBasicDescription inDesc, outDesc;
|
|
|
|
ComponentDescription desc;
|
|
|
|
Component comp;
|
|
|
|
AURenderCallbackStruct renderCallback;
|
|
|
|
OSStatus err;
|
|
|
|
UInt32 size, maxFrames;
|
2005-06-29 18:17:19 +00:00
|
|
|
int aoIsCreated = ao != NULL;
|
2005-06-05 14:47:26 +00:00
|
|
|
|
2006-07-03 13:21:54 +00:00
|
|
|
if (!aoIsCreated) ao = malloc(sizeof(ao_macosx_t));
|
2005-06-05 14:47:26 +00:00
|
|
|
|
|
|
|
// Build Description for the input format
|
|
|
|
inDesc.mSampleRate=rate;
|
|
|
|
inDesc.mFormatID=kAudioFormatLinearPCM;
|
|
|
|
inDesc.mChannelsPerFrame=channels;
|
|
|
|
switch(format&AF_FORMAT_BITS_MASK){
|
|
|
|
case AF_FORMAT_8BIT:
|
|
|
|
inDesc.mBitsPerChannel=8;
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_16BIT:
|
|
|
|
inDesc.mBitsPerChannel=16;
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_24BIT:
|
|
|
|
inDesc.mBitsPerChannel=24;
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_32BIT:
|
|
|
|
inDesc.mBitsPerChannel=32;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ao_msg(MSGT_AO, MSGL_WARN, "Unsupported format (0x%08x)\n", format);
|
|
|
|
return CONTROL_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2003-05-21 21:15:46 +00:00
|
|
|
|
2005-06-05 14:47:26 +00:00
|
|
|
if((format&AF_FORMAT_POINT_MASK)==AF_FORMAT_F) {
|
|
|
|
// float
|
|
|
|
inDesc.mFormatFlags = kAudioFormatFlagIsFloat|kAudioFormatFlagIsPacked;
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
2005-06-05 14:47:26 +00:00
|
|
|
else if((format&AF_FORMAT_SIGN_MASK)==AF_FORMAT_SI) {
|
|
|
|
// signed int
|
|
|
|
inDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked;
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
2005-06-05 14:47:26 +00:00
|
|
|
else {
|
|
|
|
// unsigned int
|
|
|
|
inDesc.mFormatFlags = kAudioFormatFlagIsPacked;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((format&AF_FORMAT_END_MASK)==AF_FORMAT_BE)
|
|
|
|
inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian;
|
|
|
|
|
|
|
|
inDesc.mFramesPerPacket = 1;
|
|
|
|
ao->packetSize = inDesc.mBytesPerPacket = inDesc.mBytesPerFrame = inDesc.mFramesPerPacket*channels*(inDesc.mBitsPerChannel/8);
|
|
|
|
print_format("source: ",&inDesc);
|
2005-01-01 03:51:08 +00:00
|
|
|
|
2005-06-29 18:17:19 +00:00
|
|
|
if (!aoIsCreated) {
|
2005-06-05 14:47:26 +00:00
|
|
|
desc.componentType = kAudioUnitType_Output;
|
|
|
|
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
|
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
desc.componentFlags = 0;
|
|
|
|
desc.componentFlagsMask = 0;
|
|
|
|
|
|
|
|
comp = FindNextComponent(NULL, &desc); //Finds an component that meets the desc spec's
|
|
|
|
if (comp == NULL) {
|
|
|
|
ao_msg(MSGT_AO, MSGL_WARN, "Unable to find Output Unit component\n");
|
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = OpenAComponent(comp, &(ao->theOutputUnit)); //gains access to the services provided by the component
|
|
|
|
if (err) {
|
|
|
|
ao_msg(MSGT_AO, MSGL_WARN, "Unable to open Output Unit component (err=%d)\n", err);
|
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
2005-01-01 03:51:08 +00:00
|
|
|
|
2005-06-05 14:47:26 +00:00
|
|
|
// Initialize AudioUnit
|
|
|
|
err = AudioUnitInitialize(ao->theOutputUnit);
|
|
|
|
if (err) {
|
|
|
|
ao_msg(MSGT_AO, MSGL_WARN, "Unable to initialize Output Unit component (err=%d)\n", err);
|
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
2005-06-29 18:17:19 +00:00
|
|
|
}
|
2005-01-03 03:34:18 +00:00
|
|
|
|
2005-06-05 14:47:26 +00:00
|
|
|
size = sizeof(AudioStreamBasicDescription);
|
2005-06-14 12:41:42 +00:00
|
|
|
err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &inDesc, size);
|
2005-01-03 03:34:18 +00:00
|
|
|
|
2005-06-05 14:47:26 +00:00
|
|
|
if (err) {
|
2005-06-14 12:41:42 +00:00
|
|
|
ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the input format (err=%d)\n", err);
|
2005-01-01 03:51:08 +00:00
|
|
|
return CONTROL_FALSE;
|
2005-06-05 14:47:26 +00:00
|
|
|
}
|
2003-05-21 21:15:46 +00:00
|
|
|
|
2005-07-19 14:36:13 +00:00
|
|
|
size = sizeof(UInt32);
|
|
|
|
err = AudioUnitGetProperty(ao->theOutputUnit, kAudioDevicePropertyBufferSize, kAudioUnitScope_Input, 0, &maxFrames, &size);
|
2005-06-05 14:47:26 +00:00
|
|
|
|
2005-07-19 14:36:13 +00:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
ao_msg(MSGT_AO,MSGL_WARN, "AudioUnitGetProperty returned %d when getting kAudioDevicePropertyBufferSize\n", (int)err);
|
2005-06-05 14:47:26 +00:00
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
2003-05-22 16:12:54 +00:00
|
|
|
|
2005-07-19 14:36:13 +00:00
|
|
|
ao->chunk_size = maxFrames;//*inDesc.mBytesPerFrame;
|
|
|
|
|
2006-08-18 01:19:19 +00:00
|
|
|
ao_data.samplerate = inDesc.mSampleRate;
|
|
|
|
ao_data.channels = inDesc.mChannelsPerFrame;
|
|
|
|
ao_data.bps = ao_data.samplerate * inDesc.mBytesPerFrame;
|
|
|
|
ao_data.outburst = ao->chunk_size;
|
|
|
|
ao_data.buffersize = ao_data.bps;
|
|
|
|
|
|
|
|
ao->num_chunks = (ao_data.bps+ao->chunk_size-1)/ao->chunk_size;
|
2005-02-22 20:54:31 +00:00
|
|
|
ao->buffer_len = (ao->num_chunks + 1) * ao->chunk_size;
|
2006-07-03 13:21:54 +00:00
|
|
|
ao->buffer = aoIsCreated ? realloc(ao->buffer,(ao->num_chunks + 1)*ao->chunk_size)
|
|
|
|
: calloc(ao->num_chunks + 1, ao->chunk_size);
|
2005-06-29 18:17:19 +00:00
|
|
|
|
2006-08-18 01:19:19 +00:00
|
|
|
ao_msg(MSGT_AO,MSGL_V, "using %5d chunks of %d bytes (buffer len %d bytes)\n", (int)ao->num_chunks, (int)ao->chunk_size, (int)ao->buffer_len);
|
2005-06-05 14:47:26 +00:00
|
|
|
|
|
|
|
renderCallback.inputProc = theRenderProc;
|
|
|
|
renderCallback.inputProcRefCon = 0;
|
|
|
|
err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &renderCallback, sizeof(AURenderCallbackStruct));
|
|
|
|
if (err) {
|
|
|
|
ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the render callback (err=%d)\n", err);
|
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
2003-05-21 21:15:46 +00:00
|
|
|
|
2006-08-18 01:19:19 +00:00
|
|
|
reset();
|
2003-05-21 21:15:46 +00:00
|
|
|
|
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int play(void* output_samples,int num_bytes,int flags)
|
|
|
|
{
|
2006-08-18 01:19:19 +00:00
|
|
|
int wrote=write_buffer(output_samples, num_bytes);
|
|
|
|
|
2005-06-29 18:17:19 +00:00
|
|
|
audio_resume();
|
2006-08-18 01:19:19 +00:00
|
|
|
return wrote;
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set variables and buffer to initial state */
|
2006-07-06 04:30:19 +00:00
|
|
|
static void reset(void)
|
2003-05-21 21:15:46 +00:00
|
|
|
{
|
2005-02-22 20:54:31 +00:00
|
|
|
audio_pause();
|
2003-05-21 21:15:46 +00:00
|
|
|
/* reset ring-buffer state */
|
|
|
|
ao->buf_read_pos=0;
|
|
|
|
ao->buf_write_pos=0;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* return available space */
|
2006-07-06 04:30:19 +00:00
|
|
|
static int get_space(void)
|
2003-05-21 21:15:46 +00:00
|
|
|
{
|
2005-02-22 20:54:31 +00:00
|
|
|
return buf_free();
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-22 16:12:54 +00:00
|
|
|
/* return delay until audio is played */
|
2006-07-06 04:30:19 +00:00
|
|
|
static float get_delay(void)
|
2003-05-21 21:15:46 +00:00
|
|
|
{
|
2005-02-22 20:54:31 +00:00
|
|
|
int buffered = ao->buffer_len - ao->chunk_size - buf_free(); // could be less
|
|
|
|
// inaccurate, should also contain the data buffered e.g. by the OS
|
|
|
|
return (float)(buffered)/(float)ao_data.bps;
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* unload plugin and deregister from coreaudio */
|
2004-04-06 17:55:36 +00:00
|
|
|
static void uninit(int immed)
|
2003-05-21 21:15:46 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
OSErr status;
|
|
|
|
|
2006-08-18 01:19:19 +00:00
|
|
|
if (!immed) {
|
|
|
|
long long timeleft=(1000000LL*buf_used())/ao_data.bps;
|
|
|
|
ao_msg(MSGT_AO,MSGL_DBG2, "%d bytes left @%d bps (%ld usec)\n", buf_used(), ao_data.bps, (int)timeleft);
|
|
|
|
usec_sleep((int)timeleft);
|
|
|
|
}
|
2003-05-21 21:15:46 +00:00
|
|
|
|
2005-06-05 14:47:26 +00:00
|
|
|
AudioOutputUnitStop(ao->theOutputUnit);
|
|
|
|
AudioUnitUninitialize(ao->theOutputUnit);
|
|
|
|
CloseComponent(ao->theOutputUnit);
|
2003-05-21 21:15:46 +00:00
|
|
|
|
2005-02-22 20:54:31 +00:00
|
|
|
free(ao->buffer);
|
2003-05-21 21:15:46 +00:00
|
|
|
free(ao);
|
2005-06-29 18:17:19 +00:00
|
|
|
ao = NULL;
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* stop playing, keep buffers (for pause) */
|
2006-07-06 04:30:19 +00:00
|
|
|
static void audio_pause(void)
|
2003-05-21 21:15:46 +00:00
|
|
|
{
|
2005-06-05 14:47:26 +00:00
|
|
|
OSErr status=noErr;
|
2003-05-21 21:15:46 +00:00
|
|
|
|
|
|
|
/* stop callback */
|
2005-06-05 14:47:26 +00:00
|
|
|
status=AudioOutputUnitStop(ao->theOutputUnit);
|
2003-05-21 21:15:46 +00:00
|
|
|
if (status)
|
2005-06-05 14:47:26 +00:00
|
|
|
ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStop returned %d\n",
|
2003-05-21 21:15:46 +00:00
|
|
|
(int)status);
|
2006-08-18 01:19:19 +00:00
|
|
|
ao->paused=1;
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* resume playing, after audio_pause() */
|
2006-07-06 04:30:19 +00:00
|
|
|
static void audio_resume(void)
|
2003-05-21 21:15:46 +00:00
|
|
|
{
|
2006-08-18 01:19:19 +00:00
|
|
|
if(ao->paused) {
|
2005-06-05 14:47:26 +00:00
|
|
|
OSErr status=noErr;
|
2006-08-18 01:19:19 +00:00
|
|
|
/* start callback */
|
2005-06-05 14:47:26 +00:00
|
|
|
status=AudioOutputUnitStart(ao->theOutputUnit);
|
2003-05-21 21:15:46 +00:00
|
|
|
if (status)
|
2005-06-05 14:47:26 +00:00
|
|
|
ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStart returned %d\n",
|
2003-05-21 21:15:46 +00:00
|
|
|
(int)status);
|
2006-08-18 01:19:19 +00:00
|
|
|
ao->paused=0;
|
|
|
|
}
|
2003-05-21 21:15:46 +00:00
|
|
|
}
|