From ccde079aeeb8bec1e8b129b6c1807acea40ec297 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Tue, 28 Apr 2020 03:05:55 +0200 Subject: [PATCH] If video dimensions are invalid, wait a little and check again before performing the crop calculation --- src/ext/lib/video-transform/Resizer.js | 40 ++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/ext/lib/video-transform/Resizer.js b/src/ext/lib/video-transform/Resizer.js index 849d4cb..c28e9ef 100644 --- a/src/ext/lib/video-transform/Resizer.js +++ b/src/ext/lib/video-transform/Resizer.js @@ -8,6 +8,7 @@ import Stretch from '../../../common/enums/stretch.enum'; import VideoAlignment from '../../../common/enums/video-alignment.enum'; import AspectRatio from '../../../common/enums/aspect-ratio.enum'; import CropModePersistance from '../../../common/enums/crop-mode-persistence.enum'; +import { sleep } from '../Util'; if(Debug.debug) { console.log("Loading: Resizer.js"); @@ -139,7 +140,7 @@ class Resizer { } } - setAr(ar, lastAr) { + async setAr(ar, lastAr) { if (this.destroyed) { return; } @@ -258,15 +259,37 @@ class Resizer { || this.stretcher.mode === Stretch.FixedSource){ var stretchFactors = this.scaler.calculateCrop(ar); + this.logger.log('error', 'debug', `[Resizer::setAr] failed to set AR due to problem with calculating crop. Error:`, stretchFactors && stretchFactors.error); + if(! stretchFactors || stretchFactors.error){ - this.logger.log('error', 'debug', `[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(); + return; } - if (stretchFactors.error === 'illegal_video_dimensions') { - this.logger.log('error', 'debug', `[Resizer::setAr] Illegal video dimensions found. We will pause everything.`) + + // we could have issued calculate crop too early. Instead of spending 30 minutes trying to fix this the proper way by + // reading documentation, let's fix it in 30 seconds with some brute force code + if (stretchFactors?.error === 'illegal_video_dimensions') { + let timeout = 10; // ms + let iteration = 0; + let maxIterations = 6; + do { + if (iteration > maxIterations) { + this.logger.log('error', 'debug', `[Resizer::setAr] Video dimensions remain illegal after ${maxIterations} retries`); + return; + } + // fire first few rechecks in quick succession, but start increasing timeout + // later down the line. + if (iteration > 3) { + timeout *= 2; + } + this.logger.log('info', 'debug', `[Resizer::setAr] Sleeping for ${timeout} ms`); + await sleep(timeout); + stretchFactors = this.scaler.calculateCrop(ar); + iteration++; + } while (stretchFactors.error === 'illegal_video_dimensions'); + this.logger.log('info', 'debug', `[Resizer::setAr] Video dimensions have corrected themselves after retrying.`); } - return; } if (this.stretcher.mode === Stretch.Conditional){ @@ -589,12 +612,17 @@ class Resizer { // inject new CSS or replace existing one if (!this.userCss) { + this.logger.log('info', ['debug', 'resizer'], "[Resizer::setStyleString] Setting new css: ", newCssString); + this.injectCss(newCssString); this.userCss = newCssString; } else if (newCssString !== this.userCss) { + this.logger.log('info', ['debug', 'resizer'], "[Resizer::setStyleString] Replacing css.\nOld string:", this.userCss, "\nNew string:", newCssString); // we only replace css if it this.replaceCss(this.userCss, newCssString); this.userCss = newCssString; + } else { + this.logger.log('info', ['debug', 'resizer'], "[Resizer::setStyleString] Existing css is still valid, doing nothing."); } } }