1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2025-02-06 14:53:01 +00:00
ghostream/web/static/js/viewer.js

82 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-10-20 21:45:26 +02:00
let peerConnection;
let streamPath = window.location.href;
let stream = streamPath;
let quality = "source";
2020-09-22 22:08:14 +02:00
2020-10-20 21:45:26 +02:00
const startPeerConnection = () => {
2020-09-24 12:40:48 +02:00
// Init peer connection
peerConnection = new RTCPeerConnection({
2020-09-29 17:27:19 +02:00
iceServers: [{ urls: stunServers }]
2020-10-20 21:45:26 +02:00
});
2020-09-22 22:08:14 +02:00
2020-09-24 12:40:48 +02:00
// On connection change, change indicator color
// if connection failed, restart peer connection
peerConnection.oniceconnectionstatechange = e => {
2020-10-20 21:45:26 +02:00
console.log("ICE connection state changed, " + peerConnection.iceConnectionState);
2020-09-24 12:40:48 +02:00
switch (peerConnection.iceConnectionState) {
2020-10-20 21:45:26 +02:00
case "disconnected":
document.getElementById("connectionIndicator").style.fill = "#dc3545";
break;
case "checking":
document.getElementById("connectionIndicator").style.fill = "#ffc107";
break;
case "connected":
document.getElementById("connectionIndicator").style.fill = "#28a745";
break;
case "closed":
case "failed":
console.log("Connection failed, restarting...");
peerConnection.close();
peerConnection = null;
setTimeout(startPeerConnection, 1000);
break;
2020-09-24 12:40:48 +02:00
}
2020-10-20 21:45:26 +02:00
};
2020-09-22 22:08:14 +02:00
2020-09-24 12:40:48 +02:00
// We want to receive audio and video
2020-10-20 21:45:26 +02:00
peerConnection.addTransceiver("video", { "direction": "sendrecv" });
peerConnection.addTransceiver("audio", { "direction": "sendrecv" });
2020-09-23 13:52:12 +02:00
2020-09-24 12:40:48 +02:00
// Create offer and set local description
peerConnection.createOffer().then(offer => {
// After setLocalDescription, the browser will fire onicecandidate events
2020-10-20 21:45:26 +02:00
peerConnection.setLocalDescription(offer);
}).catch(console.log);
2020-09-23 13:52:12 +02:00
2020-09-24 12:40:48 +02:00
// When candidate is null, ICE layer has run out of potential configurations to suggest
// so let's send the offer to the server
peerConnection.onicecandidate = event => {
if (event.candidate === null) {
// Send offer to server
// The server replies with its description
// After setRemoteDescription, the browser will fire ontrack events
2020-10-20 21:45:26 +02:00
console.log("Sending session description to server");
2020-10-06 09:20:25 +02:00
fetch(streamPath, {
2020-10-20 21:45:26 +02:00
method: "POST",
2020-09-24 12:40:48 +02:00
headers: {
2020-10-20 21:45:26 +02:00
"Accept": "application/json",
"Content-Type": "application/json"
2020-09-24 12:40:48 +02:00
},
2020-10-20 21:45:26 +02:00
body: JSON.stringify({
"webRtcSdp": peerConnection.localDescription,
"stream": stream,
"quality": quality
})
2020-09-24 12:40:48 +02:00
})
.then(response => response.json())
.then((data) => peerConnection.setRemoteDescription(new RTCSessionDescription(data)))
2020-10-20 21:45:26 +02:00
.catch(console.log);
2020-09-24 12:40:48 +02:00
}
2020-10-20 21:45:26 +02:00
};
2020-09-22 22:08:14 +02:00
2020-09-24 12:40:48 +02:00
// When video track is received, configure player
peerConnection.ontrack = function (event) {
2020-10-20 21:45:26 +02:00
console.log(`New ${event.track.kind} track`);
2020-09-24 12:40:48 +02:00
if (event.track.kind === "video") {
2020-10-20 21:45:26 +02:00
const viewer = document.getElementById("viewer");
viewer.srcObject = event.streams[0];
2020-09-24 12:40:48 +02:00
}
2020-10-20 21:45:26 +02:00
};
};