Call recording in Asterisk

I´ve been using asterisk for a while and it performs like a charm. Using a basic dialplan with the IAX protocal its time to add some call recording. There are at least two way to go about it:

  • Plain extentions.conf ’scripting’. This file is being pulled through some kind of parser -everytime- a call is being requested. Furthermore all kind of system variables are available. If your familiar with PHP you can compare them to $_GLOBALS.
  • Using macro´s. Usually I’ am not a fan of macros, but this is the exception to the rule. They greatly enhance readability of your dialplans and are similar to PHP functions.

Scripting

Cut-out extentions.conf
exten => _0034[9]XXXXXXXX,1,Verbose(1|vsp-out)
exten => _0034[9]XXXXXXXX,n,NoOp()
exten => _0034[9]XXXXXXXX,n,Set(SCREEN_FILE=OUTBOUND-${EPOCH}-${CALLERID(num)}-${MACRO_EXTEN}.wav)
exten => _0034[9]XXXXXXXX,n,MixMonitor(${SCREEN_FILE},b)
exten => _0034[9]XXXXXXXX,n,Dial(SIP/vsp/${EXTEN})
exten => _0034[9]XXXXXXXX,n,StopMonitor()
exten => _0034[9]XXXXXXXX,n,Hangup()

For every outbound request you start with defining a filename for the recording eg. SCREEN_FILE. In this example I used a combination of system variables to make up a filename. By default they are put into a wav format but with some extras realtime convertion to mp3 is possible.
Second you have to start the recording; this is done with MixMonitor() which takes, among others, the filename as an parameter. By default all files are stored in /var/spool/asterisk/monitor, which is defined in asterisk.conf. Finally the recording is stopped with StopMonitor().

Macros

There comes a time, especially when you´re getting the hang of asterisk scripting ;), and feel the power of a fully fledged PABX at the tip of your fingers, rewriting of your extentsion.conf is needed. I mean really needed! After building all kind of nifty things like demo accounts to test the quality of speach/connections through Echo(), or rotating huntgroups, playing back welcome messages before actually any call is taken with Play(ing)back() the weasels, IVR´s and making your server crazy doing all kind of silly stuff controlled by DTMF… just to name a few..
Ok, uh macros… right! ;) Here it comes:

[macro-record]
exten => s,1,Set(SCREEN_FILE=OUTBOUND-${EPOCH}-${CALLERID(num)}-${MACRO_EXTEN}.wav)
exten => s,n,NoOp()
exten => s,n,Verbose(1|VR ${ARG1}/${ARG2} ${SCREEN_FILE})
exten => s,n,MixMonitor(${SCREEN_FILE})
exten => s,n,Dial(SIP/${ARG2}/${ARG1},20)
exten => s,n,StopMonitor()
exten => s,n,Hangup()

In order to get a macro called you’ll have to give it a proper name. Although they both appear in extentions.conf, written between [], asterisk distinguishes between a ‘context’ and a macro by its name prefix. So in this example the macro ‘record’ is defined as [macro_record]. It´s basically a rewrite, with some extra tweaks but more importantly it uses a variable ARGS2 for the extention being dialed.

The first dialplan can now be rewritten in just two lines:

exten => _0034[9]XXXXXXXX,1,Verbose(1|vsp, CV)
exten => _0034[9]XXXXXXXX,n,Macro(record,${EXTEN},vsp)

So if your need to add another country with a different VSP you could use:

exten => _0044[6]XXXXXXXX,1,Verbose(1|vsp2, CV)
exten => _0044[6]XXXXXXXX,n,Macro(record,${EXTEN},vsp2)

You probably have noticed all the Verbose() lines. I use them to ‘debug’ while testing in the asterisk console. I you feel confident about your creations you can erase them. Effectively reducing a channel selection with call recording to one single line! Cool hè

About:
Asterisk dialplan
Asterisk macros

July 21st, 2008 - Posted in asterisk | |

Leave a reply