Redial: Interactive Telephony : Week 3

Soft Phones and the Dialplan Continued: Core Concepts, Advanced Commands

Soft Phones

Since we are using VoIP and Asterisk, we have the option of using Soft Phones. Soft Phones are VoIP phone implementations in software and they run on a desktop or laptop (other devices too but we will get to that later).

Normally someone would use a soft phone like Skype or Gizmo to contact a service provider (a TSP) on the internet and the calls would be routed through that network. Since we are using Asterisk, we can setup our softphones to connect directly to our Asterisk server, in effect creating our own network.

As we discussed, Asterisk can work with a variety of different protocols (supposedly it even supports Skype) so we can be pretty flexible with the choice of soft phones. Our Asterisk server is setup to deal with both SIP and IAX (pronounced eecks) protocols.

SIP stands for Session Initiation Protocol and has use in a variety of technologies (presence, VoIP, video conference and so on). Without getting too involved in describing SIP, it is a means to describe and invite software to connect a stream or other network service. It actually is quite a bit like an email invitation describing the location of the event and how to be dressed. You can look at the SIP messages that are coming to and from our Asterisk server by issuing the "sip debug" ("sip no debug" to turn it off) in the Asterisk console.

SIP doesn't actually transmit the audio of the call, it just explains how to get the audio of the call and so on. The audio is transmitted via a protocol called RTP (Realtime Transport Protocol) which is derived from UDP. UDP is chosen as it provides lower latency (no error correct) than TCP. In other words if a packet of audio data is lost, it doesn't need to re-requested as it would have to be with TCP. If it arrives late, it is just discarded and the audio of the call doesn't need to be held up waiting for all of the data to arrive intact. If that weren't the case, we would have very strange calls indeed, with increasing delay between when we said something and when the other person hears it.

One of the big problems with UDP is that it is "stateless", meaning that a constant connection is not maintained between the server and the client when transmitting. This means that SIP/RTP can have trouble routing audio through routers and firewalls (NAT is especially problematic).

The alternative that we have is IAX. IAX stands for Inter-Asterisk Exchange and was developed primarily for use with Asterisk. The benefits of using IAX over SIP is that it can easily route through home firewalls and NAT routers while SIP has problems with such. Unfortunately, by using IAX we are diverging from established standards. There is some more reading offered about IAX below.

Some Softphones

iaxComm

iaxComm is an open source softphone that uses IAX to communicate with other softphones or PBX's. It is a bit buggy but is cross platform with versions for Windows, Mac and Linux. You can download is here: http://iaxclient.sourceforge.net/iaxcomm/. For the Mac version you have to scroll down the page a bit looking for "iaxcomm-mac-1.0rc3.zip".

To setup iaxComm to work with our Asterisk server we need to use an iax.conf file. Just like the other conf files that we are editing, I setup a way to dynamically include our individual conf files into the main conf file. Use the following naming convention to create your own iax.conf file in your asterisk_conf directory:

NETID_iax.conf

Mine is called sve204_iax.conf and contains the following:

[sve204_redial_iax]
type=friend
host=dynamic
username=sve204_redial_iax
secret=sve204_redial_iax
auth=md5
context=sve204_redial_iax
		
While this shouldn't be required, it seems that the top line (the IAX context) should match the username. The "type" "friend" means that this softphone can both make and receive calls. We will primarily be using this to make calls. "secret" is the password, "auth" is the type of encryption that is used when transferring the password over the network, "context" is the context in the dialplan that is first used when this softphone dials our server.

The extensions.conf context that I am using, sve204_redial_iax is defined as follows:

[sve204_redial_iax]
exten => _X,1,Answer();
exten => _X,n,Goto(2127960729,s,1)
		
It is defined in my extensions.conf file sve204_extensions.conf. The first line tells Asterisk to answer the line when a call comes in. The second line is a regular expression that matches a dialed extension. In this case a single digit. It jumps to the 2127960729" context which is the main context that is being used when we dial in from any normal phone. In a sense this context sets the softphone to behave as it is any normal phone coming in to our Asterisk setup by dialing 2127960729. (Don't forget to "reload" (choosing SIP/IAX) which will activate your iax.conf file using our reload URL: http://asterisk.itp.tsoa.nyu.edu/asterisk/asterisk_reload.php

I would suggest that if you want to use a softphone to make it easier to create and work with your applications on Asterisk that you use a similar context in your IAX configuration.

In iaxComm's account settings, (under Options), you put an Account Name, I usually match this with the username but I don't think it is important. You put the host, which should be "asterisk.itp.tsoa.nyu.edu", the username which you defined in the iax.conf file (which should be something like netid_iax) and the password which you defined in the iax.conf file.

