Change how halting AARD is handled

This commit is contained in:
Tamius Han 2021-08-25 23:13:02 +02:00
parent a2573b5aaf
commit 86a8c23999
2 changed files with 65 additions and 59 deletions

View File

@ -291,6 +291,7 @@ class ArDetector {
this.noLetterboxCanvasReset = false;
if (this.settings.canStartAutoAr() ) {
this.main();
this.start();
}
@ -305,7 +306,7 @@ class ArDetector {
destroy(){
this.logger.log('info', 'init', `%c[ArDetect::destroy] <@${this.arid}> Destroying aard.`, _ard_console_stop);
// this.debugCanvas.destroy();
this.stop();
this.halt();
}
//#endregion lifecycle
@ -323,24 +324,30 @@ class ArDetector {
this.conf.resizer.setLastAr({type: AspectRatioType.Automatic, ratio: this.defaultAr});
}
// launch main() if it's currently not running:
this.main();
// automatic detection starts halted. If halted=false when main first starts, extension won't run
// this._paused is undefined the first time we run this function, which is effectively the same thing
// as false. Still, we'll explicitly fix this here.
this._paused = false;
this._halted = false;
this._paused = false;
// start autodetection
if (this.animationFrameHandle) {
window.cancelAnimationFrame(this.animationFrameHandle);
}
this.animationFrameHandle = window.requestAnimationFrame( (ts) => this.animationFrameBootstrap(ts));
// automatic detection starts halted. If halted=false when main first starts, extension won't run
// this._paused is undefined the first time we run this function, which is effectively the same thing
// as false. Still, we'll explicitly fix this here.
}
stop() {
if (this.animationFrameHandle) {
window.cancelAnimationFrame(this.animationFrameHandle);
}
this.logger.log('info', 'debug', `"%c[ArDetect::stop] <@${this.arid}> Stopping AnimationFrame loop.`, _ard_console_stop);
}
unpause() {
// pause only if we were running before. Don't pause if we aren't running
// (we are running when _halted is neither true nor undefined)
if (this._paused && this._halted === false) {
this._paused = true;
}
this.start();
}
pause() {
@ -349,10 +356,12 @@ class ArDetector {
if (this._halted === false) {
this._paused = true;
}
this.stop();
}
stop(){
this.logger.log('info', 'debug', `"%c[ArDetect::stop] <@${this.arid}> Stopping automatic aspect ratio detection`, _ard_console_stop);
halt(){
this.logger.log('info', 'debug', `"%c[ArDetect::stop] <@${this.arid}> Halting automatic aspect ratio detection`, _ard_console_stop);
this.stop();
this._halted = true;
// this.conf.resizer.setArLastAr();
}
@ -368,10 +377,6 @@ class ArDetector {
//#region helper functions (general)
private isDRM() {
return this.video.mediaKeys instanceof MediaKeys;
}
isRunning(){
return ! (this._halted || this._paused || this._exited);
}
@ -412,9 +417,12 @@ class ArDetector {
}
this.status.lastVideoStatus = videoState;
const canTriggerFrameCheck = Date.now() >= this.timers.nextFrameCheckTime;
if (Date.now() < this.timers.nextFrameCheckTime) {
return false;
}
this.timers.nextFrameCheckTime = Date.now() + this.settings.active.arDetect.timers.playing;
return canTriggerFrameCheck;
return true;
}
private scheduleInitRestart(timeout?: number, force_reset?: boolean){
@ -512,9 +520,9 @@ class ArDetector {
//#endregion
async main() {
try {
if (this._paused) {
// unpause if paused
this._paused = false;
this.start();
return; // main loop still keeps executing. Return is needed to avoid a million instances of autodetection
}
if (!this._halted) {
@ -540,11 +548,10 @@ class ArDetector {
this._halted = false;
this._exited = false;
if (this.animationFrameHandle) {
window.cancelAnimationFrame(this.animationFrameHandle)
this.start();
} catch (e) {
this.logger.log('error', 'debug', `[ArDetect::main] <${this.arid} failed to start autodetection for some reason.`, e);
}
this.animationFrameHandle = window.requestAnimationFrame(this.animationFrameBootstrap);
}
/**
@ -555,7 +562,6 @@ class ArDetector {
this.addPerformanceTimeMeasure(this.performance.animationFrame, timestamp - this.performance.animationFrame.lastTime)
this.performance.animationFrame.lastTime = timestamp;
// trigger frame check, if we're allowed to
if ( (!this.manualTickEnabled && this.canTriggerFrameCheck()) || this._nextTick) {
this._nextTick = false;
@ -570,7 +576,7 @@ class ArDetector {
}
if (this && !this._halted && !this._paused) {
this.animationFrameHandle = window.requestAnimationFrame(this.animationFrameBootstrap);
this.animationFrameHandle = window.requestAnimationFrame( (ts) => this.animationFrameBootstrap(ts));
} else if (this._halted) {
this.logger.log('info', 'debug', `%c[ArDetect::main] <@${this.arid}> Main autodetection loop exited. Halted? ${this._halted}`, _ard_console_stop);
this._exited = true;
@ -720,7 +726,7 @@ class ArDetector {
if (! this.canvasReadyForDrawWindow()) {
// this means canvas needs to be resized, so we'll just re-run setup with all those new parameters
this.stop();
this.halt();
let newCanvasWidth = window.innerHeight * (this.video.videoWidth / this.video.videoHeight);
let newCanvasHeight = window.innerHeight;

View File

@ -323,7 +323,7 @@ class VideoData {
this.pause();
this.destroyed = true;
try {
this.arDetector.stop();
this.arDetector.halt();
this.arDetector.destroy();
} catch (e) {}
this.arDetector = undefined;
@ -608,14 +608,14 @@ class VideoData {
stopArDetection() {
if (this.arDetector) {
this.arDetector.stop();
this.arDetector.halt();
}
}
pause(){
this.paused = true;
if(this.arDetector){
this.arDetector.stop();
this.arDetector.halt();
}
if(this.player){
this.player.stop();