@techsee/techsee-media-service 4.0.0 → 5.0.1

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.
@@ -185,20 +185,34 @@ var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
185
185
  }
186
186
  };
187
187
 
188
+ var __spreadArrays = void 0 && (void 0).__spreadArrays || function () {
189
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) {
190
+ s += arguments[i].length;
191
+ }
192
+
193
+ for (var r = Array(s), k = 0, i = 0; i < il; i++) {
194
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) {
195
+ r[k] = a[j];
196
+ }
197
+ }
198
+
199
+ return r;
200
+ };
201
+
188
202
  Object.defineProperty(exports, "__esModule", {
189
203
  value: true
190
204
  });
191
205
  exports.OpentokMultiPartyService = void 0;
192
206
 
193
- var MultiPartyService_1 = require("./MultiPartyService");
194
-
195
207
  var MediaConstants_1 = require("../MediaConstants");
196
208
 
197
209
  var MediaUtils_1 = require("../MediaUtils");
198
210
 
211
+ var MediaCapabilitiesService_1 = require("./MediaCapabilitiesService");
212
+
199
213
  var MediaCapabilitiesUtils_1 = require("./MediaCapabilitiesUtils");
200
214
 
201
- var MediaCapabilitiesService_1 = require("./MediaCapabilitiesService");
215
+ var MultiPartyService_1 = require("./MultiPartyService");
202
216
 
203
217
  var trace = MediaUtils_1.getMediaTracer('Multiparty'); // The preferred camera position to use for the video source
204
218
 
