Skip to content
This repository was archived by the owner on Jun 17, 2026. It is now read-only.

Fix video export stall when trim regions cause long decoder gaps#682

Open
m8i-51 wants to merge 21 commits into
siddharthvaddem:mainfrom
m8i-51:fix/export-stall-trim-region
Open

Fix video export stall when trim regions cause long decoder gaps#682
m8i-51 wants to merge 21 commits into
siddharthvaddem:mainfrom
m8i-51:fix/export-stall-trim-region

Conversation

@m8i-51

@m8i-51 m8i-51 commented Jun 3, 2026

Copy link
Copy Markdown

Description

Fixes a false-positive encoder stall that causes video export to abort when a
project has trim regions covering a large portion of the source recording.

Motivation

StreamingVideoDecoder reads the source file sequentially — it cannot seek.
Frames inside trimmed regions must still be decoded to maintain P/B-frame
state, but are discarded. For a recording with a large trim in the middle, this
discard phase can take tens of seconds of wall time.

During the discard phase the encoder queue is empty and lastEncoderOutputAt
goes stale. When real frames arrive after the trim and fill the queue, the
timeout has already elapsed — a false positive that aborts a healthy export.

Fix: reset the timer at the start of each queue-full wait instead of
measuring from the last encoder output.

  + const stallWaitStartAt = Date.now();
    while (encoder.encodeQueueSize >= maxEncodeQueue) {
  -   if (Date.now() - this.lastEncoderOutputAt > ENCODER_STALL_TIMEOUT_MS) {
  +   if (Date.now() - stallWaitStartAt > ENCODER_STALL_TIMEOUT_MS) {

Type of Change

  • New Feature
  • Bug Fix
  • Refactor / Code Cleanup
  • Documentation Update
  • Other (please specify)

Related Issue(s)

None

Screenshots / Video

スクリーンショット 2026-06-03 14 57 06

Testing

The stall detection path exercises VideoEncoder directly, which is a browser
WebCodecs API not available in the Node test environment. I verified the fix
manually with a project that previously reproduced the issue consistently.

  1. Create a project with a trim region that removes a large span of the source
    recording (e.g. trimming out several minutes from the middle)
  2. Export to MP4
  3. Before: export aborts with "The hardware video encoder stopped responding"
  4. After: export completes normally

Checklist

  • I have performed a self-review of my code.
  • I have added any necessary screenshots or videos.
  • I have linked related issue(s) and updated the changelog if applicable.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed encoder stall detection to avoid spurious timeouts during queue waits, improving video export reliability and stability; also removed obsolete internal tracking to simplify exporter behavior.

@m8i-51 m8i-51 requested a review from siddharthvaddem as a code owner June 3, 2026 05:59

@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: a0623b38c0

ℹ️ 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 thread src/lib/exporter/videoExporter.ts Outdated
!this.cancelled
) {
if (Date.now() - this.lastEncoderOutputAt > ENCODER_STALL_TIMEOUT_MS) {
if (Date.now() - stallWaitStartAt > ENCODER_STALL_TIMEOUT_MS) {

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 Remove or keep a read of lastEncoderOutputAt

Because the stall check now compares against stallWaitStartAt, lastEncoderOutputAt is no longer read anywhere in the repo (only assigned in initializeEncoder, the encoder output callback, and cleanup). This project has noUnusedLocals: true in tsconfig.json, so the next tsc build will report TS6133 for the private field and fail; either remove the field/assignments or keep the stall logic reading it in a way that still avoids the trim-gap false positive.

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.

Good catch. Removed lastEncoderOutputAt and all its assignments — it was only ever written after this change, never read.

@coderabbitai

coderabbitai Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 12b00873-88c5-4b27-8aaf-c47104f37ca7

📥 Commits

Reviewing files that changed from the base of the PR and between a0623b3 and 1f02e7e.

📒 Files selected for processing (1)
  • src/lib/exporter/videoExporter.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/lib/exporter/videoExporter.ts

📝 Walkthrough

Walkthrough

VideoExporter replaces a global lastEncoderOutputAt timestamp with a per-wait stallWaitStartAt used during queue-full waits, and removes the global field and its initialization/update/reset logic.

Changes

Encoder stall timeout isolation

Layer / File(s) Summary
Remove global timestamp & cleanup reset
src/lib/exporter/videoExporter.ts
Deleted the lastEncoderOutputAt private field and removed its reset in cleanup.
Stop initializing/updating global timestamp in encoder setup
src/lib/exporter/videoExporter.ts
Removed initialization and output-handler updates that previously set lastEncoderOutputAt during initializeEncoder.
Per-wait stall timer in queue-full loop
src/lib/exporter/videoExporter.ts
When waiting for encoder.encodeQueueSize to drop, the code now sets stallWaitStartAt = Date.now() each wait cycle and checks elapsed time against that local timer instead of using this.lastEncoderOutputAt.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

bug

Suggested reviewers

  • siddharthvaddem

Poem

a fresh timer wakes when queues are full,
no haunted timestamps crying wolf at night —
lowkey less cursed, cleaner timeout checks,
the exporter nods, resumes its flight 🎬

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title accurately describes the core bug fix: eliminating false-positive encoder stalls triggered by trim regions causing decoder gaps.
Description check ✅ Passed Description covers all key sections: clear problem statement, thorough motivation explaining the root cause, bug fix with code snippet, manual testing details, and completed checklist items.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

EtienneLescot and others added 3 commits June 15, 2026 16:23
When a recording has a large trim region (e.g. 400s–828s removed),
the decoder must sequentially decode and discard all frames in that
region to maintain P/B-frame state. On a 3320x2160 source this can
take 40–50 seconds of wall time.

During that decode pass the encoder queue drains to empty and
lastEncoderOutputAt stops updating. When the next segment's frames
arrive and fill the encode queue, the stall detector would compare
Date.now() against the stale lastEncoderOutputAt (~50 s ago) and
incorrectly throw a stall error, aborting the export.

Fix: measure stall timeout from when the queue-full while-loop is
entered (stallWaitStartAt), not from the last global encoder output.
This gives the encoder a fresh 15 s window to produce output each
time the queue fills up, regardless of how long the decoder spent
on trimmed frames.

Also remove VideoFrame leak-tracker debug code added during diagnosis,
and switch latencyMode to "realtime" with a smaller maxEncodeQueue
to reduce encoder internal buffering depth.
EtienneLescot and others added 18 commits June 19, 2026 12:34
Extracts the queue-full wait loop into waitForEncoderQueueSpace() so
the timing logic can be unit tested without real WebCodecs. Covers
the original false-positive: a long gap before the call (e.g. decoder
discarding frames in a trim region) must not count against the
15s timeout, since the timer starts at call time, not from the
encoder's last output.
The "long gap before this call" case was mathematically identical to
the queue-drain test once now()/sleep() are injected — shifting the
fake clock's epoch doesn't change now() - stallWaitStartAt. Replaced
with a comment on why the bug can't recur: the function takes no
external "last output" timestamp to go stale in the first place.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants