fix shaka preload handoff race

This commit is contained in:
root 2026-04-16 08:12:24 +00:00 committed by edideaur
parent bd68c92a90
commit f55c637ca4

View file

@ -691,9 +691,7 @@ export class Player {
return; return;
} }
console.log('[playTrackFromQueue] Fetching more tracks!');
const newTracks = await this.fetchMoreArtistPopularTracks(); const newTracks = await this.fetchMoreArtistPopularTracks();
console.log('[playTrackFromQueue] Got tracks:', newTracks?.length);
if (newTracks && newTracks.length > 0) { if (newTracks && newTracks.length > 0) {
await this.addToQueue(newTracks); await this.addToQueue(newTracks);
} }
@ -765,26 +763,27 @@ export class Player {
await this.playTrackFromQueue(startTime, recursiveCount, false); await this.playTrackFromQueue(startTime, recursiveCount, false);
}; };
let loadPromise = Promise.resolve(); const skipPlay = Symbol('skip-immediate-play');
let handoffPromise = Promise.resolve();
if (requiresShaka) { if (requiresShaka) {
const loadTarget = streamInfo.preloadManager || streamUrl; const loadTarget = streamInfo.preloadManager || streamUrl;
loadPromise = handoffPromise =
startTime > 0 ? this.shakaPlayer.load(loadTarget, startTime) : this.shakaPlayer.load(loadTarget); startTime > 0 ? this.shakaPlayer.load(loadTarget, startTime) : this.shakaPlayer.load(loadTarget);
this.shakaInitialized = true; this.shakaInitialized = true;
void loadPromise handoffPromise = handoffPromise.then(() => {
.then(() => {
if (this.playbackSequence !== currentSequence || this.currentTrack?.id !== track.id) { if (this.playbackSequence !== currentSequence || this.currentTrack?.id !== track.id) {
return; return skipPlay;
} }
this.applyAudioEffects(); this.applyAudioEffects();
const savedAdaptiveQuality = localStorage.getItem('adaptive-playback-quality') || 'auto'; const savedAdaptiveQuality = localStorage.getItem('adaptive-playback-quality') || 'auto';
this.forceQuality(savedAdaptiveQuality); this.forceQuality(savedAdaptiveQuality);
this.updateAdaptiveQualityBadge(); this.updateAdaptiveQualityBadge();
})
.catch((error) => retryImmediateHandoff(error).catch(console.error)); return this.safePlay(activeElement);
});
} else { } else {
activeElement.src = streamUrl; activeElement.src = streamUrl;
this.applyAudioEffects(); this.applyAudioEffects();
@ -793,16 +792,25 @@ export class Player {
if (startTime > 0) { if (startTime > 0) {
activeElement.currentTime = startTime; activeElement.currentTime = startTime;
} }
handoffPromise = this.safePlay(activeElement);
} }
void this.safePlay(activeElement) void handoffPromise
.then((played) => { .then((played) => {
if (played === skipPlay) {
return;
}
if (!played) { if (!played) {
return retryImmediateHandoff(new Error('Immediate handoff did not start playback')).catch( return retryImmediateHandoff(new Error('Immediate handoff did not start playback')).catch(
console.error console.error
); );
} }
if (this.playbackSequence !== currentSequence || this.currentTrack?.id !== track.id) {
return;
}
this.preloadNextTracks(); this.preloadNextTracks();
}) })
.catch((error) => retryImmediateHandoff(error).catch(console.error)); .catch((error) => retryImmediateHandoff(error).catch(console.error));