fix(mediaplayer): target-exact seeks, clean backward seeks and true VOD end#979
Merged
Merged
Conversation
towneh
force-pushed
the
fix/mediaplayer-seek-fixes
branch
from
July 23, 2026 18:58
efc9188 to
5aacb1e
Compare
…eyframe run-up A container seek repositions the byte source to the sync sample at or before the target, and the pace clock then re-anchored on the first delivered sample — so the whole run-up played out at 1x. On a sparse-keyframe file the gap can be tens of seconds: a mid-GOP seek visibly restarted at the previous keyframe (or, with the present clock pinned, sat silent at the target) for as long as the gap was wide. A 1.1MB 46s capture with keyframes only at 0s and 31.25s reproduced it on every seek inside the first 31 seconds. Re-anchor the pace clock at the seek target when a demux leg takes the seek, so the run-up reads as late and flows at decode speed while everything from the target onwards paces at 1x as before. Both decoders then drop decoded frames short of the target instead of banking them - they exist only as references - so the run-up is never shown and the present clock releases on the first frame at or past the target. Audio needs nothing: the demuxer's audio cursor already lands at the target, and the PCM serve's clock-gated trim eats anything earlier. HLS-TS seeks inherit the same mechanism through the TS demuxer's take_seek, so they now land target-exact rather than at the start of the containing segment. Verified with a standalone decode harness against the sparse-keyframe repro (forward into the gap, backward, past the far keyframe), a normal-GOP progressive MP4, and a TS-HLS VOD: the run-up floods through in about a second, post-target frames bank for presentation, and audio resumes immediately. Presentation needs an in-Editor pass (the harness cannot drive Unity's render tick).
…n backward seeks The audio serve clock re-derives from video presents, and between a seek and the first post-seek present it still describes the pre-seek timeline. On a backward seek that clock is ahead of the target, so audio banked during the settle reads as long-stale and the clock-gated trim discards it - audibly, video resumed about a second before audio did, with the trim counter climbing through every settle. Forward seeks were immune only because their stale clock sits behind the target, which makes the serve hold rather than trim. Invalidate the offset at the seek notify on both platforms so the serve holds in either direction: post-seek audio banks through the settle and releases in sync with the first presented frame. Audio-only sources are unaffected - their offset never leaves the hold state to begin with.
ENDED fired when delivery finished rather than when presentation did, and the video decoder's reorder tail (seconds of content at low frame rates) was never flushed, so paced sources ended early. The demux loop now notifies the decoder end-of-stream (MFT drain on Windows, an EOS input plus a bounded output pump on Android) and drains presentation before raising ENDED: done once the decoder holds nothing more to show or serve and the reported position has settled, with an absolute cap as the escape hatch for a consumer that never presents. The presentation-pending probe compares against the last genuinely presented PTS rather than the reported position, which a seek snaps to the target before anything presents: a banked frame whose PTS lands exactly on the target would otherwise read as already shown, and ENDED could fire without showing it.
The fetcher thread exited at the endlist, so a seek after a VOD played to its end had nothing left to serve it. The fetcher now parks at VOD end and a seek revives it; end-of-stream is raised by the reader once the generations have settled, so a parked fetcher no longer reads as a finished stream, and producer_done again means the thread actually exited. An HLS VOD that cannot seek (the fMP4 variant) still exits rather than parking forever.
Backward seeks could flash pre-seek content and bounce the reported position: any pre-seek frame that slips through carries a PTS past a backward target, so it not only presents but can end the preroll cut and let the keyframe run-up present at decode speed. Three routes in, each closed: - Pre-seek tail AUs delivered between the seek request and the demuxer taking it are dropped at the engine sink by the seek_taken gate (mirroring the audio gate), and an await-keyframe gate in the decoders covers the HLS path the engine gate cannot see. - Frames still in flight through the AImageReader listener when the seek flush clears the ring are dropped by a seek-generation tag carried in the sub-microsecond digits of the surface timestamp. The tag is the feeder thread's generation, which only advances at the flush, so a pre-seek frame drained after the seek posts still carries the old tag. - The await-keyframe gate opened at the keyframe's submission, while a slipped tail AU's post-flush garbage frame could still be inside the codec. It now holds until the output carrying the keyframe's latched PTS emerges, with a bounded drain backstop so a dropped or re-stamped keyframe output degrades to one stale frame instead of wedging video. This applies to both decoders: Adreno emits post-flush mid-GOP garbage directly, and the Windows MFT reorder pipeline has the same post-submission window.
…s flagged The await-keyframe gate's drain backstop only counted once vAwaitKeyPts had been latched, and the latch only happens when a post-seek AU arrives flagged as a keyframe. A stream whose post-seek AUs never carry the flag (a container parsing gap, or a tail AU handed over in place of the keyframe) left the whole clear condition unreachable, so the gate held every subsequent frame unshown until the next seek: video froze indefinitely. Count drained outputs from the seek flush instead, so the backstop opens the gate after the bounded wait whether or not a keyframe PTS was ever latched. The designed PTS-match clear is unchanged, and past the backstop the preroll cut still suppresses the run-up, so the degraded case stays at worst one stale frame. Applies to both the Android and Windows backends.
…OD seek row The row only required ENDED to fire, which would also pass if the banked tail were discarded first; assert instead that the position walks to the true duration and the final content presents before ENDED is raised.
The Android software MP3 decoder stamps its output buffers from an internal anchor set by the first input timestamp plus a running sample count, ignoring later input PTS jumps, and the anchor survives an AMediaCodec_flush. After a seek the demuxer re-bases its input PTS to the target, but the codec output kept pre-seek time, so the PCM ring banked post-seek audio at stale positions: on an audio-only MP3 the position bar froze at the seek target after a forward seek, and a backward seek double-counted the re-read stretch until the reported position overran the file's duration. Measure the decoder's timeline drift at each seek instead of trusting its output timestamps: latch the first input PTS queued after the seek flush, take the difference against the first output that follows, and add that bias to every output PTS banked into the ring. Decoders that pass input timestamps through measure a bias of zero, so AAC and Opus behave as before.
…e pace gate The pre-seek audio drop in sink_audio_frame runs before pace_gate, and the gate parks the demux thread for up to the pace lead without observing seeks. A frame that passed the drop and then slept across a seek was submitted with its pre-seek PTS: it triggered the decoder's seek flush early, so the post-flush timeline re-anchor measured the decoder's drift against a stale input instead of the first post-seek one — a measurement of roughly zero, leaving that seek's audio banked at pre-seek time (position bar frozen at the target, or overrunning the duration after a backward seek) — and the frame itself survived into the flushed ring as a stale front chunk. Re-check the drop after the pace hold so the frame dies before submission. The first frame to reach the decoder after a seek is then genuinely post-seek, which is what the timeline re-anchor needs to measure against.
towneh
force-pushed
the
fix/mediaplayer-seek-fixes
branch
from
July 23, 2026 20:26
5aacb1e to
5820713
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A batch of VOD seek and end-of-stream fixes in the native plugin. It started from the 2026-07-20 test night reports; verifying those on Windows and Quest surfaced the rest. All the symptoms below are present in the current release binaries, and the fixes are C and C++ only.
TESTING.md's seek rows now spell out target-exact landings, the sparse-keyframe adversarial case, seeking from the tail of an HLS VOD, and that the tail must actually present before ENDED fires.Required checks
All boxes below must be ticked before this PR can merge. If a check is genuinely N/A, tick it anyway and explain under Notes.
TransformAccessArrayor are otherwise batched. I have not added per-frametransform.position/transform.rotation/transform.localPositioncalls inside loops. Whenever I need both position and rotation, I use the combined APIs —SetPositionAndRotation/SetLocalPositionAndRotationfor writes,GetPositionAndRotation/GetLocalPositionAndRotationfor reads — instead of two separate property accesses; the combined call does one local-to-world matrix traversal instead of two.Resources.Load, no direct asset references that pull large content into memory on scene load.GetComponent/AddComponentwhere avoidable — Where unavoidable, the result is cached on a field, and anyGetComponent<T>is replaced withTryGetComponent<T>(out var x)— bareGetComponentwill be denied.TryGetComponentis the modern API (Unity 2019.2+) and skips the Editor-only GC allocationGetComponentcauses when a component is missing: Unity wraps thenullreturn in a managed "fake null" object so its overloaded==operator can still detect destroyed C++ objects, and constructing that wrapper allocates;TryGetComponentreturns aboolplusoutparameter and never builds the wrapper. None of these calls run insideUpdate,LateUpdate,FixedUpdate, jobs, or other per-frame code paths.BasisEventDriver— Any new per-frame work hooks intoBasisEventDriverrather than adding standaloneUpdate/LateUpdate/FixedUpdatecallbacks on a MonoBehaviour.BasisEventDriveris bulletproof, or guarded bytry/catch—BasisEventDriverruns the single per-frame tick that drives the whole framework (network apply, local player sim, blendshapes, JigglePhysics, nameplates, and more) as one sequential chain. An unhandled exception anywhere in that chain aborts the rest of the tick, so every step after the throwing one is silently skipped for that frame. New work added to the driver must either be guaranteed not to throw, or be wrapped in atry/catchthat contains the failure and surfaces it throughBasisDebug— logged once / rate-limited, never every frame (see the existingHVRBasisBuiltInAddresses.Simulate()guard for the pattern). Expect this to be scrutinized closely in review.{ get; set; }properties or access lockdowns — Public fields are fine; Basis is meant to be read and modified freely, so don't wall things offprivate/internalwithout a real reason. Don't wrap a field in{ get; set; }when the accessors do nothing — property accessors have a real performance cost vs direct field access, and the lead maintainer prefers plain fields (or a method / setter-only property when only the setter needs logic) over a noop-getter pair. For.Instancesingletons, callers reassigningType.Instanceis allowed; if that would break your code, log a warning or throw — don't block the assignment. Locking down access is not your call.BasisLocalCameraDriver— Code that needs the local camera (transform, projection, rig data, etc.) pulls it fromBasisLocalCameraDriverrather than looking one up itself. Don't roll a separate camera discovery path.BasisDebug— All new logging calls go throughBasisDebug.Log/BasisDebug.LogWarning/BasisDebug.LogError(with an appropriateLogTag) instead ofUnityEngine.Debug.Log/Debug.LogWarning/Debug.LogError.BasisDebugroutes through Basis's tagged, color-coded logger and respects the project-wideLoggingDisabledtoggle so logging can be killed at runtime; bareDebug.Logcalls bypass that and will be denied.FindObjectOfType/FindObjectsOfType/GameObject.Find/FindGameObjectsWithTagto locate what it depends on. References are wired in — registered through an existing manager/driver, injected at init, or passed in by the caller — rather than discovered by scanning the scene at runtime. If a scene scan is genuinely unavoidable, justify it under Notes.newon reference types, no LINQ, nostringconcatenation/interpolation, no boxing, noforeachover interface-typed collections. Allocate once at init and reuse the buffer.BasisDebug. Hot-path logging floods the console and incurs cost on every frame regardless of whether the message is filtered out downstream. If a hot-path log is needed while iterating, gate it behind#if UNITY_EDITORand remove (or leave gated) before merge..Count(lists) /.Length(arrays) into a localintbefore the loop instead of re-reading the property each iteration. PreferT[](with a separate length int when the array is over-sized) overList<T>where the data is hot — Unity's mono BCL doesn't exposeCollectionsMarshal.AsSpan(List<T>), so a list can't be fed intoSpan<T>/ unsafe paths cleanly. Where the perf justifies it, drop intoSpan<T>/reflocals /Unsafe.As/unsafepointer code to skip bounds checks and copies, and call out the invariants you're relying on under Notes so reviewers can sanity-check them.Testing details
Tick the platforms you actually tested on. Leave the rest unticked — these are informational and do not block merge.
Input / control mode coverage:
Where applicable, confirm these flows still work after your changes:
Notes
Native~) plusTESTING.md; no C# is touched, so the Unity-specific required checks are ticked as N/A on that basis.TESTING.md's seek and end-of-stream rows on both halves. Editor (Windows): forward and backward seeks on a normal-GOP MP4, a sparse-keyframe MP4 (keyframes only at 0s and 31s, the adversarial case) and an HLS-TS VOD, each played to its true duration. Quest Pro build: the same three lanes with backward seeks mid-file and from the tail, landings and ends confirmed against the diagnostics CSV (single clean position jump per seek, ends at true duration).