11import { getLogger } from '@jitsi/logger' ;
22
3+ import JitsiConference from '../../JitsiConference' ;
34import * as JitsiConferenceEvents from '../../JitsiConferenceEvents' ;
45import RTCEvents from '../../service/RTC/RTCEvents' ;
6+ import JitsiLocalTrack from '../RTC/JitsiLocalTrack' ;
7+ import JitsiRemoteTrack from '../RTC/JitsiRemoteTrack' ;
8+ import TraceablePeerConnection from '../RTC/TraceablePeerConnection' ;
59import browser from '../browser' ;
610import Deferred from '../util/Deferred' ;
711import Listenable from '../util/Listenable' ;
12+ import JingleSessionPC from '../xmpp/JingleSessionPC' ;
813
914import E2EEContext from './E2EEContext' ;
1015
1116const 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 */
1629export 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