Files
ShakaPlayer/test/util/ts_parser_unit.js
Ivan 0e5f32818e perf(ts): derive start time from PES headers (#10132)
This PR adds a PES header-only method that extracts the first PTS/DTS
without concatenating PES payloads in the common case, and switches
MediaSourceEngine to use it — avoids the per-PES Uint8Array allocation
when the first TS packet already holds the full PES header (falls back
to concatenating the PES range otherwise)
2026-07-12 10:03:54 -07:00

99 lines
3.7 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
describe('TsParser', () => {
const Util = shaka.test.Util;
const BufferUtils = shaka.util.BufferUtils;
const ContentType = shaka.util.ManifestParserUtils.ContentType;
it('probes a TS segment', async () => {
const responses = await Promise.all([
Util.fetch('/base/test/test/assets/video.ts'),
Util.fetch('/base/test/test/assets/audio.ts'),
]);
const videoSegment = BufferUtils.toUint8(responses[0]);
const audioSegment = BufferUtils.toUint8(responses[1]);
expect(shaka.util.TsParser.probe(videoSegment)).toBeTruthy();
expect(shaka.util.TsParser.probe(audioSegment)).toBeTruthy();
});
it('probes a non TS segment', async () => {
const responses = await Promise.all([
Util.fetch('/base/test/test/assets/small.mp4'),
]);
const nonTsSegment = BufferUtils.toUint8(responses[0]);
expect(shaka.util.TsParser.probe(nonTsSegment)).toBeFalsy();
});
it('parses a TS segment', async () => {
const responses = await Promise.all([
Util.fetch('/base/test/test/assets/video.ts'),
Util.fetch('/base/test/test/assets/audio.ts'),
]);
const videoSegment = BufferUtils.toUint8(responses[0]);
const audioSegment = BufferUtils.toUint8(responses[1]);
expect(new shaka.util.TsParser().parse(videoSegment)).toBeDefined();
expect(new shaka.util.TsParser().parse(audioSegment)).toBeDefined();
});
it('parses a TS segment with metadata', async () => {
const responses = await Promise.all([
Util.fetch('/base/test/test/assets/id3-metadata.ts'),
]);
const tsSegment = BufferUtils.toUint8(responses[0]);
const metadata = new shaka.util.TsParser().parse(tsSegment)
.getMetadata();
expect(metadata).toBeTruthy();
expect(metadata.length).toBe(2);
const firstMetadata = metadata[0];
expect(firstMetadata.frames.length).toBe(3);
const secondMetadata = metadata[1];
expect(secondMetadata.frames.length).toBe(3);
});
it('get the start time from a TS segment', async () => {
const responses = await Promise.all([
Util.fetch('/base/test/test/assets/id3-metadata.ts'),
]);
const tsSegment = BufferUtils.toUint8(responses[0]);
const starttime = new shaka.util.TsParser().parse(tsSegment)
.getStartTime(ContentType.AUDIO);
expect(starttime).toBeCloseTo(90019.586, 3);
});
it('gets the start time from TS PES headers', async () => {
const responses = await Promise.all([
Util.fetch('/base/test/test/assets/video.ts'),
Util.fetch('/base/test/test/assets/audio.ts'),
]);
const videoSegment = BufferUtils.toUint8(responses[0]);
const audioSegment = BufferUtils.toUint8(responses[1]);
const videoStartTime = new shaka.util.TsParser().parse(videoSegment)
.getStartTime(ContentType.VIDEO);
const headerVideoStartTime = new shaka.util.TsParser().parse(videoSegment)
.getStartTimeFromPesHeaders(ContentType.VIDEO);
expect(headerVideoStartTime).toBe(videoStartTime);
const audioStartTime = new shaka.util.TsParser().parse(audioSegment)
.getStartTime(ContentType.AUDIO);
const headerAudioStartTime = new shaka.util.TsParser().parse(audioSegment)
.getStartTimeFromPesHeaders(ContentType.AUDIO);
expect(headerAudioStartTime).toBe(audioStartTime);
});
it('get the codecs from a TS segment', async () => {
const responses = await Promise.all([
Util.fetch('/base/test/test/assets/id3-metadata.ts'),
]);
const tsSegment = BufferUtils.toUint8(responses[0]);
const codecs = new shaka.util.TsParser().parse(tsSegment)
.getCodecs();
expect(codecs.audio).toBe('aac');
expect(codecs.video).toBe(null);
});
});