//! Player events for frontend communication via Tauri events. //! //! These events are emitted from the player backend to notify the frontend //! of playback state changes, position updates, etc. //! //! TRACES: UR-005, UR-019, UR-023, UR-026 | DR-001, DR-028, DR-047 #[cfg(test)] use crate::utils::lock::MutexSafe; use log::error; use serde::{Deserialize, Serialize}; use std::sync::Arc; use tauri::AppHandle; use tauri_specta::Event; use super::{MediaSessionType, SleepTimerMode}; /// Events emitted by the player backend to the frontend via Tauri events. /// /// These are distinct from `PlayerEvent` in state.rs, which handles internal /// state machine transitions. /// /// TRACES: UR-005, UR-019, UR-023, UR-026 | DR-001, DR-028, DR-047 #[derive(Debug, Clone, Serialize, Deserialize, specta::Type, tauri_specta::Event)] // NOTE: fields are intentionally snake_case on the wire. specta generates the // TypeScript bindings with snake_case field names (it does not apply serde's // `rename_all_fields`), so adding `rename_all_fields = "camelCase"` here makes // serde emit camelCase payloads that no longer match the generated schema — // tauri-specta then silently drops those events (e.g. state_changed, // queue_changed never reach the frontend, so the mini player never appears). // Keep serde and specta agreeing: snake_case fields, snake_case variant tags. #[serde(tag = "type", rename_all = "snake_case")] pub enum PlayerStatusEvent { /// Playback position updated (emitted periodically during playback) PositionUpdate { /// Current position in seconds position: f64, /// Total duration in seconds duration: f64, }, /// Player state changed StateChanged { /// New state: "playing", "paused", "stopped", "loading", "idle" state: String, /// ID of the current media item, if any media_id: Option, }, /// Media has finished loading and is ready to play MediaLoaded { /// Total duration in seconds duration: f64, }, /// Playback has ended naturally (reached end of media) PlaybackEnded, /// Buffering state changed Buffering { /// Buffering progress (0-100) percent: u8, }, /// An error occurred during playback Error { /// Error message message: String, /// Whether the error is recoverable recoverable: bool, }, /// Volume changed VolumeChanged { /// New volume level (0.0-1.0) volume: f32, /// Whether audio is muted muted: bool, }, /// Sleep timer state changed SleepTimerChanged { /// Sleep timer mode mode: SleepTimerMode, /// Remaining seconds (for time-based timer) remaining_seconds: u32, }, /// Time-based sleep timer expired: playback must stop. The backend stops /// its own (MPV/ExoPlayer) playback, but HTML5 video on Linux plays in the /// webview outside the backend's control — the frontend pauses it on this /// event. SleepTimerExpired, /// Show next episode popup with countdown ShowNextEpisodePopup { /// Current episode that just finished current_episode: crate::repository::types::MediaItem, /// Next episode to play next_episode: crate::repository::types::MediaItem, /// Countdown duration in seconds countdown_seconds: u32, /// Whether to auto-advance when countdown reaches 0 auto_advance: bool, }, /// Countdown tick (emitted every second during autoplay countdown) CountdownTick { /// Remaining seconds in countdown remaining_seconds: u32, }, /// Queue changed (items added, removed, reordered, or playback mode changed) QueueChanged { /// All items in the queue items: Vec, /// Current item index current_index: Option, /// Whether shuffle is enabled shuffle: bool, /// Current repeat mode repeat: crate::player::queue::RepeatMode, /// Whether there's a next track available has_next: bool, /// Whether there's a previous track available has_previous: bool, }, /// Media session changed (activity context changed: Audio/Movie/TvShow/Idle) SessionChanged { /// Current session state session: MediaSessionType, }, /// Remote sessions updated (for cast/remote control UI) SessionsUpdated { /// All active controllable sessions from Jellyfin sessions: Vec, }, /// The user asked to disconnect from the remote session and resume locally. /// /// Emitted when the lockscreen Stop button is pressed while casting. The /// frontend owns the two-step remote->local transfer (it must reload the /// media item locally), so the native side only signals intent here. RemoteDisconnectRequested, /// Backend-originated control command targeting the active frontend player /// adapter (the HTML5