Asterisk PJSIP: SIP and Audio Codecs (Latest Versions)
Updated guide for modern Asterisk (PJSIP era): sip and audio codecs with real configs, common mistakes, and troubleshooting steps.
SIP controls call signaling, but the actual voice quality in Asterisk depends on audio codecs.
Many real-world call problems — poor audio, one-way sound, robotic voice, or failed calls — are caused not by SIP itself, but by codec negotiation and media handling.
This guide explains how SIP and audio codecs work together in modern Asterisk (PJSIP), including practical troubleshooting knowledge used in production systems.
What Is an Audio Codec?
A codec is the method used to encode and decode voice audio for transmission over IP networks.
- Microphone captures raw audio
- Codec compresses it into digital packets
- Packets travel via RTP
- Receiver decodes and plays sound
Different codecs balance: quality, bandwidth usage, and CPU load.
How SIP Chooses a Codec
Codec negotiation happens inside the SDP section of SIP messages (INVITE and 200 OK).
- Caller sends a list of supported codecs
- Receiver selects one common codec
- RTP audio begins using the chosen codec
If there is no common codec, the call may fail or connect without audio.
Most Common Codecs in Asterisk Systems
G.711 (PCMU / PCMA)
- Standard telephony codec
- Very low compression → high quality
- Uses more bandwidth (~64 kbps per direction)
- Required by many SIP trunk providers
G.729
- Highly compressed (~8 kbps)
- Lower bandwidth usage
- May require commercial licensing
- Slightly reduced audio quality
Opus
- Modern high-quality codec
- Adaptive bitrate and excellent clarity
- Common in WebRTC/browser calling
- Not always supported by SIP trunks
GSM / iLBC / Speex
- Legacy or niche codecs
- Used in specific environments
- Rare in modern carrier trunks
Bandwidth Comparison (Approximate)
- G.711 → ~80–90 kbps with RTP overhead
- G.729 → ~24–32 kbps
- Opus → ~16–64 kbps (adaptive)
Call centers with many simultaneous calls must consider total bandwidth consumption.
Where Codecs Are Configured in PJSIP
Codecs are usually defined per endpoint or trunk:
allow=opus,ulaw,alaw
disallow=all
Best practice:
- Disallow everything first
- Explicitly allow only required codecs
Codec Mismatch Problems in Real Systems
Call Connects but No Audio
- No shared codec between endpoints
- Transcoding failure
Robotic or Distorted Voice
- Network packet loss or jitter
- Low-bitrate codec under poor conditions
High CPU Usage on Asterisk
- Heavy transcoding between codecs
- Multiple simultaneous conversions
What Is Transcoding (and Why It Matters)
Transcoding happens when:
- Caller uses one codec (e.g., Opus)
- Provider requires another (e.g., G.711)
- Asterisk converts audio between them
This increases:
- CPU usage
- Latency
- Risk of audio degradation
Best practice: avoid transcoding when possible.
Recommended Codec Strategy for Modern Deployments
Internal Phones / WebRTC
- Use Opus for best quality
- Allow G.711 as fallback
SIP Trunks / PSTN
- Primary → G.711 (ulaw/alaw)
- Avoid unnecessary codecs
Large Call Centers
- Minimize transcoding paths
- Keep codec plan consistent across system
Debugging Codec Negotiation in Asterisk
asterisk -rvvv
pjsip set logger on
core show channel
Look for:
- Selected codec in SDP
- Transcoding messages
- RTP packet flow
Common Beginner Mistakes
- Allowing too many codecs “just in case”
- Forgetting trunk codec limitations
- Ignoring CPU impact of transcoding
- Assuming SIP failure when problem is RTP/codec
Key Takeaway
SIP establishes the call, but audio codecs determine real voice quality and performance.
Stable Asterisk systems follow three rules:
- Use common codecs like G.711 for trunks
- Use modern codecs like Opus internally
- Avoid unnecessary transcoding
Mastering codec strategy is essential for clear audio, low CPU usage, and scalable telecom systems.
Want to see API-driven CRM + Telecom workflows in action? Try the WhatsApp bot or explore the demos.
Comments (0)
Be the first to comment.