Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(rendering): exclude mismatched system audio from playback timing

Aditya Garud024044cb8db4
diff --git a/crates/editor/src/editor_instance.rs b/crates/editor/src/editor_instance.rs
index cecbe1db9..90028f572 100644
--- a/crates/editor/src/editor_instance.rs
+++ b/crates/editor/src/editor_instance.rs
@@ -1849,8 +1849,7 @@ async fn create_segments_with_audio(
.unwrap_or_else(AudioLoader::none);
let system_audio = s
- .system_audio
- .as_ref()
+ .usable_system_audio()
.filter(|_| load_audio)
.map(|audio| {
audio_loader_with_completed(
diff --git a/crates/editor/src/export_audio.rs b/crates/editor/src/export_audio.rs
index 58973230d..5ac70dcfd 100644
--- a/crates/editor/src/export_audio.rs
+++ b/crates/editor/src/export_audio.rs
@@ -217,7 +217,7 @@ impl ExportAudioPreparation {
if let Some(audio) = &segment.mic {
inputs.push((index, recording.path(&audio.path), true));
}
- if let Some(audio) = &segment.system_audio {
+ if let Some(audio) = segment.usable_system_audio() {
inputs.push((index, recording.path(&audio.path), false));
}
}
@@ -292,7 +292,8 @@ impl ExportAudioRenderer {
.segments
.iter()
.map(|segment| {
- usize::from(segment.mic.is_some()) + usize::from(segment.system_audio.is_some())
+ usize::from(segment.mic.is_some())
+ + usize::from(segment.usable_system_audio().is_some())
})
.sum(),
};
diff --git a/crates/project/src/meta.rs b/crates/project/src/meta.rs
index 08b5a4094..a264bb690 100644
--- a/crates/project/src/meta.rs
+++ b/crates/project/src/meta.rs
@@ -666,6 +666,12 @@ pub struct MultipleSegment {
}
impl MultipleSegment {
+ pub fn usable_system_audio(&self) -> Option<&AudioMeta> {
+ self.system_audio
+ .as_ref()
+ .filter(|audio| audio.start_time.is_some() == self.display.start_time.is_some())
+ }
+
pub fn path(&self, meta: &RecordingMeta, path: impl AsRef<Path>) -> PathBuf {
meta.project_path.join(path)
}
@@ -737,7 +743,7 @@ impl MultipleSegment {
value = value.max(mic.start_time?);
}
- if let Some(system_audio) = &self.system_audio {
+ if let Some(system_audio) = self.usable_system_audio() {
value = value.max(system_audio.start_time?);
}
@@ -767,8 +773,7 @@ impl MultipleSegment {
.unwrap_or(0.0);
let system_audio_offset = self
- .system_audio
- .as_ref()
+ .usable_system_audio()
.and_then(|s| s.start_time)
.map(|t| (latest - t) as f32 + cal_offset)
.unwrap_or(0.0);
@@ -1205,6 +1210,24 @@ mod test {
assert_eq!(offsets.camera, 0.0);
assert!((offsets.mic - 0.05).abs() < 1e-6);
}
+
+ #[test]
+ fn mismatched_system_audio_does_not_affect_timing_or_audio_selection() {
+ let mut segment = segment(10.0, Some(10.5), Some(11.0));
+ segment.camera = Some(video(Some(10.25)));
+ assert_eq!(segment.latest_start_time(), Some(11.0));
+ assert!(segment.usable_system_audio().is_some());
+
+ segment.system_audio.as_mut().unwrap().start_time = None;
+ assert!(segment.usable_system_audio().is_none());
+ assert_eq!(segment.latest_start_time(), Some(10.5));
+ let offsets = segment.calculate_audio_offsets();
+ assert_eq!(offsets.mic, 0.0);
+ assert_eq!(offsets.system_audio, 0.0);
+
+ segment.display.start_time = None;
+ assert!(segment.usable_system_audio().is_some());
+ }
}
}
diff --git a/crates/render-farm/src/audio.rs b/crates/render-farm/src/audio.rs
index b75fb942d..0eb34e892 100644
--- a/crates/render-farm/src/audio.rs
+++ b/crates/render-farm/src/audio.rs
@@ -297,8 +297,7 @@ async fn load_audio_segments(
.as_ref()
.map(|audio| project.recording_meta.path(&audio.path)),
segment
- .system_audio
- .as_ref()
+ .usable_system_audio()
.map(|audio| project.recording_meta.path(&audio.path)),
)
})
diff --git a/crates/rendering/src/project_recordings.rs b/crates/rendering/src/project_recordings.rs
index b41c17e78..d3da5d908 100644
--- a/crates/rendering/src/project_recordings.rs
+++ b/crates/rendering/src/project_recordings.rs
@@ -338,7 +338,10 @@ impl ProjectRecordingsMeta {
})
};
- let system_audio = match Option::map(s.system_audio.as_ref(), load_audio)
+ if s.system_audio.is_some() && s.usable_system_audio().is_none() {
+ tracing::warn!("System audio start time does not match display, treating as no audio");
+ }
+ let system_audio = match Option::map(s.usable_system_audio(), load_audio)
.transpose()
{
Ok(audio) => audio,
diff --git a/crates/rendering/src/segment_timing.rs b/crates/rendering/src/segment_timing.rs
index 5afb79df6..ff6f454ce 100644
--- a/crates/rendering/src/segment_timing.rs
+++ b/crates/rendering/src/segment_timing.rs
@@ -129,4 +129,21 @@ mod tests {
((segment_time as f64 + offset) as f32).to_bits()
);
}
+
+ #[test]
+ fn missing_system_audio_timestamp_keeps_video_offsets() {
+ let metadata: StudioRecordingMeta = serde_json::from_value(serde_json::json!({
+ "segments": [{
+ "display": { "path": "display", "fps": 60, "start_time": 10.0 },
+ "camera": { "path": "camera", "fps": 24, "start_time": 10.25 },
+ "mic": { "path": "mic.aac", "start_time": 10.5 },
+ "system_audio": { "path": "system.ogg" }
+ }]
+ }))
+ .unwrap();
+ let timing = segment_video_timing(&metadata, 0);
+ assert_eq!(timing.latest_start_time, Some(10.5));
+ assert_eq!(timing.screen_offset, 0.5);
+ assert_eq!(timing.camera_offset, 0.25);
+ }
}