Compare commits
10
Commits
7545de6cc7
...
20e683d705
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20e683d705 | ||
|
|
8904acb5f7 | ||
|
|
a3190cd52b | ||
|
|
f4892f4cb2 | ||
|
|
3b91922cca | ||
|
|
f388777185 | ||
|
|
14b6a8609d | ||
|
|
d3ecd8ee91 | ||
|
|
2f637d4775 | ||
|
|
45144cb6b0 |
@@ -90,6 +90,7 @@ For a narrative overview of the system design, see
|
|||||||
| UR-078 | JellyTau keeps a record of what it did, and can hand it over. The app forgot everything the moment it exited: the backend logged to stdout only — which a user launching from a desktop icon never sees, and which on Android is not logcat, so the Rust half was invisible on the platform carrying the hardest bugs. A crash left nothing at all. Logs are now written to a size-capped rotating file, a panic is recorded before the process dies, the frontend's messages land in the same timeline as the backend's, and Settings exports the lot as one file to attach to a bug report. Nothing is transmitted anywhere — the user attaches it themselves, which is also what keeps this from being telemetry. Access tokens and passwords never reach the file | Medium | Done |
|
| UR-078 | JellyTau keeps a record of what it did, and can hand it over. The app forgot everything the moment it exited: the backend logged to stdout only — which a user launching from a desktop icon never sees, and which on Android is not logcat, so the Rust half was invisible on the platform carrying the hardest bugs. A crash left nothing at all. Logs are now written to a size-capped rotating file, a panic is recorded before the process dies, the frontend's messages land in the same timeline as the backend's, and Settings exports the lot as one file to attach to a bug report. Nothing is transmitted anywhere — the user attaches it themselves, which is also what keeps this from being telemetry. Access tokens and passwords never reach the file | Medium | Done |
|
||||||
| UR-079 | The app decides *what stream to play* and says so. Playing a video used to mean asking the server to re-encode it, always — a decision made nowhere, written down nowhere, and re-derived downstream by whoever needed it: the player worked out whether it had been handed a playlist by looking for `.m3u8` in the URL. So a viewer paid for a transcode of a file their device could have played untouched, and the app could not tell them which it was. Now one negotiation produces one self-describing answer — direct play, remux, or transcode; over a playlist, a plain HTTP file, or a local one — and every renderer consumes that same answer instead of guessing from a string. On Android, where the player decodes almost everything the library holds, this stops around 85% of plays from starting a transcode nobody needed | Medium | Done |
|
| UR-079 | The app decides *what stream to play* and says so. Playing a video used to mean asking the server to re-encode it, always — a decision made nowhere, written down nowhere, and re-derived downstream by whoever needed it: the player worked out whether it had been handed a playlist by looking for `.m3u8` in the URL. So a viewer paid for a transcode of a file their device could have played untouched, and the app could not tell them which it was. Now one negotiation produces one self-describing answer — direct play, remux, or transcode; over a playlist, a plain HTTP file, or a local one — and every renderer consumes that same answer instead of guessing from a string. On Android, where the player decodes almost everything the library holds, this stops around 85% of plays from starting a transcode nobody needed | Medium | Done |
|
||||||
| UR-080 | Video on the desktop plays as itself. The picture was drawn by a webview `<video>` element, which decodes little beyond h264 — so the app told the server it could accept only h264, and the server re-encoded almost everything before sending it. That was never a statement about the machine: the same machine already runs mpv for audio, which decodes essentially the whole library. Measured against a real library, 93% of desktop playback was a transcode nobody needed, against 15% on Android where a real decoder does the work. mpv now draws the picture, the app claims what it can genuinely decode, and video is sent as it was stored wherever that is possible — sparing the server the work, the network the bitrate, and the picture a generation of re-encoding | Medium | Proposed |
|
| UR-080 | Video on the desktop plays as itself. The picture was drawn by a webview `<video>` element, which decodes little beyond h264 — so the app told the server it could accept only h264, and the server re-encoded almost everything before sending it. That was never a statement about the machine: the same machine already runs mpv for audio, which decodes essentially the whole library. Measured against a real library, 93% of desktop playback was a transcode nobody needed, against 15% on Android where a real decoder does the work. mpv now draws the picture, the app claims what it can genuinely decode, and video is sent as it was stored wherever that is possible — sparing the server the work, the network the bitrate, and the picture a generation of re-encoding | Medium | Proposed |
|
||||||
|
| UR-081 | Playback behaves the same whichever engine renders it | High | In Progress |
|
||||||
| UR-074 | Video streaming can be held to a **bandwidth budget the viewer sets**, rather than spent at whatever rate the server would otherwise send. A ceiling chosen once — from the source's own bitrate down to a rung that still plays on a poor connection — governs every video the app opens, live TV included, and survives a restart, so a metered connection is not quietly drained by the next thing played. A single video can be moved to a different ceiling from the player, resuming where it was, without disturbing that default | Medium | Done |
|
| UR-074 | Video streaming can be held to a **bandwidth budget the viewer sets**, rather than spent at whatever rate the server would otherwise send. A ceiling chosen once — from the source's own bitrate down to a rung that still plays on a poor connection — governs every video the app opens, live TV included, and survives a restart, so a metered connection is not quietly drained by the next thing played. A single video can be moved to a different ceiling from the player, resuming where it was, without disturbing that default | Medium | Done |
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -432,6 +433,15 @@ Internal architecture, components, and application logic.
|
|||||||
| DR-235 | The webview video path is deleted, not merely bypassed. Staged, because a path cannot be removed while a shipped platform still needs it: Linux moves to mpv first, Windows follows, and only then do `hls.js`, `html5Adapter.ts`, `videoLoaderFor` and the `<video>` element go. The staging is the point — a Linux-only version would leave the fork alive permanently, taking video from three renderers to four and giving every seek strategy, track switch and lifecycle bug one more place to be got right. Android keeps ExoPlayer and keeps the webview as its documented opt-out; the background-audio `<audio>` path is untouched. With no HTML5 fallback left, a failed mpv init emits `backend-init-failed` and surfaces a real error rather than silently degrading to the transcode this work exists to stop paying for | Playback | UR-080 | Proposed |
|
| DR-235 | The webview video path is deleted, not merely bypassed. Staged, because a path cannot be removed while a shipped platform still needs it: Linux moves to mpv first, Windows follows, and only then do `hls.js`, `html5Adapter.ts`, `videoLoaderFor` and the `<video>` element go. The staging is the point — a Linux-only version would leave the fork alive permanently, taking video from three renderers to four and giving every seek strategy, track switch and lifecycle bug one more place to be got right. Android keeps ExoPlayer and keeps the webview as its documented opt-out; the background-audio `<audio>` path is untouched. With no HTML5 fallback left, a failed mpv init emits `backend-init-failed` and surfaces a real error rather than silently degrading to the transcode this work exists to stop paying for | Playback | UR-080 | Proposed |
|
||||||
| DR-236 | Hardware-decode policy is decided from what mpv reports it **selected** (`hwdec-current`), never from what it was asked for. The spike established that hardware decode works through the render API at all — the load-bearing result, since it means direct play is not bought with software decoding — but also that `auto` reached for the discrete GPU in copy-back mode on a hybrid Intel+NVIDIA laptop, the least efficient hardware path, and that `vaapi` fell back to software silently because the libva driver was absent. So zero-copy VA-API on the integrated GPU is preferred where the driver is present, `auto` is a fallback rather than the default, and a missing driver is detected and logged rather than mistaken for a compositing limit | Playback | UR-080 | Proposed |
|
| DR-236 | Hardware-decode policy is decided from what mpv reports it **selected** (`hwdec-current`), never from what it was asked for. The spike established that hardware decode works through the render API at all — the load-bearing result, since it means direct play is not bought with software decoding — but also that `auto` reached for the discrete GPU in copy-back mode on a hybrid Intel+NVIDIA laptop, the least efficient hardware path, and that `vaapi` fell back to software silently because the libva driver was absent. So zero-copy VA-API on the integrated GPU is preferred where the driver is present, `auto` is a fallback rather than the default, and a missing driver is detected and logged rather than mistaken for a compositing limit | Playback | UR-080 | Proposed |
|
||||||
| DR-237 | Windows reaches the same mpv path, reusing everything except the surface. The surface is genuinely different code — a native child window beneath a transparent WebView2, not GTK — but the render context, lifetime discipline, frame pacing, device profile and hwdec policy are shared, which is why none of them may be guarded on `cfg!(target_os = "linux")`. The cost is mostly build, not video: `libmpv` is currently a Linux-only dependency while Windows is cross-compiled from Linux via `x86_64-pc-windows-msvc` + `cargo-xwin`, so a Windows libmpv must reach that cross-build and its DLL must ship in the NSIS bundle, carrying the LGPL obligations DR-216 already records — dynamic linkage, licence text shipped alongside. Windows gains a native audio decoder as a side effect, which is what the long-blocked Windows audio work wants and cannot otherwise have | Playback | UR-080 | Proposed |
|
| DR-237 | Windows reaches the same mpv path, reusing everything except the surface. The surface is genuinely different code — a native child window beneath a transparent WebView2, not GTK — but the render context, lifetime discipline, frame pacing, device profile and hwdec policy are shared, which is why none of them may be guarded on `cfg!(target_os = "linux")`. The cost is mostly build, not video: `libmpv` is currently a Linux-only dependency while Windows is cross-compiled from Linux via `x86_64-pc-windows-msvc` + `cargo-xwin`, so a Windows libmpv must reach that cross-build and its DLL must ship in the NSIS bundle, carrying the LGPL obligations DR-216 already records — dynamic linkage, licence text shipped alongside. Windows gains a native audio decoder as a side effect, which is what the long-blocked Windows audio work wants and cannot otherwise have | Playback | UR-080 | Proposed |
|
||||||
|
| DR-238 | A transcoded seek re-negotiates the stream on every renderer, not just the webview. Jellyfin produces a transcode *from* `StartTimeTicks`, so where a seek lands is a property of the request rather than of the stream in hand. `determine_video_seek_strategy` treated `is_hls` as a proxy for "seekable in place", which held only because hls.js was always the HLS renderer — it seeks within the VOD playlist it is handed and lets the server catch up. mpv's HLS demuxer cannot make the server transcode from a new offset, so with native video on, every transcoded seek became a backend seek that silently did nothing and presented as "resume does not work". The rule is now written on `needs_transcoding` with hls.js as the stated exception; all four webview cells are unchanged | Player | UR-040 | Done |
|
||||||
|
| DR-239 | Properties the mpv event loop handles are registered with `observe_property`. libmpv delivers `PropertyChange` only for observed properties, so a `match` arm for an unobserved one is unreachable code that reads as implemented — the handler is right there. `pause` was handled and never observed, so `StateChanged` was never emitted on pause or resume and the play/pause control never moved. It stayed invisible while Linux video played in the webview, because the `<video>` element's own DOM events drove that control; native video made the UI depend on the event that never came | Player | UR-005 | Done |
|
||||||
|
| DR-240 | Fullscreen moves whatever actually owns the pixels. `requestFullscreen()` fullscreens the *document*, which sufficed while every renderer lived inside it — the HTML5 `<video>` element is part of the document, so WebKit scaled it and the OS window's real size never mattered. A native surface is drawn behind the webview at **window** size, so a document-only fullscreen expands the page and leaves the picture where it was; on WebKitGTK the result is a maximised window with decorations still holding a strip of the screen, which reads as "fullscreen is broken" rather than as a windowing problem. Android needed the same rule for the system bars (DR-157); this is its desktop half | Player | UR-066 | Done |
|
||||||
|
| DR-241 | A seek issued before MPV has a file to seek in is honoured, not dropped. `loadfile` returns as soon as the command is queued, so `time-pos` — a live property of the *loaded* file — does not resolve yet and setting it fails. The two callers that always hit that window are the ones a viewer notices: resume, and a transcoded seek, both of which re-open the stream and then ask for a position. The failed seek was discarded and the stream played from zero, which reads as "resume is broken" and "I cannot skip". The position is now held and applied by the `FileLoaded` handler; a seek that lands normally clears any deferred one, so the newer intent wins | Player | UR-040, UR-005 | Done |
|
||||||
|
| DR-242 | The player contract expresses intent, not device operations. `MediaPlayer::open` carries the start position, so no caller sequences load-then-seek and none can race an engine's asynchronous load; `seek` states a destination and leaves in-place-vs-re-open to the engine, which is the only layer that knows its own transport; `snapshot` is one coherent read; and `Phase::Opening` names the window a seek used to be lost in. Replaces `PlayerBackend`, which abstracted a device and required each of the three engines to re-derive the same rules | Player | UR-081 | In Progress |
|
||||||
|
| DR-243 | Every engine passes one conformance suite, and a `FakePlayer` implements the contract deterministically. The suite is written before the second engine so it cannot encode whatever the first happened to do, and it drives readiness through a harness rather than sleeping. `FakePlayer` models the one behaviour that matters — opening is not instantaneous — so the load/seek race can be expressed on purpose, and lets the controller, queue, autoplay and session logic be tested with no engine at all | Player | UR-081 | In Progress |
|
||||||
|
| DR-244 | `MpvPlayer` implements `MediaPlayer` over libmpv, applying the start position at load time via mpv's own `start` option rather than seeking after an asynchronous `loadfile`, and holding a seek that arrives during `Opening` until the file loads. A standalone `player-conformance` binary runs the suite against it with audio and video routed to null, so a wrapper is verifiable without building or launching the app | Player | UR-081, UR-040 | Done |
|
||||||
|
| DR-245 | `LegacyPlayer` drives the old `PlayerBackend` through the `MediaPlayer` contract, so engines not yet ported keep working during the migration and the two designs can be compared on one engine and one file. It reproduces the old load-then-play-then-seek sequence faithfully rather than a fixed-up version, because making it pass would defeat its purpose | Player | UR-081 | In Progress |
|
||||||
|
| DR-247 | ExoPlayer can be told where to start. `JellyTauPlayer.load(url, mediaId)` had no way to express a start position, so every caller loaded and then seeked; the position is now handed to ExoPlayer with the media item via `setMediaItem(item, startPositionMs)`, and the two-argument form delegates to it. Running the conformance cases on a device also settled which half of DR-241 was engine-specific: ExoPlayer already queues a seek issued before `prepare()` completes, so it never had the lost-seek defect mpv did — only the missing vocabulary for a start position | Player | UR-081, UR-005 | Done |
|
||||||
| DR-198 | The webview runs under a real Content-Security-Policy, and the asset protocol is scoped to the one directory it still serves. `csp` was `null`, which disables CSP entirely: any script that reached the web layer — through a future `{@html}`, a dependency, or a devtools paste — would have inherited the whole IPC surface, and with it the user's session. `script-src 'self'` (Tauri injects a nonce for SvelteKit's inline bootstrap script at build time, so no `'unsafe-inline'` is needed) plus `object-src`/`frame-src 'none'` and `base-uri 'self'` is the part that is genuinely restrictive. `img-src`/`media-src`/`connect-src` cannot be: the Jellyfin origin is typed in by the user at run time and is commonly plain `http` on a LAN, so they allow `http:`/`https:` — a wide grant for *data*, but one that still bars `file:`, `filesystem:` and scripting schemes, and leaves `script-src` untouched. `style-src` keeps `'unsafe-inline'` because Svelte compiles `style="…"` attributes (including `app.html`'s `display: contents` wrapper) into markup; this is safe only while no `<style>` element survives into `index.html`, since a nonce there would make Tauri's injection outrank — and therefore void — `'unsafe-inline'`. `worker-src blob:` and `media-src blob:` are hls.js: it demuxes in a worker built from a blob and attaches MSE through `URL.createObjectURL`. `asset:` and `http://asset.localhost` are the same protocol under the two naming schemes `convertFileSrc` emits (custom scheme on Linux/macOS, `http` host on Windows/Android); `ipc:`/`http://ipc.localhost` is the invoke transport, which would otherwise be blocked by `connect-src`. A run-time CSP naming the server origin exactly was rejected: Tauri computes the header from immutable config when it serves the HTML, so it would mean rebuilding config and reloading the webview on every server change, for a policy the user can already point anywhere. The asset-protocol scope narrows from `$APPDATA/**` to `$APPDATA/thumbnails/**` — since DR-137 moved downloaded media to the loopback server, `imageCache` is the only `convertFileSrc` caller left, so the database and the encrypted-token fallback file no longer sit inside the grant | Security | UR-012, UR-071 | Done |
|
| DR-198 | The webview runs under a real Content-Security-Policy, and the asset protocol is scoped to the one directory it still serves. `csp` was `null`, which disables CSP entirely: any script that reached the web layer — through a future `{@html}`, a dependency, or a devtools paste — would have inherited the whole IPC surface, and with it the user's session. `script-src 'self'` (Tauri injects a nonce for SvelteKit's inline bootstrap script at build time, so no `'unsafe-inline'` is needed) plus `object-src`/`frame-src 'none'` and `base-uri 'self'` is the part that is genuinely restrictive. `img-src`/`media-src`/`connect-src` cannot be: the Jellyfin origin is typed in by the user at run time and is commonly plain `http` on a LAN, so they allow `http:`/`https:` — a wide grant for *data*, but one that still bars `file:`, `filesystem:` and scripting schemes, and leaves `script-src` untouched. `style-src` keeps `'unsafe-inline'` because Svelte compiles `style="…"` attributes (including `app.html`'s `display: contents` wrapper) into markup; this is safe only while no `<style>` element survives into `index.html`, since a nonce there would make Tauri's injection outrank — and therefore void — `'unsafe-inline'`. `worker-src blob:` and `media-src blob:` are hls.js: it demuxes in a worker built from a blob and attaches MSE through `URL.createObjectURL`. `asset:` and `http://asset.localhost` are the same protocol under the two naming schemes `convertFileSrc` emits (custom scheme on Linux/macOS, `http` host on Windows/Android); `ipc:`/`http://ipc.localhost` is the invoke transport, which would otherwise be blocked by `connect-src`. A run-time CSP naming the server origin exactly was rejected: Tauri computes the header from immutable config when it serves the HTML, so it would mean rebuilding config and reloading the webview on every server change, for a policy the user can already point anywhere. The asset-protocol scope narrows from `$APPDATA/**` to `$APPDATA/thumbnails/**` — since DR-137 moved downloaded media to the loopback server, `imageCache` is the only `convertFileSrc` caller left, so the database and the encrypted-token fallback file no longer sit inside the grant | Security | UR-012, UR-071 | Done |
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -739,6 +749,11 @@ Internal architecture, components, and application logic.
|
|||||||
| UT-213 | The direct-play negotiation, one test per branch, against `PlaybackInfo` fixtures whose shapes were all observed on a live server: a supported source direct-plays; a remuxable one direct-streams and reports itself as *not* transcoding; an unsupported codec transcodes; undecodable audio overrides the server's direct-play offer (silent picture is worse than a transcode); a pinned audio track forces a transcode; a ceiling below the source bitrate transcodes even though the codec is fine, and the ladder agrees that rung constrains it; direct play wins over direct stream when both are offered. Plus the ceiling: a per-playback override governs the stream being opened without disturbing the durable default the Settings screen shows, and dropping it returns to that default | DR-225, DR-227 | Done |
|
| UT-213 | The direct-play negotiation, one test per branch, against `PlaybackInfo` fixtures whose shapes were all observed on a live server: a supported source direct-plays; a remuxable one direct-streams and reports itself as *not* transcoding; an unsupported codec transcodes; undecodable audio overrides the server's direct-play offer (silent picture is worse than a transcode); a pinned audio track forces a transcode; a ceiling below the source bitrate transcodes even though the codec is fine, and the ladder agrees that rung constrains it; direct play wins over direct stream when both are offered. Plus the ceiling: a per-playback override governs the stream being opened without disturbing the durable default the Settings screen shows, and dropping it returns to that default | DR-225, DR-227 | Done |
|
||||||
| UT-214 | The loader comes from the transport, never the URL. hls.js is attached for `hls` when available and the element's own loader when not; progressive and local files load directly; the element's `src` is emptied only when hls.js drives it. The two cases that fail against a substring check, and the reason the field exists: a `progressive` stream whose URL contains `.m3u8` is *not* given an HLS loader, and an `hls` stream whose URL contains no `.m3u8` *is*. Both failed against the pre-DR-225 implementation before the fix landed | DR-224 | Done |
|
| UT-214 | The loader comes from the transport, never the URL. hls.js is attached for `hls` when available and the element's own loader when not; progressive and local files load directly; the element's `src` is emptied only when hls.js drives it. The two cases that fail against a substring check, and the reason the field exists: a `progressive` stream whose URL contains `.m3u8` is *not* given an HLS loader, and an `hls` stream whose URL contains no `.m3u8` *is*. Both failed against the pre-DR-225 implementation before the fix landed | DR-224 | Done |
|
||||||
| UT-215 | Waiting for the repository rather than racing it: it resolves immediately when the session is already restored, resolves when the session arrives later (the race the player page lost on mount), still rejects when there genuinely is no session, unsubscribes once settled so a later store change cannot re-settle it, and leaves no armed timer to reject an already-resolved promise | DR-013 | Done |
|
| UT-215 | Waiting for the repository rather than racing it: it resolves immediately when the session is already restored, resolves when the session arrives later (the race the player page lost on mount), still rejects when there genuinely is no session, unsubscribes once settled so a later store change cannot re-settle it, and leaves no armed timer to reject an already-resolved promise | DR-013 | Done |
|
||||||
|
| UT-216 | The native-video opt-in is read from one place and only explicit truthy values enable it: absent, empty, `0`, `no`, `false` and anything unrecognised all mean off, because a half-set variable that half-enabled the renderer would configure mpv for video with nothing drawing it — audio over a black rectangle | DR-231 | Done |
|
||||||
|
| UT-217 | A transcoded HLS stream on the native backend re-negotiates rather than seeking in place, while the same stream under hls.js still seeks in place — the cell that native video made reachable for the first time | DR-238 | Done |
|
||||||
|
| UT-218 | Every property name matched by the mpv event loop also appears in an `observe_property` call, asserted against the source because the registration cannot be observed at runtime without a live mpv | DR-239 | Done |
|
||||||
|
| UT-219 | A fullscreen toggle moves the document only when an in-document `<video>` renders, and moves the OS window as well when a native surface does | DR-240 | Done |
|
||||||
|
| UT-220 | The conformance suite: opening at a position starts there and never at zero, a seek issued while opening is honoured and overrides the start it overtook, pause and play are observable, close is silent and idempotent, and an open cancelled by close never begins playing | DR-242, DR-243 | In Progress |
|
||||||
|
|
||||||
### Integration Tests
|
### Integration Tests
|
||||||
|
|
||||||
@@ -759,6 +774,7 @@ Internal architecture, components, and application logic.
|
|||||||
| IT-013 | Background-audio handoff on Android: background/lock continues audio via native service and stops video decode; foreground resumes video at position | IR-025, UR-040 | Pending |
|
| IT-013 | Background-audio handoff on Android: background/lock continues audio via native service and stops video decode; foreground resumes video at position | IR-025, UR-040 | Pending |
|
||||||
| IT-016 | Offline library listing end-to-end: with the server unreachable, a library page lists only downloaded media with the toggle off, and additionally reveals greyed-out cached catalog entries with the toggle on | UR-052, DR-078, DR-079, DR-080 | Done |
|
| IT-016 | Offline library listing end-to-end: with the server unreachable, a library page lists only downloaded media with the toggle off, and additionally reveals greyed-out cached catalog entries with the toggle on | UR-052, DR-078, DR-079, DR-080 | Done |
|
||||||
| IT-017 | A download queued from a greyed-out offline catalog entry persists and is resolved and started on reconnect | UR-052, UR-011 | Done |
|
| IT-017 | A download queued from a greyed-out offline catalog entry persists and is resolved and started on reconnect | UR-052, UR-011 | Done |
|
||||||
|
| IT-018 | The conformance cases run against ExoPlayer on a device: opening from the beginning and at a position, a seek issued while still preparing, a seek after open, pause and play observable, stop silent and idempotent, and a load cancelled by stop never playing. The fixture is a silent WAV synthesised at setup, so the repo carries no media and the duration is exact | DR-247 | Done |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,269 @@
|
|||||||
|
# Spec: MediaPlayer — one controller API, three interchangeable engines
|
||||||
|
|
||||||
|
**Status:** Proposed
|
||||||
|
**Requirements:** UR-081 (new) → DR-242 … DR-249 (new); IR-034. Re-check
|
||||||
|
`requirements.md` before allocating — ids moved several times while this was
|
||||||
|
written.
|
||||||
|
**UX spec:** n/a — no user-visible change is intended. That is the point.
|
||||||
|
**Supersedes / revises:** absorbs `determine_video_seek_strategy`
|
||||||
|
(`player/seek.rs`, DR-238) into the engines. Revises the backend half of
|
||||||
|
[playback-backend-unification.md](playback-backend-unification.md).
|
||||||
|
|
||||||
|
**Destination on completion:**
|
||||||
|
[01-rust-backend.md](../architecture/01-rust-backend.md) — replaces the player
|
||||||
|
state-machine section; and
|
||||||
|
[05-platform-backends.md](../architecture/05-platform-backends.md) — the engines
|
||||||
|
become implementations of a stated contract rather than three separate designs.
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
Replace the `PlayerBackend` trait with a `MediaPlayer` contract that expresses
|
||||||
|
**intent** ("present this item, starting here") rather than **device operations**
|
||||||
|
("load", then "seek"). MPV, ExoPlayer and the webview element implement it; a
|
||||||
|
`FakePlayer` implements it for tests; and one conformance suite runs against
|
||||||
|
every implementation so a backend is either correct or visibly failing.
|
||||||
|
|
||||||
|
No user-visible behaviour changes. What changes is that playback logic stops
|
||||||
|
being written three times in the command layer.
|
||||||
|
|
||||||
|
## Motivation
|
||||||
|
|
||||||
|
A day of debugging Linux native video produced four defects (DR-238 … DR-241).
|
||||||
|
Every one of them traces to the same missing seam, not to mpv:
|
||||||
|
|
||||||
|
| Defect | What it looked like | What it was |
|
||||||
|
|---|---|---|
|
||||||
|
| DR-241 | "Resume is broken", "I cannot skip" | `loadfile` is async, so a seek issued straight after a load fails and was discarded. The trait has no way to say *open at a position*, so every caller does load-then-seek and each races independently. |
|
||||||
|
| DR-238 | Transcoded seeks silently did nothing | `use_html5` was doing double duty as "who renders" **and** "how do I seek", decided in the command layer by a truth table. |
|
||||||
|
| DR-239 | Play/pause control never moved | `PropertyChange { name: "pause" }` was handled but never observed. Nothing in the contract required an engine to report its own state. |
|
||||||
|
| DR-240 | Fullscreen left the picture at window size | `requestFullscreen()` moves the document; whoever owns the pixels has to be told separately. |
|
||||||
|
|
||||||
|
The shape is consistent: **the same intent implemented in several places, each
|
||||||
|
with its own timing and its own idea of the rules.** Resume worked through the
|
||||||
|
adapter (which seeks after `File loaded`) and failed through the command (which
|
||||||
|
seeks immediately). Two callers, one intent, two behaviours.
|
||||||
|
|
||||||
|
Supporting evidence for the diagnosis:
|
||||||
|
|
||||||
|
- `commands/player/mod.rs` is **3,561 lines** and is where "stop → rebuild URL →
|
||||||
|
update queue → load → seek" lives. That is playback orchestration in the IPC
|
||||||
|
layer.
|
||||||
|
- `player_play_item` needed a `#[cfg(not(target_os = "linux"))]` guard, i.e. a
|
||||||
|
platform decision in a command handler.
|
||||||
|
- The frontend carries `didStartNativePlayback`, `didStopBackendEarly`,
|
||||||
|
`hasPerformedInitialSeek`, `lastAppliedInitialPosition` — playback state in the
|
||||||
|
UI, which contradicts the one-directional rule in CLAUDE.md.
|
||||||
|
|
||||||
|
### Why an abstraction, and not more fixes
|
||||||
|
|
||||||
|
Each defect above was individually cheap to patch, and patching them is what
|
||||||
|
produced a regression: routing transcoded seeks to a reload path turned "seek
|
||||||
|
does nothing" into "seek jumps to zero", because the reload path's own seek was
|
||||||
|
broken in the same way. **Symptom fixes in this area compound.**
|
||||||
|
|
||||||
|
## Layer assignment
|
||||||
|
|
||||||
|
| Logic / responsibility | Layer | Why it belongs there |
|
||||||
|
|---|---|---|
|
||||||
|
| Presenting an item at a position, in one operation | **Engine** (`MediaPlayer`) | Only the engine knows when its pipeline can accept a position. Expressing it as caller-sequenced load-then-seek exports a race the engine is the only one able to close. |
|
||||||
|
| Whether *this* stream can be seeked in place, or must be re-opened | **Engine** | A property of the engine × transport pair: hls.js seeks a VOD playlist, mpv's HLS demuxer cannot make Jellyfin transcode from a new offset. Today this is a truth table in a command handler that has to guess for engines it does not own. |
|
||||||
|
| Reporting position, phase, duration, active tracks | **Engine** | The player is the authoritative source of playback state (CLAUDE.md). An engine that does not report is not implementing the contract — DR-239 was exactly this. |
|
||||||
|
| Choosing *which* stream to open (direct play vs transcode, ceiling, transport) | **Rust, above the engine** | Domain: depends on Jellyfin's `PlaybackInfo`, codec support, quality ceiling. See [backend-owned-stream-selection.md](backend-owned-stream-selection.md). The engine is handed a `StreamSelection`; it never negotiates one. |
|
||||||
|
| Queue, autoplay, session, playback reporting | **`PlayerController`** | Policy across items. Unchanged — but it talks to one contract instead of branching per platform. |
|
||||||
|
| Which engine this platform uses | **Rust, at construction** | Already correct today; stays a single `cfg` at the composition root rather than `cfg`s scattered through command handlers. |
|
||||||
|
| Rendering surfaces, controls, fullscreen chrome | **Frontend / platform** | Presentation. The engine reports *what* is playing; it does not own the window. |
|
||||||
|
|
||||||
|
Borderline row and its tie-breaker: "should a transcoded seek re-open the
|
||||||
|
stream?" reads like domain policy. It is **engine** capability — the *decision*
|
||||||
|
is "seek to T", and how to achieve it is the engine's business. If it were
|
||||||
|
policy, every new engine would require editing a shared truth table, which is
|
||||||
|
precisely the coupling DR-238 came from.
|
||||||
|
|
||||||
|
## Design
|
||||||
|
|
||||||
|
### The contract
|
||||||
|
|
||||||
|
```rust
|
||||||
|
/// Anything that can present media: MpvPlayer, ExoPlayer, WebviewPlayer, FakePlayer.
|
||||||
|
pub trait MediaPlayer: Send {
|
||||||
|
/// Present `req.selection`, beginning at `req.start`.
|
||||||
|
///
|
||||||
|
/// One operation, deliberately. `open` is where a start position is
|
||||||
|
/// *expressible*, so no caller has to sequence load-then-seek and no caller
|
||||||
|
/// can race the engine's own load. An engine that cannot start at an offset
|
||||||
|
/// natively must absorb that internally (defer until loaded, or re-open) —
|
||||||
|
/// it is the only layer that knows when it is able to.
|
||||||
|
fn open(&mut self, req: OpenRequest) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
fn play(&mut self) -> Result<(), PlayerError>;
|
||||||
|
fn pause(&mut self) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
/// Stop and release the current item. Must be idempotent, and must leave the
|
||||||
|
/// engine producing no audio — DR-2xx exists because "stopped" and "silent"
|
||||||
|
/// were not the same thing.
|
||||||
|
fn close(&mut self) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
/// Seek to an absolute position on the item's timeline.
|
||||||
|
///
|
||||||
|
/// The engine decides in-place vs re-open. Callers never choose.
|
||||||
|
fn seek(&mut self, to: Duration) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
fn set_volume(&mut self, volume: Volume) -> Result<(), PlayerError>;
|
||||||
|
fn set_rate(&mut self, rate: f64) -> Result<(), PlayerError>;
|
||||||
|
fn select_audio_track(&mut self, index: Option<i32>) -> Result<(), PlayerError>;
|
||||||
|
fn select_subtitle_track(&mut self, index: Option<i32>) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
/// One coherent read of everything the UI consumes.
|
||||||
|
fn snapshot(&self) -> PlaybackSnapshot;
|
||||||
|
|
||||||
|
/// Engine capabilities, so callers can adapt without naming engines.
|
||||||
|
fn capabilities(&self) -> Capabilities;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub struct OpenRequest {
|
||||||
|
pub media: MediaItem,
|
||||||
|
pub selection: StreamSelection, // url + transport + playback kind
|
||||||
|
pub start: Duration, // Duration::ZERO for "from the beginning"
|
||||||
|
pub audio_track: Option<i32>,
|
||||||
|
pub subtitle_track: Option<i32>,
|
||||||
|
pub autoplay: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PlaybackSnapshot {
|
||||||
|
pub phase: Phase,
|
||||||
|
pub position: Duration,
|
||||||
|
pub duration: Option<Duration>,
|
||||||
|
pub seekable: bool,
|
||||||
|
pub volume: Volume,
|
||||||
|
pub rate: f64,
|
||||||
|
pub audio_track: Option<i32>,
|
||||||
|
pub subtitle_track: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `Opening` is the state today's code cannot express, and the direct cause of
|
||||||
|
/// DR-241: a seek arriving with nothing loaded had no phase to be rejected or
|
||||||
|
/// queued against, so it was simply lost.
|
||||||
|
pub enum Phase { Idle, Opening, Ready, Playing, Paused, Ended, Failed(String) }
|
||||||
|
```
|
||||||
|
|
||||||
|
Engines emit `PlayerEvent` for phase, position, track and error changes. Emitting
|
||||||
|
is part of the contract, and the conformance suite asserts it — an engine that
|
||||||
|
stays silent fails, which is what would have caught DR-239 the day it landed.
|
||||||
|
|
||||||
|
### What this deletes
|
||||||
|
|
||||||
|
- `determine_video_seek_strategy` and `VideoSeekStrategy` — replaced by
|
||||||
|
`seek()` + `capabilities()`. The command layer stops deciding how engines seek.
|
||||||
|
- The reload orchestration in `player_seek_video` — moves inside the engines that
|
||||||
|
need it.
|
||||||
|
- `#[cfg(target_os = "linux")]` branches in command handlers.
|
||||||
|
- Frontend playback-state flags, which become reads of `snapshot()`.
|
||||||
|
|
||||||
|
### IPC
|
||||||
|
|
||||||
|
No new commands. Existing ones keep their names and shapes; they become thin
|
||||||
|
delegations. `PlayerStatus` gains nothing the frontend does not already receive.
|
||||||
|
Regenerate `bindings.ts` only if `PlaybackSnapshot` is exposed directly — prefer
|
||||||
|
mapping it onto the existing `PlayerStatus` so this stays invisible at the wire.
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
This is the half that makes the abstraction worth having, and it is the reason to
|
||||||
|
do it rather than keep patching.
|
||||||
|
|
||||||
|
### 1. A conformance suite, run against every engine
|
||||||
|
|
||||||
|
One set of tests, parameterised over implementations. Any `MediaPlayer` must pass
|
||||||
|
it; a new engine is "done" when it does.
|
||||||
|
|
||||||
|
```
|
||||||
|
conformance::run(&mut engine, fixture) covering:
|
||||||
|
open(start = ZERO) -> phase Ready|Playing, position ~0
|
||||||
|
open(start = 10min) -> position within tolerance of 10min, NEVER 0 [DR-241]
|
||||||
|
seek while Opening -> honoured once Ready, not discarded [DR-241]
|
||||||
|
seek on a transcoded stream -> position lands, by whatever means [DR-238]
|
||||||
|
pause / play -> phase changes AND an event is emitted [DR-239]
|
||||||
|
close -> phase Idle, silent, idempotent
|
||||||
|
close during Opening -> no playback ever starts [audio-on-exit]
|
||||||
|
volume / rate / track select -> reflected in snapshot()
|
||||||
|
```
|
||||||
|
|
||||||
|
The `open(start = 10min)` and `seek while Opening` cases are the ones that fail
|
||||||
|
on today's code. They are written first, and they are the acceptance criterion.
|
||||||
|
|
||||||
|
### 2. `FakePlayer`
|
||||||
|
|
||||||
|
A deterministic in-memory implementation with a controllable clock. Lets
|
||||||
|
`PlayerController`, autoplay, queue, sleep-timer and session logic be tested with
|
||||||
|
no mpv, no device, no network — most of which is currently only reachable through
|
||||||
|
a real engine.
|
||||||
|
|
||||||
|
### 3. Per-engine runs
|
||||||
|
|
||||||
|
| Engine | Where | Note |
|
||||||
|
|---|---|---|
|
||||||
|
| `FakePlayer` | `cargo test` | Always. |
|
||||||
|
| `MpvPlayer` | `cargo test`, Linux | libmpv is already in the builder image (the Linux build links it), so **no CI toolchain install** — see CLAUDE.md. Needs a tiny local fixture file; generate it in-test rather than committing media. |
|
||||||
|
| `ExoPlayer` | instrumented, on device | Not in the standard CI job. Run via `scripts/` on a connected device; record results in the PR. |
|
||||||
|
| `WebviewPlayer` | vitest | Against a stubbed element, as `html5Adapter` is tested today. |
|
||||||
|
|
||||||
|
An engine that cannot run in CI still has the same suite; it is just run by hand.
|
||||||
|
That is the point of writing it once.
|
||||||
|
|
||||||
|
## Migration
|
||||||
|
|
||||||
|
Strangler, not a rewrite. Each step ships independently and leaves the app working.
|
||||||
|
|
||||||
|
1. **DR-242** Define `MediaPlayer`, `OpenRequest`, `PlaybackSnapshot`, `Phase`,
|
||||||
|
`Capabilities`. No implementations. Compiles alongside `PlayerBackend`.
|
||||||
|
2. **DR-243** `FakePlayer` + the conformance suite. The suite fails against
|
||||||
|
nothing yet — it is the specification.
|
||||||
|
3. **DR-244** `MpvPlayer` implementing `MediaPlayer`, wrapping today's
|
||||||
|
`MpvBackend` internals. Make conformance pass, including `open(start)`.
|
||||||
|
4. **DR-245** `PlayerController` talks to `MediaPlayer`. `PlayerBackend` retained
|
||||||
|
behind an adapter so the other engines keep working.
|
||||||
|
5. **DR-246** Move seek strategy and reload orchestration out of
|
||||||
|
`commands/player/mod.rs` into the engines; delete `seek.rs`'s truth table.
|
||||||
|
6. **DR-247** `ExoPlayerPlayer`; conformance on device.
|
||||||
|
7. **DR-248** `WebviewPlayer`; retire the adapter shim.
|
||||||
|
8. **DR-249** Delete `PlayerBackend` and the frontend playback-state flags.
|
||||||
|
|
||||||
|
Steps 1–3 are pure addition and risk nothing. Step 5 is where today's defect
|
||||||
|
classes actually die.
|
||||||
|
|
||||||
|
## Out of scope
|
||||||
|
|
||||||
|
- Stream selection (which URL, which quality) — that is
|
||||||
|
[backend-owned-stream-selection.md](backend-owned-stream-selection.md), and
|
||||||
|
this spec consumes its `StreamSelection` rather than duplicating it.
|
||||||
|
- Rendering surfaces and compositing.
|
||||||
|
- Any user-visible behaviour change. If one appears, it is a bug in the migration.
|
||||||
|
- Replacing hls.js or changing the transcode path.
|
||||||
|
|
||||||
|
## Acceptance criteria
|
||||||
|
|
||||||
|
- [ ] The conformance suite exists and `open(start = 10min)` fails against the
|
||||||
|
pre-migration mpv path — proving it reproduces DR-241 — then passes.
|
||||||
|
- [ ] `FakePlayer` lets at least one controller-level test run with no engine.
|
||||||
|
- [ ] `determine_video_seek_strategy` is deleted, not merely bypassed.
|
||||||
|
- [ ] No `cfg(target_os = ...)` remains in `commands/player/`.
|
||||||
|
- [ ] `bun run check`, `bun run test`, `bun run format:check`, `bun run lint` pass.
|
||||||
|
- [ ] `cargo fmt`, `cargo clippy -D warnings`, `bun run test:rust` pass.
|
||||||
|
- [ ] `bun run check:boundary` passes.
|
||||||
|
- [ ] `// TRACES:` on new code; `bun run traces:validate` passes; coverage stays
|
||||||
|
at or above the CI ratchet.
|
||||||
|
- [ ] Manual: resume, skip on a transcoded item, pause/play, and exit-while-playing
|
||||||
|
verified on Linux **and** Android before `PlayerBackend` is deleted.
|
||||||
|
|
||||||
|
## Notes for the implementer
|
||||||
|
|
||||||
|
- **Write the conformance suite before the second engine**, or it will encode
|
||||||
|
whatever the first engine happens to do.
|
||||||
|
- `close()` must mean *silent*. The bug that motivated this spec had `stop` being
|
||||||
|
called, reported, and audible afterwards.
|
||||||
|
- Do not let `Capabilities` grow into engine sniffing. If a caller branches on
|
||||||
|
the engine's identity, the contract is missing something — add it there.
|
||||||
|
- A parallel Claude session may be active in this repo — `git diff` before
|
||||||
|
"repairing" unexpected changes.
|
||||||
+3874
-3739
File diff suppressed because it is too large
Load Diff
@@ -36,6 +36,19 @@ if [ -d "$TEST_SOURCE_DIR" ]; then
|
|||||||
echo " Copied unit tests: src/test"
|
echo " Copied unit tests: src/test"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Instrumented tests (src/androidTest). These need a device: they drive
|
||||||
|
# ExoPlayer, which requires an Android Context and a Looper and therefore
|
||||||
|
# cannot run from the desktop conformance suite. Run with
|
||||||
|
# `./gradlew :app:connectedDebugAndroidTest` from gen/android.
|
||||||
|
ANDROID_TEST_SOURCE_DIR="$PROJECT_ROOT/src-tauri/android/src/androidTest/java/com/dtourolle/jellytau"
|
||||||
|
ANDROID_TEST_TARGET_DIR="$PROJECT_ROOT/src-tauri/gen/android/app/src/androidTest/java/com/dtourolle/jellytau"
|
||||||
|
if [ -d "$ANDROID_TEST_SOURCE_DIR" ]; then
|
||||||
|
rm -rf "$ANDROID_TEST_TARGET_DIR"
|
||||||
|
mkdir -p "$ANDROID_TEST_TARGET_DIR"
|
||||||
|
cp -r "$ANDROID_TEST_SOURCE_DIR"/. "$ANDROID_TEST_TARGET_DIR/"
|
||||||
|
echo " Copied instrumented tests: src/androidTest"
|
||||||
|
fi
|
||||||
|
|
||||||
# Copy individual Kotlin files (like VideoOverlayManager.kt)
|
# Copy individual Kotlin files (like VideoOverlayManager.kt)
|
||||||
for kt_file in "$SOURCE_DIR"/*.kt; do
|
for kt_file in "$SOURCE_DIR"/*.kt; do
|
||||||
if [ -f "$kt_file" ]; then
|
if [ -f "$kt_file" ]; then
|
||||||
|
|||||||
@@ -141,3 +141,18 @@ ndk-context = "0.1"
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3.24.0"
|
tempfile = "3.24.0"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
# Exposes the MediaPlayer conformance suite and the `player-conformance` binary
|
||||||
|
# to non-test builds, so an engine that cannot run in-process — ExoPlayer on a
|
||||||
|
# device — is driven by the same cases as the ones that can, rather than by a
|
||||||
|
# second checklist that drifts.
|
||||||
|
conformance = []
|
||||||
|
|
||||||
|
# A standalone runner for the conformance suite. Deliberately a separate binary:
|
||||||
|
# it links libmpv and nothing else, so a wrapper can be verified without building
|
||||||
|
# or launching the app.
|
||||||
|
[[bin]]
|
||||||
|
name = "player-conformance"
|
||||||
|
path = "src/bin/player_conformance.rs"
|
||||||
|
required-features = ["conformance"]
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ android {
|
|||||||
targetSdk = 36
|
targetSdk = 36
|
||||||
versionCode = tauriProperties.getProperty("tauri.android.versionCode", "1").toInt()
|
versionCode = tauriProperties.getProperty("tauri.android.versionCode", "1").toInt()
|
||||||
versionName = tauriProperties.getProperty("tauri.android.versionName", "1.0")
|
versionName = tauriProperties.getProperty("tauri.android.versionName", "1.0")
|
||||||
|
// Required to run the on-device conformance suite
|
||||||
|
// (src/androidTest). See docs/specs/media-player-controller.md.
|
||||||
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
signingConfigs {
|
signingConfigs {
|
||||||
create("release") {
|
create("release") {
|
||||||
@@ -147,6 +150,7 @@ dependencies {
|
|||||||
testImplementation("junit:junit:4.13.2")
|
testImplementation("junit:junit:4.13.2")
|
||||||
androidTestImplementation("androidx.test.ext:junit:1.1.4")
|
androidTestImplementation("androidx.test.ext:junit:1.1.4")
|
||||||
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0")
|
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0")
|
||||||
|
androidTestImplementation("androidx.test:runner:1.5.2")
|
||||||
}
|
}
|
||||||
|
|
||||||
apply(from = "tauri.build.gradle.kts")
|
apply(from = "tauri.build.gradle.kts")
|
||||||
+252
@@ -0,0 +1,252 @@
|
|||||||
|
package com.dtourolle.jellytau.player
|
||||||
|
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
|
import androidx.test.platform.app.InstrumentationRegistry
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.After
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import java.io.File
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The MediaPlayer conformance cases, run against ExoPlayer on a real device.
|
||||||
|
*
|
||||||
|
* The desktop suite (src-tauri/src/player/conformance.rs) cannot reach here:
|
||||||
|
* ExoPlayer needs an Android Context and a Looper, so it only exists inside an
|
||||||
|
* app process. These are the same behaviours, asserted against the engine
|
||||||
|
* itself rather than the Rust wrapper — the layer below the contract.
|
||||||
|
*
|
||||||
|
* The fixture is generated rather than committed: a long silent WAV written to
|
||||||
|
* the cache directory at setup. No binary in the repo, no `adb push` step, and
|
||||||
|
* the duration is exact, which matters for the seek assertions.
|
||||||
|
*
|
||||||
|
* Run: ./gradlew :app:connectedDebugAndroidTest (from src-tauri/gen/android)
|
||||||
|
*
|
||||||
|
* TRACES: UR-081 | DR-247
|
||||||
|
*/
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
|
class PlayerConformanceTest {
|
||||||
|
|
||||||
|
private lateinit var player: JellyTauPlayer
|
||||||
|
private lateinit var mediaUrl: String
|
||||||
|
|
||||||
|
/** Long enough to seek well past any buffer. */
|
||||||
|
private val fixtureSeconds = 1200
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ExoPlayer lands on the nearest sync sample, and a `prepare` is not
|
||||||
|
* instantaneous. Generous on purpose: a tight bound here produces a test
|
||||||
|
* that fails on a slow device and teaches people to re-run until green.
|
||||||
|
*/
|
||||||
|
private val toleranceSeconds = 10.0
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setUp() {
|
||||||
|
val context = InstrumentationRegistry.getInstrumentation().targetContext
|
||||||
|
JellyTauPlayer.initialize(context)
|
||||||
|
player = JellyTauPlayer.getInstance()
|
||||||
|
|
||||||
|
val fixture = File(context.cacheDir, "conformance-$fixtureSeconds.wav")
|
||||||
|
if (!fixture.exists() || fixture.length() < 1024) {
|
||||||
|
writeSilentWav(fixture, fixtureSeconds)
|
||||||
|
}
|
||||||
|
mediaUrl = fixture.toURI().toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
fun tearDown() {
|
||||||
|
onMain { player.stop() }
|
||||||
|
// Leave nothing playing for the next case.
|
||||||
|
Thread.sleep(200)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------- cases
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun opensFromTheBeginning() {
|
||||||
|
onMain { player.load(mediaUrl, "conformance") }
|
||||||
|
awaitLoaded()
|
||||||
|
|
||||||
|
assertNear(0.0, position(), "playback should start at the beginning")
|
||||||
|
assertTrue("duration should be known once loaded", duration() > 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DR-241. Opening at a position starts *there*, not at zero.
|
||||||
|
*
|
||||||
|
* `load(url, mediaId)` has no way to express a start position, so every
|
||||||
|
* caller loads and then seeks — and a seek issued against a player that is
|
||||||
|
* still preparing is the window resume was lost in on the desktop side.
|
||||||
|
* This is the same defect on ExoPlayer.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun opensAtAStartPosition() {
|
||||||
|
val start = 600.0
|
||||||
|
onMain { player.load(mediaUrl, "conformance", start) }
|
||||||
|
awaitLoaded()
|
||||||
|
|
||||||
|
assertTrue(
|
||||||
|
"opened at ${start}s but playback began at ${position()}s - " +
|
||||||
|
"the start position was dropped",
|
||||||
|
position() > 1.0
|
||||||
|
)
|
||||||
|
assertNear(start, position(), "start position")
|
||||||
|
}
|
||||||
|
|
||||||
|
/** DR-241. A seek issued while still preparing is honoured, not lost. */
|
||||||
|
@Test
|
||||||
|
fun seekWhileOpeningIsHonoured() {
|
||||||
|
val target = 300.0
|
||||||
|
onMain {
|
||||||
|
player.load(mediaUrl, "conformance")
|
||||||
|
// Deliberately before the player is ready: this is the race,
|
||||||
|
// expressed on purpose rather than stumbled into.
|
||||||
|
player.seek(target)
|
||||||
|
}
|
||||||
|
awaitLoaded()
|
||||||
|
|
||||||
|
assertNear(target, position(), "seek issued while opening")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun seeksAfterOpen() {
|
||||||
|
onMain { player.load(mediaUrl, "conformance") }
|
||||||
|
awaitLoaded()
|
||||||
|
|
||||||
|
val target = 420.0
|
||||||
|
onMain { player.seek(target) }
|
||||||
|
awaitPosition(target)
|
||||||
|
|
||||||
|
assertNear(target, position(), "seek after open")
|
||||||
|
}
|
||||||
|
|
||||||
|
/** DR-239. Pause and play are observable, not merely accepted. */
|
||||||
|
@Test
|
||||||
|
fun pauseAndPlayAreObservable() {
|
||||||
|
onMain { player.load(mediaUrl, "conformance") }
|
||||||
|
awaitLoaded()
|
||||||
|
|
||||||
|
onMain { player.pause() }
|
||||||
|
awaitPlaying(false)
|
||||||
|
assertFalse("a paused player must not report playing", isPlaying())
|
||||||
|
|
||||||
|
onMain { player.play() }
|
||||||
|
awaitPlaying(true)
|
||||||
|
assertTrue("a resumed player must report playing", isPlaying())
|
||||||
|
}
|
||||||
|
|
||||||
|
/** `stop()` releases the item, is silent, and can be called twice. */
|
||||||
|
@Test
|
||||||
|
fun closeIsSilentAndIdempotent() {
|
||||||
|
onMain { player.load(mediaUrl, "conformance") }
|
||||||
|
awaitLoaded()
|
||||||
|
|
||||||
|
onMain { player.stop() }
|
||||||
|
awaitPlaying(false)
|
||||||
|
assertFalse("a stopped player must not report playing", isPlaying())
|
||||||
|
|
||||||
|
onMain { player.stop() }
|
||||||
|
assertFalse("stop must be idempotent", isPlaying())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An open cancelled by stop must not come back to life.
|
||||||
|
*
|
||||||
|
* The shape of "audio kept playing after leaving the player": a prepare
|
||||||
|
* still in flight completed after the stop, with nothing left to tell it
|
||||||
|
* not to.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun closeDuringOpenNeverPlays() {
|
||||||
|
onMain {
|
||||||
|
player.load(mediaUrl, "conformance")
|
||||||
|
player.stop()
|
||||||
|
}
|
||||||
|
Thread.sleep(2000)
|
||||||
|
|
||||||
|
assertFalse(
|
||||||
|
"a load cancelled by stop must not start playing",
|
||||||
|
isPlaying()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------- helpers
|
||||||
|
|
||||||
|
private fun onMain(block: () -> Unit) {
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(block)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun position(): Double = readOnMain { player.getPosition() }
|
||||||
|
private fun duration(): Double = readOnMain { player.getDuration() }
|
||||||
|
private fun isPlaying(): Boolean = readOnMain { player.getExoPlayer().isPlaying }
|
||||||
|
|
||||||
|
private fun <T> readOnMain(block: () -> T): T {
|
||||||
|
var out: T? = null
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync { out = block() }
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
return out as T
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Poll a state the player publishes rather than sleeping a fixed time. */
|
||||||
|
private fun await(what: String, timeoutMs: Long = 15_000, predicate: () -> Boolean) {
|
||||||
|
val deadline = System.currentTimeMillis() + timeoutMs
|
||||||
|
while (System.currentTimeMillis() < deadline) {
|
||||||
|
if (predicate()) return
|
||||||
|
Thread.sleep(50)
|
||||||
|
}
|
||||||
|
throw AssertionError("timed out waiting for $what")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun awaitLoaded() {
|
||||||
|
await("the player to report a duration") { duration() > 0 }
|
||||||
|
// One more beat so a start position or a deferred seek has landed.
|
||||||
|
Thread.sleep(500)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun awaitPosition(target: Double) =
|
||||||
|
await("position to reach ${target}s") { abs(position() - target) <= toleranceSeconds }
|
||||||
|
|
||||||
|
private fun awaitPlaying(expected: Boolean) =
|
||||||
|
await("isPlaying == $expected", 5_000) { isPlaying() == expected }
|
||||||
|
|
||||||
|
private fun assertNear(expected: Double, actual: Double, what: String) {
|
||||||
|
assertTrue(
|
||||||
|
"$what: expected ~${expected}s, got ${actual}s (tolerance ${toleranceSeconds}s)",
|
||||||
|
abs(actual - expected) <= toleranceSeconds
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a silent 8 kHz mono 16-bit WAV of `seconds` length.
|
||||||
|
*
|
||||||
|
* Synthesised rather than committed so the repo carries no media, and so
|
||||||
|
* the duration is exact — the seek assertions depend on it.
|
||||||
|
*/
|
||||||
|
private fun writeSilentWav(file: File, seconds: Int) {
|
||||||
|
val sampleRate = 8000
|
||||||
|
val dataBytes = sampleRate * 2 * seconds
|
||||||
|
file.outputStream().buffered().use { out ->
|
||||||
|
fun le32(v: Int) = out.write(
|
||||||
|
byteArrayOf(
|
||||||
|
(v and 0xff).toByte(),
|
||||||
|
((v shr 8) and 0xff).toByte(),
|
||||||
|
((v shr 16) and 0xff).toByte(),
|
||||||
|
((v shr 24) and 0xff).toByte()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
fun le16(v: Int) =
|
||||||
|
out.write(byteArrayOf((v and 0xff).toByte(), ((v shr 8) and 0xff).toByte()))
|
||||||
|
|
||||||
|
out.write("RIFF".toByteArray()); le32(36 + dataBytes); out.write("WAVE".toByteArray())
|
||||||
|
out.write("fmt ".toByteArray()); le32(16); le16(1); le16(1)
|
||||||
|
le32(sampleRate); le32(sampleRate * 2); le16(2); le16(16)
|
||||||
|
out.write("data".toByteArray()); le32(dataBytes)
|
||||||
|
|
||||||
|
val chunk = ByteArray(sampleRate * 2) // one second of silence
|
||||||
|
repeat(seconds) { out.write(chunk) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -556,11 +556,31 @@ class JellyTauPlayer(private val appContext: Context) {
|
|||||||
* @param mediaId The unique ID for this media item
|
* @param mediaId The unique ID for this media item
|
||||||
*/
|
*/
|
||||||
fun load(url: String, mediaId: String) {
|
fun load(url: String, mediaId: String) {
|
||||||
|
load(url, mediaId, 0.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load [url] and begin at [startPositionSeconds].
|
||||||
|
*
|
||||||
|
* The start position is handed to ExoPlayer with the media item, not seeked
|
||||||
|
* to afterwards. `prepare()` is asynchronous, so a seek issued straight
|
||||||
|
* after a load targets a player that is still preparing: ExoPlayer clamps it
|
||||||
|
* back to zero and the item plays from the beginning. That is what made
|
||||||
|
* resume and transcoded skip start over, and it is why callers must never
|
||||||
|
* express a start position as load-then-seek.
|
||||||
|
*
|
||||||
|
* TRACES: UR-081, UR-005 | DR-241, DR-247
|
||||||
|
*/
|
||||||
|
fun load(url: String, mediaId: String, startPositionSeconds: Double) {
|
||||||
mainHandler.post {
|
mainHandler.post {
|
||||||
currentMediaId = mediaId
|
currentMediaId = mediaId
|
||||||
endedNotified = false
|
endedNotified = false
|
||||||
val mediaItem = MediaItem.fromUri(url)
|
val mediaItem = MediaItem.fromUri(url)
|
||||||
|
if (startPositionSeconds > 0.0) {
|
||||||
|
exoPlayer.setMediaItem(mediaItem, (startPositionSeconds * 1000).toLong())
|
||||||
|
} else {
|
||||||
exoPlayer.setMediaItem(mediaItem)
|
exoPlayer.setMediaItem(mediaItem)
|
||||||
|
}
|
||||||
exoPlayer.prepare()
|
exoPlayer.prepare()
|
||||||
exoPlayer.playWhenReady = true
|
exoPlayer.playWhenReady = true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
//! Thin entry point. The suite lives in the library so the binary needs no
|
||||||
|
//! access to the player internals — one exported function rather than a public
|
||||||
|
//! module tree.
|
||||||
|
//!
|
||||||
|
//! player-conformance <media-file> [mpv|legacy]
|
||||||
|
//!
|
||||||
|
//! `legacy` drives the old `PlayerBackend` through the same cases, so the
|
||||||
|
//! difference between the two designs is demonstrated on one engine and one
|
||||||
|
//! file rather than argued.
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-081 | DR-244, DR-245
|
||||||
|
|
||||||
|
use std::process::ExitCode;
|
||||||
|
|
||||||
|
use jellytau_lib::conformance_runner::{run_engine, Engine};
|
||||||
|
|
||||||
|
fn main() -> ExitCode {
|
||||||
|
let mut args = std::env::args().skip(1);
|
||||||
|
let Some(url) = args.next() else {
|
||||||
|
eprintln!("usage: player-conformance <media-file-or-url> [mpv|legacy]");
|
||||||
|
return ExitCode::from(2);
|
||||||
|
};
|
||||||
|
let engine = match args.next().as_deref() {
|
||||||
|
None | Some("mpv") => Engine::Mpv,
|
||||||
|
Some("legacy") => Engine::Legacy,
|
||||||
|
Some(other) => {
|
||||||
|
eprintln!("unknown engine {other:?} - expected mpv or legacy");
|
||||||
|
return ExitCode::from(2);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if run_engine(&url, engine) == 0 {
|
||||||
|
ExitCode::SUCCESS
|
||||||
|
} else {
|
||||||
|
ExitCode::FAILURE
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -725,18 +725,30 @@ pub async fn player_play_item(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let controller = player.0.lock().await;
|
let controller = player.0.lock().await;
|
||||||
// On Linux, video plays in the WebKitGTK HTML5 <video> element (see
|
// Who gets the stream depends on who is going to *render* it, which is a
|
||||||
// get_player_status -> use_html5_element). The MPV backend has no embedded
|
// runtime question, not a platform constant.
|
||||||
// window, so loading the stream into it would only start a redundant decode
|
//
|
||||||
// (and the frontend would immediately stop it). Only load into the native
|
// Historically Linux video was always the webview's (`use_html5_element`),
|
||||||
// backend on platforms that actually render video through it (e.g. Android).
|
// so handing the file to MPV as well would only have started a redundant
|
||||||
#[cfg(not(target_os = "linux"))]
|
// decode with no window to show it in — hence a `#[cfg(not(linux))]` guard
|
||||||
|
// and a queue-only path here. With mpv drawing the picture that inverts:
|
||||||
|
// the webview is no longer loading anything, so if this does not load the
|
||||||
|
// file, *nothing does*. The symptom is total silence — no picture and no
|
||||||
|
// audio — which reads like a broken stream rather than a stream nobody was
|
||||||
|
// given.
|
||||||
|
//
|
||||||
|
// This is the fifth place in this cycle where a renderer's capability was
|
||||||
|
// written as a compile-time platform fact. Same fix as the others: ask.
|
||||||
|
//
|
||||||
|
// TRACES: UR-080 | DR-231, DR-235
|
||||||
|
let renders_natively = cfg!(not(target_os = "linux")) || crate::player::native_video::enabled();
|
||||||
|
if renders_natively {
|
||||||
controller
|
controller
|
||||||
.play_item(media_item)
|
.play_item(media_item)
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
#[cfg(target_os = "linux")]
|
} else {
|
||||||
{
|
// The webview will play it; keep the queue in sync for the UI and for a
|
||||||
// Keep the queue in sync for UI/remote-transfer without starting MPV.
|
// remote transfer without starting a second decode.
|
||||||
controller
|
controller
|
||||||
.set_current_item(media_item)
|
.set_current_item(media_item)
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
@@ -1169,6 +1181,13 @@ pub async fn player_stop(
|
|||||||
// Check if we're in remote mode
|
// Check if we're in remote mode
|
||||||
let mode = playback_mode.0.get_mode();
|
let mode = playback_mode.0.get_mode();
|
||||||
|
|
||||||
|
// Stopping is a state transition worth seeing in a log. Native video is
|
||||||
|
// what made its absence matter: the webview <video> stopped implicitly when
|
||||||
|
// the component unmounted, so nothing ever had to call this — and "never
|
||||||
|
// called" and "called but the backend kept playing" look identical from
|
||||||
|
// outside without it.
|
||||||
|
info!("[player_stop] called (mode: {:?})", mode);
|
||||||
|
|
||||||
if let crate::playback_mode::PlaybackMode::Remote { session_id } = mode {
|
if let crate::playback_mode::PlaybackMode::Remote { session_id } = mode {
|
||||||
// Send stop command to remote session - clone client before await
|
// Send stop command to remote session - clone client before await
|
||||||
let client = {
|
let client = {
|
||||||
@@ -2086,7 +2105,9 @@ pub async fn player_get_capabilities() -> Result<PlaybackCapabilities, String> {
|
|||||||
|
|
||||||
Ok(PlaybackCapabilities {
|
Ok(PlaybackCapabilities {
|
||||||
uses_webview_audio: !native_audio,
|
uses_webview_audio: !native_audio,
|
||||||
supports_native_video: cfg!(target_os = "android"),
|
// TRACES: UR-080 | DR-235
|
||||||
|
supports_native_video: cfg!(target_os = "android")
|
||||||
|
|| crate::player::native_video::enabled(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2095,6 +2116,11 @@ pub(super) fn get_player_status(controller: &PlayerController) -> PlayerStatus {
|
|||||||
let (backend, use_html5_element) = if cfg!(target_os = "android") {
|
let (backend, use_html5_element) = if cfg!(target_os = "android") {
|
||||||
// Android uses ExoPlayer native backend
|
// Android uses ExoPlayer native backend
|
||||||
(VideoBackend::Native, false)
|
(VideoBackend::Native, false)
|
||||||
|
} else if crate::player::native_video::enabled() {
|
||||||
|
// mpv draws the picture on this desktop; the frontend must not also
|
||||||
|
// load it into a <video> element or the stream decodes twice and the
|
||||||
|
// two fight over the audio. TRACES: UR-080 | DR-235
|
||||||
|
(VideoBackend::Native, false)
|
||||||
} else {
|
} else {
|
||||||
// Linux and other platforms use HTML5 video element in frontend
|
// Linux and other platforms use HTML5 video element in frontend
|
||||||
(VideoBackend::Html5, true)
|
(VideoBackend::Html5, true)
|
||||||
|
|||||||
@@ -0,0 +1,157 @@
|
|||||||
|
//! Runs the `MediaPlayer` conformance suite against a real engine.
|
||||||
|
//!
|
||||||
|
//! A separate binary on purpose: it links libmpv and nothing else, so a wrapper
|
||||||
|
//! can be verified without building or launching the app — which is what made
|
||||||
|
//! the previous round of playback debugging so slow. Every failure here is a
|
||||||
|
//! wrapper bug, with no UI, no webview and no server in the way.
|
||||||
|
//!
|
||||||
|
//! cargo run --features conformance --bin player-conformance -- <media-file>
|
||||||
|
//!
|
||||||
|
//! Audio and video are routed to null, so it is safe on a headless runner and
|
||||||
|
//! does not claim the speakers.
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-081 | DR-244
|
||||||
|
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use crate::player::conformance::Harness;
|
||||||
|
use crate::player::legacy_player::LegacyPlayer;
|
||||||
|
use crate::player::media::MediaItem;
|
||||||
|
use crate::player::media_player::{MediaPlayer, OpenRequest, Phase};
|
||||||
|
use crate::player::mpv_backend::MpvBackend;
|
||||||
|
use crate::player::mpv_player::{MpvPlayer, Output};
|
||||||
|
use crate::repository::stream_selection::StreamSelection;
|
||||||
|
|
||||||
|
struct EngineHarness<P: MediaPlayer> {
|
||||||
|
player: P,
|
||||||
|
url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: MediaPlayer> Harness for EngineHarness<P> {
|
||||||
|
type Player = P;
|
||||||
|
|
||||||
|
fn player(&mut self) -> &mut P {
|
||||||
|
&mut self.player
|
||||||
|
}
|
||||||
|
|
||||||
|
fn request(&self, start: Duration) -> OpenRequest {
|
||||||
|
let selection = StreamSelection::local_file(self.url.clone());
|
||||||
|
let media = MediaItem::sample("conformance", &self.url);
|
||||||
|
OpenRequest::new(media, selection).starting_at(start)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait for mpv to leave `Opening`.
|
||||||
|
///
|
||||||
|
/// Polling a phase the engine publishes, not a fixed sleep: a suite whose
|
||||||
|
/// result depends on how fast the machine is will eventually be ignored.
|
||||||
|
fn settle(&mut self) {
|
||||||
|
let deadline = Instant::now() + Duration::from_secs(15);
|
||||||
|
while Instant::now() < deadline {
|
||||||
|
if self.player.snapshot().phase != Phase::Opening {
|
||||||
|
// Let the deferred seek land and one position tick arrive.
|
||||||
|
std::thread::sleep(Duration::from_millis(300));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::thread::sleep(Duration::from_millis(25));
|
||||||
|
}
|
||||||
|
eprintln!(" ! settle timed out - engine stayed in Opening");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// mpv is on a null audio device here, so silence cannot be observed.
|
||||||
|
/// Reporting `None` skips those assertions rather than passing them
|
||||||
|
/// vacuously — an assertion that cannot fail is worse than an absent one.
|
||||||
|
fn audible(&mut self) -> Option<bool> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Keyframe granularity: mpv lands on the nearest one, not on the request.
|
||||||
|
fn seek_tolerance(&self) -> Duration {
|
||||||
|
Duration::from_secs(10)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! run {
|
||||||
|
($failed:ident, $url:expr, $make:expr, $case:path) => {{
|
||||||
|
let name = stringify!($case).rsplit("::").next().unwrap();
|
||||||
|
print!(" {name:.<52}");
|
||||||
|
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||||
|
let mut h = EngineHarness {
|
||||||
|
player: $make,
|
||||||
|
url: $url.to_string(),
|
||||||
|
};
|
||||||
|
$case(&mut h);
|
||||||
|
// Leave nothing playing behind for the next case.
|
||||||
|
let _ = h.player.close();
|
||||||
|
}));
|
||||||
|
match result {
|
||||||
|
Ok(()) => println!(" ok"),
|
||||||
|
Err(_) => {
|
||||||
|
println!(" FAILED");
|
||||||
|
$failed += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Which engine to interrogate.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Engine {
|
||||||
|
/// The `MediaPlayer` implementation.
|
||||||
|
Mpv,
|
||||||
|
/// The old `PlayerBackend`, driven through `LegacyPlayer`.
|
||||||
|
///
|
||||||
|
/// Present so the difference between the two designs can be *demonstrated*
|
||||||
|
/// on the same engine and the same media, rather than argued.
|
||||||
|
Legacy,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run every conformance case against `engine`. Returns the failure count.
|
||||||
|
pub fn run_engine(url: &str, engine: Engine) -> u32 {
|
||||||
|
println!("MediaPlayer conformance - {engine:?}");
|
||||||
|
println!("media: {url}\n");
|
||||||
|
|
||||||
|
let mut failed = 0u32;
|
||||||
|
use crate::player::conformance as c;
|
||||||
|
|
||||||
|
macro_rules! all_cases {
|
||||||
|
($make:expr) => {
|
||||||
|
run!(failed, url, $make, c::opens_from_the_beginning);
|
||||||
|
run!(failed, url, $make, c::opens_at_a_start_position);
|
||||||
|
run!(failed, url, $make, c::seek_while_opening_is_honoured);
|
||||||
|
run!(failed, url, $make, c::seek_while_opening_overrides_start);
|
||||||
|
run!(failed, url, $make, c::seeks_after_open);
|
||||||
|
run!(failed, url, $make, c::pause_and_play_are_observable);
|
||||||
|
run!(failed, url, $make, c::close_is_silent_and_idempotent);
|
||||||
|
run!(failed, url, $make, c::close_during_open_never_plays);
|
||||||
|
run!(failed, url, $make, c::transport_settings_round_trip);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
match engine {
|
||||||
|
Engine::Mpv => {
|
||||||
|
all_cases!(MpvPlayer::new(Output::Null).expect("could not create mpv"));
|
||||||
|
}
|
||||||
|
Engine::Legacy => {
|
||||||
|
all_cases!(LegacyPlayer::new(
|
||||||
|
MpvBackend::new(
|
||||||
|
None,
|
||||||
|
std::sync::Arc::new(tokio::sync::Mutex::new(None)),
|
||||||
|
std::sync::Arc::new(crate::playback_reporting::throttle::EventThrottler::new()),
|
||||||
|
)
|
||||||
|
.expect("could not create the legacy backend")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if failed == 0 {
|
||||||
|
println!("\nall cases passed");
|
||||||
|
} else {
|
||||||
|
println!("\n{failed} case(s) failed");
|
||||||
|
}
|
||||||
|
failed
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Default entry point: the new engine.
|
||||||
|
pub fn run(url: &str) -> u32 {
|
||||||
|
run_engine(url, Engine::Mpv)
|
||||||
|
}
|
||||||
+59
-49
@@ -2,6 +2,10 @@
|
|||||||
mod android_context;
|
mod android_context;
|
||||||
mod auth;
|
mod auth;
|
||||||
mod commands;
|
mod commands;
|
||||||
|
/// The MediaPlayer conformance suite, exposed for the `player-conformance`
|
||||||
|
/// binary. One entry point rather than a public player module tree.
|
||||||
|
#[cfg(feature = "conformance")]
|
||||||
|
pub mod conformance_runner;
|
||||||
mod connectivity;
|
mod connectivity;
|
||||||
mod credentials;
|
mod credentials;
|
||||||
mod domain;
|
mod domain;
|
||||||
@@ -1210,55 +1214,6 @@ pub fn run() {
|
|||||||
// listened for on the frontend via the generated bindings.
|
// listened for on the frontend via the generated bindings.
|
||||||
builder.mount_events(app);
|
builder.mount_events(app);
|
||||||
|
|
||||||
// Native video surface: put a GL area under Tauri's webview so mpv
|
|
||||||
// can draw beneath the controls (UR-080 / DR-231).
|
|
||||||
//
|
|
||||||
// 🔴 OFF BY DEFAULT — the naive reparent crashes the app on the
|
|
||||||
// first click. `tauri-runtime-wry`'s undecorated-resizing handler
|
|
||||||
// walks a hard-coded two-hop path on every button press in the
|
|
||||||
// webview:
|
|
||||||
//
|
|
||||||
// webview.parent() // "This one should be GtkBox"
|
|
||||||
// .parent() // ...and this one the GtkWindow
|
|
||||||
// .downcast::<gtk::Window>().unwrap()
|
|
||||||
//
|
|
||||||
// Wrapping the webview in a GtkOverlay makes that chain
|
|
||||||
// webview → GtkOverlay → GtkBox, the downcast fails, and because the
|
|
||||||
// panic is non-unwinding it aborts the process. The decoration check
|
|
||||||
// that would otherwise make this handler inert runs *after* the
|
|
||||||
// unwrap, so no window configuration avoids it.
|
|
||||||
//
|
|
||||||
// This is the "only place Tauri-specific behaviour could still bite"
|
|
||||||
// that the spike named as the untested half of G1. It bites. The
|
|
||||||
// surface attaches perfectly and then dies on interaction, so
|
|
||||||
// "attached successfully" in the log is not the gate — a click is.
|
|
||||||
//
|
|
||||||
// Kept behind an env var rather than deleted so the next attempt has
|
|
||||||
// something to iterate on: JELLYTAU_NATIVE_VIDEO=1 bun run tauri dev
|
|
||||||
//
|
|
||||||
// TRACES: UR-080 | DR-231
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
if std::env::var("JELLYTAU_NATIVE_VIDEO").as_deref() == Ok("1") {
|
|
||||||
use tauri::Manager;
|
|
||||||
log::warn!(
|
|
||||||
"[INIT] JELLYTAU_NATIVE_VIDEO=1 — attaching the experimental \
|
|
||||||
video surface; the app will abort on the first click until \
|
|
||||||
the widget-tree shape is solved (DR-231)"
|
|
||||||
);
|
|
||||||
if let Some(window) = app.get_webview_window("main") {
|
|
||||||
match window.default_vbox() {
|
|
||||||
Ok(vbox) => match crate::player::video_surface::attach(&vbox) {
|
|
||||||
Ok(_surface) => {
|
|
||||||
info!("[INIT] Native video surface attached");
|
|
||||||
}
|
|
||||||
Err(e) => log::warn!("[INIT] Native video surface unavailable: {e}"),
|
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
log::warn!("[INIT] No GTK vbox for the main window: {e}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// In-app update, desktop only.
|
// In-app update, desktop only.
|
||||||
//
|
//
|
||||||
@@ -1399,6 +1354,61 @@ pub fn run() {
|
|||||||
playback_reporter.clone(),
|
playback_reporter.clone(),
|
||||||
position_throttler.clone(),
|
position_throttler.clone(),
|
||||||
);
|
);
|
||||||
|
// Attached *after* the backend exists: the mpv handle is registered
|
||||||
|
// during its construction, and doing this in the order the code
|
||||||
|
// used to read produced "no mpv handle" every time — the surface was
|
||||||
|
// built before there was anything to draw from.
|
||||||
|
// Native video surface: put a GL area under Tauri's webview so mpv
|
||||||
|
// can draw beneath the controls (UR-080 / DR-231).
|
||||||
|
//
|
||||||
|
// 🔴 OFF BY DEFAULT — the naive reparent crashes the app on the
|
||||||
|
// first click. `tauri-runtime-wry`'s undecorated-resizing handler
|
||||||
|
// walks a hard-coded two-hop path on every button press in the
|
||||||
|
// webview:
|
||||||
|
//
|
||||||
|
// webview.parent() // "This one should be GtkBox"
|
||||||
|
// .parent() // ...and this one the GtkWindow
|
||||||
|
// .downcast::<gtk::Window>().unwrap()
|
||||||
|
//
|
||||||
|
// Wrapping the webview in a GtkOverlay makes that chain
|
||||||
|
// webview → GtkOverlay → GtkBox, the downcast fails, and because the
|
||||||
|
// panic is non-unwinding it aborts the process. The decoration check
|
||||||
|
// that would otherwise make this handler inert runs *after* the
|
||||||
|
// unwrap, so no window configuration avoids it.
|
||||||
|
//
|
||||||
|
// This is the "only place Tauri-specific behaviour could still bite"
|
||||||
|
// that the spike named as the untested half of G1. It bites. The
|
||||||
|
// surface attaches perfectly and then dies on interaction, so
|
||||||
|
// "attached successfully" in the log is not the gate — a click is.
|
||||||
|
//
|
||||||
|
// Kept behind an env var rather than deleted so the next attempt has
|
||||||
|
// something to iterate on: JELLYTAU_NATIVE_VIDEO=1 bun run tauri dev
|
||||||
|
//
|
||||||
|
// TRACES: UR-080 | DR-231
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
if crate::player::native_video::enabled() {
|
||||||
|
use tauri::Manager;
|
||||||
|
log::warn!(
|
||||||
|
"[INIT] JELLYTAU_NATIVE_VIDEO=1 — attaching the experimental \
|
||||||
|
video surface (mpv drawn behind the webview, no reparenting)"
|
||||||
|
);
|
||||||
|
if let Some(window) = app.get_webview_window("main") {
|
||||||
|
match window.default_vbox() {
|
||||||
|
Ok(vbox) => {
|
||||||
|
let handle = crate::player::mpv_backend::registered_handle();
|
||||||
|
if crate::player::video_surface::attach(&vbox, handle) {
|
||||||
|
info!("[INIT] Native video surface attached");
|
||||||
|
} else {
|
||||||
|
log::warn!("[INIT] Native video surface unavailable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("[INIT] No GTK vbox for the main window: {e}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let player_controller = PlayerController::new(
|
let player_controller = PlayerController::new(
|
||||||
backend,
|
backend,
|
||||||
playback_reporter.clone(),
|
playback_reporter.clone(),
|
||||||
|
|||||||
@@ -0,0 +1,277 @@
|
|||||||
|
//! The conformance suite every [`MediaPlayer`] must pass.
|
||||||
|
//!
|
||||||
|
//! One set of behaviours, run against every engine: `FakePlayer` and `MpvPlayer`
|
||||||
|
//! in `cargo test`, `ExoPlayerPlayer` instrumented on a device, `WebviewPlayer`
|
||||||
|
//! in vitest. A new engine is finished when it passes this.
|
||||||
|
//!
|
||||||
|
//! Written *before* the second engine on purpose. A suite written afterwards
|
||||||
|
//! encodes whatever the first engine happened to do, which is how three separate
|
||||||
|
//! playback implementations drifted apart in the first place.
|
||||||
|
//!
|
||||||
|
//! Each case names the defect it exists to prevent. Two of them —
|
||||||
|
//! [`opens_at_a_start_position`] and [`seek_while_opening_is_honoured`] — fail
|
||||||
|
//! against the pre-migration mpv path, which is what makes them a reproduction
|
||||||
|
//! of DR-241 rather than a restatement of it.
|
||||||
|
//!
|
||||||
|
//! Engines differ in *when* an open completes, so the suite drives that through
|
||||||
|
//! a [`Harness`] rather than sleeping: the fake completes on demand, mpv waits
|
||||||
|
//! for its `FileLoaded` event, ExoPlayer for `STATE_READY`.
|
||||||
|
//!
|
||||||
|
//! Available to `cargo test` and, behind the `conformance` feature, to the
|
||||||
|
//! `player-conformance` binary — so an engine that cannot run in-process
|
||||||
|
//! (ExoPlayer on a device) is driven by exactly the same cases rather than by a
|
||||||
|
//! second, drifting checklist.
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-081 | DR-243 | UT-220
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::media_player::{MediaPlayer, OpenRequest, Phase};
|
||||||
|
|
||||||
|
/// How the suite drives one engine.
|
||||||
|
pub trait Harness {
|
||||||
|
type Player: MediaPlayer;
|
||||||
|
|
||||||
|
fn player(&mut self) -> &mut Self::Player;
|
||||||
|
|
||||||
|
/// A request this engine can actually open, at `start`.
|
||||||
|
fn request(&self, start: Duration) -> OpenRequest;
|
||||||
|
|
||||||
|
/// Block until an in-flight `open` has finished (or failed).
|
||||||
|
///
|
||||||
|
/// The fake completes on demand; a real engine waits for its own readiness
|
||||||
|
/// event. Never a sleep — a timing-dependent suite is worse than none.
|
||||||
|
fn settle(&mut self);
|
||||||
|
|
||||||
|
/// Whether the engine is producing audio. Engines that cannot answer may
|
||||||
|
/// return `None`, which skips the silence assertions rather than passing
|
||||||
|
/// them vacuously.
|
||||||
|
fn audible(&mut self) -> Option<bool>;
|
||||||
|
|
||||||
|
/// How far a landed position may differ from the one asked for. Keyframe
|
||||||
|
/// granularity makes exactness the wrong bar for a real decoder.
|
||||||
|
fn seek_tolerance(&self) -> Duration {
|
||||||
|
Duration::from_secs(5)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert_near(actual: Duration, expected: Duration, tolerance: Duration, what: &str) {
|
||||||
|
let delta = actual.abs_diff(expected);
|
||||||
|
assert!(
|
||||||
|
delta <= tolerance,
|
||||||
|
"{what}: expected ~{expected:?}, got {actual:?} (tolerance {tolerance:?})"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Opening at zero reaches a usable state and starts near the beginning.
|
||||||
|
pub fn opens_from_the_beginning<H: Harness>(h: &mut H) {
|
||||||
|
let req = h.request(Duration::ZERO);
|
||||||
|
h.player().open(req).expect("open failed");
|
||||||
|
h.settle();
|
||||||
|
|
||||||
|
let s = h.player().snapshot();
|
||||||
|
assert!(
|
||||||
|
matches!(s.phase, Phase::Playing | Phase::Ready),
|
||||||
|
"after open the engine should hold media, phase was {:?}",
|
||||||
|
s.phase
|
||||||
|
);
|
||||||
|
assert_near(
|
||||||
|
s.position,
|
||||||
|
Duration::ZERO,
|
||||||
|
h.seek_tolerance(),
|
||||||
|
"start of item",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// **DR-241.** Opening at a position starts *there*, not at zero.
|
||||||
|
///
|
||||||
|
/// The whole reason `OpenRequest` carries `start`. Under the previous contract a
|
||||||
|
/// caller had to `load()` then `seek()`, and because `loadfile` is asynchronous
|
||||||
|
/// the seek was issued against a player with nothing loaded, failed, and was
|
||||||
|
/// discarded — so resume and transcoded skip both played from the beginning.
|
||||||
|
pub fn opens_at_a_start_position<H: Harness>(h: &mut H) {
|
||||||
|
let start = Duration::from_secs(600);
|
||||||
|
let req = h.request(start);
|
||||||
|
h.player().open(req).expect("open failed");
|
||||||
|
h.settle();
|
||||||
|
|
||||||
|
let s = h.player().snapshot();
|
||||||
|
assert_ne!(
|
||||||
|
s.position,
|
||||||
|
Duration::ZERO,
|
||||||
|
"opened at {start:?} but playback began at zero - the start position was dropped"
|
||||||
|
);
|
||||||
|
assert_near(s.position, start, h.seek_tolerance(), "start position");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// **DR-241.** A seek issued while opening is honoured, not lost.
|
||||||
|
///
|
||||||
|
/// The engine owns this window; no caller can avoid it, because a caller cannot
|
||||||
|
/// see when the pipeline becomes ready.
|
||||||
|
pub fn seek_while_opening_is_honoured<H: Harness>(h: &mut H) {
|
||||||
|
let target = Duration::from_secs(300);
|
||||||
|
let req = h.request(Duration::ZERO);
|
||||||
|
h.player().open(req).expect("open failed");
|
||||||
|
|
||||||
|
// Deliberately before settle(): this is the race, expressed on purpose.
|
||||||
|
h.player().seek(target).expect("seek during open failed");
|
||||||
|
h.settle();
|
||||||
|
|
||||||
|
let s = h.player().snapshot();
|
||||||
|
assert_near(
|
||||||
|
s.position,
|
||||||
|
target,
|
||||||
|
h.seek_tolerance(),
|
||||||
|
"seek issued while opening",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A later intent wins: the seek replaces the start position it overtook.
|
||||||
|
pub fn seek_while_opening_overrides_start<H: Harness>(h: &mut H) {
|
||||||
|
let start = Duration::from_secs(600);
|
||||||
|
let target = Duration::from_secs(120);
|
||||||
|
let req = h.request(start);
|
||||||
|
h.player().open(req).expect("open failed");
|
||||||
|
h.player().seek(target).expect("seek during open failed");
|
||||||
|
h.settle();
|
||||||
|
|
||||||
|
assert_near(
|
||||||
|
h.player().snapshot().position,
|
||||||
|
target,
|
||||||
|
h.seek_tolerance(),
|
||||||
|
"seek should override the start position it overtook",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Seeking a settled item lands where asked.
|
||||||
|
pub fn seeks_after_open<H: Harness>(h: &mut H) {
|
||||||
|
let req = h.request(Duration::ZERO);
|
||||||
|
h.player().open(req).expect("open failed");
|
||||||
|
h.settle();
|
||||||
|
|
||||||
|
let target = Duration::from_secs(420);
|
||||||
|
h.player().seek(target).expect("seek failed");
|
||||||
|
|
||||||
|
assert_near(
|
||||||
|
h.player().snapshot().position,
|
||||||
|
target,
|
||||||
|
h.seek_tolerance(),
|
||||||
|
"seek after open",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// **DR-239.** Pause and play are reflected in the engine's own state.
|
||||||
|
///
|
||||||
|
/// An engine that changes nothing observable is indistinguishable from one that
|
||||||
|
/// ignored the call — which is exactly how a handler for mpv's `pause` property
|
||||||
|
/// sat unreachable while the UI waited for an event that never came.
|
||||||
|
pub fn pause_and_play_are_observable<H: Harness>(h: &mut H) {
|
||||||
|
let req = h.request(Duration::ZERO);
|
||||||
|
h.player().open(req).expect("open failed");
|
||||||
|
h.settle();
|
||||||
|
|
||||||
|
h.player().pause().expect("pause failed");
|
||||||
|
assert_eq!(
|
||||||
|
h.player().snapshot().phase,
|
||||||
|
Phase::Paused,
|
||||||
|
"pause must be visible in the snapshot"
|
||||||
|
);
|
||||||
|
if let Some(audible) = h.audible() {
|
||||||
|
assert!(!audible, "a paused engine must be silent");
|
||||||
|
}
|
||||||
|
|
||||||
|
h.player().play().expect("play failed");
|
||||||
|
assert_eq!(
|
||||||
|
h.player().snapshot().phase,
|
||||||
|
Phase::Playing,
|
||||||
|
"play must be visible in the snapshot"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `close()` reaches Idle, is silent, and can be called twice.
|
||||||
|
pub fn close_is_silent_and_idempotent<H: Harness>(h: &mut H) {
|
||||||
|
let req = h.request(Duration::ZERO);
|
||||||
|
h.player().open(req).expect("open failed");
|
||||||
|
h.settle();
|
||||||
|
|
||||||
|
h.player().close().expect("close failed");
|
||||||
|
assert_eq!(h.player().snapshot().phase, Phase::Idle);
|
||||||
|
if let Some(audible) = h.audible() {
|
||||||
|
assert!(!audible, "a closed engine must be silent");
|
||||||
|
}
|
||||||
|
|
||||||
|
h.player().close().expect("close must be idempotent");
|
||||||
|
assert_eq!(h.player().snapshot().phase, Phase::Idle);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Closing during an open must not let playback start afterwards.
|
||||||
|
///
|
||||||
|
/// The shape of the "audio keeps playing after leaving the player" report: an
|
||||||
|
/// open still in flight completed after the stop, and nothing was left to tell
|
||||||
|
/// it not to.
|
||||||
|
pub fn close_during_open_never_plays<H: Harness>(h: &mut H) {
|
||||||
|
let req = h.request(Duration::ZERO);
|
||||||
|
h.player().open(req).expect("open failed");
|
||||||
|
h.player().close().expect("close during open failed");
|
||||||
|
h.settle();
|
||||||
|
|
||||||
|
let s = h.player().snapshot();
|
||||||
|
assert!(
|
||||||
|
!s.phase.is_active(),
|
||||||
|
"an open cancelled by close must not start playing, phase was {:?}",
|
||||||
|
s.phase
|
||||||
|
);
|
||||||
|
if let Some(audible) = h.audible() {
|
||||||
|
assert!(!audible, "an engine closed during open must be silent");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Volume, mute and rate round-trip through the snapshot.
|
||||||
|
pub fn transport_settings_round_trip<H: Harness>(h: &mut H) {
|
||||||
|
let req = h.request(Duration::ZERO);
|
||||||
|
h.player().open(req).expect("open failed");
|
||||||
|
h.settle();
|
||||||
|
|
||||||
|
h.player().set_volume(0.25).expect("set_volume failed");
|
||||||
|
h.player().set_muted(true).expect("set_muted failed");
|
||||||
|
h.player().set_rate(1.5).expect("set_rate failed");
|
||||||
|
|
||||||
|
let s = h.player().snapshot();
|
||||||
|
assert!((s.volume - 0.25).abs() < 0.01, "volume did not round-trip");
|
||||||
|
assert!(s.muted, "mute did not round-trip");
|
||||||
|
assert!((s.rate - 1.5).abs() < 0.01, "rate did not round-trip");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run every case against one engine.
|
||||||
|
///
|
||||||
|
/// Each case gets a fresh harness, because a suite whose cases depend on each
|
||||||
|
/// other's leftovers is one that hides state bugs instead of finding them.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! media_player_conformance {
|
||||||
|
($name:ident, $make:expr) => {
|
||||||
|
mod $name {
|
||||||
|
use super::*;
|
||||||
|
use $crate::player::conformance as c;
|
||||||
|
|
||||||
|
macro_rules! case {
|
||||||
|
($case:ident) => {
|
||||||
|
#[test]
|
||||||
|
fn $case() {
|
||||||
|
let mut h = $make;
|
||||||
|
c::$case(&mut h);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
case!(opens_from_the_beginning);
|
||||||
|
case!(opens_at_a_start_position);
|
||||||
|
case!(seek_while_opening_is_honoured);
|
||||||
|
case!(seek_while_opening_overrides_start);
|
||||||
|
case!(seeks_after_open);
|
||||||
|
case!(pause_and_play_are_observable);
|
||||||
|
case!(close_is_silent_and_idempotent);
|
||||||
|
case!(close_during_open_never_plays);
|
||||||
|
case!(transport_settings_round_trip);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,230 @@
|
|||||||
|
//! A deterministic in-memory [`MediaPlayer`], for tests.
|
||||||
|
//!
|
||||||
|
//! Two jobs:
|
||||||
|
//!
|
||||||
|
//! 1. Give the conformance suite something that is correct by construction, so a
|
||||||
|
//! failure there means the *suite* is wrong rather than an engine.
|
||||||
|
//! 2. Let everything above the engine — controller, queue, autoplay, sleep
|
||||||
|
//! timer, session — be tested with no mpv, no device and no network. Most of
|
||||||
|
//! that logic is currently only reachable through a real engine, which is why
|
||||||
|
//! so little of it is covered.
|
||||||
|
//!
|
||||||
|
//! It models the one behaviour that matters most: **opening is not
|
||||||
|
//! instantaneous**. `open()` lands in [`Phase::Opening`] and stays there until
|
||||||
|
//! [`FakePlayer::complete_open`] is called, so a test can put a `seek` into that
|
||||||
|
//! window on purpose. That is the window DR-241 lived in.
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-081 | DR-243
|
||||||
|
|
||||||
|
// `tick` and `fail_open` are for tests not yet written — the controller-level
|
||||||
|
// ones DR-245 unlocks. Remove this allow once those exist.
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::backend::PlayerError;
|
||||||
|
use super::media_player::{Capabilities, MediaPlayer, OpenRequest, Phase, PlaybackSnapshot};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum FakeEvent {
|
||||||
|
Opened { url: String, start: Duration },
|
||||||
|
Played,
|
||||||
|
Paused,
|
||||||
|
Closed,
|
||||||
|
Sought(Duration),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FakePlayer {
|
||||||
|
snapshot: PlaybackSnapshot,
|
||||||
|
/// Set while `Opening`; applied when the open completes.
|
||||||
|
pending_start: Duration,
|
||||||
|
/// A seek that arrived while opening. Honoured on completion, never dropped.
|
||||||
|
deferred_seek: Option<Duration>,
|
||||||
|
autoplay: bool,
|
||||||
|
duration: Duration,
|
||||||
|
/// Every call, in order — so tests can assert what an engine was *asked* to
|
||||||
|
/// do, not only where it ended up.
|
||||||
|
pub log: Vec<FakeEvent>,
|
||||||
|
/// Whether audio is being produced. `close()` must clear it; the bug that
|
||||||
|
/// motivated all this had a "stopped" player that was still audible.
|
||||||
|
pub audible: bool,
|
||||||
|
pub capabilities: Capabilities,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for FakePlayer {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FakePlayer {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
snapshot: PlaybackSnapshot::default(),
|
||||||
|
pending_start: Duration::ZERO,
|
||||||
|
deferred_seek: None,
|
||||||
|
autoplay: true,
|
||||||
|
duration: Duration::from_secs(3600),
|
||||||
|
log: Vec::new(),
|
||||||
|
audible: false,
|
||||||
|
capabilities: Capabilities {
|
||||||
|
video: true,
|
||||||
|
audio_settings: true,
|
||||||
|
subtitle_switching: true,
|
||||||
|
audio_track_switching: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The item this fake will report once opened.
|
||||||
|
pub fn with_duration(mut self, duration: Duration) -> Self {
|
||||||
|
self.duration = duration;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finish an in-flight `open`, as a real engine's "file loaded" would.
|
||||||
|
///
|
||||||
|
/// Applies the requested start position, then any seek that arrived while
|
||||||
|
/// opening — the later intent wins.
|
||||||
|
pub fn complete_open(&mut self) {
|
||||||
|
if self.snapshot.phase != Phase::Opening {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.snapshot.duration = Some(self.duration);
|
||||||
|
self.snapshot.seekable = true;
|
||||||
|
self.snapshot.position = self.deferred_seek.take().unwrap_or(self.pending_start);
|
||||||
|
if self.autoplay {
|
||||||
|
self.snapshot.phase = Phase::Playing;
|
||||||
|
self.audible = true;
|
||||||
|
} else {
|
||||||
|
self.snapshot.phase = Phase::Ready;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Advance playback, for tests that care about time passing.
|
||||||
|
pub fn tick(&mut self, by: Duration) {
|
||||||
|
if self.snapshot.phase.is_active() {
|
||||||
|
self.snapshot.position = (self.snapshot.position + by).min(self.duration);
|
||||||
|
if self.snapshot.position >= self.duration {
|
||||||
|
self.snapshot.phase = Phase::Ended;
|
||||||
|
self.audible = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fail_open(&mut self, why: &str) {
|
||||||
|
self.snapshot.phase = Phase::Failed(why.to_string());
|
||||||
|
self.audible = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MediaPlayer for FakePlayer {
|
||||||
|
fn open(&mut self, req: OpenRequest) -> Result<(), PlayerError> {
|
||||||
|
self.log.push(FakeEvent::Opened {
|
||||||
|
url: req.selection.url.clone(),
|
||||||
|
start: req.start,
|
||||||
|
});
|
||||||
|
self.snapshot = PlaybackSnapshot {
|
||||||
|
phase: Phase::Opening,
|
||||||
|
volume: self.snapshot.volume,
|
||||||
|
muted: self.snapshot.muted,
|
||||||
|
rate: self.snapshot.rate,
|
||||||
|
audio_track: req.audio_track,
|
||||||
|
subtitle_track: req.subtitle_track,
|
||||||
|
..PlaybackSnapshot::default()
|
||||||
|
};
|
||||||
|
self.pending_start = req.start;
|
||||||
|
self.deferred_seek = None;
|
||||||
|
self.autoplay = req.autoplay;
|
||||||
|
self.audible = false;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn play(&mut self) -> Result<(), PlayerError> {
|
||||||
|
self.log.push(FakeEvent::Played);
|
||||||
|
if self.snapshot.phase.has_media() {
|
||||||
|
if self.snapshot.phase == Phase::Opening {
|
||||||
|
self.autoplay = true;
|
||||||
|
} else {
|
||||||
|
self.snapshot.phase = Phase::Playing;
|
||||||
|
self.audible = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pause(&mut self) -> Result<(), PlayerError> {
|
||||||
|
self.log.push(FakeEvent::Paused);
|
||||||
|
if self.snapshot.phase == Phase::Opening {
|
||||||
|
self.autoplay = false;
|
||||||
|
} else if self.snapshot.phase.has_media() {
|
||||||
|
self.snapshot.phase = Phase::Paused;
|
||||||
|
self.audible = false;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close(&mut self) -> Result<(), PlayerError> {
|
||||||
|
self.log.push(FakeEvent::Closed);
|
||||||
|
self.snapshot = PlaybackSnapshot {
|
||||||
|
volume: self.snapshot.volume,
|
||||||
|
muted: self.snapshot.muted,
|
||||||
|
rate: self.snapshot.rate,
|
||||||
|
..PlaybackSnapshot::default()
|
||||||
|
};
|
||||||
|
self.pending_start = Duration::ZERO;
|
||||||
|
self.deferred_seek = None;
|
||||||
|
// An open that was still in flight must not come back to life.
|
||||||
|
self.autoplay = false;
|
||||||
|
self.audible = false;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn seek(&mut self, to: Duration) -> Result<(), PlayerError> {
|
||||||
|
self.log.push(FakeEvent::Sought(to));
|
||||||
|
match self.snapshot.phase {
|
||||||
|
// The window DR-241 lived in: hold it, do not discard it.
|
||||||
|
Phase::Opening => self.deferred_seek = Some(to),
|
||||||
|
Phase::Idle | Phase::Failed(_) => {
|
||||||
|
return Err(PlayerError {
|
||||||
|
message: "seek with nothing open".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_ => self.snapshot.position = to.min(self.duration),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_volume(&mut self, volume: f32) -> Result<(), PlayerError> {
|
||||||
|
self.snapshot.volume = volume.clamp(0.0, 1.0);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_muted(&mut self, muted: bool) -> Result<(), PlayerError> {
|
||||||
|
self.snapshot.muted = muted;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_rate(&mut self, rate: f64) -> Result<(), PlayerError> {
|
||||||
|
self.snapshot.rate = rate;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn select_audio_track(&mut self, index: Option<i32>) -> Result<(), PlayerError> {
|
||||||
|
self.snapshot.audio_track = index;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn select_subtitle_track(&mut self, index: Option<i32>) -> Result<(), PlayerError> {
|
||||||
|
self.snapshot.subtitle_track = index;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn snapshot(&self) -> PlaybackSnapshot {
|
||||||
|
self.snapshot.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn capabilities(&self) -> Capabilities {
|
||||||
|
self.capabilities
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
//! `FakePlayer` runs the conformance suite.
|
||||||
|
//!
|
||||||
|
//! It is correct by construction, so a failure here means the *suite* is wrong,
|
||||||
|
//! not an engine. That is what makes it safe to trust the same cases when they
|
||||||
|
//! fail against a real one.
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-081 | DR-243 | UT-220
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::conformance::Harness;
|
||||||
|
use super::fake_player::FakePlayer;
|
||||||
|
use super::media::MediaItem;
|
||||||
|
use super::media_player::OpenRequest;
|
||||||
|
use crate::repository::stream_selection::StreamSelection;
|
||||||
|
|
||||||
|
struct FakeHarness {
|
||||||
|
player: FakePlayer,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FakeHarness {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
player: FakePlayer::new().with_duration(Duration::from_secs(7200)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Harness for FakeHarness {
|
||||||
|
type Player = FakePlayer;
|
||||||
|
|
||||||
|
fn player(&mut self) -> &mut FakePlayer {
|
||||||
|
&mut self.player
|
||||||
|
}
|
||||||
|
|
||||||
|
fn request(&self, start: Duration) -> OpenRequest {
|
||||||
|
let selection = StreamSelection::local_file("http://example.invalid/stream.mp4");
|
||||||
|
let media = MediaItem::sample("fake-item", &selection.url);
|
||||||
|
OpenRequest::new(media, selection).starting_at(start)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn settle(&mut self) {
|
||||||
|
self.player.complete_open();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn audible(&mut self) -> Option<bool> {
|
||||||
|
Some(self.player.audible)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exact: the fake has no keyframes to round to, so any drift is a bug.
|
||||||
|
fn seek_tolerance(&self) -> Duration {
|
||||||
|
Duration::ZERO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
crate::media_player_conformance!(fake, FakeHarness::new());
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
//! A [`MediaPlayer`] over the old [`PlayerBackend`] trait.
|
||||||
|
//!
|
||||||
|
//! Two purposes.
|
||||||
|
//!
|
||||||
|
//! **Migration.** Engines not yet ported — ExoPlayer, the webview element, the
|
||||||
|
//! null backend — keep working while `PlayerController` moves onto the new
|
||||||
|
//! contract (DR-245). Without this the port would have to land all four engines
|
||||||
|
//! at once.
|
||||||
|
//!
|
||||||
|
//! **Evidence.** It reproduces exactly what every caller used to do: `load`,
|
||||||
|
//! then `play`, then `seek` for a start position. Running the conformance suite
|
||||||
|
//! against it therefore shows the old path failing the cases the new one passes,
|
||||||
|
//! on the same engine and the same media — which is the difference between
|
||||||
|
//! asserting that a design was wrong and demonstrating it.
|
||||||
|
//!
|
||||||
|
//! It is deliberately a faithful reproduction, not a fixed-up one. Making it
|
||||||
|
//! pass would defeat the point.
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-081 | DR-245
|
||||||
|
|
||||||
|
#![allow(dead_code)] // Consumed when PlayerController is ported (DR-245).
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::backend::{PlayerBackend, PlayerError};
|
||||||
|
use super::media_player::{Capabilities, MediaPlayer, OpenRequest, Phase, PlaybackSnapshot};
|
||||||
|
use super::state::PlayerState;
|
||||||
|
|
||||||
|
pub struct LegacyPlayer<B: PlayerBackend> {
|
||||||
|
inner: B,
|
||||||
|
/// The old trait has no notion of "opening", so this is the best the wrapper
|
||||||
|
/// can do: it knows an item was handed over, not whether the engine is ready
|
||||||
|
/// for one. That gap is the whole problem.
|
||||||
|
has_item: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B: PlayerBackend> LegacyPlayer<B> {
|
||||||
|
pub fn new(inner: B) -> Self {
|
||||||
|
Self {
|
||||||
|
inner,
|
||||||
|
has_item: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn inner_mut(&mut self) -> &mut B {
|
||||||
|
&mut self.inner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B: PlayerBackend + Send> MediaPlayer for LegacyPlayer<B> {
|
||||||
|
/// Load, play, then seek — the sequence every caller used to write.
|
||||||
|
///
|
||||||
|
/// The seek is issued immediately, because a caller has no way to know when
|
||||||
|
/// the engine becomes ready. On an engine whose load is asynchronous it
|
||||||
|
/// fails and is discarded, and playback begins at zero: DR-241, reproduced.
|
||||||
|
fn open(&mut self, req: OpenRequest) -> Result<(), PlayerError> {
|
||||||
|
self.inner.load(&req.media)?;
|
||||||
|
self.has_item = true;
|
||||||
|
if req.autoplay {
|
||||||
|
self.inner.play()?;
|
||||||
|
}
|
||||||
|
if !req.start.is_zero() {
|
||||||
|
// Faithfully ignoring the failure, exactly as the old callers did.
|
||||||
|
let _ = self.inner.seek(req.start.as_secs_f64());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn play(&mut self) -> Result<(), PlayerError> {
|
||||||
|
self.inner.play()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pause(&mut self) -> Result<(), PlayerError> {
|
||||||
|
self.inner.pause()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close(&mut self) -> Result<(), PlayerError> {
|
||||||
|
self.has_item = false;
|
||||||
|
self.inner.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn seek(&mut self, to: Duration) -> Result<(), PlayerError> {
|
||||||
|
self.inner.seek(to.as_secs_f64())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_volume(&mut self, volume: f32) -> Result<(), PlayerError> {
|
||||||
|
self.inner.set_volume(volume)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The old trait has no mute. Folding it into volume would lose the user's
|
||||||
|
/// level, so this reports unsupported rather than pretending.
|
||||||
|
fn set_muted(&mut self, _muted: bool) -> Result<(), PlayerError> {
|
||||||
|
Err(PlayerError {
|
||||||
|
message: "mute is not supported by this backend".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_rate(&mut self, _rate: f64) -> Result<(), PlayerError> {
|
||||||
|
Err(PlayerError {
|
||||||
|
message: "playback rate is not supported by this backend".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn select_audio_track(&mut self, index: Option<i32>) -> Result<(), PlayerError> {
|
||||||
|
self.inner.set_audio_track(index.unwrap_or(-1))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn select_subtitle_track(&mut self, index: Option<i32>) -> Result<(), PlayerError> {
|
||||||
|
self.inner.set_subtitle_track(index)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn snapshot(&self) -> PlaybackSnapshot {
|
||||||
|
let phase = match self.inner.state() {
|
||||||
|
_ if !self.has_item => Phase::Idle,
|
||||||
|
PlayerState::Playing { .. } => Phase::Playing,
|
||||||
|
PlayerState::Paused { .. } => Phase::Paused,
|
||||||
|
PlayerState::Idle => Phase::Idle,
|
||||||
|
PlayerState::Error { error, .. } => Phase::Failed(error),
|
||||||
|
// `Loading` is the closest the old trait comes to an opening state,
|
||||||
|
// but it is set once the engine has accepted the item rather than
|
||||||
|
// while it is still accepting it — which is precisely the window it
|
||||||
|
// cannot describe.
|
||||||
|
PlayerState::Loading { .. } | PlayerState::Seeking { .. } => Phase::Ready,
|
||||||
|
};
|
||||||
|
PlaybackSnapshot {
|
||||||
|
phase,
|
||||||
|
position: Duration::from_secs_f64(self.inner.position().max(0.0)),
|
||||||
|
duration: self.inner.duration().map(Duration::from_secs_f64),
|
||||||
|
seekable: true,
|
||||||
|
volume: self.inner.volume(),
|
||||||
|
muted: false,
|
||||||
|
rate: 1.0,
|
||||||
|
audio_track: None,
|
||||||
|
subtitle_track: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn capabilities(&self) -> Capabilities {
|
||||||
|
Capabilities {
|
||||||
|
video: false,
|
||||||
|
audio_settings: true,
|
||||||
|
subtitle_switching: true,
|
||||||
|
audio_track_switching: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -198,6 +198,48 @@ impl MediaItem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl MediaItem {
|
||||||
|
/// A minimal item for tests.
|
||||||
|
///
|
||||||
|
/// The struct has twenty-odd fields, almost none of which any given test
|
||||||
|
/// cares about, and repeating the literal per test is how a new field ends
|
||||||
|
/// up added in thirty places. Set what matters on the result.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-081 | DR-243
|
||||||
|
#[cfg(any(test, feature = "conformance"))]
|
||||||
|
pub fn sample(id: &str, url: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
transport: None,
|
||||||
|
id: id.to_string(),
|
||||||
|
title: id.to_string(),
|
||||||
|
name: None,
|
||||||
|
artist: None,
|
||||||
|
album: None,
|
||||||
|
album_name: None,
|
||||||
|
album_id: None,
|
||||||
|
artist_items: None,
|
||||||
|
artists: None,
|
||||||
|
primary_image_tag: None,
|
||||||
|
image_id: None,
|
||||||
|
item_type: None,
|
||||||
|
playlist_id: None,
|
||||||
|
duration: None,
|
||||||
|
artwork_url: None,
|
||||||
|
media_type: MediaType::Video,
|
||||||
|
source: MediaSource::DirectUrl {
|
||||||
|
url: url.to_string(),
|
||||||
|
},
|
||||||
|
video_codec: None,
|
||||||
|
needs_transcoding: false,
|
||||||
|
video_width: None,
|
||||||
|
video_height: None,
|
||||||
|
subtitles: vec![],
|
||||||
|
series_id: None,
|
||||||
|
server_id: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -0,0 +1,206 @@
|
|||||||
|
//! The `MediaPlayer` contract: one API, interchangeable engines.
|
||||||
|
//!
|
||||||
|
//! See docs/specs/media-player-controller.md.
|
||||||
|
//!
|
||||||
|
//! This replaces [`PlayerBackend`](super::backend::PlayerBackend), which
|
||||||
|
//! abstracts a *device* — `load`, then `seek` — rather than an *intent*. That
|
||||||
|
//! distinction is not academic; it produced four shipped defects in one day:
|
||||||
|
//!
|
||||||
|
//! * A start position was not expressible, so every caller sequenced
|
||||||
|
//! `load()` + `seek()` itself and each raced the engine's asynchronous load
|
||||||
|
//! independently. Resume worked through one caller and silently failed through
|
||||||
|
//! another (DR-241).
|
||||||
|
//! * Whether a stream could be seeked in place was decided *above* the engines,
|
||||||
|
//! by a truth table in a command handler, for engines it does not own (DR-238).
|
||||||
|
//! * Nothing in the contract obliged an engine to report its own state, so a
|
||||||
|
//! handler for mpv's `pause` property sat unreachable and the play/pause
|
||||||
|
//! control never moved (DR-239).
|
||||||
|
//!
|
||||||
|
//! The contract below is written so each of those is a compile-time or
|
||||||
|
//! conformance-time failure rather than a runtime surprise.
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-081 | DR-242
|
||||||
|
|
||||||
|
// Scaffolding: nothing consumes this contract until `PlayerController` is
|
||||||
|
// ported to it (DR-245). Kept out of `cfg(test)` deliberately — it is production
|
||||||
|
// code being built in shippable steps, not a test fixture. Remove this allow
|
||||||
|
// when the controller talks to `MediaPlayer`.
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::backend::PlayerError;
|
||||||
|
use super::media::MediaItem;
|
||||||
|
use crate::repository::stream_selection::StreamSelection;
|
||||||
|
|
||||||
|
/// What an engine is doing right now.
|
||||||
|
///
|
||||||
|
/// `Opening` is the state the previous design could not express, and is the
|
||||||
|
/// direct cause of DR-241: a seek that arrived while the engine had nothing
|
||||||
|
/// loaded had no phase to be queued against, so it was simply discarded and
|
||||||
|
/// playback began at zero.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum Phase {
|
||||||
|
/// Nothing loaded. `close()` must reach this, and must be silent here.
|
||||||
|
Idle,
|
||||||
|
/// An `open` is in flight. Position is not yet meaningful; a `seek` arriving
|
||||||
|
/// now must be honoured once the engine reaches `Ready`, never dropped.
|
||||||
|
Opening,
|
||||||
|
/// Loaded and able to play, but not advancing.
|
||||||
|
Ready,
|
||||||
|
Playing,
|
||||||
|
Paused,
|
||||||
|
/// Reached the end of the item by itself. Distinct from `Idle`, because
|
||||||
|
/// autoplay cares which one happened.
|
||||||
|
Ended,
|
||||||
|
Failed(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Phase {
|
||||||
|
/// Whether the engine currently holds an item.
|
||||||
|
pub fn has_media(&self) -> bool {
|
||||||
|
!matches!(self, Phase::Idle | Phase::Failed(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether playback is advancing.
|
||||||
|
pub fn is_active(&self) -> bool {
|
||||||
|
matches!(self, Phase::Playing)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Everything the UI consumes, read as one coherent value.
|
||||||
|
///
|
||||||
|
/// Deliberately a single snapshot rather than a dozen getters: reading position
|
||||||
|
/// and duration through separate calls is how a paused player reported
|
||||||
|
/// `<position> / 0.0` when a file unloaded between them.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct PlaybackSnapshot {
|
||||||
|
pub phase: Phase,
|
||||||
|
pub position: Duration,
|
||||||
|
/// `None` while unknown — a live stream, or an item still opening.
|
||||||
|
pub duration: Option<Duration>,
|
||||||
|
/// Whether `seek` can be expected to land. False for live edges.
|
||||||
|
pub seekable: bool,
|
||||||
|
/// 0.0 – 1.0.
|
||||||
|
pub volume: f32,
|
||||||
|
pub muted: bool,
|
||||||
|
pub rate: f64,
|
||||||
|
pub audio_track: Option<i32>,
|
||||||
|
pub subtitle_track: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for PlaybackSnapshot {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
phase: Phase::Idle,
|
||||||
|
position: Duration::ZERO,
|
||||||
|
duration: None,
|
||||||
|
seekable: false,
|
||||||
|
volume: 1.0,
|
||||||
|
muted: false,
|
||||||
|
rate: 1.0,
|
||||||
|
audio_track: None,
|
||||||
|
subtitle_track: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What an engine can do, so callers adapt without naming engines.
|
||||||
|
///
|
||||||
|
/// If a caller ever branches on *which* engine it holds, this struct is missing
|
||||||
|
/// something — add it here rather than sniffing. Engine identity leaking into
|
||||||
|
/// callers is the coupling DR-238 came from.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct Capabilities {
|
||||||
|
/// The engine renders pictures, not only sound.
|
||||||
|
pub video: bool,
|
||||||
|
/// Audio settings (EQ, normalisation, gapless) are honoured.
|
||||||
|
pub audio_settings: bool,
|
||||||
|
/// Subtitle tracks can be selected without re-opening.
|
||||||
|
pub subtitle_switching: bool,
|
||||||
|
/// Audio tracks can be selected without re-opening.
|
||||||
|
pub audio_track_switching: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A request to present an item.
|
||||||
|
///
|
||||||
|
/// `start` is the reason this type exists. Carrying it here — rather than
|
||||||
|
/// leaving callers to `seek` after `open` — is what closes the load/seek race,
|
||||||
|
/// because the engine is the only layer that knows when its pipeline can accept
|
||||||
|
/// a position.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct OpenRequest {
|
||||||
|
pub media: MediaItem,
|
||||||
|
pub selection: StreamSelection,
|
||||||
|
/// Where to begin. `Duration::ZERO` means the start of the item.
|
||||||
|
pub start: Duration,
|
||||||
|
pub audio_track: Option<i32>,
|
||||||
|
pub subtitle_track: Option<i32>,
|
||||||
|
/// Begin playing as soon as the engine is able.
|
||||||
|
pub autoplay: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OpenRequest {
|
||||||
|
/// Open at the beginning, playing.
|
||||||
|
pub fn new(media: MediaItem, selection: StreamSelection) -> Self {
|
||||||
|
Self {
|
||||||
|
media,
|
||||||
|
selection,
|
||||||
|
start: Duration::ZERO,
|
||||||
|
audio_track: None,
|
||||||
|
subtitle_track: None,
|
||||||
|
autoplay: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn starting_at(mut self, start: Duration) -> Self {
|
||||||
|
self.start = start;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Anything that can present media.
|
||||||
|
///
|
||||||
|
/// Implementations: `MpvPlayer` (Linux/Windows), `ExoPlayerPlayer` (Android),
|
||||||
|
/// `WebviewPlayer` (HTML5 element), and `FakePlayer` for tests. Every one of
|
||||||
|
/// them must pass [`super::conformance`].
|
||||||
|
pub trait MediaPlayer: Send {
|
||||||
|
/// Present `req.selection`, beginning at `req.start`.
|
||||||
|
///
|
||||||
|
/// One operation, deliberately. An engine that cannot start at an offset
|
||||||
|
/// natively absorbs that internally — by deferring until loaded, or by
|
||||||
|
/// re-opening — because it is the only layer that knows when it can.
|
||||||
|
/// Callers must never follow `open` with a `seek` to achieve a start
|
||||||
|
/// position; that is the bug this signature exists to prevent.
|
||||||
|
fn open(&mut self, req: OpenRequest) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
fn play(&mut self) -> Result<(), PlayerError>;
|
||||||
|
fn pause(&mut self) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
/// Stop and release the current item.
|
||||||
|
///
|
||||||
|
/// Must be **idempotent** and must leave the engine **silent**. "Stopped"
|
||||||
|
/// and "producing no audio" were not the same thing in the previous design,
|
||||||
|
/// and the gap between them is audible.
|
||||||
|
fn close(&mut self) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
/// Seek to an absolute position on the item's own timeline.
|
||||||
|
///
|
||||||
|
/// Whether that is an in-place seek or a re-open of the stream is the
|
||||||
|
/// engine's business: hls.js seeks within a VOD playlist, mpv's HLS demuxer
|
||||||
|
/// cannot make a server transcode from a new offset. Callers state the
|
||||||
|
/// destination and nothing else.
|
||||||
|
fn seek(&mut self, to: Duration) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
fn set_volume(&mut self, volume: f32) -> Result<(), PlayerError>;
|
||||||
|
fn set_muted(&mut self, muted: bool) -> Result<(), PlayerError>;
|
||||||
|
fn set_rate(&mut self, rate: f64) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
fn select_audio_track(&mut self, index: Option<i32>) -> Result<(), PlayerError>;
|
||||||
|
fn select_subtitle_track(&mut self, index: Option<i32>) -> Result<(), PlayerError>;
|
||||||
|
|
||||||
|
/// One coherent read of the engine's state.
|
||||||
|
fn snapshot(&self) -> PlaybackSnapshot;
|
||||||
|
|
||||||
|
fn capabilities(&self) -> Capabilities;
|
||||||
|
}
|
||||||
@@ -5,8 +5,19 @@
|
|||||||
pub mod autoplay;
|
pub mod autoplay;
|
||||||
pub mod backend;
|
pub mod backend;
|
||||||
pub mod background_policy;
|
pub mod background_policy;
|
||||||
|
#[cfg(any(test, feature = "conformance"))]
|
||||||
|
pub mod conformance;
|
||||||
pub mod events;
|
pub mod events;
|
||||||
|
#[cfg(any(test, feature = "conformance"))]
|
||||||
|
pub mod fake_player;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod fake_player_conformance;
|
||||||
|
#[cfg(any(test, feature = "conformance"))]
|
||||||
|
pub mod legacy_player;
|
||||||
pub mod media;
|
pub mod media;
|
||||||
|
pub mod media_player;
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
pub mod mpv_player;
|
||||||
pub mod queue;
|
pub mod queue;
|
||||||
pub mod seek;
|
pub mod seek;
|
||||||
pub mod session;
|
pub mod session;
|
||||||
@@ -24,11 +35,22 @@ pub mod android;
|
|||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
pub mod mpv_backend;
|
pub mod mpv_backend;
|
||||||
|
|
||||||
|
/// Whether this process renders video natively — one answer, three consumers
|
||||||
|
/// (UR-080 / DR-231, DR-235).
|
||||||
|
pub mod native_video;
|
||||||
|
|
||||||
|
/// mpv's render API into a framebuffer we own (UR-080 / DR-231, IR-033).
|
||||||
|
///
|
||||||
|
/// Deliberately *not* GTK-gated beyond the platform that currently builds it:
|
||||||
|
/// everything here is the portable half, and Windows reuses it unchanged behind
|
||||||
|
/// its own surface.
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
pub mod mpv_render;
|
||||||
|
|
||||||
/// The native video surface mpv renders into (UR-080 / DR-231).
|
/// The native video surface mpv renders into (UR-080 / DR-231).
|
||||||
///
|
///
|
||||||
/// Linux-gated for now because the surface is GTK. Everything *around* it — the
|
/// Linux-gated because the *surface* is GTK. Everything around it — the render
|
||||||
/// render context, its lifetime, frame pacing, the device profile — is
|
/// context, its lifetime, frame pacing, the device profile — is not.
|
||||||
/// deliberately not, so Windows reuses it behind its own surface.
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
pub mod video_surface;
|
pub mod video_surface;
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,19 @@ pub struct MpvBackend {
|
|||||||
/// through reported 0.0 / unknown exactly when end-of-file handling needed to
|
/// through reported 0.0 / unknown exactly when end-of-file handling needed to
|
||||||
/// know where playback reached. See [`ObservedTime`].
|
/// know where playback reached. See [`ObservedTime`].
|
||||||
observed: Arc<Mutex<ObservedTime>>,
|
observed: Arc<Mutex<ObservedTime>>,
|
||||||
|
/// A seek that arrived before MPV had a file to seek in.
|
||||||
|
///
|
||||||
|
/// `loadfile` is asynchronous: it returns as soon as the command is queued,
|
||||||
|
/// so `time-pos` is not yet a resolvable property and setting it fails. A
|
||||||
|
/// seek issued in that window used to be dropped on the floor, and the two
|
||||||
|
/// callers that do exactly this are the ones a viewer notices — resume, and
|
||||||
|
/// a transcoded seek, both of which re-open the stream and then ask for a
|
||||||
|
/// position. The stream reloaded and played from zero.
|
||||||
|
///
|
||||||
|
/// Held here and applied by the `FileLoaded` arm.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-040, UR-005 | DR-241
|
||||||
|
pending_seek: Arc<Mutex<Option<f64>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct InternalState {
|
struct InternalState {
|
||||||
@@ -89,6 +102,32 @@ fn get_stream_url(media: &MediaItem) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The mpv handle of the backend this process created, for the video surface.
|
||||||
|
///
|
||||||
|
/// A `OnceLock` rather than a field reached through `PlayerBackend`, because the
|
||||||
|
/// trait is cross-platform and a raw mpv pointer is not something every backend
|
||||||
|
/// should have to pretend to have. Stored as `usize` because a raw pointer is
|
||||||
|
/// neither `Send` nor `Sync`; the only consumer is the GTK main thread, which is
|
||||||
|
/// also where mpv was created.
|
||||||
|
///
|
||||||
|
/// Written once at construction and never cleared: the backend outlives the
|
||||||
|
/// window, so there is no window in which this could dangle while a surface is
|
||||||
|
/// still using it.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | DR-231
|
||||||
|
static MPV_HANDLE: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
|
||||||
|
|
||||||
|
/// The registered handle, or null if no MPV backend was created (initialisation
|
||||||
|
/// can fail, and the app falls back to a no-op backend rather than dying).
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | DR-231
|
||||||
|
pub fn registered_handle() -> *mut libmpv_sys::mpv_handle {
|
||||||
|
MPV_HANDLE
|
||||||
|
.get()
|
||||||
|
.map(|p| *p as *mut libmpv_sys::mpv_handle)
|
||||||
|
.unwrap_or(std::ptr::null_mut())
|
||||||
|
}
|
||||||
|
|
||||||
impl MpvBackend {
|
impl MpvBackend {
|
||||||
/// Create a new MPV backend
|
/// Create a new MPV backend
|
||||||
pub fn new(
|
pub fn new(
|
||||||
@@ -137,9 +176,28 @@ impl MpvBackend {
|
|||||||
message: format!("Failed to configure MPV audio-display: {:?}", e),
|
message: format!("Failed to configure MPV audio-display: {:?}", e),
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
// Video is disabled unless this process is drawing it.
|
||||||
|
//
|
||||||
|
// `video: no` is why mpv has never decoded a frame here: Linux video has
|
||||||
|
// always gone through the webview, and decoding it twice would burn a
|
||||||
|
// core for a picture nobody sees. With native video on, mpv needs both
|
||||||
|
// the decoder *and* `vo=libmpv` — the render API only works through that
|
||||||
|
// output, and the default would try to open a window of its own.
|
||||||
|
//
|
||||||
|
// Set at construction because mpv resolves the video output when it
|
||||||
|
// initialises; flipping it later does not re-open one.
|
||||||
|
//
|
||||||
|
// TRACES: UR-080 | DR-231, DR-235
|
||||||
|
if super::native_video::enabled() {
|
||||||
|
mpv.set_property("vo", "libmpv").map_err(|e| PlayerError {
|
||||||
|
message: format!("Failed to select the libmpv video output: {:?}", e),
|
||||||
|
})?;
|
||||||
|
info!("[MpvBackend] native video enabled (vo=libmpv)");
|
||||||
|
} else {
|
||||||
mpv.set_property("video", "no").map_err(|e| PlayerError {
|
mpv.set_property("video", "no").map_err(|e| PlayerError {
|
||||||
message: format!("Failed to configure MPV video: {:?}", e),
|
message: format!("Failed to configure MPV video: {:?}", e),
|
||||||
})?;
|
})?;
|
||||||
|
}
|
||||||
|
|
||||||
// Set volume to 100% (we'll control via MPV's volume property)
|
// Set volume to 100% (we'll control via MPV's volume property)
|
||||||
mpv.set_property("volume", 100i64)
|
mpv.set_property("volume", 100i64)
|
||||||
@@ -178,13 +236,21 @@ impl MpvBackend {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
let backend = MpvBackend {
|
let backend = MpvBackend {
|
||||||
mpv: Arc::new(mpv),
|
mpv: {
|
||||||
|
let mpv = Arc::new(mpv);
|
||||||
|
// Publish the handle for the video surface (DR-231). Ignores a
|
||||||
|
// second call: only one MPV backend is ever constructed, and a
|
||||||
|
// failed re-init must not replace a live handle.
|
||||||
|
let _ = MPV_HANDLE.set(mpv.ctx.as_ptr() as usize);
|
||||||
|
mpv
|
||||||
|
},
|
||||||
state,
|
state,
|
||||||
event_emitter,
|
event_emitter,
|
||||||
audio_settings: AudioSettings::default(),
|
audio_settings: AudioSettings::default(),
|
||||||
playback_reporter,
|
playback_reporter,
|
||||||
position_throttler,
|
position_throttler,
|
||||||
last_seek_time: Arc::new(AtomicU64::new(0)),
|
last_seek_time: Arc::new(AtomicU64::new(0)),
|
||||||
|
pending_seek: Arc::new(Mutex::new(None)),
|
||||||
observed: Arc::new(Mutex::new(ObservedTime::default())),
|
observed: Arc::new(Mutex::new(ObservedTime::default())),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -202,6 +268,7 @@ impl MpvBackend {
|
|||||||
let state = self.state.clone();
|
let state = self.state.clone();
|
||||||
let reporter = self.playback_reporter.clone();
|
let reporter = self.playback_reporter.clone();
|
||||||
let throttler = self.position_throttler.clone();
|
let throttler = self.position_throttler.clone();
|
||||||
|
let pending_seek_for_events = self.pending_seek.clone();
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
info!("[MpvBackend] Event loop started");
|
info!("[MpvBackend] Event loop started");
|
||||||
@@ -211,6 +278,30 @@ impl MpvBackend {
|
|||||||
error!("[MpvBackend] Failed to disable deprecated events: {:?}", e);
|
error!("[MpvBackend] Failed to disable deprecated events: {:?}", e);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// libmpv delivers PropertyChange only for properties registered
|
||||||
|
// here. Every name matched in the loop below needs a line in this
|
||||||
|
// block or its handler is unreachable — an omission that reads as
|
||||||
|
// working code, because the handler is sitting right there.
|
||||||
|
// UT-218 holds the two lists together.
|
||||||
|
//
|
||||||
|
// `pause` drives the play/pause control: the UI consumes
|
||||||
|
// StateChanged rather than tracking playback itself, per the
|
||||||
|
// one-directional state rule. Unobserved, the event never came and
|
||||||
|
// the button never moved. Invisible until native video shipped,
|
||||||
|
// because the webview <video> element's own DOM events drove that
|
||||||
|
// control on Linux.
|
||||||
|
//
|
||||||
|
// TRACES: UR-005 | DR-239
|
||||||
|
ev_ctx
|
||||||
|
.observe_property("pause", libmpv::Format::Flag, 0)
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
|
error!(
|
||||||
|
"[MpvBackend] Failed to observe 'pause': {:?} — the play/pause \
|
||||||
|
control will not follow the player",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match ev_ctx.wait_event(1.0) {
|
match ev_ctx.wait_event(1.0) {
|
||||||
Some(Ok(event)) => match event {
|
Some(Ok(event)) => match event {
|
||||||
@@ -220,6 +311,43 @@ impl MpvBackend {
|
|||||||
libmpv::events::Event::FileLoaded => {
|
libmpv::events::Event::FileLoaded => {
|
||||||
info!("[MpvBackend] File loaded");
|
info!("[MpvBackend] File loaded");
|
||||||
|
|
||||||
|
// Apply a seek that arrived while there was nothing
|
||||||
|
// to seek in. TRACES: UR-040, UR-005 | DR-241
|
||||||
|
{
|
||||||
|
let target = pending_seek_for_events.lock_safe().take();
|
||||||
|
if let Some(position) = target {
|
||||||
|
match mpv.set_property("time-pos", position) {
|
||||||
|
Ok(()) => info!(
|
||||||
|
"[MpvBackend] applied deferred seek to {position}"
|
||||||
|
),
|
||||||
|
Err(e) => warn!(
|
||||||
|
"[MpvBackend] deferred seek to {position} failed: {:?}",
|
||||||
|
e
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Geometry, so "the picture does not fill the screen"
|
||||||
|
// can be attributed rather than guessed at. `width`/
|
||||||
|
// `height` are the decoded frame; `dwidth`/`dheight`
|
||||||
|
// are what mpv will *display* after aspect
|
||||||
|
// correction. A file that carries its letterbox
|
||||||
|
// baked into the picture reports a 16:9 dwidth and
|
||||||
|
// is then pillarboxed on a wider panel — which looks
|
||||||
|
// identical to a rendering bug from outside.
|
||||||
|
{
|
||||||
|
let n = |k: &str| mpv.get_property::<i64>(k).unwrap_or(-1);
|
||||||
|
info!(
|
||||||
|
"[MpvBackend] video geometry: {}x{} decoded, {}x{} display, aspect {:?}",
|
||||||
|
n("width"),
|
||||||
|
n("height"),
|
||||||
|
n("dwidth"),
|
||||||
|
n("dheight"),
|
||||||
|
mpv.get_property::<f64>("video-params/aspect").ok(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Get duration
|
// Get duration
|
||||||
if let Ok(duration) = mpv.get_property::<f64>("duration") {
|
if let Ok(duration) = mpv.get_property::<f64>("duration") {
|
||||||
if let Some(emitter) = &event_emitter {
|
if let Some(emitter) = &event_emitter {
|
||||||
@@ -522,11 +650,24 @@ impl PlayerBackend for MpvBackend {
|
|||||||
.as_millis() as u64;
|
.as_millis() as u64;
|
||||||
self.last_seek_time.store(now, Ordering::Relaxed);
|
self.last_seek_time.store(now, Ordering::Relaxed);
|
||||||
|
|
||||||
self.mpv
|
// `time-pos` only resolves while a file is loaded. `loadfile` is
|
||||||
.set_property("time-pos", position)
|
// asynchronous, so a seek issued straight after a reload — resume, or a
|
||||||
.map_err(|e| PlayerError {
|
// transcoded seek — lands in a window where this fails, and dropping it
|
||||||
message: format!("Failed to seek: {:?}", e),
|
// there is what makes the stream play from zero instead of the position
|
||||||
})?;
|
// that was asked for. Hold it and let `FileLoaded` apply it.
|
||||||
|
// TRACES: UR-040, UR-005 | DR-241
|
||||||
|
if let Err(e) = self.mpv.set_property("time-pos", position) {
|
||||||
|
debug!(
|
||||||
|
"[MpvBackend] seek to {position} deferred until the file loads ({:?})",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
*self.pending_seek.lock_safe() = Some(position);
|
||||||
|
self.observed.lock_safe().record_position(position);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// A seek that lands clears any earlier deferred one: the newer intent wins.
|
||||||
|
*self.pending_seek.lock_safe() = None;
|
||||||
|
|
||||||
// The poll thread suppresses updates for 150ms after a seek, so without
|
// The poll thread suppresses updates for 150ms after a seek, so without
|
||||||
// this a file ending inside that window would report the pre-seek time.
|
// this a file ending inside that window would report the pre-seek time.
|
||||||
|
|||||||
@@ -13,6 +13,52 @@ mod tests {
|
|||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use tokio::sync::Mutex as TokioMutex;
|
use tokio::sync::Mutex as TokioMutex;
|
||||||
|
|
||||||
|
/// Every property the event loop *handles* must also be *observed*.
|
||||||
|
///
|
||||||
|
/// libmpv only delivers `PropertyChange` for properties registered with
|
||||||
|
/// `mpv_observe_property`. A `match` arm for an unobserved property is
|
||||||
|
/// unreachable code that looks exactly like working code: the handler is
|
||||||
|
/// right there, so the behaviour reads as implemented.
|
||||||
|
///
|
||||||
|
/// This cost a real bug. `pause` was handled and never observed, so
|
||||||
|
/// `StateChanged` was never emitted on pause or resume. It stayed invisible
|
||||||
|
/// while Linux video played in the webview, because the `<video>` element's
|
||||||
|
/// own DOM events drove the play/pause control; turning native video on made
|
||||||
|
/// the UI depend on the event that never came, and the button stopped
|
||||||
|
/// responding.
|
||||||
|
///
|
||||||
|
/// Asserted against the source because there is no way to observe the
|
||||||
|
/// registration at runtime without a live mpv instance.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-005 | DR-239 | UT-218
|
||||||
|
#[test]
|
||||||
|
fn test_every_handled_property_is_observed() {
|
||||||
|
let src = include_str!("mpv_backend.rs");
|
||||||
|
|
||||||
|
let handled: Vec<&str> = src
|
||||||
|
.match_indices("PropertyChange { name: \"")
|
||||||
|
.filter_map(|(i, m)| {
|
||||||
|
let rest = &src[i + m.len()..];
|
||||||
|
rest.find('"').map(|end| &rest[..end])
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
!handled.is_empty(),
|
||||||
|
"no PropertyChange arms found - has the event loop been restructured?"
|
||||||
|
);
|
||||||
|
|
||||||
|
for name in handled {
|
||||||
|
let observed = format!("observe_property(\"{name}\"");
|
||||||
|
assert!(
|
||||||
|
src.contains(&observed),
|
||||||
|
"mpv_backend.rs handles PropertyChange for {name:?} but never calls \
|
||||||
|
observe_property({name:?}, ..). libmpv will never deliver that event, \
|
||||||
|
so the handler is dead code."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Test that simulates the position update thread spawning async tasks
|
/// Test that simulates the position update thread spawning async tasks
|
||||||
/// without a Tokio runtime (the bug we just fixed)
|
/// without a Tokio runtime (the bug we just fixed)
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -0,0 +1,378 @@
|
|||||||
|
//! [`MediaPlayer`] over libmpv.
|
||||||
|
//!
|
||||||
|
//! The point of difference from `MpvBackend` is [`MpvPlayer::open`]: the start
|
||||||
|
//! position is applied **at load time**, via mpv's own `start` option, instead
|
||||||
|
//! of being seeked to afterwards. `loadfile` is asynchronous, so a seek issued
|
||||||
|
//! after it targets a player that has nothing loaded, fails, and — under the old
|
||||||
|
//! contract — was discarded. That is DR-241, and it is why resume and transcoded
|
||||||
|
//! skip both played from zero.
|
||||||
|
//!
|
||||||
|
//! A seek arriving during [`Phase::Opening`] is held and applied when the file
|
||||||
|
//! loads, so no caller has to know where that window begins or ends.
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-081, UR-040, UR-005 | DR-244
|
||||||
|
|
||||||
|
#![allow(dead_code)] // Wired to PlayerController in DR-245.
|
||||||
|
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use libmpv::Mpv;
|
||||||
|
use log::{debug, info, warn};
|
||||||
|
|
||||||
|
use super::backend::PlayerError;
|
||||||
|
use super::media_player::{Capabilities, MediaPlayer, OpenRequest, Phase, PlaybackSnapshot};
|
||||||
|
use crate::utils::lock::MutexSafe;
|
||||||
|
|
||||||
|
/// State the event thread writes and the caller reads.
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Shared {
|
||||||
|
phase: Phase,
|
||||||
|
position: Duration,
|
||||||
|
duration: Option<Duration>,
|
||||||
|
seekable: bool,
|
||||||
|
/// A seek that arrived while opening. Applied on `FileLoaded`.
|
||||||
|
deferred_seek: Option<Duration>,
|
||||||
|
/// Cleared by `close()`, so an open still in flight cannot come back to life
|
||||||
|
/// and start playing after the caller has stopped it.
|
||||||
|
open_generation: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Shared {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
phase: Phase::Idle,
|
||||||
|
position: Duration::ZERO,
|
||||||
|
duration: None,
|
||||||
|
seekable: false,
|
||||||
|
deferred_seek: None,
|
||||||
|
open_generation: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MpvPlayer {
|
||||||
|
mpv: Arc<Mpv>,
|
||||||
|
shared: Arc<Mutex<Shared>>,
|
||||||
|
volume: f32,
|
||||||
|
muted: bool,
|
||||||
|
rate: f64,
|
||||||
|
audio_track: Option<i32>,
|
||||||
|
subtitle_track: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How the engine should talk to the machine.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Output {
|
||||||
|
/// Real audio and video. What the app uses.
|
||||||
|
Real,
|
||||||
|
/// No audio device, no window. What conformance uses, so the suite can run
|
||||||
|
/// on a headless runner without claiming the user's speakers.
|
||||||
|
Null,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MpvPlayer {
|
||||||
|
pub fn new(output: Output) -> Result<Self, PlayerError> {
|
||||||
|
// mpv refuses to start under a non-C LC_NUMERIC, and anything that has
|
||||||
|
// initialised GTK before us will have set one.
|
||||||
|
unsafe {
|
||||||
|
let c = std::ffi::CString::new("C").unwrap();
|
||||||
|
libc::setlocale(libc::LC_NUMERIC, c.as_ptr());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mpv = Mpv::new().map_err(|e| PlayerError {
|
||||||
|
message: format!("mpv_create failed: {e:?}"),
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let set = |k: &str, v: &str| {
|
||||||
|
if let Err(e) = mpv.set_property(k, v) {
|
||||||
|
warn!("[MpvPlayer] could not set {k}={v}: {e:?}");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match output {
|
||||||
|
Output::Real => {
|
||||||
|
set("vo", "libmpv");
|
||||||
|
}
|
||||||
|
Output::Null => {
|
||||||
|
set("ao", "null");
|
||||||
|
set("vo", "null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
set("msg-level", "all=warn");
|
||||||
|
// Survive a blip rather than ending the item on it.
|
||||||
|
set(
|
||||||
|
"stream-lavf-o",
|
||||||
|
"reconnect=1,reconnect_streamed=1,reconnect_on_network_error=1,reconnect_delay_max=5",
|
||||||
|
);
|
||||||
|
|
||||||
|
let player = Self {
|
||||||
|
mpv: Arc::new(mpv),
|
||||||
|
shared: Arc::new(Mutex::new(Shared::default())),
|
||||||
|
volume: 1.0,
|
||||||
|
muted: false,
|
||||||
|
rate: 1.0,
|
||||||
|
audio_track: None,
|
||||||
|
subtitle_track: None,
|
||||||
|
};
|
||||||
|
player.spawn_events();
|
||||||
|
Ok(player)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_events(&self) {
|
||||||
|
let mpv = self.mpv.clone();
|
||||||
|
let shared = self.shared.clone();
|
||||||
|
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
let mut ev = mpv.create_event_context();
|
||||||
|
let _ = ev.disable_deprecated_events();
|
||||||
|
// Every property matched below must be observed, or libmpv never
|
||||||
|
// delivers it and the handler is unreachable (DR-239).
|
||||||
|
for prop in ["pause", "eof-reached"] {
|
||||||
|
if let Err(e) = ev.observe_property(prop, libmpv::Format::Flag, 0) {
|
||||||
|
warn!("[MpvPlayer] could not observe {prop}: {e:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match ev.wait_event(0.25) {
|
||||||
|
Some(Ok(libmpv::events::Event::FileLoaded)) => {
|
||||||
|
let deferred = {
|
||||||
|
let mut s = shared.lock_safe();
|
||||||
|
// Closed while opening: do not start.
|
||||||
|
if s.phase == Phase::Idle {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
s.duration = mpv
|
||||||
|
.get_property::<f64>("duration")
|
||||||
|
.ok()
|
||||||
|
.map(Duration::from_secs_f64);
|
||||||
|
s.seekable = mpv.get_property::<bool>("seekable").unwrap_or(true);
|
||||||
|
s.phase = Phase::Playing;
|
||||||
|
s.deferred_seek.take()
|
||||||
|
};
|
||||||
|
if let Some(to) = deferred {
|
||||||
|
debug!("[MpvPlayer] applying deferred seek to {to:?}");
|
||||||
|
if let Err(e) = mpv.set_property("time-pos", to.as_secs_f64()) {
|
||||||
|
warn!("[MpvPlayer] deferred seek failed: {e:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(Ok(libmpv::events::Event::PropertyChange { name: "pause", .. })) => {
|
||||||
|
if let Ok(paused) = mpv.get_property::<bool>("pause") {
|
||||||
|
let mut s = shared.lock_safe();
|
||||||
|
if s.phase.has_media() {
|
||||||
|
s.phase = if paused {
|
||||||
|
Phase::Paused
|
||||||
|
} else {
|
||||||
|
Phase::Playing
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(Ok(libmpv::events::Event::EndFile(reason))) => {
|
||||||
|
let mut s = shared.lock_safe();
|
||||||
|
// 0 = EOF. Anything else is a stop, a quit or an error,
|
||||||
|
// and must not read as "the item finished".
|
||||||
|
s.phase = if reason == 0 {
|
||||||
|
Phase::Ended
|
||||||
|
} else {
|
||||||
|
Phase::Idle
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Some(Ok(libmpv::events::Event::Shutdown)) => break,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(pos) = mpv.get_property::<f64>("time-pos") {
|
||||||
|
let mut s = shared.lock_safe();
|
||||||
|
if s.phase.has_media() && s.deferred_seek.is_none() {
|
||||||
|
s.position = Duration::from_secs_f64(pos.max(0.0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MediaPlayer for MpvPlayer {
|
||||||
|
fn open(&mut self, req: OpenRequest) -> Result<(), PlayerError> {
|
||||||
|
{
|
||||||
|
let mut s = self.shared.lock_safe();
|
||||||
|
*s = Shared {
|
||||||
|
phase: Phase::Opening,
|
||||||
|
open_generation: s.open_generation + 1,
|
||||||
|
..Shared::default()
|
||||||
|
};
|
||||||
|
// Report the requested position immediately, so a caller reading
|
||||||
|
// back during the open sees where it asked to be rather than zero.
|
||||||
|
s.position = req.start;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The whole point. `start` is applied by mpv as it opens the file, so
|
||||||
|
// there is no window in which the position can be asked for and lost.
|
||||||
|
let start = if req.start.is_zero() {
|
||||||
|
"none".to_string()
|
||||||
|
} else {
|
||||||
|
format!("{:.3}", req.start.as_secs_f64())
|
||||||
|
};
|
||||||
|
self.mpv
|
||||||
|
.set_property("start", start.as_str())
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("could not set start position: {e:?}"),
|
||||||
|
})?;
|
||||||
|
self.mpv
|
||||||
|
.set_property("pause", !req.autoplay)
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("could not set pause: {e:?}"),
|
||||||
|
})?;
|
||||||
|
|
||||||
|
info!("[MpvPlayer] open {} at {:?}", req.selection.url, req.start);
|
||||||
|
self.mpv
|
||||||
|
.command("loadfile", &[&req.selection.url, "replace"])
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("loadfile failed: {e:?}"),
|
||||||
|
})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn play(&mut self) -> Result<(), PlayerError> {
|
||||||
|
self.mpv
|
||||||
|
.set_property("pause", false)
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("play failed: {e:?}"),
|
||||||
|
})?;
|
||||||
|
let mut s = self.shared.lock_safe();
|
||||||
|
if s.phase.has_media() && s.phase != Phase::Opening {
|
||||||
|
s.phase = Phase::Playing;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pause(&mut self) -> Result<(), PlayerError> {
|
||||||
|
self.mpv
|
||||||
|
.set_property("pause", true)
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("pause failed: {e:?}"),
|
||||||
|
})?;
|
||||||
|
let mut s = self.shared.lock_safe();
|
||||||
|
if s.phase.has_media() && s.phase != Phase::Opening {
|
||||||
|
s.phase = Phase::Paused;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close(&mut self) -> Result<(), PlayerError> {
|
||||||
|
// State first: an open still in flight checks this on FileLoaded and
|
||||||
|
// must not proceed to play after the caller has stopped it.
|
||||||
|
{
|
||||||
|
let mut s = self.shared.lock_safe();
|
||||||
|
*s = Shared {
|
||||||
|
open_generation: s.open_generation,
|
||||||
|
..Shared::default()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
// Idempotent: stopping an already-stopped mpv is not an error worth
|
||||||
|
// propagating, and callers legitimately close twice on teardown.
|
||||||
|
if let Err(e) = self.mpv.command("stop", &[]) {
|
||||||
|
debug!("[MpvPlayer] stop on an idle player: {e:?}");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn seek(&mut self, to: Duration) -> Result<(), PlayerError> {
|
||||||
|
{
|
||||||
|
let mut s = self.shared.lock_safe();
|
||||||
|
match s.phase {
|
||||||
|
// Held, not dropped. The caller cannot see this window.
|
||||||
|
Phase::Opening => {
|
||||||
|
s.deferred_seek = Some(to);
|
||||||
|
s.position = to;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
Phase::Idle | Phase::Failed(_) => {
|
||||||
|
return Err(PlayerError {
|
||||||
|
message: "seek with nothing open".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_ => s.position = to,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.mpv
|
||||||
|
.set_property("time-pos", to.as_secs_f64())
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("seek failed: {e:?}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_volume(&mut self, volume: f32) -> Result<(), PlayerError> {
|
||||||
|
let clamped = volume.clamp(0.0, 1.0);
|
||||||
|
self.volume = clamped;
|
||||||
|
self.mpv
|
||||||
|
.set_property("volume", (clamped as f64) * 100.0)
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("set_volume failed: {e:?}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_muted(&mut self, muted: bool) -> Result<(), PlayerError> {
|
||||||
|
self.muted = muted;
|
||||||
|
self.mpv
|
||||||
|
.set_property("mute", muted)
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("set_muted failed: {e:?}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_rate(&mut self, rate: f64) -> Result<(), PlayerError> {
|
||||||
|
self.rate = rate;
|
||||||
|
self.mpv
|
||||||
|
.set_property("speed", rate)
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("set_rate failed: {e:?}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn select_audio_track(&mut self, index: Option<i32>) -> Result<(), PlayerError> {
|
||||||
|
self.audio_track = index;
|
||||||
|
let value = index.map(|i| i.to_string()).unwrap_or_else(|| "no".into());
|
||||||
|
self.mpv
|
||||||
|
.set_property("aid", value.as_str())
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("select_audio_track failed: {e:?}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn select_subtitle_track(&mut self, index: Option<i32>) -> Result<(), PlayerError> {
|
||||||
|
self.subtitle_track = index;
|
||||||
|
let value = index.map(|i| i.to_string()).unwrap_or_else(|| "no".into());
|
||||||
|
self.mpv
|
||||||
|
.set_property("sid", value.as_str())
|
||||||
|
.map_err(|e| PlayerError {
|
||||||
|
message: format!("select_subtitle_track failed: {e:?}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn snapshot(&self) -> PlaybackSnapshot {
|
||||||
|
let s = self.shared.lock_safe();
|
||||||
|
PlaybackSnapshot {
|
||||||
|
phase: s.phase.clone(),
|
||||||
|
position: s.position,
|
||||||
|
duration: s.duration,
|
||||||
|
seekable: s.seekable,
|
||||||
|
volume: self.volume,
|
||||||
|
muted: self.muted,
|
||||||
|
rate: self.rate,
|
||||||
|
audio_track: self.audio_track,
|
||||||
|
subtitle_track: self.subtitle_track,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn capabilities(&self) -> Capabilities {
|
||||||
|
Capabilities {
|
||||||
|
video: true,
|
||||||
|
audio_settings: true,
|
||||||
|
subtitle_switching: true,
|
||||||
|
audio_track_switching: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,405 @@
|
|||||||
|
//! mpv's render API, driven into an OpenGL framebuffer we own.
|
||||||
|
//!
|
||||||
|
//! This is the half of native video that is not GTK: create a render context
|
||||||
|
//! over the mpv handle the audio backend already drives, render a frame into a
|
||||||
|
//! texture, and hand that texture id back for the toolkit to composite.
|
||||||
|
//!
|
||||||
|
//! Kept apart from `video_surface` deliberately — everything here is portable
|
||||||
|
//! across the platforms this app targets, while the surface that consumes it is
|
||||||
|
//! not. Windows reuses this file unchanged (DR-237).
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-080 | DR-231, DR-232, IR-033
|
||||||
|
|
||||||
|
use std::ffi::{c_void, CStr, CString};
|
||||||
|
use std::os::raw::{c_char, c_int};
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
|
use log::{error, info, warn};
|
||||||
|
|
||||||
|
/// GL entry points, resolved once.
|
||||||
|
///
|
||||||
|
/// Only the handful needed to own a framebuffer; mpv resolves everything else
|
||||||
|
/// it needs through [`get_proc_address`].
|
||||||
|
struct Gl {
|
||||||
|
gen_framebuffers: unsafe extern "C" fn(c_int, *mut u32),
|
||||||
|
delete_framebuffers: unsafe extern "C" fn(c_int, *const u32),
|
||||||
|
bind_framebuffer: unsafe extern "C" fn(u32, u32),
|
||||||
|
framebuffer_texture_2d: unsafe extern "C" fn(u32, u32, u32, u32, c_int),
|
||||||
|
gen_textures: unsafe extern "C" fn(c_int, *mut u32),
|
||||||
|
delete_textures: unsafe extern "C" fn(c_int, *const u32),
|
||||||
|
bind_texture: unsafe extern "C" fn(u32, u32),
|
||||||
|
tex_image_2d:
|
||||||
|
unsafe extern "C" fn(u32, c_int, c_int, c_int, c_int, c_int, u32, u32, *const c_void),
|
||||||
|
tex_parameteri: unsafe extern "C" fn(u32, u32, c_int),
|
||||||
|
check_framebuffer_status: unsafe extern "C" fn(u32) -> u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
const GL_TEXTURE_2D: u32 = 0x0DE1;
|
||||||
|
const GL_FRAMEBUFFER: u32 = 0x8D40;
|
||||||
|
const GL_COLOR_ATTACHMENT0: u32 = 0x8CE0;
|
||||||
|
const GL_RGBA: u32 = 0x1908;
|
||||||
|
const GL_RGBA8: c_int = 0x8058;
|
||||||
|
const GL_UNSIGNED_BYTE: u32 = 0x1401;
|
||||||
|
const GL_LINEAR: c_int = 0x2601;
|
||||||
|
const GL_TEXTURE_MIN_FILTER: u32 = 0x2801;
|
||||||
|
const GL_TEXTURE_MAG_FILTER: u32 = 0x2800;
|
||||||
|
const GL_FRAMEBUFFER_COMPLETE: u32 = 0x8CD5;
|
||||||
|
|
||||||
|
/// Resolve a GL symbol the way libepoxy actually exports it.
|
||||||
|
///
|
||||||
|
/// **This is the trap that cost the spike a debugging cycle.** libepoxy does not
|
||||||
|
/// export `glFoo` as a function. It exports `epoxy_glFoo` as a *data* symbol
|
||||||
|
/// holding a lazily-resolving function pointer. So the address `dlsym` returns
|
||||||
|
/// is the address *of the pointer*, not of any code: returning it makes mpv jump
|
||||||
|
/// into non-executable data and take SIGSEGV/SEGV_ACCERR on the very first GL
|
||||||
|
/// call. The value must be read *out of* that location.
|
||||||
|
///
|
||||||
|
/// The `epoxy` crate does this correctly and is unusable here — its
|
||||||
|
/// `gl_generator` dependency pulls a yanked `xml-rs`.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | IR-033
|
||||||
|
unsafe fn resolve(name: &str) -> *mut c_void {
|
||||||
|
let epoxy_name = match CString::new(format!("epoxy_{name}")) {
|
||||||
|
Ok(n) => n,
|
||||||
|
Err(_) => return ptr::null_mut(),
|
||||||
|
};
|
||||||
|
let slot = libc::dlsym(libc::RTLD_DEFAULT, epoxy_name.as_ptr());
|
||||||
|
if !slot.is_null() {
|
||||||
|
// The symbol holds the function pointer; return what is stored there.
|
||||||
|
return *(slot as *mut *mut c_void);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to a plain symbol, for a GL stack that is not behind epoxy.
|
||||||
|
match CString::new(name) {
|
||||||
|
Ok(n) => libc::dlsym(libc::RTLD_DEFAULT, n.as_ptr()),
|
||||||
|
Err(_) => ptr::null_mut(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What mpv calls to find GL entry points. Same rule as [`resolve`].
|
||||||
|
unsafe extern "C" fn get_proc_address(_ctx: *mut c_void, name: *const c_char) -> *mut c_void {
|
||||||
|
if name.is_null() {
|
||||||
|
return ptr::null_mut();
|
||||||
|
}
|
||||||
|
match CStr::from_ptr(name).to_str() {
|
||||||
|
Ok(n) => resolve(n),
|
||||||
|
Err(_) => ptr::null_mut(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! load {
|
||||||
|
($name:literal) => {{
|
||||||
|
let p = resolve($name);
|
||||||
|
if p.is_null() {
|
||||||
|
error!("[MpvRender] GL symbol not found: {}", $name);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
std::mem::transmute(p)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Gl {
|
||||||
|
/// Resolve every entry point, or none — a partially-loaded table would fail
|
||||||
|
/// later at a call site with no context.
|
||||||
|
///
|
||||||
|
/// The transmutes are unannotated on purpose: each target type is declared
|
||||||
|
/// once on the struct field above, and repeating it at the call site would
|
||||||
|
/// be two places to get the same signature wrong.
|
||||||
|
#[allow(clippy::missing_transmute_annotations)]
|
||||||
|
unsafe fn load() -> Option<Self> {
|
||||||
|
Some(Gl {
|
||||||
|
gen_framebuffers: load!("glGenFramebuffers"),
|
||||||
|
delete_framebuffers: load!("glDeleteFramebuffers"),
|
||||||
|
bind_framebuffer: load!("glBindFramebuffer"),
|
||||||
|
framebuffer_texture_2d: load!("glFramebufferTexture2D"),
|
||||||
|
gen_textures: load!("glGenTextures"),
|
||||||
|
delete_textures: load!("glDeleteTextures"),
|
||||||
|
bind_texture: load!("glBindTexture"),
|
||||||
|
tex_image_2d: load!("glTexImage2D"),
|
||||||
|
tex_parameteri: load!("glTexParameteri"),
|
||||||
|
check_framebuffer_status: load!("glCheckFramebufferStatus"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A colour-renderable framebuffer mpv draws into, sized to the widget.
|
||||||
|
struct Target {
|
||||||
|
fbo: u32,
|
||||||
|
texture: u32,
|
||||||
|
width: i32,
|
||||||
|
height: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// mpv's render context plus the framebuffer it draws into.
|
||||||
|
///
|
||||||
|
/// # Lifetime (DR-232)
|
||||||
|
///
|
||||||
|
/// The render context must not outlive the GL context it was created against.
|
||||||
|
/// `Drop` unregisters mpv's update callback *before* freeing the context, so a
|
||||||
|
/// callback cannot land on a freed pointer, and frees the GL objects while the
|
||||||
|
/// caller still has the context current. The caller is responsible for making
|
||||||
|
/// the GL context current around both creation and drop — see `video_surface`.
|
||||||
|
///
|
||||||
|
/// This is DR-184 on Android restated: a surface outliving its player. The spike
|
||||||
|
/// had no defence at all and saw one unexplained SIGSEGV in a decoder thread.
|
||||||
|
pub struct MpvRenderContext {
|
||||||
|
ctx: *mut libmpv_sys::mpv_render_context,
|
||||||
|
gl: Gl,
|
||||||
|
target: Option<Target>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// The render context is driven only from the GTK main thread; the update
|
||||||
|
// callback merely schedules a redraw and touches nothing here.
|
||||||
|
unsafe impl Send for MpvRenderContext {}
|
||||||
|
|
||||||
|
impl MpvRenderContext {
|
||||||
|
/// Create a render context over an existing mpv handle.
|
||||||
|
///
|
||||||
|
/// The GL context must already be current on this thread.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | DR-231, IR-033
|
||||||
|
pub unsafe fn new(mpv: *mut libmpv_sys::mpv_handle) -> Option<Self> {
|
||||||
|
let gl = Gl::load()?;
|
||||||
|
|
||||||
|
let mut init = libmpv_sys::mpv_opengl_init_params {
|
||||||
|
get_proc_address: Some(get_proc_address),
|
||||||
|
get_proc_address_ctx: ptr::null_mut(),
|
||||||
|
};
|
||||||
|
let mut api_type = CString::new("opengl").ok()?;
|
||||||
|
// Advanced control is deliberately OFF.
|
||||||
|
//
|
||||||
|
// With it on, mpv expects the client to drive rendering to a stricter
|
||||||
|
// contract than a GTK draw handler can promise — it will wait on us, and
|
||||||
|
// if we in turn wait on its update callback, neither side proceeds. That
|
||||||
|
// deadlock presents as a file that loads, renders one frame, and then
|
||||||
|
// sits there with no audio and a spinner.
|
||||||
|
//
|
||||||
|
// Off, mpv is tolerant of being rendered on the toolkit's schedule,
|
||||||
|
// which is what the frame clock gives us.
|
||||||
|
let mut advanced: c_int = 0;
|
||||||
|
|
||||||
|
let mut params = [
|
||||||
|
libmpv_sys::mpv_render_param {
|
||||||
|
type_: libmpv_sys::mpv_render_param_type_MPV_RENDER_PARAM_API_TYPE,
|
||||||
|
data: api_type.as_ptr() as *mut c_void,
|
||||||
|
},
|
||||||
|
libmpv_sys::mpv_render_param {
|
||||||
|
type_: libmpv_sys::mpv_render_param_type_MPV_RENDER_PARAM_OPENGL_INIT_PARAMS,
|
||||||
|
data: &mut init as *mut _ as *mut c_void,
|
||||||
|
},
|
||||||
|
libmpv_sys::mpv_render_param {
|
||||||
|
type_: libmpv_sys::mpv_render_param_type_MPV_RENDER_PARAM_ADVANCED_CONTROL,
|
||||||
|
data: &mut advanced as *mut _ as *mut c_void,
|
||||||
|
},
|
||||||
|
libmpv_sys::mpv_render_param {
|
||||||
|
type_: 0,
|
||||||
|
data: ptr::null_mut(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
let mut ctx: *mut libmpv_sys::mpv_render_context = ptr::null_mut();
|
||||||
|
let rc = libmpv_sys::mpv_render_context_create(&mut ctx, mpv, params.as_mut_ptr());
|
||||||
|
// Keep the CString alive until after the call.
|
||||||
|
let _ = &mut api_type;
|
||||||
|
|
||||||
|
if rc < 0 || ctx.is_null() {
|
||||||
|
error!("[MpvRender] mpv_render_context_create failed: {rc}");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("[MpvRender] render context created");
|
||||||
|
Some(MpvRenderContext {
|
||||||
|
ctx,
|
||||||
|
gl,
|
||||||
|
target: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ask to be told when a new frame is ready.
|
||||||
|
///
|
||||||
|
/// Paired with [`report_swap`](Self::report_swap): without both, mpv has
|
||||||
|
/// nothing to time against. The symptom is misleading — playback looks fine
|
||||||
|
/// in a window and judders at fullscreen, which reads as a compositing or
|
||||||
|
/// GPU limit and is neither (DR-233).
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | DR-233
|
||||||
|
pub unsafe fn set_update_callback(
|
||||||
|
&mut self,
|
||||||
|
callback: libmpv_sys::mpv_render_update_fn,
|
||||||
|
ctx: *mut c_void,
|
||||||
|
) {
|
||||||
|
libmpv_sys::mpv_render_context_set_update_callback(self.ctx, callback, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether mpv has a new frame waiting.
|
||||||
|
///
|
||||||
|
/// Asked of mpv directly rather than inferred from its update callback, and
|
||||||
|
/// that distinction is the whole of frame pacing here:
|
||||||
|
///
|
||||||
|
/// - Waiting only on the callback deadlocks — mpv will not progress until
|
||||||
|
/// the client renders, so if the client will not render until mpv says
|
||||||
|
/// so, neither moves. That presents as a file that loads, shows one
|
||||||
|
/// frame, and then sits silent.
|
||||||
|
/// - Rendering on *every* frame-clock tick regardless is the opposite
|
||||||
|
/// error: `report_swap` then claims a presentation far more often than
|
||||||
|
/// real frames exist, mpv has nothing coherent to time against, and
|
||||||
|
/// playback judders badly.
|
||||||
|
///
|
||||||
|
/// Polling is neither. It runs on the main thread, costs a single atomic
|
||||||
|
/// read inside mpv, and answers the only question that matters.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | DR-233
|
||||||
|
pub unsafe fn has_frame(&self) -> bool {
|
||||||
|
let flags = libmpv_sys::mpv_render_context_update(self.ctx);
|
||||||
|
(flags & libmpv_sys::mpv_render_update_flag_MPV_RENDER_UPDATE_FRAME as u64) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Render the current frame at `width` x `height`, returning the texture id
|
||||||
|
/// holding it. The GL context must be current.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | DR-231
|
||||||
|
pub unsafe fn render(&mut self, width: i32, height: i32) -> Option<u32> {
|
||||||
|
if width <= 0 || height <= 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
self.ensure_target(width, height)?;
|
||||||
|
let target = self.target.as_ref()?;
|
||||||
|
|
||||||
|
let mut fbo = libmpv_sys::mpv_opengl_fbo {
|
||||||
|
fbo: target.fbo as c_int,
|
||||||
|
w: width as c_int,
|
||||||
|
h: height as c_int,
|
||||||
|
internal_format: 0,
|
||||||
|
};
|
||||||
|
// GTK's cairo surface has its origin at the top left; mpv defaults to
|
||||||
|
// OpenGL's bottom-left. Without this the picture is drawn upside down —
|
||||||
|
// which looks like a broken decode rather than a coordinate convention.
|
||||||
|
let mut flip: c_int = 1;
|
||||||
|
|
||||||
|
let mut params = [
|
||||||
|
libmpv_sys::mpv_render_param {
|
||||||
|
type_: libmpv_sys::mpv_render_param_type_MPV_RENDER_PARAM_OPENGL_FBO,
|
||||||
|
data: &mut fbo as *mut _ as *mut c_void,
|
||||||
|
},
|
||||||
|
libmpv_sys::mpv_render_param {
|
||||||
|
type_: libmpv_sys::mpv_render_param_type_MPV_RENDER_PARAM_FLIP_Y,
|
||||||
|
data: &mut flip as *mut _ as *mut c_void,
|
||||||
|
},
|
||||||
|
libmpv_sys::mpv_render_param {
|
||||||
|
type_: 0,
|
||||||
|
data: ptr::null_mut(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
let rc = libmpv_sys::mpv_render_context_render(self.ctx, params.as_mut_ptr());
|
||||||
|
if rc < 0 {
|
||||||
|
warn!("[MpvRender] render failed: {rc}");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(target.texture)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tell mpv the frame reached the screen. See [`set_update_callback`].
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | DR-233
|
||||||
|
pub unsafe fn report_swap(&self) {
|
||||||
|
libmpv_sys::mpv_render_context_report_swap(self.ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create or resize the framebuffer. Reused across frames — reallocating per
|
||||||
|
/// frame would churn GPU memory at the display rate.
|
||||||
|
unsafe fn ensure_target(&mut self, width: i32, height: i32) -> Option<()> {
|
||||||
|
if let Some(t) = &self.target {
|
||||||
|
if t.width == width && t.height == height {
|
||||||
|
return Some(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.drop_target();
|
||||||
|
|
||||||
|
let gl = &self.gl;
|
||||||
|
let mut texture: u32 = 0;
|
||||||
|
(gl.gen_textures)(1, &mut texture);
|
||||||
|
(gl.bind_texture)(GL_TEXTURE_2D, texture);
|
||||||
|
(gl.tex_image_2d)(
|
||||||
|
GL_TEXTURE_2D,
|
||||||
|
0,
|
||||||
|
GL_RGBA8,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
0,
|
||||||
|
GL_RGBA,
|
||||||
|
GL_UNSIGNED_BYTE,
|
||||||
|
ptr::null(),
|
||||||
|
);
|
||||||
|
(gl.tex_parameteri)(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
(gl.tex_parameteri)(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
(gl.bind_texture)(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
let mut fbo: u32 = 0;
|
||||||
|
(gl.gen_framebuffers)(1, &mut fbo);
|
||||||
|
(gl.bind_framebuffer)(GL_FRAMEBUFFER, fbo);
|
||||||
|
(gl.framebuffer_texture_2d)(
|
||||||
|
GL_FRAMEBUFFER,
|
||||||
|
GL_COLOR_ATTACHMENT0,
|
||||||
|
GL_TEXTURE_2D,
|
||||||
|
texture,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
let status = (gl.check_framebuffer_status)(GL_FRAMEBUFFER);
|
||||||
|
(gl.bind_framebuffer)(GL_FRAMEBUFFER, 0);
|
||||||
|
|
||||||
|
if status != GL_FRAMEBUFFER_COMPLETE {
|
||||||
|
error!("[MpvRender] framebuffer incomplete: 0x{status:x}");
|
||||||
|
(gl.delete_framebuffers)(1, &fbo);
|
||||||
|
(gl.delete_textures)(1, &texture);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.target = Some(Target {
|
||||||
|
fbo,
|
||||||
|
texture,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
});
|
||||||
|
Some(())
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn drop_target(&mut self) {
|
||||||
|
if let Some(t) = self.target.take() {
|
||||||
|
(self.gl.delete_framebuffers)(1, &t.fbo);
|
||||||
|
(self.gl.delete_textures)(1, &t.texture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Free everything, with the GL context current.
|
||||||
|
///
|
||||||
|
/// Explicit rather than left to `Drop` because the ordering matters and the
|
||||||
|
/// caller is the only one that can guarantee the GL context is current. See
|
||||||
|
/// DR-232.
|
||||||
|
pub unsafe fn destroy(mut self) {
|
||||||
|
// Unregister first: a callback arriving after the free would be a use
|
||||||
|
// after free, and it is scheduled from mpv's own threads.
|
||||||
|
libmpv_sys::mpv_render_context_set_update_callback(self.ctx, None, ptr::null_mut());
|
||||||
|
self.drop_target();
|
||||||
|
libmpv_sys::mpv_render_context_free(self.ctx);
|
||||||
|
self.ctx = ptr::null_mut();
|
||||||
|
info!("[MpvRender] render context freed");
|
||||||
|
std::mem::forget(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for MpvRenderContext {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if !self.ctx.is_null() {
|
||||||
|
// Reached only if `destroy` was not called — the GL context may not
|
||||||
|
// be current, so the GL objects are deliberately leaked rather than
|
||||||
|
// deleted against whatever context happens to be bound. Freeing the
|
||||||
|
// render context is still safe and is the part that matters.
|
||||||
|
warn!("[MpvRender] dropped without destroy(); GL objects leaked deliberately");
|
||||||
|
unsafe {
|
||||||
|
libmpv_sys::mpv_render_context_set_update_callback(self.ctx, None, ptr::null_mut());
|
||||||
|
libmpv_sys::mpv_render_context_free(self.ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
//! Whether this process renders video natively, answered once.
|
||||||
|
//!
|
||||||
|
//! Three things need this and must agree: the mpv backend (which has to be
|
||||||
|
//! configured for video *at construction*, before anything plays), the video
|
||||||
|
//! surface (which has nothing to draw otherwise), and `get_player_status`
|
||||||
|
//! (which tells the frontend whether to use a webview `<video>` element).
|
||||||
|
//!
|
||||||
|
//! It is a function rather than three `env::var` checks for the reason this
|
||||||
|
//! codebase keeps rediscovering: a capability answered in several places is a
|
||||||
|
//! capability whose answers drift. Four separate bugs this cycle came from
|
||||||
|
//! exactly that shape — a webview's decode limits applied to ExoPlayer, a
|
||||||
|
//! transcode target contradicting a direct-play claim, a codec list hardcoded in
|
||||||
|
//! a URL builder. One source, read by everyone.
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-080 | DR-231, DR-235
|
||||||
|
|
||||||
|
/// The opt-in for native desktop video.
|
||||||
|
///
|
||||||
|
/// Off by default while the render path is unproven — the webview path still
|
||||||
|
/// works and is what ships. This becomes the *default* (and then the only path)
|
||||||
|
/// when DR-235 lands; the variable is how it is exercised until then.
|
||||||
|
const ENV_FLAG: &str = "JELLYTAU_NATIVE_VIDEO";
|
||||||
|
|
||||||
|
/// Whether mpv should decode and draw video in this process.
|
||||||
|
///
|
||||||
|
/// Read fresh rather than cached: it is consulted a handful of times at startup,
|
||||||
|
/// and a `OnceLock` here would only make it harder to test.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | DR-231, DR-235
|
||||||
|
pub fn enabled() -> bool {
|
||||||
|
// Only where a native renderer exists. On Android ExoPlayer already does
|
||||||
|
// this and `use_html5_element` is false for entirely separate reasons.
|
||||||
|
if !cfg!(all(target_os = "linux", not(target_os = "android"))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
matches!(
|
||||||
|
std::env::var(ENV_FLAG).as_deref(),
|
||||||
|
Ok("1") | Ok("true") | Ok("yes")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
/// Absent, empty, or anything unrecognised means off. A half-set variable
|
||||||
|
/// must not half-enable a renderer — the failure mode would be mpv
|
||||||
|
/// configured for video with nothing drawing it, i.e. audio playing over a
|
||||||
|
/// black rectangle.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | DR-231 | UT-216
|
||||||
|
#[test]
|
||||||
|
fn test_only_explicit_truthy_values_enable_it() {
|
||||||
|
let restore = std::env::var(ENV_FLAG).ok();
|
||||||
|
|
||||||
|
for value in ["", "0", "no", "false", "maybe", "2"] {
|
||||||
|
std::env::set_var(ENV_FLAG, value);
|
||||||
|
assert!(!enabled(), "{value:?} must not enable native video");
|
||||||
|
}
|
||||||
|
|
||||||
|
for value in ["1", "true", "yes"] {
|
||||||
|
std::env::set_var(ENV_FLAG, value);
|
||||||
|
assert_eq!(
|
||||||
|
enabled(),
|
||||||
|
cfg!(all(target_os = "linux", not(target_os = "android"))),
|
||||||
|
"{value:?} enables it exactly where a native renderer exists"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::env::remove_var(ENV_FLAG);
|
||||||
|
assert!(!enabled(), "absent means off");
|
||||||
|
|
||||||
|
match restore {
|
||||||
|
Some(v) => std::env::set_var(ENV_FLAG, v),
|
||||||
|
None => std::env::remove_var(ENV_FLAG),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,23 +39,38 @@ pub fn determine_video_seek_strategy(
|
|||||||
return VideoSeekStrategy::LocalNativeSeek;
|
return VideoSeekStrategy::LocalNativeSeek;
|
||||||
}
|
}
|
||||||
|
|
||||||
// HLS streams and direct play (non-transcoded) support native seeking
|
// A server-side transcode is produced *from* `StartTimeTicks`, so where the
|
||||||
if is_hls || !needs_transcoding {
|
// seek lands is a property of the request, not of the stream in hand.
|
||||||
if use_html5 {
|
//
|
||||||
// HTML5 backend - frontend handles seeking via videoElement.currentTime
|
// hls.js is the exception: handed a VOD playlist it seeks within it and lets
|
||||||
// We don't call backend.seek() because video is in HTML5 element, not in MPV
|
// the server catch up segment by segment. mpv's HLS demuxer cannot make
|
||||||
|
// Jellyfin transcode from a new offset, so for the native backend a
|
||||||
|
// transcoded seek must re-negotiate the stream regardless of container.
|
||||||
|
//
|
||||||
|
// Before native video shipped, `use_html5` was always true for HLS and the
|
||||||
|
// native+HLS+transcode cell was unreachable, which is why `is_hls` alone
|
||||||
|
// used to be a safe proxy for "seekable in place". It no longer is: turning
|
||||||
|
// native video on routed every transcoded seek into a backend seek that
|
||||||
|
// silently does nothing, and presents as "resume does not work".
|
||||||
|
if needs_transcoding {
|
||||||
|
return if use_html5 {
|
||||||
|
if is_hls {
|
||||||
VideoSeekStrategy::Html5NativeSeek
|
VideoSeekStrategy::Html5NativeSeek
|
||||||
} else {
|
} else {
|
||||||
// Native backend (MPV) - backend handles seeking
|
|
||||||
VideoSeekStrategy::BackendNativeSeek
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Transcoded non-HLS streams need server-side seek (reload from new position)
|
|
||||||
if use_html5 {
|
|
||||||
VideoSeekStrategy::Html5ReloadStream
|
VideoSeekStrategy::Html5ReloadStream
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
VideoSeekStrategy::BackendReloadStream
|
VideoSeekStrategy::BackendReloadStream
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Direct play and direct stream are seekable where they sit.
|
||||||
|
if use_html5 {
|
||||||
|
// The frontend seeks via videoElement.currentTime; calling backend.seek()
|
||||||
|
// would move a player that is not the one rendering.
|
||||||
|
VideoSeekStrategy::Html5NativeSeek
|
||||||
|
} else {
|
||||||
|
VideoSeekStrategy::BackendNativeSeek
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,6 +255,30 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A server-side transcode cannot be seeked by the native backend.
|
||||||
|
///
|
||||||
|
/// Jellyfin produces a transcode from `StartTimeTicks`; hls.js can seek
|
||||||
|
/// within the VOD playlist it is handed, but mpv's HLS demuxer cannot make
|
||||||
|
/// the server transcode from a new offset, so the stream has to be
|
||||||
|
/// re-negotiated. Before native video existed, `use_html5` was always true
|
||||||
|
/// for HLS and this case was unreachable — turning native video on routed
|
||||||
|
/// every transcoded seek into a native seek that silently does nothing,
|
||||||
|
/// which presents as "resume does not work".
|
||||||
|
///
|
||||||
|
/// TRACES: UR-040 | DR-238 | UT-217
|
||||||
|
#[test]
|
||||||
|
fn test_seek_strategy_transcoded_hls_native_backend() {
|
||||||
|
assert_eq!(
|
||||||
|
determine_video_seek_strategy(false, true, true, false),
|
||||||
|
VideoSeekStrategy::BackendReloadStream
|
||||||
|
);
|
||||||
|
// The HTML5 side of the same case is unchanged: hls.js seeks in-playlist.
|
||||||
|
assert_eq!(
|
||||||
|
determine_video_seek_strategy(false, true, true, true),
|
||||||
|
VideoSeekStrategy::Html5NativeSeek
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// Test video seek strategy for direct play (non-transcoded) streams
|
/// Test video seek strategy for direct play (non-transcoded) streams
|
||||||
#[test]
|
#[test]
|
||||||
fn test_seek_strategy_direct_play() {
|
fn test_seek_strategy_direct_play() {
|
||||||
|
|||||||
@@ -1,232 +1,400 @@
|
|||||||
//! The native video surface: a GL area beneath Tauri's own webview.
|
//! The native video surface: mpv drawn *behind* Tauri's webview, without
|
||||||
|
//! touching the widget tree.
|
||||||
//!
|
//!
|
||||||
//! This is the desktop counterpart of the Android arrangement — a native
|
//! # Why there is no overlay here
|
||||||
//! renderer at the bottom of the stack with a transparent webview drawn over it,
|
|
||||||
//! so the Svelte controls composite on top of moving video.
|
|
||||||
//!
|
//!
|
||||||
//! The spike that authorised this built its *own* `GtkOverlay` and proved mpv
|
//! The obvious arrangement — wrap the webview in a `GtkOverlay` with a
|
||||||
//! renders into it on X11 and Wayland. What it could not prove is the step this
|
//! `GtkGLArea` beneath — attaches cleanly and then aborts the process on the
|
||||||
//! module exists for: taking the overlay Tauri already built and reparenting the
|
//! first click. `tauri-runtime-wry` connects a button-press handler to the
|
||||||
//! real webview into it. Same widgets, one extra move, and the only place
|
//! webview that walks a hard-coded path:
|
||||||
//! Tauri-specific behaviour can still bite — which is why it is gate one.
|
|
||||||
//!
|
//!
|
||||||
//! TRACES: UR-080 | DR-231, IR-033
|
//! ```text
|
||||||
|
//! webview.parent() // "This one should be GtkBox"
|
||||||
|
//! .parent() // ...and this one the GtkWindow
|
||||||
|
//! .downcast::<gtk::Window>().unwrap()
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! An overlay makes that chain `webview → GtkOverlay → GtkBox`, the downcast
|
||||||
|
//! fails, and because the panic is non-unwinding it takes the app with it.
|
||||||
|
//! Nothing in configuration avoids it: on Linux the handler is attached
|
||||||
|
//! *unconditionally* (the Windows path guards it behind `is_decorated()`), and
|
||||||
|
//! the decoration check that would make it inert runs *after* the unwrap.
|
||||||
|
//!
|
||||||
|
//! So the widget tree is left exactly as Tauri built it. GTK draws a container
|
||||||
|
//! before its children, so rendering into the vbox's own `draw` handler puts the
|
||||||
|
//! picture underneath the webview for free — the same z-order, no reparenting,
|
||||||
|
//! one less widget, and nothing a Tauri upgrade can invalidate by assuming its
|
||||||
|
//! own layout.
|
||||||
|
//!
|
||||||
|
//! TRACES: UR-080 | DR-231, DR-232, DR-233, IR-033
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::ffi::c_void;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use log::{info, warn};
|
use gtk::{gdk, glib};
|
||||||
|
use log::{error, info, warn};
|
||||||
|
|
||||||
/// The widgets that make up the video surface, kept together because their
|
use super::mpv_render::MpvRenderContext;
|
||||||
/// lifetimes are bound: the render context (added next) is created when the GL
|
|
||||||
/// area realizes and must be freed before it unrealizes — DR-232.
|
/// GL enum for `gdk_cairo_draw_from_gl`'s `source_type`. GDK takes the GL
|
||||||
|
/// constant itself rather than an enum of its own.
|
||||||
|
const GL_TEXTURE: i32 = 0x1702;
|
||||||
|
|
||||||
|
/// Everything the draw handler needs, shared with the GTK callbacks.
|
||||||
|
struct SurfaceState {
|
||||||
|
gl: Option<gdk::GLContext>,
|
||||||
|
render: Option<MpvRenderContext>,
|
||||||
|
mpv: *mut libmpv_sys::mpv_handle,
|
||||||
|
/// Set by mpv's update callback (on an mpv thread), cleared by the frame
|
||||||
|
/// clock (on the main thread). The whole cross-thread contract.
|
||||||
|
frame_ready: Arc<AtomicBool>,
|
||||||
|
/// The boxed clone of `frame_ready` handed to mpv, reclaimed on teardown.
|
||||||
|
/// Null when no callback is registered.
|
||||||
|
callback_ctx: *mut Arc<AtomicBool>,
|
||||||
|
// One-shot diagnostic latches; see `draw`.
|
||||||
|
logged_first_draw: bool,
|
||||||
|
logged_first_frame: bool,
|
||||||
|
/// Last size we logged, so a size change re-reports rather than staying silent.
|
||||||
|
logged_size: (i32, i32),
|
||||||
|
logged_no_gl: bool,
|
||||||
|
logged_no_window: bool,
|
||||||
|
logged_no_size: bool,
|
||||||
|
logged_render_fail: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SurfaceState {
|
||||||
|
/// Tear down in the order DR-232 requires, with the GL context current.
|
||||||
|
///
|
||||||
|
/// The update callback is unregistered before the context is freed (inside
|
||||||
|
/// `destroy`), and the GL objects go while their context is still bound.
|
||||||
|
/// Getting this wrong is DR-184 on Android restated — a surface outliving
|
||||||
|
/// its player — and is the likeliest cause of the one unexplained SIGSEGV
|
||||||
|
/// the spike recorded.
|
||||||
|
fn teardown(&mut self) {
|
||||||
|
if let Some(render) = self.render.take() {
|
||||||
|
if let Some(gl) = &self.gl {
|
||||||
|
gl.make_current();
|
||||||
|
}
|
||||||
|
// Unregisters the callback before freeing the context.
|
||||||
|
unsafe { render.destroy() };
|
||||||
|
}
|
||||||
|
// Only now is it safe to reclaim what the callback was holding: mpv can
|
||||||
|
// no longer reach it. Freeing it first would be the use-after-free this
|
||||||
|
// ordering exists to prevent.
|
||||||
|
if !self.callback_ctx.is_null() {
|
||||||
|
unsafe { drop(Box::from_raw(self.callback_ctx)) };
|
||||||
|
self.callback_ctx = std::ptr::null_mut();
|
||||||
|
}
|
||||||
|
self.gl = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A live video surface. Dropping it tears the render context down.
|
||||||
pub struct VideoSurface {
|
pub struct VideoSurface {
|
||||||
/// The GL area mpv renders into. Main child of the overlay, so it sits
|
state: Rc<RefCell<SurfaceState>>,
|
||||||
/// *under* everything else.
|
widget: gtk::Box,
|
||||||
#[allow(dead_code)]
|
handlers: Vec<glib::SignalHandlerId>,
|
||||||
gl_area: gtk::GLArea,
|
|
||||||
/// The overlay holding the GL area and the webview.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
overlay: gtk::Overlay,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VideoSurface {
|
impl Drop for VideoSurface {
|
||||||
// Consumed by the render context, which binds to the GL area on `realize`
|
fn drop(&mut self) {
|
||||||
// and is freed on `unrealize` (DR-232). Held here from the moment the
|
for id in self.handlers.drain(..) {
|
||||||
// surface exists so that binding has something to attach to.
|
self.widget.disconnect(id);
|
||||||
#[allow(dead_code)]
|
|
||||||
/// The GL area, for the render context to bind to.
|
|
||||||
pub fn gl_area(&self) -> >k::GLArea {
|
|
||||||
&self.gl_area
|
|
||||||
}
|
}
|
||||||
|
self.state.borrow_mut().teardown();
|
||||||
#[allow(dead_code)]
|
self.widget.queue_draw();
|
||||||
/// The overlay, for teardown.
|
info!("[VideoSurface] detached");
|
||||||
pub fn overlay(&self) -> >k::Overlay {
|
|
||||||
&self.overlay
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Why a surface could not be attached.
|
/// mpv's update callback. Runs on an mpv thread, so it does the least possible:
|
||||||
|
/// flags the state and asks GTK to redraw on the main loop.
|
||||||
///
|
///
|
||||||
/// One variant, because there is exactly one way this fails that is not already
|
/// **Nothing here may block or re-enter the player.** The project's deadlock
|
||||||
/// reported by Tauri itself: the window exists and has a vbox, but the vbox is
|
/// gotcha applies with full force — this is called from mpv's own threads.
|
||||||
/// not shaped the way Tauri has always shaped it.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum SurfaceError {
|
|
||||||
/// The vbox held no webview to reparent — Tauri's layout has changed.
|
|
||||||
NoWebviewChild,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for SurfaceError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
SurfaceError::NoWebviewChild => write!(
|
|
||||||
f,
|
|
||||||
"Tauri's default vbox had no child to reparent — its window layout has changed"
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for SurfaceError {}
|
|
||||||
|
|
||||||
/// Build the overlay and move Tauri's webview on top of it.
|
|
||||||
///
|
///
|
||||||
/// Tauri's Linux window is an `ApplicationWindow` holding a single vertical
|
/// TRACES: UR-080 | DR-233
|
||||||
/// `gtk::Box` (`default_vbox`), with the webview packed into it. This takes that
|
unsafe extern "C" fn on_mpv_update(ctx: *mut c_void) {
|
||||||
/// webview out, puts a `GtkGLArea` in its place inside a `GtkOverlay`, and adds
|
if ctx.is_null() {
|
||||||
/// the webview back as the *overlay* child so it draws above.
|
|
||||||
///
|
|
||||||
/// **Must run on the GTK main thread.** Every GTK call here is main-thread-only,
|
|
||||||
/// and the caller reaches it via `run_on_main_thread`.
|
|
||||||
///
|
|
||||||
/// Ordering matters: the GL area is added as the overlay's main child *before*
|
|
||||||
/// the webview goes back, because `GtkOverlay` treats its first `add` as the
|
|
||||||
/// bottom of the stack. Adding them the other way round yields a webview with
|
|
||||||
/// video painted over it — an easy mistake with an obvious symptom.
|
|
||||||
///
|
|
||||||
/// TRACES: UR-080 | DR-231
|
|
||||||
pub fn attach(vbox: >k::Box) -> Result<VideoSurface, SurfaceError> {
|
|
||||||
// Tauri packs exactly one child (the webview) into the default vbox. Take it
|
|
||||||
// rather than assume its type: wry's widget is an implementation detail, and
|
|
||||||
// all this needs is "whatever Tauri put here".
|
|
||||||
let children = vbox.children();
|
|
||||||
let webview = children
|
|
||||||
.into_iter()
|
|
||||||
.next()
|
|
||||||
.ok_or(SurfaceError::NoWebviewChild)?;
|
|
||||||
|
|
||||||
let gl_area = gtk::GLArea::new();
|
|
||||||
// No depth buffer: mpv draws a flat picture into an FBO and nothing here is
|
|
||||||
// 3D. Asking for one costs memory on every resize for nothing.
|
|
||||||
gl_area.set_has_depth_buffer(false);
|
|
||||||
gl_area.set_has_stencil_buffer(false);
|
|
||||||
// Fill the overlay rather than centring at intrinsic size — the same defect
|
|
||||||
// `videoFitClass` had to fix on the webview side, where `max-w-full` only
|
|
||||||
// ever shrank and a 480p source rendered as a small box on a black screen.
|
|
||||||
gl_area.set_hexpand(true);
|
|
||||||
gl_area.set_vexpand(true);
|
|
||||||
|
|
||||||
let overlay = gtk::Overlay::new();
|
|
||||||
|
|
||||||
// Reparent. `remove` drops the container's reference, so hold one across the
|
|
||||||
// move or the widget is destroyed between the two calls.
|
|
||||||
let webview_ref = webview.clone();
|
|
||||||
vbox.remove(&webview);
|
|
||||||
|
|
||||||
overlay.add(&gl_area); // main child — the bottom of the stack
|
|
||||||
overlay.add_overlay(&webview_ref); // drawn above the video
|
|
||||||
|
|
||||||
// The webview must keep receiving input: it *is* the UI. `GtkOverlay` passes
|
|
||||||
// events to overlay children by default, so pass-through stays off — setting
|
|
||||||
// it would send clicks to the GL area, which has no controls on it.
|
|
||||||
overlay.set_overlay_pass_through(&webview_ref, false);
|
|
||||||
|
|
||||||
vbox.pack_start(&overlay, true, true, 0);
|
|
||||||
overlay.show_all();
|
|
||||||
|
|
||||||
info!("[VideoSurface] GL area attached beneath Tauri's webview");
|
|
||||||
|
|
||||||
Ok(VideoSurface { gl_area, overlay })
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Put Tauri's window back the way it was found.
|
|
||||||
///
|
|
||||||
/// Not merely tidiness: the webview outlives the video surface, so if the
|
|
||||||
/// surface is torn down without returning the webview to the vbox the UI
|
|
||||||
/// disappears while the app keeps running. Mirrors [`attach`] exactly.
|
|
||||||
///
|
|
||||||
/// TRACES: UR-080 | DR-231, DR-232
|
|
||||||
// Called by the render-context teardown, which lands with DR-232. Written now,
|
|
||||||
// beside `attach`, because a reparent whose inverse is written later is a
|
|
||||||
// reparent whose inverse is written wrong.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn detach(vbox: >k::Box, surface: &VideoSurface) {
|
|
||||||
let children = surface.overlay.children();
|
|
||||||
for child in children {
|
|
||||||
// Everything except the GL area came from the vbox and goes back to it.
|
|
||||||
if child.downcast_ref::<gtk::GLArea>().is_some() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
surface.overlay.remove(&child);
|
|
||||||
vbox.pack_start(&child, true, true, 0);
|
|
||||||
}
|
|
||||||
vbox.remove(&surface.overlay);
|
|
||||||
vbox.show_all();
|
|
||||||
warn!("[VideoSurface] detached; webview returned to Tauri's vbox");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
//! These exercise GTK widget wiring, so they need a display and are ignored
|
|
||||||
//! by default — CI has no X11 or Wayland session. Run locally with
|
|
||||||
//! `cargo test -- --ignored video_surface`.
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
/// The stacking order is the whole point, and getting it backwards produces
|
|
||||||
/// video painted over the controls rather than under them.
|
|
||||||
///
|
|
||||||
/// TRACES: UR-080 | DR-231
|
|
||||||
#[test]
|
|
||||||
#[ignore = "requires a display"]
|
|
||||||
fn test_gl_area_is_below_the_reparented_webview() {
|
|
||||||
if gtk::init().is_err() {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
// Runs on an *mpv* thread. It therefore does exactly one thing that is safe
|
||||||
// Stand in for the webview; `attach` deliberately does not care what it is.
|
// to do from there: set an atomic flag.
|
||||||
let stand_in = gtk::DrawingArea::new();
|
//
|
||||||
vbox.pack_start(&stand_in, true, true, 0);
|
// It must not touch GTK, and specifically must not schedule work with
|
||||||
|
// `idle_add_local*`, which requires the calling thread to own the default
|
||||||
let surface = attach(&vbox).expect("attaches");
|
// main context — from here that panics with "default main context already
|
||||||
let children = surface.overlay().children();
|
// acquired by another thread". Nor can it hold the `Rc<RefCell<..>>` state:
|
||||||
|
// an `Rc` is not `Send`, and cloning one from two threads races its
|
||||||
// GtkOverlay lists its main child first.
|
// refcount.
|
||||||
assert!(
|
//
|
||||||
children[0].downcast_ref::<gtk::GLArea>().is_some(),
|
// The frame clock on the widget picks the flag up on the main thread. See
|
||||||
"the GL area must be the overlay's main child, i.e. underneath"
|
// `install_frame_clock`.
|
||||||
);
|
let flag = &*(ctx as *const Arc<AtomicBool>);
|
||||||
assert!(
|
flag.store(true, Ordering::Release);
|
||||||
children.len() > 1,
|
|
||||||
"the reparented widget must still be present"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A surface that tears down without returning the webview leaves a running
|
/// Start drawing mpv's video underneath the webview.
|
||||||
/// app with no UI.
|
|
||||||
///
|
///
|
||||||
/// TRACES: UR-080 | DR-231, DR-232
|
/// `vbox` is Tauri's `default_vbox()` — the container the webview already lives
|
||||||
#[test]
|
/// in. It is not modified; only a `draw` handler is added.
|
||||||
#[ignore = "requires a display"]
|
///
|
||||||
fn test_detach_returns_the_webview_to_the_vbox() {
|
/// Must run on the GTK main thread.
|
||||||
if gtk::init().is_err() {
|
///
|
||||||
|
/// TRACES: UR-080 | DR-231, DR-232, DR-233
|
||||||
|
pub fn attach(vbox: >k::Box, mpv: *mut libmpv_sys::mpv_handle) -> bool {
|
||||||
|
if mpv.is_null() {
|
||||||
|
warn!("[VideoSurface] no mpv handle; native video unavailable");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let state = Rc::new(RefCell::new(SurfaceState {
|
||||||
|
gl: None,
|
||||||
|
render: None,
|
||||||
|
mpv,
|
||||||
|
frame_ready: Arc::new(AtomicBool::new(false)),
|
||||||
|
callback_ctx: std::ptr::null_mut(),
|
||||||
|
logged_first_draw: false,
|
||||||
|
logged_first_frame: false,
|
||||||
|
logged_size: (0, 0),
|
||||||
|
logged_no_gl: false,
|
||||||
|
logged_no_window: false,
|
||||||
|
logged_no_size: false,
|
||||||
|
logged_render_fail: false,
|
||||||
|
}));
|
||||||
|
|
||||||
|
let mut handlers = Vec::new();
|
||||||
|
|
||||||
|
// The GL context can only be created once the widget has a GdkWindow, which
|
||||||
|
// is what `realize` announces. Creating it earlier leaves nothing to attach
|
||||||
|
// to — the same ordering constraint the render context has.
|
||||||
|
let realize_state = state.clone();
|
||||||
|
handlers.push(vbox.connect_realize(move |widget| {
|
||||||
|
if let Err(e) = init_gl(widget, &realize_state) {
|
||||||
|
error!("[VideoSurface] GL init failed: {e}");
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
// A render context outliving its GL context is the defect DR-232 exists to
|
||||||
|
// prevent, so teardown is bound to `unrealize` rather than left to Drop.
|
||||||
|
let unrealize_state = state.clone();
|
||||||
|
handlers.push(vbox.connect_unrealize(move |_| {
|
||||||
|
unrealize_state.borrow_mut().teardown();
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Drive the render loop from the widget's frame clock, on the main thread,
|
||||||
|
// rendering only when mpv actually has a frame.
|
||||||
|
//
|
||||||
|
// Both nearby mistakes were made and are worth naming, because each has a
|
||||||
|
// symptom that points somewhere else:
|
||||||
|
//
|
||||||
|
// - Waiting on mpv's update callback before rendering deadlocks. mpv does
|
||||||
|
// not progress until the client renders. The file loads, one frame
|
||||||
|
// appears, and everything stops — no picture, no audio, a spinner that
|
||||||
|
// never clears. It reads as a broken stream.
|
||||||
|
// - Rendering unconditionally every tick and reporting a swap each time
|
||||||
|
// tells mpv a frame reached the screen far more often than one did. It
|
||||||
|
// plays, and judders badly. It reads as a GPU or compositing limit.
|
||||||
|
//
|
||||||
|
// Polling `has_frame` each tick is neither.
|
||||||
|
//
|
||||||
|
// The frame clock only ticks while the widget is mapped, so this costs
|
||||||
|
// nothing when the window is hidden.
|
||||||
|
//
|
||||||
|
// TRACES: UR-080 | DR-233
|
||||||
|
let tick_state = state.clone();
|
||||||
|
vbox.add_tick_callback(move |widget, _clock| {
|
||||||
|
// Ask mpv, on the main thread, whether there is anything new. The
|
||||||
|
// update callback's flag is only a hint that something *may* have
|
||||||
|
// happened; `has_frame` is the authority, and asking it here is what
|
||||||
|
// keeps this from either deadlocking or over-presenting.
|
||||||
|
let ready = match tick_state.try_borrow() {
|
||||||
|
Ok(s) => {
|
||||||
|
s.frame_ready.swap(false, Ordering::AcqRel);
|
||||||
|
match s.render.as_ref() {
|
||||||
|
Some(render) => unsafe { render.has_frame() },
|
||||||
|
None => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => false,
|
||||||
|
};
|
||||||
|
if ready {
|
||||||
|
widget.queue_draw();
|
||||||
|
}
|
||||||
|
glib::ControlFlow::Continue
|
||||||
|
});
|
||||||
|
|
||||||
|
let draw_state = state.clone();
|
||||||
|
handlers.push(vbox.connect_draw(move |widget, cr| {
|
||||||
|
draw(widget, cr, &draw_state);
|
||||||
|
// Propagate: the webview is a child and must still draw over us.
|
||||||
|
glib::Propagation::Proceed
|
||||||
|
}));
|
||||||
|
|
||||||
|
// The window is already up by the time we are called, so run the init the
|
||||||
|
// `realize` signal would have.
|
||||||
|
if vbox.is_realized() {
|
||||||
|
if let Err(e) = init_gl(vbox, &state) {
|
||||||
|
error!("[VideoSurface] GL init failed: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("[VideoSurface] attached to Tauri's vbox without reparenting");
|
||||||
|
// The surface lives as long as the window. Held in a thread-local rather
|
||||||
|
// than returned, because it owns `Rc` and GTK types and so is neither `Send`
|
||||||
|
// nor `Sync` — it cannot go into Tauri's managed state, and leaking it would
|
||||||
|
// give up the ability to tear it down at all.
|
||||||
|
//
|
||||||
|
// Teardown does not depend on this being dropped: it is driven by the
|
||||||
|
// widget's `unrealize`, which is the signal that actually means "your GL
|
||||||
|
// context is going away" (DR-232).
|
||||||
|
LIVE_SURFACE.with(|cell| {
|
||||||
|
*cell.borrow_mut() = Some(VideoSurface {
|
||||||
|
state,
|
||||||
|
widget: vbox.clone(),
|
||||||
|
handlers,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
/// The one live surface, on the GTK main thread.
|
||||||
|
static LIVE_SURFACE: RefCell<Option<VideoSurface>> = const { RefCell::new(None) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drop the live surface, if there is one. Idempotent.
|
||||||
|
///
|
||||||
|
/// TRACES: UR-080 | DR-232
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn detach() {
|
||||||
|
LIVE_SURFACE.with(|cell| {
|
||||||
|
cell.borrow_mut().take();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create the GL context and the mpv render context over it.
|
||||||
|
fn init_gl(widget: >k::Box, state: &Rc<RefCell<SurfaceState>>) -> Result<(), String> {
|
||||||
|
if state.borrow().render.is_some() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let window = widget.window().ok_or("widget has no GdkWindow")?;
|
||||||
|
|
||||||
|
let gl = window
|
||||||
|
.create_gl_context()
|
||||||
|
.map_err(|e| format!("create_gl_context: {e}"))?;
|
||||||
|
gl.realize().map_err(|e| format!("realize: {e}"))?;
|
||||||
|
gl.make_current();
|
||||||
|
|
||||||
|
let mpv = state.borrow().mpv;
|
||||||
|
let mut render =
|
||||||
|
unsafe { MpvRenderContext::new(mpv) }.ok_or("mpv render context creation failed")?;
|
||||||
|
|
||||||
|
// The callback needs an owned handle that outlives this function, so a
|
||||||
|
// clone of the flag is boxed and leaked. `Arc<AtomicBool>` rather than the
|
||||||
|
// state itself: it is the only thing that may cross to an mpv thread. The
|
||||||
|
// pointer is kept so teardown can reclaim it — after the callback is
|
||||||
|
// unregistered, never before.
|
||||||
|
let flag = state.borrow().frame_ready.clone();
|
||||||
|
let ctx_box: *mut Arc<AtomicBool> = Box::into_raw(Box::new(flag));
|
||||||
|
unsafe { render.set_update_callback(Some(on_mpv_update), ctx_box as *mut c_void) };
|
||||||
|
|
||||||
|
let mut s = state.borrow_mut();
|
||||||
|
s.gl = Some(gl);
|
||||||
|
s.render = Some(render);
|
||||||
|
s.callback_ctx = ctx_box;
|
||||||
|
info!("[VideoSurface] GL and render context ready");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Draw the current frame, if there is one.
|
||||||
|
///
|
||||||
|
/// Runs *before* the children, which is what puts the picture behind the
|
||||||
|
/// webview. Deliberately forgiving: no frame, no GL, or a borrowed state all
|
||||||
|
/// mean "draw nothing this pass" rather than an error — the webview then paints
|
||||||
|
/// over an untouched background, which is exactly the pre-native appearance.
|
||||||
|
fn draw(widget: >k::Box, cr: >k::cairo::Context, state: &Rc<RefCell<SurfaceState>>) {
|
||||||
|
// Report each way of doing nothing exactly once. Without this the whole
|
||||||
|
// path is invisible: a draw handler that never runs, one that bails on a
|
||||||
|
// zero allocation, and one that renders perfectly all look identical from
|
||||||
|
// outside — and mpv stalls if frames are never consumed, so "no audio and
|
||||||
|
// it hangs" is a plausible symptom of *any* of them.
|
||||||
|
fn once(flag: &mut bool, msg: &str) {
|
||||||
|
if !*flag {
|
||||||
|
*flag = true;
|
||||||
|
warn!("[VideoSurface] not drawing: {msg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let Ok(mut s) = state.try_borrow_mut() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if !s.logged_first_draw {
|
||||||
|
s.logged_first_draw = true;
|
||||||
|
info!("[VideoSurface] draw handler running");
|
||||||
|
}
|
||||||
|
let Some(gl) = s.gl.clone() else {
|
||||||
|
let f = &mut s.logged_no_gl;
|
||||||
|
once(f, "no GL context");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Some(window) = widget.window() else {
|
||||||
|
let f = &mut s.logged_no_window;
|
||||||
|
once(f, "widget has no GdkWindow");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let scale = widget.scale_factor();
|
||||||
|
let width = widget.allocated_width() * scale;
|
||||||
|
let height = widget.allocated_height() * scale;
|
||||||
|
if width <= 0 || height <= 0 {
|
||||||
|
let f = &mut s.logged_no_size;
|
||||||
|
once(f, "zero allocation");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
|
||||||
let stand_in = gtk::DrawingArea::new();
|
|
||||||
vbox.pack_start(&stand_in, true, true, 0);
|
|
||||||
|
|
||||||
let surface = attach(&vbox).expect("attaches");
|
gl.make_current();
|
||||||
detach(&vbox, &surface);
|
|
||||||
|
|
||||||
let children = vbox.children();
|
// Render and end the mutable borrow before touching the latches again.
|
||||||
assert_eq!(children.len(), 1, "exactly the original child comes back");
|
let rendered = match s.render.as_mut() {
|
||||||
assert!(
|
Some(render) => unsafe { render.render(width, height) },
|
||||||
children[0].downcast_ref::<gtk::DrawingArea>().is_some(),
|
None => return,
|
||||||
"and it is the webview stand-in, not the overlay"
|
};
|
||||||
);
|
let Some(texture) = rendered else {
|
||||||
}
|
let f = &mut s.logged_render_fail;
|
||||||
|
once(f, "mpv render produced no texture");
|
||||||
/// A vbox Tauri has not populated is a changed assumption, not a panic.
|
|
||||||
///
|
|
||||||
/// TRACES: UR-080 | DR-231
|
|
||||||
#[test]
|
|
||||||
#[ignore = "requires a display"]
|
|
||||||
fn test_an_empty_vbox_is_an_error_not_a_panic() {
|
|
||||||
if gtk::init().is_err() {
|
|
||||||
return;
|
return;
|
||||||
|
};
|
||||||
|
// Log the first frame, and again whenever the target size changes. Latching
|
||||||
|
// this once per session hid the case that matters: a second file, rendered
|
||||||
|
// at a different size, in a window that never moved. "The picture is a small
|
||||||
|
// box in the middle" and "the picture fills the widget" are indistinguishable
|
||||||
|
// from outside without it.
|
||||||
|
if !s.logged_first_frame || s.logged_size != (width, height) {
|
||||||
|
s.logged_first_frame = true;
|
||||||
|
s.logged_size = (width, height);
|
||||||
|
info!("[VideoSurface] rendering {width}x{height} (texture {texture})");
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
cr.draw_from_gl(
|
||||||
|
&window,
|
||||||
|
texture as i32,
|
||||||
|
GL_TEXTURE,
|
||||||
|
scale,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
);
|
||||||
|
// Tell mpv the frame reached the screen. Without this it has nothing to
|
||||||
|
// pace against — see DR-233.
|
||||||
|
if let Some(render) = s.render.as_ref() {
|
||||||
|
render.report_swap();
|
||||||
}
|
}
|
||||||
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
|
||||||
assert!(matches!(attach(&vbox), Err(SurfaceError::NoWebviewChild)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,8 @@
|
|||||||
"height": 800,
|
"height": 800,
|
||||||
"minWidth": 800,
|
"minWidth": 800,
|
||||||
"minHeight": 600,
|
"minHeight": 600,
|
||||||
"resizable": true
|
"resizable": true,
|
||||||
|
"transparent": true
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"security": {
|
"security": {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
-->
|
-->
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { goto } from "$app/navigation";
|
import { goto } from "$app/navigation";
|
||||||
|
import { formatDuration } from "$lib/utils/duration";
|
||||||
import { truncateMiddle } from "$lib/utils/truncateMiddle";
|
import { truncateMiddle } from "$lib/utils/truncateMiddle";
|
||||||
import type { MediaItem } from "$lib/api/types";
|
import type { MediaItem } from "$lib/api/types";
|
||||||
import CachedImage from "$lib/components/common/CachedImage.svelte";
|
import CachedImage from "$lib/components/common/CachedImage.svelte";
|
||||||
@@ -88,18 +89,6 @@
|
|||||||
: null,
|
: null,
|
||||||
);
|
);
|
||||||
|
|
||||||
function formatDuration(ms?: number | null): string {
|
|
||||||
if (!ms) return "";
|
|
||||||
const seconds = Math.floor(ms / 1000);
|
|
||||||
const hours = Math.floor(seconds / 3600);
|
|
||||||
const minutes = Math.floor((seconds % 3600) / 60);
|
|
||||||
|
|
||||||
if (hours > 0) {
|
|
||||||
return `${hours}h ${minutes}m`;
|
|
||||||
}
|
|
||||||
return `${minutes}m`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getProgress(ep: MediaItem): number {
|
function getProgress(ep: MediaItem): number {
|
||||||
if (!ep.userData || !ep.durationMs) {
|
if (!ep.userData || !ep.durationMs) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -117,7 +106,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const episodeLabel = $derived(`S${episode.parentIndexNumber || 1}E${episode.indexNumber || 1}`);
|
const episodeLabel = $derived(`S${episode.parentIndexNumber || 1}E${episode.indexNumber || 1}`);
|
||||||
const duration = $derived(formatDuration(episode.durationMs));
|
const duration = $derived(formatDuration(episode.durationMs, "h m"));
|
||||||
const progress = $derived(getProgress(episode));
|
const progress = $derived(getProgress(episode));
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { playerController } from "$lib/player";
|
import { playerController } from "$lib/player";
|
||||||
|
import { formatDuration } from "$lib/utils/duration";
|
||||||
import { truncateMiddle } from "$lib/utils/truncateMiddle";
|
import { truncateMiddle } from "$lib/utils/truncateMiddle";
|
||||||
import { dndzone, SOURCES, TRIGGERS } from "svelte-dnd-action";
|
import { dndzone, SOURCES, TRIGGERS } from "svelte-dnd-action";
|
||||||
import type { MediaItem } from "$lib/api/types";
|
import type { MediaItem } from "$lib/api/types";
|
||||||
@@ -34,14 +35,6 @@
|
|||||||
let dragDisabled = $state(true);
|
let dragDisabled = $state(true);
|
||||||
const flipDurationMs = 200;
|
const flipDurationMs = 200;
|
||||||
|
|
||||||
function formatDuration(ms?: number | null): string {
|
|
||||||
if (!ms) return "";
|
|
||||||
const seconds = Math.floor(ms / 1000);
|
|
||||||
const mins = Math.floor(seconds / 60);
|
|
||||||
const secs = seconds % 60;
|
|
||||||
return `${mins}:${secs.toString().padStart(2, "0")}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleConsider(
|
function handleConsider(
|
||||||
e: CustomEvent<{ items: DndItem[]; info: { source: string; trigger: string } }>,
|
e: CustomEvent<{ items: DndItem[]; info: { source: string; trigger: string } }>,
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<!-- TRACES: UR-003, UR-005, UR-020, UR-021, UR-026, UR-040, UR-061 | DR-010, DR-023, DR-024, DR-051, DR-052, DR-092, DR-098, DR-099 -->
|
<!-- TRACES: UR-003, UR-005, UR-020, UR-021, UR-026, UR-040, UR-061 | DR-010, DR-023, DR-024, DR-051, DR-052, DR-092, DR-098, DR-099 -->
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount, onDestroy, tick, untrack } from "svelte";
|
import { onMount, onDestroy, tick, untrack } from "svelte";
|
||||||
|
import { planFullscreen } from "./fullscreenTarget";
|
||||||
import { get } from "svelte/store";
|
import { get } from "svelte/store";
|
||||||
import { goto } from "$app/navigation";
|
import { goto } from "$app/navigation";
|
||||||
import { commands } from "$lib/api/bindings";
|
import { commands } from "$lib/api/bindings";
|
||||||
@@ -2085,23 +2086,47 @@
|
|||||||
// Activity, so on its own it left the status and navigation bars painted over
|
// Activity, so on its own it left the status and navigation bars painted over
|
||||||
// the video. The native bridge is what actually makes fullscreen full screen;
|
// the video. The native bridge is what actually makes fullscreen full screen;
|
||||||
// requestFullscreen() still does the work everywhere else. (UR-066, DR-157)
|
// requestFullscreen() still does the work everywhere else. (UR-066, DR-157)
|
||||||
function toggleFullscreen() {
|
async function toggleFullscreen() {
|
||||||
|
// A native surface draws the picture *behind* the webview at window size, so
|
||||||
|
// fullscreening the document alone leaves the video at its old size while
|
||||||
|
// the page around it expands. See fullscreenTarget.ts. (DR-240)
|
||||||
|
const plan = planFullscreen(!useHtml5Element);
|
||||||
|
|
||||||
if (!document.fullscreenElement) {
|
if (!document.fullscreenElement) {
|
||||||
|
if (plan.document) {
|
||||||
document.documentElement.requestFullscreen().catch((err) => {
|
document.documentElement.requestFullscreen().catch((err) => {
|
||||||
// WebKitGTK rejects when the gesture isn't recognised as user-activated;
|
// WebKitGTK rejects when the gesture isn't recognised as user-activated;
|
||||||
// the immersive call below is what matters on Android, so don't let a
|
// the immersive call below is what matters on Android, so don't let a
|
||||||
// rejection here abort it.
|
// rejection here abort it.
|
||||||
log.warn("requestFullscreen rejected:", err);
|
log.warn("requestFullscreen rejected:", err);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
if (plan.osWindow) {
|
||||||
|
await setOsWindowFullscreen(true);
|
||||||
|
}
|
||||||
enterImmersive();
|
enterImmersive();
|
||||||
isFullscreen = true;
|
isFullscreen = true;
|
||||||
} else {
|
} else {
|
||||||
document.exitFullscreen();
|
document.exitFullscreen();
|
||||||
|
if (plan.osWindow) {
|
||||||
|
await setOsWindowFullscreen(false);
|
||||||
|
}
|
||||||
exitImmersive();
|
exitImmersive();
|
||||||
isFullscreen = false;
|
isFullscreen = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resize the OS window itself. Best-effort: a platform without a window to
|
||||||
|
/// resize (Android) must not break the rest of the toggle.
|
||||||
|
async function setOsWindowFullscreen(on: boolean) {
|
||||||
|
try {
|
||||||
|
const { getCurrentWindow } = await import("@tauri-apps/api/window");
|
||||||
|
await getCurrentWindow().setFullscreen(on);
|
||||||
|
} catch (err) {
|
||||||
|
log.warn("setFullscreen on the OS window failed:", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function formatTime(seconds: number): string {
|
function formatTime(seconds: number): string {
|
||||||
const mins = Math.floor(seconds / 60);
|
const mins = Math.floor(seconds / 60);
|
||||||
const secs = Math.floor(seconds % 60);
|
const secs = Math.floor(seconds % 60);
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { planFullscreen } from "./fullscreenTarget";
|
||||||
|
|
||||||
|
describe("planFullscreen", () => {
|
||||||
|
it("fullscreens only the document when an in-document <video> renders", () => {
|
||||||
|
// Unchanged behaviour: WebKit scales the element, the window need not move.
|
||||||
|
expect(planFullscreen(false)).toEqual({ document: true, osWindow: false });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("also fullscreens the OS window when a native surface renders", () => {
|
||||||
|
// The picture is drawn behind the webview at window size, so a
|
||||||
|
// document-only fullscreen leaves it at the old size.
|
||||||
|
expect(planFullscreen(true)).toEqual({ document: true, osWindow: true });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* Which surfaces a fullscreen toggle has to move.
|
||||||
|
*
|
||||||
|
* `requestFullscreen()` only ever fullscreens the *document*. That was
|
||||||
|
* sufficient while every renderer lived inside it: the HTML5 `<video>` element
|
||||||
|
* is part of the document, so WebKit scaled it to the screen and the OS
|
||||||
|
* window's real size never mattered.
|
||||||
|
*
|
||||||
|
* A native video surface is drawn *behind* the webview at **window** size, so a
|
||||||
|
* document-only fullscreen leaves the picture exactly where it was while the
|
||||||
|
* page around it goes fullscreen. On WebKitGTK the observed result is a
|
||||||
|
* maximised window with decorations still taking a strip of the screen — the
|
||||||
|
* video renders correctly, at the wrong size, which reads as "fullscreen is
|
||||||
|
* broken" rather than as a windowing problem.
|
||||||
|
*
|
||||||
|
* Android already needed its own answer here for the system bars (DR-157); this
|
||||||
|
* is the desktop equivalent of the same rule: whoever actually owns the pixels
|
||||||
|
* has to be the thing that goes fullscreen.
|
||||||
|
*
|
||||||
|
* TRACES: UR-066 | DR-240 | UT-219
|
||||||
|
*/
|
||||||
|
export interface FullscreenPlan {
|
||||||
|
/** Ask the document to go fullscreen (harmless everywhere, needed for CSS). */
|
||||||
|
document: boolean;
|
||||||
|
/** Resize the OS window itself. Required when a native surface owns the picture. */
|
||||||
|
osWindow: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param rendersNatively true when a native surface (mpv/ExoPlayer) draws the
|
||||||
|
* picture rather than an in-document `<video>` element.
|
||||||
|
*/
|
||||||
|
export function planFullscreen(rendersNatively: boolean): FullscreenPlan {
|
||||||
|
return { document: true, osWindow: rendersNatively };
|
||||||
|
}
|
||||||
@@ -72,8 +72,11 @@ describe("waitForRepository", () => {
|
|||||||
const w = makeWaiter();
|
const w = makeWaiter();
|
||||||
const repo = {};
|
const repo = {};
|
||||||
const pending = w.waitForRepository(1000);
|
const pending = w.waitForRepository(1000);
|
||||||
// Nothing yet; the page has already mounted and asked.
|
// Nothing yet; the page has already mounted and asked. Published on a
|
||||||
setTimeout(() => w.publish(repo), 10);
|
// microtask rather than a timer: the point is *ordering* (asked before it
|
||||||
|
// arrived), and a wall-clock delay would make this a race under load.
|
||||||
|
await Promise.resolve();
|
||||||
|
w.publish(repo);
|
||||||
await expect(pending).resolves.toBe(repo);
|
await expect(pending).resolves.toBe(repo);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect } from "vitest";
|
import { describe, it, expect } from "vitest";
|
||||||
import { formatDuration, formatSecondsDuration } from "./duration";
|
import { formatDuration } from "./duration";
|
||||||
|
|
||||||
describe("formatDuration", () => {
|
describe("formatDuration", () => {
|
||||||
it("should format duration from milliseconds (mm:ss format)", () => {
|
it("should format duration from milliseconds (mm:ss format)", () => {
|
||||||
@@ -39,23 +39,3 @@ describe("formatDuration", () => {
|
|||||||
expect(formatDuration(9045000, "hh:mm:ss")).toBe("2:30:45");
|
expect(formatDuration(9045000, "hh:mm:ss")).toBe("2:30:45");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("formatSecondsDuration", () => {
|
|
||||||
it("should format duration from seconds (mm:ss format)", () => {
|
|
||||||
expect(formatSecondsDuration(1)).toBe("0:01");
|
|
||||||
expect(formatSecondsDuration(60)).toBe("1:00");
|
|
||||||
expect(formatSecondsDuration(61)).toBe("1:01");
|
|
||||||
expect(formatSecondsDuration(3661)).toBe("61:01");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should format duration with hh:mm:ss format", () => {
|
|
||||||
expect(formatSecondsDuration(3600, "hh:mm:ss")).toBe("1:00:00");
|
|
||||||
expect(formatSecondsDuration(3661, "hh:mm:ss")).toBe("1:01:01");
|
|
||||||
expect(formatSecondsDuration(7325, "hh:mm:ss")).toBe("2:02:05");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should pad minutes and seconds with leading zeros", () => {
|
|
||||||
expect(formatSecondsDuration(5, "hh:mm:ss")).toBe("0:00:05");
|
|
||||||
expect(formatSecondsDuration(65, "hh:mm:ss")).toBe("0:01:05");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|||||||
+13
-25
@@ -12,11 +12,23 @@
|
|||||||
* @param format Format type: "mm:ss" (default) or "hh:mm:ss"
|
* @param format Format type: "mm:ss" (default) or "hh:mm:ss"
|
||||||
* @returns Formatted duration string or empty string if no duration
|
* @returns Formatted duration string or empty string if no duration
|
||||||
*/
|
*/
|
||||||
export function formatDuration(ms?: number | null, format: "mm:ss" | "hh:mm:ss" = "mm:ss"): string {
|
export function formatDuration(
|
||||||
|
ms?: number | null,
|
||||||
|
format: "mm:ss" | "hh:mm:ss" | "h m" = "mm:ss",
|
||||||
|
): string {
|
||||||
if (!ms) return "";
|
if (!ms) return "";
|
||||||
|
|
||||||
const totalSeconds = Math.floor(ms / 1000);
|
const totalSeconds = Math.floor(ms / 1000);
|
||||||
|
|
||||||
|
// "1h 23m" / "45m" — the shape a runtime is read at a glance, as opposed to
|
||||||
|
// the clock shape a *position* is read at. Three components had hand-rolled
|
||||||
|
// this identically; it belongs here with the other two.
|
||||||
|
if (format === "h m") {
|
||||||
|
const hours = Math.floor(totalSeconds / 3600);
|
||||||
|
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||||
|
return hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
|
||||||
|
}
|
||||||
|
|
||||||
if (format === "hh:mm:ss") {
|
if (format === "hh:mm:ss") {
|
||||||
const hours = Math.floor(totalSeconds / 3600);
|
const hours = Math.floor(totalSeconds / 3600);
|
||||||
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||||
@@ -30,27 +42,3 @@ export function formatDuration(ms?: number | null, format: "mm:ss" | "hh:mm:ss"
|
|||||||
const seconds = totalSeconds % 60;
|
const seconds = totalSeconds % 60;
|
||||||
return `${minutes}:${seconds.toString().padStart(2, "0")}`;
|
return `${minutes}:${seconds.toString().padStart(2, "0")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert seconds to formatted duration string
|
|
||||||
* @param seconds Duration in seconds
|
|
||||||
* @param format Format type: "mm:ss" (default) or "hh:mm:ss"
|
|
||||||
* @returns Formatted duration string
|
|
||||||
*/
|
|
||||||
export function formatSecondsDuration(
|
|
||||||
seconds: number,
|
|
||||||
format: "mm:ss" | "hh:mm:ss" = "mm:ss",
|
|
||||||
): string {
|
|
||||||
if (format === "hh:mm:ss") {
|
|
||||||
const hours = Math.floor(seconds / 3600);
|
|
||||||
const minutes = Math.floor((seconds % 3600) / 60);
|
|
||||||
const secs = seconds % 60;
|
|
||||||
|
|
||||||
return `${hours}:${minutes.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default "mm:ss" format
|
|
||||||
const minutes = Math.floor(seconds / 60);
|
|
||||||
const secs = seconds % 60;
|
|
||||||
return `${minutes}:${secs.toString().padStart(2, "0")}`;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<!-- TRACES: UR-035, UR-038, UR-048, UR-058, UR-062 | DR-043, DR-062, DR-102, DR-103, DR-142 -->
|
<!-- TRACES: UR-035, UR-038, UR-048, UR-058, UR-062 | DR-043, DR-062, DR-102, DR-103, DR-142 -->
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount, untrack } from "svelte";
|
import { onMount, untrack } from "svelte";
|
||||||
|
import { formatDuration } from "$lib/utils/duration";
|
||||||
import { page } from "$app/stores";
|
import { page } from "$app/stores";
|
||||||
import { goto } from "$app/navigation";
|
import { goto } from "$app/navigation";
|
||||||
import { navigateBack } from "$lib/utils/navigation";
|
import { navigateBack } from "$lib/utils/navigation";
|
||||||
@@ -250,18 +251,6 @@
|
|||||||
|
|
||||||
// Images now handled by CachedImage component
|
// Images now handled by CachedImage component
|
||||||
|
|
||||||
function formatDuration(ms?: number | null): string {
|
|
||||||
if (!ms) return "";
|
|
||||||
const seconds = Math.floor(ms / 1000);
|
|
||||||
const hours = Math.floor(seconds / 3600);
|
|
||||||
const minutes = Math.floor((seconds % 3600) / 60);
|
|
||||||
|
|
||||||
if (hours > 0) {
|
|
||||||
return `${hours}h ${minutes}m`;
|
|
||||||
}
|
|
||||||
return `${minutes}m`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleItemClick(clickedItem: MediaItem | Library) {
|
function handleItemClick(clickedItem: MediaItem | Library) {
|
||||||
if (!("kind" in clickedItem)) {
|
if (!("kind" in clickedItem)) {
|
||||||
// Library item - navigate to library
|
// Library item - navigate to library
|
||||||
@@ -534,7 +523,7 @@
|
|||||||
>
|
>
|
||||||
{/if}
|
{/if}
|
||||||
{#if item.durationMs}
|
{#if item.durationMs}
|
||||||
<span>{formatDuration(item.durationMs)}</span>
|
<span>{formatDuration(item.durationMs, "h m")}</span>
|
||||||
{/if}
|
{/if}
|
||||||
{#if item.communityRating}
|
{#if item.communityRating}
|
||||||
<span class="flex items-center gap-1">
|
<span class="flex items-center gap-1">
|
||||||
|
|||||||
Reference in New Issue
Block a user