diff --git a/.changeset/faster-write-rbsp.md b/.changeset/faster-write-rbsp.md new file mode 100644 index 0000000000..1e3224b955 --- /dev/null +++ b/.changeset/faster-write-rbsp.md @@ -0,0 +1,5 @@ +--- +'livekit-client': patch +--- + +Speed up the emulation prevention step for encrypted H.264 and H.265 frames. `writeRbsp` now returns the input unchanged when no byte needs escaping, and otherwise copies runs of bytes instead of building a JS array. The output does not change. diff --git a/src/e2ee/utils.test.ts b/src/e2ee/utils.test.ts new file mode 100644 index 0000000000..9552a97d44 --- /dev/null +++ b/src/e2ee/utils.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'vitest'; +import { writeRbsp } from './utils'; + +describe('writeRbsp', () => { + it('inserts an emulation prevention byte before every byte <= 3 after two zeros', () => { + const cases: Array<[number[], number[]]> = [ + // No escapes. + [[], []], + [ + [0x00, 0x04, 0x00, 0x00, 0x04, 0xff], + [0x00, 0x04, 0x00, 0x00, 0x04, 0xff], + ], + // One escape. + [ + [0x11, 0x00, 0x00, 0x01, 0x22], + [0x11, 0x00, 0x00, 0x03, 0x01, 0x22], + ], + // Consecutive zero runs: the zero count resets after each escape. + [ + [0x00, 0x00, 0x00, 0x00, 0x00, 0x02], + [0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x02], + ], + [ + [0x00, 0x00, 0x03, 0x00, 0x00, 0x00], + [0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x03, 0x00], + ], + // Trailing zeros are not escaped, because no byte follows them. + [ + [0xaa, 0x00, 0x00], + [0xaa, 0x00, 0x00], + ], + [ + [0xaa, 0x00, 0x00, 0x00, 0x00], + [0xaa, 0x00, 0x00, 0x03, 0x00, 0x00], + ], + ]; + + for (const [input, expected] of cases) { + expect(writeRbsp(new Uint8Array(input))).toEqual(new Uint8Array(expected)); + } + }); + + it('returns the input itself when nothing needs escaping', () => { + const input = new Uint8Array([0x00, 0x00, 0x04, 0xff]); + expect(writeRbsp(input)).toBe(input); + }); +}); diff --git a/src/e2ee/utils.ts b/src/e2ee/utils.ts index 855d1eedef..96ed49628d 100644 --- a/src/e2ee/utils.ts +++ b/src/e2ee/utils.ts @@ -161,24 +161,36 @@ export function parseRbsp(stream: NonSharedUint8Array): NonSharedUint8Array { const kZerosInStartSequence = 2; const kEmulationByte = 3; -export function writeRbsp(data_in: NonSharedUint8Array): NonSharedUint8Array { - const dataOut: number[] = []; - var numConsecutiveZeros = 0; - for (var i = 0; i < data_in.length; ++i) { - var byte = data_in[i]; +/** + * Inserts an emulation prevention byte before every byte <= 3 that follows two zeros. + * Returns `dataIn` itself when nothing needs escaping, which is the case for almost + * every encrypted frame. Callers must not mutate the result in place. + */ +export function writeRbsp(dataIn: NonSharedUint8Array): NonSharedUint8Array { + // Indices of the bytes that need an emulation byte in front of them. + const escapes: number[] = []; + let numConsecutiveZeros = 0; + for (let i = 0; i < dataIn.length; ++i) { + const byte = dataIn[i]; if (byte <= kEmulationByte && numConsecutiveZeros >= kZerosInStartSequence) { - // Need to escape. - dataOut.push(kEmulationByte); - numConsecutiveZeros = 0; - } - dataOut.push(byte); - if (byte == 0) { - ++numConsecutiveZeros; - } else { + escapes.push(i); numConsecutiveZeros = 0; } + numConsecutiveZeros = byte === 0 ? numConsecutiveZeros + 1 : 0; } - return new Uint8Array(dataOut); + if (escapes.length === 0) { + return dataIn; + } + const dataOut = new Uint8Array(dataIn.length + escapes.length); + let from = 0; + for (let k = 0; k < escapes.length; ++k) { + // Shifted by k for the emulation bytes already written. + dataOut.set(dataIn.subarray(from, escapes[k]), from + k); + dataOut[escapes[k] + k] = kEmulationByte; + from = escapes[k]; + } + dataOut.set(dataIn.subarray(from), from + escapes.length); + return dataOut; } export function asEncryptablePacket(packet: DataPacket): EncryptedPacketPayload | undefined {