gpt4 book ai didi

node.js - 使用 webrtc 和 Node js 的点对点视频流和套接字 io 远程视频不渲染

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:06 36 4
gpt4 key购买 nike

我正在使用此代码 https://bitbucket.org/webrtc/codelab 。当我尝试连接远程系统时,它会在远程视频框中显示我的网络摄像头视频。任何人都可以帮助了解 RTCPeerConnection 的工作原理吗?这是代码。

var localStream, localPeerConnection, remotePeerConnection;

var localVideo = document.getElementById("localVideo");
var remoteVideo = document.getElementById("remoteVideo");

var startButton = document.getElementById("startButton");
var callButton = document.getElementById("callButton");
var hangupButton = document.getElementById("hangupButton");
startButton.disabled = false;
callButton.disabled = true;
hangupButton.disabled = true;
startButton.onclick = start;
callButton.onclick = call;
hangupButton.onclick = hangup;

function trace(text) {
console.log((performance.now() / 1000).toFixed(3) + ": " + text);
}

function gotStream(stream){
trace("Received local stream");
localVideo.src = URL.createObjectURL(stream);
localStream = stream;
callButton.disabled = false;
}

function start() {
trace("Requesting local stream");
startButton.disabled = true;
getUserMedia({audio:true, video:true}, gotStream,
function(error) {
trace("getUserMedia error: ", error);
});
}

function call() {
callButton.disabled = true;
hangupButton.disabled = false;
trace("Starting call");

if (localStream.getVideoTracks().length > 0) {
trace('Using video device: ' + localStream.getVideoTracks()[0].label);
}
if (localStream.getAudioTracks().length > 0) {
trace('Using audio device: ' + localStream.getAudioTracks()[0].label);
}

var servers = null;

localPeerConnection = new RTCPeerConnection(servers);
trace("Created local peer connection object localPeerConnection");
localPeerConnection.onicecandidate = gotLocalIceCandidate;

remotePeerConnection = new RTCPeerConnection(servers);
trace("Created remote peer connection object remotePeerConnection");
remotePeerConnection.onicecandidate = gotRemoteIceCandidate;
remotePeerConnection.onaddstream = gotRemoteStream;

localPeerConnection.addStream(localStream);
trace("Added localStream to localPeerConnection");
localPeerConnection.createOffer(gotLocalDescription,handleError);
}

function gotLocalDescription(description){
localPeerConnection.setLocalDescription(description);
trace("Offer from localPeerConnection: \n" + description.sdp);
remotePeerConnection.setRemoteDescription(description);
remotePeerConnection.createAnswer(gotRemoteDescription,handleError);
}

function gotRemoteDescription(description){
remotePeerConnection.setLocalDescription(description);
trace("Answer from remotePeerConnection: \n" + description.sdp);
localPeerConnection.setRemoteDescription(description);
}

function hangup() {
trace("Ending call");
localPeerConnection.close();
remotePeerConnection.close();
localPeerConnection = null;
remotePeerConnection = null;
hangupButton.disabled = true;
callButton.disabled = false;
}

function gotRemoteStream(event){
remoteVideo.src = URL.createObjectURL(event.stream);
trace("Received remote stream");
}

function gotLocalIceCandidate(event){
if (event.candidate) {
remotePeerConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
trace("Local ICE candidate: \n" + event.candidate.candidate);
}
}

function gotRemoteIceCandidate(event){
if (event.candidate) {
localPeerConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
trace("Remote ICE candidate: \n " + event.candidate.candidate);
}
}

function handleError(){}

最佳答案

您可以通过 JavaScript 使用peer.js 进行视频流。

https://github.com/peers/peerjs

关于node.js - 使用 webrtc 和 Node js 的点对点视频流和套接字 io 远程视频不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34219799/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com