Preserve HLS subtitle and closed-caption cues across recovery

This commit is contained in:
Technomancer702
2026-10-13 17:26:25 -07:00
parent b9dad30f06
commit a2c4b4a5c5
2 changed files with 28 additions and 6 deletions

View File

@ -57,15 +57,27 @@ class CaptionTracks {
}
prepareMediaRecovery() {
if (!this.remoteTracks.length) return;
const tracks = Array.from(this.hlsTracks, ([key, entry]) => ({ key, entry,
cues: Array.from(entry.element.track.cues || []), mode: entry.element.track.mode }));
const hasRemoteTracks = this.remoteTracks.length > 0;
if (!hasRemoteTracks && !tracks.length) return;
const selected = this.remoteTracks.findIndex(element => element.track.mode === 'showing');
const generation = this.generation, revision = this.selectionRevision;
return () => {
if (this.generation !== generation || this.selectionRevision !== revision) return;
// HLS detachment clears native cues, including extracted remote subtitles.
this.removeRemoteTracks();
this.addRemoteTracks();
if (selected >= 0 && this.remoteTracks[selected]) this.remoteTracks[selected].track.mode = 'showing';
if (this.generation !== generation) return;
// HLS detachment clears all native cues, including our owned caption tracks.
for (const { key, entry, cues, mode } of tracks) {
if (this.hlsTracks.get(key) !== entry) continue;
this.appendHlsCues(entry, cues);
if (this.selectionRevision === revision) entry.element.track.mode = mode;
}
if (hasRemoteTracks) {
const remoteSelection = this.selectionRevision === revision ? selected :
this.remoteTracks.findIndex(element => element.track.mode === 'showing');
this.removeRemoteTracks();
this.addRemoteTracks();
if (remoteSelection >= 0 && this.remoteTracks[remoteSelection]) this.remoteTracks[remoteSelection].track.mode = 'showing';
}
this.onChange();
};
}