on(STREAM_STATUS.FAILED, function () {

Dani

Member
Is there a way on this call - to order flashphoner to try to reconnect the stream rather than shut down ?
 

Max

Administrator
Staff member
Good day.
You can modify Player example to automatically restart a stream playback:
Code:
var retryToRestartTimeout = 3000; //ms
var addMilesecondsToRestartTryOnEveryFailed = 1000; //ms
var retryMaxTimes = 100;
var retryCount = 0;
var isManualStopped = false;
...
function playStream() {
    ...
    stream = session.createStream(options).on(STREAM_STATUS.PENDING, function(stream) {
        ...
    }).on(STREAM_STATUS.FAILED, function () {
        console.log("streamStatus",stream.status());

        setStatus(STREAM_STATUS.FAILED);
        onStopped();
        //try to restart
        retryToRestart();
    });
    stream.play();
}
...
function retryToRestart(){
    if (retryCount < retryMaxTimes){
        setTimeout(function(){
            if (stream   && (stream.status() != STREAM_STATUS.PLAYING)){
                playStream();
                retryToRestartTimeout = retryToRestartTimeout + addMilesecondsToRestartTryOnEveryFailed;
                retryCount++;
            }
        },retryToRestartTimeout);
    }
}
 

Dani

Member
here is my code: let me know if it should work:

var retryToRestartTimeout = 3000; //ms
var addMilesecondsToRestartTryOnEveryFailed = 1000; //ms
var retryMaxTimes = 100;
var retryCount = 0;
var isManualStopped = false;
var mystream;

mystream = session.createStream(optionsStream).on(STREAM_STATUS.PUBLISHING, function (pubStream) {
publishStream = pubStream;
didUmedia = true;
llVght ocaideo.style.hei= "" + userVidH.toString() + "px";

setUserMedia(kind, "on");



console.log("PUBLISHING startStreaming");
}).on(STREAM_STATUS.UNPUBLISHED, function () {

console.log("UNPUBLISHED startStreaming: ");
if (didDisconnect == false) {
if (didUmedia == true) {
if (kind == "cam") {


doCamOnError();


}
else { doMicOnError(); }
}
}
}).on(STREAM_STATUS.FAILED, function () {

console.log("FAILED startStreaming: ");
if (didDisconnect == false) {
if (didUmedia == true) {
if (kind == "cam") {

//doCamOnError();
setStatus(STREAM_STATUS.FAILED);
onStopped();
//try to restart
retryToRestart();
}
else { doMicOnError(); }
}
else {
if (kind == "cam") { doCamOffF(); }
else { doMicOffF(); }
}
}
}).publish();
}
}


function retryToRestart(){
if (retryCount < retryMaxTimes) {
setTimeout(function(){
if (mystream && (mystream.status() != STREAM_STATUS.PLAYING)){
playStream();
retryToRestartTimeout = retryToRestartTimeout + addMilesecondsToRestartTryOnEveryFailed;
retryCount++;
}
},retryToRestartTimeout);
}
}
 

Max

Administrator
Staff member
Good day.
Please clarify: are you trying to resume stream playback (as in example we provide above) or stream publishing?
In theory, it should work, but we did not test stream publishing auto resume. In this case, you should use corresponding stream status to check if it is published (STREAM_STATUS.PUBLISHING). Please refer to WebSDK API doc here.
 

Dani

Member
we're publishing live stream (it's a chat app) so both side can send video. Would the code be changed in that case ?
For some reason - and only on iphone, the stream "Stops" after 1 min.
 

Max

Administrator
Staff member
If you have to resume both playback and publishing, you should adopt the example code from this post to handle both cases.
 

Dani

Member
the link you put here is to this post....
I don't think I do playback - both sides publish live stream - is that what you mean ?
The problem is that in iphone the phone side get disconnected after 1 min. I want to restart the camera and publish the stream or better - prevent it from disconnecting.
 

Max

Administrator
Staff member
The problem is that in iphone the phone side get disconnected after 1 min
We discuss this problem in this topic
I want to restart the camera and publish the stream
You should adopt the example from this post to resume stream publishing, i.e. use STREAM_STATUS.PUBLISHING to check the stream status, move createStream() and publish() calls to separate function to call it when stream goes to FAILED status etc.
 

Dani

Member
I am not sure how to adapt - should I replace playStream() function with publishStream() function ?
should I react differently to FAILED status and UNPUBLISHED ?

Updated code:

var retryToRestartTimeout = 3000; //ms
var addMilesecondsToRestartTryOnEveryFailed = 1000; //ms
var retryMaxTimes = 100;
var retryCount = 0;
var isManualStopped = false;
var mystream;

pubStream = session.createStream(optionsStream).on(STREAM_STATUS.PUBLISHING, function (pubStream) {
publishStream = pubStream;
didUmedia = true;
localVideo.style.height = "" + userVidH.toString() + "px";

if (mediaProvid == "Flash") { doMicActive(); }

console.log("startStreaming kind: " + kind);
setUserMedia(kind, "on");
console.log("PUBLISHING startStreaming");
}).on(STREAM_STATUS.UNPUBLISHED, function () {
console.log("UNPUBLISHED startStreaming: ", pubStream.status());
if (didDisconnect == false) {
if (didUmedia == true) {
if (kind == "cam") {
// Chainging to try to reconnect.
alert("unpublished");
onStopped();
//try to restart
retryToRestart();
//doCamOnError();


}
else { doMicOnError(); }
}
}
}).on(STREAM_STATUS.FAILED, function () {
console.log("FAILED startStreaming: (mark) ");
if (didDisconnect == false) {
if (didUmedia == true) {
if (kind == "cam") {

//doCamOnError();
setStatus(STREAM_STATUS.FAILED);
onStopped();
//try to restart
retryToRestart();
}
else { doMicOnError(); }
}
else {
if (kind == "cam") { doCamOffF(); }
else { doMicOffF(); }
}
}
}).publish();
}
}

function retryToRestart(){
if (retryCount < retryMaxTimes) {
setTimeout(function(){
if (pubStream && (pubStream.status() != STREAM_STATUS.PLAYING)){
//playStream();
publishStream();
retryToRestartTimeout = retryToRestartTimeout + addMilesecondsToRestartTryOnEveryFailed;
retryCount++;
}
},retryToRestartTimeout);
}
}
 

Max

Administrator
Staff member
Good day.
We will look at your code and write the answer later.
 

Max

Administrator
Staff member
should I replace playStream() function with publishStream() function ?
You should wrap publishing code to the function publishStreamFunc()
Code:
function publishStreamFunc() {
pubStream = session.createStream(optionsStream).on(STREAM_STATUS.PUBLISHING, function (pubStream) {
...
}).publish();
}
and then call this function from retryToRestart()
Code:
function retryToRestart(){
if (retryCount < retryMaxTimes) {
setTimeout(function(){
if (pubStream && (pubStream.status() != STREAM_STATUS.PLAYING)){
publishStreamFunc();
retryToRestartTimeout = retryToRestartTimeout + addMilesecondsToRestartTryOnEveryFailed;
retryCount++;
}
},retryToRestartTimeout);
}
}
should I react differently to FAILED status and UNPUBLISHED ?
Yes. The UNPUBLISHED status means that the stream was stopped by client. To ensure stream is manually stopped you should set the variable isManualStopped to true when user presses Stop button or something. Then check this variable and do not resume publishing in this case.
 
Top