Javascript WebRTC&;对等JS调用问题
在我的函数connect(id):我使用Javascript WebRTC&;对等JS调用问题,javascript,webrtc,getusermedia,peerjs,Javascript,Webrtc,Getusermedia,Peerjs,在我的函数connect(id):我使用var call=this.peer.call(id,this.mediastream)启动一个调用然后我使用 call.on('stream', function(stream) { // `stream` is the MediaStream of the remote peer. // Here you'd add it to an HTML video/canvas element. pee
var call=this.peer.call(id,this.mediastream)启动一个调用代码>然后我使用
call.on('stream', function(stream)
{
// `stream` is the MediaStream of the remote peer.
// Here you'd add it to an HTML video/canvas element.
peerVideo.srcObject = stream;
console.log("Call stream has started! CLinet end!!!!!" + stream);
output.innerHTML = "Streaming audio from other client...";
});
从对等方获取音频。永远不会执行此call.on stream,这意味着调用不会返回任何内容
在事情的另一面,他们可以很好地听到来电者的声音。我不知道怎么了
以下是完整的代码:
const video = document.getElementById("video");
const peerVideo = document.getElementById("peervideo");
const output = document.getElementById("output");
const peerText = document.getElementById("peerid");
const peerButton = document.getElementById("peersubmit");
var promise = null;
var mediastream = null;
var peer = null;
var myId = null;
async function init()
{
//init the media devices
initMediaDevices();
//init peer
initPeer();
//Add timeout to ensure the peer.on method had time to get id from server
setTimeout(() => { console.log("My ID is: " + this.myId); output.innerHTML = "My ID: " + formatString(this.myId)}, 2000);
}
function initMediaDevices()
{
//Setup stream for usermedia
try
{
this.promise = navigator.mediaDevices.getUserMedia({audio: true})
promise.then((stream) =>
{
setStream(stream);
//video.srcObject = stream;
console.log(stream);
})
output.innerHTML = "Audio was established!";
}
catch(err)
{
console.log(err)
output.innerHTML = "Audio Failed!"
}
}
function initPeer()
{
this.peer = new Peer();
this.peer.on('open', function(id)
{
setID(id)
});
peer.on('call', function(call)
{
// Answer the call, providing our mediaStream
call.answer(this.mediastream);
console.log(call + "-------------------- got here peer call");
output.innerHTML = "Connected to Peer";
call.on('stream', function(stream)
{
// `stream` is the MediaStream of the remote peer.
// Here you'd add it to an HTML video/canvas element.
peerVideo.srcObject = stream;
console.log("Call stream has started! Peer end");
output.innerHTML = "Streaming audio from other client...";
});
});
}
function setID(id)
{
this.myId = id;
}
function setStream(stream)
{
this.mediastream = stream;
console.log("Media Stream Set! " + this.mediastream);
}
function connect(id)
{
var call = this.peer.call(id, this.mediastream);
call.on('stream', function(stream)
{
// `stream` is the MediaStream of the remote peer.
// Here you'd add it to an HTML video/canvas element.
peerVideo.srcObject = stream;
console.log("Call stream has started! CLinet end!!!!!" + stream);
output.innerHTML = "Streaming audio from other client...";
});
console.log(call + "----------------------" + this.mediastream + " This is the person that connected");
}
init();
//Event listeners
peerButton.addEventListener("click", () =>
{
let id = peerText.value;
console.log("Button Pressed!")
connect(id);
});
//unrelated
function formatString(string)
{
var newString = "";
for (var i = 0; i < string.length; i++)
{
var letter = string.charAt(i);
if(isNaN(letter))
{
newString += letter.fontcolor("red");
}
else
{
newString += letter;
}
}
return newString;
}
const video=document.getElementById(“视频”);
const peerVideo=document.getElementById(“peerVideo”);
const output=document.getElementById(“输出”);
const peerText=document.getElementById(“peerid”);
const peerButton=document.getElementById(“peersubmit”);
var-promise=null;
var mediastream=null;
var-peer=null;
var myId=null;
异步函数init()
{
//初始化媒体设备
initMediaDevices();
//初始对等
initPeer();
//添加超时以确保peer.on方法有时间从服务器获取id
setTimeout(()=>{console.log(“我的ID是:+this.myId”);output.innerHTML=“我的ID:+formatString(this.myId)},2000);
}
函数initMediaDevices()
{
//usermedia的设置流
尝试
{
this.promise=navigator.mediaDevices.getUserMedia({audio:true})
承诺。然后((流)=>
{
溪流;
//video.srcObject=流;
console.log(流);
})
output.innerHTML=“音频已建立!”;
}
捕捉(错误)
{
console.log(错误)
output.innerHTML=“音频失败!”
}
}
函数initPeer()
{
this.peer=新的peer();
this.peer.on('open',函数(id)
{
setID(id)
});
peer.on('call',函数(call)
{
//接听电话,提供我们的mediaStream
call.answer(this.mediastream);
log(call+“--------------got here peer call”);
output.innerHTML=“已连接到对等节点”;
call.on('stream',函数(stream)
{
//'stream'是远程对等方的媒体流。
//在这里,您可以将其添加到HTML视频/画布元素中。
peerVideo.srcObject=流;
log(“调用流已启动!对等端”);
output.innerHTML=“来自其他客户端的流式音频…”;
});
});
}
函数setID(id)
{
this.myId=id;
}
函数设置流(流)
{
this.mediastream=流;
log(“媒体流集!”+this.mediastream);
}
功能连接(id)
{
var call=this.peer.call(id,this.mediastream);
call.on('stream',函数(stream)
{
//'stream'是远程对等方的媒体流。
//在这里,您可以将其添加到HTML视频/画布元素中。
peerVideo.srcObject=流;
日志(“呼叫流已经开始!客户端结束!!!”+流);
output.innerHTML=“来自其他客户端的流式音频…”;
});
console.log(call+“-------------------------”+this.mediastream+“这是连接的人”);
}
init();
//事件侦听器
peerButton.addEventListener(“单击”,()=>
{
设id=peerText.value;
log(“按下按钮!”)
连接(id);
});
//无关的
函数formatString(字符串)
{
var newString=“”;
对于(变量i=0;i
还有html
<video id="video" autoplay>Video Stream no available</video>
<video id="peervideo" autoplay>Video Stream no available</video>
<h3 id="output"></h3>
<input type="text" id="peerid">
<input type="submit" value="Submit" id="peersubmit">
视频流不可用
视频流不可用
我也有同样的问题。
当您决定设置一个调试器并在周围设置断点时
var call = this.peer.call(id, this.mediastream);
call.on('stream', function(s
您将收到流,听起来您需要设置一些setTimeout来防止这种行为,我知道它是有效的。。。但这是个坏习惯