ao_coreaudio: handle latency change on hotplug

The device latency may change during hotplugging.
This commit updates p->hw_latency_ns each time
hotplug_cb is called so that it can reflect
updated device latency.
This commit is contained in:
Misaki Kasumi 2024-03-28 05:03:10 +08:00 committed by der richter
parent 1ed8607292
commit 276bbb8884
1 changed files with 13 additions and 2 deletions

View File

@ -137,6 +137,7 @@ static int control(struct ao *ao, enum aocontrol cmd, void *arg)
static bool init_audiounit(struct ao *ao, AudioStreamBasicDescription asbd);
static void init_physical_format(struct ao *ao);
static void reinit_latency(struct ao *ao);
static bool reinit_device(struct ao *ao) {
struct priv *p = ao->priv;
@ -175,6 +176,8 @@ static int init(struct ao *ao)
if (!init_audiounit(ao, asbd))
goto coreaudio_error;
reinit_latency(ao);
p->queue = dispatch_queue_create("io.mpv.coreaudio_stop_during_idle",
DISPATCH_QUEUE_SERIAL);
@ -307,8 +310,6 @@ static bool init_audiounit(struct ao *ao, AudioStreamBasicDescription asbd)
CHECK_CA_ERROR_L(coreaudio_error_audiounit,
"can't link audio unit to selected device");
p->hw_latency_ns = ca_get_hardware_latency(ao);
AURenderCallbackStruct render_cb = (AURenderCallbackStruct) {
.inputProc = render_cb_lpcm,
.inputProcRefCon = ao,
@ -332,6 +333,13 @@ coreaudio_error:
return false;
}
static void reinit_latency(struct ao *ao)
{
struct priv *p = ao->priv;
p->hw_latency_ns = ca_get_hardware_latency(ao);
}
static void stop(struct ao *ao)
{
struct priv *p = ao->priv;
@ -432,8 +440,11 @@ static OSStatus hotplug_cb(AudioObjectID id, UInt32 naddr,
void *ctx)
{
struct ao *ao = ctx;
struct priv *p = ao->priv;
MP_VERBOSE(ao, "Handling potential hotplug event...\n");
reinit_device(ao);
if (p->audio_unit)
reinit_latency(ao);
ao_hotplug_event(ao);
return noErr;
}