+ /* 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 = VLC_TRUE;
+ }
+
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
+ return VLC_FALSE;
+ }
+
+ // Find stream_id of selected device with a cac3 stream
+ 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 VLC_FALSE;
+ }
+
+ i_streams = i_param_size / sizeof( AudioStreamID );
+ p_streams = (AudioStreamID *)malloc( i_param_size );
+ if( p_streams == NULL )
+ {
+ msg_Err( p_aout, "Out of memory" );
+ return VLC_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 );
+ if( p_streams ) free( p_streams );
+ return VLC_FALSE;
+ }
+
+ for( i = 0; i < i_streams; i++ )
+ {
+ // Find a stream with a cac3 stream
+ AudioStreamBasicDescription *p_format_list = NULL;
+ int i_formats = 0, j = 0;
+
+ /* 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 )
+ {
+ msg_Err( p_aout, "Could not malloc the memory" );
+ 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 );
+ if( p_format_list) free( p_format_list);
+ continue;
+ }
+
+ for( j = 0; j < i_formats; j++ )
+ {
+ if( p_format_list[j].mFormatID == 'IAC3' ||
+ p_format_list[j].mFormatID == kAudioFormat60958AC3 )
+ {
+ // found a cac3 format
+ p_sys->i_stream_id = p_streams[i];
+ p_sys->i_stream_index = i;
+
+ if( p_sys->b_revert == VLC_FALSE )
+ {
+ 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 = VLC_TRUE;
+ }
+ if( p_format_list[j].mSampleRate == p_sys->sfmt_revert.mSampleRate )
+ {
+ p_sys->stream_format = p_format_list[j];
+ }
+ }
+ }
+ if( p_format_list ) free( p_format_list );
+ }
+
+ if( p_streams ) free( p_streams );
+
+ msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) );
+ msg_Dbg( p_aout, STREAM_FORMAT_MSG( "setting stream format: ", p_sys->stream_format ) );
+
+ /* Install the callback */
+ err = AudioStreamAddPropertyListener( p_sys->i_stream_id, 0,
+ kAudioStreamPropertyPhysicalFormat,
+ StreamListener, (void *)&w );
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "AudioStreamAddPropertyListener failed: [%4.4s]", (char *)&err );
+ return VLC_FALSE;
+ }
+
+ /* Condition because SetProperty is asynchronious */
+ vlc_cond_init( p_aout, &w.cond );
+ vlc_mutex_init( p_aout, &w.lock );
+ vlc_mutex_lock( &w.lock );
+
+ /* change the format */
+ err = AudioStreamSetProperty( p_sys->i_stream_id, 0, 0,
+ kAudioStreamPropertyPhysicalFormat,
+ sizeof( AudioStreamBasicDescription ),
+ &p_sys->stream_format );
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "could not set the stream format: [%4.4s]", (char *)&err );
+ vlc_mutex_unlock( &w.lock );
+ vlc_mutex_destroy( &w.lock );
+ vlc_cond_destroy( &w.cond );
+ return VLC_FALSE;
+ }
+
+ gettimeofday( &now, NULL );
+ timeout.tv_sec = now.tv_sec;
+ timeout.tv_nsec = (now.tv_usec + 900000) * 1000;
+
+ pthread_cond_timedwait( &w.cond.cond, &w.lock.mutex, &timeout );
+ vlc_mutex_unlock( &w.lock );
+
+ err = AudioStreamRemovePropertyListener( p_sys->i_stream_id, 0,
+ kAudioStreamPropertyPhysicalFormat,
+ StreamListener );
+ if( err != noErr )
+ {
+ msg_Err( p_aout, "AudioStreamRemovePropertyListener failed: [%4.4s]", (char *)&err );
+ vlc_mutex_destroy( &w.lock );
+ vlc_cond_destroy( &w.cond );
+ return VLC_FALSE;
+ }
+
+ vlc_mutex_destroy( &w.lock );
+ vlc_cond_destroy( &w.cond );
+
+ i_param_size = sizeof( AudioStreamBasicDescription );
+ err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
+ kAudioStreamPropertyPhysicalFormat,
+ &i_param_size,
+ &p_sys->stream_format );
+
+ msg_Dbg( p_aout, STREAM_FORMAT_MSG( "actual format in use: ", p_sys->stream_format ) );
+
+ /* set the format flags */
+ if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian )
+ p_aout->output.output.i_format = VLC_FOURCC('s','p','d','b');
+ else
+ p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
+ 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_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 VLC_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 VLC_FALSE;
+ }
+
+ return VLC_TRUE;