Tuesday, December 22, 2015

Asterisk Installation and Configuration

Asterisk Installation

  • Asterisk installation and configuration can be vary depend on OS and various distributions of the same OS type. This guide is created considering the linux release Fedora 22.
  • Following cmd operations should be done as the root. (sudo su -)



  • Change the current directory of the terminal to a suitable installation location.               eg:- cd /usr/local/src

  • Extract the downloaded tar ball.                                                                                          tar -zxvf asterisk-13-current.tar.gz

  • Extracted content is the source of particular asterisk version. Those sources should be compiled and then installed.

  • Before starting the installation of asterisk, there can be several dependencies to be installed to the system. Downloaded asterisk installation kit is included a shell script which find and install required dependencies considering the existing linux distribution.   cd /usr/local/src/asterisk-13.6.0/contrib/scripts
  • ./install_prereq
  • (There can be some packages which are not available for system’s linux distribution. If failure of a package installation, dependency installation will be stopped from that point. Most of times unavailable packages are optional for the installation of asterisk. So, to continue the process of dependency installation, remove the unavailable package name from the install_prereq script and start again)

  • Now time to compile and install the asterisk.
    • cd /usr/local/src/asterisk-13.6.0
    • ./configure
    • make
    • make install
    • If the above steps are executed without errors, we can assume that installation is successful. To confirm the setup start the asterisk server and goto the asterisk CLI as follows.
    • asterisk
    • asterisk -r
    • (If you log to the asterisk CLI, asterisk installation is success)


sip.conf Configuration
  • Backup the original /etc/asterisk/sip.conf
  • Create a new sip.conf and put following content for the testing purpose.

[general]
context=default

[6001]
type=friend
context=from-internal
host=dynamic
secret=unsecurepassword
disallow=all
allow=ulaw

  • sip.conf mention about the sip clients’ scope inside the asterisk. (SIP client can be any SIP supported IP phone connection. For testing purposes our SIP client is a softphone like linphone)


extension.conf Configuration

  • Backup the original /etc/asterisk/extensions.conf
  • Create a new extension.conf and put following content for the testing purpose.

[default]
exten => 3500, 1, Playback(hello-world)
exten => 3500, n, Hangup()


[from-internal]
exten => 1400, 1, Answer(500)
exten => 1400, n, Playback(hello-world)
exten => 1400, n, Hangup()

  • extension.conf is the place where we can write dial-plans (in other terms call flows) in asterisk. Above simplest call flow plays a prompt and hangup when made a call to shortcode 3500.
    Refer http://cdn.oreilly.com/books/9780596510480.pdf chapter 5 and chapter 6 for dial-plan developments.

  • After applying any change to above conf files, those confs should be reloaded using the asterisk CLI commands.
eg:- If you need to apply a change done to extension.conf
asterisk -r (Go into asterisk CLI)
dialplan reload (Reload extension.conf)

  • If you need to apply a change to sip.conf
asterisk -r  (Go into asterisk CLI)
sip reload  (Reload sip.conf)

Linphone Installation and configuration
  • For the testing purposes we need to install a softphone.
  • There are several softphones including commercial products. Here we use linphone which is a freely available application. RPM can be found from following site https://www.rpmfind.net/linux/rpm2html/search.php?query=linphone .
  • Installation and Configuration details can be found from following site.
  • Goto Options => Preferences => Network settings
  • Check true Send DTMFs as SIP info (To enable DTMF sending option)
  • Select     SIP(UDP) port 5061 (or other unused port. 5060 is the default port for SIP transmission. Asterisk use 5060 as default for its SIP transmission) as the Network protocol and ports.
  • User accounts should be created in linphone.
  • Goto Options => Preferences => Manage SIP Accounts
  • Linphone provides two kind of account types as Default identity and Proxy
  • Proxy account can be used for worldwide calling
  • Default identity account can be created for internal network calling
  • Considering the sip.conf of our asterisk setup we are going to create a default account with username 6001.
  • All the calls that make with username 6001 will be directed only to the extensions(shortcodes) which are under context from-internal according to the context.conf
  • Other user accounts with any other usernames will be only directed to the extensions(shortcodes) under the context of default.


Festival Installation and configuration
  • Complete reference for festival installation and configuration

  • Festival is a freely available Text-to-Speech engine that can be integrated with Asterisk system.


  • Move the downloaded contents to a suitable installation directory
eg:- /usr/local/src

  • Let’s make and install speech-tools application
    • Extract speech_tools-X.x-release.tar
    • ./configure
    • make
    • make test
    • make install

  • Let’s make and install festival application
    • Extract festival-X.x-release.tar
    • ./configure
    • make
    • make install

  • Let’s make and install festvox application
    • Extract festvox-2.7.0-release.tar.gz
    • ./configure
    • make

  • Move festlex_CMU.tar.gz, festlex_OALD.tar.gz, festlex_POSLEX.tar.gz tarballs to exactly where the festival package located (eg:-  /usr/local/src/festival) and extract them.(tarball content will be located inside the festival package as patch  contents)

  • Now festival installation is completed. To start the festival application go to bin dir in festival package (eg:- cd /usr/local/src/festival/bin) and call startup script as ./festival
  • If we are in the festival cmd, installation is success.

