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/AudioUnit.h>
44 #include <AudioUnit/AudioUnitProperties.h>
45 #include <AudioUnit/AudioUnitParameters.h>
46 #include <AudioUnit/AudioOutputUnit.h>
47 #include <AudioToolbox/AudioFormat.h>
50 #define verify_noerr(a) assert((a) == noErr)
53 #if AUDIO_UNIT_VERSION < 1060
54 #define AudioComponent Component
55 #define AudioComponentDescription ComponentDescription
56 #define AudioComponentFindNext FindNextComponent
57 #define AudioComponentInstanceNew OpenAComponent
58 #define AudioComponentInstanceDispose CloseComponent
59 #define AudioComponentInstanceNew OpenAComponent
60 #define AudioComponentInstanceNew OpenAComponent
62 #include <AudioUnit/AudioComponent.h>
65 #define STREAM_FORMAT_MSG( pre, sfm ) \
66 pre "[%u][%4.4s][%u][%u][%u][%u][%u][%u]", \
67 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
68 sfm.mFormatFlags, sfm.mBytesPerPacket, \
69 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
70 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
72 #define STREAM_FORMAT_MSG_FULL( pre, sfm ) \
73 pre ":\nsamplerate: [%u]\nFormatID: [%4.4s]\nFormatFlags: [%u]\nBypesPerPacket: [%u]\nFramesPerPacket: [%u]\nBytesPerFrame: [%u]\nChannelsPerFrame: [%u]\nBitsPerChannel[%u]", \
74 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
75 sfm.mFormatFlags, sfm.mBytesPerPacket, \
76 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
77 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
79 #define FRAMESIZE 2048
80 #define BUFSIZE (FRAMESIZE * 8)
81 #define AOUT_VAR_SPDIF_FLAG 0xf00000
85 * - clean up the debug info
87 * - be better at changing stream setup or devices setup changes while playing.
91 /*****************************************************************************
92 * aout_sys_t: private audio output method descriptor
93 *****************************************************************************
94 * This structure is part of the audio output thread descriptor.
95 * It describes the CoreAudio specific properties of an output thread.
96 *****************************************************************************/
99 AudioDeviceID i_default_dev; /* Keeps DeviceID of defaultOutputDevice */
100 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device */
101 UInt32 i_devices; /* Number of CoreAudio Devices */
102 bool b_supports_digital;/* Does the currently selected device support digital mode? */
103 bool b_digital; /* Are we running in digital mode? */
104 mtime_t clock_diff; /* Difference between VLC clock and Device clock */
107 AudioComponent au_component; /* The Audiocomponent we use */
108 AudioUnit au_unit; /* The AudioUnit we use */
109 uint8_t p_remainder_buffer[BUFSIZE];
110 uint32_t i_read_bytes;
111 uint32_t i_total_bytes;
113 /* CoreAudio SPDIF mode specific */
114 pid_t i_hog_pid; /* The keep the pid of our hog status */
115 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */
116 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */
117 AudioStreamBasicDescription stream_format; /* The format we changed the stream to */
118 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */
119 bool b_revert; /* Wether we need to revert the stream format */
120 bool b_changed_mixing;/* Wether we need to set the mixing mode back */
123 /*****************************************************************************
125 *****************************************************************************/
126 static int Open ( vlc_object_t * );
127 static int OpenAnalog ( aout_instance_t * );
128 static int OpenSPDIF ( aout_instance_t * );
129 static void Close ( vlc_object_t * );
131 static void Play ( aout_instance_t * );
132 static void Probe ( aout_instance_t * );
134 static int AudioDeviceHasOutput ( AudioDeviceID );
135 static int AudioDeviceSupportsDigital( aout_instance_t *, AudioDeviceID );
136 static int AudioStreamSupportsDigital( aout_instance_t *, AudioStreamID );
137 static int AudioStreamChangeFormat ( aout_instance_t *, AudioStreamID, AudioStreamBasicDescription );
139 static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
140 unsigned int, unsigned int, AudioBufferList *);
141 static OSStatus RenderCallbackSPDIF ( AudioDeviceID, const AudioTimeStamp *, const void *, const AudioTimeStamp *,
142 AudioBufferList *, const AudioTimeStamp *, void * );
143 static OSStatus HardwareListener ( AudioHardwarePropertyID, void *);
144 static OSStatus StreamListener ( AudioStreamID, UInt32,
145 AudioDevicePropertyID, void * );
146 static int AudioDeviceCallback ( vlc_object_t *, const char *,
147 vlc_value_t, vlc_value_t, void * );
151 /*****************************************************************************
153 *****************************************************************************/
154 #define ADEV_TEXT N_("Audio Device")
155 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
156 "audio device, as listed in your 'Audio Device' menu. This device will " \
157 "then be used by default for audio playback.")
160 set_shortname( "auhal" )
161 set_description( N_("HAL AudioUnit output") )
162 set_capability( "audio output", 101 )
163 set_category( CAT_AUDIO )
164 set_subcategory( SUBCAT_AUDIO_AOUT )
165 set_callbacks( Open, Close )
166 add_integer( "macosx-audio-device", 0, NULL, ADEV_TEXT, ADEV_LONGTEXT, false )
169 /*****************************************************************************
170 * Open: open macosx audio output
171 *****************************************************************************/
172 static int Open( vlc_object_t * p_this )
174 OSStatus err = noErr;
175 UInt32 i_param_size = 0;
176 struct aout_sys_t *p_sys = NULL;
178 aout_instance_t *p_aout = (aout_instance_t *)p_this;
180 /* Use int here, to match kAudioDevicePropertyDeviceIsAlive
184 /* Allocate structure */
185 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
186 if( p_aout->output.p_sys == NULL )
189 p_sys = p_aout->output.p_sys;
190 p_sys->i_default_dev = 0;
191 p_sys->i_selected_dev = 0;
192 p_sys->i_devices = 0;
193 p_sys->b_supports_digital = false;
194 p_sys->b_digital = false;
195 p_sys->au_component = NULL;
196 p_sys->au_unit = NULL;
197 p_sys->clock_diff = (mtime_t) 0;
198 p_sys->i_read_bytes = 0;
199 p_sys->i_total_bytes = 0;
200 p_sys->i_hog_pid = -1;
201 p_sys->i_stream_id = 0;
202 p_sys->i_stream_index = -1;
203 p_sys->b_revert = false;
204 p_sys->b_changed_mixing = false;
205 memset( p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE );
207 p_aout->output.pf_play = Play;
209 aout_FormatPrint( p_aout, "VLC is looking for:", (audio_sample_format_t *)&p_aout->output.output );
211 /* Persistent device variable */
212 if( var_Type( p_aout->p_libvlc, "macosx-audio-device" ) == 0 )
214 var_Create( p_aout->p_libvlc, "macosx-audio-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
217 /* Build a list of devices */
218 if( var_Type( p_aout, "audio-device" ) == 0 )
223 /* What device do we want? */
224 if( var_Get( p_aout, "audio-device", &val ) < 0 )
226 msg_Err( p_aout, "audio-device var does not exist. device probe failed." );
230 p_sys->i_selected_dev = val.i_int & ~AOUT_VAR_SPDIF_FLAG; /* remove SPDIF flag to get the true DeviceID */
231 p_sys->b_supports_digital = ( val.i_int & AOUT_VAR_SPDIF_FLAG ) ? true : false;
233 /* Check if the desired device is alive and usable */
234 /* TODO: add a callback to the device to alert us if the device dies */
235 i_param_size = sizeof( b_alive );
236 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
237 kAudioDevicePropertyDeviceIsAlive,
238 &i_param_size, &b_alive );
242 /* Be tolerant, only give a warning here */
243 msg_Warn( p_aout, "could not check whether device [0x%x] is alive: %4.4s", (unsigned int)p_sys->i_selected_dev, (char *)&err );
247 if( b_alive == false )
249 msg_Warn( p_aout, "selected audio device is not alive, switching to default device" );
250 p_sys->i_selected_dev = p_sys->i_default_dev;
253 i_param_size = sizeof( p_sys->i_hog_pid );
254 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
255 kAudioDevicePropertyHogMode,
256 &i_param_size, &p_sys->i_hog_pid );
260 /* This is not a fatal error. Some drivers simply don't support this property */
261 msg_Warn( p_aout, "could not check whether device is hogged: %4.4s",
263 p_sys->i_hog_pid = -1;
266 if( p_sys->i_hog_pid != -1 && p_sys->i_hog_pid != getpid() )
268 msg_Err( p_aout, "Selected audio device is exclusively in use by another program." );
269 dialog_Fatal( p_aout, _("Audio output failed"), "%s",
270 _("The selected audio output device is exclusively in "
271 "use by another program.") );
275 /* Check for Digital mode or Analog output mode */
276 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
278 if( OpenSPDIF( p_aout ) )
283 if( OpenAnalog( p_aout ) )
288 /* If we reach this, this aout has failed */
289 var_Destroy( p_aout, "audio-device" );
294 /*****************************************************************************
295 * Open: open and setup a HAL AudioUnit to do analog (multichannel) audio output
296 *****************************************************************************/
297 static int OpenAnalog( aout_instance_t *p_aout )
299 struct aout_sys_t *p_sys = p_aout->output.p_sys;
300 OSStatus err = noErr;
301 UInt32 i_param_size = 0, i = 0;
303 AudioComponentDescription desc;
304 AudioStreamBasicDescription DeviceFormat;
305 AudioChannelLayout *layout;
306 AudioChannelLayout new_layout;
307 AURenderCallbackStruct input;
309 /* Lets go find our Component */
310 desc.componentType = kAudioUnitType_Output;
311 desc.componentSubType = kAudioUnitSubType_HALOutput;
312 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
313 desc.componentFlags = 0;
314 desc.componentFlagsMask = 0;
316 p_sys->au_component = AudioComponentFindNext( NULL, &desc );
317 if( p_sys->au_component == NULL )
319 msg_Warn( p_aout, "we cannot find our HAL component" );
323 err = AudioComponentInstanceNew( p_sys->au_component, &p_sys->au_unit );
326 msg_Warn( p_aout, "we cannot open our HAL component" );
330 /* Set the device we will use for this output unit */
331 err = AudioUnitSetProperty( p_sys->au_unit,
332 kAudioOutputUnitProperty_CurrentDevice,
333 kAudioUnitScope_Global,
335 &p_sys->i_selected_dev,
336 sizeof( AudioDeviceID ));
340 msg_Warn( p_aout, "we cannot select the audio device" );
344 /* Get the current format */
345 i_param_size = sizeof(AudioStreamBasicDescription);
347 err = AudioUnitGetProperty( p_sys->au_unit,
348 kAudioUnitProperty_StreamFormat,
349 kAudioUnitScope_Input,
354 if( err != noErr ) return false;
355 else msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is: ", DeviceFormat ) );
357 /* Get the channel layout of the device side of the unit (vlc -> unit -> device) */
358 err = AudioUnitGetPropertyInfo( p_sys->au_unit,
359 kAudioDevicePropertyPreferredChannelLayout,
360 kAudioUnitScope_Output,
367 layout = (AudioChannelLayout *)malloc( i_param_size);
369 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
370 kAudioDevicePropertyPreferredChannelLayout,
371 kAudioUnitScope_Output,
376 /* We need to "fill out" the ChannelLayout, because there are multiple ways that it can be set */
377 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
379 /* bitmap defined channellayout */
380 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
381 sizeof( UInt32), &layout->mChannelBitmap,
385 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
387 /* layouttags defined channellayout */
388 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
389 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
394 msg_Dbg( p_aout, "layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
396 /* Initialize the VLC core channel count */
397 p_aout->output.output.i_physical_channels = 0;
398 i_original = p_aout->output.output.i_original_channels & AOUT_CHAN_PHYSMASK;
400 if( i_original == AOUT_CHAN_CENTER || layout->mNumberChannelDescriptions < 2 )
402 /* We only need Mono or cannot output more than 1 channel */
403 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
405 else if( i_original == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) || layout->mNumberChannelDescriptions < 3 )
407 /* We only need Stereo or cannot output more than 2 channels */
408 p_aout->output.output.i_physical_channels = AOUT_CHAN_RIGHT | AOUT_CHAN_LEFT;
412 /* We want more than stereo and we can do that */
413 for( i = 0; i < layout->mNumberChannelDescriptions; i++ )
415 msg_Dbg( p_aout, "this is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
417 switch( layout->mChannelDescriptions[i].mChannelLabel )
419 case kAudioChannelLabel_Left:
420 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
422 case kAudioChannelLabel_Right:
423 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
425 case kAudioChannelLabel_Center:
426 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
428 case kAudioChannelLabel_LFEScreen:
429 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE;
431 case kAudioChannelLabel_LeftSurround:
432 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT;
434 case kAudioChannelLabel_RightSurround:
435 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
437 case kAudioChannelLabel_RearSurroundLeft:
438 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
440 case kAudioChannelLabel_RearSurroundRight:
441 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
443 case kAudioChannelLabel_CenterSurround:
444 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER;
447 msg_Warn( p_aout, "unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
450 if( p_aout->output.output.i_physical_channels == 0 )
452 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
453 msg_Err( p_aout, "You should configure your speaker layout with Audio Midi Setup Utility in /Applications/Utilities. Now using Stereo mode." );
454 dialog_Fatal( p_aout, _("Audio device is not configured"), "%s",
455 _("You should configure your speaker layout with "
456 "the \"Audio Midi Setup\" utility in /Applications/"
457 "Utilities. Stereo mode is being used now.") );
464 msg_Warn( p_aout, "this driver does not support kAudioDevicePropertyPreferredChannelLayout. BAD DRIVER AUTHOR !!!" );
465 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
468 msg_Dbg( p_aout, "selected %d physical channels for device output", aout_FormatNbChannels( &p_aout->output.output ) );
469 msg_Dbg( p_aout, "VLC will output: %s", aout_FormatPrintChannels( &p_aout->output.output ));
471 memset (&new_layout, 0, sizeof(new_layout));
472 switch( aout_FormatNbChannels( &p_aout->output.output ) )
475 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
478 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
481 if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
483 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
485 else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
487 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
491 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
493 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
495 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
497 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
499 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
501 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
505 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
507 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
509 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
511 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
515 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
517 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
521 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_6_0; // L R Ls Rs C Cs
525 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
526 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
529 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
530 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
534 /* Set up the format to be used */
535 DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
536 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
538 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
539 p_aout->output.output.i_format = VLC_CODEC_FL32;
540 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
541 DeviceFormat.mBitsPerChannel = 32;
542 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output );
544 /* Calculate framesizes and stuff */
545 DeviceFormat.mFramesPerPacket = 1;
546 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
547 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
549 /* Set the desired format */
550 i_param_size = sizeof(AudioStreamBasicDescription);
551 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
552 kAudioUnitProperty_StreamFormat,
553 kAudioUnitScope_Input,
558 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
560 /* Retrieve actual format */
561 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
562 kAudioUnitProperty_StreamFormat,
563 kAudioUnitScope_Input,
568 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
570 /* Do the last VLC aout setups */
571 aout_FormatPrepare( &p_aout->output.output );
572 p_aout->output.i_nb_samples = FRAMESIZE;
573 aout_VolumeSoftInit( p_aout );
575 /* set the IOproc callback */
576 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
577 input.inputProcRefCon = p_aout;
579 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
580 kAudioUnitProperty_SetRenderCallback,
581 kAudioUnitScope_Input,
582 0, &input, sizeof( input ) ) );
584 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
585 input.inputProcRefCon = p_aout;
587 /* Set the new_layout as the layout VLC will use to feed the AU unit */
588 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
589 kAudioUnitProperty_AudioChannelLayout,
590 kAudioUnitScope_Input,
591 0, &new_layout, sizeof(new_layout) ) );
593 if( new_layout.mNumberChannelDescriptions > 0 )
594 free( new_layout.mChannelDescriptions );
597 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
599 /* Find the difference between device clock and mdate clock */
600 p_sys->clock_diff = - (mtime_t)
601 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
602 p_sys->clock_diff += mdate();
605 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
610 /*****************************************************************************
611 * Setup a encoded digital stream (SPDIF)
612 *****************************************************************************/
613 static int OpenSPDIF( aout_instance_t * p_aout )
615 struct aout_sys_t *p_sys = p_aout->output.p_sys;
616 OSStatus err = noErr;
617 UInt32 i_param_size = 0, b_mix = 0;
618 Boolean b_writeable = false;
619 AudioStreamID *p_streams = NULL;
620 int i = 0, i_streams = 0;
622 /* Start doing the SPDIF setup proces */
623 p_sys->b_digital = true;
626 i_param_size = sizeof( p_sys->i_hog_pid );
627 p_sys->i_hog_pid = getpid() ;
629 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
630 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
634 msg_Err( p_aout, "failed to set hogmode: [%4.4s]", (char *)&err );
638 /* Set mixable to false if we are allowed to */
639 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
640 &i_param_size, &b_writeable );
642 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
643 &i_param_size, &b_mix );
645 if( !err && b_writeable )
648 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
649 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
650 p_sys->b_changed_mixing = true;
655 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
659 /* Get a list of all the streams on this device */
660 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE,
661 kAudioDevicePropertyStreams,
662 &i_param_size, NULL );
665 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
669 i_streams = i_param_size / sizeof( AudioStreamID );
670 p_streams = (AudioStreamID *)malloc( i_param_size );
671 if( p_streams == NULL )
674 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
675 kAudioDevicePropertyStreams,
676 &i_param_size, p_streams );
680 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
685 for( i = 0; i < i_streams && p_sys->i_stream_index < 0 ; i++ )
687 /* Find a stream with a cac3 stream */
688 AudioStreamBasicDescription *p_format_list = NULL;
689 int i_formats = 0, j = 0;
690 bool b_digital = false;
692 /* Retrieve all the stream formats supported by each output stream */
693 err = AudioStreamGetPropertyInfo( p_streams[i], 0,
694 kAudioStreamPropertyPhysicalFormats,
695 &i_param_size, NULL );
698 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
702 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
703 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
704 if( p_format_list == NULL )
707 err = AudioStreamGetProperty( p_streams[i], 0,
708 kAudioStreamPropertyPhysicalFormats,
709 &i_param_size, p_format_list );
712 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
713 free( p_format_list );
717 /* Check if one of the supported formats is a digital format */
718 for( j = 0; j < i_formats; j++ )
720 if( p_format_list[j].mFormatID == 'IAC3' ||
721 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
730 /* if this stream supports a digital (cac3) format, then go set it. */
731 int i_requested_rate_format = -1;
732 int i_current_rate_format = -1;
733 int i_backup_rate_format = -1;
735 p_sys->i_stream_id = p_streams[i];
736 p_sys->i_stream_index = i;
738 if( p_sys->b_revert == false )
740 /* Retrieve the original format of this stream first if not done so already */
741 i_param_size = sizeof( p_sys->sfmt_revert );
742 err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
743 kAudioStreamPropertyPhysicalFormat,
745 &p_sys->sfmt_revert );
748 msg_Err( p_aout, "could not retrieve the original streamformat: [%4.4s]", (char *)&err );
751 p_sys->b_revert = true;
754 for( j = 0; j < i_formats; j++ )
756 if( p_format_list[j].mFormatID == 'IAC3' ||
757 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
759 if( p_format_list[j].mSampleRate == p_aout->output.output.i_rate )
761 i_requested_rate_format = j;
764 else if( p_format_list[j].mSampleRate == p_sys->sfmt_revert.mSampleRate )
766 i_current_rate_format = j;
770 if( i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate )
771 i_backup_rate_format = j;
777 if( i_requested_rate_format >= 0 ) /* We prefer to output at the samplerate of the original audio */
778 p_sys->stream_format = p_format_list[i_requested_rate_format];
779 else if( i_current_rate_format >= 0 ) /* If not possible, we will try to use the current samplerate of the device */
780 p_sys->stream_format = p_format_list[i_current_rate_format];
781 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) */
783 free( p_format_list );
787 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) );
789 if( !AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->stream_format ) )
792 /* Set the format flags */
793 if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian )
794 p_aout->output.output.i_format = VLC_CODEC_SPDIFB;
796 p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
797 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
798 p_aout->output.output.i_frame_length = A52_FRAME_NB;
799 p_aout->output.i_nb_samples = p_aout->output.output.i_frame_length;
800 p_aout->output.output.i_rate = (unsigned int)p_sys->stream_format.mSampleRate;
801 aout_FormatPrepare( &p_aout->output.output );
802 aout_VolumeNoneInit( p_aout );
804 /* Add IOProc callback */
805 err = AudioDeviceAddIOProc( p_sys->i_selected_dev,
806 (AudioDeviceIOProc)RenderCallbackSPDIF,
811 msg_Err( p_aout, "AudioDeviceAddIOProc failed: [%4.4s]", (char *)&err );
815 /* Check for the difference between the Device clock and mdate */
816 p_sys->clock_diff = - (mtime_t)
817 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
818 p_sys->clock_diff += mdate();
821 err = AudioDeviceStart( p_sys->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF );
824 msg_Err( p_aout, "AudioDeviceStart failed: [%4.4s]", (char *)&err );
826 err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
827 (AudioDeviceIOProc)RenderCallbackSPDIF );
830 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
839 /*****************************************************************************
840 * Close: Close HAL AudioUnit
841 *****************************************************************************/
842 static void Close( vlc_object_t * p_this )
844 aout_instance_t *p_aout = (aout_instance_t *)p_this;
845 struct aout_sys_t *p_sys = p_aout->output.p_sys;
846 OSStatus err = noErr;
847 UInt32 i_param_size = 0;
851 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
852 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
853 verify_noerr( AudioComponentInstanceDispose( p_sys->au_unit ) );
856 if( p_sys->b_digital )
859 err = AudioDeviceStop( p_sys->i_selected_dev,
860 (AudioDeviceIOProc)RenderCallbackSPDIF );
863 msg_Err( p_aout, "AudioDeviceStop failed: [%4.4s]", (char *)&err );
866 /* Remove IOProc callback */
867 err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
868 (AudioDeviceIOProc)RenderCallbackSPDIF );
871 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
874 if( p_sys->b_revert )
876 AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->sfmt_revert );
879 if( p_sys->b_changed_mixing && p_sys->sfmt_revert.mFormatID != kAudioFormat60958AC3 )
883 /* Revert mixable to true if we are allowed to */
884 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
885 &i_param_size, &b_writeable );
887 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
888 &i_param_size, &b_mix );
890 if( !err && b_writeable )
892 msg_Dbg( p_aout, "mixable is: %d", b_mix );
894 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
895 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
900 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
905 err = AudioHardwareRemovePropertyListener( kAudioHardwarePropertyDevices,
910 msg_Err( p_aout, "AudioHardwareRemovePropertyListener failed: [%4.4s]", (char *)&err );
913 if( p_sys->i_hog_pid == getpid() )
915 p_sys->i_hog_pid = -1;
916 i_param_size = sizeof( p_sys->i_hog_pid );
917 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
918 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
919 if( err != noErr ) msg_Err( p_aout, "Could not release hogmode: [%4.4s]", (char *)&err );
925 /*****************************************************************************
926 * Play: nothing to do
927 *****************************************************************************/
928 static void Play( aout_instance_t * p_aout )
934 /*****************************************************************************
935 * Probe: Check which devices the OS has, and add them to our audio-device menu
936 *****************************************************************************/
937 static void Probe( aout_instance_t * p_aout )
939 OSStatus err = noErr;
940 UInt32 i = 0, i_param_size = 0;
941 AudioDeviceID devid_def = 0;
942 AudioDeviceID *p_devices = NULL;
943 vlc_value_t val, text;
945 struct aout_sys_t *p_sys = p_aout->output.p_sys;
947 /* Get number of devices */
948 err = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices,
949 &i_param_size, NULL );
952 msg_Err( p_aout, "Could not get number of devices: [%4.4s]", (char *)&err );
956 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
958 if( p_sys->i_devices < 1 )
960 msg_Err( p_aout, "No audio output devices were found." );
964 msg_Dbg( p_aout, "system has [%u] device(s)", p_sys->i_devices );
966 /* Allocate DeviceID array */
967 p_devices = (AudioDeviceID*)malloc( sizeof(AudioDeviceID) * p_sys->i_devices );
968 if( p_devices == NULL )
971 /* Populate DeviceID array */
972 err = AudioHardwareGetProperty( kAudioHardwarePropertyDevices,
973 &i_param_size, p_devices );
976 msg_Err( p_aout, "could not get the device IDs: [%4.4s]", (char *)&err );
980 /* Find the ID of the default Device */
981 i_param_size = sizeof( AudioDeviceID );
982 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
983 &i_param_size, &devid_def );
986 msg_Err( p_aout, "could not get default audio device: [%4.4s]", (char *)&err );
989 p_sys->i_default_dev = devid_def;
991 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
992 text.psz_string = (char*)_("Audio Device");
993 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
995 for( i = 0; i < p_sys->i_devices; i++ )
1000 /* Retrieve the length of the device name */
1001 err = AudioDeviceGetPropertyInfo(
1002 p_devices[i], 0, false,
1003 kAudioDevicePropertyDeviceName,
1004 &i_param_size, NULL);
1005 if( err ) goto error;
1007 /* Retrieve the name of the device */
1008 psz_name = (char *)malloc( i_param_size );
1009 err = AudioDeviceGetProperty(
1010 p_devices[i], 0, false,
1011 kAudioDevicePropertyDeviceName,
1012 &i_param_size, psz_name);
1013 if( err ) goto error;
1015 msg_Dbg( p_aout, "DevID: %u DevName: %s", p_devices[i], psz_name );
1017 if( !AudioDeviceHasOutput( p_devices[i]) )
1019 msg_Dbg( p_aout, "this device is INPUT only. skipping..." );
1024 /* Add the menu entries */
1025 val.i_int = (int)p_devices[i];
1026 text.psz_string = psz_name;
1027 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1028 text.psz_string = NULL;
1029 if( p_sys->i_default_dev == p_devices[i] )
1031 /* The default device is the selected device normally */
1032 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1033 var_Set( p_aout, "audio-device", val );
1036 if( AudioDeviceSupportsDigital( p_aout, p_devices[i] ) )
1038 val.i_int = (int)p_devices[i] | AOUT_VAR_SPDIF_FLAG;
1039 if( asprintf( &text.psz_string, _("%s (Encoded Output)"), psz_name ) != -1 )
1041 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1042 free( text.psz_string );
1043 if( p_sys->i_default_dev == p_devices[i] && config_GetInt( p_aout, "spdif" ) )
1045 /* We selected to prefer SPDIF output if available
1046 * then this "dummy" entry should be selected */
1047 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1048 var_Set( p_aout, "audio-device", val );
1056 /* If a device is already "preselected", then use this device */
1057 var_Get( p_aout->p_libvlc, "macosx-audio-device", &val );
1060 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1061 var_Set( p_aout, "audio-device", val );
1064 /* If we change the device we want to use, we should renegotiate the audio chain */
1065 var_AddCallback( p_aout, "audio-device", AudioDeviceCallback, NULL );
1067 /* Attach a Listener so that we are notified of a change in the Device setup */
1068 err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
1078 msg_Warn( p_aout, "audio device already in use" );
1083 /*****************************************************************************
1084 * AudioDeviceHasOutput: Checks if the Device actually provides any outputs at all
1085 *****************************************************************************/
1086 static int AudioDeviceHasOutput( AudioDeviceID i_dev_id )
1091 verify_noerr( AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE, kAudioDevicePropertyStreams, &dataSize, &isWritable) );
1092 if (dataSize == 0) return FALSE;
1097 /*****************************************************************************
1098 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
1099 *****************************************************************************/
1100 static int AudioDeviceSupportsDigital( aout_instance_t *p_aout, AudioDeviceID i_dev_id )
1102 OSStatus err = noErr;
1103 UInt32 i_param_size = 0;
1104 AudioStreamID *p_streams = NULL;
1105 int i = 0, i_streams = 0;
1106 bool b_return = false;
1108 /* Retrieve all the output streams */
1109 err = AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE,
1110 kAudioDevicePropertyStreams,
1111 &i_param_size, NULL );
1114 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1118 i_streams = i_param_size / sizeof( AudioStreamID );
1119 p_streams = (AudioStreamID *)malloc( i_param_size );
1120 if( p_streams == NULL )
1123 err = AudioDeviceGetProperty( i_dev_id, 0, FALSE,
1124 kAudioDevicePropertyStreams,
1125 &i_param_size, p_streams );
1129 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1133 for( i = 0; i < i_streams; i++ )
1135 if( AudioStreamSupportsDigital( p_aout, p_streams[i] ) )
1143 /*****************************************************************************
1144 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
1145 *****************************************************************************/
1146 static int AudioStreamSupportsDigital( aout_instance_t *p_aout, AudioStreamID i_stream_id )
1148 OSStatus err = noErr;
1149 UInt32 i_param_size = 0;
1150 AudioStreamBasicDescription *p_format_list = NULL;
1151 int i = 0, i_formats = 0;
1152 bool b_return = false;
1154 /* Retrieve all the stream formats supported by each output stream */
1155 err = AudioStreamGetPropertyInfo( i_stream_id, 0,
1156 kAudioStreamPropertyPhysicalFormats,
1157 &i_param_size, NULL );
1160 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
1164 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
1165 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
1166 if( p_format_list == NULL )
1169 err = AudioStreamGetProperty( i_stream_id, 0,
1170 kAudioStreamPropertyPhysicalFormats,
1171 &i_param_size, p_format_list );
1174 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
1175 free( p_format_list);
1176 p_format_list = NULL;
1180 for( i = 0; i < i_formats; i++ )
1182 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format: ", p_format_list[i] ) );
1184 if( p_format_list[i].mFormatID == 'IAC3' ||
1185 p_format_list[i].mFormatID == kAudioFormat60958AC3 )
1191 free( p_format_list );
1195 /*****************************************************************************
1196 * AudioStreamChangeFormat: Change i_stream_id to change_format
1197 *****************************************************************************/
1198 static int AudioStreamChangeFormat( aout_instance_t *p_aout, AudioStreamID i_stream_id, AudioStreamBasicDescription change_format )
1200 OSStatus err = noErr;
1201 UInt32 i_param_size = 0;
1204 struct { vlc_mutex_t lock; vlc_cond_t cond; } w;
1206 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "setting stream format: ", change_format ) );
1208 /* Condition because SetProperty is asynchronious */
1209 vlc_cond_init( &w.cond );
1210 vlc_mutex_init( &w.lock );
1211 vlc_mutex_lock( &w.lock );
1213 /* Install the callback */
1214 err = AudioStreamAddPropertyListener( i_stream_id, 0,
1215 kAudioStreamPropertyPhysicalFormat,
1216 StreamListener, (void *)&w );
1219 msg_Err( p_aout, "AudioStreamAddPropertyListener failed: [%4.4s]", (char *)&err );
1223 /* change the format */
1224 err = AudioStreamSetProperty( i_stream_id, 0, 0,
1225 kAudioStreamPropertyPhysicalFormat,
1226 sizeof( AudioStreamBasicDescription ),
1230 msg_Err( p_aout, "could not set the stream format: [%4.4s]", (char *)&err );
1234 /* The AudioStreamSetProperty is not only asynchronious (requiring the locks)
1235 * it is also not atomic in its behaviour.
1236 * Therefore we check 5 times before we really give up.
1237 * FIXME: failing isn't actually implemented yet. */
1238 for( i = 0; i < 5; i++ )
1240 AudioStreamBasicDescription actual_format;
1241 mtime_t timeout = mdate() + 500000;
1243 if( vlc_cond_timedwait( &w.cond, &w.lock, timeout ) )
1245 msg_Dbg( p_aout, "reached timeout" );
1248 i_param_size = sizeof( AudioStreamBasicDescription );
1249 err = AudioStreamGetProperty( i_stream_id, 0,
1250 kAudioStreamPropertyPhysicalFormat,
1254 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "actual format in use: ", actual_format ) );
1255 if( actual_format.mSampleRate == change_format.mSampleRate &&
1256 actual_format.mFormatID == change_format.mFormatID &&
1257 actual_format.mFramesPerPacket == change_format.mFramesPerPacket )
1259 /* The right format is now active */
1262 /* We need to check again */
1265 /* Removing the property listener */
1266 err = AudioStreamRemovePropertyListener( i_stream_id, 0,
1267 kAudioStreamPropertyPhysicalFormat,
1271 msg_Err( p_aout, "AudioStreamRemovePropertyListener failed: [%4.4s]", (char *)&err );
1275 /* Destroy the lock and condition */
1276 vlc_mutex_unlock( &w.lock );
1277 vlc_mutex_destroy( &w.lock );
1278 vlc_cond_destroy( &w.cond );
1283 /*****************************************************************************
1284 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
1285 * us to provide some more audio data.
1286 * Don't print anything during normal playback, calling blocking function from
1287 * this callback is not allowed.
1288 *****************************************************************************/
1289 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
1290 AudioUnitRenderActionFlags *ioActionFlags,
1291 const AudioTimeStamp *inTimeStamp,
1292 unsigned int inBusNumber,
1293 unsigned int inNumberFrames,
1294 AudioBufferList *ioData )
1296 AudioTimeStamp host_time;
1297 mtime_t current_date = 0;
1298 uint32_t i_mData_bytes = 0;
1300 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
1301 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1303 VLC_UNUSED(ioActionFlags);
1304 VLC_UNUSED(inBusNumber);
1305 VLC_UNUSED(inNumberFrames);
1307 host_time.mFlags = kAudioTimeStampHostTimeValid;
1308 AudioDeviceTranslateTime( p_sys->i_selected_dev, inTimeStamp, &host_time );
1310 /* Check for the difference between the Device clock and mdate */
1311 p_sys->clock_diff = - (mtime_t)
1312 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
1313 p_sys->clock_diff += mdate();
1315 current_date = p_sys->clock_diff +
1316 AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
1317 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1319 if( ioData == NULL && ioData->mNumberBuffers < 1 )
1321 msg_Err( p_aout, "no iodata or buffers");
1324 if( ioData->mNumberBuffers > 1 )
1325 msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
1328 if( p_sys->i_total_bytes > 0 )
1330 i_mData_bytes = __MIN( p_sys->i_total_bytes - p_sys->i_read_bytes, ioData->mBuffers[0].mDataByteSize );
1331 vlc_memcpy( ioData->mBuffers[0].mData,
1332 &p_sys->p_remainder_buffer[p_sys->i_read_bytes],
1334 p_sys->i_read_bytes += i_mData_bytes;
1335 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1336 ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1338 if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
1339 p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
1342 while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
1344 /* We don't have enough data yet */
1345 aout_buffer_t * p_buffer;
1346 p_buffer = aout_OutputNextBuffer( p_aout, current_date , false );
1348 if( p_buffer != NULL )
1350 uint32_t i_second_mData_bytes = __MIN( p_buffer->i_buffer, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1352 vlc_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes,
1353 p_buffer->p_buffer, i_second_mData_bytes );
1354 i_mData_bytes += i_second_mData_bytes;
1356 if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
1358 p_sys->i_total_bytes = p_buffer->i_buffer - i_second_mData_bytes;
1359 vlc_memcpy( p_sys->p_remainder_buffer,
1360 &p_buffer->p_buffer[i_second_mData_bytes],
1361 p_sys->i_total_bytes );
1362 aout_BufferFree( p_buffer );
1367 /* update current_date */
1368 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1369 ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1371 aout_BufferFree( p_buffer );
1375 vlc_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes,
1376 0,ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1377 i_mData_bytes += ioData->mBuffers[0].mDataByteSize - i_mData_bytes;
1383 /*****************************************************************************
1384 * RenderCallbackSPDIF: callback for SPDIF audio output
1385 *****************************************************************************/
1386 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice,
1387 const AudioTimeStamp * inNow,
1388 const void * inInputData,
1389 const AudioTimeStamp * inInputTime,
1390 AudioBufferList * outOutputData,
1391 const AudioTimeStamp * inOutputTime,
1392 void * threadGlobals )
1394 aout_buffer_t * p_buffer;
1395 mtime_t current_date;
1397 aout_instance_t * p_aout = (aout_instance_t *)threadGlobals;
1398 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1400 VLC_UNUSED(inDevice);
1401 VLC_UNUSED(inInputData);
1402 VLC_UNUSED(inInputTime);
1404 /* Check for the difference between the Device clock and mdate */
1405 p_sys->clock_diff = - (mtime_t)
1406 AudioConvertHostTimeToNanos( inNow->mHostTime ) / 1000;
1407 p_sys->clock_diff += mdate();
1409 current_date = p_sys->clock_diff +
1410 AudioConvertHostTimeToNanos( inOutputTime->mHostTime ) / 1000;
1411 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1413 p_buffer = aout_OutputNextBuffer( p_aout, current_date, true );
1415 #define BUFFER outOutputData->mBuffers[p_sys->i_stream_index]
1416 if( p_buffer != NULL )
1418 if( (int)BUFFER.mDataByteSize != (int)p_buffer->i_buffer)
1419 msg_Warn( p_aout, "bytesize: %d nb_bytes: %d", (int)BUFFER.mDataByteSize, (int)p_buffer->i_buffer );
1421 /* move data into output data buffer */
1422 vlc_memcpy( BUFFER.mData, p_buffer->p_buffer, p_buffer->i_buffer );
1423 aout_BufferFree( p_buffer );
1427 vlc_memset( BUFFER.mData, 0, BUFFER.mDataByteSize );
1434 /*****************************************************************************
1435 * HardwareListener: Warns us of changes in the list of registered devices
1436 *****************************************************************************/
1437 static OSStatus HardwareListener( AudioHardwarePropertyID inPropertyID,
1438 void * inClientData )
1440 OSStatus err = noErr;
1441 aout_instance_t *p_aout = (aout_instance_t *)inClientData;
1443 switch( inPropertyID )
1445 case kAudioHardwarePropertyDevices:
1447 /* something changed in the list of devices */
1448 /* We trigger the audio-device's aout_ChannelsRestart callback */
1449 var_TriggerCallback( p_aout, "audio-device" );
1450 var_Destroy( p_aout, "audio-device" );
1458 /*****************************************************************************
1460 *****************************************************************************/
1461 static OSStatus StreamListener( AudioStreamID inStream,
1463 AudioDevicePropertyID inPropertyID,
1464 void * inClientData )
1466 OSStatus err = noErr;
1467 struct { vlc_mutex_t lock; vlc_cond_t cond; } * w = inClientData;
1469 VLC_UNUSED(inStream);
1470 VLC_UNUSED(inChannel);
1472 switch( inPropertyID )
1474 case kAudioStreamPropertyPhysicalFormat:
1475 vlc_mutex_lock( &w->lock );
1476 vlc_cond_signal( &w->cond );
1477 vlc_mutex_unlock( &w->lock );
1486 /*****************************************************************************
1487 * AudioDeviceCallback: Callback triggered when the audio-device variable is changed
1488 *****************************************************************************/
1489 static int AudioDeviceCallback( vlc_object_t *p_this, const char *psz_variable,
1490 vlc_value_t old_val, vlc_value_t new_val, void *param )
1492 aout_instance_t *p_aout = (aout_instance_t *)p_this;
1493 var_Set( p_aout->p_libvlc, "macosx-audio-device", new_val );
1494 msg_Dbg( p_aout, "Set Device: %#x", new_val.i_int );
1495 return aout_ChannelsRestart( p_this, psz_variable, old_val, new_val, param );