From c539a5df0250aec711d1817ceaa9cf8c852adf02 Mon Sep 17 00:00:00 2001 From: Mikhail Baranov Date: Fri, 10 May 2024 16:20:00 +0200 Subject: [PATCH] Added start latency param #688 --- src/internal_modules/roc_audio/latency_tuner.h | 7 +++++++ .../roc_pipeline/receiver_session.cpp | 6 +++++- src/public_api/include/roc/config.h | 7 +++++++ src/public_api/src/adapters.cpp | 12 ++++++++++++ src/tools/roc_recv/cmdline.ggo | 3 +++ src/tools/roc_recv/main.cpp | 15 ++++++++++++++- 6 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/internal_modules/roc_audio/latency_tuner.h b/src/internal_modules/roc_audio/latency_tuner.h index 8a90f4b51..2bf0a75a1 100644 --- a/src/internal_modules/roc_audio/latency_tuner.h +++ b/src/internal_modules/roc_audio/latency_tuner.h @@ -81,6 +81,13 @@ struct LatencyConfig { //! Negative value is an error. core::nanoseconds_t target_latency; + //! Start latency. + //! @remarks + //! In case of dynamic latency the tuner will start from this value. + //! @note + //! This value makes sense only when target_latency is set to 0. + core::nanoseconds_t start_latency; + //! Minimum allowed latency. //! @remarks //! If the latency goes out of bounds, the session is terminated. diff --git a/src/internal_modules/roc_pipeline/receiver_session.cpp b/src/internal_modules/roc_pipeline/receiver_session.cpp index e567be435..edf7352d8 100644 --- a/src/internal_modules/roc_pipeline/receiver_session.cpp +++ b/src/internal_modules/roc_pipeline/receiver_session.cpp @@ -82,7 +82,11 @@ ReceiverSession::ReceiverSession( pkt_reader = filter_.get(); delayed_reader_.reset(new (delayed_reader_) packet::DelayedReader( - *pkt_reader, session_config.latency.target_latency, pkt_encoding->sample_spec)); + *pkt_reader, + session_config.latency.target_latency != 0 + ? session_config.latency.target_latency + : session_config.latency.start_latency, + pkt_encoding->sample_spec)); if (!delayed_reader_ || !delayed_reader_->is_valid()) { return; } diff --git a/src/public_api/include/roc/config.h b/src/public_api/include/roc/config.h index ffeec67df..2880de3b4 100644 --- a/src/public_api/include/roc/config.h +++ b/src/public_api/include/roc/config.h @@ -896,6 +896,13 @@ typedef struct roc_receiver_config { */ unsigned long long target_latency; + /** Start latency, in nanoseconds. + * + * If target latency is set to zero, and latency tuning is enabled, this value + * sets initial value of latency. + */ + unsigned long long start_latency; + /** Minimum allowed latency, in nanoseconds. * * How latency is calculated depends on \c latency_tuner_backend field. diff --git a/src/public_api/src/adapters.cpp b/src/public_api/src/adapters.cpp index 9a9b326d4..b80b71d46 100644 --- a/src/public_api/src/adapters.cpp +++ b/src/public_api/src/adapters.cpp @@ -173,6 +173,18 @@ bool receiver_config_from_user(node::Context&, (core::nanoseconds_t)in.target_latency; } + if (in.start_latency != 0) { + if (in.target_latency != 0) { + roc_log (LogError, + "bad configuration:" + " start latency must be 0 if latency tuning is disabled" + " (target_latency != 0)"); + return false; + } + out.session_defaults.latency.start_latency = + (core::nanoseconds_t)in.start_latency; + } + if (in.min_latency != 0) { out.session_defaults.latency.min_latency = (core::nanoseconds_t)in.min_latency; } diff --git a/src/tools/roc_recv/cmdline.ggo b/src/tools/roc_recv/cmdline.ggo index cf1844051..3259ea14f 100644 --- a/src/tools/roc_recv/cmdline.ggo +++ b/src/tools/roc_recv/cmdline.ggo @@ -32,6 +32,9 @@ section "Options" option "target-latency" - "Target latency, TIME units" string optional + option "start-latency" - "Start latency, target-latency must be 0 or unset, TIME units" + string optional + option "io-latency" - "Playback target latency, TIME units" string optional diff --git a/src/tools/roc_recv/main.cpp b/src/tools/roc_recv/main.cpp index 5d0a7a5ef..7f437244a 100644 --- a/src/tools/roc_recv/main.cpp +++ b/src/tools/roc_recv/main.cpp @@ -110,12 +110,25 @@ int main(int argc, char** argv) { roc_log(LogError, "invalid --target-latency: bad format"); return 1; } - if (receiver_config.session_defaults.latency.target_latency <= 0) { + if (receiver_config.session_defaults.latency.target_latency < 0) { roc_log(LogError, "invalid --target-latency: should be > 0"); return 1; } } + if (args.start_latency_given) { + if (!core::parse_duration( + args.start_latency_arg, + receiver_config.session_defaults.latency.start_latency)) { + roc_log(LogError, "invalid --start-latency: bad format"); + return 1; + } + if (receiver_config.session_defaults.latency.start_latency < 0) { + roc_log(LogError, "invalid --start-latency: should be >= 0"); + return 1; + } + } + if (args.min_latency_given || args.max_latency_given) { if (!args.min_latency_given || !args.max_latency_given) { roc_log(LogError,