[festival commands should be covered with brackets since it has lisp syntax style]
  • Execute (SayText “This is the content for text-to-speech") to hear text-to-speech output from festival.
  • Available voices can be list by executing (voice.list)
  • Voices can be changed executing (voice_available voices name)
  • To quit the festival execute (quit)

  • To configure festival with asterisk add following content to festival/lib/festival.scm

;; Enable access to localhost (needed by debian users)
(set! server_access_list '("localhost\\.localdomain" "localhost"))

;; set italian voice (comment the following 2 lines to use british_american)
;(language_italian)
;;; You can set available voices for TTS via asterisk as follows (remove ; to uncomment)
;(set! voice_default 'voice_pc_diphone)
;(set! voice_default 'voice_rab_diphone)

;;; Command for Asterisk begin
(define (tts_textasterisk string mode)
"(tts_textasterisk STRING MODE)
Apply tts to STRING. This function is specifically designed for
use in server mode so a single function call may synthesize the string.
This function name may be added to the server safe functions."
(let ((wholeutt (utt.synth (eval (list 'Utterance 'Text string)))))
(utt.wave.resample wholeutt 8000)
(utt.wave.rescale wholeutt 5)
(utt.send.wave.client wholeutt)))

;;; Command for Asterisk end

  • Then start festival as follows
cd /usr/local/src/festival/bin
./festival --server

  • Go to the asterisk CLI and type ‘core show application Festival’ command if a content is available asterisk can access the festival application.
  • Festival can be accessed from dial-plans as follows.
eg :-
exten => 2500, 1, Festival('Hi ! Festival‘')






How to integrate Asterisk with Java
  • Asterisk provide an interface for other applications which are developed with other technologies to interact with it through the AGI (Asterisk Gateway Interface) scripts. AGI scripts can be developed with several technologies including PHP, Java, Pascal, Perl, C, .NET etc.
  • Here we use AGI  scripting with Java
  • Following is the maven dependency for asterisk-java library
<dependency>
 <groupId>org.asteriskjava</groupId>
 <artifactId>asterisk-java</artifactId>
 <version>1.0.0.M3</version>

 <exclusions>
     <exclusion>
         <groupId>com.sun.jmx</groupId>
         <artifactId>jmxri</artifactId>
     </exclusion>
     <exclusion>
         <groupId>com.sun.jdmk</groupId>
         <artifactId>jmxtools</artifactId>
     </exclusion>
     <exclusion>
         <groupId>javax.jms</groupId>
         <artifactId>jms</artifactId>
     </exclusion>
 </exclusions>
</dependency>


  • Any java class which is extended by BaseAgiScript class will be available with methods for AGI scripting


  • asterisk-java AGI scripting server uses fastagi-mapping.properties file to map BaseAgiScript extended class with agi script name.
  • fastagi-mapping.properties content includs entries as follows.
asteriskApi.agi = com.hms.quiz.ivr.main.AsteriskAPI
(AsteriskAPI is the class which is extended the BaseAgiScript class)
  • Agi script is linked to the asterisk through extension.conf as follows.
exten => 2500,1,Agi(agi://localhost/asteriskApi.agi)
  • By Executing asterisk-java-1.0.0.M3.jar asterisk-java AGI server can be started.
  • If we consider our class path as com.hms.quiz.ivr.main.AsteriskAPI, asterisk-java-1.0.0.M3.jar and fastagi-mapping.properties should be located in the place where com directory located.
  • If any external jar files are imported, add them to the classpath when executing the asterisk-java jar as follows
java -classpath asterisk-java-1.0.0.M3.jar:/home/udara/.m2/repository/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar:. org.asteriskjava.fastagi.DefaultAgiServer
/home/udara/.m2/repository/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar is the path where externally imported mysql-connector jar located.


How to configure a SIP trunk with asterisk
  • SIP trunk account should be retrieved from a VoIP provider.
  • As an example we are going to configure linphone free SIP service account with Asterisk.
  • Our account’s
    • user name = smuc
    • Password  = beyondm123


Eg:- conf files

sip.conf
[general]
context=default

; Register and get calls from Foo Provider, to our number 1-555-455-1337
register => smuc:beyondm123@sip.linphone.org

[smuc]
type=friend
secret=beyondm123
username=smuc
host=sip.linphone.org
canreinvite=no
disallow=all
allow=ulaw
allow=alaw
allow=gsm
insecure=port,invite
fromdomain=sip.linphone.org
context=trunk_context

extension.conf

[trunk_context]
exten => s,1,Agi(agi://localhost/asteriskApi.agi)
exten => s, n, Festival('Press 9 to go for another question.')
exten => s, n, Festival('Press 0 to end the quiz.')
exten => s, n, Read(DIGITTO,beep,1)
exten => s,n,GotoIf($[${DIGITTO} = 9]?continueQuiz:endQuiz)
exten => s,n(continueQuiz),Goto(from-internal,2300,1)
exten => s,n, Hangup()
exten => s,n(endQuiz),Playback(vm-goodbye)
exten => s,n, Hangup()

To check if the Asterisk is successfully registered in the SIP trunk account, execute following command in Asterisk CLI interface.
sip show registry


Asterisk BlackList process
  • Insert the caller ID to the Asterisk blacklist table by entering to the Asterisk CLI.
    • database put blacklist 6001 "Blacklisted for testing"

  • Blacklisted numbers can be removed as follows
    • database del blacklist 6001

  • Dial plan logic can be implemented as follows
exten => 1100,1,GotoIf(${BLACKLIST()}?blacklisted:non_blacklisted)
exten => 1100,n(non_blacklisted),Dial(SIP/2300@192.168.1.106)
exten => 1100,n(blacklisted),Hangup()

  • If you get a error message like “chan_sip.c:19294 handle_response_invite: Failed to authenticate on INVITE to '"SPA2102 L2" <sip:101@192.168.8.1>;tag=as226d3630'”
    • set insecure=invite in the [general] section in sip.conf