Skip to content

[Feature Request]: Enable Chromium MacLoopbackAudioForScreenShare flag #42605

Closed

Description

@ofagbemi

Preflight Checklist

Problem Description

From the docs

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.

Activity

  1. changed the title [-][Feature Request]: Support system audio capture on MacOS[/-] [+][Feature Request]: Enable Chromium MacLoopbackAudioForScreenShare flag[/+] on Jun 24, 2024
  2. ozturkkl commented on Jul 8, 2024

    @ozturkkl

    Anyone out there? Should we tag someone here to get some visibility?

  3. codebytere commented on Jul 15, 2024

    @codebytere
    Member

    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.

  4. hartbeatnt commented on Jul 15, 2024

    @hartbeatnt

    @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?

  5. marcello3d commented on Jul 16, 2024

    @marcello3d

    It looks like this is partially broken in Chromium too.

    @codebytere do you have a link or repro for this? would love to learn more!

  6. codebytere commented on Jul 17, 2024

    @codebytere
    Member

    @marcello3d - just opened https://issues.chromium.org/issues/353556451

    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:

    https://gist.github.com/4104b5d7d5bcbf94cd1a8011005bef3e

  7. marcello3d commented on Jul 17, 2024

    @marcello3d

    @marcello3d - just opened https://issues.chromium.org/issues/353556451

    Is there a reason you aren't able to use navigator.mediaDevices.getDisplayMedia? I've put together this repro which records sound for me just fine:

    https://gist.github.com/4104b5d7d5bcbf94cd1a8011005bef3e

    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:

    1. 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)
    2. we haven't configured a setDisplayMediaRequestHandler, I will try that
  8. codebytere commented on Jul 17, 2024

    @codebytere
    Member

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

  9. marcello3d commented on Jul 17, 2024

    @marcello3d

    I just realized we're using getUserMedia, not getDisplayMedia, which might explain the crash!

    our current flow is:

    1. call desktopCapturer.getSources on main and IPC the list of sources and thumbnails
    2. render a UI in web with the thumbnails and a checkbox for capturing audio
    3. call await navigator.mediaDevices.getUserMedia with:
    video: {
        mandatory: {
            chromeMediaSource: 'desktop',
            chromeMediaSourceId: this.config.deviceID,
        },
    },
    

    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

  10. marcello3d commented on Jul 17, 2024

    @marcello3d

    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

  11. marcello3d commented on Jul 17, 2024

    @marcello3d

    If there's a way we can make that clearer in docs we definitely can!

    so we started using getUserMedia because this page suggests it: https://www.electronjs.org/docs/latest/api/desktop-capturer

    Not sure if MacLoopbackAudioForScreenShare is stable enough to mention in the caveats section (maybe a link to this ticket?).

    Might be nice to have an example with setDisplayMediaRequestHandler and navigator.mediaDevices.getDisplayMedia

  12. codebytere commented on Jul 17, 2024

    @codebytere
    Member

    @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!

  13. marcello3d commented on Jul 18, 2024

    @marcello3d

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions