Setting up PJSIP in Asterisk

In this article I will show examples of setting up PJSIP in Asterisk.
Since chan_sip is deprecated, I use and recommend using PJSIP.

Let’s say Asterisk is installed as I described in the article:
Installing Asterisk from source

Now let’s open the configuration file in any text editor:

nano /etc/asterisk/pjsip.conf

Examples of TRANSPORTS settings (I also left commented lines for the example):

;================================ TRANSPORTS ==
[transport-udp]
type=transport
protocol=udp    ;udp,tcp,tls,ws,wss,flow
bind=0.0.0.0

;[transport-udp-nat]
;type = transport
;protocol = udp
;bind = 0.0.0.0
; NAT settings
;local_net = 10.0.0.0/8
;external_media_address = 100.64.100.1
;external_signaling_address = 100.64.100.1

Let’s restart asterisk and check the transports:

service asterisk restart
asterisk -rvv
pjsip show transports
pjsip show transport transport-udp
exit

Let’s set up the templates:

;===============ENDPOINT TEMPLATES
 
[endpoint-basic](!)
type=endpoint
transport=transport-udp
context=from-internal
;disallow=all
;allow=ulaw
allow = !all,alaw
;for NAT
direct_media=no
force_rport=yes
rewrite_contact=yes
rtp_symmetric=yes
 
[auth-userpass](!)
type=auth
auth_type=userpass
;password=ixnfo.com
 
[aor-single-reg](!)
type=aor
max_contacts=1
qualify_frequency=15
;remove_existing=yes

In the template, I specified several settings for NAT, since the clients’ SIP phones are behind NAT routers, and the Asterisk server has a white IP address.

For example, I will add two numbers, 221 and 222, for this we will add the following to the file /etc/asterisk/pjsip.conf:

;===============EXTENSION 221
[221](endpoint-basic)
auth=auth221
aors=221
[auth221](auth-userpass)
password=ixnfo.com
username=221
[221](aor-single-reg)
 
;===============EXTENSION 222
[222]
type=endpoint
transport=transport-udp
context=from-internal
disallow=all
allow=alaw
auth=auth222
aors=222
[auth222]
type=auth
auth_type=userpass
password=ixnfo.com
username=222
[222]
type=aor
max_contacts=1

In the example, both numbers are configured the same, but in 221, the repeating lines are pulled from templates, and in number 222, they are written without templates. If there are a lot of numbers, then of course it is better to use templates to shorten the configuration and make it more readable.

Let’s look at the current settings:

pjsip show endpoints
pjsip show aors
pjsip show aor 221
pjsip show auths
pjsip show contacts

View help for settings and commands:

config show help res_pjsip
core show help pjsip

allow= (codecs used, I usually prohibit all and allow the necessary ones, not specified by default)
AoR (Address of Record) – mandatory setting for communication with endpoints, voicemail, qualify, etc.

To allow 221 and 222 to call each other, let’s set up from-internal

nano /etc/asterisk/extensions.conf

[from-internal]
;exten => _2XX,1,Macro(recording,${CALLERID(num)},${EXTEN})
exten => _2XX,n,Dial(PJSIP/${EXTEN},60,tT)
exten => _2XX,n,Hangup

This is how I recorded calls:

[macro-recording]
exten => s,1,Set(fname=${UNIQUEID}-${STRFTIME(${EPOCH},,%Y-%m-%d-%H_%M)}-${ARG1}-${ARG2});
exten => s,n,Set(monopt=nice -n 19 /usr/bin/lame -b 32  --silent "/srv/monitor/${fname}.wav"  "/srv/monitor/${fname}.mp3" && rm -f "/srv/monitor/${fname}.wav" && chmod o+r "/srv/monitor/${fname}.mp3");
exten => s,n,Set(CDR(userfield)=${fname}.mp3);
exten => s,n,MixMonitor(/srv/monitor/${fname}.wav,b,${monopt});
dialplan show

I will describe an example of setting up a trunk from a provider in another article.

pjsip show identifies
pjsip set logger on
pjsip show registrations

Some clients had problems with sound, so I disabled SIP ALG on their routers. Also, one MikroTik had many SIP phones that stopped connecting to the server after updating the firmware on MiroTik from RouterOS 6 to version 7, I solved the problem by specifying a different outgoing port on the SIP phones, for example 5060, 5061, 5062…

See also my articles:
Solution Unable to retrieve PJSIP transport in Asterisk
Adding SIP clients to Asterisk

Leave a comment

Leave a Reply