From f3db7f352dd79ae7cbe9a74754761a18c3d2e212 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Tue, 16 Sep 2025 11:53:44 +0200 Subject: [PATCH 1/2] fix(e2ee): continue NALU parsing when leading data is detected --- src/e2ee/worker/naluUtils.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/e2ee/worker/naluUtils.ts b/src/e2ee/worker/naluUtils.ts index 475a10418e..f3421523c0 100644 --- a/src/e2ee/worker/naluUtils.ts +++ b/src/e2ee/worker/naluUtils.ts @@ -280,7 +280,12 @@ function findNALUIndices(stream: Uint8Array): number[] { // save current NALU if (start === 0) { - if (end !== start) throw TypeError('byte stream contains leading data'); + // Skip leading data before first NALU - this can happen when the byte stream + // contains metadata or padding before the actual NALU data + if (end !== start) { + // Leading data detected, but we'll continue parsing from the next NALU + // instead of throwing an error to handle malformed streams gracefully + } } else { result.push(start); } From d57c7ab5c34a8ba9a0e27db747f90c59069fec2d Mon Sep 17 00:00:00 2001 From: lukasIO Date: Tue, 16 Sep 2025 13:59:49 +0200 Subject: [PATCH 2/2] improve fix logic --- src/e2ee/worker/naluUtils.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/e2ee/worker/naluUtils.ts b/src/e2ee/worker/naluUtils.ts index f3421523c0..9029ff927a 100644 --- a/src/e2ee/worker/naluUtils.ts +++ b/src/e2ee/worker/naluUtils.ts @@ -251,6 +251,7 @@ function findNALUIndices(stream: Uint8Array): number[] { let start = 0, pos = 0, searchLength = stream.length - 3; // Changed to -3 to handle 4-byte start codes + let isFirstNALU = true; while (pos < searchLength) { // skip until end of current NALU - check for both 3-byte and 4-byte start codes @@ -279,13 +280,17 @@ function findNALUIndices(stream: Uint8Array): number[] { while (end > start && stream[end - 1] === 0) end--; // save current NALU - if (start === 0) { - // Skip leading data before first NALU - this can happen when the byte stream - // contains metadata or padding before the actual NALU data - if (end !== start) { - // Leading data detected, but we'll continue parsing from the next NALU - // instead of throwing an error to handle malformed streams gracefully + if (isFirstNALU) { + // For the first NALU, check if there's leading data + if (start === 0 && end !== start) { + // Leading data detected - skip it and don't add to results + // This handles cases where the byte stream contains metadata or padding + // before the actual NALU data + } else if (start > 0) { + // First NALU starts after position 0 (normal case) + result.push(start); } + isFirstNALU = false; } else { result.push(start); }