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, an automatic reconnection can be implemented by tweaking Player example code as follows:

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

Code Block
languagejs
themeRDark
var restart = eval(getUrlParam("restart")) || false;
// Settings to restart playback if failed
var restartTimeout = 3000; //ms
var restartMaxTimes = 100; //will try to restart playback for 5 minutes
var restartCount = 0;
var restartTimedrId;

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 url = $('#url').val();
    ...
    Flashphoner.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.PLAYING, function (stream) {
        ...
    }).on(STREAM_STATUS.STOPPED, function () {
        $("#preloader").hide();
        setStatus(STREAM_STATUS.STOPPED);
        clearRestart();
        onStopped();
    }).on(STREAM_STATUS.FAILED, function(stream) {
        ...
    }).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 () {
        ...
    }).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){
                $("#playBtn").click();
                restartCount++;
            }
            if (restartCount >= restartMaxTimes) {
                console.log("Tried to restart playback for "+restartMaxTimes+" times with "+restartTimeout+" ms interval, cancelled");
            }
        },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