简体   繁体   English

WebRTC - 实时更换设备/摄像机

[英]WebRTC - Change device/camera in realtime

I'm having a problem trying to change my camera in real time, It works for the local video, but the remote person cannot see the new camera, and still sees the old one. 我在尝试实时更换相机时遇到问题,它适用于本地视频,但远程人员无法看到新相机,仍然可以看到旧相机。 I tried to stop the stream and init again but still not working. 我试图再次停止流和init,但仍然无法正常工作。 This is just some of my code. 这只是我的一些代码。 I have searched everywhere and I can't find a solution. 我到处搜索,我找不到解决方案。 Can someone help me out? 有人可以帮我吗?

function init() {
        getUserMedia(constraints, connect, fail);
}

$(".webcam-devices").on('change', function() {
    var deviceID = this.value;
    constraints.video = {
        optional: [{
            sourceId: deviceID
        }]
    };
    stream.getTracks().forEach(function (track) { track.stop(); });
    init();
});

You need to actually change the track you're sending in the PeerConnection. 您需要实际更改您在PeerConnection中发送的曲目。 In Firefox, you can use RTPSender.replaceTrack(new_track); 在Firefox中,您可以使用RTPSender.replaceTrack(new_track); to change without renegotiation (this is being added to the spec now). 无需重新协商即可进行更改(现在已添加到规范中)。 Otherwise, you need to add the new stream/track to the RTCPeerConnection, and remove the old one, and then process the onnegotiationneeded event and renegotatiate 否则,您需要将新流/轨道添加到RTCPeerConnection,并删除旧流,然后处理onnegotiationneeded事件并重新协商

See one of @jib's fiddles: Jib's replaceTrack() fiddle : 看看@ jib的小提琴之一: Jib的replaceTrack()小提琴

function flip() {
  flipped = 1 - flipped;
  return pc1.getSenders()[0].replaceTrack(streams[flipped].getVideoTracks()[0])
  .then(() => log("Flip! (notice change in dimensions & framerate!)"))
  .catch(failed);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM