Skip to content

Commit 7548d32

Browse files
committed
migrated(squash)
1 parent 806e213 commit 7548d32

File tree

5 files changed

+151
-108
lines changed

5 files changed

+151
-108
lines changed

globals.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ declare global {
1616
writable: WritableStream<RTCEncodedAudioFrame | RTCEncodedVideoFrame>;
1717
}
1818
}
19+
interface RTCRtpSender {
20+
createEncodedStreams?: () => {
21+
readable: ReadableStream<RTCEncodedAudioFrame | RTCEncodedVideoFrame>;
22+
writable: WritableStream<RTCEncodedAudioFrame | RTCEncodedVideoFrame>;
23+
}
24+
}
1925
}

modules/e2ee/E2EEContext.js renamed to modules/e2ee/E2EEContext.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ const kJitsiE2EE = Symbol('kJitsiE2EE');
2121
* - allow for the key to be rotated frequently.
2222
*/
2323
export default class E2EEcontext {
24+
private _worker: Worker;
25+
2426
/**
2527
* Build a new E2EE context instance, which will be used in a given conference.
28+
* @param {Object} [options] - The options object.
2629
* @param {boolean} [options.sharedKey] - whether there is a uniques key shared amoung all participants.
2730
*/
28-
constructor({ sharedKey } = {}) {
31+
constructor({ sharedKey }: { sharedKey?: boolean; } = {}) {
2932
// Determine the URL for the worker script. Relative URLs are relative to
3033
// the entry point, not the script that launches the worker.
3134
let baseUrl = '';
32-
const ljm = document.querySelector('script[src*="lib-jitsi-meet"]');
35+
const ljm = document.querySelector('script[src*="lib-jitsi-meet"]') as HTMLScriptElement;
3336

3437
if (ljm) {
3538
const idx = ljm.src.lastIndexOf('/');
@@ -67,7 +70,7 @@ export default class E2EEcontext {
6770
*
6871
* @param {string} participantId - The participant that just left.
6972
*/
70-
cleanup(participantId) {
73+
public cleanup(participantId: string): void {
7174
this._worker.postMessage({
7275
operation: 'cleanup',
7376
participantId
@@ -78,7 +81,7 @@ export default class E2EEcontext {
7881
* Cleans up all state associated with all participants in the conference. This is needed when disabling e2ee.
7982
*
8083
*/
81-
cleanupAll() {
84+
public cleanupAll(): void {
8285
this._worker.postMessage({
8386
operation: 'cleanupAll'
8487
});
@@ -92,7 +95,7 @@ export default class E2EEcontext {
9295
* @param {string} kind - The kind of track this receiver belongs to.
9396
* @param {string} participantId - The participant id that this receiver belongs to.
9497
*/
95-
handleReceiver(receiver, kind, participantId) {
98+
public handleReceiver(receiver: RTCRtpReceiver, kind: string, participantId: string): void {
9699
if (receiver[kJitsiE2EE]) {
97100
return;
98101
}
@@ -125,7 +128,7 @@ export default class E2EEcontext {
125128
* @param {string} kind - The kind of track this sender belongs to.
126129
* @param {string} participantId - The participant id that this sender belongs to.
127130
*/
128-
handleSender(sender, kind, participantId) {
131+
public handleSender(sender: RTCRtpSender, kind: string, participantId: string): void {
129132
if (sender[kJitsiE2EE]) {
130133
return;
131134
}
@@ -155,7 +158,7 @@ export default class E2EEcontext {
155158
*
156159
* @param {boolean} enabled - whether E2EE is enabled or not.
157160
*/
158-
setEnabled(enabled) {
161+
public setEnabled(enabled: boolean): void {
159162
this._worker.postMessage({
160163
enabled,
161164
operation: 'setEnabled'
@@ -169,7 +172,7 @@ export default class E2EEcontext {
169172
* @param {Uint8Array | boolean} key - they key for the given participant.
170173
* @param {Number} keyIndex - the key index.
171174
*/
172-
setKey(participantId, key, keyIndex) {
175+
public setKey(participantId: string, key: Uint8Array | boolean, keyIndex: number): void {
173176
this._worker.postMessage({
174177
key,
175178
keyIndex,

modules/e2ee/ExternallyManagedKeyHandler.js renamed to modules/e2ee/ExternallyManagedKeyHandler.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import JitsiConference from '../../JitsiConference';
2+
13
import { KeyHandler } from './KeyHandler';
24

35
/**
@@ -8,7 +10,7 @@ export class ExternallyManagedKeyHandler extends KeyHandler {
810
* Build a new ExternallyManagedKeyHandler instance, which will be used in a given conference.
911
* @param conference - the current conference.
1012
*/
11-
constructor(conference) {
13+
constructor(conference: JitsiConference) {
1214
super(conference, { sharedKey: true });
1315
}
1416

@@ -19,7 +21,9 @@ export class ExternallyManagedKeyHandler extends KeyHandler {
1921
* @param {Number} [keyInfo.index] - the index of the encryption key.
2022
* @returns {void}
2123
*/
22-
setKey(keyInfo) {
23-
this.e2eeCtx.setKey(undefined, { encryptionKey: keyInfo.encryptionKey }, keyInfo.index);
24+
public async setKey(keyInfo: { encryptionKey: CryptoKey; index: number; }) {
25+
const keyData = await crypto.subtle.exportKey('raw', keyInfo.encryptionKey);
26+
27+
this.e2eeCtx.setKey(undefined, new Uint8Array(keyData), keyInfo.index);
2428
}
2529
}
Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
11
import { getLogger } from '@jitsi/logger';
22

3+
import JitsiConference from '../../JitsiConference';
34
import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
45
import RTCEvents from '../../service/RTC/RTCEvents';
6+
import JitsiLocalTrack from '../RTC/JitsiLocalTrack';
7+
import JitsiRemoteTrack from '../RTC/JitsiRemoteTrack';
8+
import TraceablePeerConnection from '../RTC/TraceablePeerConnection';
59
import browser from '../browser';
610
import Deferred from '../util/Deferred';
711
import Listenable from '../util/Listenable';
12+
import JingleSessionPC from '../xmpp/JingleSessionPC';
813

914
import E2EEContext from './E2EEContext';
1015

1116
const logger = getLogger('modules/e2ee/KeyHandler');
1217

18+
/**
19+
* Options for the KeyHandler constructor.
20+
*/
21+
export interface IKeyHandlerOptions {
22+
sharedKey?: boolean;
23+
}
24+
25+
1326
/**
1427
* Abstract class that integrates {@link E2EEContext} with a key management system.
1528
*/
1629
export class KeyHandler extends Listenable {
30+
protected conference: JitsiConference;
31+
protected e2eeCtx: E2EEContext;
32+
protected enabled: boolean;
33+
protected _enabling?: Deferred<void>;
34+
protected _firstEnable: boolean;
35+
protected _setEnabled?: (enabled: boolean) => Promise<void>;
36+
1737
/**
1838
* Build a new KeyHandler instance, which will be used in a given conference.
1939
* @param {JitsiConference} conference - the current conference.
2040
* @param {object} options - the options passed to {E2EEContext}, see implemention.
2141
*/
22-
constructor(conference, options = {}) {
42+
constructor(conference: JitsiConference, options: IKeyHandlerOptions = {}) {
2343
super();
2444

2545
this.conference = conference;
@@ -39,63 +59,21 @@ export class KeyHandler extends Listenable {
3959
this._onMediaSessionStarted.bind(this));
4060
this.conference.on(
4161
JitsiConferenceEvents.TRACK_ADDED,
42-
track => track.isLocal() && this._onLocalTrackAdded(track));
62+
(track: JitsiLocalTrack | JitsiRemoteTrack) => track.isLocal() && this._onLocalTrackAdded(track as JitsiLocalTrack));
4363
this.conference.rtc.on(
4464
RTCEvents.REMOTE_TRACK_ADDED,
45-
(track, tpc) => this._setupReceiverE2EEForTrack(tpc, track));
65+
(track: JitsiRemoteTrack, tpc: TraceablePeerConnection) => this._setupReceiverE2EEForTrack(tpc, track));
4666
this.conference.on(
4767
JitsiConferenceEvents.TRACK_MUTE_CHANGED,
4868
this._trackMuteChanged.bind(this));
4969
}
5070

51-
/**
52-
* Indicates whether E2EE is currently enabled or not.
53-
*
54-
* @returns {boolean}
55-
*/
56-
isEnabled() {
57-
return this.enabled;
58-
}
59-
60-
/**
61-
* Enables / disables End-To-End encryption.
62-
*
63-
* @param {boolean} enabled - whether E2EE should be enabled or not.
64-
* @returns {void}
65-
*/
66-
async setEnabled(enabled) {
67-
this._enabling && await this._enabling;
68-
69-
if (enabled === this.enabled) {
70-
return;
71-
}
72-
73-
this._enabling = new Deferred();
74-
75-
this.enabled = enabled;
76-
77-
this._setEnabled && await this._setEnabled(enabled);
78-
79-
this.conference.setLocalParticipantProperty('e2ee.enabled', enabled);
80-
81-
// Only restart media sessions if E2EE is enabled. If it's later disabled
82-
// we'll continue to use the existing media sessions with an empty transform.
83-
if (!this._firstEnable && enabled) {
84-
this._firstEnable = true;
85-
this.conference._restartMediaSessions();
86-
}
87-
88-
this.e2eeCtx.setEnabled(enabled);
89-
90-
this._enabling.resolve();
91-
}
92-
9371
/**
9472
* Setup E2EE on the new track that has been added to the conference, apply it on all the open peerconnections.
9573
* @param {JitsiLocalTrack} track - the new track that's being added to the conference.
9674
* @private
9775
*/
98-
_onLocalTrackAdded(track) {
76+
private _onLocalTrackAdded(track: JitsiLocalTrack): void {
9977
for (const session of this.conference.getMediaSessions()) {
10078
this._setupSenderE2EEForTrack(session, track);
10179
}
@@ -106,7 +84,7 @@ export class KeyHandler extends Listenable {
10684
* @param {JingleSessionPC} session - the new media session.
10785
* @private
10886
*/
109-
_onMediaSessionStarted(session) {
87+
private _onMediaSessionStarted(session: JingleSessionPC): void {
11088
const localTracks = this.conference.getLocalTracks();
11189

11290
for (const track of localTracks) {
@@ -119,7 +97,7 @@ export class KeyHandler extends Listenable {
11997
*
12098
* @private
12199
*/
122-
_setupReceiverE2EEForTrack(tpc, track) {
100+
private _setupReceiverE2EEForTrack(tpc: TraceablePeerConnection, track: JitsiRemoteTrack): void {
123101
if (!this.enabled && !this._firstEnable) {
124102
return;
125103
}
@@ -140,13 +118,13 @@ export class KeyHandler extends Listenable {
140118
* @param {JitsiLocalTrack} track - the local track for which e2e encoder will be configured.
141119
* @private
142120
*/
143-
_setupSenderE2EEForTrack(session, track) {
121+
private _setupSenderE2EEForTrack(session: JingleSessionPC, track: JitsiLocalTrack): void {
144122
if (!this.enabled && !this._firstEnable) {
145123
return;
146124
}
147125

148126
const pc = session.peerconnection;
149-
const sender = pc && pc.findSenderForTrack(track.track);
127+
const sender = pc?.findSenderForTrack(track.track);
150128

151129
if (sender) {
152130
this.e2eeCtx.handleSender(sender, track.getType(), track.getParticipantId());
@@ -160,11 +138,54 @@ export class KeyHandler extends Listenable {
160138
* @param {JitsiLocalTrack} track - the track for which muted status has changed.
161139
* @private
162140
*/
163-
_trackMuteChanged(track) {
141+
private _trackMuteChanged(track: JitsiLocalTrack): void {
164142
if (browser.doesVideoMuteByStreamRemove() && track.isLocal() && track.isVideoTrack() && !track.isMuted()) {
165143
for (const session of this.conference.getMediaSessions()) {
166-
this._setupSenderE2EEForTrack(session, track);
144+
this._setupSenderE2EEForTrack(session, track as JitsiLocalTrack);
167145
}
168146
}
169147
}
148+
149+
/**
150+
* Indicates whether E2EE is currently enabled or not.
151+
*
152+
* @returns {boolean}
153+
*/
154+
public isEnabled(): boolean {
155+
return this.enabled;
156+
}
157+
158+
/**
159+
* Enables / disables End-To-End encryption.
160+
*
161+
* @param {boolean} enabled - whether E2EE should be enabled or not.
162+
* @returns {void}
163+
*/
164+
public async setEnabled(enabled: boolean): Promise<void> {
165+
this._enabling && await this._enabling;
166+
167+
if (enabled === this.enabled) {
168+
return;
169+
}
170+
171+
this._enabling = new Deferred<void>();
172+
173+
this.enabled = enabled;
174+
175+
this._setEnabled && await this._setEnabled(enabled);
176+
177+
this.conference.setLocalParticipantProperty('e2ee.enabled', enabled.toString());
178+
179+
// Only restart media sessions if E2EE is enabled. If it's later disabled
180+
// we'll continue to use the existing media sessions with an empty transform.
181+
if (!this._firstEnable && enabled) {
182+
this._firstEnable = true;
183+
this.conference._restartMediaSessions();
184+
}
185+
186+
this.e2eeCtx.setEnabled(enabled);
187+
188+
this._enabling.resolve();
189+
}
190+
170191
}

0 commit comments

Comments
 (0)