Overview
Iqra AI provides a WebRTC gateway for embedding AI voice conversations directly in web browsers and mobile applications. WebRTC enables low-latency, peer-to-peer audio communication without requiring plugins or downloads. The WebRTC implementation uses:- SIPSorcery library for WebRTC peer connection management
- WebSocket signaling for SDP/ICE exchange
- STUN servers for NAT traversal
- Audio transceivers for bidirectional media
Architecture
Connection flow
Dual-transport design
TheWebRtcClientTransport combines:
- WebSocket channel - For signaling (SDP, ICE) and text messages
- RTP channel - For audio media via WebRTC peer connection
IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:13
Session initialization
1
Create web session
Client requests a WebRTC session via API:
2
Backend prepares session
Backend creates:
- Conversation session orchestrator
- AI agent instance
- Deferred client transport (waiting for WebSocket)
- WebSocket URL with authentication token
IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:1113
Connect WebSocket
Client connects to the provided WebSocket URL:Backend validates the session token and activates the transport:Source:
IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:2804
WebRTC negotiation
Client creates peer connection and sends offer to backend via WebSocket.
WebRTC peer connection
Client-side setup
Backend implementation
The backend uses SIPSorcery to handle WebRTC:IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:38
Audio codec configuration
Supported codecs
The backend dynamically selects codecs based on session configuration:- OPUS (Recommended)
- G.711 μ-law
- G.711 A-law
- G.722
Best for: WebRTC applications, modern browsersBenefits:
- Superior quality at low bitrates
- Built-in forward error correction
- Wide browser support
- Adaptive bitrate
IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:72
Audio configuration object
Signaling protocol
Message types
WebSocket messages during WebRTC setup:Offer (Client → Backend)
Answer (Backend → Client)
ICE Candidate (Bidirectional)
Signaling loop
Backend maintains signaling channel:IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:84
Data channel support
WebRTC includes a data channel for text messaging:Backend setup
Client usage
Connection states
Monitoring connection health
Backend monitoring
IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:239
Media streaming
Outbound audio (Backend → Client)
IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:183
Inbound audio (Client → Backend)
IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:174
Mobile implementation
React Native example
iOS/Swift with WebRTC SDK
Security considerations
IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:250
STUN vs TURN: Current implementation uses STUN for NAT traversal. For production, consider adding TURN servers for users behind restrictive firewalls.
Troubleshooting
ICE connection failures
Symptom: Peer connection stuck in “checking” state Solutions:- Verify STUN server is reachable
- Check firewall rules allow UDP traffic
- Consider deploying TURN servers for relaying
- Enable verbose ICE logging
Audio quality issues
Symptom: Choppy or distorted audio Solutions:- Verify codec compatibility (prefer OPUS)
- Check network bandwidth
- Monitor packet loss via WebRTC stats
- Adjust frame duration (20ms recommended)
SDP negotiation failures
Symptom:setRemoteDescription fails
Common causes:
- Codec mismatch (backend doesn’t support offered codec)
- Invalid SDP format
- Missing required media sections
Performance optimization
Use Opus with FEC: Enable forward error correction to handle packet loss without retransmissions.
Optimize frame size: 20ms frames balance latency and packet overhead. Smaller frames = lower latency but more overhead.
Monitor RTP stats: Track jitter, packet loss, and round-trip time to detect quality degradation early.
Next steps
- WebSocket integration - Simpler audio streaming alternative
- SIP trunking - Connect traditional telephony
- Voice configuration - Configure AI voice settings