1 /*****************************************************************************
2 * auhal.c: AUHAL and Coreaudio output plugin
3 *****************************************************************************
4 * Copyright (C) 2005, 2011 the VideoLAN team
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_dialog.h> // dialog_Fatal
36 #include <vlc_aout.h> // aout_*
37 #include <vlc_aout_intf.h>
39 #include <AudioUnit/AudioUnit.h> // AudioUnit
40 #include <CoreAudio/CoreAudio.h> // AudioDeviceID
41 #include <AudioToolbox/AudioFormat.h> // AudioFormatGetProperty
42 #include <CoreServices/CoreServices.h>
45 # define verify_noerr(a) assert((a) == noErr)
48 #define STREAM_FORMAT_MSG( pre, sfm ) \
49 pre "[%f][%4.4s][%u][%u][%u][%u][%u][%u]", \
50 sfm.mSampleRate, (char *)&sfm.mFormatID, \
51 (unsigned int)sfm.mFormatFlags, (unsigned int)sfm.mBytesPerPacket, \
52 (unsigned int)sfm.mFramesPerPacket, (unsigned int)sfm.mBytesPerFrame, \
53 (unsigned int)sfm.mChannelsPerFrame, (unsigned int)sfm.mBitsPerChannel
55 #define FRAMESIZE 2048
56 #define BUFSIZE (FRAMESIZE * 8) * 8
57 #define AOUT_VAR_SPDIF_FLAG 0xf00000
61 * - clean up the debug info
62 * - be better at changing stream setup or devices setup changes while playing.
66 /*****************************************************************************
67 * aout_sys_t: private audio output method descriptor
68 *****************************************************************************
69 * This structure is part of the audio output thread descriptor.
70 * It describes the CoreAudio specific properties of an output thread.
71 *****************************************************************************/
75 AudioDeviceID i_default_dev; /* DeviceID of defaultOutputDevice */
76 AudioDeviceID i_selected_dev; /* DeviceID of the selected device */
77 AudioDeviceIOProcID i_procID; /* DeviceID of current device */
78 UInt32 i_devices; /* Number of CoreAudio Devices */
79 bool b_digital; /* Are we running in digital mode? */
80 mtime_t clock_diff; /* Difference between VLC clock and Device clock */
83 Component au_component; /* The Audiocomponent we use */
84 AudioUnit au_unit; /* The AudioUnit we use */
85 uint8_t p_remainder_buffer[BUFSIZE];
86 uint32_t i_read_bytes;
87 uint32_t i_total_bytes;
89 /* CoreAudio SPDIF mode specific */
90 pid_t i_hog_pid; /* The keep the pid of our hog status */
91 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */
92 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */
93 AudioStreamBasicDescription stream_format; /* The format we changed the stream to */
94 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */
95 bool b_revert; /* Wether we need to revert the stream format */
96 bool b_changed_mixing; /* Wether we need to set the mixing mode back */
99 /*****************************************************************************
101 *****************************************************************************/
102 static int Open ( vlc_object_t * );
103 static int OpenAnalog ( audio_output_t * );
104 static int OpenSPDIF ( audio_output_t * );
105 static void Close ( vlc_object_t * );
107 static void Probe ( audio_output_t * );
109 static int AudioDeviceHasOutput ( AudioDeviceID );
110 static int AudioDeviceSupportsDigital( audio_output_t *, AudioDeviceID );
111 static int AudioStreamSupportsDigital( audio_output_t *, AudioStreamID );
112 static int AudioStreamChangeFormat ( audio_output_t *, AudioStreamID, AudioStreamBasicDescription );
114 static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
115 unsigned int, unsigned int, AudioBufferList *);
116 static OSStatus RenderCallbackSPDIF ( AudioDeviceID, const AudioTimeStamp *, const void *, const AudioTimeStamp *,
117 AudioBufferList *, const AudioTimeStamp *, void * );
118 static OSStatus HardwareListener ( AudioObjectID, UInt32, const AudioObjectPropertyAddress *, void * );
119 static OSStatus StreamListener ( AudioObjectID, UInt32, const AudioObjectPropertyAddress *, void * );
120 static int AudioDeviceCallback ( vlc_object_t *, const char *,
121 vlc_value_t, vlc_value_t, void * );
123 static int VolumeSet ( audio_output_t *, float, bool );
126 /*****************************************************************************
128 *****************************************************************************/
129 #define ADEV_TEXT N_("Audio Device")
130 #define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \
131 "audio device, as listed in your 'Audio Device' menu. This device will " \
132 "then be used by default for audio playback.")
135 set_shortname( "auhal" )
136 set_description( N_("HAL AudioUnit output") )
137 set_capability( "audio output", 101 )
138 set_category( CAT_AUDIO )
139 set_subcategory( SUBCAT_AUDIO_AOUT )
140 set_callbacks( Open, Close )
141 add_integer( "macosx-audio-device", 0, ADEV_TEXT, ADEV_LONGTEXT, false )
144 /*****************************************************************************
145 * Open: open macosx audio output
146 *****************************************************************************/
147 static int Open( vlc_object_t * p_this )
149 OSStatus err = noErr;
150 UInt32 i_param_size = 0;
151 struct aout_sys_t *p_sys = NULL;
153 audio_output_t *p_aout = (audio_output_t *)p_this;
155 /* Use int here, to match kAudioDevicePropertyDeviceIsAlive
159 /* Allocate structure */
160 p_aout->sys = malloc( sizeof( aout_sys_t ) );
161 if( p_aout->sys == NULL )
165 p_sys->i_default_dev = 0;
166 p_sys->i_selected_dev = 0;
167 p_sys->i_devices = 0;
168 p_sys->b_digital = false;
169 p_sys->au_component = NULL;
170 p_sys->au_unit = NULL;
171 p_sys->clock_diff = (mtime_t) 0;
172 p_sys->i_read_bytes = 0;
173 p_sys->i_total_bytes = 0;
174 p_sys->i_hog_pid = -1;
175 p_sys->i_stream_id = 0;
176 p_sys->i_stream_index = -1;
177 p_sys->b_revert = false;
178 p_sys->b_changed_mixing = false;
179 memset( p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE );
181 p_aout->pf_play = aout_PacketPlay;
182 p_aout->pf_pause = aout_PacketPause;
183 p_aout->pf_flush = aout_PacketFlush;
185 aout_FormatPrint( p_aout, "VLC is looking for:", &p_aout->format );
187 /* Persistent device variable */
188 if( var_Type( p_aout->p_libvlc, "macosx-audio-device" ) == 0 )
190 var_Create( p_aout->p_libvlc, "macosx-audio-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
193 /* Build a list of devices */
194 if( var_Type( p_aout, "audio-device" ) == 0 )
199 /* What device do we want? */
200 if( var_Get( p_aout, "audio-device", &val ) < 0 )
202 msg_Err( p_aout, "audio-device var does not exist. device probe failed." );
206 p_sys->i_selected_dev = val.i_int & ~AOUT_VAR_SPDIF_FLAG; /* remove SPDIF flag to get the true DeviceID */
207 bool b_supports_digital = ( val.i_int & AOUT_VAR_SPDIF_FLAG );
208 if( b_supports_digital )
209 msg_Dbg( p_aout, "audio device supports digital output" );
211 /* Check if the desired device is alive and usable */
212 /* TODO: add a callback to the device to alert us if the device dies */
213 i_param_size = sizeof( b_alive );
214 AudioObjectPropertyAddress audioDeviceAliveAddress = { kAudioDevicePropertyDeviceIsAlive,
215 kAudioDevicePropertyScopeOutput,
216 kAudioObjectPropertyElementMaster };
217 err = AudioObjectGetPropertyData( p_sys->i_selected_dev, &audioDeviceAliveAddress, 0, NULL, &i_param_size, &b_alive );
221 /* Be tolerant, only give a warning here */
222 msg_Warn( p_aout, "could not check whether device [0x%x] is alive: %4.4s",
223 (unsigned int)p_sys->i_selected_dev, (char *)&err );
229 msg_Warn( p_aout, "selected audio device is not alive, switching to default device" );
230 p_sys->i_selected_dev = p_sys->i_default_dev;
233 AudioObjectPropertyAddress audioDeviceHogModeAddress = { kAudioDevicePropertyHogMode,
234 kAudioDevicePropertyScopeOutput,
235 kAudioObjectPropertyElementMaster };
236 i_param_size = sizeof( p_sys->i_hog_pid );
237 err = AudioObjectGetPropertyData( p_sys->i_selected_dev, &audioDeviceHogModeAddress, 0, NULL, &i_param_size, &p_sys->i_hog_pid );
240 /* This is not a fatal error. Some drivers simply don't support this property */
241 msg_Warn( p_aout, "could not check whether device is hogged: %4.4s",
243 p_sys->i_hog_pid = -1;
246 if( p_sys->i_hog_pid != -1 && p_sys->i_hog_pid != getpid() )
248 msg_Err( p_aout, "Selected audio device is exclusively in use by another program." );
249 dialog_Fatal( p_aout, _("Audio output failed"), "%s",
250 _("The selected audio output device is exclusively in "
251 "use by another program.") );
255 /* If we change the device we want to use, we should renegotiate the audio chain */
256 var_AddCallback( p_aout, "audio-device", AudioDeviceCallback, NULL );
258 /* Check for Digital mode or Analog output mode */
259 if( AOUT_FMT_SPDIF( &p_aout->format ) && b_supports_digital )
261 if( OpenSPDIF( p_aout ) )
263 msg_Dbg( p_aout, "digital output successfully opened" );
269 if( OpenAnalog( p_aout ) )
271 msg_Dbg( p_aout, "analog output successfully opened" );
277 /* If we reach this, this aout has failed */
278 msg_Err( p_aout, "opening the auhal output failed" );
279 var_Destroy( p_aout, "audio-device" );
284 /*****************************************************************************
285 * Open: open and setup a HAL AudioUnit to do analog (multichannel) audio output
286 *****************************************************************************/
287 static int OpenAnalog( audio_output_t *p_aout )
289 struct aout_sys_t *p_sys = p_aout->sys;
290 OSStatus err = noErr;
291 UInt32 i_param_size = 0;
293 ComponentDescription desc;
294 AudioStreamBasicDescription DeviceFormat;
295 AudioChannelLayout *layout;
296 AudioChannelLayout new_layout;
297 AURenderCallbackStruct input;
299 /* Lets go find our Component */
300 desc.componentType = kAudioUnitType_Output;
301 desc.componentSubType = kAudioUnitSubType_HALOutput;
302 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
303 desc.componentFlags = 0;
304 desc.componentFlagsMask = 0;
306 p_sys->au_component = FindNextComponent( NULL, &desc );
307 if( p_sys->au_component == NULL )
309 msg_Warn( p_aout, "we cannot find our HAL component" );
313 err = OpenAComponent( p_sys->au_component, &p_sys->au_unit );
316 msg_Warn( p_aout, "we cannot open our HAL component" );
320 /* Set the device we will use for this output unit */
321 err = AudioUnitSetProperty( p_sys->au_unit,
322 kAudioOutputUnitProperty_CurrentDevice,
323 kAudioUnitScope_Global,
325 &p_sys->i_selected_dev,
326 sizeof( AudioDeviceID ));
330 msg_Warn( p_aout, "we cannot select the audio device" );
334 /* Get the current format */
335 i_param_size = sizeof(AudioStreamBasicDescription);
337 err = AudioUnitGetProperty( p_sys->au_unit,
338 kAudioUnitProperty_StreamFormat,
339 kAudioUnitScope_Input,
344 if( err != noErr ) return false;
345 else msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is: ", DeviceFormat ) );
347 /* Get the channel layout of the device side of the unit (vlc -> unit -> device) */
348 err = AudioUnitGetPropertyInfo( p_sys->au_unit,
349 kAudioDevicePropertyPreferredChannelLayout,
350 kAudioUnitScope_Output,
357 layout = (AudioChannelLayout *)malloc( i_param_size);
359 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
360 kAudioDevicePropertyPreferredChannelLayout,
361 kAudioUnitScope_Output,
366 /* We need to "fill out" the ChannelLayout, because there are multiple ways that it can be set */
367 if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
369 /* bitmap defined channellayout */
370 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap,
371 sizeof( UInt32), &layout->mChannelBitmap,
375 else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions )
377 /* layouttags defined channellayout */
378 verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag,
379 sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag,
384 msg_Dbg( p_aout, "layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions );
386 /* Initialize the VLC core channel count */
387 p_aout->format.i_physical_channels = 0;
388 i_original = p_aout->format.i_original_channels & AOUT_CHAN_PHYSMASK;
390 if( i_original == AOUT_CHAN_CENTER || layout->mNumberChannelDescriptions < 2 )
392 /* We only need Mono or cannot output more than 1 channel */
393 p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
395 else if( i_original == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) || layout->mNumberChannelDescriptions < 3 )
397 /* We only need Stereo or cannot output more than 2 channels */
398 p_aout->format.i_physical_channels = AOUT_CHAN_RIGHT | AOUT_CHAN_LEFT;
402 /* We want more than stereo and we can do that */
403 for( unsigned int i = 0; i < layout->mNumberChannelDescriptions; i++ )
405 msg_Dbg( p_aout, "this is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
407 switch( layout->mChannelDescriptions[i].mChannelLabel )
409 case kAudioChannelLabel_Left:
410 p_aout->format.i_physical_channels |= AOUT_CHAN_LEFT;
412 case kAudioChannelLabel_Right:
413 p_aout->format.i_physical_channels |= AOUT_CHAN_RIGHT;
415 case kAudioChannelLabel_Center:
416 p_aout->format.i_physical_channels |= AOUT_CHAN_CENTER;
418 case kAudioChannelLabel_LFEScreen:
419 p_aout->format.i_physical_channels |= AOUT_CHAN_LFE;
421 case kAudioChannelLabel_LeftSurround:
422 p_aout->format.i_physical_channels |= AOUT_CHAN_REARLEFT;
424 case kAudioChannelLabel_RightSurround:
425 p_aout->format.i_physical_channels |= AOUT_CHAN_REARRIGHT;
427 case kAudioChannelLabel_RearSurroundLeft:
428 p_aout->format.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
430 case kAudioChannelLabel_RearSurroundRight:
431 p_aout->format.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
433 case kAudioChannelLabel_CenterSurround:
434 p_aout->format.i_physical_channels |= AOUT_CHAN_REARCENTER;
437 msg_Warn( p_aout, "unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
440 if( p_aout->format.i_physical_channels == 0 )
442 p_aout->format.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
443 msg_Err( p_aout, "You should configure your speaker layout with Audio Midi Setup Utility in /Applications/Utilities. Now using Stereo mode." );
444 dialog_Fatal( p_aout, _("Audio device is not configured"), "%s",
445 _("You should configure your speaker layout with "
446 "the \"Audio Midi Setup\" utility in /Applications/"
447 "Utilities. Stereo mode is being used now.") );
454 msg_Warn( p_aout, "this driver does not support kAudioDevicePropertyPreferredChannelLayout. BAD DRIVER AUTHOR !!!" );
455 p_aout->format.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
458 msg_Dbg( p_aout, "selected %d physical channels for device output", aout_FormatNbChannels( &p_aout->format ) );
459 msg_Dbg( p_aout, "VLC will output: %s", aout_FormatPrintChannels( &p_aout->format ));
461 memset (&new_layout, 0, sizeof(new_layout));
462 switch( aout_FormatNbChannels( &p_aout->format ) )
465 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
468 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
471 if( p_aout->format.i_physical_channels & AOUT_CHAN_CENTER )
473 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
475 else if( p_aout->format.i_physical_channels & AOUT_CHAN_LFE )
477 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
481 if( p_aout->format.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
483 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
485 else if( p_aout->format.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
487 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
489 else if( p_aout->format.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
491 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
495 if( p_aout->format.i_physical_channels & ( AOUT_CHAN_CENTER ) )
497 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
499 else if( p_aout->format.i_physical_channels & ( AOUT_CHAN_LFE ) )
501 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
505 if( p_aout->format.i_physical_channels & ( AOUT_CHAN_LFE ) )
507 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
511 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_6_0; // L R Ls Rs C Cs
515 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
516 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
519 /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
520 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
524 /* Set up the format to be used */
525 DeviceFormat.mSampleRate = p_aout->format.i_rate;
526 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
528 /* We use float 32. It's the best supported format by both VLC and Coreaudio */
529 p_aout->format.i_format = VLC_CODEC_FL32;
530 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
531 DeviceFormat.mBitsPerChannel = 32;
532 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->format );
534 /* Calculate framesizes and stuff */
535 DeviceFormat.mFramesPerPacket = 1;
536 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
537 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
539 /* Set the desired format */
540 i_param_size = sizeof(AudioStreamBasicDescription);
541 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
542 kAudioUnitProperty_StreamFormat,
543 kAudioUnitScope_Input,
548 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) );
550 /* Retrieve actual format */
551 verify_noerr( AudioUnitGetProperty( p_sys->au_unit,
552 kAudioUnitProperty_StreamFormat,
553 kAudioUnitScope_Input,
558 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
560 /* Do the last VLC aout setups */
561 aout_FormatPrepare( &p_aout->format );
562 aout_PacketInit( p_aout, &p_sys->packet, FRAMESIZE );
563 aout_VolumeHardInit( p_aout, VolumeSet, true );
565 /* set the IOproc callback */
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 will use to feed the AU unit */
578 verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
579 kAudioUnitProperty_AudioChannelLayout,
580 kAudioUnitScope_Input,
581 0, &new_layout, sizeof(new_layout) ) );
583 if( new_layout.mNumberChannelDescriptions > 0 )
584 free( new_layout.mChannelDescriptions );
587 verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
589 /* Find the difference between device clock and mdate clock */
590 p_sys->clock_diff = - (mtime_t)
591 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
592 p_sys->clock_diff += mdate();
595 verify_noerr( AudioOutputUnitStart(p_sys->au_unit) );
600 /*****************************************************************************
601 * Setup a encoded digital stream (SPDIF)
602 *****************************************************************************/
603 static int OpenSPDIF( audio_output_t * p_aout )
605 struct aout_sys_t *p_sys = p_aout->sys;
606 OSStatus err = noErr;
607 UInt32 i_param_size = 0, b_mix = 0;
608 Boolean b_writeable = false;
609 AudioStreamID *p_streams = NULL;
610 unsigned i_streams = 0;
612 /* Start doing the SPDIF setup proces */
613 p_sys->b_digital = true;
616 AudioObjectPropertyAddress audioDeviceHogModeAddress = { kAudioDevicePropertyHogMode, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
617 i_param_size = sizeof( p_sys->i_hog_pid );
618 p_sys->i_hog_pid = getpid() ;
620 err = AudioObjectSetPropertyData( p_sys->i_selected_dev, &audioDeviceHogModeAddress, 0, NULL, i_param_size, &p_sys->i_hog_pid );
624 msg_Err( p_aout, "failed to set hogmode: [%4.4s]", (char *)&err );
628 AudioObjectPropertyAddress audioDeviceSupportsMixingAddress = { kAudioDevicePropertySupportsMixing , kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
630 if (AudioObjectHasProperty(p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress))
632 /* Set mixable to false if we are allowed to */
633 err = AudioObjectIsPropertySettable( p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, &b_writeable );
634 err = AudioObjectGetPropertyDataSize( p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, 0, NULL, &i_param_size );
635 err = AudioObjectGetPropertyData( p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, 0, NULL, &i_param_size, &b_mix );
637 if( err == noErr && b_writeable )
640 err = AudioObjectSetPropertyData( p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, 0, NULL, i_param_size, &b_mix );
641 p_sys->b_changed_mixing = true;
646 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
651 /* Get a list of all the streams on this device */
652 AudioObjectPropertyAddress streamsAddress = { kAudioDevicePropertyStreams, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
653 err = AudioObjectGetPropertyDataSize( p_sys->i_selected_dev, &streamsAddress, 0, NULL, &i_param_size );
656 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
660 i_streams = i_param_size / sizeof( AudioStreamID );
661 p_streams = (AudioStreamID *)malloc( i_param_size );
662 if( p_streams == NULL )
665 err = AudioObjectGetPropertyData( p_sys->i_selected_dev, &streamsAddress, 0, NULL, &i_param_size, p_streams );
669 msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err );
674 AudioObjectPropertyAddress physicalFormatsAddress = { kAudioStreamPropertyAvailablePhysicalFormats, kAudioObjectPropertyScopeGlobal, 0 };
675 for( unsigned i = 0; i < i_streams && p_sys->i_stream_index < 0 ; i++ )
677 /* Find a stream with a cac3 stream */
678 AudioStreamRangedDescription *p_format_list = NULL;
680 bool b_digital = false;
682 /* Retrieve all the stream formats supported by each output stream */
683 err = AudioObjectGetPropertyDataSize( p_streams[i], &physicalFormatsAddress, 0, NULL, &i_param_size );
686 msg_Err( p_aout, "OpenSPDIF: could not get number of streamformats: [%s] (%i)", (char *)&err, (int32_t)err );
690 i_formats = i_param_size / sizeof( AudioStreamRangedDescription );
691 p_format_list = (AudioStreamRangedDescription *)malloc( i_param_size );
692 if( p_format_list == NULL )
695 err = AudioObjectGetPropertyData( p_streams[i], &physicalFormatsAddress, 0, NULL, &i_param_size, p_format_list );
698 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
699 free( p_format_list );
703 /* Check if one of the supported formats is a digital format */
704 for( int j = 0; j < i_formats; j++ )
706 if( p_format_list[j].mFormat.mFormatID == 'IAC3' ||
707 p_format_list[j].mFormat.mFormatID == 'iac3' ||
708 p_format_list[j].mFormat.mFormatID == kAudioFormat60958AC3 ||
709 p_format_list[j].mFormat.mFormatID == kAudioFormatAC3 )
718 /* if this stream supports a digital (cac3) format, then go set it. */
719 int i_requested_rate_format = -1;
720 int i_current_rate_format = -1;
721 int i_backup_rate_format = -1;
723 p_sys->i_stream_id = p_streams[i];
724 p_sys->i_stream_index = i;
726 if( !p_sys->b_revert )
728 AudioObjectPropertyAddress currentPhysicalFormatAddress = { kAudioStreamPropertyPhysicalFormat, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
729 /* Retrieve the original format of this stream first if not done so already */
730 i_param_size = sizeof( p_sys->sfmt_revert );
731 err = AudioObjectGetPropertyData( p_sys->i_stream_id, ¤tPhysicalFormatAddress, 0, NULL, &i_param_size, &p_sys->sfmt_revert );
734 msg_Err( p_aout, "could not retrieve the original streamformat: [%4.4s]", (char *)&err );
737 p_sys->b_revert = true;
740 for( int j = 0; j < i_formats; j++ )
742 if( p_format_list[j].mFormat.mFormatID == 'IAC3' ||
743 p_format_list[j].mFormat.mFormatID == 'iac3' ||
744 p_format_list[j].mFormat.mFormatID == kAudioFormat60958AC3 ||
745 p_format_list[j].mFormat.mFormatID == kAudioFormatAC3 )
747 if( p_format_list[j].mFormat.mSampleRate == p_aout->format.i_rate )
749 i_requested_rate_format = j;
752 else if( p_format_list[j].mFormat.mSampleRate == p_sys->sfmt_revert.mSampleRate )
754 i_current_rate_format = j;
758 if( i_backup_rate_format < 0 || p_format_list[j].mFormat.mSampleRate > p_format_list[i_backup_rate_format].mFormat.mSampleRate )
759 i_backup_rate_format = j;
765 if( i_requested_rate_format >= 0 ) /* We prefer to output at the samplerate of the original audio */
766 p_sys->stream_format = p_format_list[i_requested_rate_format].mFormat;
767 else if( i_current_rate_format >= 0 ) /* If not possible, we will try to use the current samplerate of the device */
768 p_sys->stream_format = p_format_list[i_current_rate_format].mFormat;
769 else p_sys->stream_format = p_format_list[i_backup_rate_format].mFormat; /* And if we have to, any digital format will be just fine (highest rate possible) */
771 free( p_format_list );
775 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) );
777 if( !AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->stream_format ) )
780 /* Set the format flags */
781 if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian )
782 p_aout->format.i_format = VLC_CODEC_SPDIFB;
784 p_aout->format.i_format = VLC_CODEC_SPDIFL;
785 p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
786 p_aout->format.i_frame_length = A52_FRAME_NB;
787 p_aout->format.i_rate = (unsigned int)p_sys->stream_format.mSampleRate;
788 aout_FormatPrepare( &p_aout->format );
789 aout_PacketInit( p_aout, &p_sys->packet, A52_FRAME_NB );
790 p_aout->pf_volume_set = NULL;
792 /* Add IOProc callback */
793 err = AudioDeviceCreateIOProcID( p_sys->i_selected_dev,
794 (AudioDeviceIOProc)RenderCallbackSPDIF,
799 msg_Err( p_aout, "AudioDeviceCreateIOProcID failed: [%4.4s]", (char *)&err );
800 aout_PacketDestroy (p_aout);
804 /* Check for the difference between the Device clock and mdate */
805 p_sys->clock_diff = - (mtime_t)
806 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
807 p_sys->clock_diff += mdate();
810 err = AudioDeviceStart( p_sys->i_selected_dev, p_sys->i_procID );
813 msg_Err( p_aout, "AudioDeviceStart failed: [%4.4s]", (char *)&err );
815 err = AudioDeviceDestroyIOProcID( p_sys->i_selected_dev,
819 msg_Err( p_aout, "AudioDeviceDestroyIOProcID failed: [%4.4s]", (char *)&err );
821 aout_PacketDestroy (p_aout);
829 /*****************************************************************************
830 * Close: Close HAL AudioUnit
831 *****************************************************************************/
832 static void Close( vlc_object_t * p_this )
834 audio_output_t *p_aout = (audio_output_t *)p_this;
835 struct aout_sys_t *p_sys = p_aout->sys;
836 OSStatus err = noErr;
837 UInt32 i_param_size = 0;
841 verify_noerr( AudioOutputUnitStop( p_sys->au_unit ) );
842 verify_noerr( AudioUnitUninitialize( p_sys->au_unit ) );
843 verify_noerr( CloseComponent( p_sys->au_unit ) );
846 if( p_sys->b_digital )
849 err = AudioDeviceStop( p_sys->i_selected_dev,
853 msg_Err( p_aout, "AudioDeviceStop failed: [%4.4s]", (char *)&err );
856 /* Remove IOProc callback */
857 err = AudioDeviceDestroyIOProcID( p_sys->i_selected_dev,
861 msg_Err( p_aout, "AudioDeviceDestroyIOProcID failed: [%4.4s]", (char *)&err );
864 if( p_sys->b_revert )
866 AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->sfmt_revert );
869 if( p_sys->b_changed_mixing && p_sys->sfmt_revert.mFormatID != kAudioFormat60958AC3 )
872 Boolean b_writeable = false;
873 /* Revert mixable to true if we are allowed to */
874 AudioObjectPropertyAddress audioDeviceSupportsMixingAddress = { kAudioDevicePropertySupportsMixing , kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
875 err = AudioObjectIsPropertySettable( p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, &b_writeable );
876 err = AudioObjectGetPropertyData( p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, 0, NULL, &i_param_size, &b_mix );
878 if( err == noErr && b_writeable )
880 msg_Dbg( p_aout, "mixable is: %d", b_mix );
882 err = AudioObjectSetPropertyData( p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, 0, NULL, i_param_size, &b_mix );
887 msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err );
892 AudioObjectPropertyAddress audioDevicesAddress = { kAudioHardwarePropertyDevices, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
893 err = AudioObjectRemovePropertyListener( kAudioObjectSystemObject, &audioDevicesAddress, HardwareListener, NULL );
897 msg_Err( p_aout, "AudioHardwareRemovePropertyListener failed: [%4.4s]", (char *)&err );
900 if( p_sys->i_hog_pid == getpid() )
902 p_sys->i_hog_pid = -1;
903 i_param_size = sizeof( p_sys->i_hog_pid );
904 AudioObjectPropertyAddress audioDeviceHogModeAddress = { kAudioDevicePropertyHogMode,
905 kAudioDevicePropertyScopeOutput,
906 kAudioObjectPropertyElementMaster };
907 err = AudioObjectSetPropertyData( p_sys->i_selected_dev, &audioDeviceHogModeAddress, 0, NULL, i_param_size, &p_sys->i_hog_pid );
908 if( err != noErr ) msg_Err( p_aout, "Could not release hogmode: [%4.4s]", (char *)&err );
911 var_DelCallback( p_aout, "audio-device", AudioDeviceCallback, NULL );
913 aout_PacketDestroy( p_aout );
917 /*****************************************************************************
918 * Probe: Check which devices the OS has, and add them to our audio-device menu
919 *****************************************************************************/
920 static void Probe( audio_output_t * p_aout )
922 OSStatus err = noErr;
923 UInt32 i_param_size = 0;
924 AudioDeviceID devid_def = 0;
925 AudioDeviceID *p_devices = NULL;
926 vlc_value_t val, text;
928 struct aout_sys_t *p_sys = p_aout->sys;
930 /* Get number of devices */
931 AudioObjectPropertyAddress audioDevicesAddress = { kAudioHardwarePropertyDevices, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
932 err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &audioDevicesAddress, 0, NULL, &i_param_size);
935 msg_Err( p_aout, "Could not get number of devices: [%s]", (char *)&err );
939 p_sys->i_devices = i_param_size / sizeof( AudioDeviceID );
941 if( p_sys->i_devices < 1 )
943 msg_Err( p_aout, "No audio output devices were found." );
946 msg_Dbg( p_aout, "found %u audio device(s)", (unsigned)p_sys->i_devices );
948 /* Allocate DeviceID array */
949 p_devices = (AudioDeviceID*)malloc( sizeof(AudioDeviceID) * p_sys->i_devices );
950 if( p_devices == NULL )
953 /* Populate DeviceID array */
954 err = AudioObjectGetPropertyData( kAudioObjectSystemObject, &audioDevicesAddress, 0, NULL, &i_param_size, p_devices );
957 msg_Err( p_aout, "could not get the device IDs: [%s]", (char *)&err );
961 /* Find the ID of the default Device */
962 AudioObjectPropertyAddress defaultDeviceAddress = { kAudioHardwarePropertyDefaultOutputDevice, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
963 i_param_size = sizeof( AudioDeviceID );
964 err= AudioObjectGetPropertyData( kAudioObjectSystemObject, &defaultDeviceAddress, 0, NULL, &i_param_size, &devid_def );
967 msg_Err( p_aout, "could not get default audio device: [%s]", (char *)&err );
970 p_sys->i_default_dev = devid_def;
972 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE );
973 text.psz_string = (char*)_("Audio Device");
974 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
976 AudioObjectPropertyAddress deviceNameAddress = { kAudioDevicePropertyDeviceName, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
978 for( unsigned int i = 0; i < p_sys->i_devices; i++ )
983 /* Retrieve the length of the device name */
984 err = AudioObjectGetPropertyDataSize( p_devices[i], &deviceNameAddress, 0, NULL, &i_param_size );
985 if( err ) goto error;
987 /* Retrieve the name of the device */
988 psz_name = (char *)malloc( i_param_size );
989 err = AudioObjectGetPropertyData( p_devices[i], &deviceNameAddress, 0, NULL, &i_param_size, psz_name );
990 if( err ) goto error;
992 msg_Dbg( p_aout, "DevID: %u DevName: %s", (unsigned)p_devices[i], psz_name );
994 if( !AudioDeviceHasOutput( p_devices[i]) )
996 msg_Dbg( p_aout, "this device is INPUT only. skipping..." );
1001 /* Add the menu entries */
1002 val.i_int = (int)p_devices[i];
1003 text.psz_string = psz_name;
1004 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1005 text.psz_string = NULL;
1006 if( p_sys->i_default_dev == p_devices[i] )
1008 /* The default device is the selected device normally */
1009 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1010 var_Set( p_aout, "audio-device", val );
1013 if( AudioDeviceSupportsDigital( p_aout, p_devices[i] ) )
1015 val.i_int = (int)p_devices[i] | AOUT_VAR_SPDIF_FLAG;
1016 if( asprintf( &text.psz_string, _("%s (Encoded Output)"), psz_name ) != -1 )
1018 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
1019 free( text.psz_string );
1020 if( p_sys->i_default_dev == p_devices[i]
1021 && var_InheritBool( p_aout, "spdif" ) )
1023 /* We selected to prefer SPDIF output if available
1024 * then this "dummy" entry should be selected */
1025 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1026 var_Set( p_aout, "audio-device", val );
1034 /* If a device is already "preselected", then use this device */
1035 var_Get( p_aout->p_libvlc, "macosx-audio-device", &val );
1038 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
1039 var_Set( p_aout, "audio-device", val );
1042 /* Attach a Listener so that we are notified of a change in the Device setup */
1043 err = AudioObjectAddPropertyListener( kAudioObjectSystemObject, &audioDevicesAddress, HardwareListener, (void *)p_aout );
1051 msg_Warn( p_aout, "audio device already in use" );
1056 /*****************************************************************************
1057 * AudioDeviceHasOutput: Checks if the Device actually provides any outputs at all
1058 *****************************************************************************/
1059 static int AudioDeviceHasOutput( AudioDeviceID i_dev_id )
1063 AudioObjectPropertyAddress streamsAddress = { kAudioDevicePropertyStreams, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
1064 verify_noerr( AudioObjectGetPropertyDataSize( i_dev_id, &streamsAddress, 0, NULL, &dataSize ) );
1065 if (dataSize == 0) return FALSE;
1070 /*****************************************************************************
1071 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
1072 *****************************************************************************/
1073 static int AudioDeviceSupportsDigital( audio_output_t *p_aout, AudioDeviceID i_dev_id )
1075 OSStatus err = noErr;
1076 UInt32 i_param_size = 0;
1077 AudioStreamID *p_streams = NULL;
1079 bool b_return = false;
1081 /* Retrieve all the output streams */
1082 AudioObjectPropertyAddress streamsAddress = { kAudioDevicePropertyStreams, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
1083 err = AudioObjectGetPropertyDataSize( i_dev_id, &streamsAddress, 0, NULL, &i_param_size );
1086 msg_Err( p_aout, "could not get number of streams: [%s] (%i)", (char *)&err, (int32_t)err );
1090 i_streams = i_param_size / sizeof( AudioStreamID );
1091 p_streams = (AudioStreamID *)malloc( i_param_size );
1092 if( p_streams == NULL )
1095 err = AudioObjectGetPropertyData( i_dev_id, &streamsAddress, 0, NULL, &i_param_size, p_streams );
1098 msg_Err( p_aout, "could not get list of streams: [%s]", (char *)&err );
1102 for( int i = 0; i < i_streams; i++ )
1104 if( AudioStreamSupportsDigital( p_aout, p_streams[i] ) )
1112 /*****************************************************************************
1113 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
1114 *****************************************************************************/
1115 static int AudioStreamSupportsDigital( audio_output_t *p_aout, AudioStreamID i_stream_id )
1117 OSStatus err = noErr;
1118 UInt32 i_param_size = 0;
1119 AudioStreamRangedDescription *p_format_list = NULL;
1121 bool b_return = false;
1123 /* Retrieve all the stream formats supported by each output stream */
1124 AudioObjectPropertyAddress physicalFormatsAddress = { kAudioStreamPropertyAvailablePhysicalFormats, kAudioObjectPropertyScopeGlobal, 0 };
1125 err = AudioObjectGetPropertyDataSize( i_stream_id, &physicalFormatsAddress, 0, NULL, &i_param_size );
1128 msg_Err( p_aout, "could not get number of streamformats: [%s] (%i)", (char *)&err, (int32_t)err );
1132 i_formats = i_param_size / sizeof( AudioStreamRangedDescription );
1133 msg_Dbg( p_aout, "found %i stream formats", i_formats );
1135 p_format_list = (AudioStreamRangedDescription *)malloc( i_param_size );
1136 if( p_format_list == NULL )
1139 err = AudioObjectGetPropertyData( i_stream_id, &physicalFormatsAddress, 0, NULL, &i_param_size, p_format_list );
1142 msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err );
1143 free( p_format_list);
1144 p_format_list = NULL;
1148 for( int i = 0; i < i_formats; i++ )
1150 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "supported format: ", p_format_list[i].mFormat ) );
1152 if( p_format_list[i].mFormat.mFormatID == 'IAC3' ||
1153 p_format_list[i].mFormat.mFormatID == 'iac3' ||
1154 p_format_list[i].mFormat.mFormatID == kAudioFormat60958AC3 ||
1155 p_format_list[i].mFormat.mFormatID == kAudioFormatAC3 )
1161 free( p_format_list );
1165 /*****************************************************************************
1166 * AudioStreamChangeFormat: Change i_stream_id to change_format
1167 *****************************************************************************/
1168 static int AudioStreamChangeFormat( audio_output_t *p_aout, AudioStreamID i_stream_id, AudioStreamBasicDescription change_format )
1170 OSStatus err = noErr;
1171 UInt32 i_param_size = 0;
1173 AudioObjectPropertyAddress physicalFormatAddress = { kAudioStreamPropertyPhysicalFormat, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
1175 struct { vlc_mutex_t lock; vlc_cond_t cond; } w;
1177 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "setting stream format: ", change_format ) );
1179 /* Condition because SetProperty is asynchronious */
1180 vlc_cond_init( &w.cond );
1181 vlc_mutex_init( &w.lock );
1182 vlc_mutex_lock( &w.lock );
1184 /* Install the callback */
1185 err = AudioObjectAddPropertyListener( i_stream_id, &physicalFormatAddress, StreamListener, (void *)&w );
1188 msg_Err( p_aout, "AudioObjectAddPropertyListener for kAudioStreamPropertyPhysicalFormat failed: [%4.4s]", (char *)&err );
1192 /* change the format */
1193 err = AudioObjectSetPropertyData( i_stream_id, &physicalFormatAddress, 0, NULL, sizeof( AudioStreamBasicDescription ),
1197 msg_Err( p_aout, "could not set the stream format: [%4.4s]", (char *)&err );
1201 /* The AudioStreamSetProperty is not only asynchronious (requiring the locks)
1202 * it is also not atomic in its behaviour.
1203 * Therefore we check 5 times before we really give up.
1204 * FIXME: failing isn't actually implemented yet. */
1205 for( int i = 0; i < 5; i++ )
1207 AudioStreamBasicDescription actual_format;
1208 mtime_t timeout = mdate() + 500000;
1210 if( vlc_cond_timedwait( &w.cond, &w.lock, timeout ) )
1212 msg_Dbg( p_aout, "reached timeout" );
1215 i_param_size = sizeof( AudioStreamBasicDescription );
1216 err = AudioObjectGetPropertyData( i_stream_id, &physicalFormatAddress, 0, NULL, &i_param_size, &actual_format );
1218 msg_Dbg( p_aout, STREAM_FORMAT_MSG( "actual format in use: ", actual_format ) );
1219 if( actual_format.mSampleRate == change_format.mSampleRate &&
1220 actual_format.mFormatID == change_format.mFormatID &&
1221 actual_format.mFramesPerPacket == change_format.mFramesPerPacket )
1223 /* The right format is now active */
1226 /* We need to check again */
1229 /* Removing the property listener */
1230 err = AudioObjectRemovePropertyListener( i_stream_id, &physicalFormatAddress, StreamListener, (void *)&w );
1233 msg_Err( p_aout, "AudioStreamRemovePropertyListener failed: [%4.4s]", (char *)&err );
1237 /* Destroy the lock and condition */
1238 vlc_mutex_unlock( &w.lock );
1239 vlc_mutex_destroy( &w.lock );
1240 vlc_cond_destroy( &w.cond );
1245 /*****************************************************************************
1246 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
1247 * us to provide some more audio data.
1248 * Don't print anything during normal playback, calling blocking function from
1249 * this callback is not allowed.
1250 *****************************************************************************/
1251 static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
1252 AudioUnitRenderActionFlags *ioActionFlags,
1253 const AudioTimeStamp *inTimeStamp,
1254 unsigned int inBusNumber,
1255 unsigned int inNumberFrames,
1256 AudioBufferList *ioData )
1258 AudioTimeStamp host_time;
1259 mtime_t current_date = 0;
1260 uint32_t i_mData_bytes = 0;
1262 audio_output_t * p_aout = (audio_output_t *)_p_aout;
1263 struct aout_sys_t * p_sys = p_aout->sys;
1265 VLC_UNUSED(ioActionFlags);
1266 VLC_UNUSED(inBusNumber);
1267 VLC_UNUSED(inNumberFrames);
1269 host_time.mFlags = kAudioTimeStampHostTimeValid;
1270 AudioDeviceTranslateTime( p_sys->i_selected_dev, inTimeStamp, &host_time );
1272 /* Check for the difference between the Device clock and mdate */
1273 p_sys->clock_diff = - (mtime_t)
1274 AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000;
1275 p_sys->clock_diff += mdate();
1277 current_date = p_sys->clock_diff +
1278 AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
1279 //- ((mtime_t) 1000000 / p_aout->format.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1281 if( ioData == NULL && ioData->mNumberBuffers < 1 )
1283 msg_Err( p_aout, "no iodata or buffers");
1286 if( ioData->mNumberBuffers > 1 )
1287 msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
1290 if( p_sys->i_total_bytes > 0 )
1292 i_mData_bytes = __MIN( p_sys->i_total_bytes - p_sys->i_read_bytes, ioData->mBuffers[0].mDataByteSize );
1293 vlc_memcpy( ioData->mBuffers[0].mData,
1294 &p_sys->p_remainder_buffer[p_sys->i_read_bytes],
1296 p_sys->i_read_bytes += i_mData_bytes;
1297 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->format.i_rate ) *
1298 ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->format ) ); // 4 is fl32 specific
1300 if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
1301 p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
1304 while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
1306 /* We don't have enough data yet */
1307 aout_buffer_t * p_buffer;
1308 p_buffer = aout_PacketNext( p_aout, current_date );
1310 if( p_buffer != NULL )
1312 uint32_t i_second_mData_bytes = __MIN( p_buffer->i_buffer, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1314 vlc_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes,
1315 p_buffer->p_buffer, i_second_mData_bytes );
1316 i_mData_bytes += i_second_mData_bytes;
1318 if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
1320 p_sys->i_total_bytes = p_buffer->i_buffer - i_second_mData_bytes;
1321 vlc_memcpy( p_sys->p_remainder_buffer,
1322 &p_buffer->p_buffer[i_second_mData_bytes],
1323 p_sys->i_total_bytes );
1324 aout_BufferFree( p_buffer );
1329 /* update current_date */
1330 current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->format.i_rate ) *
1331 ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->format ) ); // 4 is fl32 specific
1333 aout_BufferFree( p_buffer );
1337 vlc_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes,
1338 0,ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
1339 i_mData_bytes += ioData->mBuffers[0].mDataByteSize - i_mData_bytes;
1345 /*****************************************************************************
1346 * RenderCallbackSPDIF: callback for SPDIF audio output
1347 *****************************************************************************/
1348 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice,
1349 const AudioTimeStamp * inNow,
1350 const void * inInputData,
1351 const AudioTimeStamp * inInputTime,
1352 AudioBufferList * outOutputData,
1353 const AudioTimeStamp * inOutputTime,
1354 void * threadGlobals )
1356 aout_buffer_t * p_buffer;
1357 mtime_t current_date;
1359 audio_output_t * p_aout = (audio_output_t *)threadGlobals;
1360 struct aout_sys_t * p_sys = p_aout->sys;
1362 VLC_UNUSED(inDevice);
1363 VLC_UNUSED(inInputData);
1364 VLC_UNUSED(inInputTime);
1366 /* Check for the difference between the Device clock and mdate */
1367 p_sys->clock_diff = - (mtime_t)
1368 AudioConvertHostTimeToNanos( inNow->mHostTime ) / 1000;
1369 p_sys->clock_diff += mdate();
1371 current_date = p_sys->clock_diff +
1372 AudioConvertHostTimeToNanos( inOutputTime->mHostTime ) / 1000;
1373 //- ((mtime_t) 1000000 / p_aout->format.i_rate * 31 ); // 31 = Latency in Frames. retrieve somewhere
1375 p_buffer = aout_PacketNext( p_aout, current_date );
1377 #define BUFFER outOutputData->mBuffers[p_sys->i_stream_index]
1378 if( p_buffer != NULL )
1380 if( (int)BUFFER.mDataByteSize != (int)p_buffer->i_buffer)
1381 msg_Warn( p_aout, "bytesize: %d nb_bytes: %d", (int)BUFFER.mDataByteSize, (int)p_buffer->i_buffer );
1383 /* move data into output data buffer */
1384 vlc_memcpy( BUFFER.mData, p_buffer->p_buffer, p_buffer->i_buffer );
1385 aout_BufferFree( p_buffer );
1389 vlc_memset( BUFFER.mData, 0, BUFFER.mDataByteSize );
1396 /*****************************************************************************
1397 * HardwareListener: Warns us of changes in the list of registered devices
1398 *****************************************************************************/
1399 static OSStatus HardwareListener( AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress inAddresses[], void*inClientData)
1401 OSStatus err = noErr;
1402 audio_output_t *p_aout = (audio_output_t *)inClientData;
1403 VLC_UNUSED(inObjectID);
1405 for ( unsigned int i = 0; i < inNumberAddresses; i++ )
1407 if( inAddresses[i].mSelector == kAudioHardwarePropertyDevices )
1409 /* something changed in the list of devices */
1410 /* We trigger the audio-device's aout_ChannelsRestart callback */
1411 var_TriggerCallback( p_aout, "audio-device" );
1412 var_Destroy( p_aout, "audio-device" );
1419 /*****************************************************************************
1421 *****************************************************************************/
1422 static OSStatus StreamListener( AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress inAddresses[], void*inClientData)
1424 OSStatus err = noErr;
1425 struct { vlc_mutex_t lock; vlc_cond_t cond; } * w = inClientData;
1427 VLC_UNUSED(inObjectID);
1429 for( unsigned int i = 0; i < inNumberAddresses; i++ )
1431 if( inAddresses[i].mSelector == kAudioStreamPropertyPhysicalFormat )
1433 vlc_mutex_lock( &w->lock );
1434 vlc_cond_signal( &w->cond );
1435 vlc_mutex_unlock( &w->lock );
1442 /*****************************************************************************
1443 * AudioDeviceCallback: Callback triggered when the audio-device variable is changed
1444 *****************************************************************************/
1445 static int AudioDeviceCallback( vlc_object_t *p_this, const char *psz_variable,
1446 vlc_value_t old_val, vlc_value_t new_val, void *param )
1448 audio_output_t *p_aout = (audio_output_t *)p_this;
1449 var_Set( p_aout->p_libvlc, "macosx-audio-device", new_val );
1450 msg_Dbg( p_aout, "Set Device: %#"PRIx64, new_val.i_int );
1451 return aout_ChannelsRestart( p_this, psz_variable, old_val, new_val, param );
1455 /*****************************************************************************
1456 * VolumeSet: Implements pf_volume_set(). Update the CoreAudio AU volume immediately.
1457 *****************************************************************************/
1458 static int VolumeSet( audio_output_t * p_aout, float volume, bool mute )
1460 struct aout_sys_t *p_sys = p_aout->sys;
1466 volume = volume * volume * volume; // cubic mapping from output.c
1468 /* Set volume for output unit */
1469 ostatus = AudioUnitSetParameter( p_sys->au_unit,
1470 kHALOutputParam_Volume,
1471 kAudioUnitScope_Global,