From 2f5acd9d9e8b1f24dbb0448ccd18fc8a1e7540af Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Tue, 7 May 2019 23:40:13 +0200 Subject: [PATCH] Fixed multiple instances running on Twitch/clips.twitch. Fixed lockups on reddit/embedded. Fixed aspect ratio calculations on fitw and fith modes --- src/ext/conf/Debug.js | 8 ++--- src/ext/lib/video-data/PageInfo.js | 20 +++++++++--- src/ext/lib/video-transform/Resizer.js | 45 ++++++++++++++++++++------ src/ext/lib/video-transform/Scaler.js | 15 +++++++-- 4 files changed, 68 insertions(+), 20 deletions(-) diff --git a/src/ext/conf/Debug.js b/src/ext/conf/Debug.js index 44db7e9..00739d9 100644 --- a/src/ext/conf/Debug.js +++ b/src/ext/conf/Debug.js @@ -7,7 +7,7 @@ var Debug = { init: true, debug: true, // debug: false, - keyboard: true, + // keyboard: true, debugResizer: true, debugArDetect: true, scaler: true, @@ -18,9 +18,9 @@ var Debug = { // showArDetectCanvas: true, // flushStoredSettings: true, // flushStoredSettings: false, - // playerDetectDebug: true, - // periodic: true, - // videoRescan: true, + playerDetectDebug: true, + periodic: true, + videoRescan: true, // mousemove: true, arDetect: { // edgeDetect: true diff --git a/src/ext/lib/video-data/PageInfo.js b/src/ext/lib/video-data/PageInfo.js index 370ed92..65978c9 100644 --- a/src/ext/lib/video-data/PageInfo.js +++ b/src/ext/lib/video-data/PageInfo.js @@ -113,12 +113,11 @@ class PageInfo { if (videoExists) { continue; } else { - if(Debug.debug && Debug.periodic && Debug.videoRescan){ + if (Debug.debug && Debug.periodic && Debug.videoRescan) { console.log("[PageInfo::rescan] found new video candidate:", video, "NOTE:: Video initialization starts here:\n--------------------------------\n") } v = new VideoData(video, this.settings, this); // console.log("[PageInfo::rescan] v is:", v) - // debugger; v.initArDetection(); this.videos.push(v); @@ -148,8 +147,21 @@ class PageInfo { } } - }catch(e){ - console.log("rescan error:",e) + } catch(e) { + // če pride do zajeba, potem lahko domnevamo da na strani ni nobenega videa. Uničimo vse objekte videoData + // da preprečimo večkratno inicializacijo. Če smo se z našim ugibom zmotili, potem se bodo vsi videi ponovno + // našli ob naslednjem preiskovanju + // + // if we encounter a fuckup, we can assume that no videos were found on the page. We destroy all videoData + // objects to prevent multiple initalization (which happened, but I don't know why). No biggie if we destroyed + // videoData objects in error — they'll be back in the next rescan + if (Debug.debug) { + console.log("rescan error: — destroying all videoData objects",e); + } + for (const v of this.videos) { + v.destroy(); + } + return; } if(rescanReason == RescanReason.PERIODIC){ diff --git a/src/ext/lib/video-transform/Resizer.js b/src/ext/lib/video-transform/Resizer.js index d47550f..2824f94 100644 --- a/src/ext/lib/video-transform/Resizer.js +++ b/src/ext/lib/video-transform/Resizer.js @@ -24,6 +24,8 @@ class Resizer { this.stretcher = new Stretcher(this.conf); this.zoom = new Zoom(this.conf); + this.cssCheckDisabled = false; + // load up default values this.correctedVideoDimensions = {}; this.currentCss = {}; @@ -74,7 +76,7 @@ class Resizer { calculateRatioForLegacyOptions(ar){ // also present as modeToAr in Scaler.js - if (ar.ratio) { + if (ar.type !== AspectRatio.FitWidth && ar.type !== AspectRatio.FitHeight && ar.ratio) { return ar; } // Skrbi za "stare" možnosti, kot na primer "na širino zaslona", "na višino zaslona" in "ponastavi". @@ -94,6 +96,9 @@ class Resizer { if (! this.conf.player.dimensions) { ratioOut = screen.width / screen.height; } else { + if (Debug.debug && Debug.debugResizer) { + console.log(`[Resizer::calculateRatioForLegacyOptions] Player dimensions:`, this.conf.player.dimensions.width ,'x', this.conf.player.dimensions.height,'aspect ratio:', this.conf.player.dimensions.width / this.conf.player.dimensions.height) + } ratioOut = this.conf.player.dimensions.width / this.conf.player.dimensions.height; } @@ -138,7 +143,8 @@ class Resizer { } if (lastAr) { - this.lastAr = lastAr; + this.lastAr = this.calculateRatioForLegacyOptions(lastAr); + ar = this.calculateRatioForLegacyOptions(ar); } else { // NOTE: "fitw" "fith" and "reset" should ignore ar.ratio bit, but // I'm not sure whether they do. Check that. @@ -190,10 +196,20 @@ class Resizer { if(Debug.debug){ console.log("[Resizer::setAr] failed to set AR due to problem with calculating crop. Error:", (stretchFactors ? stretchFactors.error : stretchFactors)); } - if(stretchFactors.error === 'no_video'){ + if (stretchFactors.error === 'no_video'){ this.conf.destroy(); } + if (stretchFactors.error === 'illegal_video_dimensions') { + if(Debug.debug){ + console.log("[Resizer::setAr] Illegal video dimensions found. We will pause everything."); + } + // if we get illegal video dimensions, cssWatcher goes nuts. This is harmful, + // so we stop it until that sorts itself out + this.stopCssWatcher(); + } return; + } else { + this.startCssWatcher(); } if(this.stretcher.mode === Stretch.Conditional){ this.stretcher.applyConditionalStretch(stretchFactors, ar.ratio); @@ -301,7 +317,8 @@ class Resizer { return; } - // this.haltCssWatcher = false; + this.cssCheckDisabled = false; + if(!this.cssWatcherTimer){ this.scheduleCssWatcher(1); } else { @@ -320,6 +337,11 @@ class Resizer { return; } + // one extra check to ensure we don't run css checks when we aren't supposed to + if (this.cssCheckDisabled) { + return; + } + if(this.cssWatcherTimeout) { clearTimeout(this.cssWatcherTimer); } @@ -328,7 +350,9 @@ class Resizer { this.cssWatcherTimer = setTimeout(function () { ths.cssWatcherTimer = null; try { - ths.cssCheck(); + if (! ths.cssCheckDisabled) { + ths.cssCheck(); + } } catch (e) { if(Debug.debug) { console.log("[Resizer.js::scheduleCssWatcher] Css check failed. Error:", e); @@ -339,9 +363,9 @@ class Resizer { stopCssWatcher() { if (Debug.debug) { - console.log("[Resizer.js] STOPPING CSS WATCHER!") + console.log(`[Resizer.js] <${this.resizerId}> STOPPING CSS WATCHER!`) } - + this.cssCheckDisabled = true; clearInterval(this.cssWatcherTimeout); } @@ -551,6 +575,10 @@ class Resizer { } cssCheck(){ + if (this.cssCheckDisabled) { + throw "fucking dont" + return; + } // this means we haven't set our CSS yet, or that we changed video. // if(! this.currentCss.tranform) { // this.scheduleCssWatcher(200); @@ -584,11 +612,10 @@ class Resizer { } this.restore(); this.scheduleCssWatcher(10); - return; } if (this.cssWatcherIncreasedFrequencyCounter > 0) { --this.cssWatcherIncreasedFrequencyCounter; - this.scheduleCssWatcher(20); + this.scheduleCssWatcher(20); } else { this.scheduleCssWatcher(1000); } diff --git a/src/ext/lib/video-transform/Scaler.js b/src/ext/lib/video-transform/Scaler.js index b3b4739..85a7d59 100644 --- a/src/ext/lib/video-transform/Scaler.js +++ b/src/ext/lib/video-transform/Scaler.js @@ -18,7 +18,7 @@ class Scaler { // Približevanje opuščeno. // handles "legacy" options, such as 'fit to widht', 'fit to height' and AspectRatio.Reset. No zoom tho modeToAr (ar) { - if (ar.ratio) { + if (ar.type !== AspectRatio.FitWidth && ar.type !== AspectRatio.FitHeight && ar.ratio) { return ar.ratio; } @@ -70,14 +70,23 @@ class Scaler { } calculateCrop(ar) { - if(!this.conf.video || this.conf.video.videoWidth == 0 || this.conf.video.videoHeight == 0){ + if(!this.conf.video){ if (Debug.debug) { - console.log("[Scaler::calculateCrop] ERROR — no video detected."); + console.log("[Scaler::calculateCrop] ERROR — no video detected. Conf:", this.conf, "video:", this.conf.video, "video dimensions:", this.conf.video && this.conf.video.videoWidth, '×', this.conf.video && this.conf.video.videoHeight); } this.conf.destroy(); return {error: "no_video"}; } + if (this.conf.video.videoWidth == 0 || this.conf.video.videoHeight == 0) { + // that's illegal, but not illegal enough to just blast our shit to high hell + // mr officer will let you go with a warning this time around + if (Debug.debug) { + console.log("[Scaler::calculateCrop] Video has illegal dimensions. Video dimensions:", this.conf.video && this.conf.video.videoWidth, '×', this.conf.video && this.conf.video.videoHeight); + } + + return {error: "illegal_video_dimensions"}; + } if (ar.type === AspectRatio.Reset){ return {xFactor: 1, yFactor: 1}