Skip to content

🎥 Make videos programmatically with React

yes

Jonny Burger83fdb3132ee9
diff --git a/packages/compositor/rust/commands/mod.rs b/packages/compositor/rust/commands/mod.rs
index ebb5a84e13..be152a63f1 100644
--- a/packages/compositor/rust/commands/mod.rs
+++ b/packages/compositor/rust/commands/mod.rs
@@ -15,7 +15,7 @@ pub fn execute_command(
let current_maximum_cache_size = max_cache_size::get_instance().lock().unwrap().get_value();
if is_about_to_run_out_of_memory() && current_maximum_cache_size.is_some() {
- ffmpeg::emergency_memory_free_up(frame_cache_manager).unwrap();
+ ffmpeg::emergency_memory_free_up(frame_cache_manager, thread_index).unwrap();
max_cache_size::get_instance()
.lock()
.unwrap()
@@ -54,6 +54,7 @@ pub fn execute_command(
payload.remaining_bytes,
opened_video_manager,
frame_cache_manager,
+ thread_index,
)?;
Ok(vec![])
}
@@ -94,6 +95,7 @@ pub fn execute_command(
current_maximum_cache_size.unwrap(),
opened_video_manager,
frame_cache_manager,
+ thread_index,
)
.unwrap();
}
diff --git a/packages/compositor/rust/ffmpeg.rs b/packages/compositor/rust/ffmpeg.rs
index e19421a671..f09c82aed7 100644
--- a/packages/compositor/rust/ffmpeg.rs
+++ b/packages/compositor/rust/ffmpeg.rs
@@ -28,19 +28,25 @@ pub fn get_open_video_stats(
}
pub fn keep_only_latest_frames(
- maximum_frame_cache_size_in_bytes: u128,
+ maximum_frame_cache_size_in_bytes: u64,
frame_cache_manager: &mut FrameCacheManager,
+ thread_index: usize,
) -> Result<(), ErrorWithBacktrace> {
- frame_cache_manager.prune(maximum_frame_cache_size_in_bytes)?;
+ frame_cache_manager.prune(maximum_frame_cache_size_in_bytes, thread_index)?;
Ok(())
}
pub fn keep_only_latest_frames_and_close_videos(
- maximum_frame_cache_size_in_bytes: u128,
+ maximum_frame_cache_size_in_bytes: u64,
opened_video_manager: &mut OpenedVideoManager,
frame_cache_manager: &mut FrameCacheManager,
+ thread_index: usize,
) -> Result<(), ErrorWithBacktrace> {
- keep_only_latest_frames(maximum_frame_cache_size_in_bytes, frame_cache_manager)?;
+ keep_only_latest_frames(
+ maximum_frame_cache_size_in_bytes,
+ frame_cache_manager,
+ thread_index,
+ )?;
opened_video_manager.close_videos_if_cache_empty(frame_cache_manager)?;
@@ -49,9 +55,10 @@ pub fn keep_only_latest_frames_and_close_videos(
pub fn emergency_memory_free_up(
frame_cache_manager: &mut FrameCacheManager,
+ thread_index: usize,
) -> Result<(), ErrorWithBacktrace> {
_print_verbose("System is about to run out of memory, freeing up memory.")?;
- frame_cache_manager.halfen_cache_size()?;
+ frame_cache_manager.halfen_cache_size(thread_index)?;
Ok(())
}
@@ -62,7 +69,7 @@ pub fn extract_frame(
time: f64,
transparent: bool,
tone_mapped: bool,
- maximum_frame_cache_size_in_bytes: Option<u128>,
+ maximum_frame_cache_size_in_bytes: Option<u64>,
thread_index: usize,
manager: &mut OpenedVideoManager,
frame_cache_manager: &mut FrameCacheManager,
@@ -162,6 +169,7 @@ pub fn extract_frame(
maximum_frame_cache_size_in_bytes,
tone_mapped,
frame_cache_manager,
+ thread_index,
)?;
let from_cache = frame_cache_manager
diff --git a/packages/compositor/rust/frame_cache.rs b/packages/compositor/rust/frame_cache.rs
index 0fb7f7c39d..3f2b904e9f 100644
--- a/packages/compositor/rust/frame_cache.rs
+++ b/packages/compositor/rust/frame_cache.rs
@@ -5,6 +5,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use crate::{
errors::ErrorWithBacktrace,
global_printer::{_print_debug, _print_verbose},
+ max_cache_size,
opened_stream::get_time,
scalable_frame::ScalableFrame,
};
@@ -74,6 +75,10 @@ impl FrameCache {
}
pub fn add_item(&mut self, item: FrameCacheItem) {
+ max_cache_size::get_instance()
+ .lock()
+ .unwrap()
+ .add_to_current_cache_size(item.frame.get_size().into());
self.items.push(item);
}
@@ -128,6 +133,10 @@ impl FrameCache {
if self.last_frame.is_some() && id == self.last_frame.expect("last_frame") {
self.last_frame = None;
}
+ max_cache_size::get_instance()
+ .lock()
+ .unwrap()
+ .remove_from_cache_size(self.items[i].frame.get_size().into());
self.items.remove(i);
break;
}
@@ -143,8 +152,8 @@ impl FrameCache {
self.items.len()
}
- pub fn get_size_in_bytes(&self) -> u128 {
- let mut size: u128 = 0;
+ pub fn get_local_size_in_bytes(&self) -> u64 {
+ let mut size: u64 = 0;
for item in &self.items {
size += item.frame.get_size();
}
diff --git a/packages/compositor/rust/frame_cache_manager.rs b/packages/compositor/rust/frame_cache_manager.rs
index 63d812053a..6bce3328df 100644
--- a/packages/compositor/rust/frame_cache_manager.rs
+++ b/packages/compositor/rust/frame_cache_manager.rs
@@ -126,7 +126,7 @@ impl FrameCacheManager {
return Ok(vec);
}
- fn get_total_size(&mut self) -> Result<u128, ErrorWithBacktrace> {
+ fn get_total_size(&mut self) -> Result<u64, ErrorWithBacktrace> {
let mut total_size = 0;
let keys = self.cache.keys().cloned().collect::<Vec<String>>();
@@ -141,7 +141,7 @@ impl FrameCacheManager {
let original_src = self.cache.get(&src).unwrap().original_src.clone();
let lock = self.get_frame_cache(&src, &original_src, transparent, tone_mapped);
let frame_cache = lock.lock()?;
- total_size += frame_cache.get_size_in_bytes();
+ total_size += frame_cache.get_local_size_in_bytes();
}
}
@@ -150,7 +150,8 @@ impl FrameCacheManager {
pub fn prune(
&mut self,
- maximum_frame_cache_size_in_bytes: u128,
+ maximum_frame_cache_size_in_bytes: u64,
+ thread_index: usize,
) -> Result<(), ErrorWithBacktrace> {
let references = self.get_frame_references()?;
let mut sorted = references.clone();
@@ -178,9 +179,10 @@ impl FrameCacheManager {
if pruned > 0 {
_print_verbose(&format!(
- "Pruned {} to save memory, keeping {}. Total cache size: {}MB",
+ "Pruned {} to save memory, keeping {}. Cache size on thread {}: {}MB",
pruned,
self.get_frames_in_cache()?,
+ thread_index,
self.get_total_size()? / 1024 / 1024
))?;
}
@@ -189,9 +191,9 @@ impl FrameCacheManager {
}
// Should be called if system is about to run out of memory
- pub fn halfen_cache_size(&mut self) -> Result<(), ErrorWithBacktrace> {
+ pub fn halfen_cache_size(&mut self, thread_index: usize) -> Result<(), ErrorWithBacktrace> {
let current_cache_size = self.get_total_size()?;
- self.prune(current_cache_size / 2)
+ self.prune(current_cache_size / 2, thread_index)
}
pub fn get_frames_in_cache(&mut self) -> Result<usize, ErrorWithBacktrace> {
diff --git a/packages/compositor/rust/main.rs b/packages/compositor/rust/main.rs
index 96efed5b61..f231a04fda 100644
--- a/packages/compositor/rust/main.rs
+++ b/packages/compositor/rust/main.rs
@@ -85,7 +85,7 @@ pub fn parse_init_command(json: &str) -> Result<CliInputCommand, ErrorWithBacktr
fn start_long_running_process(
threads: usize,
- maximum_frame_cache_size_in_bytes: u128,
+ maximum_frame_cache_size_in_bytes: u64,
) -> Result<(), ErrorWithBacktrace> {
let mut thread_handles = vec![];
let mut send_handles = vec![];
diff --git a/packages/compositor/rust/max_cache_size.rs b/packages/compositor/rust/max_cache_size.rs
index e24efa497d..d962acc0c3 100644
--- a/packages/compositor/rust/max_cache_size.rs
+++ b/packages/compositor/rust/max_cache_size.rs
@@ -2,23 +2,34 @@ use lazy_static::lazy_static;
use std::sync::Mutex;
pub struct MaxCacheSize {
- value: Option<u128>,
+ max_cache_size: Option<u64>,
+ current_cache_size: i128,
}
impl MaxCacheSize {
// Private constructor
fn new() -> Self {
- MaxCacheSize { value: None }
+ MaxCacheSize {
+ max_cache_size: None,
+ current_cache_size: 0,
+ }
}
// Getter for the value
- pub fn get_value(&self) -> Option<u128> {
- self.value
+ pub fn get_value(&self) -> Option<u64> {
+ self.max_cache_size
}
// Setter for the value
- pub fn set_value(&mut self, value: Option<u128>) {
- self.value = value;
+ pub fn set_value(&mut self, value: Option<u64>) {
+ self.max_cache_size = value;
+ }
+
+ pub fn add_to_current_cache_size(&mut self, value: i128) {
+ self.current_cache_size += value;
+ }
+ pub fn remove_from_cache_size(&mut self, value: i128) {
+ self.current_cache_size -= value;
}
}
diff --git a/packages/compositor/rust/memory.rs b/packages/compositor/rust/memory.rs
index c7a4e178b8..012208c55a 100644
--- a/packages/compositor/rust/memory.rs
+++ b/packages/compositor/rust/memory.rs
@@ -35,7 +35,7 @@ pub fn is_about_to_run_out_of_memory() -> bool {
return get_available_memory() < 100 * 1024 * 1024;
}
-pub fn get_ideal_maximum_frame_cache_size() -> u128 {
+pub fn get_ideal_maximum_frame_cache_size() -> u64 {
let available_memory = get_available_memory();
// Take 40% of available memory
diff --git a/packages/compositor/rust/opened_stream.rs b/packages/compositor/rust/opened_stream.rs
index 4abbff8c1c..e69e33f492 100644
--- a/packages/compositor/rust/opened_stream.rs
+++ b/packages/compositor/rust/opened_stream.rs
@@ -88,11 +88,11 @@ impl OpenedStream {
Ok(Some(video)) => {
let frame_cache_id = get_frame_cache_id();
- let mut size: u128 = 0;
+ let mut size: u64 = 0;
let amount_of_planes = video.planes();
for i in 0..amount_of_planes {
- size += video.data(i).len() as u128;
+ size += video.data(i).len() as u64;
}
let frame = NotRgbFrame {
@@ -156,9 +156,10 @@ impl OpenedStream {
time_base: Rational,
one_frame_in_time_base: i64,
threshold: i64,
- maximum_frame_cache_size_in_bytes: Option<u128>,
+ maximum_frame_cache_size_in_bytes: Option<u64>,
tone_mapped: bool,
- frame_cache_manager: &mut FrameCacheManager
+ frame_cache_manager: &mut FrameCacheManager,
+ thread_index: usize,
) -> Result<usize, ErrorWithBacktrace> {
let mut freshly_seeked = false;
let position_to_seek_to = match self.duration_or_zero {
@@ -325,11 +326,11 @@ impl OpenedStream {
let frame_cache_id = get_frame_cache_id();
- let mut size: u128 = 0;
+ let mut size: u64 = 0;
let amount_of_planes = unfiltered.planes();
for i in 0..amount_of_planes {
- size += unfiltered.data(i).len() as u128;
+ size += unfiltered.data(i).len() as u64;
}
let frame = NotRgbFrame {
@@ -379,7 +380,7 @@ impl OpenedStream {
if items_in_loop % 10 == 0 {
match maximum_frame_cache_size_in_bytes {
Some(cache_size) => {
- ffmpeg::keep_only_latest_frames(cache_size, frame_cache_manager)?;
+ ffmpeg::keep_only_latest_frames(cache_size, frame_cache_manager, thread_index)?;
}
None => {}
}
diff --git a/packages/compositor/rust/payloads.rs b/packages/compositor/rust/payloads.rs
index 465b6eed52..ba1b37715a 100644
--- a/packages/compositor/rust/payloads.rs
+++ b/packages/compositor/rust/payloads.rs
@@ -57,7 +57,7 @@ pub mod payloads {
#[derive(Serialize, Deserialize, Debug)]
pub struct StartPayLoad {
pub concurrency: usize,
- pub maximum_frame_cache_size_in_bytes: Option<u128>,
+ pub maximum_frame_cache_size_in_bytes: Option<u64>,
pub verbose: bool,
}
@@ -225,7 +225,7 @@ pub mod payloads {
#[derive(Serialize, Deserialize, Debug)]
pub struct FreeUpMemory {
- pub remaining_bytes: u128,
+ pub remaining_bytes: u64,
}
#[derive(Serialize, Deserialize, Debug)]
diff --git a/packages/compositor/rust/scalable_frame.rs b/packages/compositor/rust/scalable_frame.rs
index 3e5a5595a6..c4aa62fc43 100644
--- a/packages/compositor/rust/scalable_frame.rs
+++ b/packages/compositor/rust/scalable_frame.rs
@@ -30,7 +30,7 @@ pub struct NotRgbFrame {
pub rotate: Rotate,
pub original_src: String,
pub unscaled_frame: Video,
- pub size: u128,
+ pub size: u64,
pub tone_mapped: bool,
pub filter_graph: FilterGraph,
pub colorspace: color::Space,
@@ -139,12 +139,12 @@ impl ScalableFrame {
}
}
- pub fn get_size(&self) -> u128 {
- let mut size: u128 = 0;
+ pub fn get_size(&self) -> u64 {
+ let mut size: u64 = 0;
match self.rgb_frame {
None => {}
Some(ref frame) => {
- size += frame.data.len() as u128;
+ size += frame.data.len() as u64;
}
}
match self.native_frame {