4

如何在 RTCMulticonnection 中切换摄像头

我获取设备列表及其 ID

  var videoDevices = document.getElementById('video-devices');
        var audioDevices = document.getElementById('audio-devices');

        connection.DetectRTC.load(function() {

                connection.DetectRTC.audioInputDevices.forEach(function(device) {
                    var option = document.createElement('option');
                    option.innerHTML = device.label;
                    option.value = device.id;
                    document.querySelector('select').appendChild(option);
                    });
                connection.DetectRTC.videoInputDevices.forEach(function(device) {
                    var option = document.createElement('option');
                    option.innerHTML = device.label;
                    option.value = device.id;
                    videoDevices.appendChild(option);
                });
        });

并尝试通过以下方式更改相机。取自GitHub 问题

document.getElementById('switch-camera').onclick = function() {
          var videoSourceId = videoDevices.value;
          connection.codecs.video = 'VP8';
          connection.bandwidth  = { // all values in kbits/per/seconds
            audio: 192,
            video: 512,
            screen: 512
           };
           connection.stopMediaAccess();

            setTimeout(() => {
                 connection.mediaConstraints.video.optional = [{
                       sourceId: videoSourceId
                 }];

            connection.addStream({audio: true, video: true});

             },2000);
   };

但是没有使用相机根本不会改变我尝试了很多方法但失败了

这是codepen上的一个例子

WebRTC 示例中的这个示例可能有助于它做我想要的,但对与 RTCMulticoonection 的集成感到困惑。

4

1 回答 1

2

如果您想前后切换摄像头,RTCMulticonnection的文档中有一个解决方案。

如果您想切换相机(即前/后),这里是您的解决方案

function openOrJOinRoom(roomid) {
    connection.mediaConstraints = {
        audio: true,
        video: {
            facingMode: {exact : 'environment'}
        }
    };

    connection.openOrJoin(roomid);
}

function selectFrontCameraDuringActiveSession() {
    // we need to stop previous video tracks because
    // mobile device may not allow us capture
    // both front and back camera streams
    // at the same time
    connection.attachStreams.forEach(function(stream) {
        // stop only video tracks
        // so that we can recapture video track
        stream.getVideoTracks().forEach(function(track) {
            track.stop();
        });
    });

    var mediaConstraints = {
        audio: false, // NO need to capture audio again
        video: {
            facingMode: {exact : 'user'}
        }
    };

    navigator.mediaDevices.getUserMedia(mediaConstraints).then(function(frontCamera) {
        var frontCameraTrack = frontCamera.getVideoTracks()[0];
        connection.getAllParticipants().forEach(function(pid) {
            connection.peers[pid].peer.getSenders().forEach(function(sender) {
                if (sender.track.kind == 'video') {
                    sender.replaceTrack(frontCameraTrack);
                }
            });
        });
    });
}

function selectBackCameraDuringActiveSession() {
    // we need to stop previous video tracks because
    // mobile device may not allow us capture
    // both front and back camera streams
    // at the same time
    connection.attachStreams.forEach(function(stream) {
        // stop only video tracks
        // so that we can recapture video track
        stream.getVideoTracks().forEach(function(track) {
            track.stop();
        });
    });

    var mediaConstraints = {
        audio: false, // NO need to capture audio again
        video: {
            facingMode: {exact : 'environment'}
        }
    };

    navigator.mediaDevices.getUserMedia(mediaConstraints).then(function(frontCamera) {
        var frontCameraTrack = frontCamera.getVideoTracks()[0];
        connection.getAllParticipants().forEach(function(pid) {
            connection.peers[pid].peer.getSenders().forEach(function(sender) {
                if (sender.track.kind == 'video') {
                    sender.replaceTrack(frontCameraTrack);
                }
            });
        });
    });
}

我从 GitHub issue复制了这个。请不要忘记添加adapter.js 希望这个答案对您有所帮助。

于 2020-06-18T15:32:23.100 回答