mfe-player 0.3.6

Music For Everyone — a plugin-driven, terminal-native music player that tells subscriptions to get lost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
use std::fs;
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};

use anyhow::{anyhow, Context, Result};
use serde_json::{json, Value};

use crate::config;
use crate::ipc::{self, Connection};
use crate::models::StreamInfo;

#[derive(Debug, Clone, Default)]
pub struct AudioSnapshot {
    pub position: f64,
    pub duration: f64,
    pub paused: bool,
    pub volume: f64,
    pub idle: bool,
    pub ended: bool,
    pub error: Option<String>,
    pub media_title: Option<String>,
}

pub struct AudioEngine {
    child: Option<Child>,
    writer: Arc<Mutex<Connection>>,
    snapshot: Arc<Mutex<AudioSnapshot>>,
    socket_path: PathBuf,
    attached: bool,
    detached: bool,
    owns_session: bool,
    private: bool,
}

fn session_owner() -> Option<u32> {
    fs::read_to_string(config::session_lock_path())
        .ok()
        .and_then(|raw| raw.trim().parse::<u32>().ok())
        .filter(|pid| ipc::is_player_pid(*pid))
}

/// A Ctrl+B handoff writes `handoff.pid` just before the UI exits. Starting a
/// second player during that teardown used to look like "another session owns
/// mpv": the new UI grabbed a private player, so the old song kept playing
/// with nobody able to control it and the next Ctrl+B killed it.
fn wait_for_handoff() {
    let Ok(raw) = fs::read_to_string(config::handoff_path()) else {
        return;
    };
    if let Ok(pid) = raw.trim().parse::<u32>() {
        let deadline = Instant::now() + Duration::from_secs(5);
        while Instant::now() < deadline && ipc::is_player_pid(pid) {
            thread::sleep(Duration::from_millis(50));
        }
    }
    let _ = fs::remove_file(config::handoff_path());
}

impl AudioEngine {
    pub fn connect_or_spawn(volume: f64, audio_device: &str) -> Result<Self> {
        config::ensure_dirs()?;
        let our_pid = std::process::id();
        wait_for_handoff();

        // Somebody else already owns the shared player: don't steal it out
        // from under them (that used to leave both sessions fighting over one
        // mpv and the music silently "not playing"). Take a private one.
        let private = session_owner().is_some();
        let socket_path = if private {
            config::private_mpv_socket_path(our_pid)
        } else {
            config::mpv_socket_path()
        };

        let mut stream = None;
        if !private {
            // Probe by connecting instead of checking the filesystem: Windows
            // named pipes never appear as files. A failed connect is cheap and
            // means either no mpv or a stale socket.
            if let Ok(probe) = probe_socket(&socket_path) {
                stream = Some(probe);
            } else {
                let _ = fs::remove_file(&socket_path);
            }
        }

        let (stream, child, attached) = match stream {
            Some(stream) => (stream, None, true),
            None => {
                let child = spawn_mpv(&socket_path, volume, audio_device)?;
                let stream = wait_for_socket(&socket_path)?;
                (stream, Some(child), false)
            }
        };

        let owns_session = !private;
        if owns_session {
            let _ = fs::write(config::session_lock_path(), our_pid.to_string());
        }

        let writer = Arc::new(Mutex::new(stream.try_clone()?));
        let snapshot = Arc::new(Mutex::new(AudioSnapshot {
            volume,
            ..Default::default()
        }));

        let reader_stream = stream;
        let reader_snapshot = Arc::clone(&snapshot);
        thread::Builder::new()
            .name("mpv-events".to_string())
            .spawn(move || read_events(reader_stream, reader_snapshot))
            .context("starting mpv event reader")?;

        let engine = Self {
            child,
            writer,
            snapshot,
            socket_path,
            attached,
            detached: false,
            owns_session,
            private,
        };

        engine.observe_properties()?;
        engine.set_volume(volume)?;
        Ok(engine)
    }

    pub fn is_attached(&self) -> bool {
        self.attached
    }

    /// True when another live session already owned the shared player, so
    /// this one spawned a private mpv instead of stealing it.
    pub fn is_private(&self) -> bool {
        self.private
    }

    pub fn snapshot(&self) -> AudioSnapshot {
        self.snapshot.lock().map(|s| s.clone()).unwrap_or_default()
    }

