From 798fff17722f30b8f5a86cef8493b37651d0c8f5 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Fri, 14 May 2021 17:14:34 +0200 Subject: [PATCH] Fix race on settings access --- src/imp.rs | 27 ++++++++++++++++----------- tests/flexhlssink.rs | 4 +--- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/imp.rs b/src/imp.rs index 385c01f..bffa5cc 100644 --- a/src/imp.rs +++ b/src/imp.rs @@ -71,7 +71,7 @@ enum State { playlist_index: u32, current_segment_file: Option, - current_running_time_start: Option, + fragment_opened_at: Option, current_segment_location: Option, }, } @@ -124,7 +124,7 @@ impl FlexHlsSink { playlist_render_state: PlaylistRenderState::Init, playlist_index: 0, current_segment_location: None, - current_running_time_start: None, + fragment_opened_at: None, current_segment_file: None, }; } @@ -210,7 +210,7 @@ impl FlexHlsSink { match &mut *state { State::Stopped => {} State::Started { - current_running_time_start, + fragment_opened_at, playlist, current_segment_location, playlist_render_state, @@ -223,8 +223,10 @@ impl FlexHlsSink { .as_ref() .ok_or_else(|| gst::StateChangeError)?; - let segment_duration = - fragment_closed_at - current_running_time_start.as_ref().unwrap(); + let segment_duration = fragment_closed_at + - fragment_opened_at + .as_ref() + .ok_or_else(|| gst::StateChangeError)?; playlist.segments.push(MediaSegment { uri: segment_location.to_string(), @@ -319,12 +321,15 @@ impl BinImpl for FlexHlsSink { match msg.view() { MessageView::Element(ref msg) => { - let settings = self.settings.lock().unwrap(); + let event_is_from_splitmuxsink = { + let settings = self.settings.lock().unwrap(); - if settings.splitmuxsink.is_some() - && msg.src().as_ref() - == Some(settings.splitmuxsink.as_ref().unwrap().upcast_ref()) - { + settings.splitmuxsink.is_some() + && msg.src().as_ref() + == Some(settings.splitmuxsink.as_ref().unwrap().upcast_ref()) + }; + + if event_is_from_splitmuxsink { let s = msg.structure().unwrap(); match s.name() { "splitmuxsink-fragment-opened" => { @@ -334,7 +339,7 @@ impl BinImpl for FlexHlsSink { match &mut *state { State::Stopped => return, State::Started { - current_running_time_start, + fragment_opened_at: current_running_time_start, .. } => *current_running_time_start = Some(fragment_opened_at), }; diff --git a/tests/flexhlssink.rs b/tests/flexhlssink.rs index 9f3a7f5..38df69d 100644 --- a/tests/flexhlssink.rs +++ b/tests/flexhlssink.rs @@ -29,7 +29,7 @@ fn init() { fn test_basic_element_with_video_content() { init(); - const BUFFER_NB: i32 = 200; + const BUFFER_NB: i32 = 600; let pipeline = gst::Pipeline::new(Some("video_pipeline")); @@ -44,8 +44,6 @@ fn test_basic_element_with_video_content() { let hls_queue = gst::ElementFactory::make("queue", Some("test_hls_queue")).unwrap(); let flexhlssink = gst::ElementFactory::make("flexhlssink", Some("test_flexhlssink")).unwrap(); flexhlssink.set_property("target-duration", &6u32).unwrap(); - // 4 - 271 - // 6 - 391 let app_queue = gst::ElementFactory::make("queue", Some("test_app_queue")).unwrap(); let app_sink = gst::ElementFactory::make("appsink", Some("test_sink")).unwrap();