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 // Init peer connection
peerConnection = new RTCPeerConnection({ peerConnection = new RTCPeerConnection({
iceServers: [ 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 => { peerConnection.oniceconnectionstatechange = e => {
console.log(peerConnection.iceConnectionState)
switch (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 "closed":
case "failed": case "failed":
console.log("FIXME Failed"); console.log("Connection failed, restarting...")
break; peerConnection.close()
case "disconnected": peerConnection = null
console.log("temp network issue") startPeerConnection()
break; break
case "connected": default:
console.log("temp network issue resolved!") console.log(peerConnection.iceConnectionState)
break; break
} }
} }
@ -44,6 +56,7 @@ peerConnection.onicecandidate = event => {
// The server know the stream name from the url // The server know the stream name from the url
// The server replies with its description // The server replies with its description
// After setRemoteDescription, the browser will fire ontrack events // After setRemoteDescription, the browser will fire ontrack events
console.log("Sending session description to server")
fetch(window.location.href, { fetch(window.location.href, {
method: 'POST', method: 'POST',
headers: { headers: {
@ -60,11 +73,13 @@ peerConnection.onicecandidate = event => {
// When video track is received, configure player // When video track is received, configure player
peerConnection.ontrack = function (event) { peerConnection.ontrack = function (event) {
console.log(`New ${event.track.kind} track`)
if (event.track.kind === "video") { if (event.track.kind === "video") {
const viewer = document.getElementById('viewer') const viewer = document.getElementById('viewer')
viewer.srcObject = event.streams[0] 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"> <div class="col-video">
<!-- Video --> <!-- Video -->
<div class="video-responsive"> <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> </div>
<!-- Links under video --> <!-- 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" /> 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> </svg>
<code>rtmps://{{.Cfg.Hostname}}:1935/play/{{.Path}}</code>--> <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> <a href="#" id="sideWidgetToggle" title="Cacher/Afficher le chat">»</a>
</p> </p>
</div> </div>