| 1 |
/* |
| 2 |
coreaudio: audio output on MacOS X |
| 3 |
|
| 4 |
copyright ?-2020 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
initially written by Guillaume Outters |
| 7 |
modified by Nicholas J Humfrey to use SFIFO code |
| 8 |
modified by Taihei Monma to use AudioUnit and AudioConverter APIs |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
#include "../out123_int.h" |
| 13 |
|
| 14 |
/* has been around since at least 10.4 */ |
| 15 |
#include <AvailabilityMacros.h> |
| 16 |
|
| 17 |
/* Use AudioComponents API when compiling for >= 10.6, otherwise fall back to |
| 18 |
* Components Manager, which is deprecated since 10.8. |
| 19 |
* MAC_OS_X_VERSION_MIN_REQUIRED defaults to the host system version and can be |
| 20 |
* governed by MACOSX_DEPLOYMENT_TARGET environment variable and |
| 21 |
* -mmacosx-version-min= when running the compiler. */ |
| 22 |
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 || __IPHONE_OS_VERSION_MIN_REQUIRED >= 20000 |
| 23 |
#define HAVE_AUDIOCOMPONENTS 1 |
| 24 |
#endif |
| 25 |
|
| 26 |
#if HAVE_AUDIOCOMPONENTS |
| 27 |
#define MPG123_AUDIOCOMPONENTDESCRIPTION AudioComponentDescription |
| 28 |
#define MPG123_AUDIOCOMPONENT AudioComponent |
| 29 |
#define MPG123_AUDIOCOMPONENTFINDNEXT AudioComponentFindNext |
| 30 |
/* Funky API twist: AudioUnit is actually typedef'd AudioComponentInstance */ |
| 31 |
#define MPG123_AUDIOCOMPONENTINSTANCENEW AudioComponentInstanceNew |
| 32 |
#define MPG123_AUDIOCOMPONENTINSTANCEDISPOSE AudioComponentInstanceDispose |
| 33 |
#else |
| 34 |
#include <CoreServices/CoreServices.h> |
| 35 |
#define MPG123_AUDIOCOMPONENTDESCRIPTION ComponentDescription |
| 36 |
#define MPG123_AUDIOCOMPONENT Component |
| 37 |
#define MPG123_AUDIOCOMPONENTFINDNEXT FindNextComponent |
| 38 |
#define MPG123_AUDIOCOMPONENTINSTANCENEW OpenAComponent |
| 39 |
#define MPG123_AUDIOCOMPONENTINSTANCEDISPOSE CloseComponent |
| 40 |
#endif |
| 41 |
#include <AudioUnit/AudioUnit.h> |
| 42 |
#include <AudioToolbox/AudioToolbox.h> |
| 43 |
#include <errno.h> |
| 44 |
|
| 45 |
/* Including the sfifo code locally, to avoid module linkage issues. */ |
| 46 |
#define SFIFO_STATIC |
| 47 |
#include "../sfifo.c" |
| 48 |
|
| 49 |
#include "../../common/debug.h" |
| 50 |
|
| 51 |
/* Duration of the ring buffer in seconds. |
| 52 |
Is that all that there is to tunable latency? |
| 53 |
Size of 200 ms should be enough for a default value, rare is the |
| 54 |
hardware that actually allows such large buffers. */ |
| 55 |
#define FIFO_DURATION (ao->device_buffer > 0. ? ao->device_buffer : 0.2) |
| 56 |
|
| 57 |
|
| 58 |
typedef struct mpg123_coreaudio |
| 59 |
{ |
| 60 |
AudioConverterRef converter; |
| 61 |
AudioUnit outputUnit; |
| 62 |
int open; |
| 63 |
char play; |
| 64 |
int channels; |
| 65 |
int bps; |
| 66 |
int play_done; |
| 67 |
int decode_done; |
| 68 |
|
| 69 |
/* Convertion buffer */ |
| 70 |
unsigned char * buffer; |
| 71 |
size_t buffer_size; |
| 72 |
|
| 73 |
/* Ring buffer */ |
| 74 |
sfifo_t fifo; |
| 75 |
|
| 76 |
} mpg123_coreaudio_t; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static OSStatus playProc(AudioConverterRef inAudioConverter, |
| 81 |
UInt32 *ioNumberDataPackets, |
| 82 |
AudioBufferList *outOutputData, |
| 83 |
AudioStreamPacketDescription **outDataPacketDescription, |
| 84 |
void* inClientData) |
| 85 |
{ |
| 86 |
out123_handle *ao = (out123_handle*)inClientData; |
| 87 |
mpg123_coreaudio_t *ca = (mpg123_coreaudio_t *)ao->userptr; |
| 88 |
long n; |
| 89 |
|
| 90 |
/* This is not actually a loop. See the early break. */ |
| 91 |
for(n = 0; n < outOutputData->mNumberBuffers; n++) |
| 92 |
{ |
| 93 |
unsigned int wanted = *ioNumberDataPackets * ca->channels * ca->bps; |
| 94 |
unsigned char *dest; |
| 95 |
unsigned int read; |
| 96 |
int avail; |
| 97 |
|
| 98 |
/* Any buffer count > 1 would wreck havoc with this code. */ |
| 99 |
if(n > 0) |
| 100 |
break; |
| 101 |
|
| 102 |
if(ca->buffer_size < wanted) { |
| 103 |
debug1("Allocating %d byte sample conversion buffer", wanted); |
| 104 |
ca->buffer = realloc( ca->buffer, wanted); |
| 105 |
ca->buffer_size = wanted; |
| 106 |
} |
| 107 |
dest = ca->buffer; |
| 108 |
if(!dest) |
| 109 |
return -1; |
| 110 |
|
| 111 |
/* Only play if we have data left */ |
| 112 |
while((avail=sfifo_used( &ca->fifo )) < wanted && !ca->decode_done) |
| 113 |
{ |
| 114 |
int ms = (wanted-avail)/ao->framesize*1000/ao->rate; |
| 115 |
debug3("waiting for more input, %d ms missing (%i < %u)" |
| 116 |
, ms, avail, wanted); |
| 117 |
usleep(ms*100); /* Wait for 1/10th of the missing duration. Might want to adjust. */ |
| 118 |
} |
| 119 |
if(avail > wanted) |
| 120 |
avail = wanted; |
| 121 |
else if(ca->decode_done) |
| 122 |
ca->play_done = 1; |
| 123 |
|
| 124 |
/* Read audio from FIFO to CoreAudio's buffer */ |
| 125 |
read = sfifo_read(&ca->fifo, dest, avail); |
| 126 |
|
| 127 |
if(read!=avail && !AOQUIET) |
| 128 |
warning2("Error reading from the ring buffer (avail=%u, read=%u).\n", avail, read); |
| 129 |
|
| 130 |
outOutputData->mBuffers[n].mDataByteSize = read; |
| 131 |
outOutputData->mBuffers[n].mData = dest; |
| 132 |
} |
| 133 |
|
| 134 |
return noErr; |
| 135 |
} |
| 136 |
|
| 137 |
static OSStatus convertProc(void *inRefCon, AudioUnitRenderActionFlags *inActionFlags, |
| 138 |
const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, |
| 139 |
UInt32 inNumFrames, AudioBufferList *ioData) |
| 140 |
{ |
| 141 |
AudioStreamPacketDescription* outPacketDescription = NULL; |
| 142 |
out123_handle *ao = (out123_handle*)inRefCon; |
| 143 |
mpg123_coreaudio_t *ca = (mpg123_coreaudio_t *)ao->userptr; |
| 144 |
OSStatus err= noErr; |
| 145 |
|
| 146 |
err = AudioConverterFillComplexBuffer(ca->converter, playProc, inRefCon, &inNumFrames, ioData, outPacketDescription); |
| 147 |
|
| 148 |
return err; |
| 149 |
} |
| 150 |
|
| 151 |
static int open_coreaudio(out123_handle *ao) |
| 152 |
{ |
| 153 |
mpg123_coreaudio_t* ca = (mpg123_coreaudio_t*)ao->userptr; |
| 154 |
UInt32 size; |
| 155 |
MPG123_AUDIOCOMPONENTDESCRIPTION desc; |
| 156 |
MPG123_AUDIOCOMPONENT comp; |
| 157 |
AudioStreamBasicDescription inFormat; |
| 158 |
AudioStreamBasicDescription outFormat; |
| 159 |
AURenderCallbackStruct renderCallback; |
| 160 |
Boolean outWritable; |
| 161 |
|
| 162 |
/* Initialize our environment */ |
| 163 |
ca->play = 0; |
| 164 |
ca->buffer = NULL; |
| 165 |
ca->buffer_size = 0; |
| 166 |
ca->play_done = 0; |
| 167 |
ca->decode_done = 0; |
| 168 |
|
| 169 |
/* Get the default audio output unit */ |
| 170 |
desc.componentType = kAudioUnitType_Output; |
| 171 |
#if TARGET_OS_IPHONE |
| 172 |
desc.componentSubType = kAudioUnitSubType_RemoteIO; |
| 173 |
#else |
| 174 |
desc.componentSubType = kAudioUnitSubType_DefaultOutput; |
| 175 |
#endif |
| 176 |
desc.componentManufacturer = kAudioUnitManufacturer_Apple; |
| 177 |
desc.componentFlags = 0; |
| 178 |
desc.componentFlagsMask = 0; |
| 179 |
comp = MPG123_AUDIOCOMPONENTFINDNEXT(NULL, &desc); |
| 180 |
if(comp == NULL) |
| 181 |
{ |
| 182 |
if(!AOQUIET) |
| 183 |
error("AudioComponentFindNext failed"); |
| 184 |
return(-1); |
| 185 |
} |
| 186 |
|
| 187 |
if(MPG123_AUDIOCOMPONENTINSTANCENEW(comp, &(ca->outputUnit))) |
| 188 |
{ |
| 189 |
if(!AOQUIET) |
| 190 |
error("AudioComponentInstanceNew failed"); |
| 191 |
return (-1); |
| 192 |
} |
| 193 |
|
| 194 |
if(AudioUnitInitialize(ca->outputUnit)) |
| 195 |
{ |
| 196 |
if(!AOQUIET) |
| 197 |
error("AudioUnitInitialize failed"); |
| 198 |
return (-1); |
| 199 |
} |
| 200 |
|
| 201 |
/* Specify the output PCM format */ |
| 202 |
AudioUnitGetPropertyInfo(ca->outputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &size, &outWritable); |
| 203 |
if(AudioUnitGetProperty(ca->outputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &outFormat, &size)) |
| 204 |
{ |
| 205 |
if(!AOQUIET) |
| 206 |
error("AudioUnitGetProperty(kAudioUnitProperty_StreamFormat) failed"); |
| 207 |
return (-1); |
| 208 |
} |
| 209 |
|
| 210 |
if(AudioUnitSetProperty(ca->outputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &outFormat, size)) |
| 211 |
{ |
| 212 |
if(!AOQUIET) |
| 213 |
error("AudioUnitSetProperty(kAudioUnitProperty_StreamFormat) failed"); |
| 214 |
return (-1); |
| 215 |
} |
| 216 |
|
| 217 |
/* Specify the input PCM format */ |
| 218 |
ca->channels = ao->channels; |
| 219 |
inFormat.mSampleRate = ao->rate; |
| 220 |
inFormat.mChannelsPerFrame = ao->channels; |
| 221 |
inFormat.mFormatID = kAudioFormatLinearPCM; |
| 222 |
#ifdef _BIG_ENDIAN |
| 223 |
inFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsBigEndian; |
| 224 |
#else |
| 225 |
inFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked; |
| 226 |
#endif |
| 227 |
|
| 228 |
switch(ao->format) |
| 229 |
{ |
| 230 |
case MPG123_ENC_SIGNED_16: |
| 231 |
inFormat.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger; |
| 232 |
ca->bps = 2; |
| 233 |
break; |
| 234 |
case MPG123_ENC_SIGNED_8: |
| 235 |
inFormat.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger; |
| 236 |
ca->bps = 1; |
| 237 |
break; |
| 238 |
case MPG123_ENC_UNSIGNED_8: |
| 239 |
ca->bps = 1; |
| 240 |
break; |
| 241 |
case MPG123_ENC_SIGNED_32: |
| 242 |
inFormat.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger; |
| 243 |
ca->bps = 4; |
| 244 |
break; |
| 245 |
case MPG123_ENC_FLOAT_32: |
| 246 |
inFormat.mFormatFlags |= kLinearPCMFormatFlagIsFloat; |
| 247 |
ca->bps = 4; |
| 248 |
break; |
| 249 |
} |
| 250 |
|
| 251 |
inFormat.mBitsPerChannel = ca->bps << 3; |
| 252 |
inFormat.mBytesPerPacket = ca->bps*inFormat.mChannelsPerFrame; |
| 253 |
inFormat.mFramesPerPacket = 1; |
| 254 |
inFormat.mBytesPerFrame = ca->bps*inFormat.mChannelsPerFrame; |
| 255 |
|
| 256 |
/* Add our callback - but don't start it yet */ |
| 257 |
memset(&renderCallback, 0, sizeof(AURenderCallbackStruct)); |
| 258 |
renderCallback.inputProc = convertProc; |
| 259 |
renderCallback.inputProcRefCon = ao; |
| 260 |
if(AudioUnitSetProperty(ca->outputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &renderCallback, sizeof(AURenderCallbackStruct))) |
| 261 |
{ |
| 262 |
if(!AOQUIET) |
| 263 |
error("AudioUnitSetProperty(kAudioUnitProperty_SetRenderCallback) failed"); |
| 264 |
return(-1); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
/* Open an audio I/O stream and create converter */ |
| 269 |
if (ao->rate > 0 && ao->channels >0 ) { |
| 270 |
int ringbuffer_len; |
| 271 |
|
| 272 |
if(AudioConverterNew(&inFormat, &outFormat, &(ca->converter))) |
| 273 |
{ |
| 274 |
if(!AOQUIET) |
| 275 |
error("AudioConverterNew failed"); |
| 276 |
return(-1); |
| 277 |
} |
| 278 |
if(ao->channels == 1) { |
| 279 |
SInt32 channelMap[2] = { 0, 0 }; |
| 280 |
if(AudioConverterSetProperty(ca->converter, kAudioConverterChannelMap, sizeof(channelMap), channelMap)) |
| 281 |
{ |
| 282 |
if(!AOQUIET) |
| 283 |
error("AudioConverterSetProperty(kAudioConverterChannelMap) failed"); |
| 284 |
return(-1); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/* Initialise FIFO */ |
| 289 |
ringbuffer_len = ao->rate * FIFO_DURATION * ca->bps * ao->channels; |
| 290 |
debug2( "Allocating %d byte ring-buffer (%f seconds)", ringbuffer_len, (float)FIFO_DURATION); |
| 291 |
sfifo_init( &ca->fifo, ringbuffer_len ); |
| 292 |
} |
| 293 |
|
| 294 |
return(0); |
| 295 |
} |
| 296 |
|
| 297 |
static int get_formats_coreaudio(out123_handle *ao) |
| 298 |
{ |
| 299 |
return MPG123_ENC_SIGNED_16|MPG123_ENC_SIGNED_8|MPG123_ENC_UNSIGNED_8|MPG123_ENC_SIGNED_32|MPG123_ENC_FLOAT_32; |
| 300 |
} |
| 301 |
|
| 302 |
static int write_coreaudio(out123_handle *ao, unsigned char *buf, int len) |
| 303 |
{ |
| 304 |
mpg123_coreaudio_t* ca = (mpg123_coreaudio_t*)ao->userptr; |
| 305 |
int len_remain = len; |
| 306 |
|
| 307 |
/* Some busy waiting, but feed what is possible. */ |
| 308 |
while(len_remain) /* Note: input len is multiple of framesize! */ |
| 309 |
{ |
| 310 |
int block = sfifo_space(&ca->fifo); |
| 311 |
block -= block % ao->framesize; |
| 312 |
if(block > len_remain) |
| 313 |
block = len_remain; |
| 314 |
if(block) |
| 315 |
{ |
| 316 |
sfifo_write(&ca->fifo, buf, block); |
| 317 |
len_remain -= block; |
| 318 |
buf += block; |
| 319 |
/* Start playback now that we have something to play */ |
| 320 |
if(!ca->play && (sfifo_used(&ca->fifo) > (sfifo_size(&ca->fifo)/2))) |
| 321 |
{ |
| 322 |
if(AudioOutputUnitStart(ca->outputUnit)) |
| 323 |
{ |
| 324 |
if(!AOQUIET) |
| 325 |
error("AudioOutputUnitStart failed"); |
| 326 |
return(-1); |
| 327 |
} |
| 328 |
ca->play = 1; |
| 329 |
} |
| 330 |
} |
| 331 |
/* If there is no room, then sleep for a bit, but not too long. */ |
| 332 |
if(len_remain) |
| 333 |
usleep( (0.1*FIFO_DURATION) * 1000000 ); |
| 334 |
} |
| 335 |
|
| 336 |
return len; |
| 337 |
} |
| 338 |
|
| 339 |
static int close_coreaudio(out123_handle *ao) |
| 340 |
{ |
| 341 |
mpg123_coreaudio_t* ca = (mpg123_coreaudio_t*)ao->userptr; |
| 342 |
|
| 343 |
if (ca) { |
| 344 |
ca->decode_done = 1; |
| 345 |
while(!ca->play_done && ca->play) |
| 346 |
usleep((0.1*FIFO_DURATION)*1000000); |
| 347 |
/* No matter the error code, we want to close it (by brute force if necessary) */ |
| 348 |
AudioOutputUnitStop(ca->outputUnit); |
| 349 |
AudioUnitUninitialize(ca->outputUnit); |
| 350 |
MPG123_AUDIOCOMPONENTINSTANCEDISPOSE(ca->outputUnit); |
| 351 |
AudioConverterDispose(ca->converter); |
| 352 |
|
| 353 |
/* Free the ring buffer */ |
| 354 |
sfifo_close( &ca->fifo ); |
| 355 |
|
| 356 |
/* Free the conversion buffer */ |
| 357 |
if (ca->buffer) { |
| 358 |
free( ca->buffer ); |
| 359 |
ca->buffer = NULL; |
| 360 |
} |
| 361 |
|
| 362 |
} |
| 363 |
|
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
static void flush_coreaudio(out123_handle *ao) |
| 368 |
{ |
| 369 |
mpg123_coreaudio_t* ca = (mpg123_coreaudio_t*)ao->userptr; |
| 370 |
|
| 371 |
/* Flush AudioConverter's buffer */ |
| 372 |
if(AudioConverterReset(ca->converter)) |
| 373 |
{ |
| 374 |
if(!AOQUIET) |
| 375 |
error("AudioConverterReset failed"); |
| 376 |
} |
| 377 |
|
| 378 |
/* Empty out the ring buffer */ |
| 379 |
sfifo_flush( &ca->fifo ); |
| 380 |
} |
| 381 |
|
| 382 |
static void deinit_coreaudio(out123_handle* ao) |
| 383 |
{ |
| 384 |
/* Free up memory */ |
| 385 |
if (ao->userptr) { |
| 386 |
free( ao->userptr ); |
| 387 |
ao->userptr = NULL; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
static int init_coreaudio(out123_handle* ao) |
| 392 |
{ |
| 393 |
if (ao==NULL) return -1; |
| 394 |
|
| 395 |
/* Set callbacks */ |
| 396 |
ao->open = open_coreaudio; |
| 397 |
ao->flush = flush_coreaudio; |
| 398 |
ao->write = write_coreaudio; |
| 399 |
ao->get_formats = get_formats_coreaudio; |
| 400 |
ao->close = close_coreaudio; |
| 401 |
ao->deinit = deinit_coreaudio; |
| 402 |
|
| 403 |
/* Allocate memory for data structure */ |
| 404 |
ao->userptr = malloc( sizeof( mpg123_coreaudio_t ) ); |
| 405 |
if (ao->userptr==NULL) |
| 406 |
{ |
| 407 |
if(!AOQUIET) |
| 408 |
error("failed to malloc memory for 'mpg123_coreaudio_t'"); |
| 409 |
return -1; |
| 410 |
} |
| 411 |
memset( ao->userptr, 0, sizeof(mpg123_coreaudio_t) ); |
| 412 |
|
| 413 |
/* Success */ |
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
/* |
| 420 |
Module information data structure |
| 421 |
*/ |
| 422 |
mpg123_module_t mpg123_output_module_info = { |
| 423 |
/* api_version */ MPG123_MODULE_API_VERSION, |
| 424 |
/* name */ "coreaudio", |
| 425 |
/* description */ "Output audio using Mac OS X's CoreAudio.", |
| 426 |
/* revision */ "$Rev:$", |
| 427 |
/* handle */ NULL, |
| 428 |
|
| 429 |
/* init_output */ init_coreaudio, |
| 430 |
}; |
| 431 |
|
| 432 |
|