Phase 6: Real-Time Transport & Telephony · 50 min · Python · Telnyx SDK · Vonage SDK
SIP, PSTN, and Enterprise Phone System Integration
SIP is the protocol that runs the world's phone systems. If you deploy enterprise voice agents, you need it.
Real-world application: SIP and enterprise telephony knowledge distinguishes voice AI consultants and FDEs from generalists.
What you will learn
- Understand SIP signaling, SDP, and RTP for enterprise telephony
- Implement SIP trunking for enterprise PBX integration
- Compare Telnyx, Vonage, and AWS Connect for enterprise deployments
- Evaluate on-premise SIP options: Asterisk, FreeSWITCH
The Problem
Large organizations have existing phone infrastructure — Cisco PBXs, Avaya systems, SIP trunks. They won't switch to Twilio. Your voice agent needs to integrate with their SIP infrastructure, receive calls from their existing numbers, and route audio through their systems.
The Concept
What is SIP?
SIP (Session Initiation Protocol) is the signaling protocol for VoIP calls — it sets up, manages, and tears down voice sessions. It's like HTTP but for phone calls.
| SIP Component | Role | Analogy |
|---|
| SIP Registrar | Registers user locations | DNS for phones |
| SIP Proxy | Routes calls | Router for calls |
| SIP PBX | Manages extensions, routing | Switchboard |
| SIP Trunk | Connects PBX to PSTN | Phone line |
| SDP (in SIP) | Negotiates audio codecs | Menu of options |
SIP Call Flow
SIP vs Twilio
| Factor | SIP (Direct) | Twilio |
|---|
| Infrastructure | Need SIP server/PBX | Twilio handles it |
| Cost per minute | $0.001-0.005 | $0.005-0.015 |
| Audio quality | Configurable (G.711, G.722, Opus) | μ-law 8kHz default |
| Setup complexity | High | Low |
| Enterprise fit | Native | Requires adapter |
| Scalability | Need capacity planning | Auto-scaling |
| Compliance | Full control | Twilio handles |
Why would an enterprise prefer SIP integration over Twilio?
Enterprises prefer SIP for three reasons: (1) Integration — they already have SIP PBXs (Cisco, Avaya) and existing phone numbers; Twilio would require porting numbers and changing infrastructure. (2) Cost — SIP trunks cost $0.001-0.005/min vs Twilio's $0.005-0.015/min; at 1M minutes/month that's $1K vs $10K. (3) Audio quality — SIP allows G.722 wideband or Opus, while Twilio defaults to μ-law 8kHz; better audio means better ASR accuracy. SIP also gives full control over compliance, routing, and capacity.
Audio Codecs in SIP
| Codec | Bitrate | Quality | Used In |
|---|
| G.711 (μ-law/a-law) | 64 kbps | Fair | PSTN, default VoIP |
| G.722 | 64 kbps | Good | Wideband VoIP, enterprise |
| G.729 | 8 kbps | Fair | Low-bandwidth SIP |
| Opus | 6-510 kbps | Excellent | Modern SIP, WebRTC gateway |
SIP Integration with Voice Agent
# Using pjsua2 or baresip for SIP in Python
import pjsua2 as pj
class VoiceAgentSIPEndpoint(pj.Account):
"""SIP endpoint that connects calls to voice agent pipeline."""
def onIncomingCall(self, call):
"""Handle incoming SIP call."""
call_info = call.getInfo()
print(f"Incoming call from: {call_info.remoteUri}")
# Answer the call
call.answer(200)
# Get audio media
aud_med = call_info.media[0]
if aud_med.type == pj.MediaType.AUDIO:
# Connect audio to voice agent pipeline
self._connect_to_agent(aud_med)
def _connect_to_agent(self, audio_media):
"""Connect SIP audio to ASR → LLM → TTS pipeline."""
# Register audio callback
audio_media.setRxCallback(self._on_audio_received)
def _on_audio_received(self, frame):
"""Receive audio frame from SIP call."""
# Feed to ASR
asyncio.create_task(asr.process(frame))
SIP Server Options
| Server | Language | Use Case |
|---|
| Asterisk | C | PBX, media server, voice agent gateway |
| FreeSWITCH | C | Scalable SIP, WebRTC gateway |
| Kamailio | C | SIP proxy/router (high scale) |
| OpenSIPS | C | SIP proxy, load balancer |
| pjsua2 | C++/Python | SIP client library (embed in app) |
FreeSWITCH as Voice Agent Gateway
FreeSWITCH is the most popular choice for connecting SIP to voice agents:
<!-- FreeSWITCH dialplan: route call to voice agent -->
<extension name="voice_agent">
<condition field="destination_number" expression="^18005551234$">
<action application="socket" data="127.0.0.1:8080 async full" />
</condition>
</extension>
This connects the SIP call to your voice agent server via FreeSWITCH's ESL (Event Socket Layer), which streams audio bidirectionally.
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers Build, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account — the opening phases of 29 of 35 courses are free, no credit card.
Browse all courses · View pricing · DeVenture Academy