A paused preview blends the dissolve instead of hanging on plain A
Browse files at this commit7456e3449a9f6e0e008ea2dce65cf19009bc450dParent: 8108e28- fix
- a paused preview blends the dissolve instead of hanging on plain A
1 changed file +94 -0
crates/engine/src/playback.rsmodified+94 -0
| @@ -41,6 +41,15 @@ const RING_FULL_WAIT: Duration = Duration::from_millis(10); | |||
| 41 | /// for the millisecond or two it lasts. | 41 | /// for the millisecond or two it lasts. |
| 42 | const FLUSH_WAIT: Duration = Duration::from_millis(1); | 42 | const FLUSH_WAIT: Duration = Duration::from_millis(1); |
| 43 | 43 | ||
| 44 | +/// How long [`try_frame`](PlaybackSession::try_frame) blocks the caller | ||
| 45 | +/// waiting for B's first picture when the playhead lands in a dissolve | ||
| 46 | +/// window before B's eager-opened decoder ([`start_span`](PlaybackSession::start_span)) | ||
| 47 | +/// has one ready -- a seek straight into the window, mostly. Above the 98 ms | ||
| 48 | +/// worst-case VA-API init measured for a picture open, so an eager-opened B | ||
| 49 | +/// almost never actually waits the full budget; a source B cannot decode at | ||
| 50 | +/// all pays this once per span rather than freezing the preview outright. | ||
| 51 | +const DISSOLVE_B_TIMEOUT: Duration = Duration::from_millis(200); | ||
| 52 | + | ||
| 44 | /// The timeline a file with no picture scaffolds: 1080p at 30 fps, H.264 -- | 53 | /// The timeline a file with no picture scaffolds: 1080p at 30 fps, H.264 -- |
| 45 | /// nothing was shot on it, so it is the canvas a *later* import meets rather | 54 | /// nothing was shot on it, so it is the canvas a *later* import meets rather |
| 46 | /// than a description of the song. H.264 because it is the one codec that | 55 | /// than a description of the song. H.264 because it is the one codec that |
| @@ -1472,6 +1481,18 @@ impl PlaybackSession { | |||
| 1472 | while let Ok(bf) = b.frames.try_recv() { | 1481 | while let Ok(bf) = b.frames.try_recv() { |
| 1473 | b.held = Some(bf); | 1482 | b.held = Some(bf); |
| 1474 | } | 1483 | } |
| 1484 | + // The window's first tick, with B's decoder not yet | ||
| 1485 | + // caught up: a paused or scrubbed preview gets no | ||
| 1486 | + // later tick to correct itself on (nothing is due | ||
| 1487 | + // while the clock is frozen -- `Player::pump`), so | ||
| 1488 | + // this is the one chance to blend rather than show A | ||
| 1489 | + // alone. Bounded so a source B cannot decode never | ||
| 1490 | + // freezes the preview -- eager-opened in | ||
| 1491 | + // `start_span`, B is normally already warm here and | ||
| 1492 | + // this waits on nothing at all. | ||
| 1493 | + if b.held.is_none() { | ||
| 1494 | + b.held = b.frames.recv_timeout(DISSOLVE_B_TIMEOUT).ok(); | ||
| 1495 | + } | ||
| 1475 | if let Some(bf) = &b.held { | 1496 | if let Some(bf) = &b.held { |
| 1476 | let idx_in_window = (frame.index - d.tail_start).min(d.window - 1); | 1497 | let idx_in_window = (frame.index - d.tail_start).min(d.window - 1); |
| 1477 | let t = crate::export::dissolve_weight(idx_in_window, d.window); | 1498 | let t = crate::export::dissolve_weight(idx_in_window, d.window); |
| @@ -1675,6 +1696,18 @@ impl PlaybackSession { | |||
| 1675 | }, | 1696 | }, |
| 1676 | ) | 1697 | ) |
| 1677 | }); | 1698 | }); |
| 1699 | + // B's decoder opens now, at the start of the *span*, rather than | ||
| 1700 | + // waiting for the playhead to reach the window: the window is about a | ||
| 1701 | + // second, which is exactly the head start a decoder open (VA-API | ||
| 1702 | + // init measured 98ms worst case) needs to have a first picture ready | ||
| 1703 | + // by the time playback -- or a seek straight into the window -- asks | ||
| 1704 | + // for one. Without this a paused seek into the window met a `held` | ||
| 1705 | + // that was still `None` and nothing ever asked again | ||
| 1706 | + // (`try_frame`'s bounded wait below is the last-resort backstop for a | ||
| 1707 | + // seek that lands inside the window before this head start pays off). | ||
| 1708 | + if self.dissolve.is_some() { | ||
| 1709 | + self.open_dissolve_b(); | ||
| 1710 | + } | ||
| 1678 | // Which file's frames the worker about to be opened will number its | 1711 | // Which file's frames the worker about to be opened will number its |
| 1679 | // pictures in: the span's own source, and real time for a gap, whose | 1712 | // pictures in: the span's own source, and real time for a gap, whose |
| 1680 | // black worker counts timeline frames already. Set before the open, | 1713 | // black worker counts timeline frames already. Set before the open, |
| @@ -4330,6 +4363,67 @@ mod tests { | |||
| 4330 | assert_eq!(ad.channels, a0.channels, "open() still defaults to stream 0"); | 4363 | assert_eq!(ad.channels, a0.channels, "open() still defaults to stream 0"); |
| 4331 | } | 4364 | } |
| 4332 | 4365 | ||
| 4366 | + /// The bug this regression pins: a preview paused (or scrubbed) to a | ||
| 4367 | + /// frame inside a dissolve window used to show plain A forever, because | ||
| 4368 | + /// B's decoder only opened on the tick that landed in the window and | ||
| 4369 | + /// nothing polled `try_frame` again while the clock stood still | ||
| 4370 | + /// (`Player::pump` only drains while playing or a seek is owed, and one | ||
| 4371 | + /// delivered frame clears "owed"). Two clips of real fixtures, touching, | ||
| 4372 | + /// with a dissolve set on the join -- seeking into the window and taking | ||
| 4373 | + /// exactly one frame (no play, no second tick) must already show a blend: | ||
| 4374 | + /// the same position with the dissolve turned off must differ from it. | ||
| 4375 | + #[test] | ||
| 4376 | + fn a_single_paused_tick_inside_the_dissolve_window_is_already_blended() { | ||
| 4377 | + let window = 6u32; | ||
| 4378 | + let build = |transition: u32| { | ||
| 4379 | + let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open a"); | ||
| 4380 | + s.set_gain(0.0); | ||
| 4381 | + s.import(&asset("test_av2.mp4")).expect("av2 matches"); | ||
| 4382 | + let end = s.timeline_duration(); | ||
| 4383 | + assert!( | ||
| 4384 | + s.place_stream_at(end, &asset("test_av2.mp4"), 0, None) | ||
| 4385 | + .expect("av2, placed right after a") | ||
| 4386 | + ); | ||
| 4387 | + if transition > 0 { | ||
| 4388 | + assert!(s.set_transition_out(Lane::V1, 0, transition)); | ||
| 4389 | + } | ||
| 4390 | + s | ||
| 4391 | + }; | ||
| 4392 | + let mut on = build(window); | ||
| 4393 | + let mut off = build(0); | ||
| 4394 | + | ||
| 4395 | + let fps = on.meta().frame_rate; | ||
| 4396 | + let clip0_len = on.lane_clips(Lane::V1)[0].len(); | ||
| 4397 | + assert!(clip0_len > window, "the fixture needs to outlast the window"); | ||
| 4398 | + let mid_of_window = clip0_len - window / 2; | ||
| 4399 | + let t = f64::from(mid_of_window) / fps; | ||
| 4400 | + | ||
| 4401 | + // One seek, then one frame -- exactly what a paused preview's single | ||
| 4402 | + // repaint asks the engine for. Polled rather than taken on the first | ||
| 4403 | + // call because the seek itself just restarted the decoder | ||
| 4404 | + // (`start_span`) and its first picture is not synchronous; once | ||
| 4405 | + // *a* frame lands this is the one tick under test. | ||
| 4406 | + let frame_at = |s: &mut PlaybackSession| -> crate::decode::Frame { | ||
| 4407 | + s.seek(t); | ||
| 4408 | + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20); | ||
| 4409 | + loop { | ||
| 4410 | + if let Some(f) = s.try_frame() { | ||
| 4411 | + return f; | ||
| 4412 | + } | ||
| 4413 | + assert!(std::time::Instant::now() < deadline, "no frame within 20s of the seek"); | ||
| 4414 | + std::thread::sleep(std::time::Duration::from_millis(10)); | ||
| 4415 | + } | ||
| 4416 | + }; | ||
| 4417 | + | ||
| 4418 | + let off_frame = frame_at(&mut off); | ||
| 4419 | + let on_frame = frame_at(&mut on); | ||
| 4420 | + assert_eq!(on_frame.index, off_frame.index, "same timeline position"); | ||
| 4421 | + assert_ne!( | ||
| 4422 | + on_frame.bgra, off_frame.bgra, | ||
| 4423 | + "paused inside the window, the dissolve must already show B blended in" | ||
| 4424 | + ); | ||
| 4425 | + } | ||
| 4426 | + | ||
| 4333 | #[test] | 4427 | #[test] |
| 4334 | fn boundary_seconds_round_trip_to_their_own_frame() { | 4428 | fn boundary_seconds_round_trip_to_their_own_frame() { |
| 4335 | for f in 0u32..300 { | 4429 | for f in 0u32..300 { |