1 /*****************************************************************************
2 * auhal.c: AUHAL and Coreaudio output plugin
3 *****************************************************************************
4 * Copyright (C) 2005 - 2017 VLC authors and VideoLAN
6 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
7 * Felix Paul Kühne <fkuehne at videolan dot org>
8 * David Fuhrmann <david dot fuhrmann at googlemail dot com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser 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 *****************************************************************************/
27 #import "coreaudio_common.h"
29 #import <vlc_plugin.h>
30 #import <vlc_dialog.h> // vlc_dialog_display_error
32 #import <CoreAudio/CoreAudio.h> // AudioDeviceID
33 #import <CoreServices/CoreServices.h>
36 #pragma mark local prototypes & module descriptor
39 #define AOUT_VOLUME_DEFAULT 256
40 #define AOUT_VOLUME_MAX 512
42 #define VOLUME_TEXT N_("Audio volume")
43 #define VOLUME_LONGTEXT VOLUME_TEXT
45 #define DEVICE_TEXT N_("Last audio device")
46 #define DEVICE_LONGTEXT DEVICE_TEXT
48 static int Open (vlc_object_t
*);
49 static void Close (vlc_object_t
*);
52 set_shortname("auhal")
53 set_description(N_("HAL AudioUnit output"))
54 set_capability("audio output", 101)
55 set_category(CAT_AUDIO
)
56 set_subcategory(SUBCAT_AUDIO_AOUT
)
57 set_callbacks(Open
, Close
)
58 add_integer("auhal-volume", AOUT_VOLUME_DEFAULT
,
59 VOLUME_TEXT
, VOLUME_LONGTEXT
, true)
60 change_integer_range(0, AOUT_VOLUME_MAX
)
61 add_string("auhal-audio-device", "", DEVICE_TEXT
, DEVICE_LONGTEXT
, true)
62 add_obsolete_integer("macosx-audio-device") /* since 2.1.0 */
66 #pragma mark private declarations
68 #define AOUT_VAR_SPDIF_FLAG 0xf00000
70 /*****************************************************************************
71 * aout_sys_t: private audio output method descriptor
72 *****************************************************************************
73 * This structure is part of the audio output thread descriptor.
74 * It describes the CoreAudio specific properties of an output thread.
75 *****************************************************************************/
78 struct aout_sys_common c
;
80 /* DeviceID of the selected device */
81 AudioObjectID i_selected_dev
;
82 /* DeviceID of device which will be selected on start */
83 AudioObjectID i_new_selected_dev
;
84 /* true if the user selected the default audio device (id 0) */
85 bool b_selected_dev_is_default
;
87 /* DeviceID of current device */
88 AudioDeviceIOProcID i_procID
;
89 /* Are we running in digital mode? */
95 /* CoreAudio SPDIF mode specific */
96 /* Keep the pid of our hog status */
98 /* The StreamID that has a cac3 streamformat */
99 AudioStreamID i_stream_id
;
100 /* The index of i_stream_id in an AudioBufferList */
102 /* The original format of the stream */
103 AudioStreamBasicDescription sfmt_revert
;
104 /* Whether we need to revert the stream format */
106 /* Whether we need to set the mixing mode back */
107 bool b_changed_mixing
;
109 CFArrayRef device_list
;
110 /* protects access to device_list */
111 vlc_mutex_t device_list_lock
;
113 /* Synchronizes access to i_selected_dev. This is only needed between VLCs
114 * audio thread and the core audio callback thread. The value is only
115 * changed in Start, further access to this variable within the audio
116 * thread (start, stop, close) needs no protection. */
117 vlc_mutex_t selected_device_lock
;
122 bool b_ignore_streams_changed_callback
;
129 AoGetProperty(audio_output_t
*p_aout
, AudioObjectID id
,
130 const AudioObjectPropertyAddress
*p_address
, size_t i_elm_size
,
131 ssize_t i_nb_expected_elms
, size_t *p_nb_elms
, void **pp_out_data
,
132 void *p_allocated_out_data
)
134 assert(i_elm_size
> 0);
138 OSStatus err
= AudioObjectGetPropertyDataSize(id
, p_address
, 0, NULL
,
142 msg_Err(p_aout
, "AudioObjectGetPropertyDataSize failed, device id: %i, "
143 "prop: [%4.4s], OSStatus: %d", id
, (const char *) &p_address
[0],
148 size_t i_nb_elms
= i_out_size
/ i_elm_size
;
149 if (p_nb_elms
!= NULL
)
150 *p_nb_elms
= i_nb_elms
;
151 /* Check if we get the expected number of elements */
152 if (i_nb_expected_elms
!= -1 && (size_t)i_nb_expected_elms
!= i_nb_elms
)
154 msg_Err(p_aout
, "AoGetProperty error: expected elements don't match");
158 if (pp_out_data
== NULL
&& p_allocated_out_data
== NULL
)
163 if (pp_out_data
!= NULL
)
168 /* Alloc data or use pre-allocated one */
170 if (pp_out_data
!= NULL
)
172 assert(p_allocated_out_data
== NULL
);
174 *pp_out_data
= malloc(i_out_size
);
175 if (*pp_out_data
== NULL
)
177 p_out_data
= *pp_out_data
;
181 assert(p_allocated_out_data
!= NULL
);
182 p_out_data
= p_allocated_out_data
;
186 err
= AudioObjectGetPropertyData(id
, p_address
, 0, NULL
, &i_out_size
,
190 msg_Err(p_aout
, "AudioObjectGetPropertyData failed, device id: %i, "
191 "prop: [%4.4s], OSStatus: %d", id
, (const char *) &p_address
[0],
194 if (pp_out_data
!= NULL
)
198 assert(p_nb_elms
== NULL
|| *p_nb_elms
== (i_out_size
/ i_elm_size
));
202 /* Get Audio Object Property data: pp_out_data will be allocated by this MACRO
203 * and need to be freed in case of success. */
204 #define AO_GETPROP(id, type, p_out_nb_elms, pp_out_data, a1, a2) \
205 AoGetProperty(p_aout, (id), \
206 &(AudioObjectPropertyAddress) {(a1), (a2), 0}, sizeof(type), \
207 -1, (p_out_nb_elms), (void **)(pp_out_data), NULL)
209 /* Get 1 Audio Object Property data: pre-allocated by the caller */
210 #define AO_GET1PROP(id, type, p_out_data, a1, a2) \
211 AoGetProperty(p_aout, (id), \
212 &(AudioObjectPropertyAddress) {(a1), (a2), 0}, sizeof(type), \
213 1, NULL, NULL, (p_out_data))
216 AoIsPropertySettable(audio_output_t
*p_aout
, AudioObjectID id
,
217 const AudioObjectPropertyAddress
*p_address
)
220 OSStatus err
= AudioObjectIsPropertySettable(id
, p_address
, &b_settable
);
223 msg_Warn(p_aout
, "AudioObjectIsPropertySettable failed, device id: %i, "
224 "prop: [%4.4s], OSStatus: %d", id
, (const char *)&p_address
[0],
231 #define AO_ISPROPSETTABLE(id, a1, a2) \
232 AoIsPropertySettable(p_aout, (id), \
233 &(AudioObjectPropertyAddress) { (a1), (a2), 0})
235 #define AO_HASPROP(id, a1, a2) \
236 AudioObjectHasProperty((id), &(AudioObjectPropertyAddress) { (a1), (a2), 0})
239 AoSetProperty(audio_output_t
*p_aout
, AudioObjectID id
,
240 const AudioObjectPropertyAddress
*p_address
, size_t i_data
,
244 AudioObjectSetPropertyData(id
, p_address
, 0, NULL
, i_data
, p_data
);
248 msg_Err(p_aout
, "AudioObjectSetPropertyData failed, device id: %i, "
249 "prop: [%4.4s], OSStatus: %d", id
, (const char *)&p_address
[0],
256 #define AO_SETPROP(id, i_data, p_data, a1, a2) \
257 AoSetProperty(p_aout, (id), \
258 &(AudioObjectPropertyAddress) { (a1), (a2), 0}, \
262 AoUpdateListener(audio_output_t
*p_aout
, bool add
, AudioObjectID id
,
263 const AudioObjectPropertyAddress
*p_address
,
264 AudioObjectPropertyListenerProc listener
, void *data
)
267 AudioObjectAddPropertyListener(id
, p_address
, listener
, data
) :
268 AudioObjectRemovePropertyListener(id
, p_address
, listener
, data
);
272 msg_Err(p_aout
, "AudioObject%sPropertyListener failed, device id %i, "
273 "prop: [%4.4s], OSStatus: %d", add
? "Add" : "Remove", id
,
274 (const char *)&p_address
[0], (int)err
);
280 #define AO_UPDATELISTENER(id, add, listener, data, a1, a2) \
281 AoUpdateListener(p_aout, add, (id), \
282 &(AudioObjectPropertyAddress) { (a1), (a2), 0}, \
286 #pragma mark Stream / Hardware Listeners
289 IsAudioFormatDigital(AudioFormatID id
)
295 case kAudioFormat60958AC3
:
296 case kAudioFormatAC3
:
297 case kAudioFormatEnhancedAC3
:
305 StreamsChangedListener(AudioObjectID
, UInt32
,
306 const AudioObjectPropertyAddress
[], void *);
309 ManageAudioStreamsCallback(audio_output_t
*p_aout
, AudioDeviceID i_dev_id
,
312 /* Retrieve all the output streams */
314 AudioStreamID
*p_streams
;
315 int ret
= AO_GETPROP(i_dev_id
, AudioStreamID
, &i_streams
, &p_streams
,
316 kAudioDevicePropertyStreams
,
317 kAudioObjectPropertyScopeOutput
);
318 if (ret
!= VLC_SUCCESS
)
321 for (size_t i
= 0; i
< i_streams
; i
++)
323 /* get notified when physical formats change */
324 AO_UPDATELISTENER(p_streams
[i
], b_register
, StreamsChangedListener
,
325 p_aout
, kAudioStreamPropertyAvailablePhysicalFormats
,
326 kAudioObjectPropertyScopeGlobal
);
334 * AudioStreamSupportsDigital: Checks if audio stream is compatible with raw
338 AudioStreamSupportsDigital(audio_output_t
*p_aout
, AudioStreamID i_stream_id
)
340 bool b_return
= false;
342 /* Retrieve all the stream formats supported by each output stream */
344 AudioStreamRangedDescription
*p_format_list
;
345 int ret
= AO_GETPROP(i_stream_id
, AudioStreamRangedDescription
, &i_formats
,
347 kAudioStreamPropertyAvailablePhysicalFormats
,
348 kAudioObjectPropertyScopeGlobal
);
349 if (ret
!= VLC_SUCCESS
)
352 for (size_t i
= 0; i
< i_formats
; i
++)
355 msg_Dbg(p_aout
, STREAM_FORMAT_MSG("supported format: ",
356 p_format_list
[i
].mFormat
));
359 if (IsAudioFormatDigital(p_format_list
[i
].mFormat
.mFormatID
))
368 * AudioDeviceSupportsDigital: Checks if device supports raw bitstreams
371 AudioDeviceSupportsDigital(audio_output_t
*p_aout
, AudioDeviceID i_dev_id
)
374 AudioStreamID
* p_streams
;
375 int ret
= AO_GETPROP(i_dev_id
, AudioStreamID
, &i_streams
, &p_streams
,
376 kAudioDevicePropertyStreams
,
377 kAudioObjectPropertyScopeOutput
);
378 if (ret
!= VLC_SUCCESS
)
381 for (size_t i
= 0; i
< i_streams
; i
++)
383 if (AudioStreamSupportsDigital(p_aout
, p_streams
[i
]))
395 ReportDevice(audio_output_t
*p_aout
, UInt32 i_id
, char *name
)
398 sprintf(deviceid
, "%i", i_id
);
400 aout_HotplugReport(p_aout
, deviceid
, name
);
404 * AudioDeviceIsAHeadphone: Checks if device is a headphone
408 AudioDeviceIsAHeadphone(audio_output_t
*p_aout
, AudioDeviceID i_dev_id
)
410 UInt32 defaultSize
= sizeof(AudioDeviceID
);
412 const AudioObjectPropertyAddress defaultAddr
= {
413 kAudioHardwarePropertyDefaultOutputDevice
,
414 kAudioObjectPropertyScopeGlobal
,
415 kAudioObjectPropertyElementMaster
418 AudioObjectGetPropertyData(kAudioObjectSystemObject
, &defaultAddr
, 0, NULL
, &defaultSize
, &i_dev_id
);
420 AudioObjectPropertyAddress property
;
421 property
.mSelector
= kAudioDevicePropertyDataSource
;
422 property
.mScope
= kAudioDevicePropertyScopeOutput
;
423 property
.mElement
= kAudioObjectPropertyElementMaster
;
426 UInt32 size
= sizeof(UInt32
);
427 AudioObjectGetPropertyData(i_dev_id
, &property
, 0, NULL
, &size
, &data
);
431 'ispk' == internal speaker
433 ' ' == Bluetooth accessory or AirPlay
436 return data
== 'hdpn';
440 * AudioDeviceHasOutput: Checks if the device is actually an output device
443 AudioDeviceHasOutput(audio_output_t
*p_aout
, AudioDeviceID i_dev_id
)
446 int ret
= AO_GETPROP(i_dev_id
, AudioStreamID
, &i_streams
, NULL
,
447 kAudioDevicePropertyStreams
,
448 kAudioObjectPropertyScopeOutput
);
449 if (ret
!= VLC_SUCCESS
|| i_streams
== 0)
456 RebuildDeviceList(audio_output_t
* p_aout
)
458 struct aout_sys_t
*p_sys
= p_aout
->sys
;
460 msg_Dbg(p_aout
, "Rebuild device list");
462 ReportDevice(p_aout
, 0, _("System Sound Output Device"));
464 /* Get number of devices */
466 AudioDeviceID
*p_devices
;
467 int ret
= AO_GETPROP(kAudioObjectSystemObject
, AudioDeviceID
, &i_devices
,
468 &p_devices
, kAudioHardwarePropertyDevices
,
469 kAudioObjectPropertyScopeGlobal
);
471 if (ret
!= VLC_SUCCESS
|| i_devices
== 0)
473 msg_Err(p_aout
, "No audio output devices found.");
476 msg_Dbg(p_aout
, "found %zu audio device(s)", i_devices
);
478 /* setup local array */
479 CFMutableArrayRef currentListOfDevices
=
480 CFArrayCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeArrayCallBacks
);
482 for (size_t i
= 0; i
< i_devices
; i
++)
484 CFStringRef device_name_ref
;
487 UInt32 i_id
= p_devices
[i
];
489 int ret
= AO_GET1PROP(i_id
, CFStringRef
, &device_name_ref
,
490 kAudioObjectPropertyName
,
491 kAudioObjectPropertyScopeGlobal
);
492 if (ret
!= VLC_SUCCESS
)
494 msg_Dbg(p_aout
, "failed to get name for device %i", i_id
);
498 length
= CFStringGetLength(device_name_ref
);
500 psz_name
= malloc(length
);
503 CFRelease(device_name_ref
);
506 CFStringGetCString(device_name_ref
, psz_name
, length
,
507 kCFStringEncodingUTF8
);
508 CFRelease(device_name_ref
);
510 msg_Dbg(p_aout
, "DevID: %i DevName: %s", i_id
, psz_name
);
512 if (!AudioDeviceHasOutput(p_aout
, i_id
))
514 msg_Dbg(p_aout
, "this '%s' is INPUT only. skipping...", psz_name
);
519 ReportDevice(p_aout
, i_id
, psz_name
);
520 CFNumberRef deviceNumber
= CFNumberCreate(kCFAllocatorDefault
,
521 kCFNumberSInt32Type
, &i_id
);
522 CFArrayAppendValue(currentListOfDevices
, deviceNumber
);
523 CFRelease(deviceNumber
);
525 if (AudioDeviceSupportsDigital(p_aout
, i_id
))
527 msg_Dbg(p_aout
, "'%s' supports digital output", psz_name
);
528 char *psz_encoded_name
= nil
;
529 asprintf(&psz_encoded_name
, _("%s (Encoded Output)"), psz_name
);
530 i_id
= i_id
| AOUT_VAR_SPDIF_FLAG
;
531 ReportDevice(p_aout
, i_id
, psz_encoded_name
);
532 deviceNumber
= CFNumberCreate(kCFAllocatorDefault
,
533 kCFNumberSInt32Type
, &i_id
);
534 CFArrayAppendValue(currentListOfDevices
, deviceNumber
);
535 CFRelease(deviceNumber
);
536 free(psz_encoded_name
);
539 // TODO: only register once for each device
540 ManageAudioStreamsCallback(p_aout
, p_devices
[i
], true);
545 vlc_mutex_lock(&p_sys
->device_list_lock
);
547 if (p_sys
->device_list
)
548 count
= CFArrayGetCount(p_sys
->device_list
);
549 CFRange newListSearchRange
=
550 CFRangeMake(0, CFArrayGetCount(currentListOfDevices
));
554 msg_Dbg(p_aout
, "Looking for removed devices");
555 CFNumberRef cfn_device_id
;
557 for (CFIndex x
= 0; x
< count
; x
++)
559 if (!CFArrayContainsValue(currentListOfDevices
, newListSearchRange
,
560 CFArrayGetValueAtIndex(p_sys
->device_list
,
563 cfn_device_id
= CFArrayGetValueAtIndex(p_sys
->device_list
, x
);
566 CFNumberGetValue(cfn_device_id
, kCFNumberSInt32Type
,
568 msg_Dbg(p_aout
, "Device ID %i is not found in new array, "
569 "deleting.", i_device_id
);
571 ReportDevice(p_aout
, i_device_id
, NULL
);
576 if (p_sys
->device_list
)
577 CFRelease(p_sys
->device_list
);
578 p_sys
->device_list
= CFArrayCreateCopy(kCFAllocatorDefault
,
579 currentListOfDevices
);
580 CFRelease(currentListOfDevices
);
581 vlc_mutex_unlock(&p_sys
->device_list_lock
);
587 * Callback when current device is not alive anymore
590 DeviceAliveListener(AudioObjectID inObjectID
, UInt32 inNumberAddresses
,
591 const AudioObjectPropertyAddress inAddresses
[],
594 VLC_UNUSED(inObjectID
);
595 VLC_UNUSED(inNumberAddresses
);
596 VLC_UNUSED(inAddresses
);
598 audio_output_t
*p_aout
= (audio_output_t
*)inClientData
;
602 msg_Warn(p_aout
, "audio device died, resetting aout");
603 aout_RestartRequest(p_aout
, AOUT_RESTART_OUTPUT
);
609 * Callback when default audio device changed
612 DefaultDeviceChangedListener(AudioObjectID inObjectID
, UInt32 inNumberAddresses
,
613 const AudioObjectPropertyAddress inAddresses
[],
616 VLC_UNUSED(inObjectID
);
617 VLC_UNUSED(inNumberAddresses
);
618 VLC_UNUSED(inAddresses
);
620 audio_output_t
*p_aout
= (audio_output_t
*)inClientData
;
624 aout_sys_t
*p_sys
= p_aout
->sys
;
626 if (!p_aout
->sys
->b_selected_dev_is_default
)
629 AudioObjectID defaultDeviceID
;
630 int ret
= AO_GET1PROP(kAudioObjectSystemObject
, AudioObjectID
,
632 kAudioHardwarePropertyDefaultOutputDevice
,
633 kAudioObjectPropertyScopeOutput
);
634 if (ret
!= VLC_SUCCESS
)
637 msg_Dbg(p_aout
, "default device changed to %i", defaultDeviceID
);
639 /* Default device is changed by the os to allow other apps to play sound
640 * while in digital mode. But this should not affect ourself. */
641 if (p_aout
->sys
->b_digital
)
643 msg_Dbg(p_aout
, "ignore, as digital mode is active");
647 vlc_mutex_lock(&p_sys
->selected_device_lock
);
648 /* Also ignore events which announce the same device id */
649 if (defaultDeviceID
!= p_aout
->sys
->i_selected_dev
)
651 msg_Dbg(p_aout
, "default device actually changed, resetting aout");
652 aout_RestartRequest(p_aout
, AOUT_RESTART_OUTPUT
);
654 vlc_mutex_unlock(&p_sys
->selected_device_lock
);
660 * Callback when physical formats for device change
663 StreamsChangedListener(AudioObjectID inObjectID
, UInt32 inNumberAddresses
,
664 const AudioObjectPropertyAddress inAddresses
[],
667 VLC_UNUSED(inNumberAddresses
);
668 VLC_UNUSED(inAddresses
);
670 audio_output_t
*p_aout
= (audio_output_t
*)inClientData
;
674 aout_sys_t
*p_sys
= p_aout
->sys
;
675 if(unlikely(p_sys
->b_ignore_streams_changed_callback
== true))
678 msg_Dbg(p_aout
, "available physical formats for audio device changed");
679 RebuildDeviceList(p_aout
);
681 vlc_mutex_lock(&p_sys
->selected_device_lock
);
682 /* In this case audio has not yet started. Below code will not work and is
683 * not needed here. */
684 if (p_sys
->i_selected_dev
== 0)
686 vlc_mutex_unlock(&p_sys
->selected_device_lock
);
691 * check if changed stream id belongs to current device
694 AudioStreamID
*p_streams
;
695 int ret
= AO_GETPROP(p_sys
->i_selected_dev
, AudioStreamID
, &i_streams
,
696 &p_streams
, kAudioDevicePropertyStreams
,
697 kAudioObjectPropertyScopeOutput
);
698 if (ret
!= VLC_SUCCESS
)
700 vlc_mutex_unlock(&p_sys
->selected_device_lock
);
703 vlc_mutex_unlock(&p_sys
->selected_device_lock
);
705 for (size_t i
= 0; i
< i_streams
; i
++)
707 if (p_streams
[i
] == inObjectID
)
709 msg_Dbg(p_aout
, "Restart aout as this affects current device");
710 aout_RestartRequest(p_aout
, AOUT_RESTART_OUTPUT
);
720 * Callback when device list changed
723 DevicesListener(AudioObjectID inObjectID
, UInt32 inNumberAddresses
,
724 const AudioObjectPropertyAddress inAddresses
[],
727 VLC_UNUSED(inObjectID
);
728 VLC_UNUSED(inNumberAddresses
);
729 VLC_UNUSED(inAddresses
);
731 audio_output_t
*p_aout
= (audio_output_t
*)inClientData
;
734 aout_sys_t
*p_sys
= p_aout
->sys
;
736 msg_Dbg(p_aout
, "audio device configuration changed, resetting cache");
737 RebuildDeviceList(p_aout
);
739 vlc_mutex_lock(&p_sys
->selected_device_lock
);
740 vlc_mutex_lock(&p_sys
->device_list_lock
);
741 CFNumberRef selectedDevice
=
742 CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
743 &p_sys
->i_selected_dev
);
744 CFRange range
= CFRangeMake(0, CFArrayGetCount(p_sys
->device_list
));
745 if (!CFArrayContainsValue(p_sys
->device_list
, range
, selectedDevice
))
746 aout_RestartRequest(p_aout
, AOUT_RESTART_OUTPUT
);
747 CFRelease(selectedDevice
);
748 vlc_mutex_unlock(&p_sys
->device_list_lock
);
749 vlc_mutex_unlock(&p_sys
->selected_device_lock
);
755 * StreamListener: check whether the device's physical format change is complete
758 StreamListener(AudioObjectID inObjectID
, UInt32 inNumberAddresses
,
759 const AudioObjectPropertyAddress inAddresses
[],
762 OSStatus err
= noErr
;
763 struct { vlc_mutex_t lock
; vlc_cond_t cond
; } * w
= inClientData
;
765 VLC_UNUSED(inObjectID
);
767 for (unsigned int i
= 0; i
< inNumberAddresses
; i
++)
769 if (inAddresses
[i
].mSelector
== kAudioStreamPropertyPhysicalFormat
)
771 int canc
= vlc_savecancel();
772 vlc_mutex_lock(&w
->lock
);
773 vlc_cond_signal(&w
->cond
);
774 vlc_mutex_unlock(&w
->lock
);
775 vlc_restorecancel(canc
);
783 * AudioStreamChangeFormat: switch stream format based on the provided
787 AudioStreamChangeFormat(audio_output_t
*p_aout
, AudioStreamID i_stream_id
,
788 AudioStreamBasicDescription change_format
)
790 int retValue
= false;
792 struct { vlc_mutex_t lock
; vlc_cond_t cond
; } w
;
794 msg_Dbg(p_aout
, STREAM_FORMAT_MSG("setting stream format: ", change_format
));
796 /* Condition because SetProperty is asynchronious */
797 vlc_cond_init(&w
.cond
);
798 vlc_mutex_init(&w
.lock
);
799 vlc_mutex_lock(&w
.lock
);
801 /* Install the callback */
802 int ret
= AO_UPDATELISTENER(i_stream_id
, true, StreamListener
, &w
,
803 kAudioStreamPropertyPhysicalFormat
,
804 kAudioObjectPropertyScopeGlobal
);
806 if (ret
!= VLC_SUCCESS
)
812 /* change the format */
813 ret
= AO_SETPROP(i_stream_id
, sizeof(AudioStreamBasicDescription
),
814 &change_format
, kAudioStreamPropertyPhysicalFormat
,
815 kAudioObjectPropertyScopeGlobal
);
816 if (ret
!= VLC_SUCCESS
)
822 /* The AudioStreamSetProperty is not only asynchronious (requiring the
823 * locks) it is also not atomic in its behaviour. Therefore we check 9
824 * times before we really give up.
826 for (int i
= 0; i
< 9; i
++)
828 /* Callback is not always invoked. So first check if format is already
832 mtime_t timeout
= mdate() + 500000;
833 if (vlc_cond_timedwait(&w
.cond
, &w
.lock
, timeout
))
834 msg_Dbg(p_aout
, "reached timeout");
837 AudioStreamBasicDescription actual_format
;
838 int ret
= AO_GET1PROP(i_stream_id
, AudioStreamBasicDescription
,
840 kAudioStreamPropertyPhysicalFormat
,
841 kAudioObjectPropertyScopeGlobal
);
843 if (ret
!= VLC_SUCCESS
)
846 msg_Dbg(p_aout
, STREAM_FORMAT_MSG("actual format in use: ",
848 if (actual_format
.mSampleRate
== change_format
.mSampleRate
&&
849 actual_format
.mFormatID
== change_format
.mFormatID
&&
850 actual_format
.mFramesPerPacket
== change_format
.mFramesPerPacket
)
852 /* The right format is now active */
857 /* We need to check again */
861 vlc_mutex_unlock(&w
.lock
);
863 /* Removing the property listener */
864 ret
= AO_UPDATELISTENER(i_stream_id
, false, StreamListener
, &w
,
865 kAudioStreamPropertyPhysicalFormat
,
866 kAudioObjectPropertyScopeGlobal
);
867 if (ret
!= VLC_SUCCESS
)
870 vlc_mutex_destroy(&w
.lock
);
871 vlc_cond_destroy(&w
.cond
);
877 #pragma mark core interaction
880 SwitchAudioDevice(audio_output_t
*p_aout
, const char *name
)
882 struct aout_sys_t
*p_sys
= p_aout
->sys
;
885 p_sys
->i_new_selected_dev
= atoi(name
);
887 p_sys
->i_new_selected_dev
= 0;
889 p_sys
->i_new_selected_dev
= p_sys
->i_new_selected_dev
& ~AOUT_VAR_SPDIF_FLAG
;
891 aout_DeviceReport(p_aout
, name
);
892 aout_RestartRequest(p_aout
, AOUT_RESTART_OUTPUT
);
898 VolumeSet(audio_output_t
* p_aout
, float volume
)
900 struct aout_sys_t
*p_sys
= p_aout
->sys
;
901 OSStatus ostatus
= 0;
903 if (p_sys
->b_digital
)
906 p_sys
->f_volume
= volume
;
907 aout_VolumeReport(p_aout
, volume
);
909 /* Set volume for output unit */
912 ostatus
= AudioUnitSetParameter(p_sys
->au_unit
,
913 kHALOutputParam_Volume
,
914 kAudioUnitScope_Global
,
916 volume
* volume
* volume
,
920 if (var_InheritBool(p_aout
, "volume-save"))
921 config_PutInt(p_aout
, "auhal-volume",
922 lroundf(volume
* AOUT_VOLUME_DEFAULT
));
928 MuteSet(audio_output_t
* p_aout
, bool mute
)
930 struct aout_sys_t
*p_sys
= p_aout
->sys
;
935 p_sys
->b_mute
= mute
;
936 aout_MuteReport(p_aout
, mute
);
940 volume
= p_sys
->f_volume
;
943 AudioUnitSetParameter(p_sys
->au_unit
, kHALOutputParam_Volume
,
944 kAudioUnitScope_Global
, 0,
945 volume
* volume
* volume
, 0);
947 return err
== noErr
? VLC_SUCCESS
: VLC_EGENERIC
;
951 #pragma mark actual playback
954 * RenderCallbackSPDIF: callback for SPDIF audio output
957 RenderCallbackSPDIF(AudioDeviceID inDevice
, const AudioTimeStamp
* inNow
,
958 const AudioBufferList
* inInputData
,
959 const AudioTimeStamp
* inInputTime
,
960 AudioBufferList
* outOutputData
,
961 const AudioTimeStamp
* inOutputTime
, void *p_data
)
964 VLC_UNUSED(inDevice
);
965 VLC_UNUSED(inInputData
);
966 VLC_UNUSED(inInputTime
);
967 VLC_UNUSED(inOutputTime
);
969 audio_output_t
* p_aout
= p_data
;
970 aout_sys_t
*p_sys
= p_aout
->sys
;
971 uint8_t *p_output
= outOutputData
->mBuffers
[p_sys
->i_stream_index
].mData
;
972 size_t i_size
= outOutputData
->mBuffers
[p_sys
->i_stream_index
].mDataByteSize
;
974 ca_Render(p_aout
, p_output
, i_size
);
980 #pragma mark initialization
983 * StartAnalog: open and setup a HAL AudioUnit to do PCM audio output
986 StartAnalog(audio_output_t
*p_aout
, audio_sample_format_t
*fmt
,
987 mtime_t i_latency_us
)
989 struct aout_sys_t
*p_sys
= p_aout
->sys
;
990 OSStatus err
= noErr
;
992 AudioChannelLayout
*layout
= NULL
;
994 if (aout_FormatNbChannels(fmt
) == 0)
997 p_sys
->au_unit
= au_NewOutputInstance(p_aout
, kAudioUnitSubType_HALOutput
);
998 if (p_sys
->au_unit
== NULL
)
1001 p_aout
->current_sink_info
.headphones
= AudioDeviceIsAHeadphone(p_aout
, p_sys
->i_selected_dev
);
1003 /* Set the device we will use for this output unit */
1004 err
= AudioUnitSetProperty(p_sys
->au_unit
,
1005 kAudioOutputUnitProperty_CurrentDevice
,
1006 kAudioUnitScope_Global
, 0,
1007 &p_sys
->i_selected_dev
, sizeof(AudioObjectID
));
1011 ca_LogErr("cannot select audio output device, PCM output failed");
1015 /* Get the channel layout of the device side of the unit (vlc -> unit ->
1017 err
= AudioUnitGetPropertyInfo(p_sys
->au_unit
,
1018 kAudioDevicePropertyPreferredChannelLayout
,
1019 kAudioUnitScope_Output
, 0, &i_param_size
,
1023 layout
= (AudioChannelLayout
*)malloc(i_param_size
);
1028 AudioUnitGetProperty(p_sys
->au_unit
,
1029 kAudioDevicePropertyPreferredChannelLayout
,
1030 kAudioUnitScope_Output
, 0, layout
,
1036 ca_LogWarn("device driver does not support "
1037 "kAudioDevicePropertyPreferredChannelLayout - using stereo");
1039 /* Do the last VLC aout setups */
1040 int ret
= au_Initialize(p_aout
, p_sys
->au_unit
, fmt
, layout
, i_latency_us
);
1041 if (ret
!= VLC_SUCCESS
)
1044 err
= AudioOutputUnitStart(p_sys
->au_unit
);
1047 ca_LogErr("AudioUnitStart failed");
1048 au_Uninitialize(p_aout
, p_sys
->au_unit
);
1052 /* Set volume for output unit */
1053 VolumeSet(p_aout
, p_sys
->f_volume
);
1054 MuteSet(p_aout
, p_sys
->b_mute
);
1059 AudioComponentInstanceDispose(p_sys
->au_unit
);
1061 return VLC_EGENERIC
;
1065 * StartSPDIF: Setup an encoded digital stream (SPDIF) output
1068 StartSPDIF(audio_output_t
* p_aout
, audio_sample_format_t
*fmt
,
1069 mtime_t i_latency_us
)
1071 struct aout_sys_t
*p_sys
= p_aout
->sys
;
1074 /* Check if device supports digital */
1075 if (!AudioDeviceSupportsDigital(p_aout
, p_sys
->i_selected_dev
))
1077 msg_Dbg(p_aout
, "Audio device supports PCM mode only");
1078 return VLC_EGENERIC
;
1081 ret
= AO_GET1PROP(p_sys
->i_selected_dev
, pid_t
, &p_sys
->i_hog_pid
,
1082 kAudioDevicePropertyHogMode
,
1083 kAudioObjectPropertyScopeOutput
);
1084 if (ret
!= VLC_SUCCESS
)
1086 /* This is not a fatal error. Some drivers simply don't support this
1088 p_sys
->i_hog_pid
= -1;
1091 if (p_sys
->i_hog_pid
!= -1 && p_sys
->i_hog_pid
!= getpid())
1093 msg_Err(p_aout
, "Selected audio device is exclusively in use by another"
1095 vlc_dialog_display_error(p_aout
, _("Audio output failed"), "%s",
1096 _("The selected audio output device is exclusively in "
1097 "use by another program."));
1098 return VLC_EGENERIC
;
1101 AudioStreamBasicDescription desired_stream_format
;
1102 memset(&desired_stream_format
, 0, sizeof(desired_stream_format
));
1104 /* Start doing the SPDIF setup proces */
1105 p_sys
->b_digital
= true;
1107 /* Hog the device */
1108 p_sys
->i_hog_pid
= getpid();
1111 * HACK: On 10.6, auhal will trigger the streams changed callback when
1112 * calling below line, directly in the same thread. This call needs to be
1113 * ignored to avoid endless restarting.
1115 p_sys
->b_ignore_streams_changed_callback
= true;
1116 ret
= AO_SETPROP(p_sys
->i_selected_dev
, sizeof(p_sys
->i_hog_pid
),
1117 &p_sys
->i_hog_pid
, kAudioDevicePropertyHogMode
,
1118 kAudioObjectPropertyScopeOutput
);
1119 p_sys
->b_ignore_streams_changed_callback
= false;
1121 if (ret
!= VLC_SUCCESS
)
1124 if (AO_HASPROP(p_sys
->i_selected_dev
, kAudioDevicePropertySupportsMixing
,
1125 kAudioObjectPropertyScopeGlobal
))
1127 /* Set mixable to false if we are allowed to */
1129 bool b_writeable
= AO_ISPROPSETTABLE(p_sys
->i_selected_dev
,
1130 kAudioDevicePropertySupportsMixing
,
1131 kAudioObjectPropertyScopeGlobal
);
1133 ret
= AO_GET1PROP(p_sys
->i_selected_dev
, UInt32
, &b_mix
,
1134 kAudioDevicePropertySupportsMixing
,
1135 kAudioObjectPropertyScopeGlobal
);
1136 if (ret
== VLC_SUCCESS
&& b_writeable
)
1139 ret
= AO_SETPROP(p_sys
->i_selected_dev
, sizeof(UInt32
), &b_mix
,
1140 kAudioDevicePropertySupportsMixing
,
1141 kAudioObjectPropertyScopeGlobal
);
1142 p_sys
->b_changed_mixing
= true;
1145 if (ret
!= VLC_SUCCESS
)
1147 msg_Err(p_aout
, "failed to set mixmode");
1152 /* Get a list of all the streams on this device */
1154 AudioStreamID
*p_streams
;
1155 ret
= AO_GETPROP(p_sys
->i_selected_dev
, AudioStreamID
, &i_streams
,
1156 &p_streams
, kAudioDevicePropertyStreams
,
1157 kAudioObjectPropertyScopeOutput
);
1158 if (ret
!= VLC_SUCCESS
)
1161 for (unsigned i
= 0; i
< i_streams
&& p_sys
->i_stream_index
< 0 ; i
++)
1163 /* Find a stream with a cac3 stream */
1164 bool b_digital
= false;
1166 /* Retrieve all the stream formats supported by each output stream */
1168 AudioStreamRangedDescription
*p_format_list
;
1169 int ret
= AO_GETPROP(p_streams
[i
], AudioStreamRangedDescription
,
1170 &i_formats
, &p_format_list
,
1171 kAudioStreamPropertyAvailablePhysicalFormats
,
1172 kAudioObjectPropertyScopeGlobal
);
1173 if (ret
!= VLC_SUCCESS
)
1176 /* Check if one of the supported formats is a digital format */
1177 for (size_t j
= 0; j
< i_formats
; j
++)
1179 if (IsAudioFormatDigital(p_format_list
[j
].mFormat
.mFormatID
))
1188 /* if this stream supports a digital (cac3) format, then go set it.
1190 int i_requested_rate_format
= -1;
1191 int i_current_rate_format
= -1;
1192 int i_backup_rate_format
= -1;
1194 if (!p_sys
->b_revert
)
1196 /* Retrieve the original format of this stream first if not
1197 * done so already */
1198 AudioStreamBasicDescription current_streamformat
;
1199 int ret
= AO_GET1PROP(p_streams
[i
], AudioStreamBasicDescription
,
1200 ¤t_streamformat
,
1201 kAudioStreamPropertyPhysicalFormat
,
1202 kAudioObjectPropertyScopeGlobal
);
1203 if (ret
!= VLC_SUCCESS
)
1207 * Only the first found format id is accepted. In case of
1208 * another id later on, we still use the already saved one.
1209 * This can happen if the user plugs in a spdif cable while a
1210 * stream is already playing. Then, auhal already misleadingly
1211 * reports an ac3 format here whereas the original format
1212 * should be still pcm.
1214 if (p_sys
->sfmt_revert
.mFormatID
> 0
1215 && p_sys
->sfmt_revert
.mFormatID
!= current_streamformat
.mFormatID
1216 && p_streams
[i
] == p_sys
->i_stream_id
)
1218 msg_Warn(p_aout
, STREAM_FORMAT_MSG("Detected current stream"
1219 " format: ", current_streamformat
));
1220 msg_Warn(p_aout
, "... there is another stream format "
1221 "already stored, the current one is ignored");
1224 p_sys
->sfmt_revert
= current_streamformat
;
1226 p_sys
->b_revert
= true;
1229 p_sys
->i_stream_id
= p_streams
[i
];
1230 p_sys
->i_stream_index
= i
;
1232 for (size_t j
= 0; j
< i_formats
; j
++)
1234 if (IsAudioFormatDigital(p_format_list
[j
].mFormat
.mFormatID
))
1236 if (p_format_list
[j
].mFormat
.mSampleRate
== fmt
->i_rate
)
1238 i_requested_rate_format
= j
;
1241 else if (p_format_list
[j
].mFormat
.mSampleRate
==
1242 p_sys
->sfmt_revert
.mSampleRate
)
1243 i_current_rate_format
= j
;
1246 if (i_backup_rate_format
< 0
1247 || p_format_list
[j
].mFormat
.mSampleRate
>
1248 p_format_list
[i_backup_rate_format
].mFormat
.mSampleRate
)
1249 i_backup_rate_format
= j
;
1255 if (i_requested_rate_format
>= 0)
1257 /* We prefer to output at the samplerate of the original audio */
1258 desired_stream_format
=
1259 p_format_list
[i_requested_rate_format
].mFormat
;
1261 else if (i_current_rate_format
>= 0)
1263 /* If not possible, we will try to use the current samplerate
1265 desired_stream_format
=
1266 p_format_list
[i_current_rate_format
].mFormat
;
1270 /* And if we have to, any digital format will be just fine
1271 * (highest rate possible) */
1272 desired_stream_format
=
1273 p_format_list
[i_backup_rate_format
].mFormat
;
1276 free(p_format_list
);
1280 msg_Dbg(p_aout
, STREAM_FORMAT_MSG("original stream format: ",
1281 p_sys
->sfmt_revert
));
1283 if (!AudioStreamChangeFormat(p_aout
, p_sys
->i_stream_id
, desired_stream_format
))
1285 msg_Err(p_aout
, "failed to change stream format for SPDIF output");
1286 return VLC_EGENERIC
;
1289 /* Set the format flags */
1290 if (desired_stream_format
.mFormatFlags
& kAudioFormatFlagIsBigEndian
)
1291 fmt
->i_format
= VLC_CODEC_SPDIFB
;
1293 fmt
->i_format
= VLC_CODEC_SPDIFL
;
1294 fmt
->i_bytes_per_frame
= AOUT_SPDIF_SIZE
;
1295 fmt
->i_frame_length
= A52_FRAME_NB
;
1296 fmt
->i_rate
= (unsigned int)desired_stream_format
.mSampleRate
;
1297 aout_FormatPrepare(fmt
);
1299 /* Add IOProc callback */
1301 AudioDeviceCreateIOProcID(p_sys
->i_selected_dev
,
1302 RenderCallbackSPDIF
,
1303 p_aout
, &p_sys
->i_procID
);
1306 ca_LogErr("Failed to create Process ID");
1307 return VLC_EGENERIC
;
1310 ret
= ca_Initialize(p_aout
, fmt
, i_latency_us
);
1311 if (ret
!= VLC_SUCCESS
)
1313 AudioDeviceDestroyIOProcID(p_sys
->i_selected_dev
, p_sys
->i_procID
);
1314 return VLC_EGENERIC
;
1318 err
= AudioDeviceStart(p_sys
->i_selected_dev
, p_sys
->i_procID
);
1321 ca_LogErr("Failed to start audio device");
1323 err
= AudioDeviceDestroyIOProcID(p_sys
->i_selected_dev
, p_sys
->i_procID
);
1325 ca_LogErr("Failed to destroy process ID");
1327 return VLC_EGENERIC
;
1330 msg_Dbg(p_aout
, "Using audio device for digital output");
1335 Stop(audio_output_t
*p_aout
)
1337 struct aout_sys_t
*p_sys
= p_aout
->sys
;
1338 OSStatus err
= noErr
;
1340 msg_Dbg(p_aout
, "Stopping the auhal module");
1344 AudioOutputUnitStop(p_sys
->au_unit
);
1345 au_Uninitialize(p_aout
, p_sys
->au_unit
);
1346 AudioComponentInstanceDispose(p_sys
->au_unit
);
1350 assert(p_sys
->b_digital
);
1353 err
= AudioDeviceStop(p_sys
->i_selected_dev
,
1356 ca_LogErr("AudioDeviceStop failed");
1358 /* Remove IOProc callback */
1359 err
= AudioDeviceDestroyIOProcID(p_sys
->i_selected_dev
,
1362 ca_LogErr("Failed to destroy Process ID");
1365 && !AudioStreamChangeFormat(p_aout
, p_sys
->i_stream_id
, p_sys
->sfmt_revert
))
1366 msg_Err(p_aout
, "failed to revert stream format in close");
1368 if (p_sys
->b_changed_mixing
1369 && p_sys
->sfmt_revert
.mFormatID
!= kAudioFormat60958AC3
)
1372 /* Revert mixable to true if we are allowed to */
1374 AO_ISPROPSETTABLE(p_sys
->i_selected_dev
,
1375 kAudioDevicePropertySupportsMixing
,
1376 kAudioObjectPropertyScopeGlobal
);
1377 int ret
= AO_GET1PROP(p_sys
->i_selected_dev
, UInt32
, &b_mix
,
1378 kAudioDevicePropertySupportsMixing
,
1379 kAudioObjectPropertyScopeOutput
);
1380 if (ret
== VLC_SUCCESS
&& b_writeable
)
1382 msg_Dbg(p_aout
, "mixable is: %d", b_mix
);
1384 ret
= AO_SETPROP(p_sys
->i_selected_dev
,
1385 sizeof(UInt32
), &b_mix
,
1386 kAudioDevicePropertySupportsMixing
,
1387 kAudioObjectPropertyScopeOutput
);
1390 if (ret
!= VLC_SUCCESS
)
1391 msg_Err(p_aout
, "failed to re-set mixmode");
1393 ca_Uninitialize(p_aout
);
1395 if (p_sys
->i_hog_pid
== getpid())
1397 p_sys
->i_hog_pid
= -1;
1400 * HACK: On 10.6, auhal will trigger the streams changed callback
1401 * when calling below line, directly in the same thread. This call
1402 * needs to be ignored to avoid endless restarting.
1404 p_sys
->b_ignore_streams_changed_callback
= true;
1405 AO_SETPROP(p_sys
->i_selected_dev
, sizeof(p_sys
->i_hog_pid
),
1406 &p_sys
->i_hog_pid
, kAudioDevicePropertyHogMode
,
1407 kAudioObjectPropertyScopeOutput
);
1408 p_sys
->b_ignore_streams_changed_callback
= false;
1411 p_sys
->b_digital
= false;
1414 /* remove audio device alive callback */
1415 AO_UPDATELISTENER(p_sys
->i_selected_dev
, false, DeviceAliveListener
, p_aout
,
1416 kAudioDevicePropertyDeviceIsAlive
,
1417 kAudioObjectPropertyScopeGlobal
);
1421 Start(audio_output_t
*p_aout
, audio_sample_format_t
*restrict fmt
)
1423 UInt32 i_param_size
= 0;
1424 struct aout_sys_t
*p_sys
= NULL
;
1426 /* Use int here, to match kAudioDevicePropertyDeviceIsAlive
1428 int b_alive
= false;
1430 if (AOUT_FMT_HDMI(fmt
))
1431 return VLC_EGENERIC
;
1433 p_sys
= p_aout
->sys
;
1434 p_sys
->b_digital
= false;
1435 p_sys
->au_unit
= NULL
;
1436 p_sys
->i_stream_index
= -1;
1437 p_sys
->b_revert
= false;
1438 p_sys
->b_changed_mixing
= false;
1440 vlc_mutex_lock(&p_sys
->selected_device_lock
);
1441 p_sys
->i_selected_dev
= p_sys
->i_new_selected_dev
;
1443 aout_FormatPrint(p_aout
, "VLC is looking for:", fmt
);
1445 msg_Dbg(p_aout
, "attempting to use device %i", p_sys
->i_selected_dev
);
1447 if (p_sys
->i_selected_dev
> 0)
1449 /* Check if device is in devices list. Only checking for
1450 * kAudioDevicePropertyDeviceIsAlive is not sufficient, as a former
1451 * airplay device might be already gone, but the device number might be
1452 * still valid. Core Audio even says that this device would be alive.
1453 * Don't ask why, its Core Audio. */
1454 CFIndex count
= CFArrayGetCount(p_sys
->device_list
);
1455 CFNumberRef deviceNumber
=
1456 CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
1457 &p_sys
->i_selected_dev
);
1458 if (CFArrayContainsValue(p_sys
->device_list
,
1459 CFRangeMake(0, count
), deviceNumber
))
1461 /* Check if the desired device is alive and usable */
1462 i_param_size
= sizeof(b_alive
);
1463 int ret
= AO_GET1PROP(p_sys
->i_selected_dev
, int, &b_alive
,
1464 kAudioDevicePropertyDeviceIsAlive
,
1465 kAudioObjectPropertyScopeGlobal
);
1466 if (ret
!= VLC_SUCCESS
)
1467 b_alive
= false; /* Be tolerant */
1470 msg_Warn(p_aout
, "selected audio device is not alive, switching"
1471 " to default device");
1476 msg_Warn(p_aout
, "device id %i not found in the current devices "
1477 "list, fallback to default device", p_sys
->i_selected_dev
);
1479 CFRelease(deviceNumber
);
1482 p_sys
->b_selected_dev_is_default
= false;
1483 if (!b_alive
|| p_sys
->i_selected_dev
== 0)
1485 p_sys
->b_selected_dev_is_default
= true;
1487 AudioObjectID defaultDeviceID
= 0;
1488 int ret
= AO_GET1PROP(kAudioObjectSystemObject
, AudioObjectID
,
1490 kAudioHardwarePropertyDefaultOutputDevice
,
1491 kAudioObjectPropertyScopeOutput
);
1492 if (ret
!= VLC_SUCCESS
)
1494 vlc_mutex_unlock(&p_sys
->selected_device_lock
);
1495 return VLC_EGENERIC
;
1498 msg_Dbg(p_aout
, "using default audio device %i", defaultDeviceID
);
1500 p_sys
->i_selected_dev
= defaultDeviceID
;
1502 vlc_mutex_unlock(&p_sys
->selected_device_lock
);
1504 /* add a callback to see if the device dies later on */
1505 AO_UPDATELISTENER(p_sys
->i_selected_dev
, true, DeviceAliveListener
, p_aout
,
1506 kAudioDevicePropertyDeviceIsAlive
,
1507 kAudioObjectPropertyScopeGlobal
);
1509 /* get device latency */
1510 UInt32 i_latency_samples
;
1511 AO_GET1PROP(p_sys
->i_selected_dev
, UInt32
, &i_latency_samples
,
1512 kAudioDevicePropertyLatency
, kAudioObjectPropertyScopeOutput
);
1513 mtime_t i_latency_us
= i_latency_samples
* CLOCK_FREQ
/ fmt
->i_rate
;
1515 /* Check for Digital mode or Analog output mode */
1516 if (AOUT_FMT_SPDIF (fmt
))
1518 if (StartSPDIF (p_aout
, fmt
, i_latency_us
) == VLC_SUCCESS
)
1520 msg_Dbg(p_aout
, "digital output successfully opened");
1526 if (StartAnalog(p_aout
, fmt
, i_latency_us
) == VLC_SUCCESS
)
1528 msg_Dbg(p_aout
, "analog output successfully opened");
1529 fmt
->channel_type
= AUDIO_CHANNEL_TYPE_BITMAP
;
1534 msg_Err(p_aout
, "opening auhal output failed");
1535 AO_UPDATELISTENER(p_sys
->i_selected_dev
, false, DeviceAliveListener
, p_aout
,
1536 kAudioDevicePropertyDeviceIsAlive
,
1537 kAudioObjectPropertyScopeGlobal
);
1538 return VLC_EGENERIC
;
1541 static void Close(vlc_object_t
*obj
)
1543 audio_output_t
*p_aout
= (audio_output_t
*)obj
;
1544 aout_sys_t
*p_sys
= p_aout
->sys
;
1546 /* remove audio devices callback */
1547 AO_UPDATELISTENER(kAudioObjectSystemObject
, false, DevicesListener
, p_aout
,
1548 kAudioHardwarePropertyDevices
,
1549 kAudioObjectPropertyScopeGlobal
);
1551 /* remove listener to be notified about changes in default audio device */
1552 AO_UPDATELISTENER(kAudioObjectSystemObject
, false,
1553 DefaultDeviceChangedListener
, p_aout
,
1554 kAudioHardwarePropertyDefaultOutputDevice
,
1555 kAudioObjectPropertyScopeGlobal
);
1558 * StreamsChangedListener can rebuild the device list and thus held the
1559 * device_list_lock. To avoid a possible deadlock, an array copy is
1560 * created here. In rare cases, this can lead to missing
1561 * StreamsChangedListener callback deregistration (TODO).
1563 vlc_mutex_lock(&p_sys
->device_list_lock
);
1564 CFArrayRef device_list_cpy
= CFArrayCreateCopy(NULL
, p_sys
->device_list
);
1565 vlc_mutex_unlock(&p_sys
->device_list_lock
);
1567 /* remove streams callbacks */
1568 CFIndex count
= CFArrayGetCount(device_list_cpy
);
1571 for (CFIndex x
= 0; x
< count
; x
++)
1573 AudioDeviceID deviceId
= 0;
1574 CFNumberRef cfn_device_id
=
1575 CFArrayGetValueAtIndex(device_list_cpy
, x
);
1579 CFNumberGetValue(cfn_device_id
, kCFNumberSInt32Type
, &deviceId
);
1580 if (!(deviceId
& AOUT_VAR_SPDIF_FLAG
))
1581 ManageAudioStreamsCallback(p_aout
, deviceId
, false);
1585 CFRelease(device_list_cpy
);
1586 CFRelease(p_sys
->device_list
);
1588 char *psz_device
= aout_DeviceGet(p_aout
);
1589 config_PutPsz(p_aout
, "auhal-audio-device", psz_device
);
1592 vlc_mutex_destroy(&p_sys
->selected_device_lock
);
1593 vlc_mutex_destroy(&p_sys
->device_list_lock
);
1599 static int Open(vlc_object_t
*obj
)
1601 audio_output_t
*p_aout
= (audio_output_t
*)obj
;
1602 aout_sys_t
*p_sys
= calloc(1, sizeof (*p_sys
));
1603 if (unlikely(p_sys
== NULL
))
1606 vlc_mutex_init(&p_sys
->device_list_lock
);
1607 vlc_mutex_init(&p_sys
->selected_device_lock
);
1608 p_sys
->b_digital
= false;
1609 p_sys
->b_ignore_streams_changed_callback
= false;
1610 p_sys
->b_selected_dev_is_default
= false;
1611 memset(&p_sys
->sfmt_revert
, 0, sizeof(p_sys
->sfmt_revert
));
1612 p_sys
->i_stream_id
= 0;
1614 p_aout
->sys
= p_sys
;
1615 p_aout
->start
= Start
;
1616 p_aout
->stop
= Stop
;
1617 p_aout
->volume_set
= VolumeSet
;
1618 p_aout
->mute_set
= MuteSet
;
1619 p_aout
->device_select
= SwitchAudioDevice
;
1620 p_sys
->device_list
= CFArrayCreate(kCFAllocatorDefault
, NULL
, 0, NULL
);
1623 * Force an own run loop for callbacks.
1625 * According to rtaudio, this is absolutely necessary since 10.6 to get
1626 * correct notifications. It might fix issues when using the module as a
1627 * library where a proper loop is not setup already.
1629 CFRunLoopRef theRunLoop
= NULL
;
1630 AO_SETPROP(kAudioObjectSystemObject
, sizeof(CFRunLoopRef
),
1631 &theRunLoop
, kAudioHardwarePropertyRunLoop
,
1632 kAudioObjectPropertyScopeGlobal
);
1634 /* Attach a listener so that we are notified of a change in the device
1636 AO_UPDATELISTENER(kAudioObjectSystemObject
, true, DevicesListener
, p_aout
,
1637 kAudioHardwarePropertyDevices
,
1638 kAudioObjectPropertyScopeGlobal
);
1640 /* Attach a listener to be notified about changes in default audio device */
1641 AO_UPDATELISTENER(kAudioObjectSystemObject
, true,
1642 DefaultDeviceChangedListener
, p_aout
,
1643 kAudioHardwarePropertyDefaultOutputDevice
,
1644 kAudioObjectPropertyScopeGlobal
);
1646 RebuildDeviceList(p_aout
);
1648 /* remember the volume */
1649 p_sys
->f_volume
= var_InheritInteger(p_aout
, "auhal-volume")
1650 / (float)AOUT_VOLUME_DEFAULT
;
1651 aout_VolumeReport(p_aout
, p_sys
->f_volume
);
1652 p_sys
->b_mute
= var_InheritBool(p_aout
, "mute");
1653 aout_MuteReport(p_aout
, p_sys
->b_mute
);
1655 char *psz_audio_device
= var_InheritString(p_aout
, "auhal-audio-device");
1656 SwitchAudioDevice(p_aout
, psz_audio_device
);
1657 free(psz_audio_device
);