aboutsummaryrefslogtreecommitdiff
path: root/radio_tui/player.py
blob: 7f87782b1e763b5cc76173784839bac5fcd8045d (plain)
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
from __future__ import annotations

import json
import shutil
import subprocess
import threading
import time
from dataclasses import dataclass
from pathlib import Path


@dataclass
class PlayerState:
    station_name: str = ""
    stream_url: str = ""
    media_title: str = ""
    volume: int = 50
    muted: bool = False
    playing: bool = False
    offline: bool = False


class MpvPlayer:
    def __init__(self, socket_path: Path | None = None) -> None:
        self.socket_path = socket_path or Path("/tmp/radio_tui_mpvsocket")
        self.state = PlayerState()
        self._process: subprocess.Popen[bytes] | None = None
        self._poll_thread: threading.Thread | None = None
        self._stop_polling = threading.Event()
        self._lock = threading.Lock()

    @property
    def available(self) -> bool:
        return shutil.which("mpv") is not None and shutil.which("socat") is not None

    def _ipc(self, command: list[str]) -> dict | None:
        if not self.socket_path.exists():
            return None
        payload = json.dumps({"command": command}) + "\n"
        try:
            result = subprocess.run(
                ["socat", "-", str(self.socket_path)],
                input=payload.encode(),
                capture_output=True,
                timeout=2,
                check=False,
            )
        except (OSError, subprocess.TimeoutExpired):
            return None
        if not result.stdout:
            return None
        try:
            return json.loads(result.stdout.decode())
        except json.JSONDecodeError:
            return None

    def _ensure_mpv(self, stream_url: str, volume: int) -> None:
        if self._process and self._process.poll() is None:
            return
        self._stop_polling.set()
        if self._poll_thread:
            self._poll_thread.join(timeout=1)
        self._stop_polling.clear()
        if self.socket_path.exists():
            self.socket_path.unlink(missing_ok=True)
        self._process = subprocess.Popen(
            [
                "mpv",
                f"--volume={volume}",
                f"--input-ipc-server={self.socket_path}",
                "--idle=yes",
                "--no-terminal",
            ],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )
        for _ in range(30):
            if self.socket_path.exists():
                break
            time.sleep(0.1)

    def play(self, station_name: str, stream_url: str) -> None:
        with self._lock:
            volume = self.state.volume
            self._ensure_mpv(stream_url, volume)
            if self._process and self._process.poll() is None and self.socket_path.exists():
                self._ipc(["loadfile", stream_url])
            self.state.station_name = station_name
            self.state.stream_url = stream_url
            self.state.media_title = "Connecting..."
            self.state.playing = True
            self.state.offline = False
            self._start_poll()

    def stop(self) -> None:
        with self._lock:
            self._stop_polling.set()
            if self._poll_thread:
                self._poll_thread.join(timeout=1)
            if self._process and self._process.poll() is None:
                self._process.terminate()
                try:
                    self._process.wait(timeout=2)
                except subprocess.TimeoutExpired:
                    self._process.kill()
            self._process = None
            self.socket_path.unlink(missing_ok=True)
            self.state = PlayerState(volume=self.state.volume)

    def adjust_volume(self, delta: int) -> None:
        with self._lock:
            self._ipc(["add", "volume", delta])
            response = self._ipc(["get_property", "volume"])
            if response and "data" in response:
                self.state.volume = int(response["data"])

    def toggle_mute(self) -> None:
        with self._lock:
            self._ipc(["cycle", "mute"])
            response = self._ipc(["get_property", "mute"])
            if response and "data" in response:
                self.state.muted = bool(response["data"])

    def _start_poll(self) -> None:
        if self._poll_thread and self._poll_thread.is_alive():
            self._stop_polling.set()
            self._poll_thread.join(timeout=1)
        self._stop_polling.clear()

        def poll() -> None:
            misses = 0
            while not self._stop_polling.is_set():
                response = self._ipc(["get_property", "media-title"])
                title = ""
                if response and response.get("data"):
                    title = str(response["data"])
                if title:
                    misses = 0
                    with self._lock:
                        self.state.media_title = title
                        self.state.offline = title == "Station Offline"
                else:
                    misses += 1
                    if misses >= 3:
                        with self._lock:
                            self.state.media_title = "Station Offline"
                            self.state.offline = True
                self._stop_polling.wait(3)

        self._poll_thread = threading.Thread(target=poll, daemon=True)
        self._poll_thread.start()

    def snapshot(self) -> PlayerState:
        with self._lock:
            return PlayerState(
                station_name=self.state.station_name,
                stream_url=self.state.stream_url,
                media_title=self.state.media_title,
                volume=self.state.volume,
                muted=self.state.muted,
                playing=self.state.playing,
                offline=self.state.offline,
            )