Skip to content

Commit

Permalink
previous behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyBurger committed Feb 12, 2025
1 parent 19b21d0 commit 64e8e3f
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 264 deletions.
41 changes: 1 addition & 40 deletions packages/compositor/rust/errors.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use crate::frame_cache::FrameCache;
use crate::opened_stream::OpenedStream;
use crate::opened_video::OpenedVideo;
use crate::opened_video_manager::OpenedVideoManager;
use crate::payloads::payloads::{CliInputCommand, ErrorPayload};
use ffmpeg_next as remotionffmpeg;
use png::EncodingError;
use std::any::Any;
use std::backtrace::Backtrace;
use std::collections::HashMap;
use std::sync::{
mpsc, Mutex, MutexGuard, PoisonError, RwLockReadGuard, RwLockWriteGuard, TryLockError,
};
use std::sync::{mpsc, Mutex, MutexGuard, PoisonError, RwLockReadGuard};

pub fn error_to_string(err: &ErrorWithBacktrace) -> String {
match &err.error {
Expand Down Expand Up @@ -186,12 +183,6 @@ impl From<PoisonError<MutexGuard<'_, FrameCache>>> for ErrorWithBacktrace {
}
}

impl From<PoisonError<MutexGuard<'_, OpenedVideo>>> for ErrorWithBacktrace {
fn from(err: PoisonError<MutexGuard<'_, OpenedVideo>>) -> ErrorWithBacktrace {
create_error_with_backtrace(err)
}
}

impl From<PoisonError<MutexGuard<'_, OpenedStream>>> for ErrorWithBacktrace {
fn from(err: PoisonError<MutexGuard<'_, OpenedStream>>) -> ErrorWithBacktrace {
create_error_with_backtrace(err)
Expand All @@ -203,36 +194,6 @@ impl From<PoisonError<MutexGuard<'_, OpenedVideoManager>>> for ErrorWithBacktrac
}
}

impl From<PoisonError<RwLockReadGuard<'_, HashMap<std::string::String, Mutex<OpenedVideo>>>>>
for ErrorWithBacktrace
{
fn from(
err: PoisonError<RwLockReadGuard<'_, HashMap<std::string::String, Mutex<OpenedVideo>>>>,
) -> ErrorWithBacktrace {
ErrorWithBacktrace::from(err.to_string())
}
}

impl From<PoisonError<RwLockWriteGuard<'_, HashMap<std::string::String, Mutex<OpenedVideo>>>>>
for ErrorWithBacktrace
{
fn from(
err: PoisonError<RwLockWriteGuard<'_, HashMap<std::string::String, Mutex<OpenedVideo>>>>,
) -> ErrorWithBacktrace {
ErrorWithBacktrace::from(err.to_string())
}
}

impl From<TryLockError<RwLockReadGuard<'_, HashMap<std::string::String, Mutex<OpenedVideo>>>>>
for ErrorWithBacktrace
{
fn from(
err: TryLockError<RwLockReadGuard<'_, HashMap<std::string::String, Mutex<OpenedVideo>>>>,
) -> ErrorWithBacktrace {
ErrorWithBacktrace::from(err.to_string())
}
}

