Skip to content

perf(emulator): park device stream when the window is hidden - #9842

Merged
nwparker merged 1 commit into
mainfrom
nwparker/perf-14-emulator-visibility-gate
Jul 22, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/perf-14-emulator-visibility-gate

Conversation

@nwparker

@nwparker nwparker commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

What

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

This gates showStream additionally on window visibility via a new hook useEmulatorStreamWindowVisible() that:

  • returns isWindowVisible() || isDocumentVisibilityProvenStale(), reusing the terminal occlusion-staleness latch so a macOS display-sleep occlusion wedge can't freeze the emulator on a black frame with no recovery (same bug class as the 78MB terminal drop);
  • delays the visible→hidden park by 500ms so a quick Cmd+Tab round-trip doesn't tear down and renegotiate the device stream (MJPEG reconnect / scrcpy keyframe), which is heavier than a terminal resync.

ELI5

Your phone emulator kept live-streaming video from the device even when Orca was hidden behind other windows or your screen was asleep — decoding frames nobody could see, draining battery. Now it pauses the video when the window isn't visible and instantly resumes when you come back. It waits half a second before pausing so flicking away and back doesn't cause a reconnect stutter, and it has a safety catch so a known macOS "is it visible?" glitch after sleep can't leave you staring at a black screen.

Proof of zero regression

  • Reuses the exact proven teardown/restart path the existing isActive gate uses (use-emulator-frame-stream.ts / use-emulator-video-stream.ts), so no new main-process work.
  • Occlusion-wedge safety: integrates isDocumentVisibilityProvenStale() + registerStaleDocumentVisibilityRecovery() — a wedged hidden after display sleep is treated as visible and forces a re-render, so the stream can't be permanently parked on a wedge.
  • Live touch/gesture is gated by canInteract, not showStream, and the wheel-gesture flush (80ms) fires well before the 500ms park, so parking never strands a pending gesture.
  • Independent adversarial review (frontier model) verified useSyncExternalStore stability, wedge recovery, timer cleanup on unmount/rapid-toggle, MJPEG/scrcpy clean restart (registry replays cached meta + SPS/PPS + GOP keyframe on resume), and no double-teardown when isActive and window-visibility flip together. No regression found.
  • Tests: extended emulator-device-frame-visibility.test.tsx — 6/6 pass (3 existing + 3 new: park-after-delay + resume, no-teardown on quick hide/show within the delay, keeps streaming while hidden when occlusion is proven stale). pnpm typecheck:web: clean.

Proof of perf improvement

While the emulator tab is frontmost and the window is hidden/minimized/occluded/asleep, this eliminates a continuous full-device-fps pipeline (main socket read + JPEG/H.264 decode + IPC + renderer decode). The new tests assert stopFrameStream is called at the source once parked (no more per-frame IPC), directly proving the frames stop. Complements PR #7382 (which covered the background-tab case); this covers the whole-window-hidden case #7382 does not.

Made with Orca 🐋

@coderabbitai

coderabbitai Bot commented Jul 22, 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: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 695736d3-1fab-4623-bad0-765d10a438f2

📥 Commits

Reviewing files that changed from the base of the PR and between 9cfeca0 and 5720c18.

📒 Files selected for processing (3)
  • src/renderer/src/components/emulator-pane/emulator-device-frame-visibility.test.tsx
  • src/renderer/src/components/emulator-pane/emulator-device-frame.tsx
  • src/renderer/src/components/emulator-pane/use-emulator-stream-window-visibility.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/components/emulator-pane/use-emulator-stream-window-visibility.ts

📝 Walkthrough

Walkthrough

Adds a React hook that tracks window visibility, recovers from stale document visibility, and delays stream parking by 500 ms. EmulatorDeviceFrame now includes effective window visibility when deciding whether to show the stream. Tests cover delayed parking, cancellation when visibility returns quickly, document visibility reset, and continued streaming when stale visibility is proven.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the change, but it does not follow the required template sections for Summary, Screenshots, Testing, AI Review Report, Security Audit, or Notes. Rewrite the PR description using the repository template and add the missing sections, including screenshots status, testing checklist, AI review, security audit, and notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: parking emulator streams when the window is hidden.
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.
✨ 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.

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
nwparker force-pushed the nwparker/perf-14-emulator-visibility-gate branch from 9cfeca0 to 5720c18 Compare July 22, 2026 21:15
@nwparker
nwparker merged commit 8cfb8a2 into main Jul 22, 2026
1 check passed
@nwparker
nwparker deleted the nwparker/perf-14-emulator-visibility-gate branch July 22, 2026 21:33
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