Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

editor playback fix for camera

Richie McIlroy7340a2e87fae
diff --git a/crates/rendering/src/decoder/ffmpeg.rs b/crates/rendering/src/decoder/ffmpeg.rs
index 02410c058..f92a016b9 100644
--- a/crates/rendering/src/decoder/ffmpeg.rs
+++ b/crates/rendering/src/decoder/ffmpeg.rs
@@ -310,8 +310,13 @@ impl FfmpegDecoder {
}
if is_backward_seek {
- let best_cached_frame =
- cache.range(..=requested_frame).next_back().map(|(k, _)| *k);
+ let best_cached_frame = cache
+ .range(..=requested_frame)
+ .next_back()
+ .filter(|(k, _)| {
+ requested_frame.saturating_sub(**k) <= MAX_FRAME_TOLERANCE
+ })
+ .map(|(k, _)| *k);
if let Some(frame_num) = best_cached_frame
&& let Some(cached) = cache.get_mut(&frame_num)
@@ -322,13 +327,18 @@ impl FfmpegDecoder {
continue;
}
- if let Some(last_frame) = last_sent_frame.borrow().clone() {
- let _ = sender.send(last_frame.frame);
- continue;
- } else if let Some(first_frame) = first_ever_frame.borrow().clone() {
- let _ = sender.send(first_frame.frame);
- continue;
+ if requested_frame <= MAX_FRAME_TOLERANCE {
+ if let Some(first_frame) = first_ever_frame.borrow().clone() {
+ *last_sent_frame.borrow_mut() = Some(first_frame.clone());
+ let _ = sender.send(first_frame.frame);
+ continue;
+ }
}
+
+ let _ = this.reset(requested_time);
+ frames = this.frames();
+ *last_sent_frame.borrow_mut() = None;
+ cache.clear();
}
let mut sender = {
@@ -383,8 +393,10 @@ impl FfmpegDecoder {
continue;
};
- let current_frame =
- pts_to_frame(frame.pts().unwrap() - start_time, time_base, fps);
+ let Some(pts) = frame.pts() else {
+ continue;
+ };
+ let current_frame = pts_to_frame(pts - start_time, time_base, fps);
let mut cache_frame = CachedFrame::Raw {
frame,
@@ -449,8 +461,12 @@ impl FfmpegDecoder {
if current_frame > requested_frame && sender.is_some() {
let last_sent_frame_clone = last_sent_frame.borrow().clone();
- if let Some((sender, last_frame)) =
- last_sent_frame_clone.and_then(|l| Some((sender.take()?, l)))
+ if let Some((sender, last_frame)) = last_sent_frame_clone
+ .filter(|l| {
+ requested_frame.saturating_sub(l.number)
+ <= MAX_FRAME_TOLERANCE
+ })
+ .and_then(|l| Some((sender.take()?, l)))
{
(sender)(last_frame);
} else if let Some(sender) = sender.take() {
@@ -469,24 +485,39 @@ impl FfmpegDecoder {
last_active_frame = Some(requested_frame);
- let last_sent_frame_final = last_sent_frame.borrow().clone();
if let Some(sender) = sender.take() {
- if let Some(last_frame) = last_sent_frame_final {
- (sender)(last_frame);
- } else if let Some(first_frame) = first_ever_frame.borrow().clone() {
- (sender)(first_frame);
+ let best_cached = cache
+ .range(..=requested_frame)
+ .next_back()
+ .filter(|(k, _)| {
+ requested_frame.saturating_sub(**k) <= MAX_FRAME_TOLERANCE
+ })
+ .map(|(_, v)| v);
+
+ if let Some(cached) = best_cached {
+ let output = cached.clone().produce(&mut converter);
+ *last_sent_frame.borrow_mut() = Some(output.clone());
+ (sender)(output);
} else {
- let black_frame_data =
- vec![0u8; (video_width * video_height * 4) as usize];
- let black_frame = OutputFrame {
- number: requested_frame,
- frame: DecodedFrame::new_with_arc(
- Arc::new(black_frame_data),
- video_width,
- video_height,
- ),
- };
- (sender)(black_frame);
+ let last_frame_clone = last_sent_frame.borrow().clone();
+ let first_frame_clone = first_ever_frame.borrow().clone();
+ if let Some(last_frame) = last_frame_clone {
+ (sender)(last_frame);
+ } else if let Some(first_frame) = first_frame_clone {
+ (sender)(first_frame);
+ } else {
+ let black_frame_data =
+ vec![0u8; (video_width * video_height * 4) as usize];
+ let black_frame = OutputFrame {
+ number: requested_frame,
+ frame: DecodedFrame::new_with_arc(
+ Arc::new(black_frame_data),
+ video_width,
+ video_height,
+ ),
+ };
+ (sender)(black_frame);
+ }
}
}
}
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index b07bffdab..780e04439 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -191,6 +191,7 @@ impl RecordingSegmentDecoders {
needs_camera: bool,
offsets: ClipOffsets,
) -> Option<DecodedSegmentFrames> {
+ let camera_request_time = segment_time + offsets.camera;
let (screen, camera) = tokio::join!(
self.screen.get_frame(segment_time),
OptionFuture::from(
@@ -198,7 +199,7 @@ impl RecordingSegmentDecoders {
.then(|| self
.camera
.as_ref()
- .map(|d| d.get_frame(segment_time + offsets.camera)))
+ .map(|d| d.get_frame(camera_request_time)))
.flatten()
)
);
diff --git a/crates/video-decode/src/media_foundation.rs b/crates/video-decode/src/media_foundation.rs
index ff45467fd..1cf9e989a 100644
--- a/crates/video-decode/src/media_foundation.rs
+++ b/crates/video-decode/src/media_foundation.rs
@@ -129,10 +129,7 @@ fn query_mf_decoder_capabilities(device: &ID3D11Device) -> MFDecoderCapabilities
})
})();
- match result {
- Ok(caps) => caps,
- Err(_) => MFDecoderCapabilities::default(),
- }
+ result.unwrap_or_default()
}
pub fn get_mf_decoder_capabilities() -> Option<&'static MFDecoderCapabilities> {
@@ -169,6 +166,9 @@ pub struct FrameTextures {
pub uv_uav: ID3D11UnorderedAccessView,
}
+unsafe impl Send for FrameTextures {}
+unsafe impl Sync for FrameTextures {}
+
impl FrameTextures {
fn create(device: &ID3D11Device, width: u32, height: u32) -> Result<Self, String> {
let nv12 = create_internal_texture(
@@ -211,6 +211,7 @@ impl FrameTextures {
}
}
+#[derive(Default)]
pub struct FramePool {
free: Vec<FrameTextures>,
width: u32,
@@ -219,11 +220,7 @@ pub struct FramePool {
impl FramePool {
pub fn new() -> Self {
- Self {
- free: Vec::new(),
- width: 0,
- height: 0,
- }
+ Self::default()
}
pub fn acquire(
@@ -430,8 +427,8 @@ void main(uint3 dtid : SV_DispatchThreadID) {
None,
None,
None,
- PCSTR(b"main\0".as_ptr()),
- PCSTR(b"cs_5_0\0".as_ptr()),
+ PCSTR(c"main".as_ptr().cast()),
+ PCSTR(c"cs_5_0".as_ptr().cast()),
0,
0,
shader_blob_ptr,
@@ -746,10 +743,10 @@ impl MediaFoundationDecoder {
}
pub fn recycle_textures(&mut self, textures: Arc<FrameTextures>) {
- if Arc::strong_count(&textures) == 1 {
- if let Ok(textures) = Arc::try_unwrap(textures) {
- self.frame_pool.recycle(textures);
- }
+ if Arc::strong_count(&textures) == 1
+ && let Ok(textures) = Arc::try_unwrap(textures)
+ {
+ self.frame_pool.recycle(textures);
}
}
@@ -935,13 +932,11 @@ unsafe fn create_d3d11_device() -> Result<(ID3D11Device, ID3D11DeviceContext), S
return Ok((device, context));
}
- last_error = format!(
- "D3D11CreateDevice ({}) returned null device/context",
- driver_name
- );
+ last_error =
+ format!("D3D11CreateDevice ({driver_name}) returned null device/context");
}
Err(e) => {
- last_error = format!("D3D11CreateDevice ({}) failed: {e:?}", driver_name);
+ last_error = format!("D3D11CreateDevice ({driver_name}) failed: {e:?}");
if driver_type == D3D_DRIVER_TYPE_HARDWARE {
warn!("{}, trying WARP fallback", last_error);
}