@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,405 +1,538 @@
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
- var __importDefault = undefined && undefined.__importDefault || function (mod) {
23
- return mod && mod.__esModule ? mod : { "default": mod };
28
+
29
+ var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
30
+ return mod && mod.__esModule ? mod : {
31
+ "default": mod
32
+ };
24
33
  };
25
- Object.defineProperty(exports, "__esModule", { value: true });
34
+
35
+ Object.defineProperty(exports, "__esModule", {
36
+ value: true
37
+ });
26
38
  exports.MediaServerSession = void 0;
39
+
27
40
  var MediaSessionBase_1 = require("./MediaSessionBase");
41
+
28
42
  var MediaConstants_1 = require("../MediaConstants");
43
+
29
44
  var openvidu_browser_1 = require("@techsee/openvidu-browser");
45
+
30
46
  var MediaTracer_1 = require("../MediaUtils/MediaTracer");
47
+
31
48
  var guards_1 = require("@techsee/techsee-common/lib/core/guards");
49
+
32
50
  var bluebird_1 = require("bluebird");
51
+
33
52
  var get_1 = __importDefault(require("lodash/get"));
53
+
34
54
  var trace = MediaTracer_1.getMediaTracer('MediaServerSession');
35
- var MediaServerSession = /** @class */function (_super) {
36
- __extends(MediaServerSession, _super);
37
- function MediaServerSession(sessionParams, streamFactory) {
38
- var _this = _super.call(this, sessionParams, streamFactory) || this;
39
- _this.OV = null;
40
- _this._session = null;
41
- _this._lastStreamEvents = [];
42
- _this._credentials = sessionParams.credentials;
43
- _this.connect = _this.connect.bind(_this);
44
- _this.disconnect = _this.disconnect.bind(_this);
45
- _this.onMediaStreamDestroyed = _this.onMediaStreamDestroyed.bind(_this);
46
- _this.onPublisherDestroyed = _this.onPublisherDestroyed.bind(_this);
47
- _this.onMediaStreamRenewed = _this.onMediaStreamRenewed.bind(_this);
48
- _this.createStreamForPublishing = _this.createStreamForPublishing.bind(_this);
49
- _this.createMediaPublisher = _this.createMediaPublisher.bind(_this);
50
- _this.connectionCreatedHandler = _this.connectionCreatedHandler.bind(_this);
51
- _this.connectionDestroyedHandler = _this.connectionDestroyedHandler.bind(_this);
52
- _this.streamCreatedHandler = _this.streamCreatedHandler.bind(_this);
53
- _this.streamDestroyedHandler = _this.streamDestroyedHandler.bind(_this);
54
- return _this;
55
- }
56
- Object.defineProperty(MediaServerSession.prototype, "ownPeerId", {
57
- get: function get() {
58
- return this._session && this._session.connection ? this._session.connection.connectionId : '';
59
- },
60
- enumerable: false,
61
- configurable: true
55
+
56
+ var MediaServerSession =
57
+ /** @class */
58
+ function (_super) {
59
+ __extends(MediaServerSession, _super);
60
+
61
+ function MediaServerSession(sessionParams, streamFactory) {
62
+ var _this = _super.call(this, sessionParams, streamFactory) || this;
63
+
64
+ _this.OV = null;
65
+ _this._session = null;
66
+ _this._lastStreamEvents = [];
67
+ _this._credentials = sessionParams.credentials;
68
+ _this.connect = _this.connect.bind(_this);
69
+ _this.disconnect = _this.disconnect.bind(_this);
70
+ _this.onMediaStreamDestroyed = _this.onMediaStreamDestroyed.bind(_this);
71
+ _this.onPublisherDestroyed = _this.onPublisherDestroyed.bind(_this);
72
+ _this.onMediaStreamRenewed = _this.onMediaStreamRenewed.bind(_this);
73
+ _this.createStreamForPublishing = _this.createStreamForPublishing.bind(_this);
74
+ _this.createMediaPublisher = _this.createMediaPublisher.bind(_this);
75
+ _this.connectionCreatedHandler = _this.connectionCreatedHandler.bind(_this);
76
+ _this.connectionDestroyedHandler = _this.connectionDestroyedHandler.bind(_this);
77
+ _this.streamCreatedHandler = _this.streamCreatedHandler.bind(_this);
78
+ _this.streamDestroyedHandler = _this.streamDestroyedHandler.bind(_this);
79
+ return _this;
80
+ }
81
+
82
+ Object.defineProperty(MediaServerSession.prototype, "ownPeerId", {
83
+ get: function get() {
84
+ return this._session && this._session.connection ? this._session.connection.connectionId : '';
85
+ },
86
+ enumerable: false,
87
+ configurable: true
88
+ });
89
+
90
+ MediaServerSession.prototype.connect = function (connectOnly) {
91
+ trace.info('Connect to session', this._credentials);
92
+
93
+ if (this._session) {
94
+ trace.error('Session already exists');
95
+ return bluebird_1.Promise.reject('Cannot connect to session while another session is active');
96
+ } // const iceServers = this._credentials.ice.map((item: any) => ({
97
+ // urls: item.url,
98
+ // username: item.login,
99
+ // credential: item.password
100
+ // }));
101
+
102
+
103
+ this.OV = new openvidu_browser_1.OpenVidu(); // this.OV.setAdvancedConfiguration({ iceServers });
104
+
105
+ this._session = this.OV.initSession();
106
+ this.initSessionEvents();
107
+ return this._session.connect(this._credentials.token, {
108
+ clientRole: this._sessionParams.clientRole
109
+ })["catch"](function (err) {
110
+ trace.error('Error to connect to session Media Server: ', err);
111
+ throw err;
62
112
  });
63
- MediaServerSession.prototype.connect = function (connectOnly) {
64
- trace.info('Connect to session', this._credentials);
65
- if (this._session) {
66
- trace.error('Session already exists');
67
- return bluebird_1.Promise.reject('Cannot connect to session while another session is active');
68
- }
69
- // const iceServers = this._credentials.ice.map((item: any) => ({
70
- // urls: item.url,
71
- // username: item.login,
72
- // credential: item.password
73
- // }));
74
- this.OV = new openvidu_browser_1.OpenVidu();
75
- // this.OV.setAdvancedConfiguration({ iceServers });
76
- this._session = this.OV.initSession();
77
- this.initSessionEvents();
78
- return this._session.connect(this._credentials.token, { clientRole: this._sessionParams.clientRole }).catch(function (err) {
79
- trace.error('Error to connect to session Media Server: ', err);
80
- throw err;
81
- });
82
- };
83
- MediaServerSession.prototype.sessionDisconnect = function () {
84
- if (this._session) {
85
- return this._session.disconnect(true);
86
- }
87
- };
88
- MediaServerSession.prototype.disconnect = function () {
89
- var _this = this;
90
- return new bluebird_1.Promise(function (resolve) {
91
- trace.info('Disconnecting from session');
92
- var promiseDisconnect = [];
93
- _this._peerConnections.forEach(function (peerConnection) {
94
- promiseDisconnect.push(_this.destroyPeerConnection(peerConnection));
95
- });
96
- return bluebird_1.Promise.all(promiseDisconnect).then(function () {
97
- if (!_this._session) {
98
- trace.info('No active session - resolving');
99
- resolve();
100
- return;
101
- }
102
- _this.removeSessionEvent();
103
- _this._session.disconnect();
104
- _this._lastStreamEvents = [];
105
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
106
- // @ts-ignore
107
- delete _this._session;
108
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
109
- // @ts-ignore
110
- delete _this.OV;
111
- resolve();
112
- });
113
- });
114
- };
115
- MediaServerSession.prototype.removeSessionEvent = function () {
116
- if (!this._session) {
117
- return;
118
- }
119
- this._session.off('connectionCreated');
120
- this._session.off('streamCreated');
121
- this._session.off('streamDestroyed');
122
- this._session.off('connectionDestroyed');
123
- this._session.off('sessionDisconnected');
124
- this._session.off('recordingStarted');
125
- this._session.off('recordingStopped');
126
- this._sessionParams.removeHandlers && this._sessionParams.removeHandlers();
127
- trace.info('Remove Events Listener');
128
- };
129
- MediaServerSession.prototype.getRemoteTrackStats = function (mediaTrack) {
130
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
131
- // @ts-ignore// @ts-ignore
132
- return bluebird_1.Promise.resolve(undefined);
133
- };
134
- MediaServerSession.prototype.onMediaStreamDestroyed = function (clientRole) {
135
- var _this = this;
136
- trace.info('onMediaStreamDestroyed');
137
- var promises = [];
138
- this._peerConnections.forEach(function (peerConnection) {
139
- if (peerConnection.peerRole === clientRole) {
140
- promises.push(_this.destroyPublisherOnPeerConnection(peerConnection));
141
- }
142
- });
143
- return bluebird_1.Promise.all(promises).then(function () {
144
- return trace.info('onMediaStreamDestroyed: Success');
145
- });
146
- };
147
- MediaServerSession.prototype.onPublisherDestroyed = function (connectionId) {
148
- trace.info('onPublisherDestroyed');
149
- var peerConnection = this._peerConnections.get(connectionId);
150
- if (!peerConnection) {
151
- return bluebird_1.Promise.resolve();
152
- }
153
- return this.destroyPublisherOnPeerConnection(peerConnection);
154
- };
155
- MediaServerSession.prototype.onMediaStreamRenewed = function (clientRole, mediaStream) {
156
- return bluebird_1.Promise.resolve(undefined);
157
- };
158
- MediaServerSession.prototype.replaceStreamTracks = function (mediaStream) {
159
- return bluebird_1.Promise.reject(new Error('Not implemented'));
160
- };
161
- MediaServerSession.prototype.initSessionEvents = function () {
162
- var _this = this;
163
- if (!this._session) {
164
- return;
165
- }
166
- this._sessionParams.initHandlers && this._sessionParams.initHandlers();
167
- this._lastStreamEvents = [];
168
- this._session.on('connectionCreated', this.connectionCreatedHandler);
169
- this._session.on('streamCreated', this.streamCreatedHandler);
170
- this._session.on('streamDestroyed', this.streamDestroyedHandler);
171
- this._session.on('connectionDestroyed', this.connectionDestroyedHandler);
172
- this._session.on('recordingStarted', function (event) {
173
- trace.info('recordingStarted event', event);
174
- _this.emitEvent(MediaSessionBase_1.recordingEvents.RECORD_STARTED, event);
175
- });
176
- this._session.on('recordingStopped', function (event) {
177
- trace.info('recordingStopped event', event);
178
- _this.emitEvent(MediaSessionBase_1.recordingEvents.RECORD_STOPPED, event);
179
- });
180
- this._session.on('reconnecting', function () {
181
- trace.info('reconnecting event');
182
- _this.emitEvent(MediaSessionBase_1.recordingEvents.RECONNECTING);
183
- });
184
- this._session.on('sessionDisconnected', this.disconnect);
185
- };
186
- MediaServerSession.prototype.streamDestroyedHandler = function (event) {
187
- trace.info('streamDestroyedHandler', event);
188
- var stream = event.stream;
189
- if (!this.sessionExistsGuard(false) || !stream) {
190
- return;
191
- }
192
- var peerConnection = this._peerConnections.get(stream.connection.connectionId);
193
- if (!peerConnection) {
194
- trace.warn('Peer connection for remote stream not found');
195
- return;
196
- }
197
- this.destroySubscriberOnPeerConnection(peerConnection).catch(function () {
198
- return undefined;
199
- });
200
- };
201
- MediaServerSession.prototype.connectionDestroyedHandler = function (event) {
202
- trace.info('connectionDestroyedHandler', event);
203
- var connection = event.connection;
204
- if (!this.sessionExistsGuard(false) || !connection) {
205
- return;
206
- }
207
- if (this._peerConnections.has(connection.connectionId)) {
208
- var peerConnection = this._peerConnections.get(connection.connectionId);
209
- this.destroySubscriberOnPeerConnection(peerConnection).catch(function () {
210
- return undefined;
211
- });
212
- }
213
- };
214
- MediaServerSession.prototype.destroyPeerConnection = function (peerConnection) {
215
- trace.info('destroyPeerConnection');
216
- this._peerConnections.delete(peerConnection.peerId);
217
- return bluebird_1.Promise.all([this.destroyPublisherOnPeerConnection(peerConnection), this.destroySubscriberOnPeerConnection(peerConnection)]).then(function () {
218
- return undefined;
219
- }).catch(function () {
220
- trace.warn('destroyPeerConnection - something wrong with destroy');
221
- });
222
- };
223
- MediaServerSession.prototype.destroySubscriberOnPeerConnection = function (peerConnection) {
224
- trace.info('destroySubscriberOnPeerConnection', peerConnection);
225
- if (this.sessionExistsGuard(false) && peerConnection.subscriber) {
226
- this._session.unsubscribe(peerConnection.subscriber);
113
+ };
114
+
115
+ MediaServerSession.prototype.sessionDisconnect = function () {
116
+ if (this._session) {
117
+ return this._session.disconnect(true);
118
+ }
119
+ };
120
+
121
+ MediaServerSession.prototype.disconnect = function () {
122
+ var _this = this;
123
+
124
+ return new bluebird_1.Promise(function (resolve) {
125
+ trace.info('Disconnecting from session');
126
+ var promiseDisconnect = [];
127
+
128
+ _this._peerConnections.forEach(function (peerConnection) {
129
+ promiseDisconnect.push(_this.destroyPeerConnection(peerConnection));
130
+ });
131
+
132
+ return bluebird_1.Promise.all(promiseDisconnect).then(function () {
133
+ if (!_this._session) {
134
+ trace.info('No active session - resolving');
135
+ resolve();
136
+ return;
227
137
  }
228
- trace.info('destroySubscriberOnPeerConnection- before unregisterPeersRemoteMediaTracks');
229
- return this.unregisterPeersRemoteMediaTracks(peerConnection).catch(function () {
230
- return trace.warn('Error removing remote streams from peerConnection');
231
- }).then(function () {
232
- trace.info('destroySubscriberOnPeerConnection- unregisterPeersRemoteMediaTracks successfully');
233
- peerConnection.subscriber = null;
234
- });
235
- };
236
- MediaServerSession.prototype.destroyPublisherOnPeerConnection = function (peerConnection) {
237
- trace.info('destroyPublisherOnPeerConnection');
238
- if (this.sessionExistsGuard(false) && peerConnection.publisher) {
239
- trace.info('destroyPublisherOnPeerConnection- unpublish!');
240
- this._session.unpublish(peerConnection.publisher);
138
+
139
+ _this.removeSessionEvent();
140
+
141
+ _this._session.disconnect();
142
+
143
+ _this._lastStreamEvents = []; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
144
+ // @ts-ignore
145
+
146
+ delete _this._session; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
147
+ // @ts-ignore
148
+
149
+ delete _this.OV;
150
+ resolve();
151
+ });
152
+ });
153
+ };
154
+
155
+ MediaServerSession.prototype.removeSessionEvent = function () {
156
+ if (!this._session) {
157
+ return;
158
+ }
159
+
160
+ this._session.off('connectionCreated');
161
+
162
+ this._session.off('streamCreated');
163
+
164
+ this._session.off('streamDestroyed');
165
+
166
+ this._session.off('connectionDestroyed');
167
+
168
+ this._session.off('sessionDisconnected');
169
+
170
+ this._session.off('recordingStarted');
171
+
172
+ this._session.off('recordingStopped');
173
+
174
+ this._sessionParams.removeHandlers && this._sessionParams.removeHandlers();
175
+ trace.info('Remove Events Listener');
176
+ };
177
+
178
+ MediaServerSession.prototype.getRemoteTrackStats = function (mediaTrack) {
179
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
180
+ // @ts-ignore// @ts-ignore
181
+ return bluebird_1.Promise.resolve(undefined);
182
+ };
183
+
184
+ MediaServerSession.prototype.onMediaStreamDestroyed = function (clientRole) {
185
+ var _this = this;
186
+
187
+ trace.info('onMediaStreamDestroyed');
188
+ var promises = [];
189
+
190
+ this._peerConnections.forEach(function (peerConnection) {
191
+ if (peerConnection.peerRole === clientRole) {
192
+ promises.push(_this.destroyPublisherOnPeerConnection(peerConnection));
193
+ }
194
+ });
195
+
196
+ return bluebird_1.Promise.all(promises).then(function () {
197
+ return trace.info('onMediaStreamDestroyed: Success');
198
+ });
199
+ };
200
+
201
+ MediaServerSession.prototype.onPublisherDestroyed = function (connectionId) {
202
+ trace.info('onPublisherDestroyed');
203
+
204
+ var peerConnection = this._peerConnections.get(connectionId);
205
+
206
+ if (!peerConnection) {
207
+ return bluebird_1.Promise.resolve();
208
+ }
209
+
210
+ return this.destroyPublisherOnPeerConnection(peerConnection);
211
+ };
212
+
213
+ MediaServerSession.prototype.onMediaStreamRenewed = function (clientRole, mediaStream) {
214
+ return bluebird_1.Promise.resolve(undefined);
215
+ };
216
+
217
+ MediaServerSession.prototype.replaceStreamTracks = function (mediaStream) {
218
+ return bluebird_1.Promise.reject(new Error('Not implemented'));
219
+ };
220
+
221
+ MediaServerSession.prototype.initSessionEvents = function () {
222
+ var _this = this;
223
+
224
+ if (!this._session) {
225
+ return;
226
+ }
227
+
228
+ this._sessionParams.initHandlers && this._sessionParams.initHandlers();
229
+ this._lastStreamEvents = [];
230
+
231
+ this._session.on('connectionCreated', this.connectionCreatedHandler);
232
+
233
+ this._session.on('streamCreated', this.streamCreatedHandler);
234
+
235
+ this._session.on('streamDestroyed', this.streamDestroyedHandler);
236
+
237
+ this._session.on('connectionDestroyed', this.connectionDestroyedHandler);
238
+
239
+ this._session.on('recordingStarted', function (event) {
240
+ trace.info('recordingStarted event', event);
241
+
242
+ _this.emitEvent(MediaSessionBase_1.recordingEvents.RECORD_STARTED, event);
243
+ });
244
+
245
+ this._session.on('recordingStopped', function (event) {
246
+ trace.info('recordingStopped event', event);
247
+
248
+ _this.emitEvent(MediaSessionBase_1.recordingEvents.RECORD_STOPPED, event);
249
+ });
250
+
251
+ this._session.on('reconnecting', function () {
252
+ trace.info('reconnecting event');
253
+
254
+ _this.emitEvent(MediaSessionBase_1.recordingEvents.RECONNECTING);
255
+ });
256
+
257
+ this._session.on('sessionDisconnected', this.disconnect);
258
+ };
259
+
260
+ MediaServerSession.prototype.streamDestroyedHandler = function (event) {
261
+ trace.info('streamDestroyedHandler', event);
262
+ var stream = event.stream;
263
+
264
+ if (!this.sessionExistsGuard(false) || !stream) {
265
+ return;
266
+ }
267
+
268
+ var peerConnection = this._peerConnections.get(stream.connection.connectionId);
269
+
270
+ if (!peerConnection) {
271
+ trace.warn('Peer connection for remote stream not found');
272
+ return;
273
+ }
274
+
275
+ this.destroySubscriberOnPeerConnection(peerConnection)["catch"](function () {
276
+ return undefined;
277
+ });
278
+ };
279
+
280
+ MediaServerSession.prototype.connectionDestroyedHandler = function (event) {
281
+ trace.info('connectionDestroyedHandler', event);
282
+ var connection = event.connection;
283
+
284
+ if (!this.sessionExistsGuard(false) || !connection) {
285
+ return;
286
+ }
287
+
288
+ if (this._peerConnections.has(connection.connectionId)) {
289
+ var peerConnection = this._peerConnections.get(connection.connectionId);
290
+
291
+ this.destroySubscriberOnPeerConnection(peerConnection)["catch"](function () {
292
+ return undefined;
293
+ });
294
+ }
295
+ };
296
+
297
+ MediaServerSession.prototype.destroyPeerConnection = function (peerConnection) {
298
+ trace.info('destroyPeerConnection');
299
+
300
+ this._peerConnections["delete"](peerConnection.peerId);
301
+
302
+ return bluebird_1.Promise.all([this.destroyPublisherOnPeerConnection(peerConnection), this.destroySubscriberOnPeerConnection(peerConnection)]).then(function () {
303
+ return undefined;
304
+ })["catch"](function () {
305
+ trace.warn('destroyPeerConnection - something wrong with destroy');
306
+ });
307
+ };
308
+
309
+ MediaServerSession.prototype.destroySubscriberOnPeerConnection = function (peerConnection) {
310
+ trace.info('destroySubscriberOnPeerConnection', peerConnection);
311
+
312
+ if (this.sessionExistsGuard(false) && peerConnection.subscriber) {
313
+ this._session.unsubscribe(peerConnection.subscriber);
314
+ }
315
+
316
+ trace.info('destroySubscriberOnPeerConnection- before unregisterPeersRemoteMediaTracks');
317
+ return this.unregisterPeersRemoteMediaTracks(peerConnection)["catch"](function () {
318
+ return trace.warn('Error removing remote streams from peerConnection');
319
+ }).then(function () {
320
+ trace.info('destroySubscriberOnPeerConnection- unregisterPeersRemoteMediaTracks successfully');
321
+ peerConnection.subscriber = null;
322
+ });
323
+ };
324
+
325
+ MediaServerSession.prototype.destroyPublisherOnPeerConnection = function (peerConnection) {
326
+ trace.info('destroyPublisherOnPeerConnection');
327
+
328
+ if (this.sessionExistsGuard(false) && peerConnection.publisher) {
329
+ trace.info('destroyPublisherOnPeerConnection- unpublish!');
330
+
331
+ this._session.unpublish(peerConnection.publisher);
332
+ }
333
+
334
+ peerConnection.publisher = null;
335
+ peerConnection.localStream = null;
336
+ return bluebird_1.Promise.resolve();
337
+ };
338
+
339
+ MediaServerSession.prototype.createMediaSubscriber = function (remoteStream) {
340
+ var _this = this;
341
+
342
+ trace.info('createMediaSubscriber');
343
+ return new bluebird_1.Promise(function (resolve, reject) {
344
+ if (!_this.sessionExistsGuard(false)) {
345
+ return reject('Session not exists');
346
+ }
347
+
348
+ trace.info('Before create subscriber');
349
+
350
+ var subscriber = _this._session.subscribe(remoteStream, '', {}, function (err) {
351
+ if (err) {
352
+ trace.warn('Error creating subscriber', err);
353
+ return reject(err);
241
354
  }
242
- peerConnection.publisher = null;
243
- peerConnection.localStream = null;
244
- return bluebird_1.Promise.resolve();
245
- };
246
- MediaServerSession.prototype.createMediaSubscriber = function (remoteStream) {
247
- var _this = this;
248
- trace.info('createMediaSubscriber');
249
- return new bluebird_1.Promise(function (resolve, reject) {
250
- if (!_this.sessionExistsGuard(false)) {
251
- return reject('Session not exists');
252
- }
253
- trace.info('Before create subscriber');
254
- var subscriber = _this._session.subscribe(remoteStream, '', {}, function (err) {
255
- if (err) {
256
- trace.warn('Error creating subscriber', err);
257
- return reject(err);
258
- }
259
- trace.info('create subscriber successfully', subscriber);
260
- return resolve(subscriber);
261
- });
355
+
356
+ trace.info('create subscriber successfully', subscriber);
357
+ return resolve(subscriber);
358
+ });
359
+ });
360
+ };
361
+
362
+ MediaServerSession.prototype.streamCreatedHandler = function (event) {
363
+ var _this = this;
364
+
365
+ if (!event) {
366
+ return bluebird_1.Promise.resolve();
367
+ }
368
+
369
+ trace.info('streamCreatedHandler', event.stream);
370
+ var stream = event.stream;
371
+ var connection = (stream || {
372
+ connection: null
373
+ }).connection;
374
+
375
+ if (!this.sessionExistsGuard(false) || !stream || connection.connectionId === this.ownPeerId) {
376
+ return bluebird_1.Promise.resolve();
377
+ }
378
+
379
+ if (!this._peerConnections.has(connection.connectionId)) {
380
+ trace.warn('Unexpected case, connection should exist, add to streams to subscribe list', event);
381
+
382
+ this._lastStreamEvents.push(event);
383
+
384
+ return bluebird_1.Promise.resolve();
385
+ }
386
+
387
+ var peerConnection = this._peerConnections.get(connection.connectionId);
388
+
389
+ trace.info('Subscribing the stream');
390
+ return this.createMediaSubscriber(stream).then(function (subscriber) {
391
+ peerConnection.subscriber = subscriber;
392
+ trace.info('Registering remote media tracks', peerConnection.subscriber);
393
+ subscriber.stream.getMediaStream().getTracks().forEach(function (mediaTrack) {
394
+ _this.addRemoteTrackToPeer(peerConnection, mediaTrack);
395
+ });
396
+ return _this.registerPeersRemoteTracks(peerConnection);
397
+ });
398
+ };
399
+
400
+ MediaServerSession.prototype.createStreamForPublishing = function (destinationRole) {
401
+ var _this = this;
402
+
403
+ trace.info('createStreamForPublishing');
404
+ return this._sessionStreamsManager.getMediaStreamForRole(destinationRole).then(function (mediaStream) {
405
+ var stream = {
406
+ videoSource: null,
407
+ audioSource: null
408
+ };
409
+
410
+ if (mediaStream) {
411
+ trace.info('Media stream found for publishing', mediaStream);
412
+ mediaStream.getTracks().forEach(function (track) {
413
+ if (track.kind === 'video') {
414
+ stream.videoSource = track;
415
+ } else if (track.kind === 'audio') {
416
+ stream.audioSource = track;
417
+ }
262
418
  });
419
+ } else if (_this._sessionParams.clientRole === MediaConstants_1.SessionClientRole.USER) {
420
+ trace.warn('No local media was found for publishing to destinationRole:', destinationRole);
421
+ }
422
+
423
+ return stream;
424
+ });
425
+ };
426
+
427
+ MediaServerSession.prototype.createMediaPublisher = function (stream) {
428
+ var _this = this;
429
+
430
+ var ovParams = {
431
+ videoSource: stream.videoSource ? stream.videoSource : false,
432
+ audioSource: stream.audioSource ? stream.audioSource : false,
433
+ publishAudio: !!stream.audioSource,
434
+ publishVideo: !!stream.videoSource
263
435
  };
264
- MediaServerSession.prototype.streamCreatedHandler = function (event) {
265
- var _this = this;
266
- if (!event) {
267
- return bluebird_1.Promise.resolve();
268
- }
269
- trace.info('streamCreatedHandler', event.stream);
270
- var stream = event.stream;
271
- var connection = (stream || { connection: null }).connection;
272
- if (!this.sessionExistsGuard(false) || !stream || connection.connectionId === this.ownPeerId) {
273
- return bluebird_1.Promise.resolve();
436
+ return new bluebird_1.Promise(function (resolve, reject) {
437
+ var ovPublisher = _this.OV.initPublisher('', ovParams, function (error) {
438
+ if (error) {
439
+ reject(error);
440
+ return;
274
441
  }
275
- if (!this._peerConnections.has(connection.connectionId)) {
276
- trace.warn('Unexpected case, connection should exist, add to streams to subscribe list', event);
277
- this._lastStreamEvents.push(event);
278
- return bluebird_1.Promise.resolve();
279
- }
280
- var peerConnection = this._peerConnections.get(connection.connectionId);
281
- trace.info('Subscribing the stream');
282
- return this.createMediaSubscriber(stream).then(function (subscriber) {
283
- peerConnection.subscriber = subscriber;
284
- trace.info('Registering remote media tracks', peerConnection.subscriber);
285
- subscriber.stream.getMediaStream().getTracks().forEach(function (mediaTrack) {
286
- _this.addRemoteTrackToPeer(peerConnection, mediaTrack);
287
- });
288
- return _this.registerPeersRemoteTracks(peerConnection);
289
- });
290
- };
291
- MediaServerSession.prototype.createStreamForPublishing = function (destinationRole) {
292
- var _this = this;
293
- trace.info('createStreamForPublishing');
294
- return this._sessionStreamsManager.getMediaStreamForRole(destinationRole).then(function (mediaStream) {
295
- var stream = { videoSource: null, audioSource: null };
296
- if (mediaStream) {
297
- trace.info('Media stream found for publishing', mediaStream);
298
- mediaStream.getTracks().forEach(function (track) {
299
- if (track.kind === 'video') {
300
- stream.videoSource = track;
301
- } else if (track.kind === 'audio') {
302
- stream.audioSource = track;
303
- }
304
- });
305
- } else if (_this._sessionParams.clientRole === MediaConstants_1.SessionClientRole.USER) {
306
- trace.warn('No local media was found for publishing to destinationRole:', destinationRole);
307
- }
308
- return stream;
309
- });
310
- };
311
- MediaServerSession.prototype.createMediaPublisher = function (stream) {
312
- var _this = this;
313
- var ovParams = {
314
- videoSource: stream.videoSource ? stream.videoSource : false,
315
- audioSource: stream.audioSource ? stream.audioSource : false,
316
- publishAudio: !!stream.audioSource,
317
- publishVideo: !!stream.videoSource
318
- };
319
- return new bluebird_1.Promise(function (resolve, reject) {
320
- var ovPublisher = _this.OV.initPublisher('', ovParams, function (error) {
321
- if (error) {
322
- reject(error);
323
- return;
324
- }
325
- ovPublisher.on('streamDestroyed', function (event) {
326
- trace.info('Publisher stream destroyed', event);
327
- var connectionId = get_1.default(event, 'stream.connection.connectionId');
328
- _this.onPublisherDestroyed(connectionId);
329
- //We preventing the default, in order to prevent stop the stream by OV.
330
- //We managing streams by ourselves and if we allow OV to close the stream, it cannot be reused.
331
- event.preventDefault();
332
- });
333
- resolve(ovPublisher);
334
- });
442
+
443
+ ovPublisher.on('streamDestroyed', function (event) {
444
+ trace.info('Publisher stream destroyed', event);
445
+ var connectionId = get_1["default"](event, 'stream.connection.connectionId');
446
+
447
+ _this.onPublisherDestroyed(connectionId); //We preventing the default, in order to prevent stop the stream by OV.
448
+ //We managing streams by ourselves and if we allow OV to close the stream, it cannot be reused.
449
+
450
+
451
+ event.preventDefault();
335
452
  });
336
- };
337
- MediaServerSession.prototype.connectionCreatedHandler = function (event) {
338
- var _this = this;
339
- trace.info('connectionCreatedHandler', event);
340
- var connection = event.connection;
341
- if (!this.sessionExistsGuard(false) || !connection) {
342
- return;
343
- }
344
- var destinationRole = JSON.parse(connection.data).clientRole;
345
- this.createStreamForPublishing(destinationRole).then(function (streams) {
346
- var peerConnection = {
347
- peerId: connection.connectionId,
348
- peerRole: destinationRole,
349
- peerType: _this._sessionParams.clientType === MediaConstants_1.SessionClientType.INITIATOR ? MediaConstants_1.SessionClientType.GUEST : MediaConstants_1.SessionClientType.INITIATOR,
350
- connection: connection,
351
- publisher: null,
352
- subscriber: null,
353
- remoteMediaTracks: [],
354
- localStream: streams,
355
- connectionTimoutPrt: null
356
- };
357
- if (connection.connectionId !== _this.ownPeerId || !streams.videoSource && !streams.audioSource) {
358
- trace.info('Peer connection without publisher was created', peerConnection);
359
- return peerConnection;
360
- }
361
- return _this.createMediaPublisher(streams).then(function (publisher) {
362
- peerConnection.publisher = publisher;
363
- trace.info('Peer connection with publisher was created', peerConnection);
364
- return peerConnection;
453
+ resolve(ovPublisher);
454
+ });
455
+ });
456
+ };
457
+
458
+ MediaServerSession.prototype.connectionCreatedHandler = function (event) {
459
+ var _this = this;
460
+
461
+ trace.info('connectionCreatedHandler', event);
462
+ var connection = event.connection;
463
+
464
+ if (!this.sessionExistsGuard(false) || !connection) {
465
+ return;
466
+ }
467
+
468
+ var destinationRole = JSON.parse(connection.data).clientRole;
469
+ this.createStreamForPublishing(destinationRole).then(function (streams) {
470
+ var peerConnection = {
471
+ peerId: connection.connectionId,
472
+ peerRole: destinationRole,
473
+ peerType: _this._sessionParams.clientType === MediaConstants_1.SessionClientType.INITIATOR ? MediaConstants_1.SessionClientType.GUEST : MediaConstants_1.SessionClientType.INITIATOR,
474
+ connection: connection,
475
+ publisher: null,
476
+ subscriber: null,
477
+ remoteMediaTracks: [],
478
+ localStream: streams,
479
+ connectionTimoutPrt: null
480
+ };
481
+
482
+ if (connection.connectionId !== _this.ownPeerId || !streams.videoSource && !streams.audioSource) {
483
+ trace.info('Peer connection without publisher was created', peerConnection);
484
+ return peerConnection;
485
+ }
486
+
487
+ return _this.createMediaPublisher(streams).then(function (publisher) {
488
+ peerConnection.publisher = publisher;
489
+ trace.info('Peer connection with publisher was created', peerConnection);
490
+ return peerConnection;
491
+ });
492
+ }).then(function (peerConnection) {
493
+ _this._peerConnections.set(peerConnection.peerId, peerConnection);
494
+
495
+ if (connection.connectionId === _this.ownPeerId) {
496
+ trace.info('Before subscribe the event streams');
497
+ return _this._lastStreamEvents.reduce(function (promiseChain, streamEvent) {
498
+ return promiseChain.then(function () {
499
+ return _this.streamCreatedHandler(streamEvent);
500
+ });
501
+ }, bluebird_1.Promise.resolve()).then(function () {
502
+ if (peerConnection.publisher) {
503
+ trace.info('Starting publish to peerConnection.publisher');
504
+ return _this._session.publish(peerConnection.publisher)["catch"](function (err) {
505
+ trace.info('Error during publish', peerConnection.publisher);
506
+
507
+ if (peerConnection.publisher) {
508
+ trace.info('Error during publish- unpublish');
509
+
510
+ _this._session.unpublish(peerConnection.publisher);
511
+ }
512
+
513
+ throw err;
365
514
  });
366
- }).then(function (peerConnection) {
367
- _this._peerConnections.set(peerConnection.peerId, peerConnection);
368
- if (connection.connectionId === _this.ownPeerId) {
369
- trace.info('Before subscribe the event streams');
370
- return _this._lastStreamEvents.reduce(function (promiseChain, streamEvent) {
371
- return promiseChain.then(function () {
372
- return _this.streamCreatedHandler(streamEvent);
373
- });
374
- }, bluebird_1.Promise.resolve()).then(function () {
375
- if (peerConnection.publisher) {
376
- trace.info('Starting publish to peerConnection.publisher');
377
- return _this._session.publish(peerConnection.publisher).catch(function (err) {
378
- trace.info('Error during publish', peerConnection.publisher);
379
- if (peerConnection.publisher) {
380
- trace.info('Error during publish- unpublish');
381
- _this._session.unpublish(peerConnection.publisher);
382
- }
383
- throw err;
384
- });
385
- }
386
- return bluebird_1.Promise.resolve();
387
- });
388
- }
389
- }).catch(function (error) {
390
- trace.error('Error handling new peer connection', error);
515
+ }
516
+
517
+ return bluebird_1.Promise.resolve();
391
518
  });
392
- };
393
- MediaServerSession.prototype.sessionExistsGuard = function (shouldThrow) {
394
- if (shouldThrow === void 0) {
395
- shouldThrow = true;
396
- }
397
- var connected = !!(this._session && this._session.connection && this._session.connection.connectionId);
398
- return guards_1.throwableGuard(connected, 'There no MediaServer session exists', shouldThrow);
399
- };
400
- return MediaServerSession;
519
+ }
520
+ })["catch"](function (error) {
521
+ trace.error('Error handling new peer connection', error);
522
+ });
523
+ };
524
+
525
+ MediaServerSession.prototype.sessionExistsGuard = function (shouldThrow) {
526
+ if (shouldThrow === void 0) {
527
+ shouldThrow = true;
528
+ }
529
+
530
+ var connected = !!(this._session && this._session.connection && this._session.connection.connectionId);
531
+ return guards_1.throwableGuard(connected, 'There no MediaServer session exists', shouldThrow);
532
+ };
533
+
534
+ return MediaServerSession;
401
535
  }(MediaSessionBase_1.MediaSessionBase);
402
- exports.MediaServerSession = MediaServerSession;
403
536
 
404
- //# sourceMappingURL=MediaServer.js.map
537
+ exports.MediaServerSession = MediaServerSession;
405
538
  //# sourceMappingURL=MediaServer.js.map