WebRTC - 实时更改设备/摄像头 [英] WebRTC - Change device/camera in realtime
问题描述
我在尝试实时更改相机时遇到问题,它适用于本地视频,但远程人员无法看到新相机,仍然看到旧相机。我试图停止流和init,但仍然不工作。这只是我的一些代码。
我到处搜索,我找不到一个解决方案。有人可以帮我吗?
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. 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();
});
推荐答案
您需要实际更改要发送的曲目在PeerConnection中。在Firefox中,您可以使用 RTPSender.replaceTrack(new_track);
更改而不进行重新协商(现在已添加到规范中)。否则,您需要将新的流/轨道添加到RTCPeerConnection,并删除旧的流,然后处理协商所需的事件和重新协商
You need to actually change the track you're sending in the PeerConnection. In Firefox, you can use 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
查看@ jib的fiddles : Jib's replaceTrack()小提示:
See one of @jib's fiddles: Jib's replaceTrack() fiddle:
function flip() {
flipped = 1 - flipped;
return pc1.getSenders()[0].replaceTrack(streams[flipped].getVideoTracks()[0])
.then(() => log("Flip! (notice change in dimensions & framerate!)"))
.catch(failed);
}
这篇关于WebRTC - 实时更改设备/摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!