Skip to content

Commit

Permalink
fix(Transmuxer): Fix transmuxer when AAC in TS have overflow samples …
Browse files Browse the repository at this point in the history
…between PES
  • Loading branch information
avelad committed Dec 30, 2024
1 parent df8eff9 commit 361b1ab
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
2 changes: 2 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ module.exports = (config) => {
{pattern: 'test/test/assets/hls-ts-h265/*', included: false},
{pattern: 'test/test/assets/hls-ts-mp3/*', included: false},
{pattern: 'test/test/assets/hls-ts-muxed-aac-h264/*', included: false},
// eslint-disable-next-line max-len
{pattern: 'test/test/assets/hls-ts-muxed-aac-h264-with-overflow-samples/*', included: false},
{pattern: 'test/test/assets/hls-ts-muxed-aac-h265/*', included: false},
{pattern: 'test/test/assets/hls-ts-muxed-ac3-h264/*', included: false},
{pattern: 'test/test/assets/hls-ts-muxed-mp3-h264/*', included: false},
Expand Down
33 changes: 32 additions & 1 deletion lib/transmuxer/ts_transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ shaka.transmuxer.TsTransmuxer = class {
getAacStreamInfo_(tsParser, stream, duration, reference) {
const ADTS = shaka.transmuxer.ADTS;
const timescale = shaka.util.TsParser.Timescale;
const Uint8ArrayUtils = shaka.util.Uint8ArrayUtils;

/** @type {!Array.<shaka.util.Mp4Generator.Mp4Sample>} */
const samples = [];
Expand All @@ -320,12 +321,38 @@ shaka.transmuxer.TsTransmuxer = class {

let firstPts = null;

/** @type {?number} */
let nextStartOffset = null;
/** @type {?Uint8Array} */
let overflowBytes = null;

for (const audioData of tsParser.getAudioData()) {
const data = audioData.data;
if (!data) {
continue;
}
let offset = 0;
if (nextStartOffset && overflowBytes) {
offset = nextStartOffset;
const missingFrameData =
Uint8ArrayUtils.concat(overflowBytes, data.subarray(0, offset));
samples.push({
data: missingFrameData,
size: missingFrameData.byteLength,
duration: ADTS.AAC_SAMPLES_PER_FRAME,
cts: 0,
flags: {
isLeading: 0,
isDependedOn: 0,
hasRedundancy: 0,
degradPrio: 0,
dependsOn: 2,
isNonSync: 0,
},
});
overflowBytes = null;
nextStartOffset = null;
}
info = ADTS.parseInfo(data, offset);
if (!info) {
throw new shaka.util.Error(
Expand All @@ -347,7 +374,11 @@ shaka.transmuxer.TsTransmuxer = class {
continue;
}
const length = header.headerLength + header.frameLength;
if (offset + length <= data.length) {
nextStartOffset = Math.max(0, offset + length - data.length);
if (nextStartOffset != 0) {
overflowBytes = data.subarray(
offset + header.headerLength, offset + length);
} else if (offset + length <= data.length) {
const frameData = data.subarray(
offset + header.headerLength, offset + length);

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:9.97663,
fileSequence0.ts
#EXT-X-ENDLIST
17 changes: 17 additions & 0 deletions test/transmuxer/transmuxer_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,23 @@ describe('Transmuxer Player', () => {
await player.unload();
});

it('H.264+AAC with AAC sample with overflow between PES', async () => {
// eslint-disable-next-line max-len
await player.load('/base/test/test/assets/hls-ts-muxed-aac-h264-with-overflow-samples/media.m3u8');
await video.play();
expect(player.isLive()).toBe(false);

// Wait for the video to start playback. If it takes longer than 10
// seconds, fail the test.
await waiter.waitForMovementOrFailOnTimeout(video, 10);

// Play for e seconds, but stop early if the video ends. If it takes
// longer than 45 seconds, fail the test.
await waiter.waitUntilPlayheadReachesOrFailOnTimeout(video, 3, 45);

await player.unload();
});

it('H.265+AAC in TS', async () => {
if (!await Util.isTypeSupported('video/mp4; codecs="hvc1.2.4.L123.B0"',
/* width= */ 720, /* height= */ 1280)) {
Expand Down

0 comments on commit 361b1ab

Please sign in to comment.