+ /* Hog the device */
+ i_param_size = sizeof( p_sys->i_hog_pid );
+ p_sys->i_hog_pid = getpid() ;
+
+ err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
+ kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
+
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "failed to set hogmode: [%4.4s]", (char *)&err );
+ return false;
+ }
+
+ /* Set mixable to false if we are allowed to */
+ err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
+ &i_param_size, &b_writeable );
+
+ err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
+ &i_param_size, &b_mix );
+
+ if( !err && b_writeable )
+ {
+ b_mix = 0;
+ err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
+ kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
+ p_sys->b_changed_mixing = true;
+ }
+
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
+ return false;
+ }
+
+ /* Get a list of all the streams on this device */
+ err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE,
+ kAudioDevicePropertyStreams,
+ &i_param_size, NULL );
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
+ return false;
+ }
+
+ i_streams = i_param_size / sizeof( AudioStreamID );
+ p_streams = (AudioStreamID *)malloc( i_param_size );
+ if( p_streams == NULL )
+ return false;
+
+ err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
+ kAudioDevicePropertyStreams,
+ &i_param_size, p_streams );
+
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
+ free( p_streams );
+ return false;
+ }
+
+ for( i = 0; i < i_streams && p_sys->i_stream_index < 0 ; i++ )
+ {
+ /* Find a stream with a cac3 stream */
+ AudioStreamBasicDescription *p_format_list = NULL;
+ int i_formats = 0, j = 0;
+ bool b_digital = false;
+
+ /* Retrieve all the stream formats supported by each output stream */
+ err = AudioStreamGetPropertyInfo( p_streams[i], 0,
+ kAudioStreamPropertyPhysicalFormats,
+ &i_param_size, NULL );
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
+ continue;
+ }
+
+ i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
+ p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
+ if( p_format_list == NULL )
+ continue;
+
+ err = AudioStreamGetProperty( p_streams[i], 0,
+ kAudioStreamPropertyPhysicalFormats,
+ &i_param_size, p_format_list );
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
+ free( p_format_list );
+ continue;
+ }
+
+ /* Check if one of the supported formats is a digital format */
+ for( j = 0; j < i_formats; j++ )
+ {
+ if( p_format_list[j].mFormatID == 'IAC3' ||
+ p_format_list[j].mFormatID == kAudioFormat60958AC3 )
+ {
+ b_digital = true;
+ break;
+ }
+ }
+
+ if( b_digital )
+ {
+ /* if this stream supports a digital (cac3) format, then go set it. */
+ int i_requested_rate_format = -1;
+ int i_current_rate_format = -1;
+ int i_backup_rate_format = -1;
+
+ p_sys->i_stream_id = p_streams[i];
+ p_sys->i_stream_index = i;
+
+ if( p_sys->b_revert == false )
+ {
+ /* Retrieve the original format of this stream first if not done so already */
+ i_param_size = sizeof( p_sys->sfmt_revert );
+ err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
+ kAudioStreamPropertyPhysicalFormat,
+ &i_param_size,
+ &p_sys->sfmt_revert );
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "could not retrieve the original streamformat: [%4.4s]", (char *)&err );
+ continue;
+ }
+ p_sys->b_revert = true;
+ }
+
+ for( j = 0; j < i_formats; j++ )
+ {
+ if( p_format_list[j].mFormatID == 'IAC3' ||
+ p_format_list[j].mFormatID == kAudioFormat60958AC3 )
+ {
+ if( p_format_list[j].mSampleRate == p_aout->output.output.i_rate )
+ {
+ i_requested_rate_format = j;
+ break;
+ }
+ else if( p_format_list[j].mSampleRate == p_sys->sfmt_revert.mSampleRate )
+ {
+ i_current_rate_format = j;
+ }
+ else
+ {
+ if( i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate )
+ i_backup_rate_format = j;
+ }
+ }
+
+ }
+
+ if( i_requested_rate_format >= 0 ) /* We prefer to output at the samplerate of the original audio */
+ p_sys->stream_format = p_format_list[i_requested_rate_format];
+ else if( i_current_rate_format >= 0 ) /* If not possible, we will try to use the current samplerate of the device */
+ p_sys->stream_format = p_format_list[i_current_rate_format];
+ else p_sys->stream_format = p_format_list[i_backup_rate_format]; /* And if we have to, any digital format will be just fine (highest rate possible) */
+ }
+ free( p_format_list );
+ }
+ free( p_streams );
+
+ msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) );
+
+ if( !AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->stream_format ) )
+ return false;
+
+ /* Set the format flags */
+ if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian )
+ p_aout->output.output.i_format = VLC_CODEC_SPDIFB;
+ else
+ p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
+ p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
+ p_aout->output.output.i_frame_length = A52_FRAME_NB;
+ p_aout->output.i_nb_samples = p_aout->output.output.i_frame_length;
+ p_aout->output.output.i_rate = (unsigned int)p_sys->stream_format.mSampleRate;
+ aout_FormatPrepare( &p_aout->output.output );
+ aout_VolumeNoneInit( p_aout );
+
+ /* Add IOProc callback */
+ err = AudioDeviceAddIOProc( p_sys->i_selected_dev,
+ (AudioDeviceIOProc)RenderCallbackSPDIF,
+ (void *)p_aout );
+
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "AudioDeviceAddIOProc failed: [%4.4s]", (char *)&err );
+ return false;
+ }
+
+ /* Check for the difference between the Device clock and mdate */
+ p_sys->clock_diff = - (mtime_t)
+ AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
+ p_sys->clock_diff += mdate();
+
+ /* Start device */
+ err = AudioDeviceStart( p_sys->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF );
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "AudioDeviceStart failed: [%4.4s]", (char *)&err );
+
+ err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
+ (AudioDeviceIOProc)RenderCallbackSPDIF );
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
+ }
+ return false;
+ }
+
+ return true;