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
49 #define BUFSIZE 0xffffff
51 /*****************************************************************************
52 * aout_sys_t: private audio output method descriptor
53 *****************************************************************************
54 * This structure is part of the audio output thread descriptor.
55 * It describes the CoreAudio specific properties of an output thread.
56 *****************************************************************************/
59 AudioDeviceID i_default_dev; /* Keeps DeviceID of defaultOutputDevice */
60 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device */
61 UInt32 i_devices; /* Number of CoreAudio Devices */
62 vlc_bool_t b_supports_digital;/* Does the currently selected device support digital mode? */
63 vlc_bool_t b_digital; /* Are we running in digital mode? */
64 Component au_component; /* The Audiocomponent we use */
65 AudioUnit au_unit; /* The AudioUnit we use */
67 uint8_t p_remainder_buffer[BUFSIZE];
68 uint32_t i_read_bytes;
69 uint32_t i_total_bytes;
70 audio_date_t end_date_t;
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Open ( vlc_object_t * );
79 static void Close ( vlc_object_t * );
81 static void Play ( aout_instance_t *);
83 static int Probe ( aout_instance_t * );
84 static int DeviceDigitalMode ( aout_instance_t *, AudioDeviceID );
85 static int DigitalInit ( aout_instance_t * );
87 static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
88 unsigned int, unsigned int, AudioBufferList *);
89 static OSStatus HardwareListener ( AudioHardwarePropertyID, void *);
91 /*****************************************************************************
93 *****************************************************************************/
94 #define ADEV_TEXT N_("Audio Device")
95 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
96 "audio device, as listed in your 'Audio Device' menu. This device will " \
97 "then be used by default for audio playback.")
100 set_shortname( "auhal" );
101 set_description( _("HAL AudioUnit output") );
102 set_capability( "audio output", 101 );
103 set_category( CAT_AUDIO );
104 set_subcategory( SUBCAT_AUDIO_AOUT );
105 set_callbacks( Open, Close );
106 //add_integer( "coreaudio-dev", -1, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE );
109 /*****************************************************************************
110 * Open: open a HAL AudioUnit
111 *****************************************************************************/
112 static int Open( vlc_object_t * p_this )
114 OSStatus err = noErr;
115 ComponentDescription desc;
116 UInt32 i_param_size,i;
117 struct aout_sys_t *p_sys;
119 aout_instance_t *p_aout = (aout_instance_t *)p_this;
121 /* Allocate structure */
122 p_sys = (struct aout_sys_t *)malloc( sizeof( struct aout_sys_t ) );
125 msg_Err( p_aout, "out of memory" );
126 return( VLC_ENOMEM );
129 memset( p_sys, 0, sizeof( struct aout_sys_t ) );
131 p_sys->b_digital = VLC_FALSE; /* We assume we are not digital */
133 p_aout->output.p_sys = p_sys;
134 p_aout->output.pf_play = Play;
136 aout_FormatPrint( p_aout, "VLC is looking for:\n", (audio_sample_format_t *)&p_aout->output.output );
138 /* Build a list of devices */
139 if( var_Type( p_aout, "audio-device" ) == 0 )
142 /*if( Probe( p_aout ) != VLC_SUCCESS );
144 msg_Err( p_aout, "Probe failed" );
150 /* What device do we want? */
151 if( var_Get( p_aout, "audio-device", &val ) < 0 )
153 msg_Err( p_aout, "audio-device var does not exist" );
155 return( VLC_ENOVAR );
157 p_sys->i_selected_dev = val.i_int;
159 /* what is vlc format? if digital, take digital route else AUHAL route */
160 DeviceDigitalMode( p_aout, p_sys->i_selected_dev );
161 /*if( DeviceDigitalMode( p_aout, p_sys->i_selected_dev ) != VLC_SUCCESS );
163 msg_Err( p_aout, "DeviceDigitalMode failed" );
168 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
170 p_sys->b_digital = VLC_TRUE;
171 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
172 msg_Dbg( p_aout, "we found a digital stream, and we WANT a digital stream" );
174 else if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && !p_sys->b_supports_digital )
176 msg_Dbg( p_aout, "we had requested a digital stream, but it's not possible for this device" );
179 /* If analog only start setting up AUHAL */
181 /* Lets go find our Component */
182 desc.componentType = kAudioUnitType_Output;
183 desc.componentSubType = kAudioUnitSubType_HALOutput;
184 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
185 desc.componentFlags = 0;
186 desc.componentFlagsMask = 0;
188 p_sys->au_component = FindNextComponent( NULL, &desc );
189 if( p_sys->au_component == NULL )
191 msg_Err( p_aout, "we cannot find our HAL component" );
196 err = OpenAComponent( p_sys->au_component, &p_sys->au_unit );
200 msg_Err( p_aout, "we cannot find our HAL component" );
205 /* Enable IO for the component */
208 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
209 kAudioOutputUnitProperty_CurrentDevice,
210 kAudioUnitScope_Global,
212 &p_sys->i_selected_dev,
213 sizeof(p_sys->i_selected_dev)));
215 /* Get the current format */
216 AudioStreamBasicDescription DeviceFormat;
218 i_param_size = sizeof(AudioStreamBasicDescription);
220 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
221 kAudioUnitProperty_StreamFormat,
222 kAudioUnitScope_Input,
227 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is " , DeviceFormat ) );
229 /* Get the channel layout */
230 AudioChannelLayout *layout;
231 verify_noerr( AudioUnitGetPropertyInfo( p_sys->au_unit,
232 kAudioDevicePropertyPreferredChannelLayout,
233 kAudioUnitScope_Output,
238 layout = (AudioChannelLayout *)malloc( i_param_size);
240 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
241 kAudioDevicePropertyPreferredChannelLayout,
242 kAudioUnitScope_Output,
247 /* Lets fill out the ChannelLayout */
248 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
250 msg_Dbg( p_aout, "bitmap defined channellayout" );
251 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
252 sizeof( UInt32), &layout->mChannelBitmap,
256 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
258 msg_Dbg( p_aout, "layouttags defined channellayout" );
259 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
260 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
265 msg_Dbg( p_aout, "Layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
267 p_aout->output.output.i_physical_channels = 0;
268 for( i = 0; i < layout->mNumberChannelDescriptions; i++ )
270 msg_Dbg( p_aout, "This is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
272 switch( layout->mChannelDescriptions[i].mChannelLabel )
274 case kAudioChannelLabel_Left:
275 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT;
277 case kAudioChannelLabel_Right:
278 p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT;
280 case kAudioChannelLabel_Center:
281 p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER;
283 case kAudioChannelLabel_LFEScreen:
284 p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE;
286 case kAudioChannelLabel_LeftSurround:
287 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT;
289 case kAudioChannelLabel_RightSurround:
290 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
292 case kAudioChannelLabel_LeftCenter:
293 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
295 case kAudioChannelLabel_RightCenter:
296 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
298 case kAudioChannelLabel_CenterSurround:
299 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER;
302 msg_Warn( p_aout, "Unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
303 switch( layout->mNumberChannelDescriptions )
305 /* We make assumptions based on number of channels here.
306 * Unfortunatly Apple has provided no 100% method to retrieve the speaker configuration */
308 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
311 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
312 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
315 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
316 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
317 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
320 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
321 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
322 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER;
325 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
326 AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
327 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
328 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
332 p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
339 msg_Dbg( p_aout, "defined %d physical channels for vlc core", aout_FormatNbChannels( &p_aout->output.output ) );
340 msg_Dbg( p_aout, "%s", aout_FormatPrintChannels( &p_aout->output.output ));
342 AudioChannelLayout new_layout;
343 memset (&new_layout, 0, sizeof(new_layout));
344 switch( aout_FormatNbChannels( &p_aout->output.output ) )
347 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
350 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
353 if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
355 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
357 else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
359 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
363 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
365 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
367 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
369 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
371 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
373 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
377 if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
379 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
381 else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
383 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
387 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
390 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
391 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
394 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
395 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
399 /* Set up the format to be used */
400 DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
401 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
403 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
404 p_aout->output.output.i_format = VLC_FOURCC( 'f','l','3','2');
405 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
406 DeviceFormat.mBitsPerChannel = 32;
407 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output );
409 /* Calculate framesizes and stuff */
410 aout_FormatPrepare( &p_aout->output.output );
411 DeviceFormat.mFramesPerPacket = 1;
412 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
413 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
415 i_param_size = sizeof(AudioStreamBasicDescription);
416 /* Set desired format (Use CAStreamBasicDescription )*/
417 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
418 kAudioUnitProperty_StreamFormat,
419 kAudioUnitScope_Input,
424 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
426 /* Retrieve actual format??? */
427 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
428 kAudioUnitProperty_StreamFormat,
429 kAudioUnitScope_Input,
434 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
436 p_aout->output.i_nb_samples = 2048;
437 aout_VolumeSoftInit( p_aout );
439 /* Let's pray for the following operation to be atomic... */
440 p_sys->clock_diff = - (mtime_t)
441 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
442 p_sys->clock_diff += mdate();
444 p_sys->i_read_bytes = 0;
445 p_sys->i_total_bytes = 0;
447 /* set the IOproc callback */
448 AURenderCallbackStruct input;
449 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
450 input.inputProcRefCon = p_aout;
452 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
453 kAudioUnitProperty_SetRenderCallback,
454 kAudioUnitScope_Input,
455 0, &input, sizeof( input ) ) );
457 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
458 input.inputProcRefCon = p_aout;
460 /* Set the new_layout as the layout VLC feeds to the AU unit */
461 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
462 kAudioUnitProperty_AudioChannelLayout,
463 kAudioUnitScope_Input,
464 0, &new_layout, sizeof(new_layout) ) );
467 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
469 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
470 return( VLC_SUCCESS );
473 /*****************************************************************************
474 * Close: Close HAL AudioUnit
475 *****************************************************************************/
476 static void Close( vlc_object_t * p_this )
478 aout_instance_t *p_aout = (aout_instance_t *)p_this;
479 struct aout_sys_t *p_sys = p_aout->output.p_sys;
483 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
484 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
485 verify_noerr( CloseComponent( p_sys->au_unit ) );
490 /*****************************************************************************
491 * Play: nothing to do
492 *****************************************************************************/
493 static void Play( aout_instance_t * p_aout )
498 /*****************************************************************************
500 *****************************************************************************/
501 static int Probe( aout_instance_t * p_aout )
503 OSStatus err = noErr;
504 UInt32 i, i_param_size;
505 AudioDeviceID devid_def;
506 AudioDeviceID *p_devices = NULL;
507 vlc_value_t val, text;
509 struct aout_sys_t *p_sys = p_aout->output.p_sys;
511 /* Get number of devices */
512 err = AudioHardwareGetPropertyInfo( kAudioHardwarePropertyDevices,
513 &i_param_size, NULL );
516 msg_Err( p_aout, "could not get number of devices: [%4.4s]", (char *)&err );
520 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
522 if( p_sys->i_devices < 1 )
524 msg_Err( p_aout, "no devices found" );
528 msg_Dbg( p_aout, "system has [%ld] device(s)", p_sys->i_devices );
530 /* Allocate DeviceID array */
531 p_devices = (AudioDeviceID *)malloc( i_param_size );
532 if( p_devices == NULL )
534 msg_Err( p_aout, "out of memory" );
538 /* Populate DeviceID array */
539 err = AudioHardwareGetProperty( kAudioHardwarePropertyDevices,
540 &i_param_size, (void *)p_devices );
543 msg_Err( p_aout, "could not get the device ID's: [%4.4s]", (char *)&err );
547 /* Find the ID of the default Device */
548 i_param_size = sizeof( AudioDeviceID );
549 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
550 &i_param_size, (void *)&devid_def );
553 msg_Err( p_aout, "could not get default audio device: [%4.4s]", (char *)&err );
556 p_sys->i_default_dev = devid_def;
558 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
559 text.psz_string = _("Audio Device");
560 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
562 for( i = 0; i < p_sys->i_devices; i++ )
564 char psz_devuid[1024];
568 i_param_size = sizeof psz_name;
569 err = AudioDeviceGetProperty(
570 p_devices[i], 0, VLC_FALSE,
571 kAudioDevicePropertyDeviceName,
572 &i_param_size, psz_name);
576 i_param_size = sizeof(CFStringRef);
577 err = AudioDeviceGetProperty(
578 p_devices[i], 0, VLC_FALSE,
579 kAudioDevicePropertyDeviceUID,
580 &i_param_size, &devUID);
584 CFStringGetCString( devUID, psz_devuid, sizeof psz_devuid, CFStringGetSystemEncoding() );
585 msg_Dbg( p_aout, "DevID: %lu DevName: %s DevUID: %s", p_devices[i], psz_name, psz_devuid );
588 val.i_int = (int) p_devices[i];
589 text.psz_string = psz_name;
590 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
591 if( devid_def == p_devices[i] )
593 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
594 var_Set( p_aout, "audio-device", val );
597 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
599 /* attach a Listener so that we are notified of a change in the Device setup */
600 /* err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
606 msg_Dbg( p_aout, "succesful finish of deviceslist" );
607 if( p_devices ) free( p_devices );
608 return (VLC_SUCCESS);
611 var_Destroy( p_aout, "audio-device" );
612 if( p_devices ) free( p_devices );
616 /*****************************************************************************
617 * DeviceDigitalMode: Check i_dev_id for digital stream support.
618 *****************************************************************************/
619 static int DeviceDigitalMode( aout_instance_t *p_aout, AudioDeviceID i_dev_id )
621 OSStatus err = noErr;
623 AudioStreamBasicDescription *p_format_list;
625 struct aout_sys_t *p_sys = p_aout->output.p_sys;
627 p_sys->b_supports_digital = VLC_FALSE;
629 err = AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE,
630 kAudioDevicePropertyStreamFormats,
631 &i_param_size, NULL );
634 msg_Err( p_aout, "could not get number of streamsformats: [%4.4s]", (char *)&err );
635 return( VLC_EGENERIC );
638 i_formats = i_param_size / sizeof( AudioStreamBasicDescription );
639 p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size );
640 if( p_format_list == NULL )
642 return( VLC_ENOMEM );
645 err = AudioDeviceGetProperty( i_dev_id, 0, FALSE,
646 kAudioDevicePropertyStreamFormats,
647 &i_param_size, (void *)p_format_list );
650 msg_Err( p_aout, "could not get the list of formats: [%4.4s]", (char *)&err );
651 return( VLC_EGENERIC );
654 for( i = 0; i < i_formats; i++ )
656 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format", p_format_list[i] ) );
658 if( p_format_list[i].mFormatID == 'IAC3' ||
659 p_format_list[i].mFormatID == kAudioFormat60958AC3 )
661 p_sys->b_supports_digital = VLC_TRUE;
662 msg_Dbg( p_aout, "this device supports a digital stream" );
667 free( (void *)p_format_list );
671 /*****************************************************************************
672 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
673 * us to provide some more audio data.
674 * Don't print anything during normal playback, calling blocking function from
675 * this callback is not allowed.
676 *****************************************************************************/
677 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
678 AudioUnitRenderActionFlags *ioActionFlags,
679 const AudioTimeStamp *inTimeStamp,
680 unsigned int inBusNummer,
681 unsigned int inNumberFrames,
682 AudioBufferList *ioData )
684 AudioTimeStamp host_time;
685 mtime_t current_date = 0;
686 uint32_t i_mData_bytes = 0;
688 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
689 struct aout_sys_t * p_sys = p_aout->output.p_sys;
691 host_time.mFlags = kAudioTimeStampHostTimeValid;
692 AudioDeviceTranslateTime( p_sys->i_selected_dev, inTimeStamp, &host_time );
694 p_sys->clock_diff = - (mtime_t)
695 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
696 p_sys->clock_diff += mdate();
698 current_date = p_sys->clock_diff +
699 AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
701 if( ioData == NULL && ioData->mNumberBuffers < 1 )
703 msg_Err( p_aout, "no iodata or buffers");
706 if( ioData->mNumberBuffers > 1 )
707 msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
710 if( p_sys->i_total_bytes > 0 )
712 i_mData_bytes = __MIN( p_sys->i_total_bytes - p_sys->i_read_bytes, ioData->mBuffers[0].mDataByteSize );
713 p_aout->p_vlc->pf_memcpy( ioData->mBuffers[0].mData, &p_sys->p_remainder_buffer[p_sys->i_read_bytes], i_mData_bytes );
714 p_sys->i_read_bytes += i_mData_bytes;
715 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
716 ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
718 if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
719 p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
722 while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
724 /* We don't have enough data yet */
725 aout_buffer_t * p_buffer;
726 p_buffer = aout_OutputNextBuffer( p_aout, current_date , VLC_FALSE );
728 if( p_buffer != NULL )
730 uint32_t i_second_mData_bytes = __MIN( p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
732 p_aout->p_vlc->pf_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes, p_buffer->p_buffer, i_second_mData_bytes );
733 i_mData_bytes += i_second_mData_bytes;
735 if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
737 p_sys->i_total_bytes = p_buffer->i_nb_bytes - i_second_mData_bytes;
738 p_aout->p_vlc->pf_memcpy( p_sys->p_remainder_buffer, &p_buffer->p_buffer[i_second_mData_bytes], p_sys->i_total_bytes );
742 // update current_date
743 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
744 ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output ) ); // 4 is fl32 specific
746 aout_BufferFree( p_buffer );
750 p_aout->p_vlc->pf_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes, 0, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
751 i_mData_bytes += ioData->mBuffers[0].mDataByteSize - i_mData_bytes;
758 /*****************************************************************************
759 * Setup a digital stream
760 *****************************************************************************/
761 static int DigitalInit( aout_instance_t * p_aout )
763 OSStatus err = noErr;
764 UInt32 i, i_param_size;
765 AudioDeviceID devid_def;
766 AudioDeviceID *p_devices = NULL;
767 vlc_value_t val, text;
769 struct aout_sys_t *p_sys = p_aout->output.p_sys;
773 return (VLC_SUCCESS);
780 /*****************************************************************************
781 * HardwareListener: Warns us of changes in the list of registered devices
782 *****************************************************************************/
783 static OSStatus HardwareListener( AudioHardwarePropertyID inPropertyID,
784 void * inClientData )
786 OSStatus err = noErr;
788 aout_instance_t *p_aout = (aout_instance_t *)inClientData;
789 /* struct aout_sys_t *p_sys = p_aout->output.p_sys; */
791 switch( inPropertyID )
793 case kAudioHardwarePropertyDevices:
795 /* something changed in the list of devices */
796 /* We trigger the audio-device's aout_ChannelsRestart callback */
797 var_Change( p_aout, "audio-device", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );