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
This commit is contained in:
George Xu
2026-04-20 18:00:59 -07:00
parent 2c3a3db2e0
commit 23c8577da9

View File

@@ -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);