Skip to content

Commit

Permalink
Make mergeDetails a no-op when both arguments are the same object
Browse files Browse the repository at this point in the history
Fix buffer flushing when switching between audio-only from main and alt (variant and media playlists)
Add additional test coverage
  • Loading branch information
robwalch committed Dec 21, 2024
1 parent 5ca9822 commit 17a49cf
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 32 deletions.
16 changes: 11 additions & 5 deletions src/controller/audio-stream-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
alignMediaPlaylistByPDT,
} from '../utils/discontinuities';
import { mediaAttributesIdentical } from '../utils/media-option-attributes';
import { useAlternateAudio } from '../utils/rendition-helper';
import type { FragmentTracker } from './fragment-tracker';
import type Hls from '../hls';
import type { Fragment, MediaFragment, Part } from '../loader/fragment';
Expand Down Expand Up @@ -536,8 +537,8 @@ class AudioStreamController
const mainDetails = this.mainDetails;
if (
!mainDetails ||
mainDetails.expired ||
newDetails.endCC > mainDetails.endCC
newDetails.endCC > mainDetails.endCC ||
mainDetails.expired
) {
this.cachedTrackLoadedData = data;
if (this.state !== State.STOPPED) {
Expand Down Expand Up @@ -1027,9 +1028,14 @@ class AudioStreamController
bufferedTrack.name !== switchingTrack.name ||
bufferedTrack.lang !== switchingTrack.lang)
) {
this.log('Switching audio track : flushing all audio');
super.flushMainBuffer(0, Number.POSITIVE_INFINITY, 'audio');
this.bufferedTrack = null;
if (useAlternateAudio(switchingTrack.url, this.hls)) {
this.log('Switching audio track : flushing all audio');
super.flushMainBuffer(0, Number.POSITIVE_INFINITY, 'audio');
this.bufferedTrack = null;
} else {
// Main is being buffered. Set bufferedTrack so that it is flushed when switching back to alt-audio
this.bufferedTrack = switchingTrack;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/controller/base-stream-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ export default class BaseStreamController
const mainStart = this.hls.startPosition;
const liveSyncPosition = this.hls.liveSyncPosition;
const startPosition = frag
? (mainStart !== -1 && mainStart >= pos
? (mainStart !== -1 && mainStart >= start
? mainStart
: liveSyncPosition) || frag.start
: pos;
Expand Down
2 changes: 1 addition & 1 deletion src/loader/level-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class LevelDetails {
}

get expired(): boolean {
if (this.live && this.age) {
if (this.live && this.age && this.misses < 3) {
const playlistWindowDuration = this.partEnd - this.fragmentStart;
return (
this.age >
Expand Down
5 changes: 4 additions & 1 deletion src/utils/level-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ export function updateFragPTSDTS(
export function mergeDetails(
oldDetails: LevelDetails,
newDetails: LevelDetails,
): void {
) {
if (oldDetails === newDetails) {
return;
}
// Track the last initSegment processed. Initialize it to the last one on the timeline.
let currentInitSegment: Fragment | null = null;
const oldFragments = oldDetails.fragments;
Expand Down
5 changes: 4 additions & 1 deletion src/utils/rendition-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ function searchDownAndUpList(
return -1;
}

export function useAlternateAudio(audioTrackUrl: string, hls: Hls): boolean {
export function useAlternateAudio(
audioTrackUrl: string | undefined,
hls: Hls,
): boolean {
return !!audioTrackUrl && audioTrackUrl !== hls.levels[hls.loadLevel]?.uri;
}
Loading

0 comments on commit 17a49cf

Please sign in to comment.