@techsee/techsee-media-service 0.6.0 → 0.6.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/LocalStreamManager.d.ts +21 -0
- package/lib/LocalStreamManager.d.ts.map +1 -0
- package/lib/LocalStreamManager.js +268 -0
- package/lib/LocalStreamManager.js.map +1 -0
- package/lib/MediaConstants.d.ts +52 -0
- package/lib/MediaConstants.d.ts.map +1 -0
- package/lib/MediaConstants.js +68 -0
- package/lib/MediaConstants.js.map +1 -0
- package/lib/MediaContracts.d.ts +122 -0
- package/lib/MediaContracts.d.ts.map +1 -0
- package/lib/MediaContracts.js +6 -0
- package/lib/MediaContracts.js.map +1 -0
- package/lib/MediaPublisher.d.ts +14 -0
- package/lib/MediaPublisher.d.ts.map +1 -0
- package/lib/MediaPublisher.js +43 -0
- package/lib/MediaPublisher.js.map +1 -0
- package/lib/MediaServiceBase.d.ts +64 -0
- package/lib/MediaServiceBase.d.ts.map +1 -0
- package/lib/MediaServiceBase.js +591 -0
- package/lib/MediaServiceBase.js.map +1 -0
- package/lib/MediaSubscriber.d.ts +35 -0
- package/lib/MediaSubscriber.d.ts.map +1 -0
- package/lib/MediaSubscriber.js +202 -0
- package/lib/MediaSubscriber.js.map +1 -0
- package/lib/Session/SessionOpentok.v3.d.ts +33 -0
- package/lib/Session/SessionOpentok.v3.d.ts.map +1 -0
- package/lib/Session/SessionOpentok.v3.js +287 -0
- package/lib/Session/SessionOpentok.v3.js.map +1 -0
- package/lib/Session/SessionTurn.v3.d.ts +42 -0
- package/lib/Session/SessionTurn.v3.d.ts.map +1 -0
- package/lib/Session/SessionTurn.v3.js +442 -0
- package/lib/Session/SessionTurn.v3.js.map +1 -0
- package/lib/Session/TurnConstants.d.ts +18 -0
- package/lib/Session/TurnConstants.d.ts.map +1 -0
- package/lib/Session/TurnConstants.js +36 -0
- package/lib/Session/TurnConstants.js.map +1 -0
- package/lib/TechseeMediaStream.d.ts +14 -0
- package/lib/TechseeMediaStream.d.ts.map +1 -0
- package/lib/TechseeMediaStream.js +58 -0
- package/lib/TechseeMediaStream.js.map +1 -0
- package/lib/Utils/Compatibility.d.ts +11 -0
- package/lib/Utils/Compatibility.d.ts.map +1 -0
- package/lib/Utils/Compatibility.js +71 -0
- package/lib/Utils/Compatibility.js.map +1 -0
- package/lib/Utils/MediaDomUtils.d.ts +4 -0
- package/lib/Utils/MediaDomUtils.d.ts.map +1 -0
- package/lib/Utils/MediaDomUtils.js +200 -0
- package/lib/Utils/MediaDomUtils.js.map +1 -0
- package/lib/Utils/MediaTracer.d.ts +3 -0
- package/lib/Utils/MediaTracer.d.ts.map +1 -0
- package/lib/Utils/MediaTracer.js +12 -0
- package/lib/Utils/MediaTracer.js.map +1 -0
- package/lib/oldCode/constants.js.map +1 -0
- package/lib/oldCode/event-emitter.js.map +1 -0
- package/lib/oldCode/index.js.map +1 -0
- package/lib/oldCode/opentok/session.js.map +1 -0
- package/lib/oldCode/opentok/stream.js.map +1 -0
- package/lib/oldCode/publisher.js.map +1 -0
- package/lib/oldCode/qos/raw-qos.js.map +1 -0
- package/lib/oldCode/service.js.map +1 -0
- package/lib/oldCode/session.js.map +1 -0
- package/lib/oldCode/stream.js.map +1 -0
- package/lib/oldCode/subscriber.js.map +1 -0
- package/lib/oldCode/tracer.d.ts.map +1 -0
- package/lib/oldCode/tracer.js.map +1 -0
- package/lib/oldCode/utils/ImageFixer.d.ts.map +1 -0
- package/lib/oldCode/utils/ImageFixer.js.map +1 -0
- package/lib/oldCode/webrtc/constants.js.map +1 -0
- package/lib/oldCode/webrtc/helper.js.map +1 -0
- package/lib/oldCode/webrtc/session-kms.js.map +1 -0
- package/lib/oldCode/webrtc/session-loopback.js.map +1 -0
- package/lib/oldCode/webrtc/session-turn.js.map +1 -0
- package/lib/{webrtc → oldCode/webrtc}/session-turn.v2.js +0 -2
- package/lib/oldCode/webrtc/session-turn.v2.js.map +1 -0
- package/lib/oldCode/webrtc/session.js.map +1 -0
- package/lib/oldCode/webrtc/stream.js.map +1 -0
- package/lib/oldCode/webrtc/temasys/adapter-loader.js.map +1 -0
- package/lib/oldCode/webrtc/temasys/adapter.js.map +1 -0
- package/lib/oldCode/webrtc/webrtc-ie-shim.js.map +1 -0
- package/lib/webrtc-ie-shim.js +3218 -0
- package/lib/webrtc-ie-shim.js.map +1 -0
- package/package.json +4 -4
- package/lib/constants.js.map +0 -1
- package/lib/event-emitter.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/opentok/session.js.map +0 -1
- package/lib/opentok/stream.js.map +0 -1
- package/lib/publisher.js.map +0 -1
- package/lib/qos/raw-qos.js.map +0 -1
- package/lib/service.js.map +0 -1
- package/lib/session.js.map +0 -1
- package/lib/stream.js.map +0 -1
- package/lib/subscriber.js.map +0 -1
- package/lib/tracer.d.ts.map +0 -1
- package/lib/tracer.js.map +0 -1
- package/lib/utils/ImageFixer.d.ts.map +0 -1
- package/lib/utils/ImageFixer.js.map +0 -1
- package/lib/webrtc/constants.js.map +0 -1
- package/lib/webrtc/helper.js.map +0 -1
- package/lib/webrtc/session-kms.js.map +0 -1
- package/lib/webrtc/session-loopback.js.map +0 -1
- package/lib/webrtc/session-turn.js.map +0 -1
- package/lib/webrtc/session-turn.v2.js.map +0 -1
- package/lib/webrtc/session.js.map +0 -1
- package/lib/webrtc/stream.js.map +0 -1
- package/lib/webrtc/temasys/adapter-loader.js.map +0 -1
- package/lib/webrtc/temasys/adapter.js.map +0 -1
- package/lib/webrtc/webrtc-ie-shim.js.map +0 -1
- /package/lib/{constants.js → oldCode/constants.js} +0 -0
- /package/lib/{event-emitter.js → oldCode/event-emitter.js} +0 -0
- /package/lib/{index.js → oldCode/index.js} +0 -0
- /package/lib/{opentok → oldCode/opentok}/session.js +0 -0
- /package/lib/{opentok → oldCode/opentok}/stream.js +0 -0
- /package/lib/{publisher.js → oldCode/publisher.js} +0 -0
- /package/lib/{qos → oldCode/qos}/raw-qos.js +0 -0
- /package/lib/{service.js → oldCode/service.js} +0 -0
- /package/lib/{session.js → oldCode/session.js} +0 -0
- /package/lib/{stream.js → oldCode/stream.js} +0 -0
- /package/lib/{subscriber.js → oldCode/subscriber.js} +0 -0
- /package/lib/{tracer.d.ts → oldCode/tracer.d.ts} +0 -0
- /package/lib/{tracer.js → oldCode/tracer.js} +0 -0
- /package/lib/{utils → oldCode/utils}/ImageFixer.d.ts +0 -0
- /package/lib/{utils → oldCode/utils}/ImageFixer.js +0 -0
- /package/lib/{webrtc → oldCode/webrtc}/constants.js +0 -0
- /package/lib/{webrtc → oldCode/webrtc}/helper.js +0 -0
- /package/lib/{webrtc → oldCode/webrtc}/session-kms.js +0 -0
- /package/lib/{webrtc → oldCode/webrtc}/session-loopback.js +0 -0
- /package/lib/{webrtc → oldCode/webrtc}/session-turn.js +0 -0
- /package/lib/{webrtc → oldCode/webrtc}/session.js +0 -0
- /package/lib/{webrtc → oldCode/webrtc}/stream.js +0 -0
- /package/lib/{webrtc → oldCode/webrtc}/temasys/adapter-loader.js +0 -0
- /package/lib/{webrtc → oldCode/webrtc}/temasys/adapter.js +0 -0
- /package/lib/{webrtc → oldCode/webrtc}/webrtc-ie-shim.js +0 -0
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __importDefault = undefined && undefined.__importDefault || function (mod) {
|
|
4
|
+
return mod && mod.__esModule ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
var filter_1 = __importDefault(require("lodash/filter"));
|
|
8
|
+
var cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
9
|
+
var values_1 = __importDefault(require("lodash/values"));
|
|
10
|
+
var debounce_1 = __importDefault(require("lodash/debounce"));
|
|
11
|
+
var events_1 = require("events");
|
|
12
|
+
var MediaSubscriber_1 = require("./MediaSubscriber");
|
|
13
|
+
var LocalStreamManager_1 = require("./LocalStreamManager");
|
|
14
|
+
var TechseeMediaStream_1 = require("./TechseeMediaStream");
|
|
15
|
+
var SessionTurn_v3_1 = require("./Session/SessionTurn.v3");
|
|
16
|
+
var MediaDomUtils_1 = require("./Utils/MediaDomUtils");
|
|
17
|
+
var Compatibility_1 = require("./Utils/Compatibility");
|
|
18
|
+
var SessionOpentok_v3_1 = require("./Session/SessionOpentok.v3");
|
|
19
|
+
var MediaConstants_1 = require("./MediaConstants");
|
|
20
|
+
var guards_1 = require("@techsee/techsee-common/lib/core/guards");
|
|
21
|
+
var MediaTracer_1 = require("./Utils/MediaTracer");
|
|
22
|
+
var trace = MediaTracer_1.getMediaTracer('MediaServiceBase');
|
|
23
|
+
var traceStatsInfo = debounce_1.default(trace.info, 1000 * 5, { leading: true, maxWait: 1000 * 30 });
|
|
24
|
+
var privateEvents;
|
|
25
|
+
(function (privateEvents) {
|
|
26
|
+
privateEvents["STREAM_CREATED"] = "STREAM_CREATED";
|
|
27
|
+
privateEvents["STREAM_DESTROYED"] = "STREAM_DESTROYED";
|
|
28
|
+
})(privateEvents || (privateEvents = {}));
|
|
29
|
+
var TechseeMediaServiceBase = /** @class */function () {
|
|
30
|
+
function TechseeMediaServiceBase(environment, webRtcSupportInfo) {
|
|
31
|
+
this._subscribers = new Map();
|
|
32
|
+
this._publishers = new Map();
|
|
33
|
+
this._publisherPromises = new Map();
|
|
34
|
+
this._serviceOptions = null;
|
|
35
|
+
this._initLocalStreamsPromise = null;
|
|
36
|
+
this._initServicePromise = null;
|
|
37
|
+
this._registeredStreams = new Map();
|
|
38
|
+
this._isVoipEnabled = false;
|
|
39
|
+
this._session = null;
|
|
40
|
+
trace.info('TechseeMediaServiceBase created');
|
|
41
|
+
this.bindClassMethods();
|
|
42
|
+
this._emitter = new events_1.EventEmitter();
|
|
43
|
+
this._environment = environment;
|
|
44
|
+
this._deviceSupportFlags = {
|
|
45
|
+
videoPlayback: false,
|
|
46
|
+
hasCamera: false,
|
|
47
|
+
hasMicrophone: false,
|
|
48
|
+
webRtcSupportInfo: webRtcSupportInfo
|
|
49
|
+
};
|
|
50
|
+
this._localStreamsManager = new LocalStreamManager_1.LocalStreamManager(this._environment);
|
|
51
|
+
this._sessionStreamsManager = {
|
|
52
|
+
getMediaStreamForRole: this.getStreamForDestinationRole,
|
|
53
|
+
addRemoteMediaTrack: this.registerRemoteMediaTrack,
|
|
54
|
+
removeMediaTrack: this.unregisterRemoteMediaTrack
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
Object.defineProperty(TechseeMediaServiceBase.prototype, "deviceSupportInfo", {
|
|
58
|
+
get: function get() {
|
|
59
|
+
this.serviceInitGuard(false);
|
|
60
|
+
return cloneDeep_1.default(this._deviceSupportFlags);
|
|
61
|
+
},
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true
|
|
64
|
+
});
|
|
65
|
+
Object.defineProperty(TechseeMediaServiceBase.prototype, "isVoipEnabled", {
|
|
66
|
+
get: function get() {
|
|
67
|
+
return this._isVoipEnabled;
|
|
68
|
+
},
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(TechseeMediaServiceBase.prototype, "isSessionActive", {
|
|
73
|
+
get: function get() {
|
|
74
|
+
return this._session !== null;
|
|
75
|
+
},
|
|
76
|
+
enumerable: true,
|
|
77
|
+
configurable: true
|
|
78
|
+
});
|
|
79
|
+
Object.defineProperty(TechseeMediaServiceBase.prototype, "isLocalStreamInitialized", {
|
|
80
|
+
get: function get() {
|
|
81
|
+
return this._initLocalStreamsPromise !== null;
|
|
82
|
+
},
|
|
83
|
+
enumerable: true,
|
|
84
|
+
configurable: true
|
|
85
|
+
});
|
|
86
|
+
/*
|
|
87
|
+
Initializes media service. Before using anything from this service it should be initialized first.
|
|
88
|
+
All of serviceOptions should be retrieved from account settings and by evaluating application state.
|
|
89
|
+
For example, when initializing service, accountSettings is already loaded, and we know if FS session is audio enabled.
|
|
90
|
+
*/
|
|
91
|
+
TechseeMediaServiceBase.prototype.initMediaService = function (serviceOptions) {
|
|
92
|
+
var _this = this;
|
|
93
|
+
trace.info('TechseeMediaServiceBase.initMediaService', serviceOptions);
|
|
94
|
+
var warnMessage = 'Multiple initialization does not supposed to happen. Not rejecting, but check the flow for correctness.';
|
|
95
|
+
if (this._initServicePromise || this._serviceOptions) {
|
|
96
|
+
console.warn(warnMessage);
|
|
97
|
+
return this._initServicePromise ? this._initServicePromise : Promise.resolve();
|
|
98
|
+
}
|
|
99
|
+
this._serviceOptions = cloneDeep_1.default(serviceOptions);
|
|
100
|
+
var isOpenTok = this._serviceOptions.mediaServiceType === MediaConstants_1.MediaServiceType.OPENTOK;
|
|
101
|
+
this._initServicePromise = Promise.all([Compatibility_1.isVideoPlaySupportedOnDevice(isOpenTok).then(function (isSupported) {
|
|
102
|
+
return _this._deviceSupportFlags.videoPlayback = isSupported;
|
|
103
|
+
}), Compatibility_1.enumerateMediaDevices().catch(function (error) {
|
|
104
|
+
trace.warn(error);
|
|
105
|
+
return [];
|
|
106
|
+
}).then(function (localDevicesList) {
|
|
107
|
+
var groupedDevices = {
|
|
108
|
+
video: filter_1.default(localDevicesList, function (device) {
|
|
109
|
+
return device.kind.toLowerCase() === 'videoinput';
|
|
110
|
+
}),
|
|
111
|
+
audio: filter_1.default(localDevicesList, function (device) {
|
|
112
|
+
return device.kind.toLowerCase() === 'audioinput';
|
|
113
|
+
})
|
|
114
|
+
};
|
|
115
|
+
_this._deviceSupportFlags.hasCamera = groupedDevices.video.length > 0;
|
|
116
|
+
_this._deviceSupportFlags.hasMicrophone = groupedDevices.audio.length > 0;
|
|
117
|
+
})]).then(function () {
|
|
118
|
+
return undefined;
|
|
119
|
+
});
|
|
120
|
+
return this._initServicePromise;
|
|
121
|
+
};
|
|
122
|
+
//#region Media Streams Management
|
|
123
|
+
/*
|
|
124
|
+
The main method to start the initialization of local media streams.
|
|
125
|
+
This method can be executed many times and it will insure all calls are synced and only relevant stream will be created.
|
|
126
|
+
DO NOT USE ANY OTHER METHOD THAT FORCES "navigator.getUserMedia" OTHER THAN THIS ONE.
|
|
127
|
+
*/
|
|
128
|
+
TechseeMediaServiceBase.prototype.initLocalMediaStreams = function () {
|
|
129
|
+
this.serviceInitGuard();
|
|
130
|
+
if (!this._initLocalStreamsPromise) {
|
|
131
|
+
trace.info('initLocalMediaStreams creating media streams');
|
|
132
|
+
this._initLocalStreamsPromise = this.getLocalMediaImplementation().catch(function (mediaRequestFailResult) {
|
|
133
|
+
trace.error('initLocalMediaStreams failure: ', mediaRequestFailResult);
|
|
134
|
+
throw mediaRequestFailResult;
|
|
135
|
+
});
|
|
136
|
+
} else {
|
|
137
|
+
trace.info('initLocalMediaStreams already done or in progress');
|
|
138
|
+
}
|
|
139
|
+
return this._initLocalStreamsPromise;
|
|
140
|
+
};
|
|
141
|
+
/*
|
|
142
|
+
Creates an instance of subscriber that will "listen" to the requested stream.
|
|
143
|
+
Whenever requested stream is available, subscriber will render it.
|
|
144
|
+
*/
|
|
145
|
+
TechseeMediaServiceBase.prototype.createSubscriber = function (subscriberParams) {
|
|
146
|
+
if (this._subscribers.has(subscriberParams.container)) {
|
|
147
|
+
return Promise.reject('Subscriber for provided DIV element already exists');
|
|
148
|
+
}
|
|
149
|
+
var subscriber = new MediaSubscriber_1.TechseeMediaSubscriber(subscriberParams);
|
|
150
|
+
this._subscribers.set(subscriberParams.container, subscriber);
|
|
151
|
+
var streamForSubscriber = this.getRegisteredStreamByType(subscriber.streamType);
|
|
152
|
+
if (streamForSubscriber) {
|
|
153
|
+
trace.info("Stream for " + subscriberParams.streamType + " is ready.");
|
|
154
|
+
subscriber.renderStream(streamForSubscriber);
|
|
155
|
+
} else {
|
|
156
|
+
trace.info("Stream for " + subscriberParams.streamType + " not created yet");
|
|
157
|
+
}
|
|
158
|
+
return Promise.resolve(subscriber);
|
|
159
|
+
};
|
|
160
|
+
/*
|
|
161
|
+
Destroys the subscriber for provided HTML DIV container
|
|
162
|
+
*/
|
|
163
|
+
TechseeMediaServiceBase.prototype.destroySubscriber = function (container) {
|
|
164
|
+
if (this._subscribers.has(container)) {
|
|
165
|
+
var subscriber = this._subscribers.get(container);
|
|
166
|
+
subscriber.dispose();
|
|
167
|
+
}
|
|
168
|
+
this._subscribers.delete(container);
|
|
169
|
+
return Promise.resolve();
|
|
170
|
+
};
|
|
171
|
+
TechseeMediaServiceBase.prototype.onStreamDestroyed = function (callback) {
|
|
172
|
+
this.registerEventCallback(privateEvents.STREAM_DESTROYED, callback);
|
|
173
|
+
};
|
|
174
|
+
TechseeMediaServiceBase.prototype.onStreamCreated = function (callback) {
|
|
175
|
+
this.registerEventCallback(privateEvents.STREAM_CREATED, callback);
|
|
176
|
+
};
|
|
177
|
+
//#endregion Media Streams Management
|
|
178
|
+
//#region Session Management
|
|
179
|
+
/*
|
|
180
|
+
Creates instance of WebRTC session which connects to (signaling server), and begins to listen to WebRTC events.
|
|
181
|
+
*/
|
|
182
|
+
TechseeMediaServiceBase.prototype.connectToSession = function (sessionParams) {
|
|
183
|
+
var _this = this;
|
|
184
|
+
this.serviceInitGuard();
|
|
185
|
+
if (!this._session) {
|
|
186
|
+
this._session = { params: cloneDeep_1.default(sessionParams) };
|
|
187
|
+
this._session.connectPromise = new Promise(function (resolve, reject) {
|
|
188
|
+
var doAsyncReject = function doAsyncReject(error) {
|
|
189
|
+
/*
|
|
190
|
+
Reject with timeout, to change JS 'execution flow'.
|
|
191
|
+
If timeout removed, session is assigned to null, before promise returns, and "catch" will not
|
|
192
|
+
work on promise that is returned.
|
|
193
|
+
*/
|
|
194
|
+
setTimeout(function () {
|
|
195
|
+
_this._session = null;
|
|
196
|
+
reject(error);
|
|
197
|
+
});
|
|
198
|
+
};
|
|
199
|
+
var sessionHandlers = {
|
|
200
|
+
onDisconnectedHandler: _this.onSessionDisconnectHandler
|
|
201
|
+
};
|
|
202
|
+
trace.info('connectToSession', sessionParams);
|
|
203
|
+
if (_this._serviceOptions.mediaServiceType === MediaConstants_1.MediaServiceType.TURNSERVER) {
|
|
204
|
+
_this._session.instance = new SessionTurn_v3_1.TurnWebRtcSessionV3(sessionParams, sessionHandlers, _this._sessionStreamsManager);
|
|
205
|
+
} else if (_this._serviceOptions.mediaServiceType === MediaConstants_1.MediaServiceType.OPENTOK) {
|
|
206
|
+
_this._session.instance = new SessionOpentok_v3_1.OpentokSessionV3(sessionParams, _this._sessionStreamsManager);
|
|
207
|
+
} else {
|
|
208
|
+
var allowedValues = values_1.default(MediaConstants_1.MediaServiceType);
|
|
209
|
+
trace.error("mediaServiceType '" + _this._serviceOptions.mediaServiceType + "' is not supported.", { allowedValues: allowedValues });
|
|
210
|
+
doAsyncReject(new Error('mediaServiceType is not supported'));
|
|
211
|
+
}
|
|
212
|
+
if (_this._session && _this._session.instance) {
|
|
213
|
+
_this._session.instance.connect().then(function () {
|
|
214
|
+
trace.info('Session is connected');
|
|
215
|
+
resolve();
|
|
216
|
+
}).catch(function (error) {
|
|
217
|
+
trace.error('Failed to connect to session', error);
|
|
218
|
+
doAsyncReject(error);
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
} else {
|
|
223
|
+
trace.warn('Session connected already. Disconnect before connect to new session again');
|
|
224
|
+
}
|
|
225
|
+
//TODO - Alex: Change a structure of this function to be more safe
|
|
226
|
+
//(when we return, there should not be case when session or promise is null)
|
|
227
|
+
//To fix, need to return promise immediately, then execute process in setTimeout(fn, 0);
|
|
228
|
+
return this._session.connectPromise;
|
|
229
|
+
};
|
|
230
|
+
/*
|
|
231
|
+
Disconnects from WebRTC session.
|
|
232
|
+
*/
|
|
233
|
+
TechseeMediaServiceBase.prototype.disconnectFromSession = function () {
|
|
234
|
+
if (!this._session) {
|
|
235
|
+
return Promise.resolve();
|
|
236
|
+
}
|
|
237
|
+
var lastSession = this._session;
|
|
238
|
+
this._session = null;
|
|
239
|
+
trace.info('Disconnecting from session');
|
|
240
|
+
return Promise.all([lastSession.instance.disconnect(), this.clearPublishers()]).then(function () {
|
|
241
|
+
return undefined;
|
|
242
|
+
});
|
|
243
|
+
};
|
|
244
|
+
/*
|
|
245
|
+
Updates a credentials for turn server
|
|
246
|
+
*/
|
|
247
|
+
TechseeMediaServiceBase.prototype.updateSessionCredentials = function (credentials) {
|
|
248
|
+
trace.info('updateSessionCredentials');
|
|
249
|
+
if (!this.sessionExistsGuard(false)) {
|
|
250
|
+
return Promise.reject('There no session to update credentials');
|
|
251
|
+
}
|
|
252
|
+
this._session.params.credentials = cloneDeep_1.default(credentials);
|
|
253
|
+
return Promise.resolve();
|
|
254
|
+
};
|
|
255
|
+
TechseeMediaServiceBase.prototype.enableVoipDuringSession = function () {
|
|
256
|
+
if (!this._isVoipEnabled && this.isLocalStreamInitialized) {
|
|
257
|
+
throw new Error('Voip support cannot be enabled after local stream where initialized');
|
|
258
|
+
}
|
|
259
|
+
this._isVoipEnabled = true;
|
|
260
|
+
};
|
|
261
|
+
TechseeMediaServiceBase.prototype.getStatsForRemoteTrack = function (streamType) {
|
|
262
|
+
if (!this.sessionExistsGuard(false)) {
|
|
263
|
+
return Promise.reject(new Error('Media session not started'));
|
|
264
|
+
}
|
|
265
|
+
var streamForStats = this.getRegisteredStreamByType(streamType);
|
|
266
|
+
if (!streamForStats) {
|
|
267
|
+
return Promise.reject(new Error('Stream for requested type was not found'));
|
|
268
|
+
}
|
|
269
|
+
return this._session.instance.getRemoteTrackStats(streamForStats.mediaTrack).then(function (trackStats) {
|
|
270
|
+
traceStatsInfo("MediaTrackStats for " + streamType + ": " + JSON.stringify(trackStats));
|
|
271
|
+
return {
|
|
272
|
+
trackId: streamForStats.mediaTrack.id,
|
|
273
|
+
trackStats: trackStats
|
|
274
|
+
};
|
|
275
|
+
});
|
|
276
|
+
};
|
|
277
|
+
TechseeMediaServiceBase.prototype.onSessionDisconnectHandler = function (reason) {
|
|
278
|
+
var _this = this;
|
|
279
|
+
trace.info('onSessionDisconnectHandler', reason);
|
|
280
|
+
if (reason !== MediaConstants_1.MediaSessionDisconnectReason.ForcedByConsumer) {
|
|
281
|
+
var lastParams_1 = this._session && this._session.params ? this._session.params : null;
|
|
282
|
+
this.disconnectFromSession().then(function () {
|
|
283
|
+
if (lastParams_1 && reason === MediaConstants_1.MediaSessionDisconnectReason.UserPeerReconnected) {
|
|
284
|
+
trace.info('Reconnecting to media session, sessionParams:', lastParams_1);
|
|
285
|
+
_this.connectToSession(lastParams_1).then(function () {
|
|
286
|
+
trace.info('Media session reconnected');
|
|
287
|
+
}).catch(function (error) {
|
|
288
|
+
trace.error('Error while reconnecting to media session');
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
} else {
|
|
293
|
+
trace.info('Ignore Session disconnect event');
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
//#endregion Session Management
|
|
297
|
+
//#region Utils
|
|
298
|
+
TechseeMediaServiceBase.prototype.getSnapshotFromKnownStream = function (sourceStream, snapshotOptions) {
|
|
299
|
+
var _this = this;
|
|
300
|
+
return new Promise(function (resolve, reject) {
|
|
301
|
+
if (sourceStream !== MediaConstants_1.KnownMediaStream.USER_VIDEO_STREAM && sourceStream !== MediaConstants_1.KnownMediaStream.USER_SCREEN_SHARE_STREAM) {
|
|
302
|
+
trace.error('The requested stream is not video stream, and cannot be used for snapshot');
|
|
303
|
+
reject(new Error('INCOMPATIBLE_STREAM_FOR_SNAPSHOT'));
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
var snapshotStream = _this.getRegisteredStreamByType(sourceStream);
|
|
307
|
+
if (!snapshotStream) {
|
|
308
|
+
trace.error('Cannot make snapshot: The requested stream not exists yet.');
|
|
309
|
+
reject(new Error('NO_REQUESTED_STREAM'));
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
MediaDomUtils_1.getSnapshotFromMediaStream(snapshotStream.mediaStream, snapshotOptions).then(function (imageData) {
|
|
313
|
+
var urlComponents = imageData.split(';base64,');
|
|
314
|
+
var mimeType = urlComponents[0].split(':')[1];
|
|
315
|
+
var bytes = atob(urlComponents[1]);
|
|
316
|
+
var buffer = new ArrayBuffer(bytes.length);
|
|
317
|
+
var rawData = new Uint8Array(buffer);
|
|
318
|
+
for (var i = 0; i < bytes.length; i++) {
|
|
319
|
+
rawData[i] = bytes.charCodeAt(i);
|
|
320
|
+
}
|
|
321
|
+
var blob = new Blob([rawData], { type: mimeType });
|
|
322
|
+
var objectUrl = window.URL.createObjectURL(blob);
|
|
323
|
+
trace.info('Snapshot created successfully');
|
|
324
|
+
var result = {
|
|
325
|
+
base64img: imageData,
|
|
326
|
+
objectUrl: objectUrl,
|
|
327
|
+
imageBlob: blob,
|
|
328
|
+
mimeType: mimeType
|
|
329
|
+
};
|
|
330
|
+
resolve(result);
|
|
331
|
+
}).catch(function (error) {
|
|
332
|
+
trace.error('Error creating snapshot', error);
|
|
333
|
+
reject(error);
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
};
|
|
337
|
+
//Will clean streams, publishers and subscribers, but will not remove event listeners
|
|
338
|
+
TechseeMediaServiceBase.prototype.clearService = function () {
|
|
339
|
+
var _this = this;
|
|
340
|
+
trace.info('MediaService clearing all resources');
|
|
341
|
+
return this.disconnectFromSession().then(this.clearSubscribers).then(this.clearRegisteredStreams).then(this._localStreamsManager.clearAllStreams).then(function () {
|
|
342
|
+
_this._initLocalStreamsPromise = null;
|
|
343
|
+
}).then(function () {
|
|
344
|
+
return undefined;
|
|
345
|
+
});
|
|
346
|
+
};
|
|
347
|
+
//Will clear the service and remove all event listeners
|
|
348
|
+
TechseeMediaServiceBase.prototype.dispose = function () {
|
|
349
|
+
var _this = this;
|
|
350
|
+
return this.clearService().catch(function () {
|
|
351
|
+
return undefined;
|
|
352
|
+
}).then(function () {
|
|
353
|
+
_this._emitter.removeAllListeners();
|
|
354
|
+
});
|
|
355
|
+
};
|
|
356
|
+
Object.defineProperty(TechseeMediaServiceBase.prototype, "subscribers", {
|
|
357
|
+
//#endregion
|
|
358
|
+
//#region Protected Methods
|
|
359
|
+
get: function get() {
|
|
360
|
+
return this._subscribers;
|
|
361
|
+
},
|
|
362
|
+
enumerable: true,
|
|
363
|
+
configurable: true
|
|
364
|
+
});
|
|
365
|
+
TechseeMediaServiceBase.prototype.registerLocalMediaStream = function (tsMediaStream) {
|
|
366
|
+
trace.info('Registering local stream', tsMediaStream.streamType);
|
|
367
|
+
if (this._registeredStreams.has(tsMediaStream.streamType)) {
|
|
368
|
+
return Promise.reject(new Error("Stream " + tsMediaStream.streamType + " already registered"));
|
|
369
|
+
}
|
|
370
|
+
return this.registerStream(tsMediaStream);
|
|
371
|
+
};
|
|
372
|
+
TechseeMediaServiceBase.prototype.getRegisteredStreamByType = function (streamType) {
|
|
373
|
+
return this._registeredStreams.get(streamType) || null;
|
|
374
|
+
};
|
|
375
|
+
TechseeMediaServiceBase.prototype.changeEnableForKnownStream = function (streamType, isPaused) {
|
|
376
|
+
var streamToChangeState = this.getRegisteredStreamByType(streamType);
|
|
377
|
+
if (!streamToChangeState) {
|
|
378
|
+
trace.warn('There no stream found to change enable state', streamType);
|
|
379
|
+
} else {
|
|
380
|
+
streamToChangeState.mediaTrack.enabled = !isPaused;
|
|
381
|
+
if (streamToChangeState.isRemote && streamToChangeState.streamKind === MediaConstants_1.KnownMediaStreamKind.Audio) {
|
|
382
|
+
this.subscribers.forEach(function (subscriber) {
|
|
383
|
+
if (subscriber.streamType === streamType) {
|
|
384
|
+
subscriber.muteSound(isPaused);
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
trace.info('Local stream enable state is changed', { streamType: streamType, isPaused: isPaused });
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
TechseeMediaServiceBase.prototype.registerEventCallback = function (event, callback) {
|
|
392
|
+
this._emitter.on(event, callback);
|
|
393
|
+
};
|
|
394
|
+
TechseeMediaServiceBase.prototype.emitEvent = function (event, eventArgs) {
|
|
395
|
+
var _this = this;
|
|
396
|
+
setTimeout(function () {
|
|
397
|
+
_this._emitter.emit(event, eventArgs);
|
|
398
|
+
});
|
|
399
|
+
};
|
|
400
|
+
//#endregion Protected Methods
|
|
401
|
+
//#region Private Methods
|
|
402
|
+
TechseeMediaServiceBase.prototype.registerRemoteMediaTrack = function (remoteMediaTrack) {
|
|
403
|
+
trace.info("Registering remote " + remoteMediaTrack.trackType + " MediaStreamTrack", remoteMediaTrack.mediaTrack);
|
|
404
|
+
var currentStream = this._registeredStreams.get(remoteMediaTrack.trackType);
|
|
405
|
+
if (currentStream && !currentStream.isRemote) {
|
|
406
|
+
return Promise.reject(new Error('Cannot register remote stream with the same type as local stream'));
|
|
407
|
+
}
|
|
408
|
+
var newDedicatedStream = new TechseeMediaStream_1.TechseeMediaStream(remoteMediaTrack.mediaTrack, remoteMediaTrack.trackType, true);
|
|
409
|
+
return this.registerStream(newDedicatedStream);
|
|
410
|
+
};
|
|
411
|
+
TechseeMediaServiceBase.prototype.registerStream = function (mediaStream) {
|
|
412
|
+
var _this = this;
|
|
413
|
+
var currentStream = this._registeredStreams.get(mediaStream.streamType);
|
|
414
|
+
var registerTrack = function registerTrack() {
|
|
415
|
+
mediaStream.mediaTrack.onended = function () {
|
|
416
|
+
return _this.unregisterTechseeMediaStream(mediaStream, MediaConstants_1.MediaStreamUnregisterReason.NativeEvent);
|
|
417
|
+
};
|
|
418
|
+
_this._registeredStreams.set(mediaStream.streamType, mediaStream);
|
|
419
|
+
trace.info("TechseeMediaStream registered - " + mediaStream.streamType, TechseeMediaStream_1.TechseeMediaStream);
|
|
420
|
+
return _this.updateSubscribersWithNewStream(mediaStream).then(function () {
|
|
421
|
+
var eventArgs = { streamType: mediaStream.streamType };
|
|
422
|
+
_this.emitEvent(privateEvents.STREAM_CREATED, eventArgs);
|
|
423
|
+
});
|
|
424
|
+
};
|
|
425
|
+
if (currentStream) {
|
|
426
|
+
return this.unregisterTechseeMediaStream(currentStream, MediaConstants_1.MediaStreamUnregisterReason.ReplacingStream).then(registerTrack);
|
|
427
|
+
}
|
|
428
|
+
return registerTrack();
|
|
429
|
+
};
|
|
430
|
+
TechseeMediaServiceBase.prototype.unregisterTechseeMediaStream = function (streamToUnregister, reason) {
|
|
431
|
+
var _this = this;
|
|
432
|
+
var traceError = function traceError(error) {
|
|
433
|
+
trace.warn('Unregister TechseeMediaStream error:', error);
|
|
434
|
+
};
|
|
435
|
+
var promises = [];
|
|
436
|
+
trace.info('Unregister TechseeMediaStream: ', streamToUnregister.streamType);
|
|
437
|
+
this._subscribers.forEach(function (subscriber) {
|
|
438
|
+
if (subscriber.streamType === streamToUnregister.streamType) {
|
|
439
|
+
promises.push(subscriber.stopRendering().catch(traceError));
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
if (!streamToUnregister.isRemote) {
|
|
443
|
+
this._publishers.forEach(function (publisher) {
|
|
444
|
+
if (publisher.streamTypes.find(function (streamType) {
|
|
445
|
+
return streamType === streamToUnregister.streamType;
|
|
446
|
+
})) {
|
|
447
|
+
if (_this._session && _this._session.instance) {
|
|
448
|
+
promises.push(_this._session.instance.onMediaStreamDestroyed(publisher.destinationRole).catch(traceError));
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
return Promise.all(promises).then(function () {
|
|
454
|
+
_this._registeredStreams.delete(streamToUnregister.streamType);
|
|
455
|
+
trace.info('Stream deleted', streamToUnregister.streamType);
|
|
456
|
+
var eventArgs = { streamType: streamToUnregister.streamType, reason: reason };
|
|
457
|
+
_this.emitEvent(privateEvents.STREAM_DESTROYED, eventArgs);
|
|
458
|
+
});
|
|
459
|
+
};
|
|
460
|
+
TechseeMediaServiceBase.prototype.unregisterRemoteMediaTrack = function (mediaTrack) {
|
|
461
|
+
var _this = this;
|
|
462
|
+
var promises = [];
|
|
463
|
+
this._registeredStreams.forEach(function (registeredStream) {
|
|
464
|
+
if (registeredStream.mediaTrack.id === mediaTrack.id) {
|
|
465
|
+
promises.push(_this.unregisterTechseeMediaStream(registeredStream, MediaConstants_1.MediaStreamUnregisterReason.ClosedRemotely));
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
return Promise.all(promises).then(function () {
|
|
469
|
+
return undefined;
|
|
470
|
+
});
|
|
471
|
+
};
|
|
472
|
+
TechseeMediaServiceBase.prototype.removePublisher = function (publisher) {
|
|
473
|
+
this._publishers.delete(publisher.destinationRole);
|
|
474
|
+
if (this._session && this._session.instance) {
|
|
475
|
+
trace.info('Removing publisher', publisher.destinationRole);
|
|
476
|
+
return this._session.instance.onMediaStreamDestroyed(publisher.destinationRole).catch(function (error) {
|
|
477
|
+
trace.warn('Error while removing publisher', error);
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
return Promise.resolve();
|
|
481
|
+
};
|
|
482
|
+
TechseeMediaServiceBase.prototype.getStreamForDestinationRole = function (destinationRole) {
|
|
483
|
+
var _this = this;
|
|
484
|
+
this.serviceInitGuard();
|
|
485
|
+
if (this._publishers.has(destinationRole)) {
|
|
486
|
+
trace.info("Publisher for " + destinationRole + " already exists");
|
|
487
|
+
return Promise.resolve(this._publishers.get(destinationRole).mediaStream);
|
|
488
|
+
}
|
|
489
|
+
if (!this._publisherPromises.get(destinationRole)) {
|
|
490
|
+
trace.info("Creating publisher for " + destinationRole);
|
|
491
|
+
var publisherPromise = this.initLocalMediaStreams().then(function () {
|
|
492
|
+
return _this.createMediaPublisher(destinationRole);
|
|
493
|
+
}).then(function (mediaPublisher) {
|
|
494
|
+
if (mediaPublisher) {
|
|
495
|
+
_this._publishers.set(destinationRole, mediaPublisher);
|
|
496
|
+
}
|
|
497
|
+
_this._publisherPromises.delete(destinationRole);
|
|
498
|
+
return mediaPublisher ? mediaPublisher.mediaStream : null;
|
|
499
|
+
}).catch(function (ex) {
|
|
500
|
+
_this._publisherPromises.delete(destinationRole);
|
|
501
|
+
throw ex;
|
|
502
|
+
});
|
|
503
|
+
this._publisherPromises.set(destinationRole, publisherPromise);
|
|
504
|
+
} else {
|
|
505
|
+
trace.info("Create publisher promise for " + destinationRole + " already exists");
|
|
506
|
+
}
|
|
507
|
+
return this._publisherPromises.get(destinationRole);
|
|
508
|
+
};
|
|
509
|
+
TechseeMediaServiceBase.prototype.updateSubscribersWithNewStream = function (registeredStream) {
|
|
510
|
+
trace.info("Updating subscribers of " + registeredStream.streamType + " with new stream");
|
|
511
|
+
if (this._subscribers.size === 0) {
|
|
512
|
+
trace.warn("No subscribers exists for the " + registeredStream.streamType + ".");
|
|
513
|
+
} else {
|
|
514
|
+
trace.info("Total " + this._subscribers.size + " subscribers exists, will check if rerender needed.");
|
|
515
|
+
}
|
|
516
|
+
this._subscribers.forEach(function (subscriber) {
|
|
517
|
+
if (subscriber.streamType === registeredStream.streamType) {
|
|
518
|
+
trace.info(registeredStream.streamType + " rendering on subscriber");
|
|
519
|
+
subscriber.renderStream(registeredStream);
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
return Promise.resolve();
|
|
523
|
+
};
|
|
524
|
+
TechseeMediaServiceBase.prototype.bindClassMethods = function () {
|
|
525
|
+
this.updateSessionCredentials = this.updateSessionCredentials.bind(this);
|
|
526
|
+
this.getStreamForDestinationRole = this.getStreamForDestinationRole.bind(this);
|
|
527
|
+
this.registerRemoteMediaTrack = this.registerRemoteMediaTrack.bind(this);
|
|
528
|
+
this.unregisterRemoteMediaTrack = this.unregisterRemoteMediaTrack.bind(this);
|
|
529
|
+
this.createMediaPublisher = this.createMediaPublisher.bind(this);
|
|
530
|
+
this.updateSubscribersWithNewStream = this.updateSubscribersWithNewStream.bind(this);
|
|
531
|
+
this.initLocalMediaStreams = this.initLocalMediaStreams.bind(this);
|
|
532
|
+
this.onSessionDisconnectHandler = this.onSessionDisconnectHandler.bind(this);
|
|
533
|
+
this.clearService = this.clearService.bind(this);
|
|
534
|
+
this.clearRegisteredStreams = this.clearRegisteredStreams.bind(this);
|
|
535
|
+
this.clearPublishers = this.clearPublishers.bind(this);
|
|
536
|
+
this.clearSubscribers = this.clearSubscribers.bind(this);
|
|
537
|
+
};
|
|
538
|
+
//region Cleanup
|
|
539
|
+
TechseeMediaServiceBase.prototype.clearRegisteredStreams = function () {
|
|
540
|
+
var _this = this;
|
|
541
|
+
trace.info('Clearing registered streams');
|
|
542
|
+
var promises = [];
|
|
543
|
+
this._registeredStreams.forEach(function (streamToUnregister) {
|
|
544
|
+
promises.push(_this.unregisterTechseeMediaStream(streamToUnregister, MediaConstants_1.MediaStreamUnregisterReason.ServiceCleanUp));
|
|
545
|
+
});
|
|
546
|
+
return Promise.all(promises).then(function () {
|
|
547
|
+
return undefined;
|
|
548
|
+
});
|
|
549
|
+
};
|
|
550
|
+
TechseeMediaServiceBase.prototype.clearSubscribers = function () {
|
|
551
|
+
var _this = this;
|
|
552
|
+
trace.info('Clearing subscribers');
|
|
553
|
+
var promises = [];
|
|
554
|
+
this._subscribers.forEach(function (subscriber) {
|
|
555
|
+
promises.push(_this.destroySubscriber(subscriber.container));
|
|
556
|
+
});
|
|
557
|
+
return Promise.all(promises).then(function () {
|
|
558
|
+
return undefined;
|
|
559
|
+
});
|
|
560
|
+
};
|
|
561
|
+
TechseeMediaServiceBase.prototype.clearPublishers = function () {
|
|
562
|
+
var _this = this;
|
|
563
|
+
trace.info('Clearing publishers');
|
|
564
|
+
var promises = [];
|
|
565
|
+
this._publishers.forEach(function (publisher) {
|
|
566
|
+
promises.push(_this.removePublisher(publisher));
|
|
567
|
+
});
|
|
568
|
+
return Promise.all(promises).then(function () {
|
|
569
|
+
return undefined;
|
|
570
|
+
});
|
|
571
|
+
};
|
|
572
|
+
//#endregion Cleanup
|
|
573
|
+
//#region Simple Validation Methods
|
|
574
|
+
TechseeMediaServiceBase.prototype.serviceInitGuard = function (shouldThrow) {
|
|
575
|
+
if (shouldThrow === void 0) {
|
|
576
|
+
shouldThrow = true;
|
|
577
|
+
}
|
|
578
|
+
return guards_1.throwableGuard(!!this._serviceOptions, 'Media service is not initialized', shouldThrow);
|
|
579
|
+
};
|
|
580
|
+
TechseeMediaServiceBase.prototype.sessionExistsGuard = function (shouldThrow) {
|
|
581
|
+
if (shouldThrow === void 0) {
|
|
582
|
+
shouldThrow = true;
|
|
583
|
+
}
|
|
584
|
+
return guards_1.throwableGuard(!!this._session, 'There no active session', shouldThrow);
|
|
585
|
+
};
|
|
586
|
+
return TechseeMediaServiceBase;
|
|
587
|
+
}();
|
|
588
|
+
exports.TechseeMediaServiceBase = TechseeMediaServiceBase;
|
|
589
|
+
|
|
590
|
+
//# sourceMappingURL=MediaServiceBase.js.map
|
|
591
|
+
//# sourceMappingURL=MediaServiceBase.js.map
|