From 88edb4992439e127e43cd2e95db24921f5781130 Mon Sep 17 00:00:00 2001 From: Pranam Lashkari Date: Fri, 8 Sep 2023 05:44:13 +0800 Subject: [PATCH] browser: notify user for unsupported video 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 Change-Id: I22bfc836a98ec0eb2caa58b89332b2531d69d18f --- browser/src/layer/vector/SVGGroup.js | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/browser/src/layer/vector/SVGGroup.js b/browser/src/layer/vector/SVGGroup.js index f347f95940af7..0c80baefea7f9 100644 --- a/browser/src/layer/vector/SVGGroup.js +++ b/browser/src/layer/vector/SVGGroup.js @@ -3,6 +3,8 @@ * L.SVGGroup */ +/* global _ */ + L.SVGGroup = L.Layer.extend({ options: { @@ -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) {