mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 16:50:39 +00:00
wip (#979)
This commit is contained in:
@@ -6,6 +6,7 @@ const {
|
||||
TaskName,
|
||||
TaskPreconditions,
|
||||
MAX_SIMRINGS,
|
||||
MediaPath,
|
||||
KillReason
|
||||
} = require('../utils/constants');
|
||||
const assert = require('assert');
|
||||
@@ -108,6 +109,7 @@ class TaskDial extends Task {
|
||||
this.proxy = this.data.proxy;
|
||||
this.tag = this.data.tag;
|
||||
this.boostAudioSignal = this.data.boostAudioSignal;
|
||||
this._mediaPath = MediaPath.FullMedia;
|
||||
|
||||
if (this.dtmfHook) {
|
||||
const {parentDtmfCollector, childDtmfCollector} = parseDtmfOptions(logger, this.data.dtmfCapture || {});
|
||||
@@ -155,17 +157,21 @@ class TaskDial extends Task {
|
||||
|
||||
get canReleaseMedia() {
|
||||
const keepAnchor = this.data.anchorMedia ||
|
||||
this.cs.isBackGroundListen ||
|
||||
this.cs.onHoldMusic ||
|
||||
ANCHOR_MEDIA_ALWAYS ||
|
||||
this.listenTask ||
|
||||
this.dubTasks ||
|
||||
this.transcribeTask ||
|
||||
this.startAmd;
|
||||
this.cs.isBackGroundListen ||
|
||||
this.cs.onHoldMusic ||
|
||||
ANCHOR_MEDIA_ALWAYS ||
|
||||
this.listenTask ||
|
||||
this.dubTasks ||
|
||||
this.transcribeTask ||
|
||||
this.startAmd;
|
||||
|
||||
return !keepAnchor;
|
||||
}
|
||||
|
||||
get shouldExitMediaPathEntirely() {
|
||||
return this.data.exitMediaPath;
|
||||
}
|
||||
|
||||
get summary() {
|
||||
if (this.target.length === 1) {
|
||||
const target = this.target[0];
|
||||
@@ -186,6 +192,16 @@ class TaskDial extends Task {
|
||||
|
||||
async exec(cs) {
|
||||
await super.exec(cs);
|
||||
|
||||
if (this.data.anchorMedia && this.data.exitMediaPath) {
|
||||
this.logger.info('Dial:exec - incompatible anchorMedia and exitMediaPath are both set, will obey anchorMedia');
|
||||
delete this.data.exitMediaPath;
|
||||
}
|
||||
if (!this.canReleaseMedia && this.data.exitMediaPath) {
|
||||
this.logger.info(
|
||||
'Dial:exec - exitMediaPath is set so features such as transcribe and record will not work on this call');
|
||||
}
|
||||
|
||||
try {
|
||||
if (this.listenTask) {
|
||||
const {span, ctx} = this.startChildSpan(`nested:${this.listenTask.summary}`);
|
||||
@@ -303,7 +319,7 @@ class TaskDial extends Task {
|
||||
if (!cs.callGone && this.epOther) {
|
||||
|
||||
/* if we can release the media back to the SBC, do so now */
|
||||
if (this.canReleaseMedia) this._releaseMedia(cs, this.sd);
|
||||
if (this.canReleaseMedia) this._releaseMedia(cs, this.sd, this.shouldExitMediaPathEntirely);
|
||||
else this.epOther.bridge(this.ep);
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -752,7 +768,7 @@ class TaskDial extends Task {
|
||||
// Offhold, time to release media
|
||||
const newSdp = await this.ep.modify(req.body);
|
||||
await res.send(200, {body: newSdp});
|
||||
await this._releaseMedia(this.cs, this.sd);
|
||||
await this._releaseMedia(this.cs, this.sd, this.shouldExitMediaPathEntirely);
|
||||
this.isOutgoingLegHold = false;
|
||||
} else {
|
||||
this.logger.debug('Dial: _onReinvite receive unhold Request, update media server');
|
||||
@@ -861,7 +877,9 @@ class TaskDial extends Task {
|
||||
}
|
||||
|
||||
/* if we can release the media back to the SBC, do so now */
|
||||
if (this.canReleaseMedia) setTimeout(this._releaseMedia.bind(this, cs, sd), 200);
|
||||
if (this.canReleaseMedia || this.shouldExitMediaPathEntirely) {
|
||||
setTimeout(this._releaseMedia.bind(this, cs, sd, this.shouldExitMediaPathEntirely), 200);
|
||||
}
|
||||
}
|
||||
|
||||
_bridgeEarlyMedia(sd) {
|
||||
@@ -873,22 +891,57 @@ class TaskDial extends Task {
|
||||
}
|
||||
}
|
||||
|
||||
/* public api */
|
||||
async updateMediaPath(desiredPath) {
|
||||
this.logger.info(`Dial:updateMediaPath - ${this._mediaPath} => ${desiredPath}`);
|
||||
switch (desiredPath) {
|
||||
case MediaPath.NoMedia:
|
||||
assert(this._mediaPath !== MediaPath.NoMedia, 'updateMediaPath: already no-media');
|
||||
await this._releaseMedia(this.cs, this.sd, true);
|
||||
break;
|
||||
|
||||
case MediaPath.PartialMedia:
|
||||
assert(this._mediaPath !== MediaPath.PartialMedia, 'updateMediaPath: already partial-media');
|
||||
if (this._mediaPath === MediaPath.FullMedia) {
|
||||
await this._releaseMedia(this.cs, this.sd, false);
|
||||
}
|
||||
else {
|
||||
// to go from no-media to partial-media we need to go through full-media first
|
||||
await this.reAnchorMedia(this.cs, this.sd);
|
||||
await this._releaseMedia(this.cs, this.sd, false);
|
||||
}
|
||||
assert(!this.epOther, 'updateMediaPath: epOther should be null');
|
||||
assert(!this.ep, 'updateMediaPath: ep should be null');
|
||||
|
||||
break;
|
||||
case MediaPath.FullMedia:
|
||||
assert(this._mediaPath !== MediaPath.FullMedia, 'updateMediaPath: already full-media');
|
||||
await this.reAnchorMedia(this.cs, this.sd);
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(false, `updateMediaPath: invalid path request ${desiredPath}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the media from freeswitch
|
||||
* @param {*} cs
|
||||
* @param {*} sd
|
||||
*/
|
||||
async _releaseMedia(cs, sd) {
|
||||
async _releaseMedia(cs, sd, releaseEntirely = false) {
|
||||
assert(cs.ep && sd.ep);
|
||||
|
||||
try {
|
||||
// Wait until we got new SDP from B leg to ofter to A Leg
|
||||
const aLegSdp = cs.ep.remote.sdp;
|
||||
await sd.releaseMediaToSBC(aLegSdp, cs.ep.local.sdp);
|
||||
await sd.releaseMediaToSBC(aLegSdp, cs.ep.local.sdp, releaseEntirely);
|
||||
const bLegSdp = sd.dlg.remote.sdp;
|
||||
await cs.releaseMediaToSBC(bLegSdp);
|
||||
await cs.releaseMediaToSBC(bLegSdp, releaseEntirely);
|
||||
this.epOther = null;
|
||||
this.logger.info('Dial:_releaseMedia - successfully released media from freewitch');
|
||||
this._mediaPath = releaseEntirely ? MediaPath.NoMedia : MediaPath.PartialMedia;
|
||||
this.logger.info(
|
||||
`Dial:_releaseMedia - successfully released media from freewitch, media path is now ${this._mediaPath}`);
|
||||
} catch (err) {
|
||||
this.logger.info({err}, 'Dial:_releaseMedia error');
|
||||
}
|
||||
@@ -898,8 +951,14 @@ class TaskDial extends Task {
|
||||
if (cs.ep && sd.ep) return;
|
||||
|
||||
this.logger.info('Dial:reAnchorMedia - re-anchoring media to freewitch');
|
||||
await Promise.all([sd.reAnchorMedia(), cs.reAnchorMedia()]);
|
||||
await Promise.all([sd.reAnchorMedia(this._mediaPath), cs.reAnchorMedia(this._mediaPath)]);
|
||||
this.epOther = cs.ep;
|
||||
|
||||
this.epOther.bridge(this.ep);
|
||||
|
||||
this._mediaPath = MediaPath.FullMedia;
|
||||
this.logger.info(
|
||||
`Dial:_releaseMedia - successfully re-anchored media to freewitch, media path is now ${this._mediaPath}`);
|
||||
}
|
||||
|
||||
// Handle RE-INVITE hold from caller leg.
|
||||
@@ -918,11 +977,12 @@ class TaskDial extends Task {
|
||||
}
|
||||
this._onHoldHook(req);
|
||||
} else if (!isOnhold(req.body)) {
|
||||
if (this.epOther && this.ep && this.isIncomingLegHold && this.canReleaseMedia) {
|
||||
if (this.epOther && this.ep && this.isIncomingLegHold &&
|
||||
(this.canReleaseMedia || this.shouldExitMediaPathEntirely)) {
|
||||
// Offhold, time to release media
|
||||
const newSdp = await this.epOther.modify(req.body);
|
||||
await res.send(200, {body: newSdp});
|
||||
await this._releaseMedia(this.cs, this.sd);
|
||||
await this._releaseMedia(this.cs, this.sd, this.shouldExitMediaPathEntirely);
|
||||
isHandled = true;
|
||||
}
|
||||
this.isIncomingLegHold = false;
|
||||
|
||||
Reference in New Issue
Block a user