diff --git a/.github/windows-exe-imports.txt b/.github/windows-exe-imports.txt index 2260381a7..6f3ba2d7a 100644 --- a/.github/windows-exe-imports.txt +++ b/.github/windows-exe-imports.txt @@ -39,6 +39,7 @@ crypt32.dll dbghelp.dll dwmapi.dll gdi32.dll +iphlpapi.dll kernel32.dll libmpv-2.dll ntdll.dll diff --git a/Cargo.lock b/Cargo.lock index 23dfd46f1..77654132f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -739,6 +739,18 @@ dependencies = [ "toml 0.9.12+spec-1.1.0", ] +[[package]] +name = "cast" +version = "1.4.0" +dependencies = [ + "mdns-sd", + "serde", + "serde_json", + "tokio", + "tokio-rustls", + "tracing", +] + [[package]] name = "cc" version = "1.2.65" @@ -2052,6 +2064,17 @@ dependencies = [ "miniz_oxide 0.8.9", ] +[[package]] +name = "flume" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e139bc46ca777eb5efaf62df0ab8cc5fd400866427e56c68b22e414e53bd3be" +dependencies = [ + "futures-core", + "futures-sink", + "spin", +] + [[package]] name = "fnv" version = "1.0.7" @@ -3017,6 +3040,16 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "if-addrs" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0a05c691e1fae256cf7013d99dad472dc52d5543322761f83ec8d47eab40d2b" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "if_chain" version = "1.0.3" @@ -3444,9 +3477,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.186" +version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" [[package]] name = "libdbus-sys" @@ -3529,6 +3562,7 @@ dependencies = [ "any_ascii", "anyhow", "base64 0.22.1", + "cast", "csv", "discord-rich-presence", "futures-util", @@ -3537,6 +3571,7 @@ dependencies = [ "http-body-util", "hyper", "hyper-util", + "if-addrs", "innertube", "karaoke", "ksni", @@ -3984,6 +4019,21 @@ dependencies = [ "digest", ] +[[package]] +name = "mdns-sd" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6743ed743ff2bada2b8651ef298d3f98addb0b74844f19cdd57137d8bd0219e" +dependencies = [ + "fastrand 2.4.1", + "flume", + "if-addrs", + "log", + "mio", + "socket-pktinfo", + "socket2 0.6.4", +] + [[package]] name = "memchr" version = "2.8.2" @@ -6235,6 +6285,17 @@ version = "1.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" +[[package]] +name = "socket-pktinfo" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512f5c35377cf69f350b106df2366c3a1df6bd01b047a7a58c8390b70d28838d" +dependencies = [ + "libc", + "socket2 0.6.4", + "windows-sys 0.61.2", +] + [[package]] name = "socket2" version = "0.4.10" @@ -6341,6 +6402,15 @@ dependencies = [ "zvariant 3.15.2", ] +[[package]] +name = "spin" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" +dependencies = [ + "lock_api", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" diff --git a/Cargo.toml b/Cargo.toml index 9372e35ec..bb6c0640f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "crates/listen-protocol", "crates/sync-server", "crates/karaoke", + "crates/cast", "src-tauri", ] diff --git a/README.md b/README.md index 584670cb7..7344398b3 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ Discord. - Media keys and now playing on every OS - `Ctrl+K` to search, `Ctrl+H` for every shortcut - Self-updating builds +- Cast to a Chromecast, a Google Home or Nest speaker, or a TV with Google Cast ([how](docs/CASTING.md)) diff --git a/THIRD-PARTY-NOTICES.md b/THIRD-PARTY-NOTICES.md index 129f89cfa..735fd0488 100644 --- a/THIRD-PARTY-NOTICES.md +++ b/THIRD-PARTY-NOTICES.md @@ -50,3 +50,7 @@ on Linux/macOS next to the library in `/runtime/onnxruntime-1.24.4 ## Microsoft Visual C++ Redistributable (Windows) - Limusic ships four Microsoft Visual C++ runtime DLLs in the Windows installer, in the `vcruntime` folder next to the app: vcruntime140.dll, vcruntime140_1.dll, msvcp140.dll and msvcp140_1.dll (under 1 MB in total, version 14.44 or newer), unmodified, copied from the Visual Studio redistributable folder that Microsoft's licence terms allow applications to redistribute. `onnxruntime.dll` needs them. They are only loaded when karaoke is turned on; the loader refuses a copy older than 14.44, and if the folder is missing it uses the system's copy instead (the Microsoft Visual C++ Redistributable 14.44 or newer), showing a message with Microsoft's download link when that is missing or older. + +## Casting (`mdns-sd`) + +- The [`mdns-sd`](https://github.com/keepsimple1/mdns-sd) crate (0.21.7, Apache-2.0 OR MIT) is used for finding Cast devices on the local network over mDNS. diff --git a/crates/cast/Cargo.toml b/crates/cast/Cargo.toml new file mode 100644 index 000000000..11cf5b841 --- /dev/null +++ b/crates/cast/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "cast" +version.workspace = true +edition.workspace = true +license.workspace = true + +[dependencies] +mdns-sd = "0.21.7" +serde.workspace = true +serde_json.workspace = true +tokio = { workspace = true, features = ["net", "io-util"] } +# The device's control channel is TLS with a self-signed certificate. ring, like the rest of the +# tree's rustls users, so no second crypto provider is built. +tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] } +tracing.workspace = true diff --git a/crates/cast/src/channel.rs b/crates/cast/src/channel.rs new file mode 100644 index 000000000..4611a9a0d --- /dev/null +++ b/crates/cast/src/channel.rs @@ -0,0 +1,790 @@ +//! One connection to a Cast device, talking to Google's Default Media Receiver. +//! +//! A Cast device listens on TLS (port 8009 for a single device, another port for a speaker +//! group) and speaks CastV2: a protobuf envelope ([`crate::proto`]) carrying JSON. Four +//! namespaces are used: +//! +//! - `tp.connection`: CONNECT opens a virtual connection to the platform (`receiver-0`) or to the +//! receiver app's transport; CLOSE ends one. +//! - `tp.heartbeat`: PING/PONG. Both sides ping; a side that hears nothing drops the other. +//! - `receiver`: LAUNCH the receiver app, GET_STATUS, the speaker's volume, STOP the app. +//! - `media`: the queue and the transport. +//! +//! The socket is owned by one task. Commands go in over a channel, [`Event`]s come back over +//! another. The device pings every 5 s, so 15 s without a single frame means it is gone, and the +//! connection ends with [`Closed::Lost`] instead of waiting minutes for TCP to give up. + +use std::net::IpAddr; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use serde_json::{json, Value}; +use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, WriteHalf}; +use tokio::net::TcpStream; +use tokio::sync::{mpsc, oneshot}; +use tokio_rustls::rustls; + +use crate::proto; + +const NS_CONNECTION: &str = "urn:x-cast:com.google.cast.tp.connection"; +const NS_HEARTBEAT: &str = "urn:x-cast:com.google.cast.tp.heartbeat"; +const NS_RECEIVER: &str = "urn:x-cast:com.google.cast.receiver"; +const NS_MEDIA: &str = "urn:x-cast:com.google.cast.media"; + +/// The device itself, before any app runs on it. +const PLATFORM: &str = "receiver-0"; +/// This end of every virtual connection. +const SENDER: &str = "sender-0"; + +/// Google's built-in receiver. Needs no registration, shows title, artist and cover. +pub const DEFAULT_MEDIA_RECEIVER: &str = "CC1AD845"; + +const CONNECT_TIMEOUT: Duration = Duration::from_secs(5); +/// From the TLS handshake to the receiver app answering on its transport. +const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(8); +const HEARTBEAT: Duration = Duration::from_secs(5); +/// No frame for this long and the device is gone. It pings every 5 s. +const IDLE: Duration = + if cfg!(test) { Duration::from_millis(2000) } else { Duration::from_secs(15) }; +/// How long commands wait for the media session a load creates before they go out anyway. +const GATE: Duration = Duration::from_secs(8); +/// How long a queue edit waits for the item ids before it goes ahead with what it has. +const IDS_WAIT: Duration = Duration::from_secs(3); +/// A backstop for statuses the device didn't push. +const STATUS_POLL: Duration = Duration::from_secs(5); +/// How long the next song is buffered before the current one ends. +const PRELOAD_SECS: f64 = 20.0; + +/// One track for the device. +#[derive(Debug, Clone, PartialEq)] +pub struct Media { + /// Where the device fetches it from. + pub url: String, + pub content_type: String, + pub title: String, + pub artist: String, + pub album: Option, + /// Cover art. The device fetches it itself, so it must be reachable from the device. + pub image: Option, + pub duration: Option, +} + +/// The receiver's repeat mode. Repeat-all is the app's business: it wraps its own queue. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Repeat { + Off, + One, +} + +impl Repeat { + fn wire(self) -> &'static str { + match self { + Repeat::Off => "REPEAT_OFF", + Repeat::One => "REPEAT_SINGLE", + } + } +} + +/// What the app asks of the device. +#[derive(Debug)] +pub enum Command { + /// Replace the device's queue. `start` is the position in the first item. + Load { + items: Vec, + start: f64, + autoplay: bool, + repeat: Repeat, + }, + /// Add one item after the last one. + Append(Media), + /// Remove every item after the one playing. + DropNext, + /// Remove every item before the one playing. + TrimPlayed, + Repeat(Repeat), + /// Jump to the next item. + Next, + Play, + Pause, + Seek(f64), + /// The speaker's level, 0.0 to 1.0. + Volume(f64), + /// Stop the media and the receiver app, then close. The ack fires once it is written. + Close(oneshot::Sender<()>), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum State { + Idle, + Playing, + Paused, + Buffering, +} + +/// The media status the device reports. +#[derive(Debug, Clone, PartialEq)] +pub struct MediaStatus { + pub session: i64, + pub state: State, + /// Why the device went idle: `FINISHED`, `ERROR`, `CANCELLED`, `INTERRUPTED`. + pub idle_reason: Option, + pub position: f64, + pub duration: Option, + /// The URL of the item playing. Each URL handed over is unique, so this names the item. + pub content_id: Option, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Event { + /// The media status changed. `None`: the device has no media session. + Media(Option), + /// The speaker's volume, 0.0 to 1.0. + Volume { level: f64, muted: bool }, + /// The device refused a load. + LoadFailed(String), + /// The connection ended. Always the last event. + Closed(Closed), +} + +/// Why a connection ended or never started. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Closed { + /// It couldn't be reached at all. + Unreachable(String), + /// It refused to start the receiver. + Refused(String), + /// It stopped answering. + Lost, + /// Another sender started something else on it. + TakenOver, + /// The receiver app was stopped on the device (the Google Home app, a voice command). + Stopped, + /// The app closed it, or the socket ended. + Ended(String), +} + +/// The app's end of a connection. Dropping every clone closes it without stopping the device. +#[derive(Clone, Debug)] +pub struct Sender(mpsc::UnboundedSender); + +impl Sender { + pub fn send(&self, cmd: Command) { + let _ = self.0.send(cmd); + } + + /// Stop the device and close, waiting at most `limit` for the stop to be written. + pub async fn close(&self, limit: Duration) { + let (tx, rx) = oneshot::channel(); + if self.0.send(Command::Close(tx)).is_ok() { + let _ = tokio::time::timeout(limit, rx).await; + } + } +} + +/// Connect to a device and start (or join) the Default Media Receiver on it. +pub async fn connect( + ip: IpAddr, + port: u16, +) -> Result<(Sender, mpsc::UnboundedReceiver), Closed> { + let unreachable = |e: &dyn std::fmt::Display| Closed::Unreachable(e.to_string()); + let tcp = tokio::time::timeout(CONNECT_TIMEOUT, TcpStream::connect((ip, port))) + .await + .map_err(|e| unreachable(&e))? + .map_err(|e| unreachable(&e))?; + let _ = tcp.set_nodelay(true); + let tls = tokio_rustls::TlsConnector::from(Arc::new(tls_config())); + let stream = tokio::time::timeout(CONNECT_TIMEOUT, tls.connect(ip.into(), tcp)) + .await + .map_err(|e| unreachable(&e))? + .map_err(|e| unreachable(&e))?; + start(stream).await +} + +/// Run the protocol over an open stream. Split from [`connect`] so tests can hand it any stream. +async fn start(stream: S) -> Result<(Sender, mpsc::UnboundedReceiver), Closed> +where + S: AsyncRead + AsyncWrite + Send + 'static, +{ + let (cmd_tx, cmd_rx) = mpsc::unbounded_channel(); + let (ev_tx, ev_rx) = mpsc::unbounded_channel(); + let (ready_tx, ready_rx) = oneshot::channel(); + tokio::spawn(run(stream, cmd_rx, ev_tx, ready_tx)); + match tokio::time::timeout(HANDSHAKE_TIMEOUT, ready_rx).await { + Ok(Ok(Ok(()))) => Ok((Sender(cmd_tx), ev_rx)), + Ok(Ok(Err(closed))) => Err(closed), + _ => Err(Closed::Unreachable("the receiver did not answer".into())), + } +} + +/// What the socket task knows about the receiver. +struct Link { + writer: WriteHalf, + request: i64, + /// The receiver app's session (named in payloads) and transport (the address of its + /// connection). Both `None` until the app answers. + session: Option, + transport: Option, + launched: bool, + /// The media session playing; transport commands name it. 0: none. + media_session: i64, + /// A load is in flight: until the device reports a session other than this one, commands + /// that name a session would name the old one, so they wait. + gate: Option<(Instant, i64)>, + /// The commands waiting, in order. + waiting: Vec, + /// The queue's item ids in order, and the one playing. Queue edits are by id. + ids: Vec, + current_item: Option, + /// The ids were asked for and haven't come back. + ids_asked: Option, + last_volume: Option<(f64, bool)>, +} + +async fn run( + stream: S, + mut cmds: mpsc::UnboundedReceiver, + events: mpsc::UnboundedSender, + ready: oneshot::Sender>, +) where + S: AsyncRead + AsyncWrite + Send + 'static, +{ + let (mut reader, writer) = tokio::io::split(stream); + // The reader is its own task: a read cancelled by `select!` would lose half a frame. + let (frame_tx, mut frames) = mpsc::unbounded_channel::>(); + let reader_task = tokio::spawn(async move { + loop { + let mut len = [0u8; 4]; + if let Err(e) = reader.read_exact(&mut len).await { + let _ = frame_tx.send(Err(e.to_string())); + return; + } + let len = u32::from_be_bytes(len) as usize; + if len > proto::MAX_FRAME { + let _ = frame_tx.send(Err("oversized frame".into())); + return; + } + let mut body = vec![0u8; len]; + if let Err(e) = reader.read_exact(&mut body).await { + let _ = frame_tx.send(Err(e.to_string())); + return; + } + if let Some(frame) = proto::decode(&body) { + if frame_tx.send(Ok(frame)).is_err() { + return; + } + } + } + }); + + let mut link = Link { + writer, + request: 0, + session: None, + transport: None, + launched: false, + media_session: 0, + gate: None, + waiting: Vec::new(), + ids: Vec::new(), + current_item: None, + ids_asked: None, + last_volume: None, + }; + let mut ready = Some(ready); + let mut last_heard = Instant::now(); + let mut heartbeat = tokio::time::interval(HEARTBEAT); + let mut tick = tokio::time::interval(Duration::from_millis(200)); + let mut poll = tokio::time::interval(STATUS_POLL); + + let opened = async { + link.write(NS_CONNECTION, PLATFORM, connect_payload()).await?; + link.write_receiver(json!({ "type": "GET_STATUS" })).await + }; + let closed = match opened.await { + Err(e) => e, + Ok(()) => loop { + let step = tokio::select! { + cmd = cmds.recv() => match cmd { + // Every sender dropped: close without stopping the device. + None => Err(Closed::Ended("closed by the app".into())), + Some(Command::Close(ack)) => { + link.close().await; + let _ = ack.send(()); + Err(Closed::Ended("closed by the app".into())) + } + Some(cmd) => link.command(cmd).await, + }, + frame = frames.recv() => match frame { + Some(Ok(frame)) => { + last_heard = Instant::now(); + link.frame(frame, &events, &mut ready).await + } + Some(Err(e)) => Err(Closed::Ended(e)), + None => Err(Closed::Ended("the connection closed".into())), + }, + _ = heartbeat.tick() => { + link.write(NS_HEARTBEAT, PLATFORM, json!({ "type": "PING" })).await + } + _ = tick.tick() => { + if last_heard.elapsed() > IDLE { + Err(Closed::Lost) + } else { + link.timers().await + } + } + _ = poll.tick() => link.poll().await, + }; + if let Err(e) = step { + break e; + } + }, + }; + reader_task.abort(); + match ready.take() { + Some(ready) => { + let _ = ready.send(Err(closed)); + } + None => { + let _ = events.send(Event::Closed(closed)); + } + } +} + +type Step = Result<(), Closed>; + +impl Link { + async fn write(&mut self, ns: &str, to: &str, mut payload: Value) -> Step { + self.request += 1; + payload["requestId"] = json!(self.request); + let msg = proto::frame(ns, SENDER, to, &payload.to_string()); + self.writer.write_all(&msg).await.map_err(|e| Closed::Ended(e.to_string())) + } + + async fn write_receiver(&mut self, payload: Value) -> Step { + self.write(NS_RECEIVER, PLATFORM, payload).await + } + + /// A media message to the receiver app. Dropped when there is no app yet. + async fn write_media(&mut self, payload: Value) -> Step { + let Some(to) = self.transport.clone() else { return Ok(()) }; + self.write(NS_MEDIA, &to, payload).await + } + + /// A transport command naming the media session playing. + async fn write_session(&mut self, mut payload: Value) -> Step { + payload["mediaSessionId"] = json!(self.media_session); + self.write_media(payload).await + } + + async fn command(&mut self, cmd: Command) -> Step { + match cmd { + Command::Volume(level) => { + self.write_receiver(json!({ + "type": "SET_VOLUME", "volume": { "level": level.clamp(0.0, 1.0) }, + })) + .await + } + Command::Load { .. } if self.gate.is_some() || self.transport.is_none() => { + // The queue is the truth: whatever waited belongs to the queue this replaces. + self.waiting.clear(); + self.waiting.push(cmd); + Ok(()) + } + Command::Load { items, start, autoplay, repeat } => { + let items: Vec = items + .iter() + .enumerate() + .map(|(i, m)| queue_item(m, i > 0 || autoplay)) + .collect(); + let session = self.session.clone().unwrap_or_default(); + self.write_media(json!({ + "type": "QUEUE_LOAD", "sessionId": session, "items": items, "startIndex": 0, + "currentTime": start, "repeatMode": repeat.wire(), + })) + .await?; + self.gate = Some((Instant::now() + GATE, self.media_session)); + self.ids.clear(); + self.current_item = None; + if !autoplay { + // In case the receiver ignores the item's `autoplay`: a pause on a paused + // session changes nothing. + self.waiting.push(Command::Pause); + } + Ok(()) + } + cmd if self.gate.is_some() => { + self.wait(cmd); + Ok(()) + } + // Queue edits keep their order behind ones that wait for ids; the transport doesn't + // have to, or a pause would sit behind a reorder for seconds. + cmd if !self.waiting.is_empty() && !is_transport(&cmd) => { + self.wait(cmd); + Ok(()) + } + // Nothing is loaded: there is nothing for a transport command to act on. + _ if self.media_session == 0 => Ok(()), + cmd @ (Command::DropNext | Command::TrimPlayed) if self.ids_asked.is_some() => { + self.wait(cmd); + Ok(()) + } + Command::DropNext => { + let at = self.playing_index(); + let after: Vec = at.map(|i| self.ids[i + 1..].to_vec()).unwrap_or_default(); + self.remove(after).await + } + Command::TrimPlayed => { + let at = self.playing_index(); + let before: Vec = at.map(|i| self.ids[..i].to_vec()).unwrap_or_default(); + self.remove(before).await + } + Command::Append(media) => { + self.write_session( + json!({ "type": "QUEUE_INSERT", "items": [queue_item(&media, true)] }), + ) + .await?; + self.ask_ids().await + } + Command::Repeat(repeat) => { + self.write_session(json!({ "type": "QUEUE_UPDATE", "repeatMode": repeat.wire() })) + .await + } + Command::Next => self.write_session(json!({ "type": "QUEUE_UPDATE", "jump": 1 })).await, + Command::Play => self.write_session(json!({ "type": "PLAY" })).await, + Command::Pause => self.write_session(json!({ "type": "PAUSE" })).await, + Command::Seek(at) => { + self.write_session(json!({ "type": "SEEK", "currentTime": at.max(0.0) })).await + } + Command::Close(_) => Ok(()), // handled by the loop + } + } + + /// Hold a command, keeping only the latest of a kind where an older one means nothing. + fn wait(&mut self, cmd: Command) { + let same = |c: &Command| { + matches!( + (&cmd, c), + (Command::Play | Command::Pause, Command::Play | Command::Pause) + | (Command::Seek(_), Command::Seek(_)) + | (Command::Repeat(_), Command::Repeat(_)) + ) + }; + self.waiting.retain(|c| !same(c)); + self.waiting.push(cmd); + } + + fn playing_index(&self) -> Option { + let current = self.current_item?; + self.ids.iter().position(|id| *id == current) + } + + async fn remove(&mut self, ids: Vec) -> Step { + if ids.is_empty() { + return Ok(()); + } + self.write_session(json!({ "type": "QUEUE_REMOVE", "itemIds": ids })).await?; + self.ask_ids().await + } + + async fn ask_ids(&mut self) -> Step { + self.ids_asked = Some(Instant::now()); + self.write_session(json!({ "type": "QUEUE_GET_ITEM_IDS" })).await + } + + /// Send what waited, in order, until something has to wait again. + async fn flush(&mut self) -> Step { + let waiting = std::mem::take(&mut self.waiting); + let mut rest = waiting.into_iter(); + while let Some(cmd) = rest.next() { + let needs_ids = matches!(cmd, Command::DropNext | Command::TrimPlayed); + if self.gate.is_some() || (needs_ids && self.ids_asked.is_some()) { + // Keep the order: this and what follows wait behind what just went out. + self.waiting.push(cmd); + self.waiting.extend(rest); + break; + } + self.command(cmd).await?; + } + Ok(()) + } + + async fn timers(&mut self) -> Step { + let now = Instant::now(); + if self.gate.is_some_and(|(until, _)| now >= until) { + tracing::debug!("cast: the device never reported the load's session"); + self.gate = None; + } + if self.ids_asked.is_some_and(|at| now >= at + IDS_WAIT) { + self.ids_asked = None; + } + if self.gate.is_none() && self.ids_asked.is_none() && !self.waiting.is_empty() { + self.flush().await?; + } + Ok(()) + } + + async fn poll(&mut self) -> Step { + if self.transport.is_some() { + self.write_media(json!({ "type": "GET_STATUS" })).await?; + } + self.write_receiver(json!({ "type": "GET_STATUS" })).await + } + + async fn close(&mut self) { + if self.media_session != 0 { + let _ = self.write_session(json!({ "type": "STOP" })).await; + } + if let Some(session) = self.session.clone() { + let _ = self.write_receiver(json!({ "type": "STOP", "sessionId": session })).await; + } + if let Some(to) = self.transport.clone() { + let _ = self.write(NS_CONNECTION, &to, json!({ "type": "CLOSE" })).await; + } + let _ = self.writer.flush().await; + } + + async fn frame( + &mut self, + frame: proto::Frame, + events: &mpsc::UnboundedSender, + ready: &mut Option>>, + ) -> Step { + let Ok(msg) = serde_json::from_str::(&frame.payload) else { return Ok(()) }; + let kind = msg.get("type").and_then(Value::as_str).unwrap_or_default(); + match (frame.namespace.as_str(), kind) { + (NS_HEARTBEAT, "PING") => { + self.write(NS_HEARTBEAT, &frame.source, json!({ "type": "PONG" })).await + } + (NS_CONNECTION, "CLOSE") if Some(&frame.source) == self.transport.as_ref() => { + Err(Closed::TakenOver) + } + (NS_RECEIVER, "RECEIVER_STATUS") => self.receiver_status(&msg, events, ready).await, + (NS_RECEIVER, "LAUNCH_ERROR") => { + let reason = msg.get("reason").and_then(Value::as_str).unwrap_or("LAUNCH_ERROR"); + Err(Closed::Refused(reason.to_owned())) + } + (NS_MEDIA, "MEDIA_STATUS") => { + let entry = msg.get("status").and_then(Value::as_array).and_then(|s| s.first()); + let status = entry.and_then(media_status); + if let Some(entry) = entry { + self.current_item = entry.get("currentItemId").and_then(Value::as_i64); + // Only a fallback for a receiver that never answers QUEUE_GET_ITEM_IDS: the + // list in a status isn't promised to be the whole queue. + if self.ids.is_empty() { + if let Some(items) = entry.get("items").and_then(Value::as_array) { + self.ids = items.iter().filter_map(|i| i["itemId"].as_i64()).collect(); + } + } + } + let session = status.as_ref().map_or(0, |s| s.session); + self.media_session = session; + if self.gate.is_some_and(|(_, old)| session != 0 && session != old) { + self.gate = None; + self.ask_ids().await?; + } + let _ = events.send(Event::Media(status)); + Ok(()) + } + (NS_MEDIA, "QUEUE_ITEM_IDS") => { + if let Some(ids) = msg.get("itemIds").and_then(Value::as_array) { + self.ids = ids.iter().filter_map(Value::as_i64).collect(); + } + self.ids_asked = None; + if self.gate.is_none() { + self.flush().await?; + } + Ok(()) + } + (NS_MEDIA, "QUEUE_CHANGE") => self.ask_ids().await, + (NS_MEDIA, "LOAD_FAILED" | "LOAD_CANCELLED" | "INVALID_REQUEST") => { + let reason = msg.get("reason").and_then(Value::as_str).unwrap_or(kind); + tracing::warn!(kind, reason, "cast: the device refused a request"); + // A command against a session that just ended is answered this way and means + // nothing, and a cancelled load was replaced or stopped on purpose. A refusal + // while a load is in flight is that load. + let stale = reason == "INVALID_MEDIA_SESSION_ID" || kind == "LOAD_CANCELLED"; + let was_loading = self.gate.take().is_some(); + if !stale && (was_loading || kind == "LOAD_FAILED") { + self.waiting.retain(|c| matches!(c, Command::Load { .. })); + let _ = events.send(Event::LoadFailed(reason.to_owned())); + } + self.flush().await + } + _ => Ok(()), + } + } + + async fn receiver_status( + &mut self, + msg: &Value, + events: &mpsc::UnboundedSender, + ready: &mut Option>>, + ) -> Step { + let status = &msg["status"]; + if let Some(level) = status["volume"]["level"].as_f64() { + let muted = status["volume"]["muted"].as_bool().unwrap_or(false); + if self.last_volume != Some((level, muted)) { + self.last_volume = Some((level, muted)); + let _ = events.send(Event::Volume { level, muted }); + } + } + // No application list: nothing is running on the device. + let none = Vec::new(); + let apps = status.get("applications").and_then(Value::as_array).unwrap_or(&none); + let ours = apps + .iter() + .find(|a| a["appId"].as_str() == Some(DEFAULT_MEDIA_RECEIVER)) + .and_then(|a| Some((a["sessionId"].as_str()?, a["transportId"].as_str()?))); + match (ours, self.session.as_deref()) { + // Still running, the same session. + (Some((session, _)), Some(known)) if session == known => Ok(()), + // Stopped, with nothing in its place. + (None, Some(_)) if apps.is_empty() => Err(Closed::Stopped), + // Replaced by another app, or by another sender's session. + (_, Some(_)) => Err(Closed::TakenOver), + // Running before this connection (left from an earlier one, or another sender's): + // join it rather than launching it again. + (Some((session, transport)), None) => { + self.session = Some(session.to_owned()); + self.transport = Some(transport.to_owned()); + self.write(NS_CONNECTION, transport, connect_payload()).await?; + self.write_media(json!({ "type": "GET_STATUS" })).await?; + if let Some(ready) = ready.take() { + let _ = ready.send(Ok(())); + } + self.flush().await + } + (None, None) => self.launch().await, + } + } + + async fn launch(&mut self) -> Step { + if self.launched { + return Ok(()); + } + self.launched = true; + self.write_receiver(json!({ "type": "LAUNCH", "appId": DEFAULT_MEDIA_RECEIVER })).await + } +} + +/// Commands that act on the playing session and not on the queue. +fn is_transport(cmd: &Command) -> bool { + matches!( + cmd, + Command::Play | Command::Pause | Command::Seek(_) | Command::Repeat(_) | Command::Volume(_) + ) +} + +/// The CONNECT payload. `userAgent` names the sender; it can't rename the receiver app, which +/// Google calls "Default Media Receiver". +fn connect_payload() -> Value { + json!({ + "type": "CONNECT", + "origin": {}, + "userAgent": "Limusic", + "senderInfo": { + "sdkType": 2, + "version": env!("CARGO_PKG_VERSION"), + "browserVersion": "1.0.0", + "platform": 4, + "systemVersion": "Limusic", + "connectionType": 1, + }, + }) +} + +fn queue_item(media: &Media, autoplay: bool) -> Value { + let mut metadata = json!({ "metadataType": 3, "title": media.title, "artist": media.artist }); + if let Some(album) = &media.album { + metadata["albumName"] = json!(album); + } + if let Some(image) = &media.image { + metadata["images"] = json!([{ "url": image }]); + } + let mut info = json!({ + "contentId": media.url, + "contentUrl": media.url, + "contentType": media.content_type, + "streamType": "BUFFERED", + "metadata": metadata, + }); + // A null duration reads as zero on the receiver, which then thinks the track is over. + if let Some(d) = media.duration.filter(|d| *d > 0.0) { + info["duration"] = json!(d); + } + json!({ "media": info, "autoplay": autoplay, "preloadTime": PRELOAD_SECS }) +} + +/// One entry of a MEDIA_STATUS. +fn media_status(entry: &Value) -> Option { + let state = match entry.get("playerState")?.as_str()? { + "PLAYING" => State::Playing, + "PAUSED" => State::Paused, + "BUFFERING" => State::Buffering, + _ => State::Idle, + }; + Some(MediaStatus { + session: entry.get("mediaSessionId").and_then(Value::as_i64).unwrap_or(0), + state, + idle_reason: entry.get("idleReason").and_then(Value::as_str).map(str::to_owned), + position: entry.get("currentTime").and_then(Value::as_f64).unwrap_or(0.0), + duration: entry["media"]["duration"].as_f64(), + content_id: entry["media"]["contentId"].as_str().map(str::to_owned), + }) +} + +/// The device's certificate is self-signed, so there is nothing to check it against. The +/// device is found on the local network and identified by its mDNS id. +fn tls_config() -> rustls::ClientConfig { + use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier}; + use rustls::pki_types::{CertificateDer, ServerName, UnixTime}; + + #[derive(Debug)] + struct AnyCertificate(Arc); + + impl ServerCertVerifier for AnyCertificate { + fn verify_server_cert( + &self, + _: &CertificateDer<'_>, + _: &[CertificateDer<'_>], + _: &ServerName<'_>, + _: &[u8], + _: UnixTime, + ) -> Result { + Ok(ServerCertVerified::assertion()) + } + + fn verify_tls12_signature( + &self, + _: &[u8], + _: &CertificateDer<'_>, + _: &rustls::DigitallySignedStruct, + ) -> Result { + Ok(HandshakeSignatureValid::assertion()) + } + + fn verify_tls13_signature( + &self, + _: &[u8], + _: &CertificateDer<'_>, + _: &rustls::DigitallySignedStruct, + ) -> Result { + Ok(HandshakeSignatureValid::assertion()) + } + + fn supported_verify_schemes(&self) -> Vec { + self.0.signature_verification_algorithms.supported_schemes() + } + } + + let provider = Arc::new(rustls::crypto::ring::default_provider()); + rustls::ClientConfig::builder_with_provider(provider.clone()) + .with_safe_default_protocol_versions() + .expect("ring speaks TLS 1.2 and 1.3") + .dangerous() + .with_custom_certificate_verifier(Arc::new(AnyCertificate(provider))) + .with_no_client_auth() +} + +#[cfg(test)] +mod tests; diff --git a/crates/cast/src/channel/tests.rs b/crates/cast/src/channel/tests.rs new file mode 100644 index 000000000..aecd82289 --- /dev/null +++ b/crates/cast/src/channel/tests.rs @@ -0,0 +1,302 @@ +//! The channel against a fake receiver over real TLS: what it sends, in what order, and how it +//! notices a device that goes away. The fake answers the way the Default Media Receiver does for +//! the messages used here; it is not a replacement for a test on hardware. + +use super::*; +use std::sync::Mutex; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::TcpListener; +use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer}; + +/// How the fake behaves, and what it heard. +#[derive(Default)] +struct Fake { + /// The receiver app is already running when the sender connects. + running: bool, + /// Stop pinging and answering after the receiver app starts. + go_silent: bool, + /// Another app runs instead of the receiver (a phone cast YouTube). + other_app: bool, + /// The media messages it got, as `TYPE` or `TYPE(mediaSessionId)`. + heard: Vec, + session: i64, + ids: Vec, + next_id: i64, +} + +type Shared = Arc>; + +async fn fake_receiver(fake: Shared) -> u16 { + let cert = CertificateDer::from( + include_bytes!("../../tests/fixtures/fake-receiver-cert.der").to_vec(), + ); + let key = PrivateKeyDer::try_from( + include_bytes!("../../tests/fixtures/fake-receiver-key.der").to_vec(), + ) + .unwrap(); + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let config = rustls::ServerConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .unwrap() + .with_no_client_auth() + .with_single_cert(vec![cert], key) + .unwrap(); + let acceptor = tokio_rustls::TlsAcceptor::from(Arc::new(config)); + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let tls = acceptor.accept(tcp).await.unwrap(); + let (mut rd, wr) = tokio::io::split(tls); + let wr = Arc::new(tokio::sync::Mutex::new(wr)); + let send = + |wr: Arc>>, ns: &'static str, from: &str, v: Value| { + let msg = proto::frame(ns, from, SENDER, &v.to_string()); + async move { + let _ = wr.lock().await.write_all(&msg).await; + } + }; + let pinger = { + let (wr, fake) = (wr.clone(), fake.clone()); + tokio::spawn(async move { + loop { + tokio::time::sleep(Duration::from_millis(150)).await; + if fake.lock().unwrap().go_silent && fake.lock().unwrap().session >= 0 { + let started = fake.lock().unwrap().running; + if started { + continue; + } + } + send(wr.clone(), NS_HEARTBEAT, PLATFORM, json!({ "type": "PING" })).await; + } + }) + }; + loop { + let mut len = [0u8; 4]; + if rd.read_exact(&mut len).await.is_err() { + break; + } + let mut body = vec![0u8; u32::from_be_bytes(len) as usize]; + if rd.read_exact(&mut body).await.is_err() { + break; + } + let frame = proto::decode(&body).unwrap(); + let msg: Value = serde_json::from_str(&frame.payload).unwrap(); + let kind = msg["type"].as_str().unwrap_or_default().to_owned(); + if fake.lock().unwrap().go_silent && fake.lock().unwrap().running { + continue; + } + let apps = |f: &Fake| { + if f.other_app { + json!([{ "appId": "233637DE", "sessionId": "y1", "transportId": "y1" }]) + } else if f.running { + json!([{ "appId": DEFAULT_MEDIA_RECEIVER, "sessionId": "s1", "transportId": "t1" }]) + } else { + json!([]) + } + }; + let receiver_status = |f: &Fake| { + json!({ "type": "RECEIVER_STATUS", "status": { + "applications": apps(f), "volume": { "level": 0.3, "muted": false } } }) + }; + let media_status = |f: &Fake, state: &str| { + json!({ "type": "MEDIA_STATUS", "status": [{ + "mediaSessionId": f.session, "playerState": state, "currentTime": 1.0, + "currentItemId": f.ids.first(), "media": { "contentId": "http://lan/a" } }] }) + }; + match (frame.namespace.as_str(), kind.as_str()) { + (NS_RECEIVER, "GET_STATUS") => { + let v = receiver_status(&fake.lock().unwrap()); + send(wr.clone(), NS_RECEIVER, PLATFORM, v).await; + } + (NS_RECEIVER, "LAUNCH") => { + let v = { + let mut f = fake.lock().unwrap(); + f.heard.push("LAUNCH".into()); + f.running = true; + receiver_status(&f) + }; + send(wr.clone(), NS_RECEIVER, PLATFORM, v).await; + } + (NS_MEDIA, _) => { + let reply = { + let mut f = fake.lock().unwrap(); + let named = msg["mediaSessionId"].as_i64(); + f.heard.push(match named { + Some(s) => format!("{kind}({s})"), + None => kind.clone(), + }); + match kind.as_str() { + "QUEUE_LOAD" => { + f.session += 1; + f.ids.clear(); + for _ in msg["items"].as_array().unwrap() { + f.next_id += 1; + let id = f.next_id; + f.ids.push(id); + } + Some(media_status(&f, "PLAYING")) + } + "QUEUE_INSERT" => { + f.next_id += 1; + let id = f.next_id; + f.ids.push(id); + None + } + "QUEUE_REMOVE" => { + let gone: Vec = msg["itemIds"] + .as_array() + .unwrap() + .iter() + .filter_map(Value::as_i64) + .collect(); + f.ids.retain(|id| !gone.contains(id)); + None + } + "QUEUE_GET_ITEM_IDS" => { + Some(json!({ "type": "QUEUE_ITEM_IDS", "itemIds": f.ids })) + } + "PAUSE" => Some(media_status(&f, "PAUSED")), + _ => None, + } + }; + if let Some(v) = reply { + send(wr.clone(), NS_MEDIA, "t1", v).await; + } + } + _ => {} + } + } + pinger.abort(); + }); + port +} + +fn track(url: &str) -> Media { + Media { + url: url.into(), + content_type: "audio/webm".into(), + title: "A song".into(), + artist: "Someone".into(), + album: None, + image: None, + duration: Some(200.0), + } +} + +async fn open(fake: &Shared) -> (Sender, mpsc::UnboundedReceiver) { + let port = fake_receiver(fake.clone()).await; + connect("127.0.0.1".parse().unwrap(), port).await.expect("connects") +} + +/// Wait for the fake to have heard `n` media messages. +async fn heard(fake: &Shared, n: usize) -> Vec { + for _ in 0..250 { + let heard = fake.lock().unwrap().heard.clone(); + if heard.len() >= n { + return heard; + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + fake.lock().unwrap().heard.clone() +} + +#[tokio::test] +async fn a_cast_launches_the_receiver_and_reports_the_speakers_volume() { + let fake = Shared::default(); + let (_tx, mut events) = open(&fake).await; + assert_eq!(heard(&fake, 1).await, vec!["LAUNCH"]); + assert_eq!(events.recv().await, Some(Event::Volume { level: 0.3, muted: false })); +} + +#[tokio::test] +async fn a_running_receiver_is_joined_not_launched_again() { + let fake = Shared::new(Mutex::new(Fake { running: true, ..Fake::default() })); + let (_tx, _events) = open(&fake).await; + // The join asks for the media status; nothing launches. + assert_eq!(heard(&fake, 1).await, vec!["GET_STATUS"]); +} + +#[tokio::test] +async fn commands_after_a_load_wait_for_the_session_it_makes() { + let fake = Shared::default(); + let (tx, mut events) = open(&fake).await; + tx.send(Command::Load { + items: vec![track("http://lan/a")], + start: 12.0, + autoplay: true, + repeat: Repeat::Off, + }); + tx.send(Command::Pause); + let heard = heard(&fake, 4).await; + // The pause names the session the load made (1), never the empty one before it (0). + let at = |m: &str| heard.iter().position(|h| h == m); + assert!(at("QUEUE_LOAD") < at("PAUSE(1)") && at("PAUSE(1)").is_some(), "{heard:?}"); + assert!(!heard.iter().any(|h| h.ends_with("(0)")), "{heard:?}"); + loop { + if let Some(Event::Media(Some(s))) = events.recv().await { + assert_eq!(s.content_id.as_deref(), Some("http://lan/a")); + break; + } + } +} + +#[tokio::test] +async fn dropping_the_next_item_removes_the_one_that_was_appended() { + let fake = Shared::default(); + let (tx, _events) = open(&fake).await; + tx.send(Command::Load { + items: vec![track("http://lan/a")], + start: 0.0, + autoplay: true, + repeat: Repeat::Off, + }); + tx.send(Command::Append(track("http://lan/b"))); + tx.send(Command::DropNext); + let heard = heard(&fake, 6).await; + assert!(heard.contains(&"QUEUE_REMOVE(1)".to_owned()), "{heard:?}"); + assert_eq!(fake.lock().unwrap().ids, vec![1], "only the playing item is left"); +} + +#[tokio::test] +async fn a_device_that_goes_quiet_is_reported_lost() { + let fake = Shared::new(Mutex::new(Fake { go_silent: true, ..Fake::default() })); + let (_tx, mut events) = open(&fake).await; + let closed = tokio::time::timeout(Duration::from_secs(8), async { + loop { + if let Some(Event::Closed(c)) = events.recv().await { + return c; + } + } + }) + .await + .expect("noticed within seconds"); + assert_eq!(closed, Closed::Lost); +} + +async fn closed_after(fake: &Shared, change: impl FnOnce(&mut Fake)) -> Closed { + let (_tx, mut events) = open(fake).await; + heard(fake, 1).await; + change(&mut fake.lock().unwrap()); + tokio::time::timeout(Duration::from_secs(8), async { + loop { + if let Some(Event::Closed(c)) = events.recv().await { + return c; + } + } + }) + .await + .expect("noticed on the next status poll") +} + +#[tokio::test] +async fn another_app_on_the_device_ends_the_cast_as_taken_over() { + let fake = Shared::default(); + assert_eq!(closed_after(&fake, |f| f.other_app = true).await, Closed::TakenOver); +} + +#[tokio::test] +async fn a_receiver_stopped_on_the_device_ends_the_cast_as_stopped() { + let fake = Shared::default(); + assert_eq!(closed_after(&fake, |f| f.running = false).await, Closed::Stopped); +} diff --git a/crates/cast/src/discovery.rs b/crates/cast/src/discovery.rs new file mode 100644 index 000000000..e0443a814 --- /dev/null +++ b/crates/cast/src/discovery.rs @@ -0,0 +1,152 @@ +//! Finding Cast devices over mDNS (`_googlecast._tcp.local.`). +//! +//! Browsing only runs while someone could use it (the app decides when): the first multicast +//! socket is what makes Windows Firewall and macOS's Local Network permission ask, and nobody +//! should be asked at launch for a feature they never touch. + +use std::collections::HashMap; +use std::net::IpAddr; + +use mdns_sd::{ResolvedService, ServiceDaemon, ServiceEvent}; +use tokio::sync::mpsc; + +const SERVICE: &str = "_googlecast._tcp.local."; + +/// What kind of device it is, for the icon. +#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)] +#[serde(rename_all = "lowercase")] +pub enum Kind { + Speaker, + Tv, + Group, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CastDevice { + /// The device's own id (the TXT `id`), stable across renames and address changes. + pub id: String, + /// The name its owner gave it (`fn`). + pub name: String, + /// The model (`md`): "Chromecast", "Google Nest Mini", "Google Cast Group". + pub model: Option, + pub kind: Kind, + pub ip: IpAddr, + /// Always the announced one: a speaker group listens on a port of its own, not 8009. + pub port: u16, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum DiscoveryEvent { + Found(CastDevice), + Lost(String), +} + +/// A running browse. Dropping it stops the browse and the daemon. +pub struct Browser { + daemon: ServiceDaemon, +} + +impl Drop for Browser { + fn drop(&mut self) { + let _ = self.daemon.stop_browse(SERVICE); + let _ = self.daemon.shutdown(); + } +} + +/// Start browsing. Each device found or gone arrives on the receiver. +pub fn browse() -> Result<(mpsc::UnboundedReceiver, Browser), String> { + let daemon = ServiceDaemon::new().map_err(|e| e.to_string())?; + let events = daemon.browse(SERVICE).map_err(|e| e.to_string())?; + let (tx, rx) = mpsc::unbounded_channel(); + tokio::spawn(async move { + // The removal names the mDNS instance, not the device id the app keys on. + let mut instances: HashMap = HashMap::new(); + while let Ok(event) = events.recv_async().await { + let sent = match event { + ServiceEvent::ServiceResolved(info) => match device(&info) { + Some(d) => { + instances.insert(info.get_fullname().to_owned(), d.id.clone()); + tx.send(DiscoveryEvent::Found(d)) + } + None => Ok(()), + }, + ServiceEvent::ServiceRemoved(_, fullname) => match instances.remove(&fullname) { + Some(id) => tx.send(DiscoveryEvent::Lost(id)), + None => Ok(()), + }, + _ => Ok(()), + }; + if sent.is_err() { + return; + } + } + }); + Ok((rx, Browser { daemon })) +} + +fn device(info: &ResolvedService) -> Option { + let txt = |key| info.get_property_val_str(key).map(str::to_owned); + // A device whose address record hasn't arrived yet is left for the next event. + let ip = IpAddr::V4(*info.get_addresses_v4().iter().min()?); + Some(device_from(txt("id")?, txt("fn"), txt("md"), txt("ca"), ip, info.get_port())) +} + +/// The device a TXT record describes. Apart from the mDNS types so it can be tested. +fn device_from( + id: String, + name: Option, + model: Option, + capabilities: Option, + ip: IpAddr, + port: u16, +) -> CastDevice { + // `ca` is a capability bitmask; bit 0 is video out. + let video = capabilities.and_then(|c| c.parse::().ok()).is_some_and(|c| c & 1 == 1); + let group = model.as_deref().is_some_and(|m| m.contains("Group")); + let kind = if group { + Kind::Group + } else if video { + Kind::Tv + } else { + Kind::Speaker + }; + let name = name.filter(|n| !n.trim().is_empty()).unwrap_or_else(|| id.clone()); + CastDevice { id, name, model, kind, ip, port } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn ip() -> IpAddr { + "192.168.1.20".parse().unwrap() + } + + #[test] + fn the_txt_record_names_the_kind() { + let s = |v: &str| Some(v.to_owned()); + let tv = device_from("a".into(), s("Office TV"), s("Chromecast"), s("4101"), ip(), 8009); + assert_eq!(tv.kind, Kind::Tv); + assert_eq!(tv.name, "Office TV"); + let mini = + device_from("b".into(), s("Kitchen"), s("Google Nest Mini"), s("2052"), ip(), 8009); + assert_eq!(mini.kind, Kind::Speaker); + let group = device_from( + "c".into(), + s("Downstairs"), + s("Google Cast Group"), + s("2084"), + ip(), + 32187, + ); + assert_eq!(group.kind, Kind::Group); + assert_eq!(group.port, 32187); + } + + #[test] + fn a_device_without_a_name_is_shown_by_its_id() { + let d = device_from("abc".into(), Some(" ".into()), None, None, ip(), 8009); + assert_eq!(d.name, "abc"); + assert_eq!(d.kind, Kind::Speaker); + } +} diff --git a/crates/cast/src/lib.rs b/crates/cast/src/lib.rs new file mode 100644 index 000000000..bbccc3807 --- /dev/null +++ b/crates/cast/src/lib.rs @@ -0,0 +1,11 @@ +//! Google Cast for Limusic: finding devices and talking to them. +//! +//! Speaks CastV2 directly to Google's Default Media Receiver: no Google SDK, no account, no key. +//! Knows nothing about Tauri, YouTube or mpv; the app hands it URLs and metadata. + +pub mod channel; +pub mod discovery; +mod proto; + +pub use channel::{connect, Closed, Command, Event, Media, MediaStatus, Repeat, Sender, State}; +pub use discovery::{browse, Browser, CastDevice, DiscoveryEvent, Kind}; diff --git a/crates/cast/src/proto.rs b/crates/cast/src/proto.rs new file mode 100644 index 000000000..fbb998d10 --- /dev/null +++ b/crates/cast/src/proto.rs @@ -0,0 +1,189 @@ +//! The CastV2 channel envelope. +//! +//! A CastV2 frame is a 4-byte big-endian length followed by a protobuf `CastMessage` +//! (Chromium's `components/cast_channel/cast_channel.proto`): +//! +//! ```proto +//! message CastMessage { +//! enum ProtocolVersion { CASTV2_1_0 = 0; /* … */ } +//! required ProtocolVersion protocol_version = 1; +//! required string source_id = 2; +//! required string destination_id = 3; +//! required string namespace = 4; +//! enum PayloadType { STRING = 0; BINARY = 1; } +//! required PayloadType payload_type = 5; +//! optional string payload_utf8 = 6; +//! optional bytes payload_binary = 7; +//! } +//! ``` +//! +//! Only the envelope is protobuf: every namespace used here carries JSON. So the protobuf is +//! seven fields, of which three are read back; the rest are skipped by wire type. +//! +//! Ported from #717 (bosmak). + +/// The `STRING` payload type. Every message sent here is JSON. +const PAYLOAD_TYPE_STRING: u64 = 0; + +/// Wire type 0: a varint. +const WIRE_VARINT: u64 = 0; +/// Wire type 2: length-delimited (strings, bytes, submessages). +const WIRE_LEN: u64 = 2; + +/// The largest frame accepted from a device. Real ones are a few KB. +pub const MAX_FRAME: usize = 1 << 20; + +/// A decoded frame: who sent it, the namespace it belongs to and its JSON payload. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Frame { + pub source: String, + pub namespace: String, + pub payload: String, +} + +/// Frame one message: the length prefix, then the envelope. +pub fn frame(namespace: &str, source: &str, destination: &str, payload: &str) -> Vec { + let mut body = Vec::with_capacity(payload.len() + namespace.len() + source.len() + 32); + put_varint_field(&mut body, 1, 0); // protocol_version: CASTV2_1_0 + put_string_field(&mut body, 2, source); + put_string_field(&mut body, 3, destination); + put_string_field(&mut body, 4, namespace); + put_varint_field(&mut body, 5, PAYLOAD_TYPE_STRING); + put_string_field(&mut body, 6, payload); + + let mut out = Vec::with_capacity(body.len() + 4); + out.extend_from_slice(&(body.len() as u32).to_be_bytes()); + out.extend_from_slice(&body); + out +} + +/// Read one frame body (without the length prefix) back. +pub fn decode(body: &[u8]) -> Option { + let mut source = String::new(); + let mut namespace = String::new(); + let mut payload = String::new(); + let mut at = 0usize; + while at < body.len() { + let (tag, next) = read_varint(body, at)?; + at = next; + let field = tag >> 3; + match tag & 0x7 { + WIRE_VARINT => at = read_varint(body, at)?.1, + WIRE_LEN => { + let (len, next) = read_varint(body, at)?; + let end = next.checked_add(len as usize)?; + let bytes = body.get(next..end)?; + let text = || String::from_utf8_lossy(bytes).into_owned(); + match field { + 2 => source = text(), + 4 => namespace = text(), + 6 => payload = text(), + _ => {} + } + at = end; + } + // Fixed64 and fixed32: the envelope has neither, but a frame that can't be walked is + // a frame that is dropped. + 1 => at = at.checked_add(8)?, + 5 => at = at.checked_add(4)?, + _ => return None, + } + } + Some(Frame { source, namespace, payload }) +} + +fn put_tag(out: &mut Vec, field: u64, wire: u64) { + put_varint(out, (field << 3) | wire); +} + +fn put_varint_field(out: &mut Vec, field: u64, value: u64) { + put_tag(out, field, WIRE_VARINT); + put_varint(out, value); +} + +fn put_string_field(out: &mut Vec, field: u64, value: &str) { + put_tag(out, field, WIRE_LEN); + put_varint(out, value.len() as u64); + out.extend_from_slice(value.as_bytes()); +} + +fn put_varint(out: &mut Vec, mut value: u64) { + loop { + let byte = (value & 0x7f) as u8; + value >>= 7; + if value == 0 { + out.push(byte); + return; + } + out.push(byte | 0x80); + } +} + +/// Read a varint, returning it and where it ended. +fn read_varint(buf: &[u8], mut at: usize) -> Option<(u64, usize)> { + let mut value = 0u64; + let mut shift = 0u32; + loop { + let byte = *buf.get(at)?; + at += 1; + value |= ((byte & 0x7f) as u64) << shift; + if byte & 0x80 == 0 { + return Some((value, at)); + } + shift += 7; + if shift > 63 { + return None; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_frame_round_trips() { + let body = frame( + "urn:x-cast:com.google.cast.media", + "sender-0", + "receiver-0", + r#"{"type":"LOAD"}"#, + ); + assert_eq!(&body[..4], &(body.len() as u32 - 4).to_be_bytes()); + let decoded = decode(&body[4..]).expect("decodes"); + assert_eq!(decoded.source, "sender-0"); + assert_eq!(decoded.namespace, "urn:x-cast:com.google.cast.media"); + assert_eq!(decoded.payload, r#"{"type":"LOAD"}"#); + } + + #[test] + fn varints_survive_the_boundaries() { + for value in [0u64, 1, 127, 128, 300, 16_383, 16_384, u32::MAX as u64] { + let mut buf = Vec::new(); + put_varint(&mut buf, value); + let (read, at) = read_varint(&buf, 0).expect("reads back"); + assert_eq!(read, value); + assert_eq!(at, buf.len()); + } + } + + #[test] + fn an_unknown_field_is_skipped_not_fatal() { + let mut body = Vec::new(); + put_varint_field(&mut body, 1, 0); + put_string_field(&mut body, 4, "ns"); + put_tag(&mut body, 7, WIRE_LEN); + put_varint(&mut body, 3); + body.extend_from_slice(&[1, 2, 3]); + put_string_field(&mut body, 6, "{}"); + let decoded = decode(&body).expect("decodes"); + assert_eq!(decoded.namespace, "ns"); + assert_eq!(decoded.payload, "{}"); + } + + #[test] + fn a_truncated_frame_is_refused() { + let body = frame("ns", "s", "d", "{}"); + assert!(decode(&body[4..body.len() - 2]).is_none()); + } +} diff --git a/crates/cast/tests/fixtures/fake-receiver-cert.der b/crates/cast/tests/fixtures/fake-receiver-cert.der new file mode 100644 index 000000000..66e4590a1 Binary files /dev/null and b/crates/cast/tests/fixtures/fake-receiver-cert.der differ diff --git a/crates/cast/tests/fixtures/fake-receiver-key.der b/crates/cast/tests/fixtures/fake-receiver-key.der new file mode 100644 index 000000000..b57f4f111 Binary files /dev/null and b/crates/cast/tests/fixtures/fake-receiver-key.der differ diff --git a/docs/CASTING.md b/docs/CASTING.md new file mode 100644 index 000000000..bd14e52cd --- /dev/null +++ b/docs/CASTING.md @@ -0,0 +1,50 @@ +# Casting + +Limusic can play on a Chromecast, a Google Home or Nest speaker, a speaker group, or a TV with +Google Cast built in. Open the device picker in the player bar (next to the queue button) and pick +the device: the music moves there from where it is, and the bar, the media keys, the global +hotkeys and the OS media controls now control the device. Pick "This computer" to bring it back. + +## How it works + +Limusic talks to the device directly over the local network, with no Google account and no app to +install on it. The device runs Google's built-in Default Media Receiver, which shows the title, the +artist and the cover. The device keeps time and plays the songs; Limusic keeps the queue and hands +the device the next song before the current one ends, so they follow each other without a gap. + +The audio itself comes from this computer. YouTube's stream addresses only work for the address +that asked for them, so the device fetches each song from Limusic, which fetches it from YouTube the +same way it does for local playback. That is also why this computer has to stay awake and on the +network while casting. Limusic keeps it from going to sleep while a cast is playing. + +Playing, pausing, skipping and changing the volume on the device itself (the Google Home app, a +voice command, the device's buttons) all work, and Limusic follows along. + +## Troubleshooting + +- **No devices in the picker.** Your computer and the device have to be on the same network. + Guest networks and routers with "AP isolation" or "client isolation" turned on keep devices + from seeing each other. A VPN that routes all traffic can do the same. +- **Windows asks about network access.** The first time the picker looks for devices, and the + first time you cast, Windows Defender Firewall asks whether Limusic may use the network. Allow + it. Windows marks new Wi-Fi networks as public, so if your home network is set to public, tick + that box too, or change the network to private in Settings > Network & internet. If you pressed + Cancel, open Windows Defender Firewall > Allow an app through firewall and allow Limusic there. +- **macOS asks about the local network.** Allow it. You can change your answer later in System + Settings > Privacy & Security > Local Network. +- **"couldn't get the music from Limusic".** The device can reach Limusic's controls but not the + audio: almost always a firewall on this computer, or a VPN between the two. +- **Linux.** Fedora's default firewall allows it. If you turned on a firewall such as `ufw`, + allow incoming connections from your local network while casting. + +## Limitations + +- Local files, live broadcasts and karaoke play on this computer only. While casting, local files + and live broadcasts are skipped, and karaoke is switched off. +- Music videos are not cast; the device plays the audio. +- Tempo, pitch, reverb, the equalizer, crossfade and volume normalization are this computer's and + are hidden or skipped while casting. +- Casting and Listen Together can't run at the same time. +- With the Default Media Receiver, the Google Home app and Nest Hub screens show play, pause and + seek but no next or previous buttons. The device's notification on Android and voice commands + still skip. diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 4efa66789..f28f14f8f 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -48,6 +48,10 @@ http-body-util = "0.1" innertube = { path = "../crates/innertube" } player = { path = "../crates/player" } karaoke = { path = "../crates/karaoke" } +cast = { path = "../crates/cast" } +# The cast LAN listener picks the interface on the device's network (audioproxy.rs). Already in +# the tree through mdns-sd. +if-addrs = "0.15" souvlaki = { version = "0.8.3", default-features = false, features = ["use_zbus"] } # Listen Together: shared wire types + WebSocket client (wss via rustls). context/19. listen-protocol = { workspace = true } @@ -101,6 +105,7 @@ windows = { version = "0.61", features = [ "Win32_Foundation", "Win32_Graphics_Gdi", "Win32_System_Com", + "Win32_System_Power", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging", ] } diff --git a/src-tauri/Info.plist b/src-tauri/Info.plist new file mode 100644 index 000000000..682da9114 --- /dev/null +++ b/src-tauri/Info.plist @@ -0,0 +1,14 @@ + + + + + + NSLocalNetworkUsageDescription + Limusic looks for Chromecast and Google Cast speakers on your network so it can play on them. + NSBonjourServices + + _googlecast._tcp + + + diff --git a/src-tauri/src/audioproxy.rs b/src-tauri/src/audioproxy.rs index e55baf04a..c7fbd5f55 100644 --- a/src-tauri/src/audioproxy.rs +++ b/src-tauri/src/audioproxy.rs @@ -24,7 +24,7 @@ use std::collections::{HashMap, VecDeque}; use std::convert::Infallible; use std::io; -use std::net::{Ipv4Addr, TcpListener}; +use std::net::{IpAddr, Ipv4Addr, TcpListener}; use std::sync::{Arc, Mutex, OnceLock}; use std::time::Duration; @@ -89,6 +89,8 @@ struct Registration { url: String, headers: HashMap, meta: Mutex>, + /// Requests from a cast device on the LAN listener. + lan_hits: std::sync::atomic::AtomicU32, } struct Registry { @@ -160,23 +162,217 @@ pub fn register(url: &str, headers: &HashMap) -> Option return None; } let (port, token) = ENDPOINT.get()?; + let id = insert(url, headers)?; + Some(format!("http://127.0.0.1:{port}/{token}/a/{id}")) +} + +/// Add a registration and return its id, evicting the oldest past the cap. +fn insert(url: &str, headers: &HashMap) -> Option { let id = format!("{:016x}", rand::random::()); let reg = Arc::new(Registration { url: url.to_owned(), headers: headers.clone(), meta: Mutex::new(None), + lan_hits: Default::default(), }); - { - let mut r = REGISTRY.get()?.lock().ok()?; - while r.order.len() >= MAX_REGISTRATIONS { - if let Some(old) = r.order.pop_front() { - r.map.remove(&old); - } + let mut r = REGISTRY + .get_or_init(|| Mutex::new(Registry { map: HashMap::new(), order: VecDeque::new() })) + .lock() + .ok()?; + while r.order.len() >= MAX_REGISTRATIONS { + if let Some(old) = r.order.pop_front() { + r.map.remove(&old); } - r.map.insert(id.clone(), reg); - r.order.push_back(id.clone()); } - Some(format!("http://127.0.0.1:{port}/{token}/a/{id}")) + r.map.insert(id.clone(), reg); + r.order.push_back(id.clone()); + Some(id) +} + +// ---- The LAN listener, for casting (#399) ---- +// +// A Cast device fetches the audio from this machine, never from YouTube directly: a stream URL +// is signed for the address that resolved it (IPv6 included, and the `proxy` setting's), and it +// needs the headers it was signed with. So while a cast runs, the same proxy also listens on the +// LAN, with its own token, answering only the device. + +struct LanServerState { + port: u16, + token: String, + stop_tx: tokio::sync::oneshot::Sender<()>, +} + +static LAN_STATE: Mutex> = Mutex::new(None); + +/// The device allowed to fetch. `None` while nothing casts, and for a speaker group, whose +/// leader (any member) does the fetching. +static LAN_PEER: Mutex> = Mutex::new(None); + +/// Open the LAN listener for a cast to `device`. A fresh port and token every cast. `false` +/// when it couldn't listen. +pub fn start_lan(device: IpAddr, group: bool) -> bool { + stop_lan(); + if let Ok(mut peer) = LAN_PEER.lock() { + *peer = (!group).then_some(device); + } + let listener = match TcpListener::bind((Ipv4Addr::UNSPECIFIED, 0)) { + Ok(l) => l, + Err(e) => { + tracing::warn!(error = %e, "audio proxy lan: bind failed"); + return false; + } + }; + let Ok(port) = listener.local_addr().map(|a| a.port()) else { return false }; + if listener.set_nonblocking(true).is_err() { + return false; + } + let token = format!("{:016x}{:016x}", rand::random::(), rand::random::()); + let (stop_tx, mut stop_rx) = tokio::sync::oneshot::channel::<()>(); + let serve_token = token.clone(); + tauri::async_runtime::spawn(async move { + let Ok(listener) = tokio::net::TcpListener::from_std(listener) else { return }; + loop { + let (stream, peer) = tokio::select! { + _ = &mut stop_rx => break, + accepted = listener.accept() => match accepted { + Ok(v) => v, + Err(e) => { + tracing::warn!(error = %e, "audio proxy lan: accept failed"); + tokio::time::sleep(Duration::from_millis(100)).await; + continue; + } + }, + }; + let token = serve_token.clone(); + tauri::async_runtime::spawn(async move { + let svc = service_fn(move |req| { + let token = token.clone(); + async move { + Ok::<_, Infallible>( + handle_lan(req, peer.ip(), &token).await.unwrap_or_else(empty), + ) + } + }); + let _ = http1::Builder::new() + .timer(TokioTimer::new()) + .header_read_timeout(Duration::from_secs(15)) + .serve_connection(TokioIo::new(stream), svc) + .await; + }); + } + tracing::info!("audio proxy lan stopped"); + }); + if let Ok(mut state) = LAN_STATE.lock() { + *state = Some(LanServerState { port, token, stop_tx }); + } + tracing::info!(port, "audio proxy listening on the LAN for a cast"); + true +} + +/// Close the LAN listener (the cast ended). +pub fn stop_lan() { + if let Ok(mut peer) = LAN_PEER.lock() { + *peer = None; + } + if let Some(state) = LAN_STATE.lock().ok().and_then(|mut s| s.take()) { + let _ = state.stop_tx.send(()); + } +} + +/// Register a stream for the cast device and return the LAN URL it should be handed. +pub fn register_lan( + url: &str, + headers: &HashMap, + device: IpAddr, +) -> Option { + if !crate::orchestrator::is_youtube_stream(url) { + return None; + } + let (port, token) = { + let state = LAN_STATE.lock().ok()?; + let state = state.as_ref()?; + (state.port, state.token.clone()) + }; + let host = local_address_for(device)?; + let id = insert(url, headers)?; + Some(format!("http://{host}:{port}/{token}/a/{id}")) +} + +/// Whether the device has asked for this LAN URL yet. +pub fn lan_fetched(lan_url: &str) -> bool { + let Some(id) = lan_url.rsplit('/').next() else { return false }; + let reg = REGISTRY.get().and_then(|r| r.lock().ok()).and_then(|r| r.map.get(id).cloned()); + reg.is_some_and(|r| r.lan_hits.load(std::sync::atomic::Ordering::Relaxed) > 0) +} + +/// This machine's address as the device sees it: the one the route to it leaves from. A UDP +/// `connect` sends nothing, it only asks the routing table. +/// +/// A full-tunnel VPN routes everything through itself, the device included, so its address +/// can't be reached from the LAN. When the routed address isn't on the device's network but an +/// interface is, that one is used instead. +fn local_address_for(device: IpAddr) -> Option { + let IpAddr::V4(device) = device else { return None }; + let routed = std::net::UdpSocket::bind("0.0.0.0:0") + .and_then(|s| s.connect((device, 8009)).map(|_| s)) + .and_then(|s| s.local_addr()) + .ok() + .and_then(|a| match a.ip() { + IpAddr::V4(ip) if !ip.is_unspecified() && !ip.is_loopback() => Some(ip), + _ => None, + }); + if routed.is_some_and(|ip| same_network(ip, device)) { + return routed; + } + let on_lan = if_addrs::get_if_addrs().ok().and_then(|ifs| { + ifs.into_iter().find_map(|i| match i.ip() { + IpAddr::V4(ip) if !i.is_loopback() && same_network(ip, device) => Some(ip), + _ => None, + }) + }); + on_lan.or(routed) +} + +/// Close enough for a home network: the first three octets match. +fn same_network(a: Ipv4Addr, b: Ipv4Addr) -> bool { + a.octets()[..3] == b.octets()[..3] +} + +fn is_private_ip(ip: IpAddr) -> bool { + match ip { + IpAddr::V4(v4) => v4.is_private() || v4.is_link_local() || v4.is_loopback(), + IpAddr::V6(v6) => { + let s = v6.segments(); + v6.is_loopback() || (s[0] & 0xffc0 == 0xfe80) || (s[0] & 0xfe00 == 0xfc00) + } + } +} + +async fn handle_lan( + req: Request, + peer: IpAddr, + token: &str, +) -> Result, StatusCode> { + if !matches!(*req.method(), Method::GET | Method::HEAD) { + return Err(StatusCode::METHOD_NOT_ALLOWED); + } + let allowed = LAN_PEER.lock().ok().and_then(|p| *p); + if !is_private_ip(peer) || allowed.is_some_and(|a| a != peer) { + tracing::warn!(%peer, "cast LAN request refused: not the device"); + return Err(StatusCode::FORBIDDEN); + } + // Audio only: the karaoke routes stay loopback. + let Some(("a", id)) = route(req.uri().path(), token) else { + return Err(StatusCode::NOT_FOUND); + }; + if let Some(reg) = + REGISTRY.get().and_then(|r| r.lock().ok()).and_then(|r| r.map.get(id).cloned()) + { + reg.lan_hits.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } + let range = req.headers().get(header::RANGE).and_then(|v| v.to_str().ok()); + tracing::debug!(%peer, ?range, "cast: the device fetched"); + serve_audio_stream(&req, id).await } /// `///` to `(kind, id)`, once the token matches this launch's. @@ -187,6 +383,7 @@ fn route<'a>(path: &'a str, token: &str) -> Option<(&'a str, &'a str)> { } /// `//a/` to the registration id. +#[cfg(test)] fn id_from<'a>(path: &'a str, token: &str) -> Option<&'a str> { route(path, token).filter(|(kind, _)| *kind == "a").map(|(_, id)| id) } @@ -320,11 +517,19 @@ async fn handle(req: Request) -> Result, StatusCod if let Some(("k", id)) = route(req.uri().path(), token) { return serve_growing(&req, id).await; } - let id = id_from(req.uri().path(), token).ok_or(StatusCode::NOT_FOUND)?; + let Some(("a", id)) = route(req.uri().path(), token) else { + return Err(StatusCode::NOT_FOUND); + }; + serve_audio_stream(&req, id).await +} + +async fn serve_audio_stream( + req: &Request, + id: &str, +) -> Result, StatusCode> { let reg = { let r = REGISTRY - .get() - .ok_or(StatusCode::INTERNAL_SERVER_ERROR)? + .get_or_init(|| Mutex::new(Registry { map: HashMap::new(), order: VecDeque::new() })) .lock() .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; r.map.get(id).cloned() @@ -626,4 +831,24 @@ mod tests { assert_eq!(total_from_content_range("bytes 0-0/*"), None); assert_eq!(total_from_content_range("nonsense"), None); } + + #[test] + fn test_is_private_ip() { + assert!(is_private_ip("127.0.0.1".parse().unwrap())); + assert!(is_private_ip("::1".parse().unwrap())); + assert!(is_private_ip("10.0.0.1".parse().unwrap())); + assert!(is_private_ip("172.16.0.1".parse().unwrap())); + assert!(is_private_ip("192.168.1.1".parse().unwrap())); + assert!(is_private_ip("169.254.1.1".parse().unwrap())); + assert!(is_private_ip("fe80::1".parse().unwrap())); + assert!(!is_private_ip("8.8.8.8".parse().unwrap())); + assert!(!is_private_ip("1.1.1.1".parse().unwrap())); + } + + #[test] + fn a_vpn_address_is_not_on_the_devices_network() { + let ip = |s: &str| s.parse::().unwrap(); + assert!(same_network(ip("192.168.1.10"), ip("192.168.1.42"))); + assert!(!same_network(ip("10.8.0.2"), ip("192.168.1.42"))); + } } diff --git a/src-tauri/src/castout.rs b/src-tauri/src/castout.rs new file mode 100644 index 000000000..7101265c3 --- /dev/null +++ b/src-tauri/src/castout.rs @@ -0,0 +1,789 @@ +//! Casting (#399): the devices on the network, and the one Limusic is playing on. +//! +//! While a cast runs, the device is the player. `AppState` sends the transport here (its `out_*` +//! methods), and what the device reports comes back as the same [`PlayerEvent`]s mpv sends, on +//! the same pump. So the queue, history, scrobbling, MPRIS and the windows treat a cast exactly +//! like local playback, and mpv sits stopped until the cast ends. +//! +//! The device holds two items: the track playing and the next one, which it buffers ahead so +//! songs follow each other without a gap. The audio reaches it through the LAN audio proxy +//! (`audioproxy::register_lan`), never as a YouTube URL: those are signed for the address that +//! resolved them. +//! +//! Each item is known by its URL. Every registration makes a new one, so the URL the device +//! reports says which item it is on, even with the same song twice in the queue. + +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::Mutex; +use std::time::{Duration, Instant}; + +use cast::{CastDevice, Closed, Command, Event, Media, Repeat, State}; +use innertube::SongItem; +use player::PlayerEvent; +use serde::Serialize; +use tauri::{AppHandle, Emitter}; +use tokio::sync::mpsc; + +use crate::orchestrator::PlaybackData; + +/// How long discovery keeps running after the picker closes, so reopening it is instant. +const DISCOVERY_LINGER: Duration = Duration::from_secs(180); +/// A load the device hasn't fetched anything for in this long can't reach this computer. +const FETCH_DEADLINE: Duration = Duration::from_secs(8); +/// How long a new cast may take to report its first status before it is abandoned. +const SYNC_DEADLINE: Duration = Duration::from_secs(10); +/// How often the position is carried forward between the device's reports. +const TICK: Duration = Duration::from_millis(250); +/// The fastest the speaker's level is changed during a drag. +const VOLUME_EVERY: Duration = Duration::from_millis(120); + +/// What the session tells the pump, tagged with the session it belongs to. +#[derive(Debug)] +pub enum Signal { + /// The device as a player: what mpv would have said. + Player(PlayerEvent), + /// The first status of a new cast: the device has the music. + Synced, + /// The speaker's volume, 0 to 100. + Volume(i64), + /// The device refused the item, whether it ever fetched a byte of it, and how many items in + /// a row it has refused. + Refused { fetched: bool, count: u32 }, + /// The device never fetched the item it was handed. + Unreachable, + /// A new cast never reported a status. + NoStatus, + /// The connection ended. + Closed(Closed), +} + +/// Why a cast couldn't start, as the code the UI translates (`cast.`). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum StartError { + Gone, + Unreachable, + Refused, + LeaveRoom, +} + +impl StartError { + pub fn code(self) -> &'static str { + match self { + StartError::Gone => "gone", + StartError::Unreachable => "unreachable", + StartError::Refused => "refused", + StartError::LeaveRoom => "leave_room", + } + } +} + +/// A device as the windows see it: no address, they have no use for one. +#[derive(Debug, Clone, Serialize)] +pub struct DeviceView { + pub id: String, + pub name: String, + pub model: Option, + pub kind: cast::Kind, +} + +impl From<&CastDevice> for DeviceView { + fn from(d: &CastDevice) -> Self { + DeviceView { id: d.id.clone(), name: d.name.clone(), model: d.model.clone(), kind: d.kind } + } +} + +/// The carried-forward position: where the device last said it was, and when. +#[derive(Clone, Copy)] +struct Clock { + base: f64, + at: Instant, + running: bool, +} + +impl Clock { + fn now(&self) -> f64 { + if self.running { + self.base + self.at.elapsed().as_secs_f64() + } else { + self.base + } + } + + fn set(&mut self, base: f64, running: bool) { + *self = Clock { base, at: Instant::now(), running }; + } +} + +struct Active { + device: CastDevice, + tx: cast::Sender, + epoch: u64, + /// The first status arrived. + synced: bool, + /// The URL of the item the device is on, and of the one queued after it. + cur: Option, + next: Option, + /// Their items, for a repeat-one the receiver doesn't loop by itself. + cur_media: Option, + next_media: Option, + /// Playing or buffering, as the windows were last told. + playing: bool, + idle: bool, + /// The device is on an item Limusic didn't hand it: the next track is loaded fresh. + desynced: bool, + clock: Clock, + /// The media session the device was on when the last load went out. Its statuses describe + /// what the load replaced, and are dropped until a newer session shows up. + load_from: Option, + /// The newest media session the device reported. + session: i64, + duration: Option, + volume: Option, + /// A level waiting to be sent. + volume_due: Option, + repeat_one: bool, + /// Loads refused in a row. Reset once something plays. + refusals: u32, +} + +pub struct CastManager { + app: AppHandle, + devices: Mutex>, + browser: Mutex>, + picker_open: AtomicBool, + /// Bumped every time the picker closes: a stop scheduled by an earlier close is void. + picker_gen: AtomicU64, + active: Mutex>, + /// The device being connected to, for the picker's spinner. + connecting: Mutex>, + epoch: AtomicU64, + signals: mpsc::UnboundedSender<(u64, Signal)>, + signals_rx: Mutex>>, +} + +impl CastManager { + pub fn new(app: AppHandle) -> Self { + let (signals, rx) = mpsc::unbounded_channel(); + CastManager { + app, + devices: Mutex::new(Vec::new()), + browser: Mutex::new(None), + picker_open: AtomicBool::new(false), + picker_gen: AtomicU64::new(0), + active: Mutex::new(None), + connecting: Mutex::new(None), + epoch: AtomicU64::new(0), + signals, + signals_rx: Mutex::new(Some(rx)), + } + } + + /// The pump's end of the signals. Taken once, at startup. + pub fn take_signals(&self) -> Option> { + self.signals_rx.lock().ok()?.take() + } + + pub fn epoch(&self) -> u64 { + self.epoch.load(Ordering::SeqCst) + } + + fn with(&self, f: impl FnOnce(&mut Active) -> R) -> Option { + self.active.lock().ok()?.as_mut().map(f) + } + + pub fn is_active(&self) -> bool { + self.active.lock().is_ok_and(|a| a.is_some()) + } + + pub fn device(&self) -> Option { + self.with(|a| a.device.clone()) + } + + // ---- discovery ---- + + /// The picker opened or closed. Discovery starts with the first open, and stops a while + /// after the last close unless something is casting. + pub fn set_picker_open(self: &std::sync::Arc, open: bool) { + self.picker_open.store(open, Ordering::SeqCst); + if open { + self.start_discovery(); + self.emit_devices(); + return; + } + let gen = self.picker_gen.fetch_add(1, Ordering::SeqCst) + 1; + let me = self.clone(); + tauri::async_runtime::spawn(async move { + tokio::time::sleep(DISCOVERY_LINGER).await; + let stale = me.picker_gen.load(Ordering::SeqCst) != gen; + if !stale && !me.picker_open.load(Ordering::SeqCst) && !me.is_active() { + me.stop_discovery(); + } + }); + } + + fn start_discovery(self: &std::sync::Arc) { + let Ok(mut browser) = self.browser.lock() else { return }; + if browser.is_some() { + return; + } + let (mut events, handle) = match cast::browse() { + Ok(b) => b, + Err(e) => { + tracing::warn!(error = %e, "cast: discovery could not start"); + return; + } + }; + *browser = Some(handle); + tracing::info!("cast: discovery started"); + let me = self.clone(); + tauri::async_runtime::spawn(async move { + while let Some(event) = events.recv().await { + if let Ok(mut devices) = me.devices.lock() { + match event { + cast::DiscoveryEvent::Found(d) => { + devices.retain(|x| x.id != d.id); + devices.push(d); + devices.sort_by_key(|d| d.name.to_lowercase()); + } + cast::DiscoveryEvent::Lost(id) => devices.retain(|x| x.id != id), + } + } + me.emit_devices(); + } + }); + } + + fn stop_discovery(&self) { + if let Ok(mut browser) = self.browser.lock() { + if browser.take().is_some() { + tracing::info!("cast: discovery stopped"); + } + } + } + + pub fn devices(&self) -> Vec { + self.devices.lock().map(|d| d.iter().map(DeviceView::from).collect()).unwrap_or_default() + } + + fn emit_devices(&self) { + let _ = self.app.emit("cast-devices", self.devices()); + } + + /// What is casting, and what is being connected to: the `cast-state` payload. + pub fn state(&self) -> serde_json::Value { + let device = self.device().as_ref().map(DeviceView::from); + let connecting = self.connecting.lock().ok().and_then(|c| c.clone()); + serde_json::json!({ "device": device, "connecting": connecting }) + } + + fn emit_state(&self) { + let _ = self.app.emit("cast-state", self.state()); + } + + // ---- the connection ---- + + /// Connect to a device. On success it is the player from here on: the caller hands it the + /// music. A device already casting is stopped once the new one answers, so two speakers + /// never play the same song half a beat apart. + pub async fn connect(self: &std::sync::Arc, id: &str) -> Result<(), StartError> { + let device = self + .devices + .lock() + .ok() + .and_then(|d| d.iter().find(|d| d.id == id).cloned()) + .ok_or(StartError::Gone)?; + self.set_connecting(Some(id)); + tracing::info!(device = %device.name, model = ?device.model, "cast: connecting"); + let connected = cast::connect(device.ip, device.port).await; + self.set_connecting(None); + let (tx, events) = match connected { + Ok(c) => c, + Err(Closed::Refused(reason)) => { + tracing::warn!(reason, "cast: the device refused the receiver"); + return Err(StartError::Refused); + } + Err(e) => { + tracing::warn!(error = ?e, "cast: could not connect"); + return Err(StartError::Unreachable); + } + }; + self.stop().await; + let epoch = self.epoch.fetch_add(1, Ordering::SeqCst) + 1; + if let Ok(mut active) = self.active.lock() { + *active = Some(Active { + device: device.clone(), + tx, + epoch, + synced: false, + cur: None, + next: None, + cur_media: None, + next_media: None, + playing: false, + idle: true, + desynced: false, + clock: Clock { base: 0.0, at: Instant::now(), running: false }, + load_from: None, + session: 0, + duration: None, + volume: None, + volume_due: None, + repeat_one: false, + refusals: 0, + }); + } + if !crate::audioproxy::start_lan(device.ip, device.kind == cast::Kind::Group) { + // Nothing could reach the music: let the device go rather than cast silence. + self.stop().await; + return Err(StartError::Unreachable); + } + self.spawn_session(epoch, events); + self.emit_state(); + Ok(()) + } + + fn set_connecting(&self, id: Option<&str>) { + if let Ok(mut c) = self.connecting.lock() { + *c = id.map(str::to_owned); + } + self.emit_state(); + } + + /// Stop the device and let it go. The caller decides where the music goes next. + pub async fn stop(&self) { + let Some(active) = self.active.lock().ok().and_then(|mut a| a.take()) else { return }; + self.epoch.fetch_add(1, Ordering::SeqCst); + active.tx.close(Duration::from_secs(2)).await; + crate::audioproxy::stop_lan(); + tracing::info!(device = %active.device.name, "cast: stopped"); + self.emit_state(); + } + + /// The connection ended by itself: forget it without talking to the device. + pub fn forget(&self, epoch: u64) -> bool { + let Ok(mut active) = self.active.lock() else { return false }; + if !matches!(active.as_ref(), Some(a) if a.epoch == epoch) { + return false; + } + *active = None; + drop(active); + self.epoch.fetch_add(1, Ordering::SeqCst); + crate::audioproxy::stop_lan(); + self.emit_state(); + true + } + + /// Whether the first status of this cast arrived. + pub fn synced(&self) -> bool { + self.with(|a| a.synced).unwrap_or(false) + } + + // ---- the player ---- + + /// The device's item for a track, published on the LAN for it. `None` when it can't be cast. + pub fn media_for(&self, item: &SongItem, data: &PlaybackData) -> Option { + if data.live || crate::local::is_local_song(&item.video_id) { + return None; + } + let device_ip = self.device()?.ip; + let url = crate::audioproxy::register_lan(&data.stream_url, &data.headers, device_ip)?; + let duration = crate::state::parse_duration_ms(item.duration.as_deref()); + Some(Media { + url, + content_type: content_type(&data.stream_url, data.itag), + title: item.title.clone(), + artist: item.artists.clone(), + album: item.album.clone(), + image: item.thumbnail.as_deref().map(cover_url), + duration: (duration > 0).then(|| duration as f64 / 1000.0), + }) + } + + /// Play this item from `start`, replacing whatever the device had. + pub fn load(self: &std::sync::Arc, media: Media, start: f64, autoplay: bool) { + let url = media.url.clone(); + let Some((epoch, repeat)) = self.with(|a| { + a.cur = Some(url.clone()); + a.next = None; + a.cur_media = Some(media.clone()); + a.next_media = None; + a.idle = false; + a.desynced = false; + a.duration = media.duration; + a.clock.set(start, false); + a.load_from = Some(a.session); + let repeat = if a.repeat_one { Repeat::One } else { Repeat::Off }; + a.tx.send(Command::Load { items: vec![media], start, autoplay, repeat }); + (a.epoch, repeat) + }) else { + return; + }; + tracing::info!(start, autoplay, ?repeat, "cast: load"); + // A device that can't reach this computer says nothing for a long time, and then often + // only "failed". The proxy knows sooner whether it ever asked. Not for a paused load, + // which a device may not fetch until it plays. + if !autoplay { + return; + } + let me = self.clone(); + tauri::async_runtime::spawn(async move { + tokio::time::sleep(FETCH_DEADLINE).await; + let same = me.with(|a| a.epoch == epoch && a.cur.as_deref() == Some(&url)); + if same == Some(true) && !crate::audioproxy::lan_fetched(&url) { + let _ = me.signals.send((epoch, Signal::Unreachable)); + } + }); + } + + /// Queue the next track behind the one playing. + pub fn append(&self, media: Media) { + self.with(|a| { + a.next = Some(media.url.clone()); + a.next_media = Some(media.clone()); + a.tx.send(Command::Append(media)); + }); + } + + /// Drop the queued next track (the queue changed under it). + pub fn drop_next(&self) { + self.with(|a| { + a.next_media = None; + if a.next.take().is_some() { + a.tx.send(Command::DropNext); + } + }); + } + + /// Whether the next track is queued on the device already. + pub fn has_next(&self) -> bool { + self.with(|a| a.next.is_some()).unwrap_or(false) + } + + /// Go to the queued next track, which the device has buffered already. + pub fn next(&self) { + self.with(|a| a.tx.send(Command::Next)); + } + + /// Play or pause. The windows hear it at once; the device's status follows. + pub fn set_playing(&self, playing: bool) { + let changed = self.with(|a| { + if a.idle { + return false; + } + a.tx.send(if playing { Command::Play } else { Command::Pause }); + let now = a.clock.now(); + a.clock.set(now, playing); + let changed = a.playing != playing; + a.playing = playing; + changed + }); + if changed == Some(true) { + self.player(PlayerEvent::Playing(playing)); + self.player(PlayerEvent::Paused(!playing)); + } + } + + pub fn toggle(&self) { + if let Some(playing) = self.with(|a| a.playing) { + self.set_playing(!playing); + } + } + + pub fn seek(&self, to: f64) { + self.with(|a| { + a.tx.send(Command::Seek(to)); + let running = a.clock.running; + a.clock.set(to, running); + }); + self.player(PlayerEvent::Position(to)); + } + + pub fn set_repeat_one(&self, on: bool) { + self.with(|a| { + a.repeat_one = on; + a.tx.send(Command::Repeat(if on { Repeat::One } else { Repeat::Off })); + }); + } + + /// Nothing to play on the device, as `Player::is_idle` says for mpv. + pub fn is_idle(&self) -> bool { + self.with(|a| a.idle || a.cur.is_none() || a.desynced).unwrap_or(true) + } + + pub fn position(&self) -> Option { + self.with(|a| a.clock.now()) + } + + pub fn volume(&self) -> Option { + self.with(|a| a.volume).flatten() + } + + /// The speaker's level, 0 to 100. A slider drag sends one of these per frame; the device + /// gets the latest at most every [`VOLUME_EVERY`]. + pub fn set_volume(self: &std::sync::Arc, level: i64) { + let first = self.with(|a| { + a.volume = Some(level); + a.volume_due.replace(level).is_none() + }); + if first != Some(true) { + return; + } + let me = self.clone(); + tauri::async_runtime::spawn(async move { + tokio::time::sleep(VOLUME_EVERY).await; + me.with(|a| { + if let Some(level) = a.volume_due.take() { + a.tx.send(Command::Volume(level as f64 / 100.0)); + } + }); + }); + } + + fn player(&self, event: PlayerEvent) { + let _ = self.signals.send((self.epoch(), Signal::Player(event))); + } + + // ---- what the device says ---- + + fn spawn_session( + self: &std::sync::Arc, + epoch: u64, + mut events: mpsc::UnboundedReceiver, + ) { + let signals = self.signals.clone(); + let send = move |s: Signal| { + let _ = signals.send((epoch, s)); + }; + // The clock, carried forward between reports. + let (me, tick_send) = (self.clone(), send.clone()); + tauri::async_runtime::spawn(async move { + let mut tick = tokio::time::interval(TICK); + while me.epoch() == epoch { + tick.tick().await; + if let Some(Some(pos)) = me.with(|a| a.clock.running.then(|| a.clock.now())) { + tick_send(Signal::Player(PlayerEvent::Position(pos))); + } + } + }); + let (me, sync_send) = (self.clone(), send.clone()); + tauri::async_runtime::spawn(async move { + tokio::time::sleep(SYNC_DEADLINE).await; + let loaded = me.with(|a| a.cur.is_some()) == Some(true); + if me.epoch() == epoch && loaded && !me.synced() { + sync_send(Signal::NoStatus); + } + }); + let me = self.clone(); + tauri::async_runtime::spawn(async move { + while let Some(event) = events.recv().await { + if me.epoch() != epoch { + return; + } + match event { + Event::Media(status) => { + for s in me.on_status(status) { + send(s); + } + } + Event::Volume { level, muted } => { + let level = if muted { 0 } else { (level * 100.0).round() as i64 }; + if me.with(|a| a.volume.replace(level) != Some(level)) == Some(true) { + send(Signal::Volume(level)); + } + } + Event::LoadFailed(reason) => { + tracing::warn!(reason, "cast: the device refused the track"); + let url = me.with(|a| a.cur.clone()).flatten(); + let fetched = url.as_deref().is_some_and(crate::audioproxy::lan_fetched); + let count = me + .with(|a| { + a.idle = true; + a.refusals += 1; + a.refusals + }) + .unwrap_or(1); + send(Signal::Refused { fetched, count }); + } + Event::Closed(reason) => { + tracing::info!(?reason, "cast: the connection ended"); + send(Signal::Closed(reason)); + return; + } + } + } + }); + } + + /// One media status, as the player events it amounts to. + fn on_status(&self, status: Option) -> Vec { + let mut out = Vec::new(); + let Ok(mut guard) = self.active.lock() else { return out }; + let Some(a) = guard.as_mut() else { return out }; + let Some(s) = status else { + // No media session: the device stopped, or never started. Nothing to report until + // it has something. + return out; + }; + if let Some(from) = a.load_from { + if s.session <= from { + return out; // about what the last load replaced + } + a.load_from = None; + } + a.session = s.session; + // The device has the music once it plays it (or holds it paused): this computer stops + // then, not on the first "buffering", so the handover has no silence in it. + if !a.synced && a.cur.is_some() && matches!(s.state, State::Playing | State::Paused) { + a.synced = true; + out.push(Signal::Synced); + } + let on = s.content_id.as_deref(); + if on.is_some() && on == a.next.as_deref() { + // The device moved on to the queued track by itself: what mpv calls a gapless + // advance. The one it left is cleared off its queue. + a.cur = a.next.take(); + a.cur_media = a.next_media.take(); + a.duration = s.duration; + a.clock.set(s.position, s.state == State::Playing); + a.tx.send(Command::TrimPlayed); + out.push(Signal::Player(PlayerEvent::TrackEnded)); + } else if on.is_some() && on != a.cur.as_deref() { + // An item Limusic didn't hand over (a stale one, or someone else's). The app loads + // its own next track. + if !a.desynced && !a.idle { + tracing::warn!("cast: the device is on an item it wasn't given"); + a.desynced = true; + out.push(Signal::Player(PlayerEvent::TrackEnded)); + } + return out; + } + let playing = matches!(s.state, State::Playing | State::Buffering); + match s.state { + State::Idle => { + a.clock.set(a.clock.now(), false); + // Quietly: the queue decides what idle means (the next track, or the end). + a.playing = false; + let was_idle = std::mem::replace(&mut a.idle, true); + match s.idle_reason.as_deref() { + _ if was_idle => {} + Some("FINISHED") if a.repeat_one && a.cur_media.is_some() => { + // A receiver that doesn't loop by itself: hand it the track again. A + // finished session can't be seeked back into. + let media = a.cur_media.clone().into_iter().collect(); + a.idle = false; + a.clock.set(0.0, false); + a.load_from = Some(a.session); + let repeat = Repeat::One; + a.tx.send(Command::Load { + items: media, + start: 0.0, + autoplay: true, + repeat, + }); + out.push(Signal::Player(PlayerEvent::Looped)); + } + Some("FINISHED") => out.push(Signal::Player(PlayerEvent::TrackEnded)), + Some("ERROR") => out.push(Signal::Player(PlayerEvent::TrackFailed( + "the cast device could not play this track".into(), + ))), + // Cancelled or interrupted: our own stop or load. + _ => {} + } + } + _ => { + a.idle = false; + if s.state == State::Playing { + a.refusals = 0; + } + // Repeat-one looping on the device: the position jumps back to the top. + if a.repeat_one && a.clock.base > 10.0 && s.position < 3.0 && a.playing { + out.push(Signal::Player(PlayerEvent::Looped)); + } + a.clock.set(s.position, s.state == State::Playing); + out.push(Signal::Player(PlayerEvent::Position(s.position))); + } + } + if let Some(d) = s.duration.filter(|d| *d > 0.0) { + if a.duration != Some(d) { + a.duration = Some(d); + out.push(Signal::Player(PlayerEvent::Duration(d))); + } + } + if s.state != State::Idle && a.playing != playing { + a.playing = playing; + out.push(Signal::Player(PlayerEvent::Playing(playing))); + out.push(Signal::Player(PlayerEvent::Paused(!playing))); + } + out + } +} + +/// The MIME type the device is told, from the stream URL's `mime` parameter or its itag. +fn content_type(url: &str, itag: i64) -> String { + let from_url = reqwest::Url::parse(url) + .ok() + .and_then(|u| u.query_pairs().find(|(k, _)| k == "mime").map(|(_, v)| v.into_owned())); + from_url.unwrap_or_else(|| { + match itag { + 249..=251 => "audio/webm", + _ => "audio/mp4", + } + .to_owned() + }) +} + +/// The cover at a size worth putting on a TV. YouTube's thumbnail URLs carry their size after +/// the last `=`: `=w120-h120-l90-rj` or `=s120`. +fn cover_url(url: &str) -> String { + let Some((head, spec)) = url.rsplit_once('=') else { return url.to_owned() }; + let mut parts: Vec<&str> = spec.split('-').collect(); + match parts.first() { + Some(p) if p.starts_with('s') && p[1..].parse::().is_ok() => parts[0] = "s544", + Some(p) if p.starts_with('w') && parts.get(1).is_some_and(|h| h.starts_with('h')) => { + parts[0] = "w544"; + parts[1] = "h544"; + } + _ => return url.to_owned(), + } + format!("{head}={}", parts.join("-")) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_device_is_told_the_streams_own_media_type() { + let opus = "https://r1.googlevideo.com/videoplayback?itag=251&mime=audio%2Fwebm&x=1"; + assert_eq!(content_type(opus, 251), "audio/webm"); + assert_eq!(content_type("https://example.com/a", 140), "audio/mp4"); + assert_eq!(content_type("https://example.com/a", 251), "audio/webm"); + } + + #[test] + fn the_cover_is_asked_for_at_tv_size() { + assert_eq!( + cover_url("https://lh3.googleusercontent.com/abc=w120-h120-l90-rj"), + "https://lh3.googleusercontent.com/abc=w544-h544-l90-rj" + ); + assert_eq!( + cover_url("https://lh3.googleusercontent.com/abc=s60"), + "https://lh3.googleusercontent.com/abc=s544" + ); + assert_eq!( + cover_url("https://i.ytimg.com/vi/x/hq720.jpg"), + "https://i.ytimg.com/vi/x/hq720.jpg" + ); + } + + #[test] + fn the_clock_runs_only_while_playing() { + let mut c = Clock { base: 0.0, at: Instant::now(), running: false }; + c.set(30.0, false); + std::thread::sleep(Duration::from_millis(20)); + assert_eq!(c.now(), 30.0); + c.set(30.0, true); + std::thread::sleep(Duration::from_millis(20)); + assert!(c.now() > 30.0 && c.now() < 31.0); + } +} diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 7d76e7456..b4cd3393d 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -210,17 +210,41 @@ pub async fn seek(state: St<'_>, position: f64) -> Result<(), String> { #[tauri::command] pub async fn set_volume(state: St<'_>, volume: i64) -> Result<(), String> { - state.player.set_volume(volume).map_err(|e| e.to_string())?; - if volume > 0 { - crate::hotkeys::LAST_NONZERO_VOLUME.store(volume, std::sync::atomic::Ordering::Relaxed); - } - // There is one volume and there can be two windows (the mini player). Without this the one - // that didn't move the slider keeps showing the old level and lies about what you're hearing. - let _ = state.app.emit("volume", volume); - state.media_set_volume(volume); + state.set_output_volume(volume); Ok(()) } +/// The device picker opened or closed: discovery runs while it is open (castout.rs). +#[tauri::command] +pub async fn cast_picker( + state: St<'_>, + open: bool, +) -> Result, String> { + state.cast.set_picker_open(open); + Ok(state.cast.devices()) +} + +/// Play on a Cast device. The error is a key the UI translates (`cast.`). +#[tauri::command] +pub async fn cast_start(state: St<'_>, id: String) -> Result<(), String> { + let state = state.inner().clone(); + state.cast_start(&id).await.map_err(|e| e.code().to_owned()) +} + +/// Stop casting; the music comes back here. +#[tauri::command] +pub async fn cast_stop(state: St<'_>) -> Result<(), String> { + let state = state.inner().clone(); + state.cast_stop().await; + Ok(()) +} + +/// What is casting, for a window that opened after it started. +#[tauri::command] +pub async fn cast_state(state: St<'_>) -> Result { + Ok(state.cast.state()) +} + /// A fresh position and when it was read (`AppState::playback_clock`). Async, so the read of mpv /// happens off the main thread. #[tauri::command] @@ -2642,12 +2666,21 @@ pub async fn lt_set_server_url(state: St<'_>, url: String) -> Result<(), String> #[tauri::command] pub async fn lt_create_room(state: St<'_>, username: String) -> Result<(), String> { + // A room's timeline is this computer's player, which is quiet while a cast plays. + if state.cast.is_active() { + let _ = state.app.emit("cast-notice", serde_json::json!({ "key": "stop_to_join" })); + return Ok(()); + } state.lt.create_room(username).await; Ok(()) } #[tauri::command] pub async fn lt_join_room(state: St<'_>, code: String, username: String) -> Result<(), String> { + if state.cast.is_active() { + let _ = state.app.emit("cast-notice", serde_json::json!({ "key": "stop_to_join" })); + return Ok(()); + } state.lt.join_room(code, username).await; Ok(()) } diff --git a/src-tauri/src/hotkeys.rs b/src-tauri/src/hotkeys.rs index 93cf9e8bb..018dc5f02 100644 --- a/src-tauri/src/hotkeys.rs +++ b/src-tauri/src/hotkeys.rs @@ -9,7 +9,7 @@ use std::sync::atomic::{AtomicI64, Ordering}; use std::sync::{Arc, Mutex}; use serde::{Deserialize, Serialize}; -use tauri::{AppHandle, Emitter, Manager}; +use tauri::{AppHandle, Manager}; use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState}; use crate::db::Db; @@ -222,17 +222,14 @@ pub fn execute_action(app: &AppHandle, action: HotkeyAction) { /// Read, change, set, persist and echo the volume, the same as a slider commit. Serialized: each /// press is its own task, so a held key would otherwise lose steps and two mutes could both mute. -pub(crate) fn change_volume(state: &AppState, app: &AppHandle, f: impl FnOnce(i64) -> i64) { +pub(crate) fn change_volume(state: &AppState, _app: &AppHandle, f: impl FnOnce(i64) -> i64) { static LOCK: Mutex<()> = Mutex::new(()); let _guard = LOCK.lock().unwrap(); - let volume = f(state.player.volume()).clamp(0, 100); - if state.player.set_volume(volume).is_ok() { - if volume > 0 { - LAST_NONZERO_VOLUME.store(volume, Ordering::Relaxed); - } + let volume = f(state.out_volume()).clamp(0, 100); + state.set_output_volume(volume); + // The saved level is this computer's: a speaker's own level is never written over it. + if !state.cast.is_active() { state.db.set_setting("volume", &volume.to_string()); - let _ = app.emit("volume", volume); - state.media_set_volume(volume); } } diff --git a/src-tauri/src/keepawake.rs b/src-tauri/src/keepawake.rs new file mode 100644 index 000000000..5d45ed707 --- /dev/null +++ b/src-tauri/src/keepawake.rs @@ -0,0 +1,70 @@ +//! Keep Windows and macOS awake while a cast plays (#399). +//! +//! Both already stay awake while an audio stream is open (the audio driver's power request, +//! coreaudiod's assertion), which is why local playback needs nothing here. While casting, mpv is +//! stopped and nothing holds the machine up, so a laptop would fall asleep mid-song and take the +//! cast with it: the device fetches every track from this computer. Linux has `inhibit.rs`. +//! Suspend only: the screen still blanks and locks as usual. + +use std::sync::mpsc::{channel, Sender}; +use std::sync::{Mutex, OnceLock}; + +static TX: OnceLock>> = OnceLock::new(); + +/// Hold off idle sleep (`true`) or let it happen again (`false`). Cheap to repeat. +pub fn set(on: bool) { + let tx = TX.get_or_init(|| { + let (tx, rx) = channel::(); + // One thread owns the hold: on Windows the request belongs to the thread that made it. + std::thread::spawn(move || { + let mut held = Hold::default(); + for on in rx { + held.set(on); + } + }); + Mutex::new(tx) + }); + if let Ok(tx) = tx.lock() { + let _ = tx.send(on); + } +} + +#[derive(Default)] +struct Hold { + on: bool, + #[cfg(target_os = "macos")] + child: Option, +} + +impl Hold { + fn set(&mut self, on: bool) { + if on == self.on { + return; + } + self.on = on; + #[cfg(target_os = "windows")] + { + use windows::Win32::System::Power::{ + SetThreadExecutionState, ES_CONTINUOUS, ES_SYSTEM_REQUIRED, + }; + let flags = if on { ES_CONTINUOUS | ES_SYSTEM_REQUIRED } else { ES_CONTINUOUS }; + unsafe { SetThreadExecutionState(flags) }; + } + #[cfg(target_os = "macos")] + { + // ponytail: `caffeinate` rather than IOKit bindings. `-w` ties it to this process, so + // a crash never leaves the Mac held awake. + if let Some(mut child) = self.child.take() { + let _ = child.kill(); + let _ = child.wait(); + } + if on { + self.child = std::process::Command::new("/usr/bin/caffeinate") + .args(["-i", "-w", &std::process::id().to_string()]) + .spawn() + .ok(); + } + } + tracing::debug!(on, "keep awake for the cast"); + } +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 2022d8e8c..ad6848afd 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -4,6 +4,7 @@ mod appicon; mod audioproxy; mod autopause; mod blocked; +mod castout; mod cipher; mod commands; mod db; @@ -18,6 +19,8 @@ mod inhibit; mod karaoke; mod karaoke_model; mod karaoke_runtime; +#[cfg(not(target_os = "linux"))] +mod keepawake; mod lastfm; mod limusic; mod listenbrainz; @@ -1364,6 +1367,10 @@ pub fn run() { commands::seek, commands::playback_clock, commands::set_volume, + commands::cast_picker, + commands::cast_start, + commands::cast_stop, + commands::cast_state, commands::set_playback_params, commands::set_karaoke, commands::get_queue, @@ -1608,6 +1615,9 @@ pub fn run() { if let tauri::RunEvent::Exit = &event { if let Some(state) = handle.try_state::>() { state.flush_position(); + // A speaker left playing with nobody to stop it is the wrong kind of cast. + // `stop` waits at most 2 s for the device. + tauri::async_runtime::block_on(state.cast.stop()); } mini::save_position(handle); } @@ -1674,9 +1684,10 @@ fn spawn_event_pump( app: tauri::AppHandle, mut events: tokio::sync::mpsc::UnboundedReceiver, ) { + let mut cast = state.cast.take_signals(); tauri::async_runtime::spawn(async move { let mut throttle = PositionThrottle::new(); - while let Some(ev) = events.recv().await { + while let Some(ev) = next_player_event(&state, &mut events, &mut cast).await { match ev { PlayerEvent::Position(p) => { // Quiet while the windows show another linked device's playback. @@ -1768,6 +1779,29 @@ fn spawn_event_pump( }); } +/// The next player event for the pump. While a cast runs the device is the player: its events +/// take mpv's place, and mpv's (stopped, or still playing out the handover) are dropped. +async fn next_player_event( + state: &Arc, + events: &mut tokio::sync::mpsc::UnboundedReceiver, + cast: &mut Option>, +) -> Option { + loop { + tokio::select! { + ev = events.recv() => match ev { + Some(ev) if !state.cast.is_active() => return Some(ev), + Some(_) => {} + None => return None, + }, + Some((epoch, signal)) = async { cast.as_mut()?.recv().await } => { + if let Some(ev) = state.on_cast_signal(epoch, signal).await { + return Some(ev); + } + } + } + } +} + #[cfg(test)] mod tests { use super::{close_hides, PositionThrottle}; diff --git a/src-tauri/src/media.rs b/src-tauri/src/media.rs index 9e0f22fe5..45d52c44c 100644 --- a/src-tauri/src/media.rs +++ b/src-tauri/src/media.rs @@ -215,15 +215,13 @@ pub(crate) fn handle_event(app: &AppHandle, event: MediaControlEvent) { let state = state.inner().clone(); match event { MediaControlEvent::Play | MediaControlEvent::Toggle => state.resume_or_toggle().await, - MediaControlEvent::Pause | MediaControlEvent::Stop => { - let _ = state.player.pause(); - } + MediaControlEvent::Pause | MediaControlEvent::Stop => state.out_pause(), // GNOME's panel media widget calls MPRIS Raise when the entry itself is clicked. MediaControlEvent::Raise => crate::tray::show_main(&app), MediaControlEvent::Next => state.next_in_queue().await, MediaControlEvent::Previous => state.prev_in_queue().await, MediaControlEvent::SetPosition(MediaPosition(pos)) => { - let _ = state.player.seek(pos.as_secs_f64() * state.player.speed()); + let _ = state.user_seek(pos.as_secs_f64() * state.out_speed()).await; } MediaControlEvent::SeekBy(dir, by) => { let delta = if matches!(dir, SeekDirection::Forward) { @@ -231,13 +229,13 @@ pub(crate) fn handle_event(app: &AppHandle, event: MediaControlEvent) { } else { -by.as_secs_f64() }; - let delta = delta * state.player.speed(); - let _ = state.player.seek((state.current_position() + delta).max(0.0)); + let delta = delta * state.out_speed(); + let _ = state.user_seek((state.current_position() + delta).max(0.0)).await; } MediaControlEvent::Seek(dir) => { let delta = if matches!(dir, SeekDirection::Forward) { 10.0 } else { -10.0 }; - let delta = delta * state.player.speed(); - let _ = state.player.seek((state.current_position() + delta).max(0.0)); + let delta = delta * state.out_speed(); + let _ = state.user_seek((state.current_position() + delta).max(0.0)).await; } // An MPRIS widget or KDE Connect (#220). Same path as a volume hotkey, which also // pushes the new level back: without that the widget snaps to the old one. diff --git a/src-tauri/src/state.rs b/src-tauri/src/state.rs index b33f10fb2..d545554fe 100644 --- a/src-tauri/src/state.rs +++ b/src-tauri/src/state.rs @@ -14,7 +14,7 @@ use innertube::{ }; use karaoke::cache::PinSlot; use listen_protocol::{Playback, PlaybackKind, RemoteAction, Track}; -use player::Player; +use player::{Player, PlayerEvent}; use tauri::{AppHandle, Emitter, Manager}; use tokio::sync::Mutex; @@ -69,6 +69,7 @@ pub struct AppState { pub db: Arc, pub app: AppHandle, pub orchestrator: Arc, + pub cast: Arc, /// Listen Together session (context/19). Drives host broadcasts + guest gating. pub lt: Arc, // Serialize effect commands with the room's tempo reset, including requests already pending. @@ -549,8 +550,9 @@ impl AppState { clients, player, db, - app, + app: app.clone(), orchestrator, + cast: Arc::new(crate::castout::CastManager::new(app)), lt, link, cache_dir, @@ -586,6 +588,288 @@ impl AppState { } } + // ---- Casting (#399, castout.rs) ---- + // + // While a cast runs, the device is the player. The transport reaches `player` only through + // these `out_*` methods, and each one sends it to the device instead. + + fn casting(&self) -> bool { + self.cast.is_active() + } + + pub(crate) fn out_pause(&self) { + if self.casting() { + self.cast.set_playing(false); + } else { + let _ = self.player.pause(); + } + } + + fn out_toggle(&self) { + if self.casting() { + self.cast.toggle(); + } else { + let _ = self.player.toggle(); + } + } + + fn out_seek(&self, to: f64) -> Result<(), String> { + if self.casting() { + self.cast.seek(to); + Ok(()) + } else { + self.player.seek(to).map_err(|e| e.to_string()) + } + } + + pub(crate) fn out_is_idle(&self) -> bool { + if self.casting() { + self.cast.is_idle() + } else { + self.player.is_idle() + } + } + + /// Drop the queued next track, on the device or in mpv's playlist. + fn out_clear_next(&self) { + if self.casting() { + self.cast.drop_next(); + } else { + let _ = self.player.clear_playlist(); + } + } + + fn out_set_loop(&self, on: bool) { + if self.casting() { + self.cast.set_repeat_one(on); + } + // mpv keeps its own loop flag either way, for when the music comes back here. + let _ = self.player.set_loop_file(on); + } + + /// The playback speed. A device plays at 1x: tempo is this computer's. + pub(crate) fn out_speed(&self) -> f64 { + if self.casting() { + 1.0 + } else { + self.player.speed() + } + } + + /// The volume the slider shows: the speaker's while casting. + pub(crate) fn out_volume(&self) -> i64 { + self.cast.volume().filter(|_| self.casting()).unwrap_or_else(|| self.player.volume()) + } + + /// Set the volume of whatever is playing, and tell everything that shows it. + pub fn set_output_volume(&self, volume: i64) { + if self.casting() { + self.cast.set_volume(volume); + } else { + let _ = self.player.set_volume(volume); + } + if volume > 0 { + crate::hotkeys::LAST_NONZERO_VOLUME.store(volume, Ordering::Relaxed); + } + // There is one volume and there can be two windows (the mini player). + let _ = self.app.emit("volume", volume); + self.media_set_volume(volume); + } + + /// Tell the windows the playback params, at 1x while a device plays: every clock in the UI + /// divides by the speed, and a device plays at its own. + fn emit_playback_params(&self) { + let (speed, semitones, reverb) = self.player.playback_params(); + let speed = if self.casting() { 1.0 } else { speed }; + let _ = self.app.emit( + "playback-params", + serde_json::json!({ "speed": speed, "semitones": semitones, "reverb": reverb }), + ); + } + + /// A cast message for the windows, as a key they translate (`cast.`). + fn cast_notice(&self, key: &str, name: Option<&str>, title: Option<&str>) { + tracing::info!(key, name, title, "cast notice"); + let _ = self + .app + .emit("cast-notice", serde_json::json!({ "key": key, "name": name, "title": title })); + } + + /// Play on a Cast device, picking the music up where it is now. + pub async fn cast_start(self: &Arc, id: &str) -> Result<(), crate::castout::StartError> { + // A room's timeline is this computer's player; a cast would leave two masters. + if self.lt.in_room().await { + return Err(crate::castout::StartError::LeaveRoom); + } + if self.showing_remote() { + self.link_continue_here().await; + } + let switching = self.casting(); + let position = match switching { + true => self.cast.position().unwrap_or_else(|| self.current_position()), + false => self.current_position(), + }; + let playing = self.is_playing.load(Ordering::Relaxed); + self.cast.connect(id).await?; + // mpv keeps playing until the device has the music (`Signal::Synced`), but nothing it + // primed is on the device. + let repeat_one = { + let mut q = self.queue.lock().await; + q.lookahead_loaded = None; + q.repeat == RepeatMode::One + }; + if !switching { + let _ = self.player.clear_playlist(); + } + self.cast.set_repeat_one(repeat_one); + self.cast_handover(position, playing).await; + Ok(()) + } + + /// Hand the device the current track at `position`, then the next one. + async fn cast_handover(self: &Arc, position: f64, playing: bool) { + let gen = self.generation.fetch_add(1, Ordering::SeqCst) + 1; + // An empty queue: the device waits for the first play. + let Some(item) = self.current_item().await else { return }; + let secs = parse_duration_ms(item.duration.as_deref()) / 1000; + let data = match self.resolve(&item.video_id, item.is_upload, secs).await { + Ok(d) => d, + // Nothing loaded; the next play resolves it again. + Err(e) => return tracing::warn!(error = %e, "cast: the handover could not resolve"), + }; + if self.generation.load(Ordering::SeqCst) != gen || !self.casting() { + return; + } + let Some(media) = self.cast.media_for(&item, &data) else { + self.cast_notice( + "skipped", + self.cast.device().map(|d| d.name).as_deref(), + Some(&item.title), + ); + if playing { + self.next_in_queue_local().await; + } + return; + }; + self.cast.load(media, position, playing); + self.latest_position.store(position.to_bits(), Ordering::SeqCst); + self.prime_lookahead(gen).await; + } + + /// Stop casting: the device stops, and the music comes back here, the way it was there. + pub async fn cast_stop(self: &Arc) { + if !self.casting() { + return; + } + let synced = self.cast.synced(); + let position = self.cast.position().unwrap_or_else(|| self.current_position()); + let playing = self.is_playing.load(Ordering::Relaxed); + self.cast.stop().await; + self.resume_here(synced, position, playing).await; + } + + /// The cast ended without being asked to (lost, taken over, unreachable): the music comes + /// back here paused, at the device's last position, and the user hears why. + async fn cast_ended(self: &Arc, epoch: u64, key: &str, close: bool) { + if self.cast.epoch() != epoch || !self.casting() { + return; + } + let name = self.cast.device().map(|d| d.name); + let synced = self.cast.synced(); + let position = self.cast.position().unwrap_or_else(|| self.current_position()); + if close { + self.cast.stop().await; + } else if !self.cast.forget(epoch) { + return; + } + self.cast_notice(key, name.as_deref(), None); + self.resume_here(synced, position, false).await; + } + + /// The music comes back to this computer. + async fn resume_here(self: &Arc, synced: bool, position: f64, playing: bool) { + let volume = self.player.volume(); + let _ = self.app.emit("volume", volume); + self.media_set_volume(volume); + self.emit_playback_params(); + if !synced { + // The device never took over, so mpv still has the music, as it was. + let playing = !self.player.is_paused() && !self.player.is_idle(); + self.media_set_playing(playing); + let _ = self.app.emit("playback-state", if playing { "playing" } else { "paused" }); + return; + } + let (index, video_id) = { + let mut q = self.queue.lock().await; + q.lookahead_loaded = None; + (q.current, q.items.get(q.current).map(|i| i.video_id.clone())) + }; + let _ = self.player.stop(); + self.latest_position.store(position.to_bits(), Ordering::SeqCst); + if let Some(video_id) = video_id { + *self.pending_seek.lock().unwrap() = Some((video_id, position)); + } + if playing { + self.play_index_at(index, None).await; + } else { + // Loaded on the next play, at this position, like a queue restored at launch. + self.media_set_playing(false); + let _ = self.app.emit("playback-state", "paused"); + let _ = self.app.emit("position", serde_json::json!({ "position": position })); + } + } + + /// One thing the cast session said (castout.rs). A player event comes back for the pump to + /// handle exactly as one of mpv's. + pub async fn on_cast_signal( + self: &Arc, + epoch: u64, + signal: crate::castout::Signal, + ) -> Option { + use crate::castout::Signal; + if epoch != self.cast.epoch() { + return None; // an earlier cast's + } + match signal { + Signal::Player(event) => return Some(event), + Signal::Synced => { + // The device has the music: this computer goes quiet. + let _ = self.player.stop(); + self.emit_playback_params(); + tracing::info!("cast: the device took over"); + } + Signal::Volume(level) => { + if level > 0 { + crate::hotkeys::LAST_NONZERO_VOLUME.store(level, Ordering::Relaxed); + } + let _ = self.app.emit("volume", level); + self.media_set_volume(level); + } + // It fetched the track and still refused it: the track's problem, skip it the way + // a dead stream is skipped. Never fetched: the device can't reach this computer, + // and every other track would fail the same way. + // Two tracks in a row (each retried once) is the device, not the tracks: stop before + // the whole queue is skipped. + Signal::Refused { fetched: true, count } if count < 3 => { + return Some(PlayerEvent::TrackFailed("the cast device refused the track".into())) + } + Signal::Refused { fetched: true, .. } => self.cast_ended(epoch, "refused", true).await, + Signal::Refused { fetched: false, .. } | Signal::Unreachable => { + self.cast_ended(epoch, "firewall", true).await + } + Signal::NoStatus => self.cast_ended(epoch, "unreachable", true).await, + Signal::Closed(reason) => { + let key = match reason { + cast::Closed::TakenOver => "taken_over", + cast::Closed::Stopped => "stopped", + _ => "lost", + }; + self.cast_ended(epoch, key, false).await; + } + } + None + } + /// Remember where the loopback video proxy should fetch `id` from. pub fn put_video_url(&self, id: &str, url: String) { let Ok(mut map) = self.video_urls.lock() else { return }; @@ -1682,7 +1966,7 @@ impl AppState { }; // A shared link's `t=`: only a song radio has a seed track to start partway into. let start = start.filter(|s| kind == "song" && s.is_finite() && *s > 0.0); - if playing_seed && !self.player.is_idle() { + if playing_seed && !self.out_is_idle() { self.splice_radio(items, seed, title).await; if let Some(pos) = start { self.user_seek(pos).await?; @@ -1760,7 +2044,7 @@ impl AppState { splice_radio_into(&mut q, items, seed, title); // Whatever mpv had primed as the gapless next belongs to the old queue. if q.lookahead_loaded.take().is_some() { - let _ = self.player.clear_playlist(); + self.out_clear_next(); } } self.emit_queue().await; @@ -1839,7 +2123,7 @@ impl AppState { && (q.lookahead_loaded != expected || before != after) { q.lookahead_loaded = None; - let _ = self.player.clear_playlist(); + self.out_clear_next(); } } pages += 1; @@ -1887,7 +2171,7 @@ impl AppState { if self.lt.is_guest().await { return; // guest playback is host-driven } - if self.player.is_idle() { + if self.out_is_idle() { let (idx, has_items) = { let q = self.queue.lock().await; (q.current, !q.items.is_empty()) @@ -1897,7 +2181,7 @@ impl AppState { return; } } - let _ = self.player.toggle(); + self.out_toggle(); } /// Jump to a specific queue index. @@ -1920,6 +2204,19 @@ impl AppState { self.emit_guest_hint(); // guest playback is host-driven return; } + // The next track is already on the device, buffered: jump to it instead of loading it + // again. The device's move comes back as a gapless advance (`on_track_ended`). + if self.casting() && start.is_none() && self.cast.has_next() { + let primed = { + let q = self.queue.lock().await; + q.lookahead_loaded == Some(index) + && next_index(q.items.len(), q.current, q.repeat) == Some(index) + }; + if primed { + self.cast.next(); + return; + } + } let gen = self.generation.fetch_add(1, Ordering::SeqCst) + 1; let video_id = { let mut q = self.queue.lock().await; @@ -2022,7 +2319,7 @@ impl AppState { let stale = primed.is_some() && primed != primed_id(q); if stale { q.lookahead_loaded = None; - let _ = self.player.clear_playlist(); + self.out_clear_next(); } stale }; @@ -2088,7 +2385,7 @@ impl AppState { }); return; } - if !primed || self.player.is_idle() { + if !primed || self.out_is_idle() { // No gapless handoff happened. `is_idle` alone is not enough: right at EOF mpv can // still report not-idle before it settles, and with nothing primed that would send us // into the gapless branch below — a phantom "now playing" over a silent player. If we @@ -2115,7 +2412,9 @@ impl AppState { // is playing at the *previous* one's gain until this lands. if let Some(loudness) = q.lookahead_loudness_db.take() { q.current_loudness_db = loudness; - if let Err(e) = self.player.set_gain(self.track_gain(loudness)) { + if self.casting() { + // A device plays at its own level; nothing to apply. + } else if let Err(e) = self.player.set_gain(self.track_gain(loudness)) { tracing::warn!(error = %e, "applying lookahead loudness gain failed"); } } @@ -2308,6 +2607,24 @@ impl AppState { return false; } match resolved { + // A device can't play a broadcast or a file on this disk: skip it, as a track + // no client can resolve is skipped, rather than play it on this computer. + Ok(d) + if self.casting() + && (d.live || crate::local::is_local_song(&item.video_id)) => + { + let name = self.cast.device().map(|d| d.name); + self.cast_notice("skipped", name.as_deref(), Some(&item.title)); + let mut q = self.queue.lock().await; + if q.current + 1 >= q.items.len() { + return false; + } + let next = q.current + 1; + q.seek_to(next); + q.lookahead_loaded = None; + drop(q); + self.emit_queue().await; + } Ok(d) => break (item, d), // A deleted local file is gone for good, so it leaves the queue outright rather // than being skipped over — a row that can only ever fail again is noise. Every @@ -2396,7 +2713,14 @@ impl AppState { if let Some(engine) = self.karaoke_engine() { engine.set_playing_stream(None); } - if let Err(e) = self.player.load( + if self.casting() { + // The device gets the track itself: karaoke and the music video are this computer's. + // Uncastable tracks were skipped above, so `None` here is a cast that just ended. + match self.cast.media_for(&item, &data) { + Some(media) => self.cast.load(media, seek.unwrap_or(0.0), true), + None => return false, + } + } else if let Err(e) = self.player.load( &play_u, &data.headers, self.track_gain(data.loudness_db), @@ -2415,7 +2739,9 @@ impl AppState { // room's Play. Sent after, a skip while paused reaches guests as "play" on the old track // first. At the position mpv starts at, so a retry or a restored context isn't sent as 0. self.lt_broadcast_current_track((seek.unwrap_or(0.0) * 1000.0) as i64, true).await; - let _ = self.player.play(); + if !self.casting() { + let _ = self.player.play(); + } // Items played from cards/radio can arrive without a duration; the player response knows // the exact length of the cut we stream. Backfill before emitting — lyrics matching keys // on it (a wrong-cut LRCLIB match plays lyrics seconds off the audio). @@ -2427,7 +2753,9 @@ impl AppState { data.is_episode, ); // A broadcast's picture is segment bases too (#424), so there is no 720p file to pair. - self.attach_video(&item.video_id, item.is_video && !data.live, &play_u); + if !self.casting() { + self.attach_video(&item.video_id, item.is_video && !data.live, &play_u); + } { let mut q = self.queue.lock().await; q.current_client = Some(data.stream_client.clone()); @@ -2684,12 +3012,20 @@ impl AppState { } let url = self.play_url(PinSlot::Next, next_video, &stream_url); let is_karaoke = url != stream_url; - if self.player.current_path().as_deref() == Some(&url) { - return; - } - if let Err(e) = self.player.enqueue(&url) { - tracing::warn!(error = %e, "enqueue lookahead failed"); - return; + if self.casting() { + // A track the device can't play isn't queued there: the device finishes the current + // one, and `start_current` skips this one with a notice. + let Some(item) = q.items.get(next_idx).cloned() else { return }; + let Some(media) = self.cast.media_for(&item, &data) else { return }; + self.cast.append(media); + } else { + if self.player.current_path().as_deref() == Some(&url) { + return; + } + if let Err(e) = self.player.enqueue(&url) { + tracing::warn!(error = %e, "enqueue lookahead failed"); + return; + } } q.lookahead_loaded = Some(next_idx); q.lookahead_client = Some(data.stream_client.clone()); @@ -2712,7 +3048,9 @@ impl AppState { data.is_episode, ); // Resolved now, minutes early, so the picture is there the moment the track starts. - self.attach_video(&qi.video_id, qi.is_video, &url); + if !self.casting() { + self.attach_video(&qi.video_id, qi.is_video, &url); + } } tracing::debug!(index = next_idx, "gapless lookahead primed"); } @@ -2745,7 +3083,11 @@ impl AppState { } fn karaoke_active(&self) -> bool { - self.karaoke_engine().map_or(false, |e| e.is_enabled()) && !crate::http::has_proxy() + // Not while casting: an instrumental is made and played on this computer, and the device + // gets the track itself. + self.karaoke_engine().map_or(false, |e| e.is_enabled()) + && !crate::http::has_proxy() + && !self.casting() } /// What mpv is handed for `video_id` in `slot`: the prepared instrumental when karaoke has @@ -2797,6 +3139,10 @@ impl AppState { } pub async fn reload_current_source(self: &std::sync::Arc) { + // A karaoke switch is a source for mpv; a device keeps playing the track itself. + if self.casting() { + return; + } let gen = self.generation.load(Ordering::SeqCst); let Some(item) = self.current_item().await else { return }; let resolved = self @@ -2957,7 +3303,7 @@ impl AppState { { let mut q = self.queue.lock().await; q.lookahead_loaded = None; - let _ = self.player.clear_playlist(); + self.out_clear_next(); } self.unpin_karaoke(PinSlot::Next); self.prime_lookahead(gen).await; @@ -3114,6 +3460,9 @@ impl AppState { url: &str, rate: f64, ) { + if self.casting() { + return; // the device plays the original + } /// Seconds the stream must be ahead of playback at the switch. It only grows after. const LEAD: f64 = 5.0; /// The stream must be made at least this much faster than it plays, or playback would @@ -3244,7 +3593,7 @@ impl AppState { q.lookahead_loaded = None; q.lookahead_is_karaoke = false; q.lookahead_karaoke_model = None; - let _ = self.player.clear_playlist(); + self.out_clear_next(); } self.prime_lookahead(gen).await; } @@ -3278,7 +3627,7 @@ impl AppState { q.lookahead_loaded = None; q.lookahead_is_karaoke = false; q.lookahead_karaoke_model = None; - let _ = self.player.clear_playlist(); + self.out_clear_next(); } self.prime_lookahead(gen).await; } @@ -3343,8 +3692,9 @@ impl AppState { "position": self.current_position(), // 0 until mpv loads the file, which a restored track waits for until play (#502). "duration": if duration > 0.0 { duration } else { item.as_ref().map_or(0.0, row_secs) }, - "volume": saved_volume(&self.db), - "speed": speed, + // A speaker's own level while casting, and 1x: tempo is this computer's. + "volume": if self.casting() { self.out_volume() } else { saved_volume(&self.db) }, + "speed": if self.casting() { 1.0 } else { speed }, "semitones": semitones, "reverb": reverb, }) @@ -3583,10 +3933,7 @@ impl AppState { ) -> Result<(), String> { self.player.set_playback_params(speed, semitones, reverb).map_err(|e| e.to_string())?; self.media_refresh_timing().await; - let _ = self.app.emit( - "playback-params", - serde_json::json!({ "speed": speed, "semitones": semitones, "reverb": reverb }), - ); + self.emit_playback_params(); Ok(()) } @@ -3604,7 +3951,7 @@ impl AppState { /// Tempo changes do not emit a new mpv duration; refresh OS and Discord timelines immediately. pub async fn media_refresh_timing(&self) { let secs = self.queue.lock().await.duration; - let speed = self.player.speed(); + let speed = self.out_speed(); if let Some(m) = &self.media { if secs > 0.0 { m.set_duration(secs, speed); @@ -3635,12 +3982,14 @@ impl AppState { let pos = self.current_position(); // Record what the controls now hold, or the next tick would repeat this very push. *self.last_media_state.lock().unwrap() = Some((playing, pos)); - m.set_playback(playing, pos, self.player.speed()); + m.set_playback(playing, pos, self.out_speed()); } #[cfg(target_os = "windows")] crate::taskbar::set_playing(&self.app, playing); #[cfg(target_os = "linux")] crate::inhibit::set_playing(playing); + #[cfg(not(target_os = "linux"))] + crate::keepawake::set(playing && self.casting()); if let Some(d) = &self.discord { d.set_playing(playing); } @@ -3709,7 +4058,8 @@ impl AppState { async fn apply_external_audio(self: &Arc, active: bool) { // A host's pause would stop the whole Listen Together room, and while the bar shows another // device this desktop's player is not what the user is controlling. - if self.lt.in_room().await || self.showing_remote() { + // Nor while casting: something playing on this computer says nothing about the speaker. + if self.lt.in_room().await || self.showing_remote() || self.casting() { return; } if active { @@ -3741,14 +4091,14 @@ impl AppState { return None; } let before = std::time::SystemTime::now(); - let position = self.player.clock()?; + let position = if self.casting() { self.cast.position()? } else { self.player.clock()? }; // The middle of the read, which takes microseconds unless mpv's core is busy. let at = before + before.elapsed().unwrap_or_default() / 2; let at_ms = at.duration_since(std::time::UNIX_EPOCH).ok()?.as_secs_f64() * 1000.0; Some(serde_json::json!({ "position": position, "atMs": at_ms, - "speed": self.player.speed(), + "speed": self.out_speed(), "paused": !self.is_playing.load(Ordering::Relaxed), })) } @@ -3934,7 +4284,7 @@ impl AppState { } /// A transient toast. Same channel as [`Self::emit_skip`], for messages that phrase themselves. - fn emit_notice(&self, message: &str) { + pub(crate) fn emit_notice(&self, message: &str) { tracing::info!(message, "playback notice"); let _ = self.app.emit("playback-notice", serde_json::json!({ "message": message })); } @@ -4059,7 +4409,7 @@ impl AppState { pub async fn on_duration(&self, secs: f64) { if secs.is_finite() && secs > 0.0 { self.queue.lock().await.duration = secs; - let speed = self.player.speed(); + let speed = self.out_speed(); if let Some(m) = &self.media { m.set_duration(secs, speed); } @@ -4427,7 +4777,7 @@ impl AppState { // synchronous on mpv's core lock. `is_playing` is kept current by `PlayerEvent::Playing`, // which mpv now pushes (it derives from `idle-active`, not just `pause`). let playing = self.is_playing.load(Ordering::Relaxed); - let speed = self.player.speed(); + let speed = self.out_speed(); if let Some(m) = &self.media { // Push when the play state flipped, or when the position moved further than one // tick of ordinary playback explains (a seek, a track change). Not every second: @@ -4832,7 +5182,7 @@ impl AppState { /// The remote's volume slider starts from what the player last said. fn link_publish_volume(&self) { let mut v = Playback::new(PlaybackKind::SetVolume); - v.volume = self.player.volume() as f64 / 100.0; + v.volume = self.out_volume() as f64 / 100.0; self.link.publish(v); } @@ -4896,7 +5246,7 @@ impl AppState { if self.lt.is_guest().await { return Ok(()); // guests can't scrub — the host controls the timeline } - if self.player.is_idle() { + if self.out_is_idle() { // Nothing loaded to seek, as with a track restored at launch: move where it will // start instead, which an exit before play also saves (#502). if let Some(item) = self.current_item().await { @@ -4905,7 +5255,7 @@ impl AppState { } return Ok(()); } - self.player.seek(position).map_err(|e| e.to_string())?; + self.out_seek(position)?; let p = Playback::at(PlaybackKind::Seek, (position * 1000.0) as i64); self.link.publish(p.clone()); if self.lt.is_host().await { @@ -4952,7 +5302,7 @@ impl AppState { // The primed lookahead almost certainly points at the wrong song now — drop it // unconditionally (cheap; re-primed below). if q.lookahead_loaded.take().is_some() { - let _ = self.player.clear_playlist(); + self.out_clear_next(); } } self.emit_queue().await; @@ -5004,7 +5354,7 @@ impl AppState { { let mut q = self.queue.lock().await; q.repeat = next(q.repeat); - let _ = self.player.set_loop_file(q.repeat == RepeatMode::One); + self.out_set_loop(q.repeat == RepeatMode::One); } self.emit_queue().await; // carries the new repeat state to the UI self.persist_queue().await; @@ -5193,7 +5543,7 @@ impl AppState { && (removed || q.lookahead_loaded != expected || before != after) { q.lookahead_loaded = None; - let _ = self.player.clear_playlist(); + self.out_clear_next(); } was_empty }; @@ -5245,7 +5595,7 @@ impl AppState { // recorded index, not `current + 1`, so a primed repeat-all wrap target is caught.) Some(i) if i == index => { q.lookahead_loaded = None; - let _ = self.player.clear_playlist(); + self.out_clear_next(); true } // The primed entry is the same song at a shifted index. @@ -5292,7 +5642,7 @@ impl AppState { self.remove_from_queue_local(*i).await; } // After the purge, so the skip cannot land on another blocked track. - if playing_blocked && !self.player.is_idle() { + if playing_blocked && !self.out_is_idle() { self.next_in_queue_local().await; // The purge can empty the tail: with repeat off and autoplay dead there is nothing to // advance into, and `next_in_queue` leaves the blocked track playing. Pausing is the @@ -5302,7 +5652,7 @@ impl AppState { q.items.get(q.current).is_some_and(|i| bl.blocks_song(i)) }; if stuck { - let _ = self.player.pause(); + self.out_pause(); } } if !upcoming.is_empty() || playing_blocked { @@ -5355,7 +5705,7 @@ impl AppState { || before != q.items.get(first).map(|i| i.video_id.clone())); if stale { q.lookahead_loaded = None; - let _ = self.player.clear_playlist(); + self.out_clear_next(); } stale }; @@ -5404,7 +5754,7 @@ impl AppState { // Indices shifted, so a primed lookahead may point at the wrong slot. Drop it // unconditionally (cheap; re-primed below), same as toggle_shuffle. if q.lookahead_loaded.take().is_some() { - let _ = self.player.clear_playlist(); + self.out_clear_next(); } } self.emit_queue().await; @@ -5445,9 +5795,7 @@ impl AppState { self.resume_or_toggle().await; } } - RemoteAction::Pause => { - let _ = self.player.pause(); - } + RemoteAction::Pause => self.out_pause(), RemoteAction::Next => self.next_in_queue_local().await, RemoteAction::Previous => self.prev_in_queue().await, RemoteAction::Seek { position_ms } => { @@ -5506,7 +5854,7 @@ impl AppState { LinkCommand::Changed => self.link_remote_refresh().await, LinkCommand::HostLost => { if self.is_playing.load(Ordering::Relaxed) { - let _ = self.player.pause(); + self.out_pause(); } } LinkCommand::Republish => { @@ -5633,7 +5981,7 @@ impl AppState { self.media_show(item); } if let Some(m) = &self.media { - let speed = self.player.speed(); + let speed = self.out_speed(); m.set_duration(snap["duration"].as_f64().unwrap_or(0.0), speed); m.set_playback(!paused, self.current_position(), speed); } diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.ts index 87620d6e7..07bb705df 100644 --- a/ui/src/lib/api.ts +++ b/ui/src/lib/api.ts @@ -1750,3 +1750,36 @@ export const onMatugenThemeChanged = (cb: (css: string | null) => void): Promise listen('matugen-theme-changed', (event) => cb(event.payload)); export const startMatugenWatcher = () => invoke('start_matugen_watcher'); export const stopMatugenWatcher = () => invoke('stop_matugen_watcher'); + +/** A Cast device on the network (src-tauri/src/castout.rs). */ +export interface CastDevice { + id: string; + name: string; + model?: string | null; + kind: 'speaker' | 'tv' | 'group'; +} +/** What is casting, and what is being connected to. */ +export interface CastState { + device: CastDevice | null; + connecting: string | null; +} +/** A cast message: `key` is translated as `cast.`. */ +export interface CastNotice { + key: string; + name?: string | null; + title?: string | null; +} + +/** The device picker opened or closed: discovery runs while it is open. Answers the devices + * already found. */ +export const castPicker = (open: boolean) => invoke('cast_picker', { open }); +/** Play on a device. Rejects with a key (`cast.`) when it can't. */ +export const castStart = (id: string) => invoke('cast_start', { id }); +export const castStop = () => invoke('cast_stop'); +export const castState = () => invoke('cast_state'); +export const onCastDevices = (cb: (devices: CastDevice[]) => void): Promise => + listen('cast-devices', (e) => cb(e.payload)); +export const onCastState = (cb: (state: CastState) => void): Promise => + listen('cast-state', (e) => cb(e.payload)); +export const onCastNotice = (cb: (notice: CastNotice) => void): Promise => + listen('cast-notice', (e) => cb(e.payload)); diff --git a/ui/src/lib/cast.svelte.ts b/ui/src/lib/cast.svelte.ts new file mode 100644 index 000000000..92acd1578 --- /dev/null +++ b/ui/src/lib/cast.svelte.ts @@ -0,0 +1,29 @@ +// Casting (#399): the devices on the network, and the one playing. Rust owns it +// (src-tauri/src/castout.rs); this mirrors its events for the device picker and the bar. +import type { CastDevice, CastState } from './api'; +import { t, type TranslationKey } from './i18n.svelte'; + +export const cast = $state<{ + devices: CastDevice[]; + /** The device playing, or null. */ + device: CastDevice | null; + /** The device being connected to, for its row's spinner. */ + connecting: string | null; +}>({ + devices: [], + device: null, + connecting: null +}); + +export function applyCastDevices(devices: CastDevice[]) { + cast.devices = devices; +} + +export function applyCastState(state: CastState) { + cast.device = state.device; + cast.connecting = state.connecting; +} + +/** A cast message from Rust, which names it by key: a notice, or why a cast couldn't start. */ +export const castMessage = (key: string, name = '', title = '') => + t(`cast.${key}` as TranslationKey, { name, title }); diff --git a/ui/src/lib/components/DevicePicker.svelte b/ui/src/lib/components/DevicePicker.svelte index 8aa70cb48..5b3691119 100644 --- a/ui/src/lib/components/DevicePicker.svelte +++ b/ui/src/lib/components/DevicePicker.svelte @@ -1,34 +1,60 @@ - {#snippet row(icon: typeof ComputerIcon, name: string, status: string, active: boolean, disabled: boolean, pick: () => void)} + {#snippet row(icon: typeof ComputerIcon, name: string, status: string, active: boolean, disabled: boolean, pick: () => void, busy = false)} {/snippet} - + {remotePlayer.name} + {:else if casting} + + {casting.name} {:else} {/if} @@ -130,18 +196,39 @@ : t('player.devices_this_computer_hint'), here, false, - () => (here ? (open = false) : playOn('')) + selectHere )} {#each link.devices as d (d.id)} {@render row( SmartPhone01Icon, d.name, deviceStatus(d), - !here && link.player?.id === d.id, + !!remotePlayer && link.player?.id === d.id, !d.online, - () => (!here && link.player?.id === d.id ? (open = false) : playOn(d.id)) + () => (remotePlayer && link.player?.id === d.id ? (open = false) : playOn(d.id)) + )} + {/each} + {#each cast.devices as d (d.id)} + {@render row( + castIcon(d), + d.name, + castStatus(d), + casting?.id === d.id, + !!cast.connecting && cast.connecting !== d.id, + () => castTo(d), + cast.connecting === d.id )} {/each} + {#if cast.devices.length === 0} +

+ {#if searching} + + {t('cast.searching')} + {:else} + {t('cast.none_found')} + {/if} +

+ {/if}