summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefano Pigozzi <stefano.pigozzi@gmail.com>2013-06-20 13:57:05 +0200
committerStefano Pigozzi <stefano.pigozzi@gmail.com>2013-07-22 21:53:17 +0200
commitf35f6a34b534cf4524f035e442fff6324875ed85 (patch)
treefd69e88fccf8ad6e6b958f7d8702531d0ff6d10c
parentdc8eb9d77a7b43cd2f76a556c6887b3f9eadb75c (diff)
downloadmpv-f35f6a34b534cf4524f035e442fff6324875ed85.tar.bz2
mpv-f35f6a34b534cf4524f035e442fff6324875ed85.tar.xz
ao_coreaudio: split out some utility functions and refactor them
Split out some utility functions that use the CoreAudio API but are not related the main task of the AOs (which is to move data correctly to the ringbuffer). These are mainly need for the verbosity of the CoreAudio API and are just obscuring the 'real' code.
-rw-r--r--audio/out/ao_coreaudio.c321
-rw-r--r--audio/out/ao_coreaudio_common.c304
2 files changed, 315 insertions, 310 deletions
diff --git a/audio/out/ao_coreaudio.c b/audio/out/ao_coreaudio.c
index 6b71aec339..4986e3a817 100644
--- a/audio/out/ao_coreaudio.c
+++ b/audio/out/ao_coreaudio.c
@@ -32,18 +32,9 @@
* when you are wanting to do good buffering of audio).
*/
-#include <CoreServices/CoreServices.h>
-#include <AudioUnit/AudioUnit.h>
-#include <AudioToolbox/AudioToolbox.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <inttypes.h>
-#include <sys/types.h>
-#include <unistd.h>
-
#include "config.h"
-#include "core/mp_msg.h"
+
+#include "audio/out/ao_coreaudio_common.c"
#include "ao.h"
#include "audio/format.h"
@@ -51,8 +42,6 @@
#include "core/subopt-helper.h"
#include "core/mp_ring.h"
-#define ca_msg(a, b ...) mp_msg(MSGT_AO, a, "AO: [coreaudio] " b)
-
static void audio_pause(struct ao *ao);
static void audio_resume(struct ao *ao);
static void reset(struct ao *ao);
@@ -64,47 +53,6 @@ static void print_buffer(struct mp_ring *buffer)
talloc_free(tctx);
}
-static bool check_ca_st(int level, OSStatus code, const char *message)
-{
- if (code == noErr) return true;
-
- // Extract FourCC letters from the uint32_t and finde out if it's a valid
- // code that is made of letters.
- char fcc[4] = {
- (code >> 24) & 0xFF,
- (code >> 16) & 0xFF,
- (code >> 8) & 0xFF,
- code & 0xFF,
- };
-
- bool valid_fourcc = true;
- for (int i = 0; i < 4; i++)
- if (!isprint(fcc[i]))
- valid_fourcc = false;
-
- char *error_string;
- if (valid_fourcc)
- error_string =
- talloc_asprintf(NULL, "'%c%c%c%c'", fcc[0], fcc[1], fcc[2], fcc[3]);
- else
- error_string = talloc_asprintf(NULL, "%d", code);
-
- ca_msg(level, "%s (%s)\n", message, error_string);
-
- talloc_free(error_string);
-
- return false;
-}
-
-#define CHECK_CA_ERROR_L(label, message) \
- do { \
- if (!check_ca_st(MSGL_ERR, err, message)) { \
- goto label; \
- } \
- } while (0)
-
-#define CHECK_CA_ERROR(message) CHECK_CA_ERROR_L(coreaudio_error, message)
-
struct priv
{
AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device. */
@@ -229,151 +177,9 @@ coreaudio_error:
return CONTROL_ERROR;
}
-
-static void print_format(int lev, const char *str,
- const AudioStreamBasicDescription *f)
-{
- uint32_t flags = (uint32_t) f->mFormatFlags;
- ca_msg(lev,
- "%s %7.1fHz %" PRIu32 "bit [%c%c%c%c][%" PRIu32 "][%" PRIu32 "][%" PRIu32 "][%" PRIu32 "][%" PRIu32 "] %s %s %s%s%s%s\n",
- str, f->mSampleRate, f->mBitsPerChannel,
- (int)(f->mFormatID & 0xff000000) >> 24,
- (int)(f->mFormatID & 0x00ff0000) >> 16,
- (int)(f->mFormatID & 0x0000ff00) >> 8,
- (int)(f->mFormatID & 0x000000ff) >> 0,
- f->mFormatFlags, f->mBytesPerPacket,
- f->mFramesPerPacket, f->mBytesPerFrame,
- f->mChannelsPerFrame,
- (flags & kAudioFormatFlagIsFloat) ? "float" : "int",
- (flags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE",
- (flags & kAudioFormatFlagIsSignedInteger) ? "S" : "U",
- (flags & kAudioFormatFlagIsPacked) ? " packed" : "",
- (flags & kAudioFormatFlagIsAlignedHigh) ? " aligned" : "",
- (flags & kAudioFormatFlagIsNonInterleaved) ? " ni" : "");
-}
-
-static OSStatus GetAudioProperty(AudioObjectID id,
- AudioObjectPropertySelector selector,
- UInt32 outSize, void *outData)
-{
- AudioObjectPropertyAddress p_addr;
-
- p_addr.mSelector = selector;
- p_addr.mScope = kAudioObjectPropertyScopeGlobal;
- p_addr.mElement = kAudioObjectPropertyElementMaster;
-
- return AudioObjectGetPropertyData(id, &p_addr, 0, NULL, &outSize,
- outData);
-}
-
-static UInt32 GetAudioPropertyArray(AudioObjectID id,
- AudioObjectPropertySelector selector,
- AudioObjectPropertyScope scope,
- void **data)
-{
- OSStatus err;
- AudioObjectPropertyAddress p_addr;
- UInt32 p_size;
-
- p_addr.mSelector = selector;
- p_addr.mScope = scope;
- p_addr.mElement = kAudioObjectPropertyElementMaster;
-
- err = AudioObjectGetPropertyDataSize(id, &p_addr, 0, NULL, &p_size);
- CHECK_CA_ERROR("Can't fetch property size");
-
- *data = malloc(p_size);
-
- err = AudioObjectGetPropertyData(id, &p_addr, 0, NULL, &p_size, *data);
- CHECK_CA_ERROR_L(coreaudio_error_free, "Can't fetch property data %s");
-
- return p_size;
-
-coreaudio_error_free:
- free(*data);
-coreaudio_error:
- return 0;
-}
-
-static UInt32 GetGlobalAudioPropertyArray(AudioObjectID id,
- AudioObjectPropertySelector selector,
- void **outData)
-{
- return GetAudioPropertyArray(id, selector, kAudioObjectPropertyScopeGlobal,
- outData);
-}
-
-static OSStatus GetAudioPropertyString(AudioObjectID id,
- AudioObjectPropertySelector selector,
- char **outData)
-{
- OSStatus err;
- AudioObjectPropertyAddress property_address;
- UInt32 i_param_size;
- CFStringRef string;
- CFIndex string_length;
-
- property_address.mSelector = selector;
- property_address.mScope = kAudioObjectPropertyScopeGlobal;
- property_address.mElement = kAudioObjectPropertyElementMaster;
-
- i_param_size = sizeof(CFStringRef);
- err = AudioObjectGetPropertyData(id, &property_address, 0, NULL,
- &i_param_size, &string);
- if (err != noErr)
- return err;
-
- string_length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(string),
- kCFStringEncodingASCII);
- *outData = malloc(string_length + 1);
- CFStringGetCString(string, *outData, string_length + 1,
- kCFStringEncodingASCII);
-
- CFRelease(string);
-
- return err;
-}
-
-static OSStatus SetAudioProperty(AudioObjectID id,
- AudioObjectPropertySelector selector,
- UInt32 inDataSize, void *inData)
-{
- AudioObjectPropertyAddress p_addr;
-
- p_addr.mSelector = selector;
- p_addr.mScope = kAudioObjectPropertyScopeGlobal;
- p_addr.mElement = kAudioObjectPropertyElementMaster;
-
- return AudioObjectSetPropertyData(id, &p_addr, 0, NULL,
- inDataSize, inData);
-}
-
-static Boolean IsAudioPropertySettable(AudioObjectID id,
- AudioObjectPropertySelector selector,
- Boolean *outData)
-{
- AudioObjectPropertyAddress p_addr;
-
- p_addr.mSelector = selector;
- p_addr.mScope = kAudioObjectPropertyScopeGlobal;
- p_addr.mElement = kAudioObjectPropertyElementMaster;
-
- return AudioObjectIsPropertySettable(id, &p_addr, outData);
-}
-
-static int AudioDeviceSupportsDigital(AudioDeviceID i_dev_id);
-static int AudioStreamSupportsDigital(AudioStreamID i_stream_id);
static int OpenSPDIF(struct ao *ao);
static int AudioStreamChangeFormat(AudioStreamID i_stream_id,
AudioStreamBasicDescription change_format);
-static OSStatus StreamListener(AudioObjectID inObjectID,
- UInt32 inNumberAddresses,
- const AudioObjectPropertyAddress inAddresses[],
- void *inClientData);
-static OSStatus DeviceListener(AudioObjectID inObjectID,
- UInt32 inNumberAddresses,
- const AudioObjectPropertyAddress inAddresses[],
- void *inClientData);
static void print_help(void)
{
@@ -544,7 +350,7 @@ static int init(struct ao *ao, char *params)
inDesc.mFramesPerPacket *
ao->channels.num *
(inDesc.mBitsPerChannel / 8);
- print_format(MSGL_V, "source:", &inDesc);
+ ca_print_asbd("source format:", &inDesc);
if (p->b_supports_digital) {
b_alive = 1;
@@ -839,7 +645,7 @@ static int OpenSPDIF(struct ao *ao)
goto err_out;
}
- print_format(MSGL_V, "original stream format:", &p->sfmt_revert);
+ ca_print_asbd("original stream format:", &p->sfmt_revert);
if (!AudioStreamChangeFormat(p->i_stream_id, p->stream_format))
goto err_out;
@@ -848,10 +654,11 @@ static int OpenSPDIF(struct ao *ao)
p_addr.mScope = kAudioObjectPropertyScopeGlobal;
p_addr.mElement = kAudioObjectPropertyElementMaster;
+ const int *stream_format_changed = &(p->b_stream_format_changed);
err = AudioObjectAddPropertyListener(p->i_selected_dev,
&p_addr,
- DeviceListener,
- NULL);
+ ca_device_listener,
+ (void *)stream_format_changed);
if (err != noErr)
ca_msg(MSGL_WARN,
"AudioDeviceAddPropertyListener for kAudioDevicePropertyDeviceHasChanged failed: [%4.4s]\n",
@@ -926,73 +733,6 @@ err_out:
}
/*****************************************************************************
-* AudioDeviceSupportsDigital: Check i_dev_id for digital stream support.
-*****************************************************************************/
-static int AudioDeviceSupportsDigital(AudioDeviceID i_dev_id)
-{
- UInt32 i_param_size = 0;
- AudioStreamID *p_streams = NULL;
- int i = 0, i_streams = 0;
- int b_return = CONTROL_FALSE;
-
- /* Retrieve all the output streams. */
- i_param_size = GetAudioPropertyArray(i_dev_id,
- kAudioDevicePropertyStreams,
- kAudioDevicePropertyScopeOutput,
- (void **)&p_streams);
-
- if (!i_param_size) {
- ca_msg(MSGL_WARN, "could not get number of streams.\n");
- return CONTROL_FALSE;
- }
-
- i_streams = i_param_size / sizeof(AudioStreamID);
-
- for (i = 0; i < i_streams; ++i) {
- if (AudioStreamSupportsDigital(p_streams[i]))
- b_return = CONTROL_OK;
- }
-
- free(p_streams);
- return b_return;
-}
-
-/*****************************************************************************
-* AudioStreamSupportsDigital: Check i_stream_id for digital stream support.
-*****************************************************************************/
-static int AudioStreamSupportsDigital(AudioStreamID i_stream_id)
-{
- UInt32 i_param_size;
- AudioStreamRangedDescription *p_format_list = NULL;
- int i, i_formats, b_return = CONTROL_FALSE;
-
- /* Retrieve all the stream formats supported by each output stream. */
- i_param_size = GetGlobalAudioPropertyArray(i_stream_id,
- kAudioStreamPropertyAvailablePhysicalFormats,
- (void **)&p_format_list);
-
- if (!i_param_size) {
- ca_msg(MSGL_WARN, "Could not get number of stream formats.\n");
- return CONTROL_FALSE;
- }
-
- i_formats = i_param_size / sizeof(AudioStreamRangedDescription);
-
- for (i = 0; i < i_formats; ++i) {
- print_format(MSGL_V, "supported format:", &(p_format_list[i].mFormat));
-
- if (p_format_list[i].mFormat.mFormatID == 'IAC3' ||
- p_format_list[i].mFormat.mFormatID == 'iac3' ||
- p_format_list[i].mFormat.mFormatID == kAudioFormat60958AC3 ||
- p_format_list[i].mFormat.mFormatID == kAudioFormatAC3)
- b_return = CONTROL_OK;
- }
-
- free(p_format_list);
- return b_return;
-}
-
-/*****************************************************************************
* AudioStreamChangeFormat: Change i_stream_id to change_format
*****************************************************************************/
static int AudioStreamChangeFormat(AudioStreamID i_stream_id,
@@ -1005,7 +745,7 @@ static int AudioStreamChangeFormat(AudioStreamID i_stream_id,
static volatile int stream_format_changed;
stream_format_changed = 0;
- print_format(MSGL_V, "setting stream format:", &change_format);
+ ca_print_asbd("setting stream format:", &change_format);
/* Install the callback. */
p_addr.mSelector = kAudioStreamPropertyPhysicalFormat;
@@ -1014,7 +754,7 @@ static int AudioStreamChangeFormat(AudioStreamID i_stream_id,
err = AudioObjectAddPropertyListener(i_stream_id,
&p_addr,
- StreamListener,
+ ca_stream_listener,
(void *)&stream_format_changed);
if (err != noErr) {
ca_msg(MSGL_WARN,
@@ -1052,7 +792,7 @@ static int AudioStreamChangeFormat(AudioStreamID i_stream_id,
sizeof(AudioStreamBasicDescription),
&actual_format);
- print_format(MSGL_V, "actual format in use:", &actual_format);
+ ca_print_asbd("actual format in use:", &actual_format);
if (actual_format.mSampleRate == change_format.mSampleRate &&
actual_format.mFormatID == change_format.mFormatID &&
actual_format.mFramesPerPacket == change_format.mFramesPerPacket) {
@@ -1065,7 +805,7 @@ static int AudioStreamChangeFormat(AudioStreamID i_stream_id,
/* Removing the property listener. */
err = AudioObjectRemovePropertyListener(i_stream_id,
&p_addr,
- StreamListener,
+ ca_stream_listener,
(void *)&stream_format_changed);
if (err != noErr) {
ca_msg(MSGL_WARN,
@@ -1248,45 +988,6 @@ static void audio_resume(struct ao *ao)
p->paused = 0;
}
-/*****************************************************************************
-* StreamListener
-*****************************************************************************/
-static OSStatus StreamListener(AudioObjectID inObjectID,
- UInt32 inNumberAddresses,
- const AudioObjectPropertyAddress inAddresses[],
- void *inClientData)
-{
- for (int i = 0; i < inNumberAddresses; ++i) {
- if (inAddresses[i].mSelector == kAudioStreamPropertyPhysicalFormat) {
- ca_msg(MSGL_WARN,
- "got notify kAudioStreamPropertyPhysicalFormat changed.\n");
- if (inClientData)
- *(volatile int *)inClientData = 1;
- break;
- }
- }
- return noErr;
-}
-
-static OSStatus DeviceListener(AudioObjectID inObjectID,
- UInt32 inNumberAddresses,
- const AudioObjectPropertyAddress inAddresses[],
- void *inClientData)
-{
- struct ao *ao = inClientData;
- struct priv *p = ao->priv;
-
- for (int i = 0; i < inNumberAddresses; ++i) {
- if (inAddresses[i].mSelector == kAudioDevicePropertyDeviceHasChanged) {
- ca_msg(MSGL_WARN,
- "got notify kAudioDevicePropertyDeviceHasChanged.\n");
- p->b_stream_format_changed = 1;
- break;
- }
- }
- return noErr;
-}
-
const struct ao_driver audio_out_coreaudio = {
.info = &(const struct ao_info) {
"CoreAudio (Native OS X Audio Output)",
diff --git a/audio/out/ao_coreaudio_common.c b/audio/out/ao_coreaudio_common.c
new file mode 100644
index 0000000000..56bb99314f
--- /dev/null
+++ b/audio/out/ao_coreaudio_common.c
@@ -0,0 +1,304 @@
+/*
+ * This file is part of mpv.
+ *
+ * mpv is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * mpv is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with mpv. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * This file contains functions interacting with the CoreAudio framework
+ * that are not specific to the AUHAL. These are split in a separate file for
+ * the sake of readability. In the future the could be used by other AOs based
+ * on CoreAudio but not the AUHAL (such as using AudioQueue services).
+ */
+
+#include <AudioToolbox/AudioToolbox.h>
+#include <AudioUnit/AudioUnit.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include "core/mp_msg.h"
+
+#define ca_msg(a, b ...) mp_msg(MSGT_AO, a, "AO: [coreaudio] " b)
+#define CA_CFSTR_ENCODING kCFStringEncodingASCII
+
+static char *fourcc_repr(void *talloc_ctx, uint32_t code)
+{
+ // Extract FourCC letters from the uint32_t and finde out if it's a valid
+ // code that is made of letters.
+ char fcc[4] = {
+ (code >> 24) & 0xFF,
+ (code >> 16) & 0xFF,
+ (code >> 8) & 0xFF,
+ code & 0xFF,
+ };
+
+ bool valid_fourcc = true;
+ for (int i = 0; i < 4; i++)
+ if (!isprint(fcc[i]))
+ valid_fourcc = false;
+
+ char *repr;
+ if (valid_fourcc)
+ repr = talloc_asprintf(talloc_ctx, "'%c%c%c%c'",
+ fcc[0], fcc[1], fcc[2], fcc[3]);
+ else
+ repr = talloc_asprintf(NULL, "%d", code);
+
+ return repr;
+}
+
+static bool check_ca_st(int level, OSStatus code, const char *message)
+{
+ if (code == noErr) return true;
+
+ char *error_string = fourcc_repr(NULL, code);
+ ca_msg(level, "%s (%s)\n", message, error_string);
+ talloc_free(error_string);
+
+ return false;
+}
+
+#define CHECK_CA_ERROR_L(label, message) \
+ do { \
+ if (!check_ca_st(MSGL_ERR, err, message)) { \
+ goto label; \
+ } \
+ } while (0)
+
+#define CHECK_CA_ERROR(message) CHECK_CA_ERROR_L(coreaudio_error, message)
+
+static void ca_print_asbd(const char *description,
+ const AudioStreamBasicDescription *asbd)
+{
+ uint32_t flags = asbd->mFormatFlags;
+ char *format = fourcc_repr(NULL, asbd->mFormatID);
+
+ ca_msg(MSGL_V,
+ "%s %7.1fHz %" PRIu32 "bit [%s]"
+ "[%" PRIu32 "][%" PRIu32 "][%" PRIu32 "]"
+ "[%" PRIu32 "][%" PRIu32 "] "
+ "%s %s %s%s%s%s\n",
+ description, asbd->mSampleRate, asbd->mBitsPerChannel, format,
+ asbd->mFormatFlags, asbd->mBytesPerPacket, asbd->mFramesPerPacket,
+ asbd->mBytesPerFrame, asbd->mChannelsPerFrame,
+ (flags & kAudioFormatFlagIsFloat) ? "float" : "int",
+ (flags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE",
+ (flags & kAudioFormatFlagIsSignedInteger) ? "S" : "U",
+ (flags & kAudioFormatFlagIsPacked) ? " packed" : "",
+ (flags & kAudioFormatFlagIsAlignedHigh) ? " aligned" : "",
+ (flags & kAudioFormatFlagIsNonInterleaved) ? " P" : "");
+
+ talloc_free(format);
+}
+
+static OSStatus GetAudioProperty(AudioObjectID id,
+ AudioObjectPropertySelector selector,
+ UInt32 outSize, void *outData)
+{
+ AudioObjectPropertyAddress p_addr;
+
+ p_addr.mSelector = selector;
+ p_addr.mScope = kAudioObjectPropertyScopeGlobal;
+ p_addr.mElement = kAudioObjectPropertyElementMaster;
+
+ return AudioObjectGetPropertyData(id, &p_addr, 0, NULL, &outSize,
+ outData);
+}
+
+static UInt32 GetAudioPropertyArray(AudioObjectID id,
+ AudioObjectPropertySelector selector,
+ AudioObjectPropertyScope scope,
+ void **data)
+{
+ OSStatus err;
+ AudioObjectPropertyAddress p_addr;
+ UInt32 p_size;
+
+ p_addr.mSelector = selector;
+ p_addr.mScope = scope;
+ p_addr.mElement = kAudioObjectPropertyElementMaster;
+
+ err = AudioObjectGetPropertyDataSize(id, &p_addr, 0, NULL, &p_size);
+ CHECK_CA_ERROR("Can't fetch property size");
+
+ *data = malloc(p_size);
+
+ err = AudioObjectGetPropertyData(id, &p_addr, 0, NULL, &p_size, *data);
+ CHECK_CA_ERROR_L(coreaudio_error_free, "Can't fetch property data %s");
+
+ return p_size;
+
+coreaudio_error_free:
+ free(*data);
+coreaudio_error:
+ return 0;
+}
+
+static UInt32 GetGlobalAudioPropertyArray(AudioObjectID id,
+ AudioObjectPropertySelector selector,
+ void **outData)
+{
+ return GetAudioPropertyArray(id, selector, kAudioObjectPropertyScopeGlobal,
+ outData);
+}
+
+static OSStatus GetAudioPropertyString(AudioObjectID id,
+ AudioObjectPropertySelector selector,
+ char **data)
+{
+ OSStatus err;
+ AudioObjectPropertyAddress p_addr;
+ UInt32 p_size = sizeof(CFStringRef);
+ CFStringRef string;
+
+ p_addr.mSelector = selector;
+ p_addr.mScope = kAudioObjectPropertyScopeGlobal;
+ p_addr.mElement = kAudioObjectPropertyElementMaster;
+
+ err = AudioObjectGetPropertyData(id, &p_addr, 0, NULL, &p_size, &string);
+ CHECK_CA_ERROR("Can't fetch array property");
+
+ CFIndex size =
+ CFStringGetMaximumSizeForEncoding(
+ CFStringGetLength(string), CA_CFSTR_ENCODING) + 1;
+
+ *data = malloc(size);
+ CFStringGetCString(string, *data, size, CA_CFSTR_ENCODING);
+ CFRelease(string);
+coreaudio_error:
+ return err;
+}
+
+static OSStatus SetAudioProperty(AudioObjectID id,
+ AudioObjectPropertySelector selector,
+ UInt32 inDataSize, void *inData)
+{
+ AudioObjectPropertyAddress p_addr;
+
+ p_addr.mSelector = selector;
+ p_addr.mScope = kAudioObjectPropertyScopeGlobal;
+ p_addr.mElement = kAudioObjectPropertyElementMaster;
+
+ return AudioObjectSetPropertyData(id, &p_addr, 0, NULL,
+ inDataSize, inData);
+}
+
+static Boolean IsAudioPropertySettable(AudioObjectID id,
+ AudioObjectPropertySelector selector,
+ Boolean *outData)
+{
+ AudioObjectPropertyAddress p_addr;
+
+ p_addr.mSelector = selector;
+ p_addr.mScope = kAudioObjectPropertyScopeGlobal;
+ p_addr.mElement = kAudioObjectPropertyElementMaster;
+
+ return AudioObjectIsPropertySettable(id, &p_addr, outData);
+}
+
+static int AudioStreamSupportsDigital(AudioStreamID stream)
+{
+ AudioStreamRangedDescription *formats = NULL;
+
+ /* Retrieve all the stream formats supported by each output stream. */
+ uint32_t size =
+ GetGlobalAudioPropertyArray(stream,
+ kAudioStreamPropertyAvailablePhysicalFormats,
+ (void **)&formats);
+
+ if (!size) {
+ ca_msg(MSGL_WARN, "Could not get number of stream formats.\n");
+ return CONTROL_FALSE;
+ }
+
+ const int n_formats = size / sizeof(AudioStreamRangedDescription);
+ for (int i = 0; i < n_formats; ++i) {
+ AudioStreamBasicDescription asbd = formats[i].mFormat;
+ ca_print_asbd("supported format:", &(asbd));
+
+ switch (asbd.mFormatID)
+ case 'IAC3':
+ case 'iac3':
+ case kAudioFormat60958AC3:
+ case kAudioFormatAC3:
+ free(formats);
+ return CONTROL_OK;
+ }
+
+ free(formats);
+ return CONTROL_FALSE;
+}
+
+static int AudioDeviceSupportsDigital(AudioDeviceID device)
+{
+ AudioStreamID *streams = NULL;
+
+ /* Retrieve all the output streams. */
+ uint32_t size = GetAudioPropertyArray(device,
+ kAudioDevicePropertyStreams,
+ kAudioDevicePropertyScopeOutput,
+ (void **)&streams);
+
+ if (!size) {
+ ca_msg(MSGL_WARN, "could not get number of streams.\n");
+ return CONTROL_FALSE;
+ }
+
+ const int n_streams = size / sizeof(AudioStreamID);
+ for (int i = 0; i < n_streams; ++i) {
+ if (AudioStreamSupportsDigital(streams[i])) {
+ free(streams);
+ return CONTROL_OK;
+ }
+ }
+
+ free(streams);
+ return CONTROL_FALSE;
+}
+
+static OSStatus ca_property_listener(AudioObjectPropertySelector selector,
+ AudioObjectID object, uint32_t n_addresses,
+ const AudioObjectPropertyAddress addresses[],
+ void *data)
+{
+ // TODO: ++i seems wrong in this context. Check out if it was a programmer
+ // mistake
+ void *talloc_ctx = talloc_new(NULL);
+
+ for (int i = 0; i < n_addresses; ++i) {
+ if (addresses[i].mSelector == selector) {
+ ca_msg(MSGL_WARN, "event: property %s changed",
+ fourcc_repr(talloc_ctx, selector));
+ if (data) *(volatile int *)data = 1;
+ break;
+ }
+ }
+ talloc_free(talloc_ctx);
+ return noErr;
+}
+
+static OSStatus ca_stream_listener(AudioObjectID object, uint32_t n_addresses,
+ const AudioObjectPropertyAddress addresses[],
+ void *data)
+{
+ return ca_property_listener(kAudioStreamPropertyPhysicalFormat,
+ object, n_addresses, addresses, data);
+}
+
+static OSStatus ca_device_listener(AudioObjectID object, uint32_t n_addresses,
+ const AudioObjectPropertyAddress addresses[],
+ void *data)
+{
+ return ca_property_listener(kAudioDevicePropertyDeviceHasChanged,
+ object, n_addresses, addresses, data);
+}