1 /*****************************************************************************
2 * auhal.c: AUHAL output plugin
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
5 * $Id: coreaudio.c 10101 2005-03-02 16:47:31Z robux4 $
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 *****************************************************************************/
33 #include "aout_internal.h"
35 #include <CoreAudio/CoreAudio.h>
36 #include <CoreAudio/CoreAudioTypes.h>
37 #include <AudioUnit/AudioUnitProperties.h>
38 #include <AudioUnit/AudioUnitParameters.h>
39 #include <AudioUnit/AudioOutputUnit.h>
40 #include <AudioToolbox/AudioFormat.h>
42 #define STREAM_FORMAT_MSG( pre, sfm ) \
43 pre ":\nsamplerate: [%ld]\nFormatID: [%4.4s]\nFormatFlags: [%ld]\nBypesPerPacket: [%ld]\nFramesPerPacket: [%ld]\nBytesPerFrame: [%ld]\nChannelsPerFrame: [%ld]\nBitsPerChannel[%ld]", \
44 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
45 sfm.mFormatFlags, sfm.mBytesPerPacket, \
46 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
47 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
50 /*****************************************************************************
51 * aout_sys_t: private audio output method descriptor
52 *****************************************************************************
53 * This structure is part of the audio output thread descriptor.
54 * It describes the CoreAudio specific properties of an output thread.
55 *****************************************************************************/
58 AudioDeviceID i_default_dev; /* Keeps DeviceID of defaultOutputDevice */
59 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device */
60 UInt32 i_devices; /* Number of CoreAudio Devices */
61 vlc_bool_t b_supports_digital;/* Does the currently selected device support digital mode? */
62 vlc_bool_t b_digital; /* Are we running in digital mode? */
63 Component au_component; /* The Audiocomponent we use */
64 AudioUnit au_unit; /* The AudioUnit we use */
68 /*****************************************************************************
70 *****************************************************************************/
71 static int Open ( vlc_object_t * );
72 static void Close ( vlc_object_t * );
74 static void Play ( aout_instance_t *);
76 static int Probe ( aout_instance_t * );
77 static int DeviceDigitalMode ( aout_instance_t *, AudioDeviceID );
78 static int DigitalInit ( aout_instance_t * );
80 static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
81 unsigned int, unsigned int, AudioBufferList *);
82 static OSStatus HardwareListener ( AudioHardwarePropertyID, void *);
84 /*****************************************************************************
86 *****************************************************************************/
87 #define ADEV_TEXT N_("Audio Device")
88 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
89 "audio device, as listed in your 'Audio Device' menu. This device will " \
90 "then be used by default for audio playback.")
93 set_shortname( "auhal" );
94 set_description( _("HAL AudioUnit output") );
95 set_capability( "audio output", 101 );
96 set_category( CAT_AUDIO );
97 set_subcategory( SUBCAT_AUDIO_AOUT );
98 set_callbacks( Open, Close );
99 //add_integer( "coreaudio-dev", -1, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE );
102 /*****************************************************************************
103 * Open: open a HAL AudioUnit
104 *****************************************************************************/
105 static int Open( vlc_object_t * p_this )
107 OSStatus err = noErr;
108 ComponentDescription desc;
109 UInt32 i_param_size,i;
110 struct aout_sys_t *p_sys;
112 aout_instance_t *p_aout = (aout_instance_t *)p_this;
114 /* Allocate structure */
115 p_sys = (struct aout_sys_t *)malloc( sizeof( struct aout_sys_t ) );
118 msg_Err( p_aout, "out of memory" );
119 return( VLC_ENOMEM );
122 memset( p_sys, 0, sizeof( struct aout_sys_t ) );
124 p_sys->b_digital = VLC_FALSE; /* We assume we are not digital */
126 p_aout->output.p_sys = p_sys;
127 p_aout->output.pf_play = Play;
129 aout_FormatPrint( p_aout, "VLC is looking for:\n", (audio_sample_format_t *)&p_aout->output.output );
131 /* Build a list of devices */
132 if( var_Type( p_aout, "audio-device" ) == 0 )
135 /*if( Probe( p_aout ) != VLC_SUCCESS );
137 msg_Err( p_aout, "Probe failed" );
143 /* What device do we want? */
144 if( var_Get( p_aout, "audio-device", &val ) < 0 )
146 msg_Err( p_aout, "audio-device var does not exist" );
148 return( VLC_ENOVAR );
150 p_sys->i_selected_dev = val.i_int;
152 /* what is vlc format? if digital, take digital route else AUHAL route */
153 DeviceDigitalMode( p_aout, p_sys->i_selected_dev );
154 /*if( DeviceDigitalMode( p_aout, p_sys->i_selected_dev ) != VLC_SUCCESS );
156 msg_Err( p_aout, "DeviceDigitalMode failed" );
161 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
163 p_sys->b_digital = VLC_TRUE;
164 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
165 msg_Dbg( p_aout, "we found a digital stream, and we WANT a digital stream" );
167 else if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && !p_sys->b_supports_digital )
169 msg_Dbg( p_aout, "we had requested a digital stream, but it's not possible for this device" );
172 /* If analog only start setting up AUHAL */
174 /* Lets go find our Component */
175 desc.componentType = kAudioUnitType_Output;
176 desc.componentSubType = kAudioUnitSubType_HALOutput;
177 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
178 desc.componentFlags = 0;
179 desc.componentFlagsMask = 0;
181 p_sys->au_component = FindNextComponent( NULL, &desc );
182 if( p_sys->au_component == NULL )
184 msg_Err( p_aout, "we cannot find our HAL component" );
189 err = OpenAComponent( p_sys->au_component, &p_sys->au_unit );
193 msg_Err( p_aout, "we cannot find our HAL component" );
198 /* Enable IO for the component */
201 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
202 kAudioOutputUnitProperty_CurrentDevice,
203 kAudioUnitScope_Global,
205 &p_sys->i_selected_dev,
206 sizeof(p_sys->i_selected_dev)));
208 /* Get the current format */
209 AudioStreamBasicDescription DeviceFormat;
211 i_param_size = sizeof(AudioStreamBasicDescription);
213 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
214 kAudioUnitProperty_StreamFormat,
215 kAudioUnitScope_Input,
220 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is " , DeviceFormat ) );
222 /* Get the channel layout */
223 AudioChannelLayout *layout;
224 verify_noerr( AudioUnitGetPropertyInfo( p_sys->au_unit,
225 kAudioDevicePropertyPreferredChannelLayout,
226 kAudioUnitScope_Output,
231 layout = (AudioChannelLayout *)malloc( i_param_size);
233 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
234 kAudioDevicePropertyPreferredChannelLayout,
235 kAudioUnitScope_Output,
240 /* Lets fill out the ChannelLayout */
241 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
243 msg_Dbg( p_aout, "bitmap defined channellayout" );
244 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
245 sizeof( UInt32), &layout->mChannelBitmap,
249 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
251 msg_Dbg( p_aout, "layouttags defined channellayout" );
252 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
253 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
258 msg_Dbg( p_aout, "Layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
260 p_aout->output.output.i_physical_channels = 0;
261 for( i = 0; i < layout->mNumberChannelDescriptions; i++ )
263 msg_Dbg( p_aout, "This is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
265 switch( layout->mChannelDescriptions[i].mChannelLabel )
267 case kAudioChannelLabel_Left:
268 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
270 case kAudioChannelLabel_Right:
271 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
273 case kAudioChannelLabel_Center:
274 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
276 case kAudioChannelLabel_LFEScreen:
277 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE;
279 case kAudioChannelLabel_LeftSurround:
280 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT;
282 case kAudioChannelLabel_RightSurround:
283 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
285 case kAudioChannelLabel_LeftCenter:
286 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
288 case kAudioChannelLabel_RightCenter:
289 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
291 case kAudioChannelLabel_CenterSurround:
292 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER;
295 msg_Warn( p_aout, "Unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
296 switch( layout->mNumberChannelDescriptions )
298 /* We make assumptions based on number of channels here.
299 * Unfortunatly Apple has provided no 100% method to retrieve the speaker configuration */
301 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
304 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
305 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
308 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
309 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
310 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
313 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
314 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
315 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER;
318 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
319 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
320 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
321 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
325 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
332 msg_Dbg( p_aout, "defined %d physical channels for vlc core", aout_FormatNbChannels( &p_aout->output.output ) );
333 msg_Dbg( p_aout, "%s", aout_FormatPrintChannels( &p_aout->output.output ));
335 AudioChannelLayout new_layout;
336 memset (&new_layout, 0, sizeof(new_layout));
337 switch( aout_FormatNbChannels( &p_aout->output.output ) )
340 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
343 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
346 if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
348 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
350 else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
352 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
356 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
358 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
360 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
362 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
364 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
366 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
370 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
372 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
374 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
376 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
380 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
383 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
384 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
387 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
388 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
392 /* Set up the format to be used */
393 DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
394 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
396 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
397 p_aout->output.output.i_format = VLC_FOURCC( 'f','l','3','2');
398 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
399 DeviceFormat.mBitsPerChannel = 32;
400 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output );
402 /* Calculate framesizes and stuff */
403 aout_FormatPrepare( &p_aout->output.output );
404 DeviceFormat.mFramesPerPacket = 1;
405 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
406 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
408 i_param_size = sizeof(AudioStreamBasicDescription);
409 /* Set desired format (Use CAStreamBasicDescription )*/
410 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
411 kAudioUnitProperty_StreamFormat,
412 kAudioUnitScope_Input,
417 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
419 /* Retrieve actual format??? */
420 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
421 kAudioUnitProperty_StreamFormat,
422 kAudioUnitScope_Input,
427 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
429 aout_VolumeSoftInit( p_aout );
431 /* Let's pray for the following operation to be atomic... */
432 p_sys->clock_diff = - (mtime_t)
433 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
434 p_sys->clock_diff += mdate();
436 /* set the IOproc callback */
437 AURenderCallbackStruct input;
438 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
439 input.inputProcRefCon = p_aout;
441 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
442 kAudioUnitProperty_SetRenderCallback,
443 kAudioUnitScope_Input,
444 0, &input, sizeof( input ) ) );
446 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
447 input.inputProcRefCon = p_aout;
449 /* Set the new_layout as the layout VLC feeds to the AU unit */
450 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
451 kAudioUnitProperty_AudioChannelLayout,
452 kAudioUnitScope_Input,
453 0, &new_layout, sizeof(new_layout) ) );
456 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
458 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
459 return( VLC_SUCCESS );
462 /*****************************************************************************
463 * Close: Close HAL AudioUnit
464 *****************************************************************************/
465 static void Close( vlc_object_t * p_this )
467 aout_instance_t *p_aout = (aout_instance_t *)p_this;
468 struct aout_sys_t *p_sys = p_aout->output.p_sys;
472 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
473 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
474 verify_noerr( CloseComponent( p_sys->au_unit ) );
479 /*****************************************************************************
480 * Play: nothing to do
481 *****************************************************************************/
482 static void Play( aout_instance_t * p_aout )
487 /*****************************************************************************
489 *****************************************************************************/
490 static int Probe( aout_instance_t * p_aout )
492 OSStatus err = noErr;
493 UInt32 i, i_param_size;
494 AudioDeviceID devid_def;
495 AudioDeviceID *p_devices = NULL;
496 vlc_value_t val, text;
498 struct aout_sys_t *p_sys = p_aout->output.p_sys;
500 /* Get number of devices */
501 err = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices,
502 &i_param_size, NULL );
505 msg_Err( p_aout, "could not get number of devices: [%4.4s]", (char *)&err );
509 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
511 if( p_sys->i_devices < 1 )
513 msg_Err( p_aout, "no devices found" );
517 msg_Dbg( p_aout, "system has [%ld] device(s)", p_sys->i_devices );
519 /* Allocate DeviceID array */
520 p_devices = (AudioDeviceID *)malloc( i_param_size );
521 if( p_devices == NULL )
523 msg_Err( p_aout, "out of memory" );
527 /* Populate DeviceID array */
528 err = AudioHardwareGetProperty( kAudioHardwarePropertyDevices,
529 &i_param_size, (void *)p_devices );
532 msg_Err( p_aout, "could not get the device ID's: [%4.4s]", (char *)&err );
536 /* Find the ID of the default Device */
537 i_param_size = sizeof( AudioDeviceID );
538 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
539 &i_param_size, (void *)&devid_def );
542 msg_Err( p_aout, "could not get default audio device: [%4.4s]", (char *)&err );
545 p_sys->i_default_dev = devid_def;
547 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
548 text.psz_string = _("Audio Device");
549 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
551 for( i = 0; i < p_sys->i_devices; i++ )
553 char psz_devuid[1024];
557 i_param_size = sizeof psz_name;
558 err = AudioDeviceGetProperty(
559 p_devices[i], 0, VLC_FALSE,
560 kAudioDevicePropertyDeviceName,
561 &i_param_size, psz_name);
565 i_param_size = sizeof(CFStringRef);
566 err = AudioDeviceGetProperty(
567 p_devices[i], 0, VLC_FALSE,
568 kAudioDevicePropertyDeviceUID,
569 &i_param_size, &devUID);
573 CFStringGetCString( devUID, psz_devuid, sizeof psz_devuid, CFStringGetSystemEncoding() );
574 msg_Dbg( p_aout, "DevID: %lu DevName: %s DevUID: %s", p_devices[i], psz_name, psz_devuid );
577 val.i_int = (int) p_devices[i];
578 text.psz_string = psz_name;
579 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
580 if( devid_def == p_devices[i] )
582 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
583 var_Set( p_aout, "audio-device", val );
586 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
588 /* attach a Listener so that we are notified of a change in the Device setup */
589 /*err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
595 msg_Dbg( p_aout, "succesful finish of deviceslist" );
596 if( p_devices ) free( p_devices );
597 return (VLC_SUCCESS);
600 var_Destroy( p_aout, "audio-device" );
601 if( p_devices ) free( p_devices );
605 /*****************************************************************************
606 * DeviceDigitalMode: Check i_dev_id for digital stream support.
607 *****************************************************************************/
608 static int DeviceDigitalMode( aout_instance_t *p_aout, AudioDeviceID i_dev_id )
610 OSStatus err = noErr;
612 AudioStreamBasicDescription *p_format_list;
614 struct aout_sys_t *p_sys = p_aout->output.p_sys;
616 p_sys->b_supports_digital = VLC_FALSE;
618 err = AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE,
619 kAudioDevicePropertyStreamFormats,
620 &i_param_size, NULL );
623 msg_Err( p_aout, "could not get number of streamsformats: [%4.4s]", (char *)&err );
624 return( VLC_EGENERIC );
627 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
628 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
629 if( p_format_list == NULL )
631 return( VLC_ENOMEM );
634 err = AudioDeviceGetProperty( i_dev_id, 0, FALSE,
635 kAudioDevicePropertyStreamFormats,
636 &i_param_size, (void *)p_format_list );
639 msg_Err( p_aout, "could not get the list of formats: [%4.4s]", (char *)&err );
640 return( VLC_EGENERIC );
643 for( i = 0; i < i_formats; i++ )
645 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format", p_format_list[i] ) );
647 if( p_format_list[i].mFormatID == 'IAC3' ||
648 p_format_list[i].mFormatID == kAudioFormat60958AC3 )
650 p_sys->b_supports_digital = VLC_TRUE;
651 msg_Dbg( p_aout, "this device supports a digital stream" );
656 free( (void *)p_format_list );
660 /*****************************************************************************
661 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
662 * us to provide some more audio data.
663 * Don't print anything during normal playback, calling blocking function from
664 * this callback is not allowed.
665 *****************************************************************************/
666 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
667 AudioUnitRenderActionFlags *ioActionFlags,
668 const AudioTimeStamp *inTimeStamp,
669 unsigned int inBusNummer,
670 unsigned int inNumberFrames,
671 AudioBufferList *ioData )
673 aout_buffer_t * p_buffer;
674 AudioTimeStamp host_time;
675 mtime_t current_date = 0;
677 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
678 struct aout_sys_t * p_sys = p_aout->output.p_sys;
680 host_time.mFlags = kAudioTimeStampHostTimeValid;
681 AudioDeviceTranslateTime( p_sys->i_selected_dev, inTimeStamp, &host_time );
683 p_sys->clock_diff = - (mtime_t)
684 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
685 p_sys->clock_diff += mdate();
687 current_date = p_sys->clock_diff +
688 AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
690 p_aout->output.i_nb_samples = inNumberFrames;
692 if( ioData == NULL && ioData->mNumberBuffers < 1 )
694 msg_Err( p_aout, "no iodata or buffers");
697 if( ioData->mNumberBuffers > 1 )
698 msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
701 p_buffer = aout_OutputNextBuffer( p_aout, current_date , VLC_FALSE );
702 if( p_buffer != NULL )
704 p_aout->p_vlc->pf_memcpy(ioData->mBuffers[0].mData, p_buffer->p_buffer, __MIN( p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize ) );
706 if( p_buffer->i_nb_bytes != ioData->mBuffers[0].mDataByteSize )
709 //msg_Err( p_aout, "byte sizes don't match %d:%d\nframes: %d, nb_samples: %d", p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize, inNumberFrames, p_aout->output.i_nb_samples);
711 aout_BufferFree( p_buffer );
715 p_aout->p_vlc->pf_memset(ioData->mBuffers[0].mData, 0, ioData->mBuffers[0].mDataByteSize);
721 /*****************************************************************************
722 * Setup a digital stream
723 *****************************************************************************/
724 static int DigitalInit( aout_instance_t * p_aout )
726 OSStatus err = noErr;
727 UInt32 i, i_param_size;
728 AudioDeviceID devid_def;
729 AudioDeviceID *p_devices = NULL;
730 vlc_value_t val, text;
732 struct aout_sys_t *p_sys = p_aout->output.p_sys;
736 return (VLC_SUCCESS);
743 /*****************************************************************************
744 * HardwareListener: Warns us of changes in the list of registered devices
745 *****************************************************************************/
746 static OSStatus HardwareListener( AudioHardwarePropertyID inPropertyID,
747 void * inClientData )
749 OSStatus err = noErr;
751 aout_instance_t *p_aout = (aout_instance_t *)inClientData;
752 /* struct aout_sys_t *p_sys = p_aout->output.p_sys; */
754 switch( inPropertyID )
756 case kAudioHardwarePropertyDevices:
758 /* something changed in the list of devices */
759 /* We trigger the audio-device's aout_ChannelsRestart callback */
760 var_Change( p_aout, "audio-device", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );