Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Make SwscaleConverter thread-safe using thread_local context

Richie McIlroy0b99ec7f2c52
diff --git a/Cargo.lock b/Cargo.lock
index f817ede7a..d2f861963 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1428,6 +1428,7 @@ dependencies = [
"flume",
"parking_lot",
"thiserror 1.0.69",
+ "thread_local",
"tracing",
"windows 0.60.0",
"workspace-hack",
diff --git a/crates/frame-converter/Cargo.toml b/crates/frame-converter/Cargo.toml
index 39b4e7ce7..3d363ef70 100644
--- a/crates/frame-converter/Cargo.toml
+++ b/crates/frame-converter/Cargo.toml
@@ -14,6 +14,7 @@ thiserror.workspace = true
tracing.workspace = true
flume.workspace = true
crossbeam-channel = "0.5"
+thread_local = "1.1"
parking_lot = "0.12"
workspace-hack = { version = "0.1", path = "../workspace-hack" }
diff --git a/crates/frame-converter/src/swscale.rs b/crates/frame-converter/src/swscale.rs
index eb07b0d7a..1d5f72041 100644
--- a/crates/frame-converter/src/swscale.rs
+++ b/crates/frame-converter/src/swscale.rs
@@ -1,9 +1,25 @@
use crate::{ConversionConfig, ConvertError, ConverterBackend, FrameConverter};
use ffmpeg::{format::Pixel, frame, software::scaling};
-use parking_lot::Mutex;
+use std::cell::UnsafeCell;
+use thread_local::ThreadLocal;
+
+struct SendableContext(UnsafeCell<scaling::Context>);
+
+unsafe impl Send for SendableContext {}
+
+impl SendableContext {
+ fn new(ctx: scaling::Context) -> Self {
+ Self(UnsafeCell::new(ctx))
+ }
+
+ fn run(&self, input: &frame::Video, output: &mut frame::Video) -> Result<(), ffmpeg::Error> {
+ unsafe { (*self.0.get()).run(input, output) }
+ }
+}
pub struct SwscaleConverter {
- context: Mutex<scaling::Context>,
+ contexts: ThreadLocal<SendableContext>,
+ config: ConversionConfig,
output_format: Pixel,
output_width: u32,
output_height: u32,
@@ -11,13 +27,25 @@ pub struct SwscaleConverter {
impl SwscaleConverter {
pub fn new(config: ConversionConfig) -> Result<Self, ConvertError> {
+ Self::create_context(&config)?;
+
+ Ok(Self {
+ contexts: ThreadLocal::new(),
+ output_format: config.output_format,
+ output_width: config.output_width,
+ output_height: config.output_height,
+ config,
+ })
+ }
+
+ fn create_context(config: &ConversionConfig) -> Result<scaling::Context, ConvertError> {
let flags = if config.needs_scaling() {
scaling::flag::Flags::BICUBIC
} else {
scaling::flag::Flags::FAST_BILINEAR
};
- let context = scaling::Context::get(
+ scaling::Context::get(
config.input_format,
config.input_width,
config.input_height,
@@ -26,14 +54,12 @@ impl SwscaleConverter {
config.output_height,
flags,
)
- .map_err(|_| ConvertError::UnsupportedFormat(config.input_format, config.output_format))?;
+ .map_err(|_| ConvertError::UnsupportedFormat(config.input_format, config.output_format))
+ }
- Ok(Self {
- context: Mutex::new(context),
- output_format: config.output_format,
- output_width: config.output_width,
- output_height: config.output_height,
- })
+ fn get_or_create_context(&self) -> Result<&SendableContext, ConvertError> {
+ self.contexts
+ .get_or_try(|| Self::create_context(&self.config).map(SendableContext::new))
}
}
@@ -43,8 +69,8 @@ impl FrameConverter for SwscaleConverter {
let mut output =
frame::Video::new(self.output_format, self.output_width, self.output_height);
- self.context
- .lock()
+ let context = self.get_or_create_context()?;
+ context
.run(&input, &mut output)
.map_err(|e| ConvertError::ConversionFailed(e.to_string()))?;