Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PulseAudio return accurate latency information #53547

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions drivers/pulseaudio/audio_driver_pulseaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ Error AudioDriverPulseAudio::init_device() {

int latency = GLOBAL_GET("audio/driver/output_latency");
buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
real_latency = (float)buffer_frames / (float)mix_rate;

pa_buffer_size = buffer_frames * pa_map.channels;

print_verbose("PulseAudio: detected " + itos(pa_map.channels) + " channels");
Expand Down Expand Up @@ -343,28 +345,7 @@ Error AudioDriverPulseAudio::init() {
}

float AudioDriverPulseAudio::get_latency() {
if (latency == 0) { //only do this once since it's approximate anyway
lock();

pa_usec_t palat = 0;
if (pa_stream_get_state(pa_str) == PA_STREAM_READY) {
int negative = 0;

if (pa_stream_get_latency(pa_str, &palat, &negative) >= 0) {
if (negative) {
palat = 0;
}
}
}

if (palat > 0) {
latency = double(palat) / 1000000.0;
}

unlock();
}

return latency;
return real_latency;
}

void AudioDriverPulseAudio::thread_func(void *p_udata) {
Expand Down
3 changes: 1 addition & 2 deletions drivers/pulseaudio/audio_driver_pulseaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ class AudioDriverPulseAudio : public AudioDriver {
bool active = false;
bool thread_exited = false;
mutable bool exit_thread = false;

float latency = 0;
float real_latency = 0;

static void pa_state_cb(pa_context *c, void *userdata);
static void pa_sink_info_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata);
Expand Down