graph TB
subgraph "Client Layer"
WebBrowser["Web Browser\n(WebRTC Client)"]
SIPPhone["SIP Phone"]
end
subgraph "Frontend Layer"
ReactApp["React Application\n(Audio Bridge UI)"]
end
subgraph "Backend Layer"
PHPBackend["PHP Backend\n(Swoole Server)"]
JanusGW["Janus Gateway"]
Asterisk["Asterisk PBX"]
end
subgraph "Database Layer"
MySQL[(MySQL)]
Redis[(Redis)]
end
WebBrowser --> ReactApp
ReactApp --> PHPBackend
PHPBackend --> JanusGW
PHPBackend --> MySQL
PHPBackend --> Redis
SIPPhone --> Asterisk
Asterisk --> JanusGW
JanusGW --> WebBrowser
This project implements a WebRTC-SIP bridge that allows:
- WebRTC clients to join audio rooms through a web browser
- SIP clients (e.g., IP phones) to connect to WebRTC rooms
- Bidirectional audio communication between WebRTC and SIP clients
The system consists of three main components:
- Frontend (React + WebRTC)
- Backend (PHP + Swoole)
- Media Gateway (Janus Gateway + Asterisk PBX)
- PHP 8.4.3 or higher
- Node.js 20.10.0
- MySQL 8.0
- Redis 6.0
- Asterisk PBX
- Janus Gateway
- pnpm package manager
- Install required dependencies:
sudo apt-get update
sudo apt-get install -y libmicrohttpd-dev libjansson-dev libssl-dev libsrtp2-dev libsofia-sip-ua-dev libglib2.0-dev libopus-dev libogg-dev libcurl4-openssl-dev liblua5.3-dev libconfig-dev pkg-config gengetopt libtool automake- Install libnice:
git clone https://gitlab.freedesktop.org/libnice/libnice
cd libnice
./autogen.sh
./configure --prefix=/usr
make && sudo make install- Install Janus Gateway:
git clone https://github.com/meetecho/janus-gateway.git
cd janus-gateway
sh autogen.sh
./configure --prefix=/opt/janus
make
sudo make install
sudo make configs- Configure Janus:
- Copy the configuration files from the project:
sudo cp dockers/config/janus.jcfg /opt/janus/etc/janus/
sudo cp dockers/config/janus.plugin.audiobridge.jcfg /opt/janus/etc/janus/
sudo cp dockers/config/janus.plugin.sip.jcfg /opt/janus/etc/janus/- Start Janus:
/opt/janus/bin/janus- Install Asterisk:
sudo apt-get install -y asterisk- Configure Asterisk:
- Copy the configuration files from the project:
sudo cp dockers/config/asterisk/sip.conf /etc/asterisk/
sudo cp dockers/config/asterisk/extensions.conf /etc/asterisk/- Restart Asterisk:
sudo systemctl restart asterisk- Install PHP extensions:
sudo pecl install swoole
sudo echo "extension=swoole.so" >> $(php -i | grep "Loaded Configuration File" | awk '{print $5}')- Install Composer dependencies:
composer install- Configure the environment:
cp config/.env.sample config/.envEdit the .env file with your configuration:
# Application settings
APP_ENV=production
APP_DEBUG=false
APP_KEY=your_secure_key
# Database settings
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=rtp_bridge
DB_USERNAME=your_username
DB_PASSWORD=your_password
# Janus settings
JANUS_HTTP_ENDPOINT=http://127.0.0.1:8088/janus
JANUS_API_SECRET=janusrocks
# Asterisk settings
ASTERISK_HOST=127.0.0.1
ASTERISK_PORT=5038
ASTERISK_USERNAME=admin
ASTERISK_SECRET=your_secret- Run database migrations:
php database/migrate.php- Start the backend service:
php src/index.php- Install Node.js 20.10.0:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install 20.10.0
nvm use 20.10.0- Install pnpm:
npm install -g pnpm- Deploy the frontend application:
cd sample/janus-audio-bridge
pnpm install- Configure the frontend:
Create
.env.localfile:
VITE_API_BASE_URL=http://your_backend_ip:9501
VITE_JANUS_SERVER_URL=http://your_janus_ip:8088/janus- Build and start the frontend:
pnpm build
pnpm preview- Open the web application in your browser
- Navigate to the Audio Room page
- Enter a room number and join
- Grant microphone permissions when prompted
- You can now communicate with other participants in the room
- Configure your SIP phone with one of the extensions (e.g., 6001, 6002)
- To join a WebRTC room:
- Dial 9xxx (where xxx is the room number)
- Example: To join room 123, dial 9123
- You will be connected to the WebRTC room
- You can now communicate with WebRTC participants
- Navigate to the SIP Call page
- Enter the SIP extension (e.g., 6001)
- Enter the room number you want to connect to
- Click "Make Call"
- The SIP phone will receive the call and be connected to the room
-
WebRTC to WebRTC:
- Open two browser tabs
- Join the same room number
- Test audio communication
-
SIP to WebRTC:
- From a SIP phone, dial 9xxx (xxx = room number)
- Join the same room from a browser
- Test audio communication
-
WebRTC to SIP:
- Join a room from the browser
- Use the SIP Call page to call a SIP extension
- Test audio communication
-
SIP Connection Issues:
- Check Asterisk logs:
tail -f /var/log/asterisk/messages - Verify SIP registration:
asterisk -rx 'sip show peers' - Test SIP connectivity:
asterisk -rx 'sip show registry'
- Check Asterisk logs:
-
Janus Issues:
- Check Janus logs:
tail -f /var/log/janus.log - Verify WebSocket connection
- Check SIP plugin status
- Check Janus logs:
-
WebRTC Issues:
- Check browser console for errors
- Verify microphone permissions
- Check WebRTC stats in browser
-
Default Ports:
- Janus Gateway: 8088 (HTTP), 8089 (HTTPS), 8188 (Admin/Monitor)
- Asterisk: 5060 (SIP), 5038 (AMI)
- Backend: 9501 (HTTP), 9502 (WebSocket)
- Frontend: 4173 (Production), 5173 (Development)
-
Security Considerations:
- Configure firewall rules
- Use HTTPS in production
- Secure API endpoints
- Implement proper authentication
-
Production Deployment:
- Set up SSL certificates
- Configure STUN/TURN servers
- Enable logging rotation
- Set up monitoring
- Use load balancing if needed
The system handles inbound SIP calls through a sophisticated integration between Asterisk PBX and Janus WebRTC Gateway. Here's the detailed flow:
-
Call Reception
- Inbound calls are received by Asterisk PBX
- Dial pattern:
9XXX(e.g., 9123 connects to room 123) - Example: Dialing 9456 will connect to WebRTC room 456
-
Call Processing in Asterisk
[default] exten => _9XXX,1,NoOp(Forwarding to Janus WebRTC room ${EXTEN:1}) same => n,Set(ROOM_NUMBER=${EXTEN:1}) same => n,Set(CALLERID(name)=SIP-${CALLERID(num)}) same => n,Set(__SIPADDHEADER01=X-Room-Number: ${ROOM_NUMBER}) same => n,Set(__SIPADDHEADER02=X-Janus-Room: ${ROOM_NUMBER}) same => n,Dial(${JANUS_GATEWAY},30,b) -
Janus Integration
- Calls are forwarded to Janus Gateway
- Room information is passed via SIP headers
- Janus creates RTP bridge for audio streaming
- WebRTC participants receive the audio stream
-
Media Handling
- SIP audio: RTP/PCMA or PCMU
- WebRTC audio: Opus codec
- Automatic transcoding when needed
- Bidirectional audio support
-
Backend Components
JanusGateway: Manages Janus WebRTC Gateway communicationMediaManager: Handles SDP negotiation and media setupAsteriskService: Controls Asterisk through AMIPbxController: Exposes PBX control API endpoints
-
Frontend Components
JanusClient: WebRTC connection managementAudioRoom: Room UI and participant managementSipCall: SIP call control interface- Real-time audio level monitoring
-
API Endpoints
POST /api/pbx/call # Initiate SIP call GET /api/pbx/status/:ext # Get call status GET /api/pbx/channels # List active channels -
WebSocket Events
room:joined: Participant joined roomroom:left: Participant left roomcall:connected: SIP call connectedcall:ended: SIP call terminated
-
Configuration Files
janus.plugin.sip.jcfg: Janus SIP plugin configsip.conf: Asterisk SIP configurationextensions.conf: Asterisk dialplan
-
Security Measures
- SIP authentication required
- WebRTC secure signaling
- Room access control
- Rate limiting on API endpoints
-
Monitoring and Logging
- Call status tracking
- Media quality metrics
- Error logging and alerts
- Performance monitoring
-
SIP Call Testing
# Test inbound call sip-call -u 6001 -p password6001 -d 9123 # Monitor Asterisk asterisk -rx 'sip show peers' asterisk -rx 'core show channels'
-
WebRTC Testing
- Open web interface
- Create/join room 123
- Verify audio connection
- Check participant list
-
Integration Testing
- Make SIP call to room
- Verify WebRTC participants receive audio
- Test bidirectional communication
- Monitor media quality
-
Performance Validation
- Check CPU/memory usage
- Monitor network bandwidth
- Verify concurrent call handling
- Test reconnection scenarios