From 82690ef32781e2890fa71acd88da5cc4b7589527 Mon Sep 17 00:00:00 2001 From: Vanilagy <1696106+Vanilagy@users.noreply.github.com> Date: Tue, 2 Jun 2026 13:06:41 +0200 Subject: [PATCH] Clean up AVC packet NALUs with AUD in them for Chromium key frame detection (fixes #396) --- src/media-sink.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/media-sink.ts b/src/media-sink.ts index fda943f..c245b16 100644 --- a/src/media-sink.ts +++ b/src/media-sink.ts @@ -8,6 +8,7 @@ import { parsePcmCodec, PCM_AUDIO_CODECS, PcmAudioCodec, VideoCodec, AudioCodec } from './codec'; import { + AvcNalUnitType, concatAvcNalUnits, deserializeAvcDecoderConfigurationRecord, determineVideoPacketType, @@ -997,6 +998,14 @@ class VideoDecoderWrapper extends DecoderWrapper { for (const loc of iterateAvcNalUnits(packet.data, this.decoderConfig)) { const type = extractNalUnitTypeForAvc(packet.data[loc.offset]!); + + if (type === AvcNalUnitType.AUD) { + // If packets contain an AUD and have NALUs before it, this trips up Chromium's key frame + // detector. Clear the NALUs if an AUD is encountered. + // https://github.com/Vanilagy/mediabunny/issues/396 + filteredNalUnits.length = 0; + } + // These trip up Chromium's key frame detection, so let's strip them if (!(type >= 20 && type <= 31)) { filteredNalUnits.push(packet.data.subarray(loc.offset, loc.offset + loc.length));