Probleme de NAT sur webrtc et installation de STUN

🔧 Problème RTP et NAT – Solution complète

🔍 Analyse du problème

Vous avez raison ! Le problème est typique des environnements NAT/Firewall :

  • SIP passe (signalisation)
  • RTP est bloqué (média/audio)
  • Les paquets RTP n’atteignent pas le client WebRTC

🌐 Solution 1 : Configuration STUN/TURN

1. Installer un serveur STUN/TURN (coturn)

# Installation
yum install coturn -y
# ou
apt-get install coturn -y

2. Configuration coturn

nano /etc/turnserver.conf

Configuration minimale :

# Interfaces d’écoute
listening-port=3478
tls-listening-port=5349
listening-ip=0.0.0.0
# IP externe (votre IP publique)
external-ip=VOTRE_IP_PUBLIQUE
# Domaine
realm=crm.oclasoft.com
server-name=crm.oclasoft.com
# Base de données utilisateurs
userdb=/var/lib/turn/turndb
lt-cred-mech
# Logs
log-file=/var/log/turn/turn.log
verbose
# Sécurité
no-multicast-peers
no-cli

3. Créer un utilisateur TURN

turnadmin -a -u webrtc -r crm.oclasoft.com -p votre_mot_de_passe_turn

4. Démarrer coturn

systemctl enable coturn
systemctl start coturn
# Vérifier le statut
systemctl status coturn

⚙️ Solution 2 : Configuration FreePBX pour WebRTC

1. Configuration des adresses IP externes

Settings → Asterisk SIP Settings → SIP Settings [chan_pjsip] → Advanced

External Media Address: VOTRE_IP_PUBLIQUE
External Signaling Address: VOTRE_IP_PUBLIQUE
External Signaling Port: 5060
Local Network: 192.168.0.0/16,172.16.0.0/12,10.0.0.0/8

2. Configuration RTP

Settings → Asterisk RTP Settings

RTP Start: 10000
RTP End: 20000
STUN Server: stun:crm.oclasoft.com:3478
ICE Support: Yes

3. Configuration manuelle dans rtp.conf

nano /etc/asterisk/rtp.conf
[general]
; Plage RTP
rtpstart=10000
rtpend=20000
; Configuration ICE/STUN
icesupport=yes
stunaddr=crm.oclasoft.com:3478
; NAT
strictrtp=yes

4. Configuration PJSIP pour ICE

nano /etc/asterisk/pjsip.conf

Ajouter dans la section générale :

[global]
type=global
; Configuration ICE
ice_support=yes

Configuration des endpoints WebRTC :

[webrtc_template](!)
type=endpoint
; … configuration existante …
; ICE/STUN/RTP
ice_support=yes
media_use_received_transport=yes
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes
; Configuration NAT
direct_media=no
trust_id_outbound=yes

🔥 Solution 3 : Configuration Firewall

1. Ouvrir les ports RTP

# Firewall-cmd
firewall-cmd –permanent –add-port=10000-20000/udp
firewall-cmd –permanent –add-port=3478/udp
firewall-cmd –permanent –add-port=5349/tcp
firewall-cmd –reload
# Ou iptables
iptables -A INPUT -p udp –dport 10000:20000 -j ACCEPT
iptables -A INPUT -p udp –dport 3478 -j ACCEPT
iptables -A INPUT -p tcp –dport 5349 -j ACCEPT

2. Configuration NAT sur le routeur

Redirection de ports nécessaires :

Port 5060 (UDP) → IP_FREEPBX:5060
Port 8088 (TCP) → IP_FREEPBX:8088  
Port 8089 (TCP) → IP_FREEPBX:8089
Ports 10000-20000 (UDP) → IP_FREEPBX:10000-20000
Port 3478 (UDP) → IP_FREEPBX:3478

🧪 Solution 4 : Configuration côté client WebRTC

Configuration STUN/TURN dans le client

const iceServers = [
{
urls: [‘stun:crm.oclasoft.com:3478’]
},
{
urls: [‘turn:crm.oclasoft.com:3478’],
username: ‘webrtc’,
credential: ‘votre_mot_de_passe_turn’
}
];
const pcConfig = {
iceServers: iceServers,
iceCandidatePoolSize: 10
};

✅ Tests et vérification

1. Tester le serveur STUN

# Test STUN basique
stun crm.oclasoft.com 3478

2. Vérifier les ports RTP utilisés

# Pendant un appel WebRTC actif
asterisk -rx « rtp set debug on »
netstat -aunp | grep asterisk

3. Test des candidats ICE

Dans la console du navigateur (F12) :

pc.onicecandidate = function(event) {
if (event.candidate) {
console.log(‘ICE Candidate:’, event.candidate);
}
};

4. Vérifier la connectivité RTP

# Capture des paquets RTP
tcpdump -i any -n port 10000 or portrange 10000-20000

🔧 Configuration alternative : Direct RTP

Si STUN/TURN n’est pas possible :

nano /etc/asterisk/pjsip.conf
[webrtc_endpoint](!)
type=endpoint
; … configuration existante …
; Forcer RTP direct
direct_media=yes
direct_media_method=invite
rtp_symmetric=yes
force_rport=yes
media_use_received_transport=yes

📋 Checklist de dépannage

  1. ✅ Ports firewall ouverts (10000-20000/UDP, 3478/UDP)
  2. ✅ Configuration NAT routeur (redirection ports)
  3. ✅ Serveur STUN/TURN fonctionnel
  4. ✅ Configuration ICE dans FreePBX
  5. ✅ Configuration client WebRTC avec STUN/TURN
  6. ✅ IP externe configurée dans FreePBX

🎯 Test final

Après configuration complète :

# Redémarrer les services
systemctl restart asterisk
systemctl restart coturn
# Tester un appel WebRTC
asterisk -rx « rtp set debug on »
# Effectuer l’appel et vérifier les logs RTP

Cette configuration devrait résoudre le problème d’audio ! 🎉