Embed: webrtc: fix for .play() racecondition causing repeated reconnects

This commit is contained in:
Cat 2019-09-09 16:19:19 +02:00
parent 0f7ea57ea1
commit 1ff69ab56d
2 changed files with 32 additions and 11 deletions

File diff suppressed because one or more lines are too long

View file

@ -68,6 +68,7 @@ p.prototype.build = function (MistVideo,callback) {
var seekoffset = 0; var seekoffset = 0;
var hasended = false; var hasended = false;
var currenttracks = [];
this.listeners = { this.listeners = {
on_connected: function() { on_connected: function() {
seekoffset = 0; seekoffset = 0;
@ -102,6 +103,10 @@ p.prototype.build = function (MistVideo,callback) {
seekoffset = ev.current*1e-3 - video.currentTime; seekoffset = ev.current*1e-3 - video.currentTime;
if (Math.abs(oldoffset - seekoffset) > 1) { correctSubtitleSync(); } if (Math.abs(oldoffset - seekoffset) > 1) { correctSubtitleSync(); }
if ((!("paused" in ev) || (!ev.paused)) && (video.paused)) {
video.play();
}
var d = (ev.end == 0 ? Infinity : ev.end*1e-3); var d = (ev.end == 0 ? Infinity : ev.end*1e-3);
if (d != duration) { if (d != duration) {
duration = d; duration = d;
@ -174,6 +179,7 @@ p.prototype.build = function (MistVideo,callback) {
this.peerConn = null; this.peerConn = null;
this.localOffer = null; this.localOffer = null;
this.isConnected = false; this.isConnected = false;
this.isConnecting = false;
this.play_rate = "auto"; this.play_rate = "auto";
var thisWebRTCPlayer = this; var thisWebRTCPlayer = this;
@ -182,6 +188,7 @@ p.prototype.build = function (MistVideo,callback) {
switch (ev.type) { switch (ev.type) {
case "on_connected": { case "on_connected": {
thisWebRTCPlayer.isConnected = true; thisWebRTCPlayer.isConnected = true;
thisWebRTCPlayer.isConnecting = false;
break; break;
} }
case "on_answer_sdp": { case "on_answer_sdp": {
@ -205,6 +212,7 @@ p.prototype.build = function (MistVideo,callback) {
}; };
this.connect = function(callback){ this.connect = function(callback){
thisWebRTCPlayer.isConnecting = true;
thisWebRTCPlayer.signaling = new WebRTCSignaling(thisWebRTCPlayer.on_event); thisWebRTCPlayer.signaling = new WebRTCSignaling(thisWebRTCPlayer.on_event);
thisWebRTCPlayer.peerConn = new RTCPeerConnection(); thisWebRTCPlayer.peerConn = new RTCPeerConnection();
thisWebRTCPlayer.peerConn.ontrack = function(ev) { thisWebRTCPlayer.peerConn.ontrack = function(ev) {
@ -240,7 +248,7 @@ p.prototype.build = function (MistVideo,callback) {
this.seek = function(seekTime){ this.seek = function(seekTime){
var p = new Promise(function(resolve,reject){ var p = new Promise(function(resolve,reject){
if (!thisWebRTCPlayer.isConnected || !thisWebRTCPlayer.signaling) { return reject("Failed seek: not connected"); } if (!thisWebRTCPlayer.isConnected || !thisWebRTCPlayer.signaling) { return reject("Failed seek: not connected"); }
thisWebRTCPlayer.signaling.send({type: "seek", "seek_time": seekTime*1e3}); thisWebRTCPlayer.signaling.send({type: "seek", "seek_time": (seekTime == "live" ? "live" : seekTime*1e3)});
if ("seekPromise" in thisWebRTCPlayer.signaling) { if ("seekPromise" in thisWebRTCPlayer.signaling) {
thisWebRTCPlayer.signaling.seekPromise.reject("Doing new seek"); thisWebRTCPlayer.signaling.seekPromise.reject("Doing new seek");
} }
@ -348,7 +356,6 @@ p.prototype.build = function (MistVideo,callback) {
//override video duration //override video duration
var duration; var duration;
var currenttracks = [];
Object.defineProperty(this.api,"duration",{ Object.defineProperty(this.api,"duration",{
get: function(){ return duration; } get: function(){ return duration; }
}); });
@ -416,11 +423,20 @@ p.prototype.build = function (MistVideo,callback) {
//redirect play //redirect play
me.api.play = function(){ me.api.play = function(){
var seekto;
if (me.api.currentTime) { if (me.api.currentTime) {
seekto = me.api.currentTime;
}
if ((MistVideo.info) && (MistVideo.info.type == "live")) {
seekto = "live";
}
if (seekto) {
var p = new Promise(function(resolve,reject){ var p = new Promise(function(resolve,reject){
if ((!me.webrtc.isConnected) || (me.webrtc.peerConn.iceConnectionState != "completed")) { if ((!me.webrtc.isConnected) && (me.webrtc.peerConn.iceConnectionState != "completed")) {
if (!me.webrtc.isConnecting) {
MistVideo.log("Received call to play while not connected, connecting "+me.webrtc.peerConn.iceConnectionState);
me.webrtc.connect(function(){ me.webrtc.connect(function(){
me.webrtc.seek(me.api.currentTime).then(function(msg){ me.webrtc.seek(seekto).then(function(msg){
resolve("played "+msg); resolve("played "+msg);
}).catch(function(msg){ }).catch(function(msg){
reject(msg); reject(msg);
@ -428,13 +444,18 @@ p.prototype.build = function (MistVideo,callback) {
}); });
} }
else { else {
me.webrtc.seek(me.api.currentTime).then(function(msg){ reject("Still connecting");
}
}
else {
me.webrtc.seek(seekto).then(function(msg){
resolve("played "+msg); resolve("played "+msg);
}).catch(function(msg){ }).catch(function(msg){
reject(msg); reject(msg);
}); });
} }
}); });
return p; return p;
} }
else { else {