rtp-tools: update pacing based on packet size and time it would take to play

This commit is contained in:
Preston Baxter 2024-09-11 13:41:55 -05:00
parent 089dbffa1e
commit f1fe0be15b
1 changed files with 13 additions and 5 deletions

View File

@ -1,4 +1,4 @@
use std::{cmp::min, fs::File, io::Read, vec::Vec}; use std::{cmp::min, fs::File, io::Read, vec::Vec, time::Duration};
use anyhow::Result; use anyhow::Result;
@ -25,6 +25,15 @@ enum Commands {
const MTU: usize = 1500; const MTU: usize = 1500;
fn pacing(sample_rate: u32, bitdepth: usize, size: usize) -> Duration {
let samples = size / bitdepth;
let seconds = samples as f32 / sample_rate as f32;
let just_seconds = seconds.floor();
let nanos = (seconds - just_seconds) * 1e9;
Duration::new(just_seconds as u64, nanos as u32)
}
fn main() -> Result<()> { fn main() -> Result<()> {
let cli = Args::parse(); let cli = Args::parse();
@ -33,8 +42,6 @@ fn main() -> Result<()> {
None => "0.0.0.0:6767".to_string(), None => "0.0.0.0:6767".to_string(),
}; };
let pacing = std::time::Duration::new(0, 10000);
match &cli.command { match &cli.command {
Some(Commands::Play { file, addr }) => { Some(Commands::Play { file, addr }) => {
println!("Playing {file:?} -> {addr}"); println!("Playing {file:?} -> {addr}");
@ -70,7 +77,8 @@ fn main() -> Result<()> {
let udp_socket = std::net::UdpSocket::bind(local_addr)?; let udp_socket = std::net::UdpSocket::bind(local_addr)?;
udp_socket.connect(addr)?; udp_socket.connect(addr)?;
let mut i = 0; let pacing = pacing(48000, 16, header_len);
let mut i = 1;
for packet in packets { for packet in packets {
std::thread::sleep(pacing); std::thread::sleep(pacing);
print!("Playing chunk {i} of {packet_len}\r"); print!("Playing chunk {i} of {packet_len}\r");
@ -78,7 +86,7 @@ fn main() -> Result<()> {
i += 1; i += 1;
} }
println!("Finished playing all the things."); println!("\nFinished playing all the things.");
//stream file to socket //stream file to socket
Ok(()) Ok(())
} }