@@ -80,6 +80,9 @@ export class EncryptionManager implements IEncryptionManager {
80
80
// if it looks like a membership has been updated.
81
81
private lastMembershipFingerprints : Set < string > | undefined ;
82
82
83
+ // TODO: remove this value since I think this is not helpful to use
84
+ // it represents what index we actually sent over to ElementCall (which we do in a delayed manner)
85
+ // but it has no releavnt information for the encryption manager.
83
86
private mediaTrailerKeyIndexInUse = - 1 ;
84
87
private latestGeneratedKeyIndex = - 1 ;
85
88
private joinConfig : EncryptionConfig | undefined ;
@@ -267,6 +270,12 @@ export class EncryptionManager implements IEncryptionManager {
267
270
}
268
271
269
272
const keyIndexToSend = indexToSend ?? this . latestGeneratedKeyIndex ;
273
+ // TODO remove this debug log. it just shows then when sending mediaTrailerKeyIndexInUse contained the wrong index.
274
+ logger . debug (
275
+ `Compare key in use to last generated key\n` ,
276
+ `latestGeneratedKeyIndex: ${ this . latestGeneratedKeyIndex } \n` ,
277
+ `mediaTrailerKeyIndexInUse: ${ this . mediaTrailerKeyIndexInUse } ` ,
278
+ ) ;
270
279
logger . info (
271
280
`Try sending encryption keys event. keyIndexToSend=${ keyIndexToSend } (method parameter: ${ indexToSend } )` ,
272
281
) ;
@@ -359,7 +368,15 @@ export class EncryptionManager implements IEncryptionManager {
359
368
}
360
369
}
361
370
362
- this . latestGeneratedKeyIndex = encryptionKeyIndex ;
371
+ if ( userId === this . userId && deviceId === this . deviceId ) {
372
+ // It is important to already update the latestGeneratedKeyIndex here
373
+ // NOT IN THE `delayBeforeUse` `setTimeout`.
374
+ // Even though this is where we call onEncryptionKeysChanged and set the key in EC (and livekit).
375
+ // It needs to happen here because we will send the key before the timeout has passed and sending
376
+ // the key will use latestGeneratedKeyIndex as the index. if we update it in the `setTimeout` callback
377
+ // it will use the wrong index (index - 1)!
378
+ this . latestGeneratedKeyIndex = encryptionKeyIndex ;
379
+ }
363
380
participantKeys [ encryptionKeyIndex ] = {
364
381
key : keyBin ,
365
382
timestamp,
@@ -372,14 +389,14 @@ export class EncryptionManager implements IEncryptionManager {
372
389
if ( userId === this . userId && deviceId === this . deviceId ) {
373
390
this . mediaTrailerKeyIndexInUse = encryptionKeyIndex ;
374
391
}
375
- this . onEncryptionKeysChanged ( keyBin , this . mediaTrailerKeyIndexInUse , participantId ) ;
392
+ this . onEncryptionKeysChanged ( keyBin , encryptionKeyIndex , participantId ) ;
376
393
} , this . useKeyDelay ) ;
377
394
this . setNewKeyTimeouts . add ( useKeyTimeout ) ;
378
395
} else {
379
396
if ( userId === this . userId && deviceId === this . deviceId ) {
380
397
this . mediaTrailerKeyIndexInUse = encryptionKeyIndex ;
381
398
}
382
- this . onEncryptionKeysChanged ( keyBin , this . mediaTrailerKeyIndexInUse , participantId ) ;
399
+ this . onEncryptionKeysChanged ( keyBin , encryptionKeyIndex , participantId ) ;
383
400
}
384
401
}
385
402
0 commit comments