Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2c12615c5 | ||
|
|
0b5a3aa176 | ||
|
|
37455bc470 | ||
|
|
a64e1b1fb4 | ||
|
|
1f6977cd01 | ||
|
|
6af7f7dcca |
@@ -90,6 +90,50 @@ flowchart LR
|
||||
|
||||
**Important**: The command is `player_get_queue` (returns `QueueStatus` with `hasNext`/`hasPrevious`). There is no `player_get_queue_status` command.
|
||||
|
||||
## HTML5 Video Adapter (webview-rendered video)
|
||||
|
||||
**Location**: `src/lib/player/html5Adapter.ts`, `src/lib/player/index.ts`, report commands in
|
||||
`src-tauri/src/commands/player/timers.rs`
|
||||
|
||||
Video on desktop (Linux WebKitGTK) — and, per current interim behavior, Android — is rendered by an
|
||||
HTML5 `<video>`/HLS element **inside the webview**. libmpv is initialized audio-only (`vo=null`,
|
||||
`video=false`), so the native backend cannot render or observe this element. The `<video>` is therefore
|
||||
the real player, living outside Rust's reach.
|
||||
|
||||
To keep the `PlayerController` the single source of truth (matching the audio path), the HTML5 element
|
||||
is treated as **a dumb output device that reports back into Rust**, rather than an independent state
|
||||
authority:
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph Webview["Webview"]
|
||||
Video["HTML5 <video> / HLS.js"]
|
||||
Adapter["html5Adapter.ts<br/>(reports DOM events)"]
|
||||
end
|
||||
subgraph Backend["Rust"]
|
||||
Cmds["player_report_state<br/>player_report_position<br/>player_report_media_loaded"]
|
||||
Controller["PlayerController"]
|
||||
Emitter["TauriEventEmitter"]
|
||||
end
|
||||
subgraph Frontend["Frontend"]
|
||||
Events["playerEvents.ts"]
|
||||
Store["player store"]
|
||||
end
|
||||
|
||||
Video -->|DOM events| Adapter --> Cmds --> Controller --> Emitter --> Events --> Store
|
||||
```
|
||||
|
||||
**Key points:**
|
||||
- The adapter re-emits the *same* `PlayerStatusEvent`s (`StateChanged`, `PositionUpdate`, `MediaLoaded`)
|
||||
the native backends emit, so `playerEvents.ts` needs **no** HTML5-specific branch — HTML5 is just
|
||||
another event source feeding the existing pipeline.
|
||||
- Position reports are throttled (~250ms) to match the MPV cadence and avoid flooding IPC from the
|
||||
60fps RAF loop.
|
||||
- **Boundary rule**: UI components never touch the report commands or `videoElement` state directly.
|
||||
Playback *control* goes through the unified facade `src/lib/player/index.ts` (`playerController`);
|
||||
HTML5 *state reporting* goes through `html5Adapter.ts`. This restores the documented invariant
|
||||
("frontend only displays state and invokes commands") for the video path.
|
||||
|
||||
## MpvBackend (Linux)
|
||||
|
||||
**Location**: `src-tauri/src/player/mpv/`
|
||||
|
||||
@@ -13,6 +13,7 @@ JellyTau uses a client-server architecture: business logic lives in a comprehens
|
||||
- **Business Logic in Rust**: Core logic — playback, repository, sync, downloads, connectivity — lives in Rust for performance, reliability, and type safety.
|
||||
- **Presentation in Svelte**: The frontend (~20.5k non-test lines) owns UI, layout, navigation, and interaction state and invokes Rust commands. It is intentionally UI-heavy, **not** a thin wrapper. Largest pieces: components + routes (~14.6k lines), stores (~3.4k), api/services/utils (~2.4k); `VideoPlayer.svelte` alone is ~1.6k lines.
|
||||
- **Events + Polling hybrid**: Rust emits events the frontend listens to, and the UI also polls status on short intervals in a few hot spots (e.g. queue status in `library/+layout.svelte`, playback progress in `VideoPlayer.svelte`).
|
||||
- **Unified player boundary**: UI components control playback only through the frontend facade `src/lib/player/index.ts` (`playerController`), never by calling `commands.player*` directly. Webview-rendered HTML5 video reports its state back into Rust via `src/lib/player/html5Adapter.ts` and the `player_report_*` commands, so the `PlayerController` stays the single source of truth in both native (MPV/ExoPlayer) and HTML5 modes (see [05-platform-backends.md](05-platform-backends.md)).
|
||||
- **Handle-Based Resources**: UUID handles for stateful Rust objects.
|
||||
- **Cache-First**: Parallel queries with intelligent fallback.
|
||||
- **Single source of truth for reachability**: Server reachability is derived from the outcome of *real repository traffic*, not a side-channel poller. The `OnlineRepository` reports each server result to the `ConnectivityMonitor` (classified via `RepoError`), which applies a time-window debounce before declaring the server offline and recovers instantly on the first success. The standalone `/System/Info/Public` probe runs *only while offline*, as a recovery detector for idle sessions.
|
||||
@@ -166,6 +167,9 @@ src/lib/
|
||||
│ ├── repository-client.ts # RepositoryClient wrapper (~100 lines)
|
||||
│ ├── client.ts # JellyfinClient (helper for streaming)
|
||||
│ └── sessions.ts # SessionsApi (remote session control)
|
||||
├── player/ # Unified player boundary (frontend)
|
||||
│ ├── index.ts # playerController facade — the only write-side entry point for playback
|
||||
│ └── html5Adapter.ts # Reports webview <video> DOM events back into Rust (player_report_*)
|
||||
├── services/
|
||||
│ ├── playerEvents.ts # Tauri event listener for player events
|
||||
│ └── playbackReporting.ts # Thin wrapper (~50 lines)
|
||||
|
||||
@@ -3,15 +3,19 @@
|
||||
|
||||
set -e
|
||||
|
||||
BUILD_TYPE="${1:-debug}"
|
||||
|
||||
echo "🚀 Build and Deploy Android APK"
|
||||
echo ""
|
||||
|
||||
# Build APK
|
||||
./scripts/build-android.sh "$BUILD_TYPE"
|
||||
# Pass all args (build type and/or --clean) through to the build script.
|
||||
./scripts/build-android.sh "$@"
|
||||
|
||||
echo ""
|
||||
|
||||
# Deploy APK
|
||||
# Deploy APK — extract build type (default debug), ignoring flags like --clean.
|
||||
BUILD_TYPE="debug"
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
debug|release) BUILD_TYPE="$arg" ;;
|
||||
esac
|
||||
done
|
||||
./scripts/deploy-android.sh "$BUILD_TYPE"
|
||||
|
||||
@@ -15,13 +15,24 @@ echo "Android SDK: $ANDROID_HOME"
|
||||
echo "NDK: $NDK_HOME"
|
||||
echo ""
|
||||
|
||||
# Build type: debug or release (default: debug)
|
||||
BUILD_TYPE="${1:-debug}"
|
||||
# Parse args: build type (debug/release) and optional --clean flag.
|
||||
# By default the build is INCREMENTAL — Cargo and Vite reuse their caches.
|
||||
# Pass --clean (or CLEAN=1) to wipe all caches for a from-scratch build.
|
||||
BUILD_TYPE="debug"
|
||||
CLEAN="${CLEAN:-0}"
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--clean) CLEAN=1 ;;
|
||||
debug|release) BUILD_TYPE="$arg" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Step 0: Clear build caches to ensure fresh builds
|
||||
echo "🧹 Clearing build caches..."
|
||||
rm -rf node_modules/.vite dist .svelte-kit .next build target src-tauri/target 2>/dev/null || true
|
||||
npm install > /dev/null 2>&1
|
||||
# Step 0: Optionally clear build caches for a fully fresh build.
|
||||
if [ "$CLEAN" = "1" ]; then
|
||||
echo "🧹 Clearing build caches (clean build)..."
|
||||
rm -rf node_modules/.vite dist .svelte-kit .next build target src-tauri/target 2>/dev/null || true
|
||||
npm install > /dev/null 2>&1
|
||||
fi
|
||||
|
||||
# Step 1: Sync Android source files
|
||||
echo "🔄 Syncing Android sources..."
|
||||
|
||||
@@ -41,4 +41,30 @@ if [ -f "$APP_GRADLE_SRC" ]; then
|
||||
echo " Copied: app/build.gradle.kts"
|
||||
fi
|
||||
|
||||
# Custom ProGuard/R8 keep rules. Required for minified release builds:
|
||||
# the player/ and security/ Kotlin classes are loaded by name via JNI from
|
||||
# Rust, so R8 can't see the references and would strip them without this.
|
||||
# build.gradle.kts globs **/*.pro, so dropping it in app/ is enough.
|
||||
PROGUARD_SRC="$PROJECT_ROOT/src-tauri/android/app/proguard-jellytau.pro"
|
||||
PROGUARD_DST="$PROJECT_ROOT/src-tauri/gen/android/app/proguard-jellytau.pro"
|
||||
if [ -f "$PROGUARD_SRC" ]; then
|
||||
cp "$PROGUARD_SRC" "$PROGUARD_DST"
|
||||
echo " Copied: app/proguard-jellytau.pro"
|
||||
fi
|
||||
|
||||
# Launcher icons / adaptive-icon mipmaps. `tauri android init` generates
|
||||
# low-quality launcher icons from tauri.conf.json (which has no high-res
|
||||
# Android source), so overwrite them with the real committed mipmaps.
|
||||
RES_SRC="$PROJECT_ROOT/src-tauri/android/src/main/res"
|
||||
RES_DST="$PROJECT_ROOT/src-tauri/gen/android/app/src/main/res"
|
||||
if [ -d "$RES_SRC" ]; then
|
||||
for dir in "$RES_SRC"/mipmap-*; do
|
||||
[ -d "$dir" ] || continue
|
||||
name="$(basename "$dir")"
|
||||
mkdir -p "$RES_DST/$name"
|
||||
cp "$dir"/* "$RES_DST/$name/"
|
||||
echo " Copied res: $name"
|
||||
done
|
||||
fi
|
||||
|
||||
echo "✓ Android sources synced successfully"
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
# JellyTau custom keep rules.
|
||||
#
|
||||
# These classes are loaded by name from the Rust backend via JNI
|
||||
# (env.find_class / class-loader lookups), so R8 cannot see the
|
||||
# references and would otherwise strip or rename them in a minified
|
||||
# release build — causing an instant ClassNotFoundException crash on
|
||||
# startup. See src-tauri/src/player/android/mod.rs and
|
||||
# src-tauri/src/credentials.rs.
|
||||
-keep class com.dtourolle.jellytau.player.** { *; }
|
||||
-keep class com.dtourolle.jellytau.security.** { *; }
|
||||
|
||||
# Media3 / ExoPlayer is accessed reflectively in places; keep it intact.
|
||||
-keep class androidx.media3.** { *; }
|
||||
-dontwarn androidx.media3.**
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@mipmap/ic_launcher_background"/>
|
||||
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
|
||||
<monochrome android:drawable="@mipmap/ic_launcher_monochrome"/>
|
||||
</adaptive-icon>
|
||||
|
After Width: | Height: | Size: 6.2 KiB |
|
After Width: | Height: | Size: 870 B |
|
After Width: | Height: | Size: 8.7 KiB |
|
After Width: | Height: | Size: 8.7 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 476 B |
|
After Width: | Height: | Size: 4.1 KiB |
|
After Width: | Height: | Size: 4.1 KiB |
|
After Width: | Height: | Size: 9.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 19 KiB |
|
After Width: | Height: | Size: 2.9 KiB |
|
After Width: | Height: | Size: 32 KiB |
|
After Width: | Height: | Size: 32 KiB |
|
After Width: | Height: | Size: 29 KiB |
|
After Width: | Height: | Size: 4.1 KiB |
|
After Width: | Height: | Size: 60 KiB |
|
After Width: | Height: | Size: 60 KiB |
@@ -247,3 +247,51 @@ pub async fn player_on_playback_ended(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ===== HTML5 video state-report commands =====
|
||||
//
|
||||
// On platforms where video renders in the webview (Linux WebKitGTK HTML5
|
||||
// <video>), the real player lives outside the native backend, so the frontend
|
||||
// HTML5 adapter reports DOM events back through these commands. The controller
|
||||
// re-emits them through the same PlayerStatusEvent pipeline the native backends
|
||||
// use, keeping the Rust controller the single source of truth and the frontend
|
||||
// player store fed from one place (playerEvents.ts) in both modes.
|
||||
|
||||
/// Report an HTML5 <video> state change (playing/paused/loading/stopped/idle).
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn player_report_state(
|
||||
player: State<'_, PlayerStateWrapper>,
|
||||
state: String,
|
||||
media_id: Option<String>,
|
||||
) -> Result<(), String> {
|
||||
let controller = player.0.lock().await;
|
||||
controller.report_html5_state(state, media_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Report an HTML5 <video> position tick (seconds). The adapter should throttle
|
||||
/// these to roughly match the native backends' ~250ms cadence.
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn player_report_position(
|
||||
player: State<'_, PlayerStateWrapper>,
|
||||
position: f64,
|
||||
duration: f64,
|
||||
) -> Result<(), String> {
|
||||
let controller = player.0.lock().await;
|
||||
controller.report_html5_position(position, duration);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Report that the HTML5 <video> finished loading and knows its duration.
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn player_report_media_loaded(
|
||||
player: State<'_, PlayerStateWrapper>,
|
||||
duration: f64,
|
||||
) -> Result<(), String> {
|
||||
let controller = player.0.lock().await;
|
||||
controller.report_html5_media_loaded(duration);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ use commands::{
|
||||
player_set_sleep_timer, player_cancel_sleep_timer, player_get_sleep_timer,
|
||||
player_get_autoplay_settings, player_set_autoplay_settings,
|
||||
player_cancel_autoplay_countdown, player_play_next_episode, player_on_playback_ended,
|
||||
// HTML5 video state-report commands
|
||||
player_report_state, player_report_position, player_report_media_loaded,
|
||||
// Queue manipulation commands
|
||||
player_add_to_queue, player_add_track_by_id, player_add_tracks_by_ids,
|
||||
player_remove_from_queue, player_move_in_queue, player_skip_to,
|
||||
@@ -476,6 +478,9 @@ fn specta_builder() -> Builder<tauri::Wry> {
|
||||
player_cancel_autoplay_countdown,
|
||||
player_play_next_episode,
|
||||
player_on_playback_ended,
|
||||
player_report_state,
|
||||
player_report_position,
|
||||
player_report_media_loaded,
|
||||
// Preload commands
|
||||
player_preload_upcoming,
|
||||
player_set_cache_config,
|
||||
|
||||
@@ -130,6 +130,17 @@ pub enum PlayerStatusEvent {
|
||||
/// frontend owns the two-step remote->local transfer (it must reload the
|
||||
/// media item locally), so the native side only signals intent here.
|
||||
RemoteDisconnectRequested,
|
||||
/// Backend-originated control command targeting the active frontend player
|
||||
/// adapter (the HTML5 <video> that lives in the webview, which Rust cannot
|
||||
/// drive directly). Emitted by control paths like the sleep timer, lockscreen,
|
||||
/// or remote so they can pause/play/seek/stop the webview element.
|
||||
/// `playerEvents.ts` routes this to the active PlayerAdapter via the facade.
|
||||
ControlCommand {
|
||||
/// One of: "play", "pause", "stop", "seek".
|
||||
action: String,
|
||||
/// Target position in seconds (only meaningful for "seek").
|
||||
position: Option<f64>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Trait for emitting player events to the frontend.
|
||||
|
||||
@@ -777,6 +777,44 @@ impl PlayerController {
|
||||
}
|
||||
}
|
||||
|
||||
// ===== HTML5 video report methods =====
|
||||
//
|
||||
// On platforms where video is rendered in the webview (Linux WebKitGTK
|
||||
// HTML5 <video>), the real player lives outside the native backend, so it
|
||||
// cannot emit PlayerStatusEvents itself. The frontend HTML5 adapter reports
|
||||
// DOM events here, and these methods re-emit them through the SAME event
|
||||
// pipeline the native backends use. This keeps the frontend's player store
|
||||
// fed from one place (playerEvents.ts) in both native and HTML5 modes, so
|
||||
// the Rust controller stays the single source of truth for player state.
|
||||
|
||||
/// Report an HTML5 <video> state change (playing/paused/loading/stopped).
|
||||
///
|
||||
/// Re-emits a `StateChanged` event identical to what MpvBackend/ExoPlayer
|
||||
/// would emit, so `playerEvents.ts` needs no HTML5-specific branch.
|
||||
pub fn report_html5_state(&self, state: String, media_id: Option<String>) {
|
||||
if let Some(emitter) = self.event_emitter.lock_safe().as_ref() {
|
||||
emitter.emit(PlayerStatusEvent::StateChanged { state, media_id });
|
||||
}
|
||||
}
|
||||
|
||||
/// Report an HTML5 <video> position tick.
|
||||
///
|
||||
/// Re-emits a `PositionUpdate` event mirroring the native backends' periodic
|
||||
/// position updates (the adapter is expected to throttle to ~250ms like MPV).
|
||||
pub fn report_html5_position(&self, position: f64, duration: f64) {
|
||||
if let Some(emitter) = self.event_emitter.lock_safe().as_ref() {
|
||||
emitter.emit(PlayerStatusEvent::PositionUpdate { position, duration });
|
||||
}
|
||||
}
|
||||
|
||||
/// Report that the HTML5 <video> element finished loading and knows its
|
||||
/// duration. Mirrors the native `MediaLoaded` event.
|
||||
pub fn report_html5_media_loaded(&self, duration: f64) {
|
||||
if let Some(emitter) = self.event_emitter.lock_safe().as_ref() {
|
||||
emitter.emit(PlayerStatusEvent::MediaLoaded { duration });
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Autoplay Methods =====
|
||||
|
||||
/// Get autoplay settings
|
||||
@@ -1139,6 +1177,83 @@ impl Default for PlayerController {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Test emitter that captures events for asserting the HTML5 report methods
|
||||
/// re-emit through the normal PlayerStatusEvent pipeline.
|
||||
struct CapturingEmitter {
|
||||
events: std::sync::Mutex<Vec<PlayerStatusEvent>>,
|
||||
}
|
||||
|
||||
impl CapturingEmitter {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
events: std::sync::Mutex::new(Vec::new()),
|
||||
}
|
||||
}
|
||||
fn events(&self) -> Vec<PlayerStatusEvent> {
|
||||
self.events.lock_safe().clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl PlayerEventEmitter for CapturingEmitter {
|
||||
fn emit(&self, event: PlayerStatusEvent) {
|
||||
self.events.lock_safe().push(event);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_report_html5_state_emits_state_changed() {
|
||||
let controller = PlayerController::default();
|
||||
let emitter = Arc::new(CapturingEmitter::new());
|
||||
controller.set_event_emitter(emitter.clone());
|
||||
|
||||
controller.report_html5_state("playing".to_string(), Some("item-1".to_string()));
|
||||
|
||||
let events = emitter.events();
|
||||
assert_eq!(events.len(), 1);
|
||||
match &events[0] {
|
||||
PlayerStatusEvent::StateChanged { state, media_id } => {
|
||||
assert_eq!(state, "playing");
|
||||
assert_eq!(media_id.as_deref(), Some("item-1"));
|
||||
}
|
||||
other => panic!("expected StateChanged, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_report_html5_position_emits_position_update() {
|
||||
let controller = PlayerController::default();
|
||||
let emitter = Arc::new(CapturingEmitter::new());
|
||||
controller.set_event_emitter(emitter.clone());
|
||||
|
||||
controller.report_html5_position(12.5, 300.0);
|
||||
|
||||
let events = emitter.events();
|
||||
assert_eq!(events.len(), 1);
|
||||
match &events[0] {
|
||||
PlayerStatusEvent::PositionUpdate { position, duration } => {
|
||||
assert_eq!(*position, 12.5);
|
||||
assert_eq!(*duration, 300.0);
|
||||
}
|
||||
other => panic!("expected PositionUpdate, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_report_html5_media_loaded_emits_media_loaded() {
|
||||
let controller = PlayerController::default();
|
||||
let emitter = Arc::new(CapturingEmitter::new());
|
||||
controller.set_event_emitter(emitter.clone());
|
||||
|
||||
controller.report_html5_media_loaded(420.0);
|
||||
|
||||
let events = emitter.events();
|
||||
assert_eq!(events.len(), 1);
|
||||
match &events[0] {
|
||||
PlayerStatusEvent::MediaLoaded { duration } => assert_eq!(*duration, 420.0),
|
||||
other => panic!("expected MediaLoaded, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_controller_volume_default() {
|
||||
let controller = PlayerController::default();
|
||||
|
||||
@@ -197,6 +197,25 @@ async playerPlayNextEpisode(item: PlayItemRequest) : Promise<PlayerStatus> {
|
||||
async playerOnPlaybackEnded(itemId: string | null, repositoryHandle: string | null) : Promise<null> {
|
||||
return await TAURI_INVOKE("player_on_playback_ended", { itemId, repositoryHandle });
|
||||
},
|
||||
/**
|
||||
* Report an HTML5 <video> state change (playing/paused/loading/stopped/idle).
|
||||
*/
|
||||
async playerReportState(state: string, mediaId: string | null) : Promise<null> {
|
||||
return await TAURI_INVOKE("player_report_state", { state, mediaId });
|
||||
},
|
||||
/**
|
||||
* Report an HTML5 <video> position tick (seconds). The adapter should throttle
|
||||
* these to roughly match the native backends' ~250ms cadence.
|
||||
*/
|
||||
async playerReportPosition(position: number, duration: number) : Promise<null> {
|
||||
return await TAURI_INVOKE("player_report_position", { position, duration });
|
||||
},
|
||||
/**
|
||||
* Report that the HTML5 <video> finished loading and knows its duration.
|
||||
*/
|
||||
async playerReportMediaLoaded(duration: number) : Promise<null> {
|
||||
return await TAURI_INVOKE("player_report_media_loaded", { duration });
|
||||
},
|
||||
/**
|
||||
* Preload upcoming tracks from the queue
|
||||
* This queues background downloads for the next N tracks that aren't already downloaded
|
||||
@@ -1977,7 +1996,15 @@ export type PlayerStatusEvent =
|
||||
* frontend owns the two-step remote->local transfer (it must reload the
|
||||
* media item locally), so the native side only signals intent here.
|
||||
*/
|
||||
{ type: "remote_disconnect_requested" }
|
||||
{ type: "remote_disconnect_requested" } |
|
||||
/**
|
||||
* Backend-originated control command targeting the active frontend player
|
||||
* adapter (the HTML5 <video> that lives in the webview, which Rust cannot
|
||||
* drive directly). Emitted by control paths like the sleep timer, lockscreen,
|
||||
* or remote so they can pause/play/seek/stop the webview element.
|
||||
* `playerEvents.ts` routes this to the active PlayerAdapter via the facade.
|
||||
*/
|
||||
{ type: "control_command"; action: string; position: number | null }
|
||||
/**
|
||||
* Result of creating a playlist
|
||||
*
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte";
|
||||
import { goto } from "$app/navigation";
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import { playerController } from "$lib/player";
|
||||
import type { MediaItem, PlaylistEntry } from "$lib/api/types";
|
||||
import { auth } from "$lib/stores/auth";
|
||||
import { toast } from "$lib/stores/toast";
|
||||
@@ -48,10 +48,8 @@
|
||||
async function handlePlayAll() {
|
||||
if (entries.length === 0) return;
|
||||
try {
|
||||
const repo = auth.getRepository();
|
||||
const repositoryHandle = repo.getHandle();
|
||||
const trackIds = entries.map(e => e.id);
|
||||
await commands.playerPlayTracks(repositoryHandle, {
|
||||
await playerController.playTracks({
|
||||
trackIds,
|
||||
startIndex: 0,
|
||||
shuffle: false,
|
||||
@@ -70,10 +68,8 @@
|
||||
async function handleShufflePlay() {
|
||||
if (entries.length === 0) return;
|
||||
try {
|
||||
const repo = auth.getRepository();
|
||||
const repositoryHandle = repo.getHandle();
|
||||
const trackIds = entries.map(e => e.id);
|
||||
await commands.playerPlayTracks(repositoryHandle, {
|
||||
await playerController.playTracks({
|
||||
trackIds,
|
||||
startIndex: 0,
|
||||
shuffle: true,
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
<script lang="ts">
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import { playerController } from "$lib/player";
|
||||
import { truncateMiddle } from "$lib/utils/truncateMiddle";
|
||||
import type { PlayTracksContext } from "$lib/api/bindings";
|
||||
import { goto } from "$app/navigation";
|
||||
import { queue } from "$lib/stores/queue";
|
||||
import { auth } from "$lib/stores/auth";
|
||||
import { currentMedia } from "$lib/stores/player";
|
||||
import { toast } from "$lib/stores/toast";
|
||||
import type { MediaItem } from "$lib/api/types";
|
||||
@@ -54,17 +53,10 @@
|
||||
try {
|
||||
isPlayingTrack = track.id;
|
||||
|
||||
// Validate auth before proceeding
|
||||
const repo = auth.getRepository();
|
||||
if (!repo) {
|
||||
throw new Error("Not authenticated");
|
||||
}
|
||||
|
||||
// If this is an album, use the backend album command (more efficient)
|
||||
if (context && context.type === "album") {
|
||||
const repositoryHandle = repo.getHandle();
|
||||
console.log(`[TrackList] Playing track: "${track.name}" (ID: ${track.id}, index in list: ${index})`);
|
||||
await commands.playerPlayAlbumTrack(repositoryHandle, {
|
||||
await playerController.playAlbumTrack({
|
||||
albumId: context.albumId,
|
||||
albumName: context.albumName,
|
||||
trackId: track.id,
|
||||
@@ -75,7 +67,6 @@
|
||||
|
||||
// Use new backend command for non-album contexts (playlists, custom queues, etc.)
|
||||
// Backend handles all metadata fetching and queue building
|
||||
const repositoryHandle = repo.getHandle();
|
||||
const trackIds = tracks.map((t) => t.id);
|
||||
|
||||
// Determine context for queue
|
||||
@@ -90,7 +81,7 @@
|
||||
playContext = { type: "custom", label: null };
|
||||
}
|
||||
|
||||
await commands.playerPlayTracks(repositoryHandle, {
|
||||
await playerController.playTracks({
|
||||
trackIds,
|
||||
startIndex: index,
|
||||
shuffle: false,
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
<!-- TRACES: UR-004, UR-005, UR-028 | DR-009 -->
|
||||
<script lang="ts">
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import { playerController } from "$lib/player";
|
||||
import { truncateMiddle } from "$lib/utils/truncateMiddle";
|
||||
import { goto } from "$app/navigation";
|
||||
import type { MediaItem } from "$lib/api/types";
|
||||
import { auth } from "$lib/stores/auth";
|
||||
import { sleepTimerActive } from "$lib/stores/sleepTimer";
|
||||
import { queue, queueItems, currentQueueIndex } from "$lib/stores/queue";
|
||||
import {
|
||||
@@ -75,28 +74,28 @@
|
||||
async function handleSeekEnd() {
|
||||
seeking = false;
|
||||
seekPending = true; // Keep showing target position until backend catches up
|
||||
await commands.playerSeek(seekValue);
|
||||
await playerController.seek(seekValue);
|
||||
}
|
||||
|
||||
// Control handlers for Controls component
|
||||
async function handlePlayPause() {
|
||||
await commands.playerToggle();
|
||||
await playerController.toggle();
|
||||
}
|
||||
|
||||
async function handlePrevious() {
|
||||
await commands.playerPrevious();
|
||||
await playerController.previous();
|
||||
}
|
||||
|
||||
async function handleNext() {
|
||||
await commands.playerNext();
|
||||
await playerController.next();
|
||||
}
|
||||
|
||||
async function handleToggleShuffle() {
|
||||
await commands.playerToggleShuffle();
|
||||
await playerController.toggleShuffle();
|
||||
}
|
||||
|
||||
async function handleCycleRepeat() {
|
||||
await commands.playerCycleRepeat();
|
||||
await playerController.cycleRepeat();
|
||||
}
|
||||
|
||||
// Prefer album ID for artwork (all tracks in an album share the same cover)
|
||||
@@ -129,7 +128,7 @@
|
||||
async function handleQueueItemClick(index: number) {
|
||||
try {
|
||||
queue.skipTo(index);
|
||||
await commands.playerSkipTo(index);
|
||||
await playerController.skipTo(index);
|
||||
} catch (e) {
|
||||
console.error("Failed to skip to queue item:", e);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* @req: UR-010 - Control playback of Jellyfin remote sessions
|
||||
*/
|
||||
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import { playerController } from "$lib/player";
|
||||
import { truncateMiddle } from "$lib/utils/truncateMiddle";
|
||||
import { goto } from "$app/navigation";
|
||||
import type { MediaItem } from "$lib/api/types";
|
||||
@@ -106,23 +106,23 @@
|
||||
|
||||
// Control handlers for Controls component
|
||||
async function handlePlayPause() {
|
||||
await commands.playerToggle();
|
||||
await playerController.toggle();
|
||||
}
|
||||
|
||||
async function handlePrevious() {
|
||||
await commands.playerPrevious();
|
||||
await playerController.previous();
|
||||
}
|
||||
|
||||
async function handleNext() {
|
||||
await commands.playerNext();
|
||||
await playerController.next();
|
||||
}
|
||||
|
||||
async function handleToggleShuffle() {
|
||||
await commands.playerToggleShuffle();
|
||||
await playerController.toggleShuffle();
|
||||
}
|
||||
|
||||
async function handleCycleRepeat() {
|
||||
await commands.playerCycleRepeat();
|
||||
await playerController.cycleRepeat();
|
||||
}
|
||||
|
||||
// Scrubbing (seek) handler
|
||||
@@ -134,7 +134,7 @@
|
||||
const newPosition = percent * displayDuration;
|
||||
|
||||
try {
|
||||
await commands.playerSeek(newPosition);
|
||||
await playerController.seek(newPosition);
|
||||
haptics.tap();
|
||||
} catch (err) {
|
||||
console.error("Failed to seek:", err);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import { playerController } from "$lib/player";
|
||||
import { truncateMiddle } from "$lib/utils/truncateMiddle";
|
||||
import { dndzone, SOURCES, TRIGGERS } from "svelte-dnd-action";
|
||||
import type { MediaItem } from "$lib/api/types";
|
||||
@@ -80,7 +80,7 @@
|
||||
queue.moveInQueue(fromIndex, toIndex);
|
||||
|
||||
// Sync with backend
|
||||
await commands.playerMoveInQueue(fromIndex, toIndex);
|
||||
await playerController.moveInQueue(fromIndex, toIndex);
|
||||
} catch (e) {
|
||||
console.error("Failed to move queue item:", e);
|
||||
// The store already updated optimistically, refresh if needed
|
||||
@@ -107,7 +107,7 @@
|
||||
e.stopPropagation();
|
||||
try {
|
||||
queue.removeFromQueue(index);
|
||||
await commands.playerRemoveFromQueue(index);
|
||||
await playerController.removeFromQueue(index);
|
||||
} catch (err) {
|
||||
console.error("Failed to remove from queue:", err);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
/**
|
||||
* VideoPlayer scrub regression tests (Android backend path)
|
||||
*
|
||||
* Reproduces the reported bug: with a sleep timer active, scrubbing the
|
||||
* video seek bar "seeks, then jumps back to the old position".
|
||||
*
|
||||
* Root cause history:
|
||||
* - Native init called onDestroy() after an await -> lifecycle_outside_component
|
||||
* -> the catch treated init as failed and silently flipped useHtml5Element to
|
||||
* true, so seeks went down the HTML5 path while ExoPlayer kept playing.
|
||||
* - The native SurfaceView has never been visible through the webview, so the
|
||||
* INTERIM behavior (until the video-player API refactor) is: when the backend
|
||||
* reports native mode, VideoPlayer deliberately overrides to HTML5 rendering
|
||||
* and stops the native backend (single audio source, webview owns playback).
|
||||
*
|
||||
* These tests pin the interim behavior: Android's native response is
|
||||
* overridden, the backend is stopped exactly once, and scrubbing keeps
|
||||
* working (and holds its position) with a sleep timer active.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
// ---- Mocks (must precede component import) --------------------------------
|
||||
|
||||
const channelHandlers: Record<string, (event: any) => void> = {};
|
||||
vi.mock("@tauri-apps/api/event", () => ({
|
||||
listen: vi.fn(async (channel: string, handler: any) => {
|
||||
channelHandlers[channel] = handler;
|
||||
return () => {
|
||||
delete channelHandlers[channel];
|
||||
};
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@tauri-apps/api/core", () => ({
|
||||
invoke: vi.fn(),
|
||||
}));
|
||||
|
||||
const playerPlayItem = vi.fn(async () => ({
|
||||
// What Android reports: native ExoPlayer backend
|
||||
useHtml5Element: false,
|
||||
backend: "exoplayer",
|
||||
state: { kind: "playing" },
|
||||
}));
|
||||
const playerSeekVideo = vi.fn(async (_h: string, position: number) => ({
|
||||
strategy: "native",
|
||||
position,
|
||||
}));
|
||||
const playerStop = vi.fn(async () => ({}));
|
||||
const playerToggle = vi.fn(async () => ({ state: "playing" }));
|
||||
const playerSetSleepTimer = vi.fn(async (mode: any) => ({ mode, remainingSeconds: 0 }));
|
||||
const playerCancelSleepTimer = vi.fn(async () => ({ mode: { kind: "off" }, remainingSeconds: 0 }));
|
||||
|
||||
vi.mock("$lib/api/bindings", () => ({
|
||||
commands: {
|
||||
playerPlayItem: (...a: any[]) => playerPlayItem(...(a as [])),
|
||||
playerSeekVideo: (...a: any[]) => playerSeekVideo(...(a as [string, number])),
|
||||
playerStop: (...a: any[]) => playerStop(...(a as [])),
|
||||
playerToggle: (...a: any[]) => playerToggle(...(a as [])),
|
||||
playerSetSleepTimer: (...a: any[]) => playerSetSleepTimer(...(a as [any])),
|
||||
playerCancelSleepTimer: (...a: any[]) => playerCancelSleepTimer(...(a as [])),
|
||||
playerSetSubtitleTrack: vi.fn(async () => ({})),
|
||||
playerSwitchAudioTrack: vi.fn(async () => ({})),
|
||||
storageGetSeriesAudioPreference: vi.fn(async () => null),
|
||||
storageSaveSeriesAudioPreference: vi.fn(async () => ({})),
|
||||
},
|
||||
events: {
|
||||
playerStatusEvent: { listen: vi.fn(async () => () => {}) },
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("$lib/stores/auth", () => ({
|
||||
auth: {
|
||||
getUserId: () => "user-1",
|
||||
getRepository: () => ({
|
||||
getHandle: () => "repo-1",
|
||||
getSubtitleUrl: async () => "",
|
||||
jrayActorsAt: async () => [],
|
||||
}),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("$app/navigation", () => ({
|
||||
goto: vi.fn(),
|
||||
}));
|
||||
|
||||
// Use the REAL sleepTimer store module so timer activation flows exactly as
|
||||
// in production (playerEvents.ts writes to it on every backend tick).
|
||||
|
||||
import { render, fireEvent, waitFor } from "@testing-library/svelte";
|
||||
import { tick } from "svelte";
|
||||
import VideoPlayer from "./VideoPlayer.svelte";
|
||||
import { sleepTimer, sleepTimerExpiredSignal } from "$lib/stores/sleepTimer";
|
||||
import type { MediaItem } from "$lib/api/types";
|
||||
|
||||
function makeEpisode(): MediaItem {
|
||||
return {
|
||||
id: "ep1",
|
||||
name: "Episode 1",
|
||||
type: "Episode",
|
||||
runTimeTicks: 24 * 60 * 10_000_000, // 24 min
|
||||
} as MediaItem;
|
||||
}
|
||||
|
||||
/** Simulate one backend sleep-timer tick, exactly as playerEvents.ts does. */
|
||||
function sleepTimerTick(remaining = 2) {
|
||||
sleepTimer.set({
|
||||
mode: { kind: "episodes", remaining },
|
||||
remainingSeconds: 0,
|
||||
});
|
||||
}
|
||||
|
||||
async function mountAndroidPlayer() {
|
||||
const utils = render(VideoPlayer, {
|
||||
props: {
|
||||
media: makeEpisode(),
|
||||
streamUrl: "http://server/videos/ep1/master.m3u8",
|
||||
mediaSourceId: "src-1",
|
||||
needsTranscoding: false,
|
||||
onClose: vi.fn(),
|
||||
},
|
||||
});
|
||||
|
||||
// Init: backend reports native, component overrides to HTML5 and stops it.
|
||||
await waitFor(() => expect(playerPlayItem).toHaveBeenCalled());
|
||||
await waitFor(() => expect(playerStop).toHaveBeenCalled());
|
||||
|
||||
const slider = utils.container.querySelector(
|
||||
'input[type="range"]'
|
||||
) as HTMLInputElement;
|
||||
const video = utils.container.querySelector("video") as HTMLVideoElement;
|
||||
expect(slider).not.toBeNull();
|
||||
expect(video).not.toBeNull();
|
||||
return { ...utils, slider, video };
|
||||
}
|
||||
|
||||
/** Scrub the seek bar to `target` seconds like a user drag. */
|
||||
async function scrubTo(
|
||||
slider: HTMLInputElement,
|
||||
video: HTMLVideoElement,
|
||||
target: number
|
||||
) {
|
||||
await fireEvent.mouseDown(slider);
|
||||
slider.value = String(target);
|
||||
await fireEvent.input(slider);
|
||||
await fireEvent.change(slider);
|
||||
await fireEvent.mouseUp(slider);
|
||||
// Resolve the "wait for seeked" step of the HTML5 native-seek path.
|
||||
await fireEvent(video, new Event("seeked"));
|
||||
await tick();
|
||||
}
|
||||
|
||||
describe("VideoPlayer scrubbing with active sleep timer (Android)", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
for (const key of Object.keys(channelHandlers)) delete channelHandlers[key];
|
||||
sleepTimer.set({ mode: { kind: "off" }, remainingSeconds: 0 });
|
||||
sleepTimerExpiredSignal.set(0);
|
||||
});
|
||||
|
||||
it("overrides the native backend response to HTML5 rendering and stops the backend once", async () => {
|
||||
await mountAndroidPlayer();
|
||||
// The native backend must be stopped so it doesn't play audio behind the
|
||||
// webview (frozen picture + double audio source).
|
||||
expect(playerStop).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("scrubbing without a timer seeks via the HTML5 path and keeps the new position", async () => {
|
||||
const { slider, video } = await mountAndroidPlayer();
|
||||
|
||||
await scrubTo(slider, video, 600);
|
||||
|
||||
await waitFor(() =>
|
||||
expect(playerSeekVideo).toHaveBeenCalledWith(
|
||||
"repo-1",
|
||||
600,
|
||||
"src-1",
|
||||
null,
|
||||
true // HTML5 path: the webview owns playback after the override
|
||||
)
|
||||
);
|
||||
expect(parseFloat(slider.value)).toBeCloseTo(600);
|
||||
});
|
||||
|
||||
it("scrubbing still works (and holds position) after enabling an episodes sleep timer", async () => {
|
||||
const { slider, video } = await mountAndroidPlayer();
|
||||
|
||||
// Enable "2 more episodes" timer; backend then ticks every second.
|
||||
sleepTimerTick(2);
|
||||
await tick();
|
||||
sleepTimerTick(2);
|
||||
await tick();
|
||||
|
||||
await scrubTo(slider, video, 600);
|
||||
await waitFor(() => expect(playerSeekVideo).toHaveBeenCalledTimes(1));
|
||||
expect(parseFloat(slider.value)).toBeCloseTo(600);
|
||||
|
||||
// Timer ticks after the seek must not snap the bar back.
|
||||
sleepTimerTick(2);
|
||||
await tick();
|
||||
expect(parseFloat(slider.value)).toBeCloseTo(600);
|
||||
|
||||
// A second scrub must also work.
|
||||
await scrubTo(slider, video, 900);
|
||||
await waitFor(() => expect(playerSeekVideo).toHaveBeenCalledTimes(2));
|
||||
expect(parseFloat(slider.value)).toBeCloseTo(900);
|
||||
});
|
||||
|
||||
it("sleep-timer ticks alone never move the seek bar", async () => {
|
||||
const { slider } = await mountAndroidPlayer();
|
||||
|
||||
const before = slider.value;
|
||||
for (let i = 0; i < 5; i++) {
|
||||
sleepTimerTick(2);
|
||||
await tick();
|
||||
}
|
||||
|
||||
expect(slider.value).toBe(before);
|
||||
expect(playerSeekVideo).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -13,6 +13,11 @@
|
||||
import SleepTimerIndicator from "./SleepTimerIndicator.svelte";
|
||||
import CachedImage from "../common/CachedImage.svelte";
|
||||
import { sleepTimerActive, sleepTimerExpiredSignal } from "$lib/stores/sleepTimer";
|
||||
import { playbackPosition } from "$lib/stores/player";
|
||||
import * as html5Adapter from "$lib/player/html5Adapter";
|
||||
import { playerController } from "$lib/player";
|
||||
import { Html5PlayerAdapter, type Html5ElementBridge } from "$lib/player/adapters";
|
||||
import { createRustReportHost } from "$lib/player/adapters/rustReportHost";
|
||||
|
||||
interface Props {
|
||||
media: MediaItem | null;
|
||||
@@ -65,7 +70,10 @@
|
||||
let controlsTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
let seekOffset = $state(0); // Track offset when seeking in transcoded streams
|
||||
let isSeeking = $state(false);
|
||||
let currentStreamUrl = $state(streamUrl);
|
||||
// Capture only the initial streamUrl prop; later prop changes are applied via
|
||||
// the $effect below (untrack keeps this a one-time snapshot, matching
|
||||
// reportMediaId above and silencing state_referenced_locally).
|
||||
let currentStreamUrl = $state(untrack(() => streamUrl));
|
||||
let hasReportedStart = $state(false);
|
||||
let progressInterval: ReturnType<typeof setInterval> | null = null;
|
||||
let isMediaReady = $state(false); // Track if media is ready to play (implements Loading state from DR-001)
|
||||
@@ -88,12 +96,47 @@
|
||||
|
||||
// Backend info from Rust (Rust decides which backend to use based on platform)
|
||||
let useHtml5Element = $state(true); // Default to HTML5, Rust will override if using native backend
|
||||
let backendChosen = false; // playerPlayItem succeeded and told us which backend to use
|
||||
let nativeUnlisteners: Array<() => void> = []; // raw-channel listeners for native backend mode
|
||||
// Position updates captured before a native seek can land after it and snap
|
||||
// the bar back; suppress backend position feeds briefly after each seek
|
||||
// (same idea as the MPV backend's last_seek_time suppression).
|
||||
let lastNativeSeekAt = 0;
|
||||
const NATIVE_SEEK_SETTLE_MS = 1500;
|
||||
function nativeSeekSettling(): boolean {
|
||||
return Date.now() - lastNativeSeekAt < NATIVE_SEEK_SETTLE_MS;
|
||||
}
|
||||
let didStartNativePlayback = $state(false); // Track if we started playback (to know if we should stop on unmount)
|
||||
let didStopBackendEarly = $state(false); // Track if we stopped backend early for non-transcoded content
|
||||
let swipeType = $state<"brightness" | null>(null);
|
||||
let hls: Hls | null = null; // HLS.js instance for streaming HLS content
|
||||
let hlsFatalRecoveryAttempts = 0; // Track recovery attempts to prevent infinite restarts
|
||||
|
||||
// ===== Player adapter (control boundary) =====
|
||||
// The adapter owns the high-level control contract (play/pause/seek/track).
|
||||
// VideoPlayer supplies a narrow bridge for the element/HLS-coupled parts and
|
||||
// registers the adapter with the facade so control intents — from UI OR from a
|
||||
// backend control event (lockscreen/remote/sleep) — reach this element.
|
||||
let playerAdapter: Html5PlayerAdapter | null = null;
|
||||
|
||||
function tearDownHls() {
|
||||
if (hls) {
|
||||
hls.detachMedia();
|
||||
hls.stopLoad();
|
||||
hls.destroy();
|
||||
hls = null;
|
||||
}
|
||||
}
|
||||
|
||||
const adapterBridge: Html5ElementBridge = {
|
||||
getElement: () => videoElement,
|
||||
getSeekOffset: () => seekOffset,
|
||||
setSeekOffset: (o) => { seekOffset = o; },
|
||||
setStreamUrl: (u) => { currentStreamUrl = u; },
|
||||
destroyHls: tearDownHls,
|
||||
getMediaSourceId: () => mediaSourceId ?? null,
|
||||
};
|
||||
|
||||
// Audio track selection
|
||||
let showAudioTrackMenu = $state(false);
|
||||
let selectedAudioTrackIndex = $state<number | null>(null);
|
||||
@@ -205,22 +248,25 @@
|
||||
hasPerformedInitialSeek = false; // Reset so new video can seek to initial position
|
||||
lastAppliedInitialPosition = undefined; // New stream - forget the previously-applied resume point
|
||||
endedFired = false; // New stream loaded - allow onEnded to fire again
|
||||
html5Adapter.resetReporting(); // New stream - clear position-report throttle
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Pause playback when the time-based sleep timer expires. The backend stops
|
||||
// its own (MPV/ExoPlayer) playback itself, but the HTML5 <video> element
|
||||
// plays in the webview outside the backend's control, so it must be paused
|
||||
// here or the sleep timer never actually stops video playback on Linux.
|
||||
let lastSleepExpirySeen = $sleepTimerExpiredSignal;
|
||||
// Sleep-timer expiry pause is now driven by the backend through the player
|
||||
// adapter: playerEvents.ts routes `sleep_timer_expired` to the active adapter's
|
||||
// pause() (see handleControlCommand / the sleep_timer_expired case). This
|
||||
// removes the component's direct videoElement.pause() reach-in — the backend
|
||||
// has control authority over the webview element via the adapter boundary.
|
||||
|
||||
// Native backend (Android ExoPlayer): drive the seek bar from the player
|
||||
// store, which is fed by the backend's PositionUpdate events. The legacy
|
||||
// "player://position-update" raw channel was never emitted by the backend,
|
||||
// so without this the bar only moves when the user scrubs.
|
||||
$effect(() => {
|
||||
if ($sleepTimerExpiredSignal !== lastSleepExpirySeen) {
|
||||
lastSleepExpirySeen = $sleepTimerExpiredSignal;
|
||||
if (useHtml5Element && videoElement && !videoElement.paused) {
|
||||
console.log("[VideoPlayer] Sleep timer expired - pausing playback");
|
||||
videoElement.pause();
|
||||
}
|
||||
const position = $playbackPosition;
|
||||
if (!useHtml5Element && !isDraggingSeekBar && !isSeeking && !nativeSeekSettling()) {
|
||||
currentTime = position;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -297,6 +343,25 @@
|
||||
console.log('[VideoPlayer] HLS manifest parsed, ready to play');
|
||||
});
|
||||
|
||||
// On the Android WebView the element's own `canplay` may not fire for
|
||||
// MSE-fed HLS, so treat the first buffered fragment as "ready" too.
|
||||
// This reveals the <video> element (otherwise it stays invisible behind
|
||||
// the black poster card while audio plays).
|
||||
hls.on(Hls.Events.FRAG_BUFFERED, () => {
|
||||
markMediaReady();
|
||||
});
|
||||
|
||||
// The canplay-fallback timeout is normally armed from the element's
|
||||
// `loadstart` event, but with hls.js the element's `src` is "" and
|
||||
// `loadstart` may not fire, so arm a backstop here directly.
|
||||
if (canplayFallbackTimeout) clearTimeout(canplayFallbackTimeout);
|
||||
canplayFallbackTimeout = setTimeout(() => {
|
||||
if (!isMediaReady && videoElement && videoElement.readyState >= 2) {
|
||||
console.warn('[VideoPlayer] HLS canplay fallback - revealing video (readyState:', videoElement.readyState, ')');
|
||||
markMediaReady();
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
// Reset recovery attempts for new HLS instance
|
||||
hlsFatalRecoveryAttempts = 0;
|
||||
|
||||
@@ -465,11 +530,30 @@
|
||||
|
||||
// Rust tells us which backend it's using
|
||||
useHtml5Element = response.useHtml5Element;
|
||||
backendChosen = true;
|
||||
console.log(`[VideoPlayer] Backend: ${response.backend}, useHtml5Element: ${useHtml5Element}`);
|
||||
|
||||
// INTERIM (until the video-player API refactor lands): always render
|
||||
// through the webview HTML5 element, including Android. The native
|
||||
// ExoPlayer SurfaceView sits behind an opaque webview and has never
|
||||
// actually been visible (an init bug kept the app on the HTML5 path
|
||||
// since the POC), so true native mode plays audio behind a frozen
|
||||
// picture. Stop the native backend and let the webview own playback,
|
||||
// matching Linux behavior and avoiding dual audio.
|
||||
if (!useHtml5Element) {
|
||||
console.warn("[VideoPlayer] Native video backend reported - overriding to HTML5 rendering (native surface not visible through webview)");
|
||||
useHtml5Element = true;
|
||||
try {
|
||||
await commands.playerStop();
|
||||
didStopBackendEarly = true;
|
||||
} catch (err) {
|
||||
console.warn("[VideoPlayer] Failed to stop native backend:", err);
|
||||
}
|
||||
}
|
||||
|
||||
// If using HTML5 element for non-transcoded content, stop the backend player
|
||||
// For transcoded content, we need to keep the backend running to handle seeking/audio track switching
|
||||
if (useHtml5Element && !needsTranscoding) {
|
||||
if (useHtml5Element && !needsTranscoding && !didStopBackendEarly) {
|
||||
try {
|
||||
console.log("[VideoPlayer] Using HTML5 for direct stream - stopping backend player to prevent dual audio");
|
||||
await commands.playerStop();
|
||||
@@ -483,27 +567,48 @@
|
||||
didStartNativePlayback = true; // Track that we need to stop backend on unmount
|
||||
}
|
||||
|
||||
// Register the HTML5 player adapter with the facade so control intents
|
||||
// (UI or backend lockscreen/remote/sleep events) route to this element.
|
||||
if (useHtml5Element) {
|
||||
const host = createRustReportHost(media.id, {
|
||||
onEnded: () => notifyEnded(),
|
||||
onStreamUrlChanged: (u) => { currentStreamUrl = u; },
|
||||
});
|
||||
playerAdapter = new Html5PlayerAdapter(host, adapterBridge);
|
||||
playerAdapter.attach(videoElement);
|
||||
playerController.setActiveAdapter(playerAdapter);
|
||||
}
|
||||
|
||||
if (!useHtml5Element) {
|
||||
// Using native backend, subscribe to player events
|
||||
didStartNativePlayback = true; // Track that we started native playback
|
||||
const unlisten1 = await listen("player://position-update", (event: any) => {
|
||||
if (!isDraggingSeekBar) {
|
||||
isPlaying = (response.state?.kind ?? response.state) === "playing";
|
||||
// Cleanup happens in the component's top-level onDestroy. Calling
|
||||
// onDestroy() here — after an await — throws lifecycle_outside_component,
|
||||
// which the catch below used to misread as an init failure: it flipped
|
||||
// useHtml5Element to true, so every seek went down the HTML5 path and
|
||||
// never reached ExoPlayer (the video "seeked" then snapped back).
|
||||
nativeUnlisteners.push(
|
||||
await listen("player://position-update", (event: any) => {
|
||||
if (!isDraggingSeekBar && !isSeeking && !nativeSeekSettling()) {
|
||||
currentTime = event.payload.position;
|
||||
}
|
||||
});
|
||||
|
||||
const unlisten2 = await listen("player://state-changed", (event: any) => {
|
||||
})
|
||||
);
|
||||
nativeUnlisteners.push(
|
||||
await listen("player://state-changed", (event: any) => {
|
||||
isPlaying = event.payload.state === "playing";
|
||||
});
|
||||
|
||||
// Clean up listeners on destroy
|
||||
onDestroy(() => {
|
||||
unlisten1();
|
||||
unlisten2();
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[VideoPlayer] Failed to initialize player:", err);
|
||||
if (backendChosen) {
|
||||
// The backend already accepted the item; a later error (e.g. event
|
||||
// subscription) must not silently switch the seek/controls path to
|
||||
// HTML5 while the native backend keeps playing.
|
||||
console.warn("[VideoPlayer] Backend already initialized - keeping native mode despite error");
|
||||
} else {
|
||||
// Fallback to HTML5 on error
|
||||
useHtml5Element = true;
|
||||
|
||||
@@ -521,6 +626,7 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load series audio preference (for TV shows)
|
||||
await loadSeriesAudioPreference();
|
||||
@@ -561,6 +667,12 @@
|
||||
// Stop RAF loop
|
||||
stopTimeUpdates();
|
||||
|
||||
// Unregister the adapter from the facade (guarded so we only clear our own).
|
||||
if (playerAdapter) {
|
||||
playerController.clearActiveAdapter(playerAdapter);
|
||||
playerAdapter = null;
|
||||
}
|
||||
|
||||
if (progressInterval) {
|
||||
clearInterval(progressInterval);
|
||||
}
|
||||
@@ -568,6 +680,12 @@
|
||||
clearInterval(debugLogInterval);
|
||||
}
|
||||
|
||||
// Remove native backend event listeners
|
||||
for (const unlisten of nativeUnlisteners) {
|
||||
unlisten();
|
||||
}
|
||||
nativeUnlisteners = [];
|
||||
|
||||
// Clean up HLS.js instance - prevent dual audio on unmount
|
||||
if (hls) {
|
||||
console.log("[VideoPlayer] Destroying HLS.js instance on unmount");
|
||||
@@ -607,6 +725,9 @@
|
||||
const newCurrentTime = seekOffset + videoElement.currentTime;
|
||||
if (videoElement.readyState >= 2) {
|
||||
currentTime = newCurrentTime;
|
||||
// Feed the Rust controller a throttled position tick (~250ms) so it
|
||||
// stays the source of truth for HTML5 video without flooding IPC.
|
||||
html5Adapter.reportPosition(currentTime, duration);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -656,6 +777,10 @@
|
||||
console.log("[VideoPlayer] videoDuration state is now:", videoDuration);
|
||||
}
|
||||
|
||||
// Tell the Rust controller the media is loaded and its duration (mirrors the
|
||||
// native MediaLoaded event so the backend has a duration for HTML5 video).
|
||||
html5Adapter.reportMediaLoaded(duration);
|
||||
|
||||
// Use setTimeout to log the derived value after reactive updates
|
||||
setTimeout(() => {
|
||||
console.log("[VideoPlayer] Derived duration value:", duration);
|
||||
@@ -663,6 +788,20 @@
|
||||
}, 0);
|
||||
}
|
||||
|
||||
// Flip out of the Loading state and reveal the <video> element (which is
|
||||
// `invisible` and covered by the black poster card until then). Multiple
|
||||
// signals can legitimately mean "ready": the native `canplay` event, hls.js
|
||||
// buffering its first fragment, or the element actually reaching `playing`.
|
||||
// On the Android system WebView the HLS path feeds the element through MSE
|
||||
// with `src=""`, so `loadstart`/`canplay` don't fire reliably and the
|
||||
// canplay-fallback timeout was never armed — audio played while the video
|
||||
// stayed invisible. Any of these callers now reveals it.
|
||||
function markMediaReady() {
|
||||
if (isMediaReady) return;
|
||||
console.log("[VideoPlayer] Marking media ready");
|
||||
isMediaReady = true;
|
||||
}
|
||||
|
||||
async function handleCanPlay() {
|
||||
// Media is ready to play - transition from Loading to Playing state (DR-001)
|
||||
console.log("[VideoPlayer] canplay event fired - media is ready");
|
||||
@@ -760,6 +899,9 @@
|
||||
function handlePlaying() {
|
||||
console.log("[VideoPlayer] playing event - playback resumed");
|
||||
isBuffering = false;
|
||||
// Safety net: if we reached `playing` we are definitely renderable, even if
|
||||
// `canplay`/hls FRAG_BUFFERED were missed on this WebView.
|
||||
markMediaReady();
|
||||
}
|
||||
|
||||
function handleLoadStart() {
|
||||
@@ -842,6 +984,10 @@
|
||||
function handlePlay() {
|
||||
isPlaying = true;
|
||||
startTimeUpdates(); // Start RAF loop for smooth time updates
|
||||
// Mirror the DOM state into the Rust PlayerController so it is the single
|
||||
// source of truth for HTML5 video (the <video> lives in the webview, which
|
||||
// Rust cannot observe directly). See html5Adapter.ts.
|
||||
html5Adapter.reportState("playing", reportMediaId ?? null);
|
||||
// Report playback start on first play (skip for live - no resume tracking)
|
||||
if (!isLive && !hasReportedStart && onReportStart) {
|
||||
onReportStart(currentTime, reportMediaId);
|
||||
@@ -852,6 +998,8 @@
|
||||
function handlePause() {
|
||||
isPlaying = false;
|
||||
stopTimeUpdates(); // Stop RAF loop when paused
|
||||
html5Adapter.reportState("paused", reportMediaId ?? null);
|
||||
html5Adapter.reportPosition(currentTime, duration, { force: true });
|
||||
// Report progress when paused
|
||||
if (onReportProgress) {
|
||||
onReportProgress(currentTime, true, reportMediaId);
|
||||
@@ -861,7 +1009,13 @@
|
||||
function handleEnded() {
|
||||
isPlaying = false;
|
||||
stopTimeUpdates(); // Stop RAF loop when ended
|
||||
// Report stop when video ends (skip for live - no resume tracking)
|
||||
// NOTE: do NOT report a "stopped" player state here. Natural end-of-video is
|
||||
// an autoplay handoff, not a stop: the backend's on_video_playback_ended
|
||||
// decides whether to advance to the next episode (incl. sleep-timer episode
|
||||
// counting). Emitting StateChanged{stopped} would flip the player/mode to
|
||||
// idle mid-handoff and suppress the next-episode auto-advance (pauses at the
|
||||
// end of an episode instead of continuing). onReportStop below still reports
|
||||
// progress to Jellyfin; notifyEnded() drives the autoplay decision.
|
||||
if (!isLive && onReportStop) {
|
||||
onReportStop(currentTime, reportMediaId);
|
||||
}
|
||||
@@ -870,19 +1024,13 @@
|
||||
}
|
||||
|
||||
async function togglePlayPause() {
|
||||
if (!useHtml5Element) {
|
||||
// Route through the facade → active adapter so the toggle goes through the
|
||||
// one control boundary (and the adapter reports the resulting element state
|
||||
// back into Rust). The element's own play/pause handlers update isPlaying.
|
||||
try {
|
||||
const response = (await commands.playerToggle()) as any;
|
||||
isPlaying = response.state === "playing";
|
||||
await playerController.toggle();
|
||||
} catch (err) {
|
||||
console.error("[VideoPlayer] Failed to toggle native player:", err);
|
||||
}
|
||||
} else if (videoElement) {
|
||||
if (videoElement.paused) {
|
||||
videoElement.play();
|
||||
} else {
|
||||
videoElement.pause();
|
||||
}
|
||||
console.error("[VideoPlayer] Failed to toggle playback:", err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -902,130 +1050,29 @@
|
||||
isDraggingSeekBar = false;
|
||||
|
||||
try {
|
||||
console.log("[VideoPlayer] Seeking to:", targetTime.toFixed(2), "useHtml5Element:", useHtml5Element);
|
||||
console.log("[VideoPlayer] Seeking to:", targetTime.toFixed(2));
|
||||
|
||||
const repo = auth.getRepository();
|
||||
if (!repo) {
|
||||
console.error("[VideoPlayer] No repository available");
|
||||
return;
|
||||
}
|
||||
// Optimistic display; the primitive updates currentTime/seekOffset as it
|
||||
// completes (reloadSource drives the stream URL via the adapter bridge).
|
||||
currentTime = targetTime;
|
||||
stopTimeUpdates(); // pause RAF while the seek settles
|
||||
|
||||
// Backend smart seeking handles both native and HTML5
|
||||
const response = (await commands.playerSeekVideo(
|
||||
repo.getHandle(),
|
||||
// The BACKEND decides the strategy (in-place vs transcode reload); the
|
||||
// facade dispatches the matching adapter PRIMITIVE. This is the shared
|
||||
// decision-in-Rust design — no strategy branch lives here anymore.
|
||||
lastNativeSeekAt = Date.now();
|
||||
await playerController.seekVideo(
|
||||
targetTime,
|
||||
mediaSourceId ?? null,
|
||||
selectedAudioTrackIndex ?? null,
|
||||
useHtml5Element
|
||||
)) as any;
|
||||
selectedAudioTrackIndex ?? null
|
||||
);
|
||||
|
||||
console.log("[VideoPlayer] Backend seek response:", response);
|
||||
|
||||
// For native backend, the backend handles everything internally
|
||||
if (!useHtml5Element) {
|
||||
// Backend already stopped, reloaded, and seeked if needed
|
||||
currentTime = response.position ?? targetTime;
|
||||
if (response.strategy === "reloadStream") {
|
||||
seekOffset = response.seekOffset ?? targetTime;
|
||||
currentStreamUrl = response.newUrl ?? currentStreamUrl;
|
||||
} else {
|
||||
seekOffset = 0;
|
||||
}
|
||||
console.log("[VideoPlayer] Native backend seek completed at position:", currentTime);
|
||||
return;
|
||||
// Resume smooth updates if still playing after the seek settled.
|
||||
if (videoElement && !videoElement.paused) {
|
||||
startTimeUpdates();
|
||||
}
|
||||
|
||||
// HTML5 backend - handle video element management
|
||||
if (!videoElement) {
|
||||
console.warn("[VideoPlayer] Cannot seek - video element not available");
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.strategy === "reloadStream") {
|
||||
// Transcoded stream - reload with new URL
|
||||
console.log("[VideoPlayer] Reloading HTML5 stream from position:", targetTime);
|
||||
const wasPlaying = !videoElement.paused;
|
||||
|
||||
// CRITICAL: Stop playback completely to prevent dual audio
|
||||
videoElement.pause();
|
||||
stopTimeUpdates(); // Stop RAF updates
|
||||
|
||||
// CRITICAL: Destroy old HLS instance completely to prevent dual audio
|
||||
if (hls) {
|
||||
console.log("[VideoPlayer] Destroying old HLS instance for seek");
|
||||
hls.detachMedia(); // Detach from video element
|
||||
hls.stopLoad(); // Stop loading fragments
|
||||
hls.destroy(); // Completely destroy the instance
|
||||
hls = null; // Clear reference
|
||||
}
|
||||
|
||||
// CRITICAL: Clear video element buffers completely
|
||||
if (videoElement.src) {
|
||||
videoElement.removeAttribute('src');
|
||||
videoElement.load(); // Reset and flush all buffers
|
||||
}
|
||||
|
||||
// Small delay to ensure cleanup completes before creating new HLS instance
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
|
||||
// Update stream URL (this will trigger $effect to create new HLS instance)
|
||||
seekOffset = response.seekOffset ?? targetTime;
|
||||
currentStreamUrl = response.newUrl ?? currentStreamUrl;
|
||||
currentTime = targetTime;
|
||||
|
||||
// Wait for video to be ready
|
||||
await new Promise<void>((resolve) => {
|
||||
const onCanPlay = () => {
|
||||
console.log("[VideoPlayer] Transcoded video loaded after seek");
|
||||
videoElement?.removeEventListener("canplay", onCanPlay);
|
||||
resolve();
|
||||
};
|
||||
if (videoElement) {
|
||||
videoElement.addEventListener("canplay", onCanPlay);
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
console.warn("[VideoPlayer] Transcoded seek timeout");
|
||||
videoElement?.removeEventListener("canplay", onCanPlay);
|
||||
resolve();
|
||||
}, 10000);
|
||||
});
|
||||
|
||||
if (wasPlaying && videoElement) {
|
||||
await videoElement.play();
|
||||
startTimeUpdates(); // Restart RAF updates
|
||||
}
|
||||
} else {
|
||||
// Native browser seeking
|
||||
console.log("[VideoPlayer] Using native HTML5 seek to:", targetTime.toFixed(2));
|
||||
videoElement.currentTime = targetTime;
|
||||
currentTime = targetTime;
|
||||
seekOffset = 0;
|
||||
|
||||
// Wait for seek to complete
|
||||
await new Promise<void>((resolve) => {
|
||||
const onSeeked = () => {
|
||||
console.log("[VideoPlayer] Native seek completed");
|
||||
videoElement?.removeEventListener("seeked", onSeeked);
|
||||
resolve();
|
||||
};
|
||||
if (videoElement) {
|
||||
videoElement.addEventListener("seeked", onSeeked);
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
videoElement?.removeEventListener("seeked", onSeeked);
|
||||
resolve();
|
||||
}, 2000);
|
||||
});
|
||||
}
|
||||
|
||||
console.log("[VideoPlayer] HTML5 seek completed:", {
|
||||
strategy: response.strategy,
|
||||
targetTime: targetTime.toFixed(2),
|
||||
actualTime: videoElement.currentTime.toFixed(2),
|
||||
seekOffset,
|
||||
});
|
||||
console.log("[VideoPlayer] Seek completed at:", currentTime.toFixed(2), "offset:", seekOffset);
|
||||
} catch (err) {
|
||||
console.error("[VideoPlayer] Seek failed:", err);
|
||||
} finally {
|
||||
@@ -1195,73 +1242,19 @@
|
||||
showAudioTrackMenu = false;
|
||||
|
||||
try {
|
||||
const repo = auth.getRepository();
|
||||
if (!repo) throw new Error("Not authenticated");
|
||||
|
||||
// Call unified backend command
|
||||
const response = (await commands.playerSwitchAudioTrack(
|
||||
repo.getHandle(),
|
||||
// The BACKEND decides whether the audio-track switch needs a transcode
|
||||
// reload; the facade dispatches the resulting adapter PRIMITIVE
|
||||
// (reloadSource) which runs the invariant dual-audio teardown sequence.
|
||||
// No strategy branch lives here anymore.
|
||||
stopTimeUpdates();
|
||||
await playerController.switchAudioTrack(
|
||||
streamIndex,
|
||||
arrayIndex,
|
||||
useHtml5Element,
|
||||
useHtml5Element && videoElement ? videoElement.currentTime + seekOffset : null,
|
||||
videoElement ? videoElement.currentTime + seekOffset : null,
|
||||
mediaSourceId ?? null
|
||||
)) as any;
|
||||
|
||||
// Handle response based on strategy
|
||||
if (response.strategy === "reloadStream" && useHtml5Element && videoElement) {
|
||||
console.log("[VideoPlayer] Switching audio track - reloading stream");
|
||||
|
||||
// Save state before reload
|
||||
const wasPlaying = !videoElement.paused;
|
||||
|
||||
// CRITICAL: Stop playback completely to prevent dual audio
|
||||
videoElement.pause();
|
||||
stopTimeUpdates(); // Stop RAF updates
|
||||
|
||||
// CRITICAL: Destroy old HLS instance completely to prevent dual audio
|
||||
if (hls) {
|
||||
console.log("[VideoPlayer] Destroying old HLS instance for audio track switch");
|
||||
hls.detachMedia(); // Detach from video element
|
||||
hls.stopLoad(); // Stop loading fragments
|
||||
hls.destroy(); // Completely destroy the instance
|
||||
hls = null; // Clear reference
|
||||
}
|
||||
|
||||
// CRITICAL: Clear video element buffers completely
|
||||
if (videoElement.src) {
|
||||
videoElement.removeAttribute('src');
|
||||
videoElement.load(); // Reset and flush all buffers
|
||||
}
|
||||
|
||||
// Small delay to ensure cleanup completes before creating new HLS instance
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
|
||||
// Update stream URL (this will trigger $effect to create new HLS instance)
|
||||
currentStreamUrl = response.newUrl!;
|
||||
seekOffset = response.position!;
|
||||
|
||||
// Wait for video to be ready
|
||||
await new Promise<void>((resolve) => {
|
||||
const onCanPlay = () => {
|
||||
console.log("[VideoPlayer] Video reloaded with new audio track");
|
||||
videoElement?.removeEventListener("canplay", onCanPlay);
|
||||
resolve();
|
||||
};
|
||||
videoElement!.addEventListener("canplay", onCanPlay);
|
||||
|
||||
// Timeout fallback
|
||||
setTimeout(() => {
|
||||
videoElement?.removeEventListener("canplay", onCanPlay);
|
||||
resolve();
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
// Resume playback if it was playing
|
||||
if (wasPlaying) {
|
||||
await videoElement.play();
|
||||
startTimeUpdates(); // Restart RAF updates
|
||||
}
|
||||
);
|
||||
if (videoElement && !videoElement.paused) {
|
||||
startTimeUpdates();
|
||||
}
|
||||
|
||||
console.log("[VideoPlayer] Successfully changed audio track");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import { playerController } from "$lib/player";
|
||||
import { volume, isMuted, mergedVolume } from "$lib/stores/player";
|
||||
import { isRemoteMode } from "$lib/stores/playbackMode";
|
||||
import { selectedSession, sessions } from "$lib/stores/sessions";
|
||||
@@ -31,7 +31,7 @@
|
||||
// Remote mode: send volume as 0-100 integer to remote session
|
||||
await sessions.sendVolume($selectedSession.id, Math.round(newVolume * 100));
|
||||
} else {
|
||||
await commands.playerSetVolume(newVolume);
|
||||
await playerController.setVolume(newVolume);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
if ($isRemoteMode && $selectedSession) {
|
||||
await sessions.sendToggleMute($selectedSession.id);
|
||||
} else {
|
||||
await commands.playerToggleMute();
|
||||
await playerController.toggleMute();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* Unit tests for Html5PlayerAdapter.
|
||||
*
|
||||
* The Option-1 primitive design makes the adapter pure, decision-free mechanics
|
||||
* — it takes a mock <video> element + bridge + host, so we can assert each
|
||||
* primitive drives the element correctly without any real DOM or backend.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { Html5PlayerAdapter, type Html5ElementBridge } from "./html5Adapter";
|
||||
import type { AdapterHost } from "./types";
|
||||
|
||||
/** A minimal fake <video> element that records mutations and fires events. */
|
||||
function makeFakeVideo() {
|
||||
const listeners: Record<string, Array<() => void>> = {};
|
||||
const el: any = {
|
||||
paused: true,
|
||||
currentTime: 0,
|
||||
volume: 1,
|
||||
muted: false,
|
||||
src: "blob:existing",
|
||||
play: vi.fn(async () => {
|
||||
el.paused = false;
|
||||
}),
|
||||
pause: vi.fn(() => {
|
||||
el.paused = true;
|
||||
}),
|
||||
load: vi.fn(),
|
||||
removeAttribute: vi.fn((attr: string) => {
|
||||
if (attr === "src") el.src = "";
|
||||
}),
|
||||
addEventListener: (event: string, cb: () => void) => {
|
||||
(listeners[event] ??= []).push(cb);
|
||||
},
|
||||
removeEventListener: (event: string, cb: () => void) => {
|
||||
listeners[event] = (listeners[event] ?? []).filter((f) => f !== cb);
|
||||
},
|
||||
// Test helper: fire an event so waitForEvent resolves immediately.
|
||||
_fire: (event: string) => {
|
||||
(listeners[event] ?? []).slice().forEach((f) => f());
|
||||
},
|
||||
querySelectorAll: () => [] as any,
|
||||
textTracks: [] as any,
|
||||
};
|
||||
return el;
|
||||
}
|
||||
type FakeVideo = ReturnType<typeof makeFakeVideo>;
|
||||
|
||||
function makeBridge(overrides: Partial<Html5ElementBridge> = {}): Html5ElementBridge {
|
||||
let offset = 0;
|
||||
return {
|
||||
getElement: () => null,
|
||||
getSeekOffset: () => offset,
|
||||
setSeekOffset: vi.fn((o: number) => { offset = o; }),
|
||||
setStreamUrl: vi.fn(),
|
||||
destroyHls: vi.fn(),
|
||||
getMediaSourceId: () => "msid-1",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function makeHost(): AdapterHost {
|
||||
return {
|
||||
onState: vi.fn(),
|
||||
onPosition: vi.fn(),
|
||||
onMediaLoaded: vi.fn(),
|
||||
onEnded: vi.fn(),
|
||||
onError: vi.fn(),
|
||||
onStreamUrlChanged: vi.fn(),
|
||||
onBuffering: vi.fn(),
|
||||
onReady: vi.fn(),
|
||||
};
|
||||
}
|
||||
|
||||
describe("Html5PlayerAdapter", () => {
|
||||
let host: AdapterHost;
|
||||
let bridge: Html5ElementBridge;
|
||||
let adapter: Html5PlayerAdapter;
|
||||
let video: ReturnType<typeof makeFakeVideo>;
|
||||
|
||||
beforeEach(() => {
|
||||
host = makeHost();
|
||||
bridge = makeBridge();
|
||||
adapter = new Html5PlayerAdapter(host, bridge);
|
||||
video = makeFakeVideo();
|
||||
adapter.attach(video);
|
||||
});
|
||||
|
||||
it("is an html5-kind adapter", () => {
|
||||
expect(adapter.kind).toBe("html5");
|
||||
});
|
||||
|
||||
it("play() calls element.play()", async () => {
|
||||
await adapter.play();
|
||||
expect(video.play).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("pause() calls element.pause()", async () => {
|
||||
video.paused = false;
|
||||
await adapter.pause();
|
||||
expect(video.pause).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("toggle() plays when paused and reports the resulting state", async () => {
|
||||
video.paused = true;
|
||||
const playing = await adapter.toggle();
|
||||
expect(video.play).toHaveBeenCalled();
|
||||
expect(playing).toBe(true);
|
||||
});
|
||||
|
||||
it("toggle() pauses when playing", async () => {
|
||||
video.paused = false;
|
||||
const playing = await adapter.toggle();
|
||||
expect(video.pause).toHaveBeenCalled();
|
||||
expect(playing).toBe(false);
|
||||
});
|
||||
|
||||
it("seekElement() sets currentTime, offset, and waits for 'seeked'", async () => {
|
||||
const p = adapter.seekElement(42, 0);
|
||||
expect(video.currentTime).toBe(42);
|
||||
expect(bridge.setSeekOffset).toHaveBeenCalledWith(0);
|
||||
video._fire("seeked"); // resolve the wait
|
||||
await p;
|
||||
});
|
||||
|
||||
it("reloadSource() runs the invariant teardown->swap->resume sequence", async () => {
|
||||
video.paused = false; // was playing → should resume
|
||||
const p = adapter.reloadSource("http://new/master.m3u8", 120);
|
||||
|
||||
// Teardown happened synchronously before the awaited canplay wait.
|
||||
expect(video.pause).toHaveBeenCalled();
|
||||
expect(bridge.destroyHls).toHaveBeenCalledTimes(1);
|
||||
expect(video.removeAttribute).toHaveBeenCalledWith("src");
|
||||
expect(video.load).toHaveBeenCalled();
|
||||
|
||||
// Allow the internal 100ms settle delay, then fire canplay to resume.
|
||||
await new Promise((r) => setTimeout(r, 110));
|
||||
expect(bridge.setSeekOffset).toHaveBeenCalledWith(120);
|
||||
expect(bridge.setStreamUrl).toHaveBeenCalledWith("http://new/master.m3u8");
|
||||
video._fire("canplay");
|
||||
await p;
|
||||
expect(video.play).toHaveBeenCalled(); // resumed because it was playing
|
||||
});
|
||||
|
||||
it("reloadSource() does not resume when it was paused", async () => {
|
||||
video.paused = true;
|
||||
const p = adapter.reloadSource("http://new/master.m3u8", 30);
|
||||
await new Promise((r) => setTimeout(r, 110));
|
||||
video._fire("canplay");
|
||||
await p;
|
||||
expect(video.play).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("setVolume() clamps to 0..1", () => {
|
||||
adapter.setVolume(1.5);
|
||||
expect(video.volume).toBe(1);
|
||||
adapter.setVolume(-0.5);
|
||||
expect(video.volume).toBe(0);
|
||||
adapter.setVolume(0.4);
|
||||
expect(video.volume).toBeCloseTo(0.4);
|
||||
});
|
||||
|
||||
it("setMuted() sets the element muted flag", () => {
|
||||
adapter.setMuted(true);
|
||||
expect(video.muted).toBe(true);
|
||||
});
|
||||
|
||||
it("getPosition() returns element time plus the transcode offset", () => {
|
||||
video.currentTime = 10;
|
||||
(bridge.getSeekOffset as any) = () => 100;
|
||||
// Rebuild adapter with the offset-returning bridge.
|
||||
const a = new Html5PlayerAdapter(host, bridge);
|
||||
a.attach(video);
|
||||
expect(a.getPosition()).toBe(110);
|
||||
});
|
||||
|
||||
it("dispose() tears down hls and clears the element", async () => {
|
||||
await adapter.dispose();
|
||||
expect(bridge.destroyHls).toHaveBeenCalled();
|
||||
expect(video.pause).toHaveBeenCalled();
|
||||
// After dispose, primitives are no-ops (element detached).
|
||||
await adapter.play();
|
||||
// play was called once during dispose teardown? no — play only on reload/resume.
|
||||
expect(video.play).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("primitives are safe no-ops before an element is attached", async () => {
|
||||
const bare = new Html5PlayerAdapter(host, bridge);
|
||||
await expect(bare.play()).resolves.toBeUndefined();
|
||||
await expect(bare.pause()).resolves.toBeUndefined();
|
||||
await expect(bare.seekElement(5, 0)).resolves.toBeUndefined();
|
||||
expect(await bare.toggle()).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* Html5PlayerAdapter — the Linux/desktop (and interim Android) PlayerAdapter
|
||||
* implementation. It owns the high-level control surface for an HTML5 `<video>`
|
||||
* element and reports the element's lifecycle back into Rust via its
|
||||
* {@link AdapterHost}.
|
||||
*
|
||||
* Design note on the split with VideoPlayer.svelte:
|
||||
* The delicate, timing-sensitive parts (hls.js instance lifecycle, the transcode
|
||||
* "reload stream" seek/audio-track dance with its dual-audio teardown and
|
||||
* canplay waits) are inherently coupled to Svelte reactive state and the DOM
|
||||
* element. Rather than relocate that reactive machinery wholesale (high
|
||||
* regression risk), the adapter receives an {@link Html5ElementBridge} of narrow
|
||||
* callbacks the owning component supplies. The adapter is the single OWNER of the
|
||||
* control contract (play/pause/seek/track/volume) and of reporting; the bridge is
|
||||
* the seam to the component's element/HLS/reactive state. This keeps all control
|
||||
* intents flowing through the PlayerAdapter interface while preserving the
|
||||
* hard-won element behavior verbatim.
|
||||
*
|
||||
* TRACES: UR-003, UR-005, UR-020, UR-021 | DR-001, DR-023, DR-024, DR-028
|
||||
*/
|
||||
|
||||
import type { AdapterHost, PlayerAdapter, PlayerLoadOptions } from "./types";
|
||||
|
||||
/**
|
||||
* Narrow seam the owning component provides so the adapter can execute the
|
||||
* element/HLS-coupled parts of a control action without re-implementing the
|
||||
* component's reactive HLS lifecycle. Every function here is a thin wrapper over
|
||||
* work the component already does.
|
||||
*/
|
||||
export interface Html5ElementBridge {
|
||||
/** The bound <video> element, or null before mount / after teardown. */
|
||||
getElement(): HTMLVideoElement | null;
|
||||
/** Current seek offset (seconds) for transcoded streams. */
|
||||
getSeekOffset(): number;
|
||||
setSeekOffset(offset: number): void;
|
||||
/** Update the stream URL the component renders (triggers its HLS $effect). */
|
||||
setStreamUrl(url: string): void;
|
||||
/** Tear down the component-owned hls.js instance (dual-audio prevention). */
|
||||
destroyHls(): void;
|
||||
/** Media source id for seek/audio-track URLs. */
|
||||
getMediaSourceId(): string | null;
|
||||
}
|
||||
|
||||
export class Html5PlayerAdapter implements PlayerAdapter {
|
||||
readonly kind = "html5" as const;
|
||||
|
||||
private attachedElement: HTMLVideoElement | null = null;
|
||||
private host: AdapterHost;
|
||||
private bridge: Html5ElementBridge;
|
||||
|
||||
constructor(host: AdapterHost, bridge: Html5ElementBridge) {
|
||||
this.host = host;
|
||||
this.bridge = bridge;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the LIVE <video> element. The bridge's `getElement()` returns the
|
||||
* component's current reactive `videoElement`, which is authoritative: the
|
||||
* element can be re-bound when the {#if} block re-renders, so a value captured
|
||||
* once in `attach()` may go stale (this caused play/pause to silently no-op).
|
||||
* Falls back to the attach()-captured element for unit tests whose bridge
|
||||
* returns null.
|
||||
*/
|
||||
private get element(): HTMLVideoElement | null {
|
||||
return this.bridge.getElement() ?? this.attachedElement;
|
||||
}
|
||||
|
||||
attach(element: HTMLVideoElement | null): void {
|
||||
this.attachedElement = element;
|
||||
}
|
||||
|
||||
async load(streamUrl: string, _options: PlayerLoadOptions): Promise<void> {
|
||||
// The component's reactive HLS $effect performs the actual attach/load when
|
||||
// the stream URL is set; loading is therefore driven by setStreamUrl. The
|
||||
// component's canplay/frag-buffered path reports readiness through the host.
|
||||
this.bridge.setSeekOffset(0);
|
||||
this.bridge.setStreamUrl(streamUrl);
|
||||
this.host.onState("loading");
|
||||
}
|
||||
|
||||
async play(): Promise<void> {
|
||||
const el = this.element;
|
||||
if (!el) return;
|
||||
try {
|
||||
await el.play();
|
||||
// handlePlay on the element reports "playing"; no double-report here.
|
||||
} catch (err) {
|
||||
this.host.onError(`play() failed: ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
async pause(): Promise<void> {
|
||||
this.element?.pause();
|
||||
}
|
||||
|
||||
async toggle(): Promise<boolean> {
|
||||
const el = this.element;
|
||||
if (!el) return false;
|
||||
if (el.paused) {
|
||||
await this.play();
|
||||
return true;
|
||||
}
|
||||
await this.pause();
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* PRIMITIVE: in-place element seek (no reload). The backend already decided
|
||||
* this seek does not need a transcode reload.
|
||||
*/
|
||||
async seekElement(positionSeconds: number, offset: number): Promise<void> {
|
||||
const el = this.element;
|
||||
if (!el) return;
|
||||
el.currentTime = positionSeconds;
|
||||
this.bridge.setSeekOffset(offset);
|
||||
await this.waitForEvent(el, "seeked", 2000);
|
||||
}
|
||||
|
||||
/**
|
||||
* PRIMITIVE: compound reload — the invariant HTML5 sequence to swap the source
|
||||
* and resume at `offset`. Contains NO strategy decision; the backend already
|
||||
* decided to reload and supplied the url/offset. Preserves the hard-won
|
||||
* dual-audio teardown and canplay wait.
|
||||
*/
|
||||
async reloadSource(url: string, offset: number): Promise<void> {
|
||||
const el = this.element;
|
||||
if (!el) {
|
||||
// Still update the stream URL so the component's HLS $effect can pick it up.
|
||||
this.bridge.setSeekOffset(offset);
|
||||
this.bridge.setStreamUrl(url);
|
||||
return;
|
||||
}
|
||||
const wasPlaying = !el.paused;
|
||||
el.pause();
|
||||
this.bridge.destroyHls();
|
||||
if (el.src) {
|
||||
el.removeAttribute("src");
|
||||
el.load();
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
this.bridge.setSeekOffset(offset);
|
||||
this.bridge.setStreamUrl(url);
|
||||
await this.waitForEvent(el, "canplay", 10000);
|
||||
if (wasPlaying) await el.play();
|
||||
}
|
||||
|
||||
setVolume(volume: number): void {
|
||||
if (this.element) this.element.volume = Math.max(0, Math.min(1, volume));
|
||||
}
|
||||
|
||||
setMuted(muted: boolean): void {
|
||||
if (this.element) this.element.muted = muted;
|
||||
}
|
||||
|
||||
/** Subtitle selection: HTML5 toggles textTracks on the element directly. */
|
||||
async selectSubtitle(streamIndex: number | null, _arrayIndex?: number): Promise<void> {
|
||||
const el = this.element;
|
||||
if (!el || !el.textTracks) return;
|
||||
for (let i = 0; i < el.textTracks.length; i++) {
|
||||
el.textTracks[i].mode = "disabled";
|
||||
}
|
||||
if (streamIndex !== null) {
|
||||
const tracks = el.querySelectorAll("track");
|
||||
tracks.forEach((track) => {
|
||||
const idx = parseInt(track.getAttribute("data-stream-index") || "-1");
|
||||
if (idx === streamIndex && track.track) {
|
||||
track.track.mode = "showing";
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getPosition(): number {
|
||||
const el = this.element;
|
||||
if (!el) return 0;
|
||||
return el.currentTime + this.bridge.getSeekOffset();
|
||||
}
|
||||
|
||||
async dispose(): Promise<void> {
|
||||
this.bridge.destroyHls();
|
||||
const el = this.element;
|
||||
if (el) {
|
||||
el.pause();
|
||||
el.removeAttribute("src");
|
||||
el.load();
|
||||
}
|
||||
this.attachedElement = null;
|
||||
}
|
||||
|
||||
/** Resolve when `event` fires on `el`, or after `timeoutMs` as a fallback. */
|
||||
private waitForEvent(el: HTMLVideoElement, event: string, timeoutMs: number): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
const done = () => {
|
||||
el.removeEventListener(event, done);
|
||||
resolve();
|
||||
};
|
||||
el.addEventListener(event, done);
|
||||
setTimeout(done, timeoutMs);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Player adapter factory + public exports.
|
||||
*
|
||||
* `createAdapter` selects the concrete PlayerAdapter for the current platform.
|
||||
* It is the single place that encodes the INTERIM Android override: the Rust
|
||||
* backend may report a native ExoPlayer backend, but native Android video
|
||||
* rendering is blocked upstream (tauri#10152 — transparent webview / SurfaceView
|
||||
* compositing), so we render Android video through the HTML5 adapter for now.
|
||||
* When that upstream limitation is resolved, flip this to honor `backendKind`.
|
||||
*
|
||||
* TRACES: UR-003 | DR-004
|
||||
*/
|
||||
|
||||
import { Html5PlayerAdapter, type Html5ElementBridge } from "./html5Adapter";
|
||||
import { NativePlayerAdapter } from "./nativeAdapter";
|
||||
import type { AdapterHost, PlayerAdapter } from "./types";
|
||||
|
||||
export type { PlayerAdapter, AdapterHost, PlayerLoadOptions, SubtitleTrackInput } from "./types";
|
||||
export type { Html5ElementBridge } from "./html5Adapter";
|
||||
export { Html5PlayerAdapter } from "./html5Adapter";
|
||||
export { NativePlayerAdapter } from "./nativeAdapter";
|
||||
|
||||
/** What the Rust `player_play_item` response says it chose. */
|
||||
export type BackendKind = "html5" | "native";
|
||||
|
||||
export interface CreateAdapterArgs {
|
||||
/** Backend kind reported by `player_play_item` (`useHtml5Element`). */
|
||||
backendKind: BackendKind;
|
||||
host: AdapterHost;
|
||||
/** Required for the HTML5 adapter; ignored by the native adapter. */
|
||||
bridge?: Html5ElementBridge;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the adapter for this platform/stream.
|
||||
*
|
||||
* INTERIM: always returns the HTML5 adapter, because the native surface is not
|
||||
* visible through the webview on current Tauri (see module docs). The bridge is
|
||||
* therefore required.
|
||||
*/
|
||||
export function createAdapter({ backendKind, host, bridge }: CreateAdapterArgs): PlayerAdapter {
|
||||
// INTERIM OVERRIDE: force HTML5 rendering even when the backend reports native.
|
||||
const effectiveKind: BackendKind = "html5";
|
||||
|
||||
if (effectiveKind === "html5") {
|
||||
if (!bridge) {
|
||||
throw new Error("createAdapter: Html5ElementBridge is required for the HTML5 adapter");
|
||||
}
|
||||
return new Html5PlayerAdapter(host, bridge);
|
||||
}
|
||||
|
||||
// Reached only once the interim override is lifted (native Android unblocked).
|
||||
void backendKind;
|
||||
return new NativePlayerAdapter(host);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Unit tests for NativePlayerAdapter — thin delegate to backend commands.
|
||||
* Pins the primitive→command mapping so the ExoPlayer path stays correct.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
const playerPlay = vi.fn((..._a: any[]): any => ({}));
|
||||
const playerPause = vi.fn((..._a: any[]): any => ({}));
|
||||
const playerToggle = vi.fn((..._a: any[]): any => ({ state: "playing" }));
|
||||
const playerSetVolume = vi.fn((..._a: any[]): any => ({}));
|
||||
const playerToggleMute = vi.fn((..._a: any[]): any => ({}));
|
||||
const playerSetSubtitleTrack = vi.fn((..._a: any[]): any => ({}));
|
||||
|
||||
vi.mock("$lib/api/bindings", () => ({
|
||||
commands: {
|
||||
playerPlay: (...a: any[]) => playerPlay(...a),
|
||||
playerPause: (...a: any[]) => playerPause(...a),
|
||||
playerToggle: (...a: any[]) => playerToggle(...a),
|
||||
playerSetVolume: (...a: any[]) => playerSetVolume(...a),
|
||||
playerToggleMute: (...a: any[]) => playerToggleMute(...a),
|
||||
playerSetSubtitleTrack: (...a: any[]) => playerSetSubtitleTrack(...a),
|
||||
},
|
||||
}));
|
||||
|
||||
import { NativePlayerAdapter } from "./nativeAdapter";
|
||||
import type { AdapterHost } from "./types";
|
||||
|
||||
function makeHost(): AdapterHost {
|
||||
return {
|
||||
onState: vi.fn(), onPosition: vi.fn(), onMediaLoaded: vi.fn(), onEnded: vi.fn(),
|
||||
onError: vi.fn(), onStreamUrlChanged: vi.fn(), onBuffering: vi.fn(), onReady: vi.fn(),
|
||||
};
|
||||
}
|
||||
|
||||
describe("NativePlayerAdapter", () => {
|
||||
let adapter: NativePlayerAdapter;
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
adapter = new NativePlayerAdapter(makeHost());
|
||||
});
|
||||
|
||||
it("is a native-kind adapter", () => {
|
||||
expect(adapter.kind).toBe("native");
|
||||
});
|
||||
|
||||
it("delegates play/pause to backend commands", async () => {
|
||||
await adapter.play();
|
||||
await adapter.pause();
|
||||
expect(playerPlay).toHaveBeenCalledTimes(1);
|
||||
expect(playerPause).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("toggle() reflects the backend's resulting playing state", async () => {
|
||||
expect(await adapter.toggle()).toBe(true);
|
||||
expect(playerToggle).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("records position on seek/reload primitives (backend does the real work)", async () => {
|
||||
await adapter.seekElement(55, 0);
|
||||
expect(adapter.getPosition()).toBe(55);
|
||||
await adapter.reloadSource("ignored", 200);
|
||||
expect(adapter.getPosition()).toBe(200);
|
||||
});
|
||||
|
||||
it("load() seeds a resume position", async () => {
|
||||
await adapter.load("url", {
|
||||
mediaId: "m", mediaSourceId: null, needsTranscoding: false,
|
||||
initialPosition: 90, isLive: false, audioTrackIndex: null,
|
||||
knownDuration: 0, subtitleTracks: [],
|
||||
});
|
||||
expect(adapter.getPosition()).toBe(90);
|
||||
});
|
||||
|
||||
it("setVolume clamps and delegates; setMuted toggles mute", () => {
|
||||
adapter.setVolume(2);
|
||||
expect(playerSetVolume).toHaveBeenCalledWith(1);
|
||||
adapter.setMuted(true);
|
||||
expect(playerToggleMute).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("selectSubtitle maps null to disable and uses arrayIndex when given", async () => {
|
||||
await adapter.selectSubtitle(null);
|
||||
expect(playerSetSubtitleTrack).toHaveBeenCalledWith(null);
|
||||
await adapter.selectSubtitle(5, 2);
|
||||
expect(playerSetSubtitleTrack).toHaveBeenCalledWith(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* NativePlayerAdapter — the Android/ExoPlayer PlayerAdapter implementation.
|
||||
*
|
||||
* ExoPlayer is driven entirely by the Rust backend (JNI), which already emits
|
||||
* PlayerStatusEvents and handles seek/audio-track internally. So this adapter is
|
||||
* a thin delegate to backend commands; there is no DOM element to touch and no
|
||||
* hls.js. State reporting is unnecessary here because the native backend emits
|
||||
* events directly — the adapter's job is only to forward control intents.
|
||||
*
|
||||
* NOTE: On current Tauri, native Android video rendering is blocked upstream
|
||||
* (transparent webview / SurfaceView compositing — tauri#10152), so video on
|
||||
* Android currently runs through the HTML5 adapter via the interim override in
|
||||
* the factory. This adapter exists for the audio/native path and for when that
|
||||
* upstream limitation is resolved.
|
||||
*
|
||||
* TRACES: UR-003, UR-005 | DR-004, DR-028
|
||||
*/
|
||||
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import type { AdapterHost, PlayerAdapter, PlayerLoadOptions } from "./types";
|
||||
|
||||
export class NativePlayerAdapter implements PlayerAdapter {
|
||||
readonly kind = "native" as const;
|
||||
|
||||
// Kept for symmetry / future reporting needs; the native backend emits events.
|
||||
private host: AdapterHost;
|
||||
private position = 0;
|
||||
|
||||
constructor(host: AdapterHost) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
// The native surface is owned by the backend; nothing to attach in the DOM.
|
||||
attach(_element: HTMLVideoElement | null): void {}
|
||||
|
||||
async load(_streamUrl: string, options: PlayerLoadOptions): Promise<void> {
|
||||
// player_play_item already initiated native playback before this adapter is
|
||||
// created; nothing further to do. Seed a resume position if requested (the
|
||||
// native backend performs the actual seek internally).
|
||||
if (options.initialPosition > 0) {
|
||||
this.position = options.initialPosition;
|
||||
}
|
||||
}
|
||||
|
||||
async play(): Promise<void> {
|
||||
await commands.playerPlay();
|
||||
}
|
||||
|
||||
async pause(): Promise<void> {
|
||||
await commands.playerPause();
|
||||
}
|
||||
|
||||
async toggle(): Promise<boolean> {
|
||||
const response = (await commands.playerToggle()) as any;
|
||||
return response?.state === "playing";
|
||||
}
|
||||
|
||||
/**
|
||||
* PRIMITIVE: in-place seek. For the native backend, the backend drives
|
||||
* ExoPlayer's seek internally, so this simply records the target position.
|
||||
* (The decision to seek-in-place vs reload was already made by the backend.)
|
||||
*/
|
||||
async seekElement(positionSeconds: number, _offset: number): Promise<void> {
|
||||
this.position = positionSeconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* PRIMITIVE: reload source. For the native backend the backend already
|
||||
* performed the reload+seek internally as part of the seek decision; nothing
|
||||
* to do on the frontend beyond recording position.
|
||||
*/
|
||||
async reloadSource(_url: string, offset: number): Promise<void> {
|
||||
this.position = offset;
|
||||
}
|
||||
|
||||
setVolume(volume: number): void {
|
||||
void commands.playerSetVolume(Math.max(0, Math.min(1, volume)));
|
||||
}
|
||||
|
||||
setMuted(_muted: boolean): void {
|
||||
void commands.playerToggleMute();
|
||||
}
|
||||
|
||||
async selectSubtitle(streamIndex: number | null, arrayIndex?: number): Promise<void> {
|
||||
const indexToUse = streamIndex === null ? null : arrayIndex ?? streamIndex;
|
||||
await commands.playerSetSubtitleTrack(indexToUse);
|
||||
}
|
||||
|
||||
getPosition(): number {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
async dispose(): Promise<void> {
|
||||
// The backend is stopped via player_stop by the owning view; nothing to free.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* An {@link AdapterHost} implementation that forwards a player adapter's outward
|
||||
* lifecycle events into the Rust `PlayerController` via the `player_report_*`
|
||||
* commands. The controller re-emits the same `PlayerStatusEvent`s the native
|
||||
* backends emit, so the frontend `player` store is fed from ONE pipeline
|
||||
* (playerEvents.ts) in both native and HTML5 modes — keeping Rust the single
|
||||
* source of truth.
|
||||
*
|
||||
* This is the sole place that talks to the report commands; adapters depend only
|
||||
* on the {@link AdapterHost} interface, never on `commands` directly, which keeps
|
||||
* them unit-testable with a mock host.
|
||||
*
|
||||
* TRACES: UR-003, UR-005 | DR-001, DR-028
|
||||
*/
|
||||
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import type { AdapterHost } from "./types";
|
||||
|
||||
const POSITION_REPORT_INTERVAL_MS = 250;
|
||||
|
||||
/** Report options that let a caller bypass throttling for discrete events. */
|
||||
export interface ReportPositionOptions {
|
||||
force?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Low-level report helpers, exported so the legacy `$lib/player/html5Adapter`
|
||||
* shim can keep its function-style API while there are still direct callers.
|
||||
* Prefer {@link createRustReportHost} for new adapter code.
|
||||
*/
|
||||
let lastPositionReport = 0;
|
||||
|
||||
export async function reportState(
|
||||
state: "playing" | "paused" | "loading" | "stopped" | "idle",
|
||||
mediaId: string | null
|
||||
): Promise<void> {
|
||||
try {
|
||||
await commands.playerReportState(state, mediaId);
|
||||
} catch (err) {
|
||||
console.warn("[rustReportHost] Failed to report state:", err);
|
||||
}
|
||||
}
|
||||
|
||||
export async function reportPosition(
|
||||
position: number,
|
||||
duration: number,
|
||||
{ force = false }: ReportPositionOptions = {}
|
||||
): Promise<void> {
|
||||
const now = Date.now();
|
||||
if (!force && now - lastPositionReport < POSITION_REPORT_INTERVAL_MS) {
|
||||
return;
|
||||
}
|
||||
lastPositionReport = now;
|
||||
try {
|
||||
await commands.playerReportPosition(position, Number.isFinite(duration) ? duration : 0);
|
||||
} catch (err) {
|
||||
console.warn("[rustReportHost] Failed to report position:", err);
|
||||
}
|
||||
}
|
||||
|
||||
export async function reportMediaLoaded(duration: number): Promise<void> {
|
||||
try {
|
||||
await commands.playerReportMediaLoaded(Number.isFinite(duration) ? duration : 0);
|
||||
} catch (err) {
|
||||
console.warn("[rustReportHost] Failed to report media loaded:", err);
|
||||
}
|
||||
}
|
||||
|
||||
export function resetReporting(): void {
|
||||
lastPositionReport = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an {@link AdapterHost} bound to a specific media id that forwards adapter
|
||||
* events to Rust. `onStreamUrlChanged`, `onBuffering`, and `onReady` are wired by
|
||||
* the owning view (they affect the `<video src>` / spinner), so this host accepts
|
||||
* optional view callbacks and defaults them to no-ops.
|
||||
*/
|
||||
export function createRustReportHost(
|
||||
mediaId: string,
|
||||
view: Partial<Pick<AdapterHost, "onStreamUrlChanged" | "onBuffering" | "onReady" | "onEnded" | "onError">> = {}
|
||||
): AdapterHost {
|
||||
return {
|
||||
onState: (state) => void reportState(state, mediaId),
|
||||
onPosition: (position, duration) => void reportPosition(position, duration),
|
||||
onMediaLoaded: (duration) => void reportMediaLoaded(duration),
|
||||
onEnded: view.onEnded ?? (() => {}),
|
||||
onError: view.onError ?? ((message) => console.warn("[rustReportHost] adapter error:", message)),
|
||||
onStreamUrlChanged: view.onStreamUrlChanged ?? (() => {}),
|
||||
onBuffering: view.onBuffering ?? (() => {}),
|
||||
onReady: view.onReady ?? (() => {}),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* PlayerAdapter contract — the decoupled boundary between the UI/backend and a
|
||||
* concrete video player implementation (Linux HTML5+hls.js, or Android native).
|
||||
*
|
||||
* The whole point: UI components and the Rust backend interact with video ONLY
|
||||
* through this interface. All element / hls.js / ExoPlayer / textTracks detail —
|
||||
* and the backend seek/audio-track *strategy* round-trip — is internal to an
|
||||
* implementation. A control intent (from UI or a backend lockscreen/remote/sleep
|
||||
* event) reaches the element by the facade dispatching to the active adapter.
|
||||
*
|
||||
* State flows OUTWARD through the {@link AdapterHost} callback bag rather than the
|
||||
* adapter importing stores/commands directly — this keeps adapters unit-testable
|
||||
* with a mock host and keeps the reporting-to-Rust wiring in one place.
|
||||
*
|
||||
* TRACES: UR-003, UR-005, UR-020, UR-021 | DR-001, DR-023, DR-024, DR-028
|
||||
*/
|
||||
|
||||
/** A subtitle track handed to the adapter at load time (WebVTT for HTML5). */
|
||||
export interface SubtitleTrackInput {
|
||||
index: number;
|
||||
url: string;
|
||||
language: string | null;
|
||||
label: string;
|
||||
mimeType: string;
|
||||
}
|
||||
|
||||
/** Everything an adapter needs to load and begin a stream. */
|
||||
export interface PlayerLoadOptions {
|
||||
/** Jellyfin item id — used as the media_id when reporting state to Rust. */
|
||||
mediaId: string;
|
||||
/** Media source id for subtitle/seek URLs (null for local/direct). */
|
||||
mediaSourceId: string | null;
|
||||
/** HEVC/10-bit content that needs server transcoding (affects seek strategy). */
|
||||
needsTranscoding: boolean;
|
||||
/** Resume position in seconds (0 = start from beginning). */
|
||||
initialPosition: number;
|
||||
/** Live stream — no seek bar, no resume, no progress reporting. */
|
||||
isLive: boolean;
|
||||
/** Preselected audio track stream index, or null for the default. */
|
||||
audioTrackIndex: number | null;
|
||||
/** Known total duration in seconds (from runTimeTicks), or 0 if unknown. */
|
||||
knownDuration: number;
|
||||
/** Subtitle tracks available for this media. */
|
||||
subtitleTracks: SubtitleTrackInput[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback bag the adapter uses to report the element's lifecycle outward. The
|
||||
* facade supplies an implementation that forwards to Rust (via the
|
||||
* `player_report_*` commands) and, where needed, to the UI.
|
||||
*/
|
||||
export interface AdapterHost {
|
||||
/** Playback state changed (playing/paused/loading/stopped/idle). */
|
||||
onState(state: "playing" | "paused" | "loading" | "stopped" | "idle"): void;
|
||||
/** Position/duration tick (adapter throttles; host forwards to Rust). */
|
||||
onPosition(position: number, duration: number): void;
|
||||
/** Media finished loading and knows its duration. */
|
||||
onMediaLoaded(duration: number): void;
|
||||
/** Playback reached the natural end of the stream (fires at most once). */
|
||||
onEnded(): void;
|
||||
/** A non-fatal or fatal playback error occurred. */
|
||||
onError(message: string): void;
|
||||
/**
|
||||
* The stream URL the adapter is now playing changed (e.g. transcode reload on
|
||||
* seek/audio-track switch). Lets the owning view keep its `<video src>` in sync.
|
||||
*/
|
||||
onStreamUrlChanged(url: string): void;
|
||||
/** Buffering/ready transitions, so the view can show/hide its spinner. */
|
||||
onBuffering(isBuffering: boolean): void;
|
||||
onReady(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* One concrete player implementation per platform. Methods are high-level
|
||||
* intents; strategy objects, hls instances, and textTracks never cross this line.
|
||||
*/
|
||||
export interface PlayerAdapter {
|
||||
/** Which platform backend this adapter represents. */
|
||||
readonly kind: "html5" | "native";
|
||||
|
||||
/**
|
||||
* Bind the output target. For the HTML5 adapter this is the `<video>` element
|
||||
* (pass null on teardown); the native adapter ignores it (ExoPlayer renders to
|
||||
* its own surface).
|
||||
*/
|
||||
attach(element: HTMLVideoElement | null): void;
|
||||
|
||||
/** Load a stream and begin playback at `options.initialPosition`. */
|
||||
load(streamUrl: string, options: PlayerLoadOptions): Promise<void>;
|
||||
|
||||
play(): Promise<void>;
|
||||
pause(): Promise<void>;
|
||||
/** Toggle play/pause; resolves to the resulting playing state. */
|
||||
toggle(): Promise<boolean>;
|
||||
|
||||
// --- Seek/reload PRIMITIVES (decision-free) ---------------------------------
|
||||
// The backend DECIDES whether a seek is an in-place element seek or a full
|
||||
// source reload (transcode). The adapter only executes the chosen primitive;
|
||||
// it contains no strategy branch. This is what keeps the decision logic shared
|
||||
// in Rust (Option 1).
|
||||
|
||||
/**
|
||||
* In-place seek of the already-loaded source (no reload). `offset` is the
|
||||
* transcode seek offset the element position is relative to (0 for direct).
|
||||
*/
|
||||
seekElement(positionSeconds: number, offset: number): Promise<void>;
|
||||
|
||||
/**
|
||||
* Compound reload: swap to `url` and resume at `offset` seconds. Runs the
|
||||
* invariant mechanical sequence for this platform (html5: pause → hls teardown
|
||||
* → clear src → set new url → wait ready → resume; native: ExoPlayer setMediaItem
|
||||
* + seekTo). No decision is made here — the backend already decided to reload.
|
||||
*/
|
||||
reloadSource(url: string, offset: number): Promise<void>;
|
||||
|
||||
setVolume(volume: number): void; // 0..1
|
||||
setMuted(muted: boolean): void;
|
||||
|
||||
/** Enable a subtitle track (null disables) — DOM textTracks is a webview primitive. */
|
||||
selectSubtitle(streamIndex: number | null, arrayIndex?: number): Promise<void>;
|
||||
|
||||
/** Current position in seconds (adapter's own truth, e.g. element.currentTime + offset). */
|
||||
getPosition(): number;
|
||||
|
||||
/** Tear down: destroy hls, detach element, stop reporting. Idempotent. */
|
||||
dispose(): Promise<void>;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Compatibility shim.
|
||||
*
|
||||
* The HTML5 → Rust reporting functions moved to `adapters/rustReportHost.ts` as
|
||||
* part of the PlayerAdapter refactor. Existing callers import the reporter as
|
||||
* `import * as html5Adapter from "$lib/player/html5Adapter"`; this shim keeps
|
||||
* that working while the migration proceeds. New adapter code should depend on
|
||||
* the `AdapterHost` interface (see `adapters/types.ts`) instead.
|
||||
*/
|
||||
|
||||
export {
|
||||
reportState,
|
||||
reportPosition,
|
||||
reportMediaLoaded,
|
||||
resetReporting,
|
||||
} from "./adapters/rustReportHost";
|
||||
|
||||
/** @deprecated states are defined on the AdapterHost interface now. */
|
||||
export type Html5PlayerState = "playing" | "paused" | "loading" | "stopped" | "idle";
|
||||
@@ -0,0 +1,334 @@
|
||||
/**
|
||||
* Unified frontend player API (the boundary).
|
||||
*
|
||||
* This is the single write-side entry point for playback. Every UI component
|
||||
* that wants to *control* the player calls a method here; nothing else should
|
||||
* invoke `commands.player*` directly. The Rust `PlayerController` remains the
|
||||
* single source of truth — these methods only send intent-level commands and
|
||||
* let state flow back through `PlayerStatusEvent` → `playerEvents.ts` → the
|
||||
* `player`/`queue` stores.
|
||||
*
|
||||
* Reads stay on the established stores: this module re-exports the read-only
|
||||
* derived + merged (remote-session-aware) stores so UI can import state and
|
||||
* actions from one place, in both local and remote modes.
|
||||
*
|
||||
* TRACES: UR-005 | DR-001, DR-009
|
||||
*/
|
||||
|
||||
import { get } from "svelte/store";
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import type {
|
||||
PlayTracksContext,
|
||||
PlayAlbumTrackRequest,
|
||||
PlayItemRequest,
|
||||
} from "$lib/api/bindings";
|
||||
import { auth } from "$lib/stores/auth";
|
||||
import type { PlayerAdapter } from "./adapters/types";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Active player adapter registry
|
||||
//
|
||||
// When a video is playing, VideoPlayer registers its PlayerAdapter here so that
|
||||
// control intents — whether from UI or routed from a backend control event
|
||||
// (lockscreen/remote/sleep-timer) — reach the actual player element/surface.
|
||||
// When no adapter is registered (audio-only playback), control falls through to
|
||||
// the queue-level backend commands, which is the correct behavior there.
|
||||
// ---------------------------------------------------------------------------
|
||||
let activeAdapter: PlayerAdapter | null = null;
|
||||
|
||||
function setActiveAdapter(adapter: PlayerAdapter): void {
|
||||
activeAdapter = adapter;
|
||||
}
|
||||
|
||||
function clearActiveAdapter(adapter?: PlayerAdapter): void {
|
||||
// Only clear if it's still the one we think is active (guards against a newly
|
||||
// mounted player's adapter being cleared by the outgoing player's teardown).
|
||||
if (!adapter || activeAdapter === adapter) {
|
||||
activeAdapter = null;
|
||||
}
|
||||
}
|
||||
|
||||
function getActiveAdapter(): PlayerAdapter | null {
|
||||
return activeAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the current repository handle, throwing a clear error if the user is
|
||||
* not authenticated. Centralizes the `auth.getRepository().getHandle()` dance
|
||||
* that was previously duplicated across every context-play call site.
|
||||
*/
|
||||
function requireHandle(): string {
|
||||
// The repository is the source of truth for the handle. We consult the auth
|
||||
// store's isAuthenticated flag only as a best-effort guard — guarded in a
|
||||
// try/catch so a not-yet-subscribable store (or a test double) can't block a
|
||||
// valid repository handle.
|
||||
try {
|
||||
const authState = get(auth);
|
||||
if (authState && authState.isAuthenticated === false) {
|
||||
throw new Error("User not authenticated");
|
||||
}
|
||||
} catch (err) {
|
||||
// get(auth) failed (e.g. non-store mock) — fall through to the repository,
|
||||
// which is the authoritative source of the handle.
|
||||
if (err instanceof Error && err.message === "User not authenticated") throw err;
|
||||
}
|
||||
const repo = auth.getRepository();
|
||||
if (!repo) {
|
||||
throw new Error("No repository available");
|
||||
}
|
||||
return repo.getHandle();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Transport controls (no repository handle required)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function play() {
|
||||
if (activeAdapter) return void (await activeAdapter.play());
|
||||
await commands.playerPlay();
|
||||
}
|
||||
|
||||
async function pause() {
|
||||
if (activeAdapter) return void (await activeAdapter.pause());
|
||||
await commands.playerPause();
|
||||
}
|
||||
|
||||
async function toggle() {
|
||||
if (activeAdapter) return void (await activeAdapter.toggle());
|
||||
await commands.playerToggle();
|
||||
}
|
||||
|
||||
async function stop() {
|
||||
// Stop is a queue/session-level action (clears playback); always go to backend.
|
||||
// The adapter is disposed by VideoPlayer's own teardown.
|
||||
await commands.playerStop();
|
||||
}
|
||||
|
||||
async function seek(positionSeconds: number) {
|
||||
// Audio path: backend seeks the native backend directly.
|
||||
if (!activeAdapter) {
|
||||
await commands.playerSeek(positionSeconds);
|
||||
return;
|
||||
}
|
||||
// Video path: ask the backend to DECIDE the strategy (in-place vs reload), then
|
||||
// execute the matching adapter primitive. The decision logic stays in Rust
|
||||
// (player_seek_video); the adapter only runs the chosen mechanical primitive.
|
||||
await seekVideo(positionSeconds, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Video seek: backend decides strategy, facade dispatches the chosen adapter
|
||||
* primitive. `mediaSourceId`/`audioTrackIndex` come from the video view (they are
|
||||
* needed for the transcode reload URL). Requires an active video adapter.
|
||||
*/
|
||||
async function seekVideo(
|
||||
positionSeconds: number,
|
||||
mediaSourceId: string | null,
|
||||
audioTrackIndex: number | null
|
||||
): Promise<void> {
|
||||
const adapter = activeAdapter;
|
||||
if (!adapter) {
|
||||
await commands.playerSeek(positionSeconds);
|
||||
return;
|
||||
}
|
||||
const response = (await commands.playerSeekVideo(
|
||||
requireHandle(),
|
||||
positionSeconds,
|
||||
mediaSourceId,
|
||||
audioTrackIndex,
|
||||
adapter.kind === "html5"
|
||||
)) as any;
|
||||
// Serde keeps these snake_case (only the "strategy" tag is camelCase).
|
||||
if (response.strategy === "reloadStream") {
|
||||
await adapter.reloadSource(response.new_url ?? "", response.seek_offset ?? positionSeconds);
|
||||
} else {
|
||||
await adapter.seekElement(response.position ?? positionSeconds, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch audio track: backend decides (may reload the stream), facade dispatches
|
||||
* the resulting primitive. Requires an active video adapter.
|
||||
*/
|
||||
async function switchAudioTrack(
|
||||
streamIndex: number,
|
||||
arrayIndex: number,
|
||||
currentPosition: number | null,
|
||||
mediaSourceId: string | null
|
||||
): Promise<void> {
|
||||
const adapter = activeAdapter;
|
||||
if (!adapter) return;
|
||||
const response = (await commands.playerSwitchAudioTrack(
|
||||
requireHandle(),
|
||||
streamIndex,
|
||||
arrayIndex,
|
||||
adapter.kind === "html5",
|
||||
currentPosition,
|
||||
mediaSourceId
|
||||
)) as any;
|
||||
if (response.strategy === "reloadStream") {
|
||||
await adapter.reloadSource(response.new_url!, response.position!);
|
||||
}
|
||||
}
|
||||
|
||||
async function next() {
|
||||
await commands.playerNext();
|
||||
}
|
||||
|
||||
async function previous() {
|
||||
await commands.playerPrevious();
|
||||
}
|
||||
|
||||
async function skipTo(index: number) {
|
||||
await commands.playerSkipTo(index);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Queue mode controls
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function toggleShuffle() {
|
||||
await commands.playerToggleShuffle();
|
||||
}
|
||||
|
||||
async function cycleRepeat() {
|
||||
await commands.playerCycleRepeat();
|
||||
}
|
||||
|
||||
async function removeFromQueue(index: number) {
|
||||
await commands.playerRemoveFromQueue(index);
|
||||
}
|
||||
|
||||
async function moveInQueue(fromIndex: number, toIndex: number) {
|
||||
await commands.playerMoveInQueue(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Volume
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function setVolume(volume: number) {
|
||||
if (activeAdapter) activeAdapter.setVolume(volume);
|
||||
await commands.playerSetVolume(volume);
|
||||
}
|
||||
|
||||
async function toggleMute() {
|
||||
await commands.playerToggleMute();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Track selection (video) — dispatch to the active video adapter when present
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function setSubtitleTrack(streamIndex: number | null) {
|
||||
if (activeAdapter) return void (await activeAdapter.selectSubtitle(streamIndex));
|
||||
await commands.playerSetSubtitleTrack(streamIndex);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Context-aware playback (repository handle required — resolved internally)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Play a set of tracks by ID with an explicit queue context. The backend
|
||||
* fetches all metadata and builds the queue; the frontend queue store updates
|
||||
* from the resulting `queue_changed` event.
|
||||
*/
|
||||
async function playTracks(request: {
|
||||
trackIds: string[];
|
||||
startIndex: number;
|
||||
shuffle: boolean;
|
||||
context: PlayTracksContext;
|
||||
startPosition?: number;
|
||||
}) {
|
||||
await commands.playerPlayTracks(requireHandle(), request);
|
||||
}
|
||||
|
||||
/** Play a single track within its album context (more efficient than playTracks). */
|
||||
async function playAlbumTrack(request: PlayAlbumTrackRequest) {
|
||||
await commands.playerPlayAlbumTrack(requireHandle(), request);
|
||||
}
|
||||
|
||||
/** Play a single explicit media item (used by the video path). */
|
||||
async function playItem(request: PlayItemRequest) {
|
||||
return commands.playerPlayItem(request);
|
||||
}
|
||||
|
||||
/** Add a single track to the queue by ID. */
|
||||
async function addTrackById(trackId: string, position: "next" | "end" = "end") {
|
||||
await commands.playerAddTrackById(requireHandle(), { trackId, position });
|
||||
}
|
||||
|
||||
/** Add multiple tracks to the queue by ID. */
|
||||
async function addTracksByIds(
|
||||
trackIds: string[],
|
||||
position: "next" | "end" = "end"
|
||||
) {
|
||||
await commands.playerAddTracksByIds(requireHandle(), { trackIds, position });
|
||||
}
|
||||
|
||||
/**
|
||||
* The unified player facade. Import this and call its methods instead of
|
||||
* reaching for `commands.player*` in UI code.
|
||||
*/
|
||||
export const playerController = {
|
||||
play,
|
||||
pause,
|
||||
toggle,
|
||||
stop,
|
||||
seek,
|
||||
next,
|
||||
previous,
|
||||
skipTo,
|
||||
toggleShuffle,
|
||||
cycleRepeat,
|
||||
removeFromQueue,
|
||||
moveInQueue,
|
||||
setVolume,
|
||||
toggleMute,
|
||||
setSubtitleTrack,
|
||||
seekVideo,
|
||||
switchAudioTrack,
|
||||
playTracks,
|
||||
playAlbumTrack,
|
||||
playItem,
|
||||
addTrackById,
|
||||
addTracksByIds,
|
||||
// Active-adapter registry (used by VideoPlayer to register its element adapter
|
||||
// and by playerEvents.ts to route backend control commands to it).
|
||||
setActiveAdapter,
|
||||
clearActiveAdapter,
|
||||
getActiveAdapter,
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Read-side re-exports: UI reads state from ONE place, in both local & remote
|
||||
// modes. These remain the single source of truth fed by playerEvents.ts.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
playerState,
|
||||
currentMedia,
|
||||
isPlaying,
|
||||
isPaused,
|
||||
isLoading,
|
||||
playbackPosition,
|
||||
playbackDuration,
|
||||
volume,
|
||||
isMuted,
|
||||
mergedMedia,
|
||||
mergedIsPlaying,
|
||||
mergedPosition,
|
||||
mergedDuration,
|
||||
mergedVolume,
|
||||
} from "$lib/stores/player";
|
||||
|
||||
export {
|
||||
queueItems,
|
||||
currentQueueIndex,
|
||||
currentQueueItem,
|
||||
isShuffle,
|
||||
repeatMode,
|
||||
hasNext,
|
||||
hasPrevious,
|
||||
} from "$lib/stores/queue";
|
||||
@@ -17,6 +17,7 @@ import { sleepTimer, sleepTimerExpiredSignal } from "$lib/stores/sleepTimer";
|
||||
import { nextEpisode, nextEpisodeItem as nextEpisodeItemStore } from "$lib/stores/nextEpisode";
|
||||
import { autoPlayNext } from "$lib/services/nextEpisodeService";
|
||||
import { preloadUpcomingTracks } from "$lib/services/preload";
|
||||
import { playerController } from "$lib/player";
|
||||
import type { MediaItem } from "$lib/api/types";
|
||||
import { get } from "svelte/store";
|
||||
|
||||
@@ -116,9 +117,20 @@ function handlePlayerEvent(event: PlayerStatusEvent): void {
|
||||
case "sleep_timer_expired":
|
||||
// Backend stops its own playback; this signal lets HTML5 video (which
|
||||
// plays outside the backend on Linux) pause itself too.
|
||||
// Preferred path: drive the active video adapter directly so the backend
|
||||
// has real control authority over the webview element. The legacy
|
||||
// sleepTimerExpiredSignal is kept for any remaining subscribers.
|
||||
playerController.getActiveAdapter()?.pause();
|
||||
sleepTimerExpiredSignal.update((n) => n + 1);
|
||||
break;
|
||||
|
||||
case "control_command":
|
||||
// Backend-originated control targeting the active frontend player adapter
|
||||
// (lockscreen/remote/sleep). Route it to the adapter so a backend intent
|
||||
// reaches the webview <video> element.
|
||||
handleControlCommand(event.action, event.position);
|
||||
break;
|
||||
|
||||
case "show_next_episode_popup":
|
||||
handleShowNextEpisodePopup(
|
||||
event.current_episode,
|
||||
@@ -292,6 +304,36 @@ function handleSleepTimerChanged(mode: SleepTimerMode, remainingSeconds: number)
|
||||
sleepTimer.set({ mode, remainingSeconds });
|
||||
}
|
||||
|
||||
/**
|
||||
* Route a backend-originated control command to the active player adapter, so a
|
||||
* backend intent (lockscreen/remote/sleep) can drive the webview <video> element
|
||||
* that Rust cannot reach directly. No-op when no video adapter is active (audio
|
||||
* playback is already fully backend-driven).
|
||||
*/
|
||||
function handleControlCommand(action: string, position: number | null): void {
|
||||
const adapter = playerController.getActiveAdapter();
|
||||
if (!adapter) return;
|
||||
switch (action) {
|
||||
case "play":
|
||||
void adapter.play();
|
||||
break;
|
||||
case "pause":
|
||||
void adapter.pause();
|
||||
break;
|
||||
case "seek":
|
||||
// Backend-driven in-place seek (e.g. lockscreen scrub). The backend has
|
||||
// already decided this is a simple position change, so use the element
|
||||
// seek primitive with no transcode offset.
|
||||
if (position != null) void adapter.seekElement(position, 0);
|
||||
break;
|
||||
case "stop":
|
||||
void adapter.pause();
|
||||
break;
|
||||
default:
|
||||
console.warn("[playerEvents] Unknown control command:", action);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle show next episode popup event.
|
||||
*
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
reportPlaybackStopped,
|
||||
} from "$lib/services/playbackReporting";
|
||||
import { cleanup as cleanupNextEpisode } from "$lib/services/nextEpisodeService";
|
||||
import * as html5Adapter from "$lib/player/html5Adapter";
|
||||
|
||||
const itemId = $derived($page.params.id);
|
||||
const queueParam = $derived($page.url.searchParams.get("queue"));
|
||||
@@ -450,7 +451,10 @@
|
||||
savedProgress = null;
|
||||
const id = itemId;
|
||||
if (id) {
|
||||
loadAndPlay(id, 0);
|
||||
// forceRestart bypasses the resume-progress check; without it, passing a
|
||||
// start position of 0 is treated as "no position" (`!startPosition`), which
|
||||
// re-runs the resume check and re-shows this very dialog in a loop.
|
||||
loadAndPlay(id, 0, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -514,6 +518,11 @@
|
||||
if (id) {
|
||||
reportPlaybackStart(id, positionSeconds, context.type, context.id);
|
||||
}
|
||||
// Mirror HTML5 <video> state into the Rust PlayerController so it is the
|
||||
// single source of truth for video playback (see html5Adapter.ts). The
|
||||
// element lives in the webview and Rust cannot observe it directly.
|
||||
html5Adapter.reportState("playing", id ?? null);
|
||||
html5Adapter.reportPosition(positionSeconds, get(playbackDuration), { force: true });
|
||||
}
|
||||
|
||||
function handleReportProgress(positionSeconds: number, isPaused: boolean, reportId?: string) {
|
||||
@@ -521,6 +530,9 @@
|
||||
if (id) {
|
||||
reportPlaybackProgress(id, positionSeconds, isPaused);
|
||||
}
|
||||
// Feed the Rust controller the current position and play/pause state.
|
||||
html5Adapter.reportState(isPaused ? "paused" : "playing", id ?? null);
|
||||
html5Adapter.reportPosition(positionSeconds, get(playbackDuration), { force: true });
|
||||
}
|
||||
|
||||
function handleReportStop(positionSeconds: number, reportId?: string) {
|
||||
@@ -528,6 +540,12 @@
|
||||
if (id) {
|
||||
reportPlaybackStopped(id, positionSeconds);
|
||||
}
|
||||
// Intentionally do NOT emit a "stopped" player state here. This runs on both
|
||||
// natural end-of-video (an autoplay handoff the backend's on_video_playback_ended
|
||||
// owns) and on player close/unmount (where player_stop already drives the
|
||||
// backend state). Emitting StateChanged{stopped} on natural end flips the
|
||||
// player/mode to idle mid-handoff and suppresses next-episode auto-advance —
|
||||
// the "sleep timer pauses at the end of an episode instead of continuing" bug.
|
||||
}
|
||||
|
||||
async function handleVideoEnded() {
|
||||
|
||||