//! Versioned, device-local control API. This is not a device-sync wire protocol. use std::path::PathBuf; use serde::{Deserialize, Serialize}; pub const API_VERSION: u16 = 1; pub const MAX_MESSAGE_BYTES: usize = 2 * 1024 * 1024; /// Third-octave levels, from 20 Hz to 20 kHz, quantized to 0..=255. pub const SPECTRUM_BANDS: usize = 31; pub fn discovery_path() -> Option { directories::ProjectDirs::from("", "", "furumi") .map(|dirs| dirs.data_local_dir().join("control-endpoint.json")) } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Discovery { pub api_version: u16, pub port: u16, pub token: String, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct Snapshot { pub api_version: u16, #[serde(default, skip_serializing_if = "is_false")] pub spectrum_supported: bool, #[serde(default, skip_serializing_if = "is_false")] pub liked_radio_supported: bool, pub revision: u64, pub updated_at_ms: u64, pub role: Role, pub self_device_id: String, pub active_device_id: Option, pub active_device_name: Option, pub playing: bool, pub paused: bool, pub position_secs: f64, pub duration_secs: f64, pub volume: u8, pub shuffle: bool, pub repeat: Repeat, pub radio: Radio, /// Optional for backward compatibility with local API servers predating /// remote command acknowledgement. This is not a device-sync wire field. #[serde(default, skip_serializing_if = "Option::is_none")] pub remote_command: Option, pub track: Option, pub devices: Vec, } #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum Role { Active, Control, Jam, } #[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum Repeat { #[default] Off, All, One, } #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)] pub struct Radio { pub id: u64, #[serde(default, skip_serializing_if = "Option::is_none")] pub request_id: Option, pub state: RadioState, pub error: Option, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct LikedRadioSeed { pub key: String, pub title: String, pub artist: String, } #[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum RadioState { #[default] Idle, Searching, Starting, Playing, Failed, } #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)] pub struct RemoteCommand { pub id: u64, pub state: RemoteCommandState, pub error: Option, } #[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum RemoteCommandState { #[default] Idle, Pending, Confirmed, Failed, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct Track { /// Stable playback identity, including podcast episodes (never a row ID). pub key: String, pub title: String, pub artist: String, pub album: String, pub year: Option, /// Personal library state for this track. Absent on older servers and /// when the current item cannot be liked. #[serde(default, skip_serializing_if = "Option::is_none")] pub liked: Option, /// Opaque reference; clients cannot ask the server to read arbitrary paths. pub artwork_id: Option, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct Device { pub id: String, pub name: String, pub is_self: bool, pub active: bool, pub online: bool, pub can_transfer: bool, pub unavailable_reason: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] pub enum ClientMessage { Authenticate { token: String, api_version: u16, }, Command { id: String, command: Command, }, /// Local to this authenticated connection. Only request after the server /// advertises spectrum_supported. SubscribeSpectrum { enabled: bool, }, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[serde(tag = "type", rename_all = "snake_case")] pub enum Command { PlayPause, Next, Previous, Seek { seconds: i64, }, VolumeUp, VolumeDown, ToggleShuffle, CycleRepeat, StartRadio, PickLikedRadioSeed { #[serde(default, skip_serializing_if = "Option::is_none")] exclude: Option, #[serde(default, skip_serializing_if = "Option::is_none")] replace_request_id: Option, }, StartLikedRadio { seed_key: String, }, SetLiked { track_key: String, liked: bool, }, Transfer { device_id: String, }, Artwork { artwork_id: String, #[serde(default, skip_serializing_if = "is_false")] federation: bool, }, } fn is_false(value: &bool) -> bool { !*value } #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] pub enum ServerMessage { Snapshot { snapshot: Box, }, /// A new epoch invalidates visual history. No bands means clear the display. /// Only explicit subscribers receive this message. Spectrum { epoch: u64, sequence: u64, bands: Option<[u8; SPECTRUM_BANDS]>, }, /// `accepted` means dispatched locally, not acknowledged by a remote engine. Result { id: String, accepted: bool, error: Option, }, LikedRadioSeed { id: String, seed: LikedRadioSeed, }, Artwork { id: String, artwork_id: String, png_base64: Option, }, }