From b721af37ca669dbecd35ccbd8c1f59b4501ad2e0 Mon Sep 17 00:00:00 2001 From: FredTsang Date: Thu, 25 Jan 2024 09:02:49 +0800 Subject: [PATCH] fix: generate silent aac frame based on original codec (#6123) --- src/demux/audio/adts.ts | 9 +++++++-- src/remux/mp4-remuxer.ts | 4 ++-- src/types/demuxer.ts | 1 + tests/unit/demuxer/adts.js | 10 ++++++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/demux/audio/adts.ts b/src/demux/audio/adts.ts index 5fabc39be23..20e59a21fd5 100644 --- a/src/demux/audio/adts.ts +++ b/src/demux/audio/adts.ts @@ -17,6 +17,7 @@ type AudioConfig = { samplerate: number; channelCount: number; codec: string; + parsedCodec: string; manifestCodec: string; }; @@ -32,6 +33,7 @@ export function getAudioConfig( audioCodec: string, ): AudioConfig | void { let adtsObjectType: number; + let originalAdtsObjectType: number; let adtsExtensionSamplingIndex: number; let adtsChannelConfig: number; let config: number[]; @@ -42,7 +44,8 @@ export function getAudioConfig( 8000, 7350, ]; // byte 2 - adtsObjectType = ((data[offset + 2] & 0xc0) >>> 6) + 1; + adtsObjectType = originalAdtsObjectType = + ((data[offset + 2] & 0xc0) >>> 6) + 1; const adtsSamplingIndex = (data[offset + 2] & 0x3c) >>> 2; if (adtsSamplingIndex > adtsSamplingRates.length - 1) { const error = new Error(`invalid ADTS sampling index:${adtsSamplingIndex}`); @@ -167,6 +170,7 @@ export function getAudioConfig( samplerate: adtsSamplingRates[adtsSamplingIndex], channelCount: adtsChannelConfig, codec: 'mp4a.40.' + adtsObjectType, + parsedCodec: 'mp4a.40.' + originalAdtsObjectType, manifestCodec, }; } @@ -244,8 +248,9 @@ export function initTrackConfig( track.channelCount = config.channelCount; track.codec = config.codec; track.manifestCodec = config.manifestCodec; + track.parsedCodec = config.parsedCodec; logger.log( - `parsed codec:${track.codec}, rate:${config.samplerate}, channels:${config.channelCount}`, + `parsed codec:${track.parsedCodec}, codec:${track.codec}, rate:${config.samplerate}, channels:${config.channelCount}`, ); } } diff --git a/src/remux/mp4-remuxer.ts b/src/remux/mp4-remuxer.ts index 14367e26ee8..8e194f6a209 100644 --- a/src/remux/mp4-remuxer.ts +++ b/src/remux/mp4-remuxer.ts @@ -928,7 +928,7 @@ export default class MP4Remuxer implements Remuxer { for (let j = 0; j < missing; j++) { const newStamp = Math.max(nextPts as number, 0); let fillFrame = AAC.getSilentFrame( - track.manifestCodec || track.codec, + track.parsedCodec || track.manifestCodec || track.codec, track.channelCount, ); if (!fillFrame) { @@ -1078,7 +1078,7 @@ export default class MP4Remuxer implements Remuxer { const nbSamples: number = Math.ceil((endDTS - startDTS) / frameDuration); // silent frame const silentFrame: Uint8Array | undefined = AAC.getSilentFrame( - track.manifestCodec || track.codec, + track.parsedCodec || track.manifestCodec || track.codec, track.channelCount, ); diff --git a/src/types/demuxer.ts b/src/types/demuxer.ts index 011c791229d..7b04a122514 100644 --- a/src/types/demuxer.ts +++ b/src/types/demuxer.ts @@ -64,6 +64,7 @@ export interface DemuxedAudioTrack extends DemuxedTrack { segmentCodec?: string; channelCount?: number; manifestCodec?: string; + parsedCodec?: string; samples: AudioSample[]; } diff --git a/tests/unit/demuxer/adts.js b/tests/unit/demuxer/adts.js index 64506ce2ffd..48c6d272ead 100644 --- a/tests/unit/demuxer/adts.js +++ b/tests/unit/demuxer/adts.js @@ -44,6 +44,7 @@ describe('getAudioConfig', function () { samplerate: 96000, channelCount: 0, codec: 'mp4a.40.2', + parsedCodec: 'mp4a.40.1', manifestCodec: 'mp4a.40.29', }); }); @@ -61,6 +62,7 @@ describe('getAudioConfig', function () { samplerate: 11025, channelCount: 0, codec: 'mp4a.40.5', + parsedCodec: 'mp4a.40.1', manifestCodec: 'mp4a.40.29', }); }); @@ -78,6 +80,7 @@ describe('getAudioConfig', function () { samplerate: 11025, channelCount: 0, codec: 'mp4a.40.2', + parsedCodec: 'mp4a.40.1', manifestCodec: 'mp4a.40.29', }); }); @@ -95,6 +98,7 @@ describe('getAudioConfig', function () { samplerate: 11025, channelCount: 0, codec: 'mp4a.40.5', + parsedCodec: 'mp4a.40.1', manifestCodec: 'mp4a.40.29', }); }); @@ -112,6 +116,7 @@ describe('getAudioConfig', function () { samplerate: 11025, channelCount: 0, codec: 'mp4a.40.5', + parsedCodec: 'mp4a.40.1', manifestCodec: undefined, }); }); @@ -129,6 +134,7 @@ describe('getAudioConfig', function () { samplerate: 64000, channelCount: 0, codec: 'mp4a.40.5', + parsedCodec: 'mp4a.40.1', manifestCodec: undefined, }); }); @@ -146,6 +152,7 @@ describe('getAudioConfig', function () { samplerate: 11025, channelCount: 0, codec: 'mp4a.40.5', + parsedCodec: 'mp4a.40.1', manifestCodec: 'mp4a.40.5', }); }); @@ -164,6 +171,7 @@ describe('getAudioConfig', function () { samplerate: 11025, channelCount: 1, codec: 'mp4a.40.2', + parsedCodec: 'mp4a.40.1', manifestCodec: 'mp4a.40.2', }); }); @@ -181,6 +189,7 @@ describe('getAudioConfig', function () { samplerate: 64000, channelCount: 0, codec: 'mp4a.40.2', + parsedCodec: 'mp4a.40.1', manifestCodec: 'mp4a.40.2', }); }); @@ -350,6 +359,7 @@ describe('initTrackConfig', function () { samplerate: 11025, channelCount: 0, codec: 'mp4a.40.5', + parsedCodec: 'mp4a.40.1', manifestCodec: 'mp4a.40.29', }); });