@techsee/techsee-media-service 0.1.5 → 0.4.0-alpha.13

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.
@@ -21,6 +21,8 @@ var _constants = require('./constants');
21
21
 
22
22
  var _helper = require('./helper');
23
23
 
24
+ var _tracer = require('../tracer');
25
+
24
26
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
25
27
 
26
28
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -31,14 +33,12 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
31
33
 
32
34
  var isIE11 = new _tsEnvironmentDetect.TsEnvironmentDetect().isIE11();
33
35
 
34
- var _ref = isIE11 ? require('kurento-utils-temasys-mod') : require('kurento-utils'),
35
- WebRtcPeer = _ref.WebRtcPeer; // eslint-disable-line global-require
36
-
37
- var AdapterJS = void 0;
36
+ var _require = require('kurento-utils'),
37
+ WebRtcPeer = _require.WebRtcPeer; // eslint-disable-line global-require
38
38
 
39
- if (isIE11) {
40
- AdapterJS = require('./temasys/adapter-loader')(); // eslint-disable-line global-require
41
- }
39
+ var trace = (0, _tracer.getTracer)('TurnWebRTCSession', function () {
40
+ return window.TurnSessionDebug === true;
41
+ });
42
42
 
43
43
  /**
44
44
  * WebRTC-specific implementation of TsMediaSession.
@@ -51,19 +51,22 @@ if (isIE11) {
51
51
  *
52
52
  * @see TsMediaSession
53
53
  */
54
+
54
55
  var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
55
56
  _inherits(TurnWebRTCSession, _TsMediaSession);
56
57
 
