rtp-tools: add pacing to play

This commit is contained in:
Preston Baxter 2024-09-11 13:29:46 -05:00
parent f774eb52cb
commit 089dbffa1e
1 changed files with 23 additions and 16 deletions

View File

@ -1,9 +1,4 @@
use std::{ use std::{cmp::min, fs::File, io::Read, vec::Vec};
fs::File,
io::Read,
vec::Vec,
cmp::min
};
use anyhow::Result; use anyhow::Result;
@ -33,19 +28,25 @@ const MTU: usize = 1500;
fn main() -> Result<()> { fn main() -> Result<()> {
let cli = Args::parse(); let cli = Args::parse();
let local_addr = match cli.bind {
Some(s) => s,
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}");
//open file //open file
let mut fd = File::open(file)?; let mut fd = File::open(file)?;
//buffer //read file into memory
let mut file_buffer = Vec::with_capacity(1500 * 512); let mut file_buffer = Vec::with_capacity(1500 * 4096); //16 packets
let read_bytes = fd.read_to_end(&mut file_buffer)?; let buffer_len = fd.read_to_end(&mut file_buffer)?;
let buffer_len = file_buffer.len();
println!("ReadBytes {read_bytes} Len {buffer_len}");
//Construct the size of data allowed
//TODO: Support variable MTU. Also calculate RTP packet size
let header_len = MTU - 8 - 176; let header_len = MTU - 8 - 176;
let mut packet_len = buffer_len / header_len; let mut packet_len = buffer_len / header_len;
if buffer_len % header_len > 0 { if buffer_len % header_len > 0 {
@ -65,13 +66,19 @@ fn main() -> Result<()> {
} }
} }
//open socket
let udp_socket = std::net::UdpSocket::bind(local_addr)?;
udp_socket.connect(addr)?;
let mut i = 0;
for packet in packets { for packet in packets {
let s = String::from_utf8(packet)?; std::thread::sleep(pacing);
println!("Packet: {s}"); print!("Playing chunk {i} of {packet_len}\r");
udp_socket.send(&packet)?;
i += 1;
} }
//open socket println!("Finished playing all the things.");
//stream file to socket //stream file to socket
Ok(()) Ok(())
} }