jittr
1.0.0
此實作旨在對抗網路抖動並透過 udp 資料包創建可靠的媒體串流。當嘗試透過 udp(很可能是 rtp)持續傳輸音訊時,無序資料包和變化的網路延遲是最大的問題之一。該資料結構緩衝資料包並對它們重新排序,同時引入盡可能小的延遲。
透過抖動緩衝區播放 udp/rtp 串流中的 opus 封包:
use async_std :: stream :: interval ;
use std :: time :: Duration ;
use jittr :: { JitterBuffer , Packet } ;
let mut rtp_stream = /* your rtp stream */ ;
/// Your Packet implementation
struct Opus { .. }
impl Packet for Opus { .. }
/// Create a jitter buffer for Opus packets
/// It can hold up to 10 packets before it starts to discard old packets
let mut jitter = JitterBuffer :: < Opus , 10 > :: new ( ) ;
/// Create an interval for packet playback
/// A typical length for audio packets is between 10 and 20ms
let mut playback = interval ( Duration :: from_millis ( 20 ) ) ;
loop {
futures :: select! {
_ = playback . next ( ) . fuse ( ) => {
let packet = jittr . pop ( ) ;
let pcm = /* Opus decodes audio if present or infers if none */
// Write pcm to speaker
} ,
rtp = rtp_stream . next ( ) . fuse ( ) => {
let opus : Opus = rtp . unwrap ( ) ;
jittr . push ( opus ) ;
} ,
}
}