Skip to content

Commit

Permalink
initial changes to change media routing during call
Browse files Browse the repository at this point in the history
  • Loading branch information
davehorton committed Nov 17, 2024
1 parent 5efb895 commit d85e302
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
22 changes: 22 additions & 0 deletions lib/session/call-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,18 @@ Duration=${duration} `
this.logger.info({response}, '_lccBoostAudioSignal: response from freeswitch');
}

async _lccMediaPath(opts, callSid) {
const {type} = opts;

// only valid in a Dial verb
const task = this.currentTask;
if (!task || task.name !== TaskName.Dial) {
return this.logger.info('CallSession:_lccMediaPath - invalid command since we are not in a dial verb');
}
task.updateMediaPath(type)
.catch((err) => this.logger.error(err, 'CallSession:_lccMediaPath'));
}

_lccToolOutput(tool_call_id, opts, callSid) {
// only valid if we are in an LLM verb
const task = this.currentTask;
Expand Down Expand Up @@ -1672,6 +1684,9 @@ Duration=${duration} `
else if (opts.boostAudioSignal) {
return this._lccBoostAudioSignal(opts, callSid);
}
else if (opts.media_path) {
return this._lccMediaPath(opts.media_path, callSid);
}
else if (opts.llm_tool_output) {
return this._lccToolOutput(opts.tool_call_id, opts.llm_tool_output, callSid);
}
Expand Down Expand Up @@ -1975,6 +1990,13 @@ Duration=${duration} `
});
break;

case 'media-path':
this._lccMediaPath(data, call_sid)
.catch((err) => {
this.logger.info({err, data}, 'CallSession:_onCommand - error setting media path');
});
break;

case 'llm:tool-output':
this._lccToolOutput(tool_call_id, data, call_sid);
break;
Expand Down
43 changes: 37 additions & 6 deletions lib/tasks/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
TaskName,
TaskPreconditions,
MAX_SIMRINGS,
MediaPath,
KillReason
} = require('../utils/constants');
const assert = require('assert');
Expand Down Expand Up @@ -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 || {});
Expand Down Expand Up @@ -167,7 +169,7 @@ class TaskDial extends Task {
}

get shouldExitMediaPathEntirely() {
return this.canReleaseMedia && this.data.exitMediaPath;
return this.data.exitMediaPath;
}

get summary() {
Expand Down Expand Up @@ -307,7 +309,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) {
Expand Down Expand Up @@ -756,7 +758,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');
Expand Down Expand Up @@ -865,7 +867,7 @@ 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) setTimeout(this._releaseMedia.bind(this, cs, sd, this.shouldExitMediaPathEntirely), 200);
}

_bridgeEarlyMedia(sd) {
Expand All @@ -877,12 +879,40 @@ class TaskDial extends Task {
}
}

/* public api */
async updateMediaPath(type) {
switch (type) {
case 'no-media':
assert(this._mediaPath !== MediaPath.NoMedia, 'updateMediaPath: already no-media');
await this._releaseMedia(this.cs, this.sd, true);
this._mediaPath = MediaPath.NoMedia;
break;
case 'partial-media':
assert(this._mediaPath !== MediaPath.PartialMedia, 'updateMediaPath: already partial-media');
if (this._mediaPath === MediaPath.FullMedia) {
await this._releaseMedia(this.cs, this.sd, false);
}
else {
await this.reAnchorMedia(this.cs, this.sd);
}
this._mediaPath = MediaPath.PartialMedia;
break;
case 'full-media':
assert(this._mediaPath !== MediaPath.FullMedia, 'updateMediaPath: already full-media');
await this.reAnchorMedia(this.cs, this.sd);
this._mediaPath = MediaPath.FullMedia;
break;
default:
assert(false, `updateMediaPath: invalid type ${type}`);
}
}

/**
* 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 {
Expand All @@ -893,6 +923,7 @@ class TaskDial extends Task {
await cs.releaseMediaToSBC(bLegSdp, this.shouldExitMediaPathEntirely);
this.epOther = null;
this.logger.info('Dial:_releaseMedia - successfully released media from freewitch');
this._mediaPath = this.shouldExitMediaPathEntirely ? MediaPath.NoMedia : MediaPath.PartialMedia;
} catch (err) {
this.logger.info({err}, 'Dial:_releaseMedia error');
}
Expand Down Expand Up @@ -926,7 +957,7 @@ class TaskDial extends Task {
// 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;
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@
"ToneTimeout": "amd_tone_timeout",
"Stopped": "amd_stopped"
},
"MediaPath": {
"NoMedia": "no-media",
"PartialMedia": "partial-media",
"FullMedia": "full-media"
},
"MAX_SIMRINGS": 10,
"BONG_TONE": "tone_stream://v=-7;%(100,0,941.0,1477.0);v=-7;>=2;+=.1;%(1400,0,350,440)",
"FS_UUID_SET_NAME": "fsUUIDs"
Expand Down

0 comments on commit d85e302

Please sign in to comment.