🎥 Make videos programmatically with React
message queue
Jonny Burger95e70c54423a
diff --git a/packages/Cargo.lock b/packages/Cargo.lockindex 05e2bbd0cd..e8b32ddd6f 100644--- a/packages/Cargo.lock+++ b/packages/Cargo.lock@@ -365,6 +365,7 @@ dependencies = ["png","serde","serde_json",+ "threadpool",][[package]]@@ -433,6 +434,15 @@ dependencies = ["unicode-ident",]+[[package]]+name = "threadpool"+version = "1.8.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa"+dependencies = [+ "num_cpus",+]+[[package]]name = "unicode-ident"version = "1.0.5"diff --git a/packages/Cargo.toml b/packages/Cargo.tomlindex 2fa1bfe904..4bf094b374 100644--- a/packages/Cargo.toml+++ b/packages/Cargo.toml@@ -13,6 +13,7 @@ serde = {version = "1.0.151", features = ["derive"]}serde_json = "1.0.89"jpeg-decoder = "0.3"jpeg-encoder = "0.5.1"+threadpool = "1.8.1"ffmpeg-next = {git = "https://github.com/JonnyBurger/rust-ffmpeg", rev ="1809209407dc3a515e9ec2b5864ad0e0b3998cd0"}[[bin]]diff --git a/packages/compositor-darwin-arm64/compositor b/packages/compositor-darwin-arm64/compositorindex 3c244927f6..29ef3afbff 100755Binary files a/packages/compositor-darwin-arm64/compositor and b/packages/compositor-darwin-arm64/compositor differdiff --git a/packages/renderer/rust/errors.rs b/packages/renderer/rust/errors.rsindex 7003e7d3fb..49c43c50f5 100644--- a/packages/renderer/rust/errors.rs+++ b/packages/renderer/rust/errors.rs@@ -1,5 +1,6 @@use crate::payloads::payloads::ErrorPayload;use ffmpeg_next as remotionffmepg;+use std::any::Any;use std::backtrace::Backtrace;fn error_to_string(err: PossibleErrors) -> String {@@ -10,6 +11,7 @@ fn error_to_string(err: PossibleErrors) -> String {PossibleErrors::DecodingError(err) => err.to_string(),PossibleErrors::JpegDecoderError(err) => err.to_string(),PossibleErrors::SerdeError(err) => err.to_string(),+ PossibleErrors::WorkerError(err) => format!("{:?}", err),}}@@ -31,6 +33,13 @@ pub enum PossibleErrors {DecodingError(png::DecodingError),JpegDecoderError(jpeg_decoder::Error),SerdeError(serde_json::Error),+ WorkerError(Box<dyn Any + Send>),+}++impl From<Box<dyn Any + Send>> for PossibleErrors {+ fn from(err: Box<dyn Any + Send>) -> PossibleErrors {+ PossibleErrors::WorkerError(err)+ }}impl From<remotionffmepg::Error> for PossibleErrors {@@ -68,3 +77,17 @@ impl From<jpeg_decoder::Error> for PossibleErrors {PossibleErrors::JpegDecoderError(err)}}++impl std::fmt::Debug for PossibleErrors {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ match self {+ PossibleErrors::IoError(err) => write!(f, "IoError: {:?}", err),+ PossibleErrors::FfmpegError(err) => write!(f, "FfmpegError: {:?}", err),+ PossibleErrors::TryFromIntError(err) => write!(f, "TryFromIntError: {:?}", err),+ PossibleErrors::DecodingError(err) => write!(f, "DecodingError: {:?}", err),+ PossibleErrors::JpegDecoderError(err) => write!(f, "JpegDecoderError: {:?}", err),+ PossibleErrors::SerdeError(err) => write!(f, "SerdeError: {:?}", err),+ PossibleErrors::WorkerError(err) => write!(f, "WorkerError: {:?}", err),+ }+ }+}diff --git a/packages/renderer/rust/main.rs b/packages/renderer/rust/main.rsindex 557cf25c40..894454fd73 100644--- a/packages/renderer/rust/main.rs+++ b/packages/renderer/rust/main.rs@@ -6,6 +6,9 @@ mod image;mod payloads;use commands::execute_command;use errors::PossibleErrors;+use std::sync::mpsc;+use std::thread;+use threadpool::ThreadPool;use payloads::payloads::{parse_cli, CliInputCommand};use std::io::Read;@@ -21,7 +24,54 @@ fn read_stdin_to_string() -> Result<String, std::io::Error> {fn mainfn() -> Result<(), PossibleErrors> {let input = read_stdin_to_string()?;let opts: CliInputCommand = parse_cli(&input)?;- execute_command(opts)?;+ match opts {+ CliInputCommand::StartLongRunningProcess(_) => {+ start_long_running_process()?;+ }+ _ => {+ execute_command(opts)?;+ }+ }++ Ok(())+}++fn start_long_running_process() -> Result<(), PossibleErrors> {+ let (command_tx, command_rx) = mpsc::channel::<String>();++ let pool = ThreadPool::new(4); // Create a thread pool with 4 threads++ // Read messages from stdin in a separate thread++ let thread_handle = thread::spawn(move || loop {+ let mut input = String::new();+ std::io::stdin().read_line(&mut input).unwrap();+ input = input.trim().to_string();+ if input == "EOF" {+ break;+ }+ command_tx.send(input).unwrap();+ });++ let worker_queue = thread::spawn(move || loop {+ let message = match command_rx.recv() {+ Ok(message) => message,+ Err(_) => {+ break;+ }+ };+ if message == "EOF" {+ break;+ }++ pool.execute(move || {+ let opts: CliInputCommand = parse_cli(&message).unwrap();+ execute_command(opts).unwrap();+ });+ });++ worker_queue.join()?;+ thread_handle.join()?;Ok(())}diff --git a/packages/renderer/src/compositor/compositor.ts b/packages/renderer/src/compositor/compositor.tsindex ed1881d27f..f859b46a94 100644--- a/packages/renderer/src/compositor/compositor.ts+++ b/packages/renderer/src/compositor/compositor.ts@@ -39,7 +39,7 @@ export const waitForCompositorWithIdToQuit = (renderId: string) => {return compositorMap[renderId].waitForDone();};-export const startCompositor = (payload: CliInputCommand) => {+export const startCompositor = (payload: CliInputCommand): Compositor => {const bin = getExecutablePath('compositor');const child = spawn(bin, dynamicLibraryPathOptions());const stderrChunks: Buffer[] = [];@@ -58,5 +58,13 @@ export const startCompositor = (payload: CliInputCommand) => {});});},+ finishCommands: () => {+ child.stdin.write('EOF\n');+ },++ executeCommand: (command: CompositorCommand) => {+ child.stdin.write(JSON.stringify(command) + '\n');+ return Promise.resolve();+ },};};