From 7b73a42e5cfaf8c9cccf7e85b86609480af07b06 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sat, 21 Sep 2019 23:50:06 +0200 Subject: [PATCH] Force fallback mode, always. --- src/ext/lib/video-data/PlayerData.js | 15 +++++++++++ src/ext/lib/video-data/VideoData.js | 38 +++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/ext/lib/video-data/PlayerData.js b/src/ext/lib/video-data/PlayerData.js index ac4ff0b..7a846cb 100644 --- a/src/ext/lib/video-data/PlayerData.js +++ b/src/ext/lib/video-data/PlayerData.js @@ -86,6 +86,7 @@ class PlayerData { if (this.invalid) { return; } + const ths = this; this.observer = new MutationObserver((m,o) => this.onPlayerDimensionsChanged(m,o,ths)); @@ -96,7 +97,21 @@ class PlayerData { }; this.observer.observe(this.element, observerConf); + + // legacy mode still exists, but acts as a fallback for observers and is triggered less + // frequently in order to avoid too many pointless checks + this.legacyChangeDetection(); } + + async legacyChangeDetection() { + while (!this.halted) { + await this.sleep(1000); + if (this.checkPlayerSizeChange()) { + this.videoData.restore(); + } + } + } + stopChangeDetection(){ this.observer.disconnect(); } diff --git a/src/ext/lib/video-data/VideoData.js b/src/ext/lib/video-data/VideoData.js index 48bd69b..7c48360 100644 --- a/src/ext/lib/video-data/VideoData.js +++ b/src/ext/lib/video-data/VideoData.js @@ -36,6 +36,11 @@ class VideoData { return; } + this.dimensions = { + width: this.video.offsetWidth, + height: this.video.offsetHeight, + }; + this.resizer = new Resizer(this); this.arDetector = new ArDetector(this); // this starts Ar detection. needs optional parameter that prevets ardetdctor from starting // player dimensions need to be in: @@ -56,7 +61,7 @@ class VideoData { async fallbackChangeDetection() { while (!this.destroyed && !this.invalid) { - await this.sleep(1000); + await this.sleep(500); this.validateVideoOffsets(); } } @@ -368,6 +373,37 @@ class VideoData { isPlaying() { return this.video && this.video.currentTime > 0 && !this.video.paused && !this.video.ended; } + + checkVideoSizeChange(){ + const videoWidth = this.video.offsetWidth; + const videoHeight = this.video.offsetHeight; + // this 'if' is just here for debugging — real code starts later. It's safe to collapse and + // ignore the contents of this if (unless we need to change how logging works) + if (this.logger.canLog('debug')){ + if(! this.video) { + this.logger.log('info', 'videoDetect', "[VideoDetect] player element isn't defined"); + } + if ( this.video && this.dimensions && + ( this.dimensions.width != videoWidth || + this.dimensions.height != videoHeight ) + ) { + this.logger.log('info', 'debug', "[VideoDetect] player size changed. reason: dimension change. Old dimensions?", this.dimensions.width, this.dimensions.height, "new dimensions:", this.video.offsetWidth, this.video.offsetHeight); + } + } + + // if size doesn't match, update & return true + if (!this.dimensions + || this.dimensions.width != videoWidth + || this.dimensions.height != videoHeight ){ + this.dimensions = { + width: videoWidth, + height: videoHeight, + }; + return true; + } + + return false; + } } export default VideoData;