我一直在尝试实施点对点webrtc视频流应用。 我已经跟随了flussonic https://gist.github.com/theleon/80dd514b435e295f272988c1064d3c4c
提供的示例代码`<html>
<body>
<video id="videoElement" controls ></video>
<script src="https://webrtc.github.io/adapter/adapter-latest.js">
</script>
<script>
window.onload = function () {
var candidates = [];
var websocket;
var peerConnection;
var video = document.getElementById('videoElement');
function sendWebSocketMessage(data) {
console.log('data:', data);
websocket.send(JSON.stringify(data));
}
function onWebSocketMessage(event) {
var message = JSON.parse(event.data);
console.log("message:", message.type);
switch (message.type) {
case "offer":
var description = new
window.RTCSessionDescription(message);
peerConnection.setRemoteDescription(description)
.then(function () {
var answer = peerConnection.createAnswer();
console.log("1 answer:",answer);
return answer;
})
.then(function (answer) {
console.log("2 answer:",answer);
return peerConnection.setLocalDescription(answer);
})
.then(function () {
console.log("3 peerConnection.localDescription:",peerConnection.localDescription);
websocket.send(JSON.stringify(peerConnection.localDescription));
});
break;
case "candidate":
candidates.push(message);
if(message.candidate !== null){
console.log("message candidate:",message.candidate);
var candidate = new window.RTCIceCandidate(message.candidate);
peerConnection.addIceCandidate(candidate);
}
console.log("candidates");
console.log(candidates);
break;
}
}
function openFlussonicWebSocketConnection(options) {
var url =
options.protocol + "://" +
options.server + ":" +
options.port + "/" +
options.stream;
console.log("url:", url);
websocket = new WebSocket(url);
websocket.onopen = initPeerConnection;
websocket.onmessage = onWebSocketMessage;
}
function initPeerConnection() {
peerConnection = new window.RTCPeerConnection(null);
peerConnection.stream_id = "remote1";
peerConnection.onicecandidate = gotIceCandidate;
peerConnection.ontrack = gotRemoteTrack;
console.log("peerConnection:",peerConnection);
}
function gotIceCandidate(event) {
var candidate = event.candidate;
if (candidate) {
sendWebSocketMessage({
type: 'candidate',
stream_id: "remote1",
label: candidate.sdpMLineIndex,
id: candidate.sdpMid,
candidate: candidate
});
}
}
function gotRemoteTrack(event) {
if (event.track.kind === "video") {
video.srcObject = event.streams[0];
//peerConnection.addStream(event.streams[0]);
}
}
openFlussonicWebSocketConnection({
"protocol": "wss",
"server": "fluss.dev-tst.ninja",
"port": "443",
"stream": "indian/webrtc"
});
};
</script>
</body>
</html>`
The URL generated by flussonic is as follows
虽然视频是在本地网络上的多个客户端上流式传输的,但是flussonic服务器返回的唯一候选者是服务器本身。我在初始化rtcpeerconnection时尝试添加google stun服务器(stun.l.google.com:19302),但也将服务器作为候选者返回。我觉得我错过了一些信号,我错过了什么?