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>
45 #include <sys/types.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",
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? */
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 */
96 unsigned int buffer_len
; ///< must always be num_chunks * chunk_size
97 unsigned int num_chunks
;
98 unsigned int chunk_size
;
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
);
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
;
131 read_buffer((unsigned char *)ioData
->mBuffers
[0].mData
, amt
);
133 ioData
->mBuffers
[0].mDataByteSize
= amt
;
138 static int control(int cmd
,void *arg
){
139 ao_control_vol_t
*control_vol
;
143 case AOCONTROL_GET_VOLUME
:
144 control_vol
= (ao_control_vol_t
*)arg
;
146 // Digital output has no volume adjust.
147 return CONTROL_FALSE
;
149 err
= AudioUnitGetParameter(ao
->theOutputUnit
, kHALOutputParam_Volume
, kAudioUnitScope_Global
, 0, &vol
);
152 // printf("GET VOL=%f\n", vol);
153 control_vol
->left
=control_vol
->right
=vol
*100.0/4.0;
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
;
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)
179 vol
=(control_vol
->left
+control_vol
->right
)*4.0/200.0;
180 err
= AudioUnitSetParameter(ao
->theOutputUnit
, kHALOutputParam_Volume
, kAudioUnitScope_Global
, 0, vol
, 0);
182 // printf("SET VOL=%f\n", vol);
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 */
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
,
230 AudioDevicePropertyID inPropertyID
,
231 void * inClientData
);
232 static OSStatus
DeviceListener( AudioDeviceID inDevice
,
235 AudioDevicePropertyID inPropertyID
,
236 void* inClientData
);
238 static int init(int rate
,int channels
,int format
,int flags
)
240 AudioStreamBasicDescription inDesc
;
241 ComponentDescription desc
;
243 AURenderCallbackStruct renderCallback
;
245 UInt32 size
, maxFrames
, i_param_size
;
247 AudioDeviceID devid_def
= 0;
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;
258 ao
->b_stream_format_changed
= 0;
261 ao
->i_stream_index
= -1;
263 ao
->b_changed_mixing
= 0;
265 /* Probe whether device support S/PDIF stream output if input is AC3 stream. */
266 if ((format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_AC3
)
268 /* Find the ID of the default Device. */
269 i_param_size
= sizeof(AudioDeviceID
);
270 err
= AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice
,
271 &i_param_size
, &devid_def
);
274 ao_msg(MSGT_AO
, MSGL_WARN
, "could not get default audio device: [%4.4s]\n", (char *)&err
);
278 /* Retrieve the length of the device name. */
280 err
= AudioDeviceGetPropertyInfo(devid_def
, 0, 0,
281 kAudioDevicePropertyDeviceName
,
282 &i_param_size
, NULL
);
285 ao_msg(MSGT_AO
, MSGL_WARN
, "could not get default audio device name length: [%4.4s]\n", (char *)&err
);
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
);
296 ao_msg(MSGT_AO
, MSGL_WARN
, "could not get default audio device name: [%4.4s]\n", (char *)&err
);
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
);
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 switch(format
&AF_FORMAT_BITS_MASK
){
319 inDesc
.mBitsPerChannel
=8;
321 case AF_FORMAT_16BIT
:
322 inDesc
.mBitsPerChannel
=16;
324 case AF_FORMAT_24BIT
:
325 inDesc
.mBitsPerChannel
=24;
327 case AF_FORMAT_32BIT
:
328 inDesc
.mBitsPerChannel
=32;
331 ao_msg(MSGT_AO
, MSGL_WARN
, "Unsupported format (0x%08x)\n", format
);
335 if((format
&AF_FORMAT_POINT_MASK
)==AF_FORMAT_F
) {
337 inDesc
.mFormatFlags
= kAudioFormatFlagIsFloat
|kAudioFormatFlagIsPacked
;
339 else if((format
&AF_FORMAT_SIGN_MASK
)==AF_FORMAT_SI
) {
341 inDesc
.mFormatFlags
= kAudioFormatFlagIsSignedInteger
|kAudioFormatFlagIsPacked
;
345 inDesc
.mFormatFlags
= kAudioFormatFlagIsPacked
;
347 if ((format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_AC3
) {
348 // Currently ac3 input (comes from hwac3) is always in native byte-order.
350 inDesc
.mFormatFlags
|= kAudioFormatFlagIsBigEndian
;
353 else if ((format
& AF_FORMAT_END_MASK
) == AF_FORMAT_BE
)
354 inDesc
.mFormatFlags
|= kAudioFormatFlagIsBigEndian
;
356 inDesc
.mFramesPerPacket
= 1;
357 ao
->packetSize
= inDesc
.mBytesPerPacket
= inDesc
.mBytesPerFrame
= inDesc
.mFramesPerPacket
*channels
*(inDesc
.mBitsPerChannel
/8);
358 print_format(MSGL_V
, "source:",&inDesc
);
360 if (ao
->b_supports_digital
)
363 i_param_size
= sizeof(b_alive
);
364 err
= AudioDeviceGetProperty(ao
->i_selected_dev
, 0, FALSE
,
365 kAudioDevicePropertyDeviceIsAlive
,
366 &i_param_size
, &b_alive
);
368 ao_msg(MSGT_AO
, MSGL_WARN
, "could not check whether device is alive: [%4.4s]\n", (char *)&err
);
370 ao_msg(MSGT_AO
, MSGL_WARN
, "device is not alive\n" );
371 /* S/PDIF output need device in HogMode. */
372 i_param_size
= sizeof(ao
->i_hog_pid
);
373 err
= AudioDeviceGetProperty(ao
->i_selected_dev
, 0, FALSE
,
374 kAudioDevicePropertyHogMode
,
375 &i_param_size
, &ao
->i_hog_pid
);
379 /* This is not a fatal error. Some drivers simply don't support this property. */
380 ao_msg(MSGT_AO
, MSGL_WARN
, "could not check whether device is hogged: [%4.4s]\n",
385 if (ao
->i_hog_pid
!= -1 && ao
->i_hog_pid
!= getpid())
387 ao_msg(MSGT_AO
, MSGL_WARN
, "Selected audio device is exclusively in use by another program.\n" );
390 ao
->stream_format
= inDesc
;
394 /* original analog output code */
395 desc
.componentType
= kAudioUnitType_Output
;
396 desc
.componentSubType
= kAudioUnitSubType_DefaultOutput
;
397 desc
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
398 desc
.componentFlags
= 0;
399 desc
.componentFlagsMask
= 0;
401 comp
= FindNextComponent(NULL
, &desc
); //Finds an component that meets the desc spec's
403 ao_msg(MSGT_AO
, MSGL_WARN
, "Unable to find Output Unit component\n");
407 err
= OpenAComponent(comp
, &(ao
->theOutputUnit
)); //gains access to the services provided by the component
409 ao_msg(MSGT_AO
, MSGL_WARN
, "Unable to open Output Unit component: [%4.4s]\n", (char *)&err
);
413 // Initialize AudioUnit
414 err
= AudioUnitInitialize(ao
->theOutputUnit
);
416 ao_msg(MSGT_AO
, MSGL_WARN
, "Unable to initialize Output Unit component: [%4.4s]\n", (char *)&err
);
420 size
= sizeof(AudioStreamBasicDescription
);
421 err
= AudioUnitSetProperty(ao
->theOutputUnit
, kAudioUnitProperty_StreamFormat
, kAudioUnitScope_Input
, 0, &inDesc
, size
);
424 ao_msg(MSGT_AO
, MSGL_WARN
, "Unable to set the input format: [%4.4s]\n", (char *)&err
);
428 size
= sizeof(UInt32
);
429 err
= AudioUnitGetProperty(ao
->theOutputUnit
, kAudioDevicePropertyBufferSize
, kAudioUnitScope_Input
, 0, &maxFrames
, &size
);
433 ao_msg(MSGT_AO
,MSGL_WARN
, "AudioUnitGetProperty returned [%4.4s] when getting kAudioDevicePropertyBufferSize\n", (char *)&err
);
437 ao
->chunk_size
= maxFrames
;//*inDesc.mBytesPerFrame;
439 ao_data
.samplerate
= inDesc
.mSampleRate
;
440 ao_data
.channels
= inDesc
.mChannelsPerFrame
;
441 ao_data
.bps
= ao_data
.samplerate
* inDesc
.mBytesPerFrame
;
442 ao_data
.outburst
= ao
->chunk_size
;
443 ao_data
.buffersize
= ao_data
.bps
;
445 ao
->num_chunks
= (ao_data
.bps
+ao
->chunk_size
-1)/ao
->chunk_size
;
446 ao
->buffer_len
= ao
->num_chunks
* ao
->chunk_size
;
447 ao
->buffer
= av_fifo_alloc(ao
->buffer_len
);
449 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
);
451 renderCallback
.inputProc
= theRenderProc
;
452 renderCallback
.inputProcRefCon
= 0;
453 err
= AudioUnitSetProperty(ao
->theOutputUnit
, kAudioUnitProperty_SetRenderCallback
, kAudioUnitScope_Input
, 0, &renderCallback
, sizeof(AURenderCallbackStruct
));
455 ao_msg(MSGT_AO
, MSGL_WARN
, "Unable to set the render callback: [%4.4s]\n", (char *)&err
);
464 AudioUnitUninitialize(ao
->theOutputUnit
);
466 CloseComponent(ao
->theOutputUnit
);
468 av_fifo_free(ao
->buffer
);
471 return CONTROL_FALSE
;
474 /*****************************************************************************
475 * Setup a encoded digital stream (SPDIF)
476 *****************************************************************************/
477 static int OpenSPDIF(void)
479 OSStatus err
= noErr
;
480 UInt32 i_param_size
, b_mix
= 0;
481 Boolean b_writeable
= 0;
482 AudioStreamID
*p_streams
= NULL
;
483 int i
, i_streams
= 0;
485 /* Start doing the SPDIF setup process. */
488 /* Hog the device. */
489 i_param_size
= sizeof(ao
->i_hog_pid
);
490 ao
->i_hog_pid
= getpid() ;
492 err
= AudioDeviceSetProperty(ao
->i_selected_dev
, 0, 0, FALSE
,
493 kAudioDevicePropertyHogMode
, i_param_size
, &ao
->i_hog_pid
);
497 ao_msg(MSGT_AO
, MSGL_WARN
, "failed to set hogmode: [%4.4s]\n", (char *)&err
);
502 /* Set mixable to false if we are allowed to. */
503 err
= AudioDeviceGetPropertyInfo(ao
->i_selected_dev
, 0, FALSE
,
504 kAudioDevicePropertySupportsMixing
,
505 &i_param_size
, &b_writeable
);
506 err
= AudioDeviceGetProperty(ao
->i_selected_dev
, 0, FALSE
,
507 kAudioDevicePropertySupportsMixing
,
508 &i_param_size
, &b_mix
);
509 if (err
!= noErr
&& b_writeable
)
512 err
= AudioDeviceSetProperty(ao
->i_selected_dev
, 0, 0, FALSE
,
513 kAudioDevicePropertySupportsMixing
,
514 i_param_size
, &b_mix
);
515 ao
->b_changed_mixing
= 1;
519 ao_msg(MSGT_AO
, MSGL_WARN
, "failed to set mixmode: [%4.4s]\n", (char *)&err
);
523 /* Get a list of all the streams on this device. */
524 err
= AudioDeviceGetPropertyInfo(ao
->i_selected_dev
, 0, FALSE
,
525 kAudioDevicePropertyStreams
,
526 &i_param_size
, NULL
);
529 ao_msg(MSGT_AO
, MSGL_WARN
, "could not get number of streams: [%4.4s]\n", (char *)&err
);
533 i_streams
= i_param_size
/ sizeof(AudioStreamID
);
534 p_streams
= (AudioStreamID
*)malloc(i_param_size
);
535 if (p_streams
== NULL
)
537 ao_msg(MSGT_AO
, MSGL_WARN
, "out of memory\n" );
541 err
= AudioDeviceGetProperty(ao
->i_selected_dev
, 0, FALSE
,
542 kAudioDevicePropertyStreams
,
543 &i_param_size
, p_streams
);
546 ao_msg(MSGT_AO
, MSGL_WARN
, "could not get number of streams: [%4.4s]\n", (char *)&err
);
547 if (p_streams
) free(p_streams
);
551 ao_msg(MSGT_AO
, MSGL_V
, "current device stream number: %d\n", i_streams
);
553 for (i
= 0; i
< i_streams
&& ao
->i_stream_index
< 0; ++i
)
555 /* Find a stream with a cac3 stream. */
556 AudioStreamBasicDescription
*p_format_list
= NULL
;
557 int i_formats
= 0, j
= 0, b_digital
= 0;
559 /* Retrieve all the stream formats supported by each output stream. */
560 err
= AudioStreamGetPropertyInfo(p_streams
[i
], 0,
561 kAudioStreamPropertyPhysicalFormats
,
562 &i_param_size
, NULL
);
565 ao_msg(MSGT_AO
, MSGL_WARN
, "could not get number of streamformats: [%4.4s]\n", (char *)&err
);
569 i_formats
= i_param_size
/ sizeof(AudioStreamBasicDescription
);
570 p_format_list
= (AudioStreamBasicDescription
*)malloc(i_param_size
);
571 if (p_format_list
== NULL
)
573 ao_msg(MSGT_AO
, MSGL_WARN
, "could not malloc the memory\n" );
577 err
= AudioStreamGetProperty(p_streams
[i
], 0,
578 kAudioStreamPropertyPhysicalFormats
,
579 &i_param_size
, p_format_list
);
582 ao_msg(MSGT_AO
, MSGL_WARN
, "could not get the list of streamformats: [%4.4s]\n", (char *)&err
);
583 if (p_format_list
) free(p_format_list
);
587 /* Check if one of the supported formats is a digital format. */
588 for (j
= 0; j
< i_formats
; ++j
)
590 if (p_format_list
[j
].mFormatID
== 'IAC3' ||
591 p_format_list
[j
].mFormatID
== kAudioFormat60958AC3
)
600 /* If this stream supports a digital (cac3) format, then set it. */
601 int i_requested_rate_format
= -1;
602 int i_current_rate_format
= -1;
603 int i_backup_rate_format
= -1;
605 ao
->i_stream_id
= p_streams
[i
];
606 ao
->i_stream_index
= i
;
608 if (ao
->b_revert
== 0)
610 /* Retrieve the original format of this stream first if not done so already. */
611 i_param_size
= sizeof(ao
->sfmt_revert
);
612 err
= AudioStreamGetProperty(ao
->i_stream_id
, 0,
613 kAudioStreamPropertyPhysicalFormat
,
618 ao_msg(MSGT_AO
, MSGL_WARN
, "could not retrieve the original streamformat: [%4.4s]\n", (char *)&err
);
619 if (p_format_list
) free(p_format_list
);
625 for (j
= 0; j
< i_formats
; ++j
)
626 if (p_format_list
[j
].mFormatID
== 'IAC3' ||
627 p_format_list
[j
].mFormatID
== kAudioFormat60958AC3
)
629 if (p_format_list
[j
].mSampleRate
== ao
->stream_format
.mSampleRate
)
631 i_requested_rate_format
= j
;
634 if (p_format_list
[j
].mSampleRate
== ao
->sfmt_revert
.mSampleRate
)
635 i_current_rate_format
= j
;
636 else if (i_backup_rate_format
< 0 || p_format_list
[j
].mSampleRate
> p_format_list
[i_backup_rate_format
].mSampleRate
)
637 i_backup_rate_format
= j
;
640 if (i_requested_rate_format
>= 0) /* We prefer to output at the samplerate of the original audio. */
641 ao
->stream_format
= p_format_list
[i_requested_rate_format
];
642 else if (i_current_rate_format
>= 0) /* If not possible, we will try to use the current samplerate of the device. */
643 ao
->stream_format
= p_format_list
[i_current_rate_format
];
644 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). */
646 if (p_format_list
) free(p_format_list
);
648 if (p_streams
) free(p_streams
);
650 if (ao
->i_stream_index
< 0)
652 ao_msg(MSGT_AO
, MSGL_WARN
, "can not find any digital output stream format when OpenSPDIF().\n");
656 print_format(MSGL_V
, "original stream format:", &ao
->sfmt_revert
);
658 if (!AudioStreamChangeFormat(ao
->i_stream_id
, ao
->stream_format
))
661 err
= AudioDeviceAddPropertyListener(ao
->i_selected_dev
,
662 kAudioPropertyWildcardChannel
,
664 kAudioDevicePropertyDeviceHasChanged
,
668 ao_msg(MSGT_AO
, MSGL_WARN
, "AudioDeviceAddPropertyListener for kAudioDevicePropertyDeviceHasChanged failed: [%4.4s]\n", (char *)&err
);
671 /* FIXME: If output stream is not native byte-order, we need change endian somewhere. */
672 /* Although there's no such case reported. */
674 if (!(ao
->stream_format
.mFormatFlags
& kAudioFormatFlagIsBigEndian
))
676 if (ao
->stream_format
.mFormatFlags
& kAudioFormatFlagIsBigEndian
)
678 ao_msg(MSGT_AO
, MSGL_WARN
, "output stream has a no-native byte-order, digital output may failed.\n");
680 /* For ac3/dts, just use packet size 6144 bytes as chunk size. */
681 ao
->chunk_size
= ao
->stream_format
.mBytesPerPacket
;
683 ao_data
.samplerate
= ao
->stream_format
.mSampleRate
;
684 ao_data
.channels
= ao
->stream_format
.mChannelsPerFrame
;
685 ao_data
.bps
= ao_data
.samplerate
* (ao
->stream_format
.mBytesPerPacket
/ao
->stream_format
.mFramesPerPacket
);
686 ao_data
.outburst
= ao
->chunk_size
;
687 ao_data
.buffersize
= ao_data
.bps
;
689 ao
->num_chunks
= (ao_data
.bps
+ao
->chunk_size
-1)/ao
->chunk_size
;
690 ao
->buffer_len
= ao
->num_chunks
* ao
->chunk_size
;
691 ao
->buffer
= av_fifo_alloc(ao
->buffer_len
);
693 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
);
696 /* Add IOProc callback. */
697 err
= AudioDeviceAddIOProc(ao
->i_selected_dev
,
698 (AudioDeviceIOProc
)RenderCallbackSPDIF
,
702 ao_msg(MSGT_AO
, MSGL_WARN
, "AudioDeviceAddIOProc failed: [%4.4s]\n", (char *)&err
);
712 AudioStreamChangeFormat(ao
->i_stream_id
, ao
->sfmt_revert
);
714 if (ao
->b_changed_mixing
&& ao
->sfmt_revert
.mFormatID
!= kAudioFormat60958AC3
)
717 err
= AudioDeviceSetProperty(ao
->i_selected_dev
, 0, 0, FALSE
,
718 kAudioDevicePropertySupportsMixing
,
719 i_param_size
, &b_mix
);
721 ao_msg(MSGT_AO
, MSGL_WARN
, "failed to set mixmode: [%4.4s]\n",
724 if (ao
->i_hog_pid
== getpid())
727 i_param_size
= sizeof(ao
->i_hog_pid
);
728 err
= AudioDeviceSetProperty(ao
->i_selected_dev
, 0, 0, FALSE
,
729 kAudioDevicePropertyHogMode
,
730 i_param_size
, &ao
->i_hog_pid
);
732 ao_msg(MSGT_AO
, MSGL_WARN
, "Could not release hogmode: [%4.4s]\n",
735 av_fifo_free(ao
->buffer
);
738 return CONTROL_FALSE
;
741 /*****************************************************************************
742 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
743 *****************************************************************************/
744 static int AudioDeviceSupportsDigital( AudioDeviceID i_dev_id
)
746 OSStatus err
= noErr
;
747 UInt32 i_param_size
= 0;
748 AudioStreamID
*p_streams
= NULL
;
749 int i
= 0, i_streams
= 0;
750 int b_return
= CONTROL_FALSE
;
752 /* Retrieve all the output streams. */
753 err
= AudioDeviceGetPropertyInfo(i_dev_id
, 0, FALSE
,
754 kAudioDevicePropertyStreams
,
755 &i_param_size
, NULL
);
758 ao_msg(MSGT_AO
,MSGL_V
, "could not get number of streams: [%4.4s]\n", (char *)&err
);
759 return CONTROL_FALSE
;
762 i_streams
= i_param_size
/ sizeof(AudioStreamID
);
763 p_streams
= (AudioStreamID
*)malloc(i_param_size
);
764 if (p_streams
== NULL
)
766 ao_msg(MSGT_AO
,MSGL_V
, "out of memory\n");
767 return CONTROL_FALSE
;
770 err
= AudioDeviceGetProperty(i_dev_id
, 0, FALSE
,
771 kAudioDevicePropertyStreams
,
772 &i_param_size
, p_streams
);
776 ao_msg(MSGT_AO
,MSGL_V
, "could not get number of streams: [%4.4s]\n", (char *)&err
);
778 return CONTROL_FALSE
;
781 for (i
= 0; i
< i_streams
; ++i
)
783 if (AudioStreamSupportsDigital(p_streams
[i
]))
784 b_return
= CONTROL_OK
;
791 /*****************************************************************************
792 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
793 *****************************************************************************/
794 static int AudioStreamSupportsDigital( AudioStreamID i_stream_id
)
796 OSStatus err
= noErr
;
798 AudioStreamBasicDescription
*p_format_list
= NULL
;
799 int i
, i_formats
, b_return
= CONTROL_FALSE
;
801 /* Retrieve all the stream formats supported by each output stream. */
802 err
= AudioStreamGetPropertyInfo(i_stream_id
, 0,
803 kAudioStreamPropertyPhysicalFormats
,
804 &i_param_size
, NULL
);
807 ao_msg(MSGT_AO
,MSGL_V
, "could not get number of streamformats: [%4.4s]\n", (char *)&err
);
808 return CONTROL_FALSE
;
811 i_formats
= i_param_size
/ sizeof(AudioStreamBasicDescription
);
812 p_format_list
= (AudioStreamBasicDescription
*)malloc(i_param_size
);
813 if (p_format_list
== NULL
)
815 ao_msg(MSGT_AO
,MSGL_V
, "could not malloc the memory\n" );
816 return CONTROL_FALSE
;
819 err
= AudioStreamGetProperty(i_stream_id
, 0,
820 kAudioStreamPropertyPhysicalFormats
,
821 &i_param_size
, p_format_list
);
824 ao_msg(MSGT_AO
,MSGL_V
, "could not get the list of streamformats: [%4.4s]\n", (char *)&err
);
826 return CONTROL_FALSE
;
829 for (i
= 0; i
< i_formats
; ++i
)
831 print_format(MSGL_V
, "supported format:", &p_format_list
[i
]);
833 if (p_format_list
[i
].mFormatID
== 'IAC3' ||
834 p_format_list
[i
].mFormatID
== kAudioFormat60958AC3
)
835 b_return
= CONTROL_OK
;
842 /*****************************************************************************
843 * AudioStreamChangeFormat: Change i_stream_id to change_format
844 *****************************************************************************/
845 static int AudioStreamChangeFormat( AudioStreamID i_stream_id
, AudioStreamBasicDescription change_format
)
847 OSStatus err
= noErr
;
848 UInt32 i_param_size
= 0;
851 static volatile int stream_format_changed
;
852 stream_format_changed
= 0;
854 print_format(MSGL_V
, "setting stream format:", &change_format
);
856 /* Install the callback. */
857 err
= AudioStreamAddPropertyListener(i_stream_id
, 0,
858 kAudioStreamPropertyPhysicalFormat
,
860 (void *)&stream_format_changed
);
863 ao_msg(MSGT_AO
, MSGL_WARN
, "AudioStreamAddPropertyListener failed: [%4.4s]\n", (char *)&err
);
864 return CONTROL_FALSE
;
867 /* Change the format. */
868 err
= AudioStreamSetProperty(i_stream_id
, 0, 0,
869 kAudioStreamPropertyPhysicalFormat
,
870 sizeof(AudioStreamBasicDescription
),
874 ao_msg(MSGT_AO
, MSGL_WARN
, "could not set the stream format: [%4.4s]\n", (char *)&err
);
875 return CONTROL_FALSE
;
878 /* The AudioStreamSetProperty is not only asynchronious,
879 * it is also not Atomic, in its behaviour.
880 * Therefore we check 5 times before we really give up.
881 * FIXME: failing isn't actually implemented yet. */
882 for (i
= 0; i
< 5; ++i
)
884 AudioStreamBasicDescription actual_format
;
886 for (j
= 0; !stream_format_changed
&& j
< 50; ++j
)
888 if (stream_format_changed
)
889 stream_format_changed
= 0;
891 ao_msg(MSGT_AO
, MSGL_V
, "reached timeout\n" );
893 i_param_size
= sizeof(AudioStreamBasicDescription
);
894 err
= AudioStreamGetProperty(i_stream_id
, 0,
895 kAudioStreamPropertyPhysicalFormat
,
899 print_format(MSGL_V
, "actual format in use:", &actual_format
);
900 if (actual_format
.mSampleRate
== change_format
.mSampleRate
&&
901 actual_format
.mFormatID
== change_format
.mFormatID
&&
902 actual_format
.mFramesPerPacket
== change_format
.mFramesPerPacket
)
904 /* The right format is now active. */
907 /* We need to check again. */
910 /* Removing the property listener. */
911 err
= AudioStreamRemovePropertyListener(i_stream_id
, 0,
912 kAudioStreamPropertyPhysicalFormat
,
916 ao_msg(MSGT_AO
, MSGL_WARN
, "AudioStreamRemovePropertyListener failed: [%4.4s]\n", (char *)&err
);
917 return CONTROL_FALSE
;
923 /*****************************************************************************
924 * RenderCallbackSPDIF: callback for SPDIF audio output
925 *****************************************************************************/
926 static OSStatus
RenderCallbackSPDIF( AudioDeviceID inDevice
,
927 const AudioTimeStamp
* inNow
,
928 const void * inInputData
,
929 const AudioTimeStamp
* inInputTime
,
930 AudioBufferList
* outOutputData
,
931 const AudioTimeStamp
* inOutputTime
,
932 void * threadGlobals
)
934 int amt
= av_fifo_size(ao
->buffer
);
935 int req
= outOutputData
->mBuffers
[ao
->i_stream_index
].mDataByteSize
;
940 read_buffer(ao
->b_muted
? NULL
: (unsigned char *)outOutputData
->mBuffers
[ao
->i_stream_index
].mData
, amt
);
946 static int play(void* output_samples
,int num_bytes
,int flags
)
948 int wrote
, b_digital
;
950 // Check whether we need to reset the digital output stream.
951 if (ao
->b_digital
&& ao
->b_stream_format_changed
)
953 ao
->b_stream_format_changed
= 0;
954 b_digital
= AudioStreamSupportsDigital(ao
->i_stream_id
);
957 /* Current stream support digital format output, let's set it. */
958 ao_msg(MSGT_AO
, MSGL_V
, "detected current stream support digital, try to restore digital output...\n");
960 if (!AudioStreamChangeFormat(ao
->i_stream_id
, ao
->stream_format
))
962 ao_msg(MSGT_AO
, MSGL_WARN
, "restore digital output failed.\n");
966 ao_msg(MSGT_AO
, MSGL_WARN
, "restore digital output succeed.\n");
971 ao_msg(MSGT_AO
, MSGL_V
, "detected current stream do not support digital.\n");
974 wrote
=write_buffer(output_samples
, num_bytes
);
979 /* set variables and buffer to initial state */
980 static void reset(void)
983 av_fifo_reset(ao
->buffer
);
987 /* return available space */
988 static int get_space(void)
990 return ao
->buffer_len
- av_fifo_size(ao
->buffer
);
994 /* return delay until audio is played */
995 static float get_delay(void)
997 // inaccurate, should also contain the data buffered e.g. by the OS
998 return (float)av_fifo_size(ao
->buffer
)/(float)ao_data
.bps
;
1002 /* unload plugin and deregister from coreaudio */
1003 static void uninit(int immed
)
1005 OSStatus err
= noErr
;
1006 UInt32 i_param_size
= 0;
1009 long long timeleft
=(1000000LL*av_fifo_size(ao
->buffer
))/ao_data
.bps
;
1010 ao_msg(MSGT_AO
,MSGL_DBG2
, "%d bytes left @%d bps (%d usec)\n", av_fifo_size(ao
->buffer
), ao_data
.bps
, (int)timeleft
);
1011 usec_sleep((int)timeleft
);
1014 if (!ao
->b_digital
) {
1015 AudioOutputUnitStop(ao
->theOutputUnit
);
1016 AudioUnitUninitialize(ao
->theOutputUnit
);
1017 CloseComponent(ao
->theOutputUnit
);
1021 err
= AudioDeviceStop(ao
->i_selected_dev
,
1022 (AudioDeviceIOProc
)RenderCallbackSPDIF
);
1024 ao_msg(MSGT_AO
, MSGL_WARN
, "AudioDeviceStop failed: [%4.4s]\n", (char *)&err
);
1026 /* Remove IOProc callback. */
1027 err
= AudioDeviceRemoveIOProc(ao
->i_selected_dev
,
1028 (AudioDeviceIOProc
)RenderCallbackSPDIF
);
1030 ao_msg(MSGT_AO
, MSGL_WARN
, "AudioDeviceRemoveIOProc failed: [%4.4s]\n", (char *)&err
);
1033 AudioStreamChangeFormat(ao
->i_stream_id
, ao
->sfmt_revert
);
1035 if (ao
->b_changed_mixing
&& ao
->sfmt_revert
.mFormatID
!= kAudioFormat60958AC3
)
1038 Boolean b_writeable
;
1039 /* Revert mixable to true if we are allowed to. */
1040 err
= AudioDeviceGetPropertyInfo(ao
->i_selected_dev
, 0, FALSE
, kAudioDevicePropertySupportsMixing
,
1041 &i_param_size
, &b_writeable
);
1042 err
= AudioDeviceGetProperty(ao
->i_selected_dev
, 0, FALSE
, kAudioDevicePropertySupportsMixing
,
1043 &i_param_size
, &b_mix
);
1044 if (err
!= noErr
&& b_writeable
)
1047 err
= AudioDeviceSetProperty(ao
->i_selected_dev
, 0, 0, FALSE
,
1048 kAudioDevicePropertySupportsMixing
, i_param_size
, &b_mix
);
1051 ao_msg(MSGT_AO
, MSGL_WARN
, "failed to set mixmode: [%4.4s]\n", (char *)&err
);
1053 if (ao
->i_hog_pid
== getpid())
1056 i_param_size
= sizeof(ao
->i_hog_pid
);
1057 err
= AudioDeviceSetProperty(ao
->i_selected_dev
, 0, 0, FALSE
,
1058 kAudioDevicePropertyHogMode
, i_param_size
, &ao
->i_hog_pid
);
1059 if (err
!= noErr
) ao_msg(MSGT_AO
, MSGL_WARN
, "Could not release hogmode: [%4.4s]\n", (char *)&err
);
1063 av_fifo_free(ao
->buffer
);
1069 /* stop playing, keep buffers (for pause) */
1070 static void audio_pause(void)
1074 /* Stop callback. */
1077 err
=AudioOutputUnitStop(ao
->theOutputUnit
);
1079 ao_msg(MSGT_AO
,MSGL_WARN
, "AudioOutputUnitStop returned [%4.4s]\n", (char *)&err
);
1083 err
= AudioDeviceStop(ao
->i_selected_dev
, (AudioDeviceIOProc
)RenderCallbackSPDIF
);
1085 ao_msg(MSGT_AO
, MSGL_WARN
, "AudioDeviceStop failed: [%4.4s]\n", (char *)&err
);
1091 /* resume playing, after audio_pause() */
1092 static void audio_resume(void)
1099 /* Start callback. */
1102 err
= AudioOutputUnitStart(ao
->theOutputUnit
);
1104 ao_msg(MSGT_AO
,MSGL_WARN
, "AudioOutputUnitStart returned [%4.4s]\n", (char *)&err
);
1108 err
= AudioDeviceStart(ao
->i_selected_dev
, (AudioDeviceIOProc
)RenderCallbackSPDIF
);
1110 ao_msg(MSGT_AO
, MSGL_WARN
, "AudioDeviceStart failed: [%4.4s]\n", (char *)&err
);
1115 /*****************************************************************************
1117 *****************************************************************************/
1118 static OSStatus
StreamListener( AudioStreamID inStream
,
1120 AudioDevicePropertyID inPropertyID
,
1121 void * inClientData
)
1123 switch (inPropertyID
)
1125 case kAudioStreamPropertyPhysicalFormat
:
1126 ao_msg(MSGT_AO
, MSGL_V
, "got notify kAudioStreamPropertyPhysicalFormat changed.\n");
1128 *(volatile int *)inClientData
= 1;
1135 static OSStatus
DeviceListener( AudioDeviceID inDevice
,
1138 AudioDevicePropertyID inPropertyID
,
1139 void* inClientData
)
1141 switch (inPropertyID
)
1143 case kAudioDevicePropertyDeviceHasChanged
:
1144 ao_msg(MSGT_AO
, MSGL_WARN
, "got notify kAudioDevicePropertyDeviceHasChanged.\n");
1145 ao
->b_stream_format_changed
= 1;