@rtsee/conference 0.0.113 → 0.0.114

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -103,6 +103,9 @@ var RTSeeConference = /** @class */ (function (_super) {
103
103
  _this.settingsOpened = false;
104
104
  _this.constraints = __assign({}, DEFAULT_MEDIA_CONSTRAINTS);
105
105
  _this._callSessionId = 0;
106
+ _this._currentFacingMode = "user";
107
+ _this.trackedLocalTracks = new Set();
108
+ _this.pendingPeerLeaveTimers = new Map();
106
109
  if (options) {
107
110
  _this.init(options);
108
111
  }
@@ -142,7 +145,115 @@ var RTSeeConference = /** @class */ (function (_super) {
142
145
  this.initControls(options.uiControls);
143
146
  };
144
147
  RTSeeConference.prototype.attachMessenger = function (messengerClient) {
148
+ var _this = this;
149
+ var _a;
150
+ var prevMod = this.messengerClient;
151
+ if ((prevMod === null || prevMod === void 0 ? void 0 : prevMod.off) && this._blockedUsersHandler) {
152
+ prevMod.off("blocked-users-updated", this._blockedUsersHandler);
153
+ }
145
154
  this.messengerClient = messengerClient;
155
+ this._blockedUsersHandler = function () {
156
+ _this.disconnectBlockedPeers();
157
+ };
158
+ var nextMod = this.messengerClient;
159
+ (_a = nextMod === null || nextMod === void 0 ? void 0 : nextMod.on) === null || _a === void 0 ? void 0 : _a.call(nextMod, "blocked-users-updated", this._blockedUsersHandler);
160
+ };
161
+ Object.defineProperty(RTSeeConference.prototype, "activeSpeakerPeer", {
162
+ get: function () {
163
+ return this.rtcPeerConnections.activeSpeaker;
164
+ },
165
+ enumerable: false,
166
+ configurable: true
167
+ });
168
+ RTSeeConference.prototype.isPeerBlocked = function (peerId) {
169
+ if (!peerId || !this.messengerClient) {
170
+ return false;
171
+ }
172
+ var baseId = peerId.split("::")[0];
173
+ var mod = this.messengerClient;
174
+ if (typeof mod.isUserBlocked === "function") {
175
+ return Boolean(mod.isUserBlocked(baseId) || mod.isUserBlocked(peerId));
176
+ }
177
+ if (mod.blockedUserIds instanceof Set) {
178
+ return mod.blockedUserIds.has(baseId) || mod.blockedUserIds.has(peerId);
179
+ }
180
+ if (Array.isArray(mod.blockedUserIds)) {
181
+ return (mod.blockedUserIds.includes(baseId) ||
182
+ mod.blockedUserIds.includes(peerId));
183
+ }
184
+ return false;
185
+ };
186
+ RTSeeConference.prototype.disconnectBlockedPeers = function () {
187
+ var _this = this;
188
+ var blockedPeers = this.rtcPeerConnections.connections.filter(function (p) {
189
+ return _this.isPeerBlocked(p.clientId);
190
+ });
191
+ blockedPeers.forEach(function (p) {
192
+ var _a;
193
+ _this.rtcPeerConnections.removeByPeerId(p.clientId);
194
+ _this.screenSharePeers.removeByPeerId(p.clientId);
195
+ if (((_a = _this.screenSharePeer) === null || _a === void 0 ? void 0 : _a.clientId) === p.clientId) {
196
+ _this.screenSharePeer.endCall();
197
+ _this.screenSharePeer = null;
198
+ }
199
+ });
200
+ };
201
+ RTSeeConference.prototype.registerLocalStreamTracks = function (stream) {
202
+ var _this = this;
203
+ if (!stream) {
204
+ return;
205
+ }
206
+ stream.getTracks().forEach(function (track) {
207
+ _this.trackedLocalTracks.add(track);
208
+ });
209
+ };
210
+ RTSeeConference.prototype.disposeDetachedStream = function (stream) {
211
+ var _this = this;
212
+ if (!stream) {
213
+ return;
214
+ }
215
+ stream.getTracks().forEach(function (track) {
216
+ track.enabled = false;
217
+ track.stop();
218
+ _this.trackedLocalTracks.delete(track);
219
+ });
220
+ };
221
+ RTSeeConference.prototype.attachPageLifecycleCleanup = function () {
222
+ var _this = this;
223
+ this.detachPageLifecycleCleanup();
224
+ if (typeof window === "undefined") {
225
+ return;
226
+ }
227
+ this._pageUnloadHandler = function () {
228
+ if (_this.isInActiveCall) {
229
+ _this.end();
230
+ }
231
+ };
232
+ window.addEventListener("pagehide", this._pageUnloadHandler);
233
+ window.addEventListener("beforeunload", this._pageUnloadHandler);
234
+ };
235
+ RTSeeConference.prototype.detachPageLifecycleCleanup = function () {
236
+ if (!this._pageUnloadHandler || typeof window === "undefined") {
237
+ this._pageUnloadHandler = undefined;
238
+ return;
239
+ }
240
+ window.removeEventListener("pagehide", this._pageUnloadHandler);
241
+ window.removeEventListener("beforeunload", this._pageUnloadHandler);
242
+ this._pageUnloadHandler = undefined;
243
+ };
244
+ RTSeeConference.prototype.clearPeerLeaveTimers = function () {
245
+ this.pendingPeerLeaveTimers.forEach(function (timer) { return clearTimeout(timer); });
246
+ this.pendingPeerLeaveTimers.clear();
247
+ };
248
+ RTSeeConference.prototype.cancelPeerLeaveTimer = function (peerId) {
249
+ if (!peerId) {
250
+ return;
251
+ }
252
+ var existing = this.pendingPeerLeaveTimers.get(peerId);
253
+ if (existing) {
254
+ clearTimeout(existing);
255
+ this.pendingPeerLeaveTimers.delete(peerId);
256
+ }
146
257
  };
147
258
  RTSeeConference.prototype.onReady = function () {
148
259
  this.startActiveSpeakerInterval();
@@ -215,7 +326,7 @@ var RTSeeConference = /** @class */ (function (_super) {
215
326
  */
216
327
  RTSeeConference.prototype.getUserMedia = function (mediaOptions) {
217
328
  return __awaiter(this, void 0, void 0, function () {
218
- var wantAudio, wantVideo, audioConstraint, videoConstraint, mediaStream, e_1, audioOnlyStream, audioErr_1;
329
+ var wantAudio, wantVideo, sessionId, audioConstraint, videoConstraint, mediaStream, e_1, audioOnlyStream, audioErr_1;
219
330
  var _a, _b, _c, _d, _e, _f, _g;
220
331
  return __generator(this, function (_h) {
221
332
  switch (_h.label) {
@@ -234,6 +345,7 @@ var RTSeeConference = /** @class */ (function (_super) {
234
345
  this.camera = false;
235
346
  return [2 /*return*/, null];
236
347
  }
348
+ sessionId = this._callSessionId;
237
349
  audioConstraint = wantAudio ? this.buildAudioConstraint() : false;
238
350
  videoConstraint = wantVideo ? this.buildVideoConstraint() : false;
239
351
  _h.label = 1;
@@ -245,6 +357,11 @@ var RTSeeConference = /** @class */ (function (_super) {
245
357
  })];
246
358
  case 2:
247
359
  mediaStream = _h.sent();
360
+ this.registerLocalStreamTracks(mediaStream);
361
+ if (sessionId !== this._callSessionId || !this.isInActiveCall) {
362
+ this.disposeDetachedStream(mediaStream);
363
+ return [2 /*return*/, null];
364
+ }
248
365
  this.onGetUserMediaSuccess(mediaStream);
249
366
  return [2 /*return*/, mediaStream];
250
367
  case 3:
@@ -259,6 +376,11 @@ var RTSeeConference = /** @class */ (function (_super) {
259
376
  })];
260
377
  case 5:
261
378
  audioOnlyStream = _h.sent();
379
+ this.registerLocalStreamTracks(audioOnlyStream);
380
+ if (sessionId !== this._callSessionId || !this.isInActiveCall) {
381
+ this.disposeDetachedStream(audioOnlyStream);
382
+ return [2 /*return*/, null];
383
+ }
262
384
  this.onGetUserMediaSuccess(audioOnlyStream);
263
385
  return [2 /*return*/, audioOnlyStream];
264
386
  case 6:
@@ -337,10 +459,19 @@ var RTSeeConference = /** @class */ (function (_super) {
337
459
  };
338
460
  RTSeeConference.prototype.incomingDescription = function (options) {
339
461
  return __awaiter(this, void 0, void 0, function () {
340
- var peer, isNewRegularPeer, JANUS_SESSION_PREFIX, JANUS_SESSION_PREFIX_S, peerMeta;
341
- return __generator(this, function (_a) {
342
- switch (_a.label) {
462
+ var sessionId, peer, isNewRegularPeer, JANUS_SESSION_PREFIX, JANUS_SESSION_PREFIX_S, peerMeta;
463
+ var _a;
464
+ return __generator(this, function (_b) {
465
+ switch (_b.label) {
343
466
  case 0:
467
+ if (!this.isInActiveCall ||
468
+ !this.roomId ||
469
+ !(options === null || options === void 0 ? void 0 : options.from) ||
470
+ this.isPeerBlocked(options.from)) {
471
+ return [2 /*return*/];
472
+ }
473
+ sessionId = this._callSessionId;
474
+ this.cancelPeerLeaveTimer(options.from);
344
475
  peer = this.getPeer(options.from, options.isScreenShare);
345
476
  isNewRegularPeer = false;
346
477
  JANUS_SESSION_PREFIX = "janus-session";
@@ -374,7 +505,15 @@ var RTSeeConference = /** @class */ (function (_super) {
374
505
  }
375
506
  return [4 /*yield*/, peer.negotiation.setDescription(options.description)];
376
507
  case 1:
377
- _a.sent();
508
+ _b.sent();
509
+ if (sessionId !== this._callSessionId || !this.isInActiveCall) {
510
+ this.rtcPeerConnections.removeByPeerId(options.from);
511
+ if (((_a = this.screenSharePeer) === null || _a === void 0 ? void 0 : _a.clientId) === options.from) {
512
+ this.screenSharePeer.endCall();
513
+ this.screenSharePeer = null;
514
+ }
515
+ return [2 /*return*/];
516
+ }
378
517
  if (isNewRegularPeer) {
379
518
  this.broadcastLocalDeviceStates();
380
519
  }
@@ -384,6 +523,9 @@ var RTSeeConference = /** @class */ (function (_super) {
384
523
  });
385
524
  };
386
525
  RTSeeConference.prototype.incomingCandidate = function (options) {
526
+ if (!this.isInActiveCall || !this.roomId || !(options === null || options === void 0 ? void 0 : options.from)) {
527
+ return;
528
+ }
387
529
  var peer = this.getPeer(options.from, options.isScreenShare);
388
530
  if (!peer) {
389
531
  return;
@@ -391,6 +533,9 @@ var RTSeeConference = /** @class */ (function (_super) {
391
533
  peer.negotiation.addCandidate(options.candidate);
392
534
  };
393
535
  RTSeeConference.prototype.incomingRestart = function (options) {
536
+ if (!this.isInActiveCall || !this.roomId || !(options === null || options === void 0 ? void 0 : options.from)) {
537
+ return;
538
+ }
394
539
  var peer = this.getPeer(options.from, options.isScreenShare);
395
540
  if (!peer) {
396
541
  return;
@@ -398,6 +543,13 @@ var RTSeeConference = /** @class */ (function (_super) {
398
543
  void peer.negotiation.restart();
399
544
  };
400
545
  RTSeeConference.prototype.offer = function (peerOptions) {
546
+ if (!this.isInActiveCall ||
547
+ !this.roomId ||
548
+ !(peerOptions === null || peerOptions === void 0 ? void 0 : peerOptions.clientId) ||
549
+ this.isPeerBlocked(peerOptions.clientId)) {
550
+ return;
551
+ }
552
+ this.cancelPeerLeaveTimer(peerOptions.clientId);
401
553
  this.removeStaleSameUserPeers(peerOptions.clientId);
402
554
  this.rtcPeerConnections.removeByPeerId(peerOptions.clientId);
403
555
  this.screenSharePeers.removeByPeerId(peerOptions.clientId);
@@ -427,6 +579,31 @@ var RTSeeConference = /** @class */ (function (_super) {
427
579
  void this.shareScreenToPeer(peer);
428
580
  }
429
581
  };
582
+ RTSeeConference.prototype.onPeerLeft = function (peerId, roomId, reason) {
583
+ var _this = this;
584
+ if (!this.isInActiveCall || !peerId) {
585
+ return;
586
+ }
587
+ if (roomId && this.roomId && roomId !== this.roomId) {
588
+ return;
589
+ }
590
+ this.cancelPeerLeaveTimer(peerId);
591
+ var delayMs = reason === "disconnect" ? 1500 : 350;
592
+ var timer = setTimeout(function () {
593
+ var _a;
594
+ _this.pendingPeerLeaveTimers.delete(peerId);
595
+ if (!_this.isInActiveCall) {
596
+ return;
597
+ }
598
+ _this.rtcPeerConnections.removeByPeerId(peerId);
599
+ _this.screenSharePeers.removeByPeerId(peerId);
600
+ if (((_a = _this.screenSharePeer) === null || _a === void 0 ? void 0 : _a.clientId) === peerId) {
601
+ _this.screenSharePeer.endCall();
602
+ _this.screenSharePeer = null;
603
+ }
604
+ }, delayMs);
605
+ this.pendingPeerLeaveTimers.set(peerId, timer);
606
+ };
430
607
  RTSeeConference.prototype.onPeerCameraToggled = function (peerId, isEnabled) {
431
608
  this.rtcPeerConnections.onPeerCameraToggled(peerId, isEnabled);
432
609
  // Also match base clientId if signal used bare clientId instead of clientId::socketId
@@ -520,6 +697,7 @@ var RTSeeConference = /** @class */ (function (_super) {
520
697
  _this.roomId = roomId;
521
698
  _this.isAudioOnly = Boolean((options === null || options === void 0 ? void 0 : options.audioOnly) || ((_a = _this.constraints) === null || _a === void 0 ? void 0 : _a.video) === false);
522
699
  _this.isInActiveCall = true;
700
+ _this.attachPageLifecycleCleanup();
523
701
  var joinPayload = {
524
702
  clientId: resolvedClientId,
525
703
  rtseeAppId: (data === null || data === void 0 ? void 0 : data.rtseeAppId) || ((_b = _this.myPeer) === null || _b === void 0 ? void 0 : _b.rtseeAppId),
@@ -692,10 +870,11 @@ var RTSeeConference = /** @class */ (function (_super) {
692
870
  };
693
871
  RTSeeConference.prototype.enableMicrophone = function () {
694
872
  return __awaiter(this, void 0, void 0, function () {
695
- var liveAudioTracks, _i, _a, peer, audioConstraint, audioStream, newAudioTrack, existingVideoTracks, _b, _c, peer, e_3;
696
- var _d, _e, _f;
697
- return __generator(this, function (_g) {
698
- switch (_g.label) {
873
+ var liveAudioTracks, _i, _a, peer, sessionId, audioConstraint, audioStream, newAudioTrack_1, oldAudioTracks, existingVideoTracks, _b, _c, peer, e_3;
874
+ var _this = this;
875
+ var _d, _e, _f, _g;
876
+ return __generator(this, function (_h) {
877
+ switch (_h.label) {
699
878
  case 0:
700
879
  liveAudioTracks = (((_d = this.localStream) === null || _d === void 0 ? void 0 : _d.getAudioTracks()) || []).filter(function (t) { return t.readyState === "live"; });
701
880
  if (!(liveAudioTracks.length > 0)) return [3 /*break*/, 5];
@@ -704,14 +883,14 @@ var RTSeeConference = /** @class */ (function (_super) {
704
883
  });
705
884
  this.microphone = true;
706
885
  _i = 0, _a = this.rtcPeerConnections.connections;
707
- _g.label = 1;
886
+ _h.label = 1;
708
887
  case 1:
709
888
  if (!(_i < _a.length)) return [3 /*break*/, 4];
710
889
  peer = _a[_i];
711
890
  return [4 /*yield*/, peer.attachOrReplaceTrack(liveAudioTracks[0], this.localStream)];
712
891
  case 2:
713
- _g.sent();
714
- _g.label = 3;
892
+ _h.sent();
893
+ _h.label = 3;
715
894
  case 3:
716
895
  _i++;
717
896
  return [3 /*break*/, 1];
@@ -723,9 +902,10 @@ var RTSeeConference = /** @class */ (function (_super) {
723
902
  this.microphone = false;
724
903
  return [2 /*return*/];
725
904
  }
726
- _g.label = 6;
905
+ sessionId = this._callSessionId;
906
+ _h.label = 6;
727
907
  case 6:
728
- _g.trys.push([6, 12, , 13]);
908
+ _h.trys.push([6, 12, , 13]);
729
909
  audioConstraint = this.buildAudioConstraint() || {
730
910
  echoCancellation: true,
731
911
  noiseSuppression: true,
@@ -735,28 +915,42 @@ var RTSeeConference = /** @class */ (function (_super) {
735
915
  video: false,
736
916
  })];
737
917
  case 7:
738
- audioStream = _g.sent();
739
- newAudioTrack = audioStream.getAudioTracks()[0];
740
- if (!newAudioTrack) {
918
+ audioStream = _h.sent();
919
+ this.registerLocalStreamTracks(audioStream);
920
+ if (sessionId !== this._callSessionId || !this.isInActiveCall) {
921
+ this.disposeDetachedStream(audioStream);
922
+ return [2 /*return*/];
923
+ }
924
+ newAudioTrack_1 = audioStream.getAudioTracks()[0];
925
+ if (!newAudioTrack_1) {
741
926
  this.microphone = false;
742
927
  return [2 /*return*/];
743
928
  }
744
- existingVideoTracks = (((_f = this.localStream) === null || _f === void 0 ? void 0 : _f.getVideoTracks()) || []).filter(function (t) { return t.readyState === "live"; });
929
+ oldAudioTracks = ((_f = this.localStream) === null || _f === void 0 ? void 0 : _f.getAudioTracks()) || [];
930
+ oldAudioTracks.forEach(function (t) {
931
+ if (t !== newAudioTrack_1) {
932
+ t.enabled = false;
933
+ t.stop();
934
+ _this.trackedLocalTracks.delete(t);
935
+ }
936
+ });
937
+ existingVideoTracks = (((_g = this.localStream) === null || _g === void 0 ? void 0 : _g.getVideoTracks()) || []).filter(function (t) { return t.readyState === "live"; });
745
938
  this.localStream = new MediaStream(__spreadArray([
746
- newAudioTrack
939
+ newAudioTrack_1
747
940
  ], existingVideoTracks, true));
941
+ this.registerLocalStreamTracks(this.localStream);
748
942
  this.getActiveDevicesFromStream(audioStream);
749
943
  void this.refreshMediaDevicesList();
750
944
  this.microphone = true;
751
945
  _b = 0, _c = this.rtcPeerConnections.connections;
752
- _g.label = 8;
946
+ _h.label = 8;
753
947
  case 8:
754
948
  if (!(_b < _c.length)) return [3 /*break*/, 11];
755
949
  peer = _c[_b];
756
- return [4 /*yield*/, peer.attachOrReplaceTrack(newAudioTrack, this.localStream)];
950
+ return [4 /*yield*/, peer.attachOrReplaceTrack(newAudioTrack_1, this.localStream)];
757
951
  case 9:
758
- _g.sent();
759
- _g.label = 10;
952
+ _h.sent();
953
+ _h.label = 10;
760
954
  case 10:
761
955
  _b++;
762
956
  return [3 /*break*/, 8];
@@ -764,7 +958,7 @@ var RTSeeConference = /** @class */ (function (_super) {
764
958
  this.onMicrophoneToggled();
765
959
  return [3 /*break*/, 13];
766
960
  case 12:
767
- e_3 = _g.sent();
961
+ e_3 = _h.sent();
768
962
  this.onGetUserMediaError(e_3);
769
963
  this.microphone = false;
770
964
  return [3 /*break*/, 13];
@@ -784,10 +978,11 @@ var RTSeeConference = /** @class */ (function (_super) {
784
978
  };
785
979
  RTSeeConference.prototype.enableCamera = function () {
786
980
  return __awaiter(this, void 0, void 0, function () {
787
- var liveVideoTracks, _i, _a, peer, videoConstraint, videoStream, newVideoTrack, existingAudioTracks, _b, _c, peer, e_4;
788
- var _d, _e, _f;
789
- return __generator(this, function (_g) {
790
- switch (_g.label) {
981
+ var liveVideoTracks, _i, _a, peer, sessionId, videoConstraint, videoStream, newVideoTrack_1, oldVideoTracks, existingAudioTracks, _b, _c, peer, e_4;
982
+ var _this = this;
983
+ var _d, _e, _f, _g;
984
+ return __generator(this, function (_h) {
985
+ switch (_h.label) {
791
986
  case 0:
792
987
  liveVideoTracks = (((_d = this.localStream) === null || _d === void 0 ? void 0 : _d.getVideoTracks()) || []).filter(function (t) { return t.readyState === "live"; });
793
988
  if (!(liveVideoTracks.length > 0)) return [3 /*break*/, 5];
@@ -797,14 +992,14 @@ var RTSeeConference = /** @class */ (function (_super) {
797
992
  this.isAudioOnly = false;
798
993
  this.camera = true;
799
994
  _i = 0, _a = this.rtcPeerConnections.connections;
800
- _g.label = 1;
995
+ _h.label = 1;
801
996
  case 1:
802
997
  if (!(_i < _a.length)) return [3 /*break*/, 4];
803
998
  peer = _a[_i];
804
999
  return [4 /*yield*/, peer.attachOrReplaceTrack(liveVideoTracks[0], this.localStream)];
805
1000
  case 2:
806
- _g.sent();
807
- _g.label = 3;
1001
+ _h.sent();
1002
+ _h.label = 3;
808
1003
  case 3:
809
1004
  _i++;
810
1005
  return [3 /*break*/, 1];
@@ -816,9 +1011,10 @@ var RTSeeConference = /** @class */ (function (_super) {
816
1011
  this.camera = false;
817
1012
  return [2 /*return*/];
818
1013
  }
819
- _g.label = 6;
1014
+ sessionId = this._callSessionId;
1015
+ _h.label = 6;
820
1016
  case 6:
821
- _g.trys.push([6, 12, , 13]);
1017
+ _h.trys.push([6, 12, , 13]);
822
1018
  videoConstraint = this.buildVideoConstraint() || {
823
1019
  width: { ideal: 1280 },
824
1020
  height: { ideal: 720 },
@@ -829,29 +1025,43 @@ var RTSeeConference = /** @class */ (function (_super) {
829
1025
  video: videoConstraint,
830
1026
  })];
831
1027
  case 7:
832
- videoStream = _g.sent();
833
- newVideoTrack = videoStream.getVideoTracks()[0];
834
- if (!newVideoTrack) {
1028
+ videoStream = _h.sent();
1029
+ this.registerLocalStreamTracks(videoStream);
1030
+ if (sessionId !== this._callSessionId || !this.isInActiveCall) {
1031
+ this.disposeDetachedStream(videoStream);
1032
+ return [2 /*return*/];
1033
+ }
1034
+ newVideoTrack_1 = videoStream.getVideoTracks()[0];
1035
+ if (!newVideoTrack_1) {
835
1036
  this.camera = false;
836
1037
  return [2 /*return*/];
837
1038
  }
838
- existingAudioTracks = (((_f = this.localStream) === null || _f === void 0 ? void 0 : _f.getAudioTracks()) || []).filter(function (t) { return t.readyState === "live"; });
1039
+ oldVideoTracks = ((_f = this.localStream) === null || _f === void 0 ? void 0 : _f.getVideoTracks()) || [];
1040
+ oldVideoTracks.forEach(function (t) {
1041
+ if (t !== newVideoTrack_1) {
1042
+ t.enabled = false;
1043
+ t.stop();
1044
+ _this.trackedLocalTracks.delete(t);
1045
+ }
1046
+ });
1047
+ existingAudioTracks = (((_g = this.localStream) === null || _g === void 0 ? void 0 : _g.getAudioTracks()) || []).filter(function (t) { return t.readyState === "live"; });
839
1048
  this.localStream = new MediaStream(__spreadArray(__spreadArray([], existingAudioTracks, true), [
840
- newVideoTrack,
1049
+ newVideoTrack_1,
841
1050
  ], false));
1051
+ this.registerLocalStreamTracks(this.localStream);
842
1052
  this.getActiveDevicesFromStream(videoStream);
843
1053
  void this.refreshMediaDevicesList();
844
1054
  this.isAudioOnly = false;
845
1055
  this.camera = true;
846
1056
  _b = 0, _c = this.rtcPeerConnections.connections;
847
- _g.label = 8;
1057
+ _h.label = 8;
848
1058
  case 8:
849
1059
  if (!(_b < _c.length)) return [3 /*break*/, 11];
850
1060
  peer = _c[_b];
851
- return [4 /*yield*/, peer.attachOrReplaceTrack(newVideoTrack, this.localStream)];
1061
+ return [4 /*yield*/, peer.attachOrReplaceTrack(newVideoTrack_1, this.localStream)];
852
1062
  case 9:
853
- _g.sent();
854
- _g.label = 10;
1063
+ _h.sent();
1064
+ _h.label = 10;
855
1065
  case 10:
856
1066
  _b++;
857
1067
  return [3 /*break*/, 8];
@@ -859,7 +1069,7 @@ var RTSeeConference = /** @class */ (function (_super) {
859
1069
  this.onCameraToggled();
860
1070
  return [3 /*break*/, 13];
861
1071
  case 12:
862
- e_4 = _g.sent();
1072
+ e_4 = _h.sent();
863
1073
  this.onGetUserMediaError(e_4);
864
1074
  this.camera = false;
865
1075
  return [3 /*break*/, 13];
@@ -938,14 +1148,21 @@ var RTSeeConference = /** @class */ (function (_super) {
938
1148
  else {
939
1149
  this.constraints.audio = { deviceId: { exact: this.audioInputDeviceId } };
940
1150
  }
1151
+ var sessionId = this._callSessionId;
941
1152
  void navigator.mediaDevices
942
1153
  .getUserMedia({ audio: this.constraints.audio, video: false })
943
1154
  .then(function (audioStream) { return __awaiter(_this, void 0, void 0, function () {
944
1155
  var newAudioTrack, oldAudioTracks, liveVideoTracks, _i, _a, peer;
1156
+ var _this = this;
945
1157
  var _b, _c;
946
1158
  return __generator(this, function (_d) {
947
1159
  switch (_d.label) {
948
1160
  case 0:
1161
+ this.registerLocalStreamTracks(audioStream);
1162
+ if (sessionId !== this._callSessionId || !this.isInActiveCall) {
1163
+ this.disposeDetachedStream(audioStream);
1164
+ return [2 /*return*/];
1165
+ }
949
1166
  newAudioTrack = audioStream.getAudioTracks()[0];
950
1167
  if (!newAudioTrack) {
951
1168
  return [2 /*return*/];
@@ -953,10 +1170,15 @@ var RTSeeConference = /** @class */ (function (_super) {
953
1170
  newAudioTrack.enabled = Boolean(this.microphone);
954
1171
  oldAudioTracks = ((_b = this.localStream) === null || _b === void 0 ? void 0 : _b.getAudioTracks()) || [];
955
1172
  liveVideoTracks = (((_c = this.localStream) === null || _c === void 0 ? void 0 : _c.getVideoTracks()) || []).filter(function (t) { return t.readyState === "live"; });
956
- oldAudioTracks.forEach(function (t) { return t.stop(); });
1173
+ oldAudioTracks.forEach(function (t) {
1174
+ t.enabled = false;
1175
+ t.stop();
1176
+ _this.trackedLocalTracks.delete(t);
1177
+ });
957
1178
  this.localStream = new MediaStream(__spreadArray([
958
1179
  newAudioTrack
959
1180
  ], liveVideoTracks, true));
1181
+ this.registerLocalStreamTracks(this.localStream);
960
1182
  _i = 0, _a = this.rtcPeerConnections.connections;
961
1183
  _d.label = 1;
962
1184
  case 1:
@@ -987,14 +1209,21 @@ var RTSeeConference = /** @class */ (function (_super) {
987
1209
  else {
988
1210
  this.constraints.video = { deviceId: { exact: this.videoInputDeviceId } };
989
1211
  }
1212
+ var sessionId = this._callSessionId;
990
1213
  void navigator.mediaDevices
991
1214
  .getUserMedia({ audio: false, video: this.constraints.video })
992
1215
  .then(function (videoStream) { return __awaiter(_this, void 0, void 0, function () {
993
1216
  var newVideoTrack, oldVideoTracks, liveAudioTracks, _i, _a, peer;
1217
+ var _this = this;
994
1218
  var _b, _c;
995
1219
  return __generator(this, function (_d) {
996
1220
  switch (_d.label) {
997
1221
  case 0:
1222
+ this.registerLocalStreamTracks(videoStream);
1223
+ if (sessionId !== this._callSessionId || !this.isInActiveCall) {
1224
+ this.disposeDetachedStream(videoStream);
1225
+ return [2 /*return*/];
1226
+ }
998
1227
  newVideoTrack = videoStream.getVideoTracks()[0];
999
1228
  if (!newVideoTrack) {
1000
1229
  return [2 /*return*/];
@@ -1002,10 +1231,15 @@ var RTSeeConference = /** @class */ (function (_super) {
1002
1231
  newVideoTrack.enabled = Boolean(this.camera);
1003
1232
  oldVideoTracks = ((_b = this.localStream) === null || _b === void 0 ? void 0 : _b.getVideoTracks()) || [];
1004
1233
  liveAudioTracks = (((_c = this.localStream) === null || _c === void 0 ? void 0 : _c.getAudioTracks()) || []).filter(function (t) { return t.readyState === "live"; });
1005
- oldVideoTracks.forEach(function (t) { return t.stop(); });
1234
+ oldVideoTracks.forEach(function (t) {
1235
+ t.enabled = false;
1236
+ t.stop();
1237
+ _this.trackedLocalTracks.delete(t);
1238
+ });
1006
1239
  this.localStream = new MediaStream(__spreadArray(__spreadArray([], liveAudioTracks, true), [
1007
1240
  newVideoTrack,
1008
1241
  ], false));
1242
+ this.registerLocalStreamTracks(this.localStream);
1009
1243
  _i = 0, _a = this.rtcPeerConnections.connections;
1010
1244
  _d.label = 1;
1011
1245
  case 1:
@@ -1024,6 +1258,85 @@ var RTSeeConference = /** @class */ (function (_super) {
1024
1258
  }); })
1025
1259
  .catch(function (e) { return _this.onGetUserMediaError(e); });
1026
1260
  };
1261
+ RTSeeConference.prototype.switchCamera = function () {
1262
+ return __awaiter(this, void 0, void 0, function () {
1263
+ var currentIdx, nextIdx, nextDevice, sessionId, nextFacingMode, videoStream, newVideoTrack, oldVideoTracks, liveAudioTracks, _i, _a, peer, e_5;
1264
+ var _this = this;
1265
+ var _b, _c, _d;
1266
+ return __generator(this, function (_e) {
1267
+ switch (_e.label) {
1268
+ case 0:
1269
+ if (!this.isInActiveCall || !((_b = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _b === void 0 ? void 0 : _b.getUserMedia)) {
1270
+ return [2 /*return*/];
1271
+ }
1272
+ if (this.videoInputDevices && this.videoInputDevices.length > 1) {
1273
+ currentIdx = this.videoInputDevices.findIndex(function (d) { return d.deviceId === _this.videoInputDeviceId; });
1274
+ nextIdx = (currentIdx + 1) % this.videoInputDevices.length;
1275
+ nextDevice = this.videoInputDevices[nextIdx];
1276
+ if (nextDevice === null || nextDevice === void 0 ? void 0 : nextDevice.deviceId) {
1277
+ this.videoInputDeviceId = nextDevice.deviceId;
1278
+ this.setVideoInputDevice();
1279
+ return [2 /*return*/];
1280
+ }
1281
+ }
1282
+ sessionId = this._callSessionId;
1283
+ nextFacingMode = this._currentFacingMode === "user" ? "environment" : "user";
1284
+ _e.label = 1;
1285
+ case 1:
1286
+ _e.trys.push([1, 7, , 8]);
1287
+ return [4 /*yield*/, navigator.mediaDevices.getUserMedia({
1288
+ audio: false,
1289
+ video: {
1290
+ facingMode: { ideal: nextFacingMode },
1291
+ width: { ideal: 1280 },
1292
+ height: { ideal: 720 },
1293
+ },
1294
+ })];
1295
+ case 2:
1296
+ videoStream = _e.sent();
1297
+ this.registerLocalStreamTracks(videoStream);
1298
+ if (sessionId !== this._callSessionId || !this.isInActiveCall) {
1299
+ this.disposeDetachedStream(videoStream);
1300
+ return [2 /*return*/];
1301
+ }
1302
+ newVideoTrack = videoStream.getVideoTracks()[0];
1303
+ if (!newVideoTrack) {
1304
+ return [2 /*return*/];
1305
+ }
1306
+ this._currentFacingMode = nextFacingMode;
1307
+ newVideoTrack.enabled = Boolean(this.camera);
1308
+ oldVideoTracks = ((_c = this.localStream) === null || _c === void 0 ? void 0 : _c.getVideoTracks()) || [];
1309
+ liveAudioTracks = (((_d = this.localStream) === null || _d === void 0 ? void 0 : _d.getAudioTracks()) || []).filter(function (t) { return t.readyState === "live"; });
1310
+ oldVideoTracks.forEach(function (t) {
1311
+ t.enabled = false;
1312
+ t.stop();
1313
+ _this.trackedLocalTracks.delete(t);
1314
+ });
1315
+ this.localStream = new MediaStream(__spreadArray(__spreadArray([], liveAudioTracks, true), [newVideoTrack], false));
1316
+ this.registerLocalStreamTracks(this.localStream);
1317
+ this.getActiveDevicesFromStream(videoStream);
1318
+ _i = 0, _a = this.rtcPeerConnections.connections;
1319
+ _e.label = 3;
1320
+ case 3:
1321
+ if (!(_i < _a.length)) return [3 /*break*/, 6];
1322
+ peer = _a[_i];
1323
+ return [4 /*yield*/, peer.attachOrReplaceTrack(newVideoTrack, this.localStream)];
1324
+ case 4:
1325
+ _e.sent();
1326
+ _e.label = 5;
1327
+ case 5:
1328
+ _i++;
1329
+ return [3 /*break*/, 3];
1330
+ case 6: return [3 /*break*/, 8];
1331
+ case 7:
1332
+ e_5 = _e.sent();
1333
+ this.onGetUserMediaError(e_5);
1334
+ return [3 /*break*/, 8];
1335
+ case 8: return [2 /*return*/];
1336
+ }
1337
+ });
1338
+ });
1339
+ };
1027
1340
  RTSeeConference.prototype.setAudioOutputDevice = function () {
1028
1341
  if (!this.audioOutputDeviceId) {
1029
1342
  return;
@@ -1045,6 +1358,12 @@ var RTSeeConference = /** @class */ (function (_super) {
1045
1358
  this.isInActiveCall = false;
1046
1359
  this.roomId = "";
1047
1360
  this.isAudioOnly = false;
1361
+ this.settingsOpened = false;
1362
+ if (this.uiControls) {
1363
+ this.uiControls.isExpanded = false;
1364
+ }
1365
+ this.detachPageLifecycleCleanup();
1366
+ this.clearPeerLeaveTimers();
1048
1367
  if (wasInActiveCall && previousRoomId) {
1049
1368
  (_b = (_a = this.signalingClient) === null || _a === void 0 ? void 0 : _a.leaveCall) === null || _b === void 0 ? void 0 : _b.call(_a, previousRoomId, {
1050
1369
  clientId: ((_e = (_d = (_c = this.signalingClient) === null || _c === void 0 ? void 0 : _c.getMyPeer) === null || _d === void 0 ? void 0 : _d.call(_c)) === null || _e === void 0 ? void 0 : _e.clientId) || this.clientId,
@@ -1057,7 +1376,10 @@ var RTSeeConference = /** @class */ (function (_super) {
1057
1376
  this.screenSharePeer = null;
1058
1377
  }
1059
1378
  if (this.localScreenStream) {
1060
- this.localScreenStream.getTracks().forEach(function (t) { return t.stop(); });
1379
+ this.localScreenStream.getTracks().forEach(function (t) {
1380
+ t.enabled = false;
1381
+ t.stop();
1382
+ });
1061
1383
  this.localScreenStream = null;
1062
1384
  }
1063
1385
  this.isPresenter = false;
@@ -1091,6 +1413,7 @@ var RTSeeConference = /** @class */ (function (_super) {
1091
1413
  if (!stream) {
1092
1414
  return;
1093
1415
  }
1416
+ this.registerLocalStreamTracks(stream);
1094
1417
  this.localStream = stream;
1095
1418
  this.microphone = stream
1096
1419
  .getAudioTracks()
@@ -1117,14 +1440,27 @@ var RTSeeConference = /** @class */ (function (_super) {
1117
1440
  if (!stream) {
1118
1441
  return;
1119
1442
  }
1443
+ this.registerLocalStreamTracks(stream);
1120
1444
  this.localScreenStream = stream;
1121
1445
  };
1122
1446
  RTSeeConference.prototype.stopLocalStream = function () {
1123
- if (!this.localStream) {
1124
- return;
1447
+ if (this.localStream) {
1448
+ this.localStream.getTracks().forEach(function (track) {
1449
+ track.enabled = false;
1450
+ track.stop();
1451
+ });
1452
+ this.localStream = null;
1125
1453
  }
1126
- this.localStream.getTracks().forEach(function (track) { return track.stop(); });
1127
- this.localStream = null;
1454
+ this.trackedLocalTracks.forEach(function (track) {
1455
+ try {
1456
+ track.enabled = false;
1457
+ track.stop();
1458
+ }
1459
+ catch (_a) { }
1460
+ });
1461
+ this.trackedLocalTracks.clear();
1462
+ this.camera = false;
1463
+ this.microphone = false;
1128
1464
  };
1129
1465
  return RTSeeConference;
1130
1466
  }(core_1.RTSeeCore));