Skip to content

perf(emulator): stop decoding frames for hidden emulator panes - #7382

Merged
nwparker merged 1 commit into
mainfrom
nwparker/perf-emulator-hidden-stream
Jul 5, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/perf-emulator-hidden-stream

Conversation

@nwparker

@nwparker nwparker commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

EmulatorDeviceFrame computed showStream = isLive && Boolean(previewUrl) — no visibility gate — so a live emulator on a hidden tab/worktree kept its frame stream fully alive:

  • Android: decodes every H.264 access unit with WebCodecs and ctx.drawImages it onto a visibility:hidden canvas.
  • iOS: allocates a Blob + createObjectURL per MJPEG frame and calls setState.
  • Both: frames keep flowing over IPC — and over the network for paired SSH / remote clients — to a viewer that can't see them. This stacks per open worktree holding a live emulator.

EmulatorPane already computes isActive (it uses it for autoAttachOnMount) but never passed it to EmulatorDeviceFrame. Thread it through and gate the stream: showStream = isActive && isLive && Boolean(previewUrl).

ELI5

You've got a phone emulator running in a tab. You switch to another tab. The emulator's video kept playing full-speed in the background where nobody could see it — decoding every frame, burning CPU/GPU/battery, and shipping frames over the network to your phone/web client for nothing. Now it pauses when you're not looking and un-pauses when you come back.

Evidence

New emulator-device-frame-visibility.test.tsx asserts the IPC contract, not just the DOM:

  • active pane → startFrameStream is called
  • inactive pane → startFrameStream is not called (no per-frame IPC / decode)
  • active → inactive → stopFrameStream is called (parks at source); re-show restarts

Reverting the one-line gate fails 2 of the 3 tests. Full renderer typecheck + oxlint clean.

npx vitest run --config config/vitest.config.ts src/renderer/src/components/emulator-pane/

A live end-to-end repro needs a real attached Android/iOS emulator (not available in headless CI), so the unit test is the reproduction harness.

Why it's safe: the overlay slot (EmulatorPaneOverlayLayer) already renders the pane with visibility: isActive ? 'visible' : 'hidden', so isActive === false means the pane is already invisible — gating on it can never blank a stream the user can see. The session stays attached (autoAttachOnMount untouched), so the stream re-fires via the existing enabled effect. Mirrors the browser pane's park-when-hidden precedent.

Trade-offs

  • Re-show is no longer instant. Returning to a parked emulator restarts the stream from scratch — Android requests a fresh scrcpy stream and waits for the next keyframe, so you may see a brief "Connecting emulator…" flash (a fraction of a second, up to ~1s) instead of an already-live picture. Previously instant because it never stopped.
  • Tab-hopping causes stop/start churn. Flipping rapidly between tabs stops and restarts the stream each time (bounded by how fast you click). This mirrors what the browser pane already does, but is more start/stop work than before — traded for zero decode while parked.
  • Not a full detach. The emulator session stays attached, so no state loss — only the video pipe pauses. A hidden emulator still holds its session + an idle control WebSocket; I intentionally left the control WS alive to avoid a ~750ms reconnect on re-show. So the savings are "stop decoding," not "disconnect everything."

Scope

Renderer-only, ~4 lines of behavior + tests. No change to attach/detach lifecycle.

A backgrounded but still-attached emulator kept the frame stream running:
continuous WebCodecs H.264 decode + ctx.drawImage (Android) or Blob +
createObjectURL per MJPEG frame (iOS) painted to a visibility:hidden canvas,
with frames still flowing over IPC (and the network for paired SSH/remote
clients) to no viewer. This stacks per open worktree holding a live emulator.

EmulatorPane already threads isActive (it gates autoAttachOnMount) but never
passed it to EmulatorDeviceFrame, so showStream gated only on isLive. Gate it
on isActive too. The overlay slot already sets visibility:hidden when isActive
is false, so this can never blank a visible stream. The session stays attached
(autoAttachOnMount untouched), so the stream re-fires within a frame on re-show
via the existing enabled effect. Mirrors the browser pane's park-when-hidden.

Both stream hooks tear down on disable: useEmulatorVideoStream stops the H.264
forwarding at the source (stopVideoStream) and closes the decoder;
useEmulatorFrameStream calls stopFrameStream and revokes the Blob URL.

Test asserts the IPC contract (not just DOM): no startFrameStream while the
pane is inactive, and stopFrameStream on hide. Reverting the gate fails 2/3.

Co-authored-by: Orca <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This change introduces an isActive boolean prop on EmulatorDeviceFrame, forwarded from EmulatorPane, to gate whether the MJPEG frame stream is started or stopped based on pane visibility. The showStream condition now additionally requires isActive alongside existing isLive and previewUrl checks. A new test suite verifies stream start/stop behavior across active/inactive transitions, and an existing input test is updated to pass isActive={true}.

Changes

Cohort / File(s) Summary
Visibility gating — EmulatorPane.tsx, emulator-device-frame.tsx Adds isActive prop to EmulatorDeviceFrameProps, forwards it from EmulatorPane, and updates showStream gating logic.
Tests — emulator-device-frame-visibility.test.tsx, emulator-device-frame.input.test.tsx Adds a new visibility-gating test suite and updates existing test to pass isActive={true}.

Sequence Diagram(s)

sequenceDiagram
  participant EmulatorPane
  participant EmulatorDeviceFrame
  participant emulatorApi as window.api.emulator

  EmulatorPane->>EmulatorDeviceFrame: isActive prop
  EmulatorDeviceFrame->>EmulatorDeviceFrame: compute showStream
  alt showStream true
    EmulatorDeviceFrame->>emulatorApi: startFrameStream
  else showStream false
    EmulatorDeviceFrame->>emulatorApi: stopFrameStream
  end
Loading

Estimated code review effort: 2/5

Related PRs: None identified.

Suggested labels: frontend, tests

Suggested reviewers: None identified.

Poem:
A rabbit peeks when panes come near,
It streams the frames when you're right here,
And when you hop to tabs elsewhere,
It stops the stream without a care,
Tests confirm the toggle's neat—
Active on, inactive, retreat! 🐰

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is informative but does not follow the required template and omits Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes. Rewrite the PR description to match the repository template and add the missing sections, including testing checklist, screenshots/no visual change, review report, security audit, and notes.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly summarizes the main change: stopping frame decoding for hidden emulator panes.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

@nwparker
nwparker merged commit 62f5ad6 into main Jul 5, 2026
2 checks passed
nwparker added a commit that referenced this pull request Jul 22, 2026
The iOS MJPEG and Android scrcpy device streams are gated only on the pane
being the active tab (isActive, PR #7382). When the emulator tab is frontmost
but the whole Orca window is hidden/minimized/occluded/display-asleep, the
full-fps pipeline keeps running: main-process socket read + JPEG/H.264 decode
+ IPC + renderer decode. Renderer background-throttling (#9395) cannot stop it
because the pipeline is IPC-push driven from main.

Gate showStream additionally on window visibility via a new occlusion-safe
hook that honors the terminal stale-visibility latch (so a display-sleep
occlusion wedge can't freeze the emulator on a black frame) and delays the
visible->hidden park by 500ms so a quick Cmd+Tab round-trip doesn't renegotiate
the device stream.

Co-authored-by: Orca <[email protected]>
nwparker added a commit that referenced this pull request Jul 22, 2026
The iOS MJPEG and Android scrcpy device streams are gated only on the pane
being the active tab (isActive, PR #7382). When the emulator tab is frontmost
but the whole Orca window is hidden/minimized/occluded/display-asleep, the
full-fps pipeline keeps running: main-process socket read + JPEG/H.264 decode
+ IPC + renderer decode. Renderer background-throttling (#9395) cannot stop it
because the pipeline is IPC-push driven from main.

Gate showStream additionally on window visibility via a new occlusion-safe
hook that honors the terminal stale-visibility latch (so a display-sleep
occlusion wedge can't freeze the emulator on a black frame) and delays the
visible->hidden park by 500ms so a quick Cmd+Tab round-trip doesn't renegotiate
the device stream.

Co-authored-by: Orca <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…i#9842)

The iOS MJPEG and Android scrcpy device streams are gated only on the pane
being the active tab (isActive, PR stablyai#7382). When the emulator tab is frontmost
but the whole Orca window is hidden/minimized/occluded/display-asleep, the
full-fps pipeline keeps running: main-process socket read + JPEG/H.264 decode
+ IPC + renderer decode. Renderer background-throttling (stablyai#9395) cannot stop it
because the pipeline is IPC-push driven from main.

Gate showStream additionally on window visibility via a new occlusion-safe
hook that honors the terminal stale-visibility latch (so a display-sleep
occlusion wedge can't freeze the emulator on a black frame) and delays the
visible->hidden park by 500ms so a quick Cmd+Tab round-trip doesn't renegotiate
the device stream.

Co-authored-by: Orca <[email protected]>
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