impl From<PoisonError<RwLockReadGuard<'_, HashMap<std::string::String, Mutex<FrameCache>>>>>
for ErrorWithBacktrace
{
Expand Down
99 changes: 28 additions & 71 deletions packages/compositor/rust/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ pub fn get_open_video_stats(
frame_cache_manager: &mut FrameCacheManager,
manager: &OpenedVideoManager,
) -> Result<OpenVideoStats, ErrorWithBacktrace> {
let open_videos = manager.get_open_videos()?;
let open_streams = manager.get_open_video_streams()?;
let frames_in_cache = frame_cache_manager.get_frames_in_cache()?;

Ok(OpenVideoStats {
open_videos,
open_streams,
frames_in_cache,
})
Expand Down Expand Up @@ -69,8 +67,32 @@ pub fn extract_frame(
manager: &mut OpenedVideoManager,
frame_cache_manager: &mut FrameCacheManager,
) -> Result<Vec<u8>, ErrorWithBacktrace> {
let video_locked = manager.get_video(&src, &original_src, transparent)?;
let mut vid = video_locked.lock()?;
// Don't allow previous frame, but allow for some flexibility
let cache_item = match manager.get_position_and_threshold_of_video(time, &src) {
Ok(Some((position, threshold))) => frame_cache_manager.get_cache_item_id(
&src,
&original_src,
transparent,
tone_mapped,
position,
threshold - 1,
),
Ok(None) => Ok(None),
Err(err) => return Err(err),
}?;

if cache_item.is_some() {
return Ok(frame_cache_manager.get_cache_item_from_id(
&src,
&original_src,
transparent,
tone_mapped,
cache_item.unwrap(),
)?);
}

let vid_index = manager.get_video_index(&src, &original_src, transparent, time)?;
let vid = manager.get_video(vid_index)?;
// The requested position in the video.
let position = calc_position(time, vid.time_base);

Expand All @@ -87,75 +109,10 @@ pub fn extract_frame(
false => one_frame_in_time_base,
};

// Don't allow previous frame, but allow for some flexibility
let cache_item = frame_cache_manager.get_cache_item_id(
&src,
&original_src,
transparent,
tone_mapped,
position,
threshold - 1,
);

match cache_item {
Ok(Some(item)) => {
return Ok(frame_cache_manager.get_cache_item_from_id(
&src,
&original_src,
transparent,
tone_mapped,
item,
)?);
}
Ok(None) => {}
Err(err) => {
return Err(err);
}
}

let open_stream_count = vid.opened_streams.len();
let mut suitable_open_stream: Option<usize> = None;

// Seeking too far back in a stream is not efficient, rather open a new stream
// 15 seconds was chosen arbitrarily
let max_stream_position = calc_position(time + 5.0, vid.time_base);
let min_stream_position = calc_position(time - 5.0, vid.time_base);
for i in 0..open_stream_count {
let stream = vid.opened_streams[i].lock()?;
if stream.reached_eof {
continue;
}
if transparent != stream.transparent {
continue;
}
if stream.last_position.unwrap_or(0) > max_stream_position {
continue;
}
if stream.last_position.unwrap_or(0) < min_stream_position {
continue;
}
suitable_open_stream = Some(i);
break;
}

let stream_index = match suitable_open_stream {
Some(index) => Ok(index),
None => vid.open_new_stream(transparent, thread_index),
};

let opened_stream = match vid.opened_streams.get(stream_index?) {
Some(stream) => stream,
None => Err(std::io::Error::new(
ErrorKind::Other,
"Stream index out of bounds",
))?,
};

let mut first_opened_stream = opened_stream.lock()?;

let time_base = vid.time_base;

let frame_id = first_opened_stream.get_frame(
let frame_id = manager.get_frame_id(
vid_index,
time,
position,
time_base,
Expand Down
4 changes: 4 additions & 0 deletions packages/compositor/rust/frame_cache_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl FrameCacheManager {
self.cache.remove(src);
}

pub fn remove_all(&mut self) {
self.cache.clear();
}

fn add_frame_cache(&mut self, src: &str, original_src: &str) {
let frame_cache_and_original_src = FrameCacheAndOriginalSource {
transparent_original: Mutex::new(FrameCache::new()),
Expand Down
1 change: 0 additions & 1 deletion packages/compositor/rust/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ impl LongRunningProcess {
let concated: OpenVideoStats = OpenVideoStats {
frames_in_cache: open_video_stats_all.iter().map(|x| x.frames_in_cache).sum(),
open_streams: open_video_stats_all.iter().map(|x| x.open_streams).sum(),
open_videos: open_video_stats_all.iter().map(|x| x.open_videos).sum(),
};
let str = serde_json::to_string(&concated)?;
global_printer::synchronized_write_buf(0, &nonce, &str.as_bytes())?;
Expand Down
11 changes: 8 additions & 3 deletions packages/compositor/rust/opened_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct OpenedStream {
pub transparent: bool,
pub rotation: Rotate,
pub filter_graph: FilterGraph,
pub time_base: Rational,
pub fps: Rational
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -151,6 +153,7 @@ impl OpenedStream {

pub fn get_frame(
&mut self,
stream_index: usize,
time: f64,
target_position: i64,
time_base: Rational,
Expand Down Expand Up @@ -322,7 +325,7 @@ impl OpenedStream {

match result {
Ok(Some(unfiltered)) => {
_print_verbose(&format!("Thread {} - received frame, tone_mapped ={}", thread_index, tone_mapped))?;
_print_verbose(&format!("Thread {}, stream {} - received frame, tone_mapped = {}", thread_index, stream_index, tone_mapped))?;

let frame_cache_id = get_frame_cache_id();

Expand Down Expand Up @@ -469,7 +472,7 @@ pub fn open_stream(
src: &str,
original_src: &str,
transparent: bool,
) -> Result<(OpenedStream, Rational, Rational), ErrorWithBacktrace> {
) -> Result<OpenedStream, ErrorWithBacktrace> {
let mut dictionary = Dictionary::new();
dictionary.set("fflags", "+genpts");
let mut input = remotionffmpeg::format::input_with_dictionary(&src, dictionary)?;
Expand Down Expand Up @@ -600,7 +603,9 @@ pub fn open_stream(
rotation: rotate,
original_src: original_src.to_string(),
filter_graph,
fps,
time_base
};

Ok((opened_stream, fps, time_base))
Ok(opened_stream)
}
Loading

0 comments on commit 64e8e3f

Please sign in to comment.