    pub fn take_ended(&self) -> bool {
        if let Ok(mut snapshot) = self.snapshot.lock() {
            if snapshot.ended {
                snapshot.ended = false;
                return true;
            }
        }
        false
    }

    pub fn take_error(&self) -> Option<String> {
        self.snapshot.lock().ok().and_then(|mut s| s.error.take())
    }

    pub fn load(&self, info: &StreamInfo) -> Result<()> {
        self.load_from(info, 0.0)
    }

    /// Load a stream and start it at `start` seconds. mpv's `start` is a
    /// per-file option, so it is passed to `loadfile` itself; this lets a
    /// restored session resume mid-song without racing the first position
    /// update.
    pub fn load_from(&self, info: &StreamInfo, start: f64) -> Result<()> {
        if let Ok(mut snapshot) = self.snapshot.lock() {
            snapshot.ended = false;
            snapshot.error = None;
            snapshot.position = 0.0;
            snapshot.duration = 0.0;
        }
        // yt-dlp negotiates headers (User-Agent, Referer, Sec-Fetch-*) that the
        // CDN expects. Without them direct googlevideo URLs intermittently 403.
        let fields: Vec<String> = info
            .http_headers
            .iter()
            .filter(|(key, value)| {
                !value.is_empty()
                    && !key.eq_ignore_ascii_case("accept-encoding")
                    && !key.eq_ignore_ascii_case("host")
            })
            .map(|(key, value)| format!("{key}: {}", value.replace([',', '\n', '\r'], " ")))
            .collect();
        let _ = self.command(json!([
            "set_property",
            "http-header-fields",
            fields.join(",")
        ]));

        let start = start.max(0.0);
        if start > 0.5 {
            // The `-1` index keeps the fourth argument in the per-file options
            // slot (mpv >= 0.38).
            self.command(json!([
                "loadfile",
                info.url.as_str(),
                "replace",
                -1,
                { "start": format!("{start:.3}") }
            ]))?;
        } else {
            self.command(json!(["loadfile", info.url.as_str(), "replace"]))?;
        }
        self.set_pause(false)?;
        Ok(())
    }

    pub fn set_pause(&self, paused: bool) -> Result<()> {
        self.command(json!(["set_property", "pause", paused]))?;
        if let Ok(mut snapshot) = self.snapshot.lock() {
            snapshot.paused = paused;
        }
        Ok(())
    }

    pub fn toggle_pause(&self) -> Result<()> {
        let paused = self.snapshot().paused;
        self.set_pause(!paused)
    }

    /// Jump to an absolute position (used by clicking the progress bar).
    pub fn seek_absolute(&self, seconds: f64) -> Result<()> {
        let target = seconds.max(0.0);
        self.command(json!(["seek", target, "absolute"]))?;
        Ok(())
    }

    /// Apply an mpv audio filter chain (`af`). An empty string clears it.
    pub fn set_filters(&self, filters: &str) -> Result<()> {
        self.command(json!(["set_property", "af", filters]))?;
        Ok(())
    }

    /// ReplayGain mode (`off`, `track`, `album`).
    pub fn set_replaygain(&self, mode: &str) -> Result<()> {
        self.command(json!(["set_property", "replaygain", mode]))?;
        Ok(())
    }

    pub fn seek_relative(&self, seconds: f64) -> Result<()> {
        let snapshot = self.snapshot();
        let target = (snapshot.position + seconds).max(0.0);
        let duration = snapshot.duration;
        let target = if duration > 0.0 {
            target.min(duration)
        } else {
            target
        };
        self.command(json!(["seek", target, "absolute"]))?;
        Ok(())
    }

    pub fn set_volume(&self, volume: f64) -> Result<()> {
        let volume = volume.clamp(0.0, 130.0);
        self.command(json!(["set_property", "volume", volume]))?;
        if let Ok(mut snapshot) = self.snapshot.lock() {
            snapshot.volume = volume;
        }
        Ok(())
    }

    /// Playback speed (0.5×–2.0×). Great for podcasts and 10-hour mixes.
    pub fn set_speed(&self, speed: f64) -> Result<()> {
        self.command(json!(["set_property", "speed", speed.clamp(0.25, 4.0)]))?;
        Ok(())
    }

    pub fn stop(&self) -> Result<()> {
        self.command(json!(["stop"]))?;
        Ok(())
    }

