Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sidx duration #4849

Merged
merged 5 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/loader/playlist-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import { Events } from '../events';
import { ErrorDetails, ErrorTypes } from '../errors';
import { logger } from '../utils/logger';
import { parseSegmentIndex } from '../utils/mp4-tools';
import { parseSegmentIndex, findBox } from '../utils/mp4-tools';
import M3U8Parser from './m3u8-parser';
import type { LevelParsed } from '../types/level';
import type {
Expand Down Expand Up @@ -543,10 +543,13 @@ class PlaylistLoader {
response: LoaderResponse,
context: PlaylistLoaderContext
): void {
const sidxInfo = parseSegmentIndex(
new Uint8Array(response.data as ArrayBuffer)
);
const data = new Uint8Array(response.data as ArrayBuffer);
const sidxBox = findBox(data, ['sidx'])[0];
// if provided fragment does not contain sidx, early return
if (!sidxBox) {
return;
}
const sidxInfo = parseSegmentIndex(sidxBox);
if (!sidxInfo) {
return;
}
Expand All @@ -564,7 +567,9 @@ class PlaylistLoader {
);
}
if (frag.initSegment) {
frag.initSegment.setByteRange(String(sidxInfo.moovEndOffset) + '@0');
const moovBox = findBox(data, ['moov'])[0];
const moovEndOffset = moovBox ? moovBox.length : null;
frag.initSegment.setByteRange(String(moovEndOffset) + '@0');
}
});
}
Expand Down
33 changes: 13 additions & 20 deletions src/utils/mp4-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,10 @@ type SidxInfo = {
version: number;
referencesCount: number;
references: any[];
moovEndOffset: number | null;
};

export function parseSegmentIndex(initSegment: Uint8Array): SidxInfo | null {
const moovBox = findBox(initSegment, ['moov']);
const moov = moovBox[0];
const moovEndOffset = moov ? moov.length : null; // we need this in case we need to chop of garbage of the end of current data

const sidxBox = findBox(initSegment, ['sidx']);

if (!sidxBox || !sidxBox[0]) {
return null;
}

export function parseSegmentIndex(sidx: Uint8Array): SidxInfo | null {
const references: any[] = [];
const sidx = sidxBox[0];

const version = sidx[0];

Expand Down Expand Up @@ -179,7 +167,6 @@ export function parseSegmentIndex(initSegment: Uint8Array): SidxInfo | null {
version,
referencesCount,
references,
moovEndOffset,
};
}

Expand Down Expand Up @@ -409,13 +396,19 @@ export function getDuration(data: Uint8Array, initData: InitData) {
}
if (videoDuration === 0 && audioDuration === 0) {
// If duration samples are not available in the traf use sidx subsegment_duration
const sidx = parseSegmentIndex(data);
if (sidx?.references) {
return sidx.references.reduce(
(dur, ref) => dur + ref.info.duration || 0,
0
);
let sidxDuration = 0;
const sidxs = findBox(data, ['sidx']);
for (let i = 0; i < sidxs.length; i++) {
const sidx = parseSegmentIndex(sidxs[i]);
if (sidx?.references) {
sidxDuration += sidx.references.reduce(
(dur, ref) => dur + ref.info.duration || 0,
0
);
}
}

return sidxDuration;
}
if (videoDuration) {
return videoDuration;
Expand Down