Skip to main content

measure_round_trip_latency

Function measure_round_trip_latency 

Source
pub fn measure_round_trip_latency(
    audio_service: State<'_, Mutex<AudioService>>,
) -> Result<RoundTripLatencyDto, String>
Expand description

Measures true end-to-end round-trip latency using dedicated CPAL streams.

This is the only latency command that performs a real hardware measurement rather than an analytical estimate. The procedure is:

  1. The AudioService mutex is locked just long enough to clone the handler arc, then released so the main loopback and UI remain unblocked.
  2. A dedicated OS thread is spawned that calls AudioLatencyMeasurementService::measure_round_trip_latency, which in turn opens its own CPAL input/output streams, runs calibration and impulse detection, and returns the averaged result.
  3. The command blocks until the thread finishes (typically 3–15 s depending on warmup and timeout settings), then logs and returns the result.

§Physical requirement

The audio interface output must be physically (or virtually) connected back to its input. Without this loopback the impulse can never be detected and the measurement will time out.

§Returns

Ok(result) — a RoundTripLatencyDto with:

  • is_valid = true and latency_ms set on success.
  • is_valid = false and a human-readable error on failure (e.g. timeout or no echo).

§Errors

Returns Err only if the AudioService mutex cannot be locked or the measurement thread panics unexpectedly.