You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature Request]: Enable Chromium MacLoopbackAudioForScreenShare flag #42605
navigator.mediaDevices.getUserMedia does not work on macOS for audio capture due to a fundamental limitation whereby apps that want to access the system's audio require a signed kernel extension. Chromium, and by extension Electron, does not provide this.
Chromium added a MacLoopbackAudioForScreenShare flag a little while ago to enable system audio recording. Does that mean it's now possible to support audio capture natively?
Proposed Solution
Support audio capture on screen recordings on MacOS with the MacLoopbackAudioForScreenShare Chromium flag
Alternatives Considered
I've considered packaging another app with mine to capture system audio. I'd like to avoid this if possible — we're really concerned with keeping the wrapper around our web app light.
Additional Information
I tried passing the Chromium flag in via commandLine.appendSwitch, and I still get renderer.js:34 DOMException: Could not start audio source. Here's a quick Electron Fiddle.
Reacted by Marcello Bastéa-Forte, Angela Gong, sylvielee, Matt Blair, yingming-d, Nicole Branagan, Sridhar, Michał Zarach, Kemal Ozturk, Nate Hart and 1 more
changed the title [-][Feature Request]: Support system audio capture on MacOS[/-][+][Feature Request]: Enable Chromium MacLoopbackAudioForScreenShare flag[/+]on Jun 24, 2024
It looks like this is partially broken in Chromium too. I can try to take a better look sometime soon but given it's an experimental feature in Chromium and also not part of our public API i can't promise when i'll get to it.
@codebytere thanks for looking into this, and I understand that it might take a while on your end. In the meantime, do you have any recommendation of where I would look in the codebase to try to implement this on a fork?
Is there a reason you aren't able to use navigator.mediaDevices.getDisplayMedia (as opposed to navigator.mediaDevices.getUserMedia) ? I've put together this repro which records sound for me just fine:
ooh, that gist is super helpful, thanks for making that! it works for me on macOS 14.5 with electron 31.2.1 and will be a helpful reference.
The two differences:
we were setting audio: { mandatory: { chromeMediaSource: 'desktop' } } instead of audio: true (which works on Windows.
if I set that to true it just crashes the renderer process on macOS (I don't see the error from your chromium issue in Console.app logs, but I'm not sure how to get a formal stack trace from the renderer process crashing)
we haven't configured a setDisplayMediaRequestHandler, I will try that
@marcello3d ah yeah - setDisplayMediaRequestHandler is a necessary prerequisite to using navigator.mediaDevices.getDisplayMedia and won't work without it. If there's a way we can make that clearer in docs we definitely can! Let me know what happens and if that achieves your use case :)
if I change this to getDisplayMedia with { audio: true, video: true } and add the request handler, it works (with audio!), but the request doesn't include any of the parameters from getDisplayMedia (for example, if I do video: { deviceId }).
I'll need to either rebuild the window chooser UI in electron (rather than our web app, so not ideal) or exfiltrate some state to be used by the request handler via IPC
update: so I added an IPC method for 'setting preferred device' that is invoked before calling await navigator.mediaDevices.getDisplayMedia and then added a setDisplayMediaRequestHandler that returns that same device id. (I'm using a couple of WeakMaps to pair the desktopCapturer.getSources() results and preferred device id to a given WebFrameMain)
It's a bit hacky, but it works!
Would it be possible to extend setDisplayMediaRequestHandler to get the full constraints object? Or if I could at least access the deviceId? That would simplify the logic a bunch
@marcello3d there are a few more properties we could potentially expose, but unfortunately we probably won't be able to expose the device ids without patching or something for the same reason they wouldn't show up when using navigator.mediaDevices.enumerateDevices() and why you can't request a device by id via navigator.mediaDevices.getDisplayMedia - it's disallowed by spec for privacy reasons.
More specifically - content::MediaStreamRequest does have properties for requested audio and video ids but they're not populated when navigator.mediaDevices.getDisplayMedia is called, so I don't think they'd address your use case.
As for the docs - agree we should update them to make clearer what web platform apis to invoke for screen capture related needs and i'll take a look at tweaking those soon!
I think that’s fine. I don’t mean to suggest we expose more information to the web browser, but rather allow the fields the browser passes to getDisplayMedia() to be accessed in electron main’s request handler. We’re using preload + context bridge + IPC to expose the device ids that desktopCapturer.getSources() returns.
Right now the setDisplayMediaRequestHandler callback receives a very simple object (the WebFrameMain and a few booleans), but maybe that’s all chromium exposes?
edit: I guess that’s what you meant by the audio/video ids link. It does seem like some of those other booleans would be useful if someone were building a video selector in electron, but wouldn’t help in our use-case. Will stick with the hacky approach for now, thanks for your help!
Activity
[-][Feature Request]: Support system audio capture on MacOS[/-][+][Feature Request]: Enable Chromium MacLoopbackAudioForScreenShare flag[/+]ozturkkl commentedon Jul 8, 2024
Anyone out there? Should we tag someone here to get some visibility?
codebytere commentedon Jul 15, 2024
It looks like this is partially broken in Chromium too. I can try to take a better look sometime soon but given it's an experimental feature in Chromium and also not part of our public API i can't promise when i'll get to it.
hartbeatnt commentedon Jul 15, 2024
@codebytere thanks for looking into this, and I understand that it might take a while on your end. In the meantime, do you have any recommendation of where I would look in the codebase to try to implement this on a fork?
marcello3d commentedon Jul 16, 2024
@codebytere do you have a link or repro for this? would love to learn more!
codebytere commentedon Jul 17, 2024
@marcello3d - just opened https://issues.chromium.org/issues/353556451
Is there a reason you aren't able to use
navigator.mediaDevices.getDisplayMedia(as opposed tonavigator.mediaDevices.getUserMedia) ? I've put together this repro which records sound for me just fine:https://gist.github.com/4104b5d7d5bcbf94cd1a8011005bef3e
marcello3d commentedon Jul 17, 2024
ooh, that gist is super helpful, thanks for making that! it works for me on macOS 14.5 with electron 31.2.1 and will be a helpful reference.
The two differences:
audio: { mandatory: { chromeMediaSource: 'desktop' } }instead ofaudio: true(which works on Windows.setDisplayMediaRequestHandler, I will try thatcodebytere commentedon Jul 17, 2024
@marcello3d ah yeah -
setDisplayMediaRequestHandleris a necessary prerequisite to usingnavigator.mediaDevices.getDisplayMediaand won't work without it. If there's a way we can make that clearer in docs we definitely can! Let me know what happens and if that achieves your use case :)marcello3d commentedon Jul 17, 2024
I just realized we're using
getUserMedia, notgetDisplayMedia, which might explain the crash!our current flow is:
desktopCapturer.getSourceson main and IPC the list of sources and thumbnailsawait navigator.mediaDevices.getUserMediawith:if I change this to getDisplayMedia with
{ audio: true, video: true }and add the request handler, it works (with audio!), but the request doesn't include any of the parameters from getDisplayMedia (for example, if I dovideo: { deviceId }).I'll need to either rebuild the window chooser UI in electron (rather than our web app, so not ideal) or exfiltrate some state to be used by the request handler via IPC
marcello3d commentedon Jul 17, 2024
update: so I added an IPC method for 'setting preferred device' that is invoked before calling
await navigator.mediaDevices.getDisplayMediaand then added asetDisplayMediaRequestHandlerthat returns that same device id. (I'm using a couple of WeakMaps to pair thedesktopCapturer.getSources()results and preferred device id to a givenWebFrameMain)It's a bit hacky, but it works!
Would it be possible to extend
setDisplayMediaRequestHandlerto get the full constraints object? Or if I could at least access the deviceId? That would simplify the logic a bunchmarcello3d commentedon Jul 17, 2024
so we started using getUserMedia because this page suggests it: https://www.electronjs.org/docs/latest/api/desktop-capturer
Not sure if
MacLoopbackAudioForScreenShareis stable enough to mention in the caveats section (maybe a link to this ticket?).Might be nice to have an example with
setDisplayMediaRequestHandlerandnavigator.mediaDevices.getDisplayMediacodebytere commentedon Jul 17, 2024
@marcello3d there are a few more properties we could potentially expose, but unfortunately we probably won't be able to expose the device ids without patching or something for the same reason they wouldn't show up when using
navigator.mediaDevices.enumerateDevices()and why you can't request a device by id vianavigator.mediaDevices.getDisplayMedia- it's disallowed by spec for privacy reasons.More specifically -
content::MediaStreamRequestdoes have properties for requested audio and video ids but they're not populated whennavigator.mediaDevices.getDisplayMediais called, so I don't think they'd address your use case.As for the docs - agree we should update them to make clearer what web platform apis to invoke for screen capture related needs and i'll take a look at tweaking those soon!
marcello3d commentedon Jul 18, 2024
I think that’s fine. I don’t mean to suggest we expose more information to the web browser, but rather allow the fields the browser passes to getDisplayMedia() to be accessed in electron main’s request handler. We’re using preload + context bridge + IPC to expose the device ids that desktopCapturer.getSources() returns.
Right now the
setDisplayMediaRequestHandlercallback receives a very simple object (the WebFrameMain and a few booleans), but maybe that’s all chromium exposes?edit: I guess that’s what you meant by the audio/video ids link. It does seem like some of those other booleans would be useful if someone were building a video selector in electron, but wouldn’t help in our use-case. Will stick with the hacky approach for now, thanks for your help!