Avoid reloading synced YouTube playback retries
This commit is contained in:
+21
-5
@@ -48,6 +48,7 @@ let pauseEnforcementUntil = 0;
|
|||||||
let desiredYoutubePlaying = false;
|
let desiredYoutubePlaying = false;
|
||||||
let pendingWatchState = null;
|
let pendingWatchState = null;
|
||||||
let pendingWatchStateOptions = null;
|
let pendingWatchStateOptions = null;
|
||||||
|
let youtubePlaybackRequestId = 0;
|
||||||
let watchPlaylist = [];
|
let watchPlaylist = [];
|
||||||
const peers = new Map();
|
const peers = new Map();
|
||||||
|
|
||||||
@@ -305,11 +306,14 @@ async function ensureYoutubePlayer() {
|
|||||||
controls: 1,
|
controls: 1,
|
||||||
disablekb: 0,
|
disablekb: 0,
|
||||||
modestbranding: 1,
|
modestbranding: 1,
|
||||||
|
playsinline: 1,
|
||||||
rel: 0
|
rel: 0
|
||||||
},
|
},
|
||||||
events: {
|
events: {
|
||||||
onReady: () => {
|
onReady: () => {
|
||||||
youtubeReady = true;
|
youtubeReady = true;
|
||||||
|
const iframe = youtubePlayer?.getIframe?.();
|
||||||
|
iframe?.setAttribute("allow", "autoplay; encrypted-media; fullscreen; picture-in-picture");
|
||||||
if (pendingWatchState) {
|
if (pendingWatchState) {
|
||||||
const state = pendingWatchState;
|
const state = pendingWatchState;
|
||||||
const options = pendingWatchStateOptions || {};
|
const options = pendingWatchStateOptions || {};
|
||||||
@@ -325,11 +329,18 @@ async function ensureYoutubePlayer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function requestYoutubePlayback(player, videoId, position) {
|
function requestYoutubePlayback(player, videoId, position) {
|
||||||
|
const requestId = ++youtubePlaybackRequestId;
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
|
|
||||||
for (const delay of [0, 150, 750, 1500, 3000, 5000]) {
|
for (const delay of [0, 150, 750, 1500, 3000]) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (!desiredYoutubePlaying || currentYoutubeVideoId !== videoId) return;
|
if (
|
||||||
|
requestId !== youtubePlaybackRequestId ||
|
||||||
|
!desiredYoutubePlaying ||
|
||||||
|
currentYoutubeVideoId !== videoId
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const playerState = player.getPlayerState?.();
|
const playerState = player.getPlayerState?.();
|
||||||
if (playerState === YT.PlayerState.PLAYING) return;
|
if (playerState === YT.PlayerState.PLAYING) return;
|
||||||
@@ -338,10 +349,13 @@ function requestYoutubePlayback(player, videoId, position) {
|
|||||||
const nextPosition = Math.max(0, position + elapsed);
|
const nextPosition = Math.max(0, position + elapsed);
|
||||||
const loadedVideoId = player.getVideoData?.().video_id;
|
const loadedVideoId = player.getVideoData?.().video_id;
|
||||||
if (
|
if (
|
||||||
loadedVideoId !== videoId ||
|
loadedVideoId === videoId &&
|
||||||
[YT.PlayerState.UNSTARTED, YT.PlayerState.CUED, YT.PlayerState.ENDED].includes(playerState)
|
![YT.PlayerState.CUED, YT.PlayerState.UNSTARTED].includes(playerState)
|
||||||
) {
|
) {
|
||||||
player.loadVideoById(videoId, nextPosition);
|
player.seekTo(nextPosition, true);
|
||||||
|
}
|
||||||
|
if (delay >= 1500) {
|
||||||
|
player.mute?.();
|
||||||
}
|
}
|
||||||
player.playVideo();
|
player.playVideo();
|
||||||
}, delay);
|
}, delay);
|
||||||
@@ -440,6 +454,7 @@ async function applyWatchState(state, { suppressEvents = true } = {}) {
|
|||||||
|
|
||||||
if (!currentYoutubeVideoId) {
|
if (!currentYoutubeVideoId) {
|
||||||
desiredYoutubePlaying = false;
|
desiredYoutubePlaying = false;
|
||||||
|
youtubePlaybackRequestId++;
|
||||||
youtubePlayer?.stopVideo?.();
|
youtubePlayer?.stopVideo?.();
|
||||||
youtubeShell.classList.remove("is-active");
|
youtubeShell.classList.remove("is-active");
|
||||||
setWatchCollapsed(true);
|
setWatchCollapsed(true);
|
||||||
@@ -458,6 +473,7 @@ async function applyWatchState(state, { suppressEvents = true } = {}) {
|
|||||||
if (shouldPlay) {
|
if (shouldPlay) {
|
||||||
pauseEnforcementUntil = 0;
|
pauseEnforcementUntil = 0;
|
||||||
} else {
|
} else {
|
||||||
|
youtubePlaybackRequestId++;
|
||||||
pauseEnforcementUntil = Math.max(pauseEnforcementUntil, Date.now() + 3000);
|
pauseEnforcementUntil = Math.max(pauseEnforcementUntil, Date.now() + 3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user