    pub fn detach(&mut self) {
        self.detached = true;
    }

    /// Hand the player over without stopping it. Used by the headless daemon
    /// when a TUI takes control back: mpv must keep playing across the handoff.
    pub fn release(&mut self) {
        self.detached = true;
    }

    pub fn socket_path(&self) -> &Path {
        &self.socket_path
    }

    fn observe_properties(&self) -> Result<()> {
        for (id, name) in [
            (1, "time-pos"),
            (2, "duration"),
            (3, "pause"),
            (4, "volume"),
            (5, "idle-active"),
            (6, "media-title"),
        ] {
            self.command(json!(["observe_property", id, name]))?;
        }
        Ok(())
    }

    fn command(&self, command: Value) -> Result<()> {
        let mut writer = self
            .writer
            .lock()
            .map_err(|_| anyhow!("mpv IPC writer lock poisoned"))?;
        let line = json!({ "command": command }).to_string();
        writeln!(writer, "{line}").context("writing to mpv IPC")?;
        writer.flush().context("flushing mpv IPC")?;
        Ok(())
    }
}

impl Drop for AudioEngine {
    fn drop(&mut self) {
        // Release the shared-player lock (also when detached, so the next
        // launch can re-attach without a second session being locked out).
        if self.owns_session {
            let lock = config::session_lock_path();
            let ours = std::process::id().to_string();
            if fs::read_to_string(&lock)
                .map(|raw| raw.trim() == ours)
                .unwrap_or(false)
            {
                let _ = fs::remove_file(&lock);
            }
        }
        // Only clear the handoff marker when it is ours: another session may
        // be handing off at the same time.
        let handoff = config::handoff_path();
        let ours = std::process::id().to_string();
        if fs::read_to_string(&handoff)
            .map(|raw| raw.trim() == ours)
            .unwrap_or(false)
        {
            let _ = fs::remove_file(&handoff);
        }
        if self.detached {
            return;
        }
        let _ = self.command(json!(["quit"]));
        if let Some(mut child) = self.child.take() {
            let deadline = Instant::now() + Duration::from_millis(400);
            loop {
                match child.try_wait() {
                    Ok(Some(_)) => break,
                    Ok(None) if Instant::now() < deadline => {
                        thread::sleep(Duration::from_millis(20));
                    }
                    _ => {
                        let _ = child.kill();
                        let _ = child.wait();
                        break;
                    }
                }
            }
        }
        if !self.attached {
            let _ = fs::remove_file(&self.socket_path);
        }
    }
}

fn spawn_mpv(socket_path: &Path, volume: f64, audio_device: &str) -> Result<Child> {
    // Named pipes on Windows live outside the filesystem; Unix sockets need
    // their parent directory to exist and any stale socket removed first.
    #[cfg(unix)]
    if let Some(parent) = socket_path.parent() {
        fs::create_dir_all(parent)?;
    }
    let _ = fs::remove_file(socket_path);

    let log = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(config::mpv_log_path())
        .ok();

    let mut command = Command::new("mpv");
    command
        .arg("--no-config")
        .arg("--no-video")
        .arg("--force-window=no")
        .arg("--idle=yes")
        .arg("--no-terminal")
        .arg("--msg-level=all=warn")
        .arg("--audio-display=no")
        .arg("--ytdl=yes")
        .arg("--network-timeout=20")
        .arg("--cache=yes")
        // Cap mpv's demuxer cache: the defaults are tuned for video and can
        // hold a lot of a streaming file in RAM. Audio streams only need a
        // small forward window to stay gapless.
        .arg("--demuxer-max-bytes=64MiB")
        .arg("--demuxer-max-back-bytes=16MiB")
        .arg("--prefetch-playlist=yes")
        .arg("--gapless-audio=yes")
        .arg("--audio-client-name=mfe-player")
        .arg(format!("--volume={}", volume.clamp(0.0, 130.0)))
        .arg(format!("--input-ipc-server={}", socket_path.display()))
        .stdin(Stdio::null())
        .stdout(Stdio::null());

    if !audio_device.trim().is_empty() {
        command.arg(format!("--audio-device={}", audio_device.trim()));
    }

    if let Some(log) = log {
        command.stderr(Stdio::from(log));
    } else {
        command.stderr(Stdio::null());
    }

    // Detach mpv from the terminal so closing the pane does not kill playback.
    ipc::hide_window(&mut command);
    // A private player exists only for this session: the daemon attaches to
    // the shared socket, so nobody could adopt it. If the session dies without
    // a clean shutdown, take mpv with it instead of leaving an orphan.
    if socket_path != config::mpv_socket_path() {
        ipc::die_with_parent(&mut command);
    }

    command.spawn().map_err(|err| {
        anyhow!(
            "could not launch mpv ({err}). Install mpv, then restart. Search and plugins still work without it."
        )
    })
}

