Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/e2ee/E2eeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import { isE2EESupported, isScriptTransformSupported } from './utils';

export interface BaseE2EEManager {
setup(room: Room): void;
setupEngine(engine: RTCEngine): void;
setParticipantCryptorEnabled(enabled: boolean, participantIdentity: string): void;
setSifTrailer(trailer: Uint8Array): void;
on<E extends keyof E2EEManagerCallbacks>(event: E, listener: E2EEManagerCallbacks[E]): this;
Expand Down Expand Up @@ -174,6 +173,9 @@ export class E2EEManager
engine.on(EngineEvent.RTPVideoMapUpdate, (rtpMap) => {
this.postRTPMap(rtpMap);
});
engine.on(EngineEvent.SupersededBy, (newEngine) => {
this.setupEngine(newEngine);
});
}

private setupEventListeners(room: Room, keyProvider: BaseKeyProvider) {
Expand Down
5 changes: 4 additions & 1 deletion src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ enum PCState {
Closed,
}

const EngineEventEmitter = EventEmitter as new () => TypedEventEmitter<EngineEventCallbacks>;

/** @internal */
export default class RTCEngine extends (EventEmitter as new () => TypedEventEmitter<EngineEventCallbacks>) {
export default class RTCEngine extends EngineEventEmitter {
Comment on lines 96 to +100
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admittedly this is a little weird - EngineEventCallbacks now contains a reference to RTCEngine, so putting this expression directly within the extends creates a reference cycle that typescript doesn't seem to like. Breaking it up like this seems to solve that problem, though.

client: SignalClient;

rtcConfig: RTCConfiguration = {};
Expand Down Expand Up @@ -1580,6 +1582,7 @@ export type EngineEventCallbacks = {
offline: () => void;
signalRequestResponse: (response: RequestResponse) => void;
signalConnected: (joinResp: JoinResponse) => void;
supersededBy: (newEngine: RTCEngine) => void;
};

function supportOptionalDatachannel(protocol: number | undefined): boolean {
Expand Down
8 changes: 2 additions & 6 deletions src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
return;
}

const oldEngine = this.engine;
this.engine = new RTCEngine(this.options);

this.engine
Expand Down Expand Up @@ -610,12 +611,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
}
});

if (this.localParticipant) {
this.localParticipant.setupEngine(this.engine);
}
if (this.e2eeManager) {
this.e2eeManager.setupEngine(this.engine);
}
oldEngine?.emit(EngineEvent.SupersededBy, this.engine);
}
Comment on lines 613 to 615
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, n function calls were required. Now, only one publish does the same thing!


/**
Expand Down
1 change: 1 addition & 0 deletions src/room/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ export enum EngineEvent {
SignalRequestResponse = 'signalRequestResponse',
SignalConnected = 'signalConnected',
RoomMoved = 'roomMoved',
SupersededBy = 'supersededBy',
}

export enum TrackEvent {
Expand Down
9 changes: 7 additions & 2 deletions src/room/participant/LocalParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export default class LocalParticipant extends Participant {
/**
* @internal
*/
setupEngine(engine: RTCEngine) {
private setupEngine(engine: RTCEngine) {
this.engine = engine;
this.engine.on(EngineEvent.RemoteMute, (trackSid: string, muted: boolean) => {
const pub = this.trackPublications.get(trackSid);
Expand Down Expand Up @@ -263,7 +263,8 @@ export default class LocalParticipant extends Participant {
.on(EngineEvent.SubscribedQualityUpdate, this.handleSubscribedQualityUpdate)
.on(EngineEvent.Closing, this.handleClosing)
.on(EngineEvent.SignalRequestResponse, this.handleSignalRequestResponse)
.on(EngineEvent.DataPacketReceived, this.handleDataPacket);
.on(EngineEvent.DataPacketReceived, this.handleDataPacket)
.on(EngineEvent.SupersededBy, this.handleNewEngine);
}

private handleReconnecting = () => {
Expand Down Expand Up @@ -337,6 +338,10 @@ export default class LocalParticipant extends Participant {
}
};

private handleNewEngine = (newEngine: RTCEngine) => {
this.setupEngine(newEngine);
};

/**
* Sets and updates the metadata of the local participant.
* Note: this requires `canUpdateOwnMetadata` permission.
Expand Down
Loading