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

View file

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