Parcourir la source

fix(proxy): provide way to clean up some RTC state

dev1
Leonard Kim il y a 6 ans
Parent
révision
dc1121c71d
2 fichiers modifiés avec 64 ajouts et 10 suppressions
  1. 51
    4
      modules/RTC/RTC.js
  2. 13
    6
      modules/proxyconnection/ProxyConnectionPC.js

+ 51
- 4
modules/RTC/RTC.js Voir le fichier

@@ -189,16 +189,51 @@ export default class RTC extends Listenable {
189 189
         // The last N change listener.
190 190
         this._lastNChangeListener = this._onLastNChanged.bind(this);
191 191
 
192
+        this._onDeviceListChanged = this._onDeviceListChanged.bind(this);
193
+        this._updateAudioOutputForAudioTracks
194
+            = this._updateAudioOutputForAudioTracks.bind(this);
195
+
192 196
         // Switch audio output device on all remote audio tracks. Local audio
193 197
         // tracks handle this event by themselves.
194 198
         if (RTCUtils.isDeviceChangeAvailable('output')) {
195
-            RTCUtils.addListener(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
196
-                deviceId => this._updateAudioOutputForAudioTracks(deviceId));
199
+            RTCUtils.addListener(
200
+                RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
201
+                this._updateAudioOutputForAudioTracks
202
+            );
197 203
 
198 204
             RTCUtils.addListener(
199 205
                 RTCEvents.DEVICE_LIST_CHANGED,
200
-                () => this._updateAudioOutputForAudioTracks(
201
-                    RTCUtils.getAudioOutputDevice()));
206
+                this._onDeviceListChanged
207
+            );
208
+        }
209
+    }
210
+
211
+    /**
212
+     * Removes any listeners and stored state from this {@code RTC} instance.
213
+     *
214
+     * @returns {void}
215
+     */
216
+    destroy() {
217
+        RTCUtils.removeListener(
218
+            RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
219
+            this._updateAudioOutputForAudioTracks
220
+        );
221
+
222
+        RTCUtils.removeListener(
223
+            RTCEvents.DEVICE_LIST_CHANGED,
224
+            this._onDeviceListChanged
225
+        );
226
+
227
+        this.removeListener(
228
+            RTCEvents.LASTN_ENDPOINT_CHANGED,
229
+            this._lastNChangeListener
230
+        );
231
+
232
+        if (this._channelOpenListener) {
233
+            this.removeListener(
234
+                RTCEvents.DATA_CHANNEL_OPEN,
235
+                this._channelOpenListener
236
+            );
202 237
         }
203 238
     }
204 239
 
@@ -302,6 +337,18 @@ export default class RTC extends Listenable {
302 337
             this._lastNChangeListener);
303 338
     }
304 339
 
340
+    /**
341
+     * Callback invoked when the list of known audio and video devices has
342
+     * been updated. Attempts to update the known available audio output
343
+     * devices.
344
+     *
345
+     * @private
346
+     * @returns {void}
347
+     */
348
+    _onDeviceListChanged() {
349
+        this._updateAudioOutputForAudioTracks(RTCUtils.getAudioOutputDevice());
350
+    }
351
+
305 352
     /**
306 353
      * Receives events when Last N had changed.
307 354
      * @param {array} lastNEndpoints The new Last N endpoints.

+ 13
- 6
modules/proxyconnection/ProxyConnectionPC.js Voir le fichier

@@ -231,14 +231,14 @@ export default class ProxyConnectionPC {
231 231
          * of {@code RTC} from elsewhere should not be re-used because it is
232 232
          * a stateful grouping of utilities.
233 233
          */
234
-        const rtc = new RTC(this, {});
234
+        this._rtc = new RTC(this, {});
235 235
 
236 236
         /**
237 237
          * Add the remote track listener here as {@code JingleSessionPC} has
238 238
          * {@code TraceablePeerConnection} which uses {@code RTC}'s event
239 239
          * emitter.
240 240
          */
241
-        rtc.addListener(
241
+        this._rtc.addListener(
242 242
             RTCEvents.REMOTE_TRACK_ADDED,
243 243
             this._onRemoteStream
244 244
         );
@@ -261,7 +261,7 @@ export default class ProxyConnectionPC {
261 261
          * An additional initialize call is necessary to properly set instance
262 262
          * variable for calling.
263 263
          */
264
-        peerConnection.initialize(roomStub, rtc, configStub);
264
+        peerConnection.initialize(roomStub, this._rtc, configStub);
265 265
 
266 266
         return peerConnection;
267 267
     }
@@ -364,11 +364,18 @@ export default class ProxyConnectionPC {
364 364
         this._tracks.forEach(track => track.dispose());
365 365
         this._tracks = [];
366 366
 
367
-        if (!this._peerConnection) {
368
-            return;
367
+        if (this._peerConnection) {
368
+            this._peerConnection.onTerminated();
369 369
         }
370 370
 
371
-        this._peerConnection.onTerminated();
371
+        if (this._rtc) {
372
+            this._rtc.removeListener(
373
+                RTCEvents.REMOTE_TRACK_ADDED,
374
+                this._onRemoteStream
375
+            );
376
+
377
+            this._rtc.destroy();
378
+        }
372 379
     }
373 380
 
374 381
     /**

Chargement…
Annuler
Enregistrer