add support for multithread buffer sending

This commit is contained in:
cediackermann 2025-01-13 16:24:17 +01:00
parent 3b61210761
commit d0e86603ae
4 changed files with 32 additions and 20 deletions

BIN
audios/rickroll.mp3 Normal file

Binary file not shown.

View file

@ -1,5 +1,6 @@
use std::io::BufReader;
use std::net::TcpStream;
use std::thread;
use std::time::{Duration, SystemTime};
use std::{fs::File, io::Write};
@ -34,27 +35,31 @@ impl Dispatcher {
continue;
}
let mut sock = TcpStream::connect(addr).expect("Failed to connect");
let source = source.clone();
while offset < duration {
let sound_data = source
.clone()
.skip_duration(offset)
.take_duration(chunk_len)
.collect::<Vec<i16>>();
thread::spawn(move || {
let mut sock = TcpStream::connect(addr).expect("Failed to connect");
offset += chunk_len;
while offset < duration {
let sound_data = source
.clone()
.skip_duration(offset)
.take_duration(chunk_len)
.collect::<Vec<i16>>();
let msg = Message::PlaySound(PlaySound {
timestamp: (now + offset).as_micros(),
channels: channels,
sample_rate: sample_rate,
sound_data: sound_data,
});
offset += chunk_len;
let buf = bincode::serialize(&msg).expect("Failed to serialize");
sock.write_all(&buf).unwrap();
}
let msg = Message::PlaySound(PlaySound {
timestamp: (now + offset).as_micros(),
channels: channels,
sample_rate: sample_rate,
sound_data: sound_data,
});
let buf = bincode::serialize(&msg).expect("Failed to serialize");
sock.write_all(&buf).unwrap();
}
});
}
}
}

View file

@ -15,7 +15,7 @@ mod ui_app;
fn main() -> eframe::Result {
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([1000.0, 1000.0]),
viewport: egui::ViewportBuilder::default().with_inner_size([700.0, 400.0]),
..Default::default()
};

View file

@ -46,15 +46,22 @@ impl eframe::App for UIApp {
if ui.button("Add address").clicked() {
self.addresses.push("localhost:3000".to_owned());
}
ui.add_space(25.0);
ui.separator();
ui.add_space(25.0);
if ui.button("Pick file").clicked() {
if let Some(file) = FileDialog::new().set_directory("/").pick_file() {
self.picked_file = Some(file);
let file = FileDialog::new().pick_file();
if !file.is_none() {
self.picked_file = file;
}
}
ui.label(format!("Picked file: {:?}", self.picked_file));
ui.add_space(25.0);
ui.separator();
ui.add_space(25.0);
if ui.button("Play sound").clicked() {
if let Some(path) = &self.picked_file {