@microsoft/omnichannel-chat-sdk
Version:
Microsoft Omnichannel Chat SDK
476 lines • 27.6 kB
JavaScript
"use strict";
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["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 __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));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VoiceVideoCallingProxy = void 0;
var TelemetryEvent_1 = require("../telemetry/TelemetryEvent");
var SecondaryChannelType;
(function (SecondaryChannelType) {
SecondaryChannelType[SecondaryChannelType["Voice"] = 192370000] = "Voice";
SecondaryChannelType[SecondaryChannelType["Video"] = 192380000] = "Video";
SecondaryChannelType[SecondaryChannelType["Cobrowse"] = 192390000] = "Cobrowse";
})(SecondaryChannelType || (SecondaryChannelType = {}));
var SecondaryChannelEvents;
(function (SecondaryChannelEvents) {
SecondaryChannelEvents["Accept"] = "accept";
SecondaryChannelEvents["Reject"] = "reject";
SecondaryChannelEvents["Connect"] = "connect";
SecondaryChannelEvents["End"] = "end";
})(SecondaryChannelEvents || (SecondaryChannelEvents = {}));
var CallingEvents;
(function (CallingEvents) {
CallingEvents["CallAdded"] = "callAdded";
CallingEvents["LocalVideoStreamAdded"] = "localVideoStreamAdded";
CallingEvents["LocalVideoStreamRemoved"] = "localVideoStreamRemoved";
CallingEvents["RemoteVideoStreamAdded"] = "remoteVideoStreamAdded";
CallingEvents["RemoteVideoStreamRemoved"] = "remoteVideoStreamRemoved";
CallingEvents["CallDisconnected"] = "callDisconnected";
CallingEvents["ParticipantDisconnected"] = "participantDisconnected";
CallingEvents["IncomingCallEnded"] = "incomingCallEnded";
})(CallingEvents || (CallingEvents = {}));
var VoiceVideoCallingProxy = /** @class */ (function () {
function VoiceVideoCallingProxy() {
this.debug = false;
this.callClientName = 'ElevateToVoiceVideo';
this.proxy = window["Microsoft.Omnichannel.Calling.SDK"].VoiceVideoCalling; // eslint-disable-line @typescript-eslint/no-explicit-any
this.proxyInstance = this.proxy.getInstance();
}
VoiceVideoCallingProxy.getInstance = function () {
if (!this._instance) {
this._instance = new VoiceVideoCallingProxy();
}
return this._instance;
};
/* istanbul ignore next */
VoiceVideoCallingProxy.prototype.setDebug = function (flag) {
this.debug = flag;
};
VoiceVideoCallingProxy.prototype.useScenarioMarker = function (scenarioMarker) {
this.scenarioMarker = scenarioMarker;
};
VoiceVideoCallingProxy.prototype.load = function () {
return __awaiter(this, arguments, void 0, function (params) {
if (params === void 0) { params = {}; }
return __generator(this, function (_a) {
if (params.logger) {
this.logger = params.logger;
}
this.proxyInstance.load(params);
return [2 /*return*/];
});
});
};
VoiceVideoCallingProxy.prototype.isInitialized = function () {
return this.proxyInstance.isInitialized();
};
VoiceVideoCallingProxy.prototype.initialize = function (params) {
return __awaiter(this, void 0, void 0, function () {
var error_1;
var _a, _b, _c, _d, _e, _f, _g, _h;
return __generator(this, function (_j) {
switch (_j.label) {
case 0:
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy] VoiceVideoCallingParams: ".concat(JSON.stringify(params)));
this.callingParams = params;
(_a = this.scenarioMarker) === null || _a === void 0 ? void 0 : _a.startScenario(TelemetryEvent_1.default.InitializeVoiceVideoCallingSDK);
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][initialize] _isLoaded: ".concat(this.proxyInstance._isLoaded));
if (!!this.proxyInstance._isLoaded) return [3 /*break*/, 2];
return [4 /*yield*/, this.proxyInstance.load({
logger: this.logger || undefined
})];
case 1:
_j.sent();
_j.label = 2;
case 2:
_j.trys.push([2, 4, , 5]);
return [4 /*yield*/, this.proxyInstance.initialize({
callClientName: this.callClientName,
accesstoken: ((_c = (_b = this.callingParams) === null || _b === void 0 ? void 0 : _b.chatToken.voiceVideoCallToken) === null || _c === void 0 ? void 0 : _c.Token) || ((_d = this.callingParams) === null || _d === void 0 ? void 0 : _d.chatToken.token),
selfVideoHTMLElementId: (_e = this.callingParams) === null || _e === void 0 ? void 0 : _e.selfVideoHTMLElementId,
remoteVideoHTMLElementId: (_f = this.callingParams) === null || _f === void 0 ? void 0 : _f.remoteVideoHTMLElementId
})];
case 3:
_j.sent();
(_g = this.scenarioMarker) === null || _g === void 0 ? void 0 : _g.completeScenario(TelemetryEvent_1.default.InitializeVoiceVideoCallingSDK);
return [3 /*break*/, 5];
case 4:
error_1 = _j.sent();
console.log(error_1);
(_h = this.scenarioMarker) === null || _h === void 0 ? void 0 : _h.failScenario(TelemetryEvent_1.default.InitializeVoiceVideoCallingSDK);
return [3 /*break*/, 5];
case 5: return [2 /*return*/];
}
});
});
};
VoiceVideoCallingProxy.prototype.addEventListener = function (eventName, callback) {
var _this = this;
this.proxyInstance.registerEvent(eventName, function (params) {
var callId = params.callId;
if (eventName === CallingEvents.CallAdded) {
_this.callId = callId;
}
/* istanbul ignore next */
_this.debug && console.debug("[VoiceVideoCallingProxy][".concat(eventName, "] callId: ").concat(callId));
if (callId !== _this.callId) {
return;
}
callback(params);
});
};
VoiceVideoCallingProxy.prototype.isMicrophoneMuted = function () {
var _a = this, callClientName = _a.callClientName, callId = _a.callId;
return this.proxyInstance.isMicrophoneMuted({ callClientName: callClientName, callId: callId });
};
VoiceVideoCallingProxy.prototype.acceptCall = function () {
return __awaiter(this, arguments, void 0, function (params) {
var _a, callClientName, callId, _b, exceptionDetails, body, exceptionDetails;
var _c, _d, _e, _f, _g, _h;
if (params === void 0) { params = {}; }
return __generator(this, function (_j) {
switch (_j.label) {
case 0:
_a = this, callClientName = _a.callClientName, callId = _a.callId;
(_c = this.scenarioMarker) === null || _c === void 0 ? void 0 : _c.startScenario(params.withVideo ? TelemetryEvent_1.default.AcceptVideoCall : TelemetryEvent_1.default.AcceptVoiceCall, {
CallId: callId || ''
});
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][acceptCall] callId: ".concat(callId));
/* istanbul ignore next */
this.debug && console.debug(params);
_j.label = 1;
case 1:
_j.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.proxyInstance.acceptCall({ callClientName: callClientName, callId: callId, withVideo: params.withVideo || false })];
case 2:
_j.sent();
return [3 /*break*/, 4];
case 3:
_b = _j.sent();
exceptionDetails = {
response: params.withVideo ? "AcceptVideoCallFailed" : "AcceptVoiceCallFailed"
};
(_d = this.scenarioMarker) === null || _d === void 0 ? void 0 : _d.failScenario(params.withVideo ? TelemetryEvent_1.default.AcceptVideoCall : TelemetryEvent_1.default.AcceptVoiceCall, {
CallId: callId || '',
ExceptionDetails: JSON.stringify(exceptionDetails)
});
return [3 /*break*/, 4];
case 4:
body = {
SecondaryChannelType: params.withVideo ? SecondaryChannelType.Video : SecondaryChannelType.Voice,
SecondaryChannelEventType: SecondaryChannelEvents.Accept
};
try {
(_e = this.callingParams) === null || _e === void 0 ? void 0 : _e.OCClient.makeSecondaryChannelEventRequest((_f = this.callingParams) === null || _f === void 0 ? void 0 : _f.chatToken.requestId, body);
(_g = this.scenarioMarker) === null || _g === void 0 ? void 0 : _g.completeScenario(params.withVideo ? TelemetryEvent_1.default.AcceptVideoCall : TelemetryEvent_1.default.AcceptVoiceCall, {
CallId: callId || ''
});
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][acceptCall][makeSecondaryChannelEventRequest]");
}
catch (e) {
exceptionDetails = {
response: "OCClientMakeSecondaryChannelEventRequestFailed"
};
(_h = this.scenarioMarker) === null || _h === void 0 ? void 0 : _h.failScenario(params.withVideo ? TelemetryEvent_1.default.AcceptVideoCall : TelemetryEvent_1.default.AcceptVoiceCall, {
CallId: callId || '',
ExceptionDetails: JSON.stringify(exceptionDetails)
});
console.error("[VoiceVideoCallingProxy][acceptCall][makeSecondaryChannelEventRequest] Failure ".concat(e));
}
return [2 /*return*/];
}
});
});
};
VoiceVideoCallingProxy.prototype.rejectCall = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, callClientName, callId, _b, exceptionDetails, body, exceptionDetails;
var _c, _d, _e, _f, _g, _h;
return __generator(this, function (_j) {
switch (_j.label) {
case 0:
_a = this, callClientName = _a.callClientName, callId = _a.callId;
(_c = this.scenarioMarker) === null || _c === void 0 ? void 0 : _c.startScenario(TelemetryEvent_1.default.RejectCall, {
CallId: callId || ''
});
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][rejectCall] callId: ".concat(callId));
_j.label = 1;
case 1:
_j.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.proxyInstance.rejectCall({ callClientName: callClientName, callId: callId })];
case 2:
_j.sent();
(_d = this.scenarioMarker) === null || _d === void 0 ? void 0 : _d.completeScenario(TelemetryEvent_1.default.RejectCall, {
CallId: callId || ''
});
return [3 /*break*/, 4];
case 3:
_b = _j.sent();
exceptionDetails = {
response: "RejectCallFailed"
};
(_e = this.scenarioMarker) === null || _e === void 0 ? void 0 : _e.failScenario(TelemetryEvent_1.default.RejectCall, {
CallId: callId || '',
ExceptionDetails: JSON.stringify(exceptionDetails)
});
return [3 /*break*/, 4];
case 4:
body = {
SecondaryChannelType: this.isRemoteVideoEnabled() ? SecondaryChannelType.Video : SecondaryChannelType.Voice,
SecondaryChannelEventType: SecondaryChannelEvents.Reject
};
try {
(_f = this.callingParams) === null || _f === void 0 ? void 0 : _f.OCClient.makeSecondaryChannelEventRequest((_g = this.callingParams) === null || _g === void 0 ? void 0 : _g.chatToken.requestId, body);
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][rejectCall][makeSecondaryChannelEventRequest]");
}
catch (e) {
exceptionDetails = {
response: "OCClientMakeSecondaryChannelEventRequestFailed"
};
(_h = this.scenarioMarker) === null || _h === void 0 ? void 0 : _h.failScenario(TelemetryEvent_1.default.RejectCall, {
CallId: callId || '',
ExceptionDetails: JSON.stringify(exceptionDetails)
});
console.error("[VoiceVideoCallingProxy][rejectCall][makeSecondaryChannelEventRequest] Failure ".concat(e));
}
return [2 /*return*/];
}
});
});
};
VoiceVideoCallingProxy.prototype.stopCall = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, callClientName, callId, forEveryone, _b;
var _c, _d, _e, _f, _g;
return __generator(this, function (_h) {
switch (_h.label) {
case 0:
_a = this, callClientName = _a.callClientName, callId = _a.callId;
(_c = this.scenarioMarker) === null || _c === void 0 ? void 0 : _c.startScenario(TelemetryEvent_1.default.StopCall, {
CallId: callId || ''
});
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][stopCall] callId: ".concat(callId));
forEveryone = ((_e = (_d = this.callingParams) === null || _d === void 0 ? void 0 : _d.chatToken.voiceVideoCallToken) === null || _e === void 0 ? void 0 : _e.Token) ? true : false;
_h.label = 1;
case 1:
_h.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.proxyInstance.stopCall({ callClientName: callClientName, callId: callId, forEveryone: forEveryone })];
case 2:
_h.sent();
(_f = this.scenarioMarker) === null || _f === void 0 ? void 0 : _f.completeScenario(TelemetryEvent_1.default.StopCall, {
CallId: callId || ''
});
return [3 /*break*/, 4];
case 3:
_b = _h.sent();
(_g = this.scenarioMarker) === null || _g === void 0 ? void 0 : _g.failScenario(TelemetryEvent_1.default.StopCall, {
CallId: callId || '',
});
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
});
};
VoiceVideoCallingProxy.prototype.toggleMute = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, callClientName, callId;
return __generator(this, function (_b) {
_a = this, callClientName = _a.callClientName, callId = _a.callId;
return [2 /*return*/, this.proxyInstance.toggleMute({ callClientName: callClientName, callId: callId })];
});
});
};
VoiceVideoCallingProxy.prototype.isRemoteVideoEnabled = function () {
var _a = this, callClientName = _a.callClientName, callId = _a.callId;
return this.proxyInstance.isRemoteVideoEnabled({ callClientName: callClientName, callId: callId });
};
VoiceVideoCallingProxy.prototype.isLocalVideoEnabled = function () {
var _a = this, callClientName = _a.callClientName, callId = _a.callId;
return this.proxyInstance.isLocalVideoEnabled({ callClientName: callClientName, callId: callId });
};
VoiceVideoCallingProxy.prototype.toggleLocalVideo = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, callClientName, callId;
return __generator(this, function (_b) {
_a = this, callClientName = _a.callClientName, callId = _a.callId;
return [2 /*return*/, this.proxyInstance.toggleLocalVideo({ callClientName: callClientName, callId: callId })];
});
});
};
VoiceVideoCallingProxy.prototype.isInACall = function () {
var _a = this, callClientName = _a.callClientName, callId = _a.callId;
return this.proxyInstance.isInACall({ callClientName: callClientName, callId: callId });
};
VoiceVideoCallingProxy.prototype.renderVideoStreams = function () {
var _a = this, callClientName = _a.callClientName, callId = _a.callId;
return this.proxyInstance.renderVideoStreams({ callClientName: callClientName, callId: callId });
};
VoiceVideoCallingProxy.prototype.disposeVideoRenderers = function () {
var _a = this, callClientName = _a.callClientName, callId = _a.callId;
return this.proxyInstance.disposeVideoRenderers({ callClientName: callClientName, callId: callId });
};
VoiceVideoCallingProxy.prototype.close = function () {
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][close]");
this.proxyInstance.dispose();
this.callingParams = undefined;
this.callId = undefined;
};
VoiceVideoCallingProxy.prototype.onCallAdded = function (callback) {
var eventName = CallingEvents.CallAdded;
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][".concat(eventName, "]"));
this.addEventListener(eventName, callback);
};
VoiceVideoCallingProxy.prototype.onLocalVideoStreamAdded = function (callback) {
var eventName = CallingEvents.LocalVideoStreamAdded;
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][".concat(eventName, "]"));
this.addEventListener(eventName, callback);
};
VoiceVideoCallingProxy.prototype.onLocalVideoStreamRemoved = function (callback) {
var eventName = CallingEvents.LocalVideoStreamRemoved;
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][".concat(eventName, "]"));
this.addEventListener(eventName, callback);
};
VoiceVideoCallingProxy.prototype.onRemoteVideoStreamAdded = function (callback) {
var eventName = CallingEvents.RemoteVideoStreamAdded;
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][".concat(eventName, "]"));
this.addEventListener(eventName, callback);
};
VoiceVideoCallingProxy.prototype.onRemoteVideoStreamRemoved = function (callback) {
var eventName = CallingEvents.RemoteVideoStreamRemoved;
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][".concat(eventName, "]"));
this.addEventListener(eventName, callback);
};
VoiceVideoCallingProxy.prototype.onCallDisconnected = function (callback) {
var _this = this;
var eventName = CallingEvents.CallDisconnected;
/* istanbul ignore next */
this.debug && console.debug("[VoiceVideoCallingProxy][".concat(eventName, "]"));
this.addEventListener(eventName, function (params) { return __awaiter(_this, void 0, void 0, function () {
var body, exceptionDetails;
var _a, _b, _c, _d, _e;
return __generator(this, function (_f) {
body = {
SecondaryChannelType: SecondaryChannelType.Voice,
SecondaryChannelEventType: SecondaryChannelEvents.End
};
(_a = this.scenarioMarker) === null || _a === void 0 ? void 0 : _a.startScenario(TelemetryEvent_1.default.OnCallDisconnected, {
CallId: this.callId || ''
});
try {
(_b = this.callingParams) === null || _b === void 0 ? void 0 : _b.OCClient.makeSecondaryChannelEventRequest((_c = this.callingParams) === null || _c === void 0 ? void 0 : _c.chatToken.requestId, body);
this.debug && console.debug("[VoiceVideoCallingProxy][onCallDisconnected][makeSecondaryChannelEventRequest]");
(_d = this.scenarioMarker) === null || _d === void 0 ? void 0 : _d.completeScenario(TelemetryEvent_1.default.OnCallDisconnected, {
CallId: this.callId || ''
});
}
catch (e) {
console.error("[VoiceVideoCallingProxy][onCallDisconnected][makeSecondaryChannelEventRequest] Failure ".concat(e));
exceptionDetails = {
response: 'OCClientMakeSecondaryChannelEventRequestFailed'
};
(_e = this.scenarioMarker) === null || _e === void 0 ? void 0 : _e.failScenario(TelemetryEvent_1.default.OnCallDisconnected, {
CallId: this.callId || '',
ExceptionDetails: JSON.stringify(exceptionDetails)
});
}
this.clearRemoteVideoElementChildren();
callback(params);
return [2 /*return*/];
});
}); });
};
VoiceVideoCallingProxy.prototype.clearRemoteVideoElementChildren = function () {
var _a, _b;
// Remove remoteVideoHTMLElement children if any or video won't be rendered properly
if ((_a = this.callingParams) === null || _a === void 0 ? void 0 : _a.remoteVideoHTMLElementId) {
var remoteVideoElement = document.getElementById((_b = this.callingParams) === null || _b === void 0 ? void 0 : _b.remoteVideoHTMLElementId);
while (remoteVideoElement === null || remoteVideoElement === void 0 ? void 0 : remoteVideoElement.firstChild) {
/* istanbul ignore next */
this.debug && console.debug('[VoiceVideoCallingProxy][clearRemoteVideoElement]');
remoteVideoElement.firstChild.remove();
}
}
};
return VoiceVideoCallingProxy;
}());
exports.VoiceVideoCallingProxy = VoiceVideoCallingProxy;
var createVoiceVideoCalling = function () {
var args_1 = [];
for (var _i = 0; _i < arguments.length; _i++) {
args_1[_i] = arguments[_i];
}
return __awaiter(void 0, __spreadArray([], args_1, true), void 0, function (params) {
var proxy;
if (params === void 0) { params = {}; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
proxy = VoiceVideoCallingProxy.getInstance();
return [4 /*yield*/, proxy.load({
logger: params.logger || undefined
})];
case 1:
_a.sent();
return [2 /*return*/, Promise.resolve(proxy)];
}
});
});
};
exports.default = createVoiceVideoCalling;
//# sourceMappingURL=createVoiceVideoCalling.js.map