Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2EE support for Opus RED #866

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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: 0 additions & 5 deletions .changeset/gorgeous-cameras-peel.md

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@bufbuild/protobuf": "^1.3.0",
"events": "^3.3.0",
"loglevel": "^1.8.0",
"opus-red-parser": "^1.0.0",
"sdp-transform": "^2.14.1",
"ts-debounce": "^4.0.0",
"typed-emitter": "^2.1.0",
Expand Down
21 changes: 13 additions & 8 deletions src/e2ee/E2eeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EngineEvent, ParticipantEvent, RoomEvent } from '../room/events';
import LocalTrack from '../room/track/LocalTrack';
import type RemoteTrack from '../room/track/RemoteTrack';
import type { Track } from '../room/track/Track';
import type { VideoCodec } from '../room/track/options';
import type { AudioCodec, VideoCodec } from '../room/track/options';
import type { BaseKeyProvider } from './KeyProvider';
import { E2EE_FLAG } from './constants';
import { type E2EEManagerCallbacks, EncryptionEvent, KeyProviderEvent } from './events';
Expand All @@ -21,14 +21,14 @@ import type {
EncodeMessage,
InitMessage,
KeyInfo,
RTPVideoMapMessage,
RTPMapMessage,
RatchetRequestMessage,
RemoveTransformMessage,
SetKeyMessage,
SifTrailerMessage,
UpdateCodecMessage,
} from './types';
import { isE2EESupported, isScriptTransformSupported, mimeTypeToVideoCodecString } from './utils';
import { isE2EESupported, isScriptTransformSupported, mimeTypeToCodecString } from './utils';

/**
* @experimental
Expand Down Expand Up @@ -155,6 +155,9 @@ export class E2EEManager extends (EventEmitter as new () => TypedEventEmitter<E2
engine.on(EngineEvent.RTPVideoMapUpdate, (rtpMap) => {
this.postRTPMap(rtpMap);
});
engine.on(EngineEvent.RTPAudioMapUpdate, (rtpMap) => {
this.postRTPMap(rtpMap);
});
}

private setupEventListeners(room: Room, keyProvider: BaseKeyProvider) {
Expand Down Expand Up @@ -203,6 +206,8 @@ export class E2EEManager extends (EventEmitter as new () => TypedEventEmitter<E2
});
});
room.localParticipant.on(ParticipantEvent.LocalTrackPublished, async (publication) => {
console.log('sender info', publication.trackInfo);

this.setupE2EESender(publication.track!, publication.track!.sender!);
});

Expand Down Expand Up @@ -258,14 +263,14 @@ export class E2EEManager extends (EventEmitter as new () => TypedEventEmitter<E2
}
}

private postRTPMap(map: Map<number, VideoCodec>) {
private postRTPMap(map: Map<number, VideoCodec> | Map<number, AudioCodec>) {
if (!this.worker) {
throw TypeError('could not post rtp map, worker is missing');
}
if (!this.room?.localParticipant.identity) {
throw TypeError('could not post rtp map, local participant identity is missing');
}
const msg: RTPVideoMapMessage = {
const msg: RTPMapMessage = {
kind: 'setRTPMap',
data: {
map,
Expand Down Expand Up @@ -299,7 +304,7 @@ export class E2EEManager extends (EventEmitter as new () => TypedEventEmitter<E2
track.receiver,
track.mediaStreamID,
remoteId,
track.kind === 'video' ? mimeTypeToVideoCodecString(trackInfo.mimeType) : undefined,
mimeTypeToCodecString(trackInfo.mimeType),
);
}

Expand All @@ -320,7 +325,7 @@ export class E2EEManager extends (EventEmitter as new () => TypedEventEmitter<E2
receiver: RTCRtpReceiver,
trackId: string,
participantIdentity: string,
codec?: VideoCodec,
codec?: VideoCodec | AudioCodec,
) {
if (!this.worker) {
return;
Expand Down Expand Up @@ -386,7 +391,7 @@ export class E2EEManager extends (EventEmitter as new () => TypedEventEmitter<E2
* a frame encoder.
*
*/
private handleSender(sender: RTCRtpSender, trackId: string, codec?: VideoCodec) {
private handleSender(sender: RTCRtpSender, trackId: string, codec?: VideoCodec | AudioCodec) {
if (E2EE_FLAG in sender || !this.worker) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/e2ee/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const UNENCRYPTED_BYTES = {
key: 10,
delta: 3,
audio: 1, // frame.type is not set on audio, so this is set manually
red1: 5, // opus red with 1 redundancy frame
empty: 0,
} as const;

Expand Down
12 changes: 6 additions & 6 deletions src/e2ee/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VideoCodec } from '../room/track/options';
import type { AudioCodec, VideoCodec } from '../room/track/options';
import type { BaseKeyProvider } from './KeyProvider';

export interface BaseMessage {
Expand All @@ -23,10 +23,10 @@ export interface SetKeyMessage extends BaseMessage {
};
}

export interface RTPVideoMapMessage extends BaseMessage {
export interface RTPMapMessage extends BaseMessage {
kind: 'setRTPMap';
data: {
map: Map<number, VideoCodec>;
map: Map<number, VideoCodec> | Map<number, AudioCodec>;
participantIdentity: string;
};
}
Expand All @@ -45,7 +45,7 @@ export interface EncodeMessage extends BaseMessage {
readableStream: ReadableStream;
writableStream: WritableStream;
trackId: string;
codec?: VideoCodec;
codec?: VideoCodec | AudioCodec;
};
}

Expand All @@ -62,7 +62,7 @@ export interface UpdateCodecMessage extends BaseMessage {
data: {
participantIdentity: string;
trackId: string;
codec: VideoCodec;
codec: VideoCodec | AudioCodec;
};
}

Expand Down Expand Up @@ -112,7 +112,7 @@ export type E2EEWorkerMessage =
| ErrorMessage
| EnableMessage
| RemoveTransformMessage
| RTPVideoMapMessage
| RTPMapMessage
| UpdateCodecMessage
| RatchetRequestMessage
| RatchetMessage
Expand Down
14 changes: 7 additions & 7 deletions src/e2ee/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { videoCodecs } from '../room/track/options';
import type { VideoCodec } from '../room/track/options';
import type { AudioCodec, VideoCodec } from '../room/track/options';
import { isAudioCodec, isVideoCodec } from '../room/utils';
import { ENCRYPTION_ALGORITHM } from './constants';

export function isE2EESupported() {
Expand Down Expand Up @@ -116,12 +116,12 @@ export function createE2EEKey(): Uint8Array {
return window.crypto.getRandomValues(new Uint8Array(32));
}

export function mimeTypeToVideoCodecString(mimeType: string) {
const codec = mimeType.split('/')[1].toLowerCase() as VideoCodec;
if (!videoCodecs.includes(codec)) {
throw Error(`Video codec not supported: ${codec}`);
export function mimeTypeToCodecString(mimeType: string) {
const codec = mimeType.split('/')[1].toLowerCase() as VideoCodec | AudioCodec;
if (isVideoCodec(codec) || isAudioCodec(codec)) {
return codec;
}
return codec;
throw Error(`Codec not supported: ${codec}`);
}

/**
Expand Down
Loading
Loading