Asterisk WebRTC: A Comprehensive Guide
Web Real-Time Communication (WebRTC) enables real-time audio, video, and data sharing in web applications without plugins. This open-source technology, supported by all major browsers, provides developers with the tools to build P2P communication solutions ideal for voice and video applications.
Asterisk, a leading open-source telephony platform, can handle WebRTC signaling and media, making it a powerful choice for WebRTC solutions. By configuring Asterisk as a WebRTC-enabled SIP server, developers can enable browser-based calling and other real-time applications.
Step 1: Setting Up Asterisk for WebRTC
Required Asterisk Modules
To integrate WebRTC, Asterisk requires several modules and configuration steps:
- res_http_websocket.so: Supports WebSockets for handling WebRTC signaling.
- chan_sip.so or chan_pjsip.so: The SIP channels, with
chan_pjsip
preferred for newer installations. - res_rtp_asterisk.so: Handles Real-Time Protocol (RTP) streams.
- res_srtp.so: Provides Secure Real-Time Protocol (SRTP) encryption for secure media transport.
Basic Asterisk Configuration for WebRTC
Enabling HTTP and WebSocket Support
WebRTC uses WebSocket (wss://
) for SIP signaling, so enabling Asterisk’s HTTP server is essential:
- Edit
http.conf
:
[general]
enabled=yes
bindaddr=0.0.0.0
bindport=8088
tlsenable=yes
tlsbindaddr=0.0.0.0:8089
tlscertfile=/etc/asterisk/keys/asterisk.pem
- Generate TLS Certificates (if you haven’t already):
mkdir /etc/asterisk/keys
cd /etc/asterisk/keys
openssl req -new -x509 -days 365 -nodes -out asterisk.pem -keyout asterisk.pem
- Restart Asterisk to apply changes:
sudo systemctl restart asterisk
SIP and WebRTC Configuration
Next, configure SIP (using pjsip.conf
) to handle WebRTC clients:
[transport-wss]
type=transport
protocol=wss
bind=0.0.0.0
[transport-wss]
type=transport
protocol=wss
bind=0.0.0.0
[webrtc_user]
type=aor
contact=sip:webrtc_user@dynamic
[webrtc_user]
type=auth
auth_type=userpass
username=webrtc_user
password=your_password
[webrtc_user]
type=endpoint
context=default
disallow=all
allow=opus,ulaw,vp8
webrtc=yes
transport=transport-wss
aors=webrtc_user
Explanation of Key Sections
- Transport-WSS: Configures WebSocket over TLS (WSS) for SIP signaling.
- WebRTC User Configuration: Sets up a WebRTC-compatible SIP endpoint with DTLS encryption for media.
Step 2: WebRTC Client Setup with SIP.js
To connect to Asterisk, we’ll use SIP.js, a JavaScript library for SIP over WebSocket.
Installing SIP.js
Include SIP.js in your HTML file:
<script src="https://unpkg.com/sip.js@0.20.0/dist/sip.min.js"></script>
SIP.js Client Configuration
Create a basic SIP.js client to connect to Asterisk:
const uri = 'sip:webrtc_user@your-asterisk-domain';
const transportOptions = {
wsServers: ['wss://your-asterisk-domain:8089/ws'],
traceSip: true
};
// Initialize user agent
const userAgent = new SIP.UserAgent({
uri: SIP.UserAgent.makeURI(uri),
transportOptions: transportOptions,
authorizationUsername: 'webrtc_user',
authorizationPassword: 'your_password',
sessionDescriptionHandlerFactoryOptions: {
constraints: {
audio: true,
video: true
}
}
});
userAgent.start();
Making a Call with SIP.js
To make a call, use SIP.js to create a session and invite the destination:
function makeCall(targetUri) {
const target = SIP.UserAgent.makeURI(targetUri);
const options = {
sessionDescriptionHandlerOptions: {
constraints: {
audio: true,
video: true
}
}
};
const inviter = new SIP.Inviter(userAgent, target, options);
inviter.stateChange.addListener((state) => {
console.log(`Call state: ${state}`);
});
inviter.invite().catch(error => console.error("Call failed", error));
}
makeCall('sip:destination@your-asterisk-domain');
Receiving a Call with SIP.js
To handle incoming calls, set up an event listener for INVITE
requests:
userAgent.delegate = {
onInvite(invitation) {
invitation.stateChange.addListener((state) => {
console.log(`Incoming call state: ${state}`);
});
// Auto-answer the call
const options = {
sessionDescriptionHandlerOptions: {
constraints: {
audio: true,
video: true
}
}
};
invitation.accept(options).catch(error => console.error("Failed to answer", error));
}
};
This configuration allows the client to automatically accept incoming calls and establish media connections with audio and video.
Conclusion
By following this guide, you can configure Asterisk to work with WebRTC and set up a SIP.js client to handle WebRTC calls. Asterisk’s flexibility and SIP.js’s compatibility make this setup ideal for building real-time web applications with voice and video capabilities.
For production, consider:
- Securing WebRTC signaling with firewalls and strong authentication.
- Testing NAT traversal solutions for various network setups.
- Monitoring and optimizing call quality for high-performance deployments.
With this foundation, you can leverage WebRTC and Asterisk to develop scalable, browser-compatible communication solutions.