From a2d106cb3152be82ecef02ebf77b00f00341295d Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 1 Jul 2013 19:13:34 +0200 Subject: ao_coreaudio: remove device property listener on uninit Also extract this functionality inside a function in coreaudio_common --- audio/out/ao_coreaudio.c | 18 ++++++------------ audio/out/ao_coreaudio_common.c | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 12 deletions(-) (limited to 'audio') diff --git a/audio/out/ao_coreaudio.c b/audio/out/ao_coreaudio.c index 649f372cf4..9206d31bf9 100644 --- a/audio/out/ao_coreaudio.c +++ b/audio/out/ao_coreaudio.c @@ -430,7 +430,6 @@ static int init_digital(struct ao *ao, AudioStreamBasicDescription asbd) struct priv *p = ao->priv; struct priv_d *d = p->digital; OSStatus err = noErr; - AudioObjectPropertyAddress p_addr; uint32_t size; uint32_t is_alive = 1; @@ -524,17 +523,8 @@ static int init_digital(struct ao *ao, AudioStreamBasicDescription asbd) if (!AudioStreamChangeFormat(d->stream, d->stream_asbd)) goto coreaudio_error; - p_addr = (AudioObjectPropertyAddress) { - .mSelector = kAudioDevicePropertyDeviceHasChanged, - .mScope = kAudioObjectPropertyScopeGlobal, - .mElement = kAudioObjectPropertyElementMaster, - }; - - const int *stream_asdb_changed = &(d->stream_asbd_changed); - err = AudioObjectAddPropertyListener(p->device, - &p_addr, - ca_device_listener, - (void *)stream_asdb_changed); + void *changed = (void *) &(d->stream_asbd_changed); + err = ca_enable_device_listener(p->device, changed); CHECK_CA_ERROR("cannot install format change listener during init"); #if BYTE_ORDER == BIG_ENDIAN @@ -634,6 +624,10 @@ static void uninit(struct ao *ao, bool immed) } else { struct priv_d *d = p->digital; + void *changed = (void *) &(d->stream_asbd_changed); + err = ca_disable_device_listener(p->device, changed); + CHECK_CA_WARN("can't remove device listener, this may cause a crash"); + err = AudioDeviceStop(p->device, d->render_cb); CHECK_CA_WARN("failed to stop audio device"); diff --git a/audio/out/ao_coreaudio_common.c b/audio/out/ao_coreaudio_common.c index a3b7a741e3..1ea9bfe290 100644 --- a/audio/out/ao_coreaudio_common.c +++ b/audio/out/ao_coreaudio_common.c @@ -380,6 +380,32 @@ static OSStatus ca_enable_mixing(AudioDeviceID device, bool changed) { return noErr; } +static OSStatus ca_change_device_listening(AudioDeviceID device, + void *flag, bool enabled) +{ + AudioObjectPropertyAddress p_addr = (AudioObjectPropertyAddress) { + .mSelector = kAudioDevicePropertyDeviceHasChanged, + .mScope = kAudioObjectPropertyScopeGlobal, + .mElement = kAudioObjectPropertyElementMaster, + }; + + if (enabled) { + return AudioObjectAddPropertyListener( + device, &p_addr, ca_device_listener, flag); + } else { + return AudioObjectRemovePropertyListener( + device, &p_addr, ca_device_listener, flag); + } +} + +static OSStatus ca_enable_device_listener(AudioDeviceID device, void *flag) { + return ca_change_device_listening(device, flag, true); +} + +static OSStatus ca_disable_device_listener(AudioDeviceID device, void *flag) { + return ca_change_device_listening(device, flag, false); +} + static int AudioStreamChangeFormat(AudioStreamID i_stream_id, AudioStreamBasicDescription change_format) { -- cgit v1.2.3