1 /*****************************************************************************
2 * auhal.c: AUHAL and Coreaudio output plugin
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc_interface.h>
33 #include <CoreAudio/CoreAudio.h>
34 #include <AudioUnit/AudioUnitProperties.h>
35 #include <AudioUnit/AudioUnitParameters.h>
36 #include <AudioUnit/AudioOutputUnit.h>
37 #include <AudioToolbox/AudioFormat.h>
39 #define STREAM_FORMAT_MSG( pre, sfm ) \
40 pre "[%ld][%4.4s][%ld][%ld][%ld][%ld][%ld][%ld]", \
41 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
42 sfm.mFormatFlags, sfm.mBytesPerPacket, \
43 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
44 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
46 #define STREAM_FORMAT_MSG_FULL( pre, sfm ) \
47 pre ":\nsamplerate: [%ld]\nFormatID: [%4.4s]\nFormatFlags: [%ld]\nBypesPerPacket: [%ld]\nFramesPerPacket: [%ld]\nBytesPerFrame: [%ld]\nChannelsPerFrame: [%ld]\nBitsPerChannel[%ld]", \
48 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
49 sfm.mFormatFlags, sfm.mBytesPerPacket, \
50 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
51 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
53 #define BUFSIZE 0xffffff
54 #define AOUT_VAR_SPDIF_FLAG 0xf00000
58 * - clean up the debug info
60 * - be better at changing stream setup or devices setup changes while playing.
64 /*****************************************************************************
65 * aout_sys_t: private audio output method descriptor
66 *****************************************************************************
67 * This structure is part of the audio output thread descriptor.
68 * It describes the CoreAudio specific properties of an output thread.
69 *****************************************************************************/
72 AudioDeviceID i_default_dev; /* Keeps DeviceID of defaultOutputDevice */
73 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device */
74 UInt32 i_devices; /* Number of CoreAudio Devices */
75 vlc_bool_t b_supports_digital;/* Does the currently selected device support digital mode? */
76 vlc_bool_t b_digital; /* Are we running in digital mode? */
77 mtime_t clock_diff; /* Difference between VLC clock and Device clock */
80 Component au_component; /* The Audiocomponent we use */
81 AudioUnit au_unit; /* The AudioUnit we use */
82 uint8_t p_remainder_buffer[BUFSIZE];
83 uint32_t i_read_bytes;
84 uint32_t i_total_bytes;
86 /* CoreAudio SPDIF mode specific */
87 pid_t i_hog_pid; /* The keep the pid of our hog status */
88 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */
89 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */
90 AudioStreamBasicDescription stream_format; /* The format we changed the stream to */
91 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */
92 vlc_bool_t b_revert; /* Wether we need to revert the stream format */
93 vlc_bool_t b_changed_mixing;/* Wether we need to set the mixing mode back */
96 /*****************************************************************************
98 *****************************************************************************/
99 static int Open ( vlc_object_t * );
100 static int OpenAnalog ( aout_instance_t * );
101 static int OpenSPDIF ( aout_instance_t * );
102 static void Close ( vlc_object_t * );
104 static void Play ( aout_instance_t * );
105 static void Probe ( aout_instance_t * );
107 static int AudioDeviceHasOutput ( AudioDeviceID );
108 static int AudioDeviceSupportsDigital( aout_instance_t *, AudioDeviceID );
109 static int AudioStreamSupportsDigital( aout_instance_t *, AudioStreamID );
110 static int AudioStreamChangeFormat ( aout_instance_t *, AudioStreamID, AudioStreamBasicDescription );
112 static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
113 unsigned int, unsigned int, AudioBufferList *);
114 static OSStatus RenderCallbackSPDIF ( AudioDeviceID, const AudioTimeStamp *, const void *, const AudioTimeStamp *,
115 AudioBufferList *, const AudioTimeStamp *, void * );
116 static OSStatus HardwareListener ( AudioHardwarePropertyID, void *);
117 static OSStatus StreamListener ( AudioStreamID, UInt32,
118 AudioDevicePropertyID, void * );
119 static int AudioDeviceCallback ( vlc_object_t *, const char *,
120 vlc_value_t, vlc_value_t, void * );
123 /*****************************************************************************
125 *****************************************************************************/
126 #define ADEV_TEXT N_("Audio Device")
127 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
128 "audio device, as listed in your 'Audio Device' menu. This device will " \
129 "then be used by default for audio playback.")
132 set_shortname( "auhal" );
133 set_description( _("HAL AudioUnit output") );
134 set_capability( "audio output", 101 );
135 set_category( CAT_AUDIO );
136 set_subcategory( SUBCAT_AUDIO_AOUT );
137 set_callbacks( Open, Close );
138 add_integer( "macosx-audio-device", 0, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE );
141 /*****************************************************************************
142 * Open: open macosx audio output
143 *****************************************************************************/
144 static int Open( vlc_object_t * p_this )
146 OSStatus err = noErr;
147 UInt32 i_param_size = 0;
148 struct aout_sys_t *p_sys = NULL;
149 vlc_bool_t b_alive = VLC_FALSE;
151 aout_instance_t *p_aout = (aout_instance_t *)p_this;
153 /* Allocate structure */
154 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
155 if( p_aout->output.p_sys == NULL )
157 msg_Err( p_aout, "out of memory" );
158 return( VLC_ENOMEM );
161 p_sys = p_aout->output.p_sys;
162 p_sys->i_default_dev = 0;
163 p_sys->i_selected_dev = 0;
164 p_sys->i_devices = 0;
165 p_sys->b_supports_digital = VLC_FALSE;
166 p_sys->b_digital = VLC_FALSE;
167 p_sys->au_component = NULL;
168 p_sys->au_unit = NULL;
169 p_sys->clock_diff = (mtime_t) 0;
170 p_sys->i_read_bytes = 0;
171 p_sys->i_total_bytes = 0;
172 p_sys->i_hog_pid = -1;
173 p_sys->i_stream_id = 0;
174 p_sys->i_stream_index = -1;
175 p_sys->b_revert = VLC_FALSE;
176 p_sys->b_changed_mixing = VLC_FALSE;
177 memset( p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE );
179 p_aout->output.pf_play = Play;
181 aout_FormatPrint( p_aout, "VLC is looking for:", (audio_sample_format_t *)&p_aout->output.output );
183 /* Persistent device variable */
184 if( var_Type( p_aout->p_libvlc, "macosx-audio-device" ) == 0 )
186 var_Create( p_aout->p_libvlc, "macosx-audio-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
189 /* Build a list of devices */
190 if( var_Type( p_aout, "audio-device" ) == 0 )
195 /* What device do we want? */
196 if( var_Get( p_aout, "audio-device", &val ) < 0 )
198 msg_Err( p_aout, "audio-device var does not exist. device probe failed." );
202 p_sys->i_selected_dev = val.i_int & ~AOUT_VAR_SPDIF_FLAG; /* remove SPDIF flag to get the true DeviceID */
203 p_sys->b_supports_digital = ( val.i_int & AOUT_VAR_SPDIF_FLAG ) ? VLC_TRUE : VLC_FALSE;
205 /* Check if the desired device is alive and usable */
206 /* TODO: add a callback to the device to alert us if the device dies */
207 i_param_size = sizeof( b_alive );
208 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
209 kAudioDevicePropertyDeviceIsAlive,
210 &i_param_size, &b_alive );
214 msg_Err( p_aout, "could not check whether device is alive: %4.4s", (char *)&err );
218 if( b_alive == VLC_FALSE )
220 msg_Warn( p_aout, "selected audio device is not alive, switching to default device" );
221 p_sys->i_selected_dev = p_sys->i_default_dev;
224 i_param_size = sizeof( p_sys->i_hog_pid );
225 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
226 kAudioDevicePropertyHogMode,
227 &i_param_size, &p_sys->i_hog_pid );
231 /* This is not a fatal error. Some drivers simply don't support this property */
232 msg_Warn( p_aout, "could not check whether device is hogged: %4.4s",
234 p_sys->i_hog_pid = -1;
237 if( p_sys->i_hog_pid != -1 && p_sys->i_hog_pid != getpid() )
239 msg_Err( p_aout, "Selected audio device is exclusively in use by another program." );
240 intf_UserFatal( p_aout, VLC_FALSE, _("Audio output failed"),
241 _("The selected audio output device is exclusively in "
242 "use by another program.") );
246 /* Check for Digital mode or Analog output mode */
247 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
249 if( OpenSPDIF( p_aout ) )
254 if( OpenAnalog( p_aout ) )
259 /* If we reach this, this aout has failed */
260 var_Destroy( p_aout, "audio-device" );
261 if( p_sys ) free( p_sys );
265 /*****************************************************************************
266 * Open: open and setup a HAL AudioUnit to do analog (multichannel) audio output
267 *****************************************************************************/
268 static int OpenAnalog( aout_instance_t *p_aout )
270 struct aout_sys_t *p_sys = p_aout->output.p_sys;
271 OSStatus err = noErr;
272 UInt32 i_param_size = 0, i = 0;
274 ComponentDescription desc;
275 AudioStreamBasicDescription DeviceFormat;
276 AudioChannelLayout *layout;
277 AudioChannelLayout new_layout;
278 AURenderCallbackStruct input;
280 /* Lets go find our Component */
281 desc.componentType = kAudioUnitType_Output;
282 desc.componentSubType = kAudioUnitSubType_HALOutput;
283 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
284 desc.componentFlags = 0;
285 desc.componentFlagsMask = 0;
287 p_sys->au_component = FindNextComponent( NULL, &desc );
288 if( p_sys->au_component == NULL )
290 msg_Warn( p_aout, "we cannot find our HAL component" );
294 err = OpenAComponent( p_sys->au_component, &p_sys->au_unit );
297 msg_Warn( p_aout, "we cannot open our HAL component" );
301 /* Set the device we will use for this output unit */
302 err = AudioUnitSetProperty( p_sys->au_unit,
303 kAudioOutputUnitProperty_CurrentDevice,
304 kAudioUnitScope_Global,
306 &p_sys->i_selected_dev,
307 sizeof( AudioDeviceID ));
311 msg_Warn( p_aout, "we cannot select the audio device" );
315 /* Get the current format */
316 i_param_size = sizeof(AudioStreamBasicDescription);
318 err = AudioUnitGetProperty( p_sys->au_unit,
319 kAudioUnitProperty_StreamFormat,
320 kAudioUnitScope_Input,
325 if( err != noErr ) return VLC_FALSE;
326 else msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is: ", DeviceFormat ) );
328 /* Get the channel layout of the device side of the unit (vlc -> unit -> device) */
329 err = AudioUnitGetPropertyInfo( p_sys->au_unit,
330 kAudioDevicePropertyPreferredChannelLayout,
331 kAudioUnitScope_Output,
338 layout = (AudioChannelLayout *)malloc( i_param_size);
340 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
341 kAudioDevicePropertyPreferredChannelLayout,
342 kAudioUnitScope_Output,
347 /* We need to "fill out" the ChannelLayout, because there are multiple ways that it can be set */
348 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
350 /* bitmap defined channellayout */
351 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
352 sizeof( UInt32), &layout->mChannelBitmap,
356 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
358 /* layouttags defined channellayout */
359 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
360 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
365 msg_Dbg( p_aout, "layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
367 /* Initialize the VLC core channel count */
368 p_aout->output.output.i_physical_channels = 0;
369 i_original = p_aout->output.output.i_original_channels & AOUT_CHAN_PHYSMASK;
371 if( i_original == AOUT_CHAN_CENTER || layout->mNumberChannelDescriptions < 2 )
373 /* We only need Mono or cannot output more than 1 channel */
374 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
376 else if( i_original == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) || layout->mNumberChannelDescriptions < 3 )
378 /* We only need Stereo or cannot output more than 2 channels */
379 p_aout->output.output.i_physical_channels = AOUT_CHAN_RIGHT | AOUT_CHAN_LEFT;
383 /* We want more than stereo and we can do that */
384 for( i = 0; i < layout->mNumberChannelDescriptions; i++ )
386 msg_Dbg( p_aout, "this is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
388 switch( layout->mChannelDescriptions[i].mChannelLabel )
390 case kAudioChannelLabel_Left:
391 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
393 case kAudioChannelLabel_Right:
394 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
396 case kAudioChannelLabel_Center:
397 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
399 case kAudioChannelLabel_LFEScreen:
400 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE;
402 case kAudioChannelLabel_LeftSurround:
403 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT;
405 case kAudioChannelLabel_RightSurround:
406 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
408 case kAudioChannelLabel_RearSurroundLeft:
409 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
411 case kAudioChannelLabel_RearSurroundRight:
412 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
414 case kAudioChannelLabel_CenterSurround:
415 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER;
418 msg_Warn( p_aout, "unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
421 if( p_aout->output.output.i_physical_channels == 0 )
423 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
424 msg_Err( p_aout, "You should configure your speaker layout with Audio Midi Setup Utility in /Applications/Utilities. Now using Stereo mode." );
425 intf_UserFatal( p_aout, VLC_FALSE, _("Audio device is not configured"),
426 _("You should configure your speaker layout with "
427 "the \"Audio Midi Setup Utility\" in /Applications/"
428 "Utilities. Stereo mode is being used now.") );
431 if( layout ) free( layout );
435 msg_Warn( p_aout, "this driver does not support kAudioDevicePropertyPreferredChannelLayout. BAD DRIVER AUTHOR !!!" );
436 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
439 msg_Dbg( p_aout, "selected %d physical channels for device output", aout_FormatNbChannels( &p_aout->output.output ) );
440 msg_Dbg( p_aout, "VLC will output: %s", aout_FormatPrintChannels( &p_aout->output.output ));
442 memset (&new_layout, 0, sizeof(new_layout));
443 switch( aout_FormatNbChannels( &p_aout->output.output ) )
446 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
449 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
452 if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
454 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
456 else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
458 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
462 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
464 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
466 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
468 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
470 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
472 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
476 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
478 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
480 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
482 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
486 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
488 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
492 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_6_0; // L R Ls Rs C Cs
496 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
497 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
500 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
501 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
505 /* Set up the format to be used */
506 DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
507 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
509 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
510 p_aout->output.output.i_format = VLC_FOURCC( 'f','l','3','2');
511 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
512 DeviceFormat.mBitsPerChannel = 32;
513 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output );
515 /* Calculate framesizes and stuff */
516 DeviceFormat.mFramesPerPacket = 1;
517 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
518 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
520 /* Set the desired format */
521 i_param_size = sizeof(AudioStreamBasicDescription);
522 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
523 kAudioUnitProperty_StreamFormat,
524 kAudioUnitScope_Input,
529 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
531 /* Retrieve actual format */
532 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
533 kAudioUnitProperty_StreamFormat,
534 kAudioUnitScope_Input,
539 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
541 /* Do the last VLC aout setups */
542 aout_FormatPrepare( &p_aout->output.output );
543 p_aout->output.i_nb_samples = 2048;
544 aout_VolumeSoftInit( p_aout );
546 /* set the IOproc callback */
547 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
548 input.inputProcRefCon = p_aout;
550 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
551 kAudioUnitProperty_SetRenderCallback,
552 kAudioUnitScope_Input,
553 0, &input, sizeof( input ) ) );
555 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
556 input.inputProcRefCon = p_aout;
558 /* Set the new_layout as the layout VLC will use to feed the AU unit */
559 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
560 kAudioUnitProperty_AudioChannelLayout,
561 kAudioUnitScope_Input,
562 0, &new_layout, sizeof(new_layout) ) );
564 if( new_layout.mNumberChannelDescriptions > 0 )
565 free( new_layout.mChannelDescriptions );
568 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
570 /* Find the difference between device clock and mdate clock */
571 p_sys->clock_diff = - (mtime_t)
572 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
573 p_sys->clock_diff += mdate();
576 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
581 /*****************************************************************************
582 * Setup a encoded digital stream (SPDIF)
583 *****************************************************************************/
584 static int OpenSPDIF( aout_instance_t * p_aout )
586 struct aout_sys_t *p_sys = p_aout->output.p_sys;
587 OSStatus err = noErr;
588 UInt32 i_param_size = 0, b_mix = 0;
589 Boolean b_writeable = VLC_FALSE;
590 AudioStreamID *p_streams = NULL;
591 int i = 0, i_streams = 0;
593 /* Start doing the SPDIF setup proces */
594 p_sys->b_digital = VLC_TRUE;
597 i_param_size = sizeof( p_sys->i_hog_pid );
598 p_sys->i_hog_pid = getpid() ;
600 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
601 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
605 msg_Err( p_aout, "failed to set hogmode: [%4.4s]", (char *)&err );
609 /* Set mixable to false if we are allowed to */
610 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
611 &i_param_size, &b_writeable );
613 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
614 &i_param_size, &b_mix );
616 if( !err && b_writeable )
619 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
620 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
621 p_sys->b_changed_mixing = VLC_TRUE;
626 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
630 /* Get a list of all the streams on this device */
631 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE,
632 kAudioDevicePropertyStreams,
633 &i_param_size, NULL );
636 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
640 i_streams = i_param_size / sizeof( AudioStreamID );
641 p_streams = (AudioStreamID *)malloc( i_param_size );
642 if( p_streams == NULL )
644 msg_Err( p_aout, "out of memory" );
648 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
649 kAudioDevicePropertyStreams,
650 &i_param_size, p_streams );
654 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
655 if( p_streams ) free( p_streams );
659 for( i = 0; i < i_streams && p_sys->i_stream_index < 0 ; i++ )
661 /* Find a stream with a cac3 stream */
662 AudioStreamBasicDescription *p_format_list = NULL;
663 int i_formats = 0, j = 0;
664 vlc_bool_t b_digital = VLC_FALSE;
666 /* Retrieve all the stream formats supported by each output stream */
667 err = AudioStreamGetPropertyInfo( p_streams[i], 0,
668 kAudioStreamPropertyPhysicalFormats,
669 &i_param_size, NULL );
672 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
676 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
677 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
678 if( p_format_list == NULL )
680 msg_Err( p_aout, "could not malloc the memory" );
684 err = AudioStreamGetProperty( p_streams[i], 0,
685 kAudioStreamPropertyPhysicalFormats,
686 &i_param_size, p_format_list );
689 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
690 if( p_format_list) free( p_format_list);
694 /* Check if one of the supported formats is a digital format */
695 for( j = 0; j < i_formats; j++ )
697 if( p_format_list[j].mFormatID == 'IAC3' ||
698 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
700 b_digital = VLC_TRUE;
707 /* if this stream supports a digital (cac3) format, then go set it. */
708 int i_requested_rate_format = -1;
709 int i_current_rate_format = -1;
710 int i_backup_rate_format = -1;
712 p_sys->i_stream_id = p_streams[i];
713 p_sys->i_stream_index = i;
715 if( p_sys->b_revert == VLC_FALSE )
717 /* Retrieve the original format of this stream first if not done so already */
718 i_param_size = sizeof( p_sys->sfmt_revert );
719 err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
720 kAudioStreamPropertyPhysicalFormat,
722 &p_sys->sfmt_revert );
725 msg_Err( p_aout, "could not retrieve the original streamformat: [%4.4s]", (char *)&err );
728 p_sys->b_revert = VLC_TRUE;
731 for( j = 0; j < i_formats; j++ )
733 if( p_format_list[j].mFormatID == 'IAC3' ||
734 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
736 if( p_format_list[j].mSampleRate == p_aout->output.output.i_rate )
738 i_requested_rate_format = j;
741 else if( p_format_list[j].mSampleRate == p_sys->sfmt_revert.mSampleRate )
743 i_current_rate_format = j;
747 if( i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate )
748 i_backup_rate_format = j;
754 if( i_requested_rate_format >= 0 ) /* We prefer to output at the samplerate of the original audio */
755 p_sys->stream_format = p_format_list[i_requested_rate_format];
756 else if( i_current_rate_format >= 0 ) /* If not possible, we will try to use the current samplerate of the device */
757 p_sys->stream_format = p_format_list[i_current_rate_format];
758 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) */
760 if( p_format_list ) free( p_format_list );
762 if( p_streams ) free( p_streams );
764 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) );
766 if( !AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->stream_format ) )
769 /* Set the format flags */
770 if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian )
771 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','b');
773 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
774 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
775 p_aout->output.output.i_frame_length = A52_FRAME_NB;
776 p_aout->output.i_nb_samples = p_aout->output.output.i_frame_length;
777 p_aout->output.output.i_rate = (unsigned int)p_sys->stream_format.mSampleRate;
778 aout_FormatPrepare( &p_aout->output.output );
779 aout_VolumeNoneInit( p_aout );
781 /* Add IOProc callback */
782 err = AudioDeviceAddIOProc( p_sys->i_selected_dev,
783 (AudioDeviceIOProc)RenderCallbackSPDIF,
787 msg_Err( p_aout, "AudioDeviceAddIOProc failed: [%4.4s]", (char *)&err );
791 /* Check for the difference between the Device clock and mdate */
792 p_sys->clock_diff = - (mtime_t)
793 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
794 p_sys->clock_diff += mdate();
797 err = AudioDeviceStart( p_sys->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF );
800 msg_Err( p_aout, "AudioDeviceStart failed: [%4.4s]", (char *)&err );
802 err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
803 (AudioDeviceIOProc)RenderCallbackSPDIF );
806 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
815 /*****************************************************************************
816 * Close: Close HAL AudioUnit
817 *****************************************************************************/
818 static void Close( vlc_object_t * p_this )
820 aout_instance_t *p_aout = (aout_instance_t *)p_this;
821 struct aout_sys_t *p_sys = p_aout->output.p_sys;
822 OSStatus err = noErr;
823 UInt32 i_param_size = 0;
827 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
828 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
829 verify_noerr( CloseComponent( p_sys->au_unit ) );
832 if( p_sys->b_digital )
835 err = AudioDeviceStop( p_sys->i_selected_dev,
836 (AudioDeviceIOProc)RenderCallbackSPDIF );
839 msg_Err( p_aout, "AudioDeviceStop failed: [%4.4s]", (char *)&err );
842 /* Remove IOProc callback */
843 err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
844 (AudioDeviceIOProc)RenderCallbackSPDIF );
847 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
850 if( p_sys->b_revert )
852 AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->sfmt_revert );
855 if( p_sys->b_changed_mixing && p_sys->sfmt_revert.mFormatID != kAudioFormat60958AC3 )
859 /* Revert mixable to true if we are allowed to */
860 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
861 &i_param_size, &b_writeable );
863 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
864 &i_param_size, &b_mix );
866 if( !err && b_writeable )
868 msg_Dbg( p_aout, "mixable is: %d", b_mix );
870 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
871 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
876 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
881 err = AudioHardwareRemovePropertyListener( kAudioHardwarePropertyDevices,
886 msg_Err( p_aout, "AudioHardwareRemovePropertyListener failed: [%4.4s]", (char *)&err );
889 if( p_sys->i_hog_pid == getpid() )
891 p_sys->i_hog_pid = -1;
892 i_param_size = sizeof( p_sys->i_hog_pid );
893 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
894 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
895 if( err != noErr ) msg_Err( p_aout, "Could not release hogmode: [%4.4s]", (char *)&err );
898 if( p_sys ) free( p_sys );
901 /*****************************************************************************
902 * Play: nothing to do
903 *****************************************************************************/
904 static void Play( aout_instance_t * p_aout )
909 /*****************************************************************************
910 * Probe: Check which devices the OS has, and add them to our audio-device menu
911 *****************************************************************************/
912 static void Probe( aout_instance_t * p_aout )
914 OSStatus err = noErr;
915 UInt32 i = 0, i_param_size = 0;
916 AudioDeviceID devid_def = 0;
917 AudioDeviceID *p_devices = NULL;
918 vlc_value_t val, text;
920 struct aout_sys_t *p_sys = p_aout->output.p_sys;
922 /* Get number of devices */
923 err = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices,
924 &i_param_size, NULL );
927 msg_Err( p_aout, "Could not get number of devices: [%4.4s]", (char *)&err );
931 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
933 if( p_sys->i_devices < 1 )
935 msg_Err( p_aout, "No audio output devices were found." );
939 msg_Dbg( p_aout, "system has [%ld] device(s)", p_sys->i_devices );
941 /* Allocate DeviceID array */
942 p_devices = (AudioDeviceID*)malloc( sizeof(AudioDeviceID) * p_sys->i_devices );
943 if( p_devices == NULL )
945 msg_Err( p_aout, "out of memory" );
949 /* Populate DeviceID array */
950 err = AudioHardwareGetProperty( kAudioHardwarePropertyDevices,
951 &i_param_size, p_devices );
954 msg_Err( p_aout, "could not get the device IDs: [%4.4s]", (char *)&err );
958 /* Find the ID of the default Device */
959 i_param_size = sizeof( AudioDeviceID );
960 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
961 &i_param_size, &devid_def );
964 msg_Err( p_aout, "could not get default audio device: [%4.4s]", (char *)&err );
967 p_sys->i_default_dev = devid_def;
969 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
970 text.psz_string = _("Audio Device");
971 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
973 for( i = 0; i < p_sys->i_devices; i++ )
978 /* Retrieve the length of the device name */
979 err = AudioDeviceGetPropertyInfo(
980 p_devices[i], 0, VLC_FALSE,
981 kAudioDevicePropertyDeviceName,
982 &i_param_size, NULL);
983 if( err ) goto error;
985 /* Retrieve the name of the device */
986 psz_name = (char *)malloc( i_param_size );
987 err = AudioDeviceGetProperty(
988 p_devices[i], 0, VLC_FALSE,
989 kAudioDevicePropertyDeviceName,
990 &i_param_size, psz_name);
991 if( err ) goto error;
993 msg_Dbg( p_aout, "DevID: %#lx DevName: %s", p_devices[i], psz_name );
995 if( !AudioDeviceHasOutput( p_devices[i]) )
997 msg_Dbg( p_aout, "this device is INPUT only. skipping..." );
1001 /* Add the menu entries */
1002 val.i_int = (int)p_devices[i];
1003 text.psz_string = strdup( psz_name );
1004 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1005 if( p_sys->i_default_dev == p_devices[i] )
1007 /* The default device is the selected device normally */
1008 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1009 var_Set( p_aout, "audio-device", val );
1012 if( AudioDeviceSupportsDigital( p_aout, p_devices[i] ) )
1014 val.i_int = (int)p_devices[i] | AOUT_VAR_SPDIF_FLAG;
1015 asprintf( &text.psz_string, _("%s (Encoded Output)"), psz_name );
1016 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1017 if( p_sys->i_default_dev == p_devices[i] && config_GetInt( p_aout, "spdif" ) )
1019 /* We selected to prefer SPDIF output if available
1020 * then this "dummy" entry should be selected */
1021 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1022 var_Set( p_aout, "audio-device", val );
1029 /* If a device is already "preselected", then use this device */
1030 var_Get( p_aout->p_libvlc, "macosx-audio-device", &val );
1033 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1034 var_Set( p_aout, "audio-device", val );
1037 /* If we change the device we want to use, we should renegotiate the audio chain */
1038 var_AddCallback( p_aout, "audio-device", AudioDeviceCallback, NULL );
1040 /* Attach a Listener so that we are notified of a change in the Device setup */
1041 err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
1047 if( p_devices ) free( p_devices );
1051 var_Destroy( p_aout, "audio-device" );
1052 if( p_devices ) free( p_devices );
1056 /*****************************************************************************
1057 * AudioDeviceHasOutput: Checks if the Device actually provides any outputs at all
1058 *****************************************************************************/
1059 static int AudioDeviceHasOutput( AudioDeviceID i_dev_id )
1064 verify_noerr( AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE, kAudioDevicePropertyStreams, &dataSize, &isWritable) );
1065 if (dataSize == 0) return FALSE;
1070 /*****************************************************************************
1071 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
1072 *****************************************************************************/
1073 static int AudioDeviceSupportsDigital( aout_instance_t *p_aout, AudioDeviceID i_dev_id )
1075 OSStatus err = noErr;
1076 UInt32 i_param_size = 0;
1077 AudioStreamID *p_streams = NULL;
1078 int i = 0, i_streams = 0;
1079 vlc_bool_t b_return = VLC_FALSE;
1081 /* Retrieve all the output streams */
1082 err = AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE,
1083 kAudioDevicePropertyStreams,
1084 &i_param_size, NULL );
1087 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1091 i_streams = i_param_size / sizeof( AudioStreamID );
1092 p_streams = (AudioStreamID *)malloc( i_param_size );
1093 if( p_streams == NULL )
1095 msg_Err( p_aout, "out of memory" );
1099 err = AudioDeviceGetProperty( i_dev_id, 0, FALSE,
1100 kAudioDevicePropertyStreams,
1101 &i_param_size, p_streams );
1105 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1109 for( i = 0; i < i_streams; i++ )
1111 if( AudioStreamSupportsDigital( p_aout, p_streams[i] ) )
1112 b_return = VLC_TRUE;
1115 if( p_streams ) free( p_streams );
1119 /*****************************************************************************
1120 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
1121 *****************************************************************************/
1122 static int AudioStreamSupportsDigital( aout_instance_t *p_aout, AudioStreamID i_stream_id )
1124 OSStatus err = noErr;
1125 UInt32 i_param_size = 0;
1126 AudioStreamBasicDescription *p_format_list = NULL;
1127 int i = 0, i_formats = 0;
1128 vlc_bool_t b_return = VLC_FALSE;
1130 /* Retrieve all the stream formats supported by each output stream */
1131 err = AudioStreamGetPropertyInfo( i_stream_id, 0,
1132 kAudioStreamPropertyPhysicalFormats,
1133 &i_param_size, NULL );
1136 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
1140 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
1141 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
1142 if( p_format_list == NULL )
1144 msg_Err( p_aout, "could not malloc the memory" );
1148 err = AudioStreamGetProperty( i_stream_id, 0,
1149 kAudioStreamPropertyPhysicalFormats,
1150 &i_param_size, p_format_list );
1153 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
1154 free( p_format_list);
1155 p_format_list = NULL;
1159 for( i = 0; i < i_formats; i++ )
1161 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format: ", p_format_list[i] ) );
1163 if( p_format_list[i].mFormatID == 'IAC3' ||
1164 p_format_list[i].mFormatID == kAudioFormat60958AC3 )
1166 b_return = VLC_TRUE;
1170 if( p_format_list ) free( p_format_list );
1174 /*****************************************************************************
1175 * AudioStreamChangeFormat: Change i_stream_id to change_format
1176 *****************************************************************************/
1177 static int AudioStreamChangeFormat( aout_instance_t *p_aout, AudioStreamID i_stream_id, AudioStreamBasicDescription change_format )
1179 OSStatus err = noErr;
1180 UInt32 i_param_size = 0;
1184 struct timespec timeout;
1185 struct { vlc_mutex_t lock; vlc_cond_t cond; } w;
1187 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "setting stream format: ", change_format ) );
1189 /* Condition because SetProperty is asynchronious */
1190 vlc_cond_init( p_aout, &w.cond );
1191 vlc_mutex_init( p_aout, &w.lock );
1192 vlc_mutex_lock( &w.lock );
1194 /* Install the callback */
1195 err = AudioStreamAddPropertyListener( i_stream_id, 0,
1196 kAudioStreamPropertyPhysicalFormat,
1197 StreamListener, (void *)&w );
1200 msg_Err( p_aout, "AudioStreamAddPropertyListener failed: [%4.4s]", (char *)&err );
1204 /* change the format */
1205 err = AudioStreamSetProperty( i_stream_id, 0, 0,
1206 kAudioStreamPropertyPhysicalFormat,
1207 sizeof( AudioStreamBasicDescription ),
1211 msg_Err( p_aout, "could not set the stream format: [%4.4s]", (char *)&err );
1215 /* The AudioStreamSetProperty is not only asynchronious (requiring the locks)
1216 * it is also not atomic in its behaviour.
1217 * Therefore we check 5 times before we really give up.
1218 * FIXME: failing isn't actually implemented yet. */
1219 for( i = 0; i < 5; i++ )
1221 AudioStreamBasicDescription actual_format;
1223 gettimeofday( &now, NULL );
1224 timeout.tv_sec = now.tv_sec;
1225 timeout.tv_nsec = (now.tv_usec + 500000) * 1000;
1227 if( pthread_cond_timedwait( &w.cond.cond, &w.lock.mutex, &timeout ) )
1229 msg_Dbg( p_aout, "reached timeout" );
1232 i_param_size = sizeof( AudioStreamBasicDescription );
1233 err = AudioStreamGetProperty( i_stream_id, 0,
1234 kAudioStreamPropertyPhysicalFormat,
1238 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "actual format in use: ", actual_format ) );
1239 if( actual_format.mSampleRate == change_format.mSampleRate &&
1240 actual_format.mFormatID == change_format.mFormatID &&
1241 actual_format.mFramesPerPacket == change_format.mFramesPerPacket )
1243 /* The right format is now active */
1246 /* We need to check again */
1249 /* Removing the property listener */
1250 err = AudioStreamRemovePropertyListener( i_stream_id, 0,
1251 kAudioStreamPropertyPhysicalFormat,
1255 msg_Err( p_aout, "AudioStreamRemovePropertyListener failed: [%4.4s]", (char *)&err );
1259 /* Destroy the lock and condition */
1260 vlc_mutex_unlock( &w.lock );
1261 vlc_mutex_destroy( &w.lock );
1262 vlc_cond_destroy( &w.cond );
1267 /*****************************************************************************
1268 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
1269 * us to provide some more audio data.
1270 * Don't print anything during normal playback, calling blocking function from
1271 * this callback is not allowed.
1272 *****************************************************************************/
1273 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
1274 AudioUnitRenderActionFlags *ioActionFlags,
1275 const AudioTimeStamp *inTimeStamp,
1276 unsigned int inBusNummer,
1277 unsigned int inNumberFrames,
1278 AudioBufferList *ioData )
1280 AudioTimeStamp host_time;
1281 mtime_t current_date = 0;
1282 uint32_t i_mData_bytes = 0;
1284 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
1285 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1287 host_time.mFlags = kAudioTimeStampHostTimeValid;
1288 AudioDeviceTranslateTime( p_sys->i_selected_dev, inTimeStamp, &host_time );
1290 /* Check for the difference between the Device clock and mdate */
1291 p_sys->clock_diff = - (mtime_t)
1292 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
1293 p_sys->clock_diff += mdate();
1295 current_date = p_sys->clock_diff +
1296 AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
1297 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1299 if( ioData == NULL && ioData->mNumberBuffers < 1 )
1301 msg_Err( p_aout, "no iodata or buffers");
1304 if( ioData->mNumberBuffers > 1 )
1305 msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
1308 if( p_sys->i_total_bytes > 0 )
1310 i_mData_bytes = __MIN( p_sys->i_total_bytes - p_sys->i_read_bytes, ioData->mBuffers[0].mDataByteSize );
1311 p_aout->p_libvlc->pf_memcpy( ioData->mBuffers[0].mData, &p_sys->p_remainder_buffer[p_sys->i_read_bytes], i_mData_bytes );
1312 p_sys->i_read_bytes += i_mData_bytes;
1313 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1314 ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1316 if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
1317 p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
1320 while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
1322 /* We don't have enough data yet */
1323 aout_buffer_t * p_buffer;
1324 p_buffer = aout_OutputNextBuffer( p_aout, current_date , VLC_FALSE );
1326 if( p_buffer != NULL )
1328 uint32_t i_second_mData_bytes = __MIN( p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1330 p_aout->p_libvlc->pf_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes, p_buffer->p_buffer, i_second_mData_bytes );
1331 i_mData_bytes += i_second_mData_bytes;
1333 if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
1335 p_sys->i_total_bytes = p_buffer->i_nb_bytes - i_second_mData_bytes;
1336 p_aout->p_libvlc->pf_memcpy( p_sys->p_remainder_buffer, &p_buffer->p_buffer[i_second_mData_bytes], p_sys->i_total_bytes );
1340 /* update current_date */
1341 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1342 ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1344 aout_BufferFree( p_buffer );
1348 p_aout->p_libvlc->pf_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes, 0, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1349 i_mData_bytes += ioData->mBuffers[0].mDataByteSize - i_mData_bytes;
1355 /*****************************************************************************
1356 * RenderCallbackSPDIF: callback for SPDIF audio output
1357 *****************************************************************************/
1358 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice,
1359 const AudioTimeStamp * inNow,
1360 const void * inInputData,
1361 const AudioTimeStamp * inInputTime,
1362 AudioBufferList * outOutputData,
1363 const AudioTimeStamp * inOutputTime,
1364 void * threadGlobals )
1366 aout_buffer_t * p_buffer;
1367 mtime_t current_date;
1369 aout_instance_t * p_aout = (aout_instance_t *)threadGlobals;
1370 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1372 /* Check for the difference between the Device clock and mdate */
1373 p_sys->clock_diff = - (mtime_t)
1374 AudioConvertHostTimeToNanos( inNow->mHostTime ) / 1000;
1375 p_sys->clock_diff += mdate();
1377 current_date = p_sys->clock_diff +
1378 AudioConvertHostTimeToNanos( inOutputTime->mHostTime ) / 1000;
1379 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1381 p_buffer = aout_OutputNextBuffer( p_aout, current_date, VLC_TRUE );
1383 #define BUFFER outOutputData->mBuffers[p_sys->i_stream_index]
1384 if( p_buffer != NULL )
1386 if( (int)BUFFER.mDataByteSize != (int)p_buffer->i_nb_bytes)
1387 msg_Warn( p_aout, "bytesize: %d nb_bytes: %d", (int)BUFFER.mDataByteSize, (int)p_buffer->i_nb_bytes );
1389 /* move data into output data buffer */
1390 p_aout->p_libvlc->pf_memcpy( BUFFER.mData,
1391 p_buffer->p_buffer, p_buffer->i_nb_bytes );
1392 aout_BufferFree( p_buffer );
1396 p_aout->p_libvlc->pf_memset( BUFFER.mData, 0, BUFFER.mDataByteSize );
1403 /*****************************************************************************
1404 * HardwareListener: Warns us of changes in the list of registered devices
1405 *****************************************************************************/
1406 static OSStatus HardwareListener( AudioHardwarePropertyID inPropertyID,
1407 void * inClientData )
1409 OSStatus err = noErr;
1410 aout_instance_t *p_aout = (aout_instance_t *)inClientData;
1412 switch( inPropertyID )
1414 case kAudioHardwarePropertyDevices:
1416 /* something changed in the list of devices */
1417 /* We trigger the audio-device's aout_ChannelsRestart callback */
1418 var_Change( p_aout, "audio-device", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
1419 var_Destroy( p_aout, "audio-device" );
1427 /*****************************************************************************
1429 *****************************************************************************/
1430 static OSStatus StreamListener( AudioStreamID inStream,
1432 AudioDevicePropertyID inPropertyID,
1433 void * inClientData )
1435 OSStatus err = noErr;
1436 struct { vlc_mutex_t lock; vlc_cond_t cond; } * w = inClientData;
1438 switch( inPropertyID )
1440 case kAudioStreamPropertyPhysicalFormat:
1441 vlc_mutex_lock( &w->lock );
1442 vlc_cond_signal( &w->cond );
1443 vlc_mutex_unlock( &w->lock );
1452 /*****************************************************************************
1453 * AudioDeviceCallback: Callback triggered when the audio-device variable is changed
1454 *****************************************************************************/
1455 static int AudioDeviceCallback( vlc_object_t *p_this, const char *psz_variable,
1456 vlc_value_t old_val, vlc_value_t new_val, void *param )
1458 aout_instance_t *p_aout = (aout_instance_t *)p_this;
1459 var_Set( p_aout->p_libvlc, "macosx-audio-device", new_val );
1460 msg_Dbg( p_aout, "Set Device: %#x", new_val.i_int );
1461 return aout_ChannelsRestart( p_this, psz_variable, old_val, new_val, param );