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 *****************************************************************************/
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 "[%ld][%4.4s][%ld][%ld][%ld][%ld][%ld][%ld]", \
44 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
45 sfm.mFormatFlags, sfm.mBytesPerPacket, \
46 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
47 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
49 #define STREAM_FORMAT_MSG_FULL( pre, sfm ) \
50 pre ":\nsamplerate: [%ld]\nFormatID: [%4.4s]\nFormatFlags: [%ld]\nBypesPerPacket: [%ld]\nFramesPerPacket: [%ld]\nBytesPerFrame: [%ld]\nChannelsPerFrame: [%ld]\nBitsPerChannel[%ld]", \
51 (UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \
52 sfm.mFormatFlags, sfm.mBytesPerPacket, \
53 sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
54 sfm.mChannelsPerFrame, sfm.mBitsPerChannel
56 #define BUFSIZE 0xffffff
57 #define AOUT_VAR_SPDIF_FLAG 0xf0000000
59 /*****************************************************************************
60 * aout_sys_t: private audio output method descriptor
61 *****************************************************************************
62 * This structure is part of the audio output thread descriptor.
63 * It describes the CoreAudio specific properties of an output thread.
64 *****************************************************************************/
67 AudioDeviceID i_default_dev; /* Keeps DeviceID of defaultOutputDevice */
68 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device */
69 UInt32 i_devices; /* Number of CoreAudio Devices */
70 vlc_bool_t b_supports_digital;/* Does the currently selected device support digital mode? */
71 vlc_bool_t b_digital; /* Are we running in digital mode? */
72 Component au_component; /* The Audiocomponent we use */
73 AudioUnit au_unit; /* The AudioUnit we use */
75 uint8_t p_remainder_buffer[BUFSIZE];
76 uint32_t i_read_bytes;
77 uint32_t i_total_bytes;
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Open ( vlc_object_t * );
84 int OpenAnalog ( aout_instance_t * );
85 int OpenSPDIF ( aout_instance_t * );
86 static void Close ( vlc_object_t * );
88 static void Play ( aout_instance_t * );
89 static void Probe ( aout_instance_t * );
91 int AudioDeviceSupportsDigital( aout_instance_t *, AudioDeviceID );
92 int AudioDeviceHasOutput ( AudioDeviceID );
94 static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
95 unsigned int, unsigned int, AudioBufferList *);
96 static OSStatus HardwareListener ( AudioHardwarePropertyID, void *);
98 /*****************************************************************************
100 *****************************************************************************/
101 #define ADEV_TEXT N_("Audio Device")
102 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
103 "audio device, as listed in your 'Audio Device' menu. This device will " \
104 "then be used by default for audio playback.")
107 set_shortname( "auhal" );
108 set_description( _("HAL AudioUnit output") );
109 set_capability( "audio output", 101 );
110 set_category( CAT_AUDIO );
111 set_subcategory( SUBCAT_AUDIO_AOUT );
112 set_callbacks( Open, Close );
113 //add_integer( "macosx-audio-device", -1, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE );
116 /*****************************************************************************
117 * Open: open a HAL AudioUnit
118 *****************************************************************************/
119 static int Open( vlc_object_t * p_this )
121 OSStatus err = noErr;
122 UInt32 i_param_size = 0, i_hog = 0;
123 struct aout_sys_t *p_sys = NULL;
124 vlc_bool_t b_alive = VLC_FALSE;
126 aout_instance_t *p_aout = (aout_instance_t *)p_this;
128 /* Allocate structure */
129 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
130 if( p_aout->output.p_sys == NULL )
132 msg_Err( p_aout, "out of memory" );
133 return( VLC_ENOMEM );
136 p_sys = p_aout->output.p_sys;
137 p_sys->i_default_dev = 0;
138 p_sys->i_selected_dev = 0;
139 p_sys->i_devices = 0;
140 p_sys->b_supports_digital = VLC_FALSE;
141 p_sys->b_digital = VLC_FALSE;
142 p_sys->au_component = NULL;
143 p_sys->au_unit = NULL;
144 p_sys->clock_diff = (mtime_t) 0;
145 p_sys->i_read_bytes = 0;
146 p_sys->i_total_bytes = 0;
147 memset( p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE );
149 p_aout->output.pf_play = Play;
151 aout_FormatPrint( p_aout, "VLC is looking for:\n", (audio_sample_format_t *)&p_aout->output.output );
153 /* Build a list of devices */
154 if( var_Type( p_aout, "audio-device" ) == 0 )
159 /* What device do we want? */
160 if( var_Get( p_aout, "audio-device", &val ) < 0 )
162 msg_Err( p_aout, "audio-device var does not exist. device probe failed." );
164 return( VLC_ENOVAR );
167 p_sys->i_selected_dev = val.i_int & ~AOUT_VAR_SPDIF_FLAG;
168 p_sys->b_supports_digital = ( val.i_int & AOUT_VAR_SPDIF_FLAG ) ? VLC_TRUE : VLC_FALSE;
170 /* Check if the desired device is alive and usable */
171 i_param_size = sizeof( b_alive );
172 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
173 kAudioDevicePropertyDeviceIsAlive,
174 &i_param_size, &b_alive );
178 msg_Err( p_aout, "could not check whether device is alive: %4.4s",
183 if( b_alive == VLC_FALSE )
185 msg_Err( p_aout, "Selected audio device is not alive" );
186 var_Destroy( p_aout, "audio-device" );
191 i_param_size = sizeof( i_hog );
192 err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE,
193 kAudioDevicePropertyHogMode,
194 &i_param_size, &i_hog );
198 msg_Err( p_aout, "could not check whether device is hogged: %4.4s",
205 msg_Err( p_aout, "Selected audio device is exclusively in use by another program" );
206 var_Destroy( p_aout, "audio-device" );
211 /* Check for Digital mode or Analog output mode */
212 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
214 if( OpenSPDIF( p_aout ) )
219 if( OpenAnalog( p_aout ) )
223 /* If we reach this, the Open* failed */
224 var_Destroy( p_aout, "audio-device" );
229 int OpenAnalog( aout_instance_t *p_aout )
231 struct aout_sys_t *p_sys = p_aout->output.p_sys;
232 OSStatus err = noErr;
233 UInt32 i_param_size = 0, i = 0;
234 ComponentDescription desc;
236 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && !p_sys->b_supports_digital )
238 msg_Dbg( p_aout, "we had requested a digital stream, but it's not possible for this device" );
241 /* If analog only start setting up AUHAL */
242 /* Lets go find our Component */
243 desc.componentType = kAudioUnitType_Output;
244 desc.componentSubType = kAudioUnitSubType_HALOutput;
245 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
246 desc.componentFlags = 0;
247 desc.componentFlagsMask = 0;
249 p_sys->au_component = FindNextComponent( NULL, &desc );
250 if( p_sys->au_component == NULL )
252 msg_Err( p_aout, "we cannot find our HAL component" );
256 err = OpenAComponent( p_sys->au_component, &p_sys->au_unit );
259 msg_Err( p_aout, "we cannot find our HAL component" );
263 /* Enable IO for the component */
266 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
267 kAudioOutputUnitProperty_CurrentDevice,
268 kAudioUnitScope_Global,
270 &p_sys->i_selected_dev,
271 sizeof(p_sys->i_selected_dev)));
273 /* Get the current format */
274 AudioStreamBasicDescription DeviceFormat;
276 i_param_size = sizeof(AudioStreamBasicDescription);
278 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
279 kAudioUnitProperty_StreamFormat,
280 kAudioUnitScope_Input,
285 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is: " , DeviceFormat ) );
287 /* Get the channel layout */
288 AudioChannelLayout *layout;
289 verify_noerr( AudioUnitGetPropertyInfo( p_sys->au_unit,
290 kAudioDevicePropertyPreferredChannelLayout,
291 kAudioUnitScope_Output,
296 layout = (AudioChannelLayout *)malloc( i_param_size);
298 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
299 kAudioDevicePropertyPreferredChannelLayout,
300 kAudioUnitScope_Output,
305 /* Lets fill out the ChannelLayout */
306 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
308 msg_Dbg( p_aout, "bitmap defined channellayout" );
309 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
310 sizeof( UInt32), &layout->mChannelBitmap,
314 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
316 msg_Dbg( p_aout, "layouttags defined channellayout" );
317 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
318 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
323 msg_Dbg( p_aout, "Layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
325 p_aout->output.output.i_physical_channels = 0;
326 for( i = 0; i < layout->mNumberChannelDescriptions; i++ )
328 msg_Dbg( p_aout, "This is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
330 switch( layout->mChannelDescriptions[i].mChannelLabel )
332 case kAudioChannelLabel_Left:
333 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
335 case kAudioChannelLabel_Right:
336 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
338 case kAudioChannelLabel_Center:
339 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
341 case kAudioChannelLabel_LFEScreen:
342 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE;
344 case kAudioChannelLabel_LeftSurround:
345 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT;
347 case kAudioChannelLabel_RightSurround:
348 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
350 case kAudioChannelLabel_RearSurroundLeft:
351 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
353 case kAudioChannelLabel_RearSurroundRight:
354 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
356 case kAudioChannelLabel_CenterSurround:
357 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER;
360 msg_Warn( p_aout, "Unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
363 msg_Warn( p_aout, "Probably no channellayout is set. force based on channelcount" );
364 switch( layout->mNumberChannelDescriptions )
366 /* We make assumptions based on number of channels here.
367 * Unfortunatly Apple has provided no 100% method to retrieve the speaker configuration */
369 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
372 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
373 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
376 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
377 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
378 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
381 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
382 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
383 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER;
386 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
387 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
388 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
389 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
393 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
401 msg_Dbg( p_aout, "defined %d physical channels for vlc core", aout_FormatNbChannels( &p_aout->output.output ) );
402 msg_Dbg( p_aout, "%s", aout_FormatPrintChannels( &p_aout->output.output ));
404 AudioChannelLayout new_layout;
405 memset (&new_layout, 0, sizeof(new_layout));
406 switch( aout_FormatNbChannels( &p_aout->output.output ) )
409 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
412 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
415 if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
417 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
419 else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
421 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
425 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
427 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
429 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
431 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
433 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
435 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
439 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
441 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
443 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
445 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
449 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
452 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
453 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
456 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
457 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
461 /* Set up the format to be used */
462 DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
463 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
465 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
466 p_aout->output.output.i_format = VLC_FOURCC( 'f','l','3','2');
467 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
468 DeviceFormat.mBitsPerChannel = 32;
469 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output );
471 /* Calculate framesizes and stuff */
472 aout_FormatPrepare( &p_aout->output.output );
473 DeviceFormat.mFramesPerPacket = 1;
474 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
475 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
477 i_param_size = sizeof(AudioStreamBasicDescription);
478 /* Set desired format (Use CAStreamBasicDescription )*/
479 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
480 kAudioUnitProperty_StreamFormat,
481 kAudioUnitScope_Input,
486 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
488 /* Retrieve actual format??? */
489 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
490 kAudioUnitProperty_StreamFormat,
491 kAudioUnitScope_Input,
496 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
498 p_aout->output.i_nb_samples = 2048;
499 aout_VolumeSoftInit( p_aout );
501 /* Let's pray for the following operation to be atomic... */
502 p_sys->clock_diff = - (mtime_t)
503 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
504 p_sys->clock_diff += mdate();
506 p_sys->i_read_bytes = 0;
507 p_sys->i_total_bytes = 0;
509 /* set the IOproc callback */
510 AURenderCallbackStruct input;
511 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
512 input.inputProcRefCon = p_aout;
514 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
515 kAudioUnitProperty_SetRenderCallback,
516 kAudioUnitScope_Input,
517 0, &input, sizeof( input ) ) );
519 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
520 input.inputProcRefCon = p_aout;
522 /* Set the new_layout as the layout VLC feeds to the AU unit */
523 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
524 kAudioUnitProperty_AudioChannelLayout,
525 kAudioUnitScope_Input,
526 0, &new_layout, sizeof(new_layout) ) );
529 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
531 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
536 /*****************************************************************************
537 * Setup a encoded digital stream (SPDIF)
538 *****************************************************************************/
539 int OpenSPDIF( aout_instance_t * p_aout )
541 OSStatus err = noErr;
542 UInt32 i, i_param_size;
543 AudioDeviceID devid_def;
544 AudioDeviceID *p_devices = NULL;
545 vlc_value_t val, text;
547 struct aout_sys_t *p_sys = p_aout->output.p_sys;
549 /* Start doing the SPDIF setup proces */
550 p_sys->b_digital = VLC_TRUE;
551 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
552 msg_Dbg( p_aout, "we found a digital stream, and we WANT a digital stream" );
561 /*****************************************************************************
562 * Close: Close HAL AudioUnit
563 *****************************************************************************/
564 static void Close( vlc_object_t * p_this )
566 aout_instance_t *p_aout = (aout_instance_t *)p_this;
567 struct aout_sys_t *p_sys = p_aout->output.p_sys;
571 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
572 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
573 verify_noerr( CloseComponent( p_sys->au_unit ) );
578 /*****************************************************************************
579 * Play: nothing to do
580 *****************************************************************************/
581 static void Play( aout_instance_t * p_aout )
586 /*****************************************************************************
588 *****************************************************************************/
589 static void Probe( aout_instance_t * p_aout )
591 OSStatus err = noErr;
592 UInt32 i = 0, i_param_size = 0;
593 AudioDeviceID devid_def = 0;
594 AudioDeviceID *p_devices = NULL;
595 vlc_value_t val, text;
597 struct aout_sys_t *p_sys = p_aout->output.p_sys;
599 /* Get number of devices */
600 err = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices,
601 &i_param_size, NULL );
604 msg_Err( p_aout, "could not get number of devices: [%4.4s]", (char *)&err );
608 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
610 if( p_sys->i_devices < 1 )
612 msg_Err( p_aout, "no devices found" );
616 msg_Dbg( p_aout, "system has [%ld] device(s)", p_sys->i_devices );
618 /* Allocate DeviceID array */
619 p_devices = (AudioDeviceID*)malloc( sizeof(AudioDeviceID) * p_sys->i_devices );
620 if( p_devices == NULL )
622 msg_Err( p_aout, "out of memory" );
626 /* Populate DeviceID array */
627 err = AudioHardwareGetProperty( kAudioHardwarePropertyDevices,
628 &i_param_size, p_devices );
631 msg_Err( p_aout, "could not get the device ID's: [%4.4s]", (char *)&err );
635 /* Find the ID of the default Device */
636 i_param_size = sizeof( AudioDeviceID );
637 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
638 &i_param_size, &devid_def );
641 msg_Err( p_aout, "could not get default audio device: [%4.4s]", (char *)&err );
644 p_sys->i_default_dev = devid_def;
646 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
647 text.psz_string = _("Audio Device");
648 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
650 for( i = 0; i < p_sys->i_devices; i++ )
655 /* Retrieve the length of the device name */
656 err = AudioDeviceGetPropertyInfo(
657 p_devices[i], 0, VLC_FALSE,
658 kAudioDevicePropertyDeviceName,
659 &i_param_size, NULL);
660 if( err ) goto error;
662 /* Retrieve the name of the device */
663 psz_name = (char *)malloc( i_param_size );
664 err = AudioDeviceGetProperty(
665 p_devices[i], 0, VLC_FALSE,
666 kAudioDevicePropertyDeviceName,
667 &i_param_size, psz_name);
668 if( err ) goto error;
670 msg_Dbg( p_aout, "DevID: %lu DevName: %s", p_devices[i], psz_name );
672 if( !AudioDeviceHasOutput( p_devices[i]) )
674 msg_Dbg( p_aout, "this device is INPUT only. skipping..." );
678 val.i_int = (int)p_devices[i];
679 text.psz_string = strdup( psz_name );
680 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
681 if( p_sys->i_default_dev == p_devices[i] )
683 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
684 var_Set( p_aout, "audio-device", val );
687 if( AudioDeviceSupportsDigital( p_aout, p_devices[i]) )
689 val.i_int = (int)p_devices[i] | AOUT_VAR_SPDIF_FLAG;
690 asprintf( &text.psz_string, "%s (Encoded Output)", psz_name );
691 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
692 if( p_sys->i_default_dev == p_devices[i] && config_GetInt( p_aout, "spdif" ) )
694 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
695 var_Set( p_aout, "audio-device", val );
701 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
703 /* attach a Listener so that we are notified of a change in the Device setup */
704 err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
710 msg_Dbg( p_aout, "succesful finish of deviceslist" );
711 if( p_devices ) free( p_devices );
715 var_Destroy( p_aout, "audio-device" );
716 if( p_devices ) free( p_devices );
720 /*****************************************************************************
721 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
722 *****************************************************************************/
723 int AudioDeviceSupportsDigital( aout_instance_t *p_aout, AudioDeviceID i_dev_id )
725 OSStatus err = noErr;
726 UInt32 i_param_size = 0;
727 AudioStreamBasicDescription *p_format_list = NULL;
728 AudioStreamID *p_streams = NULL;
730 int i_formats = 0, i_streams = 0;
731 vlc_bool_t b_return = VLC_FALSE;
733 /* Retrieve all the output streams */
734 err = AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE,
735 kAudioDevicePropertyStreams,
736 &i_param_size, NULL );
739 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
743 i_streams = i_param_size / sizeof( AudioStreamID );
744 p_streams = (AudioStreamID *)malloc( i_param_size );
745 if( p_streams == NULL )
747 msg_Err( p_aout, "Out of memory" );
751 err = AudioDeviceGetProperty( i_dev_id, 0, FALSE,
752 kAudioDevicePropertyStreams,
753 &i_param_size, p_streams );
757 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
761 for( i = 0; i < i_streams; i++ )
763 p_format_list = NULL;
765 msg_Dbg( p_aout, "retrieving formats for stream: %#lx", p_streams[i] );
767 /* Retrieve all the stream formats supported by each output stream */
768 err = AudioStreamGetPropertyInfo( p_streams[i], 0,
769 kAudioDevicePropertyStreamFormats,
770 &i_param_size, NULL );
773 msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err );
777 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
778 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
779 if( p_format_list == NULL )
784 err = AudioStreamGetProperty( p_streams[i], 0,
785 kAudioDevicePropertyStreamFormats,
786 &i_param_size, p_format_list );
789 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
790 free( p_format_list);
794 for( j = 0; j < i_formats; j++ )
796 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format", p_format_list[j] ) );
798 if( p_format_list[j].mFormatID == 'IAC3' ||
799 p_format_list[j].mFormatID == kAudioFormat60958AC3 )
802 msg_Dbg( p_aout, "this device supports a digital stream" );
807 if( p_format_list ) free( p_format_list );
810 if( p_streams ) free( p_streams );
814 /*****************************************************************************
815 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
816 * us to provide some more audio data.
817 * Don't print anything during normal playback, calling blocking function from
818 * this callback is not allowed.
819 *****************************************************************************/
820 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
821 AudioUnitRenderActionFlags *ioActionFlags,
822 const AudioTimeStamp *inTimeStamp,
823 unsigned int inBusNummer,
824 unsigned int inNumberFrames,
825 AudioBufferList *ioData )
827 AudioTimeStamp host_time;
828 mtime_t current_date = 0;
829 uint32_t i_mData_bytes = 0;
831 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
832 struct aout_sys_t * p_sys = p_aout->output.p_sys;
834 host_time.mFlags = kAudioTimeStampHostTimeValid;
835 AudioDeviceTranslateTime( p_sys->i_selected_dev, inTimeStamp, &host_time );
837 p_sys->clock_diff = - (mtime_t)
838 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
839 p_sys->clock_diff += mdate();
841 current_date = p_sys->clock_diff +
842 AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
843 //- ((mtime_t) 1000000 / p_aout->output.output.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
845 if( ioData == NULL && ioData->mNumberBuffers < 1 )
847 msg_Err( p_aout, "no iodata or buffers");
850 if( ioData->mNumberBuffers > 1 )
851 msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
854 if( p_sys->i_total_bytes > 0 )
856 i_mData_bytes = __MIN( p_sys->i_total_bytes - p_sys->i_read_bytes, ioData->mBuffers[0].mDataByteSize );
857 p_aout->p_vlc->pf_memcpy( ioData->mBuffers[0].mData, &p_sys->p_remainder_buffer[p_sys->i_read_bytes], i_mData_bytes );
858 p_sys->i_read_bytes += i_mData_bytes;
859 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
860 ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
862 if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
863 p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
866 while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
868 /* We don't have enough data yet */
869 aout_buffer_t * p_buffer;
870 p_buffer = aout_OutputNextBuffer( p_aout, current_date , VLC_FALSE );
872 if( p_buffer != NULL )
874 uint32_t i_second_mData_bytes = __MIN( p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
876 p_aout->p_vlc->pf_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes, p_buffer->p_buffer, i_second_mData_bytes );
877 i_mData_bytes += i_second_mData_bytes;
879 if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
881 p_sys->i_total_bytes = p_buffer->i_nb_bytes - i_second_mData_bytes;
882 p_aout->p_vlc->pf_memcpy( p_sys->p_remainder_buffer, &p_buffer->p_buffer[i_second_mData_bytes], p_sys->i_total_bytes );
886 // update current_date
887 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
888 ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
890 aout_BufferFree( p_buffer );
894 p_aout->p_vlc->pf_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes, 0, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
895 i_mData_bytes += ioData->mBuffers[0].mDataByteSize - i_mData_bytes;
901 int AudioDeviceHasOutput( AudioDeviceID i_dev_id )
906 verify_noerr( AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE, kAudioDevicePropertyStreams, &dataSize, &isWritable) );
907 if (dataSize == 0) return FALSE;
912 /*****************************************************************************
913 * HardwareListener: Warns us of changes in the list of registered devices
914 *****************************************************************************/
915 static OSStatus HardwareListener( AudioHardwarePropertyID inPropertyID,
916 void * inClientData )
918 OSStatus err = noErr;
919 aout_instance_t *p_aout = (aout_instance_t *)inClientData;
921 switch( inPropertyID )
923 case kAudioHardwarePropertyDevices:
925 /* something changed in the list of devices */
926 /* We trigger the audio-device's aout_ChannelsRestart callback */
927 var_Change( p_aout, "audio-device", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );