From 23c8577da97b79fddd8aac8d7a5a92829a239310 Mon Sep 17 00:00:00 2001 From: George Xu Date: Mon, 20 Apr 2026 18:00:59 -0700 Subject: [PATCH] fix: clean up native system picker flow in setDisplayMediaRequestHandler Replace the frozen-object-with-getter pattern for generating native picker source IDs with a simpler createNativePickerSource() function. Each call produces a unique DesktopMediaID so Chromium treats concurrent streams as distinct. Add clear comments explaining the magic kMacOsNativePickerId (-4) constant and why unique window_ids are needed. Notes: none --- lib/browser/api/session.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/browser/api/session.ts b/lib/browser/api/session.ts index 39c098d695..6b26fe1e2a 100644 --- a/lib/browser/api/session.ts +++ b/lib/browser/api/session.ts @@ -7,20 +7,21 @@ import { net } from 'electron/main'; const { fromPartition, fromPath, Session } = process._linkedBinding('electron_browser_session'); const { isDisplayMediaSystemPickerAvailable } = process._linkedBinding('electron_browser_desktop_capturer'); -// Fake video window that activates the native system picker -// This is used to get around the need for a screen/window -// id in Chrome's desktopCapturer. -let fakeVideoWindowId = -1; -// See content/public/browser/desktop_media_id.h +// Generates a source that triggers the native macOS SCContentSharingPicker. +// The magic ID kMacOsNativePickerId (-4) is recognized by the Chromium +// content layer (see content/public/browser/desktop_media_id.h) and routes +// to ScreenCaptureKitDeviceMac which presents the native picker. +// Each request needs a unique window_id so Chromium treats them as distinct +// streams (it deduplicates by DesktopMediaID). +let nextNativePickerWindowId = -1; const kMacOsNativePickerId = -4; -const systemPickerVideoSource = Object.create(null); -Object.defineProperty(systemPickerVideoSource, 'id', { - get() { - return `window:${kMacOsNativePickerId}:${fakeVideoWindowId--}`; - } -}); -systemPickerVideoSource.name = ''; -Object.freeze(systemPickerVideoSource); + +function createNativePickerSource () { + return { + id: `window:${kMacOsNativePickerId}:${nextNativePickerWindowId--}`, + name: '' + }; +} Session.prototype._init = function () { addIpcDispatchListeners(this); @@ -50,7 +51,9 @@ Session.prototype.setDisplayMediaRequestHandler = function (handler, opts) { this._setDisplayMediaRequestHandler(async (req, callback) => { if (opts && opts.useSystemPicker && isDisplayMediaSystemPickerAvailable()) { - return callback({ video: systemPickerVideoSource }); + // On macOS 15+, use the native SCContentSharingPicker. This bypasses + // the JS handler and lets the OS present its own picker UI. + return callback({ video: createNativePickerSource() }); } return handler(req, callback);