Skip to content

Commit 0fe824b

Browse files
authored
Support swapping out the E2EEManager for RN (#1345)
* Support swapping out the E2EEManager for RN * changeset * formatting
1 parent 5ecc1d9 commit 0fe824b

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

.changeset/dirty-carpets-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'livekit-client': patch
3+
---
4+
5+
Support swapping out the E2EEManager for react-native

src/e2ee/E2eeManager.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type { BaseKeyProvider } from './KeyProvider';
1616
import { E2EE_FLAG } from './constants';
1717
import { type E2EEManagerCallbacks, EncryptionEvent, KeyProviderEvent } from './events';
1818
import type {
19-
E2EEOptions,
19+
E2EEManagerOptions,
2020
E2EEWorkerMessage,
2121
EnableMessage,
2222
EncodeMessage,
@@ -31,10 +31,21 @@ import type {
3131
} from './types';
3232
import { isE2EESupported, isScriptTransformSupported } from './utils';
3333

34+
export interface BaseE2EEManager {
35+
setup(room: Room): void;
36+
setupEngine(engine: RTCEngine): void;
37+
setParticipantCryptorEnabled(enabled: boolean, participantIdentity: string): void;
38+
setSifTrailer(trailer: Uint8Array): void;
39+
on<E extends keyof E2EEManagerCallbacks>(event: E, listener: E2EEManagerCallbacks[E]): this;
40+
}
41+
3442
/**
3543
* @experimental
3644
*/
37-
export class E2EEManager extends (EventEmitter as new () => TypedEventEmitter<E2EEManagerCallbacks>) {
45+
export class E2EEManager
46+
extends (EventEmitter as new () => TypedEventEmitter<E2EEManagerCallbacks>)
47+
implements BaseE2EEManager
48+
{
3849
protected worker: Worker;
3950

4051
protected room?: Room;
@@ -43,7 +54,7 @@ export class E2EEManager extends (EventEmitter as new () => TypedEventEmitter<E2
4354

4455
private keyProvider: BaseKeyProvider;
4556

46-
constructor(options: E2EEOptions) {
57+
constructor(options: E2EEManagerOptions) {
4758
super();
4859
this.keyProvider = options.keyProvider;
4960
this.worker = options.worker;

src/e2ee/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { LogLevel } from '../logger';
22
import type { VideoCodec } from '../room/track/options';
3+
import type { BaseE2EEManager } from './E2eeManager';
34
import type { BaseKeyProvider } from './KeyProvider';
45

56
export interface BaseMessage {
@@ -137,10 +138,16 @@ export type KeyInfo = {
137138
keyIndex?: number;
138139
};
139140

140-
export type E2EEOptions = {
141+
export type E2EEManagerOptions = {
141142
keyProvider: BaseKeyProvider;
142143
worker: Worker;
143144
};
145+
export type E2EEOptions =
146+
| E2EEManagerOptions
147+
| {
148+
/** For react-native usage. */
149+
e2eeManager: BaseE2EEManager;
150+
};
144151

145152
export type DecodeRatchetOptions = {
146153
/** attempts */

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export { RpcError, type RpcInvocationData, type PerformRpcParams } from './room/
4545
export * from './connectionHelper/ConnectionCheck';
4646
export * from './connectionHelper/checks/Checker';
4747
export * from './e2ee';
48+
export type { BaseE2EEManager } from './e2ee/E2eeManager';
4849
export * from './options';
4950
export * from './room/errors';
5051
export * from './room/events';

src/room/Room.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { EventEmitter } from 'events';
3333
import type TypedEmitter from 'typed-emitter';
3434
import 'webrtc-adapter';
3535
import { EncryptionEvent } from '../e2ee';
36-
import { E2EEManager } from '../e2ee/E2eeManager';
36+
import { type BaseE2EEManager, E2EEManager } from '../e2ee/E2eeManager';
3737
import log, { LoggerNames, getLogger } from '../logger';
3838
import type {
3939
InternalRoomConnectOptions,
@@ -158,7 +158,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
158158

159159
private disconnectLock: Mutex;
160160

161-
private e2eeManager: E2EEManager | undefined;
161+
private e2eeManager: BaseE2EEManager | undefined;
162162

163163
private connectionReconcileInterval?: ReturnType<typeof setInterval>;
164164

@@ -252,7 +252,11 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
252252

253253
private setupE2EE() {
254254
if (this.options.e2ee) {
255-
this.e2eeManager = new E2EEManager(this.options.e2ee);
255+
if ('e2eeManager' in this.options.e2ee) {
256+
this.e2eeManager = this.options.e2ee.e2eeManager;
257+
} else {
258+
this.e2eeManager = new E2EEManager(this.options.e2ee);
259+
}
256260
this.e2eeManager.on(
257261
EncryptionEvent.ParticipantEncryptionStatusChanged,
258262
(enabled, participant) => {

0 commit comments

Comments
 (0)