Asterisk WebRTC With PJSip From Scratch
Asterisk WebRTC With PJSip From Scratch
(https://vitalpbx.com)
1.- Introduction
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 1/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
WebRTC (Web Real-Time Communication) is a free, open-source, project providing web browsers and
mobile applications with real-time communications (RTC) via simple application programming interfaces
(https://vitalpbx.com)
(APIs). It allows audio and video communication to work inside web pages by allowing direct peer-to-peer
communication, eliminating the need to install plugins or download native apps. Supported by Apple,
Google, Microsoft, Mozilla, and Opera, WebRTC specifications have been published by the World Wide
Web Consortium (W3C) and the Internet Engineering Task Force (IETF).
This tutorial will walk you through configuring Asterisk to service WebRTC clients.
Create PJSIP Endpoint, AOR and Authentication objects that represent a WebRTC client.
2.- Installation
2.1.- Preparing our server
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 2/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
Now, we enable extra security for ssh access. We change the port by doing the following steps. (Optional)
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 2235
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
Uncomment the line #Port 22 and change the port to one of your preference.
Now add the new rule with the new port and restart the sshd service and log in with the new port.
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 3/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
(https://vitalpbx.com/wp-content/uploads/2021/06/VitalPBX_Asterisk_Succefully_Completed-
1024x601.png)
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 4/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
We make sure that on the codec “codec_opus” is selected. After selecting everything we need, we
(https://vitalpbx.com/wp-content/uploads/2021/06/VitalPBX_Asterisk_Menu_Select-1024x464.png)
Create a separate user and group to run asterisk services, and assign correct permissions:
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 5/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
We create a temporary website with our domain (Our example is: wrtc.new.vitalpbx.org).
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 6/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
Throughout this tutorial you should substitute the domain wrtc.new.vitalpbx.org for yours.
DocumentRoot /var/www/html/mydomain.com/public_html/
ErrorLog /var/www/html/mydomain.com/logs/error.log
CustomLog /var/www/html/mydomain.com/logs/access.log combined
</VirtualHost>
First, we have to ensure that our domain or subdomain points to the IP address of our server, for this we
Now we will proceed to install our certificate by first installing the dependencies.
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 7/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
Answer Y to proceed
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let’s Encrypt project and the non-profit organization that
develops Certbot? We’d like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
(Y)es/(N)o:
Answer Y to proceed
At the end we see the following message confirming that all is correct.
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 8/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
Account registered.
Requesting a certificate for mydomain.com
Performing the following challenges:
(https://vitalpbx.com)
http-01 challenge for mydomain.com
Waiting for verification…
Cleaning up challenges
Created an SSL vhost at /etc/httpd/conf.d/mydomain.com-le-ssl.conf
Deploying Certificate to VirtualHost /etc/httpd/conf.d/ mydomain.com-le-ssl.conf
Redirecting vhost in /etc/httpd/conf.d/mydomain.com.conf to ssl vhost in /etc/httpd/conf.d/
mydomain.com-le-ssl.conf
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Congratulations! You have successfully enabled https://mydomain.com
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Subscribe to the EFF mailing list (email: [email protected]).
Starting new HTTPS connection (1): supporters.eff.org
We were unable to subscribe you the EFF mailing list because your e-mail address appears to be
invalid. You can try again later by visiting https://act.eff.org.
IMPORTANT NOTES:
– Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/mydomain.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/mydomain.com/privkey.pem
Your certificate will expire on 2021-09-14. To obtain a new or
tweaked version of this certificate in the future, simply run
certbot again with the “certonly” option. To non-interactively
renew *all* of your certificates, run “certbot renew”
– If you like Certbot, please consider supporting our work by:
[root@localhost ~]#
We write down the path of both certificates, since we are going to use them later.
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 9/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
Set Up Auto-Renewal
Now that certbot is installed and working, we need to have it check for expiring certificates automatically.
Press Insert
In this instance, I’ve added a cron to our example server that looks like this:
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 10/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
This cron will, at 3:45 AM every Saturday, run the certbot renew function to renew any already-installed
certificates that are due to expire, and then reload the Apache configuration. Save the crontab after you
You should have a working chan_pjsip based Asterisk installation. Either install Asterisk from your
distribution’s packages or, preferably, install Asterisk from source. Either way, there are a few modules
over and above the standard ones that must be present for WebSockets and WebRTC to work:
res_crypto
res_http_websocket
res_pjsip_transport_websocket
We recommend installing Asterisk from source because it’s easy to make sure these modules are built
and installed.
2.5.1.- Certificates
Technically, a client can use WebRTC over an insecure WebSocket to connect to Asterisk. In practice
though, most browsers will require a TLS based WebSocket to be used. You can use self-signed
certificates to set up the Asterisk TLS server but getting browsers to accept them is tricky. So if you’re able,
As the objective of this presentation is not to teach how to install a certificate with LetsEncrypt, we
https://www.tecmint.com/install-lets-encrypt-ssl-certificate-to-secure-apache-on-rhel-centos/
(https://www.tecmint.com/install-lets-encrypt-ssl-certificate-to-secure-apache-on-rhel-centos/)
To communicate with WebSocket clients, Asterisk uses its built-in HTTP server. So we configure the
following:
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 11/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
Although the HTTP server does the heavy lifting for WebSockets, we still need to define a basic PJSIP
Transport:
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 12/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
[global]
type=global
max_initial_qualify_time=0
keep_alive_interval=90
contact_expiration_check_interval=30
default_voicemail_extension=*97
unidentified_request_count=3
unidentified_request_period=5
unidentified_request_prune_interval=30
mwi_tps_queue_high=500
mwi_tps_queue_low=-1
mwi_disable_initial_unsolicited=yes
send_contact_status_on_update_registration=yes
[transport-wss]
type=transport
protocol=wss
bind=0.0.0.0:8089
local_net=10.10.0.9/16
local_net=10.116.0.6/20
external_media_address=178.128.149.185
external_signaling_address=178.128.149.185
allow_reload=yes
We now need to create the basic PJSIP objects that represent the client. In this example, we’ll call the
client webrtc_client, but you can use any name you like, such as an extension number. Only the minimum
options needed for a working configuration are shown. NOTE: It’s normal for multiple objects in pjsip.conf
to have the same name as long as the types differ. Add the following content to the end of the file.
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 13/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
[webrtc-phones](!)
(https://vitalpbx.com)
context=main-context
transport=transport-wss
allow=!all,opus,ulaw,alaw,vp8,vp9
webrtc=yes
[User1](webrtc-phones)
type=endpoint
callerid=”User One” <100>
auth=User1
aors=User1
[User1]
type=aor
max_contacts=3
[User1]
type=auth
auth_type=userpass
username=User1
password=1234
[User2](webrtc-phones)
type=endpoint
callerid=”User Two” <101>
auth=User2
aors=User2
[User2]
type=aor
max_contacts=3
[User2]
type=auth
auth_type=userpass
username=User2
password=1234
[User3](webrtc-phones)
type=endpoint
callerid=”User Three” <102>
auth=User3
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 14/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
aors=User3
[User3]
type=aor (https://vitalpbx.com)
max_contacts=3
[User3]
type=auth
auth_type=userpass
username=User3
password=1234
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 15/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
[globals]
ATTENDED_TRANSFER_COMPLETE_SOUND=beep
[main-context]
include => from-extensions
include => subscriptions
include => textmessages
include => echo-test
include => speak-exte-nnum
[echo-test]
exten => 777,1,NoOp(FEATURE: ECHO TEST)
same => n,Answer
same => n,Wait(1)
same => n,Playback(demo-echotest)
same => n,Echo()
same => n,Playback(demo-echodone)
same => n,Hangup()
;END of [echo-test]
[speak-exte-nnum]
exten => 888,1,NoOp(FEATURE: SPEAK MY EXTENSION NUMBER)
same => n,Answer
same => n,Wait(1)
same => n,Playback(extension)
same => n,Wait(1)
same => n,SayDigits(${CALLERID(num)})
same => n,Wait(2)
same => n,Hangup()
;END of [speak-exte-nnum]
[textmessages]
exten => 100,1,Gosub(send-text,s,1,(User1))
exten => 101,1,Gosub(send-text,s,1,(User2))
exten => 102,1,Gosub(send-text,s,1,(User3))
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 16/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
[subscriptions]
exten => 100,hint,PJSIP/User1
exten => 101,hint,PJSIP/User2
exten => 102,hint,PJSIP/User3
(https://vitalpbx.com)
[from-extensions]
; Feature Codes:
exten => *65,1,Gosub(moh,s,1)
; Extensions
exten => 100,1,Gosub(dial-extension,s,1,(User1))
exten => 101,1,Gosub(dial-extension,s,1,(User2))
exten => 102,1,Gosub(dial-extension,s,1,(User3))
[moh]
exten => s,1,NoOp(Music On Hold)
exten => s,n,Ringing()
exten => s,n,Wait(2)
exten => s,n,Answer()
exten => s,n,Wait(1)
exten => s,n,MusicOnHold()
[dial-extension]
exten => s,1,NoOp(Calling: ${ARG1})
exten => s,n,Set(JITTERBUFFER(adaptive)=default)
exten => s,n,Dial(PJSIP/${ARG1},30)
exten => s,n,Hangup()
[send-text]
exten => s,1,NoOp(Sending Text To: ${ARG1})
exten => s,n,Set(PEER=${CUT(CUT(CUT(MESSAGE(from),@,1),<,2),:,2)})
exten => s,n,Set(FROM=${SHELL(asterisk -rx ‘pjsip show endpoint ${PEER}’ | grep ‘callerid ‘ |
cut -d’:’ -f2- | sed ‘s/^ *//’ | tr -d ‘
‘)})
exten => s,n,Set(CALLERID_NUM=${CUT(CUT(FROM,>,1),<,2)})
exten => s,n,Set(FROM_SIP=${STRREPLACE(MESSAGE(from),
exten => s,n,MessageSend(pjsip:${ARG1},${FROM_SIP})
exten => s,n,Hangup()
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 17/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
(https://vitalpbx.com)
Restart Asterisk to pick up the changes and if you have a firewall, don’t forget to allow TCP port 8089
Enabled URI’s:
/httpstatus => Asterisk HTTP General Status
/phoneprov/… => Asterisk HTTP Phone Provisioning Tool
/metrics/… => Prometheus Metrics URI
/ari/… => Asterisk RESTful API
/ws => Asterisk HTTP WebSocket
Enabled Redirects:
None.
2.5.6.- Wrap Up
At this point, your WebRTC client should be able to register and make calls. If you’ve used self-signed
certificates however, your browser may not allow the connection and because the attempt is not from a
normal URI supplied by the user, the user might not even be notified that there’s an issue. You may be
able to get the browser to accept the certificate by visiting “https://pbx.example.com:8089/ws” directly.
This will usually result in a warning from the browser and may give you the opportunity to accept the self-
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 18/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
signed certificate and/or create an exception. If you generated your certificate from a pre-existing local
Certificate Authority, you could also import that Certificate Authority’s certificate into your trusted store
(https://vitalpbx.com)
but that procedure is beyond the scope of this document.
**Information Source:
https://wiki.asterisk.org/wiki/display/AST/Configuring+Asterisk+for+WebRTC+Clients
This web application is designed to work with Asterisk PBX (v13, v16 & v18). Once loaded application will
connect to Asterisk PBX on its web socket, and register an extension. Calls are made between contacts,
and a full call detail is saved. Audio Calls can be recorded. Video Calls can be recorded, and can be saved
with 5 different recording layouts and 3 different quality settings. This application does not use any cloud
systems or services, and is designed to be stand-alone. Additional libraries will be downloaded at run time
(but can also be saved to the web server for a complete off-line solution).
Asterisk PBX version 13|16|17|18 (with Websockets and Text Messaging, chan_sip or chan_pjsip).
[root@localhost ~] cd /var/www/html/mydomain.com/public_html/
[root@localhost ~] git clone https://github.com/InnovateAsterisk/Browser-Phone.git
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 19/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
https://mydomain/Browser-Phone/Phone/ (https://mydomain/Browser-Phone/Phone/)
(https://vitalpbx.com)
We enter the following data and we give it Save. Make sure to write all the data including the /ws value in
WebSocket Path.
(https://vitalpbx.com/wp-
content/uploads/2021/06/VitalPBX_Asterisk_WebRTC_Browser_Phone_Account.png)
After pressing Save, your registered account should appear at the top left.
Below we show an image of how extension 100 registered with a call in progress should look like.
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 20/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
(https://vitalpbx.com)
(https://vitalpbx.com/wp-content/uploads/2021/06/VitalPBX_Asterisk_WebRTC_Browse_Phone_Gui-
1024x573.png)
NEWS HOW-TO
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 21/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
HOW-TO
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 22/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
(https://vitalpbx.com)
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 23/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
(https://vitalpbx.com)
Contact Info
2292 NW 82nd Ave HB 002998 Miami, Florida 33198.
VitalPBX provides a robust and scalable platform, which will allow you to
manage your PBX in an easy and intuitive way.
Add-ons Support
Multi Tenant PBX System(https://vitalpbx.com/multi-tenant-pbx/) Professional Support (https://vitalpbx.com/product/professional-
Packages support-packages/)
Rebranding Module(https://vitalpbx.com/rebranding-module/)
Forum(https://forums.vitalpbx.org/)
Sonata Recording(https://vitalpbx.com/sonata-recordings/)
F.A.Q's(https://forums.vitalpbx.org/c/faq/14)
Sonata Switchboard(https://vitalpbx.com/sonata-switchboard/)
Sonata Stats(https://vitalpbx.com/sonata-stats/)
Manuals(https://wiki.vitalpbx.com/) (h
tt
VitalPBX Changes (https://vitalpbx.com/vitalpbx-phone-system-change-
p
VitXi WebRTC - (https://vitalpbx.com/vitxi-webrtc-softphone-client- (h
Log log/) s: (
Softphone for-vitalpbx/) tt (h
// t
p tt
Contact (https://vitalpbx.com/unified-communications-system-contact-
w
s: p
Us us/) (h w ( s
// tt w. s: m /
w //
p li (h ai w
w w
s: n tt lt w
w. w
// k p o: w
fa w.
t e s: sa i
c y
w di // le s
e o
itt n. t. s
b er c ut m @ g
Copyright @ 2023 VitalPBX, All rights reserved. o .c o u e vi
o b
o m /v ta m
k. e.
m /c it lp .
c c
/v o al b o
o o
it m p x. m
m m
al p b c /
/v p a /v x) o
it it
b n m a
al al
x) y/ )
p p
vi
b b
ta x
x/ x)
lp
)
b
x/
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 24/25
13/4/23, 16:36 Asterisk WebRTC With PJSip From Scratch
x/
)
https://vitalpbx.com/blog/asterisk-webrtc-from-scratch/ 25/25