From 9710476c9670861c8fee8b84cab327974e90f7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 13 Aug 2025 13:57:17 +0300 Subject: [PATCH] transcriberbin: Don't keep state locked while querying upstream latency Part-of: --- video/closedcaption/src/transcriberbin/imp.rs | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/video/closedcaption/src/transcriberbin/imp.rs b/video/closedcaption/src/transcriberbin/imp.rs index 3ebaeeaf6..cd18e83e8 100644 --- a/video/closedcaption/src/transcriberbin/imp.rs +++ b/video/closedcaption/src/transcriberbin/imp.rs @@ -16,6 +16,7 @@ use std::collections::{HashMap, HashSet}; use std::sync::Mutex; use std::sync::LazyLock; +use std::sync::MutexGuard; use super::{CaptionSource, MuxMethod}; @@ -2175,19 +2176,21 @@ impl TranscriberBin { ); } - fn query_upstream_latency(&self, state: &State) -> gst::ClockTime { + fn query_upstream_latency(&self, state_guard: MutexGuard>) -> gst::ClockTime { + let state = state_guard.as_ref().unwrap(); let mut min = gst::ClockTime::from_seconds(0); - for pad in state - .audio_sink_pads - .values() - .map(|p| p.upcast_ref::()) - .chain( - [&self.video_sinkpad] - .iter() - .map(|p| p.upcast_ref::()), - ) - { + let pads = Iterator::chain( + state + .audio_sink_pads + .values() + .map(|p| p.clone().upcast::()), + std::iter::once(&self.video_sinkpad).map(|p| p.clone().upcast::()), + ) + .collect::>(); + drop(state_guard); + + for pad in pads { let mut upstream_query = gst::query::Latency::new(); if pad.peer_query(&mut upstream_query) { @@ -2270,11 +2273,12 @@ impl TranscriberBin { match query.view_mut() { QueryViewMut::Latency(q) => { - let state = self.state.lock().unwrap(); - if let Some(state) = state.as_ref() { - let upstream_min = self.query_upstream_latency(state); - let min = - upstream_min + self.our_latency(state, &self.settings.lock().unwrap()); + let state_guard = self.state.lock().unwrap(); + if let Some(ref state) = &*state_guard { + let our_latency = self.our_latency(state, &self.settings.lock().unwrap()); + let upstream_min = self.query_upstream_latency(state_guard); + + let min = upstream_min + our_latency; gst::debug!(CAT, imp = self, "calculated latency: {}", min);