Skip to content

Commit

Permalink
coreaudio: don't call AudioUnit API from the render callback
Browse files Browse the repository at this point in the history
It is very unlikely that AudioUnitGetProperty(kAudioUnitProperty_Latency) can
change midstream (contrary to [AVInstance outputLatency]), so only fetch
this latency when starting the AudioUnit.

This fixes a deadlock between the render callback and AudioUnitStop().

Fixes #27591
  • Loading branch information
tguillem authored and fkuehne committed Dec 7, 2022
1 parent 32160df commit c8cd58f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
30 changes: 10 additions & 20 deletions modules/audio_output/coreaudio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,7 @@ GetLatency(audio_output_t *p_aout)
: p_sys->i_dev_latency_ticks;

/* Add the AudioUnit latency to the auhal/audiounit_ios latency */
if (p_sys->au != NULL)
{
Float64 unit_s;
if (AudioUnitGetProperty(p_sys->au, kAudioUnitProperty_Latency,
kAudioUnitScope_Global, 0, &unit_s,
&(UInt32) { sizeof(unit_s) }) != noErr)
unit_s = 0;
vlc_tick_t us = vlc_tick_from_sec(unit_s);
if (us != p_sys->au_latency_ticks)
{
msg_Dbg(p_aout, "Adding AudioUnit latency: %" PRId64 "us", us);
p_sys->au_latency_ticks = us;
}

dev_latency_ticks += us;
}

return dev_latency_ticks;
return dev_latency_ticks + p_sys->au_latency_ticks;
}

/* Called from render callbacks. No lock, wait, and IO here */
Expand Down Expand Up @@ -361,7 +344,6 @@ ca_Initialize(audio_output_t *p_aout, const audio_sample_format_t *fmt,
{
struct aout_sys_common *p_sys = (struct aout_sys_common *) p_aout->sys;

p_sys->au = NULL;
p_sys->au_latency_ticks = 0;
p_sys->i_underrun_size = 0;
p_sys->b_paused = false;
Expand Down Expand Up @@ -874,7 +856,15 @@ au_Initialize(audio_output_t *p_aout, AudioUnit au, audio_sample_format_t *fmt,
AudioUnitUninitialize(au);
return VLC_EGENERIC;
}
p_sys->au = au;

Float64 unit_s;
if (AudioUnitGetProperty(au, kAudioUnitProperty_Latency,
kAudioUnitScope_Global, 0, &unit_s,
&(UInt32) { sizeof(unit_s) }) == noErr)
{
p_sys->au_latency_ticks = vlc_tick_from_sec(unit_s);
msg_Dbg(p_aout, "AudioUnit latency: %" PRId64 "us", p_sys->au_latency_ticks);
}

return VLC_SUCCESS;
}
Expand Down
4 changes: 1 addition & 3 deletions modules/audio_output/coreaudio_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ struct aout_sys_common
{
/* The following is owned by common.c (initialized from ca_Open) */

AudioUnit au; /* Can be NULL (pass-through) */

mach_timebase_info_data_t tinfo;

size_t i_underrun_size;
Expand All @@ -73,7 +71,7 @@ struct aout_sys_common
size_t timing_report_last_written_bytes;
/* Number of bytes to write before sending a timing report */
size_t timing_report_delay_bytes;
/* Last AudioUnit Latency, for debug/log purpose */
/* AudioUnit Latency */
vlc_tick_t au_latency_ticks;

union lock
Expand Down

0 comments on commit c8cd58f

Please sign in to comment.