Skip to content

Commit 96eae88

Browse files
ryanbrclaude
andcommitted
Extract startDriftCorrection + use for buffer gap seek
Refactor drift correction into reusable function. Used by: 1. Post-reload drift correction (existing, now deduplicated) 2. Buffer gap seek (new — seeks past gap then catches up at 1.1x) Net effect: ~15 lines of duplicated drift code removed, buffer gap transitions now recover to live edge instead of staying behind. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 006feda commit 96eae88

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

vaft/vaft-testing-ublock-origin.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,31 @@ twitch-videoad.js text/javascript
873873
let playerForMonitoringBuffering = null;
874874
let driftCatchUpInterval = null;
875875
let driftCatchUpTimeout = null;
876+
function startDriftCorrection(videoElement) {
877+
if (DriftCorrectionRate <= 1) return;
878+
if (driftCatchUpInterval) { clearInterval(driftCatchUpInterval); driftCatchUpInterval = null; }
879+
if (driftCatchUpTimeout) { clearTimeout(driftCatchUpTimeout); driftCatchUpTimeout = null; }
880+
videoElement.playbackRate = DriftCorrectionRate;
881+
console.log('[AD DEBUG] Drift correction: catching up at ' + DriftCorrectionRate + 'x');
882+
driftCatchUpInterval = setInterval(() => {
883+
try {
884+
const vid = document.querySelector('video');
885+
if (vid && vid.buffered.length > 0) {
886+
if (vid.buffered.end(vid.buffered.length - 1) - vid.currentTime <= 1) {
887+
vid.playbackRate = 1.0;
888+
console.log('[AD DEBUG] Drift correction complete — resumed normal playback speed');
889+
clearInterval(driftCatchUpInterval); driftCatchUpInterval = null;
890+
if (driftCatchUpTimeout) { clearTimeout(driftCatchUpTimeout); driftCatchUpTimeout = null; }
891+
}
892+
}
893+
} catch { clearInterval(driftCatchUpInterval); driftCatchUpInterval = null; }
894+
}, 500);
895+
driftCatchUpTimeout = setTimeout(() => {
896+
try { videoElement.playbackRate = 1.0; } catch {}
897+
if (driftCatchUpInterval) { clearInterval(driftCatchUpInterval); driftCatchUpInterval = null; }
898+
driftCatchUpTimeout = null;
899+
}, 30000);
900+
}
876901
const playerBufferState = {
877902
channelName: null,
878903
hasStreamStarted: false,
@@ -944,6 +969,7 @@ twitch-videoad.js text/javascript
944969
if (video.buffered.start(bi) > video.currentTime + 0.5) {
945970
console.log('[AD DEBUG] Seeking past ' + (video.buffered.start(bi) - video.currentTime).toFixed(1) + 's buffer gap');
946971
video.currentTime = video.buffered.start(bi);
972+
startDriftCorrection(video);
947973
break;
948974
}
949975
}
@@ -1198,31 +1224,13 @@ twitch-videoad.js text/javascript
11981224
if (videos.length > 0 && videos[0].muted) {
11991225
videos[0].muted = false;
12001226
}
1201-
// Correct live drift after reload — gradual catch-up via playback rate instead of jarring seek
1202-
if (videos.length > 0 && videos[0].buffered.length > 0 && videos[0].readyState >= 3 && DriftCorrectionRate > 1) {
1227+
// Correct live drift after reload
1228+
if (videos.length > 0 && videos[0].buffered.length > 0 && videos[0].readyState >= 3) {
12031229
const liveEdge = videos[0].buffered.end(videos[0].buffered.length - 1);
12041230
const drift = liveEdge - videos[0].currentTime;
12051231
if (drift > 2) {
1206-
console.log('[AD DEBUG] Post-reload live drift correction: ' + drift.toFixed(1) + 's behind — catching up at ' + DriftCorrectionRate + 'x');
1207-
if (driftCatchUpInterval) { clearInterval(driftCatchUpInterval); driftCatchUpInterval = null; }
1208-
if (driftCatchUpTimeout) { clearTimeout(driftCatchUpTimeout); driftCatchUpTimeout = null; }
1209-
videos[0].playbackRate = DriftCorrectionRate;
1210-
driftCatchUpInterval = setInterval(() => {
1211-
try {
1212-
const vid = document.querySelector('video');
1213-
if (vid && vid.buffered.length > 0) {
1214-
const remaining = vid.buffered.end(vid.buffered.length - 1) - vid.currentTime;
1215-
if (remaining <= 1) {
1216-
vid.playbackRate = 1.0;
1217-
console.log('[AD DEBUG] Drift correction complete — resumed normal playback speed');
1218-
clearInterval(driftCatchUpInterval);
1219-
driftCatchUpInterval = null;
1220-
if (driftCatchUpTimeout) { clearTimeout(driftCatchUpTimeout); driftCatchUpTimeout = null; }
1221-
}
1222-
}
1223-
} catch { clearInterval(driftCatchUpInterval); driftCatchUpInterval = null; }
1224-
}, 500);
1225-
driftCatchUpTimeout = setTimeout(() => { try { videos[0].playbackRate = 1.0; } catch {} if (driftCatchUpInterval) { clearInterval(driftCatchUpInterval); driftCatchUpInterval = null; } driftCatchUpTimeout = null; }, 30000);
1232+
console.log('[AD DEBUG] Post-reload live drift correction: ' + drift.toFixed(1) + 's behind');
1233+
startDriftCorrection(videos[0]);
12261234
}
12271235
}
12281236
} catch {}

0 commit comments

Comments
 (0)