Skip to content

Commit

Permalink
Handle alternate audio switching and switched states to prevent main …
Browse files Browse the repository at this point in the history
…audio from being appended while switching to alternate. (#6845)

Fixes #6812
  • Loading branch information
robwalch authored Nov 15, 2024
1 parent 8ef794a commit 8d1dbf7
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/controller/stream-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ import type { BufferInfo } from '../utils/buffer-helper';

const TICK_INTERVAL = 100; // how often to tick in ms

const enum AlternateAudio {
DISABLED = 0,
SWITCHING,
SWITCHED,
}

export default class StreamController
extends BaseStreamController
implements NetworkComponentAPI
Expand All @@ -53,7 +59,7 @@ export default class StreamController
private level: number = -1;
private _forceStartLoad: boolean = false;
private _hasEnoughToStart: boolean = false;
private altAudio: boolean = false;
private altAudio: AlternateAudio = AlternateAudio.DISABLED;
private audioOnly: boolean = false;
private fragPlaying: Fragment | null = null;
private fragLastKbps: number = 0;
Expand Down Expand Up @@ -256,7 +262,7 @@ export default class StreamController
const lastDetails = this.getLevelDetails();
if (lastDetails && this._streamEnded(bufferInfo, lastDetails)) {
const data: BufferEOSData = {};
if (this.altAudio) {
if (this.altAudio === AlternateAudio.SWITCHED) {
data.type = 'video';
}

Expand Down Expand Up @@ -509,7 +515,7 @@ export default class StreamController
super.flushMainBuffer(
startOffset,
endOffset,
this.altAudio ? 'video' : null,
this.altAudio === AlternateAudio.SWITCHED ? 'video' : null,
);
}

Expand Down Expand Up @@ -606,7 +612,8 @@ export default class StreamController
this.couldBacktrack = false;
this.fragLastKbps = 0;
this.fragPlaying = this.backtrackFragment = null;
this.altAudio = this.audioOnly = false;
this.altAudio = AlternateAudio.DISABLED;
this.audioOnly = false;
}

private onManifestParsed(
Expand Down Expand Up @@ -837,7 +844,7 @@ export default class StreamController
data: AudioTrackSwitchingData,
) {
// if any URL found on new audio track, it is an alternate audio track
const fromAltAudio = this.altAudio;
const fromAltAudio = this.altAudio === AlternateAudio.SWITCHED;
const altAudio = !!data.url;
// if we switch on main audio, ensure that main fragment scheduling is synced with media.buffered
// don't do anything if we switch to alt audio: audio stream controller is handling it.
Expand Down Expand Up @@ -874,6 +881,8 @@ export default class StreamController
this.fragmentTracker.removeAllFragments();
}
hls.trigger(Events.AUDIO_TRACK_SWITCHED, data);
} else {
this.altAudio = AlternateAudio.SWITCHING;
}
}

Expand All @@ -893,7 +902,9 @@ export default class StreamController
this.mediaBuffer = videoBuffer;
}
}
this.altAudio = altAudio;
this.altAudio = altAudio
? AlternateAudio.SWITCHED
: AlternateAudio.DISABLED;
this.tick();
}

Expand Down

0 comments on commit 8d1dbf7

Please sign in to comment.