@techsee/techsee-media-service 999.0.2-switch → 999.0.3-alpha2

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.
Files changed (150) hide show
  1. package/lib/LocalStreamManager.d.ts +12 -0
  2. package/lib/LocalStreamManager.d.ts.map +1 -1
  3. package/lib/LocalStreamManager.js +440 -283
  4. package/lib/LocalStreamManager.js.map +1 -1
  5. package/lib/MediaConstants.d.ts +14 -4
  6. package/lib/MediaConstants.d.ts.map +1 -1
  7. package/lib/MediaConstants.js +83 -38
  8. package/lib/MediaConstants.js.map +1 -1
  9. package/lib/MediaContracts.d.ts +17 -14
  10. package/lib/MediaContracts.d.ts.map +1 -1
  11. package/lib/MediaContracts.js +3 -3
  12. package/lib/MediaContracts.js.map +1 -1
  13. package/lib/MediaPublisher.d.ts +3 -3
  14. package/lib/MediaPublisher.d.ts.map +1 -1
  15. package/lib/MediaPublisher.js +46 -35
  16. package/lib/MediaPublisher.js.map +1 -1
  17. package/lib/MediaServiceBase.d.ts +21 -7
  18. package/lib/MediaServiceBase.d.ts.map +1 -1
  19. package/lib/MediaServiceBase.js +924 -586
  20. package/lib/MediaServiceBase.js.map +1 -1
  21. package/lib/MediaSession/MediaServer.d.ts +45 -0
  22. package/lib/MediaSession/MediaServer.d.ts.map +1 -0
  23. package/lib/MediaSession/MediaServer.js +538 -0
  24. package/lib/MediaSession/MediaServer.js.map +1 -0
  25. package/lib/MediaSession/MediaSessionBase.d.ts +10 -1
  26. package/lib/MediaSession/MediaSessionBase.d.ts.map +1 -1
  27. package/lib/MediaSession/MediaSessionBase.js +128 -66
  28. package/lib/MediaSession/MediaSessionBase.js.map +1 -1
  29. package/lib/MediaSession/SessionOpentok.d.ts +11 -9
  30. package/lib/MediaSession/SessionOpentok.d.ts.map +1 -1
  31. package/lib/MediaSession/SessionOpentok.js +500 -352
  32. package/lib/MediaSession/SessionOpentok.js.map +1 -1
  33. package/lib/MediaSession/SessionTurn.d.ts +4 -1
  34. package/lib/MediaSession/SessionTurn.d.ts.map +1 -1
  35. package/lib/MediaSession/SessionTurn.js +611 -422
  36. package/lib/MediaSession/SessionTurn.js.map +1 -1
  37. package/lib/MediaSession/TurnConstants.js +31 -30
  38. package/lib/MediaSession/TurnConstants.js.map +1 -1
  39. package/lib/MediaSubscriber.d.ts +11 -11
  40. package/lib/MediaSubscriber.d.ts.map +1 -1
  41. package/lib/MediaSubscriber.js +236 -192
  42. package/lib/MediaSubscriber.js.map +1 -1
  43. package/lib/MediaUtils/Compatibility.d.ts +2 -1
  44. package/lib/MediaUtils/Compatibility.d.ts.map +1 -1
  45. package/lib/MediaUtils/Compatibility.js +130 -85
  46. package/lib/MediaUtils/Compatibility.js.map +1 -1
  47. package/lib/MediaUtils/MediaDomUtils.d.ts +3 -2
  48. package/lib/MediaUtils/MediaDomUtils.d.ts.map +1 -1
  49. package/lib/MediaUtils/MediaDomUtils.js +235 -174
  50. package/lib/MediaUtils/MediaDomUtils.js.map +1 -1
  51. package/lib/MediaUtils/MediaTracer.js +9 -4
  52. package/lib/MediaUtils/MediaTracer.js.map +1 -1
  53. package/lib/MediaUtils/index.d.ts +1 -0
  54. package/lib/MediaUtils/index.d.ts.map +1 -0
  55. package/lib/MediaUtils/index.js +16 -0
  56. package/lib/MediaUtils/index.js.map +1 -0
  57. package/lib/MultiParty/Contracts.d.ts +16 -0
  58. package/lib/MultiParty/Contracts.d.ts.map +1 -0
  59. package/lib/MultiParty/Contracts.js +6 -0
  60. package/lib/MultiParty/Contracts.js.map +1 -0
  61. package/lib/MultiParty/DetectWebRtcService.d.ts +16 -0
  62. package/lib/MultiParty/DetectWebRtcService.d.ts.map +1 -0
  63. package/lib/MultiParty/DetectWebRtcService.js +79 -0
  64. package/lib/MultiParty/DetectWebRtcService.js.map +1 -0
  65. package/lib/MultiParty/MediaCapabilitiesService.d.ts +23 -0
  66. package/lib/MultiParty/MediaCapabilitiesService.d.ts.map +1 -0
  67. package/lib/MultiParty/MediaCapabilitiesService.js +369 -0
  68. package/lib/MultiParty/MediaCapabilitiesService.js.map +1 -0
  69. package/lib/MultiParty/MediaCapabilitiesUtils.d.ts +5 -0
  70. package/lib/MultiParty/MediaCapabilitiesUtils.d.ts.map +1 -0
  71. package/lib/MultiParty/MediaCapabilitiesUtils.js +305 -0
  72. package/lib/MultiParty/MediaCapabilitiesUtils.js.map +1 -0
  73. package/lib/MultiParty/MultiPartyService.d.ts +40 -0
  74. package/lib/MultiParty/MultiPartyService.d.ts.map +1 -0
  75. package/lib/MultiParty/MultiPartyService.js +70 -0
  76. package/lib/MultiParty/MultiPartyService.js.map +1 -0
  77. package/lib/MultiParty/MultiPartyServiceFactory.d.ts +3 -0
  78. package/lib/MultiParty/MultiPartyServiceFactory.d.ts.map +1 -0
  79. package/lib/MultiParty/MultiPartyServiceFactory.js +21 -0
  80. package/lib/MultiParty/MultiPartyServiceFactory.js.map +1 -0
  81. package/lib/MultiParty/OpentokMultiPartyService.d.ts +29 -0
  82. package/lib/MultiParty/OpentokMultiPartyService.d.ts.map +1 -0
  83. package/lib/MultiParty/OpentokMultiPartyService.js +888 -0
  84. package/lib/MultiParty/OpentokMultiPartyService.js.map +1 -0
  85. package/lib/MultiParty/VideoLayoutType.d.ts +4 -0
  86. package/lib/MultiParty/VideoLayoutType.d.ts.map +1 -0
  87. package/lib/MultiParty/VideoLayoutType.js +13 -0
  88. package/lib/MultiParty/VideoLayoutType.js.map +1 -0
  89. package/lib/MultiParty/index.d.ts +4 -0
  90. package/lib/MultiParty/index.d.ts.map +1 -0
  91. package/lib/MultiParty/index.js +61 -0
  92. package/lib/MultiParty/index.js.map +1 -0
  93. package/lib/MultiParty/opentok.d.ts +503 -0
  94. package/lib/TechseeMediaStream.d.ts +6 -6
  95. package/lib/TechseeMediaStream.d.ts.map +1 -1
  96. package/lib/TechseeMediaStream.js +59 -53
  97. package/lib/TechseeMediaStream.js.map +1 -1
  98. package/lib/oldCode/constants.js +20 -15
  99. package/lib/oldCode/constants.js.map +1 -1
  100. package/lib/oldCode/event-emitter.js +45 -22
  101. package/lib/oldCode/event-emitter.js.map +1 -1
  102. package/lib/oldCode/index.js +48 -52
  103. package/lib/oldCode/index.js.map +1 -1
  104. package/lib/oldCode/opentok/session.js +261 -236
  105. package/lib/oldCode/opentok/session.js.map +1 -1
  106. package/lib/oldCode/opentok/stream.js +283 -269
  107. package/lib/oldCode/opentok/stream.js.map +1 -1
  108. package/lib/oldCode/publisher.js +96 -71
  109. package/lib/oldCode/publisher.js.map +1 -1
  110. package/lib/oldCode/service.js +294 -272
  111. package/lib/oldCode/service.js.map +1 -1
  112. package/lib/oldCode/session.js +82 -60
  113. package/lib/oldCode/session.js.map +1 -1
  114. package/lib/oldCode/stream.js +153 -125
  115. package/lib/oldCode/stream.js.map +1 -1
  116. package/lib/oldCode/subscriber.js +60 -35
  117. package/lib/oldCode/subscriber.js.map +1 -1
  118. package/lib/oldCode/tracer.d.ts +3 -2
  119. package/lib/oldCode/tracer.d.ts.map +1 -1
  120. package/lib/oldCode/tracer.js +181 -130
  121. package/lib/oldCode/tracer.js.map +1 -1
  122. package/lib/oldCode/utils/ImageFixer.js +67 -44
  123. package/lib/oldCode/utils/ImageFixer.js.map +1 -1
  124. package/lib/oldCode/webrtc/constants.js +102 -110
  125. package/lib/oldCode/webrtc/constants.js.map +1 -1
  126. package/lib/oldCode/webrtc/helper.js +176 -124
  127. package/lib/oldCode/webrtc/helper.js.map +1 -1
  128. package/lib/oldCode/webrtc/session-kms.js +408 -393
  129. package/lib/oldCode/webrtc/session-kms.js.map +1 -1
  130. package/lib/oldCode/webrtc/session-loopback.js +245 -227
  131. package/lib/oldCode/webrtc/session-loopback.js.map +1 -1
  132. package/lib/oldCode/webrtc/session-turn.js +705 -658
  133. package/lib/oldCode/webrtc/session-turn.js.map +1 -1
  134. package/lib/oldCode/webrtc/session-turn.v2.js +480 -471
  135. package/lib/oldCode/webrtc/session-turn.v2.js.map +1 -1
  136. package/lib/oldCode/webrtc/session.js +12 -6
  137. package/lib/oldCode/webrtc/session.js.map +1 -1
  138. package/lib/oldCode/webrtc/stream.js +526 -504
  139. package/lib/oldCode/webrtc/stream.js.map +1 -1
  140. package/lib/oldCode/webrtc/temasys/adapter-loader.js +5 -6
  141. package/lib/oldCode/webrtc/temasys/adapter-loader.js.map +1 -1
  142. package/lib/oldCode/webrtc/temasys/adapter.js +5264 -5192
  143. package/lib/oldCode/webrtc/temasys/adapter.js.map +1 -1
  144. package/lib/oldCode/webrtc/webrtc-ie-shim.js +3357 -2927
  145. package/lib/oldCode/webrtc/webrtc-ie-shim.js.map +1 -1
  146. package/lib/qos/raw-qos.js +348 -273
  147. package/lib/qos/raw-qos.js.map +1 -1
  148. package/lib/webrtc-ie-shim.js +3386 -2985
  149. package/lib/webrtc-ie-shim.js.map +1 -1
  150. package/package.json +54 -42
