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);
}
}