When you launch iaxComm it should automatically connect to our Asterisk server. To make a call into the context as I have suggested, you just need to dial one digit and push "Dial". If you are having problems, log into the asterisk manager interface and issue the command: iax2 set debug. This will give you debugging output. To turn off debugging (which I would do quickly to be nice to everyone else using the interface, issue the command "iax2 set debug off".

Gizmo

Gizmo is a free SIP based soft phone that is cross platform (unfortunately, I don't think it is open source). It was developed to be used with the SIPPhone network (which is cool and you should check it out) but has the means to be used with a different network by setting up a "secondary" connection. You can download it here: http://www.gizmoproject.com/

To use Gizmo (or any other SIP softphone) you need to create an entry in sip.conf. Just like all of our other configuration files, you can create a file in the asterisk_conf directory using the naming convention "netid_sip.conf" and it will be automatically included in the main configuration when you "reload".

Here are the contents of sve204_sip.conf:
[sve204_gizmo]
type=friend
username=sve204_gizmo
secret=sve204_gizmo
host=dynamic
dtmfmode=rfc2833
allowguest=yes
insecure=very
promiscredir=yes
context=sve204_redial_sip
		
Almost all of the options are the same as with the iax.conf file. As with using an IAX softphone, a context from the dialplan is specified. In this case, I am using redial_sip which is defined in redial_sip_extensions.conf.

Here are the contexts of redial_sip_extensions.conf:
[sve204_redial_sip]
exten => s,1,Goto(redial_sve204_sip,1,1);
exten => _X,1,Answer();
exten => _X,n,Goto(2127960729,s,1);
		
It does the exact same thing as sve204_redial_iax, drops the caller into the 2127960729 context so that everything behaves as if it was a normal incoming call.

Unfortunately, in order to use Gizmo you first have to register an account on the SIPPhone network. Gizmo will prompt you to do that when you first launch it. After you have done this you can specify a "secondary account". This is where you will put the corresponding information from your sip.conf file.

When you launch Gizmo from now on it should automatically connect to our Asterisk server. Before you call, you should choose your secondary account for your "Outgoing Call Account" under "Account" and only dial 1 digit.

There are a bunch more SIP and IAX based softphones. Please feel free to try another one if you like. Here is a good list: http://en.wikipedia.org/wiki/Comparison_of_VoIP_software

More Information:

  • Session Initiation Protocol - Wikipedia
  • Real-time Transport Protocol - Wikipedia
  • IAX - voip-info.org
  • IAX vs SIP


  • Dialplan Dialplan Dialplan Dialplan

    Last week we went over several commands/applications that can be used in the Asterisk Dialplan. This week I want to go over some of the more difficult or advanced features.

    Predefined Channel Variables:
    Under the heading "Predefined Channel Variables" this page lists all of the variables that are predefined for you. You can use these variables in your dialplan logic.

    A good way to get started is to use them with the NoOp command. This will output them on the Asterisk console so you can see their values:
    exten => s,n,NoOp(${CALLERID(all)});
    exten => s,n,NoOp(${SIPUSERAGENT});
    exten => s,n,NoOp(${EXTEN});
    		
    Pattern Matching:
    When you define an extension in Asterisk you can either choose to put the exact digits or use a pattern to match the extension.

    The "_" indicates that we are using a pattern.
    The "X" matches any digit
    The "." matches anything one or more times.
    Brackets "[" enclose ranges. "[13-5]" matches 1, 3, 4, and 5.

    Using patterns allows you to capture user input. The exact input will always be in the EXTEN variable so you can use that in your logic.

    There is a very good write-up regarding pattern matching in the Asterisk book. Check Chapter 5 around page 137.

    Database Interaction:
    Last week we discussed variables and so on. The problem with variables is that they go away once a call is completed. To save data you can use the AstDB.

    To put a value in the AstDB you use the Set command (just like variables) but also use the DB command inside the Set command, like so:

    exten => s,1,Set(DB(sve204/lastcalltime)=${DATETIME})

    This sets a value in the "sve204" family called "lastcalltime". The value of the variable is the current date and time (a predefined variable).

    I can get the value of this out of the database by using the following command:

    exten => s,n,Set(LASTCALLTIME=${DB(sve204/lastcalltime)});

    This would set the value in a variable called LASTCALLTIME which tell me the last time someone called and the above code got executed.

    Say I wanted to keep a log of how many times anyone did something on the system. I could do the following:

    exten => s,n,Set(SOMETHING=${DB(sve204/something)})
    exten => s,n,SetVar(SOMETHING=$[${SOMETHING} + 1])
    exten => s,n,Set(DB(sve204/something)=${SOMETHING})
    		


    Outbound Dialing:
    Asterisk can make outbound calls as well as receive incoming calls. To make an outbound call from your dialplan you use the Dial application.

    exten => s,1,Dial(SIP/itp_jnctn/17188096659,30,r);

    The above command will always dial my cell phone. The command is Dial, the first argument the connection type which for our purposes will be SIP. The second is the SIP context to use for dialing. For our purposes it will always be "itp_jnctn". This is followed by the number or extension to dial. The 30 means wait 30 seconds and the "r" means indicate ringing to us.

    You can also call your softphone in this manner. The SIP context will be what you defined in your sip.conf file.

    exten => s,1,Dial(SIP/sve204_gizmo,30,r);

    Asterisk cmd Dial - Voip-Info.org

    Recording Audio:
    Records what a user says for a specified period of time.

    exten => s,1,Record(/home/sve204/sve204_menu.gsm|5|30);

    The first argument is the name of the file with the format, after the slash it is the amount of silence detection (in seconds I beleive) and the next argument is the amount of time to record.

    Asterisk cmd Record

    Playback with Controls:
    Plays an audio file with controls.

    exten => s,n,ControlPlayback(/home/sve204/asterisk_sounds/shawn_test,1000,#,*,0,1,2);

    The first argument is the name of the file to play, the second the number of milliseconds to move per button push. The last arguments are the buttons for rewind, fast forward, stop, play and pause.

    Asterisk cmd ControlPlayback

    Monitoring/Recording a Channel:
    Asterisk can record the audio of a call. By default it saves the audio in two separate files, one for each side of the "conversation".

    exten => s,1,Monitor(wav,/home/sve204/asterisk_sounds/shawn_monitor);

    This command will create two files, both in /var/spool/asterisk/monitor. One will be named shawn_monitor-in.wav, the other shawn_monitor-out.wav.

    Asterisk cmd Monitor
    Also checkout MixMonitor which mixes the two channels at: Asterisk cmd MixMonitory

    Executing a System Command:
    Asterisk can work with other applications running on the same machine. For instance the following command will use a program called LAME (lame ain't an mp3 encoder) to convert a recorded audio file to an MP3 file (which might be useful for those working posting these files online).

    exten => s,1,Record(/home/sve204/converter.wav|5|30); exten => s,n,TrySystem(lame /home/sve204/converter.wav /home/sve204/converter.mp3);

    Asterisk cmd System

    Automated Outbound Calling

    Asterisk can make outbound calls without having someone call in the first place (as with the Dial command shown above). The means to do this is in the creation of a "call file", simply a text file such as follows:

    Channel: SIP/itp_jnctn/12129929344
    MaxRetries: 1
    RetryTime: 60
    WaitTime: 30
    Context: sve204_redial
    Extension: 1
    Priority: 1
    Set: variablename=variablevalue
    CallerID: Someone <2129929344>
    		
    This "call file" is simply a text file. The first line specifies the channel to use, in this case SIP/itp_jnctn and the extension, in this case, my phone number. The next line is the number of times that asterisk should try calling, the amount of times in between retries (in seconds) and the initial wait time to make the call. Next it specifies which dialplan context to put the call in along with what extension and priority to use in that context.

    There are other options available such as setting the caller id of the outbound call: CallerID: Some Name <2129929344>

    In order to tell asterisk to read and use this call file it needs to be placed into the /var/spool/asterisk/outgoing/ directory. You need to "mv" this file there instead of creating it there in the first place. Also Asterisk needs to have permission enough to delete the file when it is done with it. Here are the steps that I use on the command line after creating the call file:
    chmod 777 callfile.call
    mv callfile.call /var/spool/asterisk/outgoing/.
    
    Note above that the call file enters the call into a context, extension and priority. Here is the context that the above example is using:
    [sve204_redial]
    exten => 1,1,Monitor(wav,/home/sve204/asterisk_sounds/shawn_test,m)
    exten => 1,2,Playback(vm-goodbye)
    exten => 1,3,Hangup()
    
    Nothing special about it except that since we are going to extension 1 in the call file we need to have extension 1 defined in the context.

    Call Files for use in the future

    If you change the modified date of a call file (using the touch command as shown below) you can have asterisk make the call at that point.
     touch -t YYYYMMDDHHMM.SS filename
     touch -t 200610041320.15 callfile.call
    
    You can look at the files that are waiting to be executed with the following command:
    ls --full-time /var/spool/asterisk/outgoing/
    


    More information
  • Asterisk auto-dial out