Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

If WebRTC stream playback is stopped by some reason, it an automatic reconnection can be restored automatically implemented by tweaking player Player example code as follows:

1. Add the variables to configure autonmatic reconnection to the script beginning

Code Block
languagejs
themeRDark
var retryToRestartTimeoutrestart = 3000; //mseval(getUrlParam("restart")) || false;
// Settings to restart playback if failed
var addMilesecondsToRestartTryOnEveryFailedrestartTimeout = 10003000; //ms
var retryMaxTimesrestartMaxTimes = 100; //will try to restart playback for 5 minutes
var retryCountrestartCount = 0;
var isManualStopped = false;
...
function playStreamrestartTimedrId;

In this example, player will try to reconnect to the stream every 3 seconds until 100 tries are expired. Therefore, maximum time to wait for the stream to be restored is 5 minutes, then automatic reconnection is cancelled

2. Add clearRestart() function call to clear reconnection timer if enabled to STREAM_STATUS.PLAYING handler

Code Block
languagejs
themeRDark
function onStarted(stream) {
    ...
    clearRestart();
}

3. Add tryToRestart() function call to enable reconnection timer to SESSION_STATUS.FAILED handler

Code Block
languagejs
themeRDark
function start() {
    var sessionurl = $('#url').val();
    ...
    Flashphoner.getSessions()[0];.createSession({urlServer: url}).on(SESSION_STATUS.ESTABLISHED, function(session){
        ...
    }).on(SESSION_STATUS.DISCONNECTED, function(){
        ...
    }).on(SESSION_STATUS.FAILED, function(){
        setStatus(SESSION_STATUS.FAILED);
        onStopped();
        tryToRestart();
    });
}

4. Add clearRestart() function call to clear reconnection timer if enabled to STREAM_STATUS.STOPPED handler

Code Block
languagejs
themeRDark
function playStream(session) {
    ...
    stream = session.createStream(options).on(STREAM_STATUS.PENDING, function (stream) {
        ...
    }).on(STREAM_STATUS.FAILEDPLAYING, function (stream) {
        ...
    console.log("streamStatus",stream.status());

}).on(STREAM_STATUS.STOPPED, function () {
        $("#preloader").hide();
        setStatus(STREAM_STATUS.FAILEDSTOPPED);
        clearRestart();
        onStopped();
    }).on(STREAM_STATUS.FAILED, function(stream) {
   //try to restart
   ...
     retryToRestart();}).on(STREAM_STATUS.NOT_ENOUGH_BANDWIDTH, function(stream){
        ...
    });
    stream.play();
}

5. Add tryToRestart() function call to enable reconnection timer to STREAM_STATUS.FAILED handler

Code Block
languagejs
themeRDark
function playStream(session) {
    ...
    stream = session.createStream(options).on(STREAM_STATUS.PENDING, function (stream) {
        ...
    }).on(STREAM_STATUS.PLAYING, function (stream) {
        ...
    }).on(STREAM_STATUS.STOPPED, function retryToRestart(){
    if (retryCount < retryMaxTimes) () {
        ...
    }).on(STREAM_STATUS.FAILED, function(stream) {
        $("#preloader").hide();
        setStatus(STREAM_STATUS.FAILED, stream);
        onStopped();
        tryToRestart();
    }).on(STREAM_STATUS.NOT_ENOUGH_BANDWIDTH, function(stream){
        ...
    });
    stream.play();
}

6. Add the function to enable reconnection timer

Code Block
languagejs
themeRDark
function tryToRestart() {
    if (restart) {
        restartTimerId = setTimeout(function(){
            if (stream   && (stream.status() != STREAM_STATUS.PLAYING) && restartCount < restartMaxTimes){
                playStream()$("#playBtn").click();
                restartCount++;
            }
      retryToRestartTimeout = retryToRestartTimeout + addMilesecondsToRestartTryOnEveryFailed;
      if (restartCount >= restartMaxTimes) {
                console.log("Tried to restart playback for "+restartMaxTimes+" times with retryCount"+restartTimeout+" ms interval, cancelled");
            }
        },retryToRestartTimeout)restartTimeout);
    }
}

7. Add the function to clear reconnection timer

Code Block
languagejs
themeRDark
function clearRestart() {
    if (restart) {
        clearTimeout(restartTimerId);
        restartCount = 0;
    }
}

Ready to use samples

Ready to use Player example based on WebSDK 2.0.189 can be downloaded here

View file
nameplayer_restart.tar.gz
height250

Ready to use Embed Player example based on WebSDK 2.0.189 can be downloaded here

View file
nameembed_player_restart.tar.gz
height250