Skip to content

Commit

Permalink
coreaudio: add the AudioUnit latency to the device latency
Browse files Browse the repository at this point in the history
  • Loading branch information
tguillem authored and jbkempf committed Dec 2, 2022
1 parent 9793d06 commit 88fd1e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
30 changes: 28 additions & 2 deletions modules/audio_output/coreaudio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,30 @@ static vlc_tick_t
GetLatency(audio_output_t *p_aout)
{
struct aout_sys_common *p_sys = (struct aout_sys_common *) p_aout->sys;
return p_sys->get_latency != NULL ? p_sys->get_latency(p_aout)
: p_sys->i_dev_latency_ticks;

vlc_tick_t dev_latency_ticks =
p_sys->get_latency != NULL ? p_sys->get_latency(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;
}

/* Called from render callbacks. No lock, wait, and IO here */
Expand Down Expand Up @@ -339,6 +361,8 @@ 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;
p_sys->started = false;
Expand Down Expand Up @@ -725,6 +749,7 @@ au_Initialize(audio_output_t *p_aout, AudioUnit au, audio_sample_format_t *fmt,
const AudioChannelLayout *outlayout, vlc_tick_t i_dev_latency_ticks,
get_latency_cb get_latency, bool *warn_configuration)
{
struct aout_sys_common *p_sys = (struct aout_sys_common *) p_aout->sys;
int ret;
AudioChannelLayout *inlayout_buf = NULL;
const AudioChannelLayout *inlayout = NULL;
Expand Down Expand Up @@ -852,6 +877,7 @@ au_Initialize(audio_output_t *p_aout, AudioUnit au, audio_sample_format_t *fmt,
AudioUnitUninitialize(au);
return VLC_EGENERIC;
}
p_sys->au = au;

return VLC_SUCCESS;
}
Expand Down
4 changes: 4 additions & 0 deletions modules/audio_output/coreaudio_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ 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 @@ -71,6 +73,8 @@ 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 */
vlc_tick_t au_latency_ticks;

union lock
{
Expand Down

0 comments on commit 88fd1e6

Please sign in to comment.