@@ -1,583 +1,592 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
- exports.__esModule = true;
4
- exports.TurnWebRtcSessionV2 = undefined;
3
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
5
4
 
6
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.TurnWebRtcSessionV2 = void 0;
7
9
 
8
- var _session = require('../session');
10
+ var _session = require("../session");
9
11
 
10
- var _socket = require('socket.io-client');
12
+ var io = _interopRequireWildcard(require("socket.io-client"));
11
13
 
12
- var io = _interopRequireWildcard(_socket);
14
+ var _constants = require("./constants");
13
15
 
14
- var _constants = require('./constants');
16
+ var _stream = require("./stream");
15
17
 
16
- var _stream = require('./stream');
18
+ var _tracer = require("../tracer");
17
19
 
18
- var _tracer = require('../tracer');
20
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
19
21
 
20
- 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; } }
22
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
23
 
22
24
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23
25
 
24
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
26
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
25
27
 
26
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
28
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
27
29
 
28
- var tracer = (0, _tracer.getTracer)();
30
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
29
31
 
30
- //Currently this version supports subscribing only. Publishing will be implemented as needed.
32
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
31
33
 
32
- var TurnWebRtcSessionV2 = exports.TurnWebRtcSessionV2 = function (_TsMediaSession) {
33
- _inherits(TurnWebRtcSessionV2, _TsMediaSession);
34
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
34
35
 
35
- _createClass(TurnWebRtcSessionV2, [{
36
- key: 'id',
37
- get: function get() {
38
- return this._credentials.sessionId;
39
- }
40
- }]);
41
-
42
- function TurnWebRtcSessionV2(_ref) {
43
- var credentials = _ref.credentials,
44
- constraints = _ref.constraints,
45
- clientType = _ref.clientType,
46
- streamFactory = _ref.streamFactory;
47
-
48
- _classCallCheck(this, TurnWebRtcSessionV2);
36
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
49
37
 
50
- var _this = _possibleConstructorReturn(this, _TsMediaSession.call(this));
38
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
51
39
 
52
- _this._socket = null;
53
- _this._localPeer = null;
54
- _this._pendingPeersIce = {};
55
- _this._peerConnections = new Map();
40
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
56
41
 
57
- _this._credentials = credentials;
58
- _this._constraints = constraints;
59
- _this._selfClientType = clientType;
60
- _this._localStreamFactory = streamFactory;
42
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
61
43
 
62
- _this._connectPromise = null;
63
-
64
- var iceServers = credentials.ice.map(function (item) {
65
- return {
66
- urls: item.url,
67
- username: item.login,
68
- credential: item.password
69
- };
70
- });
44
+ var tracer = (0, _tracer.getTracer)(); //Currently this version supports subscribing only. Publishing will be implemented as needed.
71
45
 
72
- _this._configuration = {
73
- iceServers: iceServers,
74
- iceTransportPolicy: 'relay'
75
- };
46
+ var TurnWebRtcSessionV2 = /*#__PURE__*/function (_TsMediaSession) {
47
+ _inherits(TurnWebRtcSessionV2, _TsMediaSession);
76
48
 
77
- _this._joinResponseHandler = _this._joinResponseHandler.bind(_this);
78
- _this._iceCandidateReceivedHandler = _this._iceCandidateReceivedHandler.bind(_this);
79
- _this._sendOfferHandler = _this._sendOfferHandler.bind(_this);
80
- _this._sdpOfferHandler = _this._sdpOfferHandler.bind(_this);
81
- _this._initiatorReconnectHandler = _this._initiatorReconnectHandler.bind(_this);
82
- _this._peerDisconnectedHandler = _this._peerDisconnectedHandler.bind(_this);
49
+ var _super = _createSuper(TurnWebRtcSessionV2);
83
50
 
84
- _this._extendPeerConnection();
85
- return _this;
86
- }
51
+ function TurnWebRtcSessionV2(_ref) {
52
+ var _this;
87
53
 
88
- //#region Socket Connectivity
54
+ var credentials = _ref.credentials,
55
+ constraints = _ref.constraints,
56
+ clientType = _ref.clientType,
57
+ streamFactory = _ref.streamFactory;
89
58
 
90
- TurnWebRtcSessionV2.prototype.connect = function connect() {
91
- var _this2 = this;
59
+ _classCallCheck(this, TurnWebRtcSessionV2);
92
60
 
93
- var connectOnly = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
61
+ _this = _super.call(this);
62
+ _this._socket = null;
63
+ _this._localPeer = null;
64
+ _this._pendingPeersIce = {};
65
+ _this._peerConnections = new Map();
66
+ _this._credentials = credentials;
67
+ _this._constraints = constraints;
68
+ _this._selfClientType = clientType;
69
+ _this._localStreamFactory = streamFactory;
70
+ _this._connectPromise = null;
71
+ var iceServers = credentials.ice.map(function (item) {
72
+ return {
73
+ urls: item.url,
74
+ username: item.login,
75
+ credential: item.password
76
+ };
77
+ });
78
+ _this._configuration = {
79
+ iceServers: iceServers,
80
+ iceTransportPolicy: 'relay'
81
+ };
82
+ _this._joinResponseHandler = _this._joinResponseHandler.bind(_assertThisInitialized(_this));
83
+ _this._iceCandidateReceivedHandler = _this._iceCandidateReceivedHandler.bind(_assertThisInitialized(_this));
84
+ _this._sendOfferHandler = _this._sendOfferHandler.bind(_assertThisInitialized(_this));
85
+ _this._sdpOfferHandler = _this._sdpOfferHandler.bind(_assertThisInitialized(_this));
86
+ _this._initiatorReconnectHandler = _this._initiatorReconnectHandler.bind(_assertThisInitialized(_this));
87
+ _this._peerDisconnectedHandler = _this._peerDisconnectedHandler.bind(_assertThisInitialized(_this));
94
88
 
95
- if (this._connectPromise) {
96
- tracer.trace('already connecting');
97
- return this._connectPromise;
98
- }
89
+ _this._extendPeerConnection();
99
90
 
100
- tracer.trace('connect');
91
+ return _this;
92
+ } //#region Socket Connectivity
101
93
 
102
- this._connectPromise = this._connectSocket(this._credentials.url, connectOnly).then(function () {
103
- tracer.trace('Joining SessionId: ' + _this2._credentials.sessionId);
104
94
 
105
- _this2._socket.emit(_constants.SOCKET_EVENTS.OUTGOING.JOIN_ROOM, {
106
- clientType: _this2._selfClientType,
107
- sessionId: _this2._credentials.sessionId
108
- });
95
+ _createClass(TurnWebRtcSessionV2, [{
96
+ key: "id",
97
+ get: function get() {
98
+ return this._credentials.sessionId;
99
+ }
100
+ }, {
101
+ key: "connect",
102
+ value: function connect() {
103
+ var _this2 = this;
109
104
 
110
- _this2._connectPromise = null;
111
- }).catch(function (error) {
112
- setTimeout(function () {
113
- return _this2._connectPromise = null;
114
- });
115
- _this2.emit(_session.SESSION_EVENTS.EXCEPTION, { source: 'connect', error: error });
116
- throw error;
117
- });
105
+ var connectOnly = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
118
106
 
107
+ if (this._connectPromise) {
108
+ tracer.trace('already connecting');
119
109
  return this._connectPromise;
120
- };
121
-
122
- TurnWebRtcSessionV2.prototype.disconnect = function disconnect() {
123
- var _this3 = this;
110
+ }
124
111
 
125
- tracer.trace('Disconnecting from media session');
112
+ tracer.trace('connect');
113
+ this._connectPromise = this._connectSocket(this._credentials.url, connectOnly).then(function () {
114
+ tracer.trace('Joining SessionId: ' + _this2._credentials.sessionId);
126
115
 
127
- this._peerConnections.forEach(function (peerConnection) {
128
- _this3._destroyPeerConnection(peerConnection);
116
+ _this2._socket.emit(_constants.SOCKET_EVENTS.OUTGOING.JOIN_ROOM, {
117
+ clientType: _this2._selfClientType,
118
+ sessionId: _this2._credentials.sessionId
129
119
  });
130
120
 
131
- this._socket.off();
132
- this._socket.disconnect();
121
+ _this2._connectPromise = null;
122
+ })["catch"](function (error) {
123
+ setTimeout(function () {
124
+ return _this2._connectPromise = null;
125
+ });
133
126
 
134
- this.emit(_session.SESSION_EVENTS.DISCONNECTED, { reason: 'disconnected' });
135
- };
127
+ _this2.emit(_session.SESSION_EVENTS.EXCEPTION, {
128
+ source: 'connect',
129
+ error: error
130
+ });
136
131
 
137
- TurnWebRtcSessionV2.prototype._connectSocket = function _connectSocket(url, connectOnly) {
138
- var _this4 = this;
132
+ throw error;
133
+ });
134
+ return this._connectPromise;
135
+ }
136
+ }, {
137
+ key: "disconnect",
138
+ value: function disconnect() {
139
+ var _this3 = this;
139
140
 
140
- return new Promise(function (resolve, reject) {
141
- var socket = _this4._socket = io.connect(url, {
142
- 'forceNew': true,
143
- 'reconnectionDelay': _constants.SOCKET_RECONNECTION_DELAY,
144
- 'reconnectionDelayMax': _constants.SOCKET_RECONNECTION_DELAY_MAX,
145
- 'reconnectionAttempts': _constants.SOCKET_RECONNECTION_ATTEMPTS,
146
- 'path': _constants.SOCKET_PATH
147
- });
141
+ tracer.trace('Disconnecting from media session');
148
142
 
149
- var done = _.once(function (err) {
150
- err ? reject(err) : resolve();
151
- });
143
+ this._peerConnections.forEach(function (peerConnection) {
144
+ _this3._destroyPeerConnection(peerConnection);
145
+ });
152
146
 
153
- socket.once('connect', function () {
154
- return done();
155
- });
156
- socket.on(_constants.SOCKET_EVENTS.INCOMING.ERROR, done);
157
- socket.on('connect_error', done);
158
- socket.on('connect_timeout', done);
147
+ this._socket.off();
159
148
 
160
- socket.on('reconnect_attempt', function (args) {
161
- tracer.trace('Session Socket reconnect_attempt', args);
162
- });
149
+ this._socket.disconnect();
163
150
 
164
- socket.on('reconnecting', function (args) {
165
- tracer.trace('Session Socket reconnecting', args);
166
- });
151
+ this.emit(_session.SESSION_EVENTS.DISCONNECTED, {
152
+ reason: 'disconnected'
153
+ });
154
+ }
155
+ }, {
156
+ key: "_connectSocket",
157
+ value: function _connectSocket(url, connectOnly) {
158
+ var _this4 = this;
159
+
160
+ return new Promise(function (resolve, reject) {
161
+ var socket = _this4._socket = io.connect(url, {
162
+ 'forceNew': true,
163
+ 'reconnectionDelay': _constants.SOCKET_RECONNECTION_DELAY,
164
+ 'reconnectionDelayMax': _constants.SOCKET_RECONNECTION_DELAY_MAX,
165
+ 'reconnectionAttempts': _constants.SOCKET_RECONNECTION_ATTEMPTS,
166
+ 'path': _constants.SOCKET_PATH
167
+ });
167
168
 
168
- socket.on('disconnect', function (reason) {
169
- tracer.trace('Session Socket disconnected, reason:', reason);
170
- // The initial example of differentiating between "forced" disconnect and "network" disconnect is taken from:
171
- // https://socket.io/docs/client-api/#Event-%E2%80%98disconnect%E2%80%99
172
- //if (reason !== 'io server disconnect' && reason !== 'io client disconnect') {
173
- // the disconnection was initiated by the server or by client, you need to call (connect) manually
174
- _this4.disconnect();
175
- //}
176
- });
169
+ var done = _.once(function (err) {
170
+ err ? reject(err) : resolve();
171
+ });
177
172
 
178
- if (!connectOnly) {
179
- //PUBLISHING NOT SUPPORTED YET
180
- //socket.on(SOCKET_EVENTS.INCOMING.SEND_OFFER, this._sendOfferHandler);
181
- //socket.on(SOCKET_EVENTS.INCOMING.JOIN_RESPONSE, this._joinResponseHandler);
182
- //socket.on(SOCKET_EVENTS.INCOMING.PEER_DISCONNECTED, this._peerDisconnectedHandler);
173
+ socket.once('connect', function () {
174
+ return done();
175
+ });
176
+ socket.on(_constants.SOCKET_EVENTS.INCOMING.ERROR, done);
177
+ socket.on('connect_error', done);
178
+ socket.on('connect_timeout', done);
179
+ socket.on('reconnect_attempt', function (args) {
180
+ tracer.trace('Session Socket reconnect_attempt', args);
181
+ });
182
+ socket.on('reconnecting', function (args) {
183
+ tracer.trace('Session Socket reconnecting', args);
184
+ });
185
+ socket.on('disconnect', function (reason) {
186
+ tracer.trace('Session Socket disconnected, reason:', reason); // The initial example of differentiating between "forced" disconnect and "network" disconnect is taken from:
187
+ // https://socket.io/docs/client-api/#Event-%E2%80%98disconnect%E2%80%99
188
+ //if (reason !== 'io server disconnect' && reason !== 'io client disconnect') {
189
+ // the disconnection was initiated by the server or by client, you need to call (connect) manually
183
190
 
184
- socket.on(_constants.SOCKET_EVENTS.INCOMING.SDP_OFFER, _this4._sdpOfferHandler);
185
- socket.on(_constants.SOCKET_EVENTS.INCOMING.PUBLISHER_RECONNECT, _this4._initiatorReconnectHandler);
191
+ _this4.disconnect(); //}
186
192
 
187
- socket.on(_constants.SOCKET_EVENTS.INCOMING.ICE_CANDIDATE, _this4._iceCandidateReceivedHandler);
188
- }
189
193
  });
190
- };
191
-
192
- //#endregion
193
194
 
195
+ if (!connectOnly) {
196
+ //PUBLISHING NOT SUPPORTED YET
197
+ //socket.on(SOCKET_EVENTS.INCOMING.SEND_OFFER, this._sendOfferHandler);
198
+ //socket.on(SOCKET_EVENTS.INCOMING.JOIN_RESPONSE, this._joinResponseHandler);
199
+ //socket.on(SOCKET_EVENTS.INCOMING.PEER_DISCONNECTED, this._peerDisconnectedHandler);
200
+ socket.on(_constants.SOCKET_EVENTS.INCOMING.SDP_OFFER, _this4._sdpOfferHandler);
201
+ socket.on(_constants.SOCKET_EVENTS.INCOMING.PUBLISHER_RECONNECT, _this4._initiatorReconnectHandler);
202
+ socket.on(_constants.SOCKET_EVENTS.INCOMING.ICE_CANDIDATE, _this4._iceCandidateReceivedHandler);
203
+ }
204
+ });
205
+ } //#endregion
194
206
  //#region WebRTC Initiator - session Initiator (User on mobile device)
195
-
196
207
  // Received by Initiator, causing him to emit SDP_OFFER
197
208
 
198
-
199
- TurnWebRtcSessionV2.prototype._sendOfferHandler = function _sendOfferHandler(_ref2) {
200
- var _this5 = this;
201
-
202
- var uid = _ref2.uid,
203
- peerId = _ref2.peerId;
204
-
205
- tracer.trace('_sendOfferHandler');
206
-
207
- var peerConnection = this._createPeerConnection(peerId, _constants.CLIENT_TYPES.GUEST);
208
- var offerParams = { offerToReceiveAudio: true, offerToReceiveVideo: true };
209
-
210
- this._localStreamFactory(this._constraints).then(function (localStream) {
211
- tracer.trace('_sendOfferHandler stream and peer created');
212
-
213
- var peerConnectionStream = new _stream.TsWebRTCStream(_this5, localStream.clone(), _constants.DEFAULT_STREAM_NAMES.MOBILE_CLIENT_VIDEO_STREAM, false);
214
-
215
- peerConnectionStream.peerConnection = peerConnection;
216
-
217
- localStream.getTracks().forEach(function (track) {
218
- tracer.trace('Adding local tracks to peer connection');
219
- peerConnection.addTrack(track, peerConnectionStream._mediaStream);
220
- });
221
-
222
- _this5.registerStream(peerConnectionStream.id, peerConnectionStream);
223
- }).then(function () {
224
- return peerConnection.createOffer(offerParams);
225
- }).then(function (offerDesc) {
226
- tracer.trace('Initiator has created sdp offer', { offerDesc: offerDesc });
227
-
228
- _this5._socket.emit(_constants.SOCKET_EVENTS.OUTGOING.SDP_OFFER, {
229
- uid: uid,
230
- peerId: peerId,
231
- sessionId: _this5._credentials.sessionId,
232
- sdpOffer: offerDesc.sdp
233
- });
234
-
235
- return peerConnection.setLocalDescription(offerDesc).then(function () {
236
- tracer.trace('Local description was set on');
237
- });
238
- }).catch(function (e) {
239
- tracer.trace('Error', e);
209
+ }, {
210
+ key: "_sendOfferHandler",
211
+ value: function _sendOfferHandler(_ref2) {
212
+ var _this5 = this;
213
+
214
+ var uid = _ref2.uid,
215
+ peerId = _ref2.peerId;
216
+ tracer.trace('_sendOfferHandler');
217
+
218
+ var peerConnection = this._createPeerConnection(peerId, _constants.CLIENT_TYPES.GUEST);
219
+
220
+ var offerParams = {
221
+ offerToReceiveAudio: true,
222
+ offerToReceiveVideo: true
223
+ };
224
+
225
+ this._localStreamFactory(this._constraints).then(function (localStream) {
226
+ tracer.trace('_sendOfferHandler stream and peer created');
227
+ var peerConnectionStream = new _stream.TsWebRTCStream(_this5, localStream.clone(), _constants.DEFAULT_STREAM_NAMES.MOBILE_CLIENT_VIDEO_STREAM, false);
228
+ peerConnectionStream.peerConnection = peerConnection;
229
+ localStream.getTracks().forEach(function (track) {
230
+ tracer.trace('Adding local tracks to peer connection');
231
+ peerConnection.addTrack(track, peerConnectionStream._mediaStream);
240
232
  });
241
- };
242
-
243
- // Received by the Initiator: Indicates that publish should be started
244
-
245
-
246
- TurnWebRtcSessionV2.prototype._joinResponseHandler = function _joinResponseHandler(_ref3) {
247
- var _this6 = this;
248
-
249
- var sdpAnswer = _ref3.sdpAnswer,
250
- uid = _ref3.uid,
251
- peerId = _ref3.peerId;
252
-
253
- tracer.trace('joinResponse Received', sdpAnswer);
254
-
255
- this._withPeerConnection(peerId, function (peerConnection) {
256
- var answer = new RTCSessionDescription({
257
- type: 'answer',
258
- sdp: sdpAnswer
259
- });
260
233
 
261
- peerConnection.setRemoteDescription(answer).then(function () {
262
- tracer.trace('Remote description was set', answer);
263
-
264
- _this6._processQueuedIceCandidates(peerId);
265
- }).catch(function (e) {
266
- tracer.trace('Error', e);
267
- });
234
+ _this5.registerStream(peerConnectionStream.id, peerConnectionStream);
235
+ }).then(function () {
236
+ return peerConnection.createOffer(offerParams);
237
+ }).then(function (offerDesc) {
238
+ tracer.trace('Initiator has created sdp offer', {
239
+ offerDesc: offerDesc
268
240
  });
269
- };
270
-
271
- // Received by Initiator after a guests socket has been disconnected
272
-
273
241
 
274
- TurnWebRtcSessionV2.prototype._peerDisconnectedHandler = function _peerDisconnectedHandler(_ref4) {
275
- var peerId = _ref4.peerId;
276
-
277
- tracer.trace('_peerDisconnectedHandler');
278
- };
242
+ _this5._socket.emit(_constants.SOCKET_EVENTS.OUTGOING.SDP_OFFER, {
243
+ uid: uid,
244
+ peerId: peerId,
245
+ sessionId: _this5._credentials.sessionId,
246
+ sdpOffer: offerDesc.sdp
247
+ });
279
248
 
280
- //#endregion WebRTC Initiator - session Initiator (User on mobile device)
249
+ return peerConnection.setLocalDescription(offerDesc).then(function () {
250
+ tracer.trace('Local description was set on');
251
+ });
252
+ })["catch"](function (e) {
253
+ tracer.trace('Error', e);
254
+ });
255
+ } // Received by the Initiator: Indicates that publish should be started
256
+
257
+ }, {
258
+ key: "_joinResponseHandler",
259
+ value: function _joinResponseHandler(_ref3) {
260
+ var _this6 = this;
261
+
262
+ var sdpAnswer = _ref3.sdpAnswer,
263
+ uid = _ref3.uid,
264
+ peerId = _ref3.peerId;
265
+ tracer.trace('joinResponse Received', sdpAnswer);
266
+
267
+ this._withPeerConnection(peerId, function (peerConnection) {
268
+ var answer = new RTCSessionDescription({
269
+ type: 'answer',
270
+ sdp: sdpAnswer
271
+ });
272
+ peerConnection.setRemoteDescription(answer).then(function () {
273
+ tracer.trace('Remote description was set', answer);
281
274
 
275
+ _this6._processQueuedIceCandidates(peerId);
276
+ })["catch"](function (e) {
277
+ tracer.trace('Error', e);
278
+ });
279
+ });
280
+ } // Received by Initiator after a guests socket has been disconnected
281
+
282
+ }, {
283
+ key: "_peerDisconnectedHandler",
284
+ value: function _peerDisconnectedHandler(_ref4) {
285
+ var peerId = _ref4.peerId;
286
+ tracer.trace('_peerDisconnectedHandler');
287
+ } //#endregion WebRTC Initiator - session Initiator (User on mobile device)
282
288
  //#region WebRTC Guest - session Guest (Agent on dashboard platform)
283
-
284
289
  // Received by Guest, causing him to emit SDP_ANSWER
285
290
 
291
+ }, {
292
+ key: "_sdpOfferHandler",
293
+ value: function _sdpOfferHandler(_ref5) {
294
+ var _this7 = this;
286
295
 
287
- TurnWebRtcSessionV2.prototype._sdpOfferHandler = function _sdpOfferHandler(_ref5) {
288
- var _this7 = this;
289
-
290
- var sdpOffer = _ref5.sdpOffer,
291
- peerId = _ref5.peerId,
292
- uid = _ref5.uid;
293
-
294
- tracer.trace('sdpOffer Received', { sdpOffer: sdpOffer });
295
-
296
- var peerConnection = this._createPeerConnection(peerId, _constants.CLIENT_TYPES.INITIATOR);
297
-
298
- var subscriberInitChain = Promise.resolve();
299
-
300
- if (this._constraints.audio) {
301
- subscriberInitChain = subscriberInitChain.then(function () {
302
- tracer.trace('Creating agent local streams');
303
-
304
- return _this7._localStreamFactory(_this7._constraints).then(function (localStream) {
305
- localStream.getTracks().forEach(function (track) {
306
- tracer.trace('Adding local tracks to peer connection');
307
-
308
- if (track.kind === 'audio') {
296
+ var sdpOffer = _ref5.sdpOffer,
297
+ peerId = _ref5.peerId,
298
+ uid = _ref5.uid;
299
+ tracer.trace('sdpOffer Received', {
300
+ sdpOffer: sdpOffer
301
+ });
309
302
 
310
- var stream = new MediaStream([track]);
311
- var streamName = _constants.DEFAULT_STREAM_NAMES.DASHBOARD_AUDIO_STREAM;
312
- var agentAudioStream = new _stream.TsWebRTCStream(_this7, stream, streamName, false);
303
+ var peerConnection = this._createPeerConnection(peerId, _constants.CLIENT_TYPES.INITIATOR);
313
304
 
314
- _this7.registerStream(agentAudioStream.id, agentAudioStream);
315
- peerConnection.addTrack(track, peerConnection.peerLogicalStream);
305
+ var subscriberInitChain = Promise.resolve();
316
306
 
317
- tracer.trace('Agent audio stream added to peerConnection');
318
- }
319
- });
320
- });
321
- });
322
- }
323
-
324
- subscriberInitChain.then(function () {
325
- var remoteDesc = new RTCSessionDescription({ type: 'offer', sdp: sdpOffer });
326
-
327
- return peerConnection.setRemoteDescription(remoteDesc).then(function () {
328
- tracer.trace('Remote description was set', remoteDesc);
329
-
330
- return peerConnection.createAnswer().then(function (sdpAnswer) {
331
- tracer.trace('_sdpOfferHandler answer created', sdpAnswer);
307
+ if (this._constraints.audio) {
308
+ subscriberInitChain = subscriberInitChain.then(function () {
309
+ tracer.trace('Creating agent local streams');
310
+ return _this7._localStreamFactory(_this7._constraints).then(function (localStream) {
311
+ localStream.getTracks().forEach(function (track) {
312
+ tracer.trace('Adding local tracks to peer connection');
332
313
 
333
- _this7._socket.emit(_constants.SOCKET_EVENTS.OUTGOING.SDP_ANSWER, {
334
- uid: uid,
335
- peerId: peerId,
336
- sessionId: _this7._credentials.sessionId,
337
- sdpAnswer: sdpAnswer.sdp
338
- });
314
+ if (track.kind === 'audio') {
315
+ var stream = new MediaStream([track]);
316
+ var streamName = _constants.DEFAULT_STREAM_NAMES.DASHBOARD_AUDIO_STREAM;
317
+ var agentAudioStream = new _stream.TsWebRTCStream(_this7, stream, streamName, false);
339
318
 
340
- return peerConnection.setLocalDescription(sdpAnswer).then(function () {
341
- tracer.trace('_sdpOfferHandler local desc was set');
319
+ _this7.registerStream(agentAudioStream.id, agentAudioStream);
342
320
 
343
- _this7._processQueuedIceCandidates(peerId);
344
- });
345
- });
321
+ peerConnection.addTrack(track, peerConnection.peerLogicalStream);
322
+ tracer.trace('Agent audio stream added to peerConnection');
323
+ }
346
324
  });
325
+ });
347
326
  });
327
+ }
348
328
 
349
- return subscriberInitChain.catch(function (e) {
350
- tracer.trace('Error in _sdpOfferHandler', e);
351
- throw e;
329
+ subscriberInitChain.then(function () {
330
+ var remoteDesc = new RTCSessionDescription({
331
+ type: 'offer',
332
+ sdp: sdpOffer
352
333
  });
353
- };
354
-
355
- // Received by Guest after an Initiator reconnects to room
356
-
357
-
358
- TurnWebRtcSessionV2.prototype._initiatorReconnectHandler = function _initiatorReconnectHandler() {
359
- tracer.trace('_initiatorReconnectHandler');
334
+ return peerConnection.setRemoteDescription(remoteDesc).then(function () {
335
+ tracer.trace('Remote description was set', remoteDesc);
336
+ return peerConnection.createAnswer().then(function (sdpAnswer) {
337
+ tracer.trace('_sdpOfferHandler answer created', sdpAnswer);
338
+
339
+ _this7._socket.emit(_constants.SOCKET_EVENTS.OUTGOING.SDP_ANSWER, {
340
+ uid: uid,
341
+ peerId: peerId,
342
+ sessionId: _this7._credentials.sessionId,
343
+ sdpAnswer: sdpAnswer.sdp
344
+ });
360
345
 
361
- var initiatorPeer = this._getInitiatorPeer();
346
+ return peerConnection.setLocalDescription(sdpAnswer).then(function () {
347
+ tracer.trace('_sdpOfferHandler local desc was set');
362
348
 
363
- if (initiatorPeer) {
364
- this._destroyPeerConnection(initiatorPeer);
365
- }
349
+ _this7._processQueuedIceCandidates(peerId);
350
+ });
351
+ });
352
+ });
353
+ });
354
+ return subscriberInitChain["catch"](function (e) {
355
+ tracer.trace('Error in _sdpOfferHandler', e);
356
+ throw e;
357
+ });
358
+ } // Received by Guest after an Initiator reconnects to room
366
359
 
367
- this.emit(_session.SESSION_EVENTS.RECONNECT_TURN_SERVER);
368
- };
360
+ }, {
361
+ key: "_initiatorReconnectHandler",
362
+ value: function _initiatorReconnectHandler() {
363
+ tracer.trace('_initiatorReconnectHandler');
369
364
 
370
- TurnWebRtcSessionV2.prototype._getInitiatorPeer = function _getInitiatorPeer() {
371
- var peer = this._getPeerConnectionsByType(_constants.CLIENT_TYPES.INITIATOR);
365
+ var initiatorPeer = this._getInitiatorPeer();
372
366
 
373
- return peer.length > 0 ? peer[0] : null;
374
- };
367
+ if (initiatorPeer) {
368
+ this._destroyPeerConnection(initiatorPeer);
369
+ }
375
370
 
376
- //#endregion WebRTC Guest - session Guest (Agent on dashboard platform)
371
+ this.emit(_session.SESSION_EVENTS.RECONNECT_TURN_SERVER);
372
+ }
373
+ }, {
374
+ key: "_getInitiatorPeer",
375
+ value: function _getInitiatorPeer() {
376
+ var peer = this._getPeerConnectionsByType(_constants.CLIENT_TYPES.INITIATOR);
377
377
 
378
+ return peer.length > 0 ? peer[0] : null;
379
+ } //#endregion WebRTC Guest - session Guest (Agent on dashboard platform)
378
380
  //#region Peer Connection
379
381
 
380
- TurnWebRtcSessionV2.prototype._createPeerConnection = function _createPeerConnection(peerId, peerConnectionType) {
381
- var _this8 = this;
382
-
383
- if (this._peerConnections.get(peerId)) {
384
- throw new Error(this._selfClientType + ' peer connection for ' + peerId + ' already exists');
385
- }
386
-
387
- var peerConnection = new RTCPeerConnection(this._configuration);
388
-
389
- peerConnection.peerId = peerId;
390
- peerConnection.peerType = peerConnectionType;
391
- peerConnection.peerLogicalStream = new MediaStream();
392
-
393
- peerConnection.oniceconnectionstatechange = function (state) {
394
- tracer.trace('ICE Connection state for peer ' + peerId + ' was changed to: ' + state.currentTarget.iceConnectionState, state);
395
- var cnnState = state.currentTarget.iceConnectionState;
396
- if (cnnState === 'disconnected') {
397
- //According to https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState
398
- //Connection can become active again, so we will do nothing.
399
- // if (peerConnection.peerType === CLIENT_TYPES.INITIATOR) {
400
- // //this._destroyPeerConnection(peerConnection);
401
- // this.disconnect();
402
- // }
403
- } else if (cnnState === 'failed' || cnnState === 'closed') {
404
- //According to https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState
405
- //When iceConnectionState is failed or closed, the peer connection can be destroyed.
406
- //In our case, we disconnecting from media session, and when session will be needed again, it will be connected.
407
- _this8.disconnect();
408
- }
409
- };
410
-
411
- peerConnection.onicecandidate = function (event) {
412
- if (event.candidate) {
413
- tracer.trace(_this8._selfClientType + ' peerConn.onicecandidate', event.candidate);
414
- _this8._socket.emit(_constants.SOCKET_EVENTS.OUTGOING.ICE_CANDIDATE, {
415
- sessionId: _this8._credentials.sessionId,
416
- peerId: peerId,
417
- candidate: event.candidate,
418
- clientType: _this8._selfClientType
419
- });
420
- } else {
421
- tracer.trace('ICE Candidate is null in peerConnection', event);
422
- }
423
- };
424
-
425
- if (this._selfClientType === _constants.CLIENT_TYPES.GUEST) {
426
- peerConnection.ontrack = function (trackEvent) {
427
- var track = trackEvent.track,
428
- streams = trackEvent.streams;
429
-
430
-
431
- tracer.trace('Stream created from peer connection track: ' + track.kind, track, streams.length > 0 ? streams[0] : 'NO STREAM');
432
-
433
- var tsStream = new _stream.TsWebRTCStream(_this8, new MediaStream([track]));
434
-
435
- tsStream.peerConnection = peerConnection;
436
- tsStream.remote = true;
437
-
438
- _this8.registerStream(tsStream.id, tsStream);
439
-
440
- _this8.emit(_session.SESSION_EVENTS.STREAM_CREATED, tsStream);
441
- };
442
- }
443
-
444
- this._peerConnections.set(peerId, peerConnection);
445
-
446
- tracer.trace(this._selfClientType + ' peerConnection created');
447
-
448
- return peerConnection;
449
- };
450
-
451
- TurnWebRtcSessionV2.prototype._destroyPeerConnection = function _destroyPeerConnection(peerConnection) {
452
- var _this9 = this;
453
-
454
- if (this._selfClientType === _constants.CLIENT_TYPES.GUEST) {
455
- this._getRemoteStreams(peerConnection).forEach(function (stream) {
456
- _this9.unregisterStream(stream.id);
457
- _this9.emit(_session.SESSION_EVENTS.STREAM_DESTROYED, { stream: stream });
458
- });
382
+ }, {
383
+ key: "_createPeerConnection",
384
+ value: function _createPeerConnection(peerId, peerConnectionType) {
385
+ var _this8 = this;
386
+
387
+ if (this._peerConnections.get(peerId)) {
388
+ throw new Error("".concat(this._selfClientType, " peer connection for ").concat(peerId, " already exists"));
389
+ }
390
+
391
+ var peerConnection = new RTCPeerConnection(this._configuration);
392
+ peerConnection.peerId = peerId;
393
+ peerConnection.peerType = peerConnectionType;
394
+ peerConnection.peerLogicalStream = new MediaStream();
395
+
396
+ peerConnection.oniceconnectionstatechange = function (state) {
397
+ tracer.trace("ICE Connection state for peer ".concat(peerId, " was changed to: ").concat(state.currentTarget.iceConnectionState), state);
398
+ var cnnState = state.currentTarget.iceConnectionState;
399
+
400
+ if (cnnState === 'disconnected') {//According to https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState
401
+ //Connection can become active again, so we will do nothing.
402
+ // if (peerConnection.peerType === CLIENT_TYPES.INITIATOR) {
403
+ // //this._destroyPeerConnection(peerConnection);
404
+ // this.disconnect();
405
+ // }
406
+ } else if (cnnState === 'failed' || cnnState === 'closed') {
407
+ //According to https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState
408
+ //When iceConnectionState is failed or closed, the peer connection can be destroyed.
409
+ //In our case, we disconnecting from media session, and when session will be needed again, it will be connected.
410
+ _this8.disconnect();
459
411
  }
460
-
461
- if (this._selfClientType === _constants.CLIENT_TYPES.INITIATOR && this._peerConnections.size === 0) {
462
- //TODO - ALEX: When initiator will be implemented, here we need to destroy local stream (or better raise, event).
412
+ };
413
+
414
+ peerConnection.onicecandidate = function (event) {
415
+ if (event.candidate) {
416
+ tracer.trace("".concat(_this8._selfClientType, " peerConn.onicecandidate"), event.candidate);
417
+
418
+ _this8._socket.emit(_constants.SOCKET_EVENTS.OUTGOING.ICE_CANDIDATE, {
419
+ sessionId: _this8._credentials.sessionId,
420
+ peerId: peerId,
421
+ candidate: event.candidate,
422
+ clientType: _this8._selfClientType
423
+ });
424
+ } else {
425
+ tracer.trace('ICE Candidate is null in peerConnection', event);
463
426
  }
427
+ };
464
428
 
465
- this._peerConnections.delete(peerConnection.peerId);
466
- peerConnection.close();
467
- };
429
+ if (this._selfClientType === _constants.CLIENT_TYPES.GUEST) {
430
+ peerConnection.ontrack = function (trackEvent) {
431
+ var track = trackEvent.track,
432
+ streams = trackEvent.streams;
433
+ tracer.trace("Stream created from peer connection track: ".concat(track.kind), track, streams.length > 0 ? streams[0] : 'NO STREAM');
434
+ var tsStream = new _stream.TsWebRTCStream(_this8, new MediaStream([track]));
435
+ tsStream.peerConnection = peerConnection;
436
+ tsStream.remote = true;
468
437
 
469
- TurnWebRtcSessionV2.prototype._getRemoteStreams = function _getRemoteStreams(peerConnection) {
470
- var result = [];
438
+ _this8.registerStream(tsStream.id, tsStream);
471
439
 
472
- this.streams.forEach(function (stream) {
473
- if (stream.peerConnection === peerConnection && stream.remote) {
474
- result.push(stream);
475
- }
476
- });
477
-
478
- return result;
479
- };
440
+ _this8.emit(_session.SESSION_EVENTS.STREAM_CREATED, tsStream);
441
+ };
442
+ }
480
443
 
481
- TurnWebRtcSessionV2.prototype._getPeerConnectionsByType = function _getPeerConnectionsByType(peerType) {
482
- var result = [];
444
+ this._peerConnections.set(peerId, peerConnection);
483
445
 
484
- this._peerConnections.forEach(function (peerConnection) {
485
- if (peerConnection.peerType === peerType) {
486
- result.push(peerConnection);
487
- }
446
+ tracer.trace("".concat(this._selfClientType, " peerConnection created"));
447
+ return peerConnection;
448
+ }
449
+ }, {
450
+ key: "_destroyPeerConnection",
451
+ value: function _destroyPeerConnection(peerConnection) {
452
+ var _this9 = this;
453
+
454
+ if (this._selfClientType === _constants.CLIENT_TYPES.GUEST) {
455
+ this._getRemoteStreams(peerConnection).forEach(function (stream) {
456
+ _this9.unregisterStream(stream.id);
457
+
458
+ _this9.emit(_session.SESSION_EVENTS.STREAM_DESTROYED, {
459
+ stream: stream
460
+ });
488
461
  });
462
+ }
489
463
 
490
- return result;
491
- };
464
+ if (this._selfClientType === _constants.CLIENT_TYPES.INITIATOR && this._peerConnections.size === 0) {//TODO - ALEX: When initiator will be implemented, here we need to destroy local stream (or better raise, event).
465
+ }
492
466
 
493
- TurnWebRtcSessionV2.prototype._withPeerConnection = function _withPeerConnection(peerId, callback) {
494
- if (this._peerConnections.get(peerId)) {
495
- return callback(this._peerConnections.get(peerId));
496
- }
467
+ this._peerConnections["delete"](peerConnection.peerId);
497
468
 
498
- return null;
499
- };
500
-
501
- TurnWebRtcSessionV2.prototype._extendPeerConnection = function _extendPeerConnection() {
502
- //TODO - Alex: Need to modify IE shim, and add these methods inside shim itself
503
-
504
- if (!RTCPeerConnection.prototype.peerId) {
505
- RTCPeerConnection.prototype.peerId = '';
506
- }
507
-
508
- if (!RTCPeerConnection.prototype.peerType) {
509
- RTCPeerConnection.prototype.peerType = null;
469
+ peerConnection.close();
470
+ }
471
+ }, {
472
+ key: "_getRemoteStreams",
473
+ value: function _getRemoteStreams(peerConnection) {
474
+ var result = [];
475
+ this.streams.forEach(function (stream) {
476
+ if (stream.peerConnection === peerConnection && stream.remote) {
477
+ result.push(stream);
510
478
  }
511
-
512
- if (!RTCPeerConnection.prototype.peerLogicalStream) {
513
- RTCPeerConnection.prototype.peerLogicalStream = null;
479
+ });
480
+ return result;
481
+ }
482
+ }, {
483
+ key: "_getPeerConnectionsByType",
484
+ value: function _getPeerConnectionsByType(peerType) {
485
+ var result = [];
486
+
487
+ this._peerConnections.forEach(function (peerConnection) {
488
+ if (peerConnection.peerType === peerType) {
489
+ result.push(peerConnection);
514
490
  }
515
- };
516
-
517
- //#endregion
491
+ });
518
492
 
493
+ return result;
494
+ }
495
+ }, {
496
+ key: "_withPeerConnection",
497
+ value: function _withPeerConnection(peerId, callback) {
498
+ if (this._peerConnections.get(peerId)) {
499
+ return callback(this._peerConnections.get(peerId));
500
+ }
501
+
502
+ return null;
503
+ }
504
+ }, {
505
+ key: "_extendPeerConnection",
506
+ value: function _extendPeerConnection() {
507
+ //TODO - Alex: Need to modify IE shim, and add these methods inside shim itself
508
+ if (!RTCPeerConnection.prototype.peerId) {
509
+ RTCPeerConnection.prototype.peerId = '';
510
+ }
511
+
512
+ if (!RTCPeerConnection.prototype.peerType) {
513
+ RTCPeerConnection.prototype.peerType = null;
514
+ }
515
+
516
+ if (!RTCPeerConnection.prototype.peerLogicalStream) {
517
+ RTCPeerConnection.prototype.peerLogicalStream = null;
518
+ }
519
+ } //#endregion
519
520
  //#region ICE Candidates
520
521
 
521
- TurnWebRtcSessionV2.prototype._iceCandidateReceivedHandler = function _iceCandidateReceivedHandler(_ref6) {
522
- var candidate = _ref6.candidate,
523
- peerId = _ref6.peerId;
524
-
525
- tracer.trace('_iceCandidateReceivedHandler', candidate);
526
- if (!candidate) {
527
- tracer.trace('NO iceCandidate');
528
-
529
- return;
530
- }
531
-
532
- var peerConnection = this._peerConnections.get(peerId);
522
+ }, {
523
+ key: "_iceCandidateReceivedHandler",
524
+ value: function _iceCandidateReceivedHandler(_ref6) {
525
+ var candidate = _ref6.candidate,
526
+ peerId = _ref6.peerId;
527
+ tracer.trace('_iceCandidateReceivedHandler', candidate);
533
528
 
534
- if (!peerConnection || !peerConnection.remoteDescription) {
535
- tracer.trace('Queueing iceCandidate', candidate);
536
- this._queueIceCandidate(candidate, peerId);
529
+ if (!candidate) {
530
+ tracer.trace('NO iceCandidate');
531
+ return;
532
+ }
537
533
 
538
- return;
539
- }
534
+ var peerConnection = this._peerConnections.get(peerId);
540
535
 
541
- tracer.trace('Going to add iceCandidate', candidate);
542
- this._addIceCandidateToPeer(candidate, peerConnection);
543
- };
536
+ if (!peerConnection || !peerConnection.remoteDescription) {
537
+ tracer.trace('Queueing iceCandidate', candidate);
544
538
 
545
- TurnWebRtcSessionV2.prototype._addIceCandidateToPeer = function _addIceCandidateToPeer(iceCandidate, peerConnection) {
546
- return peerConnection.addIceCandidate(iceCandidate).then(function () {
547
- tracer.trace('ICE Candidate add to peer connection');
548
- }).catch(function (e) {
549
- tracer.trace('Error adding ICE Candidate to peer connection', e);
550
- });
551
- };
539
+ this._queueIceCandidate(candidate, peerId);
552
540
 
553
- TurnWebRtcSessionV2.prototype._processQueuedIceCandidates = function _processQueuedIceCandidates(peerId) {
554
- var _this10 = this;
541
+ return;
542
+ }
555
543
 
556
- this._withPeerConnection(peerId, function (peerConnection) {
557
- var pendingIces = _this10._pendingPeersIce[peerId];
544
+ tracer.trace('Going to add iceCandidate', candidate);
558
545
 
559
- if (pendingIces) {
560
- pendingIces.forEach(function (pendingIce) {
561
- tracer.trace('Adding pending ICE to peer connections');
562
- _this10._addIceCandidateToPeer(pendingIce, peerConnection);
563
- });
564
- } else {
565
- tracer.trace('No pending ICE found for peer', peerId);
566
- }
567
- });
568
- };
569
-
570
- TurnWebRtcSessionV2.prototype._queueIceCandidate = function _queueIceCandidate(iceCandidate, peerId) {
571
- if (!this._pendingPeersIce[peerId]) {
572
- this._pendingPeersIce[peerId] = [];
546
+ this._addIceCandidateToPeer(candidate, peerConnection);
547
+ }
548
+ }, {
549
+ key: "_addIceCandidateToPeer",
550
+ value: function _addIceCandidateToPeer(iceCandidate, peerConnection) {
551
+ return peerConnection.addIceCandidate(iceCandidate).then(function () {
552
+ tracer.trace('ICE Candidate add to peer connection');
553
+ })["catch"](function (e) {
554
+ tracer.trace('Error adding ICE Candidate to peer connection', e);
555
+ });
556
+ }
557
+ }, {
558
+ key: "_processQueuedIceCandidates",
559
+ value: function _processQueuedIceCandidates(peerId) {
560
+ var _this10 = this;
561
+
562
+ this._withPeerConnection(peerId, function (peerConnection) {
563
+ var pendingIces = _this10._pendingPeersIce[peerId];
564
+
565
+ if (pendingIces) {
566
+ pendingIces.forEach(function (pendingIce) {
567
+ tracer.trace('Adding pending ICE to peer connections');
568
+
569
+ _this10._addIceCandidateToPeer(pendingIce, peerConnection);
570
+ });
571
+ } else {
572
+ tracer.trace('No pending ICE found for peer', peerId);
573
573
  }
574
+ });
575
+ }
576
+ }, {
577
+ key: "_queueIceCandidate",
578
+ value: function _queueIceCandidate(iceCandidate, peerId) {
579
+ if (!this._pendingPeersIce[peerId]) {
580
+ this._pendingPeersIce[peerId] = [];
581
+ }
574
582
 
575
- this._pendingPeersIce[peerId].push(iceCandidate);
576
- };
577
-
578
- //#endregion
583
+ this._pendingPeersIce[peerId].push(iceCandidate);
584
+ } //#endregion
579
585
 
586
+ }]);
580
587
 
581
- return TurnWebRtcSessionV2;
588
+ return TurnWebRtcSessionV2;
582
589
  }(_session.TsMediaSession);
590
+
591
+ exports.TurnWebRtcSessionV2 = TurnWebRtcSessionV2;
583
592
  //# sourceMappingURL=session-turn.v2.js.map