Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/faster-write-rbsp.md
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 47 additions & 0 deletions src/e2ee/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
40 changes: 26 additions & 14 deletions src/e2ee/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading