Background-audio handoff for video + repository/player refactor

Hand video playback off to a native audio-only stream when the app is
backgrounded or locked, with no on-device video decode (UR-040). Adds
player_enter/exit_background_audio commands, an audio-only stream URL
for video items across the repository layer, and the frontend handoff
state machine wired into VideoPlayer. Includes accompanying
repository/offline/player refactoring and regenerates the traceability
matrix.
This commit is contained in:
2026-07-22 21:52:07 +02:00
parent 4e6ab017d4
commit 3fbf6afdbc
72 changed files with 6728 additions and 2338 deletions
+132 -54
View File
@@ -72,7 +72,8 @@ impl JellyfinClient {
log::debug!("[JellyfinClient] GET {}", endpoint);
let response = self.http_client
let response = self
.http_client
.get(&url)
.header("X-Emby-Authorization", self.get_auth_header())
.send()
@@ -83,13 +84,24 @@ impl JellyfinClient {
})?;
let status = response.status();
log::debug!("[JellyfinClient] Response status for {}: {}", endpoint, status);
log::debug!(
"[JellyfinClient] Response status for {}: {}",
endpoint,
status
);
if !status.is_success() {
let error_text = response.text().await.unwrap_or_else(|_| "Unknown error".to_string());
let error_text = response
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
log::error!("[JellyfinClient] Request failed: {} {}", status, endpoint);
log::error!("[JellyfinClient] Response: {}", error_text);
return Err(format!("Jellyfin API error {}: {}", status.as_u16(), error_text));
return Err(format!(
"Jellyfin API error {}: {}",
status.as_u16(),
error_text
));
}
// Get the response text first so we can log it
@@ -100,9 +112,15 @@ impl JellyfinClient {
// Log the raw response for sessions endpoint to help debug
if endpoint.contains("/Sessions") {
debug!("[JellyfinClient] Raw response for {}: {}", endpoint,
debug!(
"[JellyfinClient] Raw response for {}: {}",
endpoint,
if response_text.len() > 500 {
format!("{}... (truncated, {} bytes total)", &response_text[..500], response_text.len())
format!(
"{}... (truncated, {} bytes total)",
&response_text[..500],
response_text.len()
)
} else {
response_text.clone()
}
@@ -112,7 +130,8 @@ impl JellyfinClient {
// Parse the response text as JSON
let data = serde_json::from_str::<T>(&response_text).map_err(|e| {
log::error!("[JellyfinClient] Failed to parse response: {}", e);
log::error!("[JellyfinClient] Response was: {}",
log::error!(
"[JellyfinClient] Response was: {}",
if response_text.len() > 200 {
format!("{}...", &response_text[..200])
} else {
@@ -132,7 +151,8 @@ impl JellyfinClient {
log::debug!("[JellyfinClient] POST {} to {}", endpoint, url);
let response: reqwest::Response = self.http_client
let response: reqwest::Response = self
.http_client
.post(&url)
.header("Content-Type", "application/json")
.header("X-Emby-Authorization", self.get_auth_header())
@@ -145,13 +165,24 @@ impl JellyfinClient {
})?;
let status = response.status();
log::debug!("[JellyfinClient] Response status for {}: {}", endpoint, status);
log::debug!(
"[JellyfinClient] Response status for {}: {}",
endpoint,
status
);
if !status.is_success() {
let error_text: String = response.text().await.unwrap_or_else(|_| "Unknown error".to_string());
let error_text: String = response
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
log::error!("[JellyfinClient] Request failed: {} {}", status, endpoint);
log::error!("[JellyfinClient] Response: {}", error_text);
return Err(format!("Jellyfin API error {}: {}", status.as_u16(), error_text));
return Err(format!(
"Jellyfin API error {}: {}",
status.as_u16(),
error_text
));
}
log::debug!("[JellyfinClient] Request successful for {}", endpoint);
@@ -193,7 +224,7 @@ impl JellyfinClient {
}
/// Report playback progress to Jellyfin
#[allow(dead_code)] // Will be used when playback_reporting is integrated
#[allow(dead_code)] // Will be used when playback_reporting is integrated
pub async fn report_playback_progress(
&self,
item_id: String,
@@ -220,9 +251,17 @@ impl JellyfinClient {
start_position_ticks: Option<i64>,
) -> Result<(), String> {
log::info!("[JellyfinClient] Playing on session: {}", session_id);
log::info!("[JellyfinClient] Item IDs: {:?}, Start index: {}", item_ids, start_index);
debug!("[JellyfinClient] play_on_session called: session={}, {} items, start_index={}",
session_id, item_ids.len(), start_index);
log::info!(
"[JellyfinClient] Item IDs: {:?}, Start index: {}",
item_ids,
start_index
);
debug!(
"[JellyfinClient] play_on_session called: session={}, {} items, start_index={}",
session_id,
item_ids.len(),
start_index
);
// Build URL with query parameters (Jellyfin expects PascalCase query params)
let mut url = format!(
@@ -244,10 +283,15 @@ impl JellyfinClient {
log::info!("[JellyfinClient] POST {}", url);
debug!("[JellyfinClient] Full URL length: {} chars", url.len());
// Don't log full URL as it may contain sensitive tokens, just log the endpoint
debug!("[JellyfinClient] POST to Sessions/{}/Playing with {} itemIds", session_id, item_ids.len());
debug!(
"[JellyfinClient] POST to Sessions/{}/Playing with {} itemIds",
session_id,
item_ids.len()
);
debug!("[JellyfinClient] Sending HTTP POST request...");
let response = self.http_client
let response = self
.http_client
.post(&url)
.header("X-Emby-Authorization", self.get_auth_header())
.send()
@@ -263,10 +307,21 @@ impl JellyfinClient {
debug!("[JellyfinClient] Response status: {}", status);
if !status.is_success() {
let error_text = response.text().await.unwrap_or_else(|_| "Unknown error".to_string());
let error_text = response
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
log::error!("[JellyfinClient] Request failed: {}", error_text);
error!("[JellyfinClient] API error {}: {}", status.as_u16(), error_text);
return Err(format!("Jellyfin API error {}: {}", status.as_u16(), error_text));
error!(
"[JellyfinClient] API error {}: {}",
status.as_u16(),
error_text
);
return Err(format!(
"Jellyfin API error {}: {}",
status.as_u16(),
error_text
));
}
log::info!("[JellyfinClient] Successfully sent play command to remote session");
@@ -280,7 +335,11 @@ impl JellyfinClient {
session_id: String,
command: &str,
) -> Result<(), String> {
self.post(&format!("/Sessions/{}/Playing/{}", session_id, command), &serde_json::json!({})).await
self.post(
&format!("/Sessions/{}/Playing/{}", session_id, command),
&serde_json::json!({}),
)
.await
}
/// Seek on a remote session
@@ -298,7 +357,8 @@ impl JellyfinClient {
self.config.server_url, session_id, position_ticks
);
let response = self.http_client
let response = self
.http_client
.post(&url)
.header("X-Emby-Authorization", self.get_auth_header())
.send()
@@ -307,11 +367,22 @@ impl JellyfinClient {
let status = response.status();
if !status.is_success() {
let error_text = response.text().await.unwrap_or_else(|_| "Unknown error".to_string());
return Err(format!("Jellyfin API error {}: {}", status.as_u16(), error_text));
let error_text = response
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
return Err(format!(
"Jellyfin API error {}: {}",
status.as_u16(),
error_text
));
}
log::info!("[JellyfinClient] Seek to {} ticks on session {}", position_ticks, session_id);
log::info!(
"[JellyfinClient] Seek to {} ticks on session {}",
position_ticks,
session_id
);
Ok(())
}
@@ -332,46 +403,42 @@ impl JellyfinClient {
payload["Arguments"] = args;
}
log::info!("[JellyfinClient] Sending GeneralCommand '{}' to session {} with payload: {}",
command_name, session_id, serde_json::to_string(&payload).unwrap_or_default());
log::info!(
"[JellyfinClient] Sending GeneralCommand '{}' to session {} with payload: {}",
command_name,
session_id,
serde_json::to_string(&payload).unwrap_or_default()
);
self.post(
&format!("/Sessions/{}/Command", session_id),
&payload
).await
self.post(&format!("/Sessions/{}/Command", session_id), &payload)
.await
}
/// Set volume on a remote session
pub async fn session_set_volume(
&self,
session_id: String,
volume: i32,
) -> Result<(), String> {
pub async fn session_set_volume(&self, session_id: String, volume: i32) -> Result<(), String> {
self.send_general_command(
&session_id,
"SetVolume",
Some(serde_json::json!({ "Volume": volume.to_string() })),
).await
)
.await
}
/// Toggle mute on a remote session
pub async fn session_toggle_mute(
&self,
session_id: String,
) -> Result<(), String> {
pub async fn session_toggle_mute(&self, session_id: String) -> Result<(), String> {
log::info!("[JellyfinClient] Toggling mute on session {}", session_id);
self.send_general_command(
&session_id,
"ToggleMute",
None,
).await
self.send_general_command(&session_id, "ToggleMute", None)
.await
}
/// Get all active sessions
pub async fn get_sessions(&self) -> Result<Vec<SessionInfo>, String> {
let sessions: Vec<SessionInfo> = self.get("/Sessions").await?;
info!("[JellyfinClient] Fetched {} sessions from API", sessions.len());
info!(
"[JellyfinClient] Fetched {} sessions from API",
sessions.len()
);
for session in &sessions {
debug!("[JellyfinClient] Session: id={:?}, device={:?}, client={:?}, supportsRemoteControl={}",
session.id, session.device_name, session.client, session.supports_remote_control);
@@ -382,7 +449,9 @@ impl JellyfinClient {
/// Get a specific session by ID
pub async fn get_session(&self, session_id: &str) -> Result<Option<SessionInfo>, String> {
let sessions = self.get_sessions().await?;
Ok(sessions.into_iter().find(|s| s.id.as_deref() == Some(session_id)))
Ok(sessions
.into_iter()
.find(|s| s.id.as_deref() == Some(session_id)))
}
// --- JellyLMS multi-room sync groups -----------------------------------
@@ -415,12 +484,14 @@ impl JellyfinClient {
/// Remove a single LMS player from whatever sync group it's in.
pub async fn lms_unsync_player(&self, mac: &str) -> Result<(), String> {
self.delete(&format!("/JellyLms/SyncGroups/Players/{}", mac)).await
self.delete(&format!("/JellyLms/SyncGroups/Players/{}", mac))
.await
}
/// Dissolve an entire LMS sync group, identified by its master's MAC.
pub async fn lms_dissolve_sync_group(&self, master_mac: &str) -> Result<(), String> {
self.delete(&format!("/JellyLms/SyncGroups/{}", master_mac)).await
self.delete(&format!("/JellyLms/SyncGroups/{}", master_mac))
.await
}
/// Make a DELETE request to the Jellyfin API (used by the JellyLMS endpoints).
@@ -429,7 +500,8 @@ impl JellyfinClient {
log::debug!("[JellyfinClient] DELETE {}", endpoint);
let response = self.http_client
let response = self
.http_client
.delete(&url)
.header("X-Emby-Authorization", self.get_auth_header())
.send()
@@ -438,8 +510,15 @@ impl JellyfinClient {
let status = response.status();
if !status.is_success() {
let error_text = response.text().await.unwrap_or_else(|_| "Unknown error".to_string());
return Err(format!("Jellyfin API error {}: {}", status.as_u16(), error_text));
let error_text = response
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
return Err(format!(
"Jellyfin API error {}: {}",
status.as_u16(),
error_text
));
}
Ok(())
}
@@ -570,7 +649,6 @@ pub struct PlayState {
pub shuffle_mode: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
+12 -40
View File
@@ -40,7 +40,7 @@ pub enum ErrorKind {
/// Enhanced HTTP client with retry logic and error classification
#[derive(Clone)]
pub struct HttpClient {
pub(crate) client: Client, // Make accessible within crate for custom requests
pub(crate) client: Client, // Make accessible within crate for custom requests
config: HttpConfig,
}
@@ -131,18 +131,15 @@ impl HttpClient {
/// Check if a request should be retried based on the error
pub fn should_retry(error: &reqwest::Error) -> bool {
match Self::classify_error(error) {
ErrorKind::Network => true, // Retry network errors
ErrorKind::Server => true, // Retry 5xx server errors
ErrorKind::Network => true, // Retry network errors
ErrorKind::Server => true, // Retry 5xx server errors
ErrorKind::Authentication => false, // Don't retry 401/403
ErrorKind::Client => false, // Don't retry other 4xx errors
ErrorKind::Client => false, // Don't retry other 4xx errors
}
}
/// Make a request with automatic retry on network errors
pub async fn request_with_retry(
&self,
request: Request,
) -> Result<Response, reqwest::Error> {
pub async fn request_with_retry(&self, request: Request) -> Result<Response, reqwest::Error> {
let max_retries = self.config.max_retries;
let mut last_error: Option<reqwest::Error> = None;
@@ -192,31 +189,6 @@ impl HttpClient {
Err(last_error.unwrap())
}
/// Make a GET request with retry
pub async fn get_with_retry(&self, url: &str) -> Result<Response, reqwest::Error> {
let request = self.client.get(url).build()?;
self.request_with_retry(request).await
}
/// Make a GET request and deserialize JSON response with retry
pub async fn get_json_with_retry<T: DeserializeOwned>(
&self,
url: &str,
) -> Result<T, String> {
let response = self.get_with_retry(url).await
.map_err(|e| format!("Request failed: {}", e))?;
if !response.status().is_success() {
let status = response.status();
let error_text = response.text().await
.unwrap_or_else(|_| "Unknown error".to_string());
return Err(format!("HTTP {}: {}", status, error_text));
}
response.json::<T>().await
.map_err(|e| format!("Failed to parse JSON: {}", e))
}
/// Make a GET request and deserialize JSON with a short timeout and no retries.
///
/// Intended for the initial "connect to server" probe on the login screen:
@@ -258,17 +230,17 @@ impl HttpClient {
/// Quick ping to check if a server is reachable (no retry)
pub async fn ping(&self, url: &str) -> bool {
let request = self.client.get(url)
let request = self
.client
.get(url)
.timeout(Duration::from_secs(5)) // Shorter timeout for ping
.build();
match request {
Ok(req) => {
match self.client.execute(req).await {
Ok(response) => response.status().is_success(),
Err(_) => false,
}
}
Ok(req) => match self.client.execute(req).await {
Ok(response) => response.status().is_success(),
Err(_) => false,
},
Err(_) => false,
}
}
+1 -1
View File
@@ -33,7 +33,7 @@ pub struct PlaybackStoppedRequest {
/// Request body for reporting playback progress
#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
#[allow(dead_code)] // Will be used when playback_reporting is integrated
#[allow(dead_code)] // Will be used when playback_reporting is integrated
pub struct PlaybackProgressRequest {
pub item_id: String,
pub position_ticks: i64,