javascript - WebRTC,如何在没有冰涓涓细的情况下收集全套候选人?
问题描述
我正在尝试在两台电脑之间进行视频通话,我不想使用冰滴,有时我可以进行视频通话,其他时候(我不知道为什么)我无法收集所有的冰候选人,iceGatheringState 停留在聚集状态,永远不会完成。
我已经尝试使用事件 onicecandidate 并等待空候选人。现在我正在使用 onIceGatheringStateChange。
pc=new RTCPeerConnection(iceServers);
pc.onicegatheringstatechange=function(){
if(pc.iceGatheringState=='complete'){
send_to_target(pc.localDescription);
}
}
localStream.getTracks().forEach(track=>pc.addTrack(track,localStream));
pc.createOffer().then(function(sessionDescription){
pc.setLocalDescription(sessionDescription);
})
我正在两台带有 chrome 的笔记本电脑上对此进行测试,我希望 iceGatheringState 进入完整状态或知道另一种方式/条件来收集候选冰,以便在不使用冰滴的情况下将 sessionDescription 发送到目标。
谢谢。
解决方案
据我所知,有两种方法可以确定“ICE 候选人聚集”/“ICE Trickle”何时完成。
- 您可以通过观察 icegatheringstatechange 事件并检查 iceGatheringState 的值是否等于“完成”来确定 ICE 候选收集已完成。
let pc = new RTCPeerConnection();
pc.onicegatheringstatechange = ev => {
let connection = ev.target;
switch(connection.iceGatheringState) {
case "gathering":
/* collection of candidates has begun */
break;
case "complete":
/* collection of candidates is finished */
break;
}
}
- 您还可以让 icecandidate 事件的处理程序查看其候选属性是否等于“null”。
let pc = new RTCPeerConnection();
pc.onicecandidate = function(event) {
if (event.candidate) {
/* Code for each candidate */
/* Send the candidate to the remote peer */
} else {
/* All ICE candidates have been sent */
}
}
WebRTC 的有用链接:
推荐阅读
- scala - 如何在 Scala 中匹配 Char?
- ag-grid - 如何提高 ag-grid 绘图性能
- python - 在 Keras/TF 中,有没有办法预训练模型并重用它来测试其他可能的层?
- xcode - Firebase 存储图像未在 iOS 上下载(在 Unity 编辑器和 android 中正常)
- gatsby - Netlify CMS 预览和 gatsby-remark-embed-video 之间的冲突
- web-scraping - 从网站检索验证码
- java - 寻找有关 Spark DStream 到 Parquet 文件的性能提示
- javascript - asyncjs 回调未执行
- python - 将字典映射标签分配给 pandas 中列的索引值
- javafx - 如何更改 JavaFX 中 Time4J CalendarPicker 的字体?