我一直在阅读这篇文章的信号解决方案.作者提到在建立连接时使用RTCDataChannel进行信令.
使用RTCDataChannel进行信令
需要信令服务来启动WebRTC会话.
然而,一旦在两个对等体之间建立了连接,RTCDataChannel理论上可以接管作为信令信道.这可能会减少信令的延迟 - 因为消息直接飞行 - 并有助于减少信令服务器带宽和处理成本.我们没有演示,但看这个空间!
为什么需要信令,因为连接已经建立?
每一方最初声明它将发送哪些音频和/或视频轨道,以便可以打开正确数量的端口,并且可以确定适用于两个对等端的分辨率和格式.需要信令信道来将得到的SDP提供/应答以及每个端口的涓流ICE候选发送到另一侧.
一旦连接,如果你单独保留这个设置 - 基本上永远不会添加轨道到连接,删除任何,或显着改变轨道属性 - 那么你将不再需要信号服务器.
如果你确实改变了这些东西,那么就需要重新协商,这就像它听起来的那样:信号通道上的另一轮,就像第一轮一样.
添加轨道的原因可能是第二个摄像头,另一个视频源(可能来自另一个参与者),或者可能是屏幕共享,类似的东西.
该文章是正确的,可以使用数据通道.这是一个演示!(目前仅限Firefox.)
如果您有另一种发现方式,那么该文章对于需要信令服务是错误的 - 正如这个演示蹩脚地证明的那样.
初始连接仅限聊天,但任何一方都可以推送添加视频.对此的重新协商是通过数据通道完成的(因为没有信令服务器!)
使用小提琴的说明:
Offer按钮并复制优惠.addTrack尚未!)addTrack两端,视频应显示在另一端.addTrack另一个方向,你应该有双向视频.var dc = null, sc = null, pc = new mozRTCPeerConnection(), live = false;
pc.onaddstream = e => v2.mozSrcObject = e.stream;
pc.ondatachannel = e => dc? scInit(sc = e.channel) : dcInit(dc = e.channel);
v2.onloadedmetadata = e => { log("Face time!"); };
function addTrack() {
navigator.mediaDevices.getUserMedia({video:true, audio:true})
.then(stream => pc.addStream(v1.mozSrcObject = stream));
}
pc.onnegotiationneeded = e => {
pc.createOffer().then(d => pc.setLocalDescription(d)).then(() => {
if (live) sc.send(JSON.stringify({ "sdp": pc.localDescription }));
}).catch(failed);
};
function scInit() {
sc.onmessage = e => {
var msg = JSON.parse(e.data);
if (msg.sdp) {
var desc = new mozRTCSessionDescription(JSON.parse(e.data).sdp);
if (desc.type == "offer") {
pc.setRemoteDescription(desc).then(() => pc.createAnswer())
.then(answer => pc.setLocalDescription(answer)).then(() => {
sc.send(JSON.stringify({ "sdp": pc.localDescription }));
}).catch(failed);
} else {
pc.setRemoteDescription(desc).catch(failed);
}
} else if (msg.candidate) {
pc.addIceCandidate(new mozRTCIceCandidate(msg.candidate)).catch(failed);
}
};
}
function dcInit() {
dc.onopen = () => { live = true; log("Chat!"); };
dc.onmessage = e => log(e.data);
}
function createOffer() {
button.disabled = true;
dcInit(dc = pc.createDataChannel("chat"));
scInit(sc = pc.createDataChannel("signaling"));
pc.createOffer().then(d => pc.setLocalDescription(d)).catch(failed);
pc.onicecandidate = e => {
if (e.candidate) return;
if (!live) {
offer.value = pc.localDescription.sdp;
offer.select();
answer.placeholder = "Paste answer here";
} else {
sc.send(JSON.stringify({ "candidate": e.candidate }));
}
};
};
offer.onkeypress = e => {
if (e.keyCode != 13 || pc.signalingState != "stable") return;
button.disabled = offer.disabled = true;
var obj = { type:"offer", sdp:offer.value };
pc.setRemoteDescription(new mozRTCSessionDescription(obj))
.then(() => pc.createAnswer()).then(d => pc.setLocalDescription(d))
.catch(failed);
pc.onicecandidate = e => {
if (e.candidate) return;
if (!live) {
answer.focus();
answer.value = pc.localDescription.sdp;
answer.select();
} else {
sc.send(JSON.stringify({ "candidate": e.candidate }));
}
};
};
answer.onkeypress = e => {
if (e.keyCode != 13 || pc.signalingState != "have-local-offer") return;
answer.disabled = true;
var obj = { type:"answer", sdp:answer.value };
pc.setRemoteDescription(new mozRTCSessionDescription(obj)).catch(failed);
};
chat.onkeypress = e => {
if (e.keyCode != 13) return;
dc.send(chat.value);
log(chat.value);
chat.value = "";
};
var log = msg => div.innerHTML += "<p>" + msg + "</p>";
var failed = e => log(e.name + ": " + e.message + " line " + e.lineNumber);Run Code Online (Sandbox Code Playgroud)
<video id="v1" height="120" width="160" autoplay muted></video>
<video id="v2" height="120" width="160" autoplay></video><br>
<button id="button" onclick="createOffer()">Offer:</button>
<textarea id="offer" placeholder="Paste offer here"></textarea><br>
Answer: <textarea id="answer"></textarea><br>
<button id="button" onclick="addTrack()">AddTrack</button>
<div id="div"></div><br>
Chat: <input id="chat"></input><br>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
353 次 |
| 最近记录: |