1 /*****************************************************************************
2 * auhal.c: AUHAL and Coreaudio output plugin
3 *****************************************************************************
4 * Copyright (C) 2005, 2011 the VideoLAN team
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_dialog.h>
39 #include <CoreAudio/CoreAudio.h>
40 #include <AudioUnit/AudioUnit.h>
41 #include <AudioToolbox/AudioFormat.h>
44 #define verify_noerr(a) assert((a) == noErr)
47 #define STREAM_FORMAT_MSG( pre, sfm ) \
48 pre "[%u][%4.4s][%u][%u][%u][%u][%u][%u]", \
49 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
50 sfm.mFormatFlags, sfm.mBytesPerPacket, \
51 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
52 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
54 #define STREAM_FORMAT_MSG_FULL( pre, sfm ) \
55 pre ":\nsamplerate: [%u]\nFormatID: [%4.4s]\nFormatFlags: [%u]\nBypesPerPacket: [%u]\nFramesPerPacket: [%u]\nBytesPerFrame: [%u]\nChannelsPerFrame: [%u]\nBitsPerChannel[%u]", \
56 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
57 sfm.mFormatFlags, sfm.mBytesPerPacket, \
58 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
59 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
61 #define FRAMESIZE 2048
62 #define BUFSIZE (FRAMESIZE * 8) * 8
63 #define AOUT_VAR_SPDIF_FLAG 0xf00000
67 * - clean up the debug info
68 * - be better at changing stream setup or devices setup changes while playing.
72 /*****************************************************************************
73 * aout_sys_t: private audio output method descriptor
74 *****************************************************************************
75 * This structure is part of the audio output thread descriptor.
76 * It describes the CoreAudio specific properties of an output thread.
77 *****************************************************************************/
80 AudioDeviceID i_default_dev; /* Keeps DeviceID of defaultOutputDevice */
81 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device */
82 AudioDeviceIOProcID i_procID; /* DeviceID of current 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 * );
133 /*****************************************************************************
135 *****************************************************************************/
136 #define ADEV_TEXT N_("Audio Device")
137 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
138 "audio device, as listed in your 'Audio Device' menu. This device will " \
139 "then be used by default for audio playback.")
142 set_shortname( "auhal" )
143 set_description( N_("HAL AudioUnit output") )
144 set_capability( "audio output", 101 )
145 set_category( CAT_AUDIO )
146 set_subcategory( SUBCAT_AUDIO_AOUT )
147 set_callbacks( Open, Close )
148 add_integer( "macosx-audio-device", 0, ADEV_TEXT, ADEV_LONGTEXT, false )
151 /*****************************************************************************
152 * Open: open macosx audio output
153 *****************************************************************************/
154 static int Open( vlc_object_t * p_this )
156 OSStatus err = noErr;
157 UInt32 i_param_size = 0;
158 struct aout_sys_t *p_sys = NULL;
160 aout_instance_t *p_aout = (aout_instance_t *)p_this;
162 /* Use int here, to match kAudioDevicePropertyDeviceIsAlive
166 /* Allocate structure */
167 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
168 if( p_aout->output.p_sys == NULL )
171 p_sys = p_aout->output.p_sys;
172 p_sys->i_default_dev = 0;
173 p_sys->i_selected_dev = 0;
174 p_sys->i_devices = 0;
175 p_sys->b_supports_digital = false;
176 p_sys->b_digital = false;
177 p_sys->au_component = NULL;
178 p_sys->au_unit = NULL;
179 p_sys->clock_diff = (mtime_t) 0;
180 p_sys->i_read_bytes = 0;
181 p_sys->i_total_bytes = 0;
182 p_sys->i_hog_pid = -1;
183 p_sys->i_stream_id = 0;
184 p_sys->i_stream_index = -1;
185 p_sys->b_revert = false;
186 p_sys->b_changed_mixing = false;
187 memset( p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE );
189 p_aout->output.pf_play = Play;
191 aout_FormatPrint( p_aout, "VLC is looking for:", (audio_sample_format_t *)&p_aout->output.output );
193 /* Persistent device variable */
194 if( var_Type( p_aout->p_libvlc, "macosx-audio-device" ) == 0 )
196 var_Create( p_aout->p_libvlc, "macosx-audio-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
199 /* Build a list of devices */
200 if( var_Type( p_aout, "audio-device" ) == 0 )
205 /* What device do we want? */
206 if( var_Get( p_aout, "audio-device", &val ) < 0 )
208 msg_Err( p_aout, "audio-device var does not exist. device probe failed." );
212 p_sys->i_selected_dev = val.i_int & ~AOUT_VAR_SPDIF_FLAG; /* remove SPDIF flag to get the true DeviceID */
213 p_sys->b_supports_digital = ( val.i_int & AOUT_VAR_SPDIF_FLAG ) ? true : false;
215 /* Check if the desired device is alive and usable */
216 /* TODO: add a callback to the device to alert us if the device dies */
217 i_param_size = sizeof( b_alive );
218 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
219 kAudioDevicePropertyDeviceIsAlive,
220 &i_param_size, &b_alive );
224 /* Be tolerant, only give a warning here */
225 msg_Warn( p_aout, "could not check whether device [0x%x] is alive: %4.4s", (unsigned int)p_sys->i_selected_dev, (char *)&err );
229 if( b_alive == false )
231 msg_Warn( p_aout, "selected audio device is not alive, switching to default device" );
232 p_sys->i_selected_dev = p_sys->i_default_dev;
235 i_param_size = sizeof( p_sys->i_hog_pid );
236 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
237 kAudioDevicePropertyHogMode,
238 &i_param_size, &p_sys->i_hog_pid );
242 /* This is not a fatal error. Some drivers simply don't support this property */
243 msg_Warn( p_aout, "could not check whether device is hogged: %4.4s",
245 p_sys->i_hog_pid = -1;
248 if( p_sys->i_hog_pid != -1 && p_sys->i_hog_pid != getpid() )
250 msg_Err( p_aout, "Selected audio device is exclusively in use by another program." );
251 dialog_Fatal( p_aout, _("Audio output failed"), "%s",
252 _("The selected audio output device is exclusively in "
253 "use by another program.") );
257 /* Check for Digital mode or Analog output mode */
258 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
260 if( OpenSPDIF( p_aout ) )
265 if( OpenAnalog( p_aout ) )
270 /* If we reach this, this aout has failed */
271 var_Destroy( p_aout, "audio-device" );
276 /*****************************************************************************
277 * Open: open and setup a HAL AudioUnit to do analog (multichannel) audio output
278 *****************************************************************************/
279 static int OpenAnalog( aout_instance_t *p_aout )
281 struct aout_sys_t *p_sys = p_aout->output.p_sys;
282 OSStatus err = noErr;
283 UInt32 i_param_size = 0, i = 0;
285 ComponentDescription desc;
286 AudioStreamBasicDescription DeviceFormat;
287 AudioChannelLayout *layout;
288 AudioChannelLayout new_layout;
289 AURenderCallbackStruct input;
291 /* Lets go find our Component */
292 desc.componentType = kAudioUnitType_Output;
293 desc.componentSubType = kAudioUnitSubType_HALOutput;
294 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
295 desc.componentFlags = 0;
296 desc.componentFlagsMask = 0;
298 p_sys->au_component = FindNextComponent( NULL, &desc );
299 if( p_sys->au_component == NULL )
301 msg_Warn( p_aout, "we cannot find our HAL component" );
305 err = OpenAComponent( p_sys->au_component, &p_sys->au_unit );
308 msg_Warn( p_aout, "we cannot open our HAL component" );
312 /* Set the device we will use for this output unit */
313 err = AudioUnitSetProperty( p_sys->au_unit,
314 kAudioOutputUnitProperty_CurrentDevice,
315 kAudioUnitScope_Global,
317 &p_sys->i_selected_dev,
318 sizeof( AudioDeviceID ));
322 msg_Warn( p_aout, "we cannot select the audio device" );
326 /* Get the current format */
327 i_param_size = sizeof(AudioStreamBasicDescription);
329 err = AudioUnitGetProperty( p_sys->au_unit,
330 kAudioUnitProperty_StreamFormat,
331 kAudioUnitScope_Input,
336 if( err != noErr ) return false;
337 else msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is: ", DeviceFormat ) );
339 /* Get the channel layout of the device side of the unit (vlc -> unit -> device) */
340 err = AudioUnitGetPropertyInfo( p_sys->au_unit,
341 kAudioDevicePropertyPreferredChannelLayout,
342 kAudioUnitScope_Output,
349 layout = (AudioChannelLayout *)malloc( i_param_size);
351 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
352 kAudioDevicePropertyPreferredChannelLayout,
353 kAudioUnitScope_Output,
358 /* We need to "fill out" the ChannelLayout, because there are multiple ways that it can be set */
359 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
361 /* bitmap defined channellayout */
362 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
363 sizeof( UInt32), &layout->mChannelBitmap,
367 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
369 /* layouttags defined channellayout */
370 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
371 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
376 msg_Dbg( p_aout, "layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
378 /* Initialize the VLC core channel count */
379 p_aout->output.output.i_physical_channels = 0;
380 i_original = p_aout->output.output.i_original_channels & AOUT_CHAN_PHYSMASK;
382 if( i_original == AOUT_CHAN_CENTER || layout->mNumberChannelDescriptions < 2 )
384 /* We only need Mono or cannot output more than 1 channel */
385 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
387 else if( i_original == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) || layout->mNumberChannelDescriptions < 3 )
389 /* We only need Stereo or cannot output more than 2 channels */
390 p_aout->output.output.i_physical_channels = AOUT_CHAN_RIGHT | AOUT_CHAN_LEFT;
394 /* We want more than stereo and we can do that */
395 for( i = 0; i < layout->mNumberChannelDescriptions; i++ )
397 msg_Dbg( p_aout, "this is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
399 switch( layout->mChannelDescriptions[i].mChannelLabel )
401 case kAudioChannelLabel_Left:
402 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
404 case kAudioChannelLabel_Right:
405 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
407 case kAudioChannelLabel_Center:
408 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
410 case kAudioChannelLabel_LFEScreen:
411 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE;
413 case kAudioChannelLabel_LeftSurround:
414 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT;
416 case kAudioChannelLabel_RightSurround:
417 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
419 case kAudioChannelLabel_RearSurroundLeft:
420 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
422 case kAudioChannelLabel_RearSurroundRight:
423 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
425 case kAudioChannelLabel_CenterSurround:
426 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER;
429 msg_Warn( p_aout, "unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
432 if( p_aout->output.output.i_physical_channels == 0 )
434 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
435 msg_Err( p_aout, "You should configure your speaker layout with Audio Midi Setup Utility in /Applications/Utilities. Now using Stereo mode." );
436 dialog_Fatal( p_aout, _("Audio device is not configured"), "%s",
437 _("You should configure your speaker layout with "
438 "the \"Audio Midi Setup\" utility in /Applications/"
439 "Utilities. Stereo mode is being used now.") );
446 msg_Warn( p_aout, "this driver does not support kAudioDevicePropertyPreferredChannelLayout. BAD DRIVER AUTHOR !!!" );
447 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
450 msg_Dbg( p_aout, "selected %d physical channels for device output", aout_FormatNbChannels( &p_aout->output.output ) );
451 msg_Dbg( p_aout, "VLC will output: %s", aout_FormatPrintChannels( &p_aout->output.output ));
453 memset (&new_layout, 0, sizeof(new_layout));
454 switch( aout_FormatNbChannels( &p_aout->output.output ) )
457 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
460 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
463 if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
465 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
467 else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
469 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
473 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
475 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
477 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
479 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
481 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
483 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
487 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
489 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
491 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
493 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
497 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
499 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
503 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_6_0; // L R Ls Rs C Cs
507 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
508 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
511 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
512 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
516 /* Set up the format to be used */
517 DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
518 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
520 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
521 p_aout->output.output.i_format = VLC_CODEC_FL32;
522 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
523 DeviceFormat.mBitsPerChannel = 32;
524 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output );
526 /* Calculate framesizes and stuff */
527 DeviceFormat.mFramesPerPacket = 1;
528 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
529 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
531 /* Set the desired format */
532 i_param_size = sizeof(AudioStreamBasicDescription);
533 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
534 kAudioUnitProperty_StreamFormat,
535 kAudioUnitScope_Input,
540 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
542 /* Retrieve actual format */
543 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
544 kAudioUnitProperty_StreamFormat,
545 kAudioUnitScope_Input,
550 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
552 /* Do the last VLC aout setups */
553 aout_FormatPrepare( &p_aout->output.output );
554 p_aout->output.i_nb_samples = FRAMESIZE;
555 aout_VolumeSoftInit( p_aout );
557 /* set the IOproc callback */
558 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
559 input.inputProcRefCon = p_aout;
561 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
562 kAudioUnitProperty_SetRenderCallback,
563 kAudioUnitScope_Input,
564 0, &input, sizeof( input ) ) );
566 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
567 input.inputProcRefCon = p_aout;
569 /* Set the new_layout as the layout VLC will use to feed the AU unit */
570 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
571 kAudioUnitProperty_AudioChannelLayout,
572 kAudioUnitScope_Input,
573 0, &new_layout, sizeof(new_layout) ) );
575 if( new_layout.mNumberChannelDescriptions > 0 )
576 free( new_layout.mChannelDescriptions );
579 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
581 /* Find the difference between device clock and mdate clock */
582 p_sys->clock_diff = - (mtime_t)
583 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
584 p_sys->clock_diff += mdate();
587 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
592 /*****************************************************************************
593 * Setup a encoded digital stream (SPDIF)
594 *****************************************************************************/
595 static int OpenSPDIF( aout_instance_t * p_aout )
597 struct aout_sys_t *p_sys = p_aout->output.p_sys;
598 OSStatus err = noErr;
599 UInt32 i_param_size = 0, b_mix = 0;
600 Boolean b_writeable = false;
601 AudioStreamID *p_streams = NULL;
602 int i = 0, i_streams = 0;
604 /* Start doing the SPDIF setup proces */
605 p_sys->b_digital = true;
608 i_param_size = sizeof( p_sys->i_hog_pid );
609 p_sys->i_hog_pid = getpid() ;
611 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
612 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
616 msg_Err( p_aout, "failed to set hogmode: [%4.4s]", (char *)&err );
620 /* Set mixable to false if we are allowed to */
621 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
622 &i_param_size, &b_writeable );
624 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
625 &i_param_size, &b_mix );
627 if( !err && b_writeable )
630 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
631 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
632 p_sys->b_changed_mixing = true;
637 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
641 /* Get a list of all the streams on this device */
642 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE,
643 kAudioDevicePropertyStreams,
644 &i_param_size, NULL );
647 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
651 i_streams = i_param_size / sizeof( AudioStreamID );
652 p_streams = (AudioStreamID *)malloc( i_param_size );
653 if( p_streams == NULL )
656 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
657 kAudioDevicePropertyStreams,
658 &i_param_size, p_streams );
662 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
667 for( i = 0; i < i_streams && p_sys->i_stream_index < 0 ; i++ )
669 /* Find a stream with a cac3 stream */
670 AudioStreamBasicDescription *p_format_list = NULL;
671 int i_formats = 0, j = 0;
672 bool b_digital = false;
674 /* Retrieve all the stream formats supported by each output stream */
675 err = AudioStreamGetPropertyInfo( p_streams[i], 0,
676 kAudioStreamPropertyPhysicalFormats,
677 &i_param_size, NULL );
680 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
684 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
685 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
686 if( p_format_list == NULL )
689 err = AudioStreamGetProperty( p_streams[i], 0,
690 kAudioStreamPropertyPhysicalFormats,
691 &i_param_size, p_format_list );
694 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
695 free( p_format_list );
699 /* Check if one of the supported formats is a digital format */
700 for( j = 0; j < i_formats; j++ )
702 if( p_format_list[j].mFormatID == 'IAC3' ||
703 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
712 /* if this stream supports a digital (cac3) format, then go set it. */
713 int i_requested_rate_format = -1;
714 int i_current_rate_format = -1;
715 int i_backup_rate_format = -1;
717 p_sys->i_stream_id = p_streams[i];
718 p_sys->i_stream_index = i;
720 if( p_sys->b_revert == false )
722 /* Retrieve the original format of this stream first if not done so already */
723 i_param_size = sizeof( p_sys->sfmt_revert );
724 err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
725 kAudioStreamPropertyPhysicalFormat,
727 &p_sys->sfmt_revert );
730 msg_Err( p_aout, "could not retrieve the original streamformat: [%4.4s]", (char *)&err );
733 p_sys->b_revert = true;
736 for( j = 0; j < i_formats; j++ )
738 if( p_format_list[j].mFormatID == 'IAC3' ||
739 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
741 if( p_format_list[j].mSampleRate == p_aout->output.output.i_rate )
743 i_requested_rate_format = j;
746 else if( p_format_list[j].mSampleRate == p_sys->sfmt_revert.mSampleRate )
748 i_current_rate_format = j;
752 if( i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate )
753 i_backup_rate_format = j;
759 if( i_requested_rate_format >= 0 ) /* We prefer to output at the samplerate of the original audio */
760 p_sys->stream_format = p_format_list[i_requested_rate_format];
761 else if( i_current_rate_format >= 0 ) /* If not possible, we will try to use the current samplerate of the device */
762 p_sys->stream_format = p_format_list[i_current_rate_format];
763 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) */
765 free( p_format_list );
769 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) );
771 if( !AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->stream_format ) )
774 /* Set the format flags */
775 if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian )
776 p_aout->output.output.i_format = VLC_CODEC_SPDIFB;
778 p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
779 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
780 p_aout->output.output.i_frame_length = A52_FRAME_NB;
781 p_aout->output.i_nb_samples = p_aout->output.output.i_frame_length;
782 p_aout->output.output.i_rate = (unsigned int)p_sys->stream_format.mSampleRate;
783 aout_FormatPrepare( &p_aout->output.output );
784 aout_VolumeNoneInit( p_aout );
786 /* Add IOProc callback */
787 err = AudioDeviceCreateIOProcID( p_sys->i_selected_dev,
788 (AudioDeviceIOProc)RenderCallbackSPDIF,
793 msg_Err( p_aout, "AudioDeviceCreateIOProcID failed: [%4.4s]", (char *)&err );
797 /* Check for the difference between the Device clock and mdate */
798 p_sys->clock_diff = - (mtime_t)
799 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
800 p_sys->clock_diff += mdate();
803 err = AudioDeviceStart( p_sys->i_selected_dev, p_sys->i_procID );
806 msg_Err( p_aout, "AudioDeviceStart failed: [%4.4s]", (char *)&err );
808 err = AudioDeviceDestroyIOProcID( p_sys->i_selected_dev,
812 msg_Err( p_aout, "AudioDeviceDestroyIOProcID failed: [%4.4s]", (char *)&err );
821 /*****************************************************************************
822 * Close: Close HAL AudioUnit
823 *****************************************************************************/
824 static void Close( vlc_object_t * p_this )
826 aout_instance_t *p_aout = (aout_instance_t *)p_this;
827 struct aout_sys_t *p_sys = p_aout->output.p_sys;
828 OSStatus err = noErr;
829 UInt32 i_param_size = 0;
833 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
834 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
835 verify_noerr( CloseComponent( p_sys->au_unit ) );
838 if( p_sys->b_digital )
841 err = AudioDeviceStop( p_sys->i_selected_dev,
845 msg_Err( p_aout, "AudioDeviceStop failed: [%4.4s]", (char *)&err );
848 /* Remove IOProc callback */
849 err = AudioDeviceDestroyIOProcID( p_sys->i_selected_dev,
853 msg_Err( p_aout, "AudioDeviceDestroyIOProcID failed: [%4.4s]", (char *)&err );
856 if( p_sys->b_revert )
858 AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->sfmt_revert );
861 if( p_sys->b_changed_mixing && p_sys->sfmt_revert.mFormatID != kAudioFormat60958AC3 )
865 /* Revert mixable to true if we are allowed to */
866 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
867 &i_param_size, &b_writeable );
869 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
870 &i_param_size, &b_mix );
872 if( !err && b_writeable )
874 msg_Dbg( p_aout, "mixable is: %d", b_mix );
876 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
877 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
882 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
887 err = AudioHardwareRemovePropertyListener( kAudioHardwarePropertyDevices,
892 msg_Err( p_aout, "AudioHardwareRemovePropertyListener failed: [%4.4s]", (char *)&err );
895 if( p_sys->i_hog_pid == getpid() )
897 p_sys->i_hog_pid = -1;
898 i_param_size = sizeof( p_sys->i_hog_pid );
899 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
900 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
901 if( err != noErr ) msg_Err( p_aout, "Could not release hogmode: [%4.4s]", (char *)&err );
907 /*****************************************************************************
908 * Play: nothing to do
909 *****************************************************************************/
910 static void Play( aout_instance_t * p_aout )
916 /*****************************************************************************
917 * Probe: Check which devices the OS has, and add them to our audio-device menu
918 *****************************************************************************/
919 static void Probe( aout_instance_t * p_aout )
921 OSStatus err = noErr;
922 UInt32 i = 0, i_param_size = 0;
923 AudioDeviceID devid_def = 0;
924 AudioDeviceID *p_devices = NULL;
925 vlc_value_t val, text;
927 struct aout_sys_t *p_sys = p_aout->output.p_sys;
929 /* Get number of devices */
930 err = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices,
931 &i_param_size, NULL );
934 msg_Err( p_aout, "Could not get number of devices: [%4.4s]", (char *)&err );
938 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
940 if( p_sys->i_devices < 1 )
942 msg_Err( p_aout, "No audio output devices were found." );
946 msg_Dbg( p_aout, "system has [%u] device(s)", p_sys->i_devices );
948 /* Allocate DeviceID array */
949 p_devices = (AudioDeviceID*)malloc( sizeof(AudioDeviceID) * p_sys->i_devices );
950 if( p_devices == NULL )
953 /* Populate DeviceID array */
954 err = AudioHardwareGetProperty( kAudioHardwarePropertyDevices,
955 &i_param_size, p_devices );
958 msg_Err( p_aout, "could not get the device IDs: [%4.4s]", (char *)&err );
962 /* Find the ID of the default Device */
963 i_param_size = sizeof( AudioDeviceID );
964 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
965 &i_param_size, &devid_def );
968 msg_Err( p_aout, "could not get default audio device: [%4.4s]", (char *)&err );
971 p_sys->i_default_dev = devid_def;
973 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
974 text.psz_string = (char*)_("Audio Device");
975 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
977 for( i = 0; i < p_sys->i_devices; i++ )
982 /* Retrieve the length of the device name */
983 err = AudioDeviceGetPropertyInfo(
984 p_devices[i], 0, false,
985 kAudioDevicePropertyDeviceName,
986 &i_param_size, NULL);
987 if( err ) goto error;
989 /* Retrieve the name of the device */
990 psz_name = (char *)malloc( i_param_size );
991 err = AudioDeviceGetProperty(
992 p_devices[i], 0, false,
993 kAudioDevicePropertyDeviceName,
994 &i_param_size, psz_name);
995 if( err ) goto error;
997 msg_Dbg( p_aout, "DevID: %u DevName: %s", p_devices[i], psz_name );
999 if( !AudioDeviceHasOutput( p_devices[i]) )
1001 msg_Dbg( p_aout, "this device is INPUT only. skipping..." );
1006 /* Add the menu entries */
1007 val.i_int = (int)p_devices[i];
1008 text.psz_string = psz_name;
1009 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1010 text.psz_string = NULL;
1011 if( p_sys->i_default_dev == p_devices[i] )
1013 /* The default device is the selected device normally */
1014 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1015 var_Set( p_aout, "audio-device", val );
1018 if( AudioDeviceSupportsDigital( p_aout, p_devices[i] ) )
1020 val.i_int = (int)p_devices[i] | AOUT_VAR_SPDIF_FLAG;
1021 if( asprintf( &text.psz_string, _("%s (Encoded Output)"), psz_name ) != -1 )
1023 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1024 free( text.psz_string );
1025 if( p_sys->i_default_dev == p_devices[i]
1026 && var_InheritBool( p_aout, "spdif" ) )
1028 /* We selected to prefer SPDIF output if available
1029 * then this "dummy" entry should be selected */
1030 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1031 var_Set( p_aout, "audio-device", val );
1039 /* If a device is already "preselected", then use this device */
1040 var_Get( p_aout->p_libvlc, "macosx-audio-device", &val );
1043 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1044 var_Set( p_aout, "audio-device", val );
1047 /* If we change the device we want to use, we should renegotiate the audio chain */
1048 var_AddCallback( p_aout, "audio-device", AudioDeviceCallback, NULL );
1050 /* Attach a Listener so that we are notified of a change in the Device setup */
1051 err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
1061 msg_Warn( p_aout, "audio device already in use" );
1066 /*****************************************************************************
1067 * AudioDeviceHasOutput: Checks if the Device actually provides any outputs at all
1068 *****************************************************************************/
1069 static int AudioDeviceHasOutput( AudioDeviceID i_dev_id )
1074 verify_noerr( AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE, kAudioDevicePropertyStreams, &dataSize, &isWritable) );
1075 if (dataSize == 0) return FALSE;
1080 /*****************************************************************************
1081 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
1082 *****************************************************************************/
1083 static int AudioDeviceSupportsDigital( aout_instance_t *p_aout, AudioDeviceID i_dev_id )
1085 OSStatus err = noErr;
1086 UInt32 i_param_size = 0;
1087 AudioStreamID *p_streams = NULL;
1088 int i = 0, i_streams = 0;
1089 bool b_return = false;
1091 /* Retrieve all the output streams */
1092 err = AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE,
1093 kAudioDevicePropertyStreams,
1094 &i_param_size, NULL );
1097 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1101 i_streams = i_param_size / sizeof( AudioStreamID );
1102 p_streams = (AudioStreamID *)malloc( i_param_size );
1103 if( p_streams == NULL )
1106 err = AudioDeviceGetProperty( i_dev_id, 0, FALSE,
1107 kAudioDevicePropertyStreams,
1108 &i_param_size, p_streams );
1112 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1116 for( i = 0; i < i_streams; i++ )
1118 if( AudioStreamSupportsDigital( p_aout, p_streams[i] ) )
1126 /*****************************************************************************
1127 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
1128 *****************************************************************************/
1129 static int AudioStreamSupportsDigital( aout_instance_t *p_aout, AudioStreamID i_stream_id )
1131 OSStatus err = noErr;
1132 UInt32 i_param_size = 0;
1133 AudioStreamBasicDescription *p_format_list = NULL;
1134 int i = 0, i_formats = 0;
1135 bool b_return = false;
1137 /* Retrieve all the stream formats supported by each output stream */
1138 err = AudioStreamGetPropertyInfo( i_stream_id, 0,
1139 kAudioStreamPropertyPhysicalFormats,
1140 &i_param_size, NULL );
1143 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
1147 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
1148 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
1149 if( p_format_list == NULL )
1152 err = AudioStreamGetProperty( i_stream_id, 0,
1153 kAudioStreamPropertyPhysicalFormats,
1154 &i_param_size, p_format_list );
1157 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
1158 free( p_format_list);
1159 p_format_list = NULL;
1163 for( i = 0; i < i_formats; i++ )
1165 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format: ", p_format_list[i] ) );
1167 if( p_format_list[i].mFormatID == 'IAC3' ||
1168 p_format_list[i].mFormatID == kAudioFormat60958AC3 )
1174 free( p_format_list );
1178 /*****************************************************************************
1179 * AudioStreamChangeFormat: Change i_stream_id to change_format
1180 *****************************************************************************/
1181 static int AudioStreamChangeFormat( aout_instance_t *p_aout, AudioStreamID i_stream_id, AudioStreamBasicDescription change_format )
1183 OSStatus err = noErr;
1184 UInt32 i_param_size = 0;
1187 struct { vlc_mutex_t lock; vlc_cond_t cond; } w;
1189 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "setting stream format: ", change_format ) );
1191 /* Condition because SetProperty is asynchronious */
1192 vlc_cond_init( &w.cond );
1193 vlc_mutex_init( &w.lock );
1194 vlc_mutex_lock( &w.lock );
1196 /* Install the callback */
1197 err = AudioStreamAddPropertyListener( i_stream_id, 0,
1198 kAudioStreamPropertyPhysicalFormat,
1199 StreamListener, (void *)&w );
1202 msg_Err( p_aout, "AudioStreamAddPropertyListener failed: [%4.4s]", (char *)&err );
1206 /* change the format */
1207 err = AudioStreamSetProperty( i_stream_id, 0, 0,
1208 kAudioStreamPropertyPhysicalFormat,
1209 sizeof( AudioStreamBasicDescription ),
1213 msg_Err( p_aout, "could not set the stream format: [%4.4s]", (char *)&err );
1217 /* The AudioStreamSetProperty is not only asynchronious (requiring the locks)
1218 * it is also not atomic in its behaviour.
1219 * Therefore we check 5 times before we really give up.
1220 * FIXME: failing isn't actually implemented yet. */
1221 for( i = 0; i < 5; i++ )
1223 AudioStreamBasicDescription actual_format;
1224 mtime_t timeout = mdate() + 500000;
1226 if( vlc_cond_timedwait( &w.cond, &w.lock, timeout ) )
1228 msg_Dbg( p_aout, "reached timeout" );
1231 i_param_size = sizeof( AudioStreamBasicDescription );
1232 err = AudioStreamGetProperty( i_stream_id, 0,
1233 kAudioStreamPropertyPhysicalFormat,
1237 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "actual format in use: ", actual_format ) );
1238 if( actual_format.mSampleRate == change_format.mSampleRate &&
1239 actual_format.mFormatID == change_format.mFormatID &&
1240 actual_format.mFramesPerPacket == change_format.mFramesPerPacket )
1242 /* The right format is now active */
1245 /* We need to check again */
1248 /* Removing the property listener */
1249 err = AudioStreamRemovePropertyListener( i_stream_id, 0,
1250 kAudioStreamPropertyPhysicalFormat,
1254 msg_Err( p_aout, "AudioStreamRemovePropertyListener failed: [%4.4s]", (char *)&err );
1258 /* Destroy the lock and condition */
1259 vlc_mutex_unlock( &w.lock );
1260 vlc_mutex_destroy( &w.lock );
1261 vlc_cond_destroy( &w.cond );
1266 /*****************************************************************************
1267 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
1268 * us to provide some more audio data.
1269 * Don't print anything during normal playback, calling blocking function from
1270 * this callback is not allowed.
1271 *****************************************************************************/
1272 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
1273 AudioUnitRenderActionFlags *ioActionFlags,
1274 const AudioTimeStamp *inTimeStamp,
1275 unsigned int inBusNumber,
1276 unsigned int inNumberFrames,
1277 AudioBufferList *ioData )
1279 AudioTimeStamp host_time;
1280 mtime_t current_date = 0;
1281 uint32_t i_mData_bytes = 0;
1283 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
1284 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1286 VLC_UNUSED(ioActionFlags);
1287 VLC_UNUSED(inBusNumber);
1288 VLC_UNUSED(inNumberFrames);
1290 host_time.mFlags = kAudioTimeStampHostTimeValid;
1291 AudioDeviceTranslateTime( p_sys->i_selected_dev, inTimeStamp, &host_time );
1293 /* Check for the difference between the Device clock and mdate */
1294 p_sys->clock_diff = - (mtime_t)
1295 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
1296 p_sys->clock_diff += mdate();
1298 current_date = p_sys->clock_diff +
1299 AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
1300 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1302 if( ioData == NULL && ioData->mNumberBuffers < 1 )
1304 msg_Err( p_aout, "no iodata or buffers");
1307 if( ioData->mNumberBuffers > 1 )
1308 msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
1311 if( p_sys->i_total_bytes > 0 )
1313 i_mData_bytes = __MIN( p_sys->i_total_bytes - p_sys->i_read_bytes, ioData->mBuffers[0].mDataByteSize );
1314 vlc_memcpy( ioData->mBuffers[0].mData,
1315 &p_sys->p_remainder_buffer[p_sys->i_read_bytes],
1317 p_sys->i_read_bytes += i_mData_bytes;
1318 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1319 ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1321 if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
1322 p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
1325 while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
1327 /* We don't have enough data yet */
1328 aout_buffer_t * p_buffer;
1329 p_buffer = aout_OutputNextBuffer( p_aout, current_date , false );
1331 if( p_buffer != NULL )
1333 uint32_t i_second_mData_bytes = __MIN( p_buffer->i_buffer, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1335 vlc_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes,
1336 p_buffer->p_buffer, i_second_mData_bytes );
1337 i_mData_bytes += i_second_mData_bytes;
1339 if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
1341 p_sys->i_total_bytes = p_buffer->i_buffer - i_second_mData_bytes;
1342 vlc_memcpy( p_sys->p_remainder_buffer,
1343 &p_buffer->p_buffer[i_second_mData_bytes],
1344 p_sys->i_total_bytes );
1345 aout_BufferFree( p_buffer );
1350 /* update current_date */
1351 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1352 ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1354 aout_BufferFree( p_buffer );
1358 vlc_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes,
1359 0,ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1360 i_mData_bytes += ioData->mBuffers[0].mDataByteSize - i_mData_bytes;
1366 /*****************************************************************************
1367 * RenderCallbackSPDIF: callback for SPDIF audio output
1368 *****************************************************************************/
1369 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice,
1370 const AudioTimeStamp * inNow,
1371 const void * inInputData,
1372 const AudioTimeStamp * inInputTime,
1373 AudioBufferList * outOutputData,
1374 const AudioTimeStamp * inOutputTime,
1375 void * threadGlobals )
1377 aout_buffer_t * p_buffer;
1378 mtime_t current_date;
1380 aout_instance_t * p_aout = (aout_instance_t *)threadGlobals;
1381 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1383 VLC_UNUSED(inDevice);
1384 VLC_UNUSED(inInputData);
1385 VLC_UNUSED(inInputTime);
1387 /* Check for the difference between the Device clock and mdate */
1388 p_sys->clock_diff = - (mtime_t)
1389 AudioConvertHostTimeToNanos( inNow->mHostTime ) / 1000;
1390 p_sys->clock_diff += mdate();
1392 current_date = p_sys->clock_diff +
1393 AudioConvertHostTimeToNanos( inOutputTime->mHostTime ) / 1000;
1394 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1396 p_buffer = aout_OutputNextBuffer( p_aout, current_date, true );
1398 #define BUFFER outOutputData->mBuffers[p_sys->i_stream_index]
1399 if( p_buffer != NULL )
1401 if( (int)BUFFER.mDataByteSize != (int)p_buffer->i_buffer)
1402 msg_Warn( p_aout, "bytesize: %d nb_bytes: %d", (int)BUFFER.mDataByteSize, (int)p_buffer->i_buffer );
1404 /* move data into output data buffer */
1405 vlc_memcpy( BUFFER.mData, p_buffer->p_buffer, p_buffer->i_buffer );
1406 aout_BufferFree( p_buffer );
1410 vlc_memset( BUFFER.mData, 0, BUFFER.mDataByteSize );
1417 /*****************************************************************************
1418 * HardwareListener: Warns us of changes in the list of registered devices
1419 *****************************************************************************/
1420 static OSStatus HardwareListener( AudioHardwarePropertyID inPropertyID,
1421 void * inClientData )
1423 OSStatus err = noErr;
1424 aout_instance_t *p_aout = (aout_instance_t *)inClientData;
1426 switch( inPropertyID )
1428 case kAudioHardwarePropertyDevices:
1430 /* something changed in the list of devices */
1431 /* We trigger the audio-device's aout_ChannelsRestart callback */
1432 var_TriggerCallback( p_aout, "audio-device" );
1433 var_Destroy( p_aout, "audio-device" );
1441 /*****************************************************************************
1443 *****************************************************************************/
1444 static OSStatus StreamListener( AudioStreamID inStream,
1446 AudioDevicePropertyID inPropertyID,
1447 void * inClientData )
1449 OSStatus err = noErr;
1450 struct { vlc_mutex_t lock; vlc_cond_t cond; } * w = inClientData;
1452 VLC_UNUSED(inStream);
1453 VLC_UNUSED(inChannel);
1455 switch( inPropertyID )
1457 case kAudioStreamPropertyPhysicalFormat:
1458 vlc_mutex_lock( &w->lock );
1459 vlc_cond_signal( &w->cond );
1460 vlc_mutex_unlock( &w->lock );
1469 /*****************************************************************************
1470 * AudioDeviceCallback: Callback triggered when the audio-device variable is changed
1471 *****************************************************************************/
1472 static int AudioDeviceCallback( vlc_object_t *p_this, const char *psz_variable,
1473 vlc_value_t old_val, vlc_value_t new_val, void *param )
1475 aout_instance_t *p_aout = (aout_instance_t *)p_this;
1476 var_Set( p_aout->p_libvlc, "macosx-audio-device", new_val );
1477 msg_Dbg( p_aout, "Set Device: %#"PRIx64, new_val.i_int );
1478 return aout_ChannelsRestart( p_this, psz_variable, old_val, new_val, param );