sinch-rtc
Version:
RTC JavaScript/Web SDK
152 lines • 6.96 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InboundSession = void 0;
const mxp_1 = require("../mxp");
const models_1 = require("../mxp/models");
const MxpClientEvent_1 = require("../mxp/MxpClientEvent");
const utils_1 = require("../utils");
const Direction_1 = require("./Direction");
const EstablishedState_1 = require("./EstablishedState");
const fsm_1 = require("./fsm");
const TimerEventType_1 = require("./fsm/TimerEventType");
const TransitionSource_1 = require("./fsm/TransitionSource");
const jsep_1 = require("./jsep");
const Session_1 = require("./Session");
const SessionState_1 = require("./SessionState");
const SessionStateBase_1 = require("./SessionStateBase");
const TerminatedState_1 = require("./TerminatedState");
const TerminationCause_1 = require("./TerminationCause");
class InboundState extends SessionStateBase_1.SessionStateBase {
constructor(state, session) {
super(state, session);
}
}
class InboundCreatedState extends InboundState {
constructor(session) {
super(SessionState_1.SessionState.Created, session);
}
}
class InboundInitiatingState extends InboundState {
constructor(session) {
super(SessionState_1.SessionState.Initiating, session);
}
handleInboundMessage(message) {
if (message.isMethod(models_1.Method.Invite)) {
if (this.Session.processInvite(message)) {
this.transition(SessionState_1.SessionState.Progress, TransitionSource_1.TransitionSource.RemotePeerMessage);
return true;
}
}
return false;
}
}
class InboundProgressState extends InboundState {
constructor(session) {
super(SessionState_1.SessionState.Progress, session);
}
onEnter(_) {
this.Session.scheduleTimeout(this, this.timeoutRelativeToStart(this.CallSetupTimeoutMs), TimerEventType_1.TimerEventType.Termination);
this.Session.emitPendingJsepEvents();
}
onWillTerminate(source) {
if (source === TransitionSource_1.TransitionSource.RemotePeerMessage) {
return;
}
switch (this.terminationCause) {
case TerminationCause_1.TerminationCause.Error:
this.sendError(this.Session.error);
return;
case TerminationCause_1.TerminationCause.Timeout:
if (this.hasSentOutboundMessage(models_1.Method.Join))
this.sendOutboundMessage(this.Session.newMessage().method(models_1.Method.Leave).build());
return;
case TerminationCause_1.TerminationCause.None:
this.terminationCause = TerminationCause_1.TerminationCause.Denied;
this.sendOutboundMessage(this.Session.newMessage().method(models_1.Method.Deny).build());
}
}
handleInboundMessage(message) {
switch (message.method) {
case models_1.Method.Cancel:
case models_1.Method.Leave:
this.terminateBasedOnMessage(message, TerminationCause_1.TerminationCause.Canceled);
return true;
case models_1.Method.Error:
this.terminateBasedOnMessage(message, TerminationCause_1.TerminationCause.Error);
return true;
case models_1.Method.Joined:
if (message.body &&
this.Session.isLocalPeer(mxp_1.Codec.decodeClient(message.body))) {
this.transition(SessionState_1.SessionState.Established, TransitionSource_1.TransitionSource.RemotePeerMessage);
}
else {
this.terminationCause = TerminationCause_1.TerminationCause.OtherPeerAccepted;
this.transition(SessionState_1.SessionState.Terminated, TransitionSource_1.TransitionSource.RemotePeerMessage);
}
return true;
case models_1.Method.PeerEvent:
this.Session.emitJsepEvent(message);
return true;
default:
return false;
}
}
onLocalSessionDescription(sd) {
this.sendOutboundMessage(this.Session.newMessage()
.method(models_1.Method.Ack)
.body(models_1.Body.sdp(sd.asJson()))
.values({ [MxpClientEvent_1.MxpClientEventsVersionKey]: MxpClientEvent_1.MxpClientEventsVersion })
.build());
}
onAccept() {
const sd = this.Session.localDescription;
if (null == sd)
throw new fsm_1.InvalidOperationError("Local Answer is not set. Cannot process accept()");
this.sendOutboundMessage(this.Session.newMessage()
.method(models_1.Method.Join)
.body(models_1.Body.sdp(sd.asJson()))
.build());
}
}
class InboundSession extends Session_1.Session {
constructor(localPeer, sessionId, scheduler) {
super(localPeer, sessionId, Direction_1.Direction.Inbound, scheduler);
this.processInvite = (m) => {
if (m.method != models_1.Method.Invite)
throw new fsm_1.ArgumentError("Invalid method", (0, utils_1.nameof)(m));
if (!mxp_1.Message.tryExtractSessionKey(m, (key) => {
this.sessionKey = key;
}))
return false;
if (!jsep_1.JsepMessage.tryParse(m, (jsep) => this.addJsepMessage(jsep)))
return false;
this.invite = m;
return true;
};
const created = new InboundCreatedState(this);
const initiating = new InboundInitiatingState(this);
const progress = new InboundProgressState(this);
const established = new EstablishedState_1.EstablishedState(this);
const terminated = new TerminatedState_1.TerminatedState(this);
this.setInitialState(created);
const t = this.transitions;
t.add(SessionState_1.SessionState.Created, SessionState_1.SessionState.Terminated, terminated);
t.add(SessionState_1.SessionState.Created, SessionState_1.SessionState.Initiating, initiating);
t.add(SessionState_1.SessionState.Initiating, SessionState_1.SessionState.Progress, progress);
t.add(SessionState_1.SessionState.Initiating, SessionState_1.SessionState.Terminated, terminated);
t.add(SessionState_1.SessionState.Progress, SessionState_1.SessionState.Terminated, terminated);
t.add(SessionState_1.SessionState.Progress, SessionState_1.SessionState.Established, established);
t.add(SessionState_1.SessionState.Established, SessionState_1.SessionState.Terminated, terminated);
}
isLocalPeer(peer) {
if (!peer)
return false;
return (this.localPeer.userId == peer.userId &&
this.localPeer.instanceId == peer.instanceId);
}
accept() {
this.tryWithCurrentState((s) => s.accept());
}
}
exports.InboundSession = InboundSession;
//# sourceMappingURL=InboundSession.js.map