Skip to content

Commit

Permalink
Fix unused part loading when selected frag passed to _doFragLoad is…
Browse files Browse the repository at this point in the history
… not used by part selection
  • Loading branch information
robwalch committed Nov 14, 2024
1 parent f53373f commit 9c5089d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
2 changes: 1 addition & 1 deletion api-extractor/report/hls.js.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ export class BaseStreamController extends TaskLoop implements NetworkComponentAP
// (undocumented)
protected loadingParts: boolean;
// (undocumented)
protected _loadInitSegment(frag: Fragment, level: Level): void;
protected _loadInitSegment(fragment: Fragment, level: Level): void;
// (undocumented)
mapToInitFragWhenRequired(frag: Fragment | null): typeof frag;
// (undocumented)
Expand Down
50 changes: 29 additions & 21 deletions src/controller/base-stream-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,18 +477,19 @@ export default class BaseStreamController
}

private _loadFragForPlayback(
frag: Fragment,
fragment: Fragment,
level: Level,
targetBufferTime: number,
) {
const progressCallback: FragmentLoadProgressCallback = (
data: FragLoadedData,
) => {
const frag = data.frag;
if (this.fragContextChanged(frag)) {
this.warn(
`Fragment ${frag.sn}${
data.part ? ' p: ' + data.part.index : ''
} of level ${frag.level} was dropped during download.`,
`${frag.type} sn: ${frag.sn}${
data.part ? ' part: ' + data.part.index : ''
} of ${this.fragInfo(frag, false, data.part)}) was dropped during download.`,
);
this.fragmentTracker.removeFragment(frag);
return;
Expand All @@ -497,13 +498,14 @@ export default class BaseStreamController
this._handleFragmentLoadProgress(data);
};

this._doFragLoad(frag, level, targetBufferTime, progressCallback)
this._doFragLoad(fragment, level, targetBufferTime, progressCallback)
.then((data) => {
if (!data) {
// if we're here we probably needed to backtrack or are waiting for more parts
return;
}
const state = this.state;
const frag = data.frag;
if (this.fragContextChanged(frag)) {
if (
state === State.FRAG_LOADING ||
Expand All @@ -530,7 +532,7 @@ export default class BaseStreamController
return;
}
this.warn(`Frag error: ${reason?.message || reason}`);
this.resetFragmentLoading(frag);
this.resetFragmentLoading(fragment);
});
}

Expand Down Expand Up @@ -608,18 +610,19 @@ export default class BaseStreamController
this.hls.trigger(Events.BUFFER_FLUSHING, flushScope);
}

protected _loadInitSegment(frag: Fragment, level: Level) {
this._doFragLoad(frag, level)
protected _loadInitSegment(fragment: Fragment, level: Level) {
this._doFragLoad(fragment, level)
.then((data) => {
if (!data || this.fragContextChanged(frag) || !this.levels) {
const frag = data?.frag;
if (!frag || this.fragContextChanged(frag) || !this.levels) {
throw new Error('init load aborted');
}

return data;
})
.then((data: FragLoadedData) => {
const { hls } = this;
const { payload } = data;
const { frag, payload } = data;
const decryptData = frag.decryptdata;

// check to see if the payload needs to be decrypted
Expand Down Expand Up @@ -671,7 +674,7 @@ export default class BaseStreamController
return;
}
this.warn(reason);
this.resetFragmentLoading(frag);
this.resetFragmentLoading(fragment);
});
}

Expand Down Expand Up @@ -705,7 +708,7 @@ export default class BaseStreamController
this.log(
`Buffered ${frag.type} sn: ${frag.sn}${
part ? ' part: ' + part.index : ''
} of ${this.fragInfo(frag)} > buffer:${
} of ${this.fragInfo(frag, false, part)} > buffer:${
media
? TimeRanges.toString(BufferHelper.getBuffered(media))
: '(detached)'
Expand Down Expand Up @@ -823,12 +826,11 @@ export default class BaseStreamController
const partIndex = this.getNextPart(partList, frag, targetBufferTime);
if (partIndex > -1) {
const part = partList[partIndex];
frag = this.fragCurrent = part.fragment;
this.log(
`Loading part sn: ${frag.sn} p: ${part.index} cc: ${
`Loading ${frag.type} sn: ${frag.sn} part: ${part.index} (${partIndex}/${partList.length - 1}) of ${this.fragInfo(frag, false, part)}) cc: ${
frag.cc
} of playlist [${details.startSN}-${
details.endSN
}] parts [0-${partIndex}-${partList.length - 1}] ${this.playlistLabel()}: ${frag.level}, target: ${parseFloat(
} [${details.startSN}-${details.endSN}], target: ${parseFloat(
targetBufferTime.toFixed(3),
)}`,
);
Expand Down Expand Up @@ -1947,7 +1949,7 @@ export default class BaseStreamController
this.log(
`Parsed ${frag.type} sn: ${frag.sn}${
part ? ' part: ' + part.index : ''
} of ${this.fragInfo(frag)})`,
} of ${this.fragInfo(frag, false, part)})`,
);
this.hls.trigger(Events.FRAG_PARSED, { frag, part });
}
Expand All @@ -1956,10 +1958,16 @@ export default class BaseStreamController
return this.playlistType === PlaylistLevelType.MAIN ? 'level' : 'track';
}

private fragInfo(frag: Fragment, pts: boolean = true): string {
return `${this.playlistLabel()} ${frag.level} (frag:[${((pts ? frag.startPTS : frag.start) ?? NaN).toFixed(3)}-${(
(pts ? frag.endPTS : frag.end) ?? NaN
).toFixed(3)}]`;
private fragInfo(
frag: Fragment,
pts: boolean = true,
part?: Part | null,
): string {
return `${this.playlistLabel()} ${frag.level} (${part ? 'part' : 'frag'}:[${((pts && !part ? frag.startPTS : (part || frag).start) ?? NaN).toFixed(3)}-${(
(pts && !part ? frag.endPTS : (part || frag).end) ?? NaN
).toFixed(
3,
)}]${part && frag.type === 'main' ? 'INDEPENDENT=' + (part.independent ? 'YES' : 'NO') : ''}`;
}

protected resetTransmuxer() {
Expand Down
9 changes: 5 additions & 4 deletions src/controller/stream-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1151,11 +1151,12 @@ export default class StreamController
return audioCodec;
}

private _loadBitrateTestFrag(frag: Fragment, level: Level) {
frag.bitrateTest = true;
this._doFragLoad(frag, level).then((data) => {
private _loadBitrateTestFrag(fragment: Fragment, level: Level) {
fragment.bitrateTest = true;
this._doFragLoad(fragment, level).then((data) => {
const { hls } = this;
if (!data || this.fragContextChanged(frag)) {
const frag = data?.frag;
if (!frag || this.fragContextChanged(frag)) {
return;
}
level.fragmentError = 0;
Expand Down
6 changes: 4 additions & 2 deletions src/demux/transmuxer-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import Transmuxer, {
} from '../demux/transmuxer';
import { ErrorDetails, ErrorTypes } from '../errors';
import { Events } from '../events';
import { PlaylistLevelType } from '../types/loader';
import { getM2TSSupportedAudioTypes } from '../utils/codecs';
import type { WorkerContext } from './inject-worker';
import type { HlsEventEmitter, HlsListeners } from '../events';
import type Hls from '../hls';
import type { MediaFragment, Part } from '../loader/fragment';
import type { ErrorData, FragDecryptedData } from '../types/events';
import type { PlaylistLevelType } from '../types/loader';
import type { ChunkMetadata, TransmuxerResult } from '../types/transmuxer';
import type { RationalTimestamp } from '../utils/timescale-conversion';

Expand Down Expand Up @@ -234,7 +234,9 @@ export default class TransmuxerInterface {
);
if (!contiguous || discontinuity || initSegmentChange) {
this.hls.logger
.log(`[transmuxer-interface, ${frag.type}]: Starting new transmux session for sn: ${chunkMeta.sn} p: ${chunkMeta.part} level: ${chunkMeta.level} id: ${chunkMeta.id}
.log(`[transmuxer-interface]: Starting new transmux session for ${frag.type} sn: ${chunkMeta.sn}${
chunkMeta.part > -1 ? ' part: ' + chunkMeta.part : ''
} ${this.id === PlaylistLevelType.MAIN ? 'level' : 'track'}: ${chunkMeta.level} id: ${chunkMeta.id}
discontinuity: ${discontinuity}
trackSwitch: ${trackSwitch}
contiguous: ${contiguous}
Expand Down
2 changes: 1 addition & 1 deletion src/demux/transmuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export default class Transmuxer {
const { accurateTimeOffset, timeOffset } = this.currentTransmuxState;
this.logger.log(
`[transmuxer.ts]: Flushed ${this.id} sn: ${chunkMeta.sn}${
chunkMeta.part > -1 ? ' p: ' + chunkMeta.part : ''
chunkMeta.part > -1 ? ' part: ' + chunkMeta.part : ''
} of ${this.id === PlaylistLevelType.MAIN ? 'level' : 'track'} ${chunkMeta.level}`,
);
const remuxResult = this.remuxer!.remux(
Expand Down

0 comments on commit 9c5089d

Please sign in to comment.