在Mac OS上使用WebRTC和Electron进行屏幕共享和视频/音频通话

时间:2018-12-11 19:14:11

标签: node.js macos electron webrtc getusermedia

我正在尝试创建一个电子应用程序,该应用程序可以使用webrtc与系统音频共享桌面,如果我设置了约束条件:

const constraints = {
 audio: {
 mandatory: {
  chromeMediaSource: 'desktop'
 }
  },
video: {
mandatory: {
 chromeMediaSource: 'desktop'
}
 }
  }

我遇到了此问题 Mac OS音频

错误:adm_helpers.cc(73)]无法查询立体声录音。,然后显示“ NotFoundError:找不到请求的设备

2 个答案:

答案 0 :(得分:0)

您需要使用电子的desktopCapturer api。

示例-

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

desktopCapturer.getSources({types: ['window', 'screen']}, (error, sources) => {
  if (error) throw error
  for (let i = 0; i < sources.length; ++i) {
    if (sources[i].name === 'Electron') {
      navigator.mediaDevices.getUserMedia({
        audio: false,
        video: {
          mandatory: {
            chromeMediaSource: 'desktop',
            chromeMediaSourceId: sources[i].id,
            minWidth: 1280,
            maxWidth: 1280,
            minHeight: 720,
            maxHeight: 720
          }
        }
      })
      .then((stream) => 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)
}

并使用上面的audio标志在屏幕共享时获取音频。

此处有更多详细信息-https://electronjs.org/docs/api/desktop-capturer

答案 1 :(得分:0)

对于MacOS用户,您需要分别获取音频和视频流,然后像这样合并流:

const stream = await navigator.mediaDevices.getUserMedia({
    audio: false,
    video: {
        mandatory: {
            chromeMediaSource: 'desktop',
            chromeMediaSourceId: source.id
        }
    }
});
navigator.mediaDevices.getUserMedia({
    audio: {
        mandatory: {
            chromeMediaSource: 'desktop'
        }
    },
    video: false
}).then(function(audioStream) {
  var audioTracks = audioStream.getAudioTracks();

  // merge audio and video tracks
  if(audioTracks.length > 0) {
    stream.addTrack(audioTracks[0]);
  }

    recorder = new MediaRecorder(stream, {
        mimeType: 'YOUR MIME TYPE'
    });

    recorder.ondataavailable = yourDataHandler;
    recorder.onstop = yourStopHandler;

    recorder.start();
}).catch(function(err) {
    console.error('audioTrackError', err);
});