@@ -23,6 +23,11 @@ import { logger as rootLogger, type Logger } from "../logger.ts";
2323import { sleep } from "../utils.ts" ;
2424import type { InboundEncryptionSession , ParticipantDeviceInfo , ParticipantId , Statistics } from "./types.ts" ;
2525import { getParticipantId , KeyBuffer } from "./utils.ts" ;
26+ import {
27+ type EnabledTransports ,
28+ RoomAndToDeviceEvents ,
29+ RoomAndToDeviceTransport ,
30+ } from "./RoomAndToDeviceKeyTransport.ts" ;
2631
2732type OutboundEncryptionSession = {
2833 key : Uint8Array ;
@@ -95,6 +100,11 @@ export class RTCEncryptionManager implements IEncryptionManager {
95100 this . logger . info ( `Joining room` ) ;
96101 this . delayRolloutTimeMillis = joinConfig ?. useKeyDelay ?? 1000 ;
97102 this . transport . on ( KeyTransportEvents . ReceivedKeys , this . onNewKeyReceived ) ;
103+ // Deprecate RoomKeyTransport: this can get removed.
104+ if ( this . transport instanceof RoomAndToDeviceTransport ) {
105+ this . transport . on ( RoomAndToDeviceEvents . EnabledTransportsChanged , this . onTransportChanged ) ;
106+ }
107+
98108 this . transport . start ( ) ;
99109 }
100110
@@ -104,6 +114,28 @@ export class RTCEncryptionManager implements IEncryptionManager {
104114 this . transport . stop ( ) ;
105115 }
106116
117+ private onTransportChanged : ( enabled : EnabledTransports ) => void = ( ) => {
118+ this . logger . info ( "Transport change detected, restarting key distribution" ) ;
119+ // Temporary for backwards compatibility
120+ if ( this . currentKeyDistributionPromise ) {
121+ this . currentKeyDistributionPromise
122+ . then ( ( ) => {
123+ if ( this . outboundSession ) {
124+ this . outboundSession . sharedWith = [ ] ;
125+ this . ensureMediaKey ( ) ;
126+ }
127+ } )
128+ . catch ( ( e ) => {
129+ this . logger . error ( "Failed to restart key distribution" , e ) ;
130+ } ) ;
131+ } else {
132+ if ( this . outboundSession ) {
133+ this . outboundSession . sharedWith = [ ] ;
134+ this . ensureMediaKey ( ) ;
135+ }
136+ }
137+ } ;
138+
107139 /**
108140 * Will ensure that a new key is distributed and used to encrypt our media.
109141 * If this function is called repeatidly, the calls will be buffered to a single key rotation.
0 commit comments