Skip to content

fix(linux): keep VA-API streams alive through decode errors and stale backlogs - #1415

Closed
TheWeaklessOne wants to merge 4 commits into
OpenCloudGaming:devfrom
TheWeaklessOne:fix/linux-vaapi-decode-recovery
Closed

TheWeaklessOne wants to merge 4 commits into
OpenCloudGaming:devfrom
TheWeaklessOne:fix/linux-vaapi-decode-recovery

Conversation

@TheWeaklessOne

Copy link
Copy Markdown

Three problems with hardware decoding through FFmpeg's VA-API path on Linux, seen on an Odin 2 Portal (Qualcomm iris through the msm VA driver) at high bitrates:

  • A picture the hardware failed to decode came back from FFmpeg as a generic backend error (EIO from a failed vaEndPicture/vaSyncSurface, or InvalidData). That made the session treat a recoverable picture as a dead decoder.
  • A queue overflow reopened the decoder. Reopening takes long enough for the encoded queue to overflow again, so one overflow turned into seconds of back-to-back recoveries.
  • After a short stall a decoder running just under the frame interval drains its backlog only slowly, which kept the picture late for seconds.

Changes:

  • vaapi_picture_error maps those errors to Error::ReferenceLost, in decode and in DMA-BUF export (mapping syncs the surface, so a failed picture can surface there).
  • VideoDecoder::reset_in_place (default false). FfmpegDecoder implements it for VA-API with avcodec_flush_buffers, and the overflow path uses it instead of reopening when it is available. Decode errors still go through ReferenceLost and a full reopen.
  • run_video_worker drops a backlog of 12 or more encoded frames that has not drained for 250 ms, then requests a keyframe, as an overflow would (BACKLOG_FRAMES, STALE_BACKLOG, BoundedQueue::remove_where).
  • The embedded Linux encoded queue goes from 16 to 24 frames, 200 ms at 120 fps, so a brief stall does not overflow.

This pairs with #1414, which keeps the keyframe request working after a recovery. Either can merge first.

Verification:

  • New tests: vaapi_picture_errors_are_recoverable_and_others_are_not and remove_where_keeps_the_other_items_in_order.
  • cargo test --release -p opennow-streamer-platform-linux --features ffmpeg-bundled,vaapi (the features the app build uses) on clean dev with this change: 254 passed, against 252 on clean dev. The one failure, fixed_audio_output_never_falls_back_and_stays_isolated, also fails on clean dev on that device (ALSA device enumeration returns -2).
  • GeForce NOW on an Odin 2 Portal (AV1 10-bit HDR, 120 fps, around 95 Mbps) on a tree with other local changes: an injected 250 ms decode stall gave one drop to live, a 1500 ms stall recovered within 1.6 s, and steady streaming had 0 overflows.

Not tested: a real iris decode error, which I could not trigger on purpose, so the ReferenceLost mapping is covered by the unit test only. Other VA-API drivers were not tested.

🤖 Generated with Claude Code

… backlogs

A picture the hardware failed to decode reached the session as a generic
backend error. Map it to ReferenceLost, in decode and in DMA-BUF export.

Overflow recovery reopened the decoder, which takes long enough for the
queue to overflow again. Decoders that can flush in place now do that
(FFmpeg VA-API: avcodec_flush_buffers).

A backlog that does not drain only adds latency, so 12 or more encoded
frames still queued after 250 ms are dropped and a keyframe is requested.
The embedded Linux queue grows to 24 frames, 200 ms at 120 fps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bd509771-f359-40de-bf03-7feafae27db3

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T04:08:13.970820Z 81366ae New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 39768b694a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +795 to +797
commands
.remove_where(|queued| matches!(queued, VideoCommand::Decode { .. }));
need_keyframe = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep the keyframe gate closed after a stale-backlog drop

When a stale backlog is dropped on MediaCodec or the polled H.264 V4L2 path, the decoder can still have pre-drop output pending. The next poll_decoder() can return that output, which clears need_keyframe in the success handler at lines 773–775. Because this path neither resets the decoder nor sets the producer-side video_needs_keyframe gate, subsequent deltas are then decoded despite depending on the access units just removed, causing corruption or another reference-loss recovery before a recovery keyframe arrives. Keep admission gated until a recovery keyframe, and reset or otherwise isolate pending output from the previous chain.

AGENTS.md reference: AGENTS.md:L152-L154

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, polled output could reopen the gate. Fixed in 443f90d: need_keyframe now clears only when a submitted frame was decoded, and the gate only lets a keyframe through while it is set. Pending output from before the drop can still be shown, since it was decoded from valid references, but it no longer lets the following deltas in.

MediaCodec and polled H.264 V4L2 can still hold output from before a
stale backlog was dropped. Polling it cleared need_keyframe, so the deltas
that follow were decoded without the frames just removed. Only a submitted
frame opens the gate now; startup and recovery already pass a keyframe
through it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TheWeaklessOne