@@ -214,74 +228,57 @@ var OpentokMultiPartyService =
214
228
  function (_super) {
215
229
  __extends(OpentokMultiPartyService, _super);
216
230
 
217
- function OpentokMultiPartyService(settings, getParticipantDomId) {
231
+ function OpentokMultiPartyService(settings, getParticipantId) {
218
232
  var _this = _super.call(this, settings) || this;
219
233
 
220
- _this.subscribers = [];
221
- _this.participantCount = 0;
234
+ _this.getParticipantId = getParticipantId;
235
+ _this.subscriberList = [];
222
236
 
223
237
  _this.connect = function (credentials) {
224
238
  try {
225
- _this.session = window === null || window === void 0 ? void 0 : window.OT.initSession(credentials.apiKey, credentials.sessionId);
226
- } catch (err) {
227
- trace.error(JSON.stringify(err));
239
+ _this.session = OT.initSession(credentials.apiKey, credentials.sessionId);
228
240
 
229
- _this.emit(MultiPartyService_1.MULTI_PARTY_EVENTS.connectFailed, {
230
- meta: {
231
- err: JSON.stringify(err)
232
- }
233
- });
241
+ _this.session.on('connectionCreated', _this.connectionCreatedHandler);
234
242
 
235
- return;
236
- }
243
+ _this.session.on('connectionDestroyed', _this.connectionDestroyedHandler);
237
244
 
238
- _this.session.on('connectionCreated', _this.connectionCreatedHandler);
245
+ _this.session.on('streamCreated', _this.streamCreatedHandler);
239
246
 
240
- _this.session.on('connectionDestroyed', _this.connectionDestroyedHandler);
247
+ _this.session.on('streamDestroyed', _this.streamDestroyedHandler);
241
248
 
242
- _this.session.on('streamCreated', _this.streamCreatedHandler);
249
+ _this.session.on('streamPropertyChanged', _this.streamPropertyChangedHandler);
243
250
 
244
- _this.session.on('streamDestroyed', _this.streamDestroyedHandler);
251
+ _this.session.on('sessionDisconnected', _this.sessionDisconnectedHandler);
245
252
 
246
- _this.session.on('streamPropertyChanged', _this.streamPropertyChangedHandler);
253
+ _this.session.connect(credentials.token, function (error) {
254
+ var _a;
247
255
 
248
- _this.session.on('sessionDisconnected', _this.sessionDisconnectedHandler);
256
+ if (error) {
257
+ (_a = _this.session) === null || _a === void 0 ? void 0 : _a.off();
258
+ _this.session = undefined;
249
259
 
250
- _this.session.connect(credentials.token, function (error) {
251
- return __awaiter(_this, void 0, void 0, function () {
252
- var _a;
260
+ _this.traceErrorCallback(error);
253
261
 
254
- return __generator(this, function (_b) {
255
- switch (_b.label) {
256
- case 0:
257
- if (error) {
258
- (_a = this.session) === null || _a === void 0 ? void 0 : _a.off();
259
- this.session = null;
260
- this.traceErrorCallback(error);
261
- this.emit(MultiPartyService_1.MULTI_PARTY_EVENTS.connectFailed, {
262
- meta: {
263
- err: JSON.stringify(error)
264
- }
265
- });
266
- return [2
267
- /*return*/
268
- ];
269
- }
262
+ _this.emit(MultiPartyService_1.MULTI_PARTY_EVENTS.connectFailed, {
263
+ meta: {
264
+ err: JSON.stringify(error)
265
+ }
266
+ });
270
267
 
271
- return [4
272
- /*yield*/
273
- , this.createPublisher(this.settings)];
268
+ return;
269
+ }
274
270
 
275
- case 1:
276
- _b.sent();
271
+ _this.createPublisher(_this.settings);
272
+ });
273
+ } catch (err) {
274
+ trace.error(JSON.stringify(err));
277
275
 
278
- return [2
279
- /*return*/
280
- ];
281
- }
282
- });
276
+ _this.emit(MultiPartyService_1.MULTI_PARTY_EVENTS.connectFailed, {
277
+ meta: {
278
+ err: JSON.stringify(err)
279
+ }
283
280
  });
284
- });
281
+ }
285
282
  };
286
283
 
287
284
  _this.disconnect = function () {
@@ -291,7 +288,6 @@ function (_super) {
291
288
  if (_this.session) {
292
289
  _this.session.disconnect();
293
290
 
294
- _this.participantCount = 0;
295
291
  trace.info('Disconnection from session completed');
296
292
  }
297
293
  } catch (err) {
@@ -526,16 +522,16 @@ function (_super) {
526
522
 
527
523
  _this.initPublisher = function (settings) {
528
524
  return __awaiter(_this, void 0, void 0, function () {
529
- var selectedCamera, isMobile, mediaCapabilitiesService, frontCameraDeviceId, backCameraDeviceId, videoSourceId, mobileCameraType, properties, err_2;
525
+ var selectedCamera, isMobile, mediaCapabilitiesService, frontCameraDeviceId, backCameraDeviceId, videoSourceId, mobileCameraType, err_2;
530
526
 
531
527
  var _this = this;
532
528
 
533
- var _a, _b;
529
+ var _a, _b, _c;
534
530
 
535
- return __generator(this, function (_c) {
536
- switch (_c.label) {
531
+ return __generator(this, function (_d) {
532
+ switch (_d.label) {
537
533
  case 0:
538
- _c.trys.push([0, 2,, 3]);
534
+ _d.trys.push([0, 2,, 3]);
539
535
 
540
536
  selectedCamera = settings.selectedCamera;
541
537
  isMobile = MediaCapabilitiesUtils_1.checkIsMobile();
@@ -544,11 +540,11 @@ function (_super) {
544
540
  , MediaCapabilitiesService_1.MediaCapabilitiesService.getInstance()];
545
541
 
546
542
  case 1:
547
- mediaCapabilitiesService = _c.sent();
543
+ mediaCapabilitiesService = _d.sent();
548
544
  frontCameraDeviceId = mediaCapabilitiesService.frontCameraDeviceId, backCameraDeviceId = mediaCapabilitiesService.backCameraDeviceId;
549
545
  videoSourceId = (selectedCamera === MediaConstants_1.MultiPartyCameraTypes.FRONT ? frontCameraDeviceId : backCameraDeviceId) || undefined;
550
546
  mobileCameraType = settings.selectedCamera === MediaConstants_1.MultiPartyCameraTypes.FRONT ? OpentokFacingMode.USER : OpentokFacingMode.ENVIRONMENT;
551
- properties = {
547
+ this.currentPublisher = OT.initPublisher(this.getParticipantId(((_b = (_a = this.session) === null || _a === void 0 ? void 0 : _a.connection) === null || _b === void 0 ? void 0 : _b.connectionId) || ''), {
552
548
  insertMode: 'append',
553
549
  fitMode: 'cover',
554
550
  width: '100%',
@@ -557,14 +553,13 @@ function (_super) {
557
553
  buttonDisplayMode: 'off',
558
554
  archiveStatusDisplayMode: 'off',
559
555
  audioLevelDisplayMode: 'off',
560
- backgroundImageURI: ((_a = this.branding) === null || _a === void 0 ? void 0 : _a.backgroundImageURI) || MultiPartyService_1.DEFAULT_AVATAR_URI
556
+ backgroundImageURI: ((_c = this.branding) === null || _c === void 0 ? void 0 : _c.backgroundImageURI) || MultiPartyService_1.DEFAULT_AVATAR_URI
561
557
  },
562
558
  publishAudio: !settings.startWithAudioMuted,
563
559
  publishVideo: settings.cameraEnabled,
564
560
  videoSource: !isMobile ? videoSourceId : undefined,
565
561
  facingMode: isMobile || !videoSourceId ? mobileCameraType : undefined
566
- };
567
- this.currentPublisher = window === null || window === void 0 ? void 0 : window.OT.initPublisher(this.getParticipantId((_b = this.session) === null || _b === void 0 ? void 0 : _b.connection.connectionId), properties, this.traceErrorCallback);
562
+ }, this.traceErrorCallback);
568
563
 
569
564
  if (!this.currentPublisher) {
570
565
  trace.error('failed to init publisher');
@@ -591,7 +586,7 @@ function (_super) {
591
586
  var connection = (_a = _this.session) === null || _a === void 0 ? void 0 : _a.connection;
592
587
  var connectionId = connection === null || connection === void 0 ? void 0 : connection.connectionId;
593
588
 
594
- var participantId = _this.getParticipantId(connection.connectionId);
589
+ var participantId = _this.getParticipantId(connectionId);
595
590
 
596
591
  var isOwnConnection = _this.ownConnectionId === connectionId;
597
592
 
@@ -645,7 +640,7 @@ function (_super) {
645
640
  , 3];
646
641
 
647
642
  case 2:
648
- err_2 = _c.sent();
643
+ err_2 = _d.sent();
649
644
  trace.error(JSON.stringify(err_2));
650
645
  throw err_2;
651
646
 
@@ -658,71 +653,93 @@ function (_super) {
658
653
  });
659
654
  };
660
655
 
661
- _this.streamCreatedHandler = function (event) {
656
+ _this.reinitializeStreams = function (settings) {
662
657
  return __awaiter(_this, void 0, void 0, function () {
663
- var stream, connection, participantId, properties, subscriber_1;
664
-
665
658
  var _this = this;
666
659
 
667
- var _a, _b, _c, _d, _e;
660
+ return __generator(this, function (_a) {
661
+ switch (_a.label) {
662
+ case 0:
663
+ if (this.currentPublisher) {
664
+ this.currentPublisher.destroy();
665
+ this.currentPublisher = undefined;
666
+ }
667
+
668
+ return [4
669
+ /*yield*/
670
+ , this.createPublisher(settings)];
671
+
672
+ case 1:
673
+ _a.sent();
674
+
675
+ this.subscriberList = this.subscriberList.map(function (s) {
676
+ var _a;
677
+
678
+ var connectionId = s.connectionId,
679
+ subscriber = s.subscriber;
680
+ var stream = subscriber.stream;
681
+ (_a = _this.session) === null || _a === void 0 ? void 0 : _a.unsubscribe(subscriber);
682
+ return __assign(__assign({}, s), {
683
+ subscriber: _this.createSubscriber(stream, connectionId)
684
+ });
685
+ });
686
+ return [2
687
+ /*return*/
688
+ ];
689
+ }
690
+ });
691
+ });
692
+ };
693
+
694
+ _this.streamCreatedHandler = function (event) {
695
+ return __awaiter(_this, void 0, void 0, function () {
696
+ var stream, connection, connectionId, participantId, subscriber;
697
+
698
+ var _a, _b;
668
699
 
669
- return __generator(this, function (_f) {
670
- switch (_f.label) {
700
+ return __generator(this, function (_c) {
701
+ switch (_c.label) {
671
702
  case 0:
672
703
  trace.info('streamCreatedHandler', event.stream);
673
704
  stream = event.stream;
674
705
  connection = stream.connection;
675
- participantId = this.getParticipantId(connection.connectionId);
706
+ connectionId = connection.connectionId;
707
+ participantId = this.getParticipantId(connectionId);
676
708
  return [4
677
709
  /*yield*/
678
710
  , MediaCapabilitiesService_1.MediaCapabilitiesService.setDevicesList()];
679
711
 
680
712
  case 1:
681
- _f.sent();
713
+ _c.sent();
682
714
 
683
715
  this.emit(MultiPartyService_1.MULTI_PARTY_EVENTS.streamCreated, {
684
716
  event: event,
685
717
  participantId: participantId,
686
718
  startWithAudioMuted: this.settings.startWithAudioMuted,
687
- isOwnConnection: this.ownConnectionId === ((_b = (_a = event === null || event === void 0 ? void 0 : event.stream) === null || _a === void 0 ? void 0 : _a.connection) === null || _b === void 0 ? void 0 : _b.connectionId),
688
- hasVideo: (_c = event === null || event === void 0 ? void 0 : event.stream) === null || _c === void 0 ? void 0 : _c.hasVideo,
689
- hasAudio: (_d = event === null || event === void 0 ? void 0 : event.stream) === null || _d === void 0 ? void 0 : _d.hasAudio
719
+ isOwnConnection: this.ownConnectionId === connectionId,
720
+ hasVideo: (_a = event === null || event === void 0 ? void 0 : event.stream) === null || _a === void 0 ? void 0 : _a.hasVideo,
721
+ hasAudio: (_b = event === null || event === void 0 ? void 0 : event.stream) === null || _b === void 0 ? void 0 : _b.hasAudio
690
722
  });
691
723
 
692
- if (!this.ownConnectionId || !stream || connection.connectionId === this.ownConnectionId) {
724
+ if (!this.ownConnectionId || !stream || connectionId === this.ownConnectionId) {
693
725
  return [2
694
726
  /*return*/
695
727
  ];
696
728
  }
697
729
 
698
- if (this.subscribers.length >= MultiPartyService_1.maxParticipantsNumber - 1) {
730
+ if (this.subscriberList.length >= MultiPartyService_1.maxParticipantsNumber - 1) {
699
731
  trace.info("a new subscriber cannot be added, as the number of participants reached its limit: " + MultiPartyService_1.maxParticipantsNumber);
700
- } else {
701
- properties = {
702
- insertMode: 'append',
703
- fitMode: 'cover',
704
- width: '100%',
705
- height: '100%',
706
- style: {
707
- buttonDisplayMode: 'off',
708
- audioLevelDisplayMode: 'off',
709
- backgroundImageURI: ((_e = this.branding) === null || _e === void 0 ? void 0 : _e.backgroundImageURI) || MultiPartyService_1.DEFAULT_AVATAR_URI
710
- }
711
- };
712
- subscriber_1 = event.target.subscribe(event.stream, participantId, properties, function (err) {
713
- _this.traceErrorCallback(err);
714
-
715
- subscriber_1 === null || subscriber_1 === void 0 ? void 0 : subscriber_1.on('videoElementCreated', function (event) {
716
- trace.info('videoElementCreated', event);
717
- });
718
- });
719
- this.subscribers.push({
720
- subscriber: subscriber_1,
721
- connectionId: connection.connectionId,
722
- clientRole: this.getClientRole(connection)
723
- });
732
+ return [2
733
+ /*return*/
734
+ ];
724
735
  }
725
736
 
737
+ subscriber = this.createSubscriber(stream, connectionId);
738
+ this.subscriberList.push({
739
+ subscriber: subscriber,
740
+ connectionId: connectionId,
741
+ clientRole: this.getClientRole(connection)
742
+ });
726
743
  return [2
727
744
  /*return*/
728
745
  ];
@@ -758,7 +775,7 @@ function (_super) {
758
775
  participantId: _this.getParticipantId((_a = event === null || event === void 0 ? void 0 : event.stream) === null || _a === void 0 ? void 0 : _a.connection.connectionId)
759
776
  });
760
777
 
761
- _this.subscribers = _this.subscribers.filter(function (subscriber) {
778
+ _this.subscriberList = _this.subscriberList.filter(function (subscriber) {
762
779
  return subscriber.connectionId !== destroyedConnectionId;
763
780
  });
764
781
  };
@@ -771,9 +788,7 @@ function (_super) {
771
788
  return;
772
789
  }
773
790
 
774
- ++_this.participantCount;
775
-
776
- _this.emit(MultiPartyService_1.MULTI_PARTY_EVENTS.participantsNumberChanged, _this.participantCount);
791
+ _this.emit(MultiPartyService_1.MULTI_PARTY_EVENTS.participantsNumberChanged, _this.subscriberList.length);
777
792
 
778
793
  var clientRole = _this.getClientRole(connection);
779
794
 
@@ -802,11 +817,9 @@ function (_super) {
802
817
  return;
803
818
  }
804
819
 
805
- --_this.participantCount;
806
-
807
- _this.emit(MultiPartyService_1.MULTI_PARTY_EVENTS.participantsNumberChanged, _this.participantCount);
820
+ _this.emit(MultiPartyService_1.MULTI_PARTY_EVENTS.participantsNumberChanged, _this.subscriberList.length);
808
821
 
809
- _this.session = null;
822
+ _this.session = undefined;
810
823
  };
811
824
 
812
825
  _this.traceErrorCallback = function (error) {
@@ -814,16 +827,36 @@ function (_super) {
814
827
  };
815
828
 
816
829
  _this.sessionDisconnectedHandler = function (event) {
817
- var _a;
830
+ trace.info('sessionDisconnectedHandler', event); // ToDo: Below line makes no sense (according to the OT types). This is not how OT signals work.
831
+ // this?.session?.signal({ retryAfterReconnect: true });
832
+ };
833
+
834
+ _this.createSubscriber = function (stream, connectionId) {
835
+ var _a; // ToDo: OT session can be undefined. This needs to be fixed.
836
+
818
837
 
819
- trace.info('sessionDisconnectedHandler', event);
820
- (_a = _this === null || _this === void 0 ? void 0 : _this.session) === null || _a === void 0 ? void 0 : _a.signal({
821
- retryAfterReconnect: true
838
+ var subscriber = _this.session.subscribe(stream, _this.getParticipantId(connectionId), {
839
+ insertMode: 'append',
840
+ fitMode: 'cover',
841
+ width: '100%',
842
+ height: '100%',
843
+ style: {
844
+ buttonDisplayMode: 'off',
845
+ audioLevelDisplayMode: 'off',
846
+ backgroundImageURI: ((_a = _this.branding) === null || _a === void 0 ? void 0 : _a.backgroundImageURI) || MultiPartyService_1.DEFAULT_AVATAR_URI
847
+ }
848
+ }, function (err) {
849
+ _this.traceErrorCallback(err);
850
+
851
+ subscriber === null || subscriber === void 0 ? void 0 : subscriber.on('videoElementCreated', function (event) {
852
+ trace.info('videoElementCreated', event);
853
+ });
822
854
  });
855
+
856
+ return subscriber;
823
857
  };
824
858
 
825
859
  _this.branding = settings.branding;
826
- _this.getParticipantId = getParticipantDomId;
827
860
  return _this;
828
861
  }
829
862
 
@@ -831,7 +864,14 @@ function (_super) {
831
864
  get: function get() {
832
865
  var _a, _b;
833
866
 
834
- return (_b = (_a = this.session) === null || _a === void 0 ? void 0 : _a.connection) === null || _b === void 0 ? void 0 : _b.connectionId;
867
+ return ((_b = (_a = this.session) === null || _a === void 0 ? void 0 : _a.connection) === null || _b === void 0 ? void 0 : _b.connectionId) || '';
868
+ },
869
+ enumerable: false,
870
+ configurable: true
871
+ });
872
+ Object.defineProperty(OpentokMultiPartyService.prototype, "subscribers", {
873
+ get: function get() {
874
+ return __spreadArrays(this.subscriberList);
835
875
  },
836
876
  enumerable: false,
837
877
  configurable: true