Skip to content

[Bug]: Could not start audio source error on mediaDevices.getUserMedia #32299

Closed as not planned

Description

@Shubham-Kumar-2000

Preflight Checklist

Electron Version

16.0.4

What operating system are you using?

Ubuntu

Operating System Version

Linux Jacob 5.11.0-43-generic #47~20.04.2-Ubuntu SMP Mon Dec 13 11:06:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

Code given in docs should work :

// In the renderer process.
const { desktopCapturer } = require('electron')

desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
  for (const source of sources) {
    if (source.name === 'Electron') {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({
          audio: {
            mandatory: {
              chromeMediaSource: "desktop",
              chromeMediaSourceId: id,
            },
          },
          video: {
            mandatory: {
              chromeMediaSource: "desktop",
              chromeMediaSourceId: id,
            },
          },
        })
        handleStream(stream)
      } catch (e) {
        handleError(e)
      }
      return
    }
  }
})

function handleStream (stream) {
  const video = document.querySelector('video')
  video.srcObject = stream
  video.onloadedmetadata = (e) => video.play()
}

function handleError (e) {
  console.log(e)
}

Actual Behavior

It doesn't work instead gives error :

> DOMException: Error could not start audio source

If we change audio to false then it gives different error :

> DOMException: Error starting screen capture

Testcase Gist URL

No response

Additional Information

The same is working on Windows seamlessly.

Activity

  1. changed the title [-][Bug]: [/-] [+][Bug]: Could not start audio source error on mediaDevices.getUserMedia[/+] on Dec 30, 2021
  2. TheRafaelFarias commented on Mar 10, 2022

    @TheRafaelFarias

    Some update on this? Its march and nobody said anything

    I tried with electron 14.2.6, 15.4.0 and the 17.1.1, none of them worked, same error

  3. Shubham-Kumar-2000 commented on Mar 10, 2022

    @Shubham-Kumar-2000
    ContributorAuthor

    Yes I got a solution.

    Its basically a workaround.

    First execute this on installation :

    pactl load-module module-remap-source master="$(pactl list | grep -A2 '^Source #' |  grep 'Name: .*\.monitor$' | awk '{print $NF}' | tail -n1)" source_name=virtmic source_properties=device.description=Virtual_Microphone
    

    Then in your renderer process :

    const system = await ipcRenderer.invoke("getOs");
          console.log({ system });
          if (!system) {
            throw new Error("System not supported");
          }
    
          const constraints = {
            audio: false,
            video: false,
          };
    
          if (system === "linux") {
            let devices = await navigator.mediaDevices.enumerateDevices();
            // console.log({ devices });
    
            devices = devices.filter(
              (device) => device.label === "harmonize_desktop"
            );
            if (devices.length === 0) {
              throw new Error("Please reinstall the application");
            }
    
            const device = devices[0];
            constraints.audio = {
              deviceId: {
                exact: device.deviceId,
              },
            };
          }
    
          if (system === "windows") {
            constraints.audio = {
              mandatory: {
                chromeMediaSource: "desktop",
                chromeMediaSourceId: id,
              },
            };
            constraints.video = {
              mandatory: {
                chromeMediaSource: "desktop",
                chromeMediaSourceId: id,
              },
            };
          }
    
          newStream = await navigator.mediaDevices.getUserMedia(constraints);

    Finally in the main process :

    ipcMain.handle("getOs", () => {
      const platforms = {
        win32: "windows",
        linux: "linux",
        darwin: "macos",
      };
      return platforms[process.platform];
    });

    Well I haven't completed the macOS implemention yet thus have excluded that.

  4. TheRafaelFarias commented on Mar 11, 2022

    @TheRafaelFarias

    Didn't worked for me. await navigator.mediaDevices.enumerateDevices(); only returns the camera and the audio and not the applications

  5. kei49 commented on Sep 12, 2022

    @kei49

    I faced the same errors on MacOS Big Sur Version 11.6.8 with electron 20.0.3. Any solutions?

  6. kei49 commented on Sep 12, 2022

    @kei49

    I found the caveats about this restriction on the Electron official 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.
    It is possible to circumvent this limitation by capturing system audio with another macOS app like Soundflower and passing it through a virtual audio input device. This virtual device can then be queried with navigator.mediaDevices.getUserMedia.

    https://www.electronjs.org/docs/latest/api/desktop-capturer#caveats

  7. PDKSophia commented on Dec 27, 2022

    @PDKSophia

    I have the same problem,does anyone have a solution?

    Electron Version

    21.3.0

    My Code

    if (this.systemAudioStream) {
      this.systemAudioStream.getTracks()?.forEach((track) => {
        track.stop();
      });
    }
    const systemAudioStream = await navigator.mediaDevices.getUserMedia({
      audio: {
        mandatory: {
          chromeMediaSource: 'desktop',
        },
      },
      video: {
        mandatory: {
         chromeMediaSource: 'desktop',
        },
      },
    });
      
    systemAudioStream.getVideoTracks()?.forEach((track) => systemAudioStream.removeTrack(track)); 
    this.systemAudioStream = systemAudioStream;
    
    log.info('init system audio stream success');

    It doesn't work instead gives error :

    DOMException: Error could not start audio source

  8. wreper12 commented on Jan 11, 2023

    @wreper12

    I am also having the same issue, it works perfectly fine on windows but on Ubuntu it does not work, the regular microphone can be assessed but anything from desktopCapturer.getSources does not work.
    Electron Version:
    22.0.0

  9. github-actions commented on Apr 11, 2023

    @github-actions
    Contributor

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

  10. github-actions commented on May 11, 2023

    @github-actions
    Contributor

    This issue has been closed due to inactivity, and will not be monitored. If this is a bug and you can reproduce this issue on a supported version of Electron please open a new issue and include instructions for reproducing the issue.

  11. Lalg28 commented on Jun 29, 2023

    @Lalg28

    I'm already having the same error with Electron 22.0.0 and MacOS Ventura 13.2 🥲

  12. vvillaluna commented on Aug 16, 2023

    @vvillaluna

    Having same error here but it's working fine on other machines. It might have to do something on the hardware or audio drivers installed on a machine.

  13. akshiii commented on Mar 2, 2024

    @akshiii

    Ive spend 2 days on this, and still no solution to this, It always throws the same error, could not start audio. But it does not even start video
    const stream = await navigator.mediaDevices .getUserMedia({ audio: true, video: { width: { max: 1080, }, }, }) .then( (st) => { videoRef.src = st; setMyStream(st); }, (err) => { console.log(err); } );

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions