1 /*****************************************************************************
2 * auhal.c: AUHAL and Coreaudio output plugin
3 *****************************************************************************
4 * Copyright (C) 2005 - 2013 VLC authors and VideoLAN
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
9 * David Fuhrmann <david dot fuhrmann at googlemail dot com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
32 #import <vlc_common.h>
33 #import <vlc_plugin.h>
34 #import <vlc_dialog.h> // dialog_Fatal
35 #import <vlc_aout.h> // aout_*
37 #import <AudioUnit/AudioUnit.h> // AudioUnit
38 #import <CoreAudio/CoreAudio.h> // AudioDeviceID
39 #import <AudioToolbox/AudioFormat.h> // AudioFormatGetProperty
40 #import <CoreServices/CoreServices.h>
42 #import "TPCircularBuffer.h"
45 #pragma mark private declarations
48 # define verify_noerr(a) assert((a) == noErr)
51 #define STREAM_FORMAT_MSG(pre, sfm) \
52 pre "[%f][%4.4s][%u][%u][%u][%u][%u][%u]", \
53 sfm.mSampleRate, (char *)&sfm.mFormatID, \
54 (unsigned int)sfm.mFormatFlags, (unsigned int)sfm.mBytesPerPacket, \
55 (unsigned int)sfm.mFramesPerPacket, (unsigned int)sfm.mBytesPerFrame, \
56 (unsigned int)sfm.mChannelsPerFrame, (unsigned int)sfm.mBitsPerChannel
58 #define AOUT_VAR_SPDIF_FLAG 0xf00000
60 #define AUDIO_BUFFER_SIZE_IN_SECONDS (AOUT_MAX_ADVANCE_TIME / CLOCK_FREQ)
63 #define AOUT_VOLUME_DEFAULT 256
64 #define AOUT_VOLUME_MAX 512
66 #define VOLUME_TEXT N_("Audio volume")
67 #define VOLUME_LONGTEXT VOLUME_TEXT
69 #define DEVICE_TEXT N_("Last audio device")
70 #define DEVICE_LONGTEXT DEVICE_TEXT
72 /*****************************************************************************
73 * aout_sys_t: private audio output method descriptor
74 *****************************************************************************
75 * This structure is part of the audio output thread descriptor.
76 * It describes the CoreAudio specific properties of an output thread.
77 *****************************************************************************/
80 AudioObjectID i_default_dev; /* DeviceID of defaultOutputDevice */
81 AudioObjectID i_selected_dev; /* DeviceID of the selected device */
82 AudioObjectID i_new_selected_dev; /* DeviceID of device which will be selected on start */
83 bool b_selected_dev_is_digital;
84 AudioDeviceIOProcID i_procID; /* DeviceID of current device */
85 bool b_digital; /* Are we running in digital mode? */
86 mtime_t clock_diff; /* Difference between VLC clock and Device clock */
88 uint8_t chans_to_reorder; /* do we need channel reordering */
89 uint8_t chan_table[AOUT_CHAN_MAX];
91 UInt32 i_numberOfChannels;
92 TPCircularBuffer circular_buffer; /* circular buffer to swap the audio data */
95 AudioComponent au_component; /* The AudioComponent we use */
96 AudioUnit au_unit; /* The AudioUnit we use */
98 /* CoreAudio SPDIF mode specific */
99 pid_t i_hog_pid; /* The keep the pid of our hog status */
100 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */
101 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */
102 AudioStreamBasicDescription stream_format; /* The format we changed the stream to */
103 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */
104 bool b_revert; /* Whether we need to revert the stream format */
105 bool b_changed_mixing; /* Whether we need to set the mixing mode back */
108 bool b_got_first_sample; /* did the aout core provide something to render? */
110 int i_rate; /* media sample rate */
111 int i_bytes_per_sample;
113 CFArrayRef device_list;
115 vlc_mutex_t var_lock; /* protects access to device_list and i_selected_dev */
125 #pragma mark local prototypes & module descriptor
127 static int Open (vlc_object_t *);
128 static void Close (vlc_object_t *);
129 static int Start (audio_output_t *, audio_sample_format_t *);
130 static int StartAnalog (audio_output_t *, audio_sample_format_t *);
131 static int StartSPDIF (audio_output_t *, audio_sample_format_t *);
132 static void Stop (audio_output_t *);
134 static void RebuildDeviceList (audio_output_t *);
135 static int SwitchAudioDevice (audio_output_t *p_aout, const char *name);
136 static int VolumeSet (audio_output_t *, float);
137 static int MuteSet (audio_output_t *, bool);
139 static void Play (audio_output_t *, block_t *);
140 static void Pause (audio_output_t *, bool, mtime_t);
141 static void Flush (audio_output_t *, bool);
142 static int TimeGet (audio_output_t *, mtime_t *);
143 static OSStatus RenderCallbackAnalog (vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
144 UInt32 , UInt32, AudioBufferList *);
146 static OSStatus RenderCallbackSPDIF (AudioDeviceID, const AudioTimeStamp *, const void *, const AudioTimeStamp *,
147 AudioBufferList *, const AudioTimeStamp *, void *);
149 static OSStatus DevicesListener (AudioObjectID, UInt32, const AudioObjectPropertyAddress *, void *);
150 static OSStatus DeviceAliveListener (AudioObjectID, UInt32, const AudioObjectPropertyAddress *, void *);
151 static OSStatus StreamsChangedListener (AudioObjectID, UInt32, const AudioObjectPropertyAddress *, void *);
153 static OSStatus StreamListener (AudioObjectID, UInt32, const AudioObjectPropertyAddress *, void *);
155 static int ManageAudioStreamsCallback(audio_output_t *p_aout, AudioDeviceID i_dev_id, bool b_register);
156 static int AudioDeviceHasOutput (AudioDeviceID);
157 static int AudioDeviceSupportsDigital(audio_output_t *, AudioDeviceID);
158 static int AudioStreamSupportsDigital(audio_output_t *, AudioStreamID);
159 static int AudioStreamChangeFormat (audio_output_t *, AudioStreamID, AudioStreamBasicDescription);
163 set_shortname("auhal")
164 set_description(N_("HAL AudioUnit output"))
165 set_capability("audio output", 101)
166 set_category(CAT_AUDIO)
167 set_subcategory(SUBCAT_AUDIO_AOUT)
168 set_callbacks(Open, Close)
169 add_integer("auhal-volume", AOUT_VOLUME_DEFAULT,
170 VOLUME_TEXT, VOLUME_LONGTEXT, true)
171 change_integer_range(0, AOUT_VOLUME_MAX)
172 add_string("auhal-audio-device", "", DEVICE_TEXT, DEVICE_LONGTEXT, true)
173 add_obsolete_integer("macosx-audio-device") /* since 2.1.0 */
177 #pragma mark initialization
179 static int Open(vlc_object_t *obj)
181 audio_output_t *p_aout = (audio_output_t *)obj;
182 aout_sys_t *p_sys = malloc(sizeof (*p_sys));
183 if (unlikely(p_sys == NULL))
186 OSStatus err = noErr;
188 vlc_mutex_init(&p_sys->var_lock);
189 vlc_mutex_init(&p_sys->lock);
190 vlc_cond_init(&p_sys->cond);
191 p_sys->b_digital = false;
194 p_aout->start = Start;
196 p_aout->volume_set = VolumeSet;
197 p_aout->mute_set = MuteSet;
198 p_aout->device_select = SwitchAudioDevice;
199 p_sys->device_list = CFArrayCreate(kCFAllocatorDefault, NULL, 0, NULL);
201 /* Attach a Listener so that we are notified of a change in the Device setup */
202 AudioObjectPropertyAddress audioDevicesAddress = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
203 err = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &audioDevicesAddress, DevicesListener, (void *)p_aout);
205 msg_Err(p_aout, "failed to add listener for audio device configuration [%4.4s]", (char *)&err);
207 RebuildDeviceList(p_aout);
209 /* remember the volume */
210 p_sys->f_volume = var_InheritInteger(p_aout, "auhal-volume") / (float)AOUT_VOLUME_DEFAULT;
211 aout_VolumeReport(p_aout, p_sys->f_volume);
212 p_sys->b_mute = var_InheritBool(p_aout, "mute");
213 aout_MuteReport(p_aout, p_sys->b_mute);
215 char *psz_audio_device = config_GetPsz(p_aout, "auhal-audio-device");
216 SwitchAudioDevice(p_aout, psz_audio_device);
217 free(psz_audio_device);
222 static void Close(vlc_object_t *obj)
224 audio_output_t *p_aout = (audio_output_t *)obj;
225 aout_sys_t *p_sys = p_aout->sys;
227 OSStatus err = noErr;
229 /* remove audio devices callback */
230 AudioObjectPropertyAddress audioDevicesAddress = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
231 err = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &audioDevicesAddress, DevicesListener, (void *)p_aout);
233 msg_Err(p_aout, "AudioHardwareRemovePropertyListener failed [%4.4s]", (char *)&err);
235 vlc_mutex_lock(&p_sys->var_lock);
236 /* remove streams callbacks */
237 CFIndex count = CFArrayGetCount(p_sys->device_list);
239 for (CFIndex x = 0; x < count; x++) {
240 AudioDeviceID deviceId = 0;
241 CFNumberRef cfn_device_id = CFArrayGetValueAtIndex(p_sys->device_list, x);
245 CFNumberGetValue(cfn_device_id, kCFNumberSInt32Type, &deviceId);
246 if (!(deviceId & AOUT_VAR_SPDIF_FLAG)) {
247 ManageAudioStreamsCallback(p_aout, deviceId, false);
252 CFRelease(p_sys->device_list);
253 vlc_mutex_unlock(&p_sys->var_lock);
255 char *psz_device = aout_DeviceGet(p_aout);
256 config_PutPsz(p_aout, "auhal-audio-device", psz_device);
259 vlc_mutex_destroy(&p_sys->var_lock);
260 vlc_mutex_destroy(&p_sys->lock);
261 vlc_cond_destroy(&p_sys->cond);
266 static int Start(audio_output_t *p_aout, audio_sample_format_t *restrict fmt)
268 OSStatus err = noErr;
269 UInt32 i_param_size = 0;
270 struct aout_sys_t *p_sys = NULL;
272 /* Use int here, to match kAudioDevicePropertyDeviceIsAlive
276 bool b_start_digital = false;
279 p_sys->b_digital = false;
280 p_sys->au_component = NULL;
281 p_sys->au_unit = NULL;
282 p_sys->clock_diff = (mtime_t) 0;
283 p_sys->i_hog_pid = -1;
284 p_sys->i_stream_id = 0;
285 p_sys->i_stream_index = -1;
286 p_sys->b_revert = false;
287 p_sys->b_changed_mixing = false;
288 p_sys->i_bytes_per_sample = 0;
290 vlc_mutex_lock(&p_sys->var_lock);
291 p_sys->i_selected_dev = p_sys->i_new_selected_dev;
293 aout_FormatPrint(p_aout, "VLC is looking for:", fmt);
295 msg_Dbg(p_aout, "attempting to use device %i", p_sys->i_selected_dev);
297 AudioObjectPropertyAddress audioDeviceAliveAddress = { kAudioDevicePropertyDeviceIsAlive, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
298 if (p_sys->i_selected_dev > 0) {
299 /* Check if the desired device is alive and usable */
300 i_param_size = sizeof(b_alive);
301 err = AudioObjectGetPropertyData(p_sys->i_selected_dev, &audioDeviceAliveAddress, 0, NULL, &i_param_size, &b_alive);
303 /* Be tolerant, only give a warning here */
304 msg_Warn(p_aout, "could not check whether device [0x%x] is alive [%4.4s]",
305 (unsigned int)p_sys->i_selected_dev, (char *)&err);
310 msg_Warn(p_aout, "selected audio device is not alive, switching to default device");
313 if (!b_alive || p_sys->i_selected_dev == 0) {
314 AudioObjectID defaultDeviceID = 0;
315 UInt32 propertySize = 0;
316 AudioObjectPropertyAddress defaultDeviceAddress = { kAudioHardwarePropertyDefaultOutputDevice, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
317 propertySize = sizeof(AudioObjectID);
318 err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &defaultDeviceAddress, 0, NULL, &propertySize, &defaultDeviceID);
320 msg_Err(p_aout, "could not get default audio device [%4.4s]", (char *)&err);
324 msg_Dbg(p_aout, "using default audio device %i", defaultDeviceID);
326 p_sys->i_selected_dev = defaultDeviceID;
327 p_sys->b_selected_dev_is_digital = var_InheritBool(p_aout, "spdif");
330 // recheck if device still supports digital
331 b_start_digital = p_sys->b_selected_dev_is_digital;
332 if(!AudioDeviceSupportsDigital(p_aout, p_sys->i_selected_dev))
333 b_start_digital = false;
336 msg_Dbg(p_aout, "Using audio device for digital output");
338 msg_Dbg(p_aout, "Audio device supports PCM mode only");
340 /* add a callback to see if the device dies later on */
341 err = AudioObjectAddPropertyListener(p_sys->i_selected_dev, &audioDeviceAliveAddress, DeviceAliveListener, (void *)p_aout);
343 /* Be tolerant, only give a warning here */
344 msg_Warn(p_aout, "could not set alive check callback on device [0x%x] [%4.4s]",
345 (unsigned int)p_sys->i_selected_dev, (char *)&err);
348 AudioObjectPropertyAddress audioDeviceHogModeAddress = { kAudioDevicePropertyHogMode,
349 kAudioDevicePropertyScopeOutput,
350 kAudioObjectPropertyElementMaster };
351 i_param_size = sizeof(p_sys->i_hog_pid);
352 err = AudioObjectGetPropertyData(p_sys->i_selected_dev, &audioDeviceHogModeAddress, 0, NULL, &i_param_size, &p_sys->i_hog_pid);
354 /* This is not a fatal error. Some drivers simply don't support this property */
355 msg_Warn(p_aout, "could not check whether device is hogged [%4.4s]",
357 p_sys->i_hog_pid = -1;
360 if (p_sys->i_hog_pid != -1 && p_sys->i_hog_pid != getpid()) {
361 msg_Err(p_aout, "Selected audio device is exclusively in use by another program.");
362 dialog_Fatal(p_aout, _("Audio output failed"), "%s",
363 _("The selected audio output device is exclusively in "
364 "use by another program."));
368 bool b_success = false;
370 /* Check for Digital mode or Analog output mode */
371 if (AOUT_FMT_SPDIF (fmt) && b_start_digital) {
372 if (StartSPDIF (p_aout, fmt)) {
373 msg_Dbg(p_aout, "digital output successfully opened");
377 if (StartAnalog(p_aout, fmt)) {
378 msg_Dbg(p_aout, "analog output successfully opened");
382 vlc_mutex_unlock(&p_sys->var_lock);
386 p_aout->flush = Flush;
387 p_aout->time_get = TimeGet;
388 p_aout->pause = Pause;
393 /* If we reach this, this aout has failed */
394 vlc_mutex_unlock(&p_sys->var_lock);
395 msg_Err(p_aout, "opening auhal output failed");
400 * StartAnalog: open and setup a HAL AudioUnit to do PCM audio output
402 static int StartAnalog(audio_output_t *p_aout, audio_sample_format_t *fmt)
404 struct aout_sys_t *p_sys = p_aout->sys;
405 OSStatus err = noErr;
406 UInt32 i_param_size = 0;
408 AudioComponentDescription desc;
409 AudioStreamBasicDescription DeviceFormat;
410 AudioChannelLayout *layout;
411 AudioChannelLayout new_layout;
412 AURenderCallbackStruct input;
413 p_aout->sys->chans_to_reorder = 0;
415 SInt32 currentMinorSystemVersion;
416 if(Gestalt(gestaltSystemVersionMinor, ¤tMinorSystemVersion) != noErr)
417 msg_Err(p_aout, "failed to check OSX version");
419 /* Lets go find our Component */
420 desc.componentType = kAudioUnitType_Output;
421 desc.componentSubType = kAudioUnitSubType_HALOutput;
422 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
423 desc.componentFlags = 0;
424 desc.componentFlagsMask = 0;
426 p_sys->au_component = AudioComponentFindNext(NULL, &desc);
427 if (p_sys->au_component == NULL) {
428 msg_Err(p_aout, "cannot find any HAL component, PCM output failed");
432 err = AudioComponentInstanceNew(p_sys->au_component, &p_sys->au_unit);
434 msg_Err(p_aout, "cannot open HAL component, PCM output failed [%4.4s]", (char *)&err);
438 /* Set the device we will use for this output unit */
439 err = AudioUnitSetProperty(p_sys->au_unit,
440 kAudioOutputUnitProperty_CurrentDevice,
441 kAudioUnitScope_Global,
443 &p_sys->i_selected_dev,
444 sizeof(AudioObjectID));
447 msg_Err(p_aout, "cannot select audio output device, PCM output failed [%4.4s]", (char *)&err);
451 /* Get the current format */
452 i_param_size = sizeof(AudioStreamBasicDescription);
454 err = AudioUnitGetProperty(p_sys->au_unit,
455 kAudioUnitProperty_StreamFormat,
456 kAudioUnitScope_Output,
462 msg_Err(p_aout, "failed to detect supported stream formats [%4.4s]", (char *)&err);
465 msg_Dbg(p_aout, STREAM_FORMAT_MSG("current format is: ", DeviceFormat));
467 /* Get the channel layout of the device side of the unit (vlc -> unit -> device) */
468 err = AudioUnitGetPropertyInfo(p_sys->au_unit,
469 kAudioDevicePropertyPreferredChannelLayout,
470 kAudioUnitScope_Output,
476 layout = (AudioChannelLayout *)malloc(i_param_size);
478 verify_noerr(AudioUnitGetProperty(p_sys->au_unit,
479 kAudioDevicePropertyPreferredChannelLayout,
480 kAudioUnitScope_Output,
485 /* We need to "fill out" the ChannelLayout, because there are multiple ways that it can be set */
486 if (layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap) {
487 /* bitmap defined channellayout */
488 verify_noerr(AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForBitmap,
489 sizeof(UInt32), &layout->mChannelBitmap,
492 } else if (layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions)
494 /* layouttags defined channellayout */
495 verify_noerr(AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForTag,
496 sizeof(AudioChannelLayoutTag), &layout->mChannelLayoutTag,
501 msg_Dbg(p_aout, "layout of AUHAL has %i channels" , layout->mNumberChannelDescriptions);
503 if (layout->mNumberChannelDescriptions == 0) {
504 msg_Err(p_aout, "insufficient number of output channels");
508 /* Initialize the VLC core channel count */
509 fmt->i_physical_channels = 0;
510 i_original = fmt->i_original_channels & AOUT_CHAN_PHYSMASK;
512 if (i_original == AOUT_CHAN_CENTER || layout->mNumberChannelDescriptions < 2) {
513 /* We only need Mono or cannot output more than 1 channel */
514 fmt->i_physical_channels = AOUT_CHAN_CENTER;
515 } else if (i_original == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) || layout->mNumberChannelDescriptions < 3) {
516 /* We only need Stereo or cannot output more than 2 channels */
517 fmt->i_physical_channels = AOUT_CHANS_STEREO;
519 /* We want more than stereo and we can do that */
520 for (unsigned int i = 0; i < layout->mNumberChannelDescriptions; i++) {
522 msg_Dbg(p_aout, "this is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel);
525 switch(layout->mChannelDescriptions[i].mChannelLabel) {
526 case kAudioChannelLabel_Left:
527 fmt->i_physical_channels |= AOUT_CHAN_LEFT;
529 case kAudioChannelLabel_Right:
530 fmt->i_physical_channels |= AOUT_CHAN_RIGHT;
532 case kAudioChannelLabel_Center:
533 fmt->i_physical_channels |= AOUT_CHAN_CENTER;
535 case kAudioChannelLabel_LFEScreen:
536 fmt->i_physical_channels |= AOUT_CHAN_LFE;
538 case kAudioChannelLabel_LeftSurround:
539 fmt->i_physical_channels |= AOUT_CHAN_REARLEFT;
541 case kAudioChannelLabel_RightSurround:
542 fmt->i_physical_channels |= AOUT_CHAN_REARRIGHT;
544 case kAudioChannelLabel_RearSurroundLeft:
545 fmt->i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
547 case kAudioChannelLabel_RearSurroundRight:
548 fmt->i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
550 case kAudioChannelLabel_CenterSurround:
551 fmt->i_physical_channels |= AOUT_CHAN_REARCENTER;
554 msg_Warn(p_aout, "unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel);
557 if (fmt->i_physical_channels == 0) {
558 fmt->i_physical_channels = AOUT_CHANS_STEREO;
559 msg_Err(p_aout, "You should configure your speaker layout with Audio Midi Setup in /Applications/Utilities. VLC will output Stereo only.");
560 dialog_Fatal(p_aout, _("Audio device is not configured"), "%s",
561 _("You should configure your speaker layout with "
562 "\"Audio Midi Setup\" in /Applications/"
563 "Utilities. VLC will output Stereo only."));
568 msg_Warn(p_aout, "device driver does not support kAudioDevicePropertyPreferredChannelLayout - using stereo fallback [%4.4s]", (char *)&err);
569 fmt->i_physical_channels = AOUT_CHANS_STEREO;
572 msg_Dbg(p_aout, "selected %d physical channels for device output", aout_FormatNbChannels(fmt));
573 msg_Dbg(p_aout, "VLC will output: %s", aout_FormatPrintChannels(fmt));
574 p_sys->i_numberOfChannels = aout_FormatNbChannels(fmt);
576 memset (&new_layout, 0, sizeof(new_layout));
577 uint32_t chans_out[AOUT_CHAN_MAX];
579 /* Some channel abbreviations used below:
584 * Rs - right surround
585 * Cs - center surround
586 * Rls - rear left surround
587 * Rrs - rear right surround
590 * Lsd - left surround direct
591 * Rsd - right surround direct
595 * Vhl - vertical height left
596 * Vhc - vertical height center
597 * Vhr - vertical height right
598 * Lt - left matrix total. for matrix encoded stereo.
599 * Rt - right matrix total. for matrix encoded stereo. */
601 switch(aout_FormatNbChannels(fmt)) {
603 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
606 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
609 if (fmt->i_physical_channels & AOUT_CHAN_CENTER)
610 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
611 else if (fmt->i_physical_channels & AOUT_CHAN_LFE)
612 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
615 if (fmt->i_physical_channels & (AOUT_CHAN_CENTER | AOUT_CHAN_LFE))
616 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
617 else if (fmt->i_physical_channels & (AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT))
618 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
619 else if (fmt->i_physical_channels & (AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER))
620 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
623 if (fmt->i_physical_channels & (AOUT_CHAN_CENTER))
624 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
625 else if (fmt->i_physical_channels & (AOUT_CHAN_LFE))
626 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
629 if (fmt->i_physical_channels & (AOUT_CHAN_LFE)) {
630 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
632 chans_out[0] = AOUT_CHAN_LEFT;
633 chans_out[1] = AOUT_CHAN_RIGHT;
634 chans_out[2] = AOUT_CHAN_REARLEFT;
635 chans_out[3] = AOUT_CHAN_REARRIGHT;
636 chans_out[4] = AOUT_CHAN_CENTER;
637 chans_out[5] = AOUT_CHAN_LFE;
639 p_aout->sys->chans_to_reorder = aout_CheckChannelReorder(NULL, chans_out, fmt->i_physical_channels, p_aout->sys->chan_table);
640 if (p_aout->sys->chans_to_reorder)
641 msg_Dbg(p_aout, "channel reordering needed for 5.1 output");
643 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_6_0; // L R Ls Rs C Cs
645 chans_out[0] = AOUT_CHAN_LEFT;
646 chans_out[1] = AOUT_CHAN_RIGHT;
647 chans_out[2] = AOUT_CHAN_REARLEFT;
648 chans_out[3] = AOUT_CHAN_REARRIGHT;
649 chans_out[4] = AOUT_CHAN_CENTER;
650 chans_out[5] = AOUT_CHAN_REARCENTER;
652 p_aout->sys->chans_to_reorder = aout_CheckChannelReorder(NULL, chans_out, fmt->i_physical_channels, p_aout->sys->chan_table);
653 if (p_aout->sys->chans_to_reorder)
654 msg_Dbg(p_aout, "channel reordering needed for 6.0 output");
658 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
660 chans_out[0] = AOUT_CHAN_LEFT;
661 chans_out[1] = AOUT_CHAN_RIGHT;
662 chans_out[2] = AOUT_CHAN_CENTER;
663 chans_out[3] = AOUT_CHAN_LFE;
664 chans_out[4] = AOUT_CHAN_REARLEFT;
665 chans_out[5] = AOUT_CHAN_REARRIGHT;
666 chans_out[6] = AOUT_CHAN_REARCENTER;
668 p_aout->sys->chans_to_reorder = aout_CheckChannelReorder(NULL, chans_out, fmt->i_physical_channels, p_aout->sys->chan_table);
669 if (p_aout->sys->chans_to_reorder)
670 msg_Dbg(p_aout, "channel reordering needed for 6.1 output");
674 if (fmt->i_physical_channels & (AOUT_CHAN_LFE) || currentMinorSystemVersion < 7) {
675 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
677 chans_out[0] = AOUT_CHAN_LEFT;
678 chans_out[1] = AOUT_CHAN_RIGHT;
679 chans_out[2] = AOUT_CHAN_CENTER;
680 chans_out[3] = AOUT_CHAN_LFE;
681 chans_out[4] = AOUT_CHAN_MIDDLELEFT;
682 chans_out[5] = AOUT_CHAN_MIDDLERIGHT;
683 chans_out[6] = AOUT_CHAN_REARLEFT;
684 chans_out[7] = AOUT_CHAN_REARRIGHT;
686 if (!(fmt->i_physical_channels & (AOUT_CHAN_LFE)))
687 msg_Warn(p_aout, "8.0 audio output not supported on OS X 10.%i, layout will be incorrect", currentMinorSystemVersion);
689 #ifdef MAC_OS_X_VERSION_10_7
691 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DTS_8_0_B; // Lc C Rc L R Ls Cs Rs
693 chans_out[0] = AOUT_CHAN_MIDDLELEFT;
694 chans_out[1] = AOUT_CHAN_CENTER;
695 chans_out[2] = AOUT_CHAN_MIDDLERIGHT;
696 chans_out[3] = AOUT_CHAN_LEFT;
697 chans_out[4] = AOUT_CHAN_RIGHT;
698 chans_out[5] = AOUT_CHAN_REARLEFT;
699 chans_out[6] = AOUT_CHAN_REARCENTER;
700 chans_out[7] = AOUT_CHAN_REARRIGHT;
703 p_aout->sys->chans_to_reorder = aout_CheckChannelReorder(NULL, chans_out, fmt->i_physical_channels, p_aout->sys->chan_table);
704 if (p_aout->sys->chans_to_reorder)
705 msg_Dbg(p_aout, "channel reordering needed for 7.1 / 8.0 output");
709 if (currentMinorSystemVersion < 7) {
710 msg_Warn(p_aout, "8.1 audio output not supported on OS X 10.%i", currentMinorSystemVersion);
714 #ifdef MAC_OS_X_VERSION_10_7
715 new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DTS_8_1_B; // Lc C Rc L R Ls Cs Rs LFE
716 chans_out[0] = AOUT_CHAN_MIDDLELEFT;
717 chans_out[1] = AOUT_CHAN_CENTER;
718 chans_out[2] = AOUT_CHAN_MIDDLERIGHT;
719 chans_out[3] = AOUT_CHAN_LEFT;
720 chans_out[4] = AOUT_CHAN_RIGHT;
721 chans_out[5] = AOUT_CHAN_REARLEFT;
722 chans_out[6] = AOUT_CHAN_REARCENTER;
723 chans_out[7] = AOUT_CHAN_REARRIGHT;
724 chans_out[8] = AOUT_CHAN_LFE;
726 p_aout->sys->chans_to_reorder = aout_CheckChannelReorder(NULL, chans_out, fmt->i_physical_channels, p_aout->sys->chan_table);
727 if (p_aout->sys->chans_to_reorder)
728 msg_Dbg(p_aout, "channel reordering needed for 8.1 output");
733 /* Set up the format to be used */
734 DeviceFormat.mSampleRate = fmt->i_rate;
735 DeviceFormat.mFormatID = kAudioFormatLinearPCM;
736 p_sys->i_rate = fmt->i_rate;
738 /* We use float 32 since this is VLC's endorsed format */
739 fmt->i_format = VLC_CODEC_FL32;
740 DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
741 DeviceFormat.mBitsPerChannel = 32;
742 DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels(fmt);
744 /* Calculate framesizes and stuff */
745 DeviceFormat.mFramesPerPacket = 1;
746 DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8;
747 DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket;
749 /* Set the desired format */
750 i_param_size = sizeof(AudioStreamBasicDescription);
751 verify_noerr(AudioUnitSetProperty(p_sys->au_unit,
752 kAudioUnitProperty_StreamFormat,
753 kAudioUnitScope_Input,
758 msg_Dbg(p_aout, STREAM_FORMAT_MSG("we set the AU format: " , DeviceFormat));
760 /* Retrieve actual format */
761 verify_noerr(AudioUnitGetProperty(p_sys->au_unit,
762 kAudioUnitProperty_StreamFormat,
763 kAudioUnitScope_Input,
768 msg_Dbg(p_aout, STREAM_FORMAT_MSG("the actual set AU format is " , DeviceFormat));
770 /* Do the last VLC aout setups */
771 aout_FormatPrepare(fmt);
773 /* set the IOproc callback */
774 input.inputProc = (AURenderCallback) RenderCallbackAnalog;
775 input.inputProcRefCon = p_aout;
777 verify_noerr(AudioUnitSetProperty(p_sys->au_unit,
778 kAudioUnitProperty_SetRenderCallback,
779 kAudioUnitScope_Input,
780 0, &input, sizeof(input)));
782 /* Set the new_layout as the layout VLC will use to feed the AU unit */
783 verify_noerr(AudioUnitSetProperty(p_sys->au_unit,
784 kAudioUnitProperty_AudioChannelLayout,
785 kAudioUnitScope_Output,
786 0, &new_layout, sizeof(new_layout)));
788 if (new_layout.mNumberChannelDescriptions > 0)
789 free(new_layout.mChannelDescriptions);
792 verify_noerr(AudioUnitInitialize(p_sys->au_unit));
794 /* Find the difference between device clock and mdate clock */
795 p_sys->clock_diff = - (mtime_t)
796 AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()) / 1000;
797 p_sys->clock_diff += mdate();
799 /* setup circular buffer */
800 TPCircularBufferInit(&p_sys->circular_buffer, AUDIO_BUFFER_SIZE_IN_SECONDS *
801 fmt->i_rate * fmt->i_bytes_per_frame);
803 p_sys->b_got_first_sample = false;
805 /* Set volume for output unit */
806 VolumeSet(p_aout, p_sys->f_volume);
807 MuteSet(p_aout, p_sys->b_mute);
813 * StartSPDIF: Setup an encoded digital stream (SPDIF) output
815 static int StartSPDIF(audio_output_t * p_aout, audio_sample_format_t *fmt)
817 struct aout_sys_t *p_sys = p_aout->sys;
818 OSStatus err = noErr;
819 UInt32 i_param_size = 0, b_mix = 0;
820 Boolean b_writeable = false;
821 AudioStreamID *p_streams = NULL;
822 unsigned i_streams = 0;
824 /* Start doing the SPDIF setup proces */
825 p_sys->b_digital = true;
828 AudioObjectPropertyAddress audioDeviceHogModeAddress = { kAudioDevicePropertyHogMode, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
829 i_param_size = sizeof(p_sys->i_hog_pid);
830 p_sys->i_hog_pid = getpid() ;
832 err = AudioObjectSetPropertyData(p_sys->i_selected_dev, &audioDeviceHogModeAddress, 0, NULL, i_param_size, &p_sys->i_hog_pid);
835 msg_Err(p_aout, "failed to set hogmode [%4.4s]", (char *)&err);
839 AudioObjectPropertyAddress audioDeviceSupportsMixingAddress = { kAudioDevicePropertySupportsMixing , kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
841 if (AudioObjectHasProperty(p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress)) {
842 /* Set mixable to false if we are allowed to */
843 err = AudioObjectIsPropertySettable(p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, &b_writeable);
844 err = AudioObjectGetPropertyDataSize(p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, 0, NULL, &i_param_size);
845 err = AudioObjectGetPropertyData(p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, 0, NULL, &i_param_size, &b_mix);
847 if (err == noErr && b_writeable) {
849 err = AudioObjectSetPropertyData(p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, 0, NULL, i_param_size, &b_mix);
850 p_sys->b_changed_mixing = true;
854 msg_Err(p_aout, "failed to set mixmode [%4.4s]", (char *)&err);
859 /* Get a list of all the streams on this device */
860 AudioObjectPropertyAddress streamsAddress = { kAudioDevicePropertyStreams, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
861 err = AudioObjectGetPropertyDataSize(p_sys->i_selected_dev, &streamsAddress, 0, NULL, &i_param_size);
863 msg_Err(p_aout, "could not get size of stream description packet [%4.4s]", (char *)&err);
867 i_streams = i_param_size / sizeof(AudioStreamID);
868 p_streams = (AudioStreamID *)malloc(i_param_size);
869 if (p_streams == NULL)
872 err = AudioObjectGetPropertyData(p_sys->i_selected_dev, &streamsAddress, 0, NULL, &i_param_size, p_streams);
875 msg_Err(p_aout, "could not fetch stream descriptions [%4.4s]", (char *)&err);
880 AudioObjectPropertyAddress physicalFormatsAddress = { kAudioStreamPropertyAvailablePhysicalFormats, kAudioObjectPropertyScopeGlobal, 0 };
881 for (unsigned i = 0; i < i_streams && p_sys->i_stream_index < 0 ; i++) {
882 /* Find a stream with a cac3 stream */
883 AudioStreamRangedDescription *p_format_list = NULL;
885 bool b_digital = false;
887 /* Retrieve all the stream formats supported by each output stream */
888 err = AudioObjectGetPropertyDataSize(p_streams[i], &physicalFormatsAddress, 0, NULL, &i_param_size);
890 msg_Err(p_aout, "could not get number of streamformats: [%4.4s] (%i)", (char *)&err, (int32_t)err);
894 i_formats = i_param_size / sizeof(AudioStreamRangedDescription);
895 p_format_list = (AudioStreamRangedDescription *)malloc(i_param_size);
896 if (p_format_list == NULL)
899 err = AudioObjectGetPropertyData(p_streams[i], &physicalFormatsAddress, 0, NULL, &i_param_size, p_format_list);
901 msg_Err(p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err);
906 /* Check if one of the supported formats is a digital format */
907 for (int j = 0; j < i_formats; j++) {
908 if (p_format_list[j].mFormat.mFormatID == 'IAC3' ||
909 p_format_list[j].mFormat.mFormatID == 'iac3' ||
910 p_format_list[j].mFormat.mFormatID == kAudioFormat60958AC3 ||
911 p_format_list[j].mFormat.mFormatID == kAudioFormatAC3) {
918 /* if this stream supports a digital (cac3) format, then go set it. */
919 int i_requested_rate_format = -1;
920 int i_current_rate_format = -1;
921 int i_backup_rate_format = -1;
923 p_sys->i_stream_id = p_streams[i];
924 p_sys->i_stream_index = i;
926 if (!p_sys->b_revert) {
927 AudioObjectPropertyAddress currentPhysicalFormatAddress = { kAudioStreamPropertyPhysicalFormat, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
928 /* Retrieve the original format of this stream first if not done so already */
929 i_param_size = sizeof(p_sys->sfmt_revert);
930 err = AudioObjectGetPropertyData(p_sys->i_stream_id, ¤tPhysicalFormatAddress, 0, NULL, &i_param_size, &p_sys->sfmt_revert);
932 msg_Err(p_aout, "could not retrieve the original streamformat [%4.4s]", (char *)&err);
935 p_sys->b_revert = true;
938 for (int j = 0; j < i_formats; j++) {
939 if (p_format_list[j].mFormat.mFormatID == 'IAC3' ||
940 p_format_list[j].mFormat.mFormatID == 'iac3' ||
941 p_format_list[j].mFormat.mFormatID == kAudioFormat60958AC3 ||
942 p_format_list[j].mFormat.mFormatID == kAudioFormatAC3) {
943 if (p_format_list[j].mFormat.mSampleRate == fmt->i_rate) {
944 i_requested_rate_format = j;
946 } else if (p_format_list[j].mFormat.mSampleRate == p_sys->sfmt_revert.mSampleRate)
947 i_current_rate_format = j;
949 if (i_backup_rate_format < 0 || p_format_list[j].mFormat.mSampleRate > p_format_list[i_backup_rate_format].mFormat.mSampleRate)
950 i_backup_rate_format = j;
956 if (i_requested_rate_format >= 0) /* We prefer to output at the samplerate of the original audio */
957 p_sys->stream_format = p_format_list[i_requested_rate_format].mFormat;
958 else if (i_current_rate_format >= 0) /* If not possible, we will try to use the current samplerate of the device */
959 p_sys->stream_format = p_format_list[i_current_rate_format].mFormat;
961 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) */
967 msg_Dbg(p_aout, STREAM_FORMAT_MSG("original stream format: ", p_sys->sfmt_revert));
969 if (!AudioStreamChangeFormat(p_aout, p_sys->i_stream_id, p_sys->stream_format)) {
970 msg_Err(p_aout, "failed to change stream format for SPDIF output");
974 /* Set the format flags */
975 if (p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian)
976 fmt->i_format = VLC_CODEC_SPDIFB;
978 fmt->i_format = VLC_CODEC_SPDIFL;
979 fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
980 fmt->i_frame_length = A52_FRAME_NB;
981 fmt->i_rate = (unsigned int)p_sys->stream_format.mSampleRate;
982 p_sys->i_rate = fmt->i_rate;
983 aout_FormatPrepare(fmt);
985 /* Add IOProc callback */
986 err = AudioDeviceCreateIOProcID(p_sys->i_selected_dev,
987 (AudioDeviceIOProc)RenderCallbackSPDIF,
991 msg_Err(p_aout, "Failed to create Process ID [%4.4s]", (char *)&err);
995 /* Check for the difference between the Device clock and mdate */
996 p_sys->clock_diff = - (mtime_t)
997 AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()) / 1000;
998 p_sys->clock_diff += mdate();
1001 err = AudioDeviceStart(p_sys->i_selected_dev, p_sys->i_procID);
1003 msg_Err(p_aout, "Failed to start audio device [%4.4s]", (char *)&err);
1005 err = AudioDeviceDestroyIOProcID(p_sys->i_selected_dev, p_sys->i_procID);
1007 msg_Err(p_aout, "Failed to destroy process ID [%4.4s]", (char *)&err);
1012 /* setup circular buffer */
1013 TPCircularBufferInit(&p_sys->circular_buffer, 200 * AOUT_SPDIF_SIZE);
1018 static void Stop(audio_output_t *p_aout)
1020 struct aout_sys_t *p_sys = p_aout->sys;
1021 OSStatus err = noErr;
1022 UInt32 i_param_size = 0;
1024 if (p_sys->au_unit) {
1025 verify_noerr(AudioOutputUnitStop(p_sys->au_unit));
1026 verify_noerr(AudioUnitUninitialize(p_sys->au_unit));
1027 verify_noerr(AudioComponentInstanceDispose(p_sys->au_unit));
1030 vlc_mutex_lock(&p_sys->var_lock);
1031 if (p_sys->b_digital) {
1033 err = AudioDeviceStop(p_sys->i_selected_dev,
1036 msg_Err(p_aout, "Failed to stop audio device [%4.4s]", (char *)&err);
1038 /* Remove IOProc callback */
1039 err = AudioDeviceDestroyIOProcID(p_sys->i_selected_dev,
1042 msg_Err(p_aout, "Failed to destroy Process ID [%4.4s]", (char *)&err);
1044 if (p_sys->b_revert)
1045 AudioStreamChangeFormat(p_aout, p_sys->i_stream_id, p_sys->sfmt_revert);
1047 if (p_sys->b_changed_mixing && p_sys->sfmt_revert.mFormatID != kAudioFormat60958AC3) {
1049 Boolean b_writeable = false;
1050 /* Revert mixable to true if we are allowed to */
1051 AudioObjectPropertyAddress audioDeviceSupportsMixingAddress = { kAudioDevicePropertySupportsMixing , kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
1052 err = AudioObjectIsPropertySettable(p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, &b_writeable);
1053 err = AudioObjectGetPropertyData(p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, 0, NULL, &i_param_size, &b_mix);
1055 if (err == noErr && b_writeable) {
1056 msg_Dbg(p_aout, "mixable is: %d", b_mix);
1058 err = AudioObjectSetPropertyData(p_sys->i_selected_dev, &audioDeviceSupportsMixingAddress, 0, NULL, i_param_size, &b_mix);
1062 msg_Err(p_aout, "failed to re-set mixmode [%4.4s]", (char *)&err);
1066 if (p_sys->i_hog_pid == getpid()) {
1067 p_sys->i_hog_pid = -1;
1068 i_param_size = sizeof(p_sys->i_hog_pid);
1069 AudioObjectPropertyAddress audioDeviceHogModeAddress = { kAudioDevicePropertyHogMode,
1070 kAudioDevicePropertyScopeOutput,
1071 kAudioObjectPropertyElementMaster };
1072 err = AudioObjectSetPropertyData(p_sys->i_selected_dev, &audioDeviceHogModeAddress, 0, NULL, i_param_size, &p_sys->i_hog_pid);
1074 msg_Err(p_aout, "Failed to release hogmode [%4.4s]", (char *)&err);
1077 /* remove audio device alive callback */
1078 AudioObjectPropertyAddress deviceAliveAddress = { kAudioDevicePropertyDeviceIsAlive, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
1079 err = AudioObjectRemovePropertyListener(p_sys->i_selected_dev, &deviceAliveAddress, DeviceAliveListener, (void *)p_aout);
1081 /* Be tolerant, only give a warning here */
1082 msg_Warn(p_aout, "failed to remove audio device life checker [%4.4s]", (char *)&err);
1085 vlc_mutex_unlock(&p_sys->var_lock);
1087 p_sys->i_bytes_per_sample = 0;
1088 p_sys->b_digital = false;
1090 /* clean-up circular buffer */
1091 TPCircularBufferCleanup(&p_sys->circular_buffer);
1095 #pragma mark core interaction
1097 static void ReportDevice(audio_output_t *p_aout, UInt32 i_id, char *name)
1100 sprintf(deviceid, "%i", i_id);
1102 aout_HotplugReport(p_aout, deviceid, name);
1105 static void RebuildDeviceList(audio_output_t * p_aout)
1107 OSStatus err = noErr;
1108 UInt32 propertySize = 0;
1109 AudioObjectID *deviceIDs;
1110 UInt32 numberOfDevices;
1111 CFMutableArrayRef currentListOfDevices;
1113 struct aout_sys_t *p_sys = p_aout->sys;
1115 ReportDevice(p_aout, 0, _("System Sound Output Device"));
1117 /* setup local array */
1118 currentListOfDevices = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
1120 /* Get number of devices */
1121 AudioObjectPropertyAddress audioDevicesAddress = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
1122 err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &audioDevicesAddress, 0, NULL, &propertySize);
1124 msg_Err(p_aout, "Could not get number of devices: [%4.4s]", (char *)&err);
1128 numberOfDevices = propertySize / sizeof(AudioDeviceID);
1130 if (numberOfDevices < 1) {
1131 msg_Err(p_aout, "No audio output devices found.");
1134 msg_Dbg(p_aout, "found %i audio device(s)", numberOfDevices);
1136 /* Allocate DeviceID array */
1137 deviceIDs = (AudioDeviceID *)calloc(numberOfDevices, sizeof(AudioDeviceID));
1138 if (deviceIDs == NULL)
1141 /* Populate DeviceID array */
1142 err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &audioDevicesAddress, 0, NULL, &propertySize, deviceIDs);
1144 msg_Err(p_aout, "could not get the device IDs [%4.4s]", (char *)&err);
1148 AudioObjectPropertyAddress deviceNameAddress = { kAudioObjectPropertyName, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
1150 for (unsigned int i = 0; i < numberOfDevices; i++) {
1151 CFStringRef device_name_ref;
1154 bool b_digital = false;
1155 UInt32 i_id = deviceIDs[i];
1157 /* Retrieve the length of the device name */
1158 err = AudioObjectGetPropertyDataSize(deviceIDs[i], &deviceNameAddress, 0, NULL, &propertySize);
1160 msg_Dbg(p_aout, "failed to get name size for device %i", deviceIDs[i]);
1164 /* Retrieve the name of the device */
1165 err = AudioObjectGetPropertyData(deviceIDs[i], &deviceNameAddress, 0, NULL, &propertySize, &device_name_ref);
1167 msg_Dbg(p_aout, "failed to get name for device %i", deviceIDs[i]);
1170 length = CFStringGetLength(device_name_ref);
1172 psz_name = (char *)malloc(length);
1173 CFStringGetCString(device_name_ref, psz_name, length, kCFStringEncodingUTF8);
1175 msg_Dbg(p_aout, "DevID: %i DevName: %s", deviceIDs[i], psz_name);
1177 if (!AudioDeviceHasOutput(deviceIDs[i])) {
1178 msg_Dbg(p_aout, "this '%s' is INPUT only. skipping...", psz_name);
1183 ReportDevice(p_aout, i_id, psz_name);
1184 CFArrayAppendValue(currentListOfDevices, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i_id));
1186 if (AudioDeviceSupportsDigital(p_aout, deviceIDs[i])) {
1188 msg_Dbg(p_aout, "'%s' supports digital output", psz_name);
1189 char *psz_encoded_name = nil;
1190 asprintf(&psz_encoded_name, _("%s (Encoded Output)"), psz_name);
1191 i_id = i_id | AOUT_VAR_SPDIF_FLAG;
1192 ReportDevice(p_aout, i_id, psz_encoded_name);
1193 CFArrayAppendValue(currentListOfDevices, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i_id));
1194 free(psz_encoded_name);
1197 // TODO: only register once for each device
1198 ManageAudioStreamsCallback(p_aout, deviceIDs[i], true);
1200 CFRelease(device_name_ref);
1204 vlc_mutex_lock(&p_sys->var_lock);
1206 if (p_sys->device_list)
1207 count = CFArrayGetCount(p_sys->device_list);
1210 CFNumberRef cfn_device_id;
1211 int i_device_id = 0;
1212 for (CFIndex x = 0; x < count; x++) {
1213 if (!CFArrayContainsValue(currentListOfDevices, CFRangeMake(0, count), CFArrayGetValueAtIndex(p_sys->device_list, x))) {
1214 cfn_device_id = CFArrayGetValueAtIndex(p_sys->device_list, x);
1216 if (cfn_device_id) {
1217 CFNumberGetValue(cfn_device_id, kCFNumberSInt32Type, &i_device_id);
1218 ReportDevice(p_aout, i_device_id, NULL);
1223 CFRelease(p_sys->device_list);
1224 p_sys->device_list = CFArrayCreateCopy(kCFAllocatorDefault, currentListOfDevices);
1225 CFRelease(currentListOfDevices);
1226 vlc_mutex_unlock(&p_sys->var_lock);
1231 static int SwitchAudioDevice(audio_output_t *p_aout, const char *name)
1233 struct aout_sys_t *p_sys = p_aout->sys;
1236 p_sys->i_new_selected_dev = atoi(name);
1238 p_sys->i_new_selected_dev = 0;
1240 bool b_supports_digital = (p_sys->i_new_selected_dev & AOUT_VAR_SPDIF_FLAG);
1241 if (b_supports_digital)
1242 p_sys->b_selected_dev_is_digital = true;
1244 p_sys->b_selected_dev_is_digital = false;
1246 p_sys->i_new_selected_dev = p_sys->i_new_selected_dev & ~AOUT_VAR_SPDIF_FLAG;
1248 aout_DeviceReport(p_aout, name);
1249 aout_RestartRequest(p_aout, AOUT_RESTART_OUTPUT);
1254 static int VolumeSet(audio_output_t * p_aout, float volume)
1256 struct aout_sys_t *p_sys = p_aout->sys;
1259 if(p_sys->b_digital)
1260 return VLC_EGENERIC;
1262 p_sys->f_volume = volume;
1263 aout_VolumeReport(p_aout, volume);
1265 /* Set volume for output unit */
1266 ostatus = AudioUnitSetParameter(p_sys->au_unit,
1267 kHALOutputParam_Volume,
1268 kAudioUnitScope_Global,
1270 volume * volume * volume,
1273 if (var_InheritBool(p_aout, "volume-save"))
1274 config_PutInt(p_aout, "auhal-volume", lroundf(volume * AOUT_VOLUME_DEFAULT));
1279 static int MuteSet(audio_output_t * p_aout, bool mute)
1281 struct aout_sys_t *p_sys = p_aout->sys;
1284 if(p_sys->b_digital)
1285 return VLC_EGENERIC;
1287 p_sys->b_mute = mute;
1288 aout_MuteReport(p_aout, mute);
1292 volume = p_sys->f_volume;
1294 ostatus = AudioUnitSetParameter(p_sys->au_unit,
1295 kHALOutputParam_Volume,
1296 kAudioUnitScope_Global,
1298 volume * volume * volume,
1305 #pragma mark actual playback
1307 static void Play(audio_output_t * p_aout, block_t * p_block)
1309 struct aout_sys_t *p_sys = p_aout->sys;
1311 if (p_block->i_nb_samples > 0) {
1312 if (!p_sys->b_got_first_sample) {
1314 verify_noerr(AudioOutputUnitStart(p_sys->au_unit));
1315 p_sys->b_got_first_sample = true;
1318 /* Do the channel reordering */
1319 if (p_sys->chans_to_reorder && !p_sys->b_digital) {
1320 aout_ChannelReorder(p_block->p_buffer,
1322 p_sys->chans_to_reorder,
1327 /* move data to buffer */
1328 if (unlikely(!TPCircularBufferProduceBytes(&p_sys->circular_buffer, p_block->p_buffer, p_block->i_buffer)))
1329 msg_Warn(p_aout, "dropped buffer");
1331 if (!p_sys->i_bytes_per_sample)
1332 p_sys->i_bytes_per_sample = p_block->i_buffer / p_block->i_nb_samples;
1335 block_Release(p_block);
1338 static void Pause(audio_output_t *p_aout, bool pause, mtime_t date)
1340 struct aout_sys_t * p_sys = p_aout->sys;
1343 if (p_aout->sys->b_digital) {
1345 AudioDeviceStop(p_sys->i_selected_dev, p_sys->i_procID);
1347 AudioDeviceStart(p_sys->i_selected_dev, p_sys->i_procID);
1350 AudioOutputUnitStop(p_sys->au_unit);
1352 AudioOutputUnitStart(p_sys->au_unit);
1356 static void Flush(audio_output_t *p_aout, bool wait)
1358 struct aout_sys_t *p_sys = p_aout->sys;
1361 int32_t availableBytes;
1362 vlc_mutex_lock(&p_sys->lock);
1363 TPCircularBufferTail(&p_sys->circular_buffer, &availableBytes);
1364 while (availableBytes > 0) {
1365 vlc_cond_wait(&p_sys->cond, &p_sys->lock);
1366 TPCircularBufferTail(&p_sys->circular_buffer, &availableBytes);
1368 vlc_mutex_unlock(&p_sys->lock);
1371 p_sys->b_got_first_sample = false;
1373 /* flush circular buffer */
1374 AudioOutputUnitStop(p_aout->sys->au_unit);
1375 TPCircularBufferClear(&p_aout->sys->circular_buffer);
1379 static int TimeGet(audio_output_t *p_aout, mtime_t *delay)
1381 struct aout_sys_t * p_sys = p_aout->sys;
1383 if (!p_sys->i_bytes_per_sample)
1386 int32_t availableBytes;
1387 TPCircularBufferTail(&p_sys->circular_buffer, &availableBytes);
1389 *delay = (availableBytes / p_sys->i_bytes_per_sample) * CLOCK_FREQ / p_sys->i_rate;
1394 /*****************************************************************************
1395 * RenderCallbackAnalog: This function is called everytime the AudioUnit wants
1396 * us to provide some more audio data.
1397 * Don't print anything during normal playback, calling blocking function from
1398 * this callback is not allowed.
1399 *****************************************************************************/
1400 static OSStatus RenderCallbackAnalog(vlc_object_t *p_obj,
1401 AudioUnitRenderActionFlags *ioActionFlags,
1402 const AudioTimeStamp *inTimeStamp,
1404 UInt32 inNumberFrames,
1405 AudioBufferList *ioData) {
1406 VLC_UNUSED(ioActionFlags);
1407 VLC_UNUSED(inTimeStamp);
1408 VLC_UNUSED(inBusNumber);
1409 VLC_UNUSED(inNumberFrames);
1411 audio_output_t * p_aout = (audio_output_t *)p_obj;
1412 struct aout_sys_t * p_sys = p_aout->sys;
1414 int bytesRequested = ioData->mBuffers[0].mDataByteSize;
1415 Float32 *targetBuffer = (Float32*)ioData->mBuffers[0].mData;
1417 vlc_mutex_lock(&p_sys->lock);
1418 /* Pull audio from buffer */
1419 int32_t availableBytes;
1420 Float32 *buffer = TPCircularBufferTail(&p_sys->circular_buffer, &availableBytes);
1422 /* check if we have enough data */
1423 if (!availableBytes) {
1424 /* return an empty buffer so silence is played until we have data */
1425 memset(targetBuffer, 0, ioData->mBuffers[0].mDataByteSize);
1427 int32_t bytesToCopy = __MIN(bytesRequested, availableBytes);
1429 memcpy(targetBuffer, buffer, bytesToCopy);
1430 TPCircularBufferConsume(&p_sys->circular_buffer, bytesToCopy);
1431 ioData->mBuffers[0].mDataByteSize = bytesToCopy;
1434 vlc_cond_signal(&p_sys->cond);
1435 vlc_mutex_unlock(&p_sys->lock);
1441 * RenderCallbackSPDIF: callback for SPDIF audio output
1443 static OSStatus RenderCallbackSPDIF(AudioDeviceID inDevice,
1444 const AudioTimeStamp * inNow,
1445 const void * inInputData,
1446 const AudioTimeStamp * inInputTime,
1447 AudioBufferList * outOutputData,
1448 const AudioTimeStamp * inOutputTime,
1449 void * threadGlobals)
1452 VLC_UNUSED(inDevice);
1453 VLC_UNUSED(inInputData);
1454 VLC_UNUSED(inInputTime);
1455 VLC_UNUSED(inOutputTime);
1457 audio_output_t * p_aout = (audio_output_t *)threadGlobals;
1458 struct aout_sys_t * p_sys = p_aout->sys;
1460 int bytesRequested = outOutputData->mBuffers[p_sys->i_stream_index].mDataByteSize;
1461 char *targetBuffer = outOutputData->mBuffers[p_sys->i_stream_index].mData;
1463 vlc_mutex_lock(&p_sys->lock);
1464 /* Pull audio from buffer */
1465 int32_t availableBytes;
1466 char *buffer = TPCircularBufferTail(&p_sys->circular_buffer, &availableBytes);
1468 /* check if we have enough data */
1469 if (!availableBytes) {
1470 /* return an empty buffer so silence is played until we have data */
1471 memset(targetBuffer, 0, outOutputData->mBuffers[p_sys->i_stream_index].mDataByteSize);
1473 int32_t bytesToCopy = __MIN(bytesRequested, availableBytes);
1475 memcpy(targetBuffer, buffer, bytesToCopy);
1476 TPCircularBufferConsume(&p_sys->circular_buffer, bytesToCopy);
1477 outOutputData->mBuffers[p_sys->i_stream_index].mDataByteSize = bytesToCopy;
1480 vlc_cond_signal(&p_sys->cond);
1481 vlc_mutex_unlock(&p_sys->lock);
1487 #pragma mark Stream / Hardware Listeners
1490 * Callback when device list changed
1492 static OSStatus DevicesListener(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress inAddresses[], void*inClientData)
1494 VLC_UNUSED(inObjectID);
1495 VLC_UNUSED(inNumberAddresses);
1496 VLC_UNUSED(inAddresses);
1498 audio_output_t *p_aout = (audio_output_t *)inClientData;
1501 aout_sys_t *p_sys = p_aout->sys;
1503 msg_Dbg(p_aout, "audio device configuration changed, resetting cache");
1504 RebuildDeviceList(p_aout);
1506 vlc_mutex_lock(&p_sys->var_lock);
1507 if(!CFArrayContainsValue(p_sys->device_list, CFRangeMake(0, CFArrayGetCount(p_sys->device_list)),CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &p_sys->i_selected_dev)))
1508 aout_RestartRequest(p_aout, AOUT_RESTART_OUTPUT);
1509 vlc_mutex_unlock(&p_sys->var_lock);
1515 * Callback when current device is not alive anymore
1517 static OSStatus DeviceAliveListener(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress inAddresses[], void*inClientData)
1519 VLC_UNUSED(inObjectID);
1520 VLC_UNUSED(inNumberAddresses);
1521 VLC_UNUSED(inAddresses);
1523 audio_output_t *p_aout = (audio_output_t *)inClientData;
1527 msg_Warn(p_aout, "audio device died, resetting aout");
1528 aout_RestartRequest(p_aout, AOUT_RESTART_OUTPUT);
1534 * Callback when streams of any audio device changed (e.g. SPDIF gets (un)available)
1536 static OSStatus StreamsChangedListener(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress inAddresses[], void*inClientData)
1538 OSStatus err = noErr;
1539 UInt32 i_param_size = 0;
1540 AudioStreamID *p_streams = NULL;
1543 VLC_UNUSED(inNumberAddresses);
1544 VLC_UNUSED(inAddresses);
1546 audio_output_t *p_aout = (audio_output_t *)inClientData;
1550 aout_sys_t *p_sys = p_aout->sys;
1552 msg_Dbg(p_aout, "available physical formats for audio device changed");
1553 RebuildDeviceList(p_aout);
1556 * check if changed stream id belongs to current device
1558 vlc_mutex_lock(&p_sys->var_lock);
1559 AudioObjectPropertyAddress streamsAddress = { kAudioDevicePropertyStreams, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
1560 err = AudioObjectGetPropertyDataSize(p_sys->i_selected_dev, &streamsAddress, 0, NULL, &i_param_size);
1562 msg_Err(p_aout, "could not get number of streams [%4.4s]", (char *)&err);
1563 return VLC_EGENERIC;
1566 i_streams = i_param_size / sizeof(AudioStreamID);
1567 p_streams = (AudioStreamID *)malloc(i_param_size);
1568 if (p_streams == NULL)
1571 err = AudioObjectGetPropertyData(p_sys->i_selected_dev, &streamsAddress, 0, NULL, &i_param_size, p_streams);
1573 msg_Err(p_aout, "could not get list of streams [%4.4s]", (char *)&err);
1574 return VLC_EGENERIC;
1576 vlc_mutex_unlock(&p_sys->var_lock);
1578 for (int i = 0; i < i_streams; i++) {
1579 if (p_streams[i] == inObjectID) {
1580 msg_Dbg(p_aout, "Restart aout as this affects current device");
1581 aout_RestartRequest(p_aout, AOUT_RESTART_OUTPUT);
1591 * StreamListener: check whether the device's physical format changes on-the-fly (unlikely)
1593 static OSStatus StreamListener(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress inAddresses[], void*inClientData)
1595 OSStatus err = noErr;
1596 struct { vlc_mutex_t lock; vlc_cond_t cond; } * w = inClientData;
1598 VLC_UNUSED(inObjectID);
1600 for (unsigned int i = 0; i < inNumberAddresses; i++) {
1601 if (inAddresses[i].mSelector == kAudioStreamPropertyPhysicalFormat) {
1602 vlc_mutex_lock(&w->lock);
1603 vlc_cond_signal(&w->cond);
1604 vlc_mutex_unlock(&w->lock);
1612 #pragma mark helpers
1614 static int ManageAudioStreamsCallback(audio_output_t *p_aout, AudioDeviceID i_dev_id, bool b_register)
1616 OSStatus err = noErr;
1617 UInt32 i_param_size = 0;
1618 AudioStreamID *p_streams = NULL;
1621 /* Retrieve all the output streams */
1622 AudioObjectPropertyAddress streamsAddress = { kAudioDevicePropertyStreams, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
1623 err = AudioObjectGetPropertyDataSize(i_dev_id, &streamsAddress, 0, NULL, &i_param_size);
1625 msg_Err(p_aout, "could not get number of streams for device id %i [%4.4s]", i_dev_id, (char *)&err);
1626 return VLC_EGENERIC;
1629 i_streams = i_param_size / sizeof(AudioStreamID);
1630 p_streams = (AudioStreamID *)malloc(i_param_size);
1631 if (p_streams == NULL)
1634 err = AudioObjectGetPropertyData(i_dev_id, &streamsAddress, 0, NULL, &i_param_size, p_streams);
1636 msg_Err(p_aout, "could not get list of streams [%4.4s]", (char *)&err);
1637 return VLC_EGENERIC;
1640 for (int i = 0; i < i_streams; i++) {
1641 /* get notified when physical formats change */
1642 AudioObjectPropertyAddress physicalFormatsAddress = { kAudioStreamPropertyAvailablePhysicalFormats, kAudioObjectPropertyScopeGlobal, 0 };
1645 err = AudioObjectAddPropertyListener(p_streams[i], &physicalFormatsAddress, StreamsChangedListener, (void *)p_aout);
1647 // nope just means that we already have a callback
1648 if (err == kAudioHardwareIllegalOperationError) {
1649 msg_Warn(p_aout, "could not set audio stream formats property callback on stream id %i, callback already set? [%4.4s]", p_streams[i],
1652 msg_Err(p_aout, "could not set audio stream formats property callback on stream id %i [%4.4s]", p_streams[i],
1657 } else { /* unregister callback */
1658 err = AudioObjectRemovePropertyListener(p_streams[i], &physicalFormatsAddress, StreamsChangedListener, (void *)p_aout);
1660 msg_Err(p_aout, "failed to remove audio device property streams callback [%4.4s]", (char *)&err);
1670 * AudioDeviceHasOutput: Checks if the device is actually an output device
1672 static int AudioDeviceHasOutput(AudioDeviceID i_dev_id)
1674 UInt32 dataSize = 0;
1677 AudioObjectPropertyAddress streamsAddress = { kAudioDevicePropertyStreams, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
1678 status = AudioObjectGetPropertyDataSize(i_dev_id, &streamsAddress, 0, NULL, &dataSize);
1680 if (dataSize == 0 || status != noErr)
1687 * AudioDeviceSupportsDigital: Checks if device supports raw bitstreams
1689 static int AudioDeviceSupportsDigital(audio_output_t *p_aout, AudioDeviceID i_dev_id)
1691 OSStatus err = noErr;
1692 UInt32 i_param_size = 0;
1693 AudioStreamID *p_streams = NULL;
1695 bool b_return = false;
1697 /* Retrieve all the output streams */
1698 AudioObjectPropertyAddress streamsAddress = { kAudioDevicePropertyStreams, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
1699 err = AudioObjectGetPropertyDataSize(i_dev_id, &streamsAddress, 0, NULL, &i_param_size);
1701 msg_Err(p_aout, "could not get number of streams [%4.4s] (%i)", (char *)&err, (int32_t)err);
1705 i_streams = i_param_size / sizeof(AudioStreamID);
1706 p_streams = (AudioStreamID *)malloc(i_param_size);
1707 if (p_streams == NULL)
1710 err = AudioObjectGetPropertyData(i_dev_id, &streamsAddress, 0, NULL, &i_param_size, p_streams);
1712 msg_Err(p_aout, "could not get list of streams [%4.4s]", (char *)&err);
1716 for (int i = 0; i < i_streams; i++) {
1717 if (AudioStreamSupportsDigital(p_aout, p_streams[i]))
1726 * AudioStreamSupportsDigital: Checks if audio stream is compatible with raw bitstreams
1728 static int AudioStreamSupportsDigital(audio_output_t *p_aout, AudioStreamID i_stream_id)
1730 OSStatus err = noErr;
1731 UInt32 i_param_size = 0;
1732 AudioStreamRangedDescription *p_format_list = NULL;
1734 bool b_return = false;
1736 /* Retrieve all the stream formats supported by each output stream */
1737 AudioObjectPropertyAddress physicalFormatsAddress = { kAudioStreamPropertyAvailablePhysicalFormats, kAudioObjectPropertyScopeGlobal, 0 };
1738 err = AudioObjectGetPropertyDataSize(i_stream_id, &physicalFormatsAddress, 0, NULL, &i_param_size);
1740 msg_Err(p_aout, "could not get number of streamformats [%4.4s] (%i)", (char *)&err, (int32_t)err);
1744 i_formats = i_param_size / sizeof(AudioStreamRangedDescription);
1745 msg_Dbg(p_aout, "found %i stream formats for stream id %i", i_formats, i_stream_id);
1747 p_format_list = (AudioStreamRangedDescription *)malloc(i_param_size);
1748 if (p_format_list == NULL)
1751 err = AudioObjectGetPropertyData(i_stream_id, &physicalFormatsAddress, 0, NULL, &i_param_size, p_format_list);
1753 msg_Err(p_aout, "could not get the list of streamformats [%4.4s]", (char *)&err);
1754 free(p_format_list);
1755 p_format_list = NULL;
1759 for (int i = 0; i < i_formats; i++) {
1761 msg_Dbg(p_aout, STREAM_FORMAT_MSG("supported format: ", p_format_list[i].mFormat));
1764 if (p_format_list[i].mFormat.mFormatID == 'IAC3' ||
1765 p_format_list[i].mFormat.mFormatID == 'iac3' ||
1766 p_format_list[i].mFormat.mFormatID == kAudioFormat60958AC3 ||
1767 p_format_list[i].mFormat.mFormatID == kAudioFormatAC3)
1771 free(p_format_list);
1776 * AudioStreamChangeFormat: switch stream format based on the provided description
1778 static int AudioStreamChangeFormat(audio_output_t *p_aout, AudioStreamID i_stream_id, AudioStreamBasicDescription change_format)
1780 OSStatus err = noErr;
1781 UInt32 i_param_size = 0;
1783 AudioObjectPropertyAddress physicalFormatAddress = { kAudioStreamPropertyPhysicalFormat, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
1785 struct { vlc_mutex_t lock; vlc_cond_t cond; } w;
1787 msg_Dbg(p_aout, STREAM_FORMAT_MSG("setting stream format: ", change_format));
1789 /* Condition because SetProperty is asynchronious */
1790 vlc_cond_init(&w.cond);
1791 vlc_mutex_init(&w.lock);
1792 vlc_mutex_lock(&w.lock);
1794 /* Install the callback */
1795 err = AudioObjectAddPropertyListener(i_stream_id, &physicalFormatAddress, StreamListener, (void *)&w);
1797 msg_Err(p_aout, "AudioObjectAddPropertyListener for kAudioStreamPropertyPhysicalFormat failed [%4.4s]", (char *)&err);
1801 /* change the format */
1802 err = AudioObjectSetPropertyData(i_stream_id, &physicalFormatAddress, 0, NULL, sizeof(AudioStreamBasicDescription),
1805 msg_Err(p_aout, "could not set the stream format [%4.4s]", (char *)&err);
1809 /* The AudioStreamSetProperty is not only asynchronious (requiring the locks)
1810 * it is also not atomic in its behaviour.
1811 * Therefore we check 5 times before we really give up.
1812 * FIXME: failing isn't actually implemented yet. */
1813 for (int i = 0; i < 5; i++) {
1814 AudioStreamBasicDescription actual_format;
1815 mtime_t timeout = mdate() + 500000;
1817 if (vlc_cond_timedwait(&w.cond, &w.lock, timeout))
1818 msg_Dbg(p_aout, "reached timeout");
1820 i_param_size = sizeof(AudioStreamBasicDescription);
1821 err = AudioObjectGetPropertyData(i_stream_id, &physicalFormatAddress, 0, NULL, &i_param_size, &actual_format);
1823 msg_Dbg(p_aout, STREAM_FORMAT_MSG("actual format in use: ", actual_format));
1824 if (actual_format.mSampleRate == change_format.mSampleRate &&
1825 actual_format.mFormatID == change_format.mFormatID &&
1826 actual_format.mFramesPerPacket == change_format.mFramesPerPacket) {
1827 /* The right format is now active */
1830 /* We need to check again */
1833 /* Removing the property listener */
1834 err = AudioObjectRemovePropertyListener(i_stream_id, &physicalFormatAddress, StreamListener, (void *)&w);
1836 msg_Err(p_aout, "AudioStreamRemovePropertyListener failed [%4.4s]", (char *)&err);
1840 /* Destroy the lock and condition */
1841 vlc_mutex_unlock(&w.lock);
1842 vlc_mutex_destroy(&w.lock);
1843 vlc_cond_destroy(&w.cond);