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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_dialog.h>
38 // By pass part of header which compile with some warnings,
39 // and that we don't require.
40 #define __MACHINEEXCEPTIONS__
42 #include <CoreAudio/CoreAudio.h>
43 #include <AudioUnit/AudioUnitProperties.h>
44 #include <AudioUnit/AudioUnitParameters.h>
45 #include <AudioUnit/AudioOutputUnit.h>
46 #include <AudioToolbox/AudioFormat.h>
48 #define STREAM_FORMAT_MSG( pre, sfm ) \
49 pre "[%ld][%4.4s][%ld][%ld][%ld][%ld][%ld][%ld]", \
50 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
51 sfm.mFormatFlags, sfm.mBytesPerPacket, \
52 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
53 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
55 #define STREAM_FORMAT_MSG_FULL( pre, sfm ) \
56 pre ":\nsamplerate: [%ld]\nFormatID: [%4.4s]\nFormatFlags: [%ld]\nBypesPerPacket: [%ld]\nFramesPerPacket: [%ld]\nBytesPerFrame: [%ld]\nChannelsPerFrame: [%ld]\nBitsPerChannel[%ld]", \
57 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
58 sfm.mFormatFlags, sfm.mBytesPerPacket, \
59 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
60 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
62 #define BUFSIZE 0xffffff
63 #define AOUT_VAR_SPDIF_FLAG 0xf00000
67 * - clean up the debug info
69 * - be better at changing stream setup or devices setup changes while playing.
73 /*****************************************************************************
74 * aout_sys_t: private audio output method descriptor
75 *****************************************************************************
76 * This structure is part of the audio output thread descriptor.
77 * It describes the CoreAudio specific properties of an output thread.
78 *****************************************************************************/
81 AudioDeviceID i_default_dev; /* Keeps DeviceID of defaultOutputDevice */
82 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device */
83 UInt32 i_devices; /* Number of CoreAudio Devices */
84 bool b_supports_digital;/* Does the currently selected device support digital mode? */
85 bool b_digital; /* Are we running in digital mode? */
86 mtime_t clock_diff; /* Difference between VLC clock and Device clock */
89 Component au_component; /* The Audiocomponent we use */
90 AudioUnit au_unit; /* The AudioUnit we use */
91 uint8_t p_remainder_buffer[BUFSIZE];
92 uint32_t i_read_bytes;
93 uint32_t i_total_bytes;
95 /* CoreAudio SPDIF mode specific */
96 pid_t i_hog_pid; /* The keep the pid of our hog status */
97 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */
98 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */
99 AudioStreamBasicDescription stream_format; /* The format we changed the stream to */
100 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */
101 bool b_revert; /* Wether we need to revert the stream format */
102 bool b_changed_mixing;/* Wether we need to set the mixing mode back */
105 /*****************************************************************************
107 *****************************************************************************/
108 static int Open ( vlc_object_t * );
109 static int OpenAnalog ( aout_instance_t * );
110 static int OpenSPDIF ( aout_instance_t * );
111 static void Close ( vlc_object_t * );
113 static void Play ( aout_instance_t * );
114 static void Probe ( aout_instance_t * );
116 static int AudioDeviceHasOutput ( AudioDeviceID );
117 static int AudioDeviceSupportsDigital( aout_instance_t *, AudioDeviceID );
118 static int AudioStreamSupportsDigital( aout_instance_t *, AudioStreamID );
119 static int AudioStreamChangeFormat ( aout_instance_t *, AudioStreamID, AudioStreamBasicDescription );
121 static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
122 unsigned int, unsigned int, AudioBufferList *);
123 static OSStatus RenderCallbackSPDIF ( AudioDeviceID, const AudioTimeStamp *, const void *, const AudioTimeStamp *,
124 AudioBufferList *, const AudioTimeStamp *, void * );
125 static OSStatus HardwareListener ( AudioHardwarePropertyID, void *);
126 static OSStatus StreamListener ( AudioStreamID, UInt32,
127 AudioDevicePropertyID, void * );
128 static int AudioDeviceCallback ( vlc_object_t *, const char *,
129 vlc_value_t, vlc_value_t, void * );
132 /*****************************************************************************
134 *****************************************************************************/
135 #define ADEV_TEXT N_("Audio Device")
136 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
137 "audio device, as listed in your 'Audio Device' menu. This device will " \
138 "then be used by default for audio playback.")
141 set_shortname( "auhal" )
142 set_description( N_("HAL AudioUnit output") )
143 set_capability( "audio output", 101 )
144 set_category( CAT_AUDIO )
145 set_subcategory( SUBCAT_AUDIO_AOUT )
146 set_callbacks( Open, Close )
147 add_integer( "macosx-audio-device", 0, NULL, ADEV_TEXT, ADEV_LONGTEXT, false )
150 /*****************************************************************************
151 * Open: open macosx audio output
152 *****************************************************************************/
153 static int Open( vlc_object_t * p_this )
155 OSStatus err = noErr;
156 UInt32 i_param_size = 0;
157 struct aout_sys_t *p_sys = NULL;
159 aout_instance_t *p_aout = (aout_instance_t *)p_this;
161 /* Use int here, to match kAudioDevicePropertyDeviceIsAlive
165 /* Allocate structure */
166 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
167 if( p_aout->output.p_sys == NULL )
170 p_sys = p_aout->output.p_sys;
171 p_sys->i_default_dev = 0;
172 p_sys->i_selected_dev = 0;
173 p_sys->i_devices = 0;
174 p_sys->b_supports_digital = false;
175 p_sys->b_digital = false;
176 p_sys->au_component = NULL;
177 p_sys->au_unit = NULL;
178 p_sys->clock_diff = (mtime_t) 0;
179 p_sys->i_read_bytes = 0;
180 p_sys->i_total_bytes = 0;
181 p_sys->i_hog_pid = -1;
182 p_sys->i_stream_id = 0;
183 p_sys->i_stream_index = -1;
184 p_sys->b_revert = false;
185 p_sys->b_changed_mixing = false;
186 memset( p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE );
188 p_aout->output.pf_play = Play;
190 aout_FormatPrint( p_aout, "VLC is looking for:", (audio_sample_format_t *)&p_aout->output.output );
192 /* Persistent device variable */
193 if( var_Type( p_aout->p_libvlc, "macosx-audio-device" ) == 0 )
195 var_Create( p_aout->p_libvlc, "macosx-audio-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
198 /* Build a list of devices */
199 if( var_Type( p_aout, "audio-device" ) == 0 )
204 /* What device do we want? */
205 if( var_Get( p_aout, "audio-device", &val ) < 0 )
207 msg_Err( p_aout, "audio-device var does not exist. device probe failed." );
211 p_sys->i_selected_dev = val.i_int & ~AOUT_VAR_SPDIF_FLAG; /* remove SPDIF flag to get the true DeviceID */
212 p_sys->b_supports_digital = ( val.i_int & AOUT_VAR_SPDIF_FLAG ) ? true : false;
214 /* Check if the desired device is alive and usable */
215 /* TODO: add a callback to the device to alert us if the device dies */
216 i_param_size = sizeof( b_alive );
217 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
218 kAudioDevicePropertyDeviceIsAlive,
219 &i_param_size, &b_alive );
223 /* Be tolerant, only give a warning here */
224 msg_Warn( p_aout, "could not check whether device [0x%x] is alive: %4.4s", (unsigned int)p_sys->i_selected_dev, (char *)&err );
228 if( b_alive == false )
230 msg_Warn( p_aout, "selected audio device is not alive, switching to default device" );
231 p_sys->i_selected_dev = p_sys->i_default_dev;
234 i_param_size = sizeof( p_sys->i_hog_pid );
235 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
236 kAudioDevicePropertyHogMode,
237 &i_param_size, &p_sys->i_hog_pid );
241 /* This is not a fatal error. Some drivers simply don't support this property */
242 msg_Warn( p_aout, "could not check whether device is hogged: %4.4s",
244 p_sys->i_hog_pid = -1;
247 if( p_sys->i_hog_pid != -1 && p_sys->i_hog_pid != getpid() )
249 msg_Err( p_aout, "Selected audio device is exclusively in use by another program." );
250 dialog_Fatal( p_aout, _("Audio output failed"), "%s",
251 _("The selected audio output device is exclusively in "
252 "use by another program.") );
256 /* Check for Digital mode or Analog output mode */
257 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
259 if( OpenSPDIF( p_aout ) )
264 if( OpenAnalog( p_aout ) )
269 /* If we reach this, this aout has failed */
270 var_Destroy( p_aout, "audio-device" );
275 /*****************************************************************************
276 * Open: open and setup a HAL AudioUnit to do analog (multichannel) audio output
277 *****************************************************************************/
278 static int OpenAnalog( aout_instance_t *p_aout )
280 struct aout_sys_t *p_sys = p_aout->output.p_sys;
281 OSStatus err = noErr;
282 UInt32 i_param_size = 0, i = 0;
284 ComponentDescription desc;
285 AudioStreamBasicDescription DeviceFormat;
286 AudioChannelLayout *layout;
287 AudioChannelLayout new_layout;
288 AURenderCallbackStruct input;
290 /* Lets go find our Component */
291 desc.componentType = kAudioUnitType_Output;
292 desc.componentSubType = kAudioUnitSubType_HALOutput;
293 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
294 desc.componentFlags = 0;
295 desc.componentFlagsMask = 0;
297 p_sys->au_component = FindNextComponent( NULL, &desc );
298 if( p_sys->au_component == NULL )
300 msg_Warn( p_aout, "we cannot find our HAL component" );
304 err = OpenAComponent( p_sys->au_component, &p_sys->au_unit );
307 msg_Warn( p_aout, "we cannot open our HAL component" );
311 /* Set the device we will use for this output unit */
312 err = AudioUnitSetProperty( p_sys->au_unit,
313 kAudioOutputUnitProperty_CurrentDevice,
314 kAudioUnitScope_Global,
316 &p_sys->i_selected_dev,
317 sizeof( AudioDeviceID ));
321 msg_Warn( p_aout, "we cannot select the audio device" );
325 /* Get the current format */
326 i_param_size = sizeof(AudioStreamBasicDescription);
328 err = AudioUnitGetProperty( p_sys->au_unit,
329 kAudioUnitProperty_StreamFormat,
330 kAudioUnitScope_Input,
335 if( err != noErr ) return false;
336 else msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is: ", DeviceFormat ) );
338 /* Get the channel layout of the device side of the unit (vlc -> unit -> device) */
339 err = AudioUnitGetPropertyInfo( p_sys->au_unit,
340 kAudioDevicePropertyPreferredChannelLayout,
341 kAudioUnitScope_Output,
348 layout = (AudioChannelLayout *)malloc( i_param_size);
350 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
351 kAudioDevicePropertyPreferredChannelLayout,
352 kAudioUnitScope_Output,
357 /* We need to "fill out" the ChannelLayout, because there are multiple ways that it can be set */
358 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
360 /* bitmap defined channellayout */
361 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
362 sizeof( UInt32), &layout->mChannelBitmap,
366 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
368 /* layouttags defined channellayout */
369 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
370 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
375 msg_Dbg( p_aout, "layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
377 /* Initialize the VLC core channel count */
378 p_aout->output.output.i_physical_channels = 0;
379 i_original = p_aout->output.output.i_original_channels & AOUT_CHAN_PHYSMASK;
381 if( i_original == AOUT_CHAN_CENTER || layout->mNumberChannelDescriptions < 2 )
383 /* We only need Mono or cannot output more than 1 channel */
384 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
386 else if( i_original == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) || layout->mNumberChannelDescriptions < 3 )
388 /* We only need Stereo or cannot output more than 2 channels */
389 p_aout->output.output.i_physical_channels = AOUT_CHAN_RIGHT | AOUT_CHAN_LEFT;
393 /* We want more than stereo and we can do that */
394 for( i = 0; i < layout->mNumberChannelDescriptions; i++ )
396 msg_Dbg( p_aout, "this is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
398 switch( layout->mChannelDescriptions[i].mChannelLabel )
400 case kAudioChannelLabel_Left:
401 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
403 case kAudioChannelLabel_Right:
404 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
406 case kAudioChannelLabel_Center:
407 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
409 case kAudioChannelLabel_LFEScreen:
410 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE;
412 case kAudioChannelLabel_LeftSurround:
413 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT;
415 case kAudioChannelLabel_RightSurround:
416 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
418 case kAudioChannelLabel_RearSurroundLeft:
419 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
421 case kAudioChannelLabel_RearSurroundRight:
422 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
424 case kAudioChannelLabel_CenterSurround:
425 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER;
428 msg_Warn( p_aout, "unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
431 if( p_aout->output.output.i_physical_channels == 0 )
433 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
434 msg_Err( p_aout, "You should configure your speaker layout with Audio Midi Setup Utility in /Applications/Utilities. Now using Stereo mode." );
435 dialog_Fatal( p_aout, _("Audio device is not configured"), "%s",
436 _("You should configure your speaker layout with "
437 "the \"Audio Midi Setup\" utility in /Applications/"
438 "Utilities. Stereo mode is being used now.") );
445 msg_Warn( p_aout, "this driver does not support kAudioDevicePropertyPreferredChannelLayout. BAD DRIVER AUTHOR !!!" );
446 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
449 msg_Dbg( p_aout, "selected %d physical channels for device output", aout_FormatNbChannels( &p_aout->output.output ) );
450 msg_Dbg( p_aout, "VLC will output: %s", aout_FormatPrintChannels( &p_aout->output.output ));
452 memset (&new_layout, 0, sizeof(new_layout));
453 switch( aout_FormatNbChannels( &p_aout->output.output ) )
456 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
459 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
462 if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
464 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
466 else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
468 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
472 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
474 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
476 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
478 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
480 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
482 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
486 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
488 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
490 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
492 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
496 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
498 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
502 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_6_0; // L R Ls Rs C 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_6_1_A; // L R C LFE Ls Rs Cs
510 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
511 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
515 /* Set up the format to be used */
516 DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
517 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
519 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
520 p_aout->output.output.i_format = VLC_CODEC_FL32;
521 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
522 DeviceFormat.mBitsPerChannel = 32;
523 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output );
525 /* Calculate framesizes and stuff */
526 DeviceFormat.mFramesPerPacket = 1;
527 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
528 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
530 /* Set the desired format */
531 i_param_size = sizeof(AudioStreamBasicDescription);
532 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
533 kAudioUnitProperty_StreamFormat,
534 kAudioUnitScope_Input,
539 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
541 /* Retrieve actual format */
542 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
543 kAudioUnitProperty_StreamFormat,
544 kAudioUnitScope_Input,
549 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
551 /* Do the last VLC aout setups */
552 aout_FormatPrepare( &p_aout->output.output );
553 p_aout->output.i_nb_samples = 2048;
554 aout_VolumeSoftInit( p_aout );
556 /* set the IOproc callback */
557 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
558 input.inputProcRefCon = p_aout;
560 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
561 kAudioUnitProperty_SetRenderCallback,
562 kAudioUnitScope_Input,
563 0, &input, sizeof( input ) ) );
565 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
566 input.inputProcRefCon = p_aout;
568 /* Set the new_layout as the layout VLC will use to feed the AU unit */
569 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
570 kAudioUnitProperty_AudioChannelLayout,
571 kAudioUnitScope_Input,
572 0, &new_layout, sizeof(new_layout) ) );
574 if( new_layout.mNumberChannelDescriptions > 0 )
575 free( new_layout.mChannelDescriptions );
578 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
580 /* Find the difference between device clock and mdate clock */
581 p_sys->clock_diff = - (mtime_t)
582 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
583 p_sys->clock_diff += mdate();
586 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
591 /*****************************************************************************
592 * Setup a encoded digital stream (SPDIF)
593 *****************************************************************************/
594 static int OpenSPDIF( aout_instance_t * p_aout )
596 struct aout_sys_t *p_sys = p_aout->output.p_sys;
597 OSStatus err = noErr;
598 UInt32 i_param_size = 0, b_mix = 0;
599 Boolean b_writeable = false;
600 AudioStreamID *p_streams = NULL;
601 int i = 0, i_streams = 0;
603 /* Start doing the SPDIF setup proces */
604 p_sys->b_digital = true;
607 i_param_size = sizeof( p_sys->i_hog_pid );
608 p_sys->i_hog_pid = getpid() ;
610 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
611 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
615 msg_Err( p_aout, "failed to set hogmode: [%4.4s]", (char *)&err );
619 /* Set mixable to false if we are allowed to */
620 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
621 &i_param_size, &b_writeable );
623 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
624 &i_param_size, &b_mix );
626 if( !err && b_writeable )
629 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
630 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
631 p_sys->b_changed_mixing = true;
636 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
640 /* Get a list of all the streams on this device */
641 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE,
642 kAudioDevicePropertyStreams,
643 &i_param_size, NULL );
646 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
650 i_streams = i_param_size / sizeof( AudioStreamID );
651 p_streams = (AudioStreamID *)malloc( i_param_size );
652 if( p_streams == NULL )
655 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
656 kAudioDevicePropertyStreams,
657 &i_param_size, p_streams );
661 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
666 for( i = 0; i < i_streams && p_sys->i_stream_index < 0 ; i++ )
668 /* Find a stream with a cac3 stream */
669 AudioStreamBasicDescription *p_format_list = NULL;
670 int i_formats = 0, j = 0;
671 bool b_digital = false;
673 /* Retrieve all the stream formats supported by each output stream */
674 err = AudioStreamGetPropertyInfo( p_streams[i], 0,
675 kAudioStreamPropertyPhysicalFormats,
676 &i_param_size, NULL );
679 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
683 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
684 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
685 if( p_format_list == NULL )
688 err = AudioStreamGetProperty( p_streams[i], 0,
689 kAudioStreamPropertyPhysicalFormats,
690 &i_param_size, p_format_list );
693 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
694 free( p_format_list );
698 /* Check if one of the supported formats is a digital format */
699 for( j = 0; j < i_formats; j++ )
701 if( p_format_list[j].mFormatID == 'IAC3' ||
702 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
711 /* if this stream supports a digital (cac3) format, then go set it. */
712 int i_requested_rate_format = -1;
713 int i_current_rate_format = -1;
714 int i_backup_rate_format = -1;
716 p_sys->i_stream_id = p_streams[i];
717 p_sys->i_stream_index = i;
719 if( p_sys->b_revert == false )
721 /* Retrieve the original format of this stream first if not done so already */
722 i_param_size = sizeof( p_sys->sfmt_revert );
723 err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
724 kAudioStreamPropertyPhysicalFormat,
726 &p_sys->sfmt_revert );
729 msg_Err( p_aout, "could not retrieve the original streamformat: [%4.4s]", (char *)&err );
732 p_sys->b_revert = true;
735 for( j = 0; j < i_formats; j++ )
737 if( p_format_list[j].mFormatID == 'IAC3' ||
738 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
740 if( p_format_list[j].mSampleRate == p_aout->output.output.i_rate )
742 i_requested_rate_format = j;
745 else if( p_format_list[j].mSampleRate == p_sys->sfmt_revert.mSampleRate )
747 i_current_rate_format = j;
751 if( i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate )
752 i_backup_rate_format = j;
758 if( i_requested_rate_format >= 0 ) /* We prefer to output at the samplerate of the original audio */
759 p_sys->stream_format = p_format_list[i_requested_rate_format];
760 else if( i_current_rate_format >= 0 ) /* If not possible, we will try to use the current samplerate of the device */
761 p_sys->stream_format = p_format_list[i_current_rate_format];
762 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) */
764 free( p_format_list );
768 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) );
770 if( !AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->stream_format ) )
773 /* Set the format flags */
774 if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian )
775 p_aout->output.output.i_format = VLC_CODEC_SPDIFB;
777 p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
778 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
779 p_aout->output.output.i_frame_length = A52_FRAME_NB;
780 p_aout->output.i_nb_samples = p_aout->output.output.i_frame_length;
781 p_aout->output.output.i_rate = (unsigned int)p_sys->stream_format.mSampleRate;
782 aout_FormatPrepare( &p_aout->output.output );
783 aout_VolumeNoneInit( p_aout );
785 /* Add IOProc callback */
786 err = AudioDeviceAddIOProc( p_sys->i_selected_dev,
787 (AudioDeviceIOProc)RenderCallbackSPDIF,
792 msg_Err( p_aout, "AudioDeviceAddIOProc failed: [%4.4s]", (char *)&err );
796 /* Check for the difference between the Device clock and mdate */
797 p_sys->clock_diff = - (mtime_t)
798 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
799 p_sys->clock_diff += mdate();
802 err = AudioDeviceStart( p_sys->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF );
805 msg_Err( p_aout, "AudioDeviceStart failed: [%4.4s]", (char *)&err );
807 err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
808 (AudioDeviceIOProc)RenderCallbackSPDIF );
811 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
820 /*****************************************************************************
821 * Close: Close HAL AudioUnit
822 *****************************************************************************/
823 static void Close( vlc_object_t * p_this )
825 aout_instance_t *p_aout = (aout_instance_t *)p_this;
826 struct aout_sys_t *p_sys = p_aout->output.p_sys;
827 OSStatus err = noErr;
828 UInt32 i_param_size = 0;
832 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
833 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
834 verify_noerr( CloseComponent( p_sys->au_unit ) );
837 if( p_sys->b_digital )
840 err = AudioDeviceStop( p_sys->i_selected_dev,
841 (AudioDeviceIOProc)RenderCallbackSPDIF );
844 msg_Err( p_aout, "AudioDeviceStop failed: [%4.4s]", (char *)&err );
847 /* Remove IOProc callback */
848 err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
849 (AudioDeviceIOProc)RenderCallbackSPDIF );
852 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
855 if( p_sys->b_revert )
857 AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->sfmt_revert );
860 if( p_sys->b_changed_mixing && p_sys->sfmt_revert.mFormatID != kAudioFormat60958AC3 )
864 /* Revert mixable to true if we are allowed to */
865 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
866 &i_param_size, &b_writeable );
868 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
869 &i_param_size, &b_mix );
871 if( !err && b_writeable )
873 msg_Dbg( p_aout, "mixable is: %d", b_mix );
875 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
876 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
881 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
886 err = AudioHardwareRemovePropertyListener( kAudioHardwarePropertyDevices,
891 msg_Err( p_aout, "AudioHardwareRemovePropertyListener failed: [%4.4s]", (char *)&err );
894 if( p_sys->i_hog_pid == getpid() )
896 p_sys->i_hog_pid = -1;
897 i_param_size = sizeof( p_sys->i_hog_pid );
898 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
899 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
900 if( err != noErr ) msg_Err( p_aout, "Could not release hogmode: [%4.4s]", (char *)&err );
906 /*****************************************************************************
907 * Play: nothing to do
908 *****************************************************************************/
909 static void Play( aout_instance_t * p_aout )
915 /*****************************************************************************
916 * Probe: Check which devices the OS has, and add them to our audio-device menu
917 *****************************************************************************/
918 static void Probe( aout_instance_t * p_aout )
920 OSStatus err = noErr;
921 UInt32 i = 0, i_param_size = 0;
922 AudioDeviceID devid_def = 0;
923 AudioDeviceID *p_devices = NULL;
924 vlc_value_t val, text;
926 struct aout_sys_t *p_sys = p_aout->output.p_sys;
928 /* Get number of devices */
929 err = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices,
930 &i_param_size, NULL );
933 msg_Err( p_aout, "Could not get number of devices: [%4.4s]", (char *)&err );
937 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
939 if( p_sys->i_devices < 1 )
941 msg_Err( p_aout, "No audio output devices were found." );
945 msg_Dbg( p_aout, "system has [%ld] device(s)", p_sys->i_devices );
947 /* Allocate DeviceID array */
948 p_devices = (AudioDeviceID*)malloc( sizeof(AudioDeviceID) * p_sys->i_devices );
949 if( p_devices == NULL )
952 /* Populate DeviceID array */
953 err = AudioHardwareGetProperty( kAudioHardwarePropertyDevices,
954 &i_param_size, p_devices );
957 msg_Err( p_aout, "could not get the device IDs: [%4.4s]", (char *)&err );
961 /* Find the ID of the default Device */
962 i_param_size = sizeof( AudioDeviceID );
963 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
964 &i_param_size, &devid_def );
967 msg_Err( p_aout, "could not get default audio device: [%4.4s]", (char *)&err );
970 p_sys->i_default_dev = devid_def;
972 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
973 text.psz_string = (char*)_("Audio Device");
974 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
976 for( i = 0; i < p_sys->i_devices; i++ )
981 /* Retrieve the length of the device name */
982 err = AudioDeviceGetPropertyInfo(
983 p_devices[i], 0, false,
984 kAudioDevicePropertyDeviceName,
985 &i_param_size, NULL);
986 if( err ) goto error;
988 /* Retrieve the name of the device */
989 psz_name = (char *)malloc( i_param_size );
990 err = AudioDeviceGetProperty(
991 p_devices[i], 0, false,
992 kAudioDevicePropertyDeviceName,
993 &i_param_size, psz_name);
994 if( err ) goto error;
996 msg_Dbg( p_aout, "DevID: %#lx DevName: %s", p_devices[i], psz_name );
998 if( !AudioDeviceHasOutput( p_devices[i]) )
1000 msg_Dbg( p_aout, "this device is INPUT only. skipping..." );
1005 /* Add the menu entries */
1006 val.i_int = (int)p_devices[i];
1007 text.psz_string = psz_name;
1008 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1009 text.psz_string = NULL;
1010 if( p_sys->i_default_dev == p_devices[i] )
1012 /* The default device is the selected device normally */
1013 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1014 var_Set( p_aout, "audio-device", val );
1017 if( AudioDeviceSupportsDigital( p_aout, p_devices[i] ) )
1019 val.i_int = (int)p_devices[i] | AOUT_VAR_SPDIF_FLAG;
1020 if( asprintf( &text.psz_string, _("%s (Encoded Output)"), psz_name ) != -1 )
1022 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1023 free( text.psz_string );
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 );
1037 /* If a device is already "preselected", then use this device */
1038 var_Get( p_aout->p_libvlc, "macosx-audio-device", &val );
1041 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1042 var_Set( p_aout, "audio-device", val );
1045 /* If we change the device we want to use, we should renegotiate the audio chain */
1046 var_AddCallback( p_aout, "audio-device", AudioDeviceCallback, NULL );
1048 /* Attach a Listener so that we are notified of a change in the Device setup */
1049 err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
1059 msg_Warn( p_aout, "audio device already in use" );
1064 /*****************************************************************************
1065 * AudioDeviceHasOutput: Checks if the Device actually provides any outputs at all
1066 *****************************************************************************/
1067 static int AudioDeviceHasOutput( AudioDeviceID i_dev_id )
1072 verify_noerr( AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE, kAudioDevicePropertyStreams, &dataSize, &isWritable) );
1073 if (dataSize == 0) return FALSE;
1078 /*****************************************************************************
1079 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
1080 *****************************************************************************/
1081 static int AudioDeviceSupportsDigital( aout_instance_t *p_aout, AudioDeviceID i_dev_id )
1083 OSStatus err = noErr;
1084 UInt32 i_param_size = 0;
1085 AudioStreamID *p_streams = NULL;
1086 int i = 0, i_streams = 0;
1087 bool b_return = false;
1089 /* Retrieve all the output streams */
1090 err = AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE,
1091 kAudioDevicePropertyStreams,
1092 &i_param_size, NULL );
1095 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1099 i_streams = i_param_size / sizeof( AudioStreamID );
1100 p_streams = (AudioStreamID *)malloc( i_param_size );
1101 if( p_streams == NULL )
1104 err = AudioDeviceGetProperty( i_dev_id, 0, FALSE,
1105 kAudioDevicePropertyStreams,
1106 &i_param_size, p_streams );
1110 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1114 for( i = 0; i < i_streams; i++ )
1116 if( AudioStreamSupportsDigital( p_aout, p_streams[i] ) )
1124 /*****************************************************************************
1125 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
1126 *****************************************************************************/
1127 static int AudioStreamSupportsDigital( aout_instance_t *p_aout, AudioStreamID i_stream_id )
1129 OSStatus err = noErr;
1130 UInt32 i_param_size = 0;
1131 AudioStreamBasicDescription *p_format_list = NULL;
1132 int i = 0, i_formats = 0;
1133 bool b_return = false;
1135 /* Retrieve all the stream formats supported by each output stream */
1136 err = AudioStreamGetPropertyInfo( i_stream_id, 0,
1137 kAudioStreamPropertyPhysicalFormats,
1138 &i_param_size, NULL );
1141 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
1145 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
1146 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
1147 if( p_format_list == NULL )
1150 err = AudioStreamGetProperty( i_stream_id, 0,
1151 kAudioStreamPropertyPhysicalFormats,
1152 &i_param_size, p_format_list );
1155 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
1156 free( p_format_list);
1157 p_format_list = NULL;
1161 for( i = 0; i < i_formats; i++ )
1163 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format: ", p_format_list[i] ) );
1165 if( p_format_list[i].mFormatID == 'IAC3' ||
1166 p_format_list[i].mFormatID == kAudioFormat60958AC3 )
1172 free( p_format_list );
1176 /*****************************************************************************
1177 * AudioStreamChangeFormat: Change i_stream_id to change_format
1178 *****************************************************************************/
1179 static int AudioStreamChangeFormat( aout_instance_t *p_aout, AudioStreamID i_stream_id, AudioStreamBasicDescription change_format )
1181 OSStatus err = noErr;
1182 UInt32 i_param_size = 0;
1185 struct { vlc_mutex_t lock; vlc_cond_t cond; } w;
1187 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "setting stream format: ", change_format ) );
1189 /* Condition because SetProperty is asynchronious */
1190 vlc_cond_init( &w.cond );
1191 vlc_mutex_init( &w.lock );
1192 vlc_mutex_lock( &w.lock );
1194 /* Install the callback */
1195 err = AudioStreamAddPropertyListener( i_stream_id, 0,
1196 kAudioStreamPropertyPhysicalFormat,
1197 StreamListener, (void *)&w );
1200 msg_Err( p_aout, "AudioStreamAddPropertyListener failed: [%4.4s]", (char *)&err );
1204 /* change the format */
1205 err = AudioStreamSetProperty( i_stream_id, 0, 0,
1206 kAudioStreamPropertyPhysicalFormat,
1207 sizeof( AudioStreamBasicDescription ),
1211 msg_Err( p_aout, "could not set the stream format: [%4.4s]", (char *)&err );
1215 /* The AudioStreamSetProperty is not only asynchronious (requiring the locks)
1216 * it is also not atomic in its behaviour.
1217 * Therefore we check 5 times before we really give up.
1218 * FIXME: failing isn't actually implemented yet. */
1219 for( i = 0; i < 5; i++ )
1221 AudioStreamBasicDescription actual_format;
1222 mtime_t timeout = mdate() + 500000;
1224 if( vlc_cond_timedwait( &w.cond, &w.lock, timeout ) )
1226 msg_Dbg( p_aout, "reached timeout" );
1229 i_param_size = sizeof( AudioStreamBasicDescription );
1230 err = AudioStreamGetProperty( i_stream_id, 0,
1231 kAudioStreamPropertyPhysicalFormat,
1235 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "actual format in use: ", actual_format ) );
1236 if( actual_format.mSampleRate == change_format.mSampleRate &&
1237 actual_format.mFormatID == change_format.mFormatID &&
1238 actual_format.mFramesPerPacket == change_format.mFramesPerPacket )
1240 /* The right format is now active */
1243 /* We need to check again */
1246 /* Removing the property listener */
1247 err = AudioStreamRemovePropertyListener( i_stream_id, 0,
1248 kAudioStreamPropertyPhysicalFormat,
1252 msg_Err( p_aout, "AudioStreamRemovePropertyListener failed: [%4.4s]", (char *)&err );
1256 /* Destroy the lock and condition */
1257 vlc_mutex_unlock( &w.lock );
1258 vlc_mutex_destroy( &w.lock );
1259 vlc_cond_destroy( &w.cond );
1264 /*****************************************************************************
1265 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
1266 * us to provide some more audio data.
1267 * Don't print anything during normal playback, calling blocking function from
1268 * this callback is not allowed.
1269 *****************************************************************************/
1270 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
1271 AudioUnitRenderActionFlags *ioActionFlags,
1272 const AudioTimeStamp *inTimeStamp,
1273 unsigned int inBusNumber,
1274 unsigned int inNumberFrames,
1275 AudioBufferList *ioData )
1277 AudioTimeStamp host_time;
1278 mtime_t current_date = 0;
1279 uint32_t i_mData_bytes = 0;
1281 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
1282 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1284 VLC_UNUSED(ioActionFlags);
1285 VLC_UNUSED(inBusNumber);
1286 VLC_UNUSED(inNumberFrames);
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 vlc_memcpy( ioData->mBuffers[0].mData,
1313 &p_sys->p_remainder_buffer[p_sys->i_read_bytes],
1315 p_sys->i_read_bytes += i_mData_bytes;
1316 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1317 ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1319 if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
1320 p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
1323 while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
1325 /* We don't have enough data yet */
1326 aout_buffer_t * p_buffer;
1327 p_buffer = aout_OutputNextBuffer( p_aout, current_date , false );
1329 if( p_buffer != NULL )
1331 uint32_t i_second_mData_bytes = __MIN( p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1333 vlc_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes,
1334 p_buffer->p_buffer, i_second_mData_bytes );
1335 i_mData_bytes += i_second_mData_bytes;
1337 if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
1339 p_sys->i_total_bytes = p_buffer->i_nb_bytes - i_second_mData_bytes;
1340 vlc_memcpy( p_sys->p_remainder_buffer,
1341 &p_buffer->p_buffer[i_second_mData_bytes],
1342 p_sys->i_total_bytes );
1346 /* update current_date */
1347 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1348 ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1350 aout_BufferFree( p_buffer );
1354 vlc_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes,
1355 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 VLC_UNUSED(inDevice);
1380 VLC_UNUSED(inInputData);
1381 VLC_UNUSED(inInputTime);
1383 /* Check for the difference between the Device clock and mdate */
1384 p_sys->clock_diff = - (mtime_t)
1385 AudioConvertHostTimeToNanos( inNow->mHostTime ) / 1000;
1386 p_sys->clock_diff += mdate();
1388 current_date = p_sys->clock_diff +
1389 AudioConvertHostTimeToNanos( inOutputTime->mHostTime ) / 1000;
1390 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1392 p_buffer = aout_OutputNextBuffer( p_aout, current_date, true );
1394 #define BUFFER outOutputData->mBuffers[p_sys->i_stream_index]
1395 if( p_buffer != NULL )
1397 if( (int)BUFFER.mDataByteSize != (int)p_buffer->i_nb_bytes)
1398 msg_Warn( p_aout, "bytesize: %d nb_bytes: %d", (int)BUFFER.mDataByteSize, (int)p_buffer->i_nb_bytes );
1400 /* move data into output data buffer */
1401 vlc_memcpy( BUFFER.mData, p_buffer->p_buffer, p_buffer->i_nb_bytes );
1402 aout_BufferFree( p_buffer );
1406 vlc_memset( BUFFER.mData, 0, BUFFER.mDataByteSize );
1413 /*****************************************************************************
1414 * HardwareListener: Warns us of changes in the list of registered devices
1415 *****************************************************************************/
1416 static OSStatus HardwareListener( AudioHardwarePropertyID inPropertyID,
1417 void * inClientData )
1419 OSStatus err = noErr;
1420 aout_instance_t *p_aout = (aout_instance_t *)inClientData;
1422 switch( inPropertyID )
1424 case kAudioHardwarePropertyDevices:
1426 /* something changed in the list of devices */
1427 /* We trigger the audio-device's aout_ChannelsRestart callback */
1428 var_TriggerCallback( p_aout, "audio-device" );
1429 var_Destroy( p_aout, "audio-device" );
1437 /*****************************************************************************
1439 *****************************************************************************/
1440 static OSStatus StreamListener( AudioStreamID inStream,
1442 AudioDevicePropertyID inPropertyID,
1443 void * inClientData )
1445 OSStatus err = noErr;
1446 struct { vlc_mutex_t lock; vlc_cond_t cond; } * w = inClientData;
1448 VLC_UNUSED(inStream);
1449 VLC_UNUSED(inChannel);
1451 switch( inPropertyID )
1453 case kAudioStreamPropertyPhysicalFormat:
1454 vlc_mutex_lock( &w->lock );
1455 vlc_cond_signal( &w->cond );
1456 vlc_mutex_unlock( &w->lock );
1465 /*****************************************************************************
1466 * AudioDeviceCallback: Callback triggered when the audio-device variable is changed
1467 *****************************************************************************/
1468 static int AudioDeviceCallback( vlc_object_t *p_this, const char *psz_variable,
1469 vlc_value_t old_val, vlc_value_t new_val, void *param )
1471 aout_instance_t *p_aout = (aout_instance_t *)p_this;
1472 var_Set( p_aout->p_libvlc, "macosx-audio-device", new_val );
1473 msg_Dbg( p_aout, "Set Device: %#x", new_val.i_int );
1474 return aout_ChannelsRestart( p_this, psz_variable, old_val, new_val, param );