Merge svn changes up to r30663
[mplayer/kovensky.git] / libao2 / ao_coreaudio.c
blobb60fb094e705386a2082e29a3d94b0861c80ed5d
1 /*
2 * CoreAudio audio output driver for Mac OS X
4 * original copyright (C) Timothy J. Wood - Aug 2000
5 * ported to MPlayer libao2 by Dan Christiansen
7 * The S/PDIF part of the code is based on the auhal audio output
8 * module from VideoLAN:
9 * Copyright (c) 2006 Derk-Jan Hartman <hartman at videolan dot org>
11 * This file is part of MPlayer.
13 * MPlayer is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * MPlayer is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * along with MPlayer; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 * The MacOS X CoreAudio framework doesn't mesh as simply as some
30 * simpler frameworks do. This is due to the fact that CoreAudio pulls
31 * audio samples rather than having them pushed at it (which is nice
32 * when you are wanting to do good buffering of audio).
34 * AC-3 and MPEG audio passthrough is possible, but has never been tested
35 * due to lack of a soundcard that supports it.
38 #include <CoreServices/CoreServices.h>
39 #include <AudioUnit/AudioUnit.h>
40 #include <AudioToolbox/AudioToolbox.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <inttypes.h>
45 #include <sys/types.h>
46 #include <unistd.h>
48 #include "config.h"
49 #include "mp_msg.h"
51 #include "audio_out.h"
52 #include "audio_out_internal.h"
53 #include "libaf/af_format.h"
54 #include "osdep/timer.h"
55 #include "libavutil/fifo.h"
57 static const ao_info_t info =
59 "Darwin/Mac OS X native audio output",
60 "coreaudio",
61 "Timothy J. Wood & Dan Christiansen & Chris Roccati",
65 LIBAO_EXTERN(coreaudio)
67 /* Prefix for all mp_msg() calls */
68 #define ao_msg(a, b, c...) mp_msg(a, b, "AO: [coreaudio] " c)
70 typedef struct ao_coreaudio_s
72 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device. */
73 int b_supports_digital; /* Does the currently selected device support digital mode? */
74 int b_digital; /* Are we running in digital mode? */
75 int b_muted; /* Are we muted in digital mode? */
77 /* AudioUnit */
78 AudioUnit theOutputUnit;
80 /* CoreAudio SPDIF mode specific */
81 pid_t i_hog_pid; /* Keeps the pid of our hog status. */
82 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */
83 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */
84 AudioStreamBasicDescription stream_format;/* The format we changed the stream to */
85 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */
86 int b_revert; /* Whether we need to revert the stream format */
87 int b_changed_mixing; /* Whether we need to set the mixing mode back */
88 int b_stream_format_changed; /* Flag for main thread to reset stream's format to digital and reset buffer */
90 /* Original common part */
91 int packetSize;
92 int paused;
94 /* Ring-buffer */
95 AVFifoBuffer *buffer;
96 unsigned int buffer_len; ///< must always be num_chunks * chunk_size
97 unsigned int num_chunks;
98 unsigned int chunk_size;
99 } ao_coreaudio_t;
101 static ao_coreaudio_t *ao = NULL;
104 * \brief add data to ringbuffer
106 static int write_buffer(unsigned char* data, int len){
107 int free = ao->buffer_len - av_fifo_size(ao->buffer);
108 if (len > free) len = free;
109 return av_fifo_generic_write(ao->buffer, data, len, NULL);
113 * \brief remove data from ringbuffer
115 static int read_buffer(unsigned char* data,int len){
116 int buffered = av_fifo_size(ao->buffer);
117 if (len > buffered) len = buffered;
118 av_fifo_generic_read(ao->buffer, data, len, NULL);
119 return len;
122 OSStatus theRenderProc(void *inRefCon, AudioUnitRenderActionFlags *inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumFrames, AudioBufferList *ioData)
124 int amt=av_fifo_size(ao->buffer);
125 int req=(inNumFrames)*ao->packetSize;
127 if(amt>req)
128 amt=req;
130 if(amt)
131 read_buffer((unsigned char *)ioData->mBuffers[0].mData, amt);
132 else audio_pause();
133 ioData->mBuffers[0].mDataByteSize = amt;
135 return noErr;
138 static int control(int cmd,void *arg){
139 ao_control_vol_t *control_vol;
140 OSStatus err;
141 Float32 vol;
142 switch (cmd) {
143 case AOCONTROL_GET_VOLUME:
144 control_vol = (ao_control_vol_t*)arg;
145 if (ao->b_digital) {
146 // Digital output has no volume adjust.
147 return CONTROL_FALSE;
149 err = AudioUnitGetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &vol);
151 if(err==0) {
152 // printf("GET VOL=%f\n", vol);
153 control_vol->left=control_vol->right=vol*100.0/4.0;
154 return CONTROL_TRUE;
156 else {
157 ao_msg(MSGT_AO, MSGL_WARN, "could not get HAL output volume: [%4.4s]\n", (char *)&err);
158 return CONTROL_FALSE;
161 case AOCONTROL_SET_VOLUME:
162 control_vol = (ao_control_vol_t*)arg;
164 if (ao->b_digital) {
165 // Digital output can not set volume. Here we have to return true
166 // to make mixer forget it. Else mixer will add a soft filter,
167 // that's not we expected and the filter not support ac3 stream
168 // will cause mplayer die.
170 // Although not support set volume, but at least we support mute.
171 // MPlayer set mute by set volume to zero, we handle it.
172 if (control_vol->left == 0 && control_vol->right == 0)
173 ao->b_muted = 1;
174 else
175 ao->b_muted = 0;
176 return CONTROL_TRUE;
179 vol=(control_vol->left+control_vol->right)*4.0/200.0;
180 err = AudioUnitSetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0);
181 if(err==0) {
182 // printf("SET VOL=%f\n", vol);
183 return CONTROL_TRUE;
185 else {
186 ao_msg(MSGT_AO, MSGL_WARN, "could not set HAL output volume: [%4.4s]\n", (char *)&err);
187 return CONTROL_FALSE;
189 /* Everything is currently unimplemented */
190 default:
191 return CONTROL_FALSE;
197 static void print_format(int lev, const char* str, const AudioStreamBasicDescription *f){
198 uint32_t flags=(uint32_t) f->mFormatFlags;
199 ao_msg(MSGT_AO,lev, "%s %7.1fHz %lubit [%c%c%c%c][%lu][%lu][%lu][%lu][%lu] %s %s %s%s%s%s\n",
200 str, f->mSampleRate, f->mBitsPerChannel,
201 (int)(f->mFormatID & 0xff000000) >> 24,
202 (int)(f->mFormatID & 0x00ff0000) >> 16,
203 (int)(f->mFormatID & 0x0000ff00) >> 8,
204 (int)(f->mFormatID & 0x000000ff) >> 0,
205 f->mFormatFlags, f->mBytesPerPacket,
206 f->mFramesPerPacket, f->mBytesPerFrame,
207 f->mChannelsPerFrame,
208 (flags&kAudioFormatFlagIsFloat) ? "float" : "int",
209 (flags&kAudioFormatFlagIsBigEndian) ? "BE" : "LE",
210 (flags&kAudioFormatFlagIsSignedInteger) ? "S" : "U",
211 (flags&kAudioFormatFlagIsPacked) ? " packed" : "",
212 (flags&kAudioFormatFlagIsAlignedHigh) ? " aligned" : "",
213 (flags&kAudioFormatFlagIsNonInterleaved) ? " ni" : "" );
217 static int AudioDeviceSupportsDigital( AudioDeviceID i_dev_id );
218 static int AudioStreamSupportsDigital( AudioStreamID i_stream_id );
219 static int OpenSPDIF(void);
220 static int AudioStreamChangeFormat( AudioStreamID i_stream_id, AudioStreamBasicDescription change_format );
221 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice,
222 const AudioTimeStamp * inNow,
223 const void * inInputData,
224 const AudioTimeStamp * inInputTime,
225 AudioBufferList * outOutputData,
226 const AudioTimeStamp * inOutputTime,
227 void * threadGlobals );
228 static OSStatus StreamListener( AudioStreamID inStream,
229 UInt32 inChannel,
230 AudioDevicePropertyID inPropertyID,
231 void * inClientData );
232 static OSStatus DeviceListener( AudioDeviceID inDevice,
233 UInt32 inChannel,
234 Boolean isInput,
235 AudioDevicePropertyID inPropertyID,
236 void* inClientData );
238 static int init(int rate,int channels,int format,int flags)
240 AudioStreamBasicDescription inDesc;
241 ComponentDescription desc;
242 Component comp;
243 AURenderCallbackStruct renderCallback;
244 OSStatus err;
245 UInt32 size, maxFrames, i_param_size;
246 char *psz_name;
247 AudioDeviceID devid_def = 0;
248 int b_alive;
250 ao_msg(MSGT_AO,MSGL_V, "init([%dHz][%dch][%s][%d])\n", rate, channels, af_fmt2str_short(format), flags);
252 ao = calloc(1, sizeof(ao_coreaudio_t));
254 ao->i_selected_dev = 0;
255 ao->b_supports_digital = 0;
256 ao->b_digital = 0;
257 ao->b_muted = 0;
258 ao->b_stream_format_changed = 0;
259 ao->i_hog_pid = -1;
260 ao->i_stream_id = 0;
261 ao->i_stream_index = -1;
262 ao->b_revert = 0;
263 ao->b_changed_mixing = 0;
265 /* Probe whether device support S/PDIF stream output if input is AC3 stream. */
266 if (AF_FORMAT_IS_AC3(format))
268 /* Find the ID of the default Device. */
269 i_param_size = sizeof(AudioDeviceID);
270 err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
271 &i_param_size, &devid_def);
272 if (err != noErr)
274 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device: [%4.4s]\n", (char *)&err);
275 goto err_out;
278 /* Retrieve the length of the device name. */
279 i_param_size = 0;
280 err = AudioDeviceGetPropertyInfo(devid_def, 0, 0,
281 kAudioDevicePropertyDeviceName,
282 &i_param_size, NULL);
283 if (err != noErr)
285 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device name length: [%4.4s]\n", (char *)&err);
286 goto err_out;
289 /* Retrieve the name of the device. */
290 psz_name = (char *)malloc(i_param_size);
291 err = AudioDeviceGetProperty(devid_def, 0, 0,
292 kAudioDevicePropertyDeviceName,
293 &i_param_size, psz_name);
294 if (err != noErr)
296 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device name: [%4.4s]\n", (char *)&err);
297 free( psz_name);
298 goto err_out;
301 ao_msg(MSGT_AO,MSGL_V, "got default audio output device ID: %#lx Name: %s\n", devid_def, psz_name );
303 if (AudioDeviceSupportsDigital(devid_def))
305 ao->b_supports_digital = 1;
306 ao->i_selected_dev = devid_def;
308 ao_msg(MSGT_AO,MSGL_V, "probe default audio output device whether support digital s/pdif output:%d\n", ao->b_supports_digital );
310 free( psz_name);
313 // Build Description for the input format
314 inDesc.mSampleRate=rate;
315 inDesc.mFormatID=ao->b_supports_digital ? kAudioFormat60958AC3 : kAudioFormatLinearPCM;
316 inDesc.mChannelsPerFrame=channels;
317 inDesc.mBitsPerChannel=af_fmt2bits(format);
319 if((format&AF_FORMAT_POINT_MASK)==AF_FORMAT_F) {
320 // float
321 inDesc.mFormatFlags = kAudioFormatFlagIsFloat|kAudioFormatFlagIsPacked;
323 else if((format&AF_FORMAT_SIGN_MASK)==AF_FORMAT_SI) {
324 // signed int
325 inDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked;
327 else {
328 // unsigned int
329 inDesc.mFormatFlags = kAudioFormatFlagIsPacked;
331 if ((format & AF_FORMAT_END_MASK) == AF_FORMAT_BE)
332 inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian;
334 inDesc.mFramesPerPacket = 1;
335 ao->packetSize = inDesc.mBytesPerPacket = inDesc.mBytesPerFrame = inDesc.mFramesPerPacket*channels*(inDesc.mBitsPerChannel/8);
336 print_format(MSGL_V, "source:",&inDesc);
338 if (ao->b_supports_digital)
340 b_alive = 1;
341 i_param_size = sizeof(b_alive);
342 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE,
343 kAudioDevicePropertyDeviceIsAlive,
344 &i_param_size, &b_alive);
345 if (err != noErr)
346 ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is alive: [%4.4s]\n", (char *)&err);
347 if (!b_alive)
348 ao_msg(MSGT_AO, MSGL_WARN, "device is not alive\n" );
349 /* S/PDIF output need device in HogMode. */
350 i_param_size = sizeof(ao->i_hog_pid);
351 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE,
352 kAudioDevicePropertyHogMode,
353 &i_param_size, &ao->i_hog_pid);
355 if (err != noErr)
357 /* This is not a fatal error. Some drivers simply don't support this property. */
358 ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is hogged: [%4.4s]\n",
359 (char *)&err);
360 ao->i_hog_pid = -1;
363 if (ao->i_hog_pid != -1 && ao->i_hog_pid != getpid())
365 ao_msg(MSGT_AO, MSGL_WARN, "Selected audio device is exclusively in use by another program.\n" );
366 goto err_out;
368 ao->stream_format = inDesc;
369 return OpenSPDIF();
372 /* original analog output code */
373 desc.componentType = kAudioUnitType_Output;
374 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
375 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
376 desc.componentFlags = 0;
377 desc.componentFlagsMask = 0;
379 comp = FindNextComponent(NULL, &desc); //Finds an component that meets the desc spec's
380 if (comp == NULL) {
381 ao_msg(MSGT_AO, MSGL_WARN, "Unable to find Output Unit component\n");
382 goto err_out;
385 err = OpenAComponent(comp, &(ao->theOutputUnit)); //gains access to the services provided by the component
386 if (err) {
387 ao_msg(MSGT_AO, MSGL_WARN, "Unable to open Output Unit component: [%4.4s]\n", (char *)&err);
388 goto err_out;
391 // Initialize AudioUnit
392 err = AudioUnitInitialize(ao->theOutputUnit);
393 if (err) {
394 ao_msg(MSGT_AO, MSGL_WARN, "Unable to initialize Output Unit component: [%4.4s]\n", (char *)&err);
395 goto err_out1;
398 size = sizeof(AudioStreamBasicDescription);
399 err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &inDesc, size);
401 if (err) {
402 ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the input format: [%4.4s]\n", (char *)&err);
403 goto err_out2;
406 size = sizeof(UInt32);
407 err = AudioUnitGetProperty(ao->theOutputUnit, kAudioDevicePropertyBufferSize, kAudioUnitScope_Input, 0, &maxFrames, &size);
409 if (err)
411 ao_msg(MSGT_AO,MSGL_WARN, "AudioUnitGetProperty returned [%4.4s] when getting kAudioDevicePropertyBufferSize\n", (char *)&err);
412 goto err_out2;
415 ao->chunk_size = maxFrames;//*inDesc.mBytesPerFrame;
417 ao_data.samplerate = inDesc.mSampleRate;
418 ao_data.channels = inDesc.mChannelsPerFrame;
419 ao_data.bps = ao_data.samplerate * inDesc.mBytesPerFrame;
420 ao_data.outburst = ao->chunk_size;
421 ao_data.buffersize = ao_data.bps;
423 ao->num_chunks = (ao_data.bps+ao->chunk_size-1)/ao->chunk_size;
424 ao->buffer_len = ao->num_chunks * ao->chunk_size;
425 ao->buffer = av_fifo_alloc(ao->buffer_len);
427 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);
429 renderCallback.inputProc = theRenderProc;
430 renderCallback.inputProcRefCon = 0;
431 err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &renderCallback, sizeof(AURenderCallbackStruct));
432 if (err) {
433 ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the render callback: [%4.4s]\n", (char *)&err);
434 goto err_out2;
437 reset();
439 return CONTROL_OK;
441 err_out2:
442 AudioUnitUninitialize(ao->theOutputUnit);
443 err_out1:
444 CloseComponent(ao->theOutputUnit);
445 err_out:
446 av_fifo_free(ao->buffer);
447 free(ao);
448 ao = NULL;
449 return CONTROL_FALSE;
452 /*****************************************************************************
453 * Setup a encoded digital stream (SPDIF)
454 *****************************************************************************/
455 static int OpenSPDIF(void)
457 OSStatus err = noErr;
458 UInt32 i_param_size, b_mix = 0;
459 Boolean b_writeable = 0;
460 AudioStreamID *p_streams = NULL;
461 int i, i_streams = 0;
463 /* Start doing the SPDIF setup process. */
464 ao->b_digital = 1;
466 /* Hog the device. */
467 i_param_size = sizeof(ao->i_hog_pid);
468 ao->i_hog_pid = getpid() ;
470 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE,
471 kAudioDevicePropertyHogMode, i_param_size, &ao->i_hog_pid);
473 if (err != noErr)
475 ao_msg(MSGT_AO, MSGL_WARN, "failed to set hogmode: [%4.4s]\n", (char *)&err);
476 ao->i_hog_pid = -1;
477 goto err_out;
480 /* Set mixable to false if we are allowed to. */
481 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE,
482 kAudioDevicePropertySupportsMixing,
483 &i_param_size, &b_writeable);
484 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE,
485 kAudioDevicePropertySupportsMixing,
486 &i_param_size, &b_mix);
487 if (err != noErr && b_writeable)
489 b_mix = 0;
490 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE,
491 kAudioDevicePropertySupportsMixing,
492 i_param_size, &b_mix);
493 ao->b_changed_mixing = 1;
495 if (err != noErr)
497 ao_msg(MSGT_AO, MSGL_WARN, "failed to set mixmode: [%4.4s]\n", (char *)&err);
498 goto err_out;
501 /* Get a list of all the streams on this device. */
502 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE,
503 kAudioDevicePropertyStreams,
504 &i_param_size, NULL);
505 if (err != noErr)
507 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streams: [%4.4s]\n", (char *)&err);
508 goto err_out;
511 i_streams = i_param_size / sizeof(AudioStreamID);
512 p_streams = (AudioStreamID *)malloc(i_param_size);
513 if (p_streams == NULL)
515 ao_msg(MSGT_AO, MSGL_WARN, "out of memory\n" );
516 goto err_out;
519 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE,
520 kAudioDevicePropertyStreams,
521 &i_param_size, p_streams);
522 if (err != noErr)
524 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streams: [%4.4s]\n", (char *)&err);
525 if (p_streams) free(p_streams);
526 goto err_out;
529 ao_msg(MSGT_AO, MSGL_V, "current device stream number: %d\n", i_streams);
531 for (i = 0; i < i_streams && ao->i_stream_index < 0; ++i)
533 /* Find a stream with a cac3 stream. */
534 AudioStreamBasicDescription *p_format_list = NULL;
535 int i_formats = 0, j = 0, b_digital = 0;
537 /* Retrieve all the stream formats supported by each output stream. */
538 err = AudioStreamGetPropertyInfo(p_streams[i], 0,
539 kAudioStreamPropertyPhysicalFormats,
540 &i_param_size, NULL);
541 if (err != noErr)
543 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streamformats: [%4.4s]\n", (char *)&err);
544 continue;
547 i_formats = i_param_size / sizeof(AudioStreamBasicDescription);
548 p_format_list = (AudioStreamBasicDescription *)malloc(i_param_size);
549 if (p_format_list == NULL)
551 ao_msg(MSGT_AO, MSGL_WARN, "could not malloc the memory\n" );
552 continue;
555 err = AudioStreamGetProperty(p_streams[i], 0,
556 kAudioStreamPropertyPhysicalFormats,
557 &i_param_size, p_format_list);
558 if (err != noErr)
560 ao_msg(MSGT_AO, MSGL_WARN, "could not get the list of streamformats: [%4.4s]\n", (char *)&err);
561 if (p_format_list) free(p_format_list);
562 continue;
565 /* Check if one of the supported formats is a digital format. */
566 for (j = 0; j < i_formats; ++j)
568 if (p_format_list[j].mFormatID == 'IAC3' ||
569 p_format_list[j].mFormatID == kAudioFormat60958AC3)
571 b_digital = 1;
572 break;
576 if (b_digital)
578 /* If this stream supports a digital (cac3) format, then set it. */
579 int i_requested_rate_format = -1;
580 int i_current_rate_format = -1;
581 int i_backup_rate_format = -1;
583 ao->i_stream_id = p_streams[i];
584 ao->i_stream_index = i;
586 if (ao->b_revert == 0)
588 /* Retrieve the original format of this stream first if not done so already. */
589 i_param_size = sizeof(ao->sfmt_revert);
590 err = AudioStreamGetProperty(ao->i_stream_id, 0,
591 kAudioStreamPropertyPhysicalFormat,
592 &i_param_size,
593 &ao->sfmt_revert);
594 if (err != noErr)
596 ao_msg(MSGT_AO, MSGL_WARN, "could not retrieve the original streamformat: [%4.4s]\n", (char *)&err);
597 if (p_format_list) free(p_format_list);
598 continue;
600 ao->b_revert = 1;
603 for (j = 0; j < i_formats; ++j)
604 if (p_format_list[j].mFormatID == 'IAC3' ||
605 p_format_list[j].mFormatID == kAudioFormat60958AC3)
607 if (p_format_list[j].mSampleRate == ao->stream_format.mSampleRate)
609 i_requested_rate_format = j;
610 break;
612 if (p_format_list[j].mSampleRate == ao->sfmt_revert.mSampleRate)
613 i_current_rate_format = j;
614 else if (i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate)
615 i_backup_rate_format = j;
618 if (i_requested_rate_format >= 0) /* We prefer to output at the samplerate of the original audio. */
619 ao->stream_format = p_format_list[i_requested_rate_format];
620 else if (i_current_rate_format >= 0) /* If not possible, we will try to use the current samplerate of the device. */
621 ao->stream_format = p_format_list[i_current_rate_format];
622 else ao->stream_format = p_format_list[i_backup_rate_format]; /* And if we have to, any digital format will be just fine (highest rate possible). */
624 if (p_format_list) free(p_format_list);
626 if (p_streams) free(p_streams);
628 if (ao->i_stream_index < 0)
630 ao_msg(MSGT_AO, MSGL_WARN, "can not find any digital output stream format when OpenSPDIF().\n");
631 goto err_out;
634 print_format(MSGL_V, "original stream format:", &ao->sfmt_revert);
636 if (!AudioStreamChangeFormat(ao->i_stream_id, ao->stream_format))
637 goto err_out;
639 err = AudioDeviceAddPropertyListener(ao->i_selected_dev,
640 kAudioPropertyWildcardChannel,
642 kAudioDevicePropertyDeviceHasChanged,
643 DeviceListener,
644 NULL);
645 if (err != noErr)
646 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceAddPropertyListener for kAudioDevicePropertyDeviceHasChanged failed: [%4.4s]\n", (char *)&err);
649 /* FIXME: If output stream is not native byte-order, we need change endian somewhere. */
650 /* Although there's no such case reported. */
651 #if HAVE_BIGENDIAN
652 if (!(ao->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian))
653 #else
654 if (ao->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian)
655 #endif
656 ao_msg(MSGT_AO, MSGL_WARN, "output stream has a no-native byte-order, digital output may failed.\n");
658 /* For ac3/dts, just use packet size 6144 bytes as chunk size. */
659 ao->chunk_size = ao->stream_format.mBytesPerPacket;
661 ao_data.samplerate = ao->stream_format.mSampleRate;
662 ao_data.channels = ao->stream_format.mChannelsPerFrame;
663 ao_data.bps = ao_data.samplerate * (ao->stream_format.mBytesPerPacket/ao->stream_format.mFramesPerPacket);
664 ao_data.outburst = ao->chunk_size;
665 ao_data.buffersize = ao_data.bps;
667 ao->num_chunks = (ao_data.bps+ao->chunk_size-1)/ao->chunk_size;
668 ao->buffer_len = ao->num_chunks * ao->chunk_size;
669 ao->buffer = av_fifo_alloc(ao->buffer_len);
671 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);
674 /* Add IOProc callback. */
675 err = AudioDeviceAddIOProc(ao->i_selected_dev,
676 (AudioDeviceIOProc)RenderCallbackSPDIF,
677 (void *)ao);
678 if (err != noErr)
680 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceAddIOProc failed: [%4.4s]\n", (char *)&err);
681 goto err_out1;
684 reset();
686 return CONTROL_TRUE;
688 err_out1:
689 if (ao->b_revert)
690 AudioStreamChangeFormat(ao->i_stream_id, ao->sfmt_revert);
691 err_out:
692 if (ao->b_changed_mixing && ao->sfmt_revert.mFormatID != kAudioFormat60958AC3)
694 int b_mix = 1;
695 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE,
696 kAudioDevicePropertySupportsMixing,
697 i_param_size, &b_mix);
698 if (err != noErr)
699 ao_msg(MSGT_AO, MSGL_WARN, "failed to set mixmode: [%4.4s]\n",
700 (char *)&err);
702 if (ao->i_hog_pid == getpid())
704 ao->i_hog_pid = -1;
705 i_param_size = sizeof(ao->i_hog_pid);
706 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE,
707 kAudioDevicePropertyHogMode,
708 i_param_size, &ao->i_hog_pid);
709 if (err != noErr)
710 ao_msg(MSGT_AO, MSGL_WARN, "Could not release hogmode: [%4.4s]\n",
711 (char *)&err);
713 av_fifo_free(ao->buffer);
714 free(ao);
715 ao = NULL;
716 return CONTROL_FALSE;
719 /*****************************************************************************
720 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
721 *****************************************************************************/
722 static int AudioDeviceSupportsDigital( AudioDeviceID i_dev_id )
724 OSStatus err = noErr;
725 UInt32 i_param_size = 0;
726 AudioStreamID *p_streams = NULL;
727 int i = 0, i_streams = 0;
728 int b_return = CONTROL_FALSE;
730 /* Retrieve all the output streams. */
731 err = AudioDeviceGetPropertyInfo(i_dev_id, 0, FALSE,
732 kAudioDevicePropertyStreams,
733 &i_param_size, NULL);
734 if (err != noErr)
736 ao_msg(MSGT_AO,MSGL_V, "could not get number of streams: [%4.4s]\n", (char *)&err);
737 return CONTROL_FALSE;
740 i_streams = i_param_size / sizeof(AudioStreamID);
741 p_streams = (AudioStreamID *)malloc(i_param_size);
742 if (p_streams == NULL)
744 ao_msg(MSGT_AO,MSGL_V, "out of memory\n");
745 return CONTROL_FALSE;
748 err = AudioDeviceGetProperty(i_dev_id, 0, FALSE,
749 kAudioDevicePropertyStreams,
750 &i_param_size, p_streams);
752 if (err != noErr)
754 ao_msg(MSGT_AO,MSGL_V, "could not get number of streams: [%4.4s]\n", (char *)&err);
755 free(p_streams);
756 return CONTROL_FALSE;
759 for (i = 0; i < i_streams; ++i)
761 if (AudioStreamSupportsDigital(p_streams[i]))
762 b_return = CONTROL_OK;
765 free(p_streams);
766 return b_return;
769 /*****************************************************************************
770 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
771 *****************************************************************************/
772 static int AudioStreamSupportsDigital( AudioStreamID i_stream_id )
774 OSStatus err = noErr;
775 UInt32 i_param_size;
776 AudioStreamBasicDescription *p_format_list = NULL;
777 int i, i_formats, b_return = CONTROL_FALSE;
779 /* Retrieve all the stream formats supported by each output stream. */
780 err = AudioStreamGetPropertyInfo(i_stream_id, 0,
781 kAudioStreamPropertyPhysicalFormats,
782 &i_param_size, NULL);
783 if (err != noErr)
785 ao_msg(MSGT_AO,MSGL_V, "could not get number of streamformats: [%4.4s]\n", (char *)&err);
786 return CONTROL_FALSE;
789 i_formats = i_param_size / sizeof(AudioStreamBasicDescription);
790 p_format_list = (AudioStreamBasicDescription *)malloc(i_param_size);
791 if (p_format_list == NULL)
793 ao_msg(MSGT_AO,MSGL_V, "could not malloc the memory\n" );
794 return CONTROL_FALSE;
797 err = AudioStreamGetProperty(i_stream_id, 0,
798 kAudioStreamPropertyPhysicalFormats,
799 &i_param_size, p_format_list);
800 if (err != noErr)
802 ao_msg(MSGT_AO,MSGL_V, "could not get the list of streamformats: [%4.4s]\n", (char *)&err);
803 free(p_format_list);
804 return CONTROL_FALSE;
807 for (i = 0; i < i_formats; ++i)
809 print_format(MSGL_V, "supported format:", &p_format_list[i]);
811 if (p_format_list[i].mFormatID == 'IAC3' ||
812 p_format_list[i].mFormatID == kAudioFormat60958AC3)
813 b_return = CONTROL_OK;
816 free(p_format_list);
817 return b_return;
820 /*****************************************************************************
821 * AudioStreamChangeFormat: Change i_stream_id to change_format
822 *****************************************************************************/
823 static int AudioStreamChangeFormat( AudioStreamID i_stream_id, AudioStreamBasicDescription change_format )
825 OSStatus err = noErr;
826 UInt32 i_param_size = 0;
827 int i;
829 static volatile int stream_format_changed;
830 stream_format_changed = 0;
832 print_format(MSGL_V, "setting stream format:", &change_format);
834 /* Install the callback. */
835 err = AudioStreamAddPropertyListener(i_stream_id, 0,
836 kAudioStreamPropertyPhysicalFormat,
837 StreamListener,
838 (void *)&stream_format_changed);
839 if (err != noErr)
841 ao_msg(MSGT_AO, MSGL_WARN, "AudioStreamAddPropertyListener failed: [%4.4s]\n", (char *)&err);
842 return CONTROL_FALSE;
845 /* Change the format. */
846 err = AudioStreamSetProperty(i_stream_id, 0, 0,
847 kAudioStreamPropertyPhysicalFormat,
848 sizeof(AudioStreamBasicDescription),
849 &change_format);
850 if (err != noErr)
852 ao_msg(MSGT_AO, MSGL_WARN, "could not set the stream format: [%4.4s]\n", (char *)&err);
853 return CONTROL_FALSE;
856 /* The AudioStreamSetProperty is not only asynchronious,
857 * it is also not Atomic, in its behaviour.
858 * Therefore we check 5 times before we really give up.
859 * FIXME: failing isn't actually implemented yet. */
860 for (i = 0; i < 5; ++i)
862 AudioStreamBasicDescription actual_format;
863 int j;
864 for (j = 0; !stream_format_changed && j < 50; ++j)
865 usec_sleep(10000);
866 if (stream_format_changed)
867 stream_format_changed = 0;
868 else
869 ao_msg(MSGT_AO, MSGL_V, "reached timeout\n" );
871 i_param_size = sizeof(AudioStreamBasicDescription);
872 err = AudioStreamGetProperty(i_stream_id, 0,
873 kAudioStreamPropertyPhysicalFormat,
874 &i_param_size,
875 &actual_format);
877 print_format(MSGL_V, "actual format in use:", &actual_format);
878 if (actual_format.mSampleRate == change_format.mSampleRate &&
879 actual_format.mFormatID == change_format.mFormatID &&
880 actual_format.mFramesPerPacket == change_format.mFramesPerPacket)
882 /* The right format is now active. */
883 break;
885 /* We need to check again. */
888 /* Removing the property listener. */
889 err = AudioStreamRemovePropertyListener(i_stream_id, 0,
890 kAudioStreamPropertyPhysicalFormat,
891 StreamListener);
892 if (err != noErr)
894 ao_msg(MSGT_AO, MSGL_WARN, "AudioStreamRemovePropertyListener failed: [%4.4s]\n", (char *)&err);
895 return CONTROL_FALSE;
898 return CONTROL_TRUE;
901 /*****************************************************************************
902 * RenderCallbackSPDIF: callback for SPDIF audio output
903 *****************************************************************************/
904 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice,
905 const AudioTimeStamp * inNow,
906 const void * inInputData,
907 const AudioTimeStamp * inInputTime,
908 AudioBufferList * outOutputData,
909 const AudioTimeStamp * inOutputTime,
910 void * threadGlobals )
912 int amt = av_fifo_size(ao->buffer);
913 int req = outOutputData->mBuffers[ao->i_stream_index].mDataByteSize;
915 if (amt > req)
916 amt = req;
917 if (amt)
918 read_buffer(ao->b_muted ? NULL : (unsigned char *)outOutputData->mBuffers[ao->i_stream_index].mData, amt);
920 return noErr;
924 static int play(void* output_samples,int num_bytes,int flags)
926 int wrote, b_digital;
928 // Check whether we need to reset the digital output stream.
929 if (ao->b_digital && ao->b_stream_format_changed)
931 ao->b_stream_format_changed = 0;
932 b_digital = AudioStreamSupportsDigital(ao->i_stream_id);
933 if (b_digital)
935 /* Current stream support digital format output, let's set it. */
936 ao_msg(MSGT_AO, MSGL_V, "detected current stream support digital, try to restore digital output...\n");
938 if (!AudioStreamChangeFormat(ao->i_stream_id, ao->stream_format))
940 ao_msg(MSGT_AO, MSGL_WARN, "restore digital output failed.\n");
942 else
944 ao_msg(MSGT_AO, MSGL_WARN, "restore digital output succeed.\n");
945 reset();
948 else
949 ao_msg(MSGT_AO, MSGL_V, "detected current stream do not support digital.\n");
952 wrote=write_buffer(output_samples, num_bytes);
953 audio_resume();
954 return wrote;
957 /* set variables and buffer to initial state */
958 static void reset(void)
960 audio_pause();
961 av_fifo_reset(ao->buffer);
965 /* return available space */
966 static int get_space(void)
968 return ao->buffer_len - av_fifo_size(ao->buffer);
972 /* return delay until audio is played */
973 static float get_delay(void)
975 // inaccurate, should also contain the data buffered e.g. by the OS
976 return (float)av_fifo_size(ao->buffer)/(float)ao_data.bps;
980 /* unload plugin and deregister from coreaudio */
981 static void uninit(int immed)
983 OSStatus err = noErr;
984 UInt32 i_param_size = 0;
986 if (!immed) {
987 long long timeleft=(1000000LL*av_fifo_size(ao->buffer))/ao_data.bps;
988 ao_msg(MSGT_AO,MSGL_DBG2, "%d bytes left @%d bps (%d usec)\n", av_fifo_size(ao->buffer), ao_data.bps, (int)timeleft);
989 usec_sleep((int)timeleft);
992 if (!ao->b_digital) {
993 AudioOutputUnitStop(ao->theOutputUnit);
994 AudioUnitUninitialize(ao->theOutputUnit);
995 CloseComponent(ao->theOutputUnit);
997 else {
998 /* Stop device. */
999 err = AudioDeviceStop(ao->i_selected_dev,
1000 (AudioDeviceIOProc)RenderCallbackSPDIF);
1001 if (err != noErr)
1002 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStop failed: [%4.4s]\n", (char *)&err);
1004 /* Remove IOProc callback. */
1005 err = AudioDeviceRemoveIOProc(ao->i_selected_dev,
1006 (AudioDeviceIOProc)RenderCallbackSPDIF);
1007 if (err != noErr)
1008 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceRemoveIOProc failed: [%4.4s]\n", (char *)&err);
1010 if (ao->b_revert)
1011 AudioStreamChangeFormat(ao->i_stream_id, ao->sfmt_revert);
1013 if (ao->b_changed_mixing && ao->sfmt_revert.mFormatID != kAudioFormat60958AC3)
1015 int b_mix;
1016 Boolean b_writeable;
1017 /* Revert mixable to true if we are allowed to. */
1018 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
1019 &i_param_size, &b_writeable);
1020 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
1021 &i_param_size, &b_mix);
1022 if (err != noErr && b_writeable)
1024 b_mix = 1;
1025 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE,
1026 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix);
1028 if (err != noErr)
1029 ao_msg(MSGT_AO, MSGL_WARN, "failed to set mixmode: [%4.4s]\n", (char *)&err);
1031 if (ao->i_hog_pid == getpid())
1033 ao->i_hog_pid = -1;
1034 i_param_size = sizeof(ao->i_hog_pid);
1035 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE,
1036 kAudioDevicePropertyHogMode, i_param_size, &ao->i_hog_pid);
1037 if (err != noErr) ao_msg(MSGT_AO, MSGL_WARN, "Could not release hogmode: [%4.4s]\n", (char *)&err);
1041 av_fifo_free(ao->buffer);
1042 free(ao);
1043 ao = NULL;
1047 /* stop playing, keep buffers (for pause) */
1048 static void audio_pause(void)
1050 OSErr err=noErr;
1052 /* Stop callback. */
1053 if (!ao->b_digital)
1055 err=AudioOutputUnitStop(ao->theOutputUnit);
1056 if (err != noErr)
1057 ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStop returned [%4.4s]\n", (char *)&err);
1059 else
1061 err = AudioDeviceStop(ao->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF);
1062 if (err != noErr)
1063 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStop failed: [%4.4s]\n", (char *)&err);
1065 ao->paused = 1;
1069 /* resume playing, after audio_pause() */
1070 static void audio_resume(void)
1072 OSErr err=noErr;
1074 if (!ao->paused)
1075 return;
1077 /* Start callback. */
1078 if (!ao->b_digital)
1080 err = AudioOutputUnitStart(ao->theOutputUnit);
1081 if (err != noErr)
1082 ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStart returned [%4.4s]\n", (char *)&err);
1084 else
1086 err = AudioDeviceStart(ao->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF);
1087 if (err != noErr)
1088 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStart failed: [%4.4s]\n", (char *)&err);
1090 ao->paused = 0;
1093 /*****************************************************************************
1094 * StreamListener
1095 *****************************************************************************/
1096 static OSStatus StreamListener( AudioStreamID inStream,
1097 UInt32 inChannel,
1098 AudioDevicePropertyID inPropertyID,
1099 void * inClientData )
1101 switch (inPropertyID)
1103 case kAudioStreamPropertyPhysicalFormat:
1104 ao_msg(MSGT_AO, MSGL_V, "got notify kAudioStreamPropertyPhysicalFormat changed.\n");
1105 if (inClientData)
1106 *(volatile int *)inClientData = 1;
1107 default:
1108 break;
1110 return noErr;
1113 static OSStatus DeviceListener( AudioDeviceID inDevice,
1114 UInt32 inChannel,
1115 Boolean isInput,
1116 AudioDevicePropertyID inPropertyID,
1117 void* inClientData )
1119 switch (inPropertyID)
1121 case kAudioDevicePropertyDeviceHasChanged:
1122 ao_msg(MSGT_AO, MSGL_WARN, "got notify kAudioDevicePropertyDeviceHasChanged.\n");
1123 ao->b_stream_format_changed = 1;
1124 default:
1125 break;
1127 return noErr;