57
- function TurnWebRTCSession(_ref2, localStreamFactory) {
58
- var credentials = _ref2.credentials,
59
- isMobile = _ref2.isMobile,
60
- resolution = _ref2.resolution,
61
- deviceOpts = _ref2.deviceOpts;
58
+ function TurnWebRTCSession(_ref, localStreamFactory) {
59
+ var credentials = _ref.credentials,
60
+ isMobile = _ref.isMobile,
61
+ resolution = _ref.resolution,
62
+ deviceOpts = _ref.deviceOpts;
62
63
 
63
64
  _classCallCheck(this, TurnWebRTCSession);
64
65
 
65
66
  var _this = _possibleConstructorReturn(this, _TsMediaSession.call(this));
66
67
 
68
+ trace('Constructor');
69
+
67
70
  _this._credentials = credentials;
68
71
  _this._isMobile = isMobile;
69
72
  _this._resolution = resolution;
@@ -124,6 +127,7 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
124
127
  var _this2 = this;
125
128
 
126
129
  return new Promise(function (resolve, reject) {
130
+ trace('Connect socket');
127
131
  var socket = io.connect(url, {
128
132
  'forceNew': true,
129
133
  'reconnectionDelay': _constants.SOCKET_RECONNECTION_DELAY,
@@ -133,6 +137,7 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
133
137
  });
134
138
 
135
139
  var done = _.once(function (err) {
140
+ trace(err ? 'Error' : 'Connected', err);
136
141
  err ? reject(err) : resolve();
137
142
  });
138
143
 
@@ -152,14 +157,17 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
152
157
  socket.on('connect_error', done);
153
158
  socket.on('connect_timeout', done);
154
159
 
155
- socket.on(_constants.SOCKET_EVENTS.INCOMING.JOIN_RESPONSE, function (_ref3) {
156
- var sdpAnswer = _ref3.sdpAnswer,
157
- uid = _ref3.uid,
158
- peerId = _ref3.peerId;
160
+ socket.on(_constants.SOCKET_EVENTS.INCOMING.JOIN_RESPONSE, function (_ref2) {
161
+ var sdpAnswer = _ref2.sdpAnswer,
162
+ uid = _ref2.uid,
163
+ peerId = _ref2.peerId;
164
+
165
+ trace('SOCKET_EVENTS.INCOMING.JOIN_RESPONSE');
159
166
 
160
167
  var pc = _this2._pcs[peerId];
161
168
 
162
169
  if (!pc) {
170
+ trace('No peer found');
163
171
  var error = 'pc not found. peer=' + peerId + ' uid=' + uid;
164
172
 
165
173
  _this2.emit(_session.EVENTS.EXCEPTION, { source: 'JOIN_RESPONSE', error: error });
@@ -174,9 +182,12 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
174
182
  return done(error);
175
183
  }
176
184
 
185
+ trace('ANSWER PROCESSED');
186
+
177
187
  // FIXME why don't we have _incomingIce per PC? current assumes we don't have parallel connections, not good
178
188
  _.forEach(_this2._incomingIce, function (candidate) {
179
189
  pc.addIceCandidate(candidate, function (error) {
190
+ trace('ICE ADDED AFTER ANSWER');
180
191
  if (error) {
181
192
  _this2.emit(_session.EVENTS.EXCEPTION, { source: 'JOIN_RESPONSE addIceCandidate', error: error });
182
193
  }
@@ -193,19 +204,22 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
193
204
  });
194
205
  });
195
206
 
196
- socket.on(_constants.SOCKET_EVENTS.INCOMING.ICE_CANDIDATE, function (_ref4) {
197
- var candidate = _ref4.candidate,
198
- peerId = _ref4.peerId;
207
+ socket.on(_constants.SOCKET_EVENTS.INCOMING.ICE_CANDIDATE, function (_ref3) {
208
+ var candidate = _ref3.candidate,
209
+ peerId = _ref3.peerId;
199
210
 
211
+ trace('SOCKET_EVENTS.INCOMING.ICE_CANDIDATE');
200
212
  var pc = _this2._pcs[peerId];
201
213
 
202
214
  if (!pc) {
215
+ trace('NO PEER FOUND ADD TO ICE QUEUE');
203
216
  _this2._incomingIce.push(candidate);
204
217
 
205
218
  return;
206
219
  }
207
220
 
208
221
  pc.addIceCandidate(candidate, function (error) {
222
+ trace('ICE ADDED AFTER INCOMING');
209
223
  if (error) {
210
224
  _this2.emit(_session.EVENTS.EXCEPTION, { source: 'ICE_CANDIDATE addIceCandidate', error: error });
211
225
  }
@@ -213,12 +227,18 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
213
227
  });
214
228
 
215
229
  // Received by publisher, causing him to emit SDP_OFFER
216
- socket.on(_constants.SOCKET_EVENTS.INCOMING.SEND_OFFER, function (_ref5) {
217
- var uid = _ref5.uid,
218
- peerId = _ref5.peerId;
230
+ socket.on(_constants.SOCKET_EVENTS.INCOMING.SEND_OFFER, function (_ref4) {
231
+ var uid = _ref4.uid,
232
+ peerId = _ref4.peerId;
233
+
234
+ trace('SOCKET_EVENTS.INCOMING.SEND_OFFER');
219
235
 
220
236
  _this2._createRTCPeerConnection(peerId).then(function (pc) {
237
+ trace('GENERATING OFFER');
238
+
221
239
  pc.generateOffer(function (error, offer) {
240
+ trace('OFFER GENERATED');
241
+
222
242
  if (error) {
223
243
  return done(error);
224
244
  }
@@ -239,21 +259,26 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
239
259
  });
240
260
 
241
261
  // Received by subscriber, causing him to emit SDP_ANSWER
242
- socket.on(_constants.SOCKET_EVENTS.INCOMING.SDP_OFFER, function (_ref6) {
243
- var sdpOffer = _ref6.sdpOffer,
244
- peerId = _ref6.peerId,
245
- uid = _ref6.uid;
262
+ socket.on(_constants.SOCKET_EVENTS.INCOMING.SDP_OFFER, function (_ref5) {
263
+ var sdpOffer = _ref5.sdpOffer,
264
+ peerId = _ref5.peerId,
265
+ uid = _ref5.uid;
266
+
267
+ trace('SOCKET_EVENTS.INCOMING.SDP_OFFER');
246
268
 
247
269
  Promise.resolve().then(function () {
248
270
  var pc = _this2._pcs[peerId];
249
271
 
250
272
  if (pc) {
273
+ trace('PEER ALREADY EXISTS');
251
274
  return pc;
252
275
  }
253
276
 
254
277
  return _this2._createRTCPeerConnection(peerId);
255
278
  }).then(function (pc) {
279
+ trace('PROCESS OFFER');
256
280
  pc.processOffer(sdpOffer, function (error, sdpAnswer) {
281
+ trace('OFFER PROCESSED');
257
282
  if (error) {
258
283
  return done(error);
259
284
  }
@@ -275,14 +300,18 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
275
300
 
276
301
  // Received by subscriber after a publisher reconnects to room
277
302
  socket.on(_constants.SOCKET_EVENTS.INCOMING.PUBLISHER_RECONNECT, function () {
303
+ trace('SOCKET_EVENTS.INCOMING.PUBLISHER_RECONNECT');
304
+
278
305
  // Clean up and reconnect with a new peer-connection,
279
306
  // to prevent reconnection issues
280
307
 
281
308
  _this2._streams.clear();
282
309
  socket.off();
283
- _.forEach(_this2._pcs, function (_ref7) {
284
- var peerConnection = _ref7.peerConnection;
285
- return peerConnection.close();
310
+ _.forEach(_this2._pcs, function (_ref6) {
311
+ var peerConnection = _ref6.peerConnection;
312
+
313
+ trace('CLOSING PEER');
314
+ peerConnection.close();
286
315
  });
287
316
  _this2._pcs = {};
288
317
 
@@ -291,8 +320,10 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
291
320
 
292
321
  // Received by publisher after a subscriber's socket has been disconnected
293
322
  socket.on(_constants.SOCKET_EVENTS.INCOMING.PEER_DISCONNECTED, function () {
294
- var _ref8 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
295
- peerId = _ref8.peerId;
323
+ var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
324
+ peerId = _ref7.peerId;
325
+
326
+ trace('SOCKET_EVENTS.INCOMING.PEER_DISCONNECTED');
296
327
 
297
328
  if (peerId && _this2._pcs[peerId]) {
298
329
  if (_this2._lastPc === _this2._pcs[peerId]) {
@@ -317,7 +348,10 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
317
348
  var _this3 = this;
318
349
 
319
350
  return new Promise(function (resolve) {
351
+ trace('_setupConnection');
320
352
  pc.peerConnection.oniceconnectionstatechange = function (e) {
353
+ trace('oniceconnectionstatechange', e);
354
+
321
355
  if (_this3.iceCheckingTimeout) {
322
356
  clearTimeout(_this3.iceCheckingTimeout);
323
357
  _this3.iceCheckingTimeout = null;
@@ -353,9 +387,10 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
353
387
  }
354
388
  };
355
389
 
356
- pc.peerConnection.onremovestream = function (_ref9) {
357
- var stream = _ref9.stream;
390
+ pc.peerConnection.onremovestream = function (_ref8) {
391
+ var stream = _ref8.stream;
358
392
 
393
+ trace('onremovestream');
359
394
  _this3._streams.delete(stream.id);
360
395
  };
361
396
 
@@ -371,35 +406,40 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
371
406
  return _constants.PEER_CONNECTION_TYPES.RECV_ONLY;
372
407
  };
373
408
 
374
- TurnWebRTCSession.prototype._connectRTCPeer = function _connectRTCPeer(_ref10) {
409
+ TurnWebRTCSession.prototype._connectRTCPeer = function _connectRTCPeer(_ref9) {
375
410
  var _this4 = this;
376
411
 
377
- var constraints = _ref10.constraints,
378
- configuration = _ref10.configuration,
379
- sessionId = _ref10.sessionId,
380
- isMobile = _ref10.isMobile,
381
- peerId = _ref10.peerId;
412
+ var constraints = _ref9.constraints,
413
+ configuration = _ref9.configuration,
414
+ sessionId = _ref9.sessionId,
415
+ isMobile = _ref9.isMobile,
416
+ peerId = _ref9.peerId;
382
417
 
383
418
  return new Promise(function (resolve, reject) {
419
+ trace('_connectRTCPeer');
420
+
384
421
  var done = _.once(function (err, pc) {
422
+ trace(err ? 'Error' : 'Peer Connected', err);
385
423
  err ? reject(err) : resolve(pc);
386
424
  });
387
425
  var setupWebRtcPeer = function setupWebRtcPeer(stream) {
426
+ trace('setupWebRtcPeer');
388
427
  var peerConnectionType = _this4._getPeerType(constraints, isMobile);
389
428
  var peerConfig = {
390
429
  configuration: configuration,
391
- videoStream: stream,
430
+ videoStream: isMobile ? stream : undefined,
392
431
  mediaConstraints: constraints,
393
432
  onicecandidate: function onicecandidate(candidate) {
433
+ trace('onicecandidate');
394
434
  _this4._socket.emit(_constants.SOCKET_EVENTS.OUTGOING.ICE_CANDIDATE, {
395
435
  sessionId: sessionId,
396
436
  peerId: peerId,
397
437
  candidate: candidate,
398
438
  clientType: isMobile ? _constants.CLIENT_TYPES.PUBLISHER : _constants.CLIENT_TYPES.SUBSCRIBER
399
439
  });
400
- },
401
- AdapterJS: AdapterJS
440
+ }
402
441
  };
442
+
403
443
  var pc = WebRtcPeer[peerConnectionType](peerConfig, function (error) {
404
444
  if (error) {
405
445
  return done(error);
@@ -423,6 +463,7 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
423
463
  };
424
464
 
425
465
  TurnWebRTCSession.prototype._setTrackStateTo = function _setTrackStateTo(state) {
466
+ trace('_setTrackStateTo');
426
467
  return function (track) {
427
468
  if (track.setEnabled) {
428
469
  track.setEnabled(state);
@@ -445,7 +486,9 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
445
486
  TurnWebRTCSession.prototype.connect = function connect() {
446
487
  var _this5 = this;
447
488
 
489
+ trace('connect');
448
490
  return this._connectSocket(this._credentials.url).then(function () {
491
+ trace('Socket connected getting constraints');
449
492
  return (0, _helper.getMediaConstraints)({
450
493
  resolution: _this5._resolution,
451
494
  deviceOpts: _this5._deviceOpts
@@ -474,6 +517,7 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
474
517
  TurnWebRTCSession.prototype._createRTCPeerConnection = function _createRTCPeerConnection(peerId) {
475
518
  var _this6 = this;
476
519
 
520
+ trace('_createRTCPeerConnection');
477
521
  return this._connectRTCPeer({
478
522
  constraints: this._constraints,
479
523
  configuration: this._configuration,
@@ -481,15 +525,20 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
481
525
  isMobile: this._isMobile,
482
526
  peerId: peerId
483
527
  }).then(function (pc) {
484
- pc.peerConnection.getLocalStreams().forEach(function (stream) {
485
- if (!_this6.publisher) {
486
- stream.getTracks().forEach(_this6._setTrackStateTo(false));
487
- }
488
- });
528
+ trace('peer connection created');
529
+
530
+ if (_this6._isMobile) {
531
+ pc.peerConnection.getLocalStreams().forEach(function (stream) {
532
+ if (!_this6.publisher) {
533
+ stream.getTracks().forEach(_this6._setTrackStateTo(false));
534
+ }
535
+ });
536
+ }
489
537
 
490
- pc.peerConnection.onaddstream = function (_ref11) {
491
- var stream = _ref11.stream;
538
+ pc.peerConnection.onaddstream = function (_ref10) {
539
+ var stream = _ref10.stream;
492
540
 
541
+ trace('peer connection onaddstream');
493
542
  var tsStream = new _stream.TsWebRTCStream(_this6, stream);
494
543
 
495
544
  _this6.emit(_session.EVENTS.STREAM_CREATED, tsStream);
@@ -502,19 +551,23 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
502
551
  TurnWebRTCSession.prototype._toggleAllStreams = function _toggleAllStreams(state) {
503
552
  var _this7 = this;
504
553
 
505
- _.forEach(this._pcs, function (pc) {
506
- pc.peerConnection.getLocalStreams().forEach(function (stream) {
507
- stream.getTracks().forEach(_this7._setTrackStateTo(state));
554
+ if (this._isMobile) {
555
+ _.forEach(this._pcs, function (pc) {
556
+ pc.peerConnection.getLocalStreams().forEach(function (stream) {
557
+ stream.getTracks().forEach(_this7._setTrackStateTo(state));
558
+ });
508
559
  });
509
- });
560
+ }
510
561
  };
511
562
 
512
563
  TurnWebRTCSession.prototype._stopStream = function _stopStream(pc) {
513
- pc.peerConnection.getLocalStreams().forEach(function (stream) {
514
- stream.getTracks().forEach(function (track) {
515
- return track.stop();
564
+ if (this._isMobile) {
565
+ pc.peerConnection.getLocalStreams().forEach(function (stream) {
566
+ stream.getTracks().forEach(function (track) {
567
+ return track.stop();
568
+ });
516
569
  });
517
- });
570
+ }
518
571
  };
519
572
 
520
573
  TurnWebRTCSession.prototype._stopAllStreams = function _stopAllStreams() {
@@ -538,6 +591,7 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
538
591
  TurnWebRTCSession.prototype.disconnect = function disconnect() {
539
592
  var _this9 = this;
540
593
 
594
+ trace('disconnect');
541
595
  return Promise.resolve().then(function () {
542
596
  if (_this9.destroyed) {
543
597
  return null;
@@ -545,8 +599,8 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
545
599
 
546
600
  _this9._stopAllStreams();
547
601
 
548
- _.forEach(_this9._pcs, function (_ref12) {
549
- var peerConnection = _ref12.peerConnection;
602
+ _.forEach(_this9._pcs, function (_ref11) {
603
+ var peerConnection = _ref11.peerConnection;
550
604
  return peerConnection.close();
551
605
  });
552
606
  _this9._pcs = {};
@@ -561,6 +615,7 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
561
615
  };
562
616
 
563
617
  TurnWebRTCSession.prototype._waitForPublishingApproval = function _waitForPublishingApproval(caller, cb) {
618
+ trace('_waitForPublishingApproval');
564
619
  try {
565
620
  var pc = _.get(caller, '_lastPc.peerConnection');
566
621
 
@@ -618,6 +673,8 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
618
673
  target = null;
619
674
  }
620
675
 
676
+ trace('createPublisher');
677
+
621
678
  return (this._lastPc ? Promise.resolve() : Promise.delay(_constants.WAIT_FOR_PC_INTERVAL)).then(function () {
622
679
  return new Promise(function (resolve, reject) {
623
680
  _this10._waitForPublishingApproval(_this10, function (err, mediaStream) {
@@ -686,6 +743,7 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
686
743
  TurnWebRTCSession.prototype.publish = function publish(publisher) {
687
744
  var _this11 = this;
688
745
 
746
+ trace('publish');
689
747
  return Promise.resolve().then(function () {
690
748
  if (!publisher) {
691
749
  return;
@@ -713,13 +771,21 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
713
771
  //
714
772
  var video = document.createElement('video');
715
773
 
774
+ //Currently this code is used only in mobile, so when publishing, then we always need only video track
775
+ //to set to video element. Othenwise, when VOIP enabled, technician side will here himself through this video element.
776
+ var videoTracks = publisher.stream._mediaStream.getVideoTracks();
777
+
716
778
  if (!isIE11) {
717
- video.src = URL.createObjectURL(publisher.stream._mediaStream);
779
+ video.srcObject = new MediaStream(videoTracks);
718
780
  }
719
781
 
720
782
  video.playsInline = true;
721
783
  video.autoplay = true;
722
784
 
785
+ if (_this11._isMobile) {
786
+ video.muted = true;
787
+ }
788
+
723
789
  if (isIE11) {
724
790
  publisher._target.appendChild(video);
725
791
  publisher._element = attachMediaStream(video, publisher.stream._mediaStream);
@@ -781,6 +847,7 @@ var TurnWebRTCSession = exports.TurnWebRTCSession = function (_TsMediaSession) {
781
847
  TurnWebRTCSession.prototype.destroyPublisher = function destroyPublisher(publisher) {
782
848
  var _this12 = this;
783
849
 
850
+ trace('destroyPublisher');
784
851
  return new Promise(function (resolve) {
785
852
  _this12._toggleAllStreams(false);
786
853