From fb192806333ca88d5acb53b849f268611773230d Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 15 Nov 2024 20:38:43 -0500 Subject: Fix Jack latency recomputation when plugin latency changes Paul Davis says "jack_recompute_total_latencies() is a server call. It is not legal to make server calls from within a server callback (like the process callback)." --- src/jack.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src/jack.c') diff --git a/src/jack.c b/src/jack.c index cf633f9..ea37ac5 100644 --- a/src/jack.c +++ b/src/jack.c @@ -1,4 +1,4 @@ -// Copyright 2007-2022 David Robillard +// Copyright 2007-2024 David Robillard // SPDX-License-Identifier: ISC #include "backend.h" @@ -46,6 +46,9 @@ struct JalvBackendImpl { bool is_internal_client; ///< Running inside jackd }; +/// Maximum supported latency in frames (at most 2^24 so all integers work) +static const float max_latency = 16777216.0f; + /// Internal Jack client initialization entry point int jack_initialize(jack_client_t* client, const char* load_init); @@ -201,9 +204,19 @@ jack_process_cb(jack_nframes_t nframes, void* data) struct Port* const port = &jalv->ports[p]; if (port->flow == FLOW_OUTPUT && port->type == TYPE_CONTROL && port->reports_latency) { - if (jalv->plugin_latency != port->control) { - jalv->plugin_latency = port->control; - jack_recompute_total_latencies(client); + // Get the latency in frames from the control output truncated to integer + const float value = port->control; + const uint32_t frames = + (value >= 0.0f && value <= max_latency) ? (uint32_t)value : 0U; + + if (jalv->plugin_latency != frames) { + // Update the cached value and notify the UI if the latency changed + jalv->plugin_latency = frames; + + const JalvLatencyChange body = {frames}; + const JalvMessageHeader header = {LATENCY_CHANGE, sizeof(body)}; + jalv_write_split_message( + jalv->plugin_to_ui, &header, sizeof(header), &body, sizeof(body)); } } else if (port->flow == FLOW_OUTPUT && port->type == TYPE_EVENT) { void* buf = NULL; @@ -461,6 +474,12 @@ jalv_backend_activate_port(Jalv* jalv, uint32_t port_index) #endif } +void +jalv_backend_recompute_latencies(Jalv* const jalv) +{ + jack_recompute_total_latencies(jalv->backend->client); +} + int jack_initialize(jack_client_t* const client, const char* const load_init) { -- cgit v1.2.1