Repository navigation
media-playback: Seek to the requested time, not the keyframe before it - #479
Merged
Merged
Conversation
Seeking a Media Source (the seek bar, or obs-websocket's SetMediaInputCursor) goes to the keyframe at or before the requested time and plays on from there: on a file with a keyframe every 3 seconds, 1500 ms shows frame 0 and reports a media time of 33 ms, 5500 ms shows the frame at 3000 ms. media.c seeks the demuxer backward to that keyframe, flushes the decoders and presents the first frame they return. With "full_decode" (the cached path) a seek goes to the start of the file instead: cache.c compares the seek position in microseconds with frame timestamps in nanoseconds. The test writes a 30 fps MPEG-4 clip with a keyframe every 90 frames whose frames carry their number in two flat luma blocks (one clip also has AAC audio whose level steps up every 500 ms), plays it through media-playback and checks the frame shown and the media time after paused and playing seeks and a seek to the last frame, the first audio after a seek, and the cached path. A seek onto a keyframe is a guard that passes before the fix. No display or graphics are needed, so it runs in CI on every platform. Reported upstream in obsproject/obs-websocket#1190 by casperboone. Refs obs-rust#478. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A seek moved the demuxer back to the keyframe at or before the target (AVSEEK_FLAG_BACKWARD), flushed the decoders and presented the first frame they returned, so the Media Source showed and played from that keyframe and reported its time: on a file with a keyframe every 3 seconds, 1500 ms went to 0 and 5500 ms to 3000. Keep the backward seek, which the decoder needs, and drop the decoded video and audio frames that end at or before the target until both streams have a frame at the target. Only seeks the user asks for do this; a restart (mp_media_reset) is unchanged, as are seeks in files without a duration (AVSEEK_FLAG_FRAME). The fully decoded (cached) path seeked to the start of the file instead: cache.c compared the seek position in microseconds with frame timestamps in nanoseconds. Convert the position first. Reported upstream in obsproject/obs-websocket#1190 by casperboone. Fixes obs-rust#478. Co-Authored-By: Claude Opus 5.5 <[email protected]> Local-Gate: v1 tree=7a30cac39ed6fb8e42a2f64568cd5b6b54a7a26e secs=399 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791877110,"gate":"rust/all/fmt","host":"linux-x86_64","key":"3e6442e4143247d9c2346fb8a9e86179da77d89da5ba16ed189e1347ba453510","lane":"lint","parents":["095f79988bc68784c7cd57a48f09f1a29bf1ab03"],"secs":180,"stamp":"5dfd36c47aa47876dd78d066c0a5fcfd","tree":"7a30cac39ed6fb8e42a2f64568cd5b6b54a7a26e","v":1,"via":"run"} Ci-Attestation: {"at":1791877110,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"3e6442e4143247d9c2346fb8a9e86179da77d89da5ba16ed189e1347ba453510","lane":"lint","parents":["095f79988bc68784c7cd57a48f09f1a29bf1ab03"],"secs":180,"stamp":"f62f4c35b0f05dbb1dcb3dbba1c83cb4","tree":"7a30cac39ed6fb8e42a2f64568cd5b6b54a7a26e","v":1,"via":"run"} Ci-Attestation: {"at":1791877110,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"eaa35d84c0669fb309e3c529ae1ba5e920a053698c381bf6b4123ef634fbf18a","lane":"rust-test","parents":["095f79988bc68784c7cd57a48f09f1a29bf1ab03"],"secs":137,"stamp":"4542d9282230d5351e6dc6a074639c16","tree":"7a30cac39ed6fb8e42a2f64568cd5b6b54a7a26e","v":1,"via":"run"} Ci-Attestation: {"at":1791877110,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"18c66903cfe61006026f1507536b3d486b52226dd36ca84ff5b516de845249b0","lane":"tier2-linux","parents":["095f79988bc68784c7cd57a48f09f1a29bf1ab03"],"secs":81,"stamp":"60af4efa2ba2cf2804df14517b9fedbd","tree":"7a30cac39ed6fb8e42a2f64568cd5b6b54a7a26e","v":1,"via":"run"}
Member
|
Verified locally under xvfb-run (Tier 2 image + mesa):
|
zackees
force-pushed
the
fix/media-seek-keyframe
branch
from
October 13, 2026 07:38
32a8a0a to
85aa5c2
Compare
This was referenced Oct 13, 2026
This was referenced Oct 22, 2026
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.
Description
A Media Source seek now lands on the requested time, not on the keyframe before it. Before, seeking (the seek bar, or obs-websocket
SetMediaInputCursor) showed and played from the keyframe at or before the target and reported that keyframe's time: on a file with a keyframe every 3 seconds, 1500 ms went to 0 and 5500 ms to 3000. With "fully decode" on, every seek went back to the start of the file.seek_to()inmedia.cseeks the demuxer withAVSEEK_FLAG_BACKWARD, which lands on the keyframe at or before the target, flushes the decoders and presents the first frame they return. The fix keeps the backward seek (decoding has to start at a keyframe) and drops the decoded video and audio frames that end at or before the target until both streams have a frame at the target. Video and audio stay in step, and the media time follows.mp_media_reset()) and seeks in files without a duration (AVSEEK_FLAG_FRAME, e.g. live inputs) are unchanged.cache.c, "fully decode") compared the seek position, in microseconds, with frame timestamps in nanoseconds, so it always picked a frame near the start. The position is now converted first, including the speed scaling the timestamps carry.A seek far from the previous keyframe now decodes the frames in between before showing the target, as video editors do. On typical files (keyframes every 2 to 10 seconds) that is a short pause on the media thread.
test/cmocka: Media Source seek lands on the keyframe before the target:test_media_seek_keyframewrites a 30 fps MPEG-4 clip with a keyframe every 90 frames, each frame carrying its number in flat luma blocks (one clip also has AAC audio whose level steps up every 500 ms), and plays it through media-playback, the code behind the Media Source. The cases are:6 of 7 fail today: 1500 ms shows frame 0 at 33 ms, 5500 ms shows frame 90 at 3033 ms, and the cached seek shows frame 1.
media-playback: Seek to the requested time, not the keyframe before it: the fix. 7 of 7 pass.Motivation and Context
Fixes #478 (upstream obsproject/obs-websocket#1190, reported by casperboone). lrq51678, jwetzell and chnoma reported the same there; chnoma traced it to
AVSEEK_FLAG_BACKWARDin media-playback, which is why the Media Source seek bar does it too and the VLC source does not.Why this one: four users over three years, on macOS and Linux, with a clear wrong result (the requested time is ignored), and it reproduces in the current release.
How Has This Been Tested?
The current release AppImage (33.0.0-beta6-799, Rust libobs), headless under a private Xvfb with obs-websocket: a Media Source on a 30 fps file with a keyframe every 3 seconds and the frame number drawn on each frame. Paused seeks to 1500, 2000, 5500 and 7000 ms reported 33, 33, 3033 and 6033 ms, and screenshots showed frames 0, 0, 90 and 180 (expected 45, 60, 165 and 210). With "fully decode" on, seeks to 1500 and 5500 ms both went back to the start.
Automated, in both libobs lanes (
ENABLE_RUST_LIBOBSOFF and ON), on current master:test_media_seek_keyframe: 6 of 7 cases fail at the RED commit and 7 of 7 pass at GREEN in each lane.ctest -Vshows every case run, none skipped. It needs no display (7 of 7 also pass with no display at all), so it runs in CI on every platform; it uses only encoders built into FFmpeg (MPEG-4, AAC) and checks the generated file's keyframes before the cases run.Not tested:
media_playback_seek(),media_playback_play_pause(),media_playback_get_current_time()). Will check in the first release that includes it.Types of changes
Checklist:
🤖 Generated with Claude Code