From 48d32829d7b9a7eab8a4b5b085528ca33e775605 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Mon, 17 Feb 2020 11:26:09 -0500 Subject: [PATCH] factor out rtpengine into package --- lib/call-session.js | 22 +++++++++++++--------- package-lock.json | 21 ++++++++++++++++++++- package.json | 3 ++- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/lib/call-session.js b/lib/call-session.js index 571fb5f..ccad7a2 100644 --- a/lib/call-session.js +++ b/lib/call-session.js @@ -1,6 +1,5 @@ const Emitter = require('events'); -const Client = require('rtpengine-client').Client ; -const rtpengine = new Client(); +const {getRtpEngine} = require('jambonz-rtpengine-utils')(process.env.JAMBONES_RTPENGINES.split(',')); const {makeRtpEngineOpts} = require('./utils'); const {forwardInDialogRequests} = require('drachtio-fn-b2b-sugar'); const {SipError} = require('drachtio-srf'); @@ -15,17 +14,22 @@ class CallSession extends Emitter { this.performLcr = this.srf.locals.dbHelpers.performLcr; this.logger = logger.child({callId: req.get('Call-ID')}); this.useWss = req.locals.registration && req.locals.registration.protocol === 'wss'; - - // TODO: we always using the first rtpengine here - // we should be load balancing, and doing some form of health checking - this.offer = rtpengine.offer.bind(rtpengine, this.srf.locals.rtpEngines[0]); - this.answer = rtpengine.answer.bind(rtpengine, this.srf.locals.rtpEngines[0]); - this.del = rtpengine.delete.bind(rtpengine, this.srf.locals.rtpEngines[0]); } async connect() { + const engine = getRtpEngine(this.logger); + if (!engine) { + this.logger.info('No available rtpengines, rejecting call!'); + return this.res.send(480); + } + debug(`got engine: ${JSON.stringify(engine)}`); + const {offer, answer, del} = engine; + this.offer = offer; + this.answer = answer; + this.del = del; + this.rtpEngineOpts = makeRtpEngineOpts(this.req, false, this.useWss); - this.rtpEngineResource = {destroy: this.del.bind(rtpengine, this.rtpEngineOpts.common)}; + this.rtpEngineResource = {destroy: this.del.bind(null, this.rtpEngineOpts.common)}; let proxy, uris; try { diff --git a/package-lock.json b/package-lock.json index 3454889..9953036 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "sbc-outbound", - "version": "0.3.1", + "version": "0.3.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1818,6 +1818,25 @@ "redis": "^2.8.0" } }, + "jambonz-rtpengine-utils": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/jambonz-rtpengine-utils/-/jambonz-rtpengine-utils-0.0.5.tgz", + "integrity": "sha512-tkAwXkV6nYVBvF/epcIoNOUX8NDepFo25WP4a2DBu0+g8e76MzDChXGuziYRGswL3TFZxd6faTbU44rg+UcoMA==", + "requires": { + "debug": "^4.1.1", + "rtpengine-client": "0.0.9" + }, + "dependencies": { + "rtpengine-client": { + "version": "0.0.9", + "resolved": "https://registry.npmjs.org/rtpengine-client/-/rtpengine-client-0.0.9.tgz", + "integrity": "sha512-AkeDx6QEk/WtIBGnoxf3/lWbV0h4JTlPPBCHW+qjYT4xfKYzzCFCmP639ovuIJhE0L6aL89Vv0YmlQdEWo6ofw==", + "requires": { + "bencode": "^1.0.0" + } + } + } + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/package.json b/package.json index 5da95b2..6217b99 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sbc-outbound", - "version": "0.3.1", + "version": "0.3.2", "main": "app.js", "engines": { "node": ">= 8.10.0" @@ -30,6 +30,7 @@ "debug": "^4.1.1", "drachtio-fn-b2b-sugar": "^0.0.12", "drachtio-srf": "^4.4.28", + "jambonz-rtpengine-utils": "0.0.5", "jambonz-db-helpers": "^0.3.2", "jambonz-mw-registrar": "^0.1.0", "pino": "^5.14.0",