Crates.io | rvoip-rtp-core |
lib.rs | rvoip-rtp-core |
version | 0.1.26 |
created_at | 2025-07-03 05:36:06.990076+00 |
updated_at | 2025-08-15 17:48:39.249302+00 |
description | RTP/RTCP protocol implementation for the rvoip stack |
homepage | https://github.com/eisenzopf/rvoip |
repository | https://github.com/eisenzopf/rvoip |
max_upload_size | |
id | 1735829 |
size | 3,447,202 |
The rtp-core
library provides comprehensive RTP/RTCP implementation and secure media transport capabilities for the rvoip VoIP stack. It handles all packet-level operations in the media transport layer, including RTP/RTCP packet processing, network transport, security (DTLS-SRTP), buffer management, and statistics collection.
The RTP Core sits at the foundation of the media transport stack, providing reliable and secure packet-level communication:
┌─────────────────────────────────────────┐
│ Application Layer │
├─────────────────────────────────────────┤
│ rvoip-media-core │
├─────────────────────────────────────────┤
│ rvoip-rtp-core ⬅️ YOU ARE HERE
├─────────────────────────────────────────┤
│ Network Layer │
└─────────────────────────────────────────┘
The library provides a complete multimedia security ecosystem with multiple protocols:
┌─────────────────────────────────────────────────────────────┐
│ Security Protocols │
├─────────────────┬──────────────┬─────────────┬─────────────┤
│ ZRTP │ MIKEY-PSK │ MIKEY-PKE │ SDES-SRTP │
│ (P2P Calls) │ (Enterprise) │ (PKI-based) │ (SIP-based) │
├─────────────────┴──────────────┴─────────────┴─────────────┤
│ DTLS-SRTP │
│ (WebRTC Compatible) │
├─────────────────────────────────────────────────────────────┤
│ SRTP/SRTCP Core │
│ (AES-CM/GCM, HMAC-SHA1/256) │
└─────────────────────────────────────────────────────────────┘
use rvoip_rtp_core::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
// Create RTP session configuration
let config = RtpSessionConfig::builder()
.local_addr("0.0.0.0:0".parse()?)
.enable_rtcp_mux(true)
.build();
// Create RTP session
let session = RtpSession::new(config).await?;
let local_addr = session.local_addr()?;
println!("RTP session listening on {}", local_addr);
// Send RTP packet
let packet = RtpPacket::builder()
.payload_type(0) // G.711 μ-law
.sequence_number(1234)
.timestamp(160000)
.ssrc(0x12345678)
.payload(audio_data)
.build();
session.send_rtp_packet(packet, remote_addr).await?;
// Receive packets
while let Some(event) = session.receive_event().await {
match event {
RtpEvent::PacketReceived { packet, source } => {
println!("Received RTP packet from {}", source);
process_audio_packet(packet);
}
RtpEvent::RtcpReceived { packet, source } => {
println!("Received RTCP packet from {}", source);
process_rtcp_feedback(packet);
}
_ => {}
}
}
Ok(())
}
use rvoip_rtp_core::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
// Create DTLS certificate
let cert = generate_self_signed_certificate()?;
// Configure secure transport
let config = SecureTransportConfig::builder()
.dtls_certificate(cert)
.srtp_profile(SrtpProfile::Aes128CmSha1_80)
.role(DtlsRole::Client)
.build();
// Create secure RTP session
let session = SecureRtpSession::new(config).await?;
// Perform DTLS handshake
session.connect(remote_addr).await?;
println!("DTLS handshake completed");
// Send encrypted RTP
let packet = RtpPacket::new(/* ... */);
session.send_secure_rtp(packet, remote_addr).await?;
Ok(())
}
use rvoip_rtp_core::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
// Create ZRTP session
let config = ZrtpConfig::builder()
.client_id("MyVoIPApp 1.0")
.supported_hash_algorithms(vec![HashAlgorithm::Sha256])
.supported_cipher_algorithms(vec![CipherAlgorithm::Aes128])
.build();
let session = ZrtpSession::new(config).await?;
// Initiate ZRTP key exchange
session.start_key_exchange(remote_addr).await?;
// Wait for SAS verification
let sas = session.wait_for_sas().await?;
println!("SAS for verification: {}", sas);
// User confirms SAS matches on both ends
session.confirm_sas(true).await?;
// Now send secure RTP
let packet = RtpPacket::new(/* ... */);
session.send_zrtp_protected_rtp(packet).await?;
Ok(())
}
use rvoip_rtp_core::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
// Load enterprise certificate
let cert = load_certificate_from_file("enterprise.crt")?;
let private_key = load_private_key_from_file("enterprise.key")?;
// Configure MIKEY-PKE
let config = MikeyPkeConfig::builder()
.certificate(cert)
.private_key(private_key)
.ca_certificates(load_ca_certificates()?)
.security_policy(SecurityPolicy::HighSecurity)
.build();
let session = MikeyPkeSession::new(config).await?;
// Perform certificate-based key exchange
session.initiate_key_exchange(remote_addr).await?;
// Enterprise PKI validation happens automatically
session.wait_for_completion().await?;
println!("Enterprise-grade security established");
Ok(())
}
use rvoip_rtp_core::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
// Configure high-performance buffers
let buffer_config = BufferConfig::builder()
.jitter_buffer_size(50) // 50ms adaptive buffer
.memory_pool_size(10 * 1024 * 1024) // 10MB pool
.max_concurrent_streams(1000)
.enable_priority_queue(true)
.congestion_control_enabled(true)
.build();
let session = RtpSession::with_buffer_config(
RtpSessionConfig::default(),
buffer_config
).await?;
// Session automatically uses optimized buffers
// Tested with 500,000+ packets across 500 streams
Ok(())
}
The SRTP implementation follows RFC 3711 and provides enterprise-grade security:
Encryption Algorithms:
Authentication Algorithms:
Key Management:
Tamper Detection:
The implementation includes critical security improvements:
ProtectedRtpPacket
struct// Create SRTP crypto context
let crypto_key = SrtpCryptoKey::new(master_key, master_salt);
let crypto = SrtpCrypto::new(SRTP_AES128_CM_SHA1_80, crypto_key)?;
// Encrypt RTP packet
let (encrypted_packet, auth_tag) = crypto.encrypt_rtp(&packet)?;
let protected = ProtectedRtpPacket::new(encrypted_packet, auth_tag);
// Serialize for transmission
let bytes = protected.serialize()?;
// On receiving side - automatically verifies auth tag
let decrypted_packet = crypto.decrypt_rtp(&bytes)?;
The library provides comprehensive quality monitoring capabilities:
// Get comprehensive statistics
let stats = session.get_statistics().await?;
println!("Packet loss: {:.2}%", stats.packet_loss_percentage);
println!("Jitter: {:.1}ms", stats.jitter_ms);
println!("RTT: {:.1}ms", stats.round_trip_time_ms);
println!("MOS score: {:.1}", stats.mos_score);
// Configure quality alerts
session.set_quality_thresholds(QualityThresholds {
max_packet_loss_percent: 1.0,
max_jitter_ms: 30.0,
min_mos_score: 3.5,
}).await?;
rvoip-sip-core
: SIP message types and SDP handlingtokio
: Async runtime for network operationsring
: Cryptographic operations for securityrcgen
: Certificate generation for DTLSThe RTP Core provides the foundation for media transport in the rvoip stack:
Run the comprehensive test suite:
# Run all tests
cargo test -p rvoip-rtp-core
# Run with specific features
cargo test -p rvoip-rtp-core --features "dtls zrtp mikey"
# Run security-specific tests
cargo test -p rvoip-rtp-core srtp
cargo test -p rvoip-rtp-core dtls
cargo test -p rvoip-rtp-core zrtp
# Run performance tests
cargo test -p rvoip-rtp-core --release buffer_performance
The library includes comprehensive examples demonstrating all features:
# Basic RTP communication
cargo run --example api_basic
# Secure DTLS-SRTP session
cargo run --example direct_dtls_media_streaming
# ZRTP peer-to-peer security
cargo run --example zrtp_p2p_demo
# Enterprise MIKEY-PKE
cargo run --example mikey_pke_enterprise
# High-performance buffers
cargo run --example high_performance_buffers
# Quality monitoring
cargo run --example rtcp_reports
# Cross-platform compatibility
cargo run --example socket_validation
The library provides comprehensive error handling with categorized error types:
use rvoip_rtp_core::Error;
match rtp_result {
Err(Error::SecurityNegotiationFailed(details)) => {
// Handle security handshake failures
log::error!("Security negotiation failed: {}", details);
attempt_fallback_security().await?;
}
Err(Error::PacketValidationFailed(reason)) => {
// Handle malformed packets
log::warn!("Invalid packet received: {}", reason);
// Continue processing other packets
}
Err(Error::NetworkTimeout(addr)) => {
// Handle network timeouts - often recoverable
if error.is_recoverable() {
retry_connection(addr).await?;
}
}
Ok(result) => {
// Handle success
}
}
Contributions are welcome! Please see the main rvoip contributing guidelines for details.
For rtp-core specific contributions:
This project is licensed under either of
at your option.