Copy link
Copy Markdown
Author

Follow-up in 443f90d for the review comment above. cargo test --release -p opennow-streamer-platform-linux --features ffmpeg-bundled,vaapi on x86_64 (WSL): 254 passed, 0 failed, 22 ignored.

No new test: the gate sits inside run_video_worker, which has no way to take a fake decoder. MediaCodec and polled H.264 V4L2 were not exercised, and this was not rerun on the device.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 443f90d06b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +795 to +797
let now = Instant::now();
let since = *backlog_since.get_or_insert(now);
if now.duration_since(since) >= STALE_BACKLOG {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reset the backlog timer when decoder recovery starts

If reference-loss recovery or reconfiguration occurs while backlog_since is set, that timestamp survives the decoder restart and any wait for a new keyframe. After a recovery lasting at least 250 ms, a fresh keyframe with 12 newly queued access units behind it immediately satisfies this comparison, discarding the new prediction chain and closing the keyframe gate again even though the new backlog has existed only briefly. This produces an unnecessary second freeze and keyframe request immediately after successful recovery. Clear the timer at decoder recovery and generation boundaries before measuring the new backlog.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. Fixed in f7cdbe2: every recovery ends with need_keyframe set, so the worker clears backlog_since for as long as that wait lasts. A new backlog is measured from the keyframe that ends it.

Comment on lines +799 to +802
commands
.remove_where(|queued| matches!(queued, VideoCommand::Decode { .. }));
need_keyframe = true;
emit(&events, BackendEvent::QueueOverflow { media: "video" });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Propagate the actual number of stale frames dropped

When this path clears a 12–24-frame backlog, it ignores the count returned by remove_where and emits only one QueueOverflow. The embedded Linux monitor in media.rs:3626–3629 converts that event into MediaFeedback::QueueDropped { count: 1 }, and queue_drops.rs:54–72 reports video counts as frames. Consequently, recovery diagnostics report one dropped frame when a dozen or more were discarded, systematically understating the cost of this new recovery mechanism. Propagate the actual removed count into the feedback.

AGENTS.md reference: AGENTS.md:L152-L152

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. Fixed in f7cdbe2: BackendEvent::QueueOverflow now carries count, this path passes what remove_where returned, and both media.rs monitors forward it to MediaFeedback::QueueDropped. The other overflow paths drop one item and report 1.

…d frames

The stale-backlog timer survived decoder recovery, so the first keyframe
after a long recovery could be dropped right away with the backlog behind
it. Clear it while waiting for a keyframe.

QueueOverflow reported one frame for a whole dropped backlog. Carry the
real count through to MediaFeedback::QueueDropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TheWeaklessOne

Copy link
Copy Markdown
Author

Second follow-up in f7cdbe2 for the two comments above. cargo test --release -p opennow-streamer-platform-linux --features ffmpeg-bundled,vaapi on x86_64 (WSL): 254 passed, 0 failed, 22 ignored, and opennow-streamer-platform passes cargo check. No new tests here either, for the same reason as before.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f7cdbe24ff

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if now.duration_since(since) >= STALE_BACKLOG {
backlog_since = None;
let dropped = commands
.remove_where(|queued| matches!(queued, VideoCommand::Decode { .. }));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve recovery frames queued by a newer generation

With a running backlog timer, an encoded-queue overflow can queue a reset: true keyframe and newer-generation deltas while the old frame is still decoding. If at least 12 new commands are waiting when that call returns and the old timer has expired, this predicate removes the recovery keyframe and its fresh prediction chain before their generation is processed. A blocked-decoder harness reproduces all 13 new-generation commands being dropped before keyframe submission, forcing another keyframe wait. Restrict stale removal to the measured generation, or preserve a queued newer-generation recovery.

AGENTS.md reference: AGENTS.md:L152-L154

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. Fixed in 81366ae: drop_stale_backlog() removes only the generation being decoded and keeps anything newer. If a newer generation's keyframe is already queued, the worker also skips the extra keyframe request, since that recovery is on its way. stale_backlog_drop_keeps_a_newer_generation_recovery covers the queue side. The worker path itself still has no test, because run_video_worker can't take a fake decoder.

A queue overflow can put a keyframe with reset set and a newer generation
behind the frames still being measured. Dropping every Decode removed that
recovery too and forced another keyframe wait. Drop only the generation in
progress, and skip the keyframe request when a recovery is already queued.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TheWeaklessOne

Copy link
Copy Markdown
Author

Third follow-up in 81366ae. cargo test --release -p opennow-streamer-platform-linux --features ffmpeg-bundled,vaapi on x86_64 (WSL): 255 passed, 0 failed, 22 ignored. Not rerun on the device.

@TheWeaklessOne

Copy link
Copy Markdown
Author

Folded into #1470 together with the other Linux decode fixes, rebased on dev and retested on the Portal. Closing this one in favour of it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant