Skip to content

Commit

Permalink
osdep: remove semaphore-mac
Browse files Browse the repository at this point in the history
It is only used in one place in ao_coreaudio_utils.c,
and can be replaced by condvar.
Removing it can reduce the maintenance burden.
  • Loading branch information
ruihe774 authored and Akemi committed Oct 12, 2024
1 parent 1821c06 commit 6fadaf6
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 167 deletions.
9 changes: 9 additions & 0 deletions audio/out/ao_coreaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#define IDLE_TIME 7 * NSEC_PER_SEC

struct priv {
// This must be put in the front
struct coreaudio_cb_sem sem;

AudioDeviceID device;
AudioUnit audio_unit;

Expand Down Expand Up @@ -543,6 +546,12 @@ const struct ao_driver audio_out_coreaudio = {
.hotplug_uninit = hotplug_uninit,
.list_devs = ca_get_device_list,
.priv_size = sizeof(struct priv),
.priv_defaults = &(const struct priv){
.sem = (struct coreaudio_cb_sem){
.mutex = MP_STATIC_MUTEX_INITIALIZER,
.cond = MP_STATIC_COND_INITIALIZER,
}
},
.options = (const struct m_option[]){
{"change-physical-format", OPT_BOOL(change_physical_format)},
{0}
Expand Down
7 changes: 7 additions & 0 deletions audio/out/ao_coreaudio_exclusive.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
#include "audio/out/ao_coreaudio_utils.h"

struct priv {
// This must be put in the front
struct coreaudio_cb_sem sem;

AudioDeviceID device; // selected device

bool paused;
Expand Down Expand Up @@ -459,6 +462,10 @@ const struct ao_driver audio_out_coreaudio_exclusive = {
.list_devs = ca_get_device_list,
.priv_size = sizeof(struct priv),
.priv_defaults = &(const struct priv){
.sem = (struct coreaudio_cb_sem){
.mutex = MP_STATIC_MUTEX_INITIALIZER,
.cond = MP_STATIC_COND_INITIALIZER,
},
.hog_pid = -1,
.stream = 0,
.stream_idx = -1,
Expand Down
24 changes: 13 additions & 11 deletions audio/out/ao_coreaudio_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "audio/out/ao_coreaudio_utils.h"
#include "osdep/timer.h"
#include "osdep/endian.h"
#include "osdep/semaphore.h"
#include "audio/format.h"

#if HAVE_COREAUDIO || HAVE_AVFOUNDATION
Expand Down Expand Up @@ -460,23 +459,23 @@ static OSStatus ca_change_format_listener(
const AudioObjectPropertyAddress addresses[],
void *data)
{
mp_sem_t *sem = data;
mp_sem_post(sem);
struct coreaudio_cb_sem *sem = data;
mp_mutex_lock(&sem->mutex);
mp_cond_broadcast(&sem->cond);
mp_mutex_unlock(&sem->mutex);
return noErr;
}

bool ca_change_physical_format_sync(struct ao *ao, AudioStreamID stream,
AudioStreamBasicDescription change_format)
{
struct coreaudio_cb_sem *sem = ao->priv;

OSStatus err = noErr;
bool format_set = false;

ca_print_asbd(ao, "setting stream physical format:", &change_format);

mp_sem_t wakeup;
if (mp_sem_init(&wakeup, 0, 0))
MP_HANDLE_OOM(0);

AudioStreamBasicDescription prev_format;
err = CA_GET(stream, kAudioStreamPropertyPhysicalFormat, &prev_format);
CHECK_CA_ERROR("can't get current physical format");
Expand All @@ -492,9 +491,11 @@ bool ca_change_physical_format_sync(struct ao *ao, AudioStreamID stream,

err = AudioObjectAddPropertyListener(stream, &p_addr,
ca_change_format_listener,
&wakeup);
sem);
CHECK_CA_ERROR("can't add property listener during format change");

mp_mutex_lock(&sem->mutex);

/* Change the format. */
err = CA_SET(stream, kAudioStreamPropertyPhysicalFormat, &change_format);
CHECK_CA_WARN("error changing physical format");
Expand All @@ -512,12 +513,14 @@ bool ca_change_physical_format_sync(struct ao *ao, AudioStreamID stream,
if (format_set)
break;

if (mp_sem_timedwait(&wakeup, wait_until)) {
if (mp_cond_timedwait_until(&sem->cond, &sem->mutex, wait_until)) {
MP_VERBOSE(ao, "reached timeout\n");
break;
}
}

mp_mutex_unlock(&sem->mutex);

ca_print_asbd(ao, "actual format in use:", &actual_format);

if (!format_set) {
Expand All @@ -530,11 +533,10 @@ bool ca_change_physical_format_sync(struct ao *ao, AudioStreamID stream,

err = AudioObjectRemovePropertyListener(stream, &p_addr,
ca_change_format_listener,
&wakeup);
sem);
CHECK_CA_ERROR("can't remove property listener");

coreaudio_error:
mp_sem_destroy(&wakeup);
return format_set;
}
#endif
6 changes: 6 additions & 0 deletions audio/out/ao_coreaudio_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
#include "audio/out/ao.h"
#include "internal.h"
#include "osdep/utils-mac.h"
#include "osdep/threads.h"

struct coreaudio_cb_sem {
mp_mutex mutex;
mp_cond cond;
};

bool check_ca_st(struct ao *ao, int level, OSStatus code, const char *message);

Expand Down
1 change: 0 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ sources = files(

## osdep
'osdep/io.c',
'osdep/semaphore-mac.c',
'osdep/subprocess.c',
'osdep/timer.c',

Expand Down
117 changes: 0 additions & 117 deletions osdep/semaphore-mac.c

This file was deleted.

38 changes: 0 additions & 38 deletions osdep/semaphore.h

This file was deleted.

0 comments on commit 6fadaf6

Please sign in to comment.