Features
- Audio Codecs: G.711 μ-law (PCMU), G.711 A-law (PCMA), Opus
- RTP/RTCP: Complete packet construction, parsing, and statistics per RFC 3550
- SRTP/SRTCP: AES-CM-128-HMAC-SHA1-80 encryption per RFC 3711
- Jitter Buffer: Adaptive and fixed modes with packet reordering and loss concealment
- Audio Devices: Cross-platform capture and playback via cpal
- Resampling: Automatic sample rate conversion between codecs and devices
- Symmetric RTP: Comedia-style NAT traversal with learned endpoints
- DTMF: RFC 2833 telephone-event transmission
- Sequence Rollover: Proper 16-bit sequence number rollover handling for long calls (22+ minutes)
Installation
Add to your Cargo.toml:
[]
= "0.1"
Or with specific features:
[]
= { = "0.1", = false, = ["g711", "srtp"] }
Quick Start
Basic Media Session
use ;
use SocketAddr;
async
With SRTP Encryption
use ;
use SocketAddr;
async
Using Codecs Directly
use ;
Using the Jitter Buffer
use ;
Building RTP Packets Manually
use ;
SRTP Protection/Unprotection
use SrtpContext;
Feature Flags
| Feature | Default | Description |
|---|---|---|
g711 |
✓ | G.711 μ-law (PCMU) and A-law (PCMA) codecs |
opus |
✓ | Opus codec with automatic resampling (requires libopus) |
srtp |
✓ | SRTP/SRTCP encryption (AES-CM-128-HMAC-SHA1-80) |
device |
✓ | Audio device capture and playback via cpal |
Minimal Build
For embedded or server-side use without audio devices:
[]
= { = "0.1", = false, = ["g711"] }
Architecture
flowchart TB
subgraph MediaSession
direction TB
subgraph TX["Transmit Path"]
direction LR
Capture["🎤 Audio Capture<br/>(cpal)"]
Enc["Encoder<br/>(G.711/Opus)"]
RTPBuild["RTP Builder"]
Capture --> Enc --> RTPBuild
end
subgraph RX["Receive Path"]
direction RL
Play["🔊 Audio Playback<br/>(cpal)"]
Dec["Decoder<br/>(G.711/Opus)"]
RTPParse["RTP Parser"]
Jitter["Jitter Buffer<br/>(adaptive)"]
RTPParse --> Jitter --> Dec --> Play
end
subgraph Stats["RTP Statistics"]
S1["📊 Packets sent/received"]
S2["📈 Jitter calculation"]
S3["🔢 Sequence rollover"]
S4["📉 Packet loss detection"]
end
RTPBuild -->|"SRTP"| Net((("🌐 Network")))
Net -->|"SRTP"| RTPParse
end
style MediaSession fill:#1a1a2e,stroke:#16213e,color:#eee
style TX fill:#0f3460,stroke:#16213e,color:#eee
style RX fill:#0f3460,stroke:#16213e,color:#eee
style Stats fill:#533483,stroke:#16213e,color:#eee
style Net fill:#e94560,stroke:#fff,color:#fff
Data Flow
sequenceDiagram
participant Mic as 🎤 Microphone
participant Enc as Encoder
participant SRTP as SRTP
participant Net as 🌐 Network
participant Jitter as Jitter Buffer
participant Dec as Decoder
participant Spk as 🔊 Speaker
Note over Mic,Spk: Outbound Audio
Mic->>Enc: PCM samples (48kHz)
Enc->>Enc: Resample to 8kHz
Enc->>SRTP: G.711 payload
SRTP->>Net: Encrypted RTP
Note over Mic,Spk: Inbound Audio
Net->>SRTP: Encrypted RTP
SRTP->>Jitter: Decrypted RTP
Jitter->>Jitter: Reorder & buffer
Jitter->>Dec: Ordered packets
Dec->>Spk: PCM samples
Module Overview
| Module | Description |
|---|---|
codec |
Audio encoder/decoder traits and implementations (G.711, Opus) |
rtp |
RTP/RTCP packet construction, parsing, and statistics |
srtp |
SRTP encryption/decryption and SDP crypto attribute handling |
jitter |
Adaptive and fixed jitter buffer with loss concealment |
device |
Cross-platform audio capture and playback |
resample |
Sample rate conversion utilities |
session |
High-level MediaSession that orchestrates everything |
Supported Platforms
| Platform | Audio Backend | Status |
|---|---|---|
| macOS | CoreAudio | ✓ Tested |
| Linux | ALSA / PulseAudio | ✓ Tested |
| Windows | WASAPI | ✓ Tested |
| iOS | CoreAudio | Should work (untested) |
| Android | AAudio / OpenSL ES | Should work (untested) |
Performance
- Codec latency: < 1ms for G.711, ~2.5ms for Opus
- Jitter buffer: Configurable 20-200ms adaptive delay
- Memory: ~50KB per active session (excluding audio buffers)
- CPU: Minimal overhead; Opus uses optimized libopus
Testing
The crate includes comprehensive tests:
# Run all tests
# Run with all features
# Run specific module tests
Test coverage: 137 unit tests + 4 doc tests covering:
- Codec roundtrip accuracy
- RTP sequence number rollover (RFC 3550)
- SRTP encryption/decryption and ROC handling
- Jitter buffer reordering and loss detection
- All edge cases and error conditions
RFC Compliance
| RFC | Description | Status |
|---|---|---|
| RFC 3550 | RTP: Real-Time Transport Protocol | ✓ Implemented |
| RFC 3551 | RTP Profile for Audio/Video | ✓ Implemented |
| RFC 3711 | SRTP: Secure Real-time Transport | ✓ Implemented |
| RFC 2833 | RTP Payload for DTMF Digits | ✓ Implemented |
| RFC 3261 | SIP (crypto attribute parsing) | Partial |
Comparison with Alternatives
| Feature | rtp-engine | PJMEDIA | GStreamer |
|---|---|---|---|
| Language | Pure Rust | C | C |
| Async | Native tokio | Thread-based | Thread-based |
| Memory safety | Guaranteed | Manual | Manual |
| Binary size | ~500KB | ~2MB | ~10MB+ |
| G.711 | ✓ | ✓ | ✓ |
| Opus | ✓ | ✓ | ✓ |
| SRTP | ✓ | ✓ | ✓ |
| Jitter buffer | Adaptive | Adaptive | Adaptive |
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Ensure tests pass (
cargo test) - Ensure clippy passes (
cargo clippy) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Acknowledgments
Made with ❤️ by 5060 Solutions