@techsee/techsee-media-service 0.21.2 → 0.22.0-alphagb2

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 (97) hide show
  1. package/lib/LocalStreamManager.js +435 -317
  2. package/lib/LocalStreamManager.js.map +1 -1
  3. package/lib/MediaConstants.js +72 -44
  4. package/lib/MediaConstants.js.map +1 -1
  5. package/lib/MediaContracts.js +3 -3
  6. package/lib/MediaContracts.js.map +1 -1
  7. package/lib/MediaPublisher.js +45 -35
  8. package/lib/MediaPublisher.js.map +1 -1
  9. package/lib/MediaServiceBase.js +922 -706
  10. package/lib/MediaServiceBase.js.map +1 -1
  11. package/lib/MediaSession/MediaServer.js +512 -379
  12. package/lib/MediaSession/MediaServer.js.map +1 -1
  13. package/lib/MediaSession/MediaSessionBase.js +123 -88
  14. package/lib/MediaSession/MediaSessionBase.js.map +1 -1
  15. package/lib/MediaSession/SessionOpentok.js +499 -384
  16. package/lib/MediaSession/SessionOpentok.js.map +1 -1
  17. package/lib/MediaSession/SessionTurn.js +606 -466
  18. package/lib/MediaSession/SessionTurn.js.map +1 -1
  19. package/lib/MediaSession/TurnConstants.js +30 -30
  20. package/lib/MediaSession/TurnConstants.js.map +1 -1
  21. package/lib/MediaSubscriber.js +235 -192
  22. package/lib/MediaSubscriber.js.map +1 -1
  23. package/lib/MediaUtils/Compatibility.js +127 -90
  24. package/lib/MediaUtils/Compatibility.js.map +1 -1
  25. package/lib/MediaUtils/MediaDomUtils.js +230 -179
  26. package/lib/MediaUtils/MediaDomUtils.js.map +1 -1
  27. package/lib/MediaUtils/MediaTracer.js +8 -4
  28. package/lib/MediaUtils/MediaTracer.js.map +1 -1
  29. package/lib/MultiParty/Contracts.js +3 -3
  30. package/lib/MultiParty/Contracts.js.map +1 -1
  31. package/lib/MultiParty/DetectWebRtcService.js +66 -50
  32. package/lib/MultiParty/DetectWebRtcService.js.map +1 -1
  33. package/lib/MultiParty/MediaCapabilitiesHelper.js +258 -154
  34. package/lib/MultiParty/MediaCapabilitiesHelper.js.map +1 -1
  35. package/lib/MultiParty/MultiParty.d.ts +1 -8
  36. package/lib/MultiParty/MultiParty.d.ts.map +1 -1
  37. package/lib/MultiParty/MultiParty.js +221 -128
  38. package/lib/MultiParty/MultiParty.js.map +1 -1
  39. package/lib/MultiParty/MultipartyFactory.d.ts +1 -2
  40. package/lib/MultiParty/MultipartyFactory.d.ts.map +1 -1
  41. package/lib/MultiParty/MultipartyFactory.js +12 -7
  42. package/lib/MultiParty/MultipartyFactory.js.map +1 -1
  43. package/lib/MultiParty/OpentokMultiparty.js +611 -384
  44. package/lib/MultiParty/OpentokMultiparty.js.map +1 -1
  45. package/lib/TechseeMediaStream.js +58 -53
  46. package/lib/TechseeMediaStream.js.map +1 -1
  47. package/lib/oldCode/constants.js +20 -15
  48. package/lib/oldCode/constants.js.map +1 -1
  49. package/lib/oldCode/event-emitter.js +45 -22
  50. package/lib/oldCode/event-emitter.js.map +1 -1
  51. package/lib/oldCode/index.js +48 -52
  52. package/lib/oldCode/index.js.map +1 -1
  53. package/lib/oldCode/opentok/session.js +261 -236
  54. package/lib/oldCode/opentok/session.js.map +1 -1
  55. package/lib/oldCode/opentok/stream.js +283 -269
  56. package/lib/oldCode/opentok/stream.js.map +1 -1
  57. package/lib/oldCode/publisher.js +96 -71
  58. package/lib/oldCode/publisher.js.map +1 -1
  59. package/lib/oldCode/service.js +294 -272
  60. package/lib/oldCode/service.js.map +1 -1
  61. package/lib/oldCode/session.js +82 -60
  62. package/lib/oldCode/session.js.map +1 -1
  63. package/lib/oldCode/stream.js +153 -125
  64. package/lib/oldCode/stream.js.map +1 -1
  65. package/lib/oldCode/subscriber.js +60 -35
  66. package/lib/oldCode/subscriber.js.map +1 -1
  67. package/lib/oldCode/tracer.js +179 -138
  68. package/lib/oldCode/tracer.js.map +1 -1
  69. package/lib/oldCode/utils/ImageFixer.js +66 -44
  70. package/lib/oldCode/utils/ImageFixer.js.map +1 -1
  71. package/lib/oldCode/webrtc/constants.js +102 -110
  72. package/lib/oldCode/webrtc/constants.js.map +1 -1
  73. package/lib/oldCode/webrtc/helper.js +176 -124
  74. package/lib/oldCode/webrtc/helper.js.map +1 -1
  75. package/lib/oldCode/webrtc/session-kms.js +408 -393
  76. package/lib/oldCode/webrtc/session-kms.js.map +1 -1
  77. package/lib/oldCode/webrtc/session-loopback.js +245 -227
  78. package/lib/oldCode/webrtc/session-loopback.js.map +1 -1
  79. package/lib/oldCode/webrtc/session-turn.js +705 -658
  80. package/lib/oldCode/webrtc/session-turn.js.map +1 -1
  81. package/lib/oldCode/webrtc/session-turn.v2.js +480 -471
  82. package/lib/oldCode/webrtc/session-turn.v2.js.map +1 -1
  83. package/lib/oldCode/webrtc/session.js +12 -6
  84. package/lib/oldCode/webrtc/session.js.map +1 -1
  85. package/lib/oldCode/webrtc/stream.js +526 -504
  86. package/lib/oldCode/webrtc/stream.js.map +1 -1
  87. package/lib/oldCode/webrtc/temasys/adapter-loader.js +5 -6
  88. package/lib/oldCode/webrtc/temasys/adapter-loader.js.map +1 -1
  89. package/lib/oldCode/webrtc/temasys/adapter.js +5264 -5192
  90. package/lib/oldCode/webrtc/temasys/adapter.js.map +1 -1
  91. package/lib/oldCode/webrtc/webrtc-ie-shim.js +3357 -2927
  92. package/lib/oldCode/webrtc/webrtc-ie-shim.js.map +1 -1
  93. package/lib/qos/raw-qos.js +348 -274
  94. package/lib/qos/raw-qos.js.map +1 -1
  95. package/lib/webrtc-ie-shim.js +3386 -2985
  96. package/lib/webrtc-ie-shim.js.map +1 -1
  97. package/package.json +12 -15
