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 *****************************************************************************/
31 #include <vlc_interface.h>
34 #include <CoreAudio/CoreAudio.h>
35 #include <AudioUnit/AudioUnitProperties.h>
36 #include <AudioUnit/AudioUnitParameters.h>
37 #include <AudioUnit/AudioOutputUnit.h>
38 #include <AudioToolbox/AudioFormat.h>
40 #define STREAM_FORMAT_MSG( pre, sfm ) \
41 pre "[%ld][%4.4s][%ld][%ld][%ld][%ld][%ld][%ld]", \
42 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
43 sfm.mFormatFlags, sfm.mBytesPerPacket, \
44 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
45 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
47 #define STREAM_FORMAT_MSG_FULL( pre, sfm ) \
48 pre ":\nsamplerate: [%ld]\nFormatID: [%4.4s]\nFormatFlags: [%ld]\nBypesPerPacket: [%ld]\nFramesPerPacket: [%ld]\nBytesPerFrame: [%ld]\nChannelsPerFrame: [%ld]\nBitsPerChannel[%ld]", \
49 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
50 sfm.mFormatFlags, sfm.mBytesPerPacket, \
51 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
52 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
54 #define BUFSIZE 0xffffff
55 #define AOUT_VAR_SPDIF_FLAG 0xf00000
59 * - clean up the debug info
61 * - be better at changing stream setup or devices setup changes while playing.
65 /*****************************************************************************
66 * aout_sys_t: private audio output method descriptor
67 *****************************************************************************
68 * This structure is part of the audio output thread descriptor.
69 * It describes the CoreAudio specific properties of an output thread.
70 *****************************************************************************/
73 AudioDeviceID i_default_dev; /* Keeps DeviceID of defaultOutputDevice */
74 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device */
75 UInt32 i_devices; /* Number of CoreAudio Devices */
76 vlc_bool_t b_supports_digital;/* Does the currently selected device support digital mode? */
77 vlc_bool_t b_digital; /* Are we running in digital mode? */
78 mtime_t clock_diff; /* Difference between VLC clock and Device clock */
81 Component au_component; /* The Audiocomponent we use */
82 AudioUnit au_unit; /* The AudioUnit we use */
83 uint8_t p_remainder_buffer[BUFSIZE];
84 uint32_t i_read_bytes;
85 uint32_t i_total_bytes;
87 /* CoreAudio SPDIF mode specific */
88 pid_t i_hog_pid; /* The keep the pid of our hog status */
89 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */
90 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */
91 AudioStreamBasicDescription stream_format; /* The format we changed the stream to */
92 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */
93 vlc_bool_t b_revert; /* Wether we need to revert the stream format */
94 vlc_bool_t b_changed_mixing;/* Wether we need to set the mixing mode back */
97 /*****************************************************************************
99 *****************************************************************************/
100 static int Open ( vlc_object_t * );
101 static int OpenAnalog ( aout_instance_t * );
102 static int OpenSPDIF ( aout_instance_t * );
103 static void Close ( vlc_object_t * );
105 static void Play ( aout_instance_t * );
106 static void Probe ( aout_instance_t * );
108 static int AudioDeviceHasOutput ( AudioDeviceID );
109 static int AudioDeviceSupportsDigital( aout_instance_t *, AudioDeviceID );
110 static int AudioStreamSupportsDigital( aout_instance_t *, AudioStreamID );
111 static int AudioStreamChangeFormat ( aout_instance_t *, AudioStreamID, AudioStreamBasicDescription );
113 static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
114 unsigned int, unsigned int, AudioBufferList *);
115 static OSStatus RenderCallbackSPDIF ( AudioDeviceID, const AudioTimeStamp *, const void *, const AudioTimeStamp *,
116 AudioBufferList *, const AudioTimeStamp *, void * );
117 static OSStatus HardwareListener ( AudioHardwarePropertyID, void *);
118 static OSStatus StreamListener ( AudioStreamID, UInt32,
119 AudioDevicePropertyID, void * );
120 static int AudioDeviceCallback ( vlc_object_t *, const char *,
121 vlc_value_t, vlc_value_t, void * );
124 /*****************************************************************************
126 *****************************************************************************/
127 #define ADEV_TEXT N_("Audio Device")
128 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
129 "audio device, as listed in your 'Audio Device' menu. This device will " \
130 "then be used by default for audio playback.")
133 set_shortname( "auhal" );
134 set_description( _("HAL AudioUnit output") );
135 set_capability( "audio output", 101 );
136 set_category( CAT_AUDIO );
137 set_subcategory( SUBCAT_AUDIO_AOUT );
138 set_callbacks( Open, Close );
139 add_integer( "macosx-audio-device", 0, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE );
142 /*****************************************************************************
143 * Open: open macosx audio output
144 *****************************************************************************/
145 static int Open( vlc_object_t * p_this )
147 OSStatus err = noErr;
148 UInt32 i_param_size = 0;
149 struct aout_sys_t *p_sys = NULL;
150 vlc_bool_t b_alive = VLC_FALSE;
152 aout_instance_t *p_aout = (aout_instance_t *)p_this;
154 /* Allocate structure */
155 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
156 if( p_aout->output.p_sys == NULL )
158 msg_Err( p_aout, "out of memory" );
159 return( VLC_ENOMEM );
162 p_sys = p_aout->output.p_sys;
163 p_sys->i_default_dev = 0;
164 p_sys->i_selected_dev = 0;
165 p_sys->i_devices = 0;
166 p_sys->b_supports_digital = VLC_FALSE;
167 p_sys->b_digital = VLC_FALSE;
168 p_sys->au_component = NULL;
169 p_sys->au_unit = NULL;
170 p_sys->clock_diff = (mtime_t) 0;
171 p_sys->i_read_bytes = 0;
172 p_sys->i_total_bytes = 0;
173 p_sys->i_hog_pid = -1;
174 p_sys->i_stream_id = 0;
175 p_sys->i_stream_index = -1;
176 p_sys->b_revert = VLC_FALSE;
177 p_sys->b_changed_mixing = VLC_FALSE;
178 memset( p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE );
180 p_aout->output.pf_play = Play;
182 aout_FormatPrint( p_aout, "VLC is looking for:", (audio_sample_format_t *)&p_aout->output.output );
184 /* Persistent device variable */
185 if( var_Type( p_aout->p_libvlc, "macosx-audio-device" ) == 0 )
187 var_Create( p_aout->p_libvlc, "macosx-audio-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
190 /* Build a list of devices */
191 if( var_Type( p_aout, "audio-device" ) == 0 )
196 /* What device do we want? */
197 if( var_Get( p_aout, "audio-device", &val ) < 0 )
199 msg_Err( p_aout, "audio-device var does not exist. device probe failed." );
203 p_sys->i_selected_dev = val.i_int & ~AOUT_VAR_SPDIF_FLAG; /* remove SPDIF flag to get the true DeviceID */
204 p_sys->b_supports_digital = ( val.i_int & AOUT_VAR_SPDIF_FLAG ) ? VLC_TRUE : VLC_FALSE;
206 /* Check if the desired device is alive and usable */
207 /* TODO: add a callback to the device to alert us if the device dies */
208 i_param_size = sizeof( b_alive );
209 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
210 kAudioDevicePropertyDeviceIsAlive,
211 &i_param_size, &b_alive );
215 msg_Err( p_aout, "could not check whether device is alive: %4.4s", (char *)&err );
219 if( b_alive == VLC_FALSE )
221 msg_Warn( p_aout, "selected audio device is not alive, switching to default device" );
222 p_sys->i_selected_dev = p_sys->i_default_dev;
225 i_param_size = sizeof( p_sys->i_hog_pid );
226 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
227 kAudioDevicePropertyHogMode,
228 &i_param_size, &p_sys->i_hog_pid );
232 /* This is not a fatal error. Some drivers simply don't support this property */
233 msg_Warn( p_aout, "could not check whether device is hogged: %4.4s",
235 p_sys->i_hog_pid = -1;
238 if( p_sys->i_hog_pid != -1 && p_sys->i_hog_pid != getpid() )
240 msg_Err( p_aout, "Selected audio device is exclusively in use by another program." );
241 intf_UserFatal( p_aout, VLC_FALSE, _("Audio output failed"),
242 _("The selected audio output device is exclusively in "
243 "use by another program.") );
247 /* Check for Digital mode or Analog output mode */
248 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
250 if( OpenSPDIF( p_aout ) )
255 if( OpenAnalog( p_aout ) )
260 /* If we reach this, this aout has failed */
261 var_Destroy( p_aout, "audio-device" );
262 if( p_sys ) free( p_sys );
266 /*****************************************************************************
267 * Open: open and setup a HAL AudioUnit to do analog (multichannel) audio output
268 *****************************************************************************/
269 static int OpenAnalog( aout_instance_t *p_aout )
271 struct aout_sys_t *p_sys = p_aout->output.p_sys;
272 OSStatus err = noErr;
273 UInt32 i_param_size = 0, i = 0;
275 ComponentDescription desc;
276 AudioStreamBasicDescription DeviceFormat;
277 AudioChannelLayout *layout;
278 AudioChannelLayout new_layout;
279 AURenderCallbackStruct input;
281 /* Lets go find our Component */
282 desc.componentType = kAudioUnitType_Output;
283 desc.componentSubType = kAudioUnitSubType_HALOutput;
284 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
285 desc.componentFlags = 0;
286 desc.componentFlagsMask = 0;
288 p_sys->au_component = FindNextComponent( NULL, &desc );
289 if( p_sys->au_component == NULL )
291 msg_Warn( p_aout, "we cannot find our HAL component" );
295 err = OpenAComponent( p_sys->au_component, &p_sys->au_unit );
298 msg_Warn( p_aout, "we cannot open our HAL component" );
302 /* Set the device we will use for this output unit */
303 err = AudioUnitSetProperty( p_sys->au_unit,
304 kAudioOutputUnitProperty_CurrentDevice,
305 kAudioUnitScope_Global,
307 &p_sys->i_selected_dev,
308 sizeof( AudioDeviceID ));
312 msg_Warn( p_aout, "we cannot select the audio device" );
316 /* Get the current format */
317 i_param_size = sizeof(AudioStreamBasicDescription);
319 err = AudioUnitGetProperty( p_sys->au_unit,
320 kAudioUnitProperty_StreamFormat,
321 kAudioUnitScope_Input,
326 if( err != noErr ) return VLC_FALSE;
327 else msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is: ", DeviceFormat ) );
329 /* Get the channel layout of the device side of the unit (vlc -> unit -> device) */
330 err = AudioUnitGetPropertyInfo( p_sys->au_unit,
331 kAudioDevicePropertyPreferredChannelLayout,
332 kAudioUnitScope_Output,
339 layout = (AudioChannelLayout *)malloc( i_param_size);
341 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
342 kAudioDevicePropertyPreferredChannelLayout,
343 kAudioUnitScope_Output,
348 /* We need to "fill out" the ChannelLayout, because there are multiple ways that it can be set */
349 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
351 /* bitmap defined channellayout */
352 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
353 sizeof( UInt32), &layout->mChannelBitmap,
357 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
359 /* layouttags defined channellayout */
360 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
361 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
366 msg_Dbg( p_aout, "layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
368 /* Initialize the VLC core channel count */
369 p_aout->output.output.i_physical_channels = 0;
370 i_original = p_aout->output.output.i_original_channels & AOUT_CHAN_PHYSMASK;
372 if( i_original == AOUT_CHAN_CENTER || layout->mNumberChannelDescriptions < 2 )
374 /* We only need Mono or cannot output more than 1 channel */
375 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
377 else if( i_original == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) || layout->mNumberChannelDescriptions < 3 )
379 /* We only need Stereo or cannot output more than 2 channels */
380 p_aout->output.output.i_physical_channels = AOUT_CHAN_RIGHT | AOUT_CHAN_LEFT;
384 /* We want more than stereo and we can do that */
385 for( i = 0; i < layout->mNumberChannelDescriptions; i++ )
387 msg_Dbg( p_aout, "this is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
389 switch( layout->mChannelDescriptions[i].mChannelLabel )
391 case kAudioChannelLabel_Left:
392 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
394 case kAudioChannelLabel_Right:
395 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
397 case kAudioChannelLabel_Center:
398 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
400 case kAudioChannelLabel_LFEScreen:
401 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE;
403 case kAudioChannelLabel_LeftSurround:
404 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT;
406 case kAudioChannelLabel_RightSurround:
407 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
409 case kAudioChannelLabel_RearSurroundLeft:
410 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
412 case kAudioChannelLabel_RearSurroundRight:
413 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
415 case kAudioChannelLabel_CenterSurround:
416 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER;
419 msg_Warn( p_aout, "unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
422 if( p_aout->output.output.i_physical_channels == 0 )
424 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
425 msg_Err( p_aout, "You should configure your speaker layout with Audio Midi Setup Utility in /Applications/Utilities. Now using Stereo mode." );
426 intf_UserFatal( p_aout, VLC_FALSE, _("Audio device is not configured"),
427 _("You should configure your speaker layout with "
428 "the \"Audio Midi Setup Utility\" in /Applications/"
429 "Utilities. Stereo mode is being used now.") );
432 if( layout ) free( layout );
436 msg_Warn( p_aout, "this driver does not support kAudioDevicePropertyPreferredChannelLayout. BAD DRIVER AUTHOR !!!" );
437 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
440 msg_Dbg( p_aout, "selected %d physical channels for device output", aout_FormatNbChannels( &p_aout->output.output ) );
441 msg_Dbg( p_aout, "VLC will output: %s", aout_FormatPrintChannels( &p_aout->output.output ));
443 memset (&new_layout, 0, sizeof(new_layout));
444 switch( aout_FormatNbChannels( &p_aout->output.output ) )
447 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
450 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
453 if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
455 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
457 else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
459 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
463 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
465 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
467 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
469 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
471 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
473 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
477 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
479 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
481 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
483 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
487 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
489 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
493 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_6_0; // L R Ls Rs C Cs
497 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
498 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
501 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
502 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
506 /* Set up the format to be used */
507 DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
508 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
510 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
511 p_aout->output.output.i_format = VLC_FOURCC( 'f','l','3','2');
512 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
513 DeviceFormat.mBitsPerChannel = 32;
514 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output );
516 /* Calculate framesizes and stuff */
517 DeviceFormat.mFramesPerPacket = 1;
518 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
519 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
521 /* Set the desired format */
522 i_param_size = sizeof(AudioStreamBasicDescription);
523 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
524 kAudioUnitProperty_StreamFormat,
525 kAudioUnitScope_Input,
530 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
532 /* Retrieve actual format */
533 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
534 kAudioUnitProperty_StreamFormat,
535 kAudioUnitScope_Input,
540 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
542 /* Do the last VLC aout setups */
543 aout_FormatPrepare( &p_aout->output.output );
544 p_aout->output.i_nb_samples = 2048;
545 aout_VolumeSoftInit( p_aout );
547 /* set the IOproc callback */
548 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
549 input.inputProcRefCon = p_aout;
551 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
552 kAudioUnitProperty_SetRenderCallback,
553 kAudioUnitScope_Input,
554 0, &input, sizeof( input ) ) );
556 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
557 input.inputProcRefCon = p_aout;
559 /* Set the new_layout as the layout VLC will use to feed the AU unit */
560 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
561 kAudioUnitProperty_AudioChannelLayout,
562 kAudioUnitScope_Input,
563 0, &new_layout, sizeof(new_layout) ) );
565 if( new_layout.mNumberChannelDescriptions > 0 )
566 free( new_layout.mChannelDescriptions );
569 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
571 /* Find the difference between device clock and mdate clock */
572 p_sys->clock_diff = - (mtime_t)
573 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
574 p_sys->clock_diff += mdate();
577 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
582 /*****************************************************************************
583 * Setup a encoded digital stream (SPDIF)
584 *****************************************************************************/
585 static int OpenSPDIF( aout_instance_t * p_aout )
587 struct aout_sys_t *p_sys = p_aout->output.p_sys;
588 OSStatus err = noErr;
589 UInt32 i_param_size = 0, b_mix = 0;
590 Boolean b_writeable = VLC_FALSE;
591 AudioStreamID *p_streams = NULL;
592 int i = 0, i_streams = 0;
594 /* Start doing the SPDIF setup proces */
595 p_sys->b_digital = VLC_TRUE;
598 i_param_size = sizeof( p_sys->i_hog_pid );
599 p_sys->i_hog_pid = getpid() ;
601 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
602 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
606 msg_Err( p_aout, "failed to set hogmode: [%4.4s]", (char *)&err );
610 /* Set mixable to false if we are allowed to */
611 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
612 &i_param_size, &b_writeable );
614 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
615 &i_param_size, &b_mix );
617 if( !err && b_writeable )
620 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
621 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
622 p_sys->b_changed_mixing = VLC_TRUE;
627 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
631 /* Get a list of all the streams on this device */
632 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE,
633 kAudioDevicePropertyStreams,
634 &i_param_size, NULL );
637 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
641 i_streams = i_param_size / sizeof( AudioStreamID );
642 p_streams = (AudioStreamID *)malloc( i_param_size );
643 if( p_streams == NULL )
645 msg_Err( p_aout, "out of memory" );
649 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
650 kAudioDevicePropertyStreams,
651 &i_param_size, p_streams );
655 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
656 if( p_streams ) free( p_streams );
660 for( i = 0; i < i_streams && p_sys->i_stream_index < 0 ; i++ )
662 /* Find a stream with a cac3 stream */
663 AudioStreamBasicDescription *p_format_list = NULL;
664 int i_formats = 0, j = 0;
665 vlc_bool_t b_digital = VLC_FALSE;
667 /* Retrieve all the stream formats supported by each output stream */
668 err = AudioStreamGetPropertyInfo( p_streams[i], 0,
669 kAudioStreamPropertyPhysicalFormats,
670 &i_param_size, NULL );
673 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
677 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
678 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
679 if( p_format_list == NULL )
681 msg_Err( p_aout, "could not malloc the memory" );
685 err = AudioStreamGetProperty( p_streams[i], 0,
686 kAudioStreamPropertyPhysicalFormats,
687 &i_param_size, p_format_list );
690 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
691 if( p_format_list) free( p_format_list);
695 /* Check if one of the supported formats is a digital format */
696 for( j = 0; j < i_formats; j++ )
698 if( p_format_list[j].mFormatID == 'IAC3' ||
699 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
701 b_digital = VLC_TRUE;
708 /* if this stream supports a digital (cac3) format, then go set it. */
709 int i_requested_rate_format = -1;
710 int i_current_rate_format = -1;
711 int i_backup_rate_format = -1;
713 p_sys->i_stream_id = p_streams[i];
714 p_sys->i_stream_index = i;
716 if( p_sys->b_revert == VLC_FALSE )
718 /* Retrieve the original format of this stream first if not done so already */
719 i_param_size = sizeof( p_sys->sfmt_revert );
720 err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
721 kAudioStreamPropertyPhysicalFormat,
723 &p_sys->sfmt_revert );
726 msg_Err( p_aout, "could not retrieve the original streamformat: [%4.4s]", (char *)&err );
729 p_sys->b_revert = VLC_TRUE;
732 for( j = 0; j < i_formats; j++ )
734 if( p_format_list[j].mFormatID == 'IAC3' ||
735 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
737 if( p_format_list[j].mSampleRate == p_aout->output.output.i_rate )
739 i_requested_rate_format = j;
742 else if( p_format_list[j].mSampleRate == p_sys->sfmt_revert.mSampleRate )
744 i_current_rate_format = j;
748 if( i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate )
749 i_backup_rate_format = j;
755 if( i_requested_rate_format >= 0 ) /* We prefer to output at the samplerate of the original audio */
756 p_sys->stream_format = p_format_list[i_requested_rate_format];
757 else if( i_current_rate_format >= 0 ) /* If not possible, we will try to use the current samplerate of the device */
758 p_sys->stream_format = p_format_list[i_current_rate_format];
759 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) */
761 if( p_format_list ) free( p_format_list );
763 if( p_streams ) free( p_streams );
765 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) );
767 if( !AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->stream_format ) )
770 /* Set the format flags */
771 if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian )
772 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','b');
774 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
775 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
776 p_aout->output.output.i_frame_length = A52_FRAME_NB;
777 p_aout->output.i_nb_samples = p_aout->output.output.i_frame_length;
778 p_aout->output.output.i_rate = (unsigned int)p_sys->stream_format.mSampleRate;
779 aout_FormatPrepare( &p_aout->output.output );
780 aout_VolumeNoneInit( p_aout );
782 /* Add IOProc callback */
783 err = AudioDeviceAddIOProc( p_sys->i_selected_dev,
784 (AudioDeviceIOProc)RenderCallbackSPDIF,
788 msg_Err( p_aout, "AudioDeviceAddIOProc failed: [%4.4s]", (char *)&err );
792 /* Check for the difference between the Device clock and mdate */
793 p_sys->clock_diff = - (mtime_t)
794 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
795 p_sys->clock_diff += mdate();
798 err = AudioDeviceStart( p_sys->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF );
801 msg_Err( p_aout, "AudioDeviceStart failed: [%4.4s]", (char *)&err );
803 err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
804 (AudioDeviceIOProc)RenderCallbackSPDIF );
807 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
816 /*****************************************************************************
817 * Close: Close HAL AudioUnit
818 *****************************************************************************/
819 static void Close( vlc_object_t * p_this )
821 aout_instance_t *p_aout = (aout_instance_t *)p_this;
822 struct aout_sys_t *p_sys = p_aout->output.p_sys;
823 OSStatus err = noErr;
824 UInt32 i_param_size = 0;
828 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
829 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
830 verify_noerr( CloseComponent( p_sys->au_unit ) );
833 if( p_sys->b_digital )
836 err = AudioDeviceStop( p_sys->i_selected_dev,
837 (AudioDeviceIOProc)RenderCallbackSPDIF );
840 msg_Err( p_aout, "AudioDeviceStop failed: [%4.4s]", (char *)&err );
843 /* Remove IOProc callback */
844 err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
845 (AudioDeviceIOProc)RenderCallbackSPDIF );
848 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
851 if( p_sys->b_revert )
853 AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->sfmt_revert );
856 if( p_sys->b_changed_mixing && p_sys->sfmt_revert.mFormatID != kAudioFormat60958AC3 )
860 /* Revert mixable to true if we are allowed to */
861 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
862 &i_param_size, &b_writeable );
864 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
865 &i_param_size, &b_mix );
867 if( !err && b_writeable )
869 msg_Dbg( p_aout, "mixable is: %d", b_mix );
871 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
872 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
877 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
882 err = AudioHardwareRemovePropertyListener( kAudioHardwarePropertyDevices,
887 msg_Err( p_aout, "AudioHardwareRemovePropertyListener failed: [%4.4s]", (char *)&err );
890 if( p_sys->i_hog_pid == getpid() )
892 p_sys->i_hog_pid = -1;
893 i_param_size = sizeof( p_sys->i_hog_pid );
894 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
895 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
896 if( err != noErr ) msg_Err( p_aout, "Could not release hogmode: [%4.4s]", (char *)&err );
899 if( p_sys ) free( p_sys );
902 /*****************************************************************************
903 * Play: nothing to do
904 *****************************************************************************/
905 static void Play( aout_instance_t * p_aout )
910 /*****************************************************************************
911 * Probe: Check which devices the OS has, and add them to our audio-device menu
912 *****************************************************************************/
913 static void Probe( aout_instance_t * p_aout )
915 OSStatus err = noErr;
916 UInt32 i = 0, i_param_size = 0;
917 AudioDeviceID devid_def = 0;
918 AudioDeviceID *p_devices = NULL;
919 vlc_value_t val, text;
921 struct aout_sys_t *p_sys = p_aout->output.p_sys;
923 /* Get number of devices */
924 err = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices,
925 &i_param_size, NULL );
928 msg_Err( p_aout, "Could not get number of devices: [%4.4s]", (char *)&err );
932 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
934 if( p_sys->i_devices < 1 )
936 msg_Err( p_aout, "No audio output devices were found." );
940 msg_Dbg( p_aout, "system has [%ld] device(s)", p_sys->i_devices );
942 /* Allocate DeviceID array */
943 p_devices = (AudioDeviceID*)malloc( sizeof(AudioDeviceID) * p_sys->i_devices );
944 if( p_devices == NULL )
946 msg_Err( p_aout, "out of memory" );
950 /* Populate DeviceID array */
951 err = AudioHardwareGetProperty( kAudioHardwarePropertyDevices,
952 &i_param_size, p_devices );
955 msg_Err( p_aout, "could not get the device IDs: [%4.4s]", (char *)&err );
959 /* Find the ID of the default Device */
960 i_param_size = sizeof( AudioDeviceID );
961 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
962 &i_param_size, &devid_def );
965 msg_Err( p_aout, "could not get default audio device: [%4.4s]", (char *)&err );
968 p_sys->i_default_dev = devid_def;
970 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
971 text.psz_string = _("Audio Device");
972 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
974 for( i = 0; i < p_sys->i_devices; i++ )
979 /* Retrieve the length of the device name */
980 err = AudioDeviceGetPropertyInfo(
981 p_devices[i], 0, VLC_FALSE,
982 kAudioDevicePropertyDeviceName,
983 &i_param_size, NULL);
984 if( err ) goto error;
986 /* Retrieve the name of the device */
987 psz_name = (char *)malloc( i_param_size );
988 err = AudioDeviceGetProperty(
989 p_devices[i], 0, VLC_FALSE,
990 kAudioDevicePropertyDeviceName,
991 &i_param_size, psz_name);
992 if( err ) goto error;
994 msg_Dbg( p_aout, "DevID: %#lx DevName: %s", p_devices[i], psz_name );
996 if( !AudioDeviceHasOutput( p_devices[i]) )
998 msg_Dbg( p_aout, "this device is INPUT only. skipping..." );
1002 /* Add the menu entries */
1003 val.i_int = (int)p_devices[i];
1004 text.psz_string = strdup( psz_name );
1005 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1006 if( p_sys->i_default_dev == p_devices[i] )
1008 /* The default device is the selected device normally */
1009 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1010 var_Set( p_aout, "audio-device", val );
1013 if( AudioDeviceSupportsDigital( p_aout, p_devices[i] ) )
1015 val.i_int = (int)p_devices[i] | AOUT_VAR_SPDIF_FLAG;
1016 asprintf( &text.psz_string, _("%s (Encoded Output)"), psz_name );
1017 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1018 if( p_sys->i_default_dev == p_devices[i] && config_GetInt( p_aout, "spdif" ) )
1020 /* We selected to prefer SPDIF output if available
1021 * then this "dummy" entry should be selected */
1022 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1023 var_Set( p_aout, "audio-device", val );
1030 /* If a device is already "preselected", then use this device */
1031 var_Get( p_aout->p_libvlc, "macosx-audio-device", &val );
1034 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1035 var_Set( p_aout, "audio-device", val );
1038 /* If we change the device we want to use, we should renegotiate the audio chain */
1039 var_AddCallback( p_aout, "audio-device", AudioDeviceCallback, NULL );
1041 /* Attach a Listener so that we are notified of a change in the Device setup */
1042 err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
1048 if( p_devices ) free( p_devices );
1052 var_Destroy( p_aout, "audio-device" );
1053 if( p_devices ) free( p_devices );
1057 /*****************************************************************************
1058 * AudioDeviceHasOutput: Checks if the Device actually provides any outputs at all
1059 *****************************************************************************/
1060 static int AudioDeviceHasOutput( AudioDeviceID i_dev_id )
1065 verify_noerr( AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE, kAudioDevicePropertyStreams, &dataSize, &isWritable) );
1066 if (dataSize == 0) return FALSE;
1071 /*****************************************************************************
1072 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
1073 *****************************************************************************/
1074 static int AudioDeviceSupportsDigital( aout_instance_t *p_aout, AudioDeviceID i_dev_id )
1076 OSStatus err = noErr;
1077 UInt32 i_param_size = 0;
1078 AudioStreamID *p_streams = NULL;
1079 int i = 0, i_streams = 0;
1080 vlc_bool_t b_return = VLC_FALSE;
1082 /* Retrieve all the output streams */
1083 err = AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE,
1084 kAudioDevicePropertyStreams,
1085 &i_param_size, NULL );
1088 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1092 i_streams = i_param_size / sizeof( AudioStreamID );
1093 p_streams = (AudioStreamID *)malloc( i_param_size );
1094 if( p_streams == NULL )
1096 msg_Err( p_aout, "out of memory" );
1100 err = AudioDeviceGetProperty( i_dev_id, 0, FALSE,
1101 kAudioDevicePropertyStreams,
1102 &i_param_size, p_streams );
1106 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1110 for( i = 0; i < i_streams; i++ )
1112 if( AudioStreamSupportsDigital( p_aout, p_streams[i] ) )
1113 b_return = VLC_TRUE;
1116 if( p_streams ) free( p_streams );
1120 /*****************************************************************************
1121 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
1122 *****************************************************************************/
1123 static int AudioStreamSupportsDigital( aout_instance_t *p_aout, AudioStreamID i_stream_id )
1125 OSStatus err = noErr;
1126 UInt32 i_param_size = 0;
1127 AudioStreamBasicDescription *p_format_list = NULL;
1128 int i = 0, i_formats = 0;
1129 vlc_bool_t b_return = VLC_FALSE;
1131 /* Retrieve all the stream formats supported by each output stream */
1132 err = AudioStreamGetPropertyInfo( i_stream_id, 0,
1133 kAudioStreamPropertyPhysicalFormats,
1134 &i_param_size, NULL );
1137 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
1141 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
1142 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
1143 if( p_format_list == NULL )
1145 msg_Err( p_aout, "could not malloc the memory" );
1149 err = AudioStreamGetProperty( i_stream_id, 0,
1150 kAudioStreamPropertyPhysicalFormats,
1151 &i_param_size, p_format_list );
1154 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
1155 free( p_format_list);
1156 p_format_list = NULL;
1160 for( i = 0; i < i_formats; i++ )
1162 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format: ", p_format_list[i] ) );
1164 if( p_format_list[i].mFormatID == 'IAC3' ||
1165 p_format_list[i].mFormatID == kAudioFormat60958AC3 )
1167 b_return = VLC_TRUE;
1171 if( p_format_list ) free( p_format_list );
1175 /*****************************************************************************
1176 * AudioStreamChangeFormat: Change i_stream_id to change_format
1177 *****************************************************************************/
1178 static int AudioStreamChangeFormat( aout_instance_t *p_aout, AudioStreamID i_stream_id, AudioStreamBasicDescription change_format )
1180 OSStatus err = noErr;
1181 UInt32 i_param_size = 0;
1185 struct timespec timeout;
1186 struct { vlc_mutex_t lock; vlc_cond_t cond; } w;
1188 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "setting stream format: ", change_format ) );
1190 /* Condition because SetProperty is asynchronious */
1191 vlc_cond_init( p_aout, &w.cond );
1192 vlc_mutex_init( p_aout, &w.lock );
1193 vlc_mutex_lock( &w.lock );
1195 /* Install the callback */
1196 err = AudioStreamAddPropertyListener( i_stream_id, 0,
1197 kAudioStreamPropertyPhysicalFormat,
1198 StreamListener, (void *)&w );
1201 msg_Err( p_aout, "AudioStreamAddPropertyListener failed: [%4.4s]", (char *)&err );
1205 /* change the format */
1206 err = AudioStreamSetProperty( i_stream_id, 0, 0,
1207 kAudioStreamPropertyPhysicalFormat,
1208 sizeof( AudioStreamBasicDescription ),
1212 msg_Err( p_aout, "could not set the stream format: [%4.4s]", (char *)&err );
1216 /* The AudioStreamSetProperty is not only asynchronious (requiring the locks)
1217 * it is also not atomic in its behaviour.
1218 * Therefore we check 5 times before we really give up.
1219 * FIXME: failing isn't actually implemented yet. */
1220 for( i = 0; i < 5; i++ )
1222 AudioStreamBasicDescription actual_format;
1224 gettimeofday( &now, NULL );
1225 timeout.tv_sec = now.tv_sec;
1226 timeout.tv_nsec = (now.tv_usec + 500000) * 1000;
1228 if( pthread_cond_timedwait( &w.cond.cond, &w.lock.mutex, &timeout ) )
1230 msg_Dbg( p_aout, "reached timeout" );
1233 i_param_size = sizeof( AudioStreamBasicDescription );
1234 err = AudioStreamGetProperty( i_stream_id, 0,
1235 kAudioStreamPropertyPhysicalFormat,
1239 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "actual format in use: ", actual_format ) );
1240 if( actual_format.mSampleRate == change_format.mSampleRate &&
1241 actual_format.mFormatID == change_format.mFormatID &&
1242 actual_format.mFramesPerPacket == change_format.mFramesPerPacket )
1244 /* The right format is now active */
1247 /* We need to check again */
1250 /* Removing the property listener */
1251 err = AudioStreamRemovePropertyListener( i_stream_id, 0,
1252 kAudioStreamPropertyPhysicalFormat,
1256 msg_Err( p_aout, "AudioStreamRemovePropertyListener failed: [%4.4s]", (char *)&err );
1260 /* Destroy the lock and condition */
1261 vlc_mutex_unlock( &w.lock );
1262 vlc_mutex_destroy( &w.lock );
1263 vlc_cond_destroy( &w.cond );
1268 /*****************************************************************************
1269 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
1270 * us to provide some more audio data.
1271 * Don't print anything during normal playback, calling blocking function from
1272 * this callback is not allowed.
1273 *****************************************************************************/
1274 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
1275 AudioUnitRenderActionFlags *ioActionFlags,
1276 const AudioTimeStamp *inTimeStamp,
1277 unsigned int inBusNummer,
1278 unsigned int inNumberFrames,
1279 AudioBufferList *ioData )
1281 AudioTimeStamp host_time;
1282 mtime_t current_date = 0;
1283 uint32_t i_mData_bytes = 0;
1285 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
1286 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1288 host_time.mFlags = kAudioTimeStampHostTimeValid;
1289 AudioDeviceTranslateTime( p_sys->i_selected_dev, inTimeStamp, &host_time );
1291 /* Check for the difference between the Device clock and mdate */
1292 p_sys->clock_diff = - (mtime_t)
1293 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
1294 p_sys->clock_diff += mdate();
1296 current_date = p_sys->clock_diff +
1297 AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
1298 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1300 if( ioData == NULL && ioData->mNumberBuffers < 1 )
1302 msg_Err( p_aout, "no iodata or buffers");
1305 if( ioData->mNumberBuffers > 1 )
1306 msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
1309 if( p_sys->i_total_bytes > 0 )
1311 i_mData_bytes = __MIN( p_sys->i_total_bytes - p_sys->i_read_bytes, ioData->mBuffers[0].mDataByteSize );
1312 p_aout->p_libvlc->pf_memcpy( ioData->mBuffers[0].mData, &p_sys->p_remainder_buffer[p_sys->i_read_bytes], i_mData_bytes );
1313 p_sys->i_read_bytes += i_mData_bytes;
1314 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1315 ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1317 if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
1318 p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
1321 while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
1323 /* We don't have enough data yet */
1324 aout_buffer_t * p_buffer;
1325 p_buffer = aout_OutputNextBuffer( p_aout, current_date , VLC_FALSE );
1327 if( p_buffer != NULL )
1329 uint32_t i_second_mData_bytes = __MIN( p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1331 p_aout->p_libvlc->pf_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes, p_buffer->p_buffer, i_second_mData_bytes );
1332 i_mData_bytes += i_second_mData_bytes;
1334 if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
1336 p_sys->i_total_bytes = p_buffer->i_nb_bytes - i_second_mData_bytes;
1337 p_aout->p_libvlc->pf_memcpy( p_sys->p_remainder_buffer, &p_buffer->p_buffer[i_second_mData_bytes], p_sys->i_total_bytes );
1341 /* update current_date */
1342 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1343 ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1345 aout_BufferFree( p_buffer );
1349 p_aout->p_libvlc->pf_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes, 0, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1350 i_mData_bytes += ioData->mBuffers[0].mDataByteSize - i_mData_bytes;
1356 /*****************************************************************************
1357 * RenderCallbackSPDIF: callback for SPDIF audio output
1358 *****************************************************************************/
1359 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice,
1360 const AudioTimeStamp * inNow,
1361 const void * inInputData,
1362 const AudioTimeStamp * inInputTime,
1363 AudioBufferList * outOutputData,
1364 const AudioTimeStamp * inOutputTime,
1365 void * threadGlobals )
1367 aout_buffer_t * p_buffer;
1368 mtime_t current_date;
1370 aout_instance_t * p_aout = (aout_instance_t *)threadGlobals;
1371 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1373 /* Check for the difference between the Device clock and mdate */
1374 p_sys->clock_diff = - (mtime_t)
1375 AudioConvertHostTimeToNanos( inNow->mHostTime ) / 1000;
1376 p_sys->clock_diff += mdate();
1378 current_date = p_sys->clock_diff +
1379 AudioConvertHostTimeToNanos( inOutputTime->mHostTime ) / 1000;
1380 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1382 p_buffer = aout_OutputNextBuffer( p_aout, current_date, VLC_TRUE );
1384 #define BUFFER outOutputData->mBuffers[p_sys->i_stream_index]
1385 if( p_buffer != NULL )
1387 if( (int)BUFFER.mDataByteSize != (int)p_buffer->i_nb_bytes)
1388 msg_Warn( p_aout, "bytesize: %d nb_bytes: %d", (int)BUFFER.mDataByteSize, (int)p_buffer->i_nb_bytes );
1390 /* move data into output data buffer */
1391 p_aout->p_libvlc->pf_memcpy( BUFFER.mData,
1392 p_buffer->p_buffer, p_buffer->i_nb_bytes );
1393 aout_BufferFree( p_buffer );
1397 p_aout->p_libvlc->pf_memset( BUFFER.mData, 0, BUFFER.mDataByteSize );
1404 /*****************************************************************************
1405 * HardwareListener: Warns us of changes in the list of registered devices
1406 *****************************************************************************/
1407 static OSStatus HardwareListener( AudioHardwarePropertyID inPropertyID,
1408 void * inClientData )
1410 OSStatus err = noErr;
1411 aout_instance_t *p_aout = (aout_instance_t *)inClientData;
1413 switch( inPropertyID )
1415 case kAudioHardwarePropertyDevices:
1417 /* something changed in the list of devices */
1418 /* We trigger the audio-device's aout_ChannelsRestart callback */
1419 var_Change( p_aout, "audio-device", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
1420 var_Destroy( p_aout, "audio-device" );
1428 /*****************************************************************************
1430 *****************************************************************************/
1431 static OSStatus StreamListener( AudioStreamID inStream,
1433 AudioDevicePropertyID inPropertyID,
1434 void * inClientData )
1436 OSStatus err = noErr;
1437 struct { vlc_mutex_t lock; vlc_cond_t cond; } * w = inClientData;
1439 switch( inPropertyID )
1441 case kAudioStreamPropertyPhysicalFormat:
1442 vlc_mutex_lock( &w->lock );
1443 vlc_cond_signal( &w->cond );
1444 vlc_mutex_unlock( &w->lock );
1453 /*****************************************************************************
1454 * AudioDeviceCallback: Callback triggered when the audio-device variable is changed
1455 *****************************************************************************/
1456 static int AudioDeviceCallback( vlc_object_t *p_this, const char *psz_variable,
1457 vlc_value_t old_val, vlc_value_t new_val, void *param )
1459 aout_instance_t *p_aout = (aout_instance_t *)p_this;
1460 var_Set( p_aout->p_libvlc, "macosx-audio-device", new_val );
1461 msg_Dbg( p_aout, "Set Device: %#x", new_val.i_int );
1462 return aout_ChannelsRestart( p_this, psz_variable, old_val, new_val, param );