Skip to content

media-playback: Seek to the requested time, not the keyframe before it - #479

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/media-seek-keyframe
Oct 13, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/media-seek-keyframe

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

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() in media.c seeks the demuxer with AVSEEK_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.
  • Only seeks the user asks for do this. A restart (mp_media_reset()) and seeks in files without a duration (AVSEEK_FLAG_FRAME, e.g. live inputs) are unchanged.
  • The cached path (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.

  1. test/cmocka: Media Source seek lands on the keyframe before the target: test_media_seek_keyframe writes 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:

    • paused seeks to 1500 ms and 5500 ms: the frame shown and the media time (the reporters' cases);
    • a seek while playing: the first frame after it;
    • a seek in a file with audio: the frame, the media time and the first audio level after it;
    • a seek to the last frame, after the last keyframe;
    • the cached ("fully decode") path;
    • a guard: a seek onto a keyframe, which already works.

    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.

  2. 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_BACKWARD in 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_LIBOBS OFF 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 -V shows 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.
  • Full ctest at GREEN, rebased on current master: 145 of 145 in each lane.
  • clang-format is clean.

Not tested:

  • The fixed code in the app itself: the frontend is not built here, and the release's FFmpeg differs from the one on this machine, so the fixed plugin cannot be dropped into the AppImage. The test drives the same media-playback calls the Media Source makes (media_playback_seek(), media_playback_play_pause(), media_playback_get_current_time()). Will check in the first release that includes it.
  • Hardware decoding.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

@rdoupe
rdoupe requested a review from zackees as a code owner October 13, 2026 07:17
rdoupe and others added 2 commits October 13, 2026 00:21
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"}
@zackees

zackees commented Oct 13, 2026

Copy link
Copy Markdown
Member

Verified locally under xvfb-run (Tier 2 image + mesa):

  • RED a90f217: test_media_seek_keyframe fails 6 of 7 (paused/playing/cached seek between keyframes, paused_seek_into_second_gop, paused_seek_to_last_frame, seek_with_audio_resumes_audio_at_target)
  • GREEN 32a8a0a: 7/7. It needs a display, so plain CI skips it.
  • Rebased on master, local gate passed; pushed as 85aa5c2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: media-playback: Media Source seek lands on the keyframe before the requested time (upstream obsproject/obs-websocket#1190)

2 participants