1 /*****************************************************************************
2 * auhal.c: AUHAL 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include "aout_internal.h"
36 #include <CoreAudio/CoreAudio.h>
37 #include <CoreAudio/CoreAudioTypes.h>
38 #include <AudioUnit/AudioUnitProperties.h>
39 #include <AudioUnit/AudioUnitParameters.h>
40 #include <AudioUnit/AudioOutputUnit.h>
41 #include <AudioToolbox/AudioFormat.h>
43 #define STREAM_FORMAT_MSG( pre, sfm ) \
44 pre "[%ld][%4.4s][%ld][%ld][%ld][%ld][%ld][%ld]", \
45 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
46 sfm.mFormatFlags, sfm.mBytesPerPacket, \
47 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
48 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
50 #define STREAM_FORMAT_MSG_FULL( pre, sfm ) \
51 pre ":\nsamplerate: [%ld]\nFormatID: [%4.4s]\nFormatFlags: [%ld]\nBypesPerPacket: [%ld]\nFramesPerPacket: [%ld]\nBytesPerFrame: [%ld]\nChannelsPerFrame: [%ld]\nBitsPerChannel[%ld]", \
52 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
53 sfm.mFormatFlags, sfm.mBytesPerPacket, \
54 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
55 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
57 #define BUFSIZE 0xffffff
58 #define AOUT_VAR_SPDIF_FLAG 0xf00000
62 * - clean up the debug info
64 * - be better at changing stream setup or devices setup changes while playing.
67 /*****************************************************************************
68 * aout_sys_t: private audio output method descriptor
69 *****************************************************************************
70 * This structure is part of the audio output thread descriptor.
71 * It describes the CoreAudio specific properties of an output thread.
72 *****************************************************************************/
75 AudioDeviceID i_default_dev; /* Keeps DeviceID of defaultOutputDevice */
76 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device */
77 UInt32 i_devices; /* Number of CoreAudio Devices */
78 vlc_bool_t b_supports_digital;/* Does the currently selected device support digital mode? */
79 vlc_bool_t b_digital; /* Are we running in digital mode? */
80 mtime_t clock_diff; /* Difference between VLC clock and Device clock */
82 Component au_component; /* The Audiocomponent we use */
83 AudioUnit au_unit; /* The AudioUnit we use */
84 uint8_t p_remainder_buffer[BUFSIZE];
85 uint32_t i_read_bytes;
86 uint32_t i_total_bytes;
87 /* CoreAudio SPDIF mode specific */
88 pid_t i_hog_pid; /* The keep the pid of our hog status */
89 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */
90 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */
91 AudioStreamBasicDescription stream_format; /* The format we changed the to */
92 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */
93 vlc_bool_t b_revert; /* Wether we need to revert the stream format */
94 vlc_bool_t b_changed_mixing;/* Wether we need to set the mixing mode back */
97 /*****************************************************************************
99 *****************************************************************************/
100 static int Open ( vlc_object_t * );
101 static int OpenAnalog ( aout_instance_t * );
102 static int OpenSPDIF ( aout_instance_t * );
103 static void Close ( vlc_object_t * );
105 static void Play ( aout_instance_t * );
106 static void Probe ( aout_instance_t * );
108 static int AudioDeviceHasOutput ( AudioDeviceID );
109 static int AudioDeviceSupportsDigital( aout_instance_t *, AudioDeviceID );
110 static int AudioStreamSupportsDigital( aout_instance_t *, AudioStreamID );
112 static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
113 unsigned int, unsigned int, AudioBufferList *);
114 static OSStatus RenderCallbackSPDIF ( AudioDeviceID, const AudioTimeStamp *, const void *, const AudioTimeStamp *,
115 AudioBufferList *, const AudioTimeStamp *, void * );
116 static OSStatus HardwareListener ( AudioHardwarePropertyID, void *);
117 static OSStatus StreamListener ( AudioStreamID, UInt32,
118 AudioDevicePropertyID, void * );
119 static int AudioDeviceCallback ( vlc_object_t *, const char *,
120 vlc_value_t, vlc_value_t, void * );
123 /*****************************************************************************
125 *****************************************************************************/
126 #define ADEV_TEXT N_("Audio Device")
127 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
128 "audio device, as listed in your 'Audio Device' menu. This device will " \
129 "then be used by default for audio playback.")
132 set_shortname( "auhal" );
133 set_description( _("HAL AudioUnit output") );
134 set_capability( "audio output", 101 );
135 set_category( CAT_AUDIO );
136 set_subcategory( SUBCAT_AUDIO_AOUT );
137 set_callbacks( Open, Close );
138 add_integer( "macosx-audio-device", 0, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE );
141 /*****************************************************************************
142 * Open: open macosx audio output
143 *****************************************************************************/
144 static int Open( vlc_object_t * p_this )
146 OSStatus err = noErr;
147 UInt32 i_param_size = 0;
148 struct aout_sys_t *p_sys = NULL;
149 vlc_bool_t b_alive = VLC_FALSE;
151 aout_instance_t *p_aout = (aout_instance_t *)p_this;
153 /* Allocate structure */
154 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
155 if( p_aout->output.p_sys == NULL )
157 msg_Err( p_aout, "out of memory" );
158 return( VLC_ENOMEM );
161 p_sys = p_aout->output.p_sys;
162 p_sys->i_default_dev = 0;
163 p_sys->i_selected_dev = 0;
164 p_sys->i_devices = 0;
165 p_sys->b_supports_digital = VLC_FALSE;
166 p_sys->b_digital = VLC_FALSE;
167 p_sys->au_component = NULL;
168 p_sys->au_unit = NULL;
169 p_sys->clock_diff = (mtime_t) 0;
170 p_sys->i_read_bytes = 0;
171 p_sys->i_total_bytes = 0;
172 p_sys->i_hog_pid = -1;
173 p_sys->i_stream_id = 0;
174 p_sys->i_stream_index = 0;
175 p_sys->b_revert = VLC_FALSE;
176 p_sys->b_changed_mixing = VLC_FALSE;
177 memset( p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE );
179 p_aout->output.pf_play = Play;
181 aout_FormatPrint( p_aout, "VLC is looking for:", (audio_sample_format_t *)&p_aout->output.output );
183 /* Persistent device variable */
184 if( var_Type( p_aout->p_vlc, "macosx-audio-device" ) == 0 )
186 msg_Dbg( p_aout, "create macosx-audio-device" );
187 var_Create( p_aout->p_vlc, "macosx-audio-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
190 /* Build a list of devices */
191 if( var_Type( p_aout, "audio-device" ) == 0 )
196 /* What device do we want? */
197 if( var_Get( p_aout, "audio-device", &val ) < 0 )
199 msg_Err( p_aout, "audio-device var does not exist. device probe failed." );
201 return( VLC_ENOVAR );
204 p_sys->i_selected_dev = val.i_int & ~AOUT_VAR_SPDIF_FLAG;
205 p_sys->b_supports_digital = ( val.i_int & AOUT_VAR_SPDIF_FLAG ) ? VLC_TRUE : VLC_FALSE;
207 /* Check if the desired device is alive and usable */
208 i_param_size = sizeof( b_alive );
209 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
210 kAudioDevicePropertyDeviceIsAlive,
211 &i_param_size, &b_alive );
215 msg_Err( p_aout, "could not check whether device is alive: %4.4s",
220 if( b_alive == VLC_FALSE )
222 msg_Err( p_aout, "Selected audio device is not alive switching to default device" );
223 p_sys->i_selected_dev = p_sys->i_default_dev;
226 i_param_size = sizeof( p_sys->i_hog_pid );
227 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
228 kAudioDevicePropertyHogMode,
229 &i_param_size, &p_sys->i_hog_pid );
233 msg_Err( p_aout, "could not check whether device is hogged: %4.4s",
238 if( p_sys->i_hog_pid != -1 && p_sys->i_hog_pid != getpid() )
240 msg_Err( p_aout, "Selected audio device is exclusively in use by another program" );
241 var_Destroy( p_aout, "audio-device" );
246 /* Check for Digital mode or Analog output mode */
247 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
249 if( OpenSPDIF( p_aout ) )
254 if( OpenAnalog( p_aout ) )
258 /* If we reach this, the Open* failed */
259 var_Destroy( p_aout, "audio-device" );
264 /*****************************************************************************
265 * Open: open and setup a HAL AudioUnit
266 *****************************************************************************/
267 static int OpenAnalog( aout_instance_t *p_aout )
269 struct aout_sys_t *p_sys = p_aout->output.p_sys;
270 OSStatus err = noErr;
271 UInt32 i_param_size = 0, i = 0;
272 ComponentDescription desc;
274 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && !p_sys->b_supports_digital )
276 msg_Dbg( p_aout, "we had requested a digital stream, but it's not possible for this device" );
279 /* If analog only start setting up AUHAL */
280 /* Lets go find our Component */
281 desc.componentType = kAudioUnitType_Output;
282 desc.componentSubType = kAudioUnitSubType_HALOutput;
283 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
284 desc.componentFlags = 0;
285 desc.componentFlagsMask = 0;
287 p_sys->au_component = FindNextComponent( NULL, &desc );
288 if( p_sys->au_component == NULL )
290 msg_Err( p_aout, "we cannot find our HAL component" );
294 err = OpenAComponent( p_sys->au_component, &p_sys->au_unit );
297 msg_Err( p_aout, "we cannot find our HAL component" );
301 /* Enable IO for the component */
302 msg_Dbg( p_aout, "Device: %#x", (int)p_sys->i_selected_dev );
305 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
306 kAudioOutputUnitProperty_CurrentDevice,
307 kAudioUnitScope_Global,
309 &p_sys->i_selected_dev,
310 sizeof( AudioDeviceID )));
312 /* Get the current format */
313 AudioStreamBasicDescription DeviceFormat;
315 i_param_size = sizeof(AudioStreamBasicDescription);
317 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
318 kAudioUnitProperty_StreamFormat,
319 kAudioUnitScope_Input,
324 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is: " , DeviceFormat ) );
326 /* Get the channel layout */
327 AudioChannelLayout *layout;
328 verify_noerr( AudioUnitGetPropertyInfo( p_sys->au_unit,
329 kAudioDevicePropertyPreferredChannelLayout,
330 kAudioUnitScope_Output,
335 layout = (AudioChannelLayout *)malloc( i_param_size);
337 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
338 kAudioDevicePropertyPreferredChannelLayout,
339 kAudioUnitScope_Output,
344 /* Lets fill out the ChannelLayout */
345 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
347 msg_Dbg( p_aout, "bitmap defined channellayout" );
348 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
349 sizeof( UInt32), &layout->mChannelBitmap,
353 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
355 msg_Dbg( p_aout, "layouttags defined channellayout" );
356 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
357 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
362 msg_Dbg( p_aout, "Layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
364 p_aout->output.output.i_physical_channels = 0;
365 int i_original = p_aout->output.output.i_original_channels & AOUT_CHAN_PHYSMASK;
367 if( i_original == AOUT_CHAN_CENTER || layout->mNumberChannelDescriptions < 2 )
369 // We only need Mono or cannot output more
370 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
372 else if( i_original == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) || layout->mNumberChannelDescriptions < 3 )
374 // We only need Stereo or cannot output more
375 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
376 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
380 // We want more then stereo and we can do that
381 for( i = 0; i < layout->mNumberChannelDescriptions; i++ )
383 msg_Dbg( p_aout, "This is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
385 switch( layout->mChannelDescriptions[i].mChannelLabel )
387 case kAudioChannelLabel_Left:
388 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
390 case kAudioChannelLabel_Right:
391 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
393 case kAudioChannelLabel_Center:
394 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
396 case kAudioChannelLabel_LFEScreen:
397 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE;
399 case kAudioChannelLabel_LeftSurround:
400 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT;
402 case kAudioChannelLabel_RightSurround:
403 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
405 case kAudioChannelLabel_RearSurroundLeft:
406 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
408 case kAudioChannelLabel_RearSurroundRight:
409 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
411 case kAudioChannelLabel_CenterSurround:
412 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER;
415 msg_Warn( p_aout, "Unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
418 msg_Warn( p_aout, "Probably no channellayout is set. force based on channelcount" );
419 switch( layout->mNumberChannelDescriptions )
421 /* We make assumptions based on number of channels here.
422 * Unfortunatly Apple has provided no 100% method to retrieve the speaker configuration */
424 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
427 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
428 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
431 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
432 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
433 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
436 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
437 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
438 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
439 AOUT_CHAN_REARCENTER;
442 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
443 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
444 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
445 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT;
449 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
456 if( layout ) free( layout );
458 msg_Dbg( p_aout, "we want to output these channels: %#x", p_aout->output.output.i_original_channels);
459 msg_Dbg( p_aout, "selected %d physical channels for device output", aout_FormatNbChannels( &p_aout->output.output ) );
460 msg_Dbg( p_aout, "%s", aout_FormatPrintChannels( &p_aout->output.output ));
462 AudioChannelLayout new_layout;
463 memset (&new_layout, 0, sizeof(new_layout));
464 switch( aout_FormatNbChannels( &p_aout->output.output ) )
467 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
470 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
473 if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
475 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
477 else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
479 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
483 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
485 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
487 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
489 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
491 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
493 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
497 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
499 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
501 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
503 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
507 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
510 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
511 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
514 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
515 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
519 /* Set up the format to be used */
520 DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
521 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
523 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
524 p_aout->output.output.i_format = VLC_FOURCC( 'f','l','3','2');
525 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
526 DeviceFormat.mBitsPerChannel = 32;
527 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output );
529 /* Calculate framesizes and stuff */
530 aout_FormatPrepare( &p_aout->output.output );
531 DeviceFormat.mFramesPerPacket = 1;
532 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
533 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
535 i_param_size = sizeof(AudioStreamBasicDescription);
536 /* Set desired format (Use CAStreamBasicDescription )*/
537 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
538 kAudioUnitProperty_StreamFormat,
539 kAudioUnitScope_Input,
544 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
546 /* Retrieve actual format??? */
547 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
548 kAudioUnitProperty_StreamFormat,
549 kAudioUnitScope_Input,
554 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
556 p_aout->output.i_nb_samples = 2048;
557 aout_VolumeSoftInit( p_aout );
559 /* Find the difference between device clock and mdate clock */
560 p_sys->clock_diff = - (mtime_t)
561 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
562 p_sys->clock_diff += mdate();
564 /* set the IOproc callback */
565 AURenderCallbackStruct input;
566 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
567 input.inputProcRefCon = p_aout;
569 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
570 kAudioUnitProperty_SetRenderCallback,
571 kAudioUnitScope_Input,
572 0, &input, sizeof( input ) ) );
574 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
575 input.inputProcRefCon = p_aout;
577 /* Set the new_layout as the layout VLC feeds to the AU unit */
578 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
579 kAudioUnitProperty_AudioChannelLayout,
580 kAudioUnitScope_Input,
581 0, &new_layout, sizeof(new_layout) ) );
584 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
586 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
591 /*****************************************************************************
592 * Setup a encoded digital stream (SPDIF)
593 *****************************************************************************/
594 static int OpenSPDIF( aout_instance_t * p_aout )
596 struct aout_sys_t *p_sys = p_aout->output.p_sys;
597 OSStatus err = noErr;
598 UInt32 i_param_size = 0, b_mix = 0;
599 Boolean b_writeable = VLC_FALSE;
600 AudioStreamID *p_streams = NULL;
601 int i = 0, i_streams = 0;
604 struct timespec timeout;
605 struct { vlc_mutex_t lock; vlc_cond_t cond; } w;
607 /* Start doing the SPDIF setup proces */
608 p_sys->b_digital = VLC_TRUE;
609 msg_Dbg( p_aout, "opening in SPDIF mode" );
612 i_param_size = sizeof( p_sys->i_hog_pid );
613 p_sys->i_hog_pid = getpid() ;
615 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
616 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
620 msg_Err( p_aout, "failed to set hogmode: : [%4.4s]", (char *)&err );
624 /* Set mixable to false if we are allowed to */
625 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
626 &i_param_size, &b_writeable );
628 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
629 &i_param_size, &b_mix );
631 if( !err && b_writeable )
634 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
635 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
636 p_sys->b_changed_mixing = VLC_TRUE;
641 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
645 // Find stream_id of selected device with a cac3 stream
646 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE,
647 kAudioDevicePropertyStreams,
648 &i_param_size, NULL );
651 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
655 i_streams = i_param_size / sizeof( AudioStreamID );
656 p_streams = (AudioStreamID *)malloc( i_param_size );
657 if( p_streams == NULL )
659 msg_Err( p_aout, "Out of memory" );
663 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
664 kAudioDevicePropertyStreams,
665 &i_param_size, p_streams );
669 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
670 if( p_streams ) free( p_streams );
674 for( i = 0; i < i_streams; i++ )
676 // Find a stream with a cac3 stream
677 AudioStreamBasicDescription *p_format_list = NULL;
678 int i_formats = 0, j = 0;
680 /* Retrieve all the stream formats supported by each output stream */
681 err = AudioStreamGetPropertyInfo( p_streams[i], 0,
682 kAudioStreamPropertyPhysicalFormats,
683 &i_param_size, NULL );
686 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
690 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
691 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
692 if( p_format_list == NULL )
694 msg_Err( p_aout, "Could not malloc the memory" );
698 err = AudioStreamGetProperty( p_streams[i], 0,
699 kAudioStreamPropertyPhysicalFormats,
700 &i_param_size, p_format_list );
703 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
704 if( p_format_list) free( p_format_list);
708 for( j = 0; j < i_formats; j++ )
710 if( p_format_list[j].mFormatID == 'IAC3' ||
711 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
713 // found a cac3 format
714 p_sys->i_stream_id = p_streams[i];
715 p_sys->i_stream_index = i;
717 if( p_sys->b_revert == VLC_FALSE )
719 i_param_size = sizeof( p_sys->sfmt_revert );
720 err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
721 kAudioStreamPropertyPhysicalFormat,
723 &p_sys->sfmt_revert );
726 msg_Err( p_aout, "could not retrieve the original streamformat: [%4.4s]", (char *)&err );
729 p_sys->b_revert = VLC_TRUE;
731 if( p_format_list[j].mSampleRate == p_sys->sfmt_revert.mSampleRate )
733 p_sys->stream_format = p_format_list[j];
737 if( p_format_list ) free( p_format_list );
740 if( p_streams ) free( p_streams );
742 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) );
743 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "setting stream format: ", p_sys->stream_format ) );
745 /* Install the callback */
746 err = AudioStreamAddPropertyListener( p_sys->i_stream_id, 0,
747 kAudioStreamPropertyPhysicalFormat,
748 StreamListener, (void *)&w );
751 msg_Err( p_aout, "AudioStreamAddPropertyListener failed: [%4.4s]", (char *)&err );
755 /* Condition because SetProperty is asynchronious */
756 vlc_cond_init( p_aout, &w.cond );
757 vlc_mutex_init( p_aout, &w.lock );
758 vlc_mutex_lock( &w.lock );
760 /* change the format */
761 err = AudioStreamSetProperty( p_sys->i_stream_id, 0, 0,
762 kAudioStreamPropertyPhysicalFormat,
763 sizeof( AudioStreamBasicDescription ),
764 &p_sys->stream_format );
767 msg_Err( p_aout, "could not set the stream format: [%4.4s]", (char *)&err );
768 vlc_mutex_unlock( &w.lock );
769 vlc_mutex_destroy( &w.lock );
770 vlc_cond_destroy( &w.cond );
774 gettimeofday( &now, NULL );
775 timeout.tv_sec = now.tv_sec;
776 timeout.tv_nsec = (now.tv_usec + 900000) * 1000;
778 pthread_cond_timedwait( &w.cond.cond, &w.lock.mutex, &timeout );
779 vlc_mutex_unlock( &w.lock );
781 err = AudioStreamRemovePropertyListener( p_sys->i_stream_id, 0,
782 kAudioStreamPropertyPhysicalFormat,
786 msg_Err( p_aout, "AudioStreamRemovePropertyListener failed: [%4.4s]", (char *)&err );
787 vlc_mutex_destroy( &w.lock );
788 vlc_cond_destroy( &w.cond );
792 vlc_mutex_destroy( &w.lock );
793 vlc_cond_destroy( &w.cond );
795 i_param_size = sizeof( AudioStreamBasicDescription );
796 err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
797 kAudioStreamPropertyPhysicalFormat,
799 &p_sys->stream_format );
801 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "actual format in use: ", p_sys->stream_format ) );
803 /* set the format flags */
804 if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian )
805 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','b');
807 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
808 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
809 p_aout->output.output.i_frame_length = A52_FRAME_NB;
810 p_aout->output.i_nb_samples = p_aout->output.output.i_frame_length;
811 p_aout->output.output.i_rate = (unsigned int)p_sys->stream_format.mSampleRate;
813 aout_VolumeNoneInit( p_aout );
815 /* Add IOProc callback */
816 err = AudioDeviceAddIOProc( p_sys->i_selected_dev,
817 (AudioDeviceIOProc)RenderCallbackSPDIF,
821 msg_Err( p_aout, "AudioDeviceAddIOProc failed: [%4.4s]", (char *)&err );
825 /* Check for the difference between the Device clock and mdate */
826 p_sys->clock_diff = - (mtime_t)
827 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
828 p_sys->clock_diff += mdate();
831 err = AudioDeviceStart( p_sys->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF );
834 msg_Err( p_aout, "AudioDeviceStart failed: [%4.4s]", (char *)&err );
836 err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
837 (AudioDeviceIOProc)RenderCallbackSPDIF );
840 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
849 /*****************************************************************************
850 * Close: Close HAL AudioUnit
851 *****************************************************************************/
852 static void Close( vlc_object_t * p_this )
854 aout_instance_t *p_aout = (aout_instance_t *)p_this;
855 struct aout_sys_t *p_sys = p_aout->output.p_sys;
856 OSStatus err = noErr;
857 UInt32 i_param_size = 0;
861 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
862 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
863 verify_noerr( CloseComponent( p_sys->au_unit ) );
866 if( p_sys->b_digital )
869 err = AudioDeviceStop( p_sys->i_selected_dev,
870 (AudioDeviceIOProc)RenderCallbackSPDIF );
873 msg_Err( p_aout, "AudioDeviceStop failed: [%4.4s]", (char *)&err );
876 /* Remove callback */
877 err = AudioDeviceRemoveIOProc( p_sys->i_selected_dev,
878 (AudioDeviceIOProc)RenderCallbackSPDIF );
881 msg_Err( p_aout, "AudioDeviceRemoveIOProc failed: [%4.4s]", (char *)&err );
884 if( p_sys->b_revert )
887 struct timespec timeout;
888 struct { vlc_mutex_t lock; vlc_cond_t cond; } w;
890 /* Install the callback */
891 err = AudioStreamAddPropertyListener( p_sys->i_stream_id, 0,
892 kAudioStreamPropertyPhysicalFormat,
893 StreamListener, (void *)&w );
896 msg_Err( p_aout, "AudioStreamAddPropertyListener failed: [%4.4s]", (char *)&err );
899 /* Condition because SetProperty is asynchronious */
900 vlc_cond_init( p_aout, &w.cond );
901 vlc_mutex_init( p_aout, &w.lock );
902 vlc_mutex_lock( &w.lock );
904 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "setting stream format: ", p_sys->sfmt_revert ) );
906 err = AudioStreamSetProperty( p_sys->i_stream_id, NULL, 0,
907 kAudioStreamPropertyPhysicalFormat,
908 sizeof( AudioStreamBasicDescription ),
909 &p_sys->sfmt_revert );
913 msg_Err( p_aout, "Streamformat reverse failed: [%4.4s]", (char *)&err );
916 gettimeofday( &now, NULL );
917 timeout.tv_sec = now.tv_sec;
918 timeout.tv_nsec = (now.tv_usec + 900000) * 1000;
920 pthread_cond_timedwait( &w.cond.cond, &w.lock.mutex, &timeout );
921 vlc_mutex_unlock( &w.lock );
923 err = AudioStreamRemovePropertyListener( p_sys->i_stream_id, 0,
924 kAudioStreamPropertyPhysicalFormat,
928 msg_Err( p_aout, "AudioStreamRemovePropertyListener failed: [%4.4s]", (char *)&err );
931 vlc_mutex_destroy( &w.lock );
932 vlc_cond_destroy( &w.cond );
934 i_param_size = sizeof( AudioStreamBasicDescription );
935 err = AudioStreamGetProperty( p_sys->i_stream_id, 0,
936 kAudioStreamPropertyPhysicalFormat,
938 &p_sys->stream_format );
940 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "actual format in use: ", p_sys->stream_format ) );
942 if( p_sys->b_changed_mixing && p_sys->sfmt_revert.mFormatID != kAudioFormat60958AC3 )
946 /* Revert mixable to true if we are allowed to */
947 err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
948 &i_param_size, &b_writeable );
950 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing,
951 &i_param_size, &b_mix );
953 if( !err && b_writeable )
955 msg_Dbg( p_aout, "mixable is: %d", b_mix );
957 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
958 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix );
963 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
968 err = AudioHardwareRemovePropertyListener( kAudioHardwarePropertyDevices,
973 msg_Err( p_aout, "AudioHardwareRemovePropertyListener failed: [%4.4s]", (char *)&err );
976 if( p_sys->i_hog_pid == getpid() )
978 p_sys->i_hog_pid = -1;
979 i_param_size = sizeof( p_sys->i_hog_pid );
980 err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE,
981 kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid );
982 if( err != noErr ) msg_Err( p_aout, "Could not release hogmode: [%4.4s]", (char *)&err );
985 if( p_sys ) free( p_sys );
988 /*****************************************************************************
989 * Play: nothing to do
990 *****************************************************************************/
991 static void Play( aout_instance_t * p_aout )
996 /*****************************************************************************
998 *****************************************************************************/
999 static void Probe( aout_instance_t * p_aout )
1001 OSStatus err = noErr;
1002 UInt32 i = 0, i_param_size = 0;
1003 AudioDeviceID devid_def = 0;
1004 AudioDeviceID *p_devices = NULL;
1005 vlc_value_t val, text;
1007 struct aout_sys_t *p_sys = p_aout->output.p_sys;
1009 /* Get number of devices */
1010 err = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices,
1011 &i_param_size, NULL );
1014 msg_Err( p_aout, "could not get number of devices: [%4.4s]", (char *)&err );
1018 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
1020 if( p_sys->i_devices < 1 )
1022 msg_Err( p_aout, "no devices found" );
1026 msg_Dbg( p_aout, "system has [%ld] device(s)", p_sys->i_devices );
1028 /* Allocate DeviceID array */
1029 p_devices = (AudioDeviceID*)malloc( sizeof(AudioDeviceID) * p_sys->i_devices );
1030 if( p_devices == NULL )
1032 msg_Err( p_aout, "out of memory" );
1036 /* Populate DeviceID array */
1037 err = AudioHardwareGetProperty( kAudioHardwarePropertyDevices,
1038 &i_param_size, p_devices );
1041 msg_Err( p_aout, "could not get the device ID's: [%4.4s]", (char *)&err );
1045 /* Find the ID of the default Device */
1046 i_param_size = sizeof( AudioDeviceID );
1047 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
1048 &i_param_size, &devid_def );
1051 msg_Err( p_aout, "could not get default audio device: [%4.4s]", (char *)&err );
1054 p_sys->i_default_dev = devid_def;
1056 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
1057 text.psz_string = _("Audio Device");
1058 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
1060 for( i = 0; i < p_sys->i_devices; i++ )
1065 /* Retrieve the length of the device name */
1066 err = AudioDeviceGetPropertyInfo(
1067 p_devices[i], 0, VLC_FALSE,
1068 kAudioDevicePropertyDeviceName,
1069 &i_param_size, NULL);
1070 if( err ) goto error;
1072 /* Retrieve the name of the device */
1073 psz_name = (char *)malloc( i_param_size );
1074 err = AudioDeviceGetProperty(
1075 p_devices[i], 0, VLC_FALSE,
1076 kAudioDevicePropertyDeviceName,
1077 &i_param_size, psz_name);
1078 if( err ) goto error;
1080 msg_Dbg( p_aout, "DevID: %#lx DevName: %s", p_devices[i], psz_name );
1082 if( !AudioDeviceHasOutput( p_devices[i]) )
1084 msg_Dbg( p_aout, "this device is INPUT only. skipping..." );
1088 val.i_int = (int)p_devices[i];
1089 text.psz_string = strdup( psz_name );
1090 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1091 if( p_sys->i_default_dev == p_devices[i] )
1093 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1094 var_Set( p_aout, "audio-device", val );
1097 if( AudioDeviceSupportsDigital( p_aout, p_devices[i] ) )
1099 val.i_int = (int)p_devices[i] | AOUT_VAR_SPDIF_FLAG;
1100 asprintf( &text.psz_string, "%s (Encoded Output)", psz_name );
1101 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1102 if( p_sys->i_default_dev == p_devices[i] && config_GetInt( p_aout, "spdif" ) )
1104 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1105 var_Set( p_aout, "audio-device", val );
1112 var_Get( p_aout->p_vlc, "macosx-audio-device", &val );
1113 msg_Dbg( p_aout, "device value override1: %#x", val.i_int );
1116 msg_Dbg( p_aout, "device value override2: %#x", val.i_int );
1117 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1118 var_Set( p_aout, "audio-device", val );
1121 var_AddCallback( p_aout, "audio-device", AudioDeviceCallback, NULL );
1123 /* attach a Listener so that we are notified of a change in the Device setup */
1124 err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
1130 msg_Dbg( p_aout, "succesful finish of deviceslist" );
1131 if( p_devices ) free( p_devices );
1135 var_Destroy( p_aout, "audio-device" );
1136 if( p_devices ) free( p_devices );
1140 /*****************************************************************************
1141 * AudioDeviceHasOutput: Checks if the Device actually provides any outputs at all
1142 *****************************************************************************/
1143 static int AudioDeviceHasOutput( AudioDeviceID i_dev_id )
1148 verify_noerr( AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE, kAudioDevicePropertyStreams, &dataSize, &isWritable) );
1149 if (dataSize == 0) return FALSE;
1154 /*****************************************************************************
1155 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
1156 *****************************************************************************/
1157 static int AudioDeviceSupportsDigital( aout_instance_t *p_aout, AudioDeviceID i_dev_id )
1159 OSStatus err = noErr;
1160 UInt32 i_param_size = 0;
1161 AudioStreamID *p_streams = NULL;
1162 int i = 0, i_streams = 0;
1163 vlc_bool_t b_return = VLC_FALSE;
1165 /* Retrieve all the output streams */
1166 err = AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE,
1167 kAudioDevicePropertyStreams,
1168 &i_param_size, NULL );
1171 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1175 i_streams = i_param_size / sizeof( AudioStreamID );
1176 p_streams = (AudioStreamID *)malloc( i_param_size );
1177 if( p_streams == NULL )
1179 msg_Err( p_aout, "Out of memory" );
1183 err = AudioDeviceGetProperty( i_dev_id, 0, FALSE,
1184 kAudioDevicePropertyStreams,
1185 &i_param_size, p_streams );
1189 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
1193 for( i = 0; i < i_streams; i++ )
1195 if( AudioStreamSupportsDigital( p_aout, p_streams[i] ) )
1196 b_return = VLC_TRUE;
1199 if( p_streams ) free( p_streams );
1203 /*****************************************************************************
1204 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
1205 *****************************************************************************/
1206 static int AudioStreamSupportsDigital( aout_instance_t *p_aout, AudioStreamID i_stream_id )
1208 OSStatus err = noErr;
1209 UInt32 i_param_size = 0;
1210 AudioStreamBasicDescription *p_format_list = NULL;
1211 int i = 0, i_formats = 0;
1212 vlc_bool_t b_return = VLC_FALSE;
1214 /* Retrieve all the stream formats supported by each output stream */
1215 err = AudioStreamGetPropertyInfo( i_stream_id, 0,
1216 kAudioStreamPropertyPhysicalFormats,
1217 &i_param_size, NULL );
1220 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
1224 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
1225 msg_Dbg( p_aout, "number of formats: %d", i_formats );
1226 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
1227 if( p_format_list == NULL )
1229 msg_Err( p_aout, "Could not malloc the memory" );
1233 err = AudioStreamGetProperty( i_stream_id, 0,
1234 kAudioStreamPropertyPhysicalFormats,
1235 &i_param_size, p_format_list );
1238 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
1239 free( p_format_list);
1240 p_format_list = NULL;
1244 for( i = 0; i < i_formats; i++ )
1246 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format: ", p_format_list[i] ) );
1248 if( p_format_list[i].mFormatID == 'IAC3' ||
1249 p_format_list[i].mFormatID == kAudioFormat60958AC3 )
1251 b_return = VLC_TRUE;
1255 if( p_format_list ) free( p_format_list );
1259 /*****************************************************************************
1260 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
1261 * us to provide some more audio data.
1262 * Don't print anything during normal playback, calling blocking function from
1263 * this callback is not allowed.
1264 *****************************************************************************/
1265 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
1266 AudioUnitRenderActionFlags *ioActionFlags,
1267 const AudioTimeStamp *inTimeStamp,
1268 unsigned int inBusNummer,
1269 unsigned int inNumberFrames,
1270 AudioBufferList *ioData )
1272 AudioTimeStamp host_time;
1273 mtime_t current_date = 0;
1274 uint32_t i_mData_bytes = 0;
1276 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
1277 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1279 host_time.mFlags = kAudioTimeStampHostTimeValid;
1280 AudioDeviceTranslateTime( p_sys->i_selected_dev, inTimeStamp, &host_time );
1282 /* Check for the difference between the Device clock and mdate */
1283 p_sys->clock_diff = - (mtime_t)
1284 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
1285 p_sys->clock_diff += mdate();
1287 current_date = p_sys->clock_diff +
1288 AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
1289 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1291 if( ioData == NULL && ioData->mNumberBuffers < 1 )
1293 msg_Err( p_aout, "no iodata or buffers");
1296 if( ioData->mNumberBuffers > 1 )
1297 msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
1300 if( p_sys->i_total_bytes > 0 )
1302 i_mData_bytes = __MIN( p_sys->i_total_bytes - p_sys->i_read_bytes, ioData->mBuffers[0].mDataByteSize );
1303 p_aout->p_vlc->pf_memcpy( ioData->mBuffers[0].mData, &p_sys->p_remainder_buffer[p_sys->i_read_bytes], i_mData_bytes );
1304 p_sys->i_read_bytes += i_mData_bytes;
1305 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1306 ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1308 if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
1309 p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
1312 while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
1314 /* We don't have enough data yet */
1315 aout_buffer_t * p_buffer;
1316 p_buffer = aout_OutputNextBuffer( p_aout, current_date , VLC_FALSE );
1318 if( p_buffer != NULL )
1320 uint32_t i_second_mData_bytes = __MIN( p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1322 p_aout->p_vlc->pf_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes, p_buffer->p_buffer, i_second_mData_bytes );
1323 i_mData_bytes += i_second_mData_bytes;
1325 if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
1327 p_sys->i_total_bytes = p_buffer->i_nb_bytes - i_second_mData_bytes;
1328 p_aout->p_vlc->pf_memcpy( p_sys->p_remainder_buffer, &p_buffer->p_buffer[i_second_mData_bytes], p_sys->i_total_bytes );
1332 // update current_date
1333 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
1334 ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
1336 aout_BufferFree( p_buffer );
1340 p_aout->p_vlc->pf_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes, 0, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1341 i_mData_bytes += ioData->mBuffers[0].mDataByteSize - i_mData_bytes;
1347 /*****************************************************************************
1348 * RenderCallbackSPDIF: callback for SPDIF audio output
1349 *****************************************************************************/
1350 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice,
1351 const AudioTimeStamp * inNow,
1352 const void * inInputData,
1353 const AudioTimeStamp * inInputTime,
1354 AudioBufferList * outOutputData,
1355 const AudioTimeStamp * inOutputTime,
1356 void * threadGlobals )
1358 aout_buffer_t * p_buffer;
1359 mtime_t current_date;
1361 aout_instance_t * p_aout = (aout_instance_t *)threadGlobals;
1362 struct aout_sys_t * p_sys = p_aout->output.p_sys;
1364 /* Check for the difference between the Device clock and mdate */
1365 p_sys->clock_diff = - (mtime_t)
1366 AudioConvertHostTimeToNanos( inNow->mHostTime ) / 1000;
1367 p_sys->clock_diff += mdate();
1369 current_date = p_sys->clock_diff +
1370 AudioConvertHostTimeToNanos( inOutputTime->mHostTime ) / 1000;
1371 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1373 p_buffer = aout_OutputNextBuffer( p_aout, current_date, VLC_TRUE );
1375 #define BUFFER outOutputData->mBuffers[p_sys->i_stream_index]
1376 if( p_buffer != NULL )
1378 if( (int)BUFFER.mDataByteSize != (int)p_buffer->i_nb_bytes)
1379 msg_Warn( p_aout, "bytesize: %d nb_bytes: %d", (int)BUFFER.mDataByteSize, (int)p_buffer->i_nb_bytes );
1381 /* move data into output data buffer */
1382 p_aout->p_vlc->pf_memcpy( BUFFER.mData,
1383 p_buffer->p_buffer, p_buffer->i_nb_bytes );
1384 aout_BufferFree( p_buffer );
1388 p_aout->p_vlc->pf_memset( BUFFER.mData, 0, BUFFER.mDataByteSize );
1395 /*****************************************************************************
1396 * HardwareListener: Warns us of changes in the list of registered devices
1397 *****************************************************************************/
1398 static OSStatus HardwareListener( AudioHardwarePropertyID inPropertyID,
1399 void * inClientData )
1401 OSStatus err = noErr;
1402 aout_instance_t *p_aout = (aout_instance_t *)inClientData;
1404 switch( inPropertyID )
1406 case kAudioHardwarePropertyDevices:
1408 /* something changed in the list of devices */
1409 /* We trigger the audio-device's aout_ChannelsRestart callback */
1410 var_Change( p_aout, "audio-device", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
1411 var_Destroy( p_aout, "audio-device" );
1419 /*****************************************************************************
1421 *****************************************************************************/
1422 static OSStatus StreamListener( AudioStreamID inStream,
1424 AudioDevicePropertyID inPropertyID,
1425 void * inClientData )
1427 OSStatus err = noErr;
1428 struct { vlc_mutex_t lock; vlc_cond_t cond; } * w = inClientData;
1430 switch( inPropertyID )
1432 case kAudioStreamPropertyPhysicalFormat:
1433 vlc_mutex_lock( &w->lock );
1434 vlc_cond_signal( &w->cond );
1435 vlc_mutex_unlock( &w->lock );
1444 /*****************************************************************************
1445 * AudioDeviceCallback: Callback triggered when the audio-device variable is changed
1446 *****************************************************************************/
1447 static int AudioDeviceCallback( vlc_object_t *p_this, const char *psz_variable,
1448 vlc_value_t old_val, vlc_value_t new_val, void *param )
1450 aout_instance_t *p_aout = (aout_instance_t *)p_this;
1451 var_Set( p_aout->p_vlc, "macosx-audio-device", new_val );
1452 msg_Dbg( p_aout, "Set Device: %#x", new_val.i_int );
1453 return aout_ChannelsRestart( p_this, psz_variable, old_val, new_val, param );