Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix windows build + decoder (#409)

Brendan Allanb0ab405548ab
diff --git a/crates/media/src/sources/screen_capture.rs b/crates/media/src/sources/screen_capture.rs
index 011c5305f..1ad4eb5f1 100644
--- a/crates/media/src/sources/screen_capture.rs
+++ b/crates/media/src/sources/screen_capture.rs
@@ -1,5 +1,4 @@
use cap_flags::FLAGS;
-use cidre::cm;
use cpal::traits::{DeviceTrait, HostTrait};
use ffmpeg::{format::Sample, frame::Audio, ChannelLayout};
use ffmpeg_sys_next::AV_TIME_BASE_Q;
diff --git a/crates/rendering/src/decoder/avassetreader.rs b/crates/rendering/src/decoder/avassetreader.rs
index 6fd81e3b5..c8a778415 100644
--- a/crates/rendering/src/decoder/avassetreader.rs
+++ b/crates/rendering/src/decoder/avassetreader.rs
@@ -4,7 +4,6 @@ use std::{
path::PathBuf,
rc::Rc,
sync::{mpsc, Arc},
- time::Instant,
};
use cidre::{
@@ -13,9 +12,8 @@ use cidre::{
};
use ffmpeg::{format, frame, Rational};
use tokio::{runtime::Handle as TokioHandle, sync::oneshot};
-use tracing::{debug, info};
-use super::{pts_to_frame, DecodedFrame, VideoDecoderMessage, FRAME_CACHE_SIZE};
+use super::{pts_to_frame, VideoDecoderMessage, FRAME_CACHE_SIZE};
#[derive(Clone)]
struct ProcessedFrame {
@@ -189,7 +187,6 @@ impl CachedFrame {
pub struct AVAssetReaderDecoder {
inner: cap_video_decode::AVAssetReaderDecoder,
- last_decoded_frame: Option<(u32, CachedFrame)>,
is_done: bool,
}
@@ -197,14 +194,12 @@ impl AVAssetReaderDecoder {
fn new(path: PathBuf, tokio_handle: TokioHandle) -> Result<Self, String> {
Ok(Self {
inner: cap_video_decode::AVAssetReaderDecoder::new(path, tokio_handle)?,
- last_decoded_frame: None,
is_done: false,
})
}
fn reset(&mut self, requested_time: f32) {
let _ = self.inner.reset(requested_time);
- self.last_decoded_frame = None;
}
pub fn spawn(
@@ -306,7 +301,6 @@ impl AVAssetReaderDecoder {
number: current_frame,
};
- this.last_decoded_frame = Some((current_frame, cache_frame.clone()));
this.is_done = false;
// Handles frame skips.
diff --git a/crates/rendering/src/decoder/ffmpeg.rs b/crates/rendering/src/decoder/ffmpeg.rs
index 09a4ba6c6..8d23341b3 100644
--- a/crates/rendering/src/decoder/ffmpeg.rs
+++ b/crates/rendering/src/decoder/ffmpeg.rs
@@ -1,6 +1,8 @@
use std::{
+ cell::RefCell,
collections::BTreeMap,
path::PathBuf,
+ rc::Rc,
sync::{mpsc, Arc},
};
@@ -8,19 +10,19 @@ use ffmpeg::{codec, format, frame, software, Codec};
use ffmpeg_sys_next::{avcodec_find_decoder, AVHWDeviceType};
use log::debug;
use tokio::sync::oneshot;
-use tracing::error;
-use super::{pts_to_frame, DecodedFrame, VideoDecoderMessage, FRAME_CACHE_SIZE};
+use super::{pts_to_frame, VideoDecoderMessage, FRAME_CACHE_SIZE};
#[derive(Clone)]
-struct CachedFrame {
- data: CachedFrameData,
+struct ProcessedFrame {
+ number: u32,
+ data: Arc<Vec<u8>>,
}
impl CachedFrame {
- fn process(&mut self, width: u32, height: u32) -> Arc<Vec<u8>> {
- match &mut self.data {
- CachedFrameData::Raw(frame) => {
+ fn process(&mut self, width: u32, height: u32) -> ProcessedFrame {
+ match self {
+ Self::Raw { frame, number } => {
let rgb_frame = if frame.format() != format::Pixel::RGBA {
// Reinitialize the scaler with the new input format
let mut scaler =
@@ -48,21 +50,24 @@ impl CachedFrame {
frame_buffer.extend_from_slice(&line_data[0..width * 4]);
}
- let data = Arc::new(frame_buffer);
+ let data = ProcessedFrame {
+ data: Arc::new(frame_buffer),
+ number: *number,
+ };
- self.data = CachedFrameData::Processed(data.clone());
+ *self = Self::Processed(data.clone());
data
}
- CachedFrameData::Processed(data) => data.clone(),
+ Self::Processed(data) => data.clone(),
}
}
}
#[derive(Clone)]
-enum CachedFrameData {
- Raw(frame::Video),
- Processed(Arc<Vec<u8>>),
+enum CachedFrame {
+ Raw { frame: frame::Video, number: u32 },
+ Processed(ProcessedFrame),
}
pub struct FfmpegDecoder;
@@ -95,12 +100,11 @@ impl FfmpegDecoder {
// frames that are within render_more_margin of this frame won't trigger decode.
let mut last_active_frame = None::<u32>;
- let mut last_decoded_frame = None::<u32>;
- let mut last_sent_frame = None::<(u32, DecodedFrame)>;
+ let last_sent_frame = Rc::new(RefCell::new(None::<ProcessedFrame>));
let mut peekable_requests = PeekableReceiver { rx, peeked: None };
- let mut frames = this.frames().peekable();
+ let mut frames = this.frames();
let _ = ready_tx.send(Ok(()));
@@ -114,11 +118,15 @@ impl FfmpegDecoder {
let mut sender = if let Some(cached) = cache.get_mut(&requested_frame) {
let data = cached.process(width, height);
- sender.send(data.clone()).ok();
- last_sent_frame = Some((requested_frame, data));
+ sender.send(data.data.clone()).ok();
+ *last_sent_frame.borrow_mut() = Some(data);
continue;
} else {
- Some(sender)
+ let last_sent_frame = last_sent_frame.clone();
+ Some(move |data: ProcessedFrame| {
+ *last_sent_frame.borrow_mut() = Some(data.clone());
+ let _ = sender.send(data.data);
+ })
};
let cache_min = requested_frame.saturating_sub(FRAME_CACHE_SIZE as u32 / 2);
@@ -126,131 +134,131 @@ impl FfmpegDecoder {
if requested_frame == 0
|| last_sent_frame
+ .borrow()
.as_ref()
.map(|last| {
- requested_frame < last.0 ||
- // seek forward for big jumps. this threshold is arbitrary but should be derived from i-frames in future
- requested_frame - last.0 > FRAME_CACHE_SIZE as u32
+ requested_frame < last.number
+ // seek forward for big jumps. this threshold is arbitrary but should be derived from i-frames in future
+ || requested_frame - last.number > FRAME_CACHE_SIZE as u32
})
.unwrap_or(true)
{
debug!("seeking to {}", requested_frame);
- this.reset(requested_time);
- frames = this.frames().peekable();
-
- last_decoded_frame = None;
+ let _ = this.reset(requested_time);
+ frames = this.frames();
}
last_active_frame = Some(requested_frame);
let mut exit = false;
- loop {
- if peekable_requests.peek().is_some() {
- break;
- }
+ for frame in &mut frames {
+ let Ok(frame) = frame.map_err(|e| format!("read frame / {e}")) else {
+ continue;
+ };
- let Some(frame) = frames.next() else {
- break;
+ let current_frame =
+ pts_to_frame(frame.pts().unwrap() - start_time, time_base, fps);
+
+ let mut cache_frame = CachedFrame::Raw {
+ frame,
+ number: current_frame,
};
+ // Handles frame skips.
+ // We use the cache instead of last_sent_frame as newer non-matching frames could have been decoded.
+ if let Some(most_recent_prev_frame) =
+ cache.iter_mut().rev().find(|v| *v.0 < requested_frame)
{
- let frame = match frame {
- Ok(f) => f,
- Err(e) => {
- error!("Error decoding frame: {}", e);
- break;
- }
- };
-
- let current_frame =
- pts_to_frame(frame.pts().unwrap() - start_time, time_base, fps);
-
- // Handles frame skips. requested_frame == last_decoded_frame should be handled by the frame cache.
- if let Some((last_decoded_frame, sender)) = last_decoded_frame
- .filter(|last_decoded_frame| {
- requested_frame > *last_decoded_frame
- && requested_frame < current_frame
- })
- .and_then(|l| Some((l, sender.take()?)))
- {
- let Some(data) = cache
- .get_mut(&last_decoded_frame)
- .map(|f| f.process(width, height))
- else {
- break;
- };
-
- last_sent_frame = Some((last_decoded_frame, data.clone()));
- sender.send(data).ok();
+ if let Some(sender) = sender.take() {
+ (sender)(most_recent_prev_frame.1.process(width, height));
}
+ }
- last_decoded_frame = Some(current_frame);
-
- let exceeds_cache_bounds = current_frame > cache_max;
- let too_small_for_cache_bounds = current_frame < cache_min;
+ let exceeds_cache_bounds = current_frame > cache_max;
+ let too_small_for_cache_bounds = current_frame < cache_min;
- if !too_small_for_cache_bounds {
- let mut cache_frame = CachedFrame {
- data: CachedFrameData::Raw(frame),
- };
+ let cache_frame = if !too_small_for_cache_bounds {
+ if current_frame == requested_frame {
+ if let Some(sender) = sender.take() {
+ let data = cache_frame.process(width, height);
+ // info!("sending frame {requested_frame}");
- if current_frame == requested_frame {
- if let Some(sender) = sender.take() {
- let data = cache_frame.process(width, height);
- last_sent_frame = Some((current_frame, data.clone()));
- sender.send(data).ok();
+ (sender)(data);
- break;
- }
+ break;
}
+ }
- if cache.len() >= FRAME_CACHE_SIZE {
- if let Some(last_active_frame) = &last_active_frame {
- let frame = if requested_frame > *last_active_frame {
- *cache.keys().next().unwrap()
- } else if requested_frame < *last_active_frame {
- *cache.keys().next_back().unwrap()
- } else {
- let min = *cache.keys().min().unwrap();
- let max = *cache.keys().max().unwrap();
+ if cache.len() >= FRAME_CACHE_SIZE {
+ if let Some(last_active_frame) = &last_active_frame {
+ let frame = if requested_frame > *last_active_frame {
+ *cache.keys().next().unwrap()
+ } else if requested_frame < *last_active_frame {
+ *cache.keys().next_back().unwrap()
+ } else {
+ let min = *cache.keys().min().unwrap();
+ let max = *cache.keys().max().unwrap();
- if current_frame > max {
- min
- } else {
- max
- }
- };
+ if current_frame > max {
+ min
+ } else {
+ max
+ }
+ };
- cache.remove(&frame);
- } else {
- cache.clear()
- }
+ cache.remove(&frame);
+ } else {
+ cache.clear()
}
-
- cache.insert(current_frame, cache_frame);
}
- exit = exit || exceeds_cache_bounds;
- }
- }
+ cache.insert(current_frame, cache_frame);
+ cache.get_mut(&current_frame).unwrap()
+ } else {
+ &mut cache_frame
+ };
+
+ if current_frame > requested_frame && sender.is_some() {
+ // not inlining this is important so that last_sent_frame is dropped before the sender is invoked
+ let last_sent_frame = last_sent_frame.borrow().clone();
- // handles the case where the cache doesn't contain a frame so we fallback to the previously sent one
- if let Some(last_sent_frame) = &last_sent_frame {
- if last_sent_frame.0 < requested_frame {
- sender.take().map(|s| s.send(last_sent_frame.1.clone()));
+ if let Some((sender, last_sent_frame)) =
+ last_sent_frame.and_then(|l| Some((sender.take()?, l)))
+ {
+ // info!(
+ // "sending previous frame {} for {requested_frame}",
+ // last_sent_frame.0
+ // );
+
+ (sender)(last_sent_frame);
+ } else if let Some(sender) = sender.take() {
+ // info!(
+ // "sending forward frame {current_frame} for {requested_frame}",
+ // );
+
+ (sender)(cache_frame.process(width, height));
+ }
}
- }
- if exit {
- continue;
+ exit = exit || exceeds_cache_bounds;
+
+ if exit {
+ break;
+ }
}
- if let Some((sender, last_sent_frame)) =
- sender.take().zip(last_sent_frame.clone())
+ // not inlining this is important so that last_sent_frame is dropped before the sender is invoked
+ let last_sent_frame = last_sent_frame.borrow().clone();
+ if let Some((sender, last_sent_frame)) = sender.take().zip(last_sent_frame)
{
- sender.send(last_sent_frame.1).ok();
+ // info!(
+ // "sending hail mary frame {} for {requested_frame}",
+ // last_sent_frame.0
+ // );
+
+ (sender)(last_sent_frame);
}
}
}
diff --git a/crates/video-decode/src/ffmpeg.rs b/crates/video-decode/src/ffmpeg.rs
index 7add56b14..401770527 100644
--- a/crates/video-decode/src/ffmpeg.rs
+++ b/crates/video-decode/src/ffmpeg.rs
@@ -42,6 +42,8 @@ impl FFmpegDecoder {
.video()
.map_err(|e| format!("video decoder / {e}"))?;
+ decoder.set_time_base(input_stream.time_base());
+
let width = decoder.width();
let height = decoder.height();
diff --git a/crates/video-decode/src/main.rs b/crates/video-decode/src/main.rs
index 497af5ded..ca992830d 100644
--- a/crates/video-decode/src/main.rs
+++ b/crates/video-decode/src/main.rs
@@ -1,6 +1,6 @@
use std::time::Instant;
-use cap_video_decode::AVAssetReaderDecoder;
+use cap_video_decode::FFmpegDecoder;
#[tokio::main]
async fn main() {
@@ -12,7 +12,7 @@ async fn main() {
let path = args.next().unwrap();
- let mut decoder = AVAssetReaderDecoder::new(path.into(), handle).unwrap();
+ let mut decoder = FFmpegDecoder::new(path, None).unwrap();
let start = Instant::now();