From fb331507e51ddbbba7e05dea091ecdc464030769 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Wed, 8 Jul 2026 09:16:46 -0400 Subject: [PATCH] Scrubbing accelerates on long videos --- CHANGELOG.md | 1 + .../src/components/VideoPlayerDev/README.md | 1 + .../VideoPlayerDev/TransportController.bs | 61 ++++++++++++++++++- .../tests/VideoPlayerDev/PlayerInvariants.bs | 18 +++++- .../TransportController.spec.bs | 54 +++++++++++++++- .../TransportControllerCompleteness.spec.bs | 13 +++- 6 files changed, 139 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6ee5c4d..f6b31856 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Video Player HUD now focuses initially on the trackbar, instead of the pause button - Pressing back cancels seek, hides UI, then closes the video player +- Scrubbing accelerates on long videos ## [0.48.1] - 2026-07-04 diff --git a/playlet-lib/src/components/VideoPlayerDev/README.md b/playlet-lib/src/components/VideoPlayerDev/README.md index fb115279..f544355b 100644 --- a/playlet-lib/src/components/VideoPlayerDev/README.md +++ b/playlet-lib/src/components/VideoPlayerDev/README.md @@ -58,6 +58,7 @@ the exhaustive + seeded-fuzz suite (`TransportControllerCompleteness.spec.bs`). | B5 | `scanDir ∈ {-1,0,1}`; `scanLevel ∈ [0,3]`; `scanLevel ≥ 1 ⟹ scan/liveDvr` | | B6 | scrub and scan scratch never both set | | B8 | `scrubHeldDir ∈ {-1,0,1}` | +| B9 | hold-accel scratch bounded: `scrubHoldTicks ≥ 0`; `scrubStepMs ∈ [storyboardStepMs, cap(duration)]` | (`seekSettling ⟹ a newly-entered transport defers its freeze-pause` is a *transition* property, not a snapshot predicate, so it is not in this catalog — it is enforced by `FreezeForTransport` and tested at the seam as diff --git a/playlet-lib/src/components/VideoPlayerDev/TransportController.bs b/playlet-lib/src/components/VideoPlayerDev/TransportController.bs index 4357cc87..27839973 100644 --- a/playlet-lib/src/components/VideoPlayerDev/TransportController.bs +++ b/playlet-lib/src/components/VideoPlayerDev/TransportController.bs @@ -64,6 +64,15 @@ namespace TransportController const SCRUB_REPEAT_INITIAL_MS = 510 const SCRUB_REPEAT_MS = 120 + ' Hold-to-seek acceleration. A held Left/Right steps the flat storyboard grid for the first + ' RAMP_DELAY ticks — a fine-adjust window before speed builds — then each tick grows the step by + ' 1/RAMP_DIVISOR of its remaining gap to the cap: an exponential approach to a terminal velocity. + ' The cap, CAP_SQRT_COEFF * sqrt(durationMs) per tick, scales with the SQUARE ROOT of the duration, + ' so the hold time to cross a whole video grows sublinearly: a many-hour VOD stays reachable in a + ' few seconds of holding without making short videos twitchy. + const SCRUB_HOLD_RAMP_DELAY_TICKS = 8 + const SCRUB_HOLD_RAMP_DIVISOR = 11 + const SCRUB_HOLD_CAP_SQRT_COEFF = 61 const SCAN_LEVEL_MIN = 1 const SCAN_LEVEL_MAX = 3 const LIVE_DVR_STEP_MS = 5000 @@ -105,12 +114,22 @@ namespace TransportController return ms >= TransportController.LIVE_EDGE_SEEK_MS - TransportController.LIVE_EDGE_SENTINEL_SLACK_MS end function + ' The hold-seek per-tick step cap (see the SCRUB_HOLD_* constants). Floors at the storyboard step + ' so a short or unknown duration keeps the flat grid step. + function ScrubHoldCapMs(durationMs as integer, storyboardStepMs as integer) as integer + cap = Int(TransportController.SCRUB_HOLD_CAP_SQRT_COEFF * Sqr(durationMs)) + if cap < storyboardStepMs + return storyboardStepMs + end if + return cap + end function + ' The transport state machine. class Controller ' ---- content facts (set once at contentReady) ---- isLive as boolean = false private hasStoryboards as boolean = false - private storyboardStepMs as integer = 2000 ' VOD per-tap step + scan base (storyboard L2 interval) + storyboardStepMs as integer = 2000 ' VOD per-tap step + scan base (storyboard L2 interval) ' Live DVR window length (0 = unknown). The generated DASH anchors t=0 to the window start, so the ' rewind floor is edge - window once the session outlives the window (content below it has expired). private dvrWindowMs as integer = 0 @@ -146,6 +165,8 @@ namespace TransportController ' ---- private scratch ---- scanDir as integer = 0 scrubHeldDir as integer = 0 + scrubHoldTicks as integer = 0 ' consecutive hold-repeat ticks of the current hold (drives the accel ramp) + scrubStepMs as integer = 2000 ' current per-step size: storyboardStepMs until the hold ramp grows it edgeMs as integer = 0 ' the live edge, fed by OnLiveEdge (the coordinator's wall-clock computation) ' Inherent live latency: the smallest CONFIRMED (edge - position) observed this content — the closest ' we have ever been IS how close this stream can get. The offset reads relative to it, so steady @@ -170,6 +191,8 @@ namespace TransportController if storyboardStepMs > 0 m.storyboardStepMs = storyboardStepMs end if + m.scrubHoldTicks = 0 ' new content -> a fresh hold ramp + m.scrubStepMs = m.storyboardStepMs m.dvrWindowMs = 0 ' new content -> unknown window until SetLiveDvrWindow m.edgeMs = 0 m.liveMinGapMs = -1 ' new content -> re-learn the inherent latency @@ -233,6 +256,10 @@ namespace TransportController if storyboardStepMs > 0 m.storyboardStepMs = storyboardStepMs end if + ' a grown grid step raises the hold-step floor (a mid-hold ramp above it just keeps its speed). + if m.scrubStepMs < m.storyboardStepMs + m.scrubStepMs = m.storyboardStepMs + end if end function function OnReady() as void @@ -294,6 +321,11 @@ namespace TransportController end if m.durationMs = ms m.ClampIdleCursor() + ' a duration that regresses mid-hold can leave the ramped step above the new cap — re-clamp. + cap = TransportController.ScrubHoldCapMs(m.durationMs, m.storyboardStepMs) + if m.scrubStepMs > cap + m.scrubStepMs = cap + end if end function ' The live edge (ms on the manifest timeline), computed by the coordinator from the wall clock anchored @@ -414,6 +446,8 @@ namespace TransportController m.StopTimer(TransportController.Timer.Scan) end if m.scrubHeldDir = dir + m.scrubHoldTicks = 0 ' every fresh press restarts the accel ramp: a tap always steps the flat grid + m.scrubStepMs = m.storyboardStepMs if dir < 0 m.glyph = TransportController.Glyph.SkipBack else @@ -424,7 +458,27 @@ namespace TransportController end function function ScrubStep(dir as integer) as void - m.cursorMs = m.Clamp(m.cursorMs + dir * m.storyboardStepMs, 0, m.durationMs) + m.cursorMs = m.Clamp(m.cursorMs + dir * m.scrubStepMs, 0, m.durationMs) + end function + + ' One hold-repeat tick of the acceleration ramp: flat for the first RAMP_DELAY ticks, then the step + ' approaches the duration-scaled cap by 1/RAMP_DIVISOR of the remaining gap per tick (the last + ' sliver, where integer division floors to 0, snaps to the cap). + function AdvanceScrubHold() as void + m.scrubHoldTicks = m.scrubHoldTicks + 1 + if m.scrubHoldTicks <= TransportController.SCRUB_HOLD_RAMP_DELAY_TICKS + return + end if + cap = TransportController.ScrubHoldCapMs(m.durationMs, m.storyboardStepMs) + if m.scrubStepMs >= cap + m.scrubStepMs = cap + return + end if + grow = (cap - m.scrubStepMs) \ TransportController.SCRUB_HOLD_RAMP_DIVISOR + if grow < 1 + grow = cap - m.scrubStepMs + end if + m.scrubStepMs = m.scrubStepMs + grow end function ' ------------------------------------------------------------------------------------------------ @@ -702,6 +756,8 @@ namespace TransportController m.scanLevel = 0 m.scanDir = 0 m.scrubHeldDir = 0 + m.scrubHoldTicks = 0 + m.scrubStepMs = m.storyboardStepMs m.StopTimer(TransportController.Timer.ScrubRepeat) m.StopTimer(TransportController.Timer.Scan) m.cursorMs = m.positionMs @@ -743,6 +799,7 @@ namespace TransportController function Tick(timerId as integer) as void if timerId = TransportController.Timer.ScrubRepeat if m.transportMode = TransportController.Mode.Scrub and m.scrubHeldDir <> 0 + m.AdvanceScrubHold() m.ScrubStep(m.scrubHeldDir) m.ArmTimer(TransportController.Timer.ScrubRepeat, TransportController.SCRUB_REPEAT_MS, false) else if m.transportMode = TransportController.Mode.LiveDvr and m.scrubHeldDir <> 0 diff --git a/playlet-lib/src/source/tests/VideoPlayerDev/PlayerInvariants.bs b/playlet-lib/src/source/tests/VideoPlayerDev/PlayerInvariants.bs index 5f3c79a2..68bfeb04 100644 --- a/playlet-lib/src/source/tests/VideoPlayerDev/PlayerInvariants.bs +++ b/playlet-lib/src/source/tests/VideoPlayerDev/PlayerInvariants.bs @@ -15,8 +15,8 @@ import "pkg:/components/VideoPlayerDev/TransportController.bs" namespace PlayerInvariants - ' Snapshot keys: transportMode, glyph, scanLevel, scanDir, scrubHeldDir, cursorMs, positionMs, durationMs, - ' edgeMs, liveOffsetMs, atLiveEdge, isLive, replayPending. + ' Snapshot keys: transportMode, glyph, scanLevel, scanDir, scrubHeldDir, scrubHoldTicks, scrubStepMs, + ' storyboardStepMs, cursorMs, positionMs, durationMs, edgeMs, liveOffsetMs, atLiveEdge, isLive, replayPending. function CheckBrain(s as object) as object hi = s.durationMs if s.isLive @@ -57,6 +57,18 @@ namespace PlayerInvariants return Fail("INV-B6") end if + ' INV-B9 — the hold-accel scratch is bounded: the tick count never goes negative and the per-tick + ' step stays within [storyboardStepMs, the duration-scaled cap] (same formula the brain ramps to). + if s.scrubHoldTicks < 0 + return Fail("INV-B9") + end if + if s.scrubStepMs < s.storyboardStepMs + return Fail("INV-B9") + end if + if s.scrubStepMs > TransportController.ScrubHoldCapMs(s.durationMs, s.storyboardStepMs) + return Fail("INV-B9") + end if + ' INV-B4 — the glyph is legal for the current mode. if not GlyphOkForMode(s.transportMode, s.glyph) return Fail("INV-B4") @@ -94,7 +106,7 @@ namespace PlayerInvariants ' The brain INV ids this module enforces — the meta-test pins this set against the VideoPlayerDev README so a ' catalog row can't be added without an implementation (or vice-versa). function BrainIds() as object - return ["INV-B1", "INV-B3", "INV-B4", "INV-B5", "INV-B6", "INV-B8"] + return ["INV-B1", "INV-B3", "INV-B4", "INV-B5", "INV-B6", "INV-B8", "INV-B9"] end function end namespace diff --git a/playlet-lib/src/source/tests/VideoPlayerDev/TransportController.spec.bs b/playlet-lib/src/source/tests/VideoPlayerDev/TransportController.spec.bs index 98eb42fd..9e4da9ab 100644 --- a/playlet-lib/src/source/tests/VideoPlayerDev/TransportController.spec.bs +++ b/playlet-lib/src/source/tests/VideoPlayerDev/TransportController.spec.bs @@ -2,7 +2,8 @@ ' No SceneGraph node, no Video node, no device clock: the suite seeds content, pushes commands, and ' fires timers by calling Tick() directly (the controller publishes the schedule; here we are the clock). ' Every behavior is asserted, including the input x mode matrix (not just each mode's happy path): -' - scrub enter/step/hold/commit/cancel; scan ladder/wrap/flip/cadence/clamp/commit. +' - scrub enter/step/hold (with the duration-scaled acceleration ramp)/commit/cancel; scan +' ladder/wrap/flip/cadence/clamp/commit. ' - LIVE: Left/Right = +/-5s frozen step (scrubRepeat); Rev/Fwd = 3-level scan LADDER (scan timer) climbing ' L1->2->3->1, reverse clamps at the DVR floor, forward auto-resumes at the edge; offset counts up frozen. ' - LIVE step<->ladder sub-mode transitions clear the other sub-mode's scratch (no stale-tick bleed). @@ -117,6 +118,57 @@ namespace Tests m.assertEqual(m.t.cursorMs, 66000) end function + @it("hold acceleration: flat through the ramp delay, then the step grows to the duration-scaled cap") + function _() + ' 10h VOD: 36000000ms is exactly representable in single precision, so Sqr is exact and the + ' cap is deterministic: 61 * Sqr(36000000) = 61 * 6000 = 366000ms per tick. + m.t.OnDuration(36000000) + m.t.HandleKey(TransportController.Command.Right, true) ' keydown -> 62000 + ' the first RAMP_DELAY ticks step the flat grid (the fine-adjust window). + for i = 1 to TransportController.SCRUB_HOLD_RAMP_DELAY_TICKS + m.t.Tick(TransportController.Timer.ScrubRepeat) + end for + m.assertEqual(m.t.cursorMs, 62000 + TransportController.SCRUB_HOLD_RAMP_DELAY_TICKS * 2000) + m.assertEqual(m.t.scrubStepMs, 2000) + ' the next tick starts the ramp: the step grows, monotonically, never past the cap. + prev = m.t.scrubStepMs + for i = 1 to 150 + m.t.Tick(TransportController.Timer.ScrubRepeat) + m.assertTrue(m.t.scrubStepMs >= prev, "the hold step must never shrink mid-hold") + m.assertTrue(m.t.scrubStepMs <= 366000, "the hold step must never exceed the cap") + prev = m.t.scrubStepMs + end for + m.assertEqual(m.t.scrubStepMs, 366000) ' saturated at the cap + end function + + @it("hold acceleration: releasing and re-pressing restarts the ramp at the flat grid step") + function _() + m.t.OnDuration(36000000) + m.t.HandleKey(TransportController.Command.Right, true) + for i = 1 to 30 ' well into the ramp + m.t.Tick(TransportController.Timer.ScrubRepeat) + end for + m.assertTrue(m.t.scrubStepMs > 2000) + m.t.HandleKey(TransportController.Command.Right, false) ' release + before = m.t.cursorMs + m.t.HandleKey(TransportController.Command.Right, true) ' fresh press + m.assertEqual(m.t.scrubStepMs, 2000) ' ramp restarted + m.assertEqual(m.t.cursorMs, before + 2000) ' the fresh press steps the flat grid + end function + + @it("hold acceleration: an unknown duration never accelerates (the cap floors at the grid step)") + function _() + t = new TransportController.Controller() + t.SetContent(false, 2000, true) + t.OnPosition(0) + t.OnReady() ' duration never learned + t.HandleKey(TransportController.Command.Right, true) + for i = 1 to 50 + t.Tick(TransportController.Timer.ScrubRepeat) + end for + m.assertEqual(t.scrubStepMs, 2000) + end function + @it("keyUp stops the hold-repeat and clears the glyph") function _() m.t.HandleKey(TransportController.Command.Right, true) diff --git a/playlet-lib/src/source/tests/VideoPlayerDev/TransportControllerCompleteness.spec.bs b/playlet-lib/src/source/tests/VideoPlayerDev/TransportControllerCompleteness.spec.bs index 60182087..da9663d7 100644 --- a/playlet-lib/src/source/tests/VideoPlayerDev/TransportControllerCompleteness.spec.bs +++ b/playlet-lib/src/source/tests/VideoPlayerDev/TransportControllerCompleteness.spec.bs @@ -49,6 +49,9 @@ namespace Tests scanLevel: t.scanLevel scanDir: t.scanDir scrubHeldDir: t.scrubHeldDir + scrubHoldTicks: t.scrubHoldTicks + scrubStepMs: t.scrubStepMs + storyboardStepMs: t.storyboardStepMs cursorMs: t.cursorMs positionMs: t.positionMs durationMs: t.durationMs @@ -238,7 +241,8 @@ namespace Tests function _() good = { transportMode: TransportController.Mode.Idle, glyph: TransportController.Glyph.None - scanLevel: 0, scanDir: 0, scrubHeldDir: 0, cursorMs: 1000, positionMs: 1000 + scanLevel: 0, scanDir: 0, scrubHeldDir: 0, scrubHoldTicks: 0, scrubStepMs: 2000 + storyboardStepMs: 2000, cursorMs: 1000, positionMs: 1000 durationMs: 600000, edgeMs: 0, liveOffsetMs: 0, atLiveEdge: true, isLive: false, replayPending: false } m.assertTrue(PlayerInvariants.CheckBrain(good).ok, "good snapshot should pass") @@ -250,6 +254,9 @@ namespace Tests m.assertEqual(m.Break(good, "scanDir", 2).failed, "INV-B5") m.assertEqual(m.Break(good, "scanLevel", 1).failed, "INV-B5") ' level>=1 but mode=idle m.assertEqual(m.Break(good, "scrubHeldDir", 5).failed, "INV-B8") + m.assertEqual(m.Break(good, "scrubHoldTicks", -1).failed, "INV-B9") + m.assertEqual(m.Break(good, "scrubStepMs", 1000).failed, "INV-B9") ' below the storyboard step + m.assertEqual(m.Break(good, "scrubStepMs", 99999999).failed, "INV-B9") ' above the duration cap ' INV-B6 needs BOTH held scratch non-zero. bleed = {} @@ -259,7 +266,7 @@ namespace Tests m.assertEqual(PlayerInvariants.CheckBrain(bleed).failed, "INV-B6") ' the catalogued id set is pinned (the VideoPlayerDev README must match BrainIds). - m.assertEqual(PlayerInvariants.BrainIds(), ["INV-B1", "INV-B3", "INV-B4", "INV-B5", "INV-B6", "INV-B8"]) + m.assertEqual(PlayerInvariants.BrainIds(), ["INV-B1", "INV-B3", "INV-B4", "INV-B5", "INV-B6", "INV-B8", "INV-B9"]) end function ' ---------- meta: the model accounts for EVERY mutable brain field (no unmodeled axis) ---------- @@ -275,7 +282,7 @@ namespace Tests ' FSM AXES — discrete state the exhaustive matrix enumerates + the invariants constrain. axes = ["transportMode", "glyph", "scanLevel", "scanDir", "scrubHeldDir", "atLiveEdge", "isLive", "ready", "hasStoryboards", "replayPending", "seekSettling"] ' CONTINUOUS — equivalence-classed by the guard predicates (cursor/edge/duration compares), not enumerated. - continuous = ["cursorMs", "positionMs", "durationMs", "edgeMs", "liveOffsetMs", "storyboardStepMs", "replayTargetMs", "dvrWindowMs", "liveMinGapMs", "liveMinCandidateMs"] + continuous = ["cursorMs", "positionMs", "durationMs", "edgeMs", "liveOffsetMs", "storyboardStepMs", "scrubHoldTicks", "scrubStepMs", "replayTargetMs", "dvrWindowMs", "liveMinGapMs", "liveMinCandidateMs"] ' OUTPUT / IO scratch — drained by the coordinator, not state the FSM branches on. io = ["videoIntent", "intentSeq", "commitSeq", "finishSeq", "timerCmdAction", "timerCmdId", "timerCmdDelayMs", "timerCmdRepeat"]