feat(playback): let Rust decide what stream to play, and say so
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.
This commit is contained in:
@@ -30,7 +30,7 @@ use crate::player::{
|
||||
};
|
||||
use crate::repository::{
|
||||
types::{GetItemsOptions, ImageOptions, ImageType},
|
||||
MediaRepository,
|
||||
MediaRepository, StreamSelection,
|
||||
};
|
||||
use crate::settings::VideoSettings;
|
||||
use crate::storage::db_service::{DatabaseService, Query, QueryParam};
|
||||
@@ -179,6 +179,18 @@ pub struct PlayItemRequest {
|
||||
pub video_codec: String,
|
||||
/// Whether the video requires server-side transcoding
|
||||
pub needs_transcoding: bool,
|
||||
/// How this item's stream is fetched, as the backend decided it.
|
||||
///
|
||||
/// Carried on the queue item so a later seek/reload does not have to guess.
|
||||
/// `None` for items queued by a path that never negotiated (audio tracks,
|
||||
/// direct URLs) and for anything queued before this field existed, where the
|
||||
/// caller falls back to `needs_transcoding` — every transcode this app
|
||||
/// requests is HLS (DR-140), so that fallback is exact rather than a guess.
|
||||
///
|
||||
/// TRACES: UR-003, UR-004, UR-079 | DR-224, DR-229
|
||||
#[serde(default)]
|
||||
pub transport: Option<crate::repository::Transport>,
|
||||
|
||||
/// Optional now-playing metadata. Used by the background-audio handoff so the
|
||||
/// lockscreen/miniplayer show the item (title/subtitle/artwork). Defaulted so
|
||||
/// existing video-only callers need not send them.
|
||||
@@ -317,9 +329,15 @@ pub enum VideoSeekResponse {
|
||||
},
|
||||
/// Reload stream from new position (transcoded non-HLS)
|
||||
ReloadStream {
|
||||
/// New stream URL starting at seek position
|
||||
new_url: String,
|
||||
/// Position offset to track (for display purposes)
|
||||
/// What to open, and how — transport included, so the frontend picks
|
||||
/// its loader from a tagged enum rather than by searching the URL for
|
||||
/// `.m3u8`. TRACES: UR-079 | DR-224
|
||||
selection: StreamSelection,
|
||||
/// `seek_offset` carries the position to RESUME AT, not a base to add to
|
||||
/// the element's clock. The reloaded stream starts at the item's zero —
|
||||
/// a position on an HLS playlist makes the server 400 every segment
|
||||
/// behind it (DR-181) — so the adapter reaches the position by seeking
|
||||
/// the element and leaves the transcode offset at zero.
|
||||
seek_offset: f64,
|
||||
},
|
||||
}
|
||||
@@ -335,8 +353,8 @@ pub enum AudioTrackSwitchResponse {
|
||||
},
|
||||
/// HTML5 needs to reload stream with new audio track
|
||||
ReloadStream {
|
||||
/// New stream URL with selected audio track
|
||||
new_url: String,
|
||||
/// What to open, and how. TRACES: UR-079 | DR-224
|
||||
selection: StreamSelection,
|
||||
/// Current position to resume from
|
||||
position: f64,
|
||||
},
|
||||
@@ -356,10 +374,13 @@ pub enum StreamQualityResponse {
|
||||
/// Position playback resumed at.
|
||||
position: f64,
|
||||
},
|
||||
/// HTML5 must reload its element with this URL.
|
||||
/// HTML5 must reload its element with this selection.
|
||||
ReloadStream {
|
||||
/// New stream URL, already transcoded to the requested ceiling.
|
||||
new_url: String,
|
||||
/// What to open, and how — already negotiated against the requested
|
||||
/// ceiling. Carries `available` too, so a picker opened after a quality
|
||||
/// change still describes the source correctly.
|
||||
/// TRACES: UR-070, UR-079 | DR-224, DR-226
|
||||
selection: StreamSelection,
|
||||
/// Position to resume from.
|
||||
position: f64,
|
||||
},
|
||||
@@ -415,6 +436,8 @@ pub(super) async fn create_media_item(
|
||||
source,
|
||||
video_codec: Some(req.video_codec),
|
||||
needs_transcoding: req.needs_transcoding,
|
||||
// The caller's negotiated transport, when it had one. TRACES: UR-079 | DR-229
|
||||
transport: req.transport,
|
||||
video_width: None, // Not available from video-only request
|
||||
video_height: None, // Not available from video-only request
|
||||
// Sideloaded subtitles, in the order the frontend sent them — that order
|
||||
@@ -663,6 +686,14 @@ pub async fn player_play_item(
|
||||
item.title, item.stream_url
|
||||
);
|
||||
|
||||
// A ceiling chosen from the in-player picker belongs to the playback it was
|
||||
// chosen for. Starting a different item returns to the device default —
|
||||
// otherwise "2 Mbps, just for this one film" quietly governs the rest of the
|
||||
// session, which is the defect DR-225 exists to close.
|
||||
//
|
||||
// TRACES: UR-074, UR-079 | DR-225
|
||||
crate::repository::online::clear_playback_quality_override();
|
||||
|
||||
// Create media item, checking for local download first
|
||||
let media_item = create_media_item(item, Some(&db)).await?;
|
||||
|
||||
@@ -762,6 +793,8 @@ pub async fn player_enter_background_audio(
|
||||
// create_media_item() because that hardcodes MediaType::Video; background
|
||||
// audio must be Audio so no video decode is started.
|
||||
let media_item = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: item.id.clone(),
|
||||
title: item.title.clone(),
|
||||
name: Some(item.title.clone()),
|
||||
@@ -935,6 +968,14 @@ pub async fn player_play_queue(
|
||||
request.shuffle
|
||||
);
|
||||
|
||||
// A ceiling chosen from the in-player picker belongs to the playback it was
|
||||
// chosen for. Starting a different item returns to the device default —
|
||||
// otherwise "2 Mbps, just for this one film" quietly governs the rest of the
|
||||
// session, which is the defect DR-225 exists to close.
|
||||
//
|
||||
// TRACES: UR-074, UR-079 | DR-225
|
||||
crate::repository::online::clear_playback_quality_override();
|
||||
|
||||
// Handle shuffle first
|
||||
if request.shuffle {
|
||||
let controller = player.0.lock().await;
|
||||
@@ -1347,7 +1388,7 @@ pub async fn player_seek(
|
||||
///
|
||||
/// This command analyzes the current video stream and automatically chooses
|
||||
/// the best seeking strategy:
|
||||
/// - HLS streams (.m3u8): Use native seeking
|
||||
/// - HLS streams: Use native seeking
|
||||
/// - Direct play streams: Use native seeking
|
||||
/// - Transcoded non-HLS: Request new stream URL from server starting at seek position
|
||||
///
|
||||
@@ -1377,7 +1418,7 @@ pub async fn player_seek_video(
|
||||
|
||||
// Get current playing item to analyze stream characteristics
|
||||
// Clone what we need to avoid holding locks across await points
|
||||
let (needs_transcoding, jellyfin_item_id, stream_url, is_local) = {
|
||||
let (needs_transcoding, jellyfin_item_id, is_local, transport) = {
|
||||
let controller = player.0.lock().await;
|
||||
let queue_arc = controller.queue();
|
||||
let queue = queue_arc.lock().map_err(|e| e.to_string())?;
|
||||
@@ -1393,18 +1434,27 @@ pub async fn player_seek_video(
|
||||
.ok_or("Current video has no Jellyfin ID")?
|
||||
.to_string();
|
||||
|
||||
let (stream_url, is_local_file) = match ¤t_item.source {
|
||||
MediaSource::Remote { stream_url, .. } => (stream_url.clone(), false),
|
||||
MediaSource::Local { .. } => (String::new(), true),
|
||||
MediaSource::DirectUrl { url } => (url.clone(), false),
|
||||
};
|
||||
// The URL itself is no longer read here: the seek strategy now comes
|
||||
// from the item's own `transport`, not from inspecting the string.
|
||||
let is_local_file = matches!(current_item.source, MediaSource::Local { .. });
|
||||
|
||||
let needs_trans = current_item.needs_transcoding;
|
||||
(needs_trans, jellyfin_id, stream_url, is_local_file)
|
||||
let transport = current_item.transport;
|
||||
(needs_trans, jellyfin_id, is_local_file, transport)
|
||||
}; // Locks are dropped here
|
||||
|
||||
// Determine seek strategy using the testable helper function
|
||||
let is_hls = stream_url.contains(".m3u8");
|
||||
// The transport comes from the backend's own decision, not from searching
|
||||
// the URL for `.m3u8` — Rust built that URL and knows what it is. Items
|
||||
// queued without one fall back to `needs_transcoding`, which is exact:
|
||||
// every transcode this app requests is HLS (DR-140).
|
||||
//
|
||||
// TRACES: UR-004, UR-079 | DR-224, DR-229
|
||||
let is_hls = match transport {
|
||||
Some(crate::repository::Transport::Hls) => true,
|
||||
Some(crate::repository::Transport::Progressive)
|
||||
| Some(crate::repository::Transport::LocalFile) => false,
|
||||
None => needs_transcoding,
|
||||
};
|
||||
let strategy = determine_video_seek_strategy(is_local, is_hls, needs_transcoding, use_html5);
|
||||
|
||||
info!("[player_seek_video] Stream analysis: is_local={}, is_hls={}, needs_transcoding={}, use_html5={}, strategy={:?}",
|
||||
@@ -1428,29 +1478,22 @@ pub async fn player_seek_video(
|
||||
// Transcoded non-HLS with HTML5 - frontend handles stream reload
|
||||
info!("[player_seek_video] HTML5 reload stream - requesting new stream URL");
|
||||
|
||||
let new_url = repository
|
||||
.get_video_stream_url(
|
||||
let selection = repository
|
||||
.get_stream_selection(
|
||||
&jellyfin_item_id,
|
||||
media_source_id.as_deref(),
|
||||
audio_stream_index,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to get video stream URL: {:?}", e))?;
|
||||
.map_err(|e| format!("Failed to select a stream: {:?}", e))?;
|
||||
|
||||
info!(
|
||||
"[player_seek_video] Got new stream URL for position {}",
|
||||
position
|
||||
"[player_seek_video] Selected {:?} over {:?} for position {}",
|
||||
selection.playback_kind, selection.transport, position
|
||||
);
|
||||
|
||||
// `seek_offset` carries the position to RESUME AT, not a base to add
|
||||
// to the element's clock. The reloaded stream starts at the item's
|
||||
// zero — a position on an HLS playlist makes the server 400 every
|
||||
// segment behind it (DR-181) — so the adapter reaches the position by
|
||||
// seeking the element and leaves the transcode offset at zero. The
|
||||
// field keeps its name only because renaming it means regenerating
|
||||
// the specta bindings; `reloadSource` documents the contract.
|
||||
Ok(VideoSeekResponse::ReloadStream {
|
||||
new_url,
|
||||
selection,
|
||||
seek_offset: position,
|
||||
})
|
||||
}
|
||||
@@ -1458,16 +1501,17 @@ pub async fn player_seek_video(
|
||||
// Transcoded non-HLS with native backend - backend handles stream reload
|
||||
info!("[player_seek_video] Backend reload stream - requesting new stream URL");
|
||||
|
||||
let new_url = repository
|
||||
.get_video_stream_url(
|
||||
let selection = repository
|
||||
.get_stream_selection(
|
||||
&jellyfin_item_id,
|
||||
media_source_id.as_deref(),
|
||||
audio_stream_index,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to get video stream URL: {:?}", e))?;
|
||||
.map_err(|e| format!("Failed to select a stream: {:?}", e))?;
|
||||
let new_url = selection.url.clone();
|
||||
|
||||
info!("[player_seek_video] Got new stream URL, handling reload internally");
|
||||
info!("[player_seek_video] Got new selection, handling reload internally");
|
||||
|
||||
// Stop current playback
|
||||
{
|
||||
@@ -1568,20 +1612,25 @@ pub async fn player_switch_audio_track(
|
||||
.to_string()
|
||||
};
|
||||
|
||||
// Get new stream URL with selected audio track. It starts at zero — an
|
||||
// HLS playlist cannot carry a position (DR-181) — and `position` below
|
||||
// tells the frontend where to seek the reloaded element back to.
|
||||
let new_url = repository
|
||||
.get_video_stream_url(
|
||||
// Select a stream carrying the chosen audio track. It starts at zero —
|
||||
// an HLS playlist cannot carry a position (DR-181) — and `position`
|
||||
// below tells the frontend where to seek the reloaded element back to.
|
||||
//
|
||||
// Pinning a track is itself a reason the source cannot be direct-played:
|
||||
// the file has one default track and the viewer asked for another, so
|
||||
// the negotiation returns a transcode. That decision lives in
|
||||
// `decide_playback_kind`, not here.
|
||||
let selection = repository
|
||||
.get_stream_selection(
|
||||
&jellyfin_item_id,
|
||||
media_source_id.as_deref(),
|
||||
Some(stream_index),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to get video stream URL: {:?}", e))?;
|
||||
.map_err(|e| format!("Failed to select a stream: {:?}", e))?;
|
||||
|
||||
Ok(AudioTrackSwitchResponse::ReloadStream {
|
||||
new_url,
|
||||
selection,
|
||||
position: current_position.unwrap_or(0.0),
|
||||
})
|
||||
} else {
|
||||
@@ -1604,23 +1653,26 @@ pub async fn player_switch_audio_track(
|
||||
/// two-sided split: HTML5 gets the URL back and reloads its own element, while a
|
||||
/// native backend is reloaded here.
|
||||
///
|
||||
/// The change applies to this playback *and* to everything started afterwards
|
||||
/// (it sets the process-wide ceiling), but it is deliberately **not** persisted:
|
||||
/// the in-player picker is a "this film, this connection" control, and the
|
||||
/// durable default belongs to Settings. `player_set_video_settings` is the one
|
||||
/// that writes to the database.
|
||||
/// The change applies to **this playback only**. The in-player picker is a
|
||||
/// "this film, this connection" control and its doc has always said so, but it
|
||||
/// used to be implemented by writing the process-wide ceiling — so choosing
|
||||
/// 2 Mbps to get one awkward film moving silently capped every video played
|
||||
/// afterwards for the rest of the process, with the Settings screen still
|
||||
/// showing the old value and nothing in the UI admitting the change. It now
|
||||
/// sets a per-playback override that the next item clears; the durable default
|
||||
/// belongs to Settings, and `player_set_video_settings` is the one that writes
|
||||
/// to the database.
|
||||
///
|
||||
/// TRACES: UR-074 | DR-162
|
||||
/// TRACES: UR-074, UR-079 | DR-162, DR-225
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
// Three of the nine arguments are Tauri `State<'_, _>` injections, not caller
|
||||
// Two of the eight arguments are Tauri `State<'_, _>` injections, not caller
|
||||
// input. Folding the rest into a struct would change the IPC contract and the
|
||||
// generated TypeScript for no readability gain.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn player_set_stream_quality(
|
||||
player: State<'_, PlayerStateWrapper>,
|
||||
repository_manager: State<'_, super::repository::RepositoryManagerWrapper>,
|
||||
video_settings: State<'_, VideoSettingsWrapper>,
|
||||
repository_handle: String,
|
||||
quality: crate::settings::StreamingQuality,
|
||||
use_html5: bool,
|
||||
@@ -1657,25 +1709,31 @@ pub async fn player_set_stream_quality(
|
||||
.to_string()
|
||||
};
|
||||
|
||||
// Set the ceiling *before* building the URL — the builder reads it.
|
||||
crate::repository::online::set_streaming_quality(quality);
|
||||
{
|
||||
let mut settings = video_settings.0.lock().map_err(|e| e.to_string())?;
|
||||
settings.streaming_quality = quality;
|
||||
}
|
||||
// Set the ceiling *before* negotiating — the negotiation and every URL
|
||||
// builder resolve through `effective_streaming_quality`, and they have to
|
||||
// agree or the cap leaks (a negotiation authorising a direct play the URL
|
||||
// builder then never gets to constrain).
|
||||
//
|
||||
// Deliberately the *override*, not the device default: see the doc above.
|
||||
// TRACES: UR-074, UR-079 | DR-225
|
||||
crate::repository::online::set_playback_quality_override(quality);
|
||||
|
||||
let position = current_position.unwrap_or(0.0);
|
||||
let new_url = repository
|
||||
.get_video_stream_url(
|
||||
let selection = repository
|
||||
.get_stream_selection(
|
||||
&jellyfin_item_id,
|
||||
media_source_id.as_deref(),
|
||||
audio_stream_index,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to get video stream URL: {:?}", e))?;
|
||||
.map_err(|e| format!("Failed to select a stream: {:?}", e))?;
|
||||
let new_url = selection.url.clone();
|
||||
|
||||
if use_html5 {
|
||||
return Ok(StreamQualityResponse::ReloadStream { new_url, position });
|
||||
return Ok(StreamQualityResponse::ReloadStream {
|
||||
selection,
|
||||
position,
|
||||
});
|
||||
}
|
||||
|
||||
// Native backend (Android/ExoPlayer): stop, repoint the queue entry at the
|
||||
@@ -2208,6 +2266,8 @@ pub async fn player_play_album_track(
|
||||
|
||||
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
|
||||
@@ -2353,6 +2413,14 @@ pub async fn player_play_tracks(
|
||||
repository_handle: String,
|
||||
request: PlayTracksRequest,
|
||||
) -> Result<PlayerStatus, String> {
|
||||
// A ceiling chosen from the in-player picker belongs to the playback it was
|
||||
// chosen for. Starting a different item returns to the device default —
|
||||
// otherwise "2 Mbps, just for this one film" quietly governs the rest of the
|
||||
// session, which is the defect DR-225 exists to close.
|
||||
//
|
||||
// TRACES: UR-074, UR-079 | DR-225
|
||||
crate::repository::online::clear_playback_quality_override();
|
||||
|
||||
info!(
|
||||
"player_play_tracks called: {} tracks, start_index={}, shuffle={}",
|
||||
request.track_ids.len(),
|
||||
@@ -2404,6 +2472,8 @@ pub async fn player_play_tracks(
|
||||
// Transform to MediaItem with 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
|
||||
@@ -3210,6 +3280,8 @@ mod tests {
|
||||
let db = DatabaseWrapper(Mutex::new(database));
|
||||
|
||||
let make_item = |id: &str| MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: id.to_string(),
|
||||
title: id.to_string(),
|
||||
name: None,
|
||||
|
||||
@@ -198,6 +198,8 @@ pub async fn player_add_track_by_id(
|
||||
// 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
|
||||
@@ -317,6 +319,8 @@ pub async fn player_add_tracks_by_ids(
|
||||
// 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
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::domain::rank_search_results;
|
||||
use crate::jellyfin::HttpClient;
|
||||
use crate::repository::{
|
||||
series_progress, types::*, HybridRepository, MediaRepository, OfflineRepository,
|
||||
OnlineRepository,
|
||||
OnlineRepository, StreamSelection,
|
||||
};
|
||||
|
||||
/// Repository handle manager
|
||||
@@ -606,6 +606,35 @@ pub async fn repository_get_video_stream_url(
|
||||
.map_err(|e| format!("{:?}", e))
|
||||
}
|
||||
|
||||
/// Decide what stream to play for a video, and describe it.
|
||||
///
|
||||
/// Replaces `repository_get_video_stream_url` for playback. The returned
|
||||
/// [`StreamSelection`] carries the transport explicitly, so the frontend picks
|
||||
/// its loader from a tagged enum instead of testing the URL for `.m3u8`; and it
|
||||
/// carries the quality ladder as it applies to *this* source, so the picker can
|
||||
/// stop offering rungs that produce the same bytes as Original.
|
||||
///
|
||||
/// No start-position parameter, for the same reason as the URL builder: a
|
||||
/// position on an HLS playlist is copied onto every segment URI and the server
|
||||
/// rejects each with `400` (DR-181). Callers resume by seeking after load.
|
||||
///
|
||||
/// TRACES: UR-070, UR-079 | DR-224, DR-226, DR-227 | UT-212
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn repository_get_stream_selection(
|
||||
manager: State<'_, RepositoryManagerWrapper>,
|
||||
handle: String,
|
||||
item_id: String,
|
||||
media_source_id: Option<String>,
|
||||
audio_stream_index: Option<i32>,
|
||||
) -> Result<StreamSelection, String> {
|
||||
let repo = manager.0.get(&handle).ok_or("Repository not found")?;
|
||||
repo.as_ref()
|
||||
.get_stream_selection(&item_id, media_source_id.as_deref(), audio_stream_index)
|
||||
.await
|
||||
.map_err(|e| format!("{:?}", e))
|
||||
}
|
||||
|
||||
/// Get an audio-only stream URL for a *video* item (background-audio handoff).
|
||||
///
|
||||
/// TRACES: UR-040 | JA-032 | UT-061
|
||||
|
||||
@@ -104,6 +104,30 @@ pub fn media_local_url(
|
||||
.ok_or_else(|| "Local media server is not running".to_string())
|
||||
}
|
||||
|
||||
/// The stream selection for a downloaded file.
|
||||
///
|
||||
/// The local-playback counterpart to `repository_get_stream_selection`. A file
|
||||
/// on disk needs no negotiation — it is a direct play over a local transport,
|
||||
/// with no quality ladder, because nothing about it can be re-negotiated — but
|
||||
/// the *frontend must not be the one to say so*. It gets the same
|
||||
/// [`StreamSelection`] shape as a streamed source so the player has one contract
|
||||
/// to consume rather than two, and so no caller has to infer a transport from a
|
||||
/// loopback URL.
|
||||
///
|
||||
/// TRACES: UR-071, UR-079 | DR-224
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn media_local_selection(
|
||||
server: State<crate::media_server::MediaServerWrapper>,
|
||||
path: String,
|
||||
) -> Result<crate::repository::StreamSelection, String> {
|
||||
server
|
||||
.0
|
||||
.as_ref()
|
||||
.map(|s| crate::repository::StreamSelection::local_file(s.url_for(&path)))
|
||||
.ok_or_else(|| "Local media server is not running".to_string())
|
||||
}
|
||||
|
||||
/// Get storage directory path (parent directory of the database file)
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
|
||||
@@ -96,6 +96,7 @@ use commands::{
|
||||
lms_unsync_player,
|
||||
mark_download_completed,
|
||||
mark_download_failed,
|
||||
media_local_selection,
|
||||
media_local_url,
|
||||
offline_get_items,
|
||||
offline_is_available,
|
||||
@@ -224,6 +225,7 @@ use commands::{
|
||||
repository_get_series_current_episode,
|
||||
repository_get_series_episodes,
|
||||
repository_get_similar_items,
|
||||
repository_get_stream_selection,
|
||||
repository_get_subtitle_url,
|
||||
repository_get_video_download_url,
|
||||
repository_get_video_stream_url,
|
||||
@@ -898,6 +900,7 @@ fn specta_builder() -> Builder<tauri::Wry> {
|
||||
mark_download_completed,
|
||||
mark_download_failed,
|
||||
media_local_url,
|
||||
media_local_selection,
|
||||
start_download,
|
||||
enqueue_download,
|
||||
enqueue_video_downloads,
|
||||
@@ -984,6 +987,7 @@ fn specta_builder() -> Builder<tauri::Wry> {
|
||||
repository_search,
|
||||
repository_get_playback_info,
|
||||
repository_get_video_stream_url,
|
||||
repository_get_stream_selection,
|
||||
repository_get_audio_stream_url,
|
||||
repository_get_audio_only_stream_url_for_video,
|
||||
repository_get_live_tv_channels,
|
||||
|
||||
@@ -1125,6 +1125,8 @@ mod tests {
|
||||
|
||||
fn create_test_item_with_jellyfin_id(id: &str, jellyfin_id: &str) -> MediaItem {
|
||||
MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: id.to_string(),
|
||||
title: format!("Track {}", id),
|
||||
name: Some(format!("Track {}", id)),
|
||||
@@ -1157,6 +1159,8 @@ mod tests {
|
||||
|
||||
fn create_test_item_local(id: &str) -> MediaItem {
|
||||
MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: id.to_string(),
|
||||
title: format!("Local Track {}", id),
|
||||
name: Some(format!("Local Track {}", id)),
|
||||
|
||||
@@ -377,6 +377,8 @@ mod tests {
|
||||
|
||||
// Create a test media item
|
||||
let media = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "test_media".to_string(),
|
||||
title: "Test Track".to_string(),
|
||||
name: Some("Test Track".to_string()),
|
||||
@@ -436,6 +438,8 @@ mod tests {
|
||||
let mut backend = NullBackend::new();
|
||||
|
||||
let media = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "test_media".to_string(),
|
||||
title: "Test Track".to_string(),
|
||||
name: Some("Test Track".to_string()),
|
||||
@@ -489,6 +493,8 @@ mod tests {
|
||||
let mut backend = NullBackend::new();
|
||||
|
||||
let media = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "test_media".to_string(),
|
||||
title: "Test Track".to_string(),
|
||||
name: Some("Test Track".to_string()),
|
||||
|
||||
@@ -115,6 +115,18 @@ pub struct MediaItem {
|
||||
/// Whether the video requires server-side transcoding
|
||||
#[serde(default)]
|
||||
pub needs_transcoding: bool,
|
||||
/// How this item's stream is fetched, as the backend decided it.
|
||||
///
|
||||
/// Carried on the queue item so a later seek/reload does not have to guess.
|
||||
/// `None` for items queued by a path that never negotiated (audio tracks,
|
||||
/// direct URLs) and for anything queued before this field existed, where the
|
||||
/// caller falls back to `needs_transcoding` — every transcode this app
|
||||
/// requests is HLS (DR-140), so that fallback is exact rather than a guess.
|
||||
///
|
||||
/// TRACES: UR-003, UR-004, UR-079 | DR-224, DR-229
|
||||
#[serde(default)]
|
||||
pub transport: Option<crate::repository::Transport>,
|
||||
|
||||
/// Video width in pixels
|
||||
#[serde(default)]
|
||||
pub video_width: Option<u32>,
|
||||
@@ -360,6 +372,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_media_item_creation_minimal() {
|
||||
let item = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "item-1".to_string(),
|
||||
title: "Test Item".to_string(),
|
||||
name: None,
|
||||
@@ -396,6 +410,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_media_item_jellyfin_id() {
|
||||
let item = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "item-2".to_string(),
|
||||
title: "Test".to_string(),
|
||||
name: None,
|
||||
@@ -431,6 +447,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_media_item_jellyfin_id_local() {
|
||||
let item = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "item-3".to_string(),
|
||||
title: "Local".to_string(),
|
||||
name: None,
|
||||
@@ -466,6 +484,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_media_item_jellyfin_id_direct_url() {
|
||||
let item = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "item-4".to_string(),
|
||||
title: "Direct".to_string(),
|
||||
name: None,
|
||||
@@ -508,6 +528,8 @@ mod tests {
|
||||
};
|
||||
|
||||
let item = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "item-subs".to_string(),
|
||||
title: "With Subs".to_string(),
|
||||
name: None,
|
||||
@@ -543,6 +565,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_media_item_serialization() {
|
||||
let item = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "serial-item".to_string(),
|
||||
title: "Serial Test".to_string(),
|
||||
name: Some("Name".to_string()),
|
||||
|
||||
@@ -1897,6 +1897,8 @@ impl PlayerController {
|
||||
.map_err(|e| format!("Failed to build audio-only URL for next episode: {}", e))?;
|
||||
|
||||
let media_item = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: next.id.clone(),
|
||||
title: next.name.clone(),
|
||||
name: Some(next.name.clone()),
|
||||
@@ -2580,6 +2582,8 @@ mod tests {
|
||||
fn create_test_items(count: usize) -> Vec<MediaItem> {
|
||||
(0..count)
|
||||
.map(|i| MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: format!("item_{}", i),
|
||||
title: format!("Track {}", i + 1),
|
||||
name: Some(format!("Track {}", i + 1)),
|
||||
@@ -3792,6 +3796,8 @@ mod tests {
|
||||
|
||||
// Queue holds the episode that just finished playing
|
||||
let episode = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
media_type: MediaType::Video,
|
||||
source: MediaSource::Remote {
|
||||
stream_url: "http://example.com/ep1.mkv".to_string(),
|
||||
@@ -3827,6 +3833,8 @@ mod tests {
|
||||
|
||||
// Mirrors what player_enter_background_audio builds: the episode as AUDIO.
|
||||
let episode = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
item_type: Some("Episode".to_string()),
|
||||
media_type: MediaType::Audio, // audio-only handoff, not Video
|
||||
series_id: Some("series1".to_string()),
|
||||
@@ -3943,6 +3951,8 @@ mod tests {
|
||||
|
||||
// Currently playing: ep2 handed off to audio-only background playback.
|
||||
let episode = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "ep2".to_string(),
|
||||
item_type: Some("Episode".to_string()),
|
||||
media_type: MediaType::Audio,
|
||||
@@ -3978,6 +3988,8 @@ mod tests {
|
||||
/// URL carrying the handoff position.
|
||||
fn audio_only_episode(runtime_seconds: f64) -> MediaItem {
|
||||
MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "ep2".to_string(),
|
||||
item_type: Some("Episode".to_string()),
|
||||
media_type: MediaType::Audio,
|
||||
@@ -3998,6 +4010,8 @@ mod tests {
|
||||
/// handoff point.
|
||||
fn local_audio_only_episode(runtime_seconds: f64) -> MediaItem {
|
||||
MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
source: MediaSource::Local {
|
||||
file_path: std::path::PathBuf::from("/downloads/ep2.mkv"),
|
||||
jellyfin_item_id: Some("ep2".to_string()),
|
||||
@@ -4682,6 +4696,8 @@ mod tests {
|
||||
controller.set_repository(Arc::new(MockEpisodeRepo::season(3)));
|
||||
|
||||
let episode = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "ep2".to_string(),
|
||||
item_type: Some("Episode".to_string()),
|
||||
media_type: MediaType::Video,
|
||||
@@ -4717,6 +4733,8 @@ mod tests {
|
||||
let controller = PlayerController::default();
|
||||
|
||||
let episode = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
media_type: MediaType::Video,
|
||||
source: MediaSource::Remote {
|
||||
stream_url: "http://example.com/ep1.mkv".to_string(),
|
||||
|
||||
@@ -541,6 +541,8 @@ mod tests {
|
||||
fn create_test_items(count: usize) -> Vec<MediaItem> {
|
||||
(0..count)
|
||||
.map(|i| MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: format!("item_{}", i),
|
||||
title: format!("Track {}", i + 1),
|
||||
name: Some(format!("Track {}", i + 1)),
|
||||
|
||||
@@ -232,6 +232,8 @@ mod tests {
|
||||
|
||||
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()),
|
||||
@@ -263,6 +265,8 @@ mod tests {
|
||||
|
||||
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()),
|
||||
|
||||
@@ -316,6 +316,8 @@ mod tests {
|
||||
// Helper function to create test MediaItem instances
|
||||
fn create_test_media_item(id: &str, title: &str) -> MediaItem {
|
||||
MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: id.to_string(),
|
||||
title: title.to_string(),
|
||||
name: None,
|
||||
|
||||
@@ -258,6 +258,8 @@ mod tests {
|
||||
/// `StartTimeTicks` is the handoff point.
|
||||
fn handoff_item() -> MediaItem {
|
||||
MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
id: "ep2".to_string(),
|
||||
title: "Episode 2".to_string(),
|
||||
name: None,
|
||||
@@ -303,6 +305,8 @@ mod tests {
|
||||
// `/Audio/{id}/stream?Static=true` — a real Content-Length and byte
|
||||
// ranges, so ExoPlayer resumes it where the load failed.
|
||||
let track = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
item_type: Some("Audio".to_string()),
|
||||
..handoff_item()
|
||||
};
|
||||
@@ -314,6 +318,8 @@ mod tests {
|
||||
// An HLS playlist declares its segments, so a failed segment load is
|
||||
// retried at that segment, not at the start of the episode.
|
||||
let video = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
media_type: MediaType::Video,
|
||||
..handoff_item()
|
||||
};
|
||||
@@ -324,6 +330,8 @@ mod tests {
|
||||
fn test_downloaded_episode_keeps_the_players_retry() {
|
||||
// A local file has no length problem and no network to lose.
|
||||
let local = MediaItem {
|
||||
// Audio and direct-URL items never negotiate a transport.
|
||||
transport: None,
|
||||
source: MediaSource::Local {
|
||||
file_path: PathBuf::from("/data/ep2.mkv"),
|
||||
jellyfin_item_id: Some("ep2".to_string()),
|
||||
|
||||
@@ -97,6 +97,24 @@ impl HybridRepository {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Decide what stream to play and describe it fully — the DR-224 contract.
|
||||
///
|
||||
/// Online-only for the same reason as `get_video_stream_url`: an offline
|
||||
/// item is a file on disk, and the caller builds
|
||||
/// [`StreamSelection::local_file`] for it rather than negotiating anything.
|
||||
///
|
||||
/// TRACES: UR-070, UR-079 | DR-224, DR-226, DR-227
|
||||
pub async fn get_stream_selection(
|
||||
&self,
|
||||
item_id: &str,
|
||||
media_source_id: Option<&str>,
|
||||
audio_stream_index: Option<i32>,
|
||||
) -> Result<super::StreamSelection, RepoError> {
|
||||
self.online
|
||||
.get_stream_selection(item_id, media_source_id, audio_stream_index)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Get an audio-only stream URL for a video item (background-audio handoff).
|
||||
/// Online-only, like `get_video_stream_url`.
|
||||
///
|
||||
|
||||
@@ -5,11 +5,14 @@ pub mod hybrid;
|
||||
pub mod offline;
|
||||
pub mod online;
|
||||
pub mod series_progress;
|
||||
/// Backend-owned stream selection (UR-079 / DR-224).
|
||||
pub mod stream_selection;
|
||||
pub mod types;
|
||||
|
||||
pub use hybrid::HybridRepository;
|
||||
pub use offline::OfflineRepository;
|
||||
pub use online::{JRayActor, OnlineRepository};
|
||||
pub use stream_selection::{StreamSelection, Transport};
|
||||
pub use types::*;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
+771
-252
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,416 @@
|
||||
//! What stream to play, decided in Rust and handed to a player whole.
|
||||
//!
|
||||
//! Every player backend — mpv, ExoPlayer, the webview `<video>`/hls.js path —
|
||||
//! used to receive a bare URL and re-derive the rest: the frontend decided
|
||||
//! "is this HLS?" by looking for `.m3u8` in the string, and nothing anywhere
|
||||
//! carried *why* a stream was transcoded or what else the source could have
|
||||
//! offered. This module is the replacement contract: one self-describing
|
||||
//! [`StreamSelection`] that says what the stream is, how to fetch it, and what
|
||||
//! the alternatives were.
|
||||
//!
|
||||
//! The division of labour it encodes — **Rust decides *what stream*, the player
|
||||
//! decides *how to deliver it*** — is the point. A multi-variant playlist handed
|
||||
//! to ExoPlayer is still ExoPlayer's to adapt over; Rust never paces bytes.
|
||||
//!
|
||||
//! TRACES: UR-079 | DR-224
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::settings::StreamingQuality;
|
||||
|
||||
/// How the bytes of a chosen stream are fetched.
|
||||
///
|
||||
/// This field exists to delete a substring search. The frontend previously
|
||||
/// decided which loader to attach by testing `url.contains(".m3u8")`, which is a
|
||||
/// domain fact reconstructed in the presentation layer — the same class of leak
|
||||
/// as the item-type taxonomy that `check:boundary` guards, and one that breaks
|
||||
/// silently the moment a server serves a playlist from a path that does not end
|
||||
/// in `.m3u8`, or serves a progressive file from one that does.
|
||||
///
|
||||
/// Tagged (`{"type":"hls"}`) rather than a bare string so the frontend matches a
|
||||
/// discriminant instead of comparing text.
|
||||
///
|
||||
/// TRACES: UR-079 | DR-224
|
||||
#[derive(specta::Type, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "camelCase")]
|
||||
pub enum Transport {
|
||||
/// An HLS playlist. The webview attaches hls.js (or Safari's native loader);
|
||||
/// ExoPlayer uses its HLS media source.
|
||||
Hls,
|
||||
/// A single progressive HTTP resource, seekable by byte range.
|
||||
Progressive,
|
||||
/// A file already on disk — a completed download, or the loopback media
|
||||
/// server standing in front of one.
|
||||
LocalFile,
|
||||
}
|
||||
|
||||
/// What the server is doing to the source to produce this stream.
|
||||
///
|
||||
/// Distinct from [`Transport`] because the two are genuinely independent: a
|
||||
/// direct-streamed remux and a transcode can both arrive over HLS, and a direct
|
||||
/// play can arrive progressively or as a local file. Keeping them apart is what
|
||||
/// lets the UI say "this is not costing the server anything" without inferring
|
||||
/// it from a URL shape.
|
||||
///
|
||||
/// TRACES: UR-079 | DR-227
|
||||
#[derive(specta::Type, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "camelCase")]
|
||||
pub enum PlaybackKind {
|
||||
/// The source file is served untouched. No server CPU, no quality loss.
|
||||
DirectPlay,
|
||||
/// The container is repackaged but the codecs are copied — cheap, and
|
||||
/// visually identical to the source.
|
||||
DirectStream,
|
||||
/// The server is re-encoding. The only case where a bitrate ceiling can
|
||||
/// actually be honoured, and the only one that costs the server real work.
|
||||
Transcode,
|
||||
}
|
||||
|
||||
impl PlaybackKind {
|
||||
/// Whether the server is spending encoder time on this stream.
|
||||
///
|
||||
/// The queue carries a `needs_transcoding` flag that predates this enum and
|
||||
/// that several seek/reload paths still branch on; this keeps the two from
|
||||
/// drifting by making one derive from the other.
|
||||
///
|
||||
/// TRACES: UR-079 | DR-227
|
||||
pub fn needs_transcoding(&self) -> bool {
|
||||
matches!(self, PlaybackKind::Transcode)
|
||||
}
|
||||
}
|
||||
|
||||
/// The rendition actually negotiated — what the viewer is receiving right now.
|
||||
///
|
||||
/// `None` on a [`StreamSelection`] when the source is being direct-played as-is:
|
||||
/// there is no *chosen* rendition in that case, only the file itself, and
|
||||
/// reporting the ceiling that happened to be set would misdescribe it.
|
||||
///
|
||||
/// TRACES: UR-079 | DR-224, DR-225
|
||||
#[derive(specta::Type, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Rendition {
|
||||
/// The rung of the ladder this stream was built against.
|
||||
pub quality: StreamingQuality,
|
||||
/// Total bits per second the stream may use, when a ceiling applies.
|
||||
pub max_bitrate: Option<u64>,
|
||||
/// Resolution ceiling, when one applies. `None` preserves the source's.
|
||||
pub max_height: Option<u32>,
|
||||
/// Video codec the server was asked to produce.
|
||||
pub video_codec: Option<String>,
|
||||
/// Audio codec the server was asked to produce.
|
||||
pub audio_codec: Option<String>,
|
||||
}
|
||||
|
||||
/// One rung of the quality picker, as it applies to *this* media source.
|
||||
///
|
||||
/// The picker used to be filled from the fixed [`StreamingQuality::ALL`] ladder,
|
||||
/// which meant offering "20 Mbps" for a 1.1 Mbps podcast — eight rungs, six of
|
||||
/// them indistinguishable from Original. `exceeds_source` is what lets the
|
||||
/// frontend render that honestly without knowing anything about bitrates.
|
||||
///
|
||||
/// TRACES: UR-070, UR-079 | DR-226, DR-121
|
||||
#[derive(specta::Type, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct QualityOption {
|
||||
pub quality: StreamingQuality,
|
||||
/// Human label ("8 Mbps"). Lives in Rust beside the number it describes.
|
||||
pub label: String,
|
||||
/// Secondary line ("1080p").
|
||||
pub detail: String,
|
||||
/// True when this rung's ceiling is at or above what the source itself
|
||||
/// carries, so selecting it yields the same stream as `Original`.
|
||||
///
|
||||
/// The frontend renders these differently (or hides them); it does not
|
||||
/// decide which they are.
|
||||
pub exceeds_source: bool,
|
||||
/// The source's own bitrate, when the server reported one. Presentation
|
||||
/// only — the picker shows "Original (6.7 Mbps)" rather than a bare word.
|
||||
pub source_bitrate: Option<u64>,
|
||||
}
|
||||
|
||||
/// Everything a player backend needs to open a stream, and everything the UI
|
||||
/// needs to describe it.
|
||||
///
|
||||
/// Replaces the bare `String` URL that `get_video_stream_url` used to return.
|
||||
///
|
||||
/// TRACES: UR-079 | DR-224, DR-226, DR-227
|
||||
#[derive(specta::Type, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct StreamSelection {
|
||||
/// The URL (or loopback URL) to open.
|
||||
pub url: String,
|
||||
/// How to fetch it. Replaces the `.m3u8` substring check.
|
||||
pub transport: Transport,
|
||||
/// What the server is doing to the source to produce it.
|
||||
pub playback_kind: PlaybackKind,
|
||||
/// The negotiated rendition; `None` when direct-playing the source as-is.
|
||||
pub rendition: Option<Rendition>,
|
||||
/// What this media source can offer, for the quality picker (DR-226).
|
||||
pub available: Vec<QualityOption>,
|
||||
/// The media source this selection is for, so a later re-open (quality
|
||||
/// change, audio-track switch, transcoded seek) targets the same one.
|
||||
pub media_source_id: Option<String>,
|
||||
/// The transcode identity the server keyed this job by, when there is one.
|
||||
pub play_session_id: Option<String>,
|
||||
/// Whether the server is spending encoder time on this stream.
|
||||
///
|
||||
/// Derived from [`playback_kind`](Self::playback_kind) rather than left for
|
||||
/// the frontend to compute: "which kinds count as transcoding" is a domain
|
||||
/// rule, and a direct *stream* is a remux that must not be counted. The
|
||||
/// queue's long-standing `needs_transcoding` flag and the seek strategy both
|
||||
/// read this, so there is one answer rather than three.
|
||||
///
|
||||
/// TRACES: UR-079 | DR-224, DR-227
|
||||
pub needs_transcoding: bool,
|
||||
}
|
||||
|
||||
impl StreamSelection {
|
||||
/// A selection for a file already on disk.
|
||||
///
|
||||
/// A downloaded file is a direct play by definition — the bytes are the
|
||||
/// source's — and offering a quality ladder over it would be a lie, since
|
||||
/// nothing about a local file can be re-negotiated.
|
||||
///
|
||||
/// TRACES: UR-071, UR-079 | DR-224
|
||||
pub fn local_file(url: impl Into<String>) -> Self {
|
||||
Self {
|
||||
url: url.into(),
|
||||
transport: Transport::LocalFile,
|
||||
playback_kind: PlaybackKind::DirectPlay,
|
||||
rendition: None,
|
||||
available: Vec::new(),
|
||||
media_source_id: None,
|
||||
play_session_id: None,
|
||||
needs_transcoding: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the quality ladder as it applies to a source of a known bitrate.
|
||||
///
|
||||
/// Every rung is returned — the picker stays a fixed, predictable list rather
|
||||
/// than one that changes length per item — but each is marked with whether it
|
||||
/// would actually constrain *this* source. A rung whose ceiling is at or above
|
||||
/// the source bitrate produces the same bytes as `Original`, so presenting it as
|
||||
/// a distinct choice is noise.
|
||||
///
|
||||
/// `source_bitrate` is `None` when the server did not report one (it is absent
|
||||
/// for some containers — the sampled library has `avi` files with no bitrate at
|
||||
/// all). In that case nothing can be judged redundant and every rung is offered,
|
||||
/// which is the safe direction: the viewer keeps every choice they had before.
|
||||
///
|
||||
/// TRACES: UR-070, UR-079 | DR-226, DR-121 | UT-211
|
||||
pub fn quality_options_for_source(source_bitrate: Option<u64>) -> Vec<QualityOption> {
|
||||
StreamingQuality::ALL
|
||||
.iter()
|
||||
.map(|quality| QualityOption {
|
||||
quality: *quality,
|
||||
label: quality.label().to_string(),
|
||||
detail: quality.detail().to_string(),
|
||||
exceeds_source: match (quality.max_bitrate(), source_bitrate) {
|
||||
// `Original` is the source; it never "exceeds" it.
|
||||
(None, _) => false,
|
||||
// Nothing known about the source — judge nothing redundant.
|
||||
(Some(_), None) => false,
|
||||
(Some(cap), Some(source)) => cap >= source,
|
||||
},
|
||||
source_bitrate,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The tag the frontend matches on has to be exactly what it expects, and
|
||||
/// it is a *string in TypeScript* — nothing but a test keeps the two in step.
|
||||
///
|
||||
/// TRACES: UR-079 | DR-224 | UT-211
|
||||
#[test]
|
||||
fn test_transport_serialises_with_the_tag_the_frontend_matches() {
|
||||
let cases = [
|
||||
(Transport::Hls, r#"{"type":"hls"}"#),
|
||||
(Transport::Progressive, r#"{"type":"progressive"}"#),
|
||||
(Transport::LocalFile, r#"{"type":"localFile"}"#),
|
||||
];
|
||||
for (transport, expected) in cases {
|
||||
let json = serde_json::to_string(&transport).expect("serialises");
|
||||
assert_eq!(json, expected, "wire shape of {transport:?}");
|
||||
let back: Transport = serde_json::from_str(&json).expect("round-trips");
|
||||
assert_eq!(back, transport);
|
||||
}
|
||||
}
|
||||
|
||||
/// TRACES: UR-079 | DR-227 | UT-211
|
||||
#[test]
|
||||
fn test_playback_kind_serialises_with_the_tag_the_frontend_matches() {
|
||||
let cases = [
|
||||
(PlaybackKind::DirectPlay, r#"{"type":"directPlay"}"#),
|
||||
(PlaybackKind::DirectStream, r#"{"type":"directStream"}"#),
|
||||
(PlaybackKind::Transcode, r#"{"type":"transcode"}"#),
|
||||
];
|
||||
for (kind, expected) in cases {
|
||||
let json = serde_json::to_string(&kind).expect("serialises");
|
||||
assert_eq!(json, expected, "wire shape of {kind:?}");
|
||||
let back: PlaybackKind = serde_json::from_str(&json).expect("round-trips");
|
||||
assert_eq!(back, kind);
|
||||
}
|
||||
}
|
||||
|
||||
/// Only a transcode costs the server encoder time. A direct *stream* is a
|
||||
/// remux — cheap, and not what `needs_transcoding` has ever meant.
|
||||
///
|
||||
/// TRACES: UR-079 | DR-227 | UT-211
|
||||
#[test]
|
||||
fn test_only_transcode_counts_as_transcoding() {
|
||||
assert!(PlaybackKind::Transcode.needs_transcoding());
|
||||
assert!(!PlaybackKind::DirectStream.needs_transcoding());
|
||||
assert!(!PlaybackKind::DirectPlay.needs_transcoding());
|
||||
}
|
||||
|
||||
/// A local file is a direct play over a local transport, with no ladder:
|
||||
/// nothing about a file on disk can be re-negotiated.
|
||||
///
|
||||
/// TRACES: UR-071, UR-079 | DR-224 | UT-211
|
||||
#[test]
|
||||
fn test_local_file_selection_offers_no_ladder() {
|
||||
let selection = StreamSelection::local_file("http://127.0.0.1:9000/media/x.mkv");
|
||||
assert_eq!(selection.transport, Transport::LocalFile);
|
||||
assert_eq!(selection.playback_kind, PlaybackKind::DirectPlay);
|
||||
assert!(selection.rendition.is_none());
|
||||
assert!(selection.available.is_empty());
|
||||
assert!(!selection.needs_transcoding);
|
||||
}
|
||||
|
||||
/// The camelCase rule applies to nested struct fields too, and
|
||||
/// `playbackKind` is the one the frontend branches on.
|
||||
///
|
||||
/// TRACES: UR-079 | DR-224 | UT-211
|
||||
#[test]
|
||||
fn test_stream_selection_fields_are_camel_case_on_the_wire() {
|
||||
let selection = StreamSelection {
|
||||
url: "https://example/master.m3u8".to_string(),
|
||||
transport: Transport::Hls,
|
||||
playback_kind: PlaybackKind::Transcode,
|
||||
rendition: Some(Rendition {
|
||||
quality: StreamingQuality::Mbps8,
|
||||
max_bitrate: Some(8_000_000),
|
||||
max_height: Some(1080),
|
||||
video_codec: Some("h264".to_string()),
|
||||
audio_codec: Some("aac".to_string()),
|
||||
}),
|
||||
available: Vec::new(),
|
||||
media_source_id: Some("src-1".to_string()),
|
||||
play_session_id: Some("sess-1".to_string()),
|
||||
needs_transcoding: true,
|
||||
};
|
||||
let json = serde_json::to_string(&selection).expect("serialises");
|
||||
assert!(
|
||||
json.contains(r#""playbackKind":{"type":"transcode"}"#),
|
||||
"{json}"
|
||||
);
|
||||
assert!(json.contains(r#""transport":{"type":"hls"}"#), "{json}");
|
||||
assert!(json.contains(r#""mediaSourceId":"src-1""#), "{json}");
|
||||
assert!(json.contains(r#""playSessionId":"sess-1""#), "{json}");
|
||||
assert!(json.contains(r#""maxBitrate":8000000"#), "{json}");
|
||||
assert!(json.contains(r#""maxHeight":1080"#), "{json}");
|
||||
assert!(json.contains(r#""needsTranscoding":true"#), "{json}");
|
||||
}
|
||||
|
||||
/// The measured library has 1.1 Mbps sources in it. Offering those a choice
|
||||
/// of 20, 10, 8, 4 and 2 Mbps is offering five ways to spell "Original".
|
||||
///
|
||||
/// TRACES: UR-070, UR-079 | DR-226, DR-121 | UT-211
|
||||
#[test]
|
||||
fn test_rungs_above_the_source_bitrate_are_marked_redundant() {
|
||||
let options = quality_options_for_source(Some(1_122_137));
|
||||
let redundant: Vec<_> = options
|
||||
.iter()
|
||||
.filter(|o| o.exceeds_source)
|
||||
.map(|o| o.quality)
|
||||
.collect();
|
||||
assert_eq!(
|
||||
redundant,
|
||||
vec![
|
||||
StreamingQuality::Mbps20,
|
||||
StreamingQuality::Mbps10,
|
||||
StreamingQuality::Mbps8,
|
||||
StreamingQuality::Mbps4,
|
||||
StreamingQuality::Mbps2,
|
||||
],
|
||||
"every rung at or above a 1.12 Mbps source is the source"
|
||||
);
|
||||
|
||||
// The rungs that genuinely constrain it are not marked.
|
||||
let constraining: Vec<_> = options
|
||||
.iter()
|
||||
.filter(|o| !o.exceeds_source)
|
||||
.map(|o| o.quality)
|
||||
.collect();
|
||||
assert_eq!(
|
||||
constraining,
|
||||
vec![
|
||||
StreamingQuality::Original,
|
||||
StreamingQuality::Mbps1,
|
||||
StreamingQuality::Kbps720,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/// `Original` is the source, so it is never "above" it — not even for a
|
||||
/// source whose bitrate is unknown or zero.
|
||||
///
|
||||
/// TRACES: UR-070, UR-079 | DR-226 | UT-211
|
||||
#[test]
|
||||
fn test_original_is_never_marked_as_exceeding_the_source() {
|
||||
for bitrate in [None, Some(0), Some(1), Some(50_000_000)] {
|
||||
let options = quality_options_for_source(bitrate);
|
||||
let original = options
|
||||
.iter()
|
||||
.find(|o| o.quality == StreamingQuality::Original)
|
||||
.expect("Original is always offered");
|
||||
assert!(!original.exceeds_source, "bitrate {bitrate:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// An `avi` with no reported bitrate must not lose the picker. Judging
|
||||
/// nothing redundant is the safe direction — the viewer keeps every choice.
|
||||
///
|
||||
/// TRACES: UR-070, UR-079 | DR-226 | UT-211
|
||||
#[test]
|
||||
fn test_an_unknown_source_bitrate_keeps_every_rung_offered() {
|
||||
let options = quality_options_for_source(None);
|
||||
assert_eq!(options.len(), StreamingQuality::ALL.len());
|
||||
assert!(
|
||||
options.iter().all(|o| !o.exceeds_source),
|
||||
"nothing can be judged redundant without a source bitrate"
|
||||
);
|
||||
assert!(options.iter().all(|o| o.source_bitrate.is_none()));
|
||||
}
|
||||
|
||||
/// A 4K remux constrains at every rung — the ladder is fully meaningful.
|
||||
///
|
||||
/// TRACES: UR-070, UR-079 | DR-226 | UT-211
|
||||
#[test]
|
||||
fn test_a_source_above_the_ladder_marks_nothing_redundant() {
|
||||
let options = quality_options_for_source(Some(40_000_000));
|
||||
assert!(options.iter().all(|o| !o.exceeds_source));
|
||||
}
|
||||
|
||||
/// The picker's text comes from Rust, beside the numbers it describes, so a
|
||||
/// relabelled rung cannot drift out of step with what it does.
|
||||
///
|
||||
/// TRACES: UR-070, UR-079 | DR-226 | UT-211
|
||||
#[test]
|
||||
fn test_options_carry_the_ladder_labels() {
|
||||
let options = quality_options_for_source(Some(6_652_961));
|
||||
assert_eq!(options.len(), StreamingQuality::ALL.len());
|
||||
for (option, quality) in options.iter().zip(StreamingQuality::ALL) {
|
||||
assert_eq!(option.quality, quality);
|
||||
assert_eq!(option.label, quality.label());
|
||||
assert_eq!(option.detail, quality.detail());
|
||||
assert_eq!(option.source_bitrate, Some(6_652_961));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -469,6 +469,15 @@ pub struct LiveStreamInfo {
|
||||
pub play_session_id: Option<String>,
|
||||
pub live_stream_id: Option<String>,
|
||||
pub media_source_id: Option<String>,
|
||||
/// How to open `stream_url`.
|
||||
///
|
||||
/// A live channel is always an HLS transcode — the server has to repackage a
|
||||
/// broadcast mux into something a browser can play, and there is no static
|
||||
/// file to direct-play. Saying so here means the player page never has to
|
||||
/// work it out from the URL, which is the whole of DR-224.
|
||||
///
|
||||
/// TRACES: UR-079 | DR-224
|
||||
pub transport: super::stream_selection::Transport,
|
||||
}
|
||||
|
||||
/// Genre
|
||||
|
||||
Reference in New Issue
Block a user