Simplify video rescans

This commit is contained in:
Tamius Han 2021-09-14 23:21:53 +02:00
parent b1ec4f7387
commit 415ebf6821
3 changed files with 103 additions and 105 deletions

View File

@ -1,7 +1,5 @@
{ {
"cSpell.words": [ "cSpell.words": [
"PILLARBOX",
"PILLARBOXED",
"aard", "aard",
"ardetector", "ardetector",
"autodetect", "autodetect",
@ -16,6 +14,7 @@
"csui", "csui",
"decycle", "decycle",
"disneyplus", "disneyplus",
"endregion",
"equalish", "equalish",
"fith", "fith",
"fitw", "fitw",
@ -24,6 +23,7 @@
"gmail", "gmail",
"guardline", "guardline",
"han", "han",
"haram",
"iframe", "iframe",
"imgur", "imgur",
"insta", "insta",
@ -34,6 +34,8 @@
"nogrow", "nogrow",
"noshrink", "noshrink",
"outro", "outro",
"PILLARBOX",
"PILLARBOXED",
"polyfill", "polyfill",
"recursing", "recursing",
"reddit", "reddit",

View File

@ -833,8 +833,6 @@ class ArDetector {
try{ try{
if(guardLineOut.blackbarFail || guardLineOut.imageFail){ if(guardLineOut.blackbarFail || guardLineOut.imageFail){
if(this.edgeDetector.findBars(imageData, null, EdgeDetectPrimaryDirection.Horizontal).status === 'ar_known'){ if(this.edgeDetector.findBars(imageData, null, EdgeDetectPrimaryDirection.Horizontal).status === 'ar_known'){
if(guardLineOut.blackbarFail){ if(guardLineOut.blackbarFail){
this.logger.log('info', 'arDetect', `[ArDetect::frameCheck] Detected blackbar violation and pillarbox. Resetting to default aspect ratio.`); this.logger.log('info', 'arDetect', `[ArDetect::frameCheck] Detected blackbar violation and pillarbox. Resetting to default aspect ratio.`);
this.conf.resizer.setAr({type: AspectRatioType.Automatic, ratio: this.defaultAr}); this.conf.resizer.setAr({type: AspectRatioType.Automatic, ratio: this.defaultAr});

View File

@ -29,7 +29,7 @@ class PageInfo {
logger: Logger; logger: Logger;
settings: Settings; settings: Settings;
comms: CommsClient; comms: CommsClient;
videos: VideoData[] = []; videos: {videoData: VideoData, element: HTMLVideoElement}[] = [];
//#endregion //#endregion
//#region misc stuff //#region misc stuff
@ -113,8 +113,8 @@ class PageInfo {
} }
for (let video of this.videos) { for (let video of this.videos) {
try { try {
(this.comms.unregisterVideo as any)(video.vdid) (this.comms.unregisterVideo as any)(video.videoData.vdid)
video.destroy(); video.videoData.destroy();
} catch (e) { } catch (e) {
this.logger.log('error', ['debug', 'init'], '[PageInfo::destroy] unable to destroy video! Error:', e); this.logger.log('error', ['debug', 'init'], '[PageInfo::destroy] unable to destroy video! Error:', e);
} }
@ -135,8 +135,10 @@ class PageInfo {
reset() { reset() {
for(let video of this.videos) { for(let video of this.videos) {
video.destroy(); video.videoData.destroy();
video.videoData = null;
} }
this.videos = [];
this.rescan(RescanReason.MANUAL); this.rescan(RescanReason.MANUAL);
} }
@ -159,7 +161,7 @@ class PageInfo {
getVideos(host) { getVideos(host) {
if (this.settings.active.sites[host]?.DOM?.video?.manual if (this.settings.active.sites[host]?.DOM?.video?.manual
&& this.settings.active.sites[host]?.DOM?.video?.querySelectors){ && this.settings.active.sites[host]?.DOM?.video?.querySelectors){
const videos = document.querySelectorAll(this.settings.active.sites[host].DOM.video.querySelectors); const videos = document.querySelectorAll(this.settings.active.sites[host].DOM.video.querySelectors) as NodeListOf<HTMLVideoElement>;
if (videos.length) { if (videos.length) {
return videos; return videos;
@ -172,9 +174,25 @@ class PageInfo {
return this.readOnly ? this.hasVideos : this.videos.length; return this.readOnly ? this.hasVideos : this.videos.length;
} }
rescan(rescanReason){ /**
const oldVideoCount = this.videos.length; * Re-scans the page for videos. Removes any videos that no longer exist from our list
* of videos. Destroys all videoData objects for all the videos that don't have their
* own <video> html element on the page.
* @param rescanReason Why was the rescan triggered. Mostly used for logging.
* @returns
*/
rescan(rescanReason?: RescanReason){
// is there any video data objects that had their HTML elements removed but not yet
// destroyed? We clean that up here.
const orphans = this.videos.filter(x => !document.body.contains(x.element));
for (const orphan of orphans) {
orphan.videoData.destroy();
}
// remove all destroyed videos.
this.videos = this.videos.filter(x => !x.videoData.destroyed);
// add new videos
try{ try{
let vids = this.getVideos(window.location.hostname); let vids = this.getVideos(window.location.hostname);
@ -191,64 +209,48 @@ class PageInfo {
// add new videos // add new videos
this.hasVideos = false; this.hasVideos = false;
let videoExists = false; let videoExists = false;
let video, v;
for (video of vids) { for (const videoElement of vids) {
// če najdemo samo en video z višino in širino, to pomeni, da imamo na strani veljavne videe // do not re-add videos that we already track:
// če trenutni video nima definiranih teh vrednostih, preskočimo vse nadaljnja preverjanja if (this.videos.find(x => x.element.isEqualNode(videoElement))) {
// <===[:::::::]===> continue;
}
// if we find even a single video with width and height, that means the page has valid videos // if we find even a single video with width and height, that means the page has valid videos
// if video lacks either of the two properties, we skip all further checks cos pointless // if video lacks either of the two properties, we skip all further checks cos pointless
if(video.offsetWidth && video.offsetHeight){ if(!videoElement.offsetWidth || !videoElement.offsetHeight) {
this.hasVideos = true;
if (this.readOnly) {
// in lite mode, we're done. This is all the info we want, but we want to actually start doing
// things that interfere with the website. We still want to be running a rescan, tho.
if(rescanReason == RescanReason.PERIODIC){
this.scheduleRescan(RescanReason.PERIODIC);
}
return;
}
} else {
continue; continue;
} }
videoExists = false; // at this point, we're certain that we found new videos. Let's update some properties:
this.hasVideos = true;
for (v of this.videos) { // if PageInfo is marked as "readOnly", we actually aren't adding any videos to anything because
if (v.destroyed) { // that's super haram. We're only interested in whether
continue; //TODO: if destroyed video is same as current video, copy aspect ratio settings to current video if (this.readOnly) {
} // in lite mode, we're done. This is all the info we want, but we want to actually start doing
// things that interfere with the website. We still want to be running a rescan, tho.
if (v.video == video) { if(rescanReason == RescanReason.PERIODIC){
videoExists = true; this.scheduleRescan(RescanReason.PERIODIC);
break;
} }
return;
} }
if (videoExists) { this.logger.log('info', 'videoRescan', "[PageInfo::rescan] found new video candidate:", videoElement, "NOTE:: Video initialization starts here:\n--------------------------------\n")
continue;
} else {
this.logger.log('info', 'videoRescan', "[PageInfo::rescan] found new video candidate:", video, "NOTE:: Video initialization starts here:\n--------------------------------\n")
try { try {
v = new VideoData(video, this.settings, this); const newVideo = new VideoData(videoElement, this.settings, this);
this.videos.push(v); this.videos.push({videoData: newVideo, element: videoElement});
} catch (e) { } catch (e) {
this.logger.log('error', 'debug', "rescan error: failed to initialize videoData. Skipping this video.",e); this.logger.log('error', 'debug', "rescan error: failed to initialize videoData. Skipping this video.",e);
}
this.logger.log('info', 'videoRescan', "END VIDEO INITIALIZATION\n\n\n-------------------------------------\nvideos[] is now this:", this.videos,"\n\n\n\n\n\n\n\n")
} }
this.logger.log('info', 'videoRescan', "END VIDEO INITIALIZATION\n\n\n-------------------------------------\nvideos[] is now this:", this.videos,"\n\n\n\n\n\n\n\n")
} }
this.removeDestroyed(); this.removeDestroyed();
// če smo ostali brez videev, potem odregistriraj stran.
// če nismo ostali brez videev, potem registriraj stran.
//
// if we're left without videos on the current page, we unregister the page. // if we're left without videos on the current page, we unregister the page.
// if we have videos, we call register. // if we have videos, we call register.
if (this.comms) { if (this.comms) {
@ -280,27 +282,23 @@ class PageInfo {
} }
} catch(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 // if we encounter a fuckup, we can assume that no videos were found on the page. We destroy all videoData
// objects to prevent multiple initialization (which happened, but I don't know why). No biggie if we destroyed // objects to prevent multiple initialization (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 // videoData objects in error — they'll be back in the next rescan
this.logger.log('error', 'debug', "rescan error: — destroying all videoData objects",e); this.logger.log('error', 'debug', "rescan error: — destroying all videoData objects",e);
for (const v of this.videos) { for (const v of this.videos) {
v.destroy(); v.videoData.destroy();
} }
this.videos = [];
return; return;
} }
if(rescanReason == RescanReason.PERIODIC){ if(rescanReason == RescanReason.PERIODIC){
this.scheduleRescan(RescanReason.PERIODIC); this.scheduleRescan(RescanReason.PERIODIC);
} }
} }
removeDestroyed(){ removeDestroyed(){
this.videos = this.videos.filter( vid => vid.destroyed === false); this.videos = this.videos.filter( vid => vid.videoData.destroyed === false);
} }
scheduleRescan(rescanReason){ scheduleRescan(rescanReason){
@ -358,14 +356,14 @@ class PageInfo {
initArDetection(playingOnly){ initArDetection(playingOnly){
if (playingOnly) { if (playingOnly) {
for(let vd of this.videos){ for(let vd of this.videos){
if(vd.isPlaying()) { if(vd.videoData.isPlaying()) {
vd.initArDetection(); vd.videoData.initArDetection();
} }
} }
return; return;
} else { } else {
for(let vd of this.videos){ for(let vd of this.videos){
vd.initArDetection(); vd.videoData.initArDetection();
} }
} }
} }
@ -376,13 +374,13 @@ class PageInfo {
pauseProcessing(playingOnly){ pauseProcessing(playingOnly){
if (playingOnly) { if (playingOnly) {
for(let vd of this.videos){ for(let vd of this.videos){
if (vd.isPlaying()) { if (vd.videoData.isPlaying()) {
vd.pause(); vd.videoData.pause();
} }
} }
} else { } else {
for(let vd of this.videos){ for(let vd of this.videos){
vd.pause(); vd.videoData.pause();
} }
} }
} }
@ -390,18 +388,18 @@ class PageInfo {
resumeProcessing(resumeAutoar = false, playingOnly = false){ resumeProcessing(resumeAutoar = false, playingOnly = false){
if (playingOnly) { if (playingOnly) {
for(let vd of this.videos){ for(let vd of this.videos){
if (vd.isPlaying()) { if (vd.videoData.isPlaying()) {
vd.resume(); vd.videoData.resume();
if(resumeAutoar){ if(resumeAutoar){
vd.resumeAutoAr(); vd.videoData.resumeAutoAr();
} }
} }
} }
} else { } else {
for(let vd of this.videos){ for(let vd of this.videos){
vd.resume(); vd.videoData.resume();
if(resumeAutoar){ if(resumeAutoar){
vd.resumeAutoAr(); vd.videoData.resumeAutoAr();
} }
} }
} }
@ -414,13 +412,13 @@ class PageInfo {
} }
if (playingOnly) { if (playingOnly) {
for(let vd of this.videos){ for(let vd of this.videos){
if (vd.isPlaying()) { if (vd.videoData.isPlaying()) {
vd.startArDetection(); vd.videoData.startArDetection();
} }
} }
} else { } else {
for(let vd of this.videos){ for(let vd of this.videos){
vd.startArDetection(); vd.videoData.startArDetection();
} }
} }
} }
@ -428,13 +426,13 @@ class PageInfo {
stopArDetection(playingOnly){ stopArDetection(playingOnly){
if (playingOnly) { if (playingOnly) {
for(let vd of this.videos){ for(let vd of this.videos){
if (vd.isPlaying()) { if (vd.videoData.isPlaying()) {
vd.stopArDetection(); vd.videoData.stopArDetection();
} }
} }
} else { } else {
for(let vd of this.videos){ for(let vd of this.videos){
vd.stopArDetection(); vd.videoData.stopArDetection();
} }
} }
} }
@ -449,8 +447,8 @@ class PageInfo {
try { try {
for (let vd of this.videos) { for (let vd of this.videos) {
if (!playingOnly || vd.isPlaying()) { if (!playingOnly || vd.videoData.isPlaying()) {
vd.resetLastAr(); vd.videoData.resetLastAr();
} }
} }
} catch (e) { } catch (e) {
@ -464,14 +462,14 @@ class PageInfo {
// TODO: find a way to only change aspect ratio for one video // TODO: find a way to only change aspect ratio for one video
if (ar === AspectRatioType.Reset) { if (ar === AspectRatioType.Reset) {
for (let vd of this.videos) { for (let vd of this.videos) {
if (!playingOnly || vd.isPlaying()) { if (!playingOnly || vd.videoData.isPlaying()) {
vd.resetAr(); vd.videoData.resetAr();
} }
} }
} else { } else {
for (let vd of this.videos) { for (let vd of this.videos) {
if (!playingOnly || vd.isPlaying()) { if (!playingOnly || vd.videoData.isPlaying()) {
vd.setAr(ar) vd.videoData.setAr(ar)
} }
} }
} }
@ -480,13 +478,13 @@ class PageInfo {
setVideoAlignment(videoAlignment, playingOnly) { setVideoAlignment(videoAlignment, playingOnly) {
if (playingOnly) { if (playingOnly) {
for(let vd of this.videos) { for(let vd of this.videos) {
if (vd.isPlaying()) { if (vd.videoData.isPlaying()) {
vd.setVideoAlignment(videoAlignment) vd.videoData.setVideoAlignment(videoAlignment)
} }
} }
} else { } else {
for(let vd of this.videos) { for(let vd of this.videos) {
vd.setVideoAlignment(videoAlignment) vd.videoData.setVideoAlignment(videoAlignment)
} }
} }
} }
@ -494,13 +492,13 @@ class PageInfo {
setPanMode(mode, playingOnly?: boolean) { setPanMode(mode, playingOnly?: boolean) {
if (playingOnly) { if (playingOnly) {
for(let vd of this.videos) { for(let vd of this.videos) {
if (vd.isPlaying()) { if (vd.videoData.isPlaying()) {
vd.setPanMode(mode); vd.videoData.setPanMode(mode);
} }
} }
} else { } else {
for(let vd of this.videos) { for(let vd of this.videos) {
vd.setPanMode(mode); vd.videoData.setPanMode(mode);
} }
} }
} }
@ -508,13 +506,13 @@ class PageInfo {
restoreAr(playingOnly?: boolean) { restoreAr(playingOnly?: boolean) {
if (playingOnly) { if (playingOnly) {
for(let vd of this.videos){ for(let vd of this.videos){
if (vd.isPlaying()) { if (vd.videoData.isPlaying()) {
vd.restoreAr(); vd.videoData.restoreAr();
} }
} }
} else { } else {
for(let vd of this.videos){ for(let vd of this.videos){
vd.restoreAr(); vd.videoData.restoreAr();
} }
} }
} }
@ -524,13 +522,13 @@ class PageInfo {
if (playingOnly) { if (playingOnly) {
for(let vd of this.videos){ for(let vd of this.videos){
if (vd.isPlaying()) { if (vd.videoData.isPlaying()) {
vd.setStretchMode(stretchMode, fixedStretchRatio) vd.videoData.setStretchMode(stretchMode, fixedStretchRatio)
} }
} }
} else { } else {
for(let vd of this.videos){ for(let vd of this.videos){
vd.setStretchMode(stretchMode, fixedStretchRatio) vd.videoData.setStretchMode(stretchMode, fixedStretchRatio)
} }
} }
} }
@ -538,33 +536,33 @@ class PageInfo {
setZoom(zoomLevel, no_announce?: boolean, playingOnly?: boolean) { setZoom(zoomLevel, no_announce?: boolean, playingOnly?: boolean) {
if (playingOnly) { if (playingOnly) {
for(let vd of this.videos) { for(let vd of this.videos) {
if (vd.isPlaying()) { if (vd.videoData.isPlaying()) {
vd.setZoom(zoomLevel, no_announce); vd.videoData.setZoom(zoomLevel, no_announce);
} }
} }
} else { } else {
for(let vd of this.videos) { for(let vd of this.videos) {
vd.setZoom(zoomLevel, no_announce); vd.videoData.setZoom(zoomLevel, no_announce);
} }
} }
} }
zoomStep(step, playingOnly?: boolean) { zoomStep(step, playingOnly?: boolean) {
for(let vd of this.videos){ for(let vd of this.videos){
if (!playingOnly || vd.isPlaying()) { if (!playingOnly || vd.videoData.isPlaying()) {
vd.zoomStep(step); vd.videoData.zoomStep(step);
} }
} }
} }
markPlayer(name, color) { markPlayer(name, color) {
for (let vd of this.videos) { for (let vd of this.videos) {
vd.markPlayer(name,color); vd.videoData.markPlayer(name,color);
} }
} }
unmarkPlayer() { unmarkPlayer() {
for (let vd of this.videos) { for (let vd of this.videos) {
vd.unmarkPlayer(); vd.videoData.unmarkPlayer();
} }
} }
@ -579,13 +577,13 @@ class PageInfo {
setManualTick(manualTick) { setManualTick(manualTick) {
for(let vd of this.videos) { for(let vd of this.videos) {
vd.setManualTick(manualTick); vd.videoData.setManualTick(manualTick);
} }
} }
tick() { tick() {
for(let vd of this.videos) { for(let vd of this.videos) {
vd.tick(); vd.videoData.tick();
} }
} }