Merge pull request #1182 from ajayyy/precise-skipping

Improve precision of skips on both chromium and firefox
This commit is contained in:
Ajay Ramachandran
2022-02-09 14:06:30 -05:00
committed by GitHub

View File

@@ -45,6 +45,7 @@ let lockedCategories: Category[] = [];
// Skips are rescheduled every seeking event. // Skips are rescheduled every seeking event.
// Skips are canceled every seeking event // Skips are canceled every seeking event
let currentSkipSchedule: NodeJS.Timeout = null; let currentSkipSchedule: NodeJS.Timeout = null;
let currentSkipInterval: NodeJS.Timeout = null;
/** Has the sponsor been skipped */ /** Has the sponsor been skipped */
let sponsorSkipped: boolean[] = []; let sponsorSkipped: boolean[] = [];
@@ -427,9 +428,13 @@ function videoOnReadyListener(): void {
function cancelSponsorSchedule(): void { function cancelSponsorSchedule(): void {
if (currentSkipSchedule !== null) { if (currentSkipSchedule !== null) {
clearTimeout(currentSkipSchedule); clearTimeout(currentSkipSchedule);
currentSkipSchedule = null; currentSkipSchedule = null;
} }
if (currentSkipInterval !== null) {
clearInterval(currentSkipInterval);
currentSkipInterval = null;
}
} }
/** /**
@@ -491,14 +496,15 @@ function startSponsorSchedule(includeIntersectingSegments = false, currentTime?:
// Don't skip if this category should not be skipped // Don't skip if this category should not be skipped
if (!shouldSkip(currentSkip) && !sponsorTimesSubmitting?.some((segment) => segment.segment === currentSkip.segment)) return; if (!shouldSkip(currentSkip) && !sponsorTimesSubmitting?.some((segment) => segment.segment === currentSkip.segment)) return;
const skippingFunction = () => { const skippingFunction = (forceVideoTime?: number) => {
let forcedSkipTime: number = null; let forcedSkipTime: number = null;
let forcedIncludeIntersectingSegments = false; let forcedIncludeIntersectingSegments = false;
let forcedIncludeNonIntersectingSegments = true; let forcedIncludeNonIntersectingSegments = true;
if (incorrectVideoCheck(videoID, currentSkip)) return; if (incorrectVideoCheck(videoID, currentSkip)) return;
forceVideoTime ||= video.currentTime;
if (video.currentTime >= skipTime[0] && video.currentTime < skipTime[1]) { if (forceVideoTime >= skipTime[0] && forceVideoTime < skipTime[1]) {
skipToTime({ skipToTime({
v: video, v: video,
skipTime, skipTime,
@@ -522,7 +528,22 @@ function startSponsorSchedule(includeIntersectingSegments = false, currentTime?:
if (timeUntilSponsor <= 0) { if (timeUntilSponsor <= 0) {
skippingFunction(); skippingFunction();
} else { } else {
currentSkipSchedule = setTimeout(skippingFunction, timeUntilSponsor * 1000 * (1 / video.playbackRate)); const delayTime = timeUntilSponsor * 1000 * (1 / video.playbackRate);
if (delayTime < 300 && utils.isFirefox()) {
// For Firefox, use interval instead of timeout near the end to combat imprecise video time
const startIntervalTime = performance.now();
const startVideoTime = video.currentTime;
currentSkipInterval = setInterval(() => {
const intervalDuration = performance.now() - startIntervalTime;
if (intervalDuration >= delayTime || video.currentTime >= skipTime[0]) {
clearInterval(currentSkipInterval);
skippingFunction(Math.max(video.currentTime, startVideoTime + intervalDuration / 1000));
}
}, 5);
} else {
// Schedule for right before to be more precise than normal timeout
currentSkipSchedule = setTimeout(skippingFunction, Math.max(0, delayTime - 30));
}
} }
} }