@@ -1,414 +1,529 @@
1
1
  "use strict";
2
2
 
3
- var __extends = undefined && undefined.__extends || function () {
4
- var _extendStatics = function extendStatics(d, b) {
5
- _extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (d, b) {
6
- d.__proto__ = b;
7
- } || function (d, b) {
8
- for (var p in b) {
9
- if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
10
- }
11
- };
12
- return _extendStatics(d, b);
13
- };
14
- return function (d, b) {
15
- _extendStatics(d, b);
16
- function __() {
17
- this.constructor = d;
18
- }
19
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
3
+ var __extends = void 0 && (void 0).__extends || function () {
4
+ var _extendStatics = function extendStatics(d, b) {
5
+ _extendStatics = Object.setPrototypeOf || {
6
+ __proto__: []
7
+ } instanceof Array && function (d, b) {
8
+ d.__proto__ = b;
9
+ } || function (d, b) {
10
+ for (var p in b) {
11
+ if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
12
+ }
20
13
  };
14
+
15
+ return _extendStatics(d, b);
16
+ };
17
+
18
+ return function (d, b) {
19
+ _extendStatics(d, b);
20
+
21
+ function __() {
22
+ this.constructor = d;
23
+ }
24
+
25
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
26
+ };
21
27
  }();
22
- Object.defineProperty(exports, "__esModule", { value: true });
28
+
29
+ Object.defineProperty(exports, "__esModule", {
30
+ value: true
31
+ });
23
32
  exports.OpentokSession = void 0;
33
+
24
34
  var MediaConstants_1 = require("../MediaConstants");
35
+
25
36
  var guards_1 = require("@techsee/techsee-common/lib/core/guards");
37
+
26
38
  var MediaSessionBase_1 = require("./MediaSessionBase");
39
+
27
40
  var MediaTracer_1 = require("../MediaUtils/MediaTracer");
28
- var trace = MediaTracer_1.getMediaTracer('OpentokSession');
29
- //Currently this version supports subscribing only. Publishing will be implemented as needed.
30
- var OpentokSession = /** @class */function (_super) {
31
- __extends(OpentokSession, _super);
32
- function OpentokSession(sessionParams, streamFactory) {
33
- var _this = _super.call(this, sessionParams, streamFactory) || this;
34
- _this._lastStreamEvents = [];
35
- _this._session = null;
36
- _this._credentials = sessionParams.credentials;
37
- _this.connect = _this.connect.bind(_this);
38
- _this.disconnect = _this.disconnect.bind(_this);
39
- _this.onMediaStreamDestroyed = _this.onMediaStreamDestroyed.bind(_this);
40
- _this.onMediaStreamRenewed = _this.onMediaStreamRenewed.bind(_this);
41
- _this.createStreamForPublishing = _this.createStreamForPublishing.bind(_this);
42
- _this.createMediaPublisher = _this.createMediaPublisher.bind(_this);
43
- _this.connectionCreatedHandler = _this.connectionCreatedHandler.bind(_this);
44
- _this.connectionDestroyedHandler = _this.connectionDestroyedHandler.bind(_this);
45
- _this.streamCreatedHandler = _this.streamCreatedHandler.bind(_this);
46
- _this.streamDestroyedHandler = _this.streamDestroyedHandler.bind(_this);
47
- return _this;
41
+
42
+ var trace = MediaTracer_1.getMediaTracer('OpentokSession'); //Currently this version supports subscribing only. Publishing will be implemented as needed.
43
+
44
+ var OpentokSession =
45
+ /** @class */
46
+ function (_super) {
47
+ __extends(OpentokSession, _super);
48
+
49
+ function OpentokSession(sessionParams, streamFactory) {
50
+ var _this = _super.call(this, sessionParams, streamFactory) || this;
51
+
52
+ _this._lastStreamEvents = [];
53
+ _this._session = null;
54
+ _this._credentials = sessionParams.credentials;
55
+ _this.connect = _this.connect.bind(_this);
56
+ _this.disconnect = _this.disconnect.bind(_this);
57
+ _this.onMediaStreamDestroyed = _this.onMediaStreamDestroyed.bind(_this);
58
+ _this.onMediaStreamRenewed = _this.onMediaStreamRenewed.bind(_this);
59
+ _this.createStreamForPublishing = _this.createStreamForPublishing.bind(_this);
60
+ _this.createMediaPublisher = _this.createMediaPublisher.bind(_this);
61
+ _this.connectionCreatedHandler = _this.connectionCreatedHandler.bind(_this);
62
+ _this.connectionDestroyedHandler = _this.connectionDestroyedHandler.bind(_this);
63
+ _this.streamCreatedHandler = _this.streamCreatedHandler.bind(_this);
64
+ _this.streamDestroyedHandler = _this.streamDestroyedHandler.bind(_this);
65
+ return _this;
66
+ }
67
+
68
+ Object.defineProperty(OpentokSession.prototype, "id", {
69
+ get: function get() {
70
+ return this._session && this._session.sessionId ? this._session.sessionId : '';
71
+ },
72
+ enumerable: false,
73
+ configurable: true
74
+ });
75
+ Object.defineProperty(OpentokSession.prototype, "ownPeerId", {
76
+ get: function get() {
77
+ return this._session && this._session.connection ? this._session.connection.connectionId : '';
78
+ },
79
+ enumerable: false,
80
+ configurable: true
81
+ });
82
+
83
+ OpentokSession.prototype.sessionDisconnect = function () {
84
+ if (this._session) {
85
+ this._session.disconnect();
86
+ }
87
+ };
88
+
89
+ OpentokSession.prototype.connect = function () {
90
+ var _this = this;
91
+
92
+ trace.info('Connect to session', this._credentials);
93
+
94
+ if (this._session) {
95
+ trace.error('Session already exists');
96
+ return Promise.reject('Cannot connect to session while another session is active');
48
97
  }
49
- Object.defineProperty(OpentokSession.prototype, "id", {
50
- get: function get() {
51
- return this._session && this._session.sessionId ? this._session.sessionId : '';
52
- },
53
- enumerable: false,
54
- configurable: true
98
+
99
+ return new Promise(function (resolve, reject) {
100
+ var handleError = function handleError(error) {
101
+ _this._session && _this._session.off();
102
+ _this._session = null;
103
+ trace.error('handleConnect error:', error);
104
+ reject(error);
105
+ };
106
+
107
+ var _a = _this._sessionParams.credentials,
108
+ apiKey = _a.apiKey,
109
+ sessionId = _a.sessionId;
110
+ _this._session = window.OTSESSION = window.OT.initSession(apiKey, sessionId);
111
+ _this._lastStreamEvents = [];
112
+
113
+ _this._session.on('connectionCreated', _this.connectionCreatedHandler);
114
+
115
+ _this._session.on('connectionDestroyed', _this.connectionDestroyedHandler);
116
+
117
+ _this._session.on('streamCreated', _this.streamCreatedHandler);
118
+
119
+ _this._session.on('streamDestroyed', _this.streamDestroyedHandler);
120
+
121
+ _this._session.on('archiveStarted', function (event) {
122
+ trace.info('archiveStarted event', event);
123
+
124
+ _this.emitEvent(MediaSessionBase_1.recordingEvents.RECORD_STARTED, event);
125
+ });
126
+
127
+ _this._session.on('archiveStopped', function (event) {
128
+ trace.info('archiveStopped event', event);
129
+
130
+ _this.emitEvent(MediaSessionBase_1.recordingEvents.RECORD_STOPPED, event);
131
+ });
132
+
133
+ var connectHandler = function connectHandler(error) {
134
+ if (error) {
135
+ handleError(error);
136
+ return;
137
+ }
138
+
139
+ resolve();
140
+ };
141
+
142
+ _this._session.connect(_this._credentials.token, connectHandler);
55
143
  });
56
- Object.defineProperty(OpentokSession.prototype, "ownPeerId", {
57
- get: function get() {
58
- return this._session && this._session.connection ? this._session.connection.connectionId : '';
59
- },
60
- enumerable: false,
61
- configurable: true
144
+ };
145
+
146
+ OpentokSession.prototype.disconnect = function () {
147
+ var _this = this;
148
+
149
+ return new Promise(function (resolve) {
150
+ trace.info('Disconnecting from session');
151
+ var currentSession = _this._session;
152
+ _this._session = null;
153
+
154
+ _this._peerConnections.forEach(function (peerConnection) {
155
+ _this.destroyPeerConnection(peerConnection);
156
+ });
157
+
158
+ if (!currentSession) {
159
+ trace.info('No active session - resolving');
160
+ resolve();
161
+ return;
162
+ }
163
+
164
+ currentSession.off();
165
+ currentSession.disconnect();
166
+ _this._lastStreamEvents = [];
167
+ trace.info('Disconnection from session completed');
168
+ resolve();
62
169
  });
63
- OpentokSession.prototype.sessionDisconnect = function () {
64
- if (this._session) {
65
- this._session.disconnect();
66
- }
67
- };
68
- OpentokSession.prototype.connect = function () {
69
- var _this = this;
70
- trace.info('Connect to session', this._credentials);
71
- if (this._session) {
72
- trace.error('Session already exists');
73
- return Promise.reject('Cannot connect to session while another session is active');
170
+ };
171
+
172
+ OpentokSession.prototype.onMediaStreamDestroyed = function (clientRole) {
173
+ var _this = this;
174
+
175
+ var promises = [];
176
+
177
+ this._peerConnections.forEach(function (peerConnection) {
178
+ if (peerConnection.peerRole === clientRole) {
179
+ promises.push(_this.destroyPublisherOnPeerConnection(peerConnection));
180
+ }
181
+ });
182
+
183
+ return Promise.all(promises).then(function () {
184
+ return undefined;
185
+ });
186
+ };
187
+
188
+ OpentokSession.prototype.onMediaStreamRenewed = function (clientRole, mediaStream) {
189
+ //TODO - Alex: need to implement adding new tracks to peer connection and forcing negotiation (in OT API)
190
+ return Promise.resolve();
191
+ };
192
+
193
+ OpentokSession.prototype.replaceStreamTracks = function (mediaStream) {
194
+ return Promise.reject(new Error('Not implemented'));
195
+ };
196
+
197
+ OpentokSession.prototype.getRemoteTrackStats = function (mediaTrack) {
198
+ var _this = this;
199
+
200
+ return new Promise(function (resolve, reject) {
201
+ var peerConnectionOfTrack = _this.getPeerConnectionByMediaTrack(mediaTrack);
202
+
203
+ if (!peerConnectionOfTrack || !peerConnectionOfTrack.subscriber) {
204
+ reject(new Error('Cannot find peer or subscriber for getting stats'));
205
+ return;
206
+ } //https://tokbox.com/developer/sdks/js/reference/Subscriber.html#getStats
207
+
208
+
209
+ peerConnectionOfTrack.subscriber.getStats(function (error, stats) {
210
+ if (error) {
211
+ trace.warn('Error getting stats', error);
212
+ resolve({
213
+ trackId: mediaTrack.id,
214
+ trackStats: {}
215
+ });
216
+ return;
74
217
  }
75
- return new Promise(function (resolve, reject) {
76
- var handleError = function handleError(error) {
77
- _this._session && _this._session.off();
78
- _this._session = null;
79
- trace.error('handleConnect error:', error);
80
- reject(error);
81
- };
82
- var _a = _this._sessionParams.credentials,
83
- apiKey = _a.apiKey,
84
- sessionId = _a.sessionId;
85
- _this._session = window.OTSESSION = window.OT.initSession(apiKey, sessionId);
86
- _this._lastStreamEvents = [];
87
- _this._session.on('connectionCreated', _this.connectionCreatedHandler);
88
- _this._session.on('connectionDestroyed', _this.connectionDestroyedHandler);
89
- _this._session.on('streamCreated', _this.streamCreatedHandler);
90
- _this._session.on('streamDestroyed', _this.streamDestroyedHandler);
91
- _this._session.on('archiveStarted', function (event) {
92
- trace.info('archiveStarted event', event);
93
- _this.emitEvent(MediaSessionBase_1.recordingEvents.RECORD_STARTED, event);
94
- });
95
- _this._session.on('archiveStopped', function (event) {
96
- trace.info('archiveStopped event', event);
97
- _this.emitEvent(MediaSessionBase_1.recordingEvents.RECORD_STOPPED, event);
98
- });
99
- var connectHandler = function connectHandler(error) {
100
- if (error) {
101
- handleError(error);
102
- return;
103
- }
104
- resolve();
105
- };
106
- _this._session.connect(_this._credentials.token, connectHandler);
107
- });
108
- };
109
- OpentokSession.prototype.disconnect = function () {
110
- var _this = this;
111
- return new Promise(function (resolve) {
112
- trace.info('Disconnecting from session');
113
- var currentSession = _this._session;
114
- _this._session = null;
115
- _this._peerConnections.forEach(function (peerConnection) {
116
- _this.destroyPeerConnection(peerConnection);
117
- });
118
- if (!currentSession) {
119
- trace.info('No active session - resolving');
120
- resolve();
121
- return;
122
- }
123
- currentSession.off();
124
- currentSession.disconnect();
125
- _this._lastStreamEvents = [];
126
- trace.info('Disconnection from session completed');
127
- resolve();
128
- });
129
- };
130
- OpentokSession.prototype.onMediaStreamDestroyed = function (clientRole) {
131
- var _this = this;
132
- var promises = [];
133
- this._peerConnections.forEach(function (peerConnection) {
134
- if (peerConnection.peerRole === clientRole) {
135
- promises.push(_this.destroyPublisherOnPeerConnection(peerConnection));
136
- }
137
- });
138
- return Promise.all(promises).then(function () {
139
- return undefined;
218
+
219
+ resolve({
220
+ trackId: mediaTrack.id,
221
+ trackStats: stats
140
222
  });
141
- };
142
- OpentokSession.prototype.onMediaStreamRenewed = function (clientRole, mediaStream) {
143
- //TODO - Alex: need to implement adding new tracks to peer connection and forcing negotiation (in OT API)
144
- return Promise.resolve();
145
- };
146
- OpentokSession.prototype.replaceStreamTracks = function (mediaStream) {
147
- return Promise.reject(new Error('Not implemented'));
148
- };
149
- OpentokSession.prototype.getRemoteTrackStats = function (mediaTrack) {
150
- var _this = this;
151
- return new Promise(function (resolve, reject) {
152
- var peerConnectionOfTrack = _this.getPeerConnectionByMediaTrack(mediaTrack);
153
- if (!peerConnectionOfTrack || !peerConnectionOfTrack.subscriber) {
154
- reject(new Error('Cannot find peer or subscriber for getting stats'));
155
- return;
156
- }
157
- //https://tokbox.com/developer/sdks/js/reference/Subscriber.html#getStats
158
- peerConnectionOfTrack.subscriber.getStats(function (error, stats) {
223
+ });
224
+ });
225
+ }; //#region OpenTok Events Handlers
226
+
227
+
228
+ OpentokSession.prototype.connectionCreatedHandler = function (event) {
229
+ var _this = this;
230
+
231
+ trace.info('connectionCreatedHandler', event);
232
+ var connection = event.connection;
233
+
234
+ if (!this.sessionExistsGuard(false) || !connection) {
235
+ return;
236
+ }
237
+
238
+ var destinationRole = JSON.parse(connection.data).clientRole;
239
+ this.createStreamForPublishing(destinationRole, connection.connectionId === this.ownPeerId).then(function (streams) {
240
+ var peerConnection = {
241
+ peerId: connection.connectionId,
242
+ peerRole: destinationRole,
243
+ peerType: _this._sessionParams.clientType === MediaConstants_1.SessionClientType.INITIATOR ? MediaConstants_1.SessionClientType.GUEST : MediaConstants_1.SessionClientType.INITIATOR,
244
+ connection: connection,
245
+ publisher: null,
246
+ subscriber: null,
247
+ remoteMediaTracks: [],
248
+ localStream: streams,
249
+ connectionTimoutPrt: null
250
+ };
251
+
252
+ if (connection.connectionId !== _this.ownPeerId || !streams.videoSource && !streams.audioSource) {
253
+ trace.info('Peer connection without publisher was created', peerConnection);
254
+ return peerConnection;
255
+ }
256
+
257
+ return _this.createMediaPublisher(streams).then(function (publisher) {
258
+ peerConnection.publisher = publisher;
259
+ trace.info('Peer connection with publisher was created', peerConnection);
260
+ return peerConnection;
261
+ });
262
+ }).then(function (peerConnection) {
263
+ _this._peerConnections.set(peerConnection.peerId, peerConnection);
264
+
265
+ if (connection.connectionId === _this.ownPeerId) {
266
+ trace.info('Before subscribe the event streams');
267
+ /*
268
+ When publisher connect after others participants,
269
+ make sure the publisher will not loose their stream to subscribe
270
+ */
271
+
272
+ return _this._lastStreamEvents.reduce(function (promiseChain, streamEvent) {
273
+ return promiseChain.then(function () {
274
+ return _this.streamCreatedHandler(streamEvent);
275
+ });
276
+ }, Promise.resolve()).then(function () {
277
+ if (peerConnection.publisher) {
278
+ return new Promise(function (resolve, reject) {
279
+ trace.info('Starting publish to peerConnection.publisher');
280
+
281
+ _this._session.publish(peerConnection.publisher, function (error) {
159
282
  if (error) {
160
- trace.warn('Error getting stats', error);
161
- resolve({ trackId: mediaTrack.id, trackStats: {} });
162
- return;
283
+ trace.error("Publish to peerConnection error " + error);
284
+ return reject(error);
163
285
  }
164
- resolve({ trackId: mediaTrack.id, trackStats: stats });
165
- });
166
- });
167
- };
168
- //#region OpenTok Events Handlers
169
- OpentokSession.prototype.connectionCreatedHandler = function (event) {
170
- var _this = this;
171
- trace.info('connectionCreatedHandler', event);
172
- var connection = event.connection;
173
- if (!this.sessionExistsGuard(false) || !connection) {
174
- return;
175
- }
176
- var destinationRole = JSON.parse(connection.data).clientRole;
177
- this.createStreamForPublishing(destinationRole, connection.connectionId === this.ownPeerId).then(function (streams) {
178
- var peerConnection = {
179
- peerId: connection.connectionId,
180
- peerRole: destinationRole,
181
- peerType: _this._sessionParams.clientType === MediaConstants_1.SessionClientType.INITIATOR ? MediaConstants_1.SessionClientType.GUEST : MediaConstants_1.SessionClientType.INITIATOR,
182
- connection: connection,
183
- publisher: null,
184
- subscriber: null,
185
- remoteMediaTracks: [],
186
- localStream: streams,
187
- connectionTimoutPrt: null
188
- };
189
- if (connection.connectionId !== _this.ownPeerId || !streams.videoSource && !streams.audioSource) {
190
- trace.info('Peer connection without publisher was created', peerConnection);
191
- return peerConnection;
192
- }
193
- return _this.createMediaPublisher(streams).then(function (publisher) {
194
- peerConnection.publisher = publisher;
195
- trace.info('Peer connection with publisher was created', peerConnection);
196
- return peerConnection;
286
+
287
+ trace.info('Publish to peerConnection success');
288
+ return resolve();
289
+ });
197
290
  });
198
- }).then(function (peerConnection) {
199
- _this._peerConnections.set(peerConnection.peerId, peerConnection);
200
- if (connection.connectionId === _this.ownPeerId) {
201
- trace.info('Before subscribe the event streams');
202
- /*
203
- When publisher connect after others participants,
204
- make sure the publisher will not loose their stream to subscribe
205
- */
206
- return _this._lastStreamEvents.reduce(function (promiseChain, streamEvent) {
207
- return promiseChain.then(function () {
208
- return _this.streamCreatedHandler(streamEvent);
209
- });
210
- }, Promise.resolve()).then(function () {
211
- if (peerConnection.publisher) {
212
- return new Promise(function (resolve, reject) {
213
- trace.info('Starting publish to peerConnection.publisher');
214
- _this._session.publish(peerConnection.publisher, function (error) {
215
- if (error) {
216
- trace.error("Publish to peerConnection error " + error);
217
- return reject(error);
218
- }
219
- trace.info('Publish to peerConnection success');
220
- return resolve();
221
- });
222
- });
223
- }
224
- return Promise.resolve();
225
- });
226
- }
227
- }).catch(function (error) {
228
- //Need to handle fail cases during reliability
229
- trace.error("Error handling new peer connection: " + error);
291
+ }
292
+
293
+ return Promise.resolve();
230
294
  });
295
+ }
296
+ })["catch"](function (error) {
297
+ //Need to handle fail cases during reliability
298
+ trace.error("Error handling new peer connection: " + error);
299
+ });
300
+ };
301
+
302
+ OpentokSession.prototype.connectionDestroyedHandler = function (event) {
303
+ trace.info('connectionDestroyedHandler', event);
304
+ var connection = event.connection;
305
+
306
+ if (!this.sessionExistsGuard(false) || !connection) {
307
+ return;
308
+ }
309
+
310
+ if (this._peerConnections.has(connection.connectionId)) {
311
+ this.destroyPeerConnection(this._peerConnections.get(connection.connectionId));
312
+ }
313
+ };
314
+
315
+ OpentokSession.prototype.streamCreatedHandler = function (event) {
316
+ trace.info('streamCreatedHandler', event.stream);
317
+ var stream = event.stream;
318
+ var connection = (stream || {
319
+ connection: null
320
+ }).connection;
321
+
322
+ if (!this.sessionExistsGuard(false) || !stream || connection.connectionId === this.ownPeerId) {
323
+ return;
324
+ }
325
+
326
+ if (!this._peerConnections.has(connection.connectionId)) {
327
+ trace.warn('Unexpected case, connection should exist on this stage');
328
+
329
+ this._lastStreamEvents.push(event);
330
+
331
+ return;
332
+ }
333
+
334
+ var peerConnection = this._peerConnections.get(connection.connectionId);
335
+
336
+ trace.info('Subscribing the stream');
337
+ this.createMediaSubscriber(stream).then(function (subscriber) {
338
+ peerConnection.subscriber = subscriber;
339
+ });
340
+ };
341
+
342
+ OpentokSession.prototype.streamDestroyedHandler = function (event) {
343
+ trace.info('streamDestroyedHandler', event);
344
+ var stream = event.stream;
345
+
346
+ if (!this.sessionExistsGuard(false) || !stream) {
347
+ return;
348
+ }
349
+
350
+ var peerConnection = this._peerConnections.get(stream.connection.connectionId);
351
+
352
+ if (!peerConnection) {
353
+ trace.warn('Peer connection for remote stream not found');
354
+ return;
355
+ }
356
+
357
+ this.destroySubscriberOnPeerConnection(peerConnection)["catch"](function () {
358
+ return trace.warn('Error destroying subscriber on stream destroy');
359
+ });
360
+ }; //#endregion
361
+ //#region PeerConnection
362
+
363
+
364
+ OpentokSession.prototype.destroyPeerConnection = function (peerConnection) {
365
+ this._peerConnections["delete"](peerConnection.peerId);
366
+
367
+ this.destroyPublisherOnPeerConnection(peerConnection)["catch"](function () {
368
+ return trace.warn('Error destroying publisher on peer destroy');
369
+ });
370
+ this.destroySubscriberOnPeerConnection(peerConnection)["catch"](function () {
371
+ return trace.warn('Error destroying subscriber on peer destroy');
372
+ });
373
+ }; //#endregion
374
+ //#region Publisher
375
+
376
+
377
+ OpentokSession.prototype.createStreamForPublishing = function (destinationRole, shouldCreateStream) {
378
+ var _this = this;
379
+
380
+ trace.info('createStreamForPublishing');
381
+ var stream = {
382
+ videoSource: null,
383
+ audioSource: null
231
384
  };
232
- OpentokSession.prototype.connectionDestroyedHandler = function (event) {
233
- trace.info('connectionDestroyedHandler', event);
234
- var connection = event.connection;
235
- if (!this.sessionExistsGuard(false) || !connection) {
236
- return;
237
- }
238
- if (this._peerConnections.has(connection.connectionId)) {
239
- this.destroyPeerConnection(this._peerConnections.get(connection.connectionId));
240
- }
241
- };
242
- OpentokSession.prototype.streamCreatedHandler = function (event) {
243
- trace.info('streamCreatedHandler', event.stream);
244
- var stream = event.stream;
245
- var connection = (stream || { connection: null }).connection;
246
- if (!this.sessionExistsGuard(false) || !stream || connection.connectionId === this.ownPeerId) {
247
- return;
248
- }
249
- if (!this._peerConnections.has(connection.connectionId)) {
250
- trace.warn('Unexpected case, connection should exist on this stage');
251
- this._lastStreamEvents.push(event);
252
- return;
253
- }
254
- var peerConnection = this._peerConnections.get(connection.connectionId);
255
- trace.info('Subscribing the stream');
256
- this.createMediaSubscriber(stream).then(function (subscriber) {
257
- peerConnection.subscriber = subscriber;
385
+
386
+ if (!shouldCreateStream) {
387
+ trace.info("createStreamForPublishing - destinationRole: " + destinationRole + " not supposed to create a stream");
388
+ return Promise.resolve(stream);
389
+ }
390
+
391
+ return this._sessionStreamsManager.getMediaStreamForRole(destinationRole).then(function (mediaStream) {
392
+ if (mediaStream) {
393
+ trace.info('Media stream found for publishing', mediaStream);
394
+ mediaStream.getTracks().forEach(function (track) {
395
+ if (track.kind === 'video') {
396
+ stream.videoSource = track;
397
+ } else if (track.kind === 'audio') {
398
+ stream.audioSource = track;
399
+ }
258
400
  });
401
+ } else if (_this._sessionParams.clientRole === MediaConstants_1.SessionClientRole.USER) {
402
+ trace.warn('No local media was found for publishing to destinationRole:', destinationRole);
403
+ }
404
+
405
+ return stream;
406
+ });
407
+ };
408
+
409
+ OpentokSession.prototype.createMediaPublisher = function (stream) {
410
+ var otParams = {
411
+ videoSource: stream.videoSource ? stream.videoSource : false,
412
+ audioSource: stream.audioSource ? stream.audioSource : false,
413
+ publishAudio: !!stream.audioSource,
414
+ publishVideo: !!stream.videoSource,
415
+ showControls: false,
416
+ insertDefaultUI: false
259
417
  };
260
- OpentokSession.prototype.streamDestroyedHandler = function (event) {
261
- trace.info('streamDestroyedHandler', event);
262
- var stream = event.stream;
263
- if (!this.sessionExistsGuard(false) || !stream) {
264
- return;
418
+ return new Promise(function (resolve, reject) {
419
+ var otPublisher = window.OT.initPublisher(undefined, otParams, function (error) {
420
+ if (error) {
421
+ reject(error);
422
+ return;
265
423
  }
266
- var peerConnection = this._peerConnections.get(stream.connection.connectionId);
267
- if (!peerConnection) {
268
- trace.warn('Peer connection for remote stream not found');
269
- return;
270
- }
271
- this.destroySubscriberOnPeerConnection(peerConnection).catch(function () {
272
- return trace.warn('Error destroying subscriber on stream destroy');
273
- });
274
- };
275
- //#endregion
276
- //#region PeerConnection
277
- OpentokSession.prototype.destroyPeerConnection = function (peerConnection) {
278
- this._peerConnections.delete(peerConnection.peerId);
279
- this.destroyPublisherOnPeerConnection(peerConnection).catch(function () {
280
- return trace.warn('Error destroying publisher on peer destroy');
281
- });
282
- this.destroySubscriberOnPeerConnection(peerConnection).catch(function () {
283
- return trace.warn('Error destroying subscriber on peer destroy');
424
+
425
+ otPublisher.on('streamDestroyed', function (event) {
426
+ trace.info('Publisher stream destroyed', event); //We preventing the default, in order to prevent stop the stream by OT.
427
+ //We managing streams by ourselves and if we allow OT to close the stream, it cannot be reused.
428
+
429
+ event.preventDefault();
284
430
  });
285
- };
286
- //#endregion
287
- //#region Publisher
288
- OpentokSession.prototype.createStreamForPublishing = function (destinationRole, shouldCreateStream) {
289
- var _this = this;
290
- trace.info('createStreamForPublishing');
291
- var stream = { videoSource: null, audioSource: null };
292
- if (!shouldCreateStream) {
293
- trace.info("createStreamForPublishing - destinationRole: " + destinationRole + " not supposed to create a stream");
294
- return Promise.resolve(stream);
295
- }
296
- return this._sessionStreamsManager.getMediaStreamForRole(destinationRole).then(function (mediaStream) {
297
- if (mediaStream) {
298
- trace.info('Media stream found for publishing', mediaStream);
299
- mediaStream.getTracks().forEach(function (track) {
300
- if (track.kind === 'video') {
301
- stream.videoSource = track;
302
- } else if (track.kind === 'audio') {
303
- stream.audioSource = track;
304
- }
305
- });
306
- } else if (_this._sessionParams.clientRole === MediaConstants_1.SessionClientRole.USER) {
307
- trace.warn('No local media was found for publishing to destinationRole:', destinationRole);
308
- }
309
- return stream;
431
+ otPublisher.on('videoElementCreated', function (event) {
432
+ event.element.muted = true;
310
433
  });
311
- };
312
- OpentokSession.prototype.createMediaPublisher = function (stream) {
313
- var otParams = {
314
- videoSource: stream.videoSource ? stream.videoSource : false,
315
- audioSource: stream.audioSource ? stream.audioSource : false,
316
- publishAudio: !!stream.audioSource,
317
- publishVideo: !!stream.videoSource,
318
- showControls: false,
319
- insertDefaultUI: false
320
- };
321
- return new Promise(function (resolve, reject) {
322
- var otPublisher = window.OT.initPublisher(undefined, otParams, function (error) {
323
- if (error) {
324
- reject(error);
325
- return;
326
- }
327
- otPublisher.on('streamDestroyed', function (event) {
328
- trace.info('Publisher stream destroyed', event);
329
- //We preventing the default, in order to prevent stop the stream by OT.
330
- //We managing streams by ourselves and if we allow OT to close the stream, it cannot be reused.
331
- event.preventDefault();
332
- });
333
- otPublisher.on('videoElementCreated', function (event) {
334
- event.element.muted = true;
335
- });
336
- otPublisher.on('streamCreated', function (event) {
337
- trace.info('publisher stream created', event, otPublisher.stream.hasAudio);
338
- });
339
- resolve(otPublisher);
340
- });
434
+ otPublisher.on('streamCreated', function (event) {
435
+ trace.info('publisher stream created', event, otPublisher.stream.hasAudio);
341
436
  });
342
- };
343
- OpentokSession.prototype.destroyPublisherOnPeerConnection = function (peerConnection) {
344
- if (this.sessionExistsGuard(false) && peerConnection.publisher) {
345
- this._session.unpublish(peerConnection.publisher);
437
+ resolve(otPublisher);
438
+ });
439
+ });
440
+ };
441
+
442
+ OpentokSession.prototype.destroyPublisherOnPeerConnection = function (peerConnection) {
443
+ if (this.sessionExistsGuard(false) && peerConnection.publisher) {
444
+ this._session.unpublish(peerConnection.publisher);
445
+ }
446
+
447
+ peerConnection.publisher = null;
448
+ peerConnection.localStream = null;
449
+ return Promise.resolve();
450
+ }; //#endregion
451
+ //#region Subscriber
452
+
453
+
454
+ OpentokSession.prototype.createMediaSubscriber = function (remoteStream) {
455
+ var _this = this;
456
+
457
+ return new Promise(function (resolve, reject) {
458
+ if (!_this.sessionExistsGuard(false)) {
459
+ reject('Session not exists');
460
+ return;
461
+ }
462
+
463
+ var subscriber = _this._session.subscribe(remoteStream, null, {
464
+ insertDefaultUI: false
465
+ }, function (err) {
466
+ if (err) {
467
+ trace.warn('Error creating subscriber', err);
468
+ reject(err);
469
+ return;
346
470
  }
347
- peerConnection.publisher = null;
348
- peerConnection.localStream = null;
349
- return Promise.resolve();
350
- };
351
- //#endregion
352
- //#region Subscriber
353
- OpentokSession.prototype.createMediaSubscriber = function (remoteStream) {
354
- var _this = this;
355
- return new Promise(function (resolve, reject) {
356
- if (!_this.sessionExistsGuard(false)) {
357
- reject('Session not exists');
358
- return;
359
- }
360
- var subscriber = _this._session.subscribe(remoteStream, null, { insertDefaultUI: false }, function (err) {
361
- if (err) {
362
- trace.warn('Error creating subscriber', err);
363
- reject(err);
364
- return;
365
- }
366
- subscriber.on('videoElementCreated', function (event) {
367
- var element = event.element;
368
- var peerConnection = _this._peerConnections.get(remoteStream.connection.connectionId);
369
- var otMediaStream = element.srcObject;
370
- peerConnection.subscriber = subscriber;
371
- if (!otMediaStream) {
372
- trace.warn('MediaStream not found in OT video element');
373
- return;
374
- }
375
- trace.info('Registering remote media tracks', peerConnection.subscriber, otMediaStream);
376
- otMediaStream.getTracks().forEach(function (mediaTrack) {
377
- _this.addRemoteTrackToPeer(peerConnection, mediaTrack);
378
- });
379
- _this.registerPeersRemoteTracks(peerConnection);
380
- });
381
- resolve(subscriber);
382
- });
471
+
472
+ subscriber.on('videoElementCreated', function (event) {
473
+ var element = event.element;
474
+
475
+ var peerConnection = _this._peerConnections.get(remoteStream.connection.connectionId);
476
+
477
+ var otMediaStream = element.srcObject;
478
+ peerConnection.subscriber = subscriber;
479
+
480
+ if (!otMediaStream) {
481
+ trace.warn('MediaStream not found in OT video element');
482
+ return;
483
+ }
484
+
485
+ trace.info('Registering remote media tracks', peerConnection.subscriber, otMediaStream);
486
+ otMediaStream.getTracks().forEach(function (mediaTrack) {
487
+ _this.addRemoteTrackToPeer(peerConnection, mediaTrack);
488
+ });
489
+
490
+ _this.registerPeersRemoteTracks(peerConnection);
383
491
  });
384
- };
385
- OpentokSession.prototype.destroySubscriberOnPeerConnection = function (peerConnection) {
386
- if (this.sessionExistsGuard(false) && peerConnection.subscriber) {
387
- this._session.unsubscribe(peerConnection.subscriber);
388
- }
389
- peerConnection.subscriber = null;
390
- return Promise.resolve();
391
- /**
392
- REMARK- opentok provide the same media track id for all participates.
393
- when we unregister the media track we check by the id which is the same for all.
394
- **/
395
- // return this.unregisterPeersRemoteMediaTracks(peerConnection)
396
- // .catch(() => trace.warn('Error removing remote streams from peerConnection'))
397
- // .then(() => {
398
- // peerConnection.subscriber = null;
399
- // });
400
- };
401
- //#endregion
402
- OpentokSession.prototype.sessionExistsGuard = function (shouldThrow) {
403
- if (shouldThrow === void 0) {
404
- shouldThrow = true;
405
- }
406
- var connected = !!(this._session && this._session.connection && this._session.connection.connectionId);
407
- return guards_1.throwableGuard(connected, 'There no OpenTok session exists', shouldThrow);
408
- };
409
- return OpentokSession;
492
+ resolve(subscriber);
493
+ });
494
+ });
495
+ };
496
+
497
+ OpentokSession.prototype.destroySubscriberOnPeerConnection = function (peerConnection) {
498
+ if (this.sessionExistsGuard(false) && peerConnection.subscriber) {
499
+ this._session.unsubscribe(peerConnection.subscriber);
500
+ }
501
+
502
+ peerConnection.subscriber = null;
503
+ return Promise.resolve();
504
+ /**
505
+ REMARK- opentok provide the same media track id for all participates.
506
+ when we unregister the media track we check by the id which is the same for all.
507
+ **/
508
+ // return this.unregisterPeersRemoteMediaTracks(peerConnection)
509
+ // .catch(() => trace.warn('Error removing remote streams from peerConnection'))
510
+ // .then(() => {
511
+ // peerConnection.subscriber = null;
512
+ // });
513
+ }; //#endregion
514
+
515
+
516
+ OpentokSession.prototype.sessionExistsGuard = function (shouldThrow) {
517
+ if (shouldThrow === void 0) {
518
+ shouldThrow = true;
519
+ }
520
+
521
+ var connected = !!(this._session && this._session.connection && this._session.connection.connectionId);
522
+ return guards_1.throwableGuard(connected, 'There no OpenTok session exists', shouldThrow);
523
+ };
524
+
525
+ return OpentokSession;
410
526
  }(MediaSessionBase_1.MediaSessionBase);
411
- exports.OpentokSession = OpentokSession;
412
527
 
413
- //# sourceMappingURL=SessionOpentok.js.map
528
+ exports.OpentokSession = OpentokSession;
414
529
  //# sourceMappingURL=SessionOpentok.js.map