For USB devices the latency jitter strongly depends on device latency. Therefore a boolean low_device_latency parameter is introduced to half the device latency. Normally 1/3 of the configured end-to-end latency is used, with the parameter this is changed to 1/6. In many situations the parameter can improve latency stability but it will also lead to significantly higher CPU consumption. --- src/modules/module-loopback.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/modules/module-loopback.c b/src/modules/module-loopback.c index 77a5c04..35a817b 100644 --- a/src/modules/module-loopback.c +++ b/src/modules/module-loopback.c @@ -47,6 +47,7 @@ PA_MODULE_USAGE( "sink=<sink to connect to> " "adjust_time=<how often to readjust rates in s, values >= 100 are in ms> " "latency_msec=<latency in ms> " + "low_device_latency=<boolean, use half of the normal device latency> " "format=<sample format> " "rate=<sample rate> " "channels=<number of channels> " @@ -97,6 +98,7 @@ struct userdata { /* Values from command line configuration */ pa_usec_t latency; pa_usec_t adjust_time; + bool low_device_latency; /* Latency boundaries and current values */ pa_usec_t min_source_latency; @@ -149,6 +151,7 @@ static const char* const valid_modargs[] = { "sink", "adjust_time", "latency_msec", + "low_device_latency", "format", "rate", "channels", @@ -590,11 +593,14 @@ static void update_latency_boundaries(struct userdata *u, pa_source *source, pa_ update_minimum_latency(u); } -/* Set source output latency to one third of the overall latency if possible */ +/* In low device latency mode set source to one sixth of the overall latency, + * else use one third to save CPU */ static void set_source_output_latency(struct userdata *u, pa_source *source) { pa_usec_t latency, requested_latency; requested_latency = u->latency / 3; + if (u->low_device_latency) + requested_latency = u->latency / 6; latency = PA_CLAMP(requested_latency , u->min_source_latency, u->max_source_latency); u->configured_source_latency = pa_source_output_set_requested_latency(u->source_output, latency); @@ -887,11 +893,14 @@ static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, in return pa_sink_input_process_msg(obj, code, data, offset, chunk); } -/* Set sink input latency to one third of the overall latency if possible */ +/* In low device latency mode set sink to one sixth of the overall latency, + * else use one third to save CPU */ static void set_sink_input_latency(struct userdata *u, pa_sink *sink) { pa_usec_t latency, requested_latency; requested_latency = u->latency / 3; + if (u->low_device_latency) + requested_latency = u->latency / 6; latency = PA_CLAMP(requested_latency , u->min_sink_latency, u->max_sink_latency); u->configured_sink_latency = pa_sink_input_set_requested_latency(u->sink_input, latency); @@ -1110,6 +1119,7 @@ int pa__init(pa_module *m) { uint32_t adjust_time; const char *n; bool remix = true; + bool low_device_latency = false; pa_assert(m); @@ -1185,6 +1195,11 @@ int pa__init(pa_module *m) { goto fail; } + if (pa_modargs_get_value_boolean(ma, "low_device_latency", &low_device_latency) < 0) { + pa_log("Invalid boolean device latency parameter"); + goto fail; + } + m->userdata = u = pa_xnew0(struct userdata, 1); u->core = m->core; u->module = m; @@ -1196,6 +1211,7 @@ int pa__init(pa_module *m) { u->underrun_occured = false; u->extra_latency = 0; u->latency_error = 0; + u->low_device_latency = low_device_latency; /* The default adjust time is set to 1s. The adjust time given on the * command line is considered to be in seconds if it is below 100, -- 2.8.1