UNPKG

vani-meeting-client

Version:
785 lines 50 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { 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; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import log from 'loglevel'; import { CameraFacingMode, LogLevel, VaniEvent } from ".."; import { Base } from "../base/Base"; import { Track, TrackKind } from "../model/Track"; import { DynamicLibHelper } from "../utility/DynamicLibHelper"; import { WebSocketBasicEvents } from "../websocket-handler/WebsocketHandler"; export var GetDevicesType; (function (GetDevicesType) { GetDevicesType["AudioIn"] = "audioinput"; GetDevicesType["VideoIn"] = "videoinput"; GetDevicesType["AudioOut"] = "audiooutput"; })(GetDevicesType || (GetDevicesType = {})); var UserMediaHandler = /** @class */ (function (_super) { __extends(UserMediaHandler, _super); function UserMediaHandler() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.isVideoAudioFetchInProgress = false; _this.isScreenShareFetchInProgress = false; _this.tracks = []; _this.registerForDeviceChange = function () { return __awaiter(_this, void 0, void 0, function () { var mediaDevice; var _this = this; return __generator(this, function (_a) { mediaDevice = new DynamicLibHelper().getMediaDevicesVariable(this.meetingStartRequest); if (mediaDevice) { mediaDevice.ondevicechange = (function (event) { return __awaiter(_this, void 0, void 0, function () { var oldTrack, isAudioInDeviceFound, canFetchAudioIn, audioInDevices, audioDevice, oldVideoTrack, isCameraDeviceFound, canFetchVideoIn, cameraDevices, cameraDevice; var _this = this; var _a, _b, _c, _d, _e; return __generator(this, function (_f) { switch (_f.label) { case 0: oldTrack = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(TrackKind.Audio); isAudioInDeviceFound = false; canFetchAudioIn = false; if (!(oldTrack || (!oldTrack && ((_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.audioInDevice)))) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDevice(GetDevicesType.AudioIn)]; case 1: audioInDevices = _f.sent(); if (this.meetingStartRequest && this.meetingStartRequest.audioInDevice) { audioDevice = audioInDevices.find(function (device) { return device.id === _this.meetingStartRequest.audioInDevice; }); if (audioDevice) { isAudioInDeviceFound = true; } else { this.meetingStartRequest.audioInDevice = undefined; } } canFetchAudioIn = true; _f.label = 2; case 2: oldVideoTrack = (_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.getSelfTrackByType(TrackKind.Video); isCameraDeviceFound = false; canFetchVideoIn = false; if (!(oldVideoTrack || (!oldVideoTrack && ((_d = this.meetingStartRequest) === null || _d === void 0 ? void 0 : _d.cameraDevice)))) return [3 /*break*/, 4]; canFetchVideoIn = true; return [4 /*yield*/, this.getDevice(GetDevicesType.VideoIn)]; case 3: cameraDevices = _f.sent(); if (this.meetingStartRequest && this.meetingStartRequest.cameraDevice) { cameraDevice = cameraDevices.find(function (device) { return device.id === _this.meetingStartRequest.cameraDevice; }); if (cameraDevice) { isCameraDeviceFound = true; } else { this.meetingStartRequest.cameraDevice = undefined; } } _f.label = 4; case 4: if ((!isAudioInDeviceFound && canFetchAudioIn) || (!isCameraDeviceFound && canFetchVideoIn)) { this.startLocalStream(!isCameraDeviceFound && canFetchVideoIn, !isAudioInDeviceFound && canFetchAudioIn, true); } (_e = this.communicationHandler) === null || _e === void 0 ? void 0 : _e.emitMessageToSource(VaniEvent.OnDevicesChanged, {}); return [2 /*return*/]; } }); }); }); } return [2 /*return*/]; }); }); }; return _this; } UserMediaHandler.prototype.switchCamera = function () { var _a, _b; return __awaiter(this, void 0, void 0, function () { var videoTrack; return __generator(this, function (_c) { if (this.meetingStartRequest && this.meetingStartRequest.isMobileApp) { videoTrack = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getAllSelfTracks().find(function (track) { return track.trackKind === TrackKind.Video; }); if (videoTrack && videoTrack.track) { videoTrack.track._switchCamera(); } } else { if (!this.cameraFacingMode) { this.cameraFacingMode = (_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.defaultCameraFacingMode; } if (this.cameraFacingMode === CameraFacingMode.Front) { this.cameraFacingMode = CameraFacingMode.Back; } else { this.cameraFacingMode = CameraFacingMode.Front; } this.meetingStartRequest.cameraDevice = undefined; this.startLocalStream(true, false, true); } return [2 /*return*/]; }); }); }; UserMediaHandler.prototype.startScreenShare = function (isAudioRequired, shouldAddTrackImmediately, screensharePayload) { if (isAudioRequired === void 0) { isAudioRequired = false; } if (shouldAddTrackImmediately === void 0) { shouldAddTrackImmediately = true; } return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { if (!this.meetingStartRequest) { log.warn("meetingStartRequest not found"); return [2 /*return*/]; } if (this.isScreenShareFetchInProgress) { log.warn("Already fetching stream. So request ignored"); return [2 /*return*/]; } if (!screensharePayload) { screensharePayload = { video: true, audio: isAudioRequired }; } this.isScreenShareFetchInProgress = true; (new DynamicLibHelper()).getMediaDevicesVariable(this.meetingStartRequest).getDisplayMedia(screensharePayload).then(function (stream) { _this.onScreenShareStreamGot(stream, shouldAddTrackImmediately); }) .catch(function (error) { var _a; log.error(error); (_a = _this.communicationHandler) === null || _a === void 0 ? void 0 : _a.emitMessageToSource(VaniEvent.OnPermissionError, error); _this.isScreenShareFetchInProgress = false; }); return [2 /*return*/]; }); }); }; UserMediaHandler.prototype.startLocalStream = function (isVideoRequired, isAudioRequired, shouldAddTrackImmediately, userMediaPayload) { var _a; return __awaiter(this, void 0, void 0, function () { var audioInDevices, cameraDevices; return __generator(this, function (_b) { switch (_b.label) { case 0: if (isVideoRequired === false && isAudioRequired === false) { this.isVideoAudioFetchInProgress = false; (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.emitMessageToSource(VaniEvent.OnPermissionApproved, {}); return [2 /*return*/]; } if (!userMediaPayload && this.userMediaPayload) { userMediaPayload = this.userMediaPayload; } this.userMediaPayload = userMediaPayload; if (this.isVideoAudioFetchInProgress) { log.warn("Already fetching stream. So request ignored"); return [2 /*return*/]; } if (!isVideoRequired) return [3 /*break*/, 2]; return [4 /*yield*/, this.getDevice(GetDevicesType.VideoIn)]; case 1: cameraDevices = _b.sent(); _b.label = 2; case 2: if (!isAudioRequired) return [3 /*break*/, 4]; return [4 /*yield*/, this.getDevice(GetDevicesType.AudioIn)]; case 3: audioInDevices = _b.sent(); _b.label = 4; case 4: isAudioRequired = (isAudioRequired && audioInDevices.length > 0); isVideoRequired = (isVideoRequired && cameraDevices.length > 0); // if (isAudioRequired && isVideoRequired) { // if (audioInDevices![0].id.length > 0 && cameraDevices![0].id.length === 0) { // isVideoRequired = false; // } // else if (audioInDevices![0].id.length === 0 && cameraDevices![0].id.length > 0) { // isAudioRequired = false; // } // } this.startCapturingStream(isVideoRequired, isAudioRequired, shouldAddTrackImmediately, userMediaPayload); return [2 /*return*/]; } }); }); }; UserMediaHandler.prototype.getVideoDictionay = function () { var _a, _b; var permissionDict = { frameRate: { ideal: 40, min: 20 } }; if (((_a = this.meetingStartRequest) === null || _a === void 0 ? void 0 : _a.isMobileApp) === false) { if (this.meetingStartRequest && ((_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.videoCaptureWidth) > 0 && this.meetingStartRequest.videoCaptureHeight > 0) { permissionDict.height = this.meetingStartRequest.videoCaptureHeight; permissionDict.width = this.meetingStartRequest.videoCaptureWidth; } } return permissionDict; }; UserMediaHandler.prototype.getAudioDictionay = function () { var permissionDict = { echoCancellation: true, noiseSuppression: true, autoGainControl: true, latency: 0.001, googAutoGainControl: false, googNoiseSuppression: true, googEchoCancellation: true, googHighpassFilter: true }; return permissionDict; }; UserMediaHandler.prototype.startCapturingStream = function (isVideoRequired, isAudioRequired, shouldAddTrackImmediately, userMediaPayload) { var _this = this; var _a, _b, _c, _d, _e; if (!userMediaPayload) { userMediaPayload = {}; } if (isVideoRequired) { var oldTrack = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(TrackKind.Video); if (oldTrack && oldTrack.track) { oldTrack.track.stop(); } if (!userMediaPayload.video) { userMediaPayload.video = this.getVideoDictionay(); } if ((_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.cameraDevice) { userMediaPayload.video.deviceId = this.meetingStartRequest.cameraDevice; } else { if (!this.cameraFacingMode) { this.cameraFacingMode = (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.defaultCameraFacingMode; } userMediaPayload.video.facingMode = this.cameraFacingMode; } } if (isAudioRequired) { var oldTrack = (_d = this.communicationHandler) === null || _d === void 0 ? void 0 : _d.getSelfTrackByType(TrackKind.Audio); if (oldTrack && oldTrack.track) { oldTrack.track.stop(); } if (!userMediaPayload.audio) { userMediaPayload.audio = this.getAudioDictionay(); } if ((_e = this.meetingStartRequest) === null || _e === void 0 ? void 0 : _e.audioInDevice) { userMediaPayload.audio.deviceId = this.meetingStartRequest.audioInDevice; } } this.meetingStartRequest && this.meetingStartRequest.logLevel === LogLevel.Debug && log.info(userMediaPayload); (new DynamicLibHelper()).getMediaDevicesVariable(this.meetingStartRequest).getUserMedia(userMediaPayload) .then(function (stream) { _this.onStreamGot(stream, isAudioRequired, isVideoRequired, shouldAddTrackImmediately); }) .catch(function (error) { _this.isVideoAudioFetchInProgress = false; log.error(error); _this.onStreamError(error, isAudioRequired, isVideoRequired); }); }; UserMediaHandler.prototype.onStreamError = function (error, isAudioRequired, isVideoRequired) { var _a; log.warn(error.message); (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.emitMessageToSource(VaniEvent.OnPermissionError, error); if (isAudioRequired && this.muteUmutePromiseResolver) { this.muteUmutePromiseResolver({ message: "Permission Declined", error: 'PermissionDenied', isSuccess: false }); this.muteUmutePromiseResolver = undefined; } if (isVideoRequired && this.videoPauseResumePromiseResolver) { this.videoPauseResumePromiseResolver({ message: "Permission Declined", error: 'PermissionDenied', isSuccess: false }); this.videoPauseResumePromiseResolver = undefined; } }; UserMediaHandler.prototype.onStreamGot = function (stream, isForAudio, isForVideo, shouldAddTrackImmediately) { var _a, _b, _c, _d, _e; return __awaiter(this, void 0, void 0, function () { var permissionApprovedFor, data, audioPause, data, audioPause; var _this = this; return __generator(this, function (_f) { try { if (stream && stream.getVideoTracks()) { stream.getVideoTracks().forEach(function (eachMediaTrack) { _this.tracks.push(eachMediaTrack); }); } if (stream && stream.getAudioTracks()) { stream.getAudioTracks().forEach(function (eachMediaTrack) { _this.tracks.push(eachMediaTrack); }); } } catch (err) { } if (!stream || (isForAudio === false && isForVideo === false)) { this.isVideoAudioFetchInProgress = false; return [2 /*return*/]; } if (!this.communicationHandler.getSelfParticipant()) { return [2 /*return*/]; } permissionApprovedFor = []; if (isForAudio && stream.getAudioTracks().length > 0) { this.onTrack(stream.getAudioTracks()[0], TrackKind.Audio, shouldAddTrackImmediately); // removeAndStreamInLocalStream(stream.getAudioTracks()[0]) // updateLocalTrackInAllTracks(stream.getAudioTracks()[0]); this.communicationHandler.getSelfParticipant().isAudioEnable = true; data = { userId: (_a = this.meetingStartRequest) === null || _a === void 0 ? void 0 : _a.userId, type: "audio", status: "resume" }; audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume }; this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause); if (this.muteUmutePromiseResolver) { this.muteUmutePromiseResolver({ message: "Success", error: 'NoError', isSuccess: true }); this.muteUmutePromiseResolver = undefined; } if (this.meetingStartRequest && stream.getAudioTracks()[0].getSettings() && stream.getAudioTracks()[0].getSettings().deviceId) { this.meetingStartRequest.audioInDevice = stream.getAudioTracks()[0].getSettings().deviceId; } else if (this.meetingStartRequest && stream.getAudioTracks()[0].getConstraints() && stream.getAudioTracks()[0].getConstraints().deviceId) { this.meetingStartRequest.audioInDevice = (_b = stream.getAudioTracks()[0].getConstraints().deviceId) === null || _b === void 0 ? void 0 : _b.toString(); } permissionApprovedFor.push(TrackKind.Audio); } if (isForVideo && stream.getVideoTracks().length > 0) { this.onTrack(stream.getVideoTracks()[0], TrackKind.Video, shouldAddTrackImmediately); // removeAndStreamInLocalStream(stream.getVideoTracks()[0]) // updateLocalTrackInAllTracks(stream.getVideoTracks()[0]) this.communicationHandler.getSelfParticipant().isVideoEnable = true; data = { userId: (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.userId, type: "video", status: "resume" }; audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume }; this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause); if (this.videoPauseResumePromiseResolver) { this.videoPauseResumePromiseResolver({ message: "Success", error: 'NoError', isSuccess: true }); this.videoPauseResumePromiseResolver = undefined; } if (this.meetingStartRequest && stream.getVideoTracks()[0].getSettings() && stream.getVideoTracks()[0].getSettings().deviceId) { this.meetingStartRequest.cameraDevice = stream.getVideoTracks()[0].getSettings().deviceId; } else if (this.meetingStartRequest && stream.getVideoTracks()[0].getConstraints() && stream.getVideoTracks()[0].getConstraints().deviceId) { this.meetingStartRequest.cameraDevice = (_d = stream.getVideoTracks()[0].getConstraints().deviceId) === null || _d === void 0 ? void 0 : _d.toString(); } permissionApprovedFor.push(TrackKind.Video); } this.isVideoAudioFetchInProgress = false; (_e = this.communicationHandler) === null || _e === void 0 ? void 0 : _e.emitMessageToSource(VaniEvent.OnPermissionApproved, permissionApprovedFor); this.registerForDeviceChange(); return [2 /*return*/]; }); }); }; UserMediaHandler.prototype.onScreenShareStreamGot = function (stream, shouldAddTrackImmediately) { var _a; return __awaiter(this, void 0, void 0, function () { var permissionApprovedFor; var _this = this; return __generator(this, function (_b) { try { if (stream && stream.getVideoTracks()) { stream.getVideoTracks().forEach(function (eachMediaTrack) { _this.tracks.push(eachMediaTrack); }); } if (stream && stream.getAudioTracks()) { stream.getAudioTracks().forEach(function (eachMediaTrack) { _this.tracks.push(eachMediaTrack); }); } } catch (err) { } permissionApprovedFor = []; if (stream.getVideoTracks().length > 0) { this.onTrack(stream.getVideoTracks()[0], TrackKind.ScreenshareVideo, shouldAddTrackImmediately); permissionApprovedFor.push(TrackKind.ScreenshareVideo); } if (stream.getAudioTracks().length > 0) { this.onTrack(stream.getAudioTracks()[0], TrackKind.ScreenshareAudio, shouldAddTrackImmediately); permissionApprovedFor.push(TrackKind.ScreenshareAudio); } this.isScreenShareFetchInProgress = false; (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.emitMessageToSource(VaniEvent.OnPermissionApproved, permissionApprovedFor); return [2 /*return*/]; }); }); }; UserMediaHandler.prototype.onTrack = function (trackStream, trackKind, shouldAddTrackImmediately) { var _a, _b, _c; var selfParticpant = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfParticipant(); if (selfParticpant) { var track = new Track(selfParticpant, true, trackKind, trackStream); if (!shouldAddTrackImmediately) { (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.emitMessageToSource(VaniEvent.OnTrack, track); } else { (_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.addUpdateLocalTrack(track); } } }; UserMediaHandler.prototype.getDevice = function (type) { return __awaiter(this, void 0, void 0, function () { var deviceInfos, devices, _i, deviceInfos_1, deviceInfo; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, navigator.mediaDevices.enumerateDevices()]; case 1: deviceInfos = _a.sent(); devices = []; for (_i = 0, deviceInfos_1 = deviceInfos; _i < deviceInfos_1.length; _i++) { deviceInfo = deviceInfos_1[_i]; if (deviceInfo.kind === type) { devices.push({ id: deviceInfo.deviceId, label: deviceInfo.label }); } } return [2 /*return*/, devices]; } }); }); }; UserMediaHandler.prototype.stopScreenSharing = function () { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var screenshareAudioTrack, screenshareVideoTrack; return __generator(this, function (_e) { screenshareAudioTrack = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(TrackKind.ScreenshareAudio); if (screenshareAudioTrack) { (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.removeTrack(screenshareAudioTrack); } screenshareVideoTrack = (_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.getSelfTrackByType(TrackKind.ScreenshareVideo); if (screenshareVideoTrack) { (_d = this.communicationHandler) === null || _d === void 0 ? void 0 : _d.removeTrack(screenshareVideoTrack); } return [2 /*return*/]; }); }); }; UserMediaHandler.prototype.pauseCamera = function (userId) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { var videoInputDevices, track, data, videoPause, data, videoBlock, participant; var _a, _b, _c, _d, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: if (!this.meetingStartRequest) { resolve({ message: "Start Meeting Object not found", error: 'InvalidMeetingRequest', isSuccess: false }); return [2 /*return*/]; } if (!(!userId || this.meetingStartRequest.userId === userId)) return [3 /*break*/, 2]; if (this.isVideoAudioFetchInProgress) { resolve({ message: "Already fetching in progress", error: 'Busy', isSuccess: false }); return [2 /*return*/]; } return [4 /*yield*/, this.getDevice(GetDevicesType.VideoIn)]; case 1: videoInputDevices = _g.sent(); if (!videoInputDevices || videoInputDevices.length === 0) { resolve({ message: "No Video In Devices", error: 'NoDevice', isSuccess: false }); return [2 /*return*/]; } this.meetingStartRequest && this.meetingStartRequest.logLevel === LogLevel.Debug && console.log("pauseCamera UserMediaHandler"); track = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(TrackKind.Video); if (track) { if (track.track) { track.track.onended = null; track.track.stop(); } (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.removeTrack(track); } data = { userId: (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.userId, type: "video", status: "pause" }; videoPause = { message: data }; (_d = this.communicationHandler) === null || _d === void 0 ? void 0 : _d.sendWebSocketMessage(WebSocketBasicEvents.OnAudioVideoPauseResume, videoPause); resolve({ message: "Success", error: 'NoError', isSuccess: true }); return [3 /*break*/, 3]; case 2: if (this.meetingStartRequest.isAdmin) { data = { admin: this.meetingStartRequest.userId, user: userId, type: WebSocketBasicEvents.OnVideoBlockRequest, to: this.meetingStartRequest.roomId }; videoBlock = { message: data }; (_e = this.communicationHandler) === null || _e === void 0 ? void 0 : _e.sendWebSocketMessage(WebSocketBasicEvents.OnVideoBlockRequest, videoBlock); participant = (_f = this.communicationHandler) === null || _f === void 0 ? void 0 : _f.participantByUserId(userId); if (participant) { participant.isVideoBlockedByAdmin = true; } resolve({ message: "Sucess", error: 'NoError', isSuccess: true }); return [2 /*return*/]; } else { resolve({ message: "Permission Not Allowed", error: 'PermissionDenied', isSuccess: false }); } _g.label = 3; case 3: return [2 /*return*/]; } }); }); })]; }); }); }; UserMediaHandler.prototype.muteUser = function (userId) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { var audioinputDevices, track, data, audioPause, data, audioBlock, participant; var _a, _b, _c, _d, _e, _f; return __generator(this, function (_g) { switch (_g.label) { case 0: if (!this.meetingStartRequest) { resolve({ message: "Start Meeting Object not found", error: 'InvalidMeetingRequest', isSuccess: false }); return [2 /*return*/]; } if (!(!userId || this.meetingStartRequest.userId === userId)) return [3 /*break*/, 2]; if (this.isVideoAudioFetchInProgress) { resolve({ message: "Already fetching in progress", error: 'Busy', isSuccess: false }); return [2 /*return*/]; } return [4 /*yield*/, this.getDevice(GetDevicesType.AudioIn)]; case 1: audioinputDevices = _g.sent(); if (!audioinputDevices || audioinputDevices.length === 0) { resolve({ message: "No Audio In Devices", error: 'NoDevice', isSuccess: false }); return [2 /*return*/]; } this.meetingStartRequest && this.meetingStartRequest.logLevel === LogLevel.Debug && console.log("muteUser UserMediaHandler"); track = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(TrackKind.Audio); if (track) { if (track.track) { track.track.onended = null; track.track.stop(); } (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.removeTrack(track); } data = { userId: (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.userId, type: "audio", status: "pause" }; audioPause = { message: data }; (_d = this.communicationHandler) === null || _d === void 0 ? void 0 : _d.sendWebSocketMessage(WebSocketBasicEvents.OnAudioVideoPauseResume, audioPause); resolve({ message: "Success", error: 'NoError', isSuccess: true }); return [3 /*break*/, 3]; case 2: if (this.meetingStartRequest.isAdmin) { data = { admin: this.meetingStartRequest.userId, user: userId, type: WebSocketBasicEvents.OnAudioBlockRequest, to: this.meetingStartRequest.roomId }; audioBlock = { message: data }; (_e = this.communicationHandler) === null || _e === void 0 ? void 0 : _e.sendWebSocketMessage(WebSocketBasicEvents.OnAudioBlockRequest, audioBlock); participant = (_f = this.communicationHandler) === null || _f === void 0 ? void 0 : _f.participantByUserId(userId); if (participant) { participant.isAudioBlockedByAdmin = true; } resolve({ message: "Sucess", error: 'NoError', isSuccess: true }); return [2 /*return*/]; } else { resolve({ message: "Permission Not Allowed", error: 'PermissionDenied', isSuccess: false }); } _g.label = 3; case 3: return [2 /*return*/]; } }); }); })]; }); }); }; UserMediaHandler.prototype.resumeCamera = function (userId) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { var participant, videoInputDevices, data, videoUnblock, participant; var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: if (!this.meetingStartRequest) { resolve({ message: "Start Meeting Object not found", error: 'InvalidMeetingRequest', isSuccess: false }); return [2 /*return*/]; } if (!(!userId || this.meetingStartRequest.userId === userId)) return [3 /*break*/, 2]; if (this.isVideoAudioFetchInProgress) { resolve({ message: "Already fetching in progress", error: 'Busy', isSuccess: false }); return [2 /*return*/]; } participant = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfParticipant(); if (!participant || participant.isVideoBlockedByAdmin) { resolve({ message: "Video Blocked By Admin", error: 'BlockedByAdmin', isSuccess: false }); return [2 /*return*/]; } return [4 /*yield*/, this.getDevice(GetDevicesType.VideoIn)]; case 1: videoInputDevices = _d.sent(); if (!videoInputDevices || videoInputDevices.length === 0) { resolve({ message: "No Video In Devices", error: 'NoDevice', isSuccess: false }); return [2 /*return*/]; } this.videoPauseResumePromiseResolver = resolve; this.startLocalStream(true, false, true); return [3 /*break*/, 3]; case 2: if (this.meetingStartRequest.isAdmin) { data = { admin: this.meetingStartRequest.userId, user: userId, type: WebSocketBasicEvents.OnVideoUnblockRequest, to: this.meetingStartRequest.roomId }; videoUnblock = { message: data }; (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.sendWebSocketMessage(WebSocketBasicEvents.OnVideoUnblockRequest, videoUnblock); participant = (_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.participantByUserId(userId); if (participant) { participant.isVideoBlockedByAdmin = false; } resolve({ message: "Sucess", error: 'NoError', isSuccess: true }); return [2 /*return*/]; } else { resolve({ message: "Permission Not Allowed", error: 'PermissionDenied', isSuccess: false }); } _d.label = 3; case 3: return [2 /*return*/]; } }); }); })]; }); }); }; UserMediaHandler.prototype.unmute = function (userId) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { var participant, audioInputDevices, data, audioUnblock, participant; var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: if (!this.meetingStartRequest) { resolve({ message: "Start Meeting Object not found", error: 'InvalidMeetingRequest', isSuccess: false }); return [2 /*return*/]; } if (!(!userId || this.meetingStartRequest.userId === userId)) return [3 /*break*/, 2]; if (this.isVideoAudioFetchInProgress) { resolve({ message: "Already fetching in progress", error: 'Busy', isSuccess: false }); return [2 /*return*/]; } participant = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfParticipant(); if (!participant || participant.isAudioBlockedByAdmin) { resolve({ message: "Audio Blocked By Admin", error: 'BlockedByAdmin', isSuccess: false }); return [2 /*return*/]; } return [4 /*yield*/, this.getDevice(GetDevicesType.AudioIn)]; case 1: audioInputDevices = _d.sent(); if (!audioInputDevices || audioInputDevices.length === 0) { resolve({ message: "No Audio In Devices", error: 'NoDevice', isSuccess: false }); return [2 /*return*/]; } this.muteUmutePromiseResolver = resolve; this.startLocalStream(false, true, true); return [3 /*break*/, 3]; case 2: if (this.meetingStartRequest.isAdmin) { data = { admin: this.meetingStartRequest.userId, user: userId, type: WebSocketBasicEvents.OnAudioUnblockRequest, to: this.meetingStartRequest.roomId }; audioUnblock = { message: data }; (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.sendWebSocketMessage(WebSocketBasicEvents.OnAudioUnblockRequest, audioUnblock); participant = (_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.participantByUserId(userId); if (participant) { participant.isAudioBlockedByAdmin = false; } resolve({ message: "Sucess", error: 'NoError', isSuccess: true }); return [2 /*return*/]; } else { resolve({ message: "Permission Not Allowed", error: 'PermissionDenied', isSuccess: false }); } _d.label = 3; case 3: return [2 /*return*/]; } }); }); })]; }); }); }; UserMediaHandler.prototype.pauseStreamWithoutStopping = function (streamKind) { var _a, _b, _c; var track = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(streamKind); if (track && track.track) { track.track.enabled = false; if (streamKind === TrackKind.Audio) { var data = { userId: (_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.userId, type: "audio", status: "pause" }; var audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume }; this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause); } else { var data = { userId: (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.userId, type: "video", status: "pause" }; var audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume }; this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause); } return { message: "Success", error: 'NoError', isSuccess: true }; } else { return ({ message: "No Track Found", error: 'NoDevice', isSuccess: false }); } }; UserMediaHandler.prototype.resumeStreamWithoutAdding = function (streamKind) { var _a, _b, _c; var track = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfTrackByType(streamKind); if (track && track.track) { track.track.enabled = true; if (streamKind === TrackKind.Audio) { var data = { userId: (_b = this.meetingStartRequest) === null || _b === void 0 ? void 0 : _b.userId, type: "audio", status: "resume" }; var audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume }; this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause); } else { var data = { userId: (_c = this.meetingStartRequest) === null || _c === void 0 ? void 0 : _c.userId, type: "video", status: "resume" }; var audioPause = { message: data, type: WebSocketBasicEvents.AudioVideoPauseResume }; this.communicationHandler.sendWebSocketMessage(WebSocketBasicEvents.AudioVideoPauseResume, audioPause); } return { message: "Success", error: 'NoError', isSuccess: true }; } else { return ({ message: "No Track Found", error: 'NoDevice', isSuccess: false }); } }; UserMediaHandler.prototype.cleanup = function () { this.tracks.forEach(function (eachTrack) { try { eachTrack.stop(); } catch (err) { } }); _super.prototype.cleanup.call(this); }; return UserMediaHandler; }(Ba