Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(editor): register playback retention before starting the worker

vedanth738ff90c55dc
diff --git a/crates/editor/src/audio.rs b/crates/editor/src/audio.rs
index 278a40fa6..7f846172b 100644
--- a/crates/editor/src/audio.rs
+++ b/crates/editor/src/audio.rs
@@ -2192,10 +2192,10 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> PrerenderedAudioBuffer<T> {
output_info,
duration_secs,
start_playhead_secs,
- estimated_output_samples,
+ cache.filter(|_| plan.as_ref().is_some_and(|plan| plan.shift == 0)),
);
match cache {
- Some(cache) => fresh.retain_in_cache(cache, plan),
+ Some(cache) => fresh.publish_in_cache(cache, plan),
None => fresh,
}
}
@@ -2339,10 +2339,15 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
output_info: AudioInfo,
duration_secs: f64,
start_playhead_secs: f64,
- estimated_output_samples: usize,
+ cache: Option<&PlaybackAudioCache>,
) -> Self {
// Clamp output info for FFmpeg compatibility (max 8 channels)
let output_info = output_info.for_ffmpeg_output();
+ let estimated_output_samples = progressive_buffer_samples(
+ duration_secs,
+ output_info.sample_rate,
+ output_info.channels,
+ );
// The producer renders f32 at the device rate/channel count; the
// callback converts to the device sample format on the fly.
let render_info = AudioInfo::new_raw(
@@ -2383,6 +2388,14 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
));
let (ready_tx, ready_rx) = std::sync::mpsc::channel();
+ // A fast worker may finish before publication, so give it its retention owner first.
+ if let Some(cache) = cache {
+ let retained = timeline.retain(CacheRelease {
+ cache: Arc::downgrade(&cache.entry),
+ idle_release: cache.idle_release,
+ });
+ debug_assert!(retained);
+ }
if !spawn_progressive_render(
timeline.clone(),
@@ -2395,7 +2408,10 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
ready_at_index,
ready_tx,
) {
- lock(&timeline.retain_slot).exited = true;
+ let mut slot = lock(&timeline.retain_slot);
+ slot.exited = true;
+ slot.release = None;
+ timeline.retained.store(false, Ordering::Release);
}
Self {
@@ -2492,18 +2508,14 @@ impl<T: FromSampleBytes + cpal::FromSample<f32>> ProgressiveAudioBuffer<T> {
})
}
- fn retain_in_cache(self, cache: &PlaybackAudioCache, plan: Option<CachePlan>) -> Self {
+ fn publish_in_cache(self, cache: &PlaybackAudioCache, plan: Option<CachePlan>) -> Self {
let Some(plan) = plan else {
let previous = lock(&cache.entry).take();
self.render.timeline.retire(previous);
return self;
};
let mut entry = lock(&cache.entry);
- let previous = if plan.shift == 0
- && self.render.timeline.retain(CacheRelease {
- cache: Arc::downgrade(&cache.entry),
- idle_release: cache.idle_release,
- }) {
+ let previous = if plan.shift == 0 && self.render.timeline.retained.load(Ordering::Acquire) {
entry.replace(CachedProgressiveRender {
key: plan.key,
render: self.render.clone(),
@@ -5369,6 +5381,60 @@ mod tests {
}
}
+ #[test]
+ fn a_render_completed_before_cache_publication_is_reused() {
+ let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
+ let fixture = ProgressiveFixture::new();
+ let output_info = stereo_48k();
+ let cache = PlaybackAudioCache::default();
+ let plan = ProgressiveAudioBuffer::<f32>::cache_plan(
+ fixture.segments.clone(),
+ &fixture.music,
+ &fixture.project,
+ output_info,
+ fixture.duration(),
+ 0.0,
+ progressive_buffer_samples(
+ fixture.duration(),
+ output_info.sample_rate,
+ output_info.channels,
+ ),
+ )
+ .unwrap();
+ assert_eq!(plan.shift, 0);
+ let fresh = ProgressiveAudioBuffer::<f32>::new(
+ fixture.segments.clone(),
+ fixture.music.clone(),
+ &fixture.project,
+ output_info,
+ fixture.duration(),
+ 0.0,
+ Some(&cache),
+ );
+ fresh.wait_until_fully_rendered();
+ let timeline = fresh.render.timeline.clone();
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
+ while !lock(&timeline.retain_slot).exited {
+ assert!(std::time::Instant::now() < deadline);
+ std::thread::sleep(std::time::Duration::from_millis(1));
+ }
+ assert!(lock(&cache.entry).is_none());
+ let fresh = fresh.publish_in_cache(&cache, Some(plan));
+ assert!(
+ lock(&cache.entry)
+ .as_ref()
+ .is_some_and(|cached| Arc::ptr_eq(&cached.render.timeline, &timeline))
+ );
+ drop(fresh);
+ let mut replay = fixture.cached(&cache, output_info, 0.0);
+ assert!(Arc::ptr_eq(&timeline, &progressive_timeline(&replay)));
+ assert_bit_identical(
+ &read_everything(&mut replay),
+ &fixture.reference(output_info, 0.0),
+ "render completed before publication",
+ );
+ }
+
#[test]
fn a_replay_resumes_a_parked_render() {
let _reuse = CACHE_RELEASE.read().unwrap_or_else(PoisonError::into_inner);
@@ -5377,9 +5443,15 @@ mod tests {
for _ in 0..20 {
let cache = PlaybackAudioCache::default();
let mut first = fixture.cached(&cache, output_info, 0.0);
+ let timeline = progressive_timeline(&first);
+ assert!(timeline.retained.load(Ordering::Acquire));
+ assert!(
+ lock(&cache.entry)
+ .as_ref()
+ .is_some_and(|cached| Arc::ptr_eq(&cached.render.timeline, &timeline))
+ );
first.set_playhead(0.0);
assert!(first.wait_until_ready(std::time::Duration::from_secs(30)));
- let timeline = progressive_timeline(&first);
drop(first);
let mut replay = fixture.cached(&cache, output_info, 0.0);
assert!(Arc::ptr_eq(&timeline, &progressive_timeline(&replay)));