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 *****************************************************************************/
28 #include <sys/time.h> /* gettimeofday() */
35 #include <vlc_interface.h>
38 #include <CoreAudio/CoreAudio.h>
39 #include <AudioUnit/AudioUnitProperties.h>
40 #include <AudioUnit/AudioUnitParameters.h>
41 #include <AudioUnit/AudioOutputUnit.h>
42 #include <AudioToolbox/AudioFormat.h>
44 #define STREAM_FORMAT_MSG( pre, sfm ) \
45 pre "[%ld][%4.4s][%ld][%ld][%ld][%ld][%ld][%ld]", \
46 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
47 sfm.mFormatFlags, sfm.mBytesPerPacket, \
48 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
49 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
51 #define STREAM_FORMAT_MSG_FULL( pre, sfm ) \
52 pre ":\nsamplerate: [%ld]\nFormatID: [%4.4s]\nFormatFlags: [%ld]\nBypesPerPacket: [%ld]\nFramesPerPacket: [%ld]\nBytesPerFrame: [%ld]\nChannelsPerFrame: [%ld]\nBitsPerChannel[%ld]", \
53 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
54 sfm.mFormatFlags, sfm.mBytesPerPacket, \
55 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
56 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
58 #define BUFSIZE 0xffffff
59 #define AOUT_VAR_SPDIF_FLAG 0xf00000
63 * - clean up the debug info
65 * - be better at changing stream setup or devices setup changes while playing.
69 /*****************************************************************************
70 * aout_sys_t: private audio output method descriptor
71 *****************************************************************************
72 * This structure is part of the audio output thread descriptor.
73 * It describes the CoreAudio specific properties of an output thread.
74 *****************************************************************************/
77 AudioDeviceID i_default_dev; /* Keeps DeviceID of defaultOutputDevice */
78 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device */
79 UInt32 i_devices; /* Number of CoreAudio Devices */
80 vlc_bool_t b_supports_digital;/* Does the currently selected device support digital mode? */
81 vlc_bool_t b_digital; /* Are we running in digital mode? */
82 mtime_t clock_diff; /* Difference between VLC clock and Device clock */
85 Component au_component; /* The Audiocomponent we use */
86 AudioUnit au_unit; /* The AudioUnit we use */
87 uint8_t p_remainder_buffer[BUFSIZE];
88 uint32_t i_read_bytes;
89 uint32_t i_total_bytes;
90 AudioDeviceIOProcID procId;
92 /* CoreAudio SPDIF mode specific */
93 pid_t i_hog_pid; /* The keep the pid of our hog status */
94 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */
95 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */
96 AudioStreamBasicDescription stream_format; /* The format we changed the stream to */
97 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */
98 vlc_bool_t b_revert; /* Wether we need to revert the stream format */
99 vlc_bool_t b_changed_mixing;/* Wether we need to set the mixing mode back */
102 /*****************************************************************************
104 *****************************************************************************/
105 static int Open ( vlc_object_t * );
106 static int OpenAnalog ( aout_instance_t * );
107 static int OpenSPDIF ( aout_instance_t * );
108 static void Close ( vlc_object_t * );
110 static void Play ( aout_instance_t * );
111 static void Probe ( aout_instance_t * );
113 static int AudioDeviceHasOutput ( AudioDeviceID );
114 static int AudioDeviceSupportsDigital( aout_instance_t *, AudioDeviceID );
115 static int AudioStreamSupportsDigital( aout_instance_t *, AudioStreamID );
116 static int AudioStreamChangeFormat ( aout_instance_t *, AudioStreamID, AudioStreamBasicDescription );
118 static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
119 unsigned int, unsigned int, AudioBufferList *);
120 static OSStatus RenderCallbackSPDIF ( AudioDeviceID, const AudioTimeStamp *, const void *, const AudioTimeStamp *,
121 AudioBufferList *, const AudioTimeStamp *, void * );
122 static OSStatus HardwareListener ( AudioHardwarePropertyID, void *);
123 static OSStatus StreamListener ( AudioStreamID, UInt32,
124 AudioDevicePropertyID, void * );
125 static int AudioDeviceCallback ( vlc_object_t *, const char *,
126 vlc_value_t, vlc_value_t, void * );
129 /*****************************************************************************
131 *****************************************************************************/
132 #define ADEV_TEXT N_("Audio Device")
133 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
134 "audio device, as listed in your 'Audio Device' menu. This device will " \
135 "then be used by default for audio playback.")
138 set_shortname( "auhal" );
139 set_description( _("HAL AudioUnit output") );
140 set_capability( "audio output", 101 );
141 set_category( CAT_AUDIO );
142 set_subcategory( SUBCAT_AUDIO_AOUT );
143 set_callbacks( Open, Close );
144 add_integer( "macosx-audio-device", 0, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE );
147 /*****************************************************************************
148 * Open: open macosx audio output
149 *****************************************************************************/
150 static int Open( vlc_object_t * p_this )
152 OSStatus err = noErr;
153 UInt32 i_param_size = 0;
154 struct aout_sys_t *p_sys = NULL;
155 vlc_bool_t b_alive = VLC_FALSE;
157 aout_instance_t *p_aout = (aout_instance_t *)p_this;
159 /* Allocate structure */
160 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
161 if( p_aout->output.p_sys == NULL )
163 msg_Err( p_aout, "out of memory" );
164 return( VLC_ENOMEM );
167 p_sys = p_aout->output.p_sys;
168 p_sys->i_default_dev = 0;
169 p_sys->i_selected_dev = 0;
170 p_sys->i_devices = 0;
171 p_sys->b_supports_digital = VLC_FALSE;
172 p_sys->b_digital = VLC_FALSE;
173 p_sys->au_component = NULL;
174 p_sys->au_unit = NULL;
175 p_sys->clock_diff = (mtime_t) 0;
176 p_sys->i_read_bytes = 0;
177 p_sys->i_total_bytes = 0;
178 p_sys->i_hog_pid = -1;
179 p_sys->i_stream_id = 0;
180 p_sys->i_stream_index = -1;
181 p_sys->b_revert = VLC_FALSE;
182 p_sys->b_changed_mixing = VLC_FALSE;
183 memset( p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE );
185 p_aout->output.pf_play = Play;
187 aout_FormatPrint( p_aout, "VLC is looking for:", (audio_sample_format_t *)&p_aout->output.output );
189 /* Persistent device variable */
190 if( var_Type( p_aout->p_libvlc, "macosx-audio-device" ) == 0 )
192 var_Create( p_aout->p_libvlc, "macosx-audio-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
195 /* Build a list of devices */
196 if( var_Type( p_aout, "audio-device" ) == 0 )
201 /* What device do we want? */
202 if( var_Get( p_aout, "audio-device", &val ) < 0 )
204 msg_Err( p_aout, "audio-device var does not exist. device probe failed." );
208 p_sys->i_selected_dev = val.i_int & ~AOUT_VAR_SPDIF_FLAG; /* remove SPDIF flag to get the true DeviceID */
209 p_sys->b_supports_digital = ( val.i_int & AOUT_VAR_SPDIF_FLAG ) ? VLC_TRUE : VLC_FALSE;
211 /* Check if the desired device is alive and usable */
212 /* TODO: add a callback to the device to alert us if the device dies */
213 i_param_size = sizeof( b_alive );
214 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
215 kAudioDevicePropertyDeviceIsAlive,
216 &i_param_size, &b_alive );
220 msg_Err( p_aout, "could not check whether device is alive: %4.4s", (char *)&err );
224 if( b_alive == VLC_FALSE )
226 msg_Warn( p_aout, "selected audio device is not alive, switching to default device" );
227 p_sys->i_selected_dev = p_sys->i_default_dev;
230 i_param_size = sizeof( p_sys->i_hog_pid );
231 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
232 kAudioDevicePropertyHogMode,
233 &i_param_size, &p_sys->i_hog_pid );
237 /* This is not a fatal error. Some drivers simply don't support this property */
238 msg_Warn( p_aout, "could not check whether device is hogged: %4.4s",
240 p_sys->i_hog_pid = -1;
243 if( p_sys->i_hog_pid != -1 && p_sys->i_hog_pid != getpid() )
245 msg_Err( p_aout, "Selected audio device is exclusively in use by another program." );
246 intf_UserFatal( p_aout, VLC_FALSE, _("Audio output failed"),
247 _("The selected audio output device is exclusively in "
248 "use by another program.") );
252 /* Check for Digital mode or Analog output mode */
253 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
255 if( OpenSPDIF( p_aout ) )
260 if( OpenAnalog( p_aout ) )
265 /* If we reach this, this aout has failed */
266 var_Destroy( p_aout, "audio-device" );
271 /*****************************************************************************
272 * Open: open and setup a HAL AudioUnit to do analog (multichannel) audio output
273 *****************************************************************************/
274 static int OpenAnalog( aout_instance_t *p_aout )
276 struct aout_sys_t *p_sys = p_aout->output.p_sys;
277 OSStatus err = noErr;
278 UInt32 i_param_size = 0, i = 0;
280 ComponentDescription desc;
281 AudioStreamBasicDescription DeviceFormat;
282 AudioChannelLayout *layout;
283 AudioChannelLayout new_layout;
284 AURenderCallbackStruct input;
286 /* Lets go find our Component */
287 desc.componentType = kAudioUnitType_Output;
288 desc.componentSubType = kAudioUnitSubType_HALOutput;
289 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
290 desc.componentFlags = 0;
291 desc.componentFlagsMask = 0;
293 p_sys->au_component = FindNextComponent( NULL, &desc );
294 if( p_sys->au_component == NULL )
296 msg_Warn( p_aout, "we cannot find our HAL component" );
300 err = OpenAComponent( p_sys->au_component, &p_sys->au_unit );
303 msg_Warn( p_aout, "we cannot open our HAL component" );
307 /* Set the device we will use for this output unit */
308 err = AudioUnitSetProperty( p_sys->au_unit,
309 kAudioOutputUnitProperty_CurrentDevice,
310 kAudioUnitScope_Global,
312 &p_sys->i_selected_dev,
313 sizeof( AudioDeviceID ));
317 msg_Warn( p_aout, "we cannot select the audio device" );
321 /* Get the current format */
322 i_param_size = sizeof(AudioStreamBasicDescription);
324 err = AudioUnitGetProperty( p_sys->au_unit,
325 kAudioUnitProperty_StreamFormat,
326 kAudioUnitScope_Input,
331 if( err != noErr ) return VLC_FALSE;
332 else msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is: ", DeviceFormat ) );
334 /* Get the channel layout of the device side of the unit (vlc -> unit -> device) */
335 err = AudioUnitGetPropertyInfo( p_sys->au_unit,
336 kAudioDevicePropertyPreferredChannelLayout,
337 kAudioUnitScope_Output,
344 layout = (AudioChannelLayout *)malloc( i_param_size);
346 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
347 kAudioDevicePropertyPreferredChannelLayout,
348 kAudioUnitScope_Output,
353 /* We need to "fill out" the ChannelLayout, because there are multiple ways that it can be set */
354 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
356 /* bitmap defined channellayout */
357 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
358 sizeof( UInt32), &layout->mChannelBitmap,
362 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
364 /* layouttags defined channellayout */
365 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
366 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
371 msg_Dbg( p_aout, "layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
373 /* Initialize the VLC core channel count */
374 p_aout->output.output.i_physical_channels = 0;
375 i_original = p_aout->output.output.i_original_channels & AOUT_CHAN_PHYSMASK;
377 if( i_original == AOUT_CHAN_CENTER || layout->mNumberChannelDescriptions < 2 )
379 /* We only need Mono or cannot output more than 1 channel */
380 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
382 else if( i_original == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) || layout->mNumberChannelDescriptions < 3 )
384 /* We only need Stereo or cannot output more than 2 channels */
385 p_aout->output.output.i_physical_channels = AOUT_CHAN_RIGHT | AOUT_CHAN_LEFT;
389 /* We want more than stereo and we can do that */
390 for( i = 0; i < layout->mNumberChannelDescriptions; i++ )
392 msg_Dbg( p_aout, "this is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
394 switch( layout->mChannelDescriptions[i].mChannelLabel )
396 case kAudioChannelLabel_Left:
397 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
399 case kAudioChannelLabel_Right:
400 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
402 case kAudioChannelLabel_Center:
403 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
405 case kAudioChannelLabel_LFEScreen:
406 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE;
408 case kAudioChannelLabel_LeftSurround:
409 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT;
411 case kAudioChannelLabel_RightSurround:
412 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
414 case kAudioChannelLabel_RearSurroundLeft:
415 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
417 case kAudioChannelLabel_RearSurroundRight:
418 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
420 case kAudioChannelLabel_CenterSurround:
421 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER;
424 msg_Warn( p_aout, "unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
427 if( p_aout->output.output.i_physical_channels == 0 )
429 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
430 msg_Err( p_aout, "You should configure your speaker layout with Audio Midi Setup Utility in /Applications/Utilities. Now using Stereo mode." );
431 intf_UserFatal( p_aout, VLC_FALSE, _("Audio device is not configured"),
432 _("You should configure your speaker layout with "
433 "the \"Audio Midi Setup\" utility in /Applications/"
434 "Utilities. Stereo mode is being used now.") );
441 msg_Warn( p_aout, "this driver does not support kAudioDevicePropertyPreferredChannelLayout. BAD DRIVER AUTHOR !!!" );
442 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
445 msg_Dbg( p_aout, "selected %d physical channels for device output", aout_FormatNbChannels( &p_aout->output.output ) );
446 msg_Dbg( p_aout, "VLC will output: %s", aout_FormatPrintChannels( &p_aout->output.output ));
448 memset (&new_layout, 0, sizeof(new_layout));
449 switch( aout_FormatNbChannels( &p_aout->output.output ) )
452 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
455 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
458 if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
460 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
462 else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
464 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
468 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
470 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
472 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
474 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
476 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
478 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
482 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
484 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
486 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
488 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
492 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
494 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
498 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_6_0; // L R Ls Rs C Cs
502 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
503 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
506 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
507 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
511 /* Set up the format to be used */
512 DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
513 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
515 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
516 p_aout->output.output.i_format = VLC_FOURCC( 'f','l','3','2');
517 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
518 DeviceFormat.mBitsPerChannel = 32;
519 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output );
521 /* Calculate framesizes and stuff */
522 DeviceFormat.mFramesPerPacket = 1;
523 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
524 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
526 /* Set the desired format */
527 i_param_size = sizeof(AudioStreamBasicDescription);
528 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
529 kAudioUnitProperty_StreamFormat,
530 kAudioUnitScope_Input,
535 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
537 /* Retrieve actual format */
538 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
539 kAudioUnitProperty_StreamFormat,
540 kAudioUnitScope_Input,
545 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
547 /* Do the last VLC aout setups */
548 aout_FormatPrepare( &p_aout->output.output );
549 p_aout->output.i_nb_samples = 2048;
550 aout_VolumeSoftInit( p_aout );
552 /* set the IOproc callback */
553 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
554 input.inputProcRefCon = p_aout;
556 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
557 kAudioUnitProperty_SetRenderCallback,
558 kAudioUnitScope_Input,
559 0, &input, sizeof( input ) ) );
561 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
562 input.inputProcRefCon = p_aout;
564 /* Set the new_layout as the layout VLC will use to feed the AU unit */
565 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
566 kAudioUnitProperty_AudioChannelLayout,
567 kAudioUnitScope_Input,
568 0, &new_layout, sizeof(new_layout) ) );
570 if( new_layout.mNumberChannelDescriptions > 0 )
571 free( new_layout.mChannelDescriptions );
574 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
576 /* Find the difference between device clock and mdate clock */
577 p_sys->clock_diff = - (mtime_t)
578 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
579 p_sys->clock_diff += mdate();
582 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
587 /*****************************************************************************
588 * Setup a encoded digital stream (SPDIF)
589 *****************************************************************************/
590 static int OpenSPDIF( aout_instance_t * p_aout )
592 struct aout_sys_t *p_sys = p_aout->output.p_sys;
593 OSStatus err = noErr;
594 UInt32 i_param_size = 0, b_mix = 0;
595 Boolean b_writeable = VLC_FALSE;
596 AudioStreamID *p_streams = NULL;
597 int i = 0, i_streams = 0;
599 /* Start doing the SPDIF setup proces */
600 p_sys->b_digital = VLC_TRUE;
603 i_param_size = sizeof( p_sys->i_hog_pid );
604 p_sys->i_hog_pid = getpid() ;
606 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
607 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
611 msg_Err( p_aout, "failed to set hogmode: [%4.4s]", (char *)&err );
615 /* Set mixable to false if we are allowed to */
616 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
617 &i_param_size, &b_writeable );
619 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
620 &i_param_size, &b_mix );
622 if( !err && b_writeable )
625 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
626 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
627 p_sys->b_changed_mixing = VLC_TRUE;
632 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
636 /* Get a list of all the streams on this device */
637 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE,
638 kAudioDevicePropertyStreams,
639 &i_param_size, NULL );
642 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
646 i_streams = i_param_size / sizeof( AudioStreamID );
647 p_streams = (AudioStreamID *)malloc( i_param_size );
648 if( p_streams == NULL )
650 msg_Err( p_aout, "out of memory" );
654 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
655 kAudioDevicePropertyStreams,
656 &i_param_size, p_streams );
660 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
665 for( i = 0; i < i_streams && p_sys->i_stream_index < 0 ; i++ )
667 /* Find a stream with a cac3 stream */
668 AudioStreamBasicDescription *p_format_list = NULL;
669 int i_formats = 0, j = 0;
670 vlc_bool_t b_digital = VLC_FALSE;
672 /* Retrieve all the stream formats supported by each output stream */
673 err = AudioStreamGetPropertyInfo( p_streams[i], 0,
674 kAudioStreamPropertyPhysicalFormats,
675 &i_param_size, NULL );
678 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
682 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
683 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
684 if( p_format_list == NULL )
686 msg_Err( p_aout, "could not malloc the memory" );
690 err = AudioStreamGetProperty( p_streams[i], 0,
691 kAudioStreamPropertyPhysicalFormats,
692 &i_param_size, p_format_list );
695 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
696 free( p_format_list );
700 /* Check if one of the supported formats is a digital format */
701 for( j = 0; j < i_formats; j++ )
703 if( p_format_list[j].mFormatID == 'IAC3' ||
704 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
706 b_digital = VLC_TRUE;
713 /* if this stream supports a digital (cac3) format, then go set it. */
714 int i_requested_rate_format = -1;
715 int i_current_rate_format = -1;
716 int i_backup_rate_format = -1;
718 p_sys->i_stream_id = p_streams[i];
719 p_sys->i_stream_index = i;
721 if( p_sys->b_revert == VLC_FALSE )
723 /* Retrieve the original format of this stream first if not done so already */
724 i_param_size = sizeof( p_sys->sfmt_revert );
725 err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
726 kAudioStreamPropertyPhysicalFormat,
728 &p_sys->sfmt_revert );
731 msg_Err( p_aout, "could not retrieve the original streamformat: [%4.4s]", (char *)&err );
734 p_sys->b_revert = VLC_TRUE;
737 for( j = 0; j < i_formats; j++ )
739 if( p_format_list[j].mFormatID == 'IAC3' ||
740 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
742 if( p_format_list[j].mSampleRate == p_aout->output.output.i_rate )
744 i_requested_rate_format = j;
747 else if( p_format_list[j].mSampleRate == p_sys->sfmt_revert.mSampleRate )
749 i_current_rate_format = j;
753 if( i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate )
754 i_backup_rate_format = j;
760 if( i_requested_rate_format >= 0 ) /* We prefer to output at the samplerate of the original audio */
761 p_sys->stream_format = p_format_list[i_requested_rate_format];
762 else if( i_current_rate_format >= 0 ) /* If not possible, we will try to use the current samplerate of the device */
763 p_sys->stream_format = p_format_list[i_current_rate_format];
764 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) */
766 free( p_format_list );
770 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) );
772 if( !AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->stream_format ) )
775 /* Set the format flags */
776 if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian )
777 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','b');
779 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
780 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
781 p_aout->output.output.i_frame_length = A52_FRAME_NB;
782 p_aout->output.i_nb_samples = p_aout->output.output.i_frame_length;
783 p_aout->output.output.i_rate = (unsigned int)p_sys->stream_format.mSampleRate;
784 aout_FormatPrepare( &p_aout->output.output );
785 aout_VolumeNoneInit( p_aout );
787 /* Add IOProc callback */
788 err = AudioDeviceCreateIOProcID( p_sys->i_selected_dev,
789 (AudioDeviceIOProc)RenderCallbackSPDIF,
794 msg_Err( p_aout, "AudioDeviceAddIOProc failed: [%4.4s]", (char *)&err );
798 /* Check for the difference between the Device clock and mdate */
799 p_sys->clock_diff = - (mtime_t)
800 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
801 p_sys->clock_diff += mdate();
804 err = AudioDeviceStart( p_sys->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF );
807 msg_Err( p_aout, "AudioDeviceStart failed: [%4.4s]", (char *)&err );
809 err = AudioDeviceDestroyIOProcID( p_sys->i_selected_dev,
813 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
822 /*****************************************************************************
823 * Close: Close HAL AudioUnit
824 *****************************************************************************/
825 static void Close( vlc_object_t * p_this )
827 aout_instance_t *p_aout = (aout_instance_t *)p_this;
828 struct aout_sys_t *p_sys = p_aout->output.p_sys;
829 OSStatus err = noErr;
830 UInt32 i_param_size = 0;
834 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
835 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
836 verify_noerr( CloseComponent( p_sys->au_unit ) );
839 if( p_sys->b_digital )
842 err = AudioDeviceStop( p_sys->i_selected_dev,
843 (AudioDeviceIOProc)RenderCallbackSPDIF );
846 msg_Err( p_aout, "AudioDeviceStop failed: [%4.4s]", (char *)&err );
849 /* Remove IOProc callback */
850 err = AudioDeviceDestroyIOProcID( p_sys->i_selected_dev,
854 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
857 if( p_sys->b_revert )
859 AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->sfmt_revert );
862 if( p_sys->b_changed_mixing && p_sys->sfmt_revert.mFormatID != kAudioFormat60958AC3 )
866 /* Revert mixable to true if we are allowed to */
867 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
868 &i_param_size, &b_writeable );
870 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
871 &i_param_size, &b_mix );
873 if( !err && b_writeable )
875 msg_Dbg( p_aout, "mixable is: %d", b_mix );
877 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
878 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
883 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
888 err = AudioHardwareRemovePropertyListener( kAudioHardwarePropertyDevices,
893 msg_Err( p_aout, "AudioHardwareRemovePropertyListener failed: [%4.4s]", (char *)&err );
896 if( p_sys->i_hog_pid == getpid() )
898 p_sys->i_hog_pid = -1;
899 i_param_size = sizeof( p_sys->i_hog_pid );
900 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
901 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
902 if( err != noErr ) msg_Err( p_aout, "Could not release hogmode: [%4.4s]", (char *)&err );
908 /*****************************************************************************
909 * Play: nothing to do
910 *****************************************************************************/
911 static void Play( aout_instance_t * p_aout )
916 /*****************************************************************************
917 * Probe: Check which devices the OS has, and add them to our audio-device menu
918 *****************************************************************************/
919 static void Probe( aout_instance_t * p_aout )
921 OSStatus err = noErr;
922 UInt32 i = 0, i_param_size = 0;
923 AudioDeviceID devid_def = 0;
924 AudioDeviceID *p_devices = NULL;
925 vlc_value_t val, text;
927 struct aout_sys_t *p_sys = p_aout->output.p_sys;
929 /* Get number of devices */
930 err = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices,
931 &i_param_size, NULL );
934 msg_Err( p_aout, "Could not get number of devices: [%4.4s]", (char *)&err );
938 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
940 if( p_sys->i_devices < 1 )
942 msg_Err( p_aout, "No audio output devices were found." );
946 msg_Dbg( p_aout, "system has [%ld] device(s)", p_sys->i_devices );
948 /* Allocate DeviceID array */
949 p_devices = (AudioDeviceID*)malloc( sizeof(AudioDeviceID) * p_sys->i_devices );
950 if( p_devices == NULL )
952 msg_Err( p_aout, "out of memory" );
956 /* Populate DeviceID array */
957 err = AudioHardwareGetProperty( kAudioHardwarePropertyDevices,
958 &i_param_size, p_devices );
961 msg_Err( p_aout, "could not get the device IDs: [%4.4s]", (char *)&err );
965 /* Find the ID of the default Device */
966 i_param_size = sizeof( AudioDeviceID );
967 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
968 &i_param_size, &devid_def );
971 msg_Err( p_aout, "could not get default audio device: [%4.4s]", (char *)&err );
974 p_sys->i_default_dev = devid_def;
976 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
977 text.psz_string = _("Audio Device");
978 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
980 for( i = 0; i < p_sys->i_devices; i++ )
985 /* Retrieve the length of the device name */
986 err = AudioDeviceGetPropertyInfo(
987 p_devices[i], 0, VLC_FALSE,
988 kAudioDevicePropertyDeviceName,
989 &i_param_size, NULL);
990 if( err ) goto error;
992 /* Retrieve the name of the device */
993 psz_name = (char *)malloc( i_param_size );
994 err = AudioDeviceGetProperty(
995 p_devices[i], 0, VLC_FALSE,
996 kAudioDevicePropertyDeviceName,
997 &i_param_size, psz_name);
998 if( err ) goto error;
1000 msg_Dbg( p_aout, "DevID: %#lx DevName: %s", p_devices[i], psz_name );
1002 if( !AudioDeviceHasOutput( p_devices[i]) )
1004 msg_Dbg( p_aout, "this device is INPUT only. skipping..." );
1008 /* Add the menu entries */
1009 val.i_int = (int)p_devices[i];
1010 text.psz_string = strdup( psz_name );
1011 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1012 if( p_sys->i_default_dev == p_devices[i] )
1014 /* The default device is the selected device normally */
1015 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1016 var_Set( p_aout, "audio-device", val );
1019 if( AudioDeviceSupportsDigital( p_aout, p_devices[i] ) )
1021 val.i_int = (int)p_devices[i] | AOUT_VAR_SPDIF_FLAG;
1022 asprintf( &text.psz_string, _("%s (Encoded Output)"), psz_name );
1023 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1024 if( p_sys->i_default_dev == p_devices[i] && config_GetInt( p_aout, "spdif" ) )
1026 /* We selected to prefer SPDIF output if available
1027 * then this "dummy" entry should be selected */
1028 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1029 var_Set( p_aout, "audio-device", val );
1036 /* If a device is already "preselected", then use this device */
1037 var_Get( p_aout->p_libvlc, "macosx-audio-device", &val );
1040 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1041 var_Set( p_aout, "audio-device", val );
1044 /* If we change the device we want to use, we should renegotiate the audio chain */
1045 var_AddCallback( p_aout, "audio-device", AudioDeviceCallback, NULL );
1047 /* Attach a Listener so that we are notified of a change in the Device setup */
1048 err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
1058 var_Destroy( p_aout, "audio-device" );
1063 /*****************************************************************************
1064 * AudioDeviceHasOutput: Checks if the Device actually provides any outputs at all
1065 *****************************************************************************/
1066 static int AudioDeviceHasOutput( AudioDeviceID i_dev_id )
1071 verify_noerr( AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE, kAudioDevicePropertyStreams, &dataSize, &isWritable) );
1072 if (dataSize == 0) return FALSE;
1077 /*****************************************************************************
1078 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
1079 *****************************************************************************/
1080 static int AudioDeviceSupportsDigital( aout_instance_t *p_aout, AudioDeviceID i_dev_id )
1082 OSStatus err = noErr;
1083 UInt32 i_param_size = 0;
1084 AudioStreamID *p_streams = NULL;
1085 int i = 0, i_streams = 0;
1086 vlc_bool_t b_return = VLC_FALSE;
1088 /* Retrieve all the output streams */
1089 err = AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE,
1090 kAudioDevicePropertyStreams,
1091 &i_param_size, NULL );
1094 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1098 i_streams = i_param_size / sizeof( AudioStreamID );
1099 p_streams = (AudioStreamID *)malloc( i_param_size );
1100 if( p_streams == NULL )
1102 msg_Err( p_aout, "out of memory" );
1106 err = AudioDeviceGetProperty( i_dev_id, 0, FALSE,
1107 kAudioDevicePropertyStreams,
1108 &i_param_size, p_streams );
1112 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1116 for( i = 0; i < i_streams; i++ )
1118 if( AudioStreamSupportsDigital( p_aout, p_streams[i] ) )
1119 b_return = VLC_TRUE;
1126 /*****************************************************************************
1127 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
1128 *****************************************************************************/
1129 static int AudioStreamSupportsDigital( aout_instance_t *p_aout, AudioStreamID i_stream_id )
1131 OSStatus err = noErr;
1132 UInt32 i_param_size = 0;
1133 AudioStreamBasicDescription *p_format_list = NULL;
1134 int i = 0, i_formats = 0;
1135 vlc_bool_t b_return = VLC_FALSE;
1137 /* Retrieve all the stream formats supported by each output stream */
1138 err = AudioStreamGetPropertyInfo( i_stream_id, 0,
1139 kAudioStreamPropertyPhysicalFormats,
1140 &i_param_size, NULL );
1143 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
1147 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
1148 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
1149 if( p_format_list == NULL )
1151 msg_Err( p_aout, "could not malloc the memory" );
1155 err = AudioStreamGetProperty( i_stream_id, 0,
1156 kAudioStreamPropertyPhysicalFormats,
1157 &i_param_size, p_format_list );
1160 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
1161 free( p_format_list);
1162 p_format_list = NULL;
1166 for( i = 0; i < i_formats; i++ )
1168 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format: ", p_format_list[i] ) );
1170 if( p_format_list[i].mFormatID == 'IAC3' ||
1171 p_format_list[i].mFormatID == kAudioFormat60958AC3 )
1173 b_return = VLC_TRUE;
1177 free( p_format_list );
1181 /*****************************************************************************
1182 * AudioStreamChangeFormat: Change i_stream_id to change_format
1183 *****************************************************************************/
1184 static int AudioStreamChangeFormat( aout_instance_t *p_aout, AudioStreamID i_stream_id, AudioStreamBasicDescription change_format )
1186 OSStatus err = noErr;
1187 UInt32 i_param_size = 0;
1191 struct timespec timeout;
1192 struct { vlc_mutex_t lock; vlc_cond_t cond; } w;
1194 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "setting stream format: ", change_format ) );
1196 /* Condition because SetProperty is asynchronious */
1197 vlc_cond_init( p_aout, &w.cond );
1198 vlc_mutex_init( p_aout, &w.lock );
1199 vlc_mutex_lock( &w.lock );
1201 /* Install the callback */
1202 err = AudioStreamAddPropertyListener( i_stream_id, 0,
1203 kAudioStreamPropertyPhysicalFormat,
1204 StreamListener, (void *)&w );
1207 msg_Err( p_aout, "AudioStreamAddPropertyListener failed: [%4.4s]", (char *)&err );
1211 /* change the format */
1212 err = AudioStreamSetProperty( i_stream_id, 0, 0,
1213 kAudioStreamPropertyPhysicalFormat,
1214 sizeof( AudioStreamBasicDescription ),
1218 msg_Err( p_aout, "could not set the stream format: [%4.4s]", (char *)&err );
1222 /* The AudioStreamSetProperty is not only asynchronious (requiring the locks)
1223 * it is also not atomic in its behaviour.
1224 * Therefore we check 5 times before we really give up.
1225 * FIXME: failing isn't actually implemented yet. */
1226 for( i = 0; i < 5; i++ )
1228 AudioStreamBasicDescription actual_format;
1230 gettimeofday( &now, NULL );
1231 timeout.tv_sec = now.tv_sec;
1232 timeout.tv_nsec = (now.tv_usec + 500000) * 1000;
1234 if( pthread_cond_timedwait( &w.cond.cond, &w.lock.mutex, &timeout ) )
1236 msg_Dbg( p_aout, "reached timeout" );
1239 i_param_size = sizeof( AudioStreamBasicDescription );
1240 err = AudioStreamGetProperty( i_stream_id, 0,
1241 kAudioStreamPropertyPhysicalFormat,
1245 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "actual format in use: ", actual_format ) );
1246 if( actual_format.mSampleRate == change_format.mSampleRate &&
1247 actual_format.mFormatID == change_format.mFormatID &&
1248 actual_format.mFramesPerPacket == change_format.mFramesPerPacket )
1250 /* The right format is now active */
1253 /* We need to check again */
1256 /* Removing the property listener */
1257 err = AudioStreamRemovePropertyListener( i_stream_id, 0,
1258 kAudioStreamPropertyPhysicalFormat,
1262 msg_Err( p_aout, "AudioStreamRemovePropertyListener failed: [%4.4s]", (char *)&err );
1266 /* Destroy the lock and condition */
1267 vlc_mutex_unlock( &w.lock );
1268 vlc_mutex_destroy( &w.lock );
1269 vlc_cond_destroy( &w.cond );
1274 /*****************************************************************************
1275 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
1276 * us to provide some more audio data.
1277 * Don't print anything during normal playback, calling blocking function from
1278 * this callback is not allowed.
1279 *****************************************************************************/
1280 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
1281 AudioUnitRenderActionFlags *ioActionFlags,
1282 const AudioTimeStamp *inTimeStamp,
1283 unsigned int inBusNummer,
1284 unsigned int inNumberFrames,
1285 AudioBufferList *ioData )
1287 AudioTimeStamp host_time;
1288 mtime_t current_date = 0;
1289 uint32_t i_mData_bytes = 0;
1291 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
1292 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1294 host_time.mFlags = kAudioTimeStampHostTimeValid;
1295 AudioDeviceTranslateTime( p_sys->i_selected_dev, inTimeStamp, &host_time );
1297 /* Check for the difference between the Device clock and mdate */
1298 p_sys->clock_diff = - (mtime_t)
1299 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
1300 p_sys->clock_diff += mdate();
1302 current_date = p_sys->clock_diff +
1303 AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
1304 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1306 if( ioData == NULL && ioData->mNumberBuffers < 1 )
1308 msg_Err( p_aout, "no iodata or buffers");
1311 if( ioData->mNumberBuffers > 1 )
1312 msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
1315 if( p_sys->i_total_bytes > 0 )
1317 i_mData_bytes = __MIN( p_sys->i_total_bytes - p_sys->i_read_bytes, ioData->mBuffers[0].mDataByteSize );
1318 p_aout->p_libvlc->pf_memcpy( ioData->mBuffers[0].mData, &p_sys->p_remainder_buffer[p_sys->i_read_bytes], i_mData_bytes );
1319 p_sys->i_read_bytes += i_mData_bytes;
1320 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1321 ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1323 if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
1324 p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
1327 while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
1329 /* We don't have enough data yet */
1330 aout_buffer_t * p_buffer;
1331 p_buffer = aout_OutputNextBuffer( p_aout, current_date , VLC_FALSE );
1333 if( p_buffer != NULL )
1335 uint32_t i_second_mData_bytes = __MIN( p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1337 p_aout->p_libvlc->pf_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes, p_buffer->p_buffer, i_second_mData_bytes );
1338 i_mData_bytes += i_second_mData_bytes;
1340 if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
1342 p_sys->i_total_bytes = p_buffer->i_nb_bytes - i_second_mData_bytes;
1343 p_aout->p_libvlc->pf_memcpy( p_sys->p_remainder_buffer, &p_buffer->p_buffer[i_second_mData_bytes], p_sys->i_total_bytes );
1347 /* update current_date */
1348 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1349 ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1351 aout_BufferFree( p_buffer );
1355 p_aout->p_libvlc->pf_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes, 0, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1356 i_mData_bytes += ioData->mBuffers[0].mDataByteSize - i_mData_bytes;
1362 /*****************************************************************************
1363 * RenderCallbackSPDIF: callback for SPDIF audio output
1364 *****************************************************************************/
1365 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice,
1366 const AudioTimeStamp * inNow,
1367 const void * inInputData,
1368 const AudioTimeStamp * inInputTime,
1369 AudioBufferList * outOutputData,
1370 const AudioTimeStamp * inOutputTime,
1371 void * threadGlobals )
1373 aout_buffer_t * p_buffer;
1374 mtime_t current_date;
1376 aout_instance_t * p_aout = (aout_instance_t *)threadGlobals;
1377 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1379 /* Check for the difference between the Device clock and mdate */
1380 p_sys->clock_diff = - (mtime_t)
1381 AudioConvertHostTimeToNanos( inNow->mHostTime ) / 1000;
1382 p_sys->clock_diff += mdate();
1384 current_date = p_sys->clock_diff +
1385 AudioConvertHostTimeToNanos( inOutputTime->mHostTime ) / 1000;
1386 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1388 p_buffer = aout_OutputNextBuffer( p_aout, current_date, VLC_TRUE );
1390 #define BUFFER outOutputData->mBuffers[p_sys->i_stream_index]
1391 if( p_buffer != NULL )
1393 if( (int)BUFFER.mDataByteSize != (int)p_buffer->i_nb_bytes)
1394 msg_Warn( p_aout, "bytesize: %d nb_bytes: %d", (int)BUFFER.mDataByteSize, (int)p_buffer->i_nb_bytes );
1396 /* move data into output data buffer */
1397 p_aout->p_libvlc->pf_memcpy( BUFFER.mData,
1398 p_buffer->p_buffer, p_buffer->i_nb_bytes );
1399 aout_BufferFree( p_buffer );
1403 p_aout->p_libvlc->pf_memset( BUFFER.mData, 0, BUFFER.mDataByteSize );
1410 /*****************************************************************************
1411 * HardwareListener: Warns us of changes in the list of registered devices
1412 *****************************************************************************/
1413 static OSStatus HardwareListener( AudioHardwarePropertyID inPropertyID,
1414 void * inClientData )
1416 OSStatus err = noErr;
1417 aout_instance_t *p_aout = (aout_instance_t *)inClientData;
1419 switch( inPropertyID )
1421 case kAudioHardwarePropertyDevices:
1423 /* something changed in the list of devices */
1424 /* We trigger the audio-device's aout_ChannelsRestart callback */
1425 var_Change( p_aout, "audio-device", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
1426 var_Destroy( p_aout, "audio-device" );
1434 /*****************************************************************************
1436 *****************************************************************************/
1437 static OSStatus StreamListener( AudioStreamID inStream,
1439 AudioDevicePropertyID inPropertyID,
1440 void * inClientData )
1442 OSStatus err = noErr;
1443 struct { vlc_mutex_t lock; vlc_cond_t cond; } * w = inClientData;
1445 switch( inPropertyID )
1447 case kAudioStreamPropertyPhysicalFormat:
1448 vlc_mutex_lock( &w->lock );
1449 vlc_cond_signal( &w->cond );
1450 vlc_mutex_unlock( &w->lock );
1459 /*****************************************************************************
1460 * AudioDeviceCallback: Callback triggered when the audio-device variable is changed
1461 *****************************************************************************/
1462 static int AudioDeviceCallback( vlc_object_t *p_this, const char *psz_variable,
1463 vlc_value_t old_val, vlc_value_t new_val, void *param )
1465 aout_instance_t *p_aout = (aout_instance_t *)p_this;
1466 var_Set( p_aout->p_libvlc, "macosx-audio-device", new_val );
1467 msg_Dbg( p_aout, "Set Device: %#x", new_val.i_int );
1468 return aout_ChannelsRestart( p_this, psz_variable, old_val, new_val, param );