Skip to content

🎥 Make videos programmatically with React

`@remotion/compositor`: Rework concurrency model

Jonny Burgera64ff06e17b6
diff --git a/packages/compositor/rust/main.rs b/packages/compositor/rust/main.rs
index f231a04fda..a317efc6f3 100644
--- a/packages/compositor/rust/main.rs
+++ b/packages/compositor/rust/main.rs
@@ -15,6 +15,7 @@ mod opened_video_manager;
mod payloads;
mod rotation;
mod scalable_frame;
+mod select_right_thread;
mod tone_map;
use commands::execute_command;
@@ -89,6 +90,7 @@ fn start_long_running_process(
) -> Result<(), ErrorWithBacktrace> {
let mut thread_handles = vec![];
let mut send_handles = vec![];
+ let mut thread_map = select_right_thread::ThreadMap::new(threads);
for thread_index in 0..threads {
let (tx, rx) = mpsc::channel::<CliInputCommand>();
@@ -155,8 +157,14 @@ fn start_long_running_process(
}
let opts: CliInputCommand = parse_cli(&input)?;
- // TODO: Find best thread to send to
- send_handles[0].send(opts).unwrap();
+ let opts_again: CliInputCommand = parse_cli(&input)?;
+ let thread_id = match opts.payload {
+ CliInputCommandPayload::ExtractFrame(command) => thread_map
+ .select_right_thread(&command.src, command.time)
+ .unwrap(),
+ _ => 0,
+ };
+ send_handles[thread_id].send(opts_again).unwrap();
}
for handle in thread_handles {
diff --git a/packages/compositor/rust/opened_stream.rs b/packages/compositor/rust/opened_stream.rs
index e69e33f492..1aa5519cd4 100644
--- a/packages/compositor/rust/opened_stream.rs
+++ b/packages/compositor/rust/opened_stream.rs
@@ -322,7 +322,7 @@ impl OpenedStream {
match result {
Ok(Some(unfiltered)) => {
- _print_verbose(&format!("received frame {}", tone_mapped))?;
+ _print_verbose(&format!("Thread {} - received frame, tone_mapped ={}", thread_index, tone_mapped))?;
let frame_cache_id = get_frame_cache_id();
diff --git a/packages/compositor/rust/select_right_thread.rs b/packages/compositor/rust/select_right_thread.rs
new file mode 100644
index 0000000000..a1c482a55c
--- /dev/null
+++ b/packages/compositor/rust/select_right_thread.rs
@@ -0,0 +1,100 @@
+use crate::{errors::ErrorWithBacktrace, global_printer::_print_verbose};
+
+struct OpenStream {
+ src: String,
+ last_time: f64,
+}
+
+struct ThreadStreams {
+ streams: Vec<OpenStream>,
+}
+
+pub struct ThreadMap {
+ map: Vec<Option<ThreadStreams>>,
+ threads: usize,
+}
+
+impl ThreadMap {
+ pub fn new(t: usize) -> Self {
+ let mut map = ThreadMap {
+ map: Vec::new(),
+ threads: t,
+ };
+ for _ in 0..t {
+ map.map.push(None);
+ }
+ map
+ }
+
+ pub fn select_right_thread(
+ &mut self,
+ src: &String,
+ time: f64,
+ ) -> Result<usize, ErrorWithBacktrace> {
+ for thread_id in 0..self.map.len() {
+ let thread_streams = self.map[thread_id].as_mut();
+ if thread_streams.is_none() {
+ continue;
+ }
+ for stream in &mut thread_streams.unwrap().streams {
+ if &stream.src == src {
+ if (stream.last_time - time).abs() <= 10.0 {
+ stream.last_time = time;
+ _print_verbose(&format!(
+ "Reusing thread {} for stream {} at time {}",
+ thread_id, src, time
+ ))?;
+ return Ok(thread_id);
+ }
+ }
+ }
+ }
+
+ let mut min_streams = usize::MAX;
+ let mut selected_thread = None;
+
+ for thread_id in 0..self.map.len() {
+ let thread_streams = self.map[thread_id].as_mut();
+ if thread_streams.is_none() {
+ continue;
+ }
+ let unwrapped = thread_streams.unwrap();
+
+ if unwrapped.streams.is_empty() {
+ selected_thread = Some(thread_id);
+ break;
+ } else if unwrapped.streams.len() < min_streams {
+ min_streams = unwrapped.streams.len();
+ selected_thread = Some(thread_id);
+ }
+ }
+
+ let new_thread_id = match self.map.iter().position(|x| x.is_none()) {
+ Some(thread_id) => thread_id,
+ None => match selected_thread {
+ Some(x) => x,
+ None => self.map.len() % self.threads,
+ },
+ };
+
+ if self.map[new_thread_id].is_none() {
+ self.map[new_thread_id] = Some(ThreadStreams {
+ streams: Vec::new(),
+ });
+ }
+
+ let new_thread_streams = self.map[new_thread_id].as_mut().unwrap();
+
+ new_thread_streams.streams.push(OpenStream {
+ src: src.clone(),
+ last_time: time,
+ });
+
+ _print_verbose(&format!(
+ "Adding stream {} to thread {}",
+ src, new_thread_id
+ ))?;
+
+ Ok(new_thread_id)
+ }
+}