UNPKG

vani-meeting-client

Version:
787 lines (786 loc) 75.9 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 __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { Track, TrackKind } from "../model/Track"; import { WebrtcMessageType, } from "../websocket-handler/WebsocketHandler"; import { BaseVideoCallHandler } from "./BaseVideoCallHandler"; import { LogLevel } from "../model/MeetingStartRequest"; import { DynamicLibHelper } from "../utility/DynamicLibHelper"; import { MediaAdaptationManager } from "./MediaAdaptationManager"; import { VaniEvent } from "../model/Event"; var WebrtcHandler = /** @class */ (function (_super) { __extends(WebrtcHandler, _super); function WebrtcHandler() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.dynamicLibHelper = new DynamicLibHelper(); _this.rtcPeerConnections = []; _this.adaptationManager = null; _this.addTransceiverifRequired = function (peerConnection) { return __awaiter(_this, void 0, void 0, function () { var kinds, kinds_1, kinds_1_1, eachKind; var e_1, _a; return __generator(this, function (_b) { kinds = ["audio", "video"]; try { for (kinds_1 = __values(kinds), kinds_1_1 = kinds_1.next(); !kinds_1_1.done; kinds_1_1 = kinds_1.next()) { eachKind = kinds_1_1.value; this.addTransceiverToEachTrackKind(eachKind, peerConnection); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (kinds_1_1 && !kinds_1_1.done && (_a = kinds_1.return)) _a.call(kinds_1); } finally { if (e_1) throw e_1.error; } } this.printLogIfRequired("no of transiver", peerConnection.rtcPeerConnection .getTransceivers()); if (peerConnection.rtcPeerConnection .getTransceivers().length === 0) { peerConnection.rtcPeerConnection.createDataChannel("test"); } return [2 /*return*/]; }); }); }; _this.addTransceiverToEachTrackKind = function (eachKind, peerConnection, shouldReplaceIfExist) { if (shouldReplaceIfExist === void 0) { shouldReplaceIfExist = false; } return __awaiter(_this, void 0, void 0, function () { var transceiver, realTrack, newTransceiver, realTrack; var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: transceiver = peerConnection.rtcPeerConnection .getTransceivers() .find(function (t) { var _a, _b; return ((_b = (_a = t === null || t === void 0 ? void 0 : t.sender) === null || _a === void 0 ? void 0 : _a.track) === null || _b === void 0 ? void 0 : _b.kind) === eachKind; }); if (!!transceiver) return [3 /*break*/, 1]; this.printLogIfRequired("addTransceiverifRequired", eachKind); realTrack = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getAllSelfTracks().find(function (t) { var _a; return ((_a = t.track) === null || _a === void 0 ? void 0 : _a.kind) === eachKind; }); newTransceiver = void 0; if (realTrack && realTrack.track) { this.printLogIfRequired("addTransceiverifRequired track real", eachKind, realTrack.track); // newTransceiver = await peerConnection.rtcPeerConnection.addTrack(realTrack.track) if (realTrack.track.kind === "audio") { newTransceiver = peerConnection.rtcPeerConnection.addTransceiver(realTrack.track, { direction: "sendonly", sendEncodings: [ { maxBitrate: 32000, priority: "high", networkPriority: "high", }, ], }); } else if (realTrack.track.kind === "video") { newTransceiver = peerConnection.rtcPeerConnection.addTransceiver(realTrack.track, { direction: "sendonly", }); } // newTransceiver = await peerConnection.rtcPeerConnection.addTransceiver(realTrack.track, { direction: "sendrecv" }) this.printLogIfRequired("addTransceiverifRequired track real end", eachKind, realTrack.track); } else { // this.printLogIfRequired("addTransceiverifRequired track fake",eachKind) // newTransceiver = await peerConnection.rtcPeerConnection.addTransceiver(eachKind, { direction: "sendrecv" }) } if (eachKind === "audio") { transceiver = peerConnection.rtcPeerConnection .getTransceivers() .find(function (t) { var _a, _b; return ((_b = (_a = t === null || t === void 0 ? void 0 : t.sender) === null || _a === void 0 ? void 0 : _a.track) === null || _b === void 0 ? void 0 : _b.kind) === eachKind; }); if (transceiver) { this.applyAudioPriority(transceiver); } // this.setOpusParams(newTransceiver); } return [3 /*break*/, 3]; case 1: if (!shouldReplaceIfExist) return [3 /*break*/, 3]; realTrack = (_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.getAllSelfTracks().find(function (t) { var _a; return ((_a = t.track) === null || _a === void 0 ? void 0 : _a.kind) === eachKind; }); if (!(realTrack && realTrack.track)) return [3 /*break*/, 3]; this.printLogIfRequired("transceiver.sender.", transceiver.sender, eachKind); return [4 /*yield*/, transceiver.sender.replaceTrack(realTrack.track)]; case 2: _c.sent(); if (eachKind === "audio") { this.applyAudioPriority(transceiver); // this.setOpusParams(newTransceiver); } _c.label = 3; case 3: return [2 /*return*/]; } }); }); }; _this.checkIfCanSendOffer = function (participant, negotiationId, shouldRestartIce, shouldForceFullyCreateOffer) { if (shouldRestartIce === void 0) { shouldRestartIce = false; } if (shouldForceFullyCreateOffer === void 0) { shouldForceFullyCreateOffer = false; } return __awaiter(_this, void 0, void 0, function () { var selfParticpant, isOfferInitParticipant, peerConnectionObject, peerConnection, offer, mungedOffer; var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: selfParticpant = this.communicationHandler.getSelfParticipant(); this.printLogIfRequired("checkIfCanSendOffer", selfParticpant, (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.isStartAndSetupWithServerCalled); this.printLogIfRequired("checkIfCanSendOffer REMOTE", participant); if (!selfParticpant.isStartMeetingCalled && !((_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.isStartAndSetupWithServerCalled)) { return [2 /*return*/]; } if (!(selfParticpant.userId !== participant.userId && (negotiationId || !selfParticpant.getPeerConnections().has(participant.userId)))) return [3 /*break*/, 5]; isOfferInitParticipant = this.isOfferInitParticipant(participant); this.printLogIfRequired("Check if Can Send Offer", isOfferInitParticipant, shouldForceFullyCreateOffer); if (!(!isOfferInitParticipant || shouldForceFullyCreateOffer)) return [3 /*break*/, 5]; peerConnectionObject = selfParticpant.getPeerConnectionsViaUserId(participant.userId, this.meetingStartRequest); peerConnection = peerConnectionObject.rtcPeerConnection ? peerConnectionObject.rtcPeerConnection : this.dynamicLibHelper.getRTCPeerConnection(this.meetingStartRequest); if (!peerConnection) return [3 /*break*/, 5]; this.rtcPeerConnections.push(peerConnection); peerConnectionObject.isCreatingOffer = true; if (negotiationId && peerConnectionObject.opertionalNegotiationId && peerConnectionObject.opertionalNegotiationId === negotiationId) { return [2 /*return*/]; } peerConnectionObject.opertionalNegotiationId = negotiationId; peerConnectionObject.rtcPeerConnection = peerConnection; return [4 /*yield*/, this.subToConnectionState(participant)]; case 1: _c.sent(); return [4 /*yield*/, this.addTransceiverifRequired(peerConnectionObject)]; case 2: _c.sent(); this.printLogIfRequired("addTransceiverifRequired done ", selfParticpant, participant); // await this.startSendTrackToPartiipant(participant) if (shouldRestartIce) { peerConnection.restartIce(); } return [4 /*yield*/, peerConnection.createOffer({ iceRestart: shouldRestartIce, })]; case 3: offer = _c.sent(); mungedOffer = { type: offer.type, sdp: this.setOpusSdpParams(offer.sdp), }; return [4 /*yield*/, peerConnection.setLocalDescription(mungedOffer)]; case 4: _c.sent(); this.printLogIfRequired("On Offer setLocalDescription", mungedOffer); peerConnectionObject.offer = mungedOffer; // send offer to peer peerConnectionObject.remoteAnswer = undefined; this.loopSendOffer(peerConnectionObject, participant, mungedOffer); this.printLogIfRequired("CheckIfCanSendOffer new", peerConnectionObject, __spreadArray([], __read(selfParticpant.getPeerConnections().entries()), false)); _c.label = 5; case 5: return [2 /*return*/]; } }); }); }; _this.loopSendOffer = function (peerConnectionObject, participant, mungedOffer, count) { if (count === void 0) { count = 0; } return __awaiter(_this, void 0, void 0, function () { var selfParticpant, messageJson; var _this = this; var _a; return __generator(this, function (_b) { selfParticpant = (_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfParticipant(); if (!selfParticpant) { return [2 /*return*/]; } if (!selfParticpant.isPeerConnectionsViaUserIdPresent(participant.userId)) { this.printLogIfRequired("loopSendOffer", "Not found", selfParticpant, participant); return [2 /*return*/]; } this.printLogIfRequired("Send Offer loop", mungedOffer, participant); messageJson = { to: participant.userId, type: WebrtcMessageType.SendOffer, data: mungedOffer, }; this.sendWebrtcMessage(messageJson); if (peerConnectionObject.remoteAnswer || count > 20) { return [2 /*return*/]; } if (peerConnectionObject.rtcPeerConnection && peerConnectionObject.rtcPeerConnection.connectionState === "closed") { return [2 /*return*/]; } setTimeout(function () { _this.printLogIfRequired("loopSendOffer "); _this.loopSendOffer(peerConnectionObject, participant, mungedOffer, count + 1); }, 3000); return [2 /*return*/]; }); }); }; _this.onNewOffer = function (data) { return __awaiter(_this, void 0, void 0, function () { var selfParticpant, participant, peerConnectionObject, isOfferInitParticipant, peerConnection, answer, mungedAnswer, messageJson; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: this.printLogIfRequired("On New Offer", data); selfParticpant = this.communicationHandler.getSelfParticipant(); participant = data.sender; peerConnectionObject = selfParticpant.getPeerConnectionsViaUserId(participant.userId, this.meetingStartRequest); if (peerConnectionObject.isCreatingOffer) { isOfferInitParticipant = this.isOfferInitParticipant(participant); if (isOfferInitParticipant) { return [2 /*return*/]; } } if (peerConnectionObject) { peerConnectionObject.remoteOffer = data.sdp; } peerConnection = peerConnectionObject.rtcPeerConnection ? peerConnectionObject.rtcPeerConnection : this.dynamicLibHelper.getRTCPeerConnection(this.meetingStartRequest); if (!peerConnection) return [3 /*break*/, 6]; this.rtcPeerConnections.push(peerConnection); peerConnectionObject.rtcPeerConnection = peerConnection; if (peerConnectionObject.opertionalNegotiationId && peerConnectionObject.negotiationId && peerConnectionObject.opertionalNegotiationId === peerConnectionObject.negotiationId) { peerConnectionObject.negotiationId = undefined; peerConnectionObject.isNegotiationNeeded = false; } return [4 /*yield*/, this.subToConnectionState(participant)]; case 1: _b.sent(); return [4 /*yield*/, peerConnection.setRemoteDescription({ type: "offer", sdp: peerConnectionObject.remoteOffer, })]; case 2: _b.sent(); this.printLogIfRequired("On New Offer setRemoteDescription", { type: "offer", sdp: peerConnectionObject.remoteOffer, }); this.printLogIfRequired("REMOTE SDP", (_a = peerConnection.remoteDescription) === null || _a === void 0 ? void 0 : _a.sdp); return [4 /*yield*/, this.addTransceiverifRequired(peerConnectionObject)]; case 3: _b.sent(); this.printLogIfRequired("addTransceiverifRequired done onNewOffer", selfParticpant, participant); return [4 /*yield*/, peerConnection.createAnswer()]; case 4: answer = _b.sent(); mungedAnswer = { type: answer.type, sdp: this.setOpusSdpParams(answer.sdp), }; return [4 /*yield*/, peerConnection.setLocalDescription(mungedAnswer)]; case 5: _b.sent(); this.printLogIfRequired("On New Offer setLocalDescription", mungedAnswer); peerConnectionObject.answer = mungedAnswer; console.log("answer", mungedAnswer); messageJson = { to: participant.userId, type: WebrtcMessageType.SendAnswer, data: mungedAnswer, }; this.sendWebrtcMessage(messageJson); peerConnectionObject.opertionalNegotiationId = undefined; _b.label = 6; case 6: return [2 /*return*/]; } }); }); }; _this.onNewAnswer = function (data) { return __awaiter(_this, void 0, void 0, function () { var selfParticpant, participant, peerConnectionObject, peerConnection; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: this.printLogIfRequired("On New Answer", data); selfParticpant = this.communicationHandler.getSelfParticipant(); participant = data.sender; peerConnectionObject = selfParticpant.getPeerConnectionsViaUserId(participant.userId, this.meetingStartRequest); this.printLogIfRequired("On New Answer peerConnectionObject", peerConnectionObject); if (peerConnectionObject.opertionalNegotiationId && peerConnectionObject.negotiationId && peerConnectionObject.opertionalNegotiationId === peerConnectionObject.negotiationId) { peerConnectionObject.negotiationId = undefined; peerConnectionObject.opertionalNegotiationId = undefined; peerConnectionObject.isNegotiationNeeded = false; } peerConnectionObject.opertionalNegotiationId = undefined; if (peerConnectionObject) { peerConnectionObject.remoteAnswer = data.sdp; } peerConnection = peerConnectionObject.rtcPeerConnection ? peerConnectionObject.rtcPeerConnection : this.dynamicLibHelper.getRTCPeerConnection(this.meetingStartRequest); if (!peerConnection) return [3 /*break*/, 3]; if (!(peerConnection.signalingState === "have-local-offer")) return [3 /*break*/, 2]; return [4 /*yield*/, peerConnection.setRemoteDescription({ type: "answer", sdp: peerConnectionObject.remoteAnswer, })]; case 1: _b.sent(); _b.label = 2; case 2: peerConnectionObject.isRtcRestartIceInProgess = false; this.printLogIfRequired("REMOTE SDP", (_a = peerConnection.remoteDescription) === null || _a === void 0 ? void 0 : _a.sdp); this.printLogIfRequired("On New Answer setRemoteDescription", { type: "answer", sdp: peerConnectionObject.remoteAnswer, }); _b.label = 3; case 3: return [2 /*return*/]; } }); }); }; _this.onRemoteNegotiationNeeded = function (data) { return __awaiter(_this, void 0, void 0, function () { var selfParticpant, peerConnectionObject; return __generator(this, function (_a) { switch (_a.label) { case 0: selfParticpant = this.communicationHandler.getSelfParticipant(); peerConnectionObject = selfParticpant.getPeerConnectionsViaUserId(data.sender.userId, this.meetingStartRequest); this.printLogIfRequired("onRemoteNegotiationNeeded", peerConnectionObject, peerConnectionObject.opertionalNegotiationId); if (!peerConnectionObject || peerConnectionObject.opertionalNegotiationId) { this.printLogIfRequired("onRemoteNegotiationNeeded exit", peerConnectionObject, peerConnectionObject.opertionalNegotiationId); return [2 /*return*/]; } peerConnectionObject.negotiationId = data.negotiationId; return [4 /*yield*/, this.checkIfCanSendOffer(data.sender, data.negotiationId, data.shouldRestartIce, true)]; case 1: _a.sent(); peerConnectionObject.opertionalNegotiationId = data.negotiationId; return [2 /*return*/]; } }); }); }; _this.onIceCandidate = function (data) { return __awaiter(_this, void 0, void 0, function () { var selfParticpant, participant, peerConnectionObject, rtcConnection, rtcCandidate; return __generator(this, function (_a) { switch (_a.label) { case 0: selfParticpant = this.communicationHandler.getSelfParticipant(); participant = data.sender; peerConnectionObject = selfParticpant.getPeerConnectionsViaUserId(participant.userId, this.meetingStartRequest); rtcConnection = peerConnectionObject.rtcPeerConnection; if (!rtcConnection) return [3 /*break*/, 2]; this.printLogIfRequired("on ice candidate", data.candidate); rtcCandidate = new RTCIceCandidate({ candidate: data.candidate.candidate, sdpMid: data.candidate.sdpMid, sdpMLineIndex: data.candidate.sdpMLineIndex, // don't make it up, you get this in onicecandidate }); return [4 /*yield*/, rtcConnection.addIceCandidate(rtcCandidate)]; case 1: _a.sent(); _a.label = 2; case 2: return [2 /*return*/]; } }); }); }; // onNewTrack = async (remoteTrack:any,remoteParticipant:Participant) =>{ // this.printLogIfRequired("New Remote before checking Track",remoteTrack) // let remoteTrackKind = remoteTrack.kind; // if(remoteTrack.kind.toLowerCase() === TrackKind.Audio.toLowerCase()){ // remoteTrackKind = TrackKind.Audio // } // else if(remoteTrack.kind.toLowerCase() === TrackKind.Video.toLowerCase()){ // remoteTrackKind = TrackKind.Video // } // if (remoteTrack.muted) { // remoteTrack.onunmute = () => { // const track = new Track(remoteParticipant, false,remoteTrackKind, remoteTrack) // this.communicationHandler?.addUpdateRemoteTrack(track, remoteParticipant, true) // this.printLogIfRequired("New Remote Track",track) // }; // } // else{ // const track = new Track(remoteParticipant, false,remoteTrackKind, remoteTrack) // this.communicationHandler?.addUpdateRemoteTrack(track, remoteParticipant, true) // this.printLogIfRequired("New Remote Track",track) // } // remoteTrack.onended = () => { // const track = this.communicationHandler?.getAllTracks().find((eachTrack) => // eachTrack.track && eachTrack.track.kind === remoteTrack.kind && // eachTrack.participant?.userId === remoteParticipant.userId // ) // if(track){ // this.communicationHandler?.removeTrack(track) // this.printLogIfRequired(" Remote Track ended ",remoteTrack) // } // }; // } _this.onRTCConnectionChanged = function (remoteParticipant, count) { if (count === void 0) { count = 0; } return __awaiter(_this, void 0, void 0, function () { var selfParticpant, peerConnectionObject, rtcConnection; var _this = this; var _a; return __generator(this, function (_b) { selfParticpant = this.communicationHandler.getSelfParticipant(); peerConnectionObject = selfParticpant.getPeerConnectionsViaUserId(remoteParticipant.userId, this.meetingStartRequest); if (!peerConnectionObject || !remoteParticipant) { return [2 /*return*/]; } rtcConnection = peerConnectionObject.rtcPeerConnection; if (!rtcConnection || peerConnectionObject.isRtcRestartIceInProgess || count > 12 || (rtcConnection.connectionState !== "failed" && rtcConnection.connectionState !== "disconnected")) { return [2 /*return*/]; } if ((_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.isWebScoketConnected()) { rtcConnection.restartIce(); peerConnectionObject.isRtcRestartIceInProgess = true; peerConnectionObject.isNegotiationNeeded = true; peerConnectionObject.negotiationId = new Date().getTime() + "_" + (Math.floor(Math.random() * 100) + 1); this.onNegotiationNeeded(remoteParticipant, true); } else { setTimeout(function () { _this.onRTCConnectionChanged(remoteParticipant, count + 1); }, 4000); } return [2 /*return*/]; }); }); }; _this.subToConnectionState = function (remoteParticipant) { return __awaiter(_this, void 0, void 0, function () { var selfParticpant, peerConnectionObject, rtcConnection; var _this = this; return __generator(this, function (_a) { selfParticpant = this.communicationHandler.getSelfParticipant(); peerConnectionObject = selfParticpant.getPeerConnectionsViaUserId(remoteParticipant.userId, this.meetingStartRequest); if (peerConnectionObject && peerConnectionObject.isSubDone) { return [2 /*return*/]; } peerConnectionObject.isSubDone = true; rtcConnection = peerConnectionObject.rtcPeerConnection; rtcConnection.addEventListener("icecandidate", function (event) { if (event.candidate && event.candidate !== null) { _this.printLogIfRequired("icecandidate", event.candidate, remoteParticipant); var sdpMid = event.candidate.sdpMid; var sdpMLineIndex = event.candidate.sdpMLineIndex; var messageJson = { to: remoteParticipant.userId, type: WebrtcMessageType.IceCandidate, data: { sdpMid: sdpMid, sdpMLineIndex: sdpMLineIndex, candidate: event.candidate, }, }; _this.sendWebrtcMessage(messageJson); } }); rtcConnection.addEventListener("iceconnectionstatechange", function (event) { _this.printLogIfRequired("iceconnectionstatechange", rtcConnection.iceConnectionState, event, remoteParticipant); }); rtcConnection.addEventListener("connectionstatechange", function (event) { var _a, _b, _c, _d, _e, _f, _g, _h; _this.printLogIfRequired("connectionstatechange", peerConnectionObject.userId, rtcConnection.connectionState); if (rtcConnection.connectionState === "connected") { var participant = (_a = _this.communicationHandler) === null || _a === void 0 ? void 0 : _a.participantByUserId(peerConnectionObject.userId); _this.printLogIfRequired("connected", participant, peerConnectionObject.userId); if (participant) { (_b = _this.communicationHandler) === null || _b === void 0 ? void 0 : _b.emitMessageToSource(VaniEvent.WebrtcReconected, participant); } (_c = _this.adaptationManager) === null || _c === void 0 ? void 0 : _c.applyToNewConnection(rtcConnection); (_d = _this.adaptationManager) === null || _d === void 0 ? void 0 : _d.start(); _this.printLogIfRequired("On Connection Change", event, remoteParticipant); _this.printLogIfRequired("mobile track start"); rtcConnection.getTransceivers().forEach(function (transceiver) { var _a; if ((_a = transceiver.receiver) === null || _a === void 0 ? void 0 : _a.track) { _this.onNewTrack(transceiver.receiver.track, transceiver.receiver, transceiver, remoteParticipant); } }); // rtcConnection.getReceivers().forEach((eachReciver)=>{ // if(eachReciver && eachReciver.track){ // const streamTrack = eachReciver.track // this.onNewTrack(streamTrack,remoteParticipant) // } // }) } else if (rtcConnection.connectionState === "disconnected") { var participant = (_e = _this.communicationHandler) === null || _e === void 0 ? void 0 : _e.participantByUserId(peerConnectionObject.userId); _this.printLogIfRequired("disconnected", participant, peerConnectionObject.userId); if (participant) { (_f = _this.communicationHandler) === null || _f === void 0 ? void 0 : _f.emitMessageToSource(VaniEvent.WebrtcReconecting, participant); } setTimeout(function () { _this.onRTCConnectionChanged(remoteParticipant); }, 4000); } else if (rtcConnection.connectionState === "failed") { var participant = (_g = _this.communicationHandler) === null || _g === void 0 ? void 0 : _g.participantByUserId(peerConnectionObject.userId); _this.printLogIfRequired("failed", participant, peerConnectionObject.userId); if (participant) { (_h = _this.communicationHandler) === null || _h === void 0 ? void 0 : _h.emitMessageToSource(VaniEvent.WebrtcReconecting, participant); } _this.onRTCConnectionChanged(remoteParticipant); } }); rtcConnection.addEventListener("signalingstatechange", function (event) { var _a; _this.printLogIfRequired("signalingstatechange", event, rtcConnection.signalingState); var selfParticpant = (_a = _this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfParticipant(); var innerPeerConnectionObject = selfParticpant === null || selfParticpant === void 0 ? void 0 : selfParticpant.isPeerConnectionsViaUserIdPresent(remoteParticipant.userId); if (innerPeerConnectionObject && rtcConnection.signalingState === "stable") { innerPeerConnectionObject.isCreatingOffer = false; if (innerPeerConnectionObject.isNegotiationNeeded) { _this.onNegotiationNeeded(remoteParticipant, false, 0, true); } } }); // rtcConnection.ontrack = ((event)=>{ // this.printLogIfRequired("New Remote Track ontrack",event) // if(event.track){ // const streamTrack = event.track // const track = new Track(remoteParticipant, false,streamTrack.kind, streamTrack) // this.communicationHandler?.addUpdateRemoteTrack(track, remoteParticipant, true) // this.printLogIfRequired("New Remote Track",track) // } // }) rtcConnection.addEventListener("negotiationneeded", function (event) { var _a, _b; _this.printLogIfRequired("negotiationneeded", rtcConnection.signalingState, remoteParticipant, peerConnectionObject); var selfParticpant = (_a = _this.communicationHandler) === null || _a === void 0 ? void 0 : _a.getSelfParticipant(); var innerPeerConnectionObject = selfParticpant === null || selfParticpant === void 0 ? void 0 : selfParticpant.isPeerConnectionsViaUserIdPresent(remoteParticipant.userId); var remoteUser = (_b = _this.communicationHandler) === null || _b === void 0 ? void 0 : _b.participantByUserId(remoteParticipant.userId); if (!remoteUser && (!innerPeerConnectionObject || (!innerPeerConnectionObject.offer && !innerPeerConnectionObject.answer))) { _this.printLogIfRequired("negotiationneeded ignored", rtcConnection.signalingState, remoteParticipant, innerPeerConnectionObject, __spreadArray([], __read(selfParticpant.getPeerConnections().entries()), false)); return; } innerPeerConnectionObject.isNegotiationNeeded = true; innerPeerConnectionObject.negotiationId = new Date().getTime() + "_" + (Math.floor(Math.random() * 100) + 1); if (rtcConnection.signalingState === "stable") { _this.onNegotiationNeeded(remoteParticipant, false, 0, true); } }); rtcConnection.addEventListener("track", function (event) { _this.printLogIfRequired("On New Track Remote", event); if (event.streams && event.streams.length > 0) { var mediaStream = event.streams.at(0); if (mediaStream.getTracks() && mediaStream.getTracks().length > 0) { var streamTrack = mediaStream.getTracks().at(0); _this.onNewTrack(streamTrack, event.receiver, event.transceiver, remoteParticipant); } } else if (event.track) { var streamTrack = event.track; _this.onNewTrack(streamTrack, event.receiver, event.transceiver, remoteParticipant); } }); return [2 /*return*/]; }); }); }; _this.onNegotiationNeeded = function (remoteParticipant, shouldRestartIce, count, shouldForceFullyCreateOffer) { if (shouldRestartIce === void 0) { shouldRestartIce = false; } if (count === void 0) { count = 0; } if (shouldForceFullyCreateOffer === void 0) { shouldForceFullyCreateOffer = false; } return __awaiter(_this, void 0, void 0, function () { var isOfferInitParticipant, selfParticpant, peerConnectionObject, isVideoAdded, isAudioAdded, messageJson; var _this = this; var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: this.printLogIfRequired("onNegotiationNeeded started", remoteParticipant, count); isOfferInitParticipant = this.isOfferInitParticipant(remoteParticipant); selfParticpant = this.communicationHandler.getSelfParticipant(); peerConnectionObject = selfParticpant.getPeerConnectionsViaUserId(remoteParticipant.userId, this.meetingStartRequest); if (peerConnectionObject && shouldRestartIce && peerConnectionObject.rtcPeerConnection && peerConnectionObject.rtcPeerConnection.connectionState === "connected") { this.printLogIfRequired("onNegotiationNeeded return", remoteParticipant, shouldRestartIce, peerConnectionObject); return [2 /*return*/]; } // if(shouldIgnoreAllOldOpetionalNego){ // peerConnectionObject.opertionalNegotiationId = undefined; // } // if(shouldIgnoreAllOldOpetionalNego && peerConnectionObject){ // this.printLogIfRequired("shouldForceFullyCreateOffer ",peerConnectionObject,peerConnectionObject.isNegotiationNeeded,peerConnectionObject.opertionalNegotiationId,count) // } // else if (!peerConnectionObject || !peerConnectionObject.isNegotiationNeeded || peerConnectionObject.opertionalNegotiationId || count > 15) { this.printLogIfRequired("onNegotiationNeeded exit", peerConnectionObject, peerConnectionObject.isNegotiationNeeded, peerConnectionObject.opertionalNegotiationId, count); return [2 /*return*/]; } if (!(!isOfferInitParticipant || shouldForceFullyCreateOffer)) return [3 /*break*/, 4]; if (!((_a = this.communicationHandler) === null || _a === void 0 ? void 0 : _a.isWebScoketConnected())) return [3 /*break*/, 2]; this.printLogIfRequired("ask remote checkIfCanSendOffer "); return [4 /*yield*/, this.checkIfCanSendOffer(remoteParticipant, peerConnectionObject.negotiationId, shouldRestartIce, shouldForceFullyCreateOffer)]; case 1: _d.sent(); peerConnectionObject.isNegotiationNeeded = false; peerConnectionObject.isRtcRestartIceInProgess = false; return [3 /*break*/, 3]; case 2: setTimeout(function () { _this.printLogIfRequired("onNegotiationNeeded setTimeout 525"); _this.onNegotiationNeeded(remoteParticipant, shouldRestartIce, count + 1, shouldForceFullyCreateOffer); }, 5000); _d.label = 3; case 3: return [3 /*break*/, 6]; case 4: isVideoAdded = ((_b = this.communicationHandler) === null || _b === void 0 ? void 0 : _b.getAllSelfTracks().find(function (eachTrack) { return eachTrack.trackKind === TrackKind.Video && eachTrack.track; })) ? true : false; isAudioAdded = ((_c = this.communicationHandler) === null || _c === void 0 ? void 0 : _c.getAllSelfTracks().find(function (eachTrack) { return eachTrack.trackKind === TrackKind.Audio && eachTrack.track; })) ? true : false; messageJson = { to: remoteParticipant.userId, type: WebrtcMessageType.NegotiationNeeded, data: { negotiationId: peerConnectionObject.negotiationId, shouldRestartIce: shouldRestartIce, isVideoAdded: isVideoAdded, isAudioAdded: isAudioAdded, }, }; return [4 /*yield*/, this.sendWebrtcMessage(messageJson)]; case 5: _d.sent(); this.printLogIfRequired("ask remote negogitation ", messageJson); setTimeout(function () { _this.printLogIfRequired("onNegotiationNeeded setTimeout 542"); _this.onNegotiationNeeded(remoteParticipant, shouldRestartIce, count + 1, shouldForceFullyCreateOffer); }, 5000); _d.label = 6; case 6: return [2 /*return*/]; } }); }); }; _this.startSendTrackToPartiipant = function (participant) { return __awaiter(_this, void 0, void 0, function () { var _a, _b, eachTrack, e_2_1; var e_2, _c; var _d; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 5, 6, 7]); _a = __values((_d = this.communicationHandler) === null || _d === void 0 ? void 0 : _d.getAllSelfTracks()), _b = _a.next(); _e.label = 1; case 1: