Skip to content

Commit

Permalink
browser: notify user for unsupported video
Browse files Browse the repository at this point in the history
detect if video is being played by browser using fps,
if fps is near zero that means video is not able to play

Signed-off-by: Pranam Lashkari <[email protected]>
Change-Id: I22bfc836a98ec0eb2caa58b89332b2531d69d18f
  • Loading branch information
lpranam committed Sep 7, 2023
1 parent 8ba14ab commit 88edb49
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions browser/src/layer/vector/SVGGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* L.SVGGroup
*/

/* global _ */

L.SVGGroup = L.Layer.extend({

options: {
Expand Down Expand Up @@ -78,6 +80,62 @@ L.SVGGroup = L.Layer.extend({
var point = this._map.latLngToLayerPoint(this._bounds.getNorthWest());
svgLastChild.setAttribute('x', point.x);
svgLastChild.setAttribute('y', point.y);

var video = svgLastChild.getElementsByTagName('video')[0];
this.checkVideoSupport(video);
},

checkVideoSupport: function(video) {
this.video_ = video;
this.startTime_ = 0;
this.decodedFrames_ = 0;
this._videoInterval = null;
var that = this;

this.video_.addEventListener('playing', function() {
that.onVideoPlaying();
});

this.video_.addEventListener('error', function() {
that.endVideoSupportCheck();
this._map.uiManager.showSnackbar(_('Document contains uncompatible video'));
});

this.video_.addEventListener('ended', function() {
that.endVideoSupportCheck();
});
},

endVideoSupportCheck: function() {
window.clearInterval(this._videoInterval);
},

onVideoPlaying: function() {
this.decodedFrames_ = 0;
this.startTime_ = window.performance.now();
this.endVideoSupportCheck();
var that = this;
this._videoInterval = window.setInterval(function() {
that.calculateFrames();}, 1000);
},

calculateFrames: function() {
if (this.video_.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA ||
this.video_.paused || this.video_.ended)
return;
var currentTime = window.performance.now();
var deltaTime = (currentTime - this.startTime_) / 1000;
this.startTime_ = currentTime;

// Calculate decoded frames per sec.
var fps = (this.video_.webkitDecodedFrameCount - this.decodedFrames_) /
deltaTime;
this.decodedFrames_ = this.video_.webkitDecodedFrameCount;

if (fps < 1) {
this._map.uiManager.showSnackbar(_('Document contains uncompatible video'));
this.endVideoSupportCheck();
}
},

addEmbeddedSVG: function (svgString) {
Expand Down

0 comments on commit 88edb49

Please sign in to comment.