Skip to content

Commit

Permalink
Merge pull request #3621 from video-dev/bugfix/current-level-with-tracks
Browse files Browse the repository at this point in the history
Fix `currentLevel` and `getAppendedFrag` with alt-audio streams
  • Loading branch information
robwalch committed Mar 17, 2021
2 parents e8e4b02 + c01513c commit 9f4cab2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
73 changes: 39 additions & 34 deletions src/controller/fragment-tracker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Events } from '../events';
import { Fragment, Part } from '../loader/fragment';
import { PlaylistLevelType } from '../types/loader';
import type { SourceBufferName } from '../types/buffer';
import type {
FragmentBufferedRange,
FragmentEntity,
FragmentTimeRange,
} from '../types/fragment-tracker';
import type { PlaylistLevelType } from '../types/loader';
import type { ComponentAPI } from '../types/component-api';
import type {
BufferAppendedData,
Expand Down Expand Up @@ -72,33 +72,36 @@ export class FragmentTracker implements ComponentAPI {
position: number,
levelType: PlaylistLevelType
): Fragment | Part | null {
const { activeFragment, activeParts } = this;
if (!activeFragment) {
return null;
}
if (activeParts) {
for (let i = activeParts.length; i--; ) {
const activePart = activeParts[i];
const appendedPTS = activePart
? activePart.end
: activeFragment.appendedPTS;
if (
activePart.start <= position &&
appendedPTS !== undefined &&
position <= appendedPTS
) {
if (i > 0) {
this.activeParts = activeParts.slice(i);
if (levelType === PlaylistLevelType.MAIN) {
const { activeFragment, activeParts } = this;
if (!activeFragment) {
return null;
}
if (activeParts) {
for (let i = activeParts.length; i--; ) {
const activePart = activeParts[i];
const appendedPTS = activePart
? activePart.end
: activeFragment.appendedPTS;
if (
activePart.start <= position &&
appendedPTS !== undefined &&
position <= appendedPTS
) {
// 9 is a magic number. remove parts from lookup after a match but keep some short seeks back.
if (i > 9) {
this.activeParts = activeParts.slice(i - 9);
}
return activePart;
}
return activePart;
}
} else if (
activeFragment.start <= position &&
activeFragment.appendedPTS !== undefined &&
position <= activeFragment.appendedPTS
) {
return activeFragment;
}
} else if (
activeFragment.start <= position &&
activeFragment.appendedPTS !== undefined &&
position <= activeFragment.appendedPTS
) {
return activeFragment;
}
return this.getBufferedFrag(position, levelType);
}
Expand Down Expand Up @@ -349,14 +352,14 @@ export class FragmentTracker implements ComponentAPI {
const { frag, part } = data;
// don't track initsegment (for which sn is not a number)
// don't track frags used for bitrateTest, they're irrelevant.
// don't track parts for memory efficiency
if (frag.sn === 'initSegment' || frag.bitrateTest || part) {
return;
}

const fragKey = getFragmentKey(frag);
this.fragments[fragKey] = {
body: frag,
part,
loaded: data,
backtrack: null,
buffered: false,
Expand All @@ -369,15 +372,17 @@ export class FragmentTracker implements ComponentAPI {
data: BufferAppendedData
) {
const { frag, part, timeRanges } = data;
this.activeFragment = frag;
if (part) {
let activeParts = this.activeParts;
if (!activeParts) {
this.activeParts = activeParts = [];
if (frag.type === PlaylistLevelType.MAIN) {
this.activeFragment = frag;
if (part) {
let activeParts = this.activeParts;
if (!activeParts) {
this.activeParts = activeParts = [];
}
activeParts.push(part);
} else {
this.activeParts = null;
}
activeParts.push(part);
} else {
this.activeParts = null;
}
// Store the latest timeRanges loaded in the buffer
this.timeRanges = timeRanges as { [key in SourceBufferName]: TimeRanges };
Expand Down
4 changes: 0 additions & 4 deletions src/types/fragment-tracker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// eslint-disable-next-line import/no-duplicates
import type { Fragment } from '../loader/fragment';
// eslint-disable-next-line import/no-duplicates
import type { Part } from '../loader/fragment';
import type { SourceBufferName } from './buffer';
import type { FragLoadedData } from './events';

export interface FragmentEntity {
body: Fragment;
part: Part | null;
loaded: FragLoadedData | null;
backtrack: FragLoadedData | null;
buffered: boolean;
Expand Down

0 comments on commit 9f4cab2

Please sign in to comment.