如何在WebRTC中一次使用getUsermedia进行多次连接?

时间:2018-10-11 02:04:33

标签: webrtc getusermedia rtcmulticonnection

我正在使用WebRTC进行多视频会议。 但是我发现,当其他人来到我的房间时,getUserMedia将被调用。

从WebRTC中的多连接开始,有没有方法只能调用一次getUsermedia?

1 个答案:

答案 0 :(得分:1)

  1. 设置dontCaptureUserMedia=true
  2. 自己调用navigator.mediaDevices.getUserMedia
  3. 将流推送到attachStreams数组
  4. 甚至触发onstream或手动设置localVideo.srcObject=stream
  5. 打开或加入房间

示例:

// first step
connection.dontCaptureUserMedia = true;

// seocond step
navigator.mediaDevices.getUserMedia({
    audio: true,
    video: true
}).then(function(stream) {
    // third step
    stream.isVideo = true;
    connection.attachStreams = [stream];

    // fourth step
    var video = document.createElement('video');
    video.playsinline = true;
    video.controls = true;
    video.muted = true;
    video.srcObject = stream;
    connection.onstream({
        stream: stream,
        type: 'local',
        streamid: stream.id,
        mediaElement: video
    });

    // last step: now open or join a room
    connection.openOrJoin('your-room-id');
});

它如何工作?

  1. dontCaptureUserMedia将忽略(绕过)任何本地getUserMedia调用
  2. attachStreams数组将用于与远程方共享

其余的代码是随意的。