From 03787ef857809563584adff568cfdb23999e0b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 14 Feb 2022 14:36:50 +0200 Subject: [PATCH] rav1eenc: Use `AtomicRefCell` instead of `Mutex` for the state It's only accessed from the streaming thread. --- video/rav1e/Cargo.toml | 1 + video/rav1e/src/rav1enc/imp.rs | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/video/rav1e/Cargo.toml b/video/rav1e/Cargo.toml index b1a49c3d..0e7654ea 100644 --- a/video/rav1e/Cargo.toml +++ b/video/rav1e/Cargo.toml @@ -13,6 +13,7 @@ gst = { package = "gstreamer", git = "https://gitlab.freedesktop.org/gstreamer/g gst-video = { package = "gstreamer-video", git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs", features = ["v1_12"] } rav1e = { version = "0.5", default-features = false } once_cell = "1.0" +atomic_refcell = "0.1" [dev-dependencies] gst-check = { package = "gstreamer-check", git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs" } diff --git a/video/rav1e/src/rav1enc/imp.rs b/video/rav1e/src/rav1enc/imp.rs index 7c832ce1..3de1873c 100644 --- a/video/rav1e/src/rav1enc/imp.rs +++ b/video/rav1e/src/rav1enc/imp.rs @@ -8,6 +8,7 @@ // // SPDX-License-Identifier: MIT/Apache-2.0 +use atomic_refcell::AtomicRefCell; use gst::glib; use gst::gst_debug; use gst::subclass::prelude::*; @@ -220,7 +221,7 @@ struct State { #[derive(Default)] pub struct Rav1Enc { - state: Mutex>, + state: AtomicRefCell>, settings: Mutex, } @@ -608,7 +609,7 @@ impl ElementImpl for Rav1Enc { impl VideoEncoderImpl for Rav1Enc { fn stop(&self, _element: &Self::Type) -> Result<(), gst::ErrorMessage> { - *self.state.lock().unwrap() = None; + *self.state.borrow_mut() = None; Ok(()) } @@ -860,7 +861,7 @@ impl VideoEncoderImpl for Rav1Enc { let container_sequence_header = gst::Buffer::from_mut_slice(context.container_sequence_header()); - *self.state.lock().unwrap() = Some(State { + *self.state.borrow_mut() = Some(State { context, video_info, }); @@ -885,7 +886,7 @@ impl VideoEncoderImpl for Rav1Enc { fn flush(&self, element: &Self::Type) -> bool { gst_debug!(CAT, obj: element, "Flushing"); - let mut state_guard = self.state.lock().unwrap(); + let mut state_guard = self.state.borrow_mut(); if let Some(ref mut state) = *state_guard { state.context.flush(); while let Ok(_) | Err(data::EncoderStatus::Encoded) = state.context.receive_packet() { @@ -899,7 +900,7 @@ impl VideoEncoderImpl for Rav1Enc { fn finish(&self, element: &Self::Type) -> Result { gst_debug!(CAT, obj: element, "Finishing"); - let mut state_guard = self.state.lock().unwrap(); + let mut state_guard = self.state.borrow_mut(); if let Some(ref mut state) = *state_guard { state.context.flush(); self.output_frames(element, state)?; @@ -913,7 +914,7 @@ impl VideoEncoderImpl for Rav1Enc { element: &Self::Type, frame: gst_video::VideoCodecFrame, ) -> Result { - let mut state_guard = self.state.lock().unwrap(); + let mut state_guard = self.state.borrow_mut(); let state = state_guard.as_mut().ok_or(gst::FlowError::NotNegotiated)?; self.output_frames(element, state)?;