infobip-rtc
Version:
Infobip RTC JavaScript SDK - Infobip WebRTC API Implementation
128 lines • 6.71 kB
JavaScript
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());
});
};
import HangupStatusFactory from "../util/HangupStatusFactory";
import { CallStatus } from "../CallStatus";
import { DefaultApplicationCall } from "./DefaultApplicationCall";
import { WsAction } from "../ws/WsAction";
import { DeclineType } from "../DeclineType";
import HangupReasonFactory from "./util/HangupReasonFactory";
import { ApplicationCallOptions } from "../options/ApplicationCallOptions";
export class DefaultIncomingApplicationCall extends DefaultApplicationCall {
constructor(eventEmitter, gateway, logger, rtcConfig, device, caller, callsConfigurationId, remoteOffer, currentUserIdentity, token, apiUrl, callId) {
super(eventEmitter, gateway, logger, rtcConfig, device, callsConfigurationId, null, currentUserIdentity, token, apiUrl, callId);
this.caller = caller;
this.remoteOffer = remoteOffer;
this.callStatus = CallStatus.RINGING;
}
from() {
return this.caller.identity;
}
fromDisplayName() {
return this.caller.displayName;
}
accept(options) {
var _a, _b, _c, _d, _e;
if (this.callStatus !== CallStatus.RINGING) {
return;
}
this.callStatus = CallStatus.CONNECTING;
this.applicationCallOptions = options || ApplicationCallOptions.builder().build();
this.localAudio = { active: (_b = (_a = this.applicationCallOptions) === null || _a === void 0 ? void 0 : _a.audio) !== null && _b !== void 0 ? _b : true };
this.localCameraVideo = { active: (_d = (_c = this.applicationCallOptions) === null || _c === void 0 ? void 0 : _c.video) !== null && _d !== void 0 ? _d : false };
if ((_e = this.applicationCallOptions) === null || _e === void 0 ? void 0 : _e.dataChannel) {
this.createDataChannel();
}
this.createAudioPeerConnection();
this.negotiateAudio(this.applicationCallOptions);
}
negotiateAudio(options) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
try {
const stream = yield this.getLocalAudioStream(options === null || options === void 0 ? void 0 : options.audio, options === null || options === void 0 ? void 0 : options.video, (_a = options === null || options === void 0 ? void 0 : options.videoOptions) === null || _a === void 0 ? void 0 : _a.cameraOrientation, (_b = options === null || options === void 0 ? void 0 : options.videoOptions) === null || _b === void 0 ? void 0 : _b.cameraVideoFrameRate);
if (this.isFinished()) {
this.logger.warn(`Call is no longer connecting, aborting audio negotiation`, this.callId);
stream === null || stream === void 0 ? void 0 : stream.getAudioTracks().forEach((track) => track.stop());
return;
}
this.setLocalAudioStream(stream);
yield this.setRemoteDescription();
if (this.isFinished()) {
this.logger.warn(`Call is no longer connecting, aborting audio negotiation`, this.callId);
return;
}
this.setRemoteCandidates();
this.addTracks();
let answer = yield this.audioPC.peerConnection.createAnswer();
answer = yield this.setLocalDescription(this.audioPC.peerConnection, answer);
if (this.isFinished()) {
this.logger.warn(`Call is no longer connecting, aborting audio negotiation`, this.callId);
return;
}
this.sendAnswer(answer, options);
}
catch (error) {
this.handleIncomingCallFlowError(error);
}
});
}
decline(options) {
if (this.callStatus !== CallStatus.RINGING) {
return;
}
this.callStatus = CallStatus.FINISHED;
this.declineCall(DeclineType.BUSY, 'Busy Here', options === null || options === void 0 ? void 0 : options.declineOnAllDevices);
}
setRemoteDescription() {
return __awaiter(this, void 0, void 0, function* () {
return yield this.audioPC.peerConnection.setRemoteDescription(this.remoteOffer);
});
}
addTracks() {
let stream = this.emptyAudioStream ? this.emptyAudioStream.stream() : this.localAudioStream;
stream.getTracks().forEach((track) => this.audioPC.peerConnection.addTrack(track, this.localAudioStream));
this.localAudio.transceiver = this.audioPC.peerConnection.getTransceivers()
.find(transceiver => transceiver.receiver.track.kind === 'audio');
}
sendAnswer(answer, options) {
this.gateway.send({
action: WsAction.APP_CALL_ACCEPT,
callId: this.callId,
description: answer,
media: {
audio: {
muted: !this.localAudio.active,
},
video: {
camera: this.localCameraVideo.active
}
},
autoReconnect: options === null || options === void 0 ? void 0 : options.autoReconnect
});
}
handleIncomingCallFlowError(error) {
this.logger.error(`Call flow error occurred: ${error} ${error === null || error === void 0 ? void 0 : error.stack}`, this.callId);
let reason = HangupReasonFactory.getHangupReason(error);
this.declineCall(DeclineType.ERROR, reason);
let hangupStatus = HangupStatusFactory.getApplicationHangupStatus(error);
this.eventEmitter.emit('call-error', { status: hangupStatus });
}
declineCall(declineType, reason, declineOnAllDevices = false) {
let message = {
action: WsAction.APP_CALL_DECLINE,
callId: this.callId,
declineType: declineType,
reason: reason,
declineOnAllDevices: declineOnAllDevices
};
this.gateway.send(message);
}
}
//# sourceMappingURL=DefaultIncomingApplicationCall.js.map