mpv now decodes video on Linux, drawn into a framebuffer we own and blitted
into the default vbox's draw handler. Tauri's widget tree is untouched, so an
upgrade that assumes its own layout cannot invalidate this. Direct play means
the original file, hardware decoding, and no server transcode at all — where
previously every desktop video was re-encoded to h264 for the browser engine,
whatever the file actually was. Off by default: JELLYTAU_NATIVE_VIDEO=1.
That settles finding 2 of playback-backend-unification.md — "native video
cannot be composited with a Tauri webview" — by demonstration rather than
argument, on X11 and Wayland both.
Turning it on exposed nine defects, none of them mpv's. Each was the same
mistake in a different place: a capability written down as a compile-time fact
about the platform, or a state asserted instead of confirmed.
DR-238/246 a seek routed by the stream's container rather than by what the
engine could do with it - correct only while one player handled
those streams, silent the moment another did
DR-239 a property handled but never observed, so the play/pause button
waited for an event that could not arrive
DR-240 fullscreen expanding the document while the window stayed put
DR-241 a seek issued before the engine had a file, failed, and discarded
- which is why resume began at zero
DR-247 a Linux-only gate outliving the caller that made it Linux-only,
breaking the Android build outright
DR-250 a stop aimed at whichever renderer bookkeeping believed was in
charge, missing the one actually making sound
DR-251 a duration of zero believed, leaving the seek bar no scale
DR-252 a junk float converted to a Duration, panicking the backend the
instant a length-less stream appeared
So the MediaPlayer contract (DR-242 … DR-247): `open` carries a start position,
so no caller sequences load-then-seek and none can race an engine's load;
`seek` states a destination and leaves in-place-versus-re-open to the engine;
`snapshot` is one coherent read; and `Phase::Opening` names the window where
intent used to be lost. One conformance suite runs against every engine —
FakePlayer and mpv under cargo test, ExoPlayer instrumented on a device — so an
engine is either correct or visibly failing.
Two of the nine were introduced during this work and caught on hardware, not by
any suite: an over-broad capability that grouped ExoPlayer with mpv, and the
Duration panic. The suites test engines that behave. That is recorded in
docs/native-player-verification.md, which asks for the exact action sequences
that found them.
Verified: all automated gates, conformance (mpv 9/9, legacy 8/9 by design,
ExoPlayer 7/7 on device), and manual desktop and Android passes on real
hardware.
Known open and deliberately shipped: resume reads local progress and never the
server's; the background-audio handoff still declares a state swap it does not
confirm (the symptom is now impossible, the race is not); and `bun run
android:dev` builds an APK carrying the release application id, whose failure
message advises an uninstall that would destroy app data. Fix that last one
before anyone else builds for Android.
Squashed from worktree-linux-native-video, which keeps the per-defect history.
385 lines
12 KiB
Rust
385 lines
12 KiB
Rust
use super::media::MediaItem;
|
|
/**
|
|
* Media Session Management
|
|
*
|
|
* Tracks high-level playback context (Audio/Movie/TvShow/Idle) that persists
|
|
* beyond individual playback states. Enables persistent UI (miniplayer) and
|
|
* proper transitions between content types.
|
|
*
|
|
* See docs/architecture/01-rust-backend.md for the state machine diagram.
|
|
*/
|
|
use log::info;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// Media session type tracking the high-level playback context
|
|
#[derive(specta::Type, Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
|
pub enum MediaSessionType {
|
|
/// No active session - browsing library
|
|
Idle,
|
|
|
|
/// Audio playback session (music, audiobooks, podcasts)
|
|
/// Persists until explicitly dismissed
|
|
Audio {
|
|
/// Last/current track being played
|
|
last_item: Option<MediaItem>,
|
|
/// True = playing/paused, False = stopped/ended
|
|
is_active: bool,
|
|
},
|
|
|
|
/// Movie playback (single video, auto-dismiss on end)
|
|
Movie {
|
|
/// Currently loaded movie
|
|
item: MediaItem,
|
|
/// True = playing/paused, False = ended
|
|
is_active: bool,
|
|
},
|
|
|
|
/// TV show playback (supports next episode auto-advance)
|
|
TvShow {
|
|
/// Currently loaded episode
|
|
item: MediaItem,
|
|
/// Series ID for fetching next episodes
|
|
series_id: String,
|
|
/// True = playing/paused, False = ended
|
|
is_active: bool,
|
|
},
|
|
}
|
|
|
|
impl MediaSessionType {
|
|
/// Check if this is an active session (any type except Idle)
|
|
#[allow(dead_code)]
|
|
pub fn is_active_session(&self) -> bool {
|
|
!matches!(self, MediaSessionType::Idle)
|
|
}
|
|
|
|
/// Check if playback is currently active within the session
|
|
#[allow(dead_code)]
|
|
pub fn is_playing_or_paused(&self) -> bool {
|
|
match self {
|
|
MediaSessionType::Audio { is_active, .. } => *is_active,
|
|
MediaSessionType::Movie { is_active, .. } => *is_active,
|
|
MediaSessionType::TvShow { is_active, .. } => *is_active,
|
|
MediaSessionType::Idle => false,
|
|
}
|
|
}
|
|
|
|
/// Get the current media item if any
|
|
#[allow(dead_code)]
|
|
pub fn current_item(&self) -> Option<&MediaItem> {
|
|
match self {
|
|
MediaSessionType::Audio { last_item, .. } => last_item.as_ref(),
|
|
MediaSessionType::Movie { item, .. } => Some(item),
|
|
MediaSessionType::TvShow { item, .. } => Some(item),
|
|
MediaSessionType::Idle => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Manages media session state transitions
|
|
pub struct MediaSessionManager {
|
|
current: MediaSessionType,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
impl MediaSessionManager {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
current: MediaSessionType::Idle,
|
|
}
|
|
}
|
|
|
|
/// Get the current session state
|
|
pub fn current(&self) -> &MediaSessionType {
|
|
&self.current
|
|
}
|
|
|
|
/// Start an audio session with a queue
|
|
/// Transitions: Idle → Audio(active), Any → Audio(active)
|
|
pub fn start_audio_session(&mut self, first_item: MediaItem) {
|
|
info!(
|
|
"[MediaSession] Starting audio session: {}",
|
|
first_item.title
|
|
);
|
|
self.current = MediaSessionType::Audio {
|
|
last_item: Some(first_item),
|
|
is_active: true,
|
|
};
|
|
}
|
|
|
|
/// Update audio session with new track (during playback)
|
|
pub fn update_audio_track(&mut self, item: MediaItem) {
|
|
if let MediaSessionType::Audio {
|
|
last_item,
|
|
is_active,
|
|
} = &mut self.current
|
|
{
|
|
info!("[MediaSession] Updating audio track: {}", item.title);
|
|
*last_item = Some(item);
|
|
*is_active = true;
|
|
}
|
|
}
|
|
|
|
/// Mark audio session as inactive (playback ended, queue finished)
|
|
/// Session persists for resume
|
|
pub fn audio_session_inactive(&mut self) {
|
|
if let MediaSessionType::Audio { is_active, .. } = &mut self.current {
|
|
info!("[MediaSession] Audio session now inactive (queue ended)");
|
|
*is_active = false;
|
|
}
|
|
}
|
|
|
|
/// Resume audio session
|
|
pub fn resume_audio_session(&mut self) {
|
|
if let MediaSessionType::Audio { is_active, .. } = &mut self.current {
|
|
info!("[MediaSession] Resuming audio session");
|
|
*is_active = true;
|
|
}
|
|
}
|
|
|
|
/// Start a movie session
|
|
/// Transitions: Any → Movie(active)
|
|
pub fn start_movie_session(&mut self, item: MediaItem) {
|
|
info!("[MediaSession] Starting movie session: {}", item.title);
|
|
self.current = MediaSessionType::Movie {
|
|
item,
|
|
is_active: true,
|
|
};
|
|
}
|
|
|
|
/// Mark movie session as inactive (playback ended)
|
|
/// Movie sessions auto-dismiss to Idle
|
|
pub fn movie_session_ended(&mut self) {
|
|
if matches!(self.current, MediaSessionType::Movie { .. }) {
|
|
info!("[MediaSession] Movie ended, transitioning to Idle");
|
|
self.current = MediaSessionType::Idle;
|
|
}
|
|
}
|
|
|
|
/// Start a TV show session
|
|
/// Transitions: Any → TvShow(active)
|
|
pub fn start_tv_session(&mut self, item: MediaItem, series_id: String) {
|
|
info!("[MediaSession] Starting TV show session: {}", item.title);
|
|
self.current = MediaSessionType::TvShow {
|
|
item,
|
|
series_id,
|
|
is_active: true,
|
|
};
|
|
}
|
|
|
|
/// Mark TV show session as inactive (episode ended, awaiting next)
|
|
pub fn tv_session_episode_ended(&mut self) {
|
|
if let MediaSessionType::TvShow { is_active, .. } = &mut self.current {
|
|
info!("[MediaSession] Episode ended, awaiting next episode");
|
|
*is_active = false;
|
|
}
|
|
}
|
|
|
|
/// Advance to next episode in TV session
|
|
pub fn tv_session_next_episode(&mut self, next_item: MediaItem) {
|
|
if let MediaSessionType::TvShow {
|
|
item, is_active, ..
|
|
} = &mut self.current
|
|
{
|
|
info!(
|
|
"[MediaSession] Advancing to next episode: {}",
|
|
next_item.title
|
|
);
|
|
*item = next_item;
|
|
*is_active = true;
|
|
}
|
|
}
|
|
|
|
/// End TV show session (series complete or user dismissed)
|
|
pub fn tv_session_ended(&mut self) {
|
|
if matches!(self.current, MediaSessionType::TvShow { .. }) {
|
|
info!("[MediaSession] TV show session ended, transitioning to Idle");
|
|
self.current = MediaSessionType::Idle;
|
|
}
|
|
}
|
|
|
|
/// Dismiss/clear current session (user action)
|
|
/// Transitions: Any → Idle
|
|
pub fn dismiss(&mut self) {
|
|
info!("[MediaSession] Dismissing session: {:?}", self.current);
|
|
self.current = MediaSessionType::Idle;
|
|
}
|
|
|
|
/// Check if we should show miniplayer
|
|
pub fn should_show_miniplayer(&self) -> bool {
|
|
matches!(self.current, MediaSessionType::Audio { .. })
|
|
}
|
|
|
|
/// Check if we should show video player
|
|
pub fn should_show_video_player(&self) -> bool {
|
|
matches!(
|
|
self.current,
|
|
MediaSessionType::Movie { .. } | MediaSessionType::TvShow { .. }
|
|
)
|
|
}
|
|
}
|
|
|
|
impl Default for MediaSessionManager {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::player::media::MediaSource;
|
|
|
|
fn create_test_audio_item(title: &str) -> MediaItem {
|
|
MediaItem {
|
|
// Audio and direct-URL items never negotiate a transport.
|
|
transport: None,
|
|
id: title.to_string(),
|
|
title: title.to_string(),
|
|
name: Some(title.to_string()),
|
|
artist: Some("Test Artist".to_string()),
|
|
album: Some("Test Album".to_string()),
|
|
album_name: Some("Test Album".to_string()),
|
|
album_id: None,
|
|
artist_items: None,
|
|
artists: Some(vec!["Test Artist".to_string()]),
|
|
primary_image_tag: None,
|
|
image_id: None,
|
|
item_type: Some("Audio".to_string()),
|
|
playlist_id: None,
|
|
duration: Some(180.0),
|
|
artwork_url: None,
|
|
media_type: crate::player::media::MediaType::Audio,
|
|
source: MediaSource::DirectUrl {
|
|
url: "http://example.com/audio.mp3".to_string(),
|
|
},
|
|
video_codec: None,
|
|
needs_transcoding: false,
|
|
video_width: None,
|
|
video_height: None,
|
|
subtitles: vec![],
|
|
series_id: None,
|
|
server_id: None,
|
|
}
|
|
}
|
|
|
|
fn create_test_movie_item(title: &str) -> MediaItem {
|
|
MediaItem {
|
|
// Audio and direct-URL items never negotiate a transport.
|
|
transport: None,
|
|
id: title.to_string(),
|
|
title: title.to_string(),
|
|
name: Some(title.to_string()),
|
|
artist: None,
|
|
album: None,
|
|
album_name: None,
|
|
album_id: None,
|
|
artist_items: None,
|
|
artists: None,
|
|
primary_image_tag: None,
|
|
image_id: None,
|
|
item_type: Some("Movie".to_string()),
|
|
playlist_id: None,
|
|
duration: Some(7200.0),
|
|
artwork_url: None,
|
|
media_type: crate::player::media::MediaType::Video,
|
|
source: MediaSource::DirectUrl {
|
|
url: "http://example.com/movie.mp4".to_string(),
|
|
},
|
|
video_codec: None,
|
|
needs_transcoding: false,
|
|
video_width: None,
|
|
video_height: None,
|
|
subtitles: vec![],
|
|
series_id: None,
|
|
server_id: None,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_initial_state_is_idle() {
|
|
let manager = MediaSessionManager::new();
|
|
assert_eq!(manager.current(), &MediaSessionType::Idle);
|
|
}
|
|
|
|
#[test]
|
|
fn test_audio_session_lifecycle() {
|
|
let mut manager = MediaSessionManager::new();
|
|
|
|
// Start audio session
|
|
let track = create_test_audio_item("Track 1");
|
|
manager.start_audio_session(track.clone());
|
|
|
|
assert!(matches!(
|
|
manager.current(),
|
|
MediaSessionType::Audio {
|
|
is_active: true,
|
|
..
|
|
}
|
|
));
|
|
assert!(manager.should_show_miniplayer());
|
|
|
|
// Update to next track
|
|
let track2 = create_test_audio_item("Track 2");
|
|
manager.update_audio_track(track2);
|
|
assert!(manager.current().is_playing_or_paused());
|
|
|
|
// Queue ends, session goes inactive
|
|
manager.audio_session_inactive();
|
|
assert!(matches!(
|
|
manager.current(),
|
|
MediaSessionType::Audio {
|
|
is_active: false,
|
|
..
|
|
}
|
|
));
|
|
assert!(manager.should_show_miniplayer()); // Still shows!
|
|
|
|
// Resume
|
|
manager.resume_audio_session();
|
|
assert!(manager.current().is_playing_or_paused());
|
|
|
|
// Dismiss
|
|
manager.dismiss();
|
|
assert_eq!(manager.current(), &MediaSessionType::Idle);
|
|
assert!(!manager.should_show_miniplayer());
|
|
}
|
|
|
|
#[test]
|
|
fn test_movie_session_auto_dismiss() {
|
|
let mut manager = MediaSessionManager::new();
|
|
|
|
let movie = create_test_movie_item("Test Movie");
|
|
manager.start_movie_session(movie);
|
|
|
|
assert!(matches!(
|
|
manager.current(),
|
|
MediaSessionType::Movie {
|
|
is_active: true,
|
|
..
|
|
}
|
|
));
|
|
assert!(manager.should_show_video_player());
|
|
|
|
// Movie ends, auto-dismiss to Idle
|
|
manager.movie_session_ended();
|
|
assert_eq!(manager.current(), &MediaSessionType::Idle);
|
|
}
|
|
|
|
#[test]
|
|
fn test_session_replacement() {
|
|
let mut manager = MediaSessionManager::new();
|
|
|
|
// Start with audio
|
|
let track = create_test_audio_item("Track 1");
|
|
manager.start_audio_session(track);
|
|
assert!(manager.should_show_miniplayer());
|
|
|
|
// Switch to movie (replaces audio session)
|
|
let movie = create_test_movie_item("Test Movie");
|
|
manager.start_movie_session(movie);
|
|
assert!(!manager.should_show_miniplayer());
|
|
assert!(manager.should_show_video_player());
|
|
}
|
|
}
|