Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Refactor KeyframeIndex usage to use Arc for sharing

Richie McIlroyfd5be87acc77
diff --git a/crates/rendering/src/decoder/avassetreader.rs b/crates/rendering/src/decoder/avassetreader.rs
index 1033824b9..6f05ba6b7 100644
--- a/crates/rendering/src/decoder/avassetreader.rs
+++ b/crates/rendering/src/decoder/avassetreader.rs
@@ -259,7 +259,7 @@ impl DecoderInstance {
path: PathBuf,
tokio_handle: TokioHandle,
start_time: f32,
- keyframe_index: Option<cap_video_decode::avassetreader::KeyframeIndex>,
+ keyframe_index: Option<Arc<cap_video_decode::avassetreader::KeyframeIndex>>,
) -> Result<Self, String> {
Ok(Self {
inner: cap_video_decode::AVAssetReaderDecoder::new_with_keyframe_index(
@@ -326,13 +326,12 @@ impl AVAssetReaderDecoder {
let pool_manager = DecoderPoolManager::new(config);
- let primary_kf = keyframe_index_arc.as_ref().map(|arc| (**arc).clone());
let primary_instance = DecoderInstance {
inner: cap_video_decode::AVAssetReaderDecoder::new_with_keyframe_index(
path.clone(),
tokio_handle.clone(),
0.0,
- primary_kf,
+ keyframe_index_arc.clone(),
)?,
is_done: false,
frames_iter_valid: true,
@@ -343,8 +342,12 @@ impl AVAssetReaderDecoder {
let initial_positions = pool_manager.positions();
for pos in initial_positions.iter().skip(1) {
let start_time = pos.position_secs;
- let kf_clone = keyframe_index_arc.as_ref().map(|arc| (**arc).clone());
- match DecoderInstance::new(path.clone(), tokio_handle.clone(), start_time, kf_clone) {
+ match DecoderInstance::new(
+ path.clone(),
+ tokio_handle.clone(),
+ start_time,
+ keyframe_index_arc.clone(),
+ ) {
Ok(instance) => {
decoders.push(instance);
tracing::info!(
diff --git a/crates/video-decode/src/avassetreader.rs b/crates/video-decode/src/avassetreader.rs
index e3768e453..fce3f6fd7 100644
--- a/crates/video-decode/src/avassetreader.rs
+++ b/crates/video-decode/src/avassetreader.rs
@@ -1,4 +1,5 @@
use std::path::{Path, PathBuf};
+use std::sync::Arc;
use cidre::{
arc::{self, R},
@@ -166,7 +167,7 @@ impl KeyframeIndex {
}
}
-fn compute_seek_time(keyframe_index: Option<&KeyframeIndex>, requested_time: f32) -> f32 {
+fn compute_seek_time(keyframe_index: Option<&Arc<KeyframeIndex>>, requested_time: f32) -> f32 {
if let Some(kf_index) = keyframe_index {
let fps = kf_index.fps();
let target_frame = (requested_time as f64 * fps).round() as u32;
@@ -188,7 +189,7 @@ pub struct AVAssetReaderDecoder {
reader: R<av::AssetReader>,
width: u32,
height: u32,
- keyframe_index: Option<KeyframeIndex>,
+ keyframe_index: Option<Arc<KeyframeIndex>>,
current_position_secs: f32,
}
@@ -203,7 +204,7 @@ impl AVAssetReaderDecoder {
start_time: f32,
) -> Result<Self, String> {
let keyframe_index = match KeyframeIndex::build(&path) {
- Ok(index) => Some(index),
+ Ok(index) => Some(Arc::new(index)),
Err(e) => {
tracing::warn!(
path = %path.display(),
@@ -221,7 +222,7 @@ impl AVAssetReaderDecoder {
path: PathBuf,
tokio_handle: TokioHandle,
start_time: f32,
- keyframe_index: Option<KeyframeIndex>,
+ keyframe_index: Option<Arc<KeyframeIndex>>,
) -> Result<Self, String> {
let (pixel_format, width, height) = {
let input = ffmpeg::format::input(&path).unwrap();
@@ -304,14 +305,18 @@ impl AVAssetReaderDecoder {
self.pixel_format
}
- pub fn take_keyframe_index(&mut self) -> Option<KeyframeIndex> {
+ pub fn take_keyframe_index(&mut self) -> Option<Arc<KeyframeIndex>> {
self.keyframe_index.take()
}
- pub fn keyframe_index(&self) -> Option<&KeyframeIndex> {
+ pub fn keyframe_index(&self) -> Option<&Arc<KeyframeIndex>> {
self.keyframe_index.as_ref()
}
+ pub fn keyframe_index_arc(&self) -> Option<Arc<KeyframeIndex>> {
+ self.keyframe_index.clone()
+ }
+
fn get_reader_track_output(
path: &Path,
time: f32,