1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-22 19:42:20 +00:00

Auto reconnect to stream

This commit is contained in:
Alexandre Iooss 2020-09-24 12:40:48 +02:00
parent 097766141f
commit 43158a655f
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
2 changed files with 87 additions and 65 deletions

View File

@ -1,3 +1,6 @@
let peerConnection;
startPeerConnection = () => {
// Init peer connection
peerConnection = new RTCPeerConnection({
iceServers: [
@ -8,21 +11,30 @@ peerConnection = new RTCPeerConnection({
]
})
// On connection change, inform user
// On connection change, change indicator color
// if connection failed, restart peer connection
peerConnection.oniceconnectionstatechange = e => {
console.log(peerConnection.iceConnectionState)
switch (peerConnection.iceConnectionState) {
case "disconnected":
console.log(peerConnection.iceConnectionState)
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("FIXME Failed");
break;
case "disconnected":
console.log("temp network issue")
break;
case "connected":
console.log("temp network issue resolved!")
break;
console.log("Connection failed, restarting...")
peerConnection.close()
peerConnection = null
startPeerConnection()
break
default:
console.log(peerConnection.iceConnectionState)
break
}
}
@ -44,6 +56,7 @@ peerConnection.onicecandidate = event => {
// The server know the stream name from the url
// The server replies with its description
// After setRemoteDescription, the browser will fire ontrack events
console.log("Sending session description to server")
fetch(window.location.href, {
method: 'POST',
headers: {
@ -60,11 +73,13 @@ peerConnection.onicecandidate = event => {
// When video track is received, configure player
peerConnection.ontrack = function (event) {
console.log(`New ${event.track.kind} track`)
if (event.track.kind === "video") {
const viewer = document.getElementById('viewer')
viewer.srcObject = event.streams[0]
viewer.autoplay = true
viewer.controls = true
viewer.muted = true
}
}
}
// Start
startPeerConnection()

View File

@ -3,7 +3,7 @@
<div class="col-video">
<!-- Video -->
<div class="video-responsive">
<video id="viewer" poster="/static/img/no_stream.svg"></video>
<video id="viewer" poster="/static/img/no_stream.svg" muted controls autoplay></video>
</div>
<!-- Links under video -->
@ -16,6 +16,13 @@
d="M14.354 1.646a.5.5 0 0 1 0 .708l-8 8a.5.5 0 0 1-.708-.708l8-8a.5.5 0 0 1 .708 0z" />
</svg>
<code>rtmps://{{.Cfg.Hostname}}:1935/play/{{.Path}}</code>-->
<svg id="connectionIndicator" width="1em" height="1em" viewBox="0 0 16 16" fill="#dc3545" xmlns="http://www.w3.org/2000/svg">
<rect width="4" height="5" x="1" y="10" rx="1"/>
<rect width="4" height="9" x="6" y="6" rx="1"/>
<rect width="4" height="14" x="11" y="1" rx="1"/>
</svg>
<a href="#" id="sideWidgetToggle" title="Cacher/Afficher le chat">»</a>
</p>
</div>