fn wait_for_socket(socket_path: &Path) -> Result<Connection> {
    let deadline = Instant::now() + Duration::from_secs(5);
    loop {
        if let Ok(stream) = ipc::connect(socket_path) {
            return Ok(stream);
        }
        if Instant::now() >= deadline {
            return Err(anyhow!(
                "mpv did not create its IPC socket at {}. Check {}",
                socket_path.display(),
                config::mpv_log_path().display()
            ));
        }
        thread::sleep(Duration::from_millis(30));
    }
}

fn probe_socket(path: &Path) -> Result<Connection> {
    let mut stream = ipc::connect(path)?;
    ipc::set_timeouts(&stream, Duration::from_millis(700));
    writeln!(
        stream,
        "{}",
        json!({ "command": ["get_property", "idle-active"], "request_id": 991 })
    )?;
    stream.flush()?;
    let mut reader = BufReader::new(stream.try_clone()?);
    let mut line = String::new();
    let deadline = Instant::now() + Duration::from_millis(900);
    while Instant::now() < deadline {
        line.clear();
        match reader.read_line(&mut line) {
            Ok(0) => break,
            Ok(_) => {
                if let Ok(value) = serde_json::from_str::<Value>(&line) {
                    if value.get("request_id").and_then(Value::as_u64) == Some(991) {
                        ipc::clear_timeouts(&stream);
                        return Ok(stream);
                    }
                }
            }
            Err(err)
                if err.kind() == std::io::ErrorKind::WouldBlock
                    || err.kind() == std::io::ErrorKind::TimedOut =>
            {
                break;
            }
            Err(err) => return Err(err.into()),
        }
    }
    Err(anyhow!("stale mpv socket"))
}

fn read_events(stream: Connection, snapshot: Arc<Mutex<AudioSnapshot>>) {
    let reader = BufReader::new(stream);
    for line in reader.lines() {
        let Ok(line) = line else { break };
        let Ok(value) = serde_json::from_str::<Value>(&line) else {
            continue;
        };
        match value.get("event").and_then(Value::as_str) {
            Some("property-change") => {
                let Some(name) = value.get("name").and_then(Value::as_str) else {
                    continue;
                };
                let data = value.get("data").cloned().unwrap_or(Value::Null);
                if let Ok(mut snapshot) = snapshot.lock() {
                    match name {
                        "time-pos" => snapshot.position = data.as_f64().unwrap_or(0.0),
                        "duration" => snapshot.duration = data.as_f64().unwrap_or(0.0),
                        "pause" => snapshot.paused = data.as_bool().unwrap_or(false),
                        "volume" => snapshot.volume = data.as_f64().unwrap_or(0.0),
                        "idle-active" => snapshot.idle = data.as_bool().unwrap_or(false),
                        "media-title" => {
                            snapshot.media_title = data.as_str().map(ToString::to_string)
                        }
                        _ => {}
                    }
                }
            }
            Some("end-file") => {
                let reason = value.get("reason").and_then(Value::as_str).unwrap_or("");
                if reason == "eof" || reason == "error" {
                    if let Ok(mut snapshot) = snapshot.lock() {
                        if reason == "error" {
                            snapshot.error = value
                                .get("error")
                                .and_then(Value::as_str)
                                .map(ToString::to_string)
                                .or_else(|| Some("mpv could not play this stream".to_string()));
                        }
                        snapshot.ended = true;
                    }
                }
            }
            Some("file-loaded") => {
                if let Ok(mut snapshot) = snapshot.lock() {
                    snapshot.ended = false;
                    snapshot.error = None;
                }
            }
            _ => {}
        }
    }
    if let Ok(mut snapshot) = snapshot.lock() {
        snapshot.idle = true;
    }
}