(cleanup): cleanup on src/

This commit is contained in:
socatlolmeow 2026-10-10 18:04:20 +02:00
commit 576801f108
10 changed files with 503 additions and 193 deletions

View file

@ -26,12 +26,9 @@ pub struct Player {
}
impl Player {
pub fn spawn(
volume: u8,
on_event: impl Fn(PlayerEvent) + Send + 'static,
) -> Result<Player> {
let sock_path =
std::env::temp_dir().join(format!("drift-{}.sock", std::process::id()));
pub fn spawn(volume: u8, on_event: impl Fn(PlayerEvent) + Send + 'static) -> Result<Player> {
let sock_dir = dirs::runtime_dir().unwrap_or_else(std::env::temp_dir);
let sock_path = sock_dir.join(format!("drift-{}.sock", std::process::id()));
let _ = std::fs::remove_file(&sock_path);
let mut cmd = Command::new("mpv");
@ -51,13 +48,14 @@ impl Player {
.arg(format!("--volume={volume}"))
.arg(format!("--input-ipc-server={}", sock_path.display()));
if let Ok(extra) = std::env::var("DRIFT_MPV_ARGS").or_else(|_| std::env::var("YTM_MPV_ARGS")) {
if let Ok(extra) =
std::env::var("DRIFT_MPV_ARGS").or_else(|_| std::env::var("YTM_MPV_ARGS"))
{
cmd.args(extra.split_whitespace());
}
cmd
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
cmd.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
#[cfg(target_os = "linux")]
unsafe {
@ -101,14 +99,21 @@ impl Player {
sock_path,
};
for (id, prop) in [(1, "time-pos"), (2, "duration"), (3, "pause"), (4, "volume")] {
for (id, prop) in [
(1, "time-pos"),
(2, "duration"),
(3, "pause"),
(4, "volume"),
] {
player.send(json!(["observe_property", id, prop]))?;
}
std::thread::spawn(move || {
for line in BufReader::new(reader).lines() {
let Ok(line) = line else { break };
let Ok(v) = serde_json::from_str::<Value>(&line) else { continue };
let Ok(v) = serde_json::from_str::<Value>(&line) else {
continue;
};
if let Some(ev) = parse_event(&v) {
on_event(ev);
}
@ -211,7 +216,9 @@ mod tests {
fn mpv_roundtrip() {
use std::sync::mpsc;
let wav = std::env::temp_dir().join("drift-test.wav");
let samples: Vec<u8> = (0..16_000u32).map(|i| if (i / 20) % 2 == 0 { 140 } else { 116 }).collect();
let samples: Vec<u8> = (0..16_000u32)
.map(|i| if (i / 20) % 2 == 0 { 140 } else { 116 })
.collect();
let mut data = Vec::new();
data.extend_from_slice(b"RIFF");
data.extend_from_slice(&(36 + samples.len() as u32).to_le_bytes());