@rtsee/conference 0.0.107 → 0.0.108

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.
@@ -61,26 +61,48 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
61
61
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
62
  }
63
63
  };
64
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
65
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
66
+ if (ar || !(i in from)) {
67
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
68
+ ar[i] = from[i];
69
+ }
70
+ }
71
+ return to.concat(ar || Array.prototype.slice.call(from));
72
+ };
64
73
  Object.defineProperty(exports, "__esModule", { value: true });
65
74
  exports.RTSeeConference = void 0;
66
75
  var core_1 = require("@rtsee/core");
67
76
  var RTSeeUiControls_1 = require("./RTSeeUiControls");
68
77
  var conference_1 = require("@rtsee/conference");
69
78
  var signaling_client_1 = require("@rtsee/signaling-client");
79
+ var DEFAULT_MEDIA_CONSTRAINTS = {
80
+ audio: { echoCancellation: true, noiseSuppression: true },
81
+ video: {
82
+ width: { ideal: 1280 },
83
+ height: { ideal: 720 },
84
+ frameRate: { ideal: 30, max: 30 },
85
+ },
86
+ };
70
87
  var RTSeeConference = /** @class */ (function (_super) {
71
88
  __extends(RTSeeConference, _super);
72
89
  function RTSeeConference(options) {
73
90
  var _this = _super.call(this, options) || this;
74
91
  _this.rtcPeerConnections = new conference_1.RTSeePeerConnectionsList();
75
92
  _this.screenSharePeers = new conference_1.RTSeePeerConnectionsList();
93
+ _this.localStream = null;
76
94
  _this.localScreenStream = null;
77
95
  _this.microphone = true;
78
96
  _this.camera = true;
97
+ _this.ougoingMode = false;
98
+ _this.isAudioOnly = false;
79
99
  _this.isPresenter = false;
80
100
  _this.audioOutputDeviceId = null;
81
101
  _this.videoInputDeviceId = null;
82
102
  _this.audioInputDeviceId = null;
83
103
  _this.settingsOpened = false;
104
+ _this.constraints = __assign({}, DEFAULT_MEDIA_CONSTRAINTS);
105
+ _this._callSessionId = 0;
84
106
  if (options) {
85
107
  _this.init(options);
86
108
  }
@@ -90,9 +112,24 @@ var RTSeeConference = /** @class */ (function (_super) {
90
112
  });
91
113
  return _this;
92
114
  }
115
+ Object.defineProperty(RTSeeConference.prototype, "outgoingMode", {
116
+ get: function () {
117
+ return this.ougoingMode;
118
+ },
119
+ set: function (value) {
120
+ this.ougoingMode = value;
121
+ },
122
+ enumerable: false,
123
+ configurable: true
124
+ });
93
125
  RTSeeConference.prototype.init = function (options) {
126
+ var _a, _b;
94
127
  this.clientId = options.clientId || RTSeeConference.uuidv4();
95
- // this.clientId = RTSeeVideoChat.uuidv4();
128
+ this.ougoingMode = Boolean((_b = (_a = options.ougoingMode) !== null && _a !== void 0 ? _a : options.outgoingMode) !== null && _b !== void 0 ? _b : false);
129
+ if (this.ougoingMode) {
130
+ this.microphone = false;
131
+ this.camera = false;
132
+ }
96
133
  if (options.rtcConfiguration) {
97
134
  this.rtcConfiguration = options.rtcConfiguration;
98
135
  }
@@ -112,7 +149,8 @@ var RTSeeConference = /** @class */ (function (_super) {
112
149
  };
113
150
  RTSeeConference.prototype.startActiveSpeakerInterval = function () {
114
151
  var _this = this;
115
- setInterval(function () {
152
+ this.stopActiveSpeakerInterval();
153
+ this.activeSpeakerIntervalId = setInterval(function () {
116
154
  if (!_this.rtcPeerConnections.connections.length) {
117
155
  return;
118
156
  }
@@ -127,12 +165,13 @@ var RTSeeConference = /** @class */ (function (_super) {
127
165
  return;
128
166
  }
129
167
  clearInterval(this.activeSpeakerIntervalId);
168
+ this.activeSpeakerIntervalId = undefined;
130
169
  };
131
170
  RTSeeConference.prototype.initControls = function (uiControls) {
132
171
  this.uiControls = new RTSeeUiControls_1.RTSeeUiControls(uiControls);
133
172
  };
134
173
  RTSeeConference.prototype.applyConstraints = function (constraints) {
135
- this.constraints = constraints;
174
+ this.constraints = __assign({}, constraints);
136
175
  if (!this.constraints.video) {
137
176
  this.camera = false;
138
177
  }
@@ -140,29 +179,100 @@ var RTSeeConference = /** @class */ (function (_super) {
140
179
  this.microphone = false;
141
180
  }
142
181
  };
143
- RTSeeConference.prototype.getUserMedia = function () {
182
+ RTSeeConference.prototype.buildAudioConstraint = function () {
183
+ var _a;
184
+ var rawAudio = (_a = this.constraints) === null || _a === void 0 ? void 0 : _a.audio;
185
+ if (rawAudio === false) {
186
+ return false;
187
+ }
188
+ var base = typeof rawAudio === "object" && rawAudio !== null
189
+ ? __assign({}, rawAudio) : { echoCancellation: true, noiseSuppression: true };
190
+ if (this.audioInputDeviceId) {
191
+ base.deviceId = { ideal: this.audioInputDeviceId };
192
+ }
193
+ return base;
194
+ };
195
+ RTSeeConference.prototype.buildVideoConstraint = function () {
196
+ var _a;
197
+ var rawVideo = (_a = this.constraints) === null || _a === void 0 ? void 0 : _a.video;
198
+ if (rawVideo === false) {
199
+ return false;
200
+ }
201
+ var base = typeof rawVideo === "object" && rawVideo !== null
202
+ ? __assign({}, rawVideo) : {
203
+ width: { ideal: 1280 },
204
+ height: { ideal: 720 },
205
+ frameRate: { ideal: 30, max: 30 },
206
+ };
207
+ if (this.videoInputDeviceId) {
208
+ base.deviceId = { ideal: this.videoInputDeviceId };
209
+ }
210
+ return base;
211
+ };
212
+ /**
213
+ * Acquires user media with automatic fallback from audio+video -> audio-only -> no media
214
+ * so calls can always start even without camera/mic hardware or browser permissions.
215
+ */
216
+ RTSeeConference.prototype.getUserMedia = function (mediaOptions) {
144
217
  return __awaiter(this, void 0, void 0, function () {
145
- var mediaStream, e_1;
146
- return __generator(this, function (_a) {
147
- switch (_a.label) {
218
+ var wantAudio, wantVideo, audioConstraint, videoConstraint, mediaStream, e_1, audioOnlyStream, audioErr_1;
219
+ var _a, _b, _c, _d, _e, _f, _g;
220
+ return __generator(this, function (_h) {
221
+ switch (_h.label) {
148
222
  case 0:
149
- if (!(navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices)) {
223
+ if (!((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia)) {
150
224
  console.log("No media devices found");
151
- return [2 /*return*/];
225
+ this.microphone = false;
226
+ this.camera = false;
227
+ return [2 /*return*/, null];
152
228
  }
153
- _a.label = 1;
229
+ wantAudio = (_b = mediaOptions === null || mediaOptions === void 0 ? void 0 : mediaOptions.audio) !== null && _b !== void 0 ? _b : Boolean((_d = (_c = this.constraints) === null || _c === void 0 ? void 0 : _c.audio) !== null && _d !== void 0 ? _d : true);
230
+ wantVideo = ((_e = mediaOptions === null || mediaOptions === void 0 ? void 0 : mediaOptions.video) !== null && _e !== void 0 ? _e : Boolean((_g = (_f = this.constraints) === null || _f === void 0 ? void 0 : _f.video) !== null && _g !== void 0 ? _g : true)) &&
231
+ !this.isAudioOnly;
232
+ if (!wantAudio && !wantVideo) {
233
+ this.microphone = false;
234
+ this.camera = false;
235
+ return [2 /*return*/, null];
236
+ }
237
+ audioConstraint = wantAudio ? this.buildAudioConstraint() : false;
238
+ videoConstraint = wantVideo ? this.buildVideoConstraint() : false;
239
+ _h.label = 1;
154
240
  case 1:
155
- _a.trys.push([1, 3, , 4]);
156
- return [4 /*yield*/, navigator.mediaDevices.getUserMedia(this.constraints)];
241
+ _h.trys.push([1, 3, , 8]);
242
+ return [4 /*yield*/, navigator.mediaDevices.getUserMedia({
243
+ audio: audioConstraint,
244
+ video: videoConstraint,
245
+ })];
157
246
  case 2:
158
- mediaStream = _a.sent();
247
+ mediaStream = _h.sent();
159
248
  this.onGetUserMediaSuccess(mediaStream);
160
249
  return [2 /*return*/, mediaStream];
161
250
  case 3:
162
- e_1 = _a.sent();
251
+ e_1 = _h.sent();
252
+ if (!(wantVideo && wantAudio && audioConstraint)) return [3 /*break*/, 7];
253
+ _h.label = 4;
254
+ case 4:
255
+ _h.trys.push([4, 6, , 7]);
256
+ return [4 /*yield*/, navigator.mediaDevices.getUserMedia({
257
+ audio: audioConstraint,
258
+ video: false,
259
+ })];
260
+ case 5:
261
+ audioOnlyStream = _h.sent();
262
+ this.onGetUserMediaSuccess(audioOnlyStream);
263
+ return [2 /*return*/, audioOnlyStream];
264
+ case 6:
265
+ audioErr_1 = _h.sent();
266
+ this.onGetUserMediaError(audioErr_1);
267
+ this.microphone = false;
268
+ this.camera = false;
269
+ return [2 /*return*/, null];
270
+ case 7:
163
271
  this.onGetUserMediaError(e_1);
164
- return [3 /*break*/, 4];
165
- case 4: return [2 /*return*/];
272
+ this.microphone = false;
273
+ this.camera = false;
274
+ return [2 /*return*/, null];
275
+ case 8: return [2 /*return*/];
166
276
  }
167
277
  });
168
278
  });
@@ -173,6 +283,9 @@ var RTSeeConference = /** @class */ (function (_super) {
173
283
  return __generator(this, function (_a) {
174
284
  switch (_a.label) {
175
285
  case 0:
286
+ if (!(navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices)) {
287
+ return [2 /*return*/];
288
+ }
176
289
  extNavigator = navigator.mediaDevices;
177
290
  _a.label = 1;
178
291
  case 1:
@@ -191,16 +304,51 @@ var RTSeeConference = /** @class */ (function (_super) {
191
304
  });
192
305
  });
193
306
  };
307
+ RTSeeConference.prototype.removeStaleSameUserPeers = function (incomingPeerId) {
308
+ var _this = this;
309
+ if (!incomingPeerId) {
310
+ return;
311
+ }
312
+ var baseId = incomingPeerId.split("::")[0];
313
+ if (!baseId) {
314
+ return;
315
+ }
316
+ var stalePeers = this.rtcPeerConnections.connections.filter(function (existing) {
317
+ var _a, _b;
318
+ if (existing.clientId === incomingPeerId) {
319
+ return false;
320
+ }
321
+ var existingBase = existing.clientId.split("::")[0];
322
+ if (existingBase !== baseId) {
323
+ return false;
324
+ }
325
+ var state = (_a = existing.connection) === null || _a === void 0 ? void 0 : _a.connectionState;
326
+ var iceState = (_b = existing.connection) === null || _b === void 0 ? void 0 : _b.iceConnectionState;
327
+ return (state === "disconnected" ||
328
+ state === "failed" ||
329
+ state === "closed" ||
330
+ iceState === "disconnected" ||
331
+ iceState === "failed" ||
332
+ iceState === "closed");
333
+ });
334
+ stalePeers.forEach(function (stale) {
335
+ _this.rtcPeerConnections.removeByPeerId(stale.clientId);
336
+ });
337
+ };
194
338
  RTSeeConference.prototype.incomingDescription = function (options) {
195
339
  return __awaiter(this, void 0, void 0, function () {
196
- var peer, JANUS_SESSION_PREFIX, JANUS_SESSION_PREFIX_S, peerMeta;
340
+ var peer, isNewRegularPeer, JANUS_SESSION_PREFIX, JANUS_SESSION_PREFIX_S, peerMeta;
197
341
  return __generator(this, function (_a) {
198
342
  switch (_a.label) {
199
343
  case 0:
200
344
  peer = this.getPeer(options.from, options.isScreenShare);
345
+ isNewRegularPeer = false;
201
346
  JANUS_SESSION_PREFIX = "janus-session";
202
347
  JANUS_SESSION_PREFIX_S = "".concat(JANUS_SESSION_PREFIX, "-subscriber");
203
348
  if (!peer) {
349
+ if (!options.isScreenShare) {
350
+ this.removeStaleSameUserPeers(options.from);
351
+ }
204
352
  peerMeta = options;
205
353
  peer = new conference_1.RTSeePeerConnection({
206
354
  clientId: options.from,
@@ -215,6 +363,7 @@ var RTSeeConference = /** @class */ (function (_super) {
215
363
  });
216
364
  if (!options.isScreenShare) {
217
365
  this.rtcPeerConnections.add(peer);
366
+ isNewRegularPeer = true;
218
367
  if (!peer.isSubscriber) {
219
368
  this.addLocalTracks(peer);
220
369
  }
@@ -226,6 +375,9 @@ var RTSeeConference = /** @class */ (function (_super) {
226
375
  return [4 /*yield*/, peer.negotiation.setDescription(options.description)];
227
376
  case 1:
228
377
  _a.sent();
378
+ if (isNewRegularPeer) {
379
+ this.broadcastLocalDeviceStates();
380
+ }
229
381
  return [2 /*return*/];
230
382
  }
231
383
  });
@@ -243,15 +395,16 @@ var RTSeeConference = /** @class */ (function (_super) {
243
395
  if (!peer) {
244
396
  return;
245
397
  }
246
- peer.negotiation.restart();
398
+ void peer.negotiation.restart();
247
399
  };
248
400
  RTSeeConference.prototype.offer = function (peerOptions) {
401
+ this.removeStaleSameUserPeers(peerOptions.clientId);
249
402
  this.rtcPeerConnections.removeByPeerId(peerOptions.clientId);
250
403
  this.screenSharePeers.removeByPeerId(peerOptions.clientId);
251
404
  if (this.screenSharePeer &&
252
405
  this.screenSharePeer.connection &&
253
406
  this.screenSharePeer.clientId === peerOptions.clientId) {
254
- this.screenSharePeer.connection.close();
407
+ this.screenSharePeer.endCall();
255
408
  this.screenSharePeer = null;
256
409
  }
257
410
  var peer = this.rtcPeerConnections.add(new conference_1.RTSeePeerConnection({
@@ -266,21 +419,35 @@ var RTSeeConference = /** @class */ (function (_super) {
266
419
  isScreenShare: false,
267
420
  }));
268
421
  this.addLocalTracks(peer);
269
- // this.attachPeerConnectionEvents(peer);
422
+ if (!peer.isSubscriber) {
423
+ void peer.negotiation.createOffer();
424
+ }
425
+ this.broadcastLocalDeviceStates();
270
426
  if (this.isPresenter) {
271
427
  void this.shareScreenToPeer(peer);
272
428
  }
273
429
  };
274
430
  RTSeeConference.prototype.onPeerCameraToggled = function (peerId, isEnabled) {
275
431
  this.rtcPeerConnections.onPeerCameraToggled(peerId, isEnabled);
432
+ // Also match base clientId if signal used bare clientId instead of clientId::socketId
433
+ this.rtcPeerConnections.connections.forEach(function (conn) {
434
+ if (conn.clientId !== peerId && conn.clientId.split("::")[0] === peerId) {
435
+ conn.setCameraState(isEnabled);
436
+ }
437
+ });
276
438
  };
277
439
  RTSeeConference.prototype.onPeerMicrophoneToggled = function (peerId, isEnabled) {
278
440
  this.rtcPeerConnections.onPeerMicrophoneToggled(peerId, isEnabled);
441
+ this.rtcPeerConnections.connections.forEach(function (conn) {
442
+ if (conn.clientId !== peerId && conn.clientId.split("::")[0] === peerId) {
443
+ conn.setMicrophoneState(isEnabled);
444
+ }
445
+ });
279
446
  };
280
447
  RTSeeConference.prototype.onRemoteScreenShareStop = function (peer) {
281
448
  if (this.screenSharePeer &&
282
449
  this.screenSharePeer.clientId === peer.clientId) {
283
- this.screenSharePeer.connection.close();
450
+ this.screenSharePeer.endCall();
284
451
  this.screenSharePeer = null;
285
452
  }
286
453
  };
@@ -289,15 +456,17 @@ var RTSeeConference = /** @class */ (function (_super) {
289
456
  };
290
457
  RTSeeConference.prototype.call = function (roomId, options) {
291
458
  return __awaiter(this, void 0, void 0, function () {
292
- var connected, call, callOptions;
459
+ var myPeer, connected, call, callOptions;
293
460
  return __generator(this, function (_a) {
294
461
  switch (_a.label) {
295
462
  case 0:
296
- this.end();
297
463
  if (!this.signalingClient) {
298
464
  return [2 /*return*/];
299
465
  }
300
- return [4 /*yield*/, this.connect(roomId, this.signalingClient.getMyPeer())];
466
+ myPeer = this.signalingClient.getMyPeer() || this.myPeer;
467
+ return [4 /*yield*/, this.connect(roomId, myPeer, {
468
+ audioOnly: options === null || options === void 0 ? void 0 : options.audioOnly,
469
+ })];
301
470
  case 1:
302
471
  connected = _a.sent();
303
472
  if (!connected) {
@@ -308,14 +477,15 @@ var RTSeeConference = /** @class */ (function (_super) {
308
477
  };
309
478
  callOptions = {
310
479
  chatId: options === null || options === void 0 ? void 0 : options.chatId,
311
- callerId: this.myPeer.clientId,
480
+ callerId: (myPeer === null || myPeer === void 0 ? void 0 : myPeer.clientId) || this.clientId,
481
+ audioOnly: options === null || options === void 0 ? void 0 : options.audioOnly,
312
482
  };
313
483
  if (this.messengerClient) {
314
484
  this.messengerClient.onCallCreated(call, callOptions);
315
485
  }
316
486
  this.signalingClient.emitEvent({
317
487
  name: core_1.RTSeeEvents.OUTGOING_CALL,
318
- data: __assign(__assign({}, call), { messengerChatId: callOptions.chatId }),
488
+ data: __assign(__assign({}, call), { messengerChatId: callOptions.chatId, audioOnly: Boolean(options === null || options === void 0 ? void 0 : options.audioOnly) }),
319
489
  });
320
490
  return [2 /*return*/];
321
491
  }
@@ -334,39 +504,144 @@ var RTSeeConference = /** @class */ (function (_super) {
334
504
  void this.messengerClient.onRoomCreated(options);
335
505
  }
336
506
  };
337
- RTSeeConference.prototype.connect = function (roomId, data) {
507
+ RTSeeConference.prototype.connect = function (roomId, data, options) {
338
508
  var _this = this;
339
509
  this.end();
340
510
  return new Promise(function (resolve, reject) {
341
- if (!roomId || !(data === null || data === void 0 ? void 0 : data.clientId)) {
342
- reject("roomId and Peer.clientId are required in order to make calls");
511
+ var _a, _b, _c, _d, _e, _f;
512
+ var resolvedClientId = (data === null || data === void 0 ? void 0 : data.clientId) || _this.clientId;
513
+ if (!roomId || !resolvedClientId) {
514
+ return reject("roomId and Peer.clientId are required in order to make calls");
343
515
  }
344
516
  if (!_this.signalingClient) {
345
- reject("Signaling Client is Required in order to make calls!");
517
+ return reject("Signaling Client is Required in order to make calls!");
346
518
  }
519
+ var sessionId = ++_this._callSessionId;
347
520
  _this.roomId = roomId;
521
+ _this.isAudioOnly = Boolean((options === null || options === void 0 ? void 0 : options.audioOnly) || ((_a = _this.constraints) === null || _a === void 0 ? void 0 : _a.video) === false);
348
522
  _this.isInActiveCall = true;
523
+ var joinPayload = {
524
+ clientId: resolvedClientId,
525
+ rtseeAppId: (data === null || data === void 0 ? void 0 : data.rtseeAppId) || ((_b = _this.myPeer) === null || _b === void 0 ? void 0 : _b.rtseeAppId),
526
+ roomId: _this.roomId,
527
+ };
528
+ // `ougoingMode`: join the call immediately as muted & camera-off,
529
+ // and prompt for microphone/camera permissions only afterwards.
530
+ if (_this.ougoingMode && !_this.localStream) {
531
+ _this.microphone = false;
532
+ _this.camera = false;
533
+ _this.signalingClient.joinCall(_this.roomId, joinPayload);
534
+ _this.broadcastLocalDeviceStates();
535
+ resolve(true);
536
+ void _this.promptMediaAfterOutgoingJoin(sessionId);
537
+ return;
538
+ }
349
539
  if (_this.localStream) {
350
- _this.signalingClient.joinCall(_this.roomId, {
351
- clientId: data.clientId,
352
- rtseeAppId: data.rtseeAppId,
353
- roomId: _this.roomId,
354
- });
540
+ _this.microphone = _this.localStream
541
+ .getAudioTracks()
542
+ .some(function (t) { return t.readyState === "live" && t.enabled; });
543
+ _this.camera =
544
+ !_this.isAudioOnly &&
545
+ _this.localStream
546
+ .getVideoTracks()
547
+ .some(function (t) { return t.readyState === "live" && t.enabled; });
548
+ _this.signalingClient.joinCall(_this.roomId, joinPayload);
549
+ _this.broadcastLocalDeviceStates();
355
550
  return resolve(true);
356
551
  }
357
- void _this.getUserMedia().then(function () {
358
- _this.signalingClient.joinCall(_this.roomId, {
359
- clientId: data.clientId,
360
- rtseeAppId: data.rtseeAppId,
361
- roomId: _this.roomId,
362
- });
552
+ void _this.getUserMedia({
553
+ audio: Boolean((_d = (_c = _this.constraints) === null || _c === void 0 ? void 0 : _c.audio) !== null && _d !== void 0 ? _d : true),
554
+ video: !_this.isAudioOnly && Boolean((_f = (_e = _this.constraints) === null || _e === void 0 ? void 0 : _e.video) !== null && _f !== void 0 ? _f : true),
555
+ }).then(function () {
556
+ if (sessionId !== _this._callSessionId || !_this.isInActiveCall) {
557
+ _this.stopLocalStream();
558
+ return resolve(false);
559
+ }
560
+ _this.signalingClient.joinCall(_this.roomId, joinPayload);
561
+ _this.broadcastLocalDeviceStates();
363
562
  resolve(true);
364
563
  });
365
564
  });
366
565
  };
566
+ RTSeeConference.prototype.promptMediaAfterOutgoingJoin = function (sessionId) {
567
+ return __awaiter(this, void 0, void 0, function () {
568
+ var stream;
569
+ var _a, _b, _c, _d;
570
+ return __generator(this, function (_e) {
571
+ switch (_e.label) {
572
+ case 0: return [4 /*yield*/, this.getUserMedia({
573
+ audio: Boolean((_b = (_a = this.constraints) === null || _a === void 0 ? void 0 : _a.audio) !== null && _b !== void 0 ? _b : true),
574
+ video: !this.isAudioOnly && Boolean((_d = (_c = this.constraints) === null || _c === void 0 ? void 0 : _c.video) !== null && _d !== void 0 ? _d : true),
575
+ })];
576
+ case 1:
577
+ stream = _e.sent();
578
+ if (sessionId !== this._callSessionId || !this.isInActiveCall) {
579
+ if (stream) {
580
+ stream.getTracks().forEach(function (t) { return t.stop(); });
581
+ }
582
+ this.stopLocalStream();
583
+ return [2 /*return*/];
584
+ }
585
+ if (!stream) {
586
+ this.microphone = false;
587
+ this.camera = false;
588
+ this.broadcastLocalDeviceStates();
589
+ return [2 /*return*/];
590
+ }
591
+ return [4 /*yield*/, this.attachStreamTracksToActivePeers(stream)];
592
+ case 2:
593
+ _e.sent();
594
+ this.broadcastLocalDeviceStates();
595
+ return [2 /*return*/];
596
+ }
597
+ });
598
+ });
599
+ };
600
+ RTSeeConference.prototype.attachStreamTracksToActivePeers = function (stream) {
601
+ return __awaiter(this, void 0, void 0, function () {
602
+ var tracks, _i, _a, peer, _b, tracks_1, track;
603
+ return __generator(this, function (_c) {
604
+ switch (_c.label) {
605
+ case 0:
606
+ tracks = stream.getTracks();
607
+ _i = 0, _a = this.rtcPeerConnections.connections;
608
+ _c.label = 1;
609
+ case 1:
610
+ if (!(_i < _a.length)) return [3 /*break*/, 6];
611
+ peer = _a[_i];
612
+ _b = 0, tracks_1 = tracks;
613
+ _c.label = 2;
614
+ case 2:
615
+ if (!(_b < tracks_1.length)) return [3 /*break*/, 5];
616
+ track = tracks_1[_b];
617
+ return [4 /*yield*/, peer.attachOrReplaceTrack(track, this.localStream || stream)];
618
+ case 3:
619
+ _c.sent();
620
+ _c.label = 4;
621
+ case 4:
622
+ _b++;
623
+ return [3 /*break*/, 2];
624
+ case 5:
625
+ _i++;
626
+ return [3 /*break*/, 1];
627
+ case 6: return [2 /*return*/];
628
+ }
629
+ });
630
+ });
631
+ };
632
+ RTSeeConference.prototype.broadcastLocalDeviceStates = function () {
633
+ if (!this.isInActiveCall || !this.roomId) {
634
+ return;
635
+ }
636
+ this.onMicrophoneToggled();
637
+ this.onCameraToggled();
638
+ };
367
639
  RTSeeConference.prototype.startScreenShare = function () {
368
640
  var _this = this;
369
641
  void this.getScreenMedia().then(function () {
642
+ if (!_this.localScreenStream) {
643
+ return;
644
+ }
370
645
  _this.rtcPeerConnections.connections.forEach(function (peer) {
371
646
  _this.shareScreenToPeer(peer);
372
647
  });
@@ -374,6 +649,9 @@ var RTSeeConference = /** @class */ (function (_super) {
374
649
  });
375
650
  };
376
651
  RTSeeConference.prototype.shareScreenToPeer = function (peer) {
652
+ if (!this.localScreenStream) {
653
+ return;
654
+ }
377
655
  var shareScreenPeer = this.screenSharePeers.add(new conference_1.RTSeePeerConnection({
378
656
  clientId: peer.clientId,
379
657
  name: peer.name,
@@ -386,10 +664,14 @@ var RTSeeConference = /** @class */ (function (_super) {
386
664
  signalingClient: this.signalingClient,
387
665
  }));
388
666
  this.addScreenTracks(shareScreenPeer);
389
- // this.attachPeerConnectionEvents(shareScreenPeer);
667
+ void shareScreenPeer.negotiation.createOffer();
390
668
  };
391
669
  RTSeeConference.prototype.stopScreenShare = function () {
392
670
  this.screenSharePeers.eraseAll();
671
+ if (this.localScreenStream) {
672
+ this.localScreenStream.getTracks().forEach(function (t) { return t.stop(); });
673
+ this.localScreenStream = null;
674
+ }
393
675
  this.isPresenter = false;
394
676
  this.emit(core_1.RTSeeEvents.SHARE_SCREEN_STOP, {
395
677
  clientId: this.clientId,
@@ -397,24 +679,191 @@ var RTSeeConference = /** @class */ (function (_super) {
397
679
  });
398
680
  };
399
681
  RTSeeConference.prototype.disableMicrophone = function () {
682
+ var _a;
400
683
  this.microphone = false;
401
- this.localStream.getAudioTracks()[0].enabled = false;
684
+ var audioTracks = ((_a = this.localStream) === null || _a === void 0 ? void 0 : _a.getAudioTracks()) || [];
685
+ audioTracks.forEach(function (track) {
686
+ track.enabled = false;
687
+ });
402
688
  this.onMicrophoneToggled();
403
689
  };
404
690
  RTSeeConference.prototype.enableMicrophone = function () {
405
- this.localStream.getAudioTracks()[0].enabled = true;
406
- this.microphone = true;
407
- this.onMicrophoneToggled();
691
+ return __awaiter(this, void 0, void 0, function () {
692
+ var liveAudioTracks, _i, _a, peer, audioConstraint, audioStream, newAudioTrack, existingVideoTracks, _b, _c, peer, e_3;
693
+ var _d, _e, _f;
694
+ return __generator(this, function (_g) {
695
+ switch (_g.label) {
696
+ case 0:
697
+ liveAudioTracks = (((_d = this.localStream) === null || _d === void 0 ? void 0 : _d.getAudioTracks()) || []).filter(function (t) { return t.readyState === "live"; });
698
+ if (!(liveAudioTracks.length > 0)) return [3 /*break*/, 5];
699
+ liveAudioTracks.forEach(function (track) {
700
+ track.enabled = true;
701
+ });
702
+ this.microphone = true;
703
+ _i = 0, _a = this.rtcPeerConnections.connections;
704
+ _g.label = 1;
705
+ case 1:
706
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
707
+ peer = _a[_i];
708
+ return [4 /*yield*/, peer.attachOrReplaceTrack(liveAudioTracks[0], this.localStream)];
709
+ case 2:
710
+ _g.sent();
711
+ _g.label = 3;
712
+ case 3:
713
+ _i++;
714
+ return [3 /*break*/, 1];
715
+ case 4:
716
+ this.onMicrophoneToggled();
717
+ return [2 /*return*/];
718
+ case 5:
719
+ if (!((_e = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _e === void 0 ? void 0 : _e.getUserMedia)) {
720
+ this.microphone = false;
721
+ return [2 /*return*/];
722
+ }
723
+ _g.label = 6;
724
+ case 6:
725
+ _g.trys.push([6, 12, , 13]);
726
+ audioConstraint = this.buildAudioConstraint() || {
727
+ echoCancellation: true,
728
+ noiseSuppression: true,
729
+ };
730
+ return [4 /*yield*/, navigator.mediaDevices.getUserMedia({
731
+ audio: audioConstraint,
732
+ video: false,
733
+ })];
734
+ case 7:
735
+ audioStream = _g.sent();
736
+ newAudioTrack = audioStream.getAudioTracks()[0];
737
+ if (!newAudioTrack) {
738
+ this.microphone = false;
739
+ return [2 /*return*/];
740
+ }
741
+ existingVideoTracks = (((_f = this.localStream) === null || _f === void 0 ? void 0 : _f.getVideoTracks()) || []).filter(function (t) { return t.readyState === "live"; });
742
+ this.localStream = new MediaStream(__spreadArray([
743
+ newAudioTrack
744
+ ], existingVideoTracks, true));
745
+ this.getActiveDevicesFromStream(audioStream);
746
+ void this.refreshMediaDevicesList();
747
+ this.microphone = true;
748
+ _b = 0, _c = this.rtcPeerConnections.connections;
749
+ _g.label = 8;
750
+ case 8:
751
+ if (!(_b < _c.length)) return [3 /*break*/, 11];
752
+ peer = _c[_b];
753
+ return [4 /*yield*/, peer.attachOrReplaceTrack(newAudioTrack, this.localStream)];
754
+ case 9:
755
+ _g.sent();
756
+ _g.label = 10;
757
+ case 10:
758
+ _b++;
759
+ return [3 /*break*/, 8];
760
+ case 11:
761
+ this.onMicrophoneToggled();
762
+ return [3 /*break*/, 13];
763
+ case 12:
764
+ e_3 = _g.sent();
765
+ this.onGetUserMediaError(e_3);
766
+ this.microphone = false;
767
+ return [3 /*break*/, 13];
768
+ case 13: return [2 /*return*/];
769
+ }
770
+ });
771
+ });
408
772
  };
409
773
  RTSeeConference.prototype.disableCamera = function () {
410
- this.localStream.getVideoTracks()[0].enabled = false;
774
+ var _a;
411
775
  this.camera = false;
776
+ var videoTracks = ((_a = this.localStream) === null || _a === void 0 ? void 0 : _a.getVideoTracks()) || [];
777
+ videoTracks.forEach(function (track) {
778
+ track.enabled = false;
779
+ });
412
780
  this.onCameraToggled();
413
781
  };
414
782
  RTSeeConference.prototype.enableCamera = function () {
415
- this.localStream.getVideoTracks()[0].enabled = true;
416
- this.camera = true;
417
- this.onCameraToggled();
783
+ return __awaiter(this, void 0, void 0, function () {
784
+ var liveVideoTracks, _i, _a, peer, videoConstraint, videoStream, newVideoTrack, existingAudioTracks, _b, _c, peer, e_4;
785
+ var _d, _e, _f;
786
+ return __generator(this, function (_g) {
787
+ switch (_g.label) {
788
+ case 0:
789
+ liveVideoTracks = (((_d = this.localStream) === null || _d === void 0 ? void 0 : _d.getVideoTracks()) || []).filter(function (t) { return t.readyState === "live"; });
790
+ if (!(liveVideoTracks.length > 0)) return [3 /*break*/, 5];
791
+ liveVideoTracks.forEach(function (track) {
792
+ track.enabled = true;
793
+ });
794
+ this.isAudioOnly = false;
795
+ this.camera = true;
796
+ _i = 0, _a = this.rtcPeerConnections.connections;
797
+ _g.label = 1;
798
+ case 1:
799
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
800
+ peer = _a[_i];
801
+ return [4 /*yield*/, peer.attachOrReplaceTrack(liveVideoTracks[0], this.localStream)];
802
+ case 2:
803
+ _g.sent();
804
+ _g.label = 3;
805
+ case 3:
806
+ _i++;
807
+ return [3 /*break*/, 1];
808
+ case 4:
809
+ this.onCameraToggled();
810
+ return [2 /*return*/];
811
+ case 5:
812
+ if (!((_e = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _e === void 0 ? void 0 : _e.getUserMedia)) {
813
+ this.camera = false;
814
+ return [2 /*return*/];
815
+ }
816
+ _g.label = 6;
817
+ case 6:
818
+ _g.trys.push([6, 12, , 13]);
819
+ videoConstraint = this.buildVideoConstraint() || {
820
+ width: { ideal: 1280 },
821
+ height: { ideal: 720 },
822
+ frameRate: { ideal: 30, max: 30 },
823
+ };
824
+ return [4 /*yield*/, navigator.mediaDevices.getUserMedia({
825
+ audio: false,
826
+ video: videoConstraint,
827
+ })];
828
+ case 7:
829
+ videoStream = _g.sent();
830
+ newVideoTrack = videoStream.getVideoTracks()[0];
831
+ if (!newVideoTrack) {
832
+ this.camera = false;
833
+ return [2 /*return*/];
834
+ }
835
+ existingAudioTracks = (((_f = this.localStream) === null || _f === void 0 ? void 0 : _f.getAudioTracks()) || []).filter(function (t) { return t.readyState === "live"; });
836
+ this.localStream = new MediaStream(__spreadArray(__spreadArray([], existingAudioTracks, true), [
837
+ newVideoTrack,
838
+ ], false));
839
+ this.getActiveDevicesFromStream(videoStream);
840
+ void this.refreshMediaDevicesList();
841
+ this.isAudioOnly = false;
842
+ this.camera = true;
843
+ _b = 0, _c = this.rtcPeerConnections.connections;
844
+ _g.label = 8;
845
+ case 8:
846
+ if (!(_b < _c.length)) return [3 /*break*/, 11];
847
+ peer = _c[_b];
848
+ return [4 /*yield*/, peer.attachOrReplaceTrack(newVideoTrack, this.localStream)];
849
+ case 9:
850
+ _g.sent();
851
+ _g.label = 10;
852
+ case 10:
853
+ _b++;
854
+ return [3 /*break*/, 8];
855
+ case 11:
856
+ this.onCameraToggled();
857
+ return [3 /*break*/, 13];
858
+ case 12:
859
+ e_4 = _g.sent();
860
+ this.onGetUserMediaError(e_4);
861
+ this.camera = false;
862
+ return [3 /*break*/, 13];
863
+ case 13: return [2 /*return*/];
864
+ }
865
+ });
866
+ });
418
867
  };
419
868
  RTSeeConference.prototype.onCameraToggled = function () {
420
869
  var toggleEvent = {
@@ -422,7 +871,7 @@ var RTSeeConference = /** @class */ (function (_super) {
422
871
  data: {
423
872
  from: this.clientId,
424
873
  roomId: this.roomId,
425
- deviceOn: this.camera,
874
+ deviceOn: Boolean(this.camera),
426
875
  },
427
876
  };
428
877
  this.emit(toggleEvent.name, toggleEvent.data);
@@ -433,7 +882,7 @@ var RTSeeConference = /** @class */ (function (_super) {
433
882
  data: {
434
883
  from: this.clientId,
435
884
  roomId: this.roomId,
436
- deviceOn: this.camera,
885
+ deviceOn: Boolean(this.microphone),
437
886
  },
438
887
  };
439
888
  this.emit(toggleEvent.name, toggleEvent.data);
@@ -450,6 +899,10 @@ var RTSeeConference = /** @class */ (function (_super) {
450
899
  return this.rtcConfiguration;
451
900
  };
452
901
  RTSeeConference.prototype.getDevices = function () {
902
+ var _a;
903
+ if (!((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.enumerateDevices)) {
904
+ return Promise.resolve([]);
905
+ }
453
906
  return navigator.mediaDevices.enumerateDevices();
454
907
  };
455
908
  RTSeeConference.prototype.setAudioInputDevicesList = function () {
@@ -472,53 +925,107 @@ var RTSeeConference = /** @class */ (function (_super) {
472
925
  };
473
926
  RTSeeConference.prototype.setAudioInputDevice = function () {
474
927
  var _this = this;
475
- console.log("audioInputDevice", this.audioInputDeviceId);
476
- if (!this.audioInputDeviceId) {
928
+ var _a;
929
+ if (!this.audioInputDeviceId || !((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia)) {
477
930
  return;
478
931
  }
479
- this.stopLocalStream();
480
932
  if (typeof this.constraints.audio === "object") {
481
933
  this.constraints.audio.deviceId = { exact: this.audioInputDeviceId };
482
934
  }
483
935
  else {
484
936
  this.constraints.audio = { deviceId: { exact: this.audioInputDeviceId } };
485
937
  }
486
- console.log("getting user media", this.constraints.audio);
487
- void this.getUserMedia().then(function () {
488
- _this.rtcPeerConnections.connections.forEach(function (elem) {
489
- elem.connection.getSenders().forEach(function (sender) {
490
- elem.connection.removeTrack(sender);
491
- });
492
- _this.addLocalTracks(elem);
938
+ void navigator.mediaDevices
939
+ .getUserMedia({ audio: this.constraints.audio, video: false })
940
+ .then(function (audioStream) { return __awaiter(_this, void 0, void 0, function () {
941
+ var newAudioTrack, oldAudioTracks, liveVideoTracks, _i, _a, peer;
942
+ var _b, _c;
943
+ return __generator(this, function (_d) {
944
+ switch (_d.label) {
945
+ case 0:
946
+ newAudioTrack = audioStream.getAudioTracks()[0];
947
+ if (!newAudioTrack) {
948
+ return [2 /*return*/];
949
+ }
950
+ newAudioTrack.enabled = Boolean(this.microphone);
951
+ oldAudioTracks = ((_b = this.localStream) === null || _b === void 0 ? void 0 : _b.getAudioTracks()) || [];
952
+ liveVideoTracks = (((_c = this.localStream) === null || _c === void 0 ? void 0 : _c.getVideoTracks()) || []).filter(function (t) { return t.readyState === "live"; });
953
+ oldAudioTracks.forEach(function (t) { return t.stop(); });
954
+ this.localStream = new MediaStream(__spreadArray([
955
+ newAudioTrack
956
+ ], liveVideoTracks, true));
957
+ _i = 0, _a = this.rtcPeerConnections.connections;
958
+ _d.label = 1;
959
+ case 1:
960
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
961
+ peer = _a[_i];
962
+ return [4 /*yield*/, peer.attachOrReplaceTrack(newAudioTrack, this.localStream)];
963
+ case 2:
964
+ _d.sent();
965
+ _d.label = 3;
966
+ case 3:
967
+ _i++;
968
+ return [3 /*break*/, 1];
969
+ case 4: return [2 /*return*/];
970
+ }
493
971
  });
494
- });
972
+ }); })
973
+ .catch(function (e) { return _this.onGetUserMediaError(e); });
495
974
  };
496
975
  RTSeeConference.prototype.setVideoInputDevice = function () {
497
976
  var _this = this;
498
- if (!this.videoInputDeviceId) {
977
+ var _a;
978
+ if (!this.videoInputDeviceId || !((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia)) {
499
979
  return;
500
980
  }
501
- this.stopLocalStream();
502
981
  if (typeof this.constraints.video === "object") {
503
982
  this.constraints.video.deviceId = { exact: this.videoInputDeviceId };
504
983
  }
505
984
  else {
506
985
  this.constraints.video = { deviceId: { exact: this.videoInputDeviceId } };
507
986
  }
508
- void this.getUserMedia().then(function () {
509
- _this.rtcPeerConnections.connections.forEach(function (elem) {
510
- elem.connection.getSenders().forEach(function (sender) {
511
- elem.connection.removeTrack(sender);
512
- });
513
- _this.addLocalTracks(elem);
987
+ void navigator.mediaDevices
988
+ .getUserMedia({ audio: false, video: this.constraints.video })
989
+ .then(function (videoStream) { return __awaiter(_this, void 0, void 0, function () {
990
+ var newVideoTrack, oldVideoTracks, liveAudioTracks, _i, _a, peer;
991
+ var _b, _c;
992
+ return __generator(this, function (_d) {
993
+ switch (_d.label) {
994
+ case 0:
995
+ newVideoTrack = videoStream.getVideoTracks()[0];
996
+ if (!newVideoTrack) {
997
+ return [2 /*return*/];
998
+ }
999
+ newVideoTrack.enabled = Boolean(this.camera);
1000
+ oldVideoTracks = ((_b = this.localStream) === null || _b === void 0 ? void 0 : _b.getVideoTracks()) || [];
1001
+ liveAudioTracks = (((_c = this.localStream) === null || _c === void 0 ? void 0 : _c.getAudioTracks()) || []).filter(function (t) { return t.readyState === "live"; });
1002
+ oldVideoTracks.forEach(function (t) { return t.stop(); });
1003
+ this.localStream = new MediaStream(__spreadArray(__spreadArray([], liveAudioTracks, true), [
1004
+ newVideoTrack,
1005
+ ], false));
1006
+ _i = 0, _a = this.rtcPeerConnections.connections;
1007
+ _d.label = 1;
1008
+ case 1:
1009
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
1010
+ peer = _a[_i];
1011
+ return [4 /*yield*/, peer.attachOrReplaceTrack(newVideoTrack, this.localStream)];
1012
+ case 2:
1013
+ _d.sent();
1014
+ _d.label = 3;
1015
+ case 3:
1016
+ _i++;
1017
+ return [3 /*break*/, 1];
1018
+ case 4: return [2 /*return*/];
1019
+ }
514
1020
  });
515
- });
1021
+ }); })
1022
+ .catch(function (e) { return _this.onGetUserMediaError(e); });
516
1023
  };
517
1024
  RTSeeConference.prototype.setAudioOutputDevice = function () {
518
1025
  if (!this.audioOutputDeviceId) {
519
1026
  return;
520
1027
  }
521
- void this.rtcPeerConnections.setAudioOutputDevice(this.audioInputDeviceId);
1028
+ void this.rtcPeerConnections.setAudioOutputDevice(this.audioOutputDeviceId);
522
1029
  };
523
1030
  RTSeeConference.prototype.refreshMediaDevicesList = function () {
524
1031
  return Promise.all([
@@ -528,14 +1035,21 @@ var RTSeeConference = /** @class */ (function (_super) {
528
1035
  ]);
529
1036
  };
530
1037
  RTSeeConference.prototype.end = function () {
1038
+ this._callSessionId++;
531
1039
  this.isInActiveCall = false;
1040
+ this.isAudioOnly = false;
532
1041
  if (this.screenSharePeer) {
533
- this.screenSharePeer.connection.close();
1042
+ this.screenSharePeer.endCall();
534
1043
  this.screenSharePeer = null;
535
1044
  }
1045
+ if (this.localScreenStream) {
1046
+ this.localScreenStream.getTracks().forEach(function (t) { return t.stop(); });
1047
+ this.localScreenStream = null;
1048
+ }
536
1049
  this.isPresenter = false;
537
1050
  this.rtcPeerConnections.eraseAll();
538
1051
  this.screenSharePeers.eraseAll();
1052
+ this.stopLocalStream();
539
1053
  };
540
1054
  RTSeeConference.prototype.toggleSettings = function () {
541
1055
  this.settingsOpened = !this.settingsOpened;
@@ -550,27 +1064,6 @@ var RTSeeConference = /** @class */ (function (_super) {
550
1064
  }
551
1065
  return this.screenSharePeers.getByPeerId(peerId);
552
1066
  };
553
- RTSeeConference.prototype.attachPeerConnectionEvents = function (peer) {
554
- peer.connection.onconnectionstatechange =
555
- this.connectionStateChangeEnclosure(peer).bind(this);
556
- };
557
- RTSeeConference.prototype.connectionStateChangeEnclosure = function (peer) {
558
- var _this = this;
559
- return function (event) {
560
- var evTarget = event.target;
561
- if (evTarget.connectionState === "disconnected" ||
562
- evTarget.connectionState === "closed" ||
563
- evTarget.connectionState === "failed") {
564
- _this.rtcPeerConnections.removeByPeerId(peer.clientId);
565
- _this.screenSharePeers.removeByPeerId(peer.clientId);
566
- if (_this.screenSharePeer &&
567
- _this.screenSharePeer.clientId === peer.clientId) {
568
- _this.screenSharePeer.connection.close();
569
- _this.screenSharePeer = null;
570
- }
571
- }
572
- };
573
- };
574
1067
  RTSeeConference.prototype.addLocalTracks = function (peer) {
575
1068
  peer.addLocalTracks(this.localStream);
576
1069
  };
@@ -585,6 +1078,12 @@ var RTSeeConference = /** @class */ (function (_super) {
585
1078
  return;
586
1079
  }
587
1080
  this.localStream = stream;
1081
+ this.microphone = stream
1082
+ .getAudioTracks()
1083
+ .some(function (t) { return t.readyState === "live" && t.enabled; });
1084
+ this.camera = stream
1085
+ .getVideoTracks()
1086
+ .some(function (t) { return t.readyState === "live" && t.enabled; });
588
1087
  this.getActiveDevicesFromStream(stream);
589
1088
  void this.refreshMediaDevicesList();
590
1089
  };