Playing a video meant asking the server to re-encode it, always. That
decision was made nowhere and written down nowhere, so whoever needed it
re-derived it downstream — the player worked out whether it had been handed
a playlist by looking for ".m3u8" in the URL, in two places. A viewer paid
for a transcode of a file their device could have played untouched, and the
app could not tell them which it was.
One negotiation now produces one self-describing StreamSelection — direct
play, remux or transcode; over a playlist, a plain HTTP file, or a local one
— and every renderer consumes that same answer.
Measured against the development server (Jellyfin 10.11.5), 400 items
sampled for codec mix and 40 put through a real PlaybackInfo negotiation
per profile:
Linux / WebKitGTK (h264 only, 2ch) 3/40 — 7% direct play
Android / ExoPlayer (hevc, ac3/eac3, 6ch) 34/40 — 85% direct play
The library is ~80% hevc, which is why the two diverge so hard. The payoff
is overwhelmingly Android, where 85% of plays were starting a transcode
nobody needed. Linux stays near 7% until libmpv decodes the picture — the
h264-only profile is a WebKitGTK constraint, not a JellyTau choice.
DR-219 StreamSelection: url + tagged Transport (hls/progressive/localFile)
+ PlaybackKind (directPlay/directStream/transcode) + the negotiated
rendition + this source's ladder + a needs_transcoding flag derived
in Rust so the rule is answered once. Both enums are serde-tagged
so the frontend matches a discriminant, not a substring. The paths
that never negotiate get the same shape from Rust rather than
assembling one — media_local_selection for a downloaded file,
LiveStreamInfo.transport for a live channel — so there is no second
place where a transport is decided.
DR-220 The ceiling becomes two levels: a durable device default (Settings,
persisted) and a per-playback override the in-player picker sets.
The picker had called itself a "this film, this connection" control
since it was written but wrote the process-wide default, so dropping
one awkward film to 2 Mbps silently capped every video played
afterwards for the rest of the process, with Settings still showing
the old value. The override is cleared whenever playback moves to a
new item, which stops it surviving into an autoplayed next episode.
effective_streaming_quality() is the single resolution point.
DR-221 The quality picker is filled from what this media source can offer.
Rust marks a rung exceeds_source when its ceiling is at or above the
source's own bitrate — such a rung is another way to spell Original
— and the frontend does not draw those. Original is never marked; a
source whose bitrate the server does not report marks nothing, which
keeps every rung offered.
DR-222 Direct play and direct stream are negotiated, with two client-side
overrides on top because the server's answer is right about the file
and wrong about what this app will do with it: undecodable audio
(Jellyfin 10.11.5 honours a DirectPlayProfile's container and video
codec but ignores its audio codec, so it offers direct play for an
E-AC-3 track the webview renders in silence) and a viewer-pinned
audio track the file does not default to. A direct stream is a remux
and is deliberately not counted as transcoding.
DR-223 Dropped on measurement, not deferred. A master playlist from this
server carries exactly one EXT-X-STREAM-INF: Jellyfin builds it from
the single rendition the request asked for rather than publishing a
ladder. So there is no adaptation for hls.js to be preserving and
none mpv would lose — the claim that there was, in
playback-backend-unification.md, does not hold. Recorded rather than
deleted because it is a measurement: a server that does publish a
ladder would change the answer.
DR-224 Every backend consumes the same selection. The queue item carries
the transport, so player_seek_video picks its seek strategy from the
backend's decision instead of the last stream_url.contains(".m3u8")
in the codebase. Items queued by a path that never negotiated carry
None and fall back to needs_transcoding, which is exact rather than
a guess because every transcode this app requests is HLS (DR-140).
The frontend loader decision moves to streamTransport.ts so it can be
tested: the two cases that pin it are the ones that failed against the old
implementation — a progressive stream whose URL contains ".m3u8" must not
get an HLS loader, and an HLS stream whose URL contains none must.
Also verified the URL the direct-play branch builds actually serves playable
bytes: 206, video/mp4, valid ISO-BMFF, and a mid-file range works, so
seeking a direct play works.
The spec is folded into docs/architecture/{01,02,03} and deleted, per the
rule that docs/specs holds only work that has not shipped. DR-121 leaves
read-through-media-cache.md with a pointer; that spec keeps its capture half.
Not verified: real playback on a device. Direct play changes what actually
gets played, and neither fixtures nor curl prove the WebKitGTK and ExoPlayer
paths render it.
420 lines
14 KiB
Rust
420 lines
14 KiB
Rust
//! Queue manipulation commands (add / remove / move / skip).
|
|
//!
|
|
//! TRACES: UR-015 | DR-005, DR-020
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use log::info;
|
|
use serde::Deserialize;
|
|
use tauri::State;
|
|
|
|
use super::{
|
|
check_for_local_download, create_media_item, get_player_status, get_queue_status,
|
|
DatabaseWrapper, PlayItemRequest, PlayerStateWrapper, PlayerStatus, QueueStatus,
|
|
};
|
|
use crate::commands::repository::RepositoryManagerWrapper;
|
|
use crate::player::{MediaItem, MediaSource, MediaType};
|
|
use crate::repository::types::{ImageOptions, ImageType};
|
|
use crate::repository::MediaRepository;
|
|
|
|
/// Request to add items to queue
|
|
#[derive(specta::Type, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct AddToQueueRequest {
|
|
pub items: Vec<PlayItemRequest>,
|
|
pub position: String, // "next" or "end"
|
|
}
|
|
|
|
/// Request to add a track by ID - backend fetches metadata
|
|
#[derive(specta::Type, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct AddTrackByIdRequest {
|
|
pub track_id: String,
|
|
pub position: String, // "next" or "end"
|
|
}
|
|
|
|
/// Request to add multiple tracks by IDs - backend fetches metadata
|
|
#[derive(specta::Type, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct AddTracksByIdsRequest {
|
|
pub track_ids: Vec<String>,
|
|
pub position: String, // "next" or "end"
|
|
}
|
|
|
|
#[tauri::command]
|
|
#[specta::specta]
|
|
pub async fn player_add_to_queue(
|
|
player: State<'_, PlayerStateWrapper>,
|
|
db: State<'_, DatabaseWrapper>,
|
|
request: AddToQueueRequest,
|
|
) -> Result<QueueStatus, String> {
|
|
use crate::player::queue::AddPosition;
|
|
|
|
let position = match request.position.as_str() {
|
|
"next" => AddPosition::Next,
|
|
_ => AddPosition::End,
|
|
};
|
|
|
|
// Create media items first (without holding any locks during await)
|
|
let mut items: Vec<MediaItem> = Vec::new();
|
|
for req in request.items {
|
|
items.push(create_media_item(req, Some(&db)).await?);
|
|
}
|
|
|
|
// Now add to queue
|
|
let controller = player.0.lock().await;
|
|
let queue = controller.queue();
|
|
let mut queue_lock = queue.lock().map_err(|e| e.to_string())?;
|
|
|
|
queue_lock.add(items, position);
|
|
|
|
let result = QueueStatus {
|
|
items: queue_lock.items().to_vec(),
|
|
current_index: queue_lock.current_index(),
|
|
shuffle: queue_lock.is_shuffle(),
|
|
repeat: queue_lock.repeat_mode(),
|
|
has_next: queue_lock.has_next(),
|
|
has_previous: queue_lock.has_previous(),
|
|
};
|
|
|
|
drop(queue_lock);
|
|
controller.emit_queue_changed();
|
|
|
|
Ok(result)
|
|
}
|
|
|
|
#[tauri::command]
|
|
#[specta::specta]
|
|
pub async fn player_remove_from_queue(
|
|
player: State<'_, PlayerStateWrapper>,
|
|
index: usize,
|
|
) -> Result<QueueStatus, String> {
|
|
let controller = player.0.lock().await;
|
|
let queue = controller.queue();
|
|
let mut queue_lock = queue.lock().map_err(|e| e.to_string())?;
|
|
|
|
queue_lock.remove(index);
|
|
|
|
let result = QueueStatus {
|
|
items: queue_lock.items().to_vec(),
|
|
current_index: queue_lock.current_index(),
|
|
shuffle: queue_lock.is_shuffle(),
|
|
repeat: queue_lock.repeat_mode(),
|
|
has_next: queue_lock.has_next(),
|
|
has_previous: queue_lock.has_previous(),
|
|
};
|
|
|
|
drop(queue_lock);
|
|
controller.emit_queue_changed();
|
|
|
|
Ok(result)
|
|
}
|
|
|
|
#[tauri::command]
|
|
#[specta::specta]
|
|
pub async fn player_move_in_queue(
|
|
player: State<'_, PlayerStateWrapper>,
|
|
from_index: usize,
|
|
to_index: usize,
|
|
) -> Result<QueueStatus, String> {
|
|
let controller = player.0.lock().await;
|
|
let queue = controller.queue();
|
|
let mut queue_lock = queue.lock().map_err(|e| e.to_string())?;
|
|
|
|
if !queue_lock.move_item(from_index, to_index) {
|
|
return Err("Invalid indices for move operation".to_string());
|
|
}
|
|
|
|
let result = QueueStatus {
|
|
items: queue_lock.items().to_vec(),
|
|
current_index: queue_lock.current_index(),
|
|
shuffle: queue_lock.is_shuffle(),
|
|
repeat: queue_lock.repeat_mode(),
|
|
has_next: queue_lock.has_next(),
|
|
has_previous: queue_lock.has_previous(),
|
|
};
|
|
|
|
drop(queue_lock);
|
|
controller.emit_queue_changed();
|
|
|
|
Ok(result)
|
|
}
|
|
|
|
/// Add a track to queue by ID - backend fetches metadata and constructs URLs
|
|
#[tauri::command]
|
|
#[specta::specta]
|
|
pub async fn player_add_track_by_id(
|
|
player: State<'_, PlayerStateWrapper>,
|
|
db: State<'_, DatabaseWrapper>,
|
|
repository_manager: State<'_, RepositoryManagerWrapper>,
|
|
repository_handle: String,
|
|
request: AddTrackByIdRequest,
|
|
) -> Result<QueueStatus, String> {
|
|
use crate::player::queue::AddPosition;
|
|
|
|
info!(
|
|
"player_add_track_by_id called: track_id={}, position={}",
|
|
request.track_id, request.position
|
|
);
|
|
|
|
// Get repository (hybrid - supports offline/online)
|
|
let repository = repository_manager
|
|
.0
|
|
.get(&repository_handle)
|
|
.ok_or("Repository not found - user may need to log in")?;
|
|
|
|
// Fetch track metadata via repository
|
|
info!(
|
|
"Fetching metadata for track {} via repository",
|
|
request.track_id
|
|
);
|
|
let track = repository
|
|
.get_item(&request.track_id)
|
|
.await
|
|
.map_err(|e| format!("Failed to fetch track metadata: {}", e))?;
|
|
|
|
// Check for local download first
|
|
let local_path = check_for_local_download(&db, &request.track_id).await?;
|
|
|
|
let source = if let Some(path) = local_path {
|
|
info!("Using local download for track {}", request.track_id);
|
|
MediaSource::Local {
|
|
file_path: PathBuf::from(path),
|
|
jellyfin_item_id: Some(track.id.clone()),
|
|
}
|
|
} else {
|
|
// Get stream URL from repository (works online/offline)
|
|
let stream_url = repository
|
|
.get_audio_stream_url(&track.id)
|
|
.await
|
|
.map_err(|e| format!("Failed to get stream URL for {}: {}", track.name, e))?;
|
|
|
|
MediaSource::Remote {
|
|
stream_url,
|
|
jellyfin_item_id: track.id.clone(),
|
|
}
|
|
};
|
|
|
|
// Build MediaItem with artwork URL from repository and frontend-compatible fields
|
|
let primary_image_tag_for_url = track.primary_image_tag.clone();
|
|
let media_item = MediaItem {
|
|
// Audio and direct-URL items never negotiate a transport.
|
|
transport: None,
|
|
id: track.id.clone(),
|
|
title: track.name.clone(),
|
|
name: Some(track.name.clone()), // Frontend compatibility
|
|
artist: track
|
|
.album_artist
|
|
.clone()
|
|
.or_else(|| track.artists.as_ref().and_then(|a| a.first().cloned())),
|
|
album: track.album_name.clone(),
|
|
album_name: track.album_name.clone(), // Frontend compatibility
|
|
album_id: track.album_id.clone(),
|
|
artist_items: track.artist_items.clone(), // For clickable artist links
|
|
artists: track.artists.clone(), // Fallback artist info
|
|
primary_image_tag: track.primary_image_tag.clone(), // For frontend image display
|
|
image_id: track.primary_image_tag.clone(),
|
|
item_type: Some(track.item_type.clone()), // Frontend compatibility
|
|
playlist_id: None,
|
|
duration: track.runtime_ticks.map(|t| t as f64 / 10_000_000.0),
|
|
artwork_url: primary_image_tag_for_url.and_then(|tag| {
|
|
track.album_id.as_ref().map(|album_id| {
|
|
repository.get_image_url(
|
|
album_id,
|
|
ImageType::Primary,
|
|
Some(ImageOptions {
|
|
max_width: Some(300),
|
|
tag: Some(tag),
|
|
..Default::default()
|
|
}),
|
|
)
|
|
})
|
|
}),
|
|
media_type: MediaType::Audio,
|
|
source,
|
|
video_codec: None,
|
|
needs_transcoding: false,
|
|
video_width: None,
|
|
video_height: None,
|
|
subtitles: vec![],
|
|
series_id: None,
|
|
server_id: None,
|
|
};
|
|
|
|
// Add to queue at specified position
|
|
let position = match request.position.as_str() {
|
|
"next" => AddPosition::Next,
|
|
_ => AddPosition::End,
|
|
};
|
|
|
|
let controller = player.0.lock().await;
|
|
let queue = controller.queue();
|
|
let mut queue_lock = queue.lock().map_err(|e| e.to_string())?;
|
|
|
|
queue_lock.add(vec![media_item], position);
|
|
|
|
let result = get_queue_status(&controller);
|
|
drop(queue_lock);
|
|
controller.emit_queue_changed();
|
|
|
|
info!("Successfully added track {} to queue", request.track_id);
|
|
Ok(result)
|
|
}
|
|
|
|
/// Add multiple tracks to queue by IDs - backend fetches metadata and constructs URLs
|
|
#[tauri::command]
|
|
#[specta::specta]
|
|
pub async fn player_add_tracks_by_ids(
|
|
player: State<'_, PlayerStateWrapper>,
|
|
db: State<'_, DatabaseWrapper>,
|
|
repository_manager: State<'_, RepositoryManagerWrapper>,
|
|
repository_handle: String,
|
|
request: AddTracksByIdsRequest,
|
|
) -> Result<QueueStatus, String> {
|
|
use crate::player::queue::AddPosition;
|
|
|
|
info!(
|
|
"player_add_tracks_by_ids called: {} tracks, position={}",
|
|
request.track_ids.len(),
|
|
request.position
|
|
);
|
|
|
|
// Get repository (hybrid - supports offline/online)
|
|
let repository = repository_manager
|
|
.0
|
|
.get(&repository_handle)
|
|
.ok_or("Repository not found - user may need to log in")?;
|
|
|
|
// Fetch metadata and build MediaItems for all tracks
|
|
let mut media_items = Vec::new();
|
|
for track_id in &request.track_ids {
|
|
info!("Fetching metadata for track {} via repository", track_id);
|
|
let track = repository
|
|
.get_item(track_id)
|
|
.await
|
|
.map_err(|e| format!("Failed to fetch track metadata for {}: {}", track_id, e))?;
|
|
|
|
// Check for local download first
|
|
let local_path = check_for_local_download(&db, track_id).await?;
|
|
|
|
let source = if let Some(path) = local_path {
|
|
info!("Using local download for track {}", track_id);
|
|
MediaSource::Local {
|
|
file_path: PathBuf::from(path),
|
|
jellyfin_item_id: Some(track.id.clone()),
|
|
}
|
|
} else {
|
|
// Get stream URL from repository (works online/offline)
|
|
let stream_url = repository
|
|
.get_audio_stream_url(&track.id)
|
|
.await
|
|
.map_err(|e| format!("Failed to get stream URL for {}: {}", track.name, e))?;
|
|
|
|
MediaSource::Remote {
|
|
stream_url,
|
|
jellyfin_item_id: track.id.clone(),
|
|
}
|
|
};
|
|
|
|
// Build MediaItem with artwork URL from repository and frontend-compatible fields
|
|
let primary_image_tag_for_url = track.primary_image_tag.clone();
|
|
let media_item = MediaItem {
|
|
// Audio and direct-URL items never negotiate a transport.
|
|
transport: None,
|
|
id: track.id.clone(),
|
|
title: track.name.clone(),
|
|
name: Some(track.name.clone()), // Frontend compatibility
|
|
artist: track
|
|
.album_artist
|
|
.clone()
|
|
.or_else(|| track.artists.as_ref().and_then(|a| a.first().cloned())),
|
|
album: track.album_name.clone(),
|
|
album_name: track.album_name.clone(), // Frontend compatibility
|
|
album_id: track.album_id.clone(),
|
|
artist_items: track.artist_items.clone(), // For clickable artist links
|
|
artists: track.artists.clone(), // Fallback artist info
|
|
primary_image_tag: track.primary_image_tag.clone(), // For frontend image display
|
|
image_id: track.primary_image_tag.clone(),
|
|
item_type: Some(track.item_type.clone()), // Frontend compatibility
|
|
playlist_id: None,
|
|
duration: track.runtime_ticks.map(|t| t as f64 / 10_000_000.0),
|
|
artwork_url: primary_image_tag_for_url.and_then(|tag| {
|
|
track.album_id.as_ref().map(|album_id| {
|
|
repository.get_image_url(
|
|
album_id,
|
|
ImageType::Primary,
|
|
Some(ImageOptions {
|
|
max_width: Some(300),
|
|
tag: Some(tag),
|
|
..Default::default()
|
|
}),
|
|
)
|
|
})
|
|
}),
|
|
media_type: MediaType::Audio,
|
|
source,
|
|
video_codec: None,
|
|
needs_transcoding: false,
|
|
video_width: None,
|
|
video_height: None,
|
|
subtitles: vec![],
|
|
series_id: None,
|
|
server_id: None,
|
|
};
|
|
|
|
media_items.push(media_item);
|
|
}
|
|
|
|
// Add to queue at specified position
|
|
let position = match request.position.as_str() {
|
|
"next" => AddPosition::Next,
|
|
_ => AddPosition::End,
|
|
};
|
|
|
|
let controller = player.0.lock().await;
|
|
let queue = controller.queue();
|
|
let mut queue_lock = queue.lock().map_err(|e| e.to_string())?;
|
|
|
|
queue_lock.add(media_items, position);
|
|
|
|
let result = get_queue_status(&controller);
|
|
drop(queue_lock);
|
|
controller.emit_queue_changed();
|
|
|
|
info!(
|
|
"Successfully added {} tracks to queue",
|
|
request.track_ids.len()
|
|
);
|
|
Ok(result)
|
|
}
|
|
|
|
#[tauri::command]
|
|
#[specta::specta]
|
|
pub async fn player_skip_to(
|
|
player: State<'_, PlayerStateWrapper>,
|
|
db: State<'_, DatabaseWrapper>,
|
|
index: usize,
|
|
) -> Result<PlayerStatus, String> {
|
|
let controller = player.0.lock().await;
|
|
|
|
// Prefer downloads that completed since the queue was built
|
|
if let Err(e) = super::refresh_queue_local_sources(&controller, &db).await {
|
|
log::warn!("[player_skip_to] Failed to refresh local sources: {}", e);
|
|
}
|
|
|
|
// Skip to the index and get the item to play
|
|
let item = {
|
|
let queue = controller.queue();
|
|
let mut queue_lock = queue.lock().map_err(|e| e.to_string())?;
|
|
queue_lock.skip_to(index).cloned().ok_or("Invalid index")?
|
|
};
|
|
|
|
// Play the item without modifying the queue
|
|
controller.load_and_play(&item).map_err(|e| e.to_string())?;
|
|
|
|
// Emit queue changed event
|
|
controller.emit_queue_changed();
|
|
|
|
Ok(get_player_status(&controller))
|
|
}
|