After a couple of hours our livestreams automatically disconnect (don’t now why)
To automatically reload them I use a modified version of the embedded player. Sometimes it works but mostly I get the error “Invalid session state”
Does anyone know where I made an error? It would also help if I can check the session state before it throws an error.
Thanks!
To automatically reload them I use a modified version of the embedded player. Sometimes it works but mostly I get the error “Invalid session state”
Does anyone know where I made an error? It would also help if I can check the session state before it throws an error.
Thanks!
JavaScript:
var SESSION_STATUS = Flashphoner.constants.SESSION_STATUS;
var STREAM_STATUS = Flashphoner.constants.STREAM_STATUS;
var session;
var restartTimeout = 5000; //ms
var restartMaxTimes = 60; //will try to restart playback for 5 minutes
var restartCount = 0;
var restartTimedrId;
function init_api() {
Flashphoner.init({});
//Connect to WCS server over websockets
session = Flashphoner.createSession({
urlServer: "wss://IP" //specify the address of your WCS
}).on(SESSION_STATUS.ESTABLISHED, function(session) {
console.log("ESTABLISHED");
$("#playBtn").click();
});
//if(session){ alert(JSON.stringify(session));}
playBtn.onclick = playStream;
}
function playStream() {
constraints = {
audio: false,
video: true
};
var options = {
name: URL,
display: document.getElementById("play"),
constraints: constraints,
};
stream = session.createStream(options).on(STREAM_STATUS.PLAYING, function(stream) {
console.log("playing");
}).on(STREAM_STATUS.FAILED, function(stream) {
console.log("try to restart");
tryToRestart();
});
stream.play();
}
function tryToRestart() {
restartTimerId = setTimeout(function(){
if (stream && (stream.status() != STREAM_STATUS.PLAYING) && restartCount < restartMaxTimes){
$("#playBtn").click();
restartCount++;
}
if (restartCount >= restartMaxTimes) {
console.log("Tried to restart playback for "+restartMaxTimes+" times with "+restartTimeout+" ms interval, cancelled");
}
},restartTimeout);
}