vani-meeting-client-native 0.0.2

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 (42) hide show
  1. package/README.md +153 -0
  2. package/lib/MeetingHandler.d.ts +55 -0
  3. package/lib/MeetingHandler.js +496 -0
  4. package/lib/base/Base.d.ts +9 -0
  5. package/lib/base/Base.js +17 -0
  6. package/lib/index.d.ts +7 -0
  7. package/lib/index.js +7 -0
  8. package/lib/inter-communication-handler/CommunicationHandler.d.ts +65 -0
  9. package/lib/inter-communication-handler/CommunicationHandler.js +521 -0
  10. package/lib/model/Event.d.ts +84 -0
  11. package/lib/model/Event.js +35 -0
  12. package/lib/model/MeetingStartRequest.d.ts +54 -0
  13. package/lib/model/MeetingStartRequest.js +60 -0
  14. package/lib/model/MessagePayload.d.ts +32 -0
  15. package/lib/model/MessagePayload.js +31 -0
  16. package/lib/model/Participant.d.ts +16 -0
  17. package/lib/model/Participant.js +22 -0
  18. package/lib/model/TaskResponse.d.ts +5 -0
  19. package/lib/model/TaskResponse.js +1 -0
  20. package/lib/model/Track.d.ts +19 -0
  21. package/lib/model/Track.js +27 -0
  22. package/lib/user-media-handler/UserMediaHandler.d.ts +38 -0
  23. package/lib/user-media-handler/UserMediaHandler.js +786 -0
  24. package/lib/utility/DynamicLibHelper.d.ts +8 -0
  25. package/lib/utility/DynamicLibHelper.js +109 -0
  26. package/lib/utility/DynamicLibHelper.native.d.ts +8 -0
  27. package/lib/utility/DynamicLibHelper.native.js +109 -0
  28. package/lib/utility/DynamicLibHelper.node.d.ts +8 -0
  29. package/lib/utility/DynamicLibHelper.node.js +108 -0
  30. package/lib/utility/Utility.d.ts +3 -0
  31. package/lib/utility/Utility.js +12 -0
  32. package/lib/utility/VaniEventListener.d.ts +4 -0
  33. package/lib/utility/VaniEventListener.js +24 -0
  34. package/lib/video-call-handler/BaseVideoCallHandler.d.ts +25 -0
  35. package/lib/video-call-handler/BaseVideoCallHandler.js +98 -0
  36. package/lib/video-call-handler/SFUHandler.d.ts +57 -0
  37. package/lib/video-call-handler/SFUHandler.js +811 -0
  38. package/lib/video-call-handler/WebrtcHandler.d.ts +17 -0
  39. package/lib/video-call-handler/WebrtcHandler.js +107 -0
  40. package/lib/websocket-handler/WebsocketHandler.d.ts +102 -0
  41. package/lib/websocket-handler/WebsocketHandler.js +541 -0
  42. package/package.json +53 -0
@@ -0,0 +1,786 @@
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = function (d, b) {
3
+ extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
+ return extendStatics(d, b);
7
+ };
8
+ return function (d, b) {
9
+ if (typeof b !== "function" && b !== null)
10
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
+ extendStatics(d, b);
12
+ function __() { this.constructor = d; }
13
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
+ };
15
+ })();
16
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ var __generator = (this && this.__generator) || function (thisArg, body) {
26
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
27
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
28
+ function verb(n) { return function (v) { return step([n, v]); }; }
29
+ function step(op) {
30
+ if (f) throw new TypeError("Generator is already executing.");
31
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
32
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
33
+ if (y = 0, t) op = [op[0] & 2, t.value];
34
+ switch (op[0]) {
35
+ case 0: case 1: t = op; break;
36
+ case 4: _.label++; return { value: op[1], done: false };
37
+ case 5: _.label++; y = op[1]; op = [0]; continue;
38
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
39
+ default:
40
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
41
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
42
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
43
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
44
+ if (t[2]) _.ops.pop();
45
+ _.trys.pop(); continue;
46
+ }
47
+ op = body.call(thisArg, _);
48
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
49
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
50
+ }
51
+ };
52
+ import log from 'loglevel';
53
+ import { CameraFacingMode, LogLevel, VaniEvent } from "..";
54
+ import { Base } from "../base/Base";
55
+ import { Track, TrackKind } from "../model/Track";
56
+ import { DynamicLibHelper } from "../utility/DynamicLibHelper";
57
+ import { WebSocketBasicEvents } from "../websocket-handler/WebsocketHandler";
58
+ export var GetDevicesType;
59
+ (function (GetDevicesType) {
60
+ GetDevicesType["AudioIn"] = "audioinput";
61
+ GetDevicesType["VideoIn"] = "videoinput";
62
+ GetDevicesType["AudioOut"] = "audiooutput";
63
+ })(GetDevicesType || (GetDevicesType = {}));
64
+ var UserMediaHandler = /** @class */ (function (_super) {
65
+ __extends(UserMediaHandler, _super);
66
+ function UserMediaHandler() {
67
+ var _this = _super !== null && _super.apply(this, arguments) || this;
68
+ _this.isVideoAudioFetchInProgress = false;
69
+ _this.isScreenShareFetchInProgress = false;
70
+ _this.tracks = [];
71
+ _this.registerForDeviceChange = function () { return __awaiter(_this, void 0, void 0, function () {
72
+ var mediaDevice;
73
+ var _this = this;
74
+ return __generator(this, function (_a) {
75
+ mediaDevice = new DynamicLibHelper().getMediaDevicesVariable(this.meetingStartRequest);
76
+ if (mediaDevice) {
77
+ mediaDevice.ondevicechange = (function (event) { return __awaiter(_this, void 0, void 0, function () {
78
+ var oldTrack, isAudioInDeviceFound, canFetchAudioIn, audioInDevices, audioDevice, oldVideoTrack, isCameraDeviceFound, canFetchVideoIn, cameraDevices, cameraDevice;
79
+ var _this = this;
80
+ var _a, _b, _c, _d, _e;
81
+ return __generator(this, function (_f) {
82
+ switch (_f.label) {
83
+ case 0:
84
+ oldTrack = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(TrackKind.Audio);
85
+ isAudioInDeviceFound = false;
86
+ canFetchAudioIn = false;
87
+ if (!(oldTrack || (!oldTrack && ((_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.audioInDevice)))) return [3 /*break*/, 2];
88
+ return [4 /*yield*/, this.getDevice(GetDevicesType.AudioIn)];
89
+ case 1:
90
+ audioInDevices = _f.sent();
91
+ if (this.meetingStartRequest && this.meetingStartRequest.audioInDevice) {
92
+ audioDevice = audioInDevices.find(function (device) { return device.id === _this.meetingStartRequest.audioInDevice; });
93
+ if (audioDevice) {
94
+ isAudioInDeviceFound = true;
95
+ }
96
+ else {
97
+ this.meetingStartRequest.audioInDevice = undefined;
98
+ }
99
+ }
100
+ canFetchAudioIn = true;
101
+ _f.label = 2;
102
+ case 2:
103
+ oldVideoTrack = (_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.getSelfTrackByType(TrackKind.Video);
104
+ isCameraDeviceFound = false;
105
+ canFetchVideoIn = false;
106
+ if (!(oldVideoTrack || (!oldVideoTrack && ((_d = this.meetingStartRequest) === null || _d === void 0 ? void 0 : _d.cameraDevice)))) return [3 /*break*/, 4];
107
+ canFetchVideoIn = true;
108
+ return [4 /*yield*/, this.getDevice(GetDevicesType.VideoIn)];
109
+ case 3:
110
+ cameraDevices = _f.sent();
111
+ if (this.meetingStartRequest && this.meetingStartRequest.cameraDevice) {
112
+ cameraDevice = cameraDevices.find(function (device) { return device.id === _this.meetingStartRequest.cameraDevice; });
113
+ if (cameraDevice) {
114
+ isCameraDeviceFound = true;
115
+ }
116
+ else {
117
+ this.meetingStartRequest.cameraDevice = undefined;
118
+ }
119
+ }
120
+ _f.label = 4;
121
+ case 4:
122
+ if ((!isAudioInDeviceFound && canFetchAudioIn) || (!isCameraDeviceFound && canFetchVideoIn)) {
123
+ this.startLocalStream(!isCameraDeviceFound && canFetchVideoIn, !isAudioInDeviceFound && canFetchAudioIn, true);
124
+ }
125
+ (_e = this.communicationHandler) === null || _e === void 0 ? void 0 : _e.emitMessageToSource(VaniEvent.OnDevicesChanged, {});
126
+ return [2 /*return*/];
127
+ }
128
+ });
129
+ }); });
130
+ }
131
+ return [2 /*return*/];
132
+ });
133
+ }); };
134
+ return _this;
135
+ }
136
+ UserMediaHandler.prototype.switchCamera = function () {
137
+ var _a, _b;
138
+ return __awaiter(this, void 0, void 0, function () {
139
+ var videoTrack;
140
+ return __generator(this, function (_c) {
141
+ if (this.meetingStartRequest && this.meetingStartRequest.isMobileApp) {
142
+ videoTrack = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getAllSelfTracks().find(function (track) { return track.trackKind === TrackKind.Video; });
143
+ if (videoTrack && videoTrack.track) {
144
+ videoTrack.track._switchCamera();
145
+ }
146
+ }
147
+ else {
148
+ if (!this.cameraFacingMode) {
149
+ this.cameraFacingMode = (_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.defaultCameraFacingMode;
150
+ }
151
+ if (this.cameraFacingMode === CameraFacingMode.Front) {
152
+ this.cameraFacingMode = CameraFacingMode.Back;
153
+ }
154
+ else {
155
+ this.cameraFacingMode = CameraFacingMode.Front;
156
+ }
157
+ this.meetingStartRequest.cameraDevice = undefined;
158
+ this.startLocalStream(true, false, true);
159
+ }
160
+ return [2 /*return*/];
161
+ });
162
+ });
163
+ };
164
+ UserMediaHandler.prototype.startScreenShare = function (isAudioRequired, shouldAddTrackImmediately, screensharePayload) {
165
+ if (isAudioRequired === void 0) { isAudioRequired = false; }
166
+ if (shouldAddTrackImmediately === void 0) { shouldAddTrackImmediately = true; }
167
+ return __awaiter(this, void 0, void 0, function () {
168
+ var _this = this;
169
+ return __generator(this, function (_a) {
170
+ if (!this.meetingStartRequest) {
171
+ log.warn("meetingStartRequest not found");
172
+ return [2 /*return*/];
173
+ }
174
+ if (this.isScreenShareFetchInProgress) {
175
+ log.warn("Already fetching stream. So request ignored");
176
+ return [2 /*return*/];
177
+ }
178
+ if (!screensharePayload) {
179
+ screensharePayload = { video: true, audio: isAudioRequired };
180
+ }
181
+ this.isScreenShareFetchInProgress = true;
182
+ (new DynamicLibHelper()).getMediaDevicesVariable(this.meetingStartRequest).getDisplayMedia(screensharePayload).then(function (stream) {
183
+ _this.onScreenShareStreamGot(stream, shouldAddTrackImmediately);
184
+ })
185
+ .catch(function (error) {
186
+ var _a;
187
+ log.error(error);
188
+ (_a = _this.communicationHandler) === null || _a === void 0 ? void 0 : _a.emitMessageToSource(VaniEvent.OnPermissionError, error);
189
+ _this.isScreenShareFetchInProgress = false;
190
+ });
191
+ return [2 /*return*/];
192
+ });
193
+ });
194
+ };
195
+ UserMediaHandler.prototype.startLocalStream = function (isVideoRequired, isAudioRequired, shouldAddTrackImmediately, userMediaPayload) {
196
+ var _a;
197
+ return __awaiter(this, void 0, void 0, function () {
198
+ var audioInDevices, cameraDevices;
199
+ return __generator(this, function (_b) {
200
+ switch (_b.label) {
201
+ case 0:
202
+ if (isVideoRequired === false && isAudioRequired === false) {
203
+ this.isVideoAudioFetchInProgress = false;
204
+ (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.emitMessageToSource(VaniEvent.OnPermissionApproved, {});
205
+ return [2 /*return*/];
206
+ }
207
+ if (!userMediaPayload && this.userMediaPayload) {
208
+ userMediaPayload = this.userMediaPayload;
209
+ }
210
+ this.userMediaPayload = userMediaPayload;
211
+ if (this.isVideoAudioFetchInProgress) {
212
+ log.warn("Already fetching stream. So request ignored");
213
+ return [2 /*return*/];
214
+ }
215
+ if (!isVideoRequired) return [3 /*break*/, 2];
216
+ return [4 /*yield*/, this.getDevice(GetDevicesType.VideoIn)];
217
+ case 1:
218
+ cameraDevices = _b.sent();
219
+ _b.label = 2;
220
+ case 2:
221
+ if (!isAudioRequired) return [3 /*break*/, 4];
222
+ return [4 /*yield*/, this.getDevice(GetDevicesType.AudioIn)];
223
+ case 3:
224
+ audioInDevices = _b.sent();
225
+ _b.label = 4;
226
+ case 4:
227
+ isAudioRequired = (isAudioRequired && audioInDevices.length > 0);
228
+ isVideoRequired = (isVideoRequired && cameraDevices.length > 0);
229
+ // if (isAudioRequired && isVideoRequired) {
230
+ // if (audioInDevices![0].id.length > 0 && cameraDevices![0].id.length === 0) {
231
+ // isVideoRequired = false;
232
+ // }
233
+ // else if (audioInDevices![0].id.length === 0 && cameraDevices![0].id.length > 0) {
234
+ // isAudioRequired = false;
235
+ // }
236
+ // }
237
+ this.startCapturingStream(isVideoRequired, isAudioRequired, shouldAddTrackImmediately, userMediaPayload);
238
+ return [2 /*return*/];
239
+ }
240
+ });
241
+ });
242
+ };
243
+ UserMediaHandler.prototype.getVideoDictionay = function () {
244
+ var _a, _b;
245
+ var permissionDict = {
246
+ frameRate: {
247
+ ideal: 40,
248
+ min: 20
249
+ }
250
+ };
251
+ if (((_a = this.meetingStartRequest) === null || _a === void 0 ? void 0 : _a.isMobileApp) === false) {
252
+ if (this.meetingStartRequest && ((_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.videoCaptureWidth) > 0 && this.meetingStartRequest.videoCaptureHeight > 0) {
253
+ permissionDict.height = this.meetingStartRequest.videoCaptureHeight;
254
+ permissionDict.width = this.meetingStartRequest.videoCaptureWidth;
255
+ }
256
+ }
257
+ return permissionDict;
258
+ };
259
+ UserMediaHandler.prototype.getAudioDictionay = function () {
260
+ var permissionDict = {
261
+ echoCancellation: true, noiseSuppression: true, autoGainControl: true, latency: 0.001, googAutoGainControl: false,
262
+ googNoiseSuppression: true,
263
+ googEchoCancellation: true,
264
+ googHighpassFilter: true
265
+ };
266
+ return permissionDict;
267
+ };
268
+ UserMediaHandler.prototype.startCapturingStream = function (isVideoRequired, isAudioRequired, shouldAddTrackImmediately, userMediaPayload) {
269
+ var _this = this;
270
+ var _a, _b, _c, _d, _e;
271
+ if (!userMediaPayload) {
272
+ userMediaPayload = {};
273
+ }
274
+ if (isVideoRequired) {
275
+ var oldTrack = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(TrackKind.Video);
276
+ if (oldTrack && oldTrack.track) {
277
+ oldTrack.track.stop();
278
+ }
279
+ if (!userMediaPayload.video) {
280
+ userMediaPayload.video = this.getVideoDictionay();
281
+ }
282
+ if ((_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.cameraDevice) {
283
+ userMediaPayload.video.deviceId = this.meetingStartRequest.cameraDevice;
284
+ }
285
+ else {
286
+ if (!this.cameraFacingMode) {
287
+ this.cameraFacingMode = (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.defaultCameraFacingMode;
288
+ }
289
+ userMediaPayload.video.facingMode = this.cameraFacingMode;
290
+ }
291
+ }
292
+ if (isAudioRequired) {
293
+ var oldTrack = (_d = this.communicationHandler) === null || _d === void 0 ? void 0 : _d.getSelfTrackByType(TrackKind.Audio);
294
+ if (oldTrack && oldTrack.track) {
295
+ oldTrack.track.stop();
296
+ }
297
+ if (!userMediaPayload.audio) {
298
+ userMediaPayload.audio = this.getAudioDictionay();
299
+ }
300
+ if ((_e = this.meetingStartRequest) === null || _e === void 0 ? void 0 : _e.audioInDevice) {
301
+ userMediaPayload.audio.deviceId = this.meetingStartRequest.audioInDevice;
302
+ }
303
+ }
304
+ this.meetingStartRequest && this.meetingStartRequest.logLevel === LogLevel.Debug && log.info(userMediaPayload);
305
+ (new DynamicLibHelper()).getMediaDevicesVariable(this.meetingStartRequest).getUserMedia(userMediaPayload)
306
+ .then(function (stream) {
307
+ _this.onStreamGot(stream, isAudioRequired, isVideoRequired, shouldAddTrackImmediately);
308
+ })
309
+ .catch(function (error) {
310
+ _this.isVideoAudioFetchInProgress = false;
311
+ log.error(error);
312
+ _this.onStreamError(error, isAudioRequired, isVideoRequired);
313
+ });
314
+ };
315
+ UserMediaHandler.prototype.onStreamError = function (error, isAudioRequired, isVideoRequired) {
316
+ var _a;
317
+ log.warn(error.message);
318
+ (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.emitMessageToSource(VaniEvent.OnPermissionError, error);
319
+ if (isAudioRequired && this.muteUmutePromiseResolver) {
320
+ this.muteUmutePromiseResolver({ message: "Permission Declined", error: 'PermissionDenied', isSuccess: false });
321
+ this.muteUmutePromiseResolver = undefined;
322
+ }
323
+ if (isVideoRequired && this.videoPauseResumePromiseResolver) {
324
+ this.videoPauseResumePromiseResolver({ message: "Permission Declined", error: 'PermissionDenied', isSuccess: false });
325
+ this.videoPauseResumePromiseResolver = undefined;
326
+ }
327
+ };
328
+ UserMediaHandler.prototype.onStreamGot = function (stream, isForAudio, isForVideo, shouldAddTrackImmediately) {
329
+ var _a, _b, _c, _d, _e;
330
+ return __awaiter(this, void 0, void 0, function () {
331
+ var permissionApprovedFor, data, audioPause, data, audioPause;
332
+ var _this = this;
333
+ return __generator(this, function (_f) {
334
+ try {
335
+ if (stream && stream.getVideoTracks()) {
336
+ stream.getVideoTracks().forEach(function (eachMediaTrack) {
337
+ _this.tracks.push(eachMediaTrack);
338
+ });
339
+ }
340
+ if (stream && stream.getAudioTracks()) {
341
+ stream.getAudioTracks().forEach(function (eachMediaTrack) {
342
+ _this.tracks.push(eachMediaTrack);
343
+ });
344
+ }
345
+ }
346
+ catch (err) {
347
+ }
348
+ if (!stream || (isForAudio === false && isForVideo === false)) {
349
+ this.isVideoAudioFetchInProgress = false;
350
+ return [2 /*return*/];
351
+ }
352
+ if (!this.communicationHandler.getSelfParticipant()) {
353
+ return [2 /*return*/];
354
+ }
355
+ permissionApprovedFor = [];
356
+ if (isForAudio && stream.getAudioTracks().length > 0) {
357
+ this.onTrack(stream.getAudioTracks()[0], TrackKind.Audio, shouldAddTrackImmediately);
358
+ // removeAndStreamInLocalStream(stream.getAudioTracks()[0])
359
+ // updateLocalTrackInAllTracks(stream.getAudioTracks()[0]);
360
+ this.communicationHandler.getSelfParticipant().isAudioEnable = true;
361
+ data = { userId: (_a = this.meetingStartRequest) === null || _a === void 0 ? void 0 : _a.userId, type: "audio", status: "resume" };
362
+ audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume };
363
+ this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause);
364
+ if (this.muteUmutePromiseResolver) {
365
+ this.muteUmutePromiseResolver({ message: "Success", error: 'NoError', isSuccess: true });
366
+ this.muteUmutePromiseResolver = undefined;
367
+ }
368
+ if (this.meetingStartRequest
369
+ && stream.getAudioTracks()[0].getSettings() && stream.getAudioTracks()[0].getSettings().deviceId) {
370
+ this.meetingStartRequest.audioInDevice = stream.getAudioTracks()[0].getSettings().deviceId;
371
+ }
372
+ else if (this.meetingStartRequest
373
+ && stream.getAudioTracks()[0].getConstraints() && stream.getAudioTracks()[0].getConstraints().deviceId) {
374
+ this.meetingStartRequest.audioInDevice = (_b = stream.getAudioTracks()[0].getConstraints().deviceId) === null || _b === void 0 ? void 0 : _b.toString();
375
+ }
376
+ permissionApprovedFor.push(TrackKind.Audio);
377
+ }
378
+ if (isForVideo && stream.getVideoTracks().length > 0) {
379
+ this.onTrack(stream.getVideoTracks()[0], TrackKind.Video, shouldAddTrackImmediately);
380
+ // removeAndStreamInLocalStream(stream.getVideoTracks()[0])
381
+ // updateLocalTrackInAllTracks(stream.getVideoTracks()[0])
382
+ this.communicationHandler.getSelfParticipant().isVideoEnable = true;
383
+ data = { userId: (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.userId, type: "video", status: "resume" };
384
+ audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume };
385
+ this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause);
386
+ if (this.videoPauseResumePromiseResolver) {
387
+ this.videoPauseResumePromiseResolver({ message: "Success", error: 'NoError', isSuccess: true });
388
+ this.videoPauseResumePromiseResolver = undefined;
389
+ }
390
+ if (this.meetingStartRequest
391
+ && stream.getVideoTracks()[0].getSettings() && stream.getVideoTracks()[0].getSettings().deviceId) {
392
+ this.meetingStartRequest.cameraDevice = stream.getVideoTracks()[0].getSettings().deviceId;
393
+ }
394
+ else if (this.meetingStartRequest
395
+ && stream.getVideoTracks()[0].getConstraints() && stream.getVideoTracks()[0].getConstraints().deviceId) {
396
+ this.meetingStartRequest.cameraDevice = (_d = stream.getVideoTracks()[0].getConstraints().deviceId) === null || _d === void 0 ? void 0 : _d.toString();
397
+ }
398
+ permissionApprovedFor.push(TrackKind.Video);
399
+ }
400
+ this.isVideoAudioFetchInProgress = false;
401
+ (_e = this.communicationHandler) === null || _e === void 0 ? void 0 : _e.emitMessageToSource(VaniEvent.OnPermissionApproved, permissionApprovedFor);
402
+ this.registerForDeviceChange();
403
+ return [2 /*return*/];
404
+ });
405
+ });
406
+ };
407
+ UserMediaHandler.prototype.onScreenShareStreamGot = function (stream, shouldAddTrackImmediately) {
408
+ var _a;
409
+ return __awaiter(this, void 0, void 0, function () {
410
+ var permissionApprovedFor;
411
+ var _this = this;
412
+ return __generator(this, function (_b) {
413
+ try {
414
+ if (stream && stream.getVideoTracks()) {
415
+ stream.getVideoTracks().forEach(function (eachMediaTrack) {
416
+ _this.tracks.push(eachMediaTrack);
417
+ });
418
+ }
419
+ if (stream && stream.getAudioTracks()) {
420
+ stream.getAudioTracks().forEach(function (eachMediaTrack) {
421
+ _this.tracks.push(eachMediaTrack);
422
+ });
423
+ }
424
+ }
425
+ catch (err) {
426
+ }
427
+ permissionApprovedFor = [];
428
+ if (stream.getVideoTracks().length > 0) {
429
+ this.onTrack(stream.getVideoTracks()[0], TrackKind.ScreenshareVideo, shouldAddTrackImmediately);
430
+ permissionApprovedFor.push(TrackKind.ScreenshareVideo);
431
+ }
432
+ if (stream.getAudioTracks().length > 0) {
433
+ this.onTrack(stream.getAudioTracks()[0], TrackKind.ScreenshareAudio, shouldAddTrackImmediately);
434
+ permissionApprovedFor.push(TrackKind.ScreenshareAudio);
435
+ }
436
+ this.isScreenShareFetchInProgress = false;
437
+ (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.emitMessageToSource(VaniEvent.OnPermissionApproved, permissionApprovedFor);
438
+ return [2 /*return*/];
439
+ });
440
+ });
441
+ };
442
+ UserMediaHandler.prototype.onTrack = function (trackStream, trackKind, shouldAddTrackImmediately) {
443
+ var _a, _b, _c;
444
+ var selfParticpant = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfParticipant();
445
+ if (selfParticpant) {
446
+ var track = new Track(selfParticpant, true, trackKind, trackStream);
447
+ if (!shouldAddTrackImmediately) {
448
+ (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.emitMessageToSource(VaniEvent.OnTrack, track);
449
+ }
450
+ else {
451
+ (_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.addUpdateLocalTrack(track);
452
+ }
453
+ }
454
+ };
455
+ UserMediaHandler.prototype.getDevice = function (type) {
456
+ return __awaiter(this, void 0, void 0, function () {
457
+ var deviceInfos, devices, _i, deviceInfos_1, deviceInfo;
458
+ return __generator(this, function (_a) {
459
+ switch (_a.label) {
460
+ case 0: return [4 /*yield*/, navigator.mediaDevices.enumerateDevices()];
461
+ case 1:
462
+ deviceInfos = _a.sent();
463
+ devices = [];
464
+ for (_i = 0, deviceInfos_1 = deviceInfos; _i < deviceInfos_1.length; _i++) {
465
+ deviceInfo = deviceInfos_1[_i];
466
+ if (deviceInfo.kind === type) {
467
+ devices.push({ id: deviceInfo.deviceId, label: deviceInfo.label });
468
+ }
469
+ }
470
+ return [2 /*return*/, devices];
471
+ }
472
+ });
473
+ });
474
+ };
475
+ UserMediaHandler.prototype.stopScreenSharing = function () {
476
+ var _a, _b, _c, _d;
477
+ return __awaiter(this, void 0, void 0, function () {
478
+ var screenshareAudioTrack, screenshareVideoTrack;
479
+ return __generator(this, function (_e) {
480
+ screenshareAudioTrack = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(TrackKind.ScreenshareAudio);
481
+ if (screenshareAudioTrack) {
482
+ (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.removeTrack(screenshareAudioTrack);
483
+ }
484
+ screenshareVideoTrack = (_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.getSelfTrackByType(TrackKind.ScreenshareVideo);
485
+ if (screenshareVideoTrack) {
486
+ (_d = this.communicationHandler) === null || _d === void 0 ? void 0 : _d.removeTrack(screenshareVideoTrack);
487
+ }
488
+ return [2 /*return*/];
489
+ });
490
+ });
491
+ };
492
+ UserMediaHandler.prototype.pauseCamera = function (userId) {
493
+ return __awaiter(this, void 0, void 0, function () {
494
+ var _this = this;
495
+ return __generator(this, function (_a) {
496
+ return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
497
+ var videoInputDevices, track, data, videoPause, data, videoBlock, participant;
498
+ var _a, _b, _c, _d, _e, _f;
499
+ return __generator(this, function (_g) {
500
+ switch (_g.label) {
501
+ case 0:
502
+ if (!this.meetingStartRequest) {
503
+ resolve({ message: "Start Meeting Object not found", error: 'InvalidMeetingRequest', isSuccess: false });
504
+ return [2 /*return*/];
505
+ }
506
+ if (!(!userId || this.meetingStartRequest.userId === userId)) return [3 /*break*/, 2];
507
+ if (this.isVideoAudioFetchInProgress) {
508
+ resolve({ message: "Already fetching in progress", error: 'Busy', isSuccess: false });
509
+ return [2 /*return*/];
510
+ }
511
+ return [4 /*yield*/, this.getDevice(GetDevicesType.VideoIn)];
512
+ case 1:
513
+ videoInputDevices = _g.sent();
514
+ if (!videoInputDevices || videoInputDevices.length === 0) {
515
+ resolve({ message: "No Video In Devices", error: 'NoDevice', isSuccess: false });
516
+ return [2 /*return*/];
517
+ }
518
+ this.meetingStartRequest && this.meetingStartRequest.logLevel === LogLevel.Debug && console.log("pauseCamera UserMediaHandler");
519
+ track = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(TrackKind.Video);
520
+ if (track) {
521
+ if (track.track) {
522
+ track.track.onended = null;
523
+ track.track.stop();
524
+ }
525
+ (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.removeTrack(track);
526
+ }
527
+ data = { userId: (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.userId, type: "video", status: "pause" };
528
+ videoPause = { message: data };
529
+ (_d = this.communicationHandler) === null || _d === void 0 ? void 0 : _d.sendWebSocketMessage(WebSocketBasicEvents.OnAudioVideoPauseResume, videoPause);
530
+ resolve({ message: "Success", error: 'NoError', isSuccess: true });
531
+ return [3 /*break*/, 3];
532
+ case 2:
533
+ if (this.meetingStartRequest.isAdmin) {
534
+ data = { admin: this.meetingStartRequest.userId, user: userId, type: WebSocketBasicEvents.OnVideoBlockRequest, to: this.meetingStartRequest.roomId };
535
+ videoBlock = { message: data };
536
+ (_e = this.communicationHandler) === null || _e === void 0 ? void 0 : _e.sendWebSocketMessage(WebSocketBasicEvents.OnVideoBlockRequest, videoBlock);
537
+ participant = (_f = this.communicationHandler) === null || _f === void 0 ? void 0 : _f.participantByUserId(userId);
538
+ if (participant) {
539
+ participant.isVideoBlockedByAdmin = true;
540
+ }
541
+ resolve({ message: "Sucess", error: 'NoError', isSuccess: true });
542
+ return [2 /*return*/];
543
+ }
544
+ else {
545
+ resolve({ message: "Permission Not Allowed", error: 'PermissionDenied', isSuccess: false });
546
+ }
547
+ _g.label = 3;
548
+ case 3: return [2 /*return*/];
549
+ }
550
+ });
551
+ }); })];
552
+ });
553
+ });
554
+ };
555
+ UserMediaHandler.prototype.muteUser = function (userId) {
556
+ return __awaiter(this, void 0, void 0, function () {
557
+ var _this = this;
558
+ return __generator(this, function (_a) {
559
+ return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
560
+ var audioinputDevices, track, data, audioPause, data, audioBlock, participant;
561
+ var _a, _b, _c, _d, _e, _f;
562
+ return __generator(this, function (_g) {
563
+ switch (_g.label) {
564
+ case 0:
565
+ if (!this.meetingStartRequest) {
566
+ resolve({ message: "Start Meeting Object not found", error: 'InvalidMeetingRequest', isSuccess: false });
567
+ return [2 /*return*/];
568
+ }
569
+ if (!(!userId || this.meetingStartRequest.userId === userId)) return [3 /*break*/, 2];
570
+ if (this.isVideoAudioFetchInProgress) {
571
+ resolve({ message: "Already fetching in progress", error: 'Busy', isSuccess: false });
572
+ return [2 /*return*/];
573
+ }
574
+ return [4 /*yield*/, this.getDevice(GetDevicesType.AudioIn)];
575
+ case 1:
576
+ audioinputDevices = _g.sent();
577
+ if (!audioinputDevices || audioinputDevices.length === 0) {
578
+ resolve({ message: "No Audio In Devices", error: 'NoDevice', isSuccess: false });
579
+ return [2 /*return*/];
580
+ }
581
+ this.meetingStartRequest && this.meetingStartRequest.logLevel === LogLevel.Debug && console.log("muteUser UserMediaHandler");
582
+ track = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(TrackKind.Audio);
583
+ if (track) {
584
+ if (track.track) {
585
+ track.track.onended = null;
586
+ track.track.stop();
587
+ }
588
+ (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.removeTrack(track);
589
+ }
590
+ data = { userId: (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.userId, type: "audio", status: "pause" };
591
+ audioPause = { message: data };
592
+ (_d = this.communicationHandler) === null || _d === void 0 ? void 0 : _d.sendWebSocketMessage(WebSocketBasicEvents.OnAudioVideoPauseResume, audioPause);
593
+ resolve({ message: "Success", error: 'NoError', isSuccess: true });
594
+ return [3 /*break*/, 3];
595
+ case 2:
596
+ if (this.meetingStartRequest.isAdmin) {
597
+ data = { admin: this.meetingStartRequest.userId, user: userId, type: WebSocketBasicEvents.OnAudioBlockRequest, to: this.meetingStartRequest.roomId };
598
+ audioBlock = { message: data };
599
+ (_e = this.communicationHandler) === null || _e === void 0 ? void 0 : _e.sendWebSocketMessage(WebSocketBasicEvents.OnAudioBlockRequest, audioBlock);
600
+ participant = (_f = this.communicationHandler) === null || _f === void 0 ? void 0 : _f.participantByUserId(userId);
601
+ if (participant) {
602
+ participant.isAudioBlockedByAdmin = true;
603
+ }
604
+ resolve({ message: "Sucess", error: 'NoError', isSuccess: true });
605
+ return [2 /*return*/];
606
+ }
607
+ else {
608
+ resolve({ message: "Permission Not Allowed", error: 'PermissionDenied', isSuccess: false });
609
+ }
610
+ _g.label = 3;
611
+ case 3: return [2 /*return*/];
612
+ }
613
+ });
614
+ }); })];
615
+ });
616
+ });
617
+ };
618
+ UserMediaHandler.prototype.resumeCamera = function (userId) {
619
+ return __awaiter(this, void 0, void 0, function () {
620
+ var _this = this;
621
+ return __generator(this, function (_a) {
622
+ return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
623
+ var participant, videoInputDevices, data, videoUnblock, participant;
624
+ var _a, _b, _c;
625
+ return __generator(this, function (_d) {
626
+ switch (_d.label) {
627
+ case 0:
628
+ if (!this.meetingStartRequest) {
629
+ resolve({ message: "Start Meeting Object not found", error: 'InvalidMeetingRequest', isSuccess: false });
630
+ return [2 /*return*/];
631
+ }
632
+ if (!(!userId || this.meetingStartRequest.userId === userId)) return [3 /*break*/, 2];
633
+ if (this.isVideoAudioFetchInProgress) {
634
+ resolve({ message: "Already fetching in progress", error: 'Busy', isSuccess: false });
635
+ return [2 /*return*/];
636
+ }
637
+ participant = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfParticipant();
638
+ if (!participant || participant.isVideoBlockedByAdmin) {
639
+ resolve({ message: "Video Blocked By Admin", error: 'BlockedByAdmin', isSuccess: false });
640
+ return [2 /*return*/];
641
+ }
642
+ return [4 /*yield*/, this.getDevice(GetDevicesType.VideoIn)];
643
+ case 1:
644
+ videoInputDevices = _d.sent();
645
+ if (!videoInputDevices || videoInputDevices.length === 0) {
646
+ resolve({ message: "No Video In Devices", error: 'NoDevice', isSuccess: false });
647
+ return [2 /*return*/];
648
+ }
649
+ this.videoPauseResumePromiseResolver = resolve;
650
+ this.startLocalStream(true, false, true);
651
+ return [3 /*break*/, 3];
652
+ case 2:
653
+ if (this.meetingStartRequest.isAdmin) {
654
+ data = { admin: this.meetingStartRequest.userId, user: userId, type: WebSocketBasicEvents.OnVideoUnblockRequest, to: this.meetingStartRequest.roomId };
655
+ videoUnblock = { message: data };
656
+ (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.sendWebSocketMessage(WebSocketBasicEvents.OnVideoUnblockRequest, videoUnblock);
657
+ participant = (_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.participantByUserId(userId);
658
+ if (participant) {
659
+ participant.isVideoBlockedByAdmin = false;
660
+ }
661
+ resolve({ message: "Sucess", error: 'NoError', isSuccess: true });
662
+ return [2 /*return*/];
663
+ }
664
+ else {
665
+ resolve({ message: "Permission Not Allowed", error: 'PermissionDenied', isSuccess: false });
666
+ }
667
+ _d.label = 3;
668
+ case 3: return [2 /*return*/];
669
+ }
670
+ });
671
+ }); })];
672
+ });
673
+ });
674
+ };
675
+ UserMediaHandler.prototype.unmute = function (userId) {
676
+ return __awaiter(this, void 0, void 0, function () {
677
+ var _this = this;
678
+ return __generator(this, function (_a) {
679
+ return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
680
+ var participant, audioInputDevices, data, audioUnblock, participant;
681
+ var _a, _b, _c;
682
+ return __generator(this, function (_d) {
683
+ switch (_d.label) {
684
+ case 0:
685
+ if (!this.meetingStartRequest) {
686
+ resolve({ message: "Start Meeting Object not found", error: 'InvalidMeetingRequest', isSuccess: false });
687
+ return [2 /*return*/];
688
+ }
689
+ if (!(!userId || this.meetingStartRequest.userId === userId)) return [3 /*break*/, 2];
690
+ if (this.isVideoAudioFetchInProgress) {
691
+ resolve({ message: "Already fetching in progress", error: 'Busy', isSuccess: false });
692
+ return [2 /*return*/];
693
+ }
694
+ participant = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfParticipant();
695
+ if (!participant || participant.isAudioBlockedByAdmin) {
696
+ resolve({ message: "Audio Blocked By Admin", error: 'BlockedByAdmin', isSuccess: false });
697
+ return [2 /*return*/];
698
+ }
699
+ return [4 /*yield*/, this.getDevice(GetDevicesType.AudioIn)];
700
+ case 1:
701
+ audioInputDevices = _d.sent();
702
+ if (!audioInputDevices || audioInputDevices.length === 0) {
703
+ resolve({ message: "No Audio In Devices", error: 'NoDevice', isSuccess: false });
704
+ return [2 /*return*/];
705
+ }
706
+ this.muteUmutePromiseResolver = resolve;
707
+ this.startLocalStream(false, true, true);
708
+ return [3 /*break*/, 3];
709
+ case 2:
710
+ if (this.meetingStartRequest.isAdmin) {
711
+ data = { admin: this.meetingStartRequest.userId, user: userId, type: WebSocketBasicEvents.OnAudioUnblockRequest, to: this.meetingStartRequest.roomId };
712
+ audioUnblock = { message: data };
713
+ (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.sendWebSocketMessage(WebSocketBasicEvents.OnAudioUnblockRequest, audioUnblock);
714
+ participant = (_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.participantByUserId(userId);
715
+ if (participant) {
716
+ participant.isAudioBlockedByAdmin = false;
717
+ }
718
+ resolve({ message: "Sucess", error: 'NoError', isSuccess: true });
719
+ return [2 /*return*/];
720
+ }
721
+ else {
722
+ resolve({ message: "Permission Not Allowed", error: 'PermissionDenied', isSuccess: false });
723
+ }
724
+ _d.label = 3;
725
+ case 3: return [2 /*return*/];
726
+ }
727
+ });
728
+ }); })];
729
+ });
730
+ });
731
+ };
732
+ UserMediaHandler.prototype.pauseStreamWithoutStopping = function (streamKind) {
733
+ var _a, _b, _c;
734
+ var track = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(streamKind);
735
+ if (track && track.track) {
736
+ track.track.enabled = false;
737
+ if (streamKind === TrackKind.Audio) {
738
+ var data = { userId: (_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.userId, type: "audio", status: "pause" };
739
+ var audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume };
740
+ this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause);
741
+ }
742
+ else {
743
+ var data = { userId: (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.userId, type: "video", status: "pause" };
744
+ var audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume };
745
+ this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause);
746
+ }
747
+ return { message: "Success", error: 'NoError', isSuccess: true };
748
+ }
749
+ else {
750
+ return ({ message: "No Track Found", error: 'NoDevice', isSuccess: false });
751
+ }
752
+ };
753
+ UserMediaHandler.prototype.resumeStreamWithoutAdding = function (streamKind) {
754
+ var _a, _b, _c;
755
+ var track = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(streamKind);
756
+ if (track && track.track) {
757
+ track.track.enabled = true;
758
+ if (streamKind === TrackKind.Audio) {
759
+ var data = { userId: (_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.userId, type: "audio", status: "resume" };
760
+ var audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume };
761
+ this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause);
762
+ }
763
+ else {
764
+ var data = { userId: (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.userId, type: "video", status: "resume" };
765
+ var audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume };
766
+ this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause);
767
+ }
768
+ return { message: "Success", error: 'NoError', isSuccess: true };
769
+ }
770
+ else {
771
+ return ({ message: "No Track Found", error: 'NoDevice', isSuccess: false });
772
+ }
773
+ };
774
+ UserMediaHandler.prototype.cleanup = function () {
775
+ this.tracks.forEach(function (eachTrack) {
776
+ try {
777
+ eachTrack.stop();
778
+ }
779
+ catch (err) {
780
+ }
781
+ });
782
+ _super.prototype.cleanup.call(this);
783
+ };
784
+ return UserMediaHandler;
785
+ }(Base));
786
+ export { UserMediaHandler };