@@ -23,6 +23,11 @@ import { logger as rootLogger, type Logger } from "../logger.ts";
23
23
import { sleep } from "../utils.ts" ;
24
24
import type { InboundEncryptionSession , ParticipantDeviceInfo , ParticipantId , Statistics } from "./types.ts" ;
25
25
import { getParticipantId , KeyBuffer } from "./utils.ts" ;
26
+ import {
27
+ type EnabledTransports ,
28
+ RoomAndToDeviceEvents ,
29
+ RoomAndToDeviceTransport ,
30
+ } from "./RoomAndToDeviceKeyTransport.ts" ;
26
31
27
32
type OutboundEncryptionSession = {
28
33
key : Uint8Array ;
@@ -95,6 +100,11 @@ export class RTCEncryptionManager implements IEncryptionManager {
95
100
this . logger . info ( `Joining room` ) ;
96
101
this . delayRolloutTimeMillis = joinConfig ?. useKeyDelay ?? 1000 ;
97
102
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
+
98
108
this . transport . start ( ) ;
99
109
}
100
110
@@ -104,6 +114,28 @@ export class RTCEncryptionManager implements IEncryptionManager {
104
114
this . transport . stop ( ) ;
105
115
}
106
116
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
+
107
139
/**
108
140
* Will ensure that a new key is distributed and used to encrypt our media.
109
141
* If this function is called repeatidly, the calls will be buffered to a single key rotation.
0 commit comments