Skip to content

Conversation

@kixelated
Copy link
Collaborator

@kixelated kixelated commented Aug 25, 2025

Untested

Summary by CodeRabbit

  • Bug Fixes

    • Forces a keyframe on the initial video frame and at GOP boundaries for smoother startup and stable playback.
    • Clears canvas to black when no frame is available to prevent visual artifacts or ghosting.
    • Improves horizontal flip rendering consistency.
  • Performance

    • Avoids unnecessary canvas resizes by only updating when dimensions change.
    • Streamlines per-frame drawing with a single transform and draw call.
  • Chores

    • Converted a runtime import to type-only for cleaner builds (no runtime impact).

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 25, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Updates publisher keyframe trigger to include the initial frame and make GOP boundary check inclusive. Adjusts watch-side imports to mark Getter as a type-only import. Refactors the watch video renderer to remove desynchronized, resize canvas only on dimension change, fill black background when needed, and simplify horizontal flip via a single transform.

Changes

Cohort / File(s) Summary
Publisher: keyframe logic
js/hang/src/publish/video/index.ts
Changed keyframe condition to `!group
Watch: imports
js/hang/src/watch/video/index.ts
Switched Getter import to a type-only import (import { Effect, type Getter, Signal }); no runtime/control-flow change.
Watch: renderer refactor
js/hang/src/watch/video/renderer.ts
Removed desynchronized option from getContext, fill canvas black when empty, compute and apply canvas resize only when width/height change, wrap draw in single save/restore, simplify horizontal flip to scale(-1,1) + translate(-canvas.width,0), unified draw path, removed redundant restores; no API changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Src as VideoSource
  participant Pub as Publisher(Video)
  participant Enc as Encoder
  Note over Pub,Enc: Keyframe decision updated (initial frame + inclusive GOP boundary)

  Src->>Pub: frame(timestamp)
  Pub->>Pub: if !group || groupTs + GOP_US <= frame.ts ⇒ keyframe=true
  Pub->>Enc: encode(frame, { keyframe })
  Enc-->>Pub: encoded chunk
Loading
sequenceDiagram
  autonumber
  participant Stream as VideoStream
  participant Ren as Renderer
  participant Canvas as Canvas2D

  Stream->>Ren: provide video frame (optional)
  Ren->>Canvas: save()
  alt no frame
    Ren->>Canvas: fillRect(black, 0,0,w,h)
  else frame available
    Ren->>Ren: if displayWidth/Height changed ⇒ resize canvas
    opt horizontal flip
      Ren->>Canvas: transform(scale(-1,1) + translate(-canvas.width,0))
    end
    Ren->>Canvas: fillRect(black, 0,0,w,h)
    Ren->>Canvas: drawImage(frame)
  end
  Ren->>Canvas: restore()
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

I hop through frames where keyframes bloom,
A GOP-bound tick dispels the gloom.
I flip the world with just one turn,
Paint it black when there’s no churn.
Canvas hums, timestamps align—🥕


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between b66f31f and e960b56.

📒 Files selected for processing (1)
  • js/hang/src/watch/video/renderer.ts (2 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch misc-fix

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@claude
Copy link
Contributor

claude bot commented Aug 25, 2025

Claude finished @kixelated's task —— View job


Code Review Complete

Reviewed the 3 changed files in this "misc fix" PR for bugs and security issues:

js/hang/src/publish/video/index.ts - Added better frame cleanup in the signal setter
js/hang/src/watch/video/index.ts - Minor change to frame buffer handling
js/hang/src/watch/video/renderer.ts - Improved canvas rendering logic and flip transformation handling

Issues Found

🚨 Security Issue: Line 32 in renderer.ts - getContext("2d", { desynchronized: true }) option could potentially lead to timing attacks or information leakage through timing variations. The desynchronized option bypasses browser's normal rendering synchronization which may expose timing information.

🐛 Potential Bug: Line 112 in renderer.ts - The throw new Error("scheduled without a canvas") could cause unhandled exceptions during normal operation if timing is off between context cleanup and render scheduling.

⚠️ Resource Leak Risk: Lines 139-140 in watch/video/index.ts - The #next frame cleanup in close() method may not be sufficient if frames are pending in decoder queue.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
js/hang/src/publish/video/index.ts (1)

164-166: Guard for undefined timestamps to avoid never-rotating GOPs

Some VideoFrame sources can yield frame.timestamp === undefined. In that case, the time-based check always evaluates false, and the GOP may never rotate after the initial group. Consider a count-based fallback keyed to the track framerate.

Suggested change in-place:

-// Force a keyframe if this is the first frame (no group yet), or GOP elapsed.
-const keyFrame = !group || groupTimestamp + GOP_DURATION_US <= frame.timestamp;
+// Force a keyframe if this is the first frame (no group yet), or GOP elapsed by time or frame count.
+const ts = frame.timestamp;
+const elapsedByTime = ts !== undefined && (groupTimestamp + GOP_DURATION_US) <= ts;
+const keyFrame = !group || elapsedByTime || framesSinceKey >= Math.max(1, Math.round(settings.frameRate * (GOP_DURATION_US / 1_000_000)));
+if (keyFrame) framesSinceKey = 0; else framesSinceKey++;

And define the counter near the loop setup:

// just before the while-loop
let framesSinceKey = 0;

If you prefer not to add a counter, minimally assert timestamps are present to fail fast during unexpected sources:

-const keyFrame = !group || groupTimestamp + GOP_DURATION_US <= frame.timestamp;
+if (frame.timestamp === undefined) {
+  console.warn("VideoFrame has no timestamp; falling back to keyFrame=true to ensure GOP rotation");
+}
+const keyFrame = !group || frame.timestamp === undefined || (groupTimestamp + GOP_DURATION_US) <= frame.timestamp;
js/hang/src/watch/video/renderer.ts (2)

132-134: Nit: translate then scale for slightly clearer transform math

Functionally equivalent, but applying translate(w, 0) before scale(-1, 1) avoids negative coordinates in intermediate space, which some readers find easier to reason about.

-ctx.scale(-1, 1);
-ctx.translate(-ctx.canvas.width, 0);
+ctx.translate(ctx.canvas.width, 0);
+ctx.scale(-1, 1);

139-142: Optional: throttle rendering when no frame is available

Right now we fill black every RAF tick when no frame is present. Consider pausing the RAF (or rendering at a lower cadence) until a new frame arrives to save CPU on long gaps.

Potential approach:

  • Only schedule RAF when this.source.frame is defined; re-schedule on frame signal changes.
  • Or, keep RAF but early-return unless a frame is present and only re-fill black when the canvas size changes.

Happy to sketch a minimal change if you want to pursue this.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 30909c1 and b66f31f.

📒 Files selected for processing (3)
  • js/hang/src/publish/video/index.ts (1 hunks)
  • js/hang/src/watch/video/index.ts (1 hunks)
  • js/hang/src/watch/video/renderer.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
{rs,js}/hang/**

📄 CodeRabbit inference engine (CLAUDE.md)

All media logic belongs in the hang layer (rs/hang and js/hang)

Files:

  • js/hang/src/watch/video/index.ts
  • js/hang/src/watch/video/renderer.ts
  • js/hang/src/publish/video/index.ts
js/**

📄 CodeRabbit inference engine (CLAUDE.md)

Place JS/TS packages under the js/ directory

Files:

  • js/hang/src/watch/video/index.ts
  • js/hang/src/watch/video/renderer.ts
  • js/hang/src/publish/video/index.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Check
🔇 Additional comments (3)
js/hang/src/watch/video/index.ts (1)

2-2: Type-only Getter import is correct and reduces runtime surface

Good cleanup. Getter is used purely as a type, so importing it with type avoids an unnecessary runtime import and helps tree-shaking.

js/hang/src/publish/video/index.ts (1)

164-166: Inclusive GOP boundary + first-frame keyframe: correct fix

Using !group || groupTimestamp + GOP_DURATION_US <= frame.timestamp ensures the initial frame is a keyframe and prevents the off-by-one at exact GOP boundaries.

js/hang/src/watch/video/renderer.ts (1)

117-138: Smarter resizing + single save/restore: solid rendering cleanup

  • Resize only when dimensions change (uses displayWidth/Height) to avoid needless context resets.
  • One save()/restore() around all per-draw state simplifies correctness, especially with transforms.
  • Unified drawImage path keeps the render loop lean.

Looks good.

It might be the cause of screen tearing.
@kixelated kixelated enabled auto-merge (squash) August 25, 2025 22:43
@claude
Copy link
Contributor

claude bot commented Aug 25, 2025

Claude finished @kixelated's task —— View job


Code Review for Misc Fixes PR

Reviewing 3 changed files for bugs and security issues:

  • Read and analyze js/hang/src/publish/video/index.ts
  • Read and analyze js/hang/src/watch/video/index.ts
  • Read and analyze js/hang/src/watch/video/renderer.ts
  • Provide concise review feedback

Review Results

👍 Overall Assessment: The changes look good - they fix keyframe timing logic, improve rendering efficiency, and clean up imports. No major bugs or security issues found.

Minor Issues Found:

  1. js/hang/src/publish/video/index.ts:165 - The keyframe logic could fail if frame.timestamp is undefined. Consider adding a fallback:

    const keyFrame = !group || frame.timestamp === undefined || (groupTimestamp + GOP_DURATION_US) <= frame.timestamp;
  2. js/hang/src/watch/video/renderer.ts:112 - The error "scheduled without a canvas" could be thrown during normal operation if timing is off between context cleanup and render scheduling. Consider handling this more gracefully.

  3. js/hang/src/watch/video/renderer.ts:32 - Using getContext("2d") without the { desynchronized: true } option I see in the original codebase might be intentional, but worth noting the behavior change if that was removed.

Positive Changes:

  • Keyframe timing fix prevents off-by-one GOP boundary issues
  • Canvas resize optimization improves performance
  • Simplified horizontal flip transformation reduces complexity
  • Type-only import cleanup reduces bundle size

View PR

@kixelated kixelated merged commit 976a7f9 into main Aug 25, 2025
2 of 3 checks passed
@kixelated kixelated deleted the misc-fix branch August 25, 2025 22:46
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.

2 participants