Repository navigation
perf(emulator): stop decoding frames for hidden emulator panes - #7382
Conversation
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]>
📝 WalkthroughWalkthroughThis change introduces an Changes
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
Estimated code review effort: 2/5 Related PRs: None identified. Suggested labels: frontend, tests Suggested reviewers: None identified. Poem: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
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. Comment |
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]>
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]>
…yai#7382) Co-authored-by: Orca <[email protected]>
…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]>
Description
EmulatorDeviceFramecomputedshowStream = isLive && Boolean(previewUrl)— no visibility gate — so a live emulator on a hidden tab/worktree kept its frame stream fully alive:ctx.drawImages it onto avisibility:hiddencanvas.Blob+createObjectURLper MJPEG frame and callssetState.EmulatorPanealready computesisActive(it uses it forautoAttachOnMount) but never passed it toEmulatorDeviceFrame. 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.tsxasserts the IPC contract, not just the DOM:startFrameStreamis calledstartFrameStreamis not called (no per-frame IPC / decode)stopFrameStreamis called (parks at source); re-show restartsReverting the one-line gate fails 2 of the 3 tests. Full renderer typecheck + oxlint clean.
Why it's safe: the overlay slot (
EmulatorPaneOverlayLayer) already renders the pane withvisibility: isActive ? 'visible' : 'hidden', soisActive === falsemeans the pane is already invisible — gating on it can never blank a stream the user can see. The session stays attached (autoAttachOnMountuntouched), so the stream re-fires via the existingenabledeffect. Mirrors the browser pane's park-when-hidden precedent.Trade-offs
Scope
Renderer-only, ~4 lines of behavior + tests. No change to attach/detach lifecycle.