feat(player): cap streaming bandwidth with a user-chosen bitrate ceiling
Video streams were opened at a fixed allowance nobody could change: MaxStreamingBitrate=20000000/VideoBitrate=18000000 on the HLS transcode URL, 20 Mbps in the PlaybackInfo negotiation, and a 999999999 device profile that let the server direct-play a source of any size. On a metered or slow connection there was no way to spend less. StreamingQuality is a ladder of bandwidth ceilings — Original, 20/10/8/ 4/2/1 Mbps and 720 kbps — where a step bundles the total ceiling, the audio share of it and the resolution that budget can carry. Those numbers are Jellyfin encoding vocabulary, so they live in Rust and the frontend only names a variant; labels and details come back over IPC from player_get_streaming_qualities, the same arrangement as the EQ presets. The cap has to reach the *negotiation*, not just the transcode URL: max_static_bitrate in the device profile is what makes the server refuse to direct-play a file fatter than the cap, and without it a 30 Mbps remux is handed over untouched and every URL parameter downstream is moot. So it is applied at all four places that decide bandwidth — the HLS URL builder, PlaybackInfo, the Live TV stream, and the background-audio handoff (which takes the lower of the cap and its own 384 kbps). Video bitrate is the total minus the audio share so the two together honour the ceiling rather than overshooting it. The ceiling is process-wide rather than a repository field: it is a preference about this device's connection, must survive a repository rebuilt on re-login, and every URL builder plus the negotiation have to agree on it or the cap leaks. Same shape as INCLUDE_CATALOG_BROWSE. Two ways in. Settings holds the durable default, persisted to app_settings and restored at startup — unlike the rest of VideoSettings, because a limit set for a metered connection that silently reverts to uncapped on the next launch spends the user's data with no changed setting to see. The in-player menu is the "this film, this connection" override: a cap is a property of the stream the server is producing, so it cannot apply to one already in flight — player_set_stream_quality re-opens the stream at the new quality and resumes at the current position, reloading the native backend itself and handing HTML5 a URL for the same reloadSource primitive the audio-track switch uses. Tests pin the URL parameters at a capped and an uncapped step, the handoff taking the lower of the two, the ladder's internal consistency (video + audio == cap, resolution descending with bitrate) and the persisted token's round trip. The ceiling is process-wide, so the tests that depend on it serialise on a guard that restores the default. TRACES: UR-074 | DR-160 | UT-156, UT-157
This commit is contained in:
@@ -148,6 +148,138 @@ impl AudioSettings {
|
||||
}
|
||||
}
|
||||
|
||||
/// A ceiling on how much bandwidth a *video* stream may consume.
|
||||
///
|
||||
/// A quality step is a bundle of concrete transcode parameters — total stream
|
||||
/// ceiling, the audio share of it, and the resolution that ceiling can carry —
|
||||
/// not just a label. Those numbers are Jellyfin encoding domain vocabulary, so
|
||||
/// they live here and the frontend only ever names a variant; the labels the
|
||||
/// picker shows are served over IPC by `player_get_streaming_qualities`.
|
||||
///
|
||||
/// The ladder is deliberately expressed in bandwidth rather than resolution: it
|
||||
/// exists to fit a connection, and the resolution cap is chosen *from* the
|
||||
/// bitrate so the encoder does not spend a small budget on pixels it cannot
|
||||
/// afford. See docs/specs/streaming-bitrate-cap.md.
|
||||
///
|
||||
/// TRACES: UR-074 | DR-160
|
||||
#[derive(specta::Type, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum StreamingQuality {
|
||||
/// No client-imposed cap — the server may direct-play the source as-is.
|
||||
#[default]
|
||||
Original,
|
||||
Mbps20,
|
||||
Mbps10,
|
||||
Mbps8,
|
||||
Mbps4,
|
||||
Mbps2,
|
||||
Mbps1,
|
||||
Kbps720,
|
||||
}
|
||||
|
||||
impl StreamingQuality {
|
||||
/// The ladder, highest first, for enumerating across the IPC boundary.
|
||||
pub const ALL: [StreamingQuality; 8] = [
|
||||
StreamingQuality::Original,
|
||||
StreamingQuality::Mbps20,
|
||||
StreamingQuality::Mbps10,
|
||||
StreamingQuality::Mbps8,
|
||||
StreamingQuality::Mbps4,
|
||||
StreamingQuality::Mbps2,
|
||||
StreamingQuality::Mbps1,
|
||||
StreamingQuality::Kbps720,
|
||||
];
|
||||
|
||||
/// Total bits per second the stream may use (video + audio), or `None` for
|
||||
/// the uncapped `Original`.
|
||||
///
|
||||
/// This is the number that goes to `PlaybackInfo` as `MaxStreamingBitrate`
|
||||
/// and into the device profile. Sending it there — not just on the transcode
|
||||
/// URL — is what makes the cap real: a stream the server decides to *direct
|
||||
/// play* is served at the source file's own bitrate, and no URL parameter
|
||||
/// afterwards can reduce it.
|
||||
pub fn max_bitrate(&self) -> Option<u64> {
|
||||
match self {
|
||||
StreamingQuality::Original => None,
|
||||
StreamingQuality::Mbps20 => Some(20_000_000),
|
||||
StreamingQuality::Mbps10 => Some(10_000_000),
|
||||
StreamingQuality::Mbps8 => Some(8_000_000),
|
||||
StreamingQuality::Mbps4 => Some(4_000_000),
|
||||
StreamingQuality::Mbps2 => Some(2_000_000),
|
||||
StreamingQuality::Mbps1 => Some(1_000_000),
|
||||
StreamingQuality::Kbps720 => Some(720_000),
|
||||
}
|
||||
}
|
||||
|
||||
/// Bits per second allotted to the audio track.
|
||||
///
|
||||
/// The value shrinks with the ladder because at the bottom rungs a fixed
|
||||
/// 384 kbps would be a third of the entire budget.
|
||||
pub fn audio_bitrate(&self) -> u64 {
|
||||
match self {
|
||||
StreamingQuality::Original
|
||||
| StreamingQuality::Mbps20
|
||||
| StreamingQuality::Mbps10
|
||||
| StreamingQuality::Mbps8 => 384_000,
|
||||
StreamingQuality::Mbps4 => 256_000,
|
||||
StreamingQuality::Mbps2 => 192_000,
|
||||
StreamingQuality::Mbps1 => 128_000,
|
||||
StreamingQuality::Kbps720 => 96_000,
|
||||
}
|
||||
}
|
||||
|
||||
/// Bits per second allotted to the video track: the total minus the audio
|
||||
/// share, so the two together honour [`max_bitrate`](Self::max_bitrate)
|
||||
/// rather than overshooting it by the size of the audio track.
|
||||
pub fn video_bitrate(&self) -> Option<u64> {
|
||||
self.max_bitrate()
|
||||
.map(|total| total.saturating_sub(self.audio_bitrate()))
|
||||
}
|
||||
|
||||
/// Resolution ceiling that suits the bitrate, or `None` to leave the source
|
||||
/// resolution alone. Scaling down is what keeps a small budget looking like
|
||||
/// clean video instead of blocky 1080p.
|
||||
pub fn max_height(&self) -> Option<u32> {
|
||||
match self {
|
||||
// 20 Mbps carries 4K, so it caps bandwidth without capping pixels.
|
||||
StreamingQuality::Original | StreamingQuality::Mbps20 => None,
|
||||
StreamingQuality::Mbps10 | StreamingQuality::Mbps8 => Some(1080),
|
||||
StreamingQuality::Mbps4 | StreamingQuality::Mbps2 => Some(720),
|
||||
StreamingQuality::Mbps1 => Some(480),
|
||||
StreamingQuality::Kbps720 => Some(360),
|
||||
}
|
||||
}
|
||||
|
||||
/// Human label for the picker. Lives in Rust with the numbers it describes,
|
||||
/// so the two cannot drift apart.
|
||||
pub fn label(&self) -> &'static str {
|
||||
match self {
|
||||
StreamingQuality::Original => "Original",
|
||||
StreamingQuality::Mbps20 => "20 Mbps",
|
||||
StreamingQuality::Mbps10 => "10 Mbps",
|
||||
StreamingQuality::Mbps8 => "8 Mbps",
|
||||
StreamingQuality::Mbps4 => "4 Mbps",
|
||||
StreamingQuality::Mbps2 => "2 Mbps",
|
||||
StreamingQuality::Mbps1 => "1 Mbps",
|
||||
StreamingQuality::Kbps720 => "720 kbps",
|
||||
}
|
||||
}
|
||||
|
||||
/// Secondary line for the picker: what the cap means in practice.
|
||||
pub fn detail(&self) -> &'static str {
|
||||
match self {
|
||||
StreamingQuality::Original => "No limit — highest quality",
|
||||
StreamingQuality::Mbps20 => "Up to 4K",
|
||||
StreamingQuality::Mbps10 => "1080p, high quality",
|
||||
StreamingQuality::Mbps8 => "1080p",
|
||||
StreamingQuality::Mbps4 => "720p",
|
||||
StreamingQuality::Mbps2 => "720p, reduced",
|
||||
StreamingQuality::Mbps1 => "480p",
|
||||
StreamingQuality::Kbps720 => "360p — slowest connections",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Video playback settings
|
||||
#[derive(specta::Type, Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -159,6 +291,14 @@ pub struct VideoSettings {
|
||||
/// Maximum number of episodes to auto-play consecutively (0 = unlimited)
|
||||
#[serde(default)]
|
||||
pub auto_play_max_episodes: u32,
|
||||
/// Bandwidth ceiling applied to every video stream.
|
||||
///
|
||||
/// `#[serde(default)]` so settings JSON persisted before this field existed
|
||||
/// loads as the previous behaviour (uncapped).
|
||||
///
|
||||
/// TRACES: UR-074 | DR-160
|
||||
#[serde(default)]
|
||||
pub streaming_quality: StreamingQuality,
|
||||
}
|
||||
|
||||
impl Default for VideoSettings {
|
||||
@@ -167,6 +307,7 @@ impl Default for VideoSettings {
|
||||
auto_play_next_episode: true,
|
||||
auto_play_countdown_seconds: 10,
|
||||
auto_play_max_episodes: 0,
|
||||
streaming_quality: StreamingQuality::Original,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -427,12 +568,14 @@ mod tests {
|
||||
auto_play_next_episode: false,
|
||||
auto_play_countdown_seconds: 15,
|
||||
auto_play_max_episodes: 5,
|
||||
streaming_quality: StreamingQuality::Mbps4,
|
||||
};
|
||||
|
||||
let json = serde_json::to_string(&settings).unwrap();
|
||||
assert!(json.contains("\"autoPlayNextEpisode\":false"));
|
||||
assert!(json.contains("\"autoPlayCountdownSeconds\":15"));
|
||||
assert!(json.contains("\"autoPlayMaxEpisodes\":5"));
|
||||
assert!(json.contains("\"streamingQuality\":\"mbps4\""));
|
||||
|
||||
let parsed: VideoSettings = serde_json::from_str(&json).unwrap();
|
||||
assert!(!parsed.auto_play_next_episode);
|
||||
@@ -448,5 +591,90 @@ mod tests {
|
||||
assert!(parsed.auto_play_next_episode);
|
||||
assert_eq!(parsed.auto_play_countdown_seconds, 10);
|
||||
assert_eq!(parsed.auto_play_max_episodes, 0);
|
||||
// Settings persisted before the cap existed must load as uncapped —
|
||||
// inventing a limit for an upgrading user would silently degrade their
|
||||
// picture with no setting having been changed.
|
||||
assert_eq!(parsed.streaming_quality, StreamingQuality::Original);
|
||||
}
|
||||
|
||||
/// The whole point of a step is the number of bits it promises not to
|
||||
/// exceed, so video + audio must fit inside the total — a video bitrate set
|
||||
/// to the full cap would overshoot it by the size of the audio track.
|
||||
///
|
||||
/// TRACES: UR-074 | DR-160 | UT-157
|
||||
#[test]
|
||||
fn test_streaming_quality_budget_is_internally_consistent() {
|
||||
for quality in StreamingQuality::ALL {
|
||||
let Some(total) = quality.max_bitrate() else {
|
||||
assert_eq!(
|
||||
quality,
|
||||
StreamingQuality::Original,
|
||||
"only Original may be uncapped"
|
||||
);
|
||||
assert!(quality.video_bitrate().is_none());
|
||||
assert!(quality.max_height().is_none());
|
||||
continue;
|
||||
};
|
||||
|
||||
let video = quality.video_bitrate().expect("a capped step caps video");
|
||||
assert_eq!(
|
||||
video + quality.audio_bitrate(),
|
||||
total,
|
||||
"{:?}: video + audio must equal the cap",
|
||||
quality
|
||||
);
|
||||
assert!(
|
||||
video > 0,
|
||||
"{:?}: audio must not consume the budget",
|
||||
quality
|
||||
);
|
||||
assert!(!quality.label().is_empty());
|
||||
assert!(!quality.detail().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
/// The ladder is presented to the user as descending, and the resolution cap
|
||||
/// must fall with it — a lower bitrate paired with a higher resolution would
|
||||
/// spend the smaller budget on more pixels, which is backwards.
|
||||
///
|
||||
/// TRACES: UR-074 | DR-160 | UT-157
|
||||
#[test]
|
||||
fn test_streaming_quality_ladder_descends() {
|
||||
let steps = StreamingQuality::ALL;
|
||||
for pair in steps.windows(2) {
|
||||
let (higher, lower) = (pair[0], pair[1]);
|
||||
let higher_bitrate = higher.max_bitrate().unwrap_or(u64::MAX);
|
||||
let lower_bitrate = lower.max_bitrate().unwrap_or(u64::MAX);
|
||||
assert!(
|
||||
higher_bitrate > lower_bitrate,
|
||||
"{:?} must sit above {:?}",
|
||||
higher,
|
||||
lower
|
||||
);
|
||||
assert!(
|
||||
higher.max_height().unwrap_or(u32::MAX) >= lower.max_height().unwrap_or(u32::MAX),
|
||||
"{:?} must not cap resolution below {:?}",
|
||||
higher,
|
||||
lower
|
||||
);
|
||||
assert!(higher.audio_bitrate() >= lower.audio_bitrate());
|
||||
}
|
||||
}
|
||||
|
||||
/// The persisted form is the serde token, and it must survive a round trip —
|
||||
/// a rename here silently resets everyone's saved cap to uncapped.
|
||||
///
|
||||
/// TRACES: UR-074 | DR-160 | UT-157
|
||||
#[test]
|
||||
fn test_streaming_quality_round_trips_through_json() {
|
||||
for quality in StreamingQuality::ALL {
|
||||
let json = serde_json::to_string(&quality).expect("serialises");
|
||||
let parsed: StreamingQuality = serde_json::from_str(&json).expect("parses back");
|
||||
assert_eq!(parsed, quality);
|
||||
}
|
||||
assert_eq!(
|
||||
serde_json::to_string(&StreamingQuality::Mbps10).unwrap(),
|
||||
"\"mbps10\""
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user