Asterisk WebRTC With PJSip From Scratch - VitalPBX - Advanced PBX System
Asterisk WebRTC With PJSip From Scratch - VitalPBX - Advanced PBX System
Asterisk WebRTC With PJSip From Scratch - VitalPBX - Advanced PBX System
1.- Introduction
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
(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
First, we update our CentOS 7 installation and install some dependencies.
https://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch/ 1/10
05/10/2021 23:50 Asterisk WebRTC with PJSip from Scratch | VitalPBX - Advanced PBX System
[root@localhost ~]# nano /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# 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.
We make sure that on the codec “codec_opus” is selected. After selecting everything we need, we
https://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch/ 2/10
05/10/2021 23:50 Asterisk WebRTC with PJSip from Scratch | VitalPBX - Advanced PBX System
[root@localhost asterisk-18.6.0]# groupadd asterisk
[root@localhost asterisk-18.6.0]# useradd -r -d /var/lib/asterisk -g asterisk asterisk
[root@localhost asterisk-18.6.0]# usermod -aG audio,dialout asterisk
[root@localhost asterisk-18.6.0]# chown asterisk. -R /etc/asterisk
[root@localhost asterisk-18.6.0]# chown asterisk. -R /var/{lib,log,spool}/asterisk
[root@localhost asterisk-18.4.0]# chown -R asterisk.asterisk /usr/lib64/asterisk
Set Asterisk default user to asterisk:
Throughout this tutorial you should substitute the domain wrtc.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>
We restart our Apache
Now we will proceed to install our certificate by first installing the dependencies.
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
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://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch/ 3/10
05/10/2021 23:50 Asterisk WebRTC with PJSip from Scratch | VitalPBX - Advanced PBX System
Account registered.
Requesting a certificate for mydomain.com
Performing the following challenges:
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.
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:
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
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
https://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch/ 4/10
05/10/2021 23:50 Asterisk WebRTC with PJSip from Scratch | VitalPBX - Advanced PBX System
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/
To communicate with WebSocket clients, Asterisk uses its built-in HTTP server. So we configure the
following:
Although the HTTP server does the heavy lifting for WebSockets, we still need to define a basic PJSIP
Transport:
[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
Replace local_net, external_media_address and external_signaling_address with their respective IPs.
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://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch/ 5/10
05/10/2021 23:50 Asterisk WebRTC with PJSip from Scratch | VitalPBX - Advanced PBX System
[root@localhost ~] nano /etc/asterisk/pjsip.conf
[webrtc-phones](!)
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
aors=User3
[User3]
type=aor
max_contacts=3
[User3]
type=auth
auth_type=userpass
username=User3
password=1234
We change the owner of the file to be asterisk
https://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch/ 6/10
05/10/2021 23:50 Asterisk WebRTC with PJSip from Scratch | VitalPBX - Advanced PBX System
[root@localhost ~] mv /etc/asterisk/extensions.conf /etc/asterisk/extensions.conf.bak
[root@localhost ~] nano /etc/asterisk/extensions.conf
[general]
static=yes
writeprotect=yes
priorityjumping=no
autofallthrough=no
[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))
[subscriptions]
exten => 100,hint,PJSIP/User1
exten => 101,hint,PJSIP/User2
exten => 102,hint,PJSIP/User3
[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()
We change the owner of the file to be asterisk
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
https://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch/ 7/10
05/10/2021 23:50 Asterisk WebRTC with PJSip from Scratch | VitalPBX - Advanced PBX System
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-
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
**Information Source:
https://wiki.asterisk.org/wiki/display/AST/Configuring+Asterisk+for+WebRTC+Clients
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).
[root@localhost ~] cd /var/www/html/mydomain.com/public_html/
[root@localhost ~] git clone https://github.com/InnovateAsterisk/Browser-Phone.git
We enter the following data and we give it Save. Make sure to write all the data including the /ws value in
WebSocket Path.
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://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch/ 8/10
05/10/2021 23:50 Asterisk WebRTC with PJSip from Scratch | VitalPBX - Advanced PBX System
Asterisk WebRTC con PJSip desde Cero dSIPRouter (Kamailio) With VitalPBX
1.- Introducción WebRTC (Web Real-Time Communication) 1.- Introduction After a long research on the internet,
es un proyecto gratuito de código abierto que proporciona googling everywhere. We decided to make a manual on how
navegadores web y aplicaciones móviles con to install Kamailio together with its
comunicaciones en tiempo real
READ MORE »
READ MORE »
INTEGRATIONS
READ MORE »
2 Comments
Arshad Ahmad
July 16, 2021 at 1:50 pm
Thanks, I have VitalPBX 3.0.9-5 and Asterisk 18.3.0-1, can i use it on existing server or i have to follow all steps as
mentioned.
https://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch/ 9/10
05/10/2021 23:50 Asterisk WebRTC with PJSip from Scratch | VitalPBX - Advanced PBX System
Log in to Reply Link
Add a Comment
You must be logged in to post a comment.
SUBSCRIBE
Subscribe to our newsletter to receive updates and the latest news from VitalPBX
Name Email
https://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch/ 10/10