Note, drachtio-fsmrf applications require a freeswitch media server, configured as per drachtio/drachtio-freeswitch-mrf. To build your own properly-configured Freeswitch to use with this module, either review that Dockerfile or have a look at drachtio-mrf-ansible
This module exports a single class, Mrf (aka Media Resource Framework).
Invoking the constructor creates an instance of the Mrf class that can then be used to connect to Mediaservers; once connected to a Mediaserver you can create and manipulate instances of Endpoints and Conferences.
That's it -- those are all the classes you need to work with. You can connect calls to a Mediaserver, producing an Endpoint. You can then perform operations like play, say, bridge, park etc on the Endpoint (which equates to a Freeswitch channel). You can create Conferences, join Endpoints into Conferences, and perform operations on the Conference. And you can call any of the myriad freeswitch applications or api methods via the Endpoint and Conference classes.
Let's dive in.
First, create an instance of both the drachtio signaling resource framework and the media resource framework, as per below.
const Srf = ;const Mrf = ;const srf = ;srf;srf;const mrf = srf ;
At that point, the mrf instance can be used to connect to and produce instances of MediaServers
In the example above, we see the
mrf#connect method returns a Promise that resolves with an instance of the media server. As with all public methods, a callback variant is available as well:
// we're connecting to the Freeswitch event socketmrf;
Having a media server instance, we can now create instances of Endpoints and Conferences and invoke operations on those objects.
Performing Media Operations
We can create an Endpoint when we have an incoming call, by connecting it to a Mediaserver.
In the example above, we use
MediaServer#connectCaller() to connect a call to a Mediaserver, producing both an Endpoint (represening the channel on Freeswitch) and a Dialog (representing the UAS dialog).
Again, note that a callback version is also available:
We can also create an Endpoint outside of any inbound call by calling
MediaServer#createEndpoint(). This will give us an initially inactive Endpoint that we can later modify to stream to a remote destination:
Once we have an Endpoint, we can do things like play a prompt and collect dtmf:
Conferences work similarly - we create them and then can join Endpoints to them.
When an Endpoint is joined to a Conference, we have an additional set of operations we can invoke on the Endpoint -- things like mute/unmute, turn on or off automatic gain control, playing a file directly to the participant on that Endpoint, etc. These actions are performed by methods that all begin with conf:
Execute any Freeswitch application or api
As shown above, some methods have been added to the
Conference class to provide syntactic sugar over freeswitch aplications and apis. However, any Freeswitch application or api can also be called directly.
Endpoint#execute executes a Freeswitch application and returns in either the callback or the Prompise the contents of the associated CHANNEL_EXECUTE_COMPLETE event that Freeswitch returns. The event structure is defined here:
// generate dtmf from an Endpointendpoint
Endpoint#api executes a Freeswitch api call and returns in either the callback or the Promise the response that Freeswitch returns to the command.
Mrf#utils#parseBodyText method, as per below:
To run tests you will need Docker and docker-compose installed on your host, as the test suite runs in a docker network created by docker-compose-testbed.yaml. The first time you run the tests, it will take a while since docker images will be downloaded to your host.
$ npm teststarting docker networkdocker network started giving extra time for freeswitch to initialize...Mrf#connect using Promise✔ mrflocalAddresses is an array✔ socket connected✔ mediaserversrf is an Srf...etc...