Compare commits

...
Author SHA1 Message Date
sanapachandClaude Opus 5.5 0da9862cf3 Add podcast RSS subscriptions with streaming playback
(cherry picked from commit 20167c5b5e2a55b1965fe7f7a095b909ce2d9267)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 15:15:13 +02:00
Aleksandr Bogomiakov 292f17fee1 Added MODE status 2026-11-18 16:18:56 +00:00
Aleksandr Bogomiakov 3e46f21804 Fixed artist card fed update 2026-11-18 15:43:43 +00:00
Aleksandr Bogomiakov 7f0ca2984f Fixed artist card fed update 2026-11-18 15:41:54 +00:00
Aleksandr Bogomiakov 0ad3844d37 Bump 2026-11-16 00:17:50 +00:00
Aleksandr Bogomiakov 2253ec7d91 Added simple status visual 2026-11-15 23:48:33 +00:00
Aleksandr Bogomiakov 8dd3ededf6 Fixed queue bug 2026-10-10 00:19:10 +01:00
Aleksandr Bogomiakov 81fdcf13ae Fixed queue bug 2026-10-10 00:18:07 +01:00
Aleksandr Bogomiakov a6d502e319 Fix duplicate queue playback position 2026-10-09 23:54:07 +01:00
Aleksandr Bogomiakov 3298204f2e Bump lock 2026-10-09 15:42:17 +01:00
Aleksandr Bogomiakov 9155bb9e00 Fixed release fed search regression 2026-10-09 15:34:20 +01:00
ab b07bcd4ddd Added fix 2026-09-10 22:56:06 +03:00
ab 966cf4b437 Fix connected devices 2026-09-10 18:05:00 +03:00
ab 532d6ee1e9 Integrate shared playback coordination and release 0.3.1 2026-09-10 17:07:41 +03:00
ab 5a193d839f Reworked settings menu 2026-09-07 13:06:46 +03:00
ab 7d6af59e97 fix lock 2026-09-07 12:41:27 +03:00
ab 32c976beb8 Added self updater 2026-09-07 12:36:48 +03:00
Ultradesu 2607314da3 Fix lock 2026-09-02 15:50:41 +01:00
Ultradesu f901836929 fix federation recovery after sleep 2026-09-02 15:10:20 +01:00
Ultradesu 0c30cdfcc6 Added ogg opus support 2026-08-14 11:42:54 +01:00
Ultradesu cfc0bfa0e5 Moved library to FRID 2026-08-11 18:13:02 +01:00
Aleksandr Bogomiakov 079d87a831 Merge local and federated similarity results 2026-08-10 20:51:21 +01:00
Aleksandr Bogomiakov add764e51d Stabilize similarity and library status cards 2026-08-10 20:18:51 +01:00
Aleksandr Bogomiakov 87cb7fe74c Route similarity search through the DHT 2026-08-10 19:40:37 +01:00
60 changed files with 14739 additions and 6257 deletions

No files matched your search

+6 -1
View File
@@ -98,7 +98,12 @@ jobs:
run: |
set -euo pipefail
gh release create "$RELEASE_TAG" artifacts/*/* \
# Hash archive bytes; manifest entries use the release asset basenames.
for archive in artifacts/*/*; do
(cd "$(dirname "$archive")" && sha256sum "$(basename "$archive")")
done > SHA256SUMS
gh release create "$RELEASE_TAG" artifacts/*/* SHA256SUMS \
--title "furumi ${RELEASE_TAG}" \
--notes "Release ${RELEASE_TAG}" \
--verify-tag
+3
View File
@@ -65,6 +65,9 @@ update it only when dependencies change.
coordination.
- `src/ui/` renders `AppState`; rendering code must not start background work
or mutate persistence.
- `src/podcasts/` owns RSS parsing, podcast persistence, feed fetching and
the HTTP range-request stream reader; `src/app/podcast_runtime.rs`
schedules its work and folds results into `AppState`.
- `src/media.rs` owns OS media controls.
- `src/visualizer.rs` and `src/visualizations/` own the Rhai visualization
host and bundled scripts.
+93
View File
@@ -123,6 +123,30 @@ The DHT is the distributed index. Nodes publish compact searchable
descriptions of their local library and query the network without contacting a
central search service.
Opening a local artist or release card in a network-enabled library view
enriches it asynchronously. DHT matches identify likely catalog owners, while
a bounded set of known peers can also answer directly when the index is
incomplete. Card results are replaceable snapshots: reopening a card or
periodic maintenance retries discovery, with at most one request per card in
flight. A failed refresh keeps the last usable snapshot, and never prevents
browsing the local tracks.
Similarity discovery uses a separate schema-independent DHT overlay. A node
derives a deterministic 256-bit routing signature from every local embedding,
groups them into fixed two-level LSH buckets, and publishes compact summaries
containing only fine-bucket representatives, its peer identity, and the ticket
needed to dial a previously unknown owner. The owner signs every summary with
its existing transport key, so storage peers can relay and cache it but cannot
impersonate or modify it. Summaries expire with the ordinary library-record TTL
and are replaceable federation cache, never local library authority.
A similarity search first performs bounded multi-probe LSH lookups to rank
likely owners, then sends the existing normalized-vector request directly to
at most 16 peers initially and 48 on fallback. Known peers remain a rollout
fallback. The model, preprocessing, durable embeddings, exact cosine search,
and consent policy remain client-owned; `music-dht` owns only compatible
routing math, signed records, replication, and wire bounds.
Once a peer is known, communication moves to direct P2P streams provided by
iroh through `music-dht`. Furumi defines separate application protocols for
catalog requests, audio transfer, and trusted-device synchronization. This
@@ -174,6 +198,13 @@ must propagate and converge, not an ephemeral server-side session flag.
## Playback across devices
A persisted device identity has exactly one live coordinator. The TUI holds
an OS file lock beside its device-sync database for the entire runtime; two
installations using the same user data directory cannot open it concurrently.
Network polling is independent per trusted peer, with one in-flight exchange
per peer and a bounded deadline. A stalled peer must not delay the next poll
of a healthy output. Poll tasks are cancelled when federation shuts down.
Playback has one logical state but remains physically local to the device
producing audio.
@@ -270,6 +301,17 @@ created or edited while Furumi is running without rebuilding or restarting the
application. Bundled scripts use the same path and contract as user scripts,
so built-in and custom visualizations exercise the same runtime.
The v2 primitives are sufficient for small perspective wireframes without a
native 3D engine. The bundled morphing cube transforms eight vertices in Rhai
and batches its twelve edges into trace/vertical-line commands. Perspective,
audio-driven deformation, and depth cues stay in the user-editable script;
Rust rasterizes the resulting 2D commands. This deliberately avoids per-cell
script command maps and does not provide filled meshes or a depth buffer.
The richer cube preset adds bounded particle, ring, inner-cage, and echo
layers. Echo poses are reconstructed from earlier times with current audio
features, rather than retaining previous frames; particles have deterministic
lifetimes. Small terminals omit expensive layers and reduce particle counts.
The Rhai engine is configured as a sandboxed computation environment. Module
loading through `import` and `export` is disabled, no filesystem or network API
is exposed to scripts, and execution is bounded by limits on operations, call
@@ -282,6 +324,34 @@ intended role: transforming current audio features into drawing commands. It
is not a plugin mechanism for accessing the library, network, or player
controls.
Fullscreen execution currently happens synchronously during UI drawing, with a fresh
Rhai scope for each frame; scripts should derive motion from time and the
already-smoothed audio features rather than rely on retained mutable state.
The 50 ms animation timer gives a nominal 20 FPS cadence (other events can
also trigger redraws). Operation limits are not wall-clock deadlines. The
host also checks the script modification time during drawing, so slow script
storage or expensive custom scripts can still delay input. Benchmark script
evaluation and buffer drawing separately from real terminal throughput before
increasing scene complexity or the refresh rate.
The embedded status surface uses the same Rhai engine, input map, command
decoder, and clipped buffer rasterizer with a one-row viewport. It has an
independent AST cache and a fixed user-editable script in
`visualizations/status/braille.rhai`, installed if absent. Exact copies of the
previous bundled default can be atomically upgraded; edited files are preserved. It is not a
fullscreen preset. During local playback the visual timer samples the audio
analyzer without increasing trusted-device publication frequency. At most
one blocking render job is in flight; excess ticks are dropped. File access,
compilation, evaluation, and buffer preparation happen on that worker.
An `AppEvent` delivers a prepared row to `AppState`; UI drawing only copies
cells. Its audio input comes from the decoded-source analyzer before output
volume scaling; the volume field remains informational. Pause suspends jobs
and retains the last frame, even when the row needs resizing. Generation
checks discard results invalidated by track changes, resize, pause/resume,
or stop. Script errors are confined to the row and retried so
editing the file repairs the display. Status scripts have a smaller operation
budget, bounded source/output sizes, and cannot draw outside their viewport.
## Music-similarity indexing
Similarity search is an optional local capability and is disabled by default.
@@ -307,6 +377,25 @@ timeouts are bounded; incompatible profiles are rejected. This direct
peer-selection layer can later be replaced by DHT routing without changing
local storage or ranking.
## Podcasts
Podcasts are a separate local source beside the music library. A dedicated
SQLite store holds subscriptions and episode metadata keyed by
`(podcast, RSS GUID)`. A feed refresh upserts episodes and never deletes
ones that dropped out of the feed. Refreshes use conditional HTTP requests
and run in the background with bounded concurrency. The store is
independent of `furumi-library`, so podcast data cannot affect the music
catalog, its migrations or federation publishing.
Episodes enter the queue as ordinary `TrackItem`s whose `file_path` is the
remote audio URL and whose ids sit in a reserved negative range. Playback
opens an HTTP range-request reader on the blocking pool. The reader buffers a
bounded window ahead of the decoder, and rodio uses coarse, byte-based
seeking for it, so a seek far into an episode fetches only the bytes it
needs. A sequence number discards a stream that finishes connecting after a
newer playback request. Episodes have no content id: listening history,
likes, playlists and cross-device handoff skip them.
## Persistence boundaries
Furumi stores different kinds of state according to their lifetime:
@@ -317,6 +406,8 @@ Furumi stores different kinds of state according to their lifetime:
| Device operation log and replicas | SQLite | Offline synchronization |
| Versioned track embeddings | SQLite | Durable, locally rebuildable similarity data |
| Federation catalog cache | SQLite/cache | Faster network browsing |
| Podcast subscriptions and episode metadata | SQLite (`podcasts.sqlite3`) | Durable local podcast catalog; refetchable from feeds |
| Podcast covers | Filesystem cache | Replaceable artwork |
| Audio and artwork cache | Filesystem cache | Reusable fetched data |
| Settings, keymap, identity | Platform config/data dirs | Node configuration |
| Queue and playback snapshots | Application/device sync state | Continuity and handoff |
@@ -353,6 +444,8 @@ The source tree follows the architectural responsibilities:
- `devices.rs` owns trusted-device replication and playback coordination;
- `app/` owns state transitions and runtime orchestration;
- `ui/` renders the TUI;
- `podcasts/` owns RSS parsing, the podcast store, feed fetching and the
HTTP range-request stream reader;
- `media.rs` integrates platform media controls;
- `visualizer.rs` hosts programmable Rhai visualizations.
+25 -2
View File
@@ -9,6 +9,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- A Podcasts tab (key `6`) for RSS subscriptions: add a feed by URL, browse
the episode catalog with title search and date sorting, and play an episode
through the regular player and queue.
- Episodes stream over HTTP with range-request seeking; only feed metadata
is stored locally, in `podcasts.sqlite3`, with covers in the cache
directory.
- Background refresh of stale feeds (conditional requests, GUID
deduplication) and a `RefreshPodcasts` action (`Shift+R` in the
`podcasts` key context).
## [0.2.8] - 2026-09-02
### Added
- Optional offline music-similarity search for local tracks, backed by
versioned SQLite embeddings and a replaceable exact in-memory cosine index.
- Automatic SHA-256-verified download and local inference for the first ONNX
@@ -20,12 +34,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Track-seeded similarity search from the track-information popup, including
bounded federated queries to compatible known peers.
- The `furumi-fd/similarity/1` protocol in the visible protocol-version status.
- Decentralized `similarity_dht` routing with signed anonymous two-level LSH
summaries, multi-probe lookup beyond the locally known peer set, and known-
peer fallback during gradual network upgrades.
### Changed
- Similarity wire types, bounds, validation, and stream framing now come from
the shared `music-dht 0.3.1` API so native, web, and future clients can
the shared `music-dht 0.4` API so native, web, and future clients can
interoperate without sharing an embedding implementation.
- Existing SQLite embeddings are backfilled once with compact 256-bit routing
signatures; new embeddings store them immediately without changing exact
local cosine search.
- A similarity result page keeps the source track first as query context while
excluding it from the actual nearest-neighbor ranking, labels the mode as
`Search similar to`, and suppresses near-identical embeddings across releases
@@ -35,6 +55,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Federation now recovers automatically after sleep, prolonged idle, or a
degraded rendezvous transport while preserving local playback and state.
- Current-track information (`Shift+I`) now uses the enriched queue entry, so
it shows the same complete metadata and similarity action as `I` on that
track in the queue.
@@ -68,5 +90,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Music-directory validation and migration now reject overlapping changes,
resolve canonical paths, and produce Windows-portable managed filenames.
[Unreleased]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.5...HEAD
[Unreleased]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.8...HEAD
[0.2.8]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.7...v0.2.8
[0.2.5]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.4...v0.2.5
Generated
+455 -234
View File
File diff suppressed because it is too large. Load diff
+23 -3
View File
@@ -1,6 +1,6 @@
[package]
name = "furumi_tui"
version = "0.2.6"
version = "0.3.5"
edition = "2024"
rust-version = "1.97"
description = "A federated P2P player for personal music libraries"
@@ -11,27 +11,44 @@ name = "furumi"
path = "src/main.rs"
[dependencies]
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
semver = "1"
self-replace = "1.5"
tempfile = "3"
fs2 = "0.4"
flate2 = "1"
tar = "0.4"
zip = { version = "4", default-features = false, features = ["deflate"] }
object = { version = "0.36", default-features = false, features = ["read", "std"] }
anyhow = "1.0.102"
blake3 = "1"
crokey = "1.4.0"
crossterm = { version = "0.29.0", features = ["event-stream"] }
directories = "6.0.0"
# Podcast feeds: non-UTF-8 XML declarations (windows-1251, koi8-r, ...).
encoding_rs = "0.8.35"
futures-util = "0.3.32"
furumi-library = "0.2.0"
image = { version = "0.25.10", default-features = false, features = ["jpeg", "png", "webp", "gif", "bmp"] }
lofty = "0.22"
# P2P federation: library index in a shared DHT + audio streaming between
# peers (same protocol as furumi-fd).
music-dht = "0.3.1"
music-dht = "0.5.0"
# Podcast RSS/Atom parsing.
quick-xml = "0.41.0"
ratatui = "0.30.1"
reqwest = { version = "0.12.28", default-features = false, features = ["rustls-tls", "stream"] }
reqwest = { version = "0.12.28", default-features = false, features = ["rustls-tls", "stream", "blocking"] }
rhai = { version = "1", features = ["sync"] }
rodio = { version = "0.22.2", default-features = false, features = ["playback", "mp3", "flac", "vorbis", "wav", "symphonia-aac", "symphonia-isomp4", "symphonia-alac"] }
rusty-opus = "0.9.1"
rustfft = "6.4.1"
rusqlite = { version = "0.32", features = ["bundled", "functions"] }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.150"
sha2 = "0.10.9"
souvlaki = { version = "0.8.3", default-features = false, features = ["use_zbus"] }
symphonia = { version = "0.5.5", default-features = false, features = ["ogg"] }
thiserror = "2.0.18"
tokio = { version = "1.52.3", features = ["rt-multi-thread", "macros", "sync", "time", "fs", "io-util"] }
toml = "1.1.2"
@@ -53,3 +70,6 @@ windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_UI_Wi
[target."cfg(unix)".dependencies]
libc = "0.2.186"
[dev-dependencies]
base64 = "0.22.1"
+104
View File
@@ -72,10 +72,40 @@ without rebuilding the player.
Optional similarity search calculates versioned embeddings for local tracks
in the background and keeps them in SQLite. It works offline; after a separate
privacy consent it can also ask a bounded set of federation peers for matches.
Compatible peers are selected through signed, anonymous LSH summaries in a
decentralized DHT; no central recommendation index or shared calibration file
is required.
The first selectable model is downloaded on demand and is licensed separately
by MTG under CC BY-NC-SA 4.0 (a proprietary license is also available from
MTG); Furumi itself remains WTFPL.
## Podcasts
The **Podcasts** tab (key `6`) subscribes to podcast RSS feeds and plays
episodes through the same player, queue and media keys as your music.
- Choose **Add podcast** and paste the feed URL, for example
`https://podster.fm/rss.xml?pid=778`. Furumi stores the feed's metadata
only: titles, notes, dates and durations. Audio files are never
downloaded in bulk.
- Press `Enter` on a podcast to open its episode list. `/` searches episode
titles as you type, `v` switches between newest-first and oldest-first,
and `Shift+R` checks for new episodes.
- `Enter` on an episode plays it right away, ahead of the rest of your
queue. `a` / `Shift+A` queue it like a track. The episode streams over HTTP
and seeks with range requests, so jumping into a long episode does not
download everything before that point.
- Subscriptions are checked for new episodes in the background. A feed is
fetched again only after 30 minutes, and the request is conditional, so
an unchanged feed costs almost nothing. Episodes are matched by their RSS
GUID, so each one is listed once.
- `Shift+D` on a podcast in the list unsubscribes from it.
Subscriptions live in Furumi's data directory (`podcasts.sqlite3`). Covers are
kept in the cache directory. Podcasts are local to each device: they are not
yet synchronized between trusted devices or shared through federation, and
playback position is not remembered.
## Install
### macOS
@@ -98,6 +128,9 @@ Download a prebuilt archive from the
[GitHub releases](https://github.com/house-of-vanity/furumi_tui/releases), or
build Furumi from source with Rust 1.97 or newer:
The repository selects the stable Rust toolchain. With rustup, run
`rustup update stable` to install or update it before building.
```bash
cargo build --release --locked
./target/release/furumi
@@ -121,6 +154,28 @@ Import a music directory from Furumi's command line:
Federation, trusted-device pairing, and key bindings are configured directly
inside the player.
### Manual updates
In **Settings → Additional settings → Updates**, select **Check for updates**, then **Install update**
when a newer stable GitHub release is available. Downloads run in the background.
After installation, restart `furumi` to use the new version; playback is not
restarted automatically. Wait for an active update operation to finish before
quitting.
Updates replace the running executable in its installation directory, which
must be writable by your user. Release archives must include a matching entry
in the release's `SHA256SUMS` asset. Older releases without it cannot be installed
through this feature. The updater checks SHA-256 and the executable's format
and architecture before replacing it. Checksums provide integrity checking,
not publisher signatures. Settings and the local library are preserved.
There are no automatic startup checks. Only the existing release asset naming
scheme is supported; missing or incompatible platform builds are rejected.
The **Additional settings** window also contains the music save directory and
visualization controls. Use Up/Down (or j/k) to navigate, Enter to select, and
Esc to return to Settings. Long lists scroll with the selection.
### Now playing in tmux
While Furumi is running, a second invocation can print a cheap, single-line
@@ -170,6 +225,55 @@ cargo check --all-targets
cargo test --all-targets
```
## Playback coordination
Connected Devices uses the shared `music_dht::playback` engine from frid.
Newly started players take an idle/paused output after discovery and become
controllers when another device is playing. Missing output reports trigger
automatic failover; concurrent claims converge to one owner. The web gateway
uses a passive server profile and reports actual browser activity.
Only one TUI process may use a device identity. Installed and locally built
binaries use the same user data directory: close the installed player before
starting a development build. An OS file lock prevents duplicate coordinators
and is released automatically on exit or a crash. Older builds do not take
this lock, so close those explicitly when upgrading.
Connected-device polling runs independently per peer, so an offline or stalled
device does not postpone updates to live players.
The existing device menu handles manual transfers. Advanced policy can be set
in `settings.toml` without changing the UI:
```toml
[playback]
claim_on_startup = true
automatic_failover = true
take_paused_on_startup = true
discovery_ms = 3000
owner_timeout_ms = 120000
```
All clients must support the new coordination envelope for eventual single
output ownership. frid's `PLAYBACK_PROTOCOL.md` describes the protocol, adapter
contract and rollout. Local Cargo patches are only for development; publish
frid and bump the client dependency versions before releasing these changes.
Run the local protocol checks from the TUI repository:
```bash
cargo test localhost_devices_exchange_state_and_handoff
python scripts/test_device_interop.py ../furumusic
```
The first test uses real iroh streams, isolated SQLite databases, ownership
handoff, and a stalled peer. The second builds both player test binaries and
exchanges their actual JSON messages over `127.0.0.1`: queue metadata,
bidirectional handoff, pause/seek and duplicate command fencing. It exercises
the TUI command adapter and web player hub, but does not start a browser or
PostgreSQL and does not cover web database migrations. Both tests use temporary
identities and data; no running player or user library is used.
## License
Furumi is released under the
+1 -1
View File
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.97.0"
channel = "stable"
profile = "minimal"
components = ["clippy", "rust-src", "rustfmt"]
+65
View File
@@ -0,0 +1,65 @@
"""Build and run the real TUI and web protocol adapters against each other.
Usage: python scripts/test_device_interop.py [path/to/furumusic]
No user databases, accounts, audio outputs, or external servers are used.
"""
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
def test_binary(repo):
result = subprocess.run(
["cargo", "test", "--locked", "--no-run", "--message-format=json"],
cwd=repo, text=True, encoding="utf-8", stdout=subprocess.PIPE,
)
result.check_returncode()
binaries = []
for line in result.stdout.splitlines():
event = json.loads(line)
if event.get("reason") == "compiler-artifact" and event.get("profile", {}).get("test") and event.get("executable"):
binaries.append(event["executable"])
if len(binaries) != 1:
raise RuntimeError(f"Expected one player test binary in {repo}, found {binaries}")
return binaries[0]
def main():
tui = Path(__file__).resolve().parents[1]
web = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else tui.parent / "furumusic"
tui_bin, web_bin = test_binary(tui), test_binary(web)
with tempfile.TemporaryDirectory(prefix="furumi-interop-") as directory:
env = dict(os.environ, FURUMI_INTEROP_DIR=directory)
processes = []
try:
for binary, test in [(web_bin, "federation::devices::interop_tests::localhost_tui_peer"),
(tui_bin, "devices::interop_tests::localhost_web_peer")]:
listing = subprocess.check_output([binary, "--list"], text=True, encoding="utf-8")
if f"{test}: test" not in listing:
raise RuntimeError(f"Required test {test} is missing from {binary}")
processes.append(subprocess.Popen(
[binary, "--ignored", "--exact", test, "--nocapture"], env=env,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, encoding="utf-8",
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
))
codes = []
for process in processes:
output, _ = process.communicate(timeout=45)
print(output, end="")
codes.append(process.returncode)
if any(codes):
raise RuntimeError(f"Cross-player test failed: exit codes {codes}")
finally:
for process in processes:
if process.poll() is None:
process.kill()
process.wait()
print("PASS: TUI <-> WEB state, bidirectional handoff, pause/seek, duplicate fencing")
if __name__ == "__main__":
main()
+5 -1
View File
@@ -57,6 +57,8 @@ pub enum Action {
OpenSearch,
EditSelected,
DeleteSelected,
/// Podcasts: check the opened (or every) subscription for new episodes.
RefreshPodcasts,
}
/// Help-window sections, in display order.
@@ -138,7 +140,8 @@ impl Action {
Action::EditSelected
| Action::DeleteSelected
| Action::CycleSourceMode
| Action::OpenLibraryFilters => Category::Library,
| Action::OpenLibraryFilters
| Action::RefreshPodcasts => Category::Library,
Action::OpenSearch | Action::OpenCommandLine => Category::Search,
Action::ToggleHelp | Action::Quit => Category::System,
}
@@ -215,6 +218,7 @@ impl Action {
Action::OpenSearch => "Search artists, releases, tracks".into(),
Action::EditSelected => "Edit the selected item".into(),
Action::DeleteSelected => "Delete the selected item".into(),
Action::RefreshPodcasts => "Podcasts: check for new episodes".into(),
}
}
}
+9 -15
View File
@@ -8,7 +8,6 @@ use crate::app::Runtime;
use crate::app::command::{self, Command, Parsed};
use crate::app::event::AppEvent;
use crate::app::state::{AppState, GlobalView, SearchState, Tab};
use crate::library::models::SearchResults;
const SEARCH_DEBOUNCE: Duration = Duration::from_millis(180);
const SEARCH_LIMIT: i64 = 12;
@@ -99,6 +98,10 @@ fn set_view_cursor_zero(state: &mut AppState) {
/// and the receiver drops responses that arrive out of date.
pub(super) fn schedule_search(state: &mut AppState, runtime: &Runtime) {
state.search.similarity_source = None;
state.search.similarity_source_track = None;
state.search.similarity_tracks.clear();
state.search.similarity_stats = None;
state.search.similarity_error = None;
let seq = runtime.search_seq.fetch_add(1, Ordering::SeqCst) + 1;
let query = state.search.query.clone();
if query.is_empty() {
@@ -160,6 +163,10 @@ pub(super) fn schedule_similarity_search(
format!("{} — {artist}", track.title)
};
state.search.similarity_source = Some(track.id);
state.search.similarity_source_track = Some(track.clone());
state.search.similarity_tracks.clear();
state.search.similarity_stats = None;
state.search.similarity_error = None;
state.search.loading = true;
state.search.results = None;
state.search.fed_tracks.clear();
@@ -178,23 +185,10 @@ pub(super) fn schedule_similarity_search(
let similarity = Arc::clone(&runtime.similarity);
let tx = runtime.event_tx.clone();
let track_id = track.id;
let source_track = track.clone();
tokio::task::spawn_blocking(move || {
let result = similarity
.search_track(track_id, 49)
.map(|(matches, query)| {
let mut tracks = Vec::with_capacity(1 + matches.len());
tracks.push(source_track);
tracks.extend(matches.into_iter().map(|found| found.track));
(
SearchResults {
artists: Vec::new(),
releases: Vec::new(),
tracks,
},
query,
)
});
.map(|(matches, query)| (matches, query));
let (result, query) = match result {
Ok((results, query)) => (Ok(results), Some(query)),
Err(err) => (Err(format!("{err:#}")), None),
+141
View File
@@ -0,0 +1,141 @@
//! Scheduling for local cards enriched by replaceable federation data.
use std::collections::HashMap;
use std::time::{Duration, Instant};
use super::state::{AppState, GlobalView, Loadable, Tab};
const REFRESH_INTERVAL: Duration = Duration::from_secs(30);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(super) enum Card {
Artist(i64),
Release(i64),
}
#[derive(Debug, Default)]
pub(super) struct RefreshState {
visible: Option<Card>,
/// None means a request is in flight; Some is its completion time.
requests: HashMap<Card, Option<Instant>>,
}
impl RefreshState {
pub(super) fn is_loading(&self, card: Card) -> bool {
matches!(self.requests.get(&card), Some(None))
}
fn begin(&mut self, visible: Option<Card>, now: Instant) -> bool {
let reopened = self.visible != visible;
self.visible = visible;
let Some(card) = visible else { return false };
match self.requests.get(&card) {
Some(None) => return false,
Some(Some(completed))
if !reopened && now.duration_since(*completed) < REFRESH_INTERVAL =>
{
return false;
}
_ => {}
}
self.requests.insert(card, None);
true
}
pub(super) fn complete(&mut self, card: Card, now: Instant) {
self.requests.insert(card, Some(now));
}
}
#[derive(Debug, PartialEq, Eq)]
pub(super) enum Request {
Artist {
id: i64,
name: String,
},
Release {
id: i64,
title: String,
artists: Vec<String>,
},
}
/// Called on navigation and maintenance ticks. A cached success or failure
/// must not suppress discovery for the rest of the session.
pub(super) fn prepare(state: &mut AppState, now: Instant) -> Option<Request> {
let visible = if state.federation.settings.enabled
&& state.global.filters.source_mode.includes_network()
&& state.active_tab == Tab::Global
{
match state.global.stack.last() {
Some(GlobalView::Artist { id, .. }) => Some(Card::Artist(*id)),
Some(GlobalView::Release { id, .. }) => Some(Card::Release(*id)),
_ => None,
}
} else {
None
};
let request = match visible {
Some(Card::Artist(id)) => match state.artist_views.get(&id) {
Some(Loadable::Ready(detail)) => Some(Request::Artist {
id,
name: detail.name.clone(),
}),
_ => None,
},
Some(Card::Release(id)) => match state.release_views.get(&id) {
Some(Loadable::Ready(detail)) => {
let mut artists = Vec::new();
for artist in &detail.artists {
if !artists.iter().any(|name: &String| {
music_dht::normalize_name(name) == music_dht::normalize_name(&artist.name)
}) {
artists.push(artist.name.clone());
}
}
(!artists.is_empty()).then(|| Request::Release {
id,
title: detail.title.clone(),
artists,
})
}
_ => None,
},
None => None,
};
if !state
.card_enrichment
.begin(visible.filter(|_| request.is_some()), now)
{
return None;
}
match &request {
Some(Request::Artist { id, .. }) => mark_loading(&mut state.artist_fed_views, *id),
Some(Request::Release { id, .. }) => mark_loading(&mut state.release_fed_views, *id),
None => {}
}
request
}
fn mark_loading<T>(views: &mut HashMap<i64, Loadable<T>>, id: i64) {
if !matches!(views.get(&id), Some(Loadable::Ready(_))) {
views.insert(id, Loadable::Loading);
}
}
/// Keep the last usable snapshot if a refresh fails while peers are offline.
pub(super) fn store_result<T>(
views: &mut HashMap<i64, Loadable<T>>,
id: i64,
result: Result<T, String>,
) {
match result {
Ok(value) => {
views.insert(id, Loadable::Ready(value));
}
Err(message) if !matches!(views.get(&id), Some(Loadable::Ready(_))) => {
views.insert(id, Loadable::Failed(message));
}
Err(_) => {}
}
}
+42 -2
View File
@@ -10,7 +10,11 @@ use crate::library::models::{
/// the playback engine, imports). Tasks never touch AppState directly.
#[derive(Debug)]
pub enum AppEvent {
UpdateChecked(Result<Option<crate::updater::Update>, String>),
UpdateProgress(String),
UpdateInstalled(Result<(), String>),
StatusMessage(String),
StatusVisualizationReady(crate::visualizer::status::Ready),
/// A page of the artists list arrived (or failed).
ArtistsLoaded(Result<ArtistsPage, String>),
/// A full reload after a library change: replaces the loaded artist
@@ -27,6 +31,11 @@ pub enum AppEvent {
id: i64,
result: Result<ReleaseDetail, String>,
},
/// Federated tracks that complete an already-open local release view.
ReleaseFederationLoaded {
id: i64,
result: Result<Option<crate::federation::FedRelease>, String>,
},
/// Live search results; `seq` drops responses that are already stale.
SearchLoaded {
seq: u64,
@@ -36,9 +45,14 @@ pub enum AppEvent {
/// text search so stale pages cannot overwrite a newer request.
SimilaritySearchLoaded {
seq: u64,
result: Result<SearchResults, String>,
result: Result<Vec<crate::similarity::SimilarTrack>, String>,
query: Option<crate::similarity::QueryVector>,
},
/// Federated candidates and diagnostics for a track-seeded search.
FedSimilaritySearchLoaded {
seq: u64,
result: Result<crate::federation::FedSimilaritySearchResults, String>,
},
SimilarityStatus(crate::similarity::SimilarityStatus),
/// `None` is emitted after clearing every stored embedding.
SimilarityProfileActivated(Option<String>),
@@ -177,7 +191,11 @@ pub enum AppEvent {
/// Trusted device playback state, delivered by personal-device sync.
DevicePlayback(crate::devices::PlaybackSnapshot),
/// Playback command addressed to this device.
PlaybackCommand(crate::devices::PlaybackCommand),
PlaybackCommand {
command: crate::devices::PlaybackCommand,
authority: music_dht::playback::CommandStamp,
origin: String,
},
/// Current lifecycle/status of the federation Jam.
JamStatus(crate::jam::JamStatus),
/// Host playback snapshot received by a Jam participant.
@@ -188,4 +206,26 @@ pub enum AppEvent {
JamInvite(Result<String, String>),
/// Result of joining a Jam capability.
JamJoined(Result<String, String>),
/// Podcast subscriptions (re)loaded from the podcast store.
PodcastsLoaded(Result<Vec<crate::podcasts::Podcast>, String>),
/// The stored episode catalog of one podcast.
PodcastEpisodesLoaded {
podcast_id: i64,
result: Result<Vec<crate::podcasts::Episode>, String>,
},
/// A new subscription was stored (its id) or rejected.
PodcastSubscribed(Result<i64, String>),
/// One feed refresh finished.
PodcastRefreshed {
podcast_id: i64,
result: Result<crate::podcasts::RefreshSummary, String>,
/// Background sweep: report errors in the log, not the status bar.
quiet: bool,
},
PodcastUnsubscribed {
podcast_id: i64,
result: Result<(), String>,
},
/// Periodic timer: refresh subscriptions whose feed went stale.
PodcastAutoRefreshDue,
}
+830 -201
View File
File diff suppressed because it is too large. Load diff
+489
View File
@@ -0,0 +1,489 @@
//! Runtime side of the Podcasts tab: opening the podcast store, running feed
//! fetches and SQLite work off the UI thread, and folding their results back
//! into `AppState`. Only RSS metadata is persisted; episode audio is streamed
//! on demand by `start_stream`.
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Duration;
use super::event::AppEvent;
use super::state::{AppState, Loadable, OpenedPodcast};
use super::update::PODCAST_STALE_SECS;
use super::{Runtime, err_string, now_epoch_seconds};
/// How often the background sweep looks for stale feeds. Each feed is only
/// fetched when it was last checked more than `PODCAST_STALE_SECS` ago.
const AUTO_REFRESH_INTERVAL: Duration = Duration::from_secs(5 * 60);
/// Delay before the first sweep, so startup work is not slowed down.
const AUTO_REFRESH_FIRST_DELAY: Duration = Duration::from_secs(20);
/// Feeds fetched at the same time; protects slow links and podcast hosts.
const MAX_CONCURRENT_REFRESHES: usize = 4;
/// Store and HTTP client shared by podcast background tasks.
pub struct PodcastRuntime {
pub store: Arc<crate::podcasts::Store>,
pub fetcher: Arc<crate::podcasts::Fetcher>,
refresh_slots: Arc<tokio::sync::Semaphore>,
}
/// Open the podcast store beside the library data and the cover cache in
/// the platform cache directory. Failure disables only the Podcasts tab.
pub async fn open() -> Result<PodcastRuntime, String> {
let dirs = crate::config::project_dirs()
.ok_or_else(|| "no home directory for podcast data".to_string())?;
let db_path = dirs.data_dir().join("podcasts.sqlite3");
let covers_dir = dirs.cache_dir().join("podcasts").join("covers");
let store = tokio::task::spawn_blocking(move || crate::podcasts::Store::open(&db_path))
.await
.map_err(|err| err.to_string())?
.map_err(err_string)?;
let fetcher = crate::podcasts::Fetcher::new(covers_dir).map_err(err_string)?;
Ok(PodcastRuntime {
store: Arc::new(store),
fetcher: Arc::new(fetcher),
refresh_slots: Arc::new(tokio::sync::Semaphore::new(MAX_CONCURRENT_REFRESHES)),
})
}
/// Ticks `PodcastAutoRefreshDue`; the handler decides which feeds are stale.
pub fn spawn_auto_refresh_timer(tx: tokio::sync::mpsc::UnboundedSender<AppEvent>) {
tokio::spawn(async move {
let start = tokio::time::Instant::now() + AUTO_REFRESH_FIRST_DELAY;
let mut interval = tokio::time::interval_at(start, AUTO_REFRESH_INTERVAL);
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
interval.tick().await;
if tx.send(AppEvent::PodcastAutoRefreshDue).is_err() {
return;
}
}
});
}
fn unavailable(state: &mut AppState) {
state.status_message = Some("podcasts are unavailable (see the Podcasts tab)".into());
}
pub fn load_list(runtime: &Runtime) {
let Some(podcasts) = &runtime.podcasts else {
return;
};
let store = Arc::clone(&podcasts.store);
let tx = runtime.event_tx.clone();
tokio::task::spawn_blocking(move || {
let result = store.list_podcasts().map_err(err_string);
let _ = tx.send(AppEvent::PodcastsLoaded(result));
});
}
fn load_episodes(runtime: &Runtime, podcast_id: i64) {
let Some(podcasts) = &runtime.podcasts else {
return;
};
let store = Arc::clone(&podcasts.store);
let tx = runtime.event_tx.clone();
tokio::task::spawn_blocking(move || {
let result = store.episodes(podcast_id).map_err(err_string);
let _ = tx.send(AppEvent::PodcastEpisodesLoaded { podcast_id, result });
});
}
/// Effect::PodcastLoadEpisodes: read the stored catalog, and fetch the feed
/// in the background when it has not been checked recently.
pub fn open_episodes(state: &mut AppState, runtime: &Runtime, podcast_id: i64) {
if runtime.podcasts.is_none() {
state.podcasts.episodes = Some((
podcast_id,
Loadable::Failed("podcast storage is unavailable".into()),
));
return;
}
load_episodes(runtime, podcast_id);
let stale = state
.podcasts
.podcast(podcast_id)
.is_some_and(|podcast| is_stale(podcast.last_checked_at, now_epoch_seconds()));
if stale && !state.podcasts.refreshing.contains(&podcast_id) {
state.podcasts.refreshing.insert(podcast_id);
spawn_refresh(runtime, vec![podcast_id], true);
}
}
fn is_stale(last_checked_at: Option<i64>, now: i64) -> bool {
last_checked_at.is_none_or(|checked| now.saturating_sub(checked) >= PODCAST_STALE_SECS)
}
pub fn subscribe(state: &mut AppState, runtime: &Runtime, url: String) {
let Some(podcasts) = &runtime.podcasts else {
return unavailable(state);
};
if state.podcasts.subscribing {
state.status_message = Some("podcasts: a subscription is already being added".into());
return;
}
state.podcasts.subscribing = true;
state.status_message = Some(format!("podcasts: reading feed {url}…"));
let store = Arc::clone(&podcasts.store);
let fetcher = Arc::clone(&podcasts.fetcher);
let tx = runtime.event_tx.clone();
tokio::spawn(async move {
let result = crate::podcasts::subscribe(store, &fetcher, &url, now_epoch_seconds())
.await
.map_err(err_string);
let _ = tx.send(AppEvent::PodcastSubscribed(result));
});
}
/// Effect::PodcastRefresh / the periodic sweep. Callers have already put the
/// ids into `state.podcasts.refreshing`.
pub fn spawn_refresh(runtime: &Runtime, ids: Vec<i64>, quiet: bool) {
let Some(podcasts) = &runtime.podcasts else {
return;
};
for podcast_id in ids {
let store = Arc::clone(&podcasts.store);
let fetcher = Arc::clone(&podcasts.fetcher);
let slots = Arc::clone(&podcasts.refresh_slots);
let tx = runtime.event_tx.clone();
tokio::spawn(async move {
let Ok(_permit) = slots.acquire_owned().await else {
return;
};
let result = crate::podcasts::refresh(store, &fetcher, podcast_id, now_epoch_seconds())
.await
.map_err(err_string);
let _ = tx.send(AppEvent::PodcastRefreshed {
podcast_id,
result,
quiet,
});
});
}
}
pub fn refresh(state: &mut AppState, runtime: &Runtime, ids: Vec<i64>) {
if runtime.podcasts.is_none() {
for id in &ids {
state.podcasts.refreshing.remove(id);
}
return unavailable(state);
}
spawn_refresh(runtime, ids, false);
}
pub fn unsubscribe(state: &mut AppState, runtime: &Runtime, podcast_id: i64) {
let Some(podcasts) = &runtime.podcasts else {
return unavailable(state);
};
let store = Arc::clone(&podcasts.store);
let tx = runtime.event_tx.clone();
tokio::task::spawn_blocking(move || {
let result = store
.remove_podcast(podcast_id)
.map(|cover_path| {
// Covers are replaceable cache; a leftover file is harmless.
if let Some(path) = cover_path
&& let Err(err) = std::fs::remove_file(&path)
&& err.kind() != std::io::ErrorKind::NotFound
{
tracing::debug!(%err, path, "podcast cover cleanup failed");
}
})
.map_err(err_string);
let _ = tx.send(AppEvent::PodcastUnsubscribed { podcast_id, result });
});
}
/// The periodic sweep: refresh every subscription not checked recently.
pub fn auto_refresh(state: &mut AppState, runtime: &Runtime) {
if runtime.podcasts.is_none() {
return;
}
let now = now_epoch_seconds();
state.podcasts.last_auto_refresh = Some(now);
let ids: Vec<i64> = state
.podcasts
.podcasts()
.iter()
.filter(|podcast| is_stale(podcast.last_checked_at, now))
.map(|podcast| podcast.id)
.filter(|id| !state.podcasts.refreshing.contains(id))
.collect();
if ids.is_empty() {
return;
}
tracing::debug!(count = ids.len(), "podcasts: refreshing stale feeds");
state.podcasts.refreshing.extend(ids.iter().copied());
spawn_refresh(runtime, ids, true);
}
/// Fold a podcast `AppEvent` into the state; any other event is handed back.
pub fn handle_event(state: &mut AppState, runtime: &Runtime, event: AppEvent) -> Option<AppEvent> {
match event {
AppEvent::PodcastsLoaded(result) => {
let selected_id = selected_podcast_id(state);
match result {
Ok(list) => state.podcasts.list = Some(Loadable::Ready(list)),
Err(err) => {
tracing::warn!(%err, "podcast list load failed");
if matches!(state.podcasts.list, Some(Loadable::Ready(_))) {
// A failed reload keeps the visible subscriptions.
state.status_message = Some(format!("podcasts: {err}"));
} else {
state.podcasts.list = Some(Loadable::Failed(err));
}
}
}
restore_list_cursor(state, selected_id);
// A podcast that disappeared (unsubscribed elsewhere) closes.
if let Some(opened) = state.podcasts.opened
&& matches!(state.podcasts.list, Some(Loadable::Ready(_)))
&& state.podcasts.podcast(opened.id).is_none()
{
state.podcasts.opened = None;
state.podcasts.episodes = None;
state.podcasts.filter.clear();
}
}
AppEvent::PodcastEpisodesLoaded { podcast_id, result } => {
// Only the catalog slot this load was issued for may take it: a
// slow read for a podcast the user already left must not replace
// the one now open (each open re-keys the slot first).
if !episodes_result_is_current(state, podcast_id) {
return None;
}
// Keep the cursor on the same episode when new ones arrive above.
let selected_episode = state
.podcasts
.selected_episode()
.filter(|episode| episode.podcast_id == podcast_id)
.map(|episode| episode.id);
match result {
Ok(episodes) => {
state.podcasts.episodes = Some((podcast_id, Loadable::Ready(episodes)));
}
Err(err) => {
tracing::warn!(%err, podcast_id, "podcast episodes load failed");
// A failed reload keeps an already visible catalog.
let keep = matches!(
&state.podcasts.episodes,
Some((id, Loadable::Ready(_))) if *id == podcast_id
);
if keep {
state.status_message = Some(format!("podcasts: {err}"));
} else {
state.podcasts.episodes = Some((podcast_id, Loadable::Failed(err)));
}
}
}
if let (Some(episode_id), Some(opened)) = (selected_episode, state.podcasts.opened)
&& opened.id == podcast_id
&& let Some(episodes) = state.podcasts.opened_episodes()
{
let visible = state.podcasts.visible_episode_indices();
if let Some(cursor) = visible
.iter()
.position(|&index| episodes[index].id == episode_id)
{
state.podcasts.opened = Some(OpenedPodcast {
id: podcast_id,
cursor,
});
}
}
}
AppEvent::PodcastSubscribed(result) => {
state.podcasts.subscribing = false;
match result {
Ok(podcast_id) => {
state.status_message = Some("podcasts: subscribed".into());
// Select the new row once the reloaded list arrives.
state.podcasts.opened = None;
state.podcasts.filter.clear();
state.podcasts.pending_select = Some(podcast_id);
load_list(runtime);
}
Err(err) => {
state.status_message = Some(format!("podcasts: cannot subscribe: {err}"));
}
}
}
AppEvent::PodcastRefreshed {
podcast_id,
result,
quiet,
} => {
state.podcasts.refreshing.remove(&podcast_id);
let title = state
.podcasts
.podcast(podcast_id)
.map(|podcast| podcast.title.clone())
.unwrap_or_else(|| "podcast".to_string());
match result {
Ok(summary) => {
if summary.new_episodes > 0 {
state.status_message = Some(format!(
"podcasts: {} new episode(s) in {title}",
summary.new_episodes
));
} else if !quiet && state.podcasts.refreshing.is_empty() {
state.status_message = Some("podcasts: no new episodes".into());
}
if state
.podcasts
.opened
.is_some_and(|opened| opened.id == podcast_id)
&& (summary.new_episodes > 0 || summary.updated_episodes > 0)
{
load_episodes(runtime, podcast_id);
}
}
Err(err) => {
tracing::warn!(%err, podcast_id, "podcast refresh failed");
if !quiet {
state.status_message =
Some(format!("podcasts: {title}: refresh failed: {err}"));
}
}
}
// Counts, check time and errors live on the list rows.
load_list(runtime);
}
AppEvent::PodcastUnsubscribed { podcast_id, result } => match result {
Ok(()) => {
if state
.podcasts
.opened
.is_some_and(|opened| opened.id == podcast_id)
{
state.podcasts.opened = None;
state.podcasts.filter.clear();
}
if matches!(&state.podcasts.episodes, Some((id, _)) if *id == podcast_id) {
state.podcasts.episodes = None;
}
state.status_message = Some("podcasts: unsubscribed".into());
load_list(runtime);
}
Err(err) => {
state.status_message = Some(format!("podcasts: unsubscribe failed: {err}"));
}
},
AppEvent::PodcastAutoRefreshDue => auto_refresh(state, runtime),
other => return Some(other),
}
None
}
fn episodes_result_is_current(state: &AppState, podcast_id: i64) -> bool {
matches!(&state.podcasts.episodes, Some((id, _)) if *id == podcast_id)
}
fn selected_podcast_id(state: &AppState) -> Option<i64> {
if state.podcasts.opened.is_some() {
return None;
}
state.podcasts.current_podcast().map(|podcast| podcast.id)
}
/// After a list reload, keep the cursor on the same podcast (or on a newly
/// subscribed one) even when rows above it were added or removed.
fn restore_list_cursor(state: &mut AppState, previous: Option<i64>) {
let target = state.podcasts.pending_select.take().or(previous);
let rows = state.podcasts.list_rows().len();
if let Some(id) = target
&& let Some(index) = state
.podcasts
.podcasts()
.iter()
.position(|podcast| podcast.id == id)
{
state.podcasts.selected = index + super::state::PODCAST_ACTION_ROWS;
}
state.podcasts.selected = state.podcasts.selected.min(rows.saturating_sub(1));
}
/// Start playing a remote episode URL. Opening the HTTP stream may take a
/// few seconds, so it runs on the blocking pool; a newer playback request
/// started meanwhile wins (`playback_seq`).
pub fn start_stream(
runtime: &Runtime,
track: crate::library::models::TrackItem,
volume: u8,
position_secs: f64,
paused: bool,
) {
let seq = runtime.playback_seq.load(Ordering::SeqCst);
let current_seq = Arc::clone(&runtime.playback_seq);
let controller = runtime.player.clone();
let tx = runtime.event_tx.clone();
let volume = crate::player::amplitude(volume);
// Silence the previous track while the stream connects. This must not
// bump `playback_seq`: the request captured above is the current one.
runtime.player.stop();
tokio::task::spawn_blocking(move || {
// Connect and probe the container here, not on the audio thread:
// both wait on the network, and a stalled host must not block
// pause/stop of whatever plays next.
let decoded = crate::podcasts::stream::open(&track.file_path).and_then(|opened| {
let coarse_seek = opened.seekable && opened.byte_len.is_some();
crate::player::decode_source_with(
Box::new(opened.reader),
opened.byte_len,
opened.mime_type.as_deref(),
opened.seekable,
coarse_seek,
)
.map_err(|err| anyhow::anyhow!("cannot decode the episode audio: {err}"))
});
// A newer start or stop happened meanwhile: neither play nor report.
if current_seq.load(Ordering::SeqCst) != seq {
tracing::debug!(title = %track.title, "podcast stream superseded before start");
return;
}
match decoded {
Ok(source) => {
controller.play_source(source, volume);
if position_secs > 0.0 {
controller.seek(Duration::from_secs_f64(position_secs));
}
if paused {
controller.pause();
}
}
Err(err) => {
tracing::warn!(title = %track.title, url = %track.file_path, err = %format!("{err:#}"), "podcast stream failed");
let _ = tx.send(AppEvent::Player(crate::player::PlayerEvent::Failed(
format!("cannot stream \"{}\": {err:#}", track.title),
)));
}
}
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn late_episode_loads_for_another_podcast_are_ignored() {
let mut state = AppState::default();
state.podcasts.opened = Some(OpenedPodcast { id: 7, cursor: 0 });
state.podcasts.episodes = Some((7, Loadable::Loading));
assert!(episodes_result_is_current(&state, 7));
assert!(!episodes_result_is_current(&state, 3));
state.podcasts.episodes = None;
assert!(!episodes_result_is_current(&state, 7));
}
#[test]
fn staleness_uses_last_check_time() {
let now = 1_000_000;
assert!(is_stale(None, now));
assert!(is_stale(Some(now - PODCAST_STALE_SECS), now));
assert!(!is_stale(Some(now - PODCAST_STALE_SECS + 1), now));
// A clock that moved backwards does not trigger a refresh storm.
assert!(!is_stale(Some(now + 60), now));
}
}
+74 -3
View File
@@ -557,7 +557,11 @@ fn handle_fed_input(
key: KeyEvent,
) {
match key.code {
KeyCode::Esc => {}
KeyCode::Esc => {
if field == FedInputField::PodcastFilter {
crate::app::update::podcasts_set_filter(state, String::new());
}
}
KeyCode::Enter => {
let value = input.trim().to_string();
match field {
@@ -592,6 +596,36 @@ fn handle_fed_input(
state.popup = Some(Popup::FedInput { field, input });
}
},
FedInputField::SimilarityMinimumScore => match value.parse::<f32>() {
Ok(score) if score.is_finite() && (0.0..=1.0).contains(&score) => {
state.similarity.settings.minimum_score = score;
super::perform_effect(
state,
runtime,
crate::app::update::Effect::SimilarityApplySettings,
);
}
_ => {
state.status_message =
Some("minimum similarity must be a number from 0.00 to 1.00".into());
state.popup = Some(Popup::FedInput { field, input });
}
},
FedInputField::SimilarityMaxTracksPerArtist => match value.parse::<usize>() {
Ok(limit @ 1..=50) => {
state.similarity.settings.max_tracks_per_artist = limit;
super::perform_effect(
state,
runtime,
crate::app::update::Effect::SimilarityApplySettings,
);
}
_ => {
state.status_message =
Some("tracks per artist must be a number from 1 to 50".into());
state.popup = Some(Popup::FedInput { field, input });
}
},
FedInputField::ConnectTicket => {
if value.is_empty() {
state.status_message = Some("ticket is empty".into());
@@ -632,10 +666,28 @@ fn handle_fed_input(
);
}
}
FedInputField::PodcastFeedUrl => {
if value.is_empty() {
state.status_message = Some("podcast feed URL is empty".into());
} else {
super::perform_effect(
state,
runtime,
crate::app::update::Effect::PodcastSubscribe(value),
);
}
}
// The filter is already applied live; Enter just keeps it.
FedInputField::PodcastFilter => {
crate::app::update::podcasts_set_filter(state, value);
}
}
}
_ => {
input.handle_key(key);
if field == FedInputField::PodcastFilter {
crate::app::update::podcasts_set_filter(state, input.to_string());
}
state.popup = Some(Popup::FedInput { field, input });
}
}
@@ -757,9 +809,15 @@ fn handle_device_leave(state: &mut AppState, runtime: &mut Runtime, key: KeyEven
/// Pasted text goes into the focused text field when one is open.
pub fn handle_paste(state: &mut AppState, pasted: &str) {
let cleaned: String = pasted.chars().filter(|c| !c.is_control()).collect();
let mut filter = None;
match &mut state.popup {
Some(Popup::NewPlaylist { input, busy, .. }) if !*busy => input.insert_str(&cleaned),
Some(Popup::FedInput { input, .. }) => input.insert_str(&cleaned),
Some(Popup::FedInput { field, input }) => {
input.insert_str(&cleaned);
if *field == FedInputField::PodcastFilter {
filter = Some(input.to_string());
}
}
Some(Popup::Edit { fields, focus, .. }) => {
if let Some(field) = fields.get_mut(*focus) {
field.value.insert_str(&cleaned);
@@ -767,6 +825,9 @@ pub fn handle_paste(state: &mut AppState, pasted: &str) {
}
_ => {}
}
if let Some(filter) = filter {
crate::app::update::podcasts_set_filter(state, filter);
}
}
// ---------------------------------------------------------------------------
@@ -939,13 +1000,22 @@ fn save_edit(
fn handle_confirm_delete(
state: &mut AppState,
runtime: &Runtime,
runtime: &mut Runtime,
target: DeleteTarget,
label: String,
key: KeyEvent,
) {
match key.code {
KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('q') => {}
KeyCode::Enter | KeyCode::Char('y') if matches!(target, DeleteTarget::Podcast(_)) => {
if let DeleteTarget::Podcast(id) = target {
super::perform_effect(
state,
runtime,
crate::app::update::Effect::PodcastUnsubscribe(id),
);
}
}
KeyCode::Enter | KeyCode::Char('y') => {
let library = Arc::clone(&runtime.library);
let result = match target {
@@ -959,6 +1029,7 @@ fn handle_confirm_delete(
library.delete_playlist(id)
}
},
DeleteTarget::Podcast(_) => unreachable!("handled by the guarded arm above"),
};
match result {
Ok(()) => {
+523 -24
View File
@@ -303,6 +303,79 @@ mod tests {
assert_eq!(fed_release_display_order(&releases), vec![2, 0, 3, 1, 4]);
}
#[test]
fn release_tracks_adds_remote_rows_without_duplicating_local_content() {
let local_content =
"b3:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string();
let detail = ReleaseDetail {
id: 7,
title: "Album".to_string(),
release_type: "album".to_string(),
year: Some(2024),
cover_path: None,
artists: vec![crate::library::models::ArtistRef {
id: 3,
name: "Artist".to_string(),
}],
tracks: vec![TrackItem {
id: 1,
title: "Local".to_string(),
track_number: Some(1),
disc_number: None,
duration_seconds: 120.0,
artists: Vec::new(),
featured_artists: Vec::new(),
release_id: 7,
release_title: "Album".to_string(),
release_year: Some(2024),
file_path: "/music/local.mp3".to_string(),
content_id: Some(local_content.clone()),
cover_path: None,
audio_format: Some("mp3".to_string()),
audio_bitrate: Some(320),
audio_sample_rate: None,
audio_bit_depth: None,
file_size_bytes: None,
play_count: 0,
fed: None,
}],
};
let mut state = AppState::default();
state.release_fed_views.insert(
detail.id,
Loadable::Ready(crate::federation::FedRelease {
title: "Album".to_string(),
tracks: vec![
crate::federation::FedCardTrack {
title: "Local".to_string(),
track_number: Some(1),
content_id: Some(local_content),
sources: vec![("peer".to_string(), "local-item".to_string())],
..Default::default()
},
crate::federation::FedCardTrack {
title: "Remote".to_string(),
track_number: Some(3),
sources: vec![("peer".to_string(), "remote-item".to_string())],
..Default::default()
},
],
..Default::default()
}),
);
let tracks = release_tracks(&state, detail.id, &detail);
assert_eq!(
tracks
.iter()
.map(|track| track.title.as_str())
.collect::<Vec<_>>(),
vec!["Local", "Remote"]
);
assert!(tracks[1].is_fed_pending());
}
#[test]
fn artist_merged_releases_marks_partially_local_federated_release() {
let local_id = "b3:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
@@ -509,6 +582,8 @@ pub enum TrackSelectionScope {
Release(i64),
Playlist(i64),
Queue,
/// The unified local + federated similar-track result list.
SimilaritySearch,
/// The federated section of the search results (its tracks).
FedSearch,
/// The tracklist of the open federated release view.
@@ -620,6 +695,8 @@ pub enum DeleteTarget {
Release(i64),
Artist(i64),
Playlist(i64),
/// Unsubscribe: drops the feed's stored metadata and cached cover.
Podcast(i64),
}
/// One text field of an edit form.
@@ -817,11 +894,16 @@ impl StatusDetailFocus {
pub enum FedInputField {
MusicDirectory,
SimilarityWorkers,
SimilarityMinimumScore,
SimilarityMaxTracksPerArtist,
NetworkId,
ConnectTicket,
DeviceName,
ConnectInvite,
JamInvite,
PodcastFeedUrl,
/// Live episode-title filter of the opened podcast.
PodcastFilter,
}
impl FedInputField {
@@ -829,11 +911,15 @@ impl FedInputField {
match self {
FedInputField::MusicDirectory => "Music save directory",
FedInputField::SimilarityWorkers => "Similarity background workers",
FedInputField::SimilarityMinimumScore => "Minimum similarity score",
FedInputField::SimilarityMaxTracksPerArtist => "Tracks per artist",
FedInputField::NetworkId => "Network ID",
FedInputField::ConnectTicket => "Connect to peer (paste ticket)",
FedInputField::DeviceName => "Device name",
FedInputField::ConnectInvite => "Connect device (paste frid://i invite)",
FedInputField::JamInvite => "Join Jam (paste frid://j invite)",
FedInputField::PodcastFeedUrl => "Add podcast (RSS feed URL)",
FedInputField::PodcastFilter => "Search episodes by title",
}
}
@@ -845,6 +931,12 @@ impl FedInputField {
FedInputField::SimilarityWorkers => {
"Enter the maximum number of tracks processed in parallel, from 1 to 16. The change takes effect immediately."
}
FedInputField::SimilarityMinimumScore => {
"Enter the minimum cosine similarity from 0.00 to 1.00. Lower values show broader matches; higher values hide weak matches. Embeddings are not recalculated."
}
FedInputField::SimilarityMaxTracksPerArtist => {
"Enter how many tracks by one primary artist may appear in similarity results, from 1 to 50. Embeddings are not recalculated."
}
FedInputField::NetworkId => {
"A unique network id. It must match exactly on every client that should see and connect to the same peers."
}
@@ -860,6 +952,12 @@ impl FedInputField {
FedInputField::JamInvite => {
"Paste a frid://j capability to control the host player for this Jam only."
}
FedInputField::PodcastFeedUrl => {
"Paste the podcast's RSS feed URL. Only the episode list is stored; audio streams when you play an episode."
}
FedInputField::PodcastFilter => {
"Type words from the episode title; the list updates as you type. Enter keeps the filter, Esc clears it."
}
}
}
}
@@ -880,15 +978,19 @@ pub enum SimilarityRow {
Toggle,
Model,
Profile,
MinimumScore,
MaxTracksPerArtist,
Workers,
Clear,
}
impl SimilarityRow {
pub const ALL: [SimilarityRow; 5] = [
pub const ALL: [SimilarityRow; 7] = [
SimilarityRow::Toggle,
SimilarityRow::Model,
SimilarityRow::Profile,
SimilarityRow::MinimumScore,
SimilarityRow::MaxTracksPerArtist,
SimilarityRow::Workers,
SimilarityRow::Clear,
];
@@ -910,6 +1012,9 @@ impl FedRow {
/// the config directory.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SettingsRow {
AdditionalSettings,
CheckUpdate,
InstallUpdate,
MusicDirectory,
Similarity(SimilarityRow),
Federation(FedRow),
@@ -1048,20 +1153,13 @@ pub fn device_status_order(state: &AppState) -> Vec<usize> {
indices
}
pub fn settings_rows(state: &AppState) -> Vec<SettingsRow> {
let mut rows = vec![SettingsRow::MusicDirectory];
rows.extend(SimilarityRow::ALL.into_iter().map(SettingsRow::Similarity));
rows.extend(FedRow::ALL.into_iter().map(SettingsRow::Federation));
rows.push(SettingsRow::DeviceName);
rows.push(SettingsRow::DeviceInvite);
rows.push(SettingsRow::DeviceConnect);
rows.push(SettingsRow::DeviceSyncNow);
rows.push(SettingsRow::DeviceLeaveGroup);
rows.extend(
device_status_order(state)
.into_iter()
.map(SettingsRow::Device),
);
/// Secondary settings share actions but have independent navigation.
pub fn additional_settings_rows(state: &AppState) -> Vec<SettingsRow> {
let mut rows = vec![
SettingsRow::MusicDirectory,
SettingsRow::CheckUpdate,
SettingsRow::InstallUpdate,
];
rows.push(SettingsRow::VisualizationClock);
rows.extend(
state
@@ -1075,7 +1173,23 @@ pub fn settings_rows(state: &AppState) -> Vec<SettingsRow> {
if !state.visualizer.scripts.is_empty() {
rows.push(SettingsRow::VisualizationEdit);
}
rows.push(SettingsRow::StatusDetails);
rows
}
pub fn settings_rows(state: &AppState) -> Vec<SettingsRow> {
let mut rows = vec![SettingsRow::AdditionalSettings, SettingsRow::StatusDetails];
rows.extend(FedRow::ALL.into_iter().map(SettingsRow::Federation));
rows.push(SettingsRow::DeviceName);
rows.push(SettingsRow::DeviceInvite);
rows.push(SettingsRow::DeviceConnect);
rows.push(SettingsRow::DeviceSyncNow);
rows.push(SettingsRow::DeviceLeaveGroup);
rows.extend(
device_status_order(state)
.into_iter()
.map(SettingsRow::Device),
);
rows.extend(SimilarityRow::ALL.into_iter().map(SettingsRow::Similarity));
rows
}
@@ -1211,6 +1325,85 @@ mod cmdline_history_tests {
}
/// Live search state driven by the `:/query` command.
#[derive(Debug, Clone)]
pub enum SimilaritySearchHit {
Local {
track: TrackItem,
score: f32,
embedding_signature: [u8; music_dht::similarity::SIMILARITY_SIGNATURE_BYTES],
},
Federated {
track: crate::federation::FedTrack,
score: f32,
embedding_signature: Option<[u8; music_dht::similarity::SIMILARITY_SIGNATURE_BYTES]>,
},
}
impl SimilaritySearchHit {
pub fn score(&self) -> f32 {
match self {
Self::Local { score, .. } | Self::Federated { score, .. } => *score,
}
}
pub fn embedding_signature(
&self,
) -> Option<[u8; music_dht::similarity::SIMILARITY_SIGNATURE_BYTES]> {
match self {
Self::Local {
embedding_signature,
..
} => Some(*embedding_signature),
Self::Federated {
embedding_signature,
..
} => *embedding_signature,
}
}
pub fn track_item(&self) -> TrackItem {
match self {
Self::Local { track, .. } => track.clone(),
Self::Federated { track, .. } => crate::federation::pending_track(track),
}
}
pub fn federated_track(&self) -> Option<&crate::federation::FedTrack> {
match self {
Self::Federated { track, .. } => Some(track),
Self::Local { .. } => None,
}
}
pub fn primary_artist_key(&self) -> String {
match self {
Self::Local { track, .. } => track
.artists
.first()
.map(|artist| music_dht::normalize_name(&artist.name))
.unwrap_or_default(),
Self::Federated { track, .. } => track
.artist_names
.first()
.map(|artist| music_dht::normalize_name(artist))
.unwrap_or_default(),
}
}
pub fn content_key(&self) -> String {
match self {
Self::Local { track, .. } => track
.content_id
.clone()
.unwrap_or_else(|| format!("local:{}", track.id)),
Self::Federated { track, .. } => track
.content_id
.clone()
.unwrap_or_else(|| format!("remote:{}:{}", track.owner, track.item_id)),
}
}
}
#[derive(Debug, Default)]
pub struct SearchState {
pub query: String,
@@ -1226,6 +1419,59 @@ pub struct SearchState {
/// Present only for a track-seeded search; text-search refreshes must not
/// replace this page with a title query.
pub similarity_source: Option<i64>,
/// The source is pinned at row zero; candidates below it are globally
/// ranked across the local library and federation.
pub similarity_source_track: Option<TrackItem>,
pub similarity_tracks: Vec<SimilaritySearchHit>,
pub similarity_stats: Option<crate::federation::SimilaritySearchStats>,
pub similarity_error: Option<String>,
}
impl SearchState {
pub fn similarity_len(&self) -> usize {
usize::from(self.similarity_source_track.is_some()) + self.similarity_tracks.len()
}
pub fn similarity_track(&self, index: usize) -> Option<TrackItem> {
if index == 0 {
return self.similarity_source_track.clone();
}
self.similarity_tracks
.get(index.checked_sub(1)?)
.map(SimilaritySearchHit::track_item)
}
pub fn similarity_fed_track(&self, index: usize) -> Option<&crate::federation::FedTrack> {
self.similarity_tracks
.get(index.checked_sub(1)?)?
.federated_track()
}
pub fn similarity_key(&self, index: usize) -> Option<String> {
if index == 0 {
return self
.similarity_source_track
.as_ref()
.map(|track| format!("source:{}", track.id));
}
self.similarity_tracks
.get(index.checked_sub(1)?)
.map(SimilaritySearchHit::content_key)
}
pub fn similarity_index_for_key(&self, key: &str) -> Option<usize> {
if self
.similarity_source_track
.as_ref()
.is_some_and(|track| key == format!("source:{}", track.id))
{
return Some(0);
}
self.similarity_tracks
.iter()
.position(|hit| hit.content_key() == key)
.map(|index| index + 1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
@@ -1236,15 +1482,19 @@ pub enum Tab {
Queue,
Federation,
Logs,
Podcasts,
}
impl Tab {
pub const ALL: [Tab; 5] = [
/// Display and `GoToTab` index order. Podcasts is appended so the
/// existing number keys (and user keymaps) keep their meaning.
pub const ALL: [Tab; 6] = [
Tab::Global,
Tab::Playlists,
Tab::Queue,
Tab::Federation,
Tab::Logs,
Tab::Podcasts,
];
pub fn title(self) -> &'static str {
@@ -1254,6 +1504,7 @@ impl Tab {
Tab::Queue => "Queue",
Tab::Federation => "Settings",
Tab::Logs => "Logs",
Tab::Podcasts => "Podcasts",
}
}
@@ -1280,6 +1531,7 @@ impl Tab {
Tab::Queue => KeyContext::Queue,
Tab::Federation => KeyContext::Federation,
Tab::Logs => KeyContext::Logs,
Tab::Podcasts => KeyContext::Podcasts,
}
}
}
@@ -1381,6 +1633,7 @@ impl DevicePlaybackRole {
#[derive(Debug, Default)]
pub struct DevicePlaybackState {
pub config: music_dht::playback::Config,
pub role: DevicePlaybackRole,
pub self_device_id: String,
pub self_device_name: String,
@@ -1391,9 +1644,6 @@ pub struct DevicePlaybackState {
pub remote: BTreeMap<String, crate::devices::PlaybackSnapshot>,
pub last_remote_snapshot: Option<crate::devices::PlaybackSnapshot>,
pub jam_host: bool,
/// A freshly started TUI owns playback by protocol. The first active
/// snapshot discovered during startup is imported and handed off here.
pub startup_takeover_pending: bool,
}
impl DevicePlaybackState {
@@ -1402,10 +1652,6 @@ impl DevicePlaybackState {
|| (self.role == DevicePlaybackRole::Jam && !self.jam_host)
}
pub fn is_personal_control(&self) -> bool {
self.role == DevicePlaybackRole::Control
}
pub fn is_audio_owner(&self) -> bool {
self.role == DevicePlaybackRole::Active
|| (self.role == DevicePlaybackRole::Jam && self.jam_host)
@@ -1419,10 +1665,174 @@ impl DevicePlaybackState {
}
}
/// Podcasts tab: subscriptions list, or the episode catalog of one opened
/// podcast. Only RSS metadata lives here; audio streams on demand.
#[derive(Debug, Default)]
pub struct PodcastsTab {
/// Subscriptions; `None` until the first load is requested.
pub list: Option<Loadable<Vec<crate::podcasts::Podcast>>>,
/// Cursor over `podcast_list_rows` (action rows first, then podcasts).
pub selected: usize,
pub opened: Option<OpenedPodcast>,
/// Episodes of the opened podcast, keyed by its id so a late result for
/// a podcast that is no longer open is ignored.
pub episodes: Option<(i64, Loadable<Vec<crate::podcasts::Episode>>)>,
/// Case-insensitive episode title filter; every word must match.
pub filter: String,
pub sort: EpisodeSort,
/// Feeds with a refresh in flight (spinner + dedup of requests).
pub refreshing: HashSet<i64>,
pub subscribing: bool,
/// Wall-clock seconds of the last automatic refresh sweep.
pub last_auto_refresh: Option<i64>,
/// Newly subscribed podcast to select once the reloaded list arrives.
pub pending_select: Option<i64>,
}
/// "Add podcast" and "Check for new episodes" rows above the podcasts.
pub const PODCAST_ACTION_ROWS: usize = 2;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OpenedPodcast {
pub id: i64,
/// Index into `visible_episode_indices`.
pub cursor: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EpisodeSort {
#[default]
NewestFirst,
OldestFirst,
}
impl EpisodeSort {
pub fn toggle(self) -> Self {
match self {
EpisodeSort::NewestFirst => EpisodeSort::OldestFirst,
EpisodeSort::OldestFirst => EpisodeSort::NewestFirst,
}
}
pub fn label(self) -> &'static str {
match self {
EpisodeSort::NewestFirst => "newest first",
EpisodeSort::OldestFirst => "oldest first",
}
}
}
/// One row of the podcast subscriptions list.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PodcastListRow {
Add,
RefreshAll,
/// Index into the loaded podcast list.
Podcast(usize),
}
impl PodcastsTab {
pub fn podcasts(&self) -> &[crate::podcasts::Podcast] {
match &self.list {
Some(Loadable::Ready(list)) => list,
_ => &[],
}
}
pub fn podcast(&self, id: i64) -> Option<&crate::podcasts::Podcast> {
self.podcasts().iter().find(|podcast| podcast.id == id)
}
pub fn list_rows(&self) -> Vec<PodcastListRow> {
let mut rows = vec![PodcastListRow::Add, PodcastListRow::RefreshAll];
rows.extend((0..self.podcasts().len()).map(PodcastListRow::Podcast));
rows
}
/// The podcast under the list cursor (list view) or the opened one.
pub fn current_podcast(&self) -> Option<&crate::podcasts::Podcast> {
if let Some(opened) = self.opened {
return self.podcast(opened.id);
}
match self.list_rows().get(self.selected)? {
PodcastListRow::Podcast(index) => self.podcasts().get(*index),
_ => None,
}
}
/// Episodes of the opened podcast, when loaded.
pub fn opened_episodes(&self) -> Option<&[crate::podcasts::Episode]> {
let opened = self.opened?;
match &self.episodes {
Some((id, Loadable::Ready(episodes))) if *id == opened.id => Some(episodes),
_ => None,
}
}
/// Indices into `opened_episodes()` after the title filter and sort.
pub fn visible_episode_indices(&self) -> Vec<usize> {
let Some(episodes) = self.opened_episodes() else {
return Vec::new();
};
visible_episode_indices(episodes, &self.filter, self.sort)
}
pub fn selected_episode(&self) -> Option<&crate::podcasts::Episode> {
let opened = self.opened?;
let episodes = self.opened_episodes()?;
let visible = visible_episode_indices(episodes, &self.filter, self.sort);
visible
.get(opened.cursor.min(visible.len().saturating_sub(1)))
.and_then(|&index| episodes.get(index))
}
}
/// Filter by title (every whitespace-separated term, case-insensitively)
/// and order by publication date; undated episodes always go last.
pub fn visible_episode_indices(
episodes: &[crate::podcasts::Episode],
filter: &str,
sort: EpisodeSort,
) -> Vec<usize> {
let terms: Vec<String> = filter.split_whitespace().map(str::to_lowercase).collect();
let mut indices: Vec<usize> = episodes
.iter()
.enumerate()
.filter(|(_, episode)| {
if terms.is_empty() {
return true;
}
let title = episode.title.to_lowercase();
terms.iter().all(|term| title.contains(term.as_str()))
})
.map(|(index, _)| index)
.collect();
indices.sort_by(|&a, &b| {
let (a_ep, b_ep) = (&episodes[a], &episodes[b]);
match (a_ep.published_at, b_ep.published_at) {
(Some(x), Some(y)) => match sort {
EpisodeSort::NewestFirst => y.cmp(&x),
EpisodeSort::OldestFirst => x.cmp(&y),
},
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => std::cmp::Ordering::Equal,
}
// Stable sort keeps the store order (newest id first) for ties;
// reverse it for oldest-first so ties follow the same direction.
.then_with(|| match sort {
EpisodeSort::NewestFirst => a.cmp(&b),
EpisodeSort::OldestFirst => b.cmp(&a),
})
});
indices
}
/// Single source of truth for the UI. Mutated only by `update()` and the
/// event handlers in the main loop; views render from `&AppState`.
#[derive(Debug, Default)]
pub struct AppState {
pub updater: crate::updater::State,
pub active_tab: Tab,
pub should_quit: bool,
pub shutting_down: bool,
@@ -1434,6 +1844,9 @@ pub struct AppState {
pub status_message: Option<String>,
pub spinner_frame: usize,
pub settings_cursor: usize,
/// Kept open underneath child dialogs and asynchronous confirmations.
pub additional_settings_open: bool,
pub additional_settings_cursor: usize,
/// Root for music permanently downloaded from federation peers.
pub music_dir: std::path::PathBuf,
pub music_dir_changing: bool,
@@ -1441,11 +1854,15 @@ pub struct AppState {
pub device_playback: DevicePlaybackState,
pub jam: crate::jam::JamStatus,
pub visualizer: crate::visualizer::VisualizerState,
pub status_visualizer: crate::visualizer::status::State,
pub global: GlobalTab,
pub artist_views: HashMap<i64, Loadable<ArtistDetail>>,
/// Federated card data that enriches a local artist page in-place.
pub artist_fed_views: HashMap<i64, Loadable<crate::federation::FedArtistCard>>,
pub release_views: HashMap<i64, Loadable<ReleaseDetail>>,
/// Federated tracks that enrich an already-open local release view.
pub release_fed_views: HashMap<i64, Loadable<crate::federation::FedRelease>>,
pub(super) card_enrichment: super::enrichment::RefreshState,
pub playlists: PlaylistsTab,
pub playlist_views: HashMap<i64, Loadable<PlaylistDetail>>,
/// Liked local-library content ids, for the ♥ markers everywhere tracks are shown.
@@ -1463,6 +1880,7 @@ pub struct AppState {
pub queue_tab: QueueTab,
pub federation: FederationTab,
pub similarity: SimilarityTab,
pub podcasts: PodcastsTab,
/// The one federated artist card being viewed (name + loading state);
/// opening another card replaces it.
pub fed_artist_view: Option<(String, Loadable<crate::federation::FedArtistCard>)>,
@@ -1483,6 +1901,18 @@ pub struct AppState {
}
impl AppState {
pub fn artist_federation_loading(&self, id: i64) -> bool {
self.card_enrichment
.is_loading(super::enrichment::Card::Artist(id))
|| matches!(self.artist_fed_views.get(&id), Some(Loadable::Loading))
}
pub fn release_federation_loading(&self, id: i64) -> bool {
self.card_enrichment
.is_loading(super::enrichment::Card::Release(id))
|| matches!(self.release_fed_views.get(&id), Some(Loadable::Loading))
}
pub fn advance_spinner(&mut self) {
self.spinner_frame = self.spinner_frame.wrapping_add(1);
}
@@ -1563,6 +1993,75 @@ pub fn artist_fed_card(state: &AppState, id: i64) -> Option<&crate::federation::
}
}
/// The tracklist rendered and played by an opened release. When federation
/// has supplied a matching release, local rows remain authoritative and only
/// tracks that are not already present locally are appended.
pub fn release_tracks(state: &AppState, id: i64, detail: &ReleaseDetail) -> Vec<TrackItem> {
let mut tracks = detail.tracks.clone();
if !state.global.filters.source_mode.includes_network() {
return tracks;
}
let Some(Loadable::Ready(fed_release)) = state.release_fed_views.get(&id) else {
return tracks;
};
for fed in &fed_release.tracks {
let Some((owner, item_id)) = fed.sources.first().cloned() else {
continue;
};
let fed_content_id = fed
.content_id
.as_deref()
.and_then(music_dht::normalize_content_id);
let already_local = tracks.iter().any(|local| {
let same_content = fed_content_id.is_some()
&& track_content_id(local).as_deref() == fed_content_id.as_deref();
let same_position = local.track_number == fed.track_number
|| local.track_number.is_none()
|| fed.track_number.is_none();
same_content
|| (music_dht::normalize_name(&local.title)
== music_dht::normalize_name(&fed.title)
&& same_position)
});
if already_local {
continue;
}
let names = if fed.artists.is_empty() {
detail
.artists
.iter()
.map(|artist| artist.name.clone())
.collect()
} else {
fed.artists.clone()
};
let featured_artists = fed.featured_artists.clone();
tracks.push(crate::federation::pending_track(
&crate::federation::FedTrack {
item_id,
owner,
own: false,
title: fed.title.clone(),
artist_names: names,
featured_artist_names: featured_artists,
year: fed_release.year.or(detail.year),
duration_seconds: fed.duration_seconds.map(|seconds| seconds.round() as i64),
content_id: fed.content_id.clone(),
release_title: Some(detail.title.clone()),
track_number: fed.track_number,
disc_number: fed.disc_number,
},
));
}
tracks.sort_by_key(|track| {
(
track.disc_number.unwrap_or(1),
track.track_number.unwrap_or(i32::MAX),
)
});
tracks
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ArtistCursorAnchor {
TopTrack(i64),
+469 -59
View File
@@ -18,6 +18,8 @@ pub const QUIT_CONFIRM_HINT: &str = "press quit again to exit";
/// owns the Runtime (audio controller, API client). Keeps update() pure.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Effect {
CheckUpdate,
InstallUpdate(crate::updater::Update),
/// (Re)start playback of `queue[queue_pos]`.
PlayCurrent,
TogglePause,
@@ -104,6 +106,13 @@ pub enum Effect {
},
/// Load qualified listening history without blocking the UI thread.
LoadListenHistory,
/// Load the episode catalog of one podcast (and refresh it when stale).
PodcastLoadEpisodes(i64),
/// Fetch an RSS feed and store it as a new subscription.
PodcastSubscribe(String),
/// Check these subscriptions for new episodes.
PodcastRefresh(Vec<i64>),
PodcastUnsubscribe(i64),
}
pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
@@ -113,6 +122,22 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
.take()
.is_some_and(|deadline| Instant::now() <= deadline);
state.status_message = None;
if state.additional_settings_open
&& !matches!(
action,
Action::PlayPause
| Action::NextTrack
| Action::PrevTrack
| Action::SeekForward { .. }
| Action::SeekBackward { .. }
| Action::VolumeUp
| Action::VolumeDown
| Action::ToggleShuffle
| Action::CycleRepeat
)
{
return update_additional_settings(state, action);
}
match action {
// While the help window is open, quit/back just close it.
Action::Quit | Action::Back if state.help_visible => {
@@ -265,6 +290,7 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
state.logs.selected_seq = None;
state.logs.follow = true;
}
Tab::Podcasts => podcasts_toggle_sort(state),
_ => {}
}
None
@@ -293,6 +319,15 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
state.cmdline.begin_history_navigation();
None
}
Action::OpenSearch
if state.active_tab == Tab::Podcasts && state.podcasts.opened.is_some() =>
{
state.popup = Some(super::state::Popup::FedInput {
field: super::state::FedInputField::PodcastFilter,
input: crate::app::input::LineEdit::new(state.podcasts.filter.clone()),
});
None
}
Action::OpenSearch => {
// The command line opens pre-filled with "/": typing continues
// the live search, exactly as if `:` then `/` were pressed.
@@ -405,6 +440,10 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
state.popup = Some(super::state::Popup::AddToPlaylist { target, cursor: 0 });
state.track_selection.clear();
}
None if state.active_tab == Tab::Podcasts => {
state.status_message =
Some("podcast episodes cannot be added to playlists".into());
}
None => state.status_message = Some("no track selected".into()),
}
None
@@ -452,11 +491,16 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
None
}
}
Action::EditSelected if state.active_tab == Tab::Podcasts => {
state.status_message = Some("podcast metadata comes from the RSS feed".into());
None
}
Action::EditSelected => {
open_edit_popup(state);
None
}
Action::DeleteSelected => delete_selected(state),
Action::RefreshPodcasts => podcasts_refresh(state),
}
}
@@ -533,14 +577,22 @@ fn selected_playlist_target(
return Some(super::state::PlaylistAddTarget::Fed(fed));
}
let local = selected_tracks(state);
// Playlists hold library rows; streamed podcast episodes have none.
let local: Vec<TrackItem> = selected_tracks(state)
.into_iter()
.filter(|track| !crate::podcasts::is_remote_url(&track.file_path))
.collect();
if !local.is_empty() {
return Some(super::state::PlaylistAddTarget::Local(local));
}
if state.active_tab == Tab::Podcasts {
return None;
}
include_current
.then(|| state.player.current.clone())
.flatten()
.filter(|track| !crate::podcasts::is_remote_url(&track.file_path))
.map(|track| super::state::PlaylistAddTarget::Local(vec![track]))
}
@@ -673,6 +725,9 @@ fn delete_selected(state: &mut AppState) -> Option<Effect> {
if state.active_tab == Tab::Queue {
return remove_selected_from_queue(state);
}
if state.active_tab == Tab::Podcasts {
return podcasts_confirm_unsubscribe(state);
}
if state.active_tab == Tab::Playlists {
match state.playlists.opened {
None => {
@@ -905,6 +960,9 @@ fn set_track_scope_cursor(state: &mut AppState, scope: &TrackSelectionScope, val
TrackSelectionScope::Queue => {
state.queue_tab.cursor = value;
}
TrackSelectionScope::SimilaritySearch => {
set_view_cursor(state, value);
}
TrackSelectionScope::FedSearch => {
let base = state.search.results.as_ref().map_or(0, |r| r.len())
+ state.search.fed_artists.len();
@@ -944,14 +1002,21 @@ fn current_track_list_context(state: &AppState) -> Option<(TrackSelectionScope,
_ => None,
},
GlobalView::Release { id, cursor } => match state.release_views.get(id)? {
Loadable::Ready(detail) => Some((
TrackSelectionScope::Release(*id),
*cursor,
detail.tracks.len(),
)),
Loadable::Ready(detail) => {
let tracks = crate::app::state::release_tracks(state, *id, detail);
Some((TrackSelectionScope::Release(*id), *cursor, tracks.len()))
}
_ => None,
},
GlobalView::Search { cursor } => {
if state.search.similarity_source.is_some() {
let len = state.search.similarity_len();
return (*cursor < len).then_some((
TrackSelectionScope::SimilaritySearch,
*cursor,
len,
));
}
// Only the federated tracks section is selectable here.
let base = state.search.results.as_ref().map_or(0, |r| r.len())
+ state.search.fed_artists.len();
@@ -988,11 +1053,11 @@ fn current_track_list_context(state: &AppState) -> Option<(TrackSelectionScope,
state.queue_tab.cursor,
state.player.queue.len(),
)),
Tab::Federation | Tab::Logs => None,
Tab::Federation | Tab::Logs | Tab::Podcasts => None,
}
}
fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, Vec<&TrackItem>)> {
fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, Vec<TrackItem>)> {
match state.active_tab {
Tab::Global => match state.global.stack.last()? {
GlobalView::Artist { id, cursor } => match state.artist_views.get(id)? {
@@ -1003,14 +1068,14 @@ fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, V
Some((
TrackSelectionScope::ArtistTop(*id),
*cursor,
detail.top_tracks.iter().collect(),
detail.top_tracks.clone(),
))
} else {
let featured = cursor.checked_sub(tracks + releases)?;
(featured < detail.featured_tracks.len()).then_some((
TrackSelectionScope::ArtistFeatured(*id),
featured,
detail.featured_tracks.iter().collect(),
detail.featured_tracks.clone(),
))
}
}
@@ -1020,7 +1085,7 @@ fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, V
Loadable::Ready(detail) => Some((
TrackSelectionScope::Release(*id),
*cursor,
detail.tracks.iter().collect(),
crate::app::state::release_tracks(state, *id, detail),
)),
_ => None,
},
@@ -1031,19 +1096,39 @@ fn current_track_list(state: &AppState) -> Option<(TrackSelectionScope, usize, V
Some((
TrackSelectionScope::Playlist(opened.id),
opened.cursor,
playlist_tracks(state, opened.id)?,
playlist_tracks(state, opened.id)?
.into_iter()
.cloned()
.collect(),
))
}
Tab::Queue => Some((
TrackSelectionScope::Queue,
state.queue_tab.cursor,
state.player.queue.iter().collect(),
state.player.queue.clone(),
)),
Tab::Federation | Tab::Logs => None,
Tab::Federation | Tab::Logs | Tab::Podcasts => None,
}
}
pub fn selected_tracks(state: &AppState) -> Vec<TrackItem> {
if state.active_tab == Tab::Podcasts {
return selected_episode_track(state).into_iter().collect();
}
if state.active_tab == Tab::Global
&& state.search.similarity_source.is_some()
&& let Some(GlobalView::Search { cursor }) = state.global.stack.last()
{
let len = state.search.similarity_len();
let indices = state
.track_selection
.indices(&TrackSelectionScope::SimilaritySearch, len)
.unwrap_or_else(|| vec![(*cursor).min(len.saturating_sub(1))]);
return indices
.into_iter()
.filter_map(|index| state.search.similarity_track(index))
.collect();
}
// Federated contexts produce queueable placeholders that behave like
// regular tracks (queue, info, playback-on-demand).
{
@@ -1061,7 +1146,7 @@ pub fn selected_tracks(state: &AppState) -> Vec<TrackItem> {
.unwrap_or_else(|| vec![cursor.min(tracks.len().saturating_sub(1))]);
indices
.into_iter()
.filter_map(|index| tracks.get(index).map(|track| (*track).clone()))
.filter_map(|index| tracks.get(index).cloned())
.collect()
}
@@ -1272,10 +1357,15 @@ pub fn selected_track(state: &AppState) -> Option<TrackItem> {
_ => None,
},
GlobalView::Release { id, cursor } => match state.release_views.get(id)? {
Loadable::Ready(detail) => detail.tracks.get(*cursor).cloned(),
Loadable::Ready(detail) => crate::app::state::release_tracks(state, *id, detail)
.get(*cursor)
.cloned(),
_ => None,
},
GlobalView::Search { cursor } => {
if state.search.similarity_source.is_some() {
return state.search.similarity_track(*cursor);
}
let results = state.search.results.as_ref()?;
let offset = cursor.checked_sub(results.artists.len() + results.releases.len())?;
match results.tracks.get(offset) {
@@ -1321,7 +1411,7 @@ pub fn selected_track(state: &AppState) -> Option<TrackItem> {
.map(|track| (*track).clone())
}
Tab::Queue => state.player.queue.get(state.queue_tab.cursor).cloned(),
Tab::Federation | Tab::Logs => None,
Tab::Federation | Tab::Logs | Tab::Podcasts => None,
}
}
@@ -1410,11 +1500,12 @@ fn open_release_for_track(state: &mut AppState, track: &TrackItem) {
}
let release_id = track.release_id;
let cursor = match state.release_views.get(&release_id) {
Some(Loadable::Ready(detail)) => detail
.tracks
.iter()
.position(|t| t.id == track.id)
.unwrap_or(0),
Some(Loadable::Ready(detail)) => {
crate::app::state::release_tracks(state, release_id, detail)
.iter()
.position(|t| t.id == track.id)
.unwrap_or(0)
}
_ => {
state.pending_release_focus = Some((release_id, track.id));
0
@@ -1724,6 +1815,10 @@ fn page_step(state: &AppState) -> isize {
}
fn move_selection(state: &mut AppState, dx: isize, dy: isize) {
if state.active_tab == Tab::Podcasts {
podcasts_move(state, dy);
return;
}
if state.active_tab == Tab::Federation {
if dy != 0 {
let last = settings_rows(state).len() as isize - 1;
@@ -1868,7 +1963,7 @@ fn move_selection(state: &mut AppState, dx: isize, dy: isize) {
let Some(Loadable::Ready(detail)) = state.release_views.get(&id) else {
return;
};
let total = detail.tracks.len() as isize;
let total = crate::app::state::release_tracks(state, id, detail).len() as isize;
if total == 0 {
return;
}
@@ -1877,10 +1972,13 @@ fn move_selection(state: &mut AppState, dx: isize, dy: isize) {
refresh_track_selection_cursor(state);
}
Some(GlobalView::Search { cursor }) => {
// Local results plus the federated section below them.
let total = (state.search.results.as_ref().map_or(0, |r| r.len())
+ state.search.fed_artists.len()
+ state.search.fed_tracks.len()) as isize;
let total = if state.search.similarity_source.is_some() {
state.search.similarity_len()
} else {
state.search.results.as_ref().map_or(0, |r| r.len())
+ state.search.fed_artists.len()
+ state.search.fed_tracks.len()
} as isize;
if total == 0 {
return;
}
@@ -1996,13 +2094,17 @@ fn current_view_len(state: &AppState) -> usize {
_ => 0,
},
Some(GlobalView::Release { id, .. }) => match state.release_views.get(id) {
Some(Loadable::Ready(d)) => d.tracks.len(),
Some(Loadable::Ready(d)) => crate::app::state::release_tracks(state, *id, d).len(),
_ => 0,
},
Some(GlobalView::Search { .. }) => {
state.search.results.as_ref().map_or(0, |r| r.len())
+ state.search.fed_artists.len()
+ state.search.fed_tracks.len()
if state.search.similarity_source.is_some() {
state.search.similarity_len()
} else {
state.search.results.as_ref().map_or(0, |r| r.len())
+ state.search.fed_artists.len()
+ state.search.fed_tracks.len()
}
}
Some(GlobalView::FedArtist { .. }) => fed_card_len(state),
Some(GlobalView::FedRelease { index, .. }) => fed_card_release(state, *index)
@@ -2111,6 +2213,10 @@ fn jump_selection(state: &mut AppState, first: bool) {
state.settings_cursor = if first { 0 } else { last };
return;
}
if state.active_tab == Tab::Podcasts {
podcasts_move(state, if first { isize::MIN } else { isize::MAX });
return;
}
if state.active_tab == Tab::Logs {
if first {
let level = super::state::LOG_LEVELS[state.logs.level_index];
@@ -2201,6 +2307,9 @@ fn select_current(state: &mut AppState) -> Option<Effect> {
if state.active_tab == Tab::Federation {
return federation_select(state);
}
if state.active_tab == Tab::Podcasts {
return podcasts_select(state);
}
// Queue: jump playback to the track under the cursor. Earlier tracks
// stay in the queue as "played"; picking one of them just moves the
// playing position back.
@@ -2286,37 +2395,60 @@ fn select_current(state: &mut AppState) -> Option<Effect> {
_ => Outcome::Nothing,
},
Some(GlobalView::Release { id, cursor }) => match state.release_views.get(&id) {
Some(Loadable::Ready(detail)) if !detail.tracks.is_empty() => Outcome::Play {
tracks: detail.tracks.clone(),
start: cursor.min(detail.tracks.len() - 1),
},
_ => Outcome::Nothing,
},
Some(GlobalView::Search { cursor }) => match &state.search.results {
Some(results) => {
let artists = results.artists.len();
let releases = results.releases.len();
if cursor < artists {
Outcome::Push(GlobalView::Artist {
id: results.artists[cursor].id,
cursor: 0,
})
} else if cursor < artists + releases {
Outcome::Push(GlobalView::Release {
id: results.releases[cursor - artists].id,
cursor: 0,
})
} else if results.tracks.get(cursor - artists - releases).is_some() {
Outcome::Play {
tracks: results.tracks.clone(),
start: cursor - artists - releases,
}
Some(Loadable::Ready(detail)) => {
let tracks = crate::app::state::release_tracks(state, id, detail);
if tracks.is_empty() {
Outcome::Nothing
} else {
fed_outcome(state, cursor - artists - releases - results.tracks.len())
Outcome::Play {
start: cursor.min(tracks.len() - 1),
tracks,
}
}
}
None => fed_outcome(state, cursor),
_ => Outcome::Nothing,
},
Some(GlobalView::Search { cursor }) => {
if state.search.similarity_source.is_some() {
let tracks = (0..state.search.similarity_len())
.filter_map(|index| state.search.similarity_track(index))
.collect::<Vec<_>>();
if tracks.is_empty() {
Outcome::Nothing
} else {
Outcome::Play {
start: cursor.min(tracks.len() - 1),
tracks,
}
}
} else {
match &state.search.results {
Some(results) => {
let artists = results.artists.len();
let releases = results.releases.len();
if cursor < artists {
Outcome::Push(GlobalView::Artist {
id: results.artists[cursor].id,
cursor: 0,
})
} else if cursor < artists + releases {
Outcome::Push(GlobalView::Release {
id: results.releases[cursor - artists].id,
cursor: 0,
})
} else if results.tracks.get(cursor - artists - releases).is_some() {
Outcome::Play {
tracks: results.tracks.clone(),
start: cursor - artists - releases,
}
} else {
fed_outcome(state, cursor - artists - releases - results.tracks.len())
}
}
None => fed_outcome(state, cursor),
}
}
}
Some(GlobalView::FedArtist { cursor }) => match &state.fed_artist_view {
Some((_, Loadable::Ready(card))) => {
let release_indices = fed_artist_visible_release_indices(state, card);
@@ -2492,6 +2624,20 @@ pub(crate) fn selected_fed_tracks(state: &AppState) -> Vec<crate::federation::Fe
if state.active_tab != Tab::Global {
return Vec::new();
}
if state.search.similarity_source.is_some()
&& let Some(GlobalView::Search { cursor }) = state.global.stack.last()
{
let scope = TrackSelectionScope::SimilaritySearch;
let len = state.search.similarity_len();
let indices = state
.track_selection
.indices(&scope, len)
.unwrap_or_else(|| vec![(*cursor).min(len.saturating_sub(1))]);
return indices
.into_iter()
.filter_map(|index| state.search.similarity_fed_track(index).cloned())
.collect();
}
// An active Shift-V range in a federated scope.
if let Some(scope) = state.track_selection.scope.clone() {
match scope {
@@ -2747,9 +2893,66 @@ fn fed_card_featured_artist_names(track: &crate::federation::FedCardTrack) -> Ve
/// Enter on Settings: toggle switches, open text inputs, run
/// one-shot operations. The heavy lifting happens in perform_effect().
fn update_additional_settings(state: &mut AppState, action: Action) -> Option<Effect> {
if state.help_visible {
if matches!(action, Action::Back | Action::Quit | Action::ToggleHelp) {
state.help_visible = false;
}
return None;
}
let rows = super::state::additional_settings_rows(state);
let last = rows.len().saturating_sub(1);
let cursor = state.additional_settings_cursor.min(last);
state.additional_settings_cursor = match action {
Action::Back | Action::Quit => {
state.additional_settings_open = false;
return None;
}
Action::MoveUp | Action::PrevTab => cursor.saturating_sub(1),
Action::MoveDown | Action::NextTab => (cursor + 1).min(last),
Action::PageUp => cursor.saturating_sub(5),
Action::PageDown => (cursor + 5).min(last),
Action::SelectFirst => 0,
Action::SelectLast => last,
Action::Select => return select_settings_row(state, *rows.get(cursor)?),
Action::ToggleHelp => {
state.help_visible = true;
cursor
}
_ => cursor,
};
None
}
fn federation_select(state: &mut AppState) -> Option<Effect> {
select_settings_row(state, *settings_rows(state).get(state.settings_cursor)?)
}
fn select_settings_row(state: &mut AppState, row: super::state::SettingsRow) -> Option<Effect> {
use super::state::{FedInputField, FedRow, Popup, SettingsRow, SimilarityRow};
match settings_rows(state).get(state.settings_cursor).copied()? {
match row {
SettingsRow::AdditionalSettings => {
state.additional_settings_open = true;
None
}
SettingsRow::CheckUpdate => {
if state.updater.busy || state.updater.installed {
return None;
}
state.updater.busy = true;
state.updater.available = None;
state.updater.message = "Checking GitHub Releases...".into();
Some(Effect::CheckUpdate)
}
SettingsRow::InstallUpdate => {
if state.updater.busy || state.updater.installed {
return None;
}
let update = state.updater.available.clone()?;
state.updater.busy = true;
state.updater.message = "Downloading update...".into();
Some(Effect::InstallUpdate(update))
}
SettingsRow::MusicDirectory => {
if state.music_dir_changing {
state.status_message = Some("music directory change is already running".into());
@@ -2796,6 +2999,25 @@ fn federation_select(state: &mut AppState) -> Option<Effect> {
});
None
}
SettingsRow::Similarity(SimilarityRow::MinimumScore) => {
state.popup = Some(Popup::FedInput {
field: FedInputField::SimilarityMinimumScore,
input: crate::app::input::LineEdit::new(format!(
"{:.2}",
state.similarity.settings.minimum_score
)),
});
None
}
SettingsRow::Similarity(SimilarityRow::MaxTracksPerArtist) => {
state.popup = Some(Popup::FedInput {
field: FedInputField::SimilarityMaxTracksPerArtist,
input: crate::app::input::LineEdit::new(
state.similarity.settings.max_tracks_per_artist.to_string(),
),
});
None
}
SettingsRow::Similarity(SimilarityRow::Workers) => {
state.popup = Some(Popup::FedInput {
field: FedInputField::SimilarityWorkers,
@@ -3026,6 +3248,7 @@ fn go_back(state: &mut AppState) {
Tab::Playlists => {
state.playlists.opened = None;
}
Tab::Podcasts => podcasts_close(state),
Tab::Global => {
// Esc on a view opened by Shift-J from another tab goes back to
// that tab, not down the Global stack.
@@ -3080,9 +3303,196 @@ fn reset_tab(state: &mut AppState, tab: Tab) {
state.logs.selected_seq = None;
}
Tab::Queue => {}
Tab::Podcasts => podcasts_close(state),
}
}
// ---------------------------------------------------------------------------
// Podcasts tab
// ---------------------------------------------------------------------------
/// How long a feed counts as fresh: opening it or the periodic sweep only
/// re-fetches feeds checked longer ago than this.
pub const PODCAST_STALE_SECS: i64 = 30 * 60;
fn podcasts_move(state: &mut AppState, dy: isize) {
let tab = &mut state.podcasts;
let (cursor, len) = match tab.opened {
Some(opened) => (opened.cursor, tab.visible_episode_indices().len()),
None => (tab.selected, tab.list_rows().len()),
};
if len == 0 {
return;
}
let target = (cursor as isize)
.saturating_add(dy)
.clamp(0, len as isize - 1) as usize;
match &mut tab.opened {
Some(opened) => opened.cursor = target,
None => tab.selected = target,
}
}
fn podcasts_toggle_sort(state: &mut AppState) {
let tab = &mut state.podcasts;
tab.sort = tab.sort.toggle();
if let Some(opened) = &mut tab.opened {
opened.cursor = 0;
}
state.status_message = Some(format!("episodes: {}", state.podcasts.sort.label()));
}
/// Esc: episode catalog → subscriptions list. The filter belongs to the
/// catalog it was typed in, so it is dropped as well.
fn podcasts_close(state: &mut AppState) {
let tab = &mut state.podcasts;
if let Some(opened) = tab.opened.take()
&& let Some(index) = tab.podcasts().iter().position(|p| p.id == opened.id)
{
tab.selected = index + super::state::PODCAST_ACTION_ROWS;
}
tab.filter.clear();
}
/// Called whenever the filter text changes: keep the cursor on the first
/// match so the list does not appear empty below a stale cursor.
pub fn podcasts_set_filter(state: &mut AppState, filter: String) {
// Caret movement and Enter on an unchanged filter keep the cursor.
if state.podcasts.filter == filter {
return;
}
state.podcasts.filter = filter;
if let Some(opened) = &mut state.podcasts.opened {
opened.cursor = 0;
}
}
fn podcasts_select(state: &mut AppState) -> Option<Effect> {
use super::state::PodcastListRow;
if state.podcasts.opened.is_some() {
return play_selected_episode(state);
}
match state
.podcasts
.list_rows()
.get(state.podcasts.selected)
.copied()?
{
PodcastListRow::Add => {
if state.podcasts.subscribing {
state.status_message =
Some("podcasts: a subscription is already being added".into());
return None;
}
state.popup = Some(super::state::Popup::FedInput {
field: super::state::FedInputField::PodcastFeedUrl,
input: crate::app::input::LineEdit::default(),
});
None
}
PodcastListRow::RefreshAll => podcasts_refresh(state),
PodcastListRow::Podcast(index) => {
let id = state.podcasts.podcasts().get(index)?.id;
state.podcasts.opened = Some(super::state::OpenedPodcast { id, cursor: 0 });
state.podcasts.filter.clear();
if !matches!(&state.podcasts.episodes, Some((loaded, Loadable::Ready(_))) if *loaded == id)
{
state.podcasts.episodes = Some((id, Loadable::Loading));
}
Some(Effect::PodcastLoadEpisodes(id))
}
}
}
/// Shift-R: the opened podcast, or every subscription from the list.
fn podcasts_refresh(state: &mut AppState) -> Option<Effect> {
if state.active_tab != Tab::Podcasts {
return None;
}
let ids: Vec<i64> = match state.podcasts.opened {
Some(opened) => vec![opened.id],
None => state.podcasts.podcasts().iter().map(|p| p.id).collect(),
};
let ids: Vec<i64> = ids
.into_iter()
.filter(|id| !state.podcasts.refreshing.contains(id))
.collect();
if ids.is_empty() {
state.status_message = Some(if state.podcasts.podcasts().is_empty() {
"podcasts: no subscriptions yet".into()
} else {
"podcasts: already checking for new episodes".into()
});
return None;
}
state.podcasts.refreshing.extend(ids.iter().copied());
state.status_message = Some(format!(
"podcasts: checking {} feed(s) for new episodes…",
ids.len()
));
Some(Effect::PodcastRefresh(ids))
}
fn podcasts_confirm_unsubscribe(state: &mut AppState) -> Option<Effect> {
if state.podcasts.opened.is_some() {
state.status_message =
Some("podcasts: go back to the podcast list (esc) to unsubscribe".into());
return None;
}
let Some(podcast) = state.podcasts.current_podcast() else {
state.status_message = Some("podcasts: select a podcast to unsubscribe".into());
return None;
};
state.popup = Some(super::state::Popup::ConfirmDelete {
target: super::state::DeleteTarget::Podcast(podcast.id),
label: format!("podcast \"{}\"", podcast.title),
});
None
}
/// The queueable track for the episode under the cursor.
fn selected_episode_track(state: &AppState) -> Option<TrackItem> {
let opened = state.podcasts.opened?;
let podcast = state.podcasts.podcast(opened.id)?;
let episode = state.podcasts.selected_episode()?;
Some(crate::podcasts::episode_track(podcast, episode))
}
/// Enter on an episode: play it right now without discarding the queue. It
/// is inserted after the current track and becomes the current track.
fn play_selected_episode(state: &mut AppState) -> Option<Effect> {
let Some(track) = selected_episode_track(state) else {
state.status_message = Some(match state.podcasts.opened_episodes() {
Some(_) if !state.podcasts.filter.is_empty() => "no episode matches the filter".into(),
Some(_) => "this podcast has no episodes".into(),
None => "episodes are still loading".into(),
});
return None;
};
Some(play_track_now(state, track))
}
pub fn play_track_now(state: &mut AppState, track: TrackItem) -> Effect {
let player = &mut state.player;
let insert_at = if player.current.is_some() {
(player.queue_pos + 1).min(player.queue.len())
} else {
player.queue_pos.min(player.queue.len())
};
player.queue.insert(insert_at, track);
if let Some(end) = player.play_next_end.as_mut()
&& *end >= insert_at
{
*end += 1;
}
player.queue_pos = insert_at;
player.prefetched_pos = None;
normalize_play_next_block(player);
state.track_selection.clear();
Effect::PlayCurrent
}
fn not_yet(state: &mut AppState, what: &str) {
state.status_message = Some(format!("{what}: coming in a later milestone"));
}
+596 -1
View File
@@ -1,6 +1,79 @@
use super::*;
use crate::library::models::{ArtistCard, ArtistDetail, TrackItem};
#[test]
fn additional_settings_preserve_main_navigation_and_child_dialog_parent() {
use crate::app::state::{FedInputField, Popup, SettingsRow, additional_settings_rows};
let mut state = AppState {
active_tab: Tab::Federation,
..AppState::default()
};
let main_rows = settings_rows(&state);
assert!(!main_rows.contains(&SettingsRow::MusicDirectory));
assert!(!main_rows.contains(&SettingsRow::CheckUpdate));
assert!(!main_rows.contains(&SettingsRow::VisualizationClock));
state.settings_cursor = main_rows
.iter()
.position(|r| *r == SettingsRow::AdditionalSettings)
.unwrap();
let main_cursor = state.settings_cursor;
update(&mut state, Action::Select);
assert!(state.additional_settings_open);
update(&mut state, Action::Select);
assert!(matches!(
state.popup,
Some(Popup::FedInput {
field: FedInputField::MusicDirectory,
..
})
));
// Child dialogs own their input; closing one leaves the parent window intact.
state.popup = None;
assert!(state.additional_settings_open);
update(&mut state, Action::SelectLast);
assert_eq!(
state.additional_settings_cursor,
additional_settings_rows(&state).len() - 1
);
update(&mut state, Action::MoveDown);
assert_eq!(
state.additional_settings_cursor,
additional_settings_rows(&state).len() - 1
);
update(&mut state, Action::ToggleHelp);
update(&mut state, Action::Back);
assert!(!state.help_visible);
assert!(state.additional_settings_open);
update(&mut state, Action::Back);
assert!(!state.additional_settings_open);
assert!(!state.should_quit);
assert_eq!(state.settings_cursor, main_cursor);
}
#[test]
fn manual_update_check_is_single_flight_and_disabled_after_install() {
let mut state = AppState::default();
state.additional_settings_cursor = crate::app::state::additional_settings_rows(&state)
.iter()
.position(|row| *row == crate::app::state::SettingsRow::CheckUpdate)
.unwrap();
assert_eq!(
update_additional_settings(&mut state, Action::Select),
Some(Effect::CheckUpdate)
);
assert!(state.updater.busy);
assert_eq!(update_additional_settings(&mut state, Action::Select), None);
state.updater.busy = false;
state.updater.installed = true;
assert_eq!(update_additional_settings(&mut state, Action::Select), None);
state.updater.installed = false;
state.additional_settings_cursor = crate::app::state::additional_settings_rows(&state)
.iter()
.position(|row| *row == crate::app::state::SettingsRow::InstallUpdate)
.unwrap();
assert_eq!(update_additional_settings(&mut state, Action::Select), None);
}
fn with_artists(n: usize) -> AppState {
let mut state = AppState::default();
state.global.artists = (0..n)
@@ -108,7 +181,7 @@ fn expired_quit_confirmation_rearms() {
fn tab_cycling_wraps() {
let mut state = AppState::default();
update(&mut state, Action::PrevTab);
assert_eq!(state.active_tab, Tab::Logs);
assert_eq!(state.active_tab, Tab::Podcasts);
update(&mut state, Action::NextTab);
assert_eq!(state.active_tab, Tab::Global);
}
@@ -1000,3 +1073,525 @@ fn view_toggle() {
update(&mut state, Action::ToggleViewMode);
assert_eq!(state.global.view, ViewMode::Tiles);
}
fn enrichment_state(release: bool) -> AppState {
use crate::library::models::{ArtistRef, ReleaseDetail};
let mut state = AppState::default();
state.federation.settings.enabled = true;
state.global.filters.source_mode = crate::config::settings::LibrarySourceMode::Global;
state.artist_views.insert(
1,
Loadable::Ready(ArtistDetail {
id: 1,
name: "Artist".into(),
image_path: None,
total_track_count: 1,
total_play_count: 0,
top_tracks: vec![test_track(1)],
featured_tracks: vec![],
releases: vec![],
}),
);
state.release_views.insert(
1,
Loadable::Ready(ReleaseDetail {
id: 1,
title: "Release".into(),
release_type: "album".into(),
year: None,
cover_path: None,
artists: vec![ArtistRef {
id: 1,
name: "Artist".into(),
}],
tracks: vec![test_track(1)],
}),
);
state.global.stack.push(if release {
GlobalView::Release { id: 1, cursor: 0 }
} else {
GlobalView::Artist { id: 1, cursor: 0 }
});
state
}
#[test]
fn card_enrichment_retries_startup_failure_without_reopening() {
use crate::app::enrichment::{self, Card, Request};
use std::time::{Duration, Instant};
for release in [false, true] {
let mut state = enrichment_state(release);
let now = Instant::now();
let request = enrichment::prepare(&mut state, now).unwrap();
let card = match request {
Request::Artist { id: 1, ref name } if name == "Artist" => Card::Artist(1),
Request::Release {
id: 1,
ref title,
ref artists,
} if title == "Release" && artists == &["Artist"] => Card::Release(1),
_ => panic!("wrong enrichment request: {request:?}"),
};
// Slow requests are never duplicated, even after the refresh interval.
assert!(enrichment::prepare(&mut state, now + Duration::from_secs(60)).is_none());
let completed = now + Duration::from_secs(60);
state.card_enrichment.complete(card, completed);
if release {
enrichment::store_result(
&mut state.release_fed_views,
1,
Err("federation is not running".into()),
);
} else {
enrichment::store_result(
&mut state.artist_fed_views,
1,
Err("federation is not running".into()),
);
}
assert!(enrichment::prepare(&mut state, completed + Duration::from_secs(1)).is_none());
assert_eq!(
enrichment::prepare(&mut state, completed + Duration::from_secs(30)),
Some(request)
);
assert!(matches!(
state.artist_views.get(&1),
Some(Loadable::Ready(_))
));
assert!(matches!(
state.release_views.get(&1),
Some(Loadable::Ready(_))
));
}
}
#[test]
fn card_enrichment_reopens_cached_cards_and_keeps_success_during_failed_refresh() {
use crate::app::enrichment::{self, Card};
use std::time::{Duration, Instant};
for release in [false, true] {
let mut state = enrichment_state(release);
let now = Instant::now();
let request = enrichment::prepare(&mut state, now).unwrap();
state.card_enrichment.complete(
if release {
Card::Release(1)
} else {
Card::Artist(1)
},
now,
);
if release {
enrichment::store_result(
&mut state.release_fed_views,
1,
Ok(crate::federation::FedRelease {
title: "Release".into(),
..Default::default()
}),
);
} else {
enrichment::store_result(
&mut state.artist_fed_views,
1,
Ok(crate::federation::FedArtistCard {
name: "Artist".into(),
..Default::default()
}),
);
}
// Successful but local-only/empty snapshots must also be refreshed.
assert!(enrichment::prepare(&mut state, now).is_none());
let view = state.global.stack.pop().unwrap();
assert!(enrichment::prepare(&mut state, now).is_none());
state.global.stack.push(view);
assert_eq!(
enrichment::prepare(&mut state, now + Duration::from_secs(1)),
Some(request)
);
if release {
assert!(matches!(
state.release_fed_views.get(&1),
Some(Loadable::Ready(_))
));
enrichment::store_result(&mut state.release_fed_views, 1, Err("offline".into()));
assert!(
matches!(state.release_fed_views.get(&1), Some(Loadable::Ready(card)) if card.title == "Release")
);
} else {
assert!(matches!(
state.artist_fed_views.get(&1),
Some(Loadable::Ready(_))
));
enrichment::store_result(&mut state.artist_fed_views, 1, Err("offline".into()));
assert!(
matches!(state.artist_fed_views.get(&1), Some(Loadable::Ready(card)) if card.name == "Artist")
);
}
}
}
#[test]
fn card_enrichment_respects_network_mode_and_deduplicates_navigation() {
use crate::app::enrichment;
use crate::config::settings::LibrarySourceMode;
use std::time::Instant;
for release in [false, true] {
let mut state = enrichment_state(release);
let now = Instant::now();
state.federation.settings.enabled = false;
assert!(enrichment::prepare(&mut state, now).is_none());
state.federation.settings.enabled = true;
state.global.filters.source_mode = LibrarySourceMode::Local;
assert!(enrichment::prepare(&mut state, now).is_none());
state.global.filters.source_mode = LibrarySourceMode::Global;
assert!(enrichment::prepare(&mut state, now).is_some());
state.active_tab = Tab::Queue;
assert!(enrichment::prepare(&mut state, now).is_none());
state.active_tab = Tab::Global;
assert!(enrichment::prepare(&mut state, now).is_none());
}
}
#[test]
fn card_enrichment_refreshes_local_only_success_while_still_open() {
use crate::app::enrichment::{self, Card};
use std::time::{Duration, Instant};
for release in [false, true] {
let mut state = enrichment_state(release);
let now = Instant::now();
let request = enrichment::prepare(&mut state, now).unwrap();
state.card_enrichment.complete(
if release {
Card::Release(1)
} else {
Card::Artist(1)
},
now,
);
if release {
enrichment::store_result(&mut state.release_fed_views, 1, Ok(Default::default()));
} else {
enrichment::store_result(&mut state.artist_fed_views, 1, Ok(Default::default()));
}
assert!(enrichment::prepare(&mut state, now + Duration::from_secs(29)).is_none());
assert_eq!(
enrichment::prepare(&mut state, now + Duration::from_secs(30)),
Some(request)
);
}
}
fn render_enrichment_card(state: &AppState) -> String {
use ratatui::{Terminal, backend::TestBackend};
let mut terminal = Terminal::new(TestBackend::new(160, 40)).unwrap();
terminal
.draw(|frame| crate::ui::global::draw(frame, frame.area(), state))
.unwrap();
terminal
.backend()
.buffer()
.content
.iter()
.map(|cell| cell.symbol())
.collect()
}
#[test]
fn local_card_explains_source_filter_instead_of_waiting_for_a_request() {
use crate::app::enrichment;
let mut state = enrichment_state(false);
state.global.filters.source_mode = crate::config::settings::LibrarySourceMode::Local;
assert!(enrichment::prepare(&mut state, std::time::Instant::now()).is_none());
let text = render_enrichment_card(&state);
assert!(
!text.contains("federation: pending"),
"Local mode must not promise a request that is disabled"
);
assert!(
text.contains("Local"),
"the card must explain why network results are hidden"
);
}
#[test]
fn card_refresh_animates_while_previous_results_stay_visible() {
use crate::app::enrichment::{self, Card};
use std::time::{Duration, Instant};
for release in [false, true] {
let mut state = enrichment_state(release);
let now = Instant::now();
assert!(enrichment::prepare(&mut state, now).is_some());
let card = if release {
Card::Release(1)
} else {
Card::Artist(1)
};
state.card_enrichment.complete(card, now);
if release {
enrichment::store_result(&mut state.release_fed_views, 1, Ok(Default::default()));
} else {
enrichment::store_result(&mut state.artist_fed_views, 1, Ok(Default::default()));
}
assert!(enrichment::prepare(&mut state, now + Duration::from_secs(30)).is_some());
let first = render_enrichment_card(&state);
assert!(first.contains(&format!("{} searching peers", state.spinner())));
state.advance_spinner();
let next = render_enrichment_card(&state);
assert!(next.contains(&format!("{} searching peers", state.spinner())));
assert_ne!(first, next);
state
.card_enrichment
.complete(card, now + Duration::from_secs(31));
assert!(!render_enrichment_card(&state).contains("searching peers"));
}
}
// ---------------------------------------------------------------------------
// Podcasts
// ---------------------------------------------------------------------------
fn podcast(id: i64, title: &str) -> crate::podcasts::Podcast {
crate::podcasts::Podcast {
id,
feed_url: format!("https://example.test/{id}.xml"),
title: title.into(),
author: None,
description: None,
link: None,
image_url: None,
cover_path: None,
episode_count: 0,
latest_published_at: None,
last_checked_at: None,
last_error: None,
}
}
fn episode(id: i64, title: &str, published_at: Option<i64>) -> crate::podcasts::Episode {
crate::podcasts::Episode {
id,
podcast_id: 7,
guid: format!("guid-{id}"),
title: title.into(),
description: None,
published_at,
duration_seconds: Some(60.0),
audio_url: format!("https://cdn.example.test/{id}.mp3"),
mime_type: Some("audio/mpeg".into()),
byte_len: None,
link: None,
image_url: None,
}
}
/// Podcasts tab with podcast 7 opened and three episodes loaded in store
/// order (newest first) plus one undated episode.
fn podcasts_state() -> AppState {
use crate::app::state::OpenedPodcast;
let mut state = AppState {
active_tab: Tab::Podcasts,
..AppState::default()
};
state.podcasts.list = Some(Loadable::Ready(vec![
podcast(3, "Another show"),
podcast(7, "Vivat"),
]));
state.podcasts.opened = Some(OpenedPodcast { id: 7, cursor: 0 });
state.podcasts.episodes = Some((
7,
Loadable::Ready(vec![
episode(30, "Выпуск 3: Рим", Some(3_000)),
episode(20, "Выпуск 2: Греция", Some(2_000)),
episode(10, "Выпуск 1: Египет", Some(1_000)),
episode(5, "Бонус без даты", None),
]),
));
state
}
#[test]
fn podcast_tab_is_reachable_by_number_without_shifting_other_tabs() {
let mut state = AppState::default();
update(&mut state, Action::GoToTab(2));
assert_eq!(state.active_tab, Tab::Queue);
update(&mut state, Action::GoToTab(5));
assert_eq!(state.active_tab, Tab::Podcasts);
}
#[test]
fn podcast_episode_filter_matches_every_word_case_insensitively() {
let mut state = podcasts_state();
let titles = |state: &AppState| -> Vec<String> {
let episodes = state.podcasts.opened_episodes().unwrap();
state
.podcasts
.visible_episode_indices()
.into_iter()
.map(|index| episodes[index].title.clone())
.collect()
};
assert_eq!(titles(&state).len(), 4);
podcasts_set_filter(&mut state, "ВЫПУСК рим".into());
assert_eq!(titles(&state), vec!["Выпуск 3: Рим"]);
podcasts_set_filter(&mut state, "nothing".into());
assert!(titles(&state).is_empty());
assert!(update(&mut state, Action::Select).is_none());
assert_eq!(
state.status_message.as_deref(),
Some("no episode matches the filter")
);
}
#[test]
fn podcast_sort_toggles_and_keeps_undated_episodes_last() {
let mut state = podcasts_state();
let ids = |state: &AppState| -> Vec<i64> {
let episodes = state.podcasts.opened_episodes().unwrap();
state
.podcasts
.visible_episode_indices()
.into_iter()
.map(|index| episodes[index].id)
.collect()
};
assert_eq!(ids(&state), vec![30, 20, 10, 5]);
state.podcasts.opened.as_mut().unwrap().cursor = 2;
update(&mut state, Action::ToggleViewMode);
assert_eq!(ids(&state), vec![10, 20, 30, 5]);
assert_eq!(state.podcasts.opened.unwrap().cursor, 0);
}
#[test]
fn podcast_search_key_opens_the_episode_filter_not_library_search() {
use crate::app::state::{FedInputField, Popup};
let mut state = podcasts_state();
update(&mut state, Action::OpenSearch);
assert_eq!(state.active_tab, Tab::Podcasts);
assert!(!state.cmdline.active);
assert!(matches!(
state.popup,
Some(Popup::FedInput {
field: FedInputField::PodcastFilter,
..
})
));
}
#[test]
fn enter_on_episode_plays_it_now_and_keeps_the_queue() {
let mut state = podcasts_state();
state.player.queue = vec![test_track(1), test_track(2)];
state.player.queue_pos = 0;
state.player.current = Some(test_track(1));
state.podcasts.opened.as_mut().unwrap().cursor = 1;
assert_eq!(
update(&mut state, Action::Select),
Some(Effect::PlayCurrent)
);
assert_eq!(state.player.queue.len(), 3);
assert_eq!(state.player.queue_pos, 1);
let queued = &state.player.queue[1];
assert_eq!(queued.title, "Выпуск 2: Греция");
assert_eq!(queued.file_path, "https://cdn.example.test/20.mp3");
assert_eq!(queued.release_title, "Vivat");
assert_eq!(
crate::podcasts::episode_id_from_track_id(queued.id),
Some(20)
);
// The music that was queued after the current track still follows.
assert_eq!(state.player.queue[2].id, 2);
}
#[test]
fn episode_can_be_queued_with_the_regular_queue_keys() {
let mut state = podcasts_state();
state.player.queue = vec![test_track(1)];
state.player.current = Some(test_track(1));
update(&mut state, Action::QueueAddLast);
assert_eq!(state.player.queue.len(), 2);
assert_eq!(state.player.queue[1].title, "Выпуск 3: Рим");
assert_eq!(state.player.queue_pos, 0);
}
#[test]
fn podcast_list_rows_open_add_refresh_and_podcasts() {
use crate::app::state::{FedInputField, PODCAST_ACTION_ROWS, Popup};
let mut state = podcasts_state();
update(&mut state, Action::Back);
assert!(state.podcasts.opened.is_none());
// Esc returns to the row of the podcast that was open.
assert_eq!(state.podcasts.selected, PODCAST_ACTION_ROWS + 1);
update(&mut state, Action::SelectFirst);
assert_eq!(state.podcasts.selected, 0);
assert!(update(&mut state, Action::Select).is_none());
assert!(matches!(
state.popup,
Some(Popup::FedInput {
field: FedInputField::PodcastFeedUrl,
..
})
));
state.popup = None;
update(&mut state, Action::MoveDown);
assert_eq!(
update(&mut state, Action::Select),
Some(Effect::PodcastRefresh(vec![3, 7]))
);
// A second request while those are running is not duplicated.
assert_eq!(update(&mut state, Action::RefreshPodcasts), None);
update(&mut state, Action::MoveDown);
state.podcasts.episodes = None;
assert_eq!(
update(&mut state, Action::Select),
Some(Effect::PodcastLoadEpisodes(3))
);
assert_eq!(state.podcasts.opened.map(|opened| opened.id), Some(3));
assert!(matches!(
state.podcasts.episodes,
Some((3, Loadable::Loading))
));
}
#[test]
fn unsubscribe_asks_for_confirmation_from_the_list_only() {
use crate::app::state::{DeleteTarget, PODCAST_ACTION_ROWS, Popup};
let mut state = podcasts_state();
assert!(update(&mut state, Action::DeleteSelected).is_none());
assert!(state.popup.is_none());
update(&mut state, Action::Back);
state.podcasts.selected = PODCAST_ACTION_ROWS;
update(&mut state, Action::DeleteSelected);
assert!(matches!(
state.popup,
Some(Popup::ConfirmDelete {
target: DeleteTarget::Podcast(3),
..
})
));
}
#[test]
fn podcast_cursor_stays_inside_the_filtered_list() {
let mut state = podcasts_state();
update(&mut state, Action::SelectLast);
assert_eq!(state.podcasts.opened.unwrap().cursor, 3);
podcasts_set_filter(&mut state, "выпуск".into());
assert_eq!(state.podcasts.opened.unwrap().cursor, 0);
update(&mut state, Action::PageDown);
assert_eq!(state.podcasts.opened.unwrap().cursor, 2);
}
#[test]
fn unchanged_podcast_filter_keeps_the_episode_cursor() {
let mut state = podcasts_state();
podcasts_set_filter(&mut state, "выпуск".into());
state.podcasts.opened.as_mut().unwrap().cursor = 2;
podcasts_set_filter(&mut state, "выпуск".into());
assert_eq!(state.podcasts.opened.unwrap().cursor, 2);
}
+10 -1
View File
@@ -9,7 +9,7 @@
# command: an Action name, optionally with parameters:
# command = { SeekForward = { seconds = 30 } }
# context: optional view filter — global (default), library, search,
# playlists, queue, logs.
# playlists, queue, settings, logs, podcasts.
[[keymaps]]
key_sequence = "q"
@@ -51,6 +51,10 @@ command = { GoToTab = 3 }
key_sequence = "5"
command = { GoToTab = 4 }
[[keymaps]]
key_sequence = "6"
command = { GoToTab = 5 }
[[keymaps]]
key_sequence = "y"
command = "DownloadSelected"
@@ -255,3 +259,8 @@ command = "OpenCommandLine"
[[keymaps]]
key_sequence = "/"
command = "OpenSearch"
[[keymaps]]
key_sequence = "shift-r"
command = "RefreshPodcasts"
context = "podcasts"
+2
View File
@@ -23,6 +23,7 @@ pub enum KeyContext {
#[serde(rename = "settings", alias = "federation")]
Federation,
Logs,
Podcasts,
}
impl KeyContext {
@@ -35,6 +36,7 @@ impl KeyContext {
KeyContext::Queue => "queue",
KeyContext::Federation => "settings",
KeyContext::Logs => "logs",
KeyContext::Podcasts => "podcasts",
}
}
}
+45 -44
View File
@@ -2,50 +2,9 @@ use anyhow::{Context as _, Result};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LibrarySourceMode {
Local,
My,
#[default]
Global,
}
pub use furumi_library::{LibraryFilters, LibrarySourceMode};
impl LibrarySourceMode {
pub fn label(self) -> &'static str {
match self {
LibrarySourceMode::Local => "Local",
LibrarySourceMode::My => "My",
LibrarySourceMode::Global => "Global",
}
}
pub fn includes_network(self) -> bool {
!matches!(self, LibrarySourceMode::Local)
}
pub fn includes_global_peers(self) -> bool {
matches!(self, LibrarySourceMode::Global)
}
pub fn next(self) -> Self {
match self {
LibrarySourceMode::Local => LibrarySourceMode::My,
LibrarySourceMode::My => LibrarySourceMode::Global,
LibrarySourceMode::Global => LibrarySourceMode::Local,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct LibraryFilters {
#[serde(default)]
pub hide_featured_only: bool,
#[serde(default)]
pub source_mode: LibrarySourceMode,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SimilaritySettings {
/// Local embedding/search master switch. Network participation follows
/// federation and additionally requires the explicit privacy consent.
@@ -57,6 +16,14 @@ pub struct SimilaritySettings {
pub profile: String,
#[serde(default = "default_similarity_workers")]
pub workers: usize,
/// Requester-side cosine score floor. This is search policy, not part of
/// the embedding profile, so changing it never invalidates vectors.
#[serde(default = "default_similarity_minimum_score")]
pub minimum_score: f32,
/// Requester-side diversity cap applied independently to local and
/// federated candidates.
#[serde(default = "default_similarity_max_tracks_per_artist")]
pub max_tracks_per_artist: usize,
#[serde(default)]
pub federation_consent: bool,
/// Exact fingerprint of the last fully usable profile. Keeping this
@@ -73,14 +40,18 @@ impl Default for SimilaritySettings {
model: default_similarity_model(),
profile: default_similarity_profile(),
workers: default_similarity_workers(),
minimum_score: default_similarity_minimum_score(),
max_tracks_per_artist: default_similarity_max_tracks_per_artist(),
federation_consent: false,
active_profile: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AppSettings {
#[serde(default)]
pub playback: music_dht::playback::Config,
#[serde(default = "default_volume")]
pub volume: u8,
#[serde(default)]
@@ -95,6 +66,7 @@ pub struct AppSettings {
impl Default for AppSettings {
fn default() -> Self {
Self {
playback: music_dht::playback::Config::default(),
volume: default_volume(),
library: LibraryFilters::default(),
music_dir: default_music_dir(),
@@ -116,6 +88,11 @@ impl AppSettings {
self.similarity.profile = default_similarity_profile();
}
self.similarity.workers = self.similarity.workers.clamp(1, 16);
if !self.similarity.minimum_score.is_finite() {
self.similarity.minimum_score = default_similarity_minimum_score();
}
self.similarity.minimum_score = self.similarity.minimum_score.clamp(0.0, 1.0);
self.similarity.max_tracks_per_artist = self.similarity.max_tracks_per_artist.clamp(1, 50);
self
}
}
@@ -138,6 +115,14 @@ fn default_similarity_workers() -> usize {
.unwrap_or(1)
}
fn default_similarity_minimum_score() -> f32 {
0.70
}
fn default_similarity_max_tracks_per_artist() -> usize {
5
}
/// The historical permanent-download location, kept as the default for
/// backward compatibility with existing installations.
pub fn default_music_dir() -> PathBuf {
@@ -204,5 +189,21 @@ hide_featured_only = true
assert!(settings.library.hide_featured_only);
assert_eq!(settings.library.source_mode, LibrarySourceMode::Global);
assert_eq!(settings.music_dir, default_music_dir());
assert_eq!(settings.similarity.minimum_score, 0.70);
assert_eq!(settings.similarity.max_tracks_per_artist, 5);
}
#[test]
fn similarity_search_policy_is_normalized_without_changing_the_profile() {
let mut settings = AppSettings::default();
let profile = settings.similarity.profile.clone();
settings.similarity.minimum_score = f32::NAN;
settings.similarity.max_tracks_per_artist = 0;
let settings = settings.normalized();
assert_eq!(settings.similarity.minimum_score, 0.70);
assert_eq!(settings.similarity.max_tracks_per_artist, 1);
assert_eq!(settings.similarity.profile, profile);
}
}
+250 -48
View File
@@ -5,6 +5,7 @@
//! materialized tables, so offline clients can merge likes, playlists and
//! membership changes deterministically.
use music_dht::playback::{Checkpoint, CommandStamp, Config as PlaybackConfig, Engine};
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
use std::str::FromStr;
@@ -24,7 +25,7 @@ use crate::library::models::{ArtistRef, TrackItem};
pub const SYNC_ALPN: &[u8] = b"furumi/sync/2";
const CLIENT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const PROTOCOL_VERSION: u16 = 2;
pub const PROTOCOL_VERSION: u16 = 3;
const INVITE_TTL_MS: i64 = 10 * 60 * 1000;
const PAIRING_WAIT_MS: i64 = 5 * 60 * 1000;
const PAIRING_RETRY_DELAY: Duration = Duration::from_secs(1);
@@ -81,6 +82,10 @@ pub struct DeviceSyncStatus {
#[derive(Clone)]
pub struct DeviceSync {
// A device identity has exactly one coordinator, including across binaries
// launched from different installation directories. The OS releases this
// lock on crashes; the file itself is deliberately never removed.
_identity_lock: Option<Arc<std::fs::File>>,
conn: Arc<std::sync::Mutex<Connection>>,
library: Arc<Library>,
event_tx: Arc<std::sync::Mutex<Option<tokio::sync::mpsc::UnboundedSender<AppEvent>>>>,
@@ -126,6 +131,12 @@ pub struct PlaybackTrack {
pub fed: Option<SyncedFedTrack>,
}
/// The part of a track's `file_path` that may cross devices: only http(s)
/// stream URLs (podcast episodes), never local filesystem paths.
fn portable_remote_url(file_path: &str) -> Option<String> {
crate::podcasts::is_remote_url(file_path).then(|| file_path.to_string())
}
impl PlaybackTrack {
fn portable_placeholder_id(&self) -> i64 {
let key = self
@@ -178,7 +189,9 @@ impl PlaybackTrack {
release_id: track.release_id,
release_title: track.release_title.clone(),
release_year: track.release_year,
file_path: String::new(),
// Local paths stay private; a streamed podcast episode's URL is
// the only way another device can play it.
file_path: portable_remote_url(&track.file_path).unwrap_or_default(),
content_id: track
.content_id
.clone()
@@ -203,8 +216,16 @@ impl PlaybackTrack {
})
.collect()
};
// A podcast episode keeps its episode-range id and stream URL, so the
// queue entry stays the same track across devices and round trips.
let episode_url = portable_remote_url(&self.file_path)
.filter(|_| crate::podcasts::episode_id_from_track_id(self.id).is_some());
TrackItem {
id: self.portable_placeholder_id(),
id: if episode_url.is_some() {
self.id
} else {
self.portable_placeholder_id()
},
title: self.title.clone(),
track_number: self.track_number,
disc_number: self.disc_number,
@@ -214,7 +235,7 @@ impl PlaybackTrack {
release_id: self.release_id,
release_title: self.release_title.clone(),
release_year: self.release_year,
file_path: String::new(),
file_path: episode_url.unwrap_or_default(),
content_id: self.content_id.clone(),
cover_path: None,
audio_format: self.audio_format.clone(),
@@ -245,32 +266,13 @@ pub struct PlaybackStateWire {
pub repeat: PlaybackRepeat,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PlaybackSnapshot {
pub device_id: String,
pub device_name: String,
pub active: bool,
pub updated_at_ms: i64,
pub state: PlaybackStateWire,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum PlaybackCommand {
SetState {
state: PlaybackStateWire,
#[serde(default)]
seek: bool,
},
ActiveChanged {
active_device_id: String,
active_device_name: String,
state: PlaybackStateWire,
},
}
pub type PlaybackSnapshot = music_dht::playback::Snapshot<PlaybackStateWire>;
pub type PlaybackCommand = music_dht::playback::Command<PlaybackStateWire>;
#[derive(Debug, Clone, Default)]
struct PlaybackShared {
engine: Option<Engine>,
engine_group: String,
local: Option<PlaybackSnapshot>,
remote: BTreeMap<String, PlaybackSnapshot>,
}
@@ -369,6 +371,8 @@ pub enum SyncOpPayload {
PlaybackCommand {
target_device_id: String,
command: PlaybackCommand,
#[serde(default)]
authority: Option<CommandStamp>,
},
ListenRecorded {
event: ListenEvent,
@@ -615,16 +619,33 @@ fn default_db_path() -> PathBuf {
.unwrap_or_else(|| PathBuf::from("devices").join("sync.sqlite3"))
}
fn acquire_identity_lock(database: &std::path::Path) -> Result<std::fs::File> {
let path = database.with_extension("lock");
let file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(&path)
.with_context(|| format!("opening device identity lock {}", path.display()))?;
file.try_lock().with_context(|| format!(
"Cannot acquire device identity lock {}. Another Furumi instance may already be using this device. Close it before launching another binary; two players must not share one device identity.", path.display()
))?;
Ok(file)
}
impl DeviceSync {
pub fn new(library: Arc<Library>) -> Result<Arc<Self>> {
let path = default_db_path();
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let identity_lock = acquire_identity_lock(&path)?;
let conn =
Connection::open(&path).with_context(|| format!("opening {}", path.display()))?;
init_schema(&conn)?;
let sync = Arc::new(Self {
_identity_lock: Some(Arc::new(identity_lock)),
conn: Arc::new(std::sync::Mutex::new(conn)),
library,
event_tx: Arc::new(std::sync::Mutex::new(None)),
@@ -648,10 +669,92 @@ impl DeviceSync {
format!("{}-{}", now_ms(), random_hex(12))
}
pub fn configure_playback(&self, config: PlaybackConfig) -> Result<()> {
set_meta(
&lock(&self.conn),
"playback_config_v1",
&serde_json::to_string(&config)?,
)?;
lock(&self.playback).engine = None;
Ok(())
}
fn with_playback_engine<R>(&self, f: impl FnOnce(&mut Engine) -> R) -> Result<R> {
let identity = self.ensure_identity()?;
let mut shared = lock(&self.playback);
if shared.engine.is_none() || shared.engine_group != identity.group_id {
shared.engine_group = identity.group_id.clone();
let conn = lock(&self.conn);
let durable = get_meta(&conn, "playback_coordination_v1")?
.map(|json| serde_json::from_str::<Checkpoint>(&json))
.transpose()?
.filter(|checkpoint| checkpoint.scope == identity.group_id)
.map(|checkpoint| checkpoint.state)
.unwrap_or_default();
let config = get_meta(&conn, "playback_config_v1")?
.map(|json| serde_json::from_str::<PlaybackConfig>(&json))
.transpose()?
.unwrap_or_default();
shared.engine = Some(Engine::new(
identity.device_id,
config,
durable,
playback_clock(),
));
}
let engine = shared.engine.as_mut().expect("initialized playback engine");
let previous = engine.clone();
let result = f(engine);
if previous.durable() != engine.durable() {
let saved = set_meta(
&lock(&self.conn),
"playback_coordination_v1",
&serde_json::to_string(&Checkpoint {
scope: identity.group_id,
state: engine.durable().clone(),
})?,
);
if let Err(error) = saved {
*engine = previous;
return Err(error);
}
}
Ok(result)
}
/// Evaluate shared ownership independently of the UI's online-device list.
pub fn playback_tick(&self, available: bool, playing: bool) -> Result<Option<String>> {
self.with_playback_engine(|engine| {
engine.set_output(available, playing);
engine.heartbeat(playback_clock());
engine.tick(playback_clock());
engine.owner().map(str::to_string)
})
}
pub fn playback_command_is_current(&self, origin: &str, stamp: &CommandStamp) -> bool {
self.with_playback_engine(|engine| engine.command_is_current(origin, stamp))
.unwrap_or(false)
}
pub fn claim_playback(&self, target: &str) -> Result<()> {
self.with_playback_engine(|engine| engine.transfer(target, playback_clock()))?
.then_some(())
.context("cannot allocate playback ownership term")
}
pub fn publish_playback(&self, mut snapshot: PlaybackSnapshot) {
if snapshot.updated_at_ms <= 0 {
snapshot.updated_at_ms = now_ms();
}
let Ok(coordination) = self.with_playback_engine(|engine| engine.announcement()) else {
return;
};
snapshot.active = coordination
.claim
.as_ref()
.is_some_and(|claim| claim.owner == snapshot.device_id);
snapshot.coordination = Some(coordination);
lock(&self.playback).local = Some(snapshot);
}
@@ -663,9 +766,22 @@ impl DeviceSync {
if target_device_id.trim().is_empty() {
return Ok(());
}
let authority = self
.with_playback_engine(|engine| {
if let PlaybackCommand::ActiveChanged {
active_device_id, ..
} = &command
&& engine.owner() != Some(active_device_id.as_str())
{
engine.transfer(active_device_id, playback_clock());
}
engine.stamp()
})?
.context("no playback owner; wait for discovery or select an output")?;
self.record_local_op(SyncOpPayload::PlaybackCommand {
target_device_id: target_device_id.to_string(),
command,
authority: Some(authority),
})
}
@@ -845,7 +961,7 @@ impl DeviceSync {
if let Some(profile) = profile {
self.apply_device_profile(&profile, true)?;
if let Some(playback) = playback {
self.apply_playback_snapshot(playback)?;
self.apply_playback_snapshot(&profile.device_id, playback)?;
}
}
self.apply_device_profiles(&devices)?;
@@ -1305,7 +1421,7 @@ impl DeviceSync {
} => {
self.apply_device_profiles(&devices)?;
if let Some(playback) = playback {
self.apply_playback_snapshot(playback)?;
self.apply_playback_snapshot(&device.device_id, playback)?;
}
self.apply_snapshot(snapshot)?;
self.apply_ops(ops)?;
@@ -1636,8 +1752,15 @@ impl DeviceSync {
SyncOpPayload::PlaybackCommand {
target_device_id,
command,
authority,
} => {
self.apply_playback_command(target_device_id, command, &op.op_id)?;
self.apply_playback_command(
target_device_id,
command,
authority.as_ref(),
&op.origin_device_id,
&op.op_id,
)?;
false
}
SyncOpPayload::ListenRecorded { event } => self
@@ -1651,10 +1774,32 @@ impl DeviceSync {
&self,
target_device_id: &str,
command: &PlaybackCommand,
authority: Option<&CommandStamp>,
origin: &str,
op_id: &str,
) -> Result<()> {
let identity = self.ensure_identity()?;
if target_device_id != identity.device_id {
// Legacy commands still replicate with the library log, but cannot
// override the versioned personal-playback protocol.
let Some(authority) = authority else {
return Ok(());
};
let handoff = matches!(command, PlaybackCommand::ActiveChanged { .. });
if !handoff && target_device_id != identity.device_id {
return Ok(());
}
if let PlaybackCommand::ActiveChanged {
active_device_id, ..
} = command
&& active_device_id != &authority.claim.owner
{
return Ok(());
}
let accepted = self.with_playback_engine(|engine| {
engine.accept_command(origin, authority, handoff, playback_clock())
&& (handoff || engine.is_owner())
})?;
if !accepted || target_device_id != identity.device_id {
return Ok(());
}
let inserted = {
@@ -1669,7 +1814,11 @@ impl DeviceSync {
return Ok(());
}
if let Some(tx) = lock(&self.event_tx).as_ref() {
let _ = tx.send(AppEvent::PlaybackCommand(command.clone()));
let _ = tx.send(AppEvent::PlaybackCommand {
command: command.clone(),
authority: authority.clone(),
origin: origin.to_string(),
});
}
Ok(())
}
@@ -2595,19 +2744,29 @@ impl DeviceSync {
lock(&self.playback).local.clone()
}
fn apply_playback_snapshot(&self, snapshot: PlaybackSnapshot) -> Result<()> {
fn apply_playback_snapshot(&self, sender: &str, snapshot: PlaybackSnapshot) -> Result<()> {
let identity = self.ensure_identity()?;
if snapshot.device_id == identity.device_id {
if snapshot.device_id != sender || snapshot.device_id == identity.device_id {
return Ok(());
}
let Some(coordination) = &snapshot.coordination else {
return Ok(());
};
if !self.with_playback_engine(|engine| {
engine.observe(&snapshot.device_id, coordination, playback_clock())
})? {
return Ok(());
}
let should_send = {
let mut playback = lock(&self.playback);
let changed = playback
.remote
.get(&snapshot.device_id)
.is_none_or(|current| snapshot.updated_at_ms > current.updated_at_ms);
let mut shared = lock(&self.playback);
let changed = shared.remote.get(&snapshot.device_id).is_none_or(|old| {
old.coordination.as_ref().is_none_or(|c| {
coordination.claim > c.claim
|| (coordination.claim == c.claim && coordination.heartbeat > c.heartbeat)
})
});
if changed {
playback
shared
.remote
.insert(snapshot.device_id.clone(), snapshot.clone());
}
@@ -2707,13 +2866,45 @@ pub async fn sync_loop(
transport_stats: Arc<crate::federation::TransportStats>,
) {
let mut interval = tokio::time::interval(SYNC_INTERVAL);
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
// Independent polls: an offline device must not hold up live outputs.
// JoinSet aborts outstanding IO when federation stops or restarts.
let mut polls = tokio::task::JoinSet::new();
let mut active = std::collections::HashMap::new();
loop {
interval.tick().await;
if let Err(err) = sync
.sync_once(Arc::clone(&service), Arc::clone(&transport_stats))
.await
{
tracing::debug!("personal sync tick failed: {err:#}");
tokio::select! {
_ = interval.tick() => {
match sync.active_remote_devices() {
Ok(devices) => for device in devices {
if device.endpoint_ticket.trim().is_empty()
|| active.values().any(|id| id == &device.device_id) { continue; }
let device_id = device.device_id.clone();
let sync = Arc::clone(&sync);
let service = Arc::clone(&service);
let stats = Arc::clone(&transport_stats);
let handle = polls.spawn(async move {
tokio::time::timeout(Duration::from_secs(30), sync.sync_device(service, &device, stats))
.await.context("device sync exchange timed out")?
});
active.insert(handle.id(), device_id);
},
Err(err) => { let _ = sync.set_last_error(Some(format!("{err:#}"))); }
}
if let Err(err) = sync.gc_tombstones() {
tracing::debug!("personal sync cleanup failed: {err:#}");
}
}
Some(completed) = polls.join_next_with_id(), if !polls.is_empty() => {
let (task, result) = match completed {
Ok((task, result)) => (task, result),
Err(error) => (error.id(), Err(anyhow::Error::from(error))),
};
if let Some(device_id) = active.remove(&task)
&& let Err(err) = result {
tracing::debug!(device = %device_id, "device sync failed: {err:#}");
let _ = sync.set_last_error(Some(format!("{}: {err:#}", short_id(&device_id))));
}
}
}
if let Some(tx) = lock(&sync.event_tx).as_ref() {
let _ = tx.send(AppEvent::DeviceSyncStatus(sync.status()));
@@ -2936,7 +3127,7 @@ async fn handle_pair_request(
}
sync.apply_device_profile(&profile, true)?;
if let Some(playback) = playback {
sync.apply_playback_snapshot(playback)?;
sync.apply_playback_snapshot(&profile.device_id, playback)?;
}
sync.apply_snapshot(snapshot)?;
sync.apply_ops(ops)?;
@@ -3026,7 +3217,7 @@ async fn handle_hello(
sync.apply_device_profile(&profile, false)?;
sync.apply_device_profiles(&devices)?;
if let Some(playback) = playback {
sync.apply_playback_snapshot(playback)?;
sync.apply_playback_snapshot(&profile.device_id, playback)?;
}
sync.apply_snapshot(snapshot)?;
sync.apply_ops(ops)?;
@@ -3635,3 +3826,14 @@ fn base64url_decode(value: &str) -> Result<Vec<u8>> {
#[cfg(test)]
#[path = "devices/tests.rs"]
mod tests;
#[cfg(test)]
mod interop_tests;
fn playback_clock() -> u64 {
static START: std::sync::OnceLock<std::time::Instant> = std::sync::OnceLock::new();
START
.get_or_init(std::time::Instant::now)
.elapsed()
.as_millis() as u64
}
+133
View File
@@ -0,0 +1,133 @@
//! Cross-binary wire test. Run with scripts/test_device_interop.py; the other
//! endpoint is compiled from furumusic's real protocol types and player hub.
use super::*;
use tokio::io::AsyncWriteExt;
#[tokio::test]
#[ignore = "run scripts/test_device_interop.py to start both player test binaries"]
async fn localhost_web_peer() {
tokio::time::timeout(Duration::from_secs(30), async {
let dir = PathBuf::from(std::env::var("FURUMI_INTEROP_DIR").expect("interop runner"));
let address = loop {
if let Ok(address) = std::fs::read_to_string(dir.join("web-address")) {
break address;
}
tokio::time::sleep(Duration::from_millis(20)).await;
};
let mut stream = tokio::net::TcpStream::connect(address.trim())
.await
.unwrap();
let conn = Connection::open_in_memory().unwrap();
init_schema(&conn).unwrap();
let sync = DeviceSync {
_identity_lock: None,
conn: Arc::new(std::sync::Mutex::new(conn)),
library: Arc::new(Library::open(&dir.join("interop-library.sqlite3")).unwrap()),
event_tx: Default::default(),
playback: Default::default(),
};
let id = sync.ensure_identity().unwrap();
let (tx, mut events) = tokio::sync::mpsc::unbounded_channel();
sync.set_event_tx(tx);
sync.claim_playback(&id.device_id).unwrap();
sync.playback_tick(true, true).unwrap();
let mut state: PlaybackStateWire = serde_json::from_value(serde_json::json!({
"queue": [{ "id": 7, "title": "Interop track", "duration_seconds": 123.5,
"release_id": 1, "release_title": "Interop album", "artist_names": ["Interop artist"],
"content_id": "b3:0000000000000000000000000000000000000000000000000000000000000000" }],
"queue_pos": 0, "playing": true, "paused": false,
"position_secs": 42.5, "volume": 73, "shuffle": true, "repeat": "all"
}))
.unwrap();
for phase in 0..3 {
sync.publish_playback(PlaybackSnapshot {
device_id: id.device_id.clone(),
device_name: "TUI interop".into(),
active: true,
updated_at_ms: now_ms(),
state: state.clone(),
coordination: None,
});
let hello = WireMessage::Hello {
group_id: id.group_id.clone(),
profile: sync.own_profile("").unwrap(),
devices: vec![],
vector: BTreeMap::new(),
ops: vec![],
snapshot: SyncSnapshot::default(),
playback: sync.local_playback_snapshot(),
};
let mut bytes = serde_json::to_vec(&hello).unwrap();
bytes.push(b'\n');
stream.write_all(&bytes).await.unwrap();
let response: WireMessage =
serde_json::from_slice(&read_line(&mut stream).await.unwrap()).unwrap();
let WireMessage::SyncResponse {
accepted: true,
playback: Some(snapshot),
ops,
..
} = response
else {
panic!("expected web response")
};
let web_id = snapshot.device_id.clone();
sync.apply_playback_snapshot(&web_id, snapshot).unwrap();
assert_eq!(ops.len(), 1);
let op = &ops[0];
// Production command adapter including durable fencing/deduplication.
sync.apply_op(op).unwrap();
sync.apply_op(op).unwrap();
let mut commands = Vec::new();
while let Ok(event) = events.try_recv() {
if let AppEvent::PlaybackCommand {
command,
authority,
origin,
} = event
{
assert!(sync.playback_command_is_current(&origin, &authority));
commands.push(command);
}
}
assert_eq!(commands.len(), 1, "one event even after duplicate delivery");
match commands.pop().unwrap() {
PlaybackCommand::ActiveChanged {
active_device_id,
state: next,
..
} => {
assert_eq!(
active_device_id,
if phase == 0 {
web_id.clone()
} else {
id.device_id.clone()
}
);
assert_eq!(next.position_secs, 42.5);
assert_eq!(next.queue, state.queue);
state = next;
}
PlaybackCommand::SetState { state: next, seek } => {
assert_eq!(phase, 2);
assert!(seek);
assert!(next.paused);
assert_eq!(next.position_secs, 87.0);
state = next;
}
}
assert_eq!(
sync.playback_tick(true, false).unwrap(),
Some(if phase == 0 {
web_id
} else {
id.device_id.clone()
})
);
}
stream.write_all(b"ok\n").await.unwrap();
})
.await
.expect("web/TUI exchange timed out");
}
+437 -6
View File
@@ -1,5 +1,329 @@
use super::*;
/// Exercises the production stream handlers and SQLite adapter, not just the
/// ownership reducer. Each peer has a fresh identity and an isolated library.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn localhost_devices_exchange_state_and_handoff() {
tokio::time::timeout(Duration::from_secs(30), async {
let dirs = [tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap()];
let network = NetworkId::from_name(&format!("device-test-{}", random_hex(16)));
let mut peers = Vec::new();
let mut servers = Vec::new();
let mut receivers = Vec::new();
for dir in &dirs {
std::fs::create_dir_all(dir.path().join("network")).unwrap();
let config = music_dht::MusicDhtConfig::builder()
.data_dir(dir.path().join("network"))
.network_id(network)
.stream_protocol(SYNC_ALPN)
.build()
.unwrap();
let (service, events) = MusicDhtService::start(config).await.unwrap();
let service = Arc::new(service);
let conn = Connection::open_in_memory().unwrap();
init_schema(&conn).unwrap();
let sync = Arc::new(DeviceSync {
_identity_lock: Some(Arc::new(
acquire_identity_lock(&dir.path().join("sync.sqlite3")).unwrap(),
)),
conn: Arc::new(std::sync::Mutex::new(conn)),
library: Arc::new(Library::open(&dir.path().join("library.sqlite3")).unwrap()),
event_tx: Default::default(),
playback: Default::default(),
});
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
sync.set_event_tx(tx);
let stats = Arc::new(crate::federation::TransportStats::default());
servers.push(tokio::spawn(serve_peers(
service.stream_acceptor(SYNC_ALPN).unwrap(),
sync.clone(),
service.clone(),
stats.clone(),
)));
receivers.push(rx);
peers.push((sync, service, stats, events));
}
let (a, service_a, stats_a, _) = &peers[0];
let (b, service_b, stats_b, _) = &peers[1];
b.ensure_identity().unwrap();
b.set_group_id(&a.ensure_identity().unwrap().group_id)
.unwrap();
let profile_a = a
.own_profile(&service_a.ticket().await.unwrap().to_string())
.unwrap();
let profile_b = b
.own_profile(&service_b.ticket().await.unwrap().to_string())
.unwrap();
a.apply_device_profile(&profile_b, true).unwrap();
b.apply_device_profile(&profile_a, true).unwrap();
a.claim_playback(&profile_a.device_id).unwrap();
a.playback_tick(true, true).unwrap();
let mut state = empty_playback_state();
state.playing = true;
state.position_secs = 42.5;
a.publish_playback(PlaybackSnapshot {
device_id: profile_a.device_id.clone(),
device_name: "TUI".into(),
active: true,
updated_at_ms: now_ms(),
state: state.clone(),
coordination: None,
});
a.sync_device(
service_a.clone(),
&a.active_remote_devices().unwrap()[0],
stats_a.clone(),
)
.await
.unwrap();
assert_eq!(
b.with_playback_engine(|e| e.owner().map(str::to_owned))
.unwrap(),
Some(profile_a.device_id.clone())
);
assert_eq!(lock(&b.playback).remote[&profile_a.device_id].state, state);
assert!(
b.status()
.devices
.iter()
.any(|d| d.device_id == profile_a.device_id && d.last_seen_ms.is_some())
);
a.record_playback_command(
&profile_b.device_id,
PlaybackCommand::ActiveChanged {
active_device_id: profile_b.device_id.clone(),
active_device_name: "Second player".into(),
state: state.clone(),
},
)
.unwrap();
a.sync_device(
service_a.clone(),
&a.active_remote_devices().unwrap()[0],
stats_a.clone(),
)
.await
.unwrap();
let mut transferred = false;
while let Ok(event) = receivers[1].try_recv() {
if let AppEvent::PlaybackCommand {
command:
PlaybackCommand::ActiveChanged {
state: received, ..
},
authority,
origin,
} = event
{
assert_eq!(received, state);
assert!(b.playback_command_is_current(&origin, &authority));
transferred = true;
}
}
assert!(transferred, "handoff must reach the player's event loop");
assert_eq!(
b.playback_tick(true, true).unwrap(),
Some(profile_b.device_id.clone())
);
b.publish_playback(PlaybackSnapshot {
device_id: profile_b.device_id.clone(),
device_name: "Second player".into(),
active: true,
updated_at_ms: now_ms(),
state,
coordination: None,
});
b.sync_device(
service_b.clone(),
&b.active_remote_devices().unwrap()[0],
stats_b.clone(),
)
.await
.unwrap();
assert_eq!(
a.playback_tick(true, false).unwrap(),
Some(profile_b.device_id)
);
// A paired peer that accepts a connection but never answers must not
// serialize or stop subsequent polls to the responsive peer.
let silent_dir = tempfile::tempdir().unwrap();
let (silent, _events) = MusicDhtService::start(
music_dht::MusicDhtConfig::builder()
.data_dir(silent_dir.path())
.network_id(network)
.stream_protocol(SYNC_ALPN)
.build()
.unwrap(),
)
.await
.unwrap();
let _silent_acceptor = silent.stream_acceptor(SYNC_ALPN).unwrap();
let mut silent_profile = profile_a.clone();
silent_profile.device_id = "silent-peer".into();
silent_profile.endpoint_id = silent.endpoint_id().to_string();
silent_profile.endpoint_ticket = silent.ticket().await.unwrap().to_string();
a.apply_device_profile(&silent_profile, true).unwrap();
lock(&a.conn)
.execute(
"UPDATE sync_devices SET last_seen_ms = ?1 WHERE device_id = 'silent-peer'",
[now_ms() + 1_000],
)
.unwrap();
assert_eq!(
a.active_remote_devices().unwrap()[0].device_id,
"silent-peer"
);
a.claim_playback(&profile_a.device_id).unwrap();
let poller = tokio::spawn(sync_loop(a.clone(), service_a.clone(), stats_a.clone()));
for position in [99.0, 100.0] {
a.playback_tick(true, true).unwrap();
let mut next = empty_playback_state();
next.playing = true;
next.position_secs = position;
a.publish_playback(PlaybackSnapshot {
device_id: profile_a.device_id.clone(),
device_name: "TUI".into(),
active: true,
updated_at_ms: now_ms(),
state: next,
coordination: None,
});
tokio::time::timeout(Duration::from_secs(6), async {
loop {
if lock(&b.playback)
.remote
.get(&profile_a.device_id)
.is_some_and(|snapshot| snapshot.state.position_secs == position)
{
break;
}
tokio::time::sleep(Duration::from_millis(20)).await;
}
})
.await
.expect("a stalled peer must not delay live playback polls");
}
poller.abort();
let _ = poller.await;
silent.shutdown().await.unwrap();
for server in servers {
server.abort();
}
for (_, service, _, _) in &peers {
service.shutdown().await.unwrap();
}
})
.await
.expect("localhost sync must complete within 30 seconds");
}
fn empty_playback_state() -> PlaybackStateWire {
PlaybackStateWire {
queue: vec![],
queue_pos: 0,
playing: false,
paused: false,
idle_since_ms: None,
position_secs: 0.0,
volume: 80,
shuffle: false,
repeat: PlaybackRepeat::Off,
}
}
#[test]
fn coordination_ignores_legacy_commands_and_wrong_snapshot_sender() {
let sync = test_sync();
let identity = sync.ensure_identity().unwrap();
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
sync.set_event_tx(tx);
let command = PlaybackCommand::SetState {
state: empty_playback_state(),
seek: false,
};
sync.apply_playback_command(&identity.device_id, &command, None, "remote", "legacy")
.unwrap();
assert!(rx.try_recv().is_err());
let mut remote = Engine::new(
"remote".into(),
PlaybackConfig::default(),
Default::default(),
0,
);
remote.transfer("remote", 0);
remote.set_output(true, true);
remote.heartbeat(1);
let snapshot = PlaybackSnapshot {
device_id: "remote".into(),
device_name: "Remote".into(),
active: true,
updated_at_ms: now_ms(),
state: empty_playback_state(),
coordination: Some(remote.announcement()),
};
sync.apply_playback_snapshot("different-sender", snapshot.clone())
.unwrap();
assert!(
sync.with_playback_engine(|engine| engine.owner().is_none())
.unwrap()
);
sync.apply_playback_snapshot("remote", snapshot).unwrap();
assert_eq!(
sync.playback_tick(true, false).unwrap().as_deref(),
Some("remote")
);
sync.publish_playback(PlaybackSnapshot {
device_id: identity.device_id,
device_name: identity.name,
active: false,
updated_at_ms: now_ms(),
state: empty_playback_state(),
coordination: None,
});
let gossip = sync
.local_playback_snapshot()
.unwrap()
.coordination
.unwrap();
assert_eq!(gossip.claim.unwrap().owner, "remote");
}
#[test]
fn checkpoint_is_not_reused_after_changing_trusted_group() {
let sync = test_sync();
sync.claim_playback("old-group-owner").unwrap();
sync.set_group_id("new-test-group").unwrap();
assert!(
sync.with_playback_engine(|engine| engine.owner().is_none())
.unwrap()
);
}
#[test]
fn queued_command_loses_its_fence_when_another_owner_wins() {
let sync = test_sync();
let identity = sync.ensure_identity().unwrap();
sync.claim_playback(&identity.device_id).unwrap();
let stamp = sync
.with_playback_engine(|engine| engine.stamp().unwrap())
.unwrap();
sync.apply_playback_command(
&identity.device_id,
&PlaybackCommand::SetState {
state: empty_playback_state(),
seek: false,
},
Some(&stamp),
&identity.device_id,
"queued",
)
.unwrap();
assert!(sync.playback_command_is_current(&identity.device_id, &stamp));
sync.claim_playback("new-owner").unwrap();
assert!(!sync.playback_command_is_current(&identity.device_id, &stamp));
}
static NEXT_TEST_DB: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
fn test_sync() -> DeviceSync {
@@ -13,6 +337,7 @@ fn test_sync() -> DeviceSync {
unique
));
let sync = DeviceSync {
_identity_lock: None,
conn: Arc::new(std::sync::Mutex::new(conn)),
library: Arc::new(Library::open(&library_path).unwrap()),
event_tx: Arc::new(std::sync::Mutex::new(None)),
@@ -22,6 +347,38 @@ fn test_sync() -> DeviceSync {
sync
}
#[test]
fn device_identity_has_one_coordinator_across_installation_paths() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("sync.sqlite3");
let first = acquire_identity_lock(&path).unwrap();
assert!(acquire_identity_lock(&path).is_err());
let child = std::process::Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"devices::tests::identity_lock_child_process",
"--nocapture",
])
.env("FURUMI_IDENTITY_LOCK_TEST_PATH", &path)
.output()
.unwrap();
assert!(
child.status.success(),
"{}",
String::from_utf8_lossy(&child.stderr)
);
drop(first);
// Reopening a leftover lock file after shutdown must succeed.
assert!(acquire_identity_lock(&path).is_ok());
}
#[test]
fn identity_lock_child_process() {
if let Some(path) = std::env::var_os("FURUMI_IDENTITY_LOCK_TEST_PATH") {
assert!(acquire_identity_lock(std::path::Path::new(&path)).is_err());
}
}
fn device_revoked(sync: &DeviceSync, device_id: &str) -> bool {
let conn = lock(&sync.conn);
conn.query_row(
@@ -212,19 +569,41 @@ fn playback_command_is_targeted_and_deduplicated() {
seek: false,
};
sync.apply_playback_command("dev_other", &command, "op_other")
sync.claim_playback(&identity.device_id).unwrap();
let stamp = sync
.with_playback_engine(|engine| engine.stamp().unwrap())
.unwrap();
sync.apply_playback_command(
"dev_other",
&command,
Some(&stamp),
&identity.device_id,
"op_other",
)
.unwrap();
assert!(rx.try_recv().is_err());
sync.apply_playback_command(&identity.device_id, &command, "op_1")
.unwrap();
sync.apply_playback_command(
&identity.device_id,
&command,
Some(&stamp),
&identity.device_id,
"op_1",
)
.unwrap();
assert!(matches!(
rx.try_recv().unwrap(),
crate::app::event::AppEvent::PlaybackCommand(_)
crate::app::event::AppEvent::PlaybackCommand { .. }
));
sync.apply_playback_command(&identity.device_id, &command, "op_1")
.unwrap();
sync.apply_playback_command(
&identity.device_id,
&command,
Some(&stamp),
&identity.device_id,
"op_1",
)
.unwrap();
assert!(rx.try_recv().is_err());
}
@@ -245,6 +624,7 @@ fn playback_commands_are_caught_up_only_by_their_target_while_fresh() {
},
seek: false,
};
sync.claim_playback("dev_target").unwrap();
sync.record_playback_command("dev_target", command.clone())
.unwrap();
sync.record_playback_command("dev_other", command).unwrap();
@@ -556,3 +936,54 @@ fn stale_synced_playlist_item_metadata_repairs_pending_fed_track() {
assert!(detail.tracks[0].is_fed_pending());
assert_eq!(detail.tracks[0].title, fed.title);
}
#[test]
fn podcast_episode_survives_the_playback_wire_but_local_paths_do_not() {
let podcast = crate::podcasts::Podcast {
id: 1,
feed_url: "https://example.test/feed.xml".into(),
title: "Show".into(),
author: None,
description: None,
link: None,
image_url: None,
cover_path: Some("/home/user/.cache/cover.jpg".into()),
episode_count: 1,
latest_published_at: None,
last_checked_at: None,
last_error: None,
};
let episode = crate::podcasts::Episode {
id: 42,
podcast_id: 1,
guid: "g".into(),
title: "Episode".into(),
description: None,
published_at: None,
duration_seconds: Some(60.0),
audio_url: "https://cdn.example.test/42.mp3".into(),
mime_type: Some("audio/mpeg".into()),
byte_len: None,
link: None,
image_url: None,
};
let track = crate::podcasts::episode_track(&podcast, &episode);
let round_trip = PlaybackTrack::from_track(&track).to_track_item();
assert_eq!(round_trip.id, track.id);
assert_eq!(round_trip.file_path, episode.audio_url);
assert_eq!(
crate::app::state::track_key(&round_trip),
crate::app::state::track_key(&track)
);
// Local files never leak their path; a URL without an episode id is
// not trusted as a stream either.
let mut local = track.clone();
local.id = 7;
local.file_path = "/home/user/Music/a.flac".into();
assert!(PlaybackTrack::from_track(&local).file_path.is_empty());
local.file_path = "https://cdn.example.test/other.mp3".into();
let wire = PlaybackTrack::from_track(&local);
assert_eq!(wire.file_path, "https://cdn.example.test/other.mp3");
assert!(wire.to_track_item().file_path.is_empty());
}
+1
View File
@@ -171,6 +171,7 @@ mod tests {
"catalog",
"audio",
"similarity",
"similarity_dht",
"device_sync",
"jam",
] {
+328 -90
View File
@@ -21,10 +21,12 @@ use std::collections::{HashMap, VecDeque};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
use std::sync::atomic::{AtomicI64, Ordering};
use std::time::Duration;
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering};
use std::time::{Duration, Instant};
use anyhow::{Context, Result};
use music_dht::similarity_dht::SimilarityDht;
use music_dht::similarity_lsh::SIMILARITY_DHT_ALPN;
use music_dht::{
ByteStream, ByteStreamConnectionStats, EndpointId, ItemKind, ItemSpec, LibraryItem,
MusicDhtConfig, MusicDhtService, NetworkId, PeerTicket, PublishStats, RendezvousConfig,
@@ -44,6 +46,11 @@ pub use similarity::SIMILARITY_ALPN;
/// How often the published library is re-synchronized with the local index.
const SYNC_INTERVAL: Duration = Duration::from_secs(60);
/// How often the application checks whether the shared transport needs a
/// full restart so all application-owned protocol acceptors are recreated.
const SUPERVISOR_INTERVAL: Duration = Duration::from_secs(15);
/// Prevents repeated restarts during a prolonged external network outage.
const RECOVERY_COOLDOWN: Duration = Duration::from_secs(5 * 60);
/// How many times a share-link content lookup is retried before the label
/// fallback kicks in.
@@ -336,44 +343,29 @@ pub struct FedSearchResults {
pub tracks: Vec<FedTrack>,
}
#[derive(Debug, Clone)]
pub struct ScoredFedTrack {
pub track: FedTrack,
pub score: f32,
pub embedding_signature: Option<[u8; music_dht::similarity::SIMILARITY_SIGNATURE_BYTES]>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SimilaritySearchStats {
pub tracks: usize,
pub artists: usize,
pub peers_queried: usize,
pub elapsed_ms: u64,
}
#[derive(Debug, Clone, Default)]
pub struct FedSimilaritySearchResults {
pub tracks: Vec<ScoredFedTrack>,
pub stats: SimilaritySearchStats,
}
/// A track found through federated search.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FedTrack {
/// Hex item id in the DHT (the key audio is requested by).
pub item_id: String,
/// Hex endpoint id of the owning peer.
pub owner: String,
/// The item is published by this very instance.
pub own: bool,
pub title: String,
pub artist_names: Vec<String>,
pub featured_artist_names: Vec<String>,
pub year: Option<i32>,
pub duration_seconds: Option<i64>,
/// Stable audio content id (`b3:<64 hex>`) when the owner published it.
pub content_id: Option<String>,
/// Release context, known when the track came from an artist card.
pub release_title: Option<String>,
pub track_number: Option<i32>,
pub disc_number: Option<i32>,
}
impl FedTrack {
pub fn artist_line(&self) -> String {
artist_line(&self.artist_names, &self.featured_artist_names)
}
pub fn owner_short(&self) -> String {
self.owner.chars().take(10).collect()
}
pub fn duration_label(&self) -> String {
match self.duration_seconds {
Some(total) => format!("{}:{:02}", total / 60, total % 60),
None => String::new(),
}
}
}
pub use furumi_library::FederatedTrack as FedTrack;
/// Live status snapshot rendered on the Federation tab.
#[derive(Debug, Clone, Default)]
@@ -383,6 +375,12 @@ pub struct FedStatus {
pub endpoint_id: String,
pub dht_node_id: String,
pub connected_peers: Vec<String>,
pub network_health: String,
pub rendezvous_failures: u32,
pub peer_dial_failures: u32,
pub rendezvous_restarts: u64,
pub recovery_count: u64,
pub last_rendezvous_error: Option<String>,
pub known_contacts: usize,
pub stored_dht_records: Option<usize>,
pub stored_dht_bytes: Option<u64>,
@@ -413,6 +411,7 @@ pub struct NetworkLibrarySource {
struct Running {
service: Arc<MusicDhtService>,
similarity_dht: Arc<SimilarityDht>,
network_name: String,
network_id: NetworkId,
tasks: Vec<tokio::task::JoinHandle<()>>,
@@ -431,6 +430,10 @@ pub struct Federation {
metadata_cache: std::sync::Mutex<std::collections::HashMap<String, CachedTrackMetadata>>,
settings: std::sync::Mutex<FedSettings>,
running: tokio::sync::Mutex<Option<Running>>,
supervisor_task: std::sync::Mutex<Option<tokio::task::JoinHandle<()>>>,
supervisor_shutdown: tokio::sync::Notify,
shutting_down: AtomicBool,
recovery_count: AtomicU64,
last_sync: std::sync::Mutex<Option<String>>,
last_error: std::sync::Mutex<Option<String>>,
transport_stats: Arc<TransportStats>,
@@ -561,6 +564,10 @@ impl Federation {
metadata_cache: std::sync::Mutex::new(Default::default()),
settings: std::sync::Mutex::new(load_settings()),
running: tokio::sync::Mutex::new(None),
supervisor_task: std::sync::Mutex::new(None),
supervisor_shutdown: tokio::sync::Notify::new(),
shutting_down: AtomicBool::new(false),
recovery_count: AtomicU64::new(0),
last_sync: std::sync::Mutex::new(None),
last_error: std::sync::Mutex::new(initial_error),
transport_stats: Arc::new(TransportStats::default()),
@@ -572,6 +579,18 @@ impl Federation {
lock(&self.settings).clone()
}
/// Starts the application-level federation supervisor once.
pub fn start_supervisor(self: &Arc<Self>) {
let mut task = lock(&self.supervisor_task);
if task.is_some() || self.shutting_down.load(Ordering::SeqCst) {
return;
}
let federation = Arc::clone(self);
*task = Some(tokio::spawn(async move {
federation.supervisor_loop().await;
}));
}
pub fn media_dir(&self) -> PathBuf {
lock(&self.media_dir).clone()
}
@@ -677,12 +696,43 @@ impl Federation {
network_id: NetworkId,
network_name: String,
) -> Result<()> {
self.start_with_network_id_mode(network_id, network_name, false)
.await
.map(|_| ())
}
async fn start_with_network_id_mode(
self: &Arc<Self>,
network_id: NetworkId,
network_name: String,
recovery_only: bool,
) -> Result<bool> {
let mut guard = self.running.lock().await;
if recovery_only {
let current = self.settings();
if !current.enabled
|| current.network_id.trim() != network_name
|| NetworkId::from_name(current.network_id.trim()) != network_id
{
return Ok(false);
}
}
if let Some(running) = guard.as_ref() {
if running.network_id == network_id {
return Ok(());
if !recovery_only || !running.service.network_health().restart_recommended {
return Ok(false);
}
tracing::warn!(
network = %network_name,
health = %running.service.network_health().state,
"restarting degraded federation service"
);
} else if recovery_only {
return Ok(false);
}
stop_running(guard.take()).await;
} else if recovery_only {
tracing::warn!(network = %network_name, "retrying stopped federation service");
}
std::fs::create_dir_all(&self.data_dir)
.with_context(|| format!("creating {}", self.data_dir.display()))?;
@@ -697,12 +747,15 @@ impl Federation {
.stream_protocol(AUDIO_ALPN)
// ...and browse each other's per-artist catalogs over this one.
.stream_protocol(CATALOG_ALPN)
// Anonymous, bounded direct embedding queries.
.stream_protocol(SIMILARITY_ALPN)
// Anonymous, bounded direct embedding queries have their own
// versioned contract and survive catalog-schema upgrades.
.schema_independent_stream_protocol(SIMILARITY_ALPN)
// Personal-device sync (likes, playlists, trusted devices).
.stream_protocol(crate::devices::SYNC_ALPN)
// Capability-scoped shared playback control.
.stream_protocol(crate::jam::JAM_ALPN)
// Signed LSH summaries form their own upgrade-safe DHT overlay.
.schema_independent_stream_protocol(SIMILARITY_DHT_ALPN)
// Informational application/protocol versions.
.schema_independent_stream_protocol(capabilities::CAPABILITIES_ALPN)
.build()
@@ -717,6 +770,25 @@ impl Federation {
"federation started"
);
let similarity_dht = SimilarityDht::open(
Arc::clone(&service),
self.data_dir.join("similarity-routing.sqlite3"),
)
.await
.map_err(|err| anyhow::anyhow!("failed to start the similarity DHT: {err}"))?;
let similarity_dht_acceptor = service
.stream_acceptor(SIMILARITY_DHT_ALPN)
.map_err(|err| anyhow::anyhow!("failed to take similarity DHT acceptor: {err}"))?;
let similarity_dht_serve_task =
tokio::spawn(Arc::clone(&similarity_dht).serve(similarity_dht_acceptor));
let similarity_dht_maintenance_task =
tokio::spawn(Arc::clone(&similarity_dht).maintenance());
let similarity_dht_sync_task = tokio::spawn(similarity_route_sync_loop(
Arc::clone(&similarity_dht),
Arc::clone(&self.similarity),
Arc::clone(&self.library),
));
// Drain DHT events into the log; the channel is bounded.
let event_task = tokio::spawn(async move {
while let Some(event) = events.recv().await {
@@ -797,6 +869,7 @@ impl Federation {
*guard = Some(Running {
service,
similarity_dht,
network_name,
network_id,
tasks: vec![
@@ -811,10 +884,13 @@ impl Federation {
jam_poll_task,
capabilities_serve_task,
capabilities_probe_task,
similarity_dht_serve_task,
similarity_dht_maintenance_task,
similarity_dht_sync_task,
],
});
self.set_error(None);
Ok(())
Ok(true)
}
async fn stop(&self) {
@@ -823,9 +899,57 @@ impl Federation {
}
pub async fn shutdown(&self) {
self.shutting_down.store(true, Ordering::SeqCst);
self.supervisor_shutdown.notify_one();
let supervisor = lock(&self.supervisor_task).take();
if let Some(supervisor) = supervisor {
let _ = supervisor.await;
}
self.stop().await;
}
async fn supervisor_loop(self: Arc<Self>) {
let mut interval = tokio::time::interval(SUPERVISOR_INTERVAL);
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
interval.tick().await;
let mut last_attempt = None;
loop {
tokio::select! {
_ = self.supervisor_shutdown.notified() => return,
_ = interval.tick() => {}
}
if self.shutting_down.load(Ordering::SeqCst) {
return;
}
if last_attempt.is_some_and(|attempt: Instant| attempt.elapsed() < RECOVERY_COOLDOWN) {
continue;
}
match self.recover_if_needed().await {
Ok(false) => {}
Ok(true) => {
last_attempt = Some(Instant::now());
self.recovery_count.fetch_add(1, Ordering::Relaxed);
self.spawn_sync_soon().await;
}
Err(err) => {
last_attempt = Some(Instant::now());
tracing::error!(error = %err, "federation recovery failed");
self.set_error(Some(format!("network recovery failed: {err}")));
}
}
}
}
async fn recover_if_needed(self: &Arc<Self>) -> Result<bool> {
let settings = self.settings();
if !settings.enabled || settings.network_id.trim().is_empty() {
return Ok(false);
}
let network_name = settings.network_id.trim().to_string();
self.start_with_network_id_mode(NetworkId::from_name(&network_name), network_name, true)
.await
}
async fn service(&self) -> Result<Arc<MusicDhtService>> {
self.running
.lock()
@@ -835,6 +959,20 @@ impl Federation {
.context("federation is not running")
}
async fn similarity_services(&self) -> Result<(Arc<MusicDhtService>, Arc<SimilarityDht>)> {
self.running
.lock()
.await
.as_ref()
.map(|running| {
(
Arc::clone(&running.service),
Arc::clone(&running.similarity_dht),
)
})
.context("federation is not running")
}
fn ensure_connected_devices_enabled(&self) -> Result<()> {
let settings = self.settings();
anyhow::ensure!(
@@ -947,6 +1085,7 @@ impl Federation {
let guard = self.running.lock().await;
let mut status = FedStatus {
network: settings.network_id,
recovery_count: self.recovery_count.load(Ordering::Relaxed),
last_sync: lock(&self.last_sync).clone(),
last_error: lock(&self.last_error).clone(),
protocols: ProtocolVersions::snapshot(&self.observed_protocols),
@@ -954,6 +1093,7 @@ impl Federation {
};
if let Some(running) = guard.as_ref() {
let service = &running.service;
let health = service.network_health();
status.running = true;
status.network = running.network_name.clone();
status.endpoint_id = service.endpoint_id().to_string();
@@ -963,6 +1103,11 @@ impl Federation {
.iter()
.map(|p| p.to_string())
.collect();
status.network_health = health.state.to_string();
status.rendezvous_failures = health.consecutive_rendezvous_failures;
status.peer_dial_failures = health.consecutive_peer_dial_failures;
status.rendezvous_restarts = health.rendezvous_restarts;
status.last_rendezvous_error = health.last_rendezvous_error;
status.known_contacts = service.known_peers().len();
status.stored_dht_records = service.dht_record_count().await.ok();
status.stored_dht_bytes =
@@ -1170,13 +1315,22 @@ impl Federation {
&self,
query: crate::similarity::QueryVector,
limit: usize,
) -> Result<FedSearchResults> {
) -> Result<FedSimilaritySearchResults> {
anyhow::ensure!(
self.similarity.network_allowed(),
"similarity federation has no consent"
);
let service = self.service().await?;
similarity::search(service, query, limit, Arc::clone(&self.transport_stats)).await
let (service, similarity_dht) = self.similarity_services().await?;
let settings = self.similarity.settings();
similarity::search(
service,
similarity_dht,
query,
limit,
settings.minimum_score,
Arc::clone(&self.transport_stats),
)
.await
}
/// Resolves a share-link content id to one playable federated track.
@@ -1332,31 +1486,38 @@ impl Federation {
let own = service.endpoint_id();
let own_hex = own.to_string();
let normalized = music_dht::normalize_name(name);
let outcome = service
.search_network(name)
.await
.map_err(|err| anyhow::anyhow!("federated search failed: {err}"))?;
// A partial/unavailable DHT index must not prevent direct catalog
// requests to peers we already know. Keep discovery bounded so an
// open card cannot remain in Loading indefinitely.
let network_results = match tokio::time::timeout(
Duration::from_secs(10),
service.search_network(name),
)
.await
{
Ok(Ok(outcome)) => outcome.network_results,
Ok(Err(error)) => {
tracing::debug!(artist = %name, %error, "artist discovery failed; trying known peers");
Vec::new()
}
Err(_) => {
tracing::debug!(artist = %name, "artist discovery timed out; trying known peers");
Vec::new()
}
};
let cached_metadata = self.cached_metadata_snapshot();
let has_cached_artist = cached_metadata
.iter()
.any(|cached| cached_has_artist(cached, &normalized));
let owners: std::collections::HashSet<EndpointId> = outcome
.network_results
.iter()
.filter(|item| {
(item.kind == ItemKind::Artist && item.normalized_name == normalized)
|| item
.artist_names
.iter()
.any(|artist| music_dht::normalize_name(artist) == normalized)
|| item
.featured_artist_names
.iter()
.any(|artist| music_dht::normalize_name(artist) == normalized)
})
.map(|item| item.owner)
.filter(|owner| *owner != own)
.collect();
let owners = artist_catalog_owners(
&network_results,
&normalized,
own,
service
.connected_peers()
.into_iter()
.chain(service.known_peers().into_iter().map(|peer| peer.peer_id)),
);
let local_library = Arc::clone(&self.library);
let local_name = name.to_string();
@@ -1411,7 +1572,7 @@ impl Federation {
}
}
let mut card = catalog::merge_catalogs(name, catalogs);
add_dht_appearance_hits(&mut card, &outcome.network_results, &normalized, name);
add_dht_appearance_hits(&mut card, &network_results, &normalized, name);
add_cached_appearance_hits(&mut card, &cached_metadata, &normalized, name);
anyhow::ensure!(
!card.releases.is_empty() || !card.appears_on.is_empty(),
@@ -2252,6 +2413,34 @@ fn cached_has_artist(cached: &CachedTrackMetadata, normalized_artist: &str) -> b
.any(|artist| music_dht::normalize_name(artist) == normalized_artist)
}
/// Prefer advertised owners, then ask a bounded set of known peers directly.
/// Catalog lookup is useful even before those peers' DHT records arrive.
fn artist_catalog_owners(
items: &[LibraryItem],
normalized: &str,
own: EndpointId,
known: impl IntoIterator<Item = EndpointId>,
) -> Vec<EndpointId> {
const MAX_CATALOG_PEERS: usize = 32;
let advertised = items
.iter()
.filter(|item| {
(item.kind == ItemKind::Artist && item.normalized_name == normalized)
|| item
.artist_names
.iter()
.chain(&item.featured_artist_names)
.any(|artist| music_dht::normalize_name(artist) == normalized)
})
.map(|item| item.owner);
let mut seen = std::collections::HashSet::from([own]);
advertised
.chain(known)
.filter(|owner| seen.insert(*owner))
.take(MAX_CATALOG_PEERS)
.collect()
}
fn rank_fed_search_results(
artists: &mut [FedArtistHit],
tracks: &mut [FedTrack],
@@ -2527,28 +2716,6 @@ fn push_artist_once(names: &mut Vec<String>, name: &str) {
names.push(name.to_string());
}
fn artist_line(artists: &[String], featured_artists: &[String]) -> String {
let mut main = Vec::new();
for artist in artists {
push_artist_once(&mut main, artist);
}
let mut featured = Vec::new();
for artist in featured_artists {
if !main
.iter()
.any(|name| music_dht::normalize_name(name) == music_dht::normalize_name(artist))
{
push_artist_once(&mut featured, artist);
}
}
match (main.is_empty(), featured.is_empty()) {
(false, false) => format!("{} feat. {}", main.join(", "), featured.join(", ")),
(false, true) => main.join(", "),
(true, false) => format!("feat. {}", featured.join(", ")),
(true, true) => String::new(),
}
}
fn sort_fed_appearances(appearances: &mut [FedAppearsOn]) {
appearances.sort_by(|a, b| {
b.year
@@ -2565,6 +2732,77 @@ fn sort_fed_appearances(appearances: &mut [FedAppearsOn]) {
});
}
async fn similarity_route_sync_loop(
routing: Arc<SimilarityDht>,
similarity: Arc<crate::similarity::Manager>,
library: Arc<Library>,
) {
let mut interval = tokio::time::interval(SYNC_INTERVAL);
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut published_marker: Option<(String, blake3::Hash)> = None;
loop {
interval.tick().await;
if !similarity.network_allowed() {
if published_marker.take().is_some() {
routing.clear_local_signatures();
tracing::info!("local similarity DHT publication disabled");
}
continue;
}
let status = similarity.status();
let Some(profile_id) = status.active_profile else {
continue;
};
if status.phase != crate::similarity::Phase::Ready {
continue;
}
let library = Arc::clone(&library);
let profile_for_task = profile_id.clone();
let loaded = tokio::task::spawn_blocking(move || {
let signatures = library.similarity_routing_signatures(&profile_for_task)?;
let mut hasher = blake3::Hasher::new();
for signature in &signatures {
hasher.update(signature);
}
Ok::<_, anyhow::Error>((signatures, hasher.finalize()))
})
.await;
let (signatures, fingerprint) = match loaded {
Ok(Ok(loaded)) => loaded,
Ok(Err(error)) => {
tracing::warn!(%error, %profile_id, "similarity routing signatures unavailable");
continue;
}
Err(error) => {
tracing::warn!(%error, "similarity routing signature task failed");
continue;
}
};
let marker = (profile_id.clone(), fingerprint);
if published_marker.as_ref() == Some(&marker) {
continue;
}
match routing
.sync_local_signatures(profile_id.clone(), signatures)
.await
{
Ok(stats) => {
tracing::info!(
profile = %profile_id,
records = stats.records,
keys = stats.keys,
remote_nodes = stats.remote_nodes,
"local similarity DHT index synchronized"
);
published_marker = Some(marker);
}
Err(error) => {
tracing::warn!(%error, %profile_id, "similarity DHT synchronization failed");
}
}
}
}
async fn stop_running(running: Option<Running>) {
let Some(running) = running else { return };
for task in &running.tasks {
+146 -56
View File
@@ -4,24 +4,30 @@
//! module owns application policy: consent, peer fan-out, local index access,
//! result conversion, deduplication, and ranking limits.
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;
use std::time::{Duration, Instant};
use anyhow::{Context as _, Result};
use futures_util::stream::{self, StreamExt as _};
use music_dht::similarity::{self as wire, SimilarityHit, SimilarityRequest, SimilarityResponse};
use music_dht::{ByteStream, EndpointId, ItemId, ItemKind, MusicDhtService, StreamAcceptor};
use music_dht::similarity_dht::SimilarityDht;
use music_dht::{
ByteStream, EndpointId, ItemId, ItemKind, MusicDhtService, PeerTicket, StreamAcceptor,
};
use crate::federation::{FedSearchResults, FedTrack, TransportStats};
use crate::federation::{
FedSimilaritySearchResults, FedTrack, ScoredFedTrack, SimilaritySearchStats, TransportStats,
};
use crate::similarity::{Manager, QueryVector};
pub use music_dht::similarity::SIMILARITY_ALPN;
const MAX_QUERY_PEERS: usize = 16;
const QUERY_CONCURRENCY: usize = 6;
const INITIAL_QUERY_PEERS: usize = 16;
const MAX_QUERY_PEERS: usize = 48;
const QUERY_CONCURRENCY: usize = 8;
const QUERY_TIMEOUT: Duration = Duration::from_secs(5);
const MAX_PER_ARTIST: usize = 3;
const ROUTING_TIMEOUT: Duration = Duration::from_secs(5);
const MAX_NEAR_DUPLICATE_SIGNATURE_DISTANCE: u32 = 8;
pub async fn serve_peers(
@@ -57,7 +63,7 @@ async fn serve_one(
let vector = request.vector;
let limit = request.limit;
let matches = tokio::task::spawn_blocking(move || {
similarity.search_vector(&profile, &vector, None, None, limit)
similarity.search_vector_for_peer(&profile, &vector, limit)
})
.await
.context("local similarity task failed")
@@ -122,20 +128,51 @@ async fn serve_one(
pub async fn search(
service: Arc<MusicDhtService>,
routing: Arc<SimilarityDht>,
query: QueryVector,
limit: usize,
minimum_score: f32,
transport: Arc<TransportStats>,
) -> Result<FedSearchResults> {
) -> Result<FedSimilaritySearchResults> {
let started = Instant::now();
let own = service.endpoint_id();
let mut peers = Vec::new();
let routed = match tokio::time::timeout(
ROUTING_TIMEOUT,
routing.find_peers(&query.profile_id, &query.vector, MAX_QUERY_PEERS),
)
.await
{
Ok(Ok(peers)) => peers,
Err(_) => {
tracing::debug!("similarity DHT lookup timed out; using known peers");
Vec::new()
}
Ok(Err(error)) => {
tracing::debug!(%error, "similarity DHT lookup unavailable; using known peers");
Vec::new()
}
};
let mut seen = HashSet::new();
let mut peers: Vec<QueryPeer> = routed
.into_iter()
.filter_map(|ticket| {
let owner = ticket.endpoint_id();
(owner != own && seen.insert(owner)).then_some(QueryPeer {
owner,
ticket: Some(ticket),
})
})
.collect();
for peer in service
.connected_peers()
.into_iter()
.chain(service.known_peers().into_iter().map(|peer| peer.peer_id))
{
if peer != own && seen.insert(peer) {
peers.push(peer);
peers.push(QueryPeer {
owner: peer,
ticket: None,
});
}
if peers.len() >= MAX_QUERY_PEERS {
break;
@@ -148,36 +185,50 @@ pub async fn search(
limit.clamp(1, wire::MAX_SIMILARITY_RESULTS),
)?);
let responses = stream::iter(peers.into_iter().map(|peer| {
let service = Arc::clone(&service);
let request = Arc::clone(&request);
let transport = Arc::clone(&transport);
async move {
tokio::time::timeout(
QUERY_TIMEOUT,
query_peer(service, peer, &request, transport),
)
.await
.map_err(|_| anyhow::anyhow!("similarity peer timed out"))?
}
}))
.buffer_unordered(QUERY_CONCURRENCY)
.collect::<Vec<_>>()
.await;
let mut hits = Vec::new();
let initial = peers.len().min(INITIAL_QUERY_PEERS);
let responses = query_peers(
Arc::clone(&service),
&peers[..initial],
Arc::clone(&request),
Arc::clone(&transport),
)
.await;
let mut successful = 0usize;
let mut peers_queried = initial;
for response in responses {
match response {
Ok(peer_hits) => hits.extend(peer_hits),
Ok(peer_hits) => {
successful += 1;
hits.extend(peer_hits);
}
Err(err) => tracing::debug!(%err, "similarity peer query skipped"),
}
}
if initial < peers.len() && (hits.len() < limit || successful < initial.min(4)) {
peers_queried = peers.len();
for response in query_peers(
Arc::clone(&service),
&peers[initial..],
Arc::clone(&request),
Arc::clone(&transport),
)
.await
{
match response {
Ok(peer_hits) => hits.extend(peer_hits),
Err(err) => tracing::debug!(%err, "fallback similarity peer query skipped"),
}
}
}
hits.sort_by(|left, right| right.1.total_cmp(&left.1));
let mut dedup = HashSet::new();
let mut embedding_signatures = vec![query_signature];
let mut artist_counts: HashMap<String, usize> = HashMap::new();
let mut tracks = Vec::new();
for (track, _, embedding_signature) in hits {
for (track, score, embedding_signature) in hits {
if score < minimum_score {
break;
}
if query
.source_content_id
.as_deref()
@@ -200,46 +251,85 @@ pub async fn search(
}) {
continue;
}
let artist = track
.artist_names
.first()
.map(|name| music_dht::normalize_name(name))
.unwrap_or_default();
let count = artist_counts.entry(artist.clone()).or_default();
if !artist.is_empty() && *count >= MAX_PER_ARTIST {
continue;
}
*count += 1;
if let Some(signature) = embedding_signature {
embedding_signatures.push(signature);
}
tracks.push(track);
tracks.push(ScoredFedTrack {
track,
score,
embedding_signature,
});
if tracks.len() >= limit.min(wire::MAX_SIMILARITY_RESULTS) {
break;
}
}
Ok(FedSearchResults {
artists: Vec::new(),
let artists = tracks
.iter()
.filter_map(|hit| hit.track.artist_names.first())
.map(|name| music_dht::normalize_name(name))
.filter(|name| !name.is_empty())
.collect::<HashSet<_>>()
.len();
let elapsed_ms = started.elapsed().as_millis().min(u128::from(u64::MAX)) as u64;
Ok(FedSimilaritySearchResults {
stats: SimilaritySearchStats {
tracks: tracks.len(),
artists,
peers_queried,
elapsed_ms,
},
tracks,
})
}
type PeerHits = Vec<(
FedTrack,
f32,
Option<[u8; wire::SIMILARITY_SIGNATURE_BYTES]>,
)>;
#[derive(Clone)]
struct QueryPeer {
owner: EndpointId,
ticket: Option<PeerTicket>,
}
async fn query_peers(
service: Arc<MusicDhtService>,
peers: &[QueryPeer],
request: Arc<SimilarityRequest>,
transport: Arc<TransportStats>,
) -> Vec<Result<PeerHits>> {
stream::iter(peers.iter().cloned().map(|peer| {
let service = Arc::clone(&service);
let request = Arc::clone(&request);
let transport = Arc::clone(&transport);
async move {
tokio::time::timeout(
QUERY_TIMEOUT,
query_peer(service, peer, &request, transport),
)
.await
.map_err(|_| anyhow::anyhow!("similarity peer timed out"))?
}
}))
.buffer_unordered(QUERY_CONCURRENCY)
.collect()
.await
}
async fn query_peer(
service: Arc<MusicDhtService>,
owner: EndpointId,
peer: QueryPeer,
request: &SimilarityRequest,
transport: Arc<TransportStats>,
) -> Result<
Vec<(
FedTrack,
f32,
Option<[u8; wire::SIMILARITY_SIGNATURE_BYTES]>,
)>,
> {
let mut stream = service
.open_stream(owner, SIMILARITY_ALPN)
.await
.map_err(|err| anyhow::anyhow!("cannot reach similarity peer: {err}"))?;
) -> Result<PeerHits> {
let owner = peer.owner;
let mut stream = match peer.ticket {
Some(ticket) => service.open_stream_to(&ticket, SIMILARITY_ALPN).await,
None => service.open_stream(owner, SIMILARITY_ALPN).await,
}
.map_err(|err| anyhow::anyhow!("cannot reach similarity peer: {err}"))?;
super::record_stream_transport(&transport, "similarity", "outbound", "open", &stream);
let response = wire::exchange(&mut stream, request).await?;
super::record_stream_transport(&transport, "similarity", "outbound", "done", &stream);
+42
View File
@@ -93,3 +93,45 @@ fn federation_search_ranks_exact_names_first() {
assert_eq!(artists[0].name, "Ежемесячные");
assert_eq!(tracks[0].title, "Ежемесячные");
}
#[test]
fn artist_catalog_discovery_uses_known_peers_without_dht_hits() {
let own = music_dht::SecretKey::from_bytes(&[8; 32]).public();
let peer = test_owner();
assert_eq!(
artist_catalog_owners(&[], "artist", own, [own, peer, peer]),
vec![peer]
);
// Unrelated records must not send the card request to their owners.
assert!(artist_catalog_owners(&[dht_track(&["Other"], &[])], "artist", own, []).is_empty());
}
#[test]
fn artist_catalog_discovery_prioritizes_matching_owners_and_bounds_fanout() {
let own = music_dht::SecretKey::from_bytes(&[8; 32]).public();
let peer = test_owner();
let known: Vec<_> = (10..60)
.map(|seed| music_dht::SecretKey::from_bytes(&[seed; 32]).public())
.collect();
for item in [
dht_track(&["Artist"], &[]),
dht_track(&["Host"], &["Artist"]),
] {
let owners = artist_catalog_owners(
&[item],
"artist",
own,
known.iter().copied().chain([own, peer]),
);
assert_eq!(owners.len(), 32);
assert_eq!(owners[0], peer);
assert!(!owners.contains(&own));
assert_eq!(
owners
.iter()
.collect::<std::collections::HashSet<_>>()
.len(),
owners.len()
);
}
}
+1
View File
@@ -668,6 +668,7 @@ mod tests {
repeat: crate::devices::PlaybackRepeat::Off,
};
host.publish_host_playback(PlaybackSnapshot {
coordination: None,
device_id: "dev_host".into(),
device_name: "Host".into(),
active: true,
-561
View File
@@ -1,561 +0,0 @@
//! Importing audio files into the library: directory scanning, tag reading
//! (via lofty) and cover extraction. Importing the same file again updates
//! its metadata instead of duplicating it.
use std::path::{Path, PathBuf};
use anyhow::{Context as _, Result};
use lofty::file::{AudioFile as _, TaggedFileExt as _};
use lofty::picture::MimeType;
use lofty::tag::{Accessor as _, ItemKey};
use rusqlite::{OptionalExtension as _, params};
use super::{Library, audio_content_id, find_or_create_artist};
/// Extensions the playback engine can decode (rodio/symphonia feature set).
const AUDIO_EXTENSIONS: [&str; 8] = ["mp3", "flac", "ogg", "oga", "wav", "m4a", "mp4", "aac"];
/// Everything known about one audio file, ready to be written to the DB.
#[derive(Debug)]
pub struct TrackImport {
pub file_path: String,
pub title: String,
pub artists: Vec<String>,
pub featured_artists: Vec<String>,
pub album_artists: Vec<String>,
pub release_title: String,
/// Release type ("album", "single", ...) when known from a richer
/// source than file tags (e.g. federation metadata); None = "album".
pub release_type: Option<String>,
pub year: Option<i32>,
pub track_number: Option<i32>,
pub disc_number: Option<i32>,
pub duration_seconds: f64,
pub audio_format: Option<String>,
pub audio_bitrate: Option<i32>,
pub audio_sample_rate: Option<i32>,
pub audio_bit_depth: Option<i32>,
pub file_size_bytes: Option<i64>,
/// Embedded cover art (bytes, file extension), if any.
pub cover: Option<(Vec<u8>, &'static str)>,
}
#[derive(Debug, Default)]
pub struct ImportOutcome {
pub added: usize,
pub updated: usize,
pub failed: Vec<(PathBuf, String)>,
}
impl ImportOutcome {
pub fn summary(&self) -> String {
let mut message = format!("imported {} track(s)", self.added);
if self.updated > 0 {
message.push_str(&format!(", updated {}", self.updated));
}
if !self.failed.is_empty() {
message.push_str(&format!(", {} failed", self.failed.len()));
}
message
}
}
/// Import a file or a directory (recursively). `progress(done, total, name)`
/// is called after every file.
pub fn import_path(
library: &Library,
path: &Path,
mut progress: impl FnMut(usize, usize, &str),
) -> Result<ImportOutcome> {
let path = path
.canonicalize()
.with_context(|| format!("{} does not exist", path.display()))?;
let mut files = Vec::new();
collect_audio_files(&path, &mut files);
anyhow::ensure!(
!files.is_empty(),
"no audio files found at {} (supported: {})",
path.display(),
AUDIO_EXTENSIONS.join(", ")
);
files.sort();
let total = files.len();
let mut outcome = ImportOutcome::default();
for (index, file) in files.iter().enumerate() {
match read_file(file).and_then(|import| upsert_track(library, &import)) {
Ok((_, created)) => {
if created {
outcome.added += 1;
} else {
outcome.updated += 1;
}
}
Err(err) => {
tracing::warn!(file = %file.display(), %err, "import failed");
outcome.failed.push((file.clone(), format!("{err:#}")));
}
}
let name = file
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_default();
progress(index + 1, total, &name);
}
Ok(outcome)
}
fn collect_audio_files(path: &Path, files: &mut Vec<PathBuf>) {
if path.is_dir() {
let Ok(entries) = std::fs::read_dir(path) else {
return;
};
for entry in entries.flatten() {
collect_audio_files(&entry.path(), files);
}
return;
}
let extension = path
.extension()
.and_then(|ext| ext.to_str())
.map(|ext| ext.to_ascii_lowercase());
if extension.is_some_and(|ext| AUDIO_EXTENSIONS.contains(&ext.as_str())) {
files.push(path.to_path_buf());
}
}
/// Read tags and audio properties from one file.
pub fn read_file(path: &Path) -> Result<TrackImport> {
let tagged = lofty::read_from_path(path).context("cannot read tags")?;
let properties = tagged.properties();
let tag = tagged.primary_tag().or_else(|| tagged.first_tag());
let fallback_title = path
.file_stem()
.map(|stem| stem.to_string_lossy().into_owned())
.unwrap_or_else(|| "Unknown".to_string());
let (mut title, artist_raw, album, year, track_number, disc_number, album_artist_raw, cover) =
match tag {
Some(tag) => (
tag.title()
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty())
.unwrap_or(fallback_title),
tag.artist().map(|value| value.into_owned()),
tag.album()
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty()),
tag.year().and_then(|value| i32::try_from(value).ok()),
tag.track().and_then(|value| i32::try_from(value).ok()),
tag.disk().and_then(|value| i32::try_from(value).ok()),
tag.get_string(&ItemKey::AlbumArtist)
.map(|value| value.to_string()),
tag.pictures().first().map(|picture| {
let extension = match picture.mime_type() {
Some(MimeType::Png) => "png",
Some(MimeType::Gif) => "gif",
Some(MimeType::Bmp) => "bmp",
_ => "jpg",
};
(picture.data().to_vec(), extension)
}),
),
None => (fallback_title, None, None, None, None, None, None, None),
};
let (mut artists, mut featured) = split_artist_tag(artist_raw.as_deref().unwrap_or(""));
// "Song (feat. X)" in the title moves X into the featured list.
if let Some((clean_title, feat)) = extract_title_feat(&title) {
title = clean_title;
for name in feat {
if !featured.iter().any(|f| f.eq_ignore_ascii_case(&name)) {
featured.push(name);
}
}
}
if artists.is_empty() {
artists.push("Unknown Artist".to_string());
}
let album_artists = match album_artist_raw.as_deref().map(split_artist_tag) {
Some((main, _)) if !main.is_empty() => main,
_ => artists.clone(),
};
let metadata = std::fs::metadata(path).ok();
Ok(TrackImport {
file_path: path.to_string_lossy().into_owned(),
title,
artists,
featured_artists: featured,
album_artists,
release_title: album.unwrap_or_else(|| "Unknown Album".to_string()),
release_type: None,
year,
track_number,
disc_number,
duration_seconds: properties.duration().as_secs_f64(),
audio_format: path
.extension()
.and_then(|ext| ext.to_str())
.map(|ext| ext.to_ascii_lowercase()),
audio_bitrate: properties
.audio_bitrate()
.and_then(|value| i32::try_from(value).ok()),
audio_sample_rate: properties
.sample_rate()
.and_then(|value| i32::try_from(value).ok()),
audio_bit_depth: properties.bit_depth().map(i32::from),
file_size_bytes: metadata.map(|meta| meta.len() as i64),
cover,
})
}
/// Insert or update one track (matching by file path). Returns the track id
/// and whether a new row was created.
pub fn upsert_track(library: &Library, import: &TrackImport) -> Result<(i64, bool)> {
let content_id = audio_content_id(&import.file_path);
let mut conn = library.lock();
let tx = conn.transaction()?;
// Release, keyed by (title, first album artist).
let album_artist_id = find_or_create_artist(
&tx,
import
.album_artists
.first()
.map(String::as_str)
.unwrap_or("Unknown Artist"),
)?;
let release_id: Option<i64> = tx
.query_row(
"SELECT r.id FROM releases r
JOIN release_artists ra ON ra.release_id = r.id
WHERE r.title = ?1 COLLATE NOCASE AND ra.artist_id = ?2",
params![import.release_title, album_artist_id],
|row| row.get(0),
)
.optional()?;
let release_id = match release_id {
Some(id) => {
// Fill in the year if this file is the first one to know it.
if import.year.is_some() {
tx.execute(
"UPDATE releases SET year = COALESCE(year, ?2) WHERE id = ?1",
params![id, import.year],
)?;
}
id
}
None => {
tx.execute(
"INSERT INTO releases (title, release_type, year) VALUES (?1, ?2, ?3)",
params![
import.release_title,
import.release_type.as_deref().unwrap_or("album"),
import.year,
],
)?;
let id = tx.last_insert_rowid();
for (position, name) in import.album_artists.iter().enumerate() {
let artist_id = find_or_create_artist(&tx, name)?;
tx.execute(
"INSERT OR IGNORE INTO release_artists (release_id, artist_id, position)
VALUES (?1, ?2, ?3)",
params![id, artist_id, position as i64],
)?;
}
id
}
};
let existing: Option<i64> = tx
.query_row(
"SELECT id FROM tracks WHERE file_path = ?1",
[&import.file_path],
|row| row.get(0),
)
.optional()?;
let (track_id, created) = match existing {
Some(id) => {
tx.execute(
"UPDATE tracks SET title = ?2, track_number = ?3, disc_number = ?4,
duration_seconds = ?5, release_id = ?6, audio_format = ?7,
audio_bitrate = ?8, audio_sample_rate = ?9, audio_bit_depth = ?10,
file_size_bytes = ?11, content_id = ?12
WHERE id = ?1",
params![
id,
import.title,
import.track_number,
import.disc_number,
import.duration_seconds,
release_id,
import.audio_format,
import.audio_bitrate,
import.audio_sample_rate,
import.audio_bit_depth,
import.file_size_bytes,
content_id.as_deref(),
],
)?;
tx.execute("DELETE FROM track_artists WHERE track_id = ?1", [id])?;
(id, false)
}
None => {
tx.execute(
"INSERT INTO tracks (title, track_number, disc_number, duration_seconds,
release_id, file_path, audio_format, audio_bitrate, audio_sample_rate,
audio_bit_depth, file_size_bytes, content_id)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)",
params![
import.title,
import.track_number,
import.disc_number,
import.duration_seconds,
release_id,
import.file_path,
import.audio_format,
import.audio_bitrate,
import.audio_sample_rate,
import.audio_bit_depth,
import.file_size_bytes,
content_id.as_deref(),
],
)?;
(tx.last_insert_rowid(), true)
}
};
for (position, name) in import.artists.iter().enumerate() {
let artist_id = find_or_create_artist(&tx, name)?;
tx.execute(
"INSERT OR IGNORE INTO track_artists (track_id, artist_id, role, position)
VALUES (?1, ?2, 'main', ?3)",
params![track_id, artist_id, position as i64],
)?;
}
for (position, name) in import.featured_artists.iter().enumerate() {
let artist_id = find_or_create_artist(&tx, name)?;
tx.execute(
"INSERT OR IGNORE INTO track_artists (track_id, artist_id, role, position)
VALUES (?1, ?2, 'featured', ?3)",
params![track_id, artist_id, position as i64],
)?;
}
// Cover: a release keeps the first cover found — an image file next to
// the audio, or the embedded picture saved into the covers directory.
let has_cover: bool = tx
.query_row(
"SELECT cover_path IS NOT NULL FROM releases WHERE id = ?1",
[release_id],
|row| row.get(0),
)
.unwrap_or(false);
if !has_cover && let Some(cover_path) = resolve_cover(library, release_id, import) {
tx.execute(
"UPDATE releases SET cover_path = ?2 WHERE id = ?1",
params![release_id, cover_path],
)?;
}
tx.commit()?;
Ok((track_id, created))
}
/// Find a cover image for the release: a cover/folder/front image in the
/// audio file's directory, or the embedded picture written to disk.
fn resolve_cover(library: &Library, release_id: i64, import: &TrackImport) -> Option<String> {
let directory = Path::new(&import.file_path).parent()?;
if let Ok(entries) = std::fs::read_dir(directory) {
for entry in entries.flatten() {
let path = entry.path();
let stem = path
.file_stem()
.and_then(|stem| stem.to_str())
.map(|stem| stem.to_ascii_lowercase());
let extension = path
.extension()
.and_then(|ext| ext.to_str())
.map(|ext| ext.to_ascii_lowercase());
let is_image = matches!(
extension.as_deref(),
Some("jpg" | "jpeg" | "png" | "webp" | "bmp" | "gif")
);
if is_image
&& matches!(
stem.as_deref(),
Some("cover" | "folder" | "front" | "album")
)
{
return Some(path.to_string_lossy().into_owned());
}
}
}
let (data, extension) = import.cover.as_ref()?;
let covers_dir = library.covers_dir();
if let Err(err) = std::fs::create_dir_all(covers_dir) {
tracing::warn!(%err, "cannot create covers directory");
return None;
}
let path = covers_dir.join(format!("release_{release_id}.{extension}"));
match std::fs::write(&path, data) {
Ok(()) => Some(path.to_string_lossy().into_owned()),
Err(err) => {
tracing::warn!(%err, path = %path.display(), "cannot save embedded cover");
None
}
}
}
/// Split an artist tag into (main artists, featured artists).
/// Separators: ";" and "/" between main artists; "feat."/"ft."/"featuring"
/// starts the featured list.
pub fn split_artist_tag(raw: &str) -> (Vec<String>, Vec<String>) {
let raw = raw.trim();
if raw.is_empty() {
return (Vec::new(), Vec::new());
}
let (main_part, feat_part) = match find_feat_marker(raw) {
Some((at, marker_len)) => {
let main = raw[..at].trim_end_matches(['(', '[', ' ', ',', '-']);
let feat = raw[at + marker_len..].trim_end_matches([')', ']']);
(main, feat)
}
None => (raw, ""),
};
(split_names(main_part), split_names(feat_part))
}
/// The earliest "feat."/"ft."/"featuring" marker that stands as its own
/// word — preceded by a separator and followed by a space — so artist names
/// like "Daft Punk" are not split on the "ft" inside them.
fn find_feat_marker(raw: &str) -> Option<(usize, usize)> {
let lowered = raw.to_lowercase();
let mut best: Option<(usize, usize)> = None;
for marker in ["featuring", "feat.", "feat", "ft.", "ft"] {
for (at, _) in lowered.match_indices(marker) {
let before_ok = raw[..at]
.chars()
.next_back()
.is_some_and(|c| matches!(c, ' ' | '(' | '[' | ',' | '-'));
let after_ok = raw[at + marker.len()..].starts_with(' ');
if before_ok && after_ok && best.is_none_or(|(current, _)| at < current) {
best = Some((at, marker.len()));
}
}
}
best
}
fn split_names(raw: &str) -> Vec<String> {
raw.split([';', '/'])
.flat_map(|part| part.split(" & "))
.map(|name| name.trim().trim_matches(',').trim().to_string())
.filter(|name| !name.is_empty())
.collect()
}
/// Extract "(feat. X)" / "[ft. Y]" from a track title.
fn extract_title_feat(title: &str) -> Option<(String, Vec<String>)> {
let lowered = title.to_lowercase();
for marker in ["(feat.", "(feat ", "(ft.", "[feat.", "[ft."] {
if let Some(start) = lowered.find(marker) {
let closer = if marker.starts_with('(') { ')' } else { ']' };
let rest = &title[start + marker.len()..];
let end = rest.find(closer)?;
let names = split_names(&rest[..end]);
if names.is_empty() {
return None;
}
let mut clean = title[..start].trim_end().to_string();
clean.push_str(rest[end + 1..].trim_end());
return Some((clean.trim().to_string(), names));
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
/// A minimal valid WAV file: 0.1s of silence at 8kHz mono 16-bit.
fn write_test_wav(path: &Path) {
let samples: u32 = 800;
let data_len = samples * 2;
let mut bytes = Vec::new();
bytes.extend_from_slice(b"RIFF");
bytes.extend_from_slice(&(36 + data_len).to_le_bytes());
bytes.extend_from_slice(b"WAVEfmt ");
bytes.extend_from_slice(&16u32.to_le_bytes());
bytes.extend_from_slice(&1u16.to_le_bytes()); // PCM
bytes.extend_from_slice(&1u16.to_le_bytes()); // mono
bytes.extend_from_slice(&8000u32.to_le_bytes()); // sample rate
bytes.extend_from_slice(&16000u32.to_le_bytes()); // byte rate
bytes.extend_from_slice(&2u16.to_le_bytes()); // block align
bytes.extend_from_slice(&16u16.to_le_bytes()); // bits per sample
bytes.extend_from_slice(b"data");
bytes.extend_from_slice(&data_len.to_le_bytes());
bytes.resize(bytes.len() + data_len as usize, 0);
std::fs::write(path, bytes).unwrap();
}
#[test]
fn imports_a_real_audio_file_end_to_end() {
let dir = std::env::temp_dir().join(format!("furumi-import-test-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let wav = dir.join("My Song.wav");
write_test_wav(&wav);
let db = dir.join("library.db");
let library = Library::open(&db).unwrap();
let outcome = import_path(&library, &dir, |_, _, _| {}).unwrap();
assert_eq!(outcome.added, 1);
assert!(outcome.failed.is_empty());
// Untagged files fall back to the file name and placeholder names.
let results = library.search("My Song", 10).unwrap();
assert_eq!(results.tracks.len(), 1);
let track = &results.tracks[0];
assert_eq!(track.title, "My Song");
assert_eq!(track.artists[0].name, "Unknown Artist");
assert_eq!(track.release_title, "Unknown Album");
assert!(track.duration_seconds > 0.05);
assert_eq!(track.audio_sample_rate, Some(8000));
assert!(std::fs::File::open(&track.file_path).is_ok());
// Re-importing the same directory only updates.
let outcome = import_path(&library, &dir, |_, _, _| {}).unwrap();
assert_eq!((outcome.added, outcome.updated), (0, 1));
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn splits_plain_artist() {
let (main, feat) = split_artist_tag("Daft Punk");
assert_eq!(main, vec!["Daft Punk"]);
assert!(feat.is_empty());
}
#[test]
fn splits_multiple_and_featured() {
let (main, feat) = split_artist_tag("A; B feat. C & D");
assert_eq!(main, vec!["A", "B"]);
assert_eq!(feat, vec!["C", "D"]);
}
#[test]
fn keeps_commas_inside_names() {
let (main, _) = split_artist_tag("Tyler, The Creator");
assert_eq!(main, vec!["Tyler, The Creator"]);
}
#[test]
fn extracts_feat_from_title() {
let (title, names) = extract_title_feat("Song (feat. X & Y)").unwrap();
assert_eq!(title, "Song");
assert_eq!(names, vec!["X", "Y"]);
assert!(extract_title_feat("Plain Song").is_none());
}
}
+2 -3480
View File
File diff suppressed because it is too large. Load diff
-255
View File
@@ -1,255 +0,0 @@
//! Data shapes the views render. They mirror what the furumusic API used to
//! return, but every field is now filled from the local SQLite library.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Availability {
#[default]
Local,
Mixed,
Remote,
}
impl Availability {
pub fn is_remoteish(self) -> bool {
matches!(self, Availability::Mixed | Availability::Remote)
}
}
#[derive(Debug, Clone)]
pub struct ArtistCard {
pub id: i64,
pub name: String,
/// Path to a local image file, if one is set for the artist.
pub image_path: Option<String>,
pub release_count: i64,
pub track_count: i64,
pub availability: Availability,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArtistRef {
pub id: i64,
pub name: String,
}
#[derive(Debug, Clone)]
pub struct TrackItem {
pub id: i64,
pub title: String,
pub track_number: Option<i32>,
pub disc_number: Option<i32>,
pub duration_seconds: f64,
pub artists: Vec<ArtistRef>,
pub featured_artists: Vec<ArtistRef>,
pub release_id: i64,
pub release_title: String,
pub release_year: Option<i32>,
/// Absolute path to the local audio file.
pub file_path: String,
/// Stable audio content id (`b3:<64 hex>`) when known.
pub content_id: Option<String>,
/// Path to a local cover image (the release cover).
pub cover_path: Option<String>,
pub audio_format: Option<String>,
pub audio_bitrate: Option<i32>,
pub audio_sample_rate: Option<i32>,
pub audio_bit_depth: Option<i32>,
pub file_size_bytes: Option<i64>,
/// Completed local plays, from the history table.
pub play_count: i64,
/// Set for federated tracks that are not in the local library (yet):
/// carries everything needed to download them from the owning peer.
/// With an empty `file_path` the player resolves the track on demand.
pub fed: Option<crate::federation::FedTrack>,
}
impl TrackItem {
/// A federated track that still needs downloading before playback.
pub fn is_fed_pending(&self) -> bool {
self.fed.is_some() && self.file_path.is_empty()
}
pub fn artist_line(&self) -> String {
let artists = self
.artists
.iter()
.map(|a| a.name.as_str())
.collect::<Vec<_>>()
.join(", ");
let featured = self
.featured_artists
.iter()
.map(|a| a.name.as_str())
.collect::<Vec<_>>()
.join(", ");
match (artists.is_empty(), featured.is_empty()) {
(false, false) => format!("{artists} feat. {featured}"),
(false, true) => artists,
(true, false) => format!("feat. {featured}"),
(true, true) => String::new(),
}
}
pub fn duration_label(&self) -> String {
let total = self.duration_seconds.round() as i64;
format!("{}:{:02}", total / 60, total % 60)
}
/// Full tech line for the status bar, including the sample rate.
pub fn tech_label_full(&self) -> String {
let mut parts = Vec::new();
if let Some(format) = &self.audio_format {
parts.push(format.to_uppercase());
}
if let Some(bitrate) = self.audio_bitrate {
parts.push(format!("{bitrate}kbps"));
}
if let Some(rate) = self.audio_sample_rate {
parts.push(format!("{:.1}kHz", f64::from(rate) / 1000.0));
}
if let Some(bytes) = self.file_size_bytes {
parts.push(format!("{:.1}MB", bytes as f64 / 1_048_576.0));
}
parts.join(" · ")
}
}
#[derive(Debug, Clone)]
pub struct ReleaseCard {
pub id: i64,
pub title: String,
pub release_type: String,
pub year: Option<i32>,
pub cover_path: Option<String>,
pub track_count: i64,
pub availability: Availability,
}
#[derive(Debug)]
pub struct ArtistDetail {
#[allow(dead_code, reason = "cache key is held by the caller")]
pub id: i64,
pub name: String,
pub image_path: Option<String>,
pub total_track_count: i64,
pub total_play_count: i64,
pub top_tracks: Vec<TrackItem>,
pub releases: Vec<ReleaseCard>,
/// Tracks where this artist is featured (the only content for artists
/// without own releases).
pub featured_tracks: Vec<TrackItem>,
}
#[derive(Debug)]
pub struct ReleaseDetail {
#[allow(dead_code, reason = "cache key is held by the caller")]
pub id: i64,
pub title: String,
pub release_type: String,
pub year: Option<i32>,
pub cover_path: Option<String>,
pub artists: Vec<ArtistRef>,
pub tracks: Vec<TrackItem>,
}
#[derive(Debug, Clone)]
pub struct PlaylistCard {
pub id: i64,
pub title: String,
pub track_count: i64,
/// "normal" for user playlists, "likes" for the virtual Likes playlist.
pub kind: String,
}
#[derive(Debug)]
pub struct PlaylistDetail {
#[allow(dead_code, reason = "cache key is held by the caller")]
pub id: i64,
pub title: String,
#[allow(dead_code, reason = "shown in a detail header later")]
pub description: Option<String>,
pub tracks: Vec<TrackItem>,
}
#[derive(Debug, Default)]
pub struct SearchResults {
pub artists: Vec<ArtistCard>,
pub releases: Vec<ReleaseCard>,
pub tracks: Vec<TrackItem>,
}
impl SearchResults {
pub fn len(&self) -> usize {
self.artists.len() + self.releases.len() + self.tracks.len()
}
}
#[derive(Debug)]
pub struct ArtistsPage {
pub items: Vec<ArtistCard>,
pub total: i64,
pub page: i64,
pub has_more: bool,
}
/// Edited values submitted from the track edit form. `None` numbers clear
/// the column.
#[derive(Debug, Clone)]
pub struct TrackEdit {
pub title: String,
pub artists: Vec<String>,
pub featured_artists: Vec<String>,
pub track_number: Option<i32>,
pub disc_number: Option<i32>,
/// Cover image path; the cover lives on the track's release (the same
/// image every view shows for the track). None clears it.
pub cover_path: Option<String>,
}
#[derive(Debug, Clone)]
pub struct ReleaseEdit {
pub title: String,
pub release_type: String,
pub year: Option<i32>,
pub artists: Vec<String>,
}
#[cfg(test)]
mod tests {
use super::*;
fn artist(name: &str) -> ArtistRef {
ArtistRef {
id: 1,
name: name.to_string(),
}
}
#[test]
fn artist_line_formats_featured_artists() {
let track = TrackItem {
id: 1,
title: "Track".into(),
track_number: None,
disc_number: None,
duration_seconds: 1.0,
artists: vec![artist("Main")],
featured_artists: vec![artist("Guest"), artist("Other")],
release_id: 1,
release_title: "Release".into(),
release_year: None,
file_path: "/tmp/track.mp3".into(),
content_id: None,
cover_path: None,
audio_format: None,
audio_bitrate: None,
audio_sample_rate: None,
audio_bit_depth: None,
file_size_bytes: None,
play_count: 0,
fed: None,
};
assert_eq!(track.artist_line(), "Main feat. Guest, Other");
}
}
-763
View File
@@ -1,763 +0,0 @@
use super::*;
fn test_library() -> Library {
let conn = Connection::open_in_memory().unwrap();
conn.pragma_update(None, "foreign_keys", "ON").unwrap();
register_norm_function(&conn).unwrap();
conn.execute_batch(SCHEMA).unwrap();
Library {
conn: Mutex::new(conn),
db_path: std::env::temp_dir().join("furumi-test-library.db"),
covers_dir: std::env::temp_dir().join("furumi-test-covers-unused"),
}
}
fn add_track(lib: &Library, title: &str, artist: &str, album: &str) -> i64 {
add_track_with_featured(lib, title, artist, &[], album)
}
fn add_track_with_featured(
lib: &Library,
title: &str,
artist: &str,
featured: &[&str],
album: &str,
) -> i64 {
let import = import::TrackImport {
release_type: None,
file_path: format!("/music/{artist}/{album}/{title}.mp3"),
title: title.to_string(),
artists: vec![artist.to_string()],
featured_artists: featured.iter().map(|name| (*name).to_string()).collect(),
album_artists: vec![artist.to_string()],
release_title: album.to_string(),
year: Some(2020),
track_number: None,
disc_number: None,
duration_seconds: 60.0,
audio_format: Some("mp3".into()),
audio_bitrate: Some(320),
audio_sample_rate: Some(44100),
audio_bit_depth: None,
file_size_bytes: Some(1),
cover: None,
};
let id = import::upsert_track(lib, &import).unwrap().0;
let content_id = format!("b3:{}", blake3::hash(import.file_path.as_bytes()).to_hex());
lib.lock()
.execute(
"UPDATE tracks SET content_id = ?2 WHERE id = ?1",
params![id, content_id],
)
.unwrap();
id
}
fn artist_filters(hide_featured_only: bool) -> crate::config::settings::LibraryFilters {
crate::config::settings::LibraryFilters {
hide_featured_only,
..Default::default()
}
}
fn unique_test_dir(label: &str) -> std::path::PathBuf {
let unique = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
std::env::temp_dir().join(format!("furumi-{label}-{}-{unique}", std::process::id()))
}
#[test]
fn managed_music_relocation_builds_artist_release_tree_and_updates_paths() {
let root = unique_test_dir("music-relocation");
let old = root.join("old");
let new = root.join("new");
let covers = root.join("covers");
std::fs::create_dir_all(&old).unwrap();
std::fs::create_dir_all(&covers).unwrap();
let audio = old.join("legacy.flac");
let cover = covers.join("release.jpg");
let artist_image = covers.join("artist.png");
std::fs::write(&audio, b"audio").unwrap();
std::fs::write(&cover, b"cover").unwrap();
std::fs::write(&artist_image, b"artist").unwrap();
let conn = Connection::open_in_memory().unwrap();
conn.pragma_update(None, "foreign_keys", "ON").unwrap();
register_norm_function(&conn).unwrap();
conn.execute_batch(SCHEMA).unwrap();
let library = Library {
conn: Mutex::new(conn),
db_path: root.join("library.db"),
covers_dir: covers,
};
let track_id = import::upsert_track(
&library,
&import::TrackImport {
file_path: audio.to_string_lossy().into_owned(),
title: "Song".into(),
artists: vec!["Artist".into()],
featured_artists: vec![],
album_artists: vec!["Artist".into()],
release_title: "Release".into(),
release_type: Some("album".into()),
year: Some(2026),
track_number: Some(1),
disc_number: Some(1),
duration_seconds: 1.0,
audio_format: Some("flac".into()),
audio_bitrate: None,
audio_sample_rate: None,
audio_bit_depth: None,
file_size_bytes: Some(5),
cover: None,
},
)
.unwrap()
.0;
let (release_id, artist_id): (i64, i64) = library
.lock()
.query_row(
"SELECT t.release_id, ta.artist_id
FROM tracks t JOIN track_artists ta ON ta.track_id = t.id
WHERE t.id = ?1 AND ta.role = 'main'",
[track_id],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.unwrap();
library
.lock()
.execute(
"UPDATE releases SET cover_path = ?2 WHERE id = ?1",
params![release_id, cover.to_string_lossy()],
)
.unwrap();
library
.lock()
.execute(
"UPDATE artists SET image_path = ?2 WHERE id = ?1",
params![artist_id, artist_image.to_string_lossy()],
)
.unwrap();
let stats = library.relocate_managed_music(&old, &new).unwrap();
assert_eq!(stats.tracks, 1);
assert_eq!(stats.images, 2);
let track = library.tracks_by_ids(&[track_id]).unwrap().remove(0);
assert_eq!(
track.file_path,
new.join("Artist/Release/legacy.flac").to_string_lossy()
);
assert_eq!(
track.cover_path.as_deref(),
Some(
new.join("Artist/Release/cover.jpg")
.to_string_lossy()
.as_ref()
)
);
let image: String = library
.lock()
.query_row(
"SELECT image_path FROM artists WHERE id = ?1",
[artist_id],
|row| row.get(0),
)
.unwrap();
assert_eq!(image, new.join("Artist/artist.png").to_string_lossy());
assert!(!audio.exists());
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn music_directory_validation_rejects_a_file_without_touching_it() {
let root = unique_test_dir("music-validation");
std::fs::create_dir_all(&root).unwrap();
let file = root.join("not-a-directory");
std::fs::write(&file, b"keep").unwrap();
assert!(Library::validate_music_directory(&file).is_err());
assert_eq!(std::fs::read(&file).unwrap(), b"keep");
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn managed_music_names_are_portable_to_windows() {
assert_eq!(storage_name("Artist/Name", "fallback"), "Artist_Name");
assert_eq!(storage_name("CON", "fallback"), "_CON");
assert_eq!(storage_name("lpt9.live", "fallback"), "_lpt9.live");
assert_eq!(storage_name("...", "fallback"), "fallback");
}
#[test]
fn local_stats_counts_library_rows_and_audio_bytes() {
let lib = test_library();
add_track(&lib, "One", "Artist", "First");
add_track(&lib, "Two", "Artist", "Second");
let stats = lib.local_stats().unwrap();
assert_eq!(stats.artist_count, 1);
assert_eq!(stats.release_count, 2);
assert_eq!(stats.track_count, 2);
assert_eq!(stats.audio_bytes, 2);
assert_eq!(stats.tracks_without_size, 0);
}
#[test]
fn artists_page_prioritizes_releases_then_tracks() {
let lib = test_library();
add_track(&lib, "Solo", "Zed", "Zed Album");
add_track_with_featured(&lib, "Guest One", "A Host", &["Guest"], "A Host Album");
add_track_with_featured(&lib, "Guest Two", "B Host", &["Guest"], "B Host Album");
let page = lib.artists(1, 10, artist_filters(false)).unwrap();
let zed_pos = page
.items
.iter()
.position(|artist| artist.name == "Zed")
.unwrap();
let guest_pos = page
.items
.iter()
.position(|artist| artist.name == "Guest")
.unwrap();
let guest = &page.items[guest_pos];
assert_eq!(guest.release_count, 0);
assert_eq!(guest.track_count, 2);
assert!(zed_pos < guest_pos);
let filtered = lib.artists(1, 10, artist_filters(true)).unwrap();
assert!(filtered.items.iter().all(|artist| artist.release_count > 0));
assert!(!filtered.items.iter().any(|artist| artist.name == "Guest"));
}
#[test]
fn network_artist_image_hint_becomes_local_image_after_fetch() {
let lib = test_library();
let artist_key = music_dht::normalize_name("Remote Artist");
lib.replace_network_artist_cache(
"peer-a",
"personal",
&[NetworkArtistPreview {
artist_key: artist_key.clone(),
name: "Remote Artist".into(),
image_path: Some("peer-local/image.jpg".into()),
release_count: 1,
track_count: 3,
}],
true,
)
.unwrap();
let filters = crate::config::settings::LibraryFilters {
source_mode: crate::config::settings::LibrarySourceMode::My,
..Default::default()
};
let page = lib.artists(1, 10, filters).unwrap();
assert_eq!(page.items[0].image_path, None);
let requests = lib
.network_artist_image_requests(filters, &["Remote Artist".into()], 8)
.unwrap();
assert_eq!(requests.len(), 1);
assert_eq!(requests[0].source_id, "peer-a");
assert_eq!(requests[0].artist_key, artist_key);
lib.set_network_artist_image("peer-a", &artist_key, "/tmp/remote-artist.jpg")
.unwrap();
let page = lib.artists(1, 10, filters).unwrap();
assert_eq!(
page.items[0].image_path.as_deref(),
Some("/tmp/remote-artist.jpg")
);
}
#[test]
fn import_creates_artist_release_track() {
let lib = test_library();
let track_id = add_track(&lib, "Song", "Artist", "Album");
let page = lib.artists(1, 10, artist_filters(false)).unwrap();
assert_eq!(page.total, 1);
assert_eq!(page.items[0].name, "Artist");
assert_eq!(page.items[0].track_count, 1);
let detail = lib.artist(page.items[0].id).unwrap();
assert_eq!(detail.releases.len(), 1);
assert_eq!(detail.top_tracks.len(), 1);
let release = lib.release(detail.releases[0].id).unwrap();
assert_eq!(release.tracks.len(), 1);
assert_eq!(release.tracks[0].id, track_id);
assert_eq!(release.tracks[0].artists[0].name, "Artist");
}
#[test]
fn reimport_updates_instead_of_duplicating() {
let lib = test_library();
let first = add_track(&lib, "Song", "Artist", "Album");
let second = add_track(&lib, "Song", "Artist", "Album");
assert_eq!(first, second);
let page = lib.artists(1, 10, artist_filters(false)).unwrap();
assert_eq!(page.items[0].track_count, 1);
}
#[test]
fn content_id_backfill_hashes_missing_track_ids() {
let lib = test_library();
let path = std::env::temp_dir().join(format!(
"furumi-content-id-test-{}-{}.bin",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::write(&path, b"portable content id").unwrap();
let file_path = path.to_string_lossy().into_owned();
let import = import::TrackImport {
release_type: None,
file_path: file_path.clone(),
title: "Portable".to_string(),
artists: vec!["Artist".to_string()],
featured_artists: Vec::new(),
album_artists: vec!["Artist".to_string()],
release_title: "Album".to_string(),
year: Some(2026),
track_number: None,
disc_number: None,
duration_seconds: 60.0,
audio_format: Some("bin".into()),
audio_bitrate: None,
audio_sample_rate: None,
audio_bit_depth: None,
file_size_bytes: Some(19),
cover: None,
};
let track_id = import::upsert_track(&lib, &import).unwrap().0;
let expected = audio_content_id(&file_path).unwrap();
{
let conn = lib.lock();
conn.execute(
"UPDATE tracks SET content_id = NULL WHERE id = ?1",
[track_id],
)
.unwrap();
}
let stats = lib.backfill_missing_content_ids().unwrap();
assert_eq!(stats.hashed, 1);
assert_eq!(stats.updated(), 1);
assert_eq!(
lib.track_content_id_by_id(track_id).unwrap().as_deref(),
Some(expected.as_str())
);
let _ = std::fs::remove_file(path);
}
#[test]
fn search_finds_all_kinds() {
let lib = test_library();
add_track(&lib, "Neon Lights", "Neon Artist", "Neon Album");
let results = lib.search("neon", 10).unwrap();
assert_eq!(results.artists.len(), 1);
assert_eq!(results.releases.len(), 1);
assert_eq!(results.tracks.len(), 1);
// LIKE wildcards in the query must not match everything.
assert_eq!(lib.search("%", 10).unwrap().len(), 0);
}
#[test]
fn search_ranks_exact_names_first() {
let lib = test_library();
add_track(&lib, "A Needle", "A Needle Artist", "A Needle Album");
add_track(&lib, "Needle", "Needle", "Needle");
let results = lib.search("needle", 10).unwrap();
assert_eq!(results.artists[0].name, "Needle");
assert_eq!(results.releases[0].title, "Needle");
assert_eq!(results.tracks[0].title, "Needle");
}
#[test]
fn search_folds_case_beyond_ascii() {
let lib = test_library();
add_track(&lib, "Nothing Else Matters", "Металлика", "Чёрный альбом");
// SQLite's LIKE/NOCASE only fold ASCII; norm() folds every script.
assert_eq!(lib.search("металлика", 10).unwrap().artists.len(), 1);
assert_eq!(lib.search("МЕТАЛЛИКА", 10).unwrap().artists.len(), 1);
assert_eq!(lib.search("чёрный", 10).unwrap().releases.len(), 1);
assert_eq!(lib.search("matters", 10).unwrap().tracks.len(), 1);
}
#[test]
fn playlists_and_likes_round_trip() {
let lib = test_library();
let track_id = add_track(&lib, "Song", "Artist", "Album");
let playlist = lib.create_playlist("Mix").unwrap();
lib.add_tracks_to_playlist(playlist.id, &[track_id])
.unwrap();
assert_eq!(lib.playlist(playlist.id).unwrap().tracks.len(), 1);
let content_id = lib.track_content_id_by_id(track_id).unwrap().unwrap();
assert!(lib.toggle_like_by_content_id(&content_id).unwrap());
assert_eq!(lib.liked_content_ids().unwrap(), vec![content_id.clone()]);
assert_eq!(lib.playlist(LIKES_PLAYLIST_ID).unwrap().tracks.len(), 1);
assert!(!lib.toggle_like_by_content_id(&content_id).unwrap());
lib.remove_tracks_from_playlist(playlist.id, &[track_id])
.unwrap();
assert_eq!(lib.playlist(playlist.id).unwrap().tracks.len(), 0);
lib.delete_playlist(playlist.id).unwrap();
// Only the virtual Likes playlist remains.
assert_eq!(lib.playlists().unwrap().len(), 1);
}
#[test]
fn likes_playlist_orders_local_and_federated_by_liked_at() {
let lib = test_library();
let old_id = add_track(&lib, "Old Local", "Artist", "Album");
let new_id = add_track(&lib, "New Local", "Artist", "Album");
let old_content_id = lib.track_content_id_by_id(old_id).unwrap().unwrap();
let new_content_id = lib.track_content_id_by_id(new_id).unwrap().unwrap();
let content_id = format!("b3:{}", "c".repeat(64));
let fed = crate::federation::FedTrack {
item_id: "fed_item_order".to_string(),
owner: "fed_owner_order".to_string(),
own: false,
title: "Middle Fed".to_string(),
artist_names: vec!["Remote Artist".to_string()],
featured_artist_names: Vec::new(),
year: Some(2026),
duration_seconds: Some(123),
content_id: Some(content_id),
release_title: Some("Remote Release".to_string()),
track_number: Some(1),
disc_number: Some(1),
};
assert!(lib.toggle_like_by_content_id(&old_content_id).unwrap());
assert!(lib.toggle_like_by_content_id(&new_content_id).unwrap());
assert!(lib.toggle_fed_like(&fed).unwrap());
{
let conn = lib.lock();
conn.execute(
"UPDATE likes SET liked_at = ?2 WHERE track_id = ?1",
params![old_id, "2026-01-01 00:00:00"],
)
.unwrap();
conn.execute(
"UPDATE likes SET liked_at = ?2 WHERE track_id = ?1",
params![new_id, "2026-01-02 00:00:00"],
)
.unwrap();
conn.execute(
"UPDATE fed_likes SET liked_at = ?2 WHERE item_id = ?1",
params![fed.item_id, "2026-01-03 00:00:00"],
)
.unwrap();
}
let titles: Vec<String> = lib
.playlist(LIKES_PLAYLIST_ID)
.unwrap()
.tracks
.into_iter()
.map(|track| track.title)
.collect();
assert_eq!(titles, vec!["Middle Fed", "New Local", "Old Local"]);
assert!(!lib.toggle_like_by_content_id(&old_content_id).unwrap());
assert!(lib.toggle_like_by_content_id(&old_content_id).unwrap());
{
let conn = lib.lock();
conn.execute(
"UPDATE likes SET liked_at = ?2 WHERE track_id = ?1",
params![old_id, "2026-01-04 00:00:00"],
)
.unwrap();
}
let titles: Vec<String> = lib
.playlist(LIKES_PLAYLIST_ID)
.unwrap()
.tracks
.into_iter()
.map(|track| track.title)
.collect();
assert_eq!(titles, vec!["Old Local", "Middle Fed", "New Local"]);
}
#[test]
fn synced_playlist_can_show_federated_pending_tracks() {
let lib = test_library();
let playlist = lib.create_playlist("Remote Mix").unwrap();
let sync_id = lib.ensure_playlist_sync_id(playlist.id).unwrap();
let content_id = format!("b3:{}", "a".repeat(64));
let fed = crate::federation::FedTrack {
item_id: "fed_item_1".to_string(),
owner: "fed_owner_1".to_string(),
own: false,
title: "Remote Song".to_string(),
artist_names: vec!["Remote Artist".to_string()],
featured_artist_names: vec!["Remote Guest".to_string()],
year: Some(2026),
duration_seconds: Some(123),
content_id: Some(content_id.clone()),
release_title: Some("Remote Release".to_string()),
track_number: Some(2),
disc_number: Some(1),
};
assert!(lib.upsert_fed_playlist_track(&sync_id, &fed, 4).unwrap());
assert!(
lib.has_playlist_content_reference(&sync_id, &content_id)
.unwrap()
);
let detail = lib.playlist(playlist.id).unwrap();
assert_eq!(detail.tracks.len(), 1);
let track = &detail.tracks[0];
assert!(track.is_fed_pending());
assert_eq!(track.title, "Remote Song");
assert_eq!(track.artist_line(), "Remote Artist feat. Remote Guest");
assert_eq!(track.release_title, "Remote Release");
assert_eq!(track.content_id.as_deref(), Some(content_id.as_str()));
let card = lib
.playlists()
.unwrap()
.into_iter()
.find(|card| card.id == playlist.id)
.unwrap();
assert_eq!(card.track_count, 1);
lib.remove_content_ids_from_playlist(playlist.id, std::slice::from_ref(&content_id))
.unwrap();
assert_eq!(lib.playlist(playlist.id).unwrap().tracks.len(), 0);
assert!(
lib.fed_playlist_track_by_content_id(&sync_id, &content_id)
.unwrap()
.is_none()
);
}
#[test]
fn add_federated_pending_track_to_playlist_records_position() {
let lib = test_library();
let local_id = add_track(&lib, "Local Song", "Artist", "Album");
let playlist = lib.create_playlist("Remote Mix").unwrap();
let content_id = format!("b3:{}", "b".repeat(64));
let fed = crate::federation::FedTrack {
item_id: "fed_item_2".to_string(),
owner: "fed_owner_2".to_string(),
own: false,
title: "Remote Song".to_string(),
artist_names: vec!["Remote Artist".to_string()],
featured_artist_names: Vec::new(),
year: Some(2026),
duration_seconds: Some(123),
content_id: Some(content_id.clone()),
release_title: Some("Remote Release".to_string()),
track_number: Some(2),
disc_number: Some(1),
};
lib.add_tracks_to_playlist(playlist.id, &[local_id])
.unwrap();
lib.add_fed_tracks_to_playlist(playlist.id, std::slice::from_ref(&fed))
.unwrap();
let position = lib
.playlist_content_position(playlist.id, &content_id)
.unwrap();
assert_eq!(position, Some(1));
let detail = lib.playlist(playlist.id).unwrap();
assert_eq!(
detail
.tracks
.into_iter()
.map(|track| track.title)
.collect::<Vec<_>>(),
vec!["Local Song", "Remote Song"]
);
}
#[test]
fn track_edit_relinks_artists() {
let lib = test_library();
let track_id = add_track(&lib, "Song", "Artist", "Album");
lib.update_track(
track_id,
&TrackEdit {
title: "Renamed".into(),
artists: vec!["Other".into()],
featured_artists: vec!["Guest".into()],
track_number: Some(2),
disc_number: None,
cover_path: None,
},
)
.unwrap();
let track = lib.tracks_by_ids(&[track_id]).unwrap().remove(0);
assert_eq!(track.title, "Renamed");
assert_eq!(track.artists[0].name, "Other");
assert_eq!(track.featured_artists[0].name, "Guest");
assert_eq!(track.track_number, Some(2));
}
#[test]
fn deleting_artist_cleans_up_own_content() {
let lib = test_library();
add_track(&lib, "Song", "Solo", "Solo Album");
let page = lib.artists(1, 10, artist_filters(false)).unwrap();
lib.delete_artist(page.items[0].id).unwrap();
assert_eq!(lib.artists(1, 10, artist_filters(false)).unwrap().total, 0);
assert_eq!(lib.search("Song", 10).unwrap().len(), 0);
}
#[test]
fn delete_track_drops_empty_release() {
let lib = test_library();
let track_id = add_track(&lib, "Only", "Artist", "Album");
lib.delete_track(track_id).unwrap();
let detail = lib
.artist(lib.artists(1, 10, artist_filters(false)).unwrap().items[0].id)
.unwrap();
assert!(detail.releases.is_empty());
}
#[test]
fn history_counts_completed_plays() {
let lib = test_library();
let track_id = add_track(&lib, "Song", "Artist", "Album");
let content_id = lib
.tracks_by_ids(&[track_id])
.unwrap()
.remove(0)
.content_id
.unwrap();
let event = music_dht::device_sync::ListenEvent {
listen_id: "listen-1".to_string(),
content_id,
started_at_ms: 1_700_000_000_000,
listened_ms: 60_000,
track_duration_ms: Some(60_000),
ended_reason: music_dht::device_sync::ListenEndReason::Finished,
track: music_dht::device_sync::ListenTrackMetadata {
title: "Song".to_string(),
artist_names: vec!["Artist".to_string()],
featured_artist_names: Vec::new(),
release_title: Some("Album".to_string()),
},
};
assert!(lib.apply_listen_event(&event, "device-a").unwrap());
assert!(!lib.apply_listen_event(&event, "device-a").unwrap());
let track = lib.tracks_by_ids(&[track_id]).unwrap().remove(0);
assert_eq!(track.play_count, 1);
let history = lib.listen_history(20).unwrap();
assert_eq!(history.len(), 1);
assert_eq!(history[0].listen_id, "listen-1");
assert_eq!(history[0].title, "Song");
assert_eq!(history[0].artist, "Artist");
assert_eq!(history[0].origin_device_id, "device-a");
}
#[test]
fn listen_history_hides_unqualified_events_and_keeps_remote_metadata() {
let lib = test_library();
let event = music_dht::device_sync::ListenEvent {
listen_id: "remote-listen".to_string(),
content_id: format!("b3:{}", "a".repeat(64)),
started_at_ms: 1_700_000_000_000,
listened_ms: 10_000,
track_duration_ms: Some(120_000),
ended_reason: music_dht::device_sync::ListenEndReason::Skipped,
track: music_dht::device_sync::ListenTrackMetadata {
title: "Remote song".to_string(),
artist_names: vec!["Remote artist".to_string()],
featured_artist_names: vec!["Guest".to_string()],
release_title: None,
},
};
assert!(lib.apply_listen_event(&event, "remote-device").unwrap());
assert!(lib.listen_history(20).unwrap().is_empty());
}
#[test]
fn similarity_embeddings_round_trip_and_keep_profiles_separate() {
let lib = test_library();
let track_id = add_track(&lib, "Song", "Artist", "Album");
for profile in ["profile-a", "profile-b"] {
lib.ensure_similarity_profile(profile, "model", "1", "sha", "prep", 3)
.unwrap();
}
let track = lib
.pending_similarity_tracks("profile-a")
.unwrap()
.into_iter()
.find(|track| track.id == track_id)
.unwrap();
lib.store_similarity_embedding(&track, "profile-a", &[0.1, 0.2, 0.3])
.unwrap();
lib.store_similarity_embedding(&track, "profile-b", &[0.3, 0.2, 0.1])
.unwrap();
assert_eq!(
lib.similarity_embedding(track_id, "profile-a").unwrap(),
Some(vec![0.1, 0.2, 0.3])
);
assert_eq!(
lib.similarity_embedding(track_id, "profile-b").unwrap(),
Some(vec![0.3, 0.2, 0.1])
);
let stats = lib.similarity_storage_stats("profile-a").unwrap();
assert_eq!(stats.total_tracks, 1);
assert_eq!(stats.embedded_tracks, 1);
assert_eq!(stats.stored_vectors, 2);
assert_eq!(stats.stored_bytes, 24);
}
#[test]
fn changed_content_id_invalidates_only_the_stale_embedding() {
let lib = test_library();
let track_id = add_track(&lib, "Song", "Artist", "Album");
lib.ensure_similarity_profile("profile", "model", "1", "sha", "prep", 2)
.unwrap();
let track = lib.pending_similarity_tracks("profile").unwrap().remove(0);
lib.store_similarity_embedding(&track, "profile", &[0.6, 0.8])
.unwrap();
lib.lock()
.execute(
"UPDATE tracks SET content_id = ?2 WHERE id = ?1",
params![track_id, format!("b3:{}", "f".repeat(64))],
)
.unwrap();
assert_eq!(lib.pending_similarity_tracks("profile").unwrap().len(), 1);
assert_eq!(lib.similarity_embedding(track_id, "profile").unwrap(), None);
assert!(lib.load_similarity_index("profile").unwrap().is_empty());
}
#[test]
fn clearing_embeddings_preserves_the_library() {
let lib = test_library();
let track_id = add_track(&lib, "Song", "Artist", "Album");
lib.ensure_similarity_profile("profile", "model", "1", "sha", "prep", 2)
.unwrap();
let track = lib.pending_similarity_tracks("profile").unwrap().remove(0);
lib.store_similarity_embedding(&track, "profile", &[0.6, 0.8])
.unwrap();
lib.clear_similarity_embeddings().unwrap();
assert_eq!(lib.tracks_by_ids(&[track_id]).unwrap().len(), 1);
assert!(
lib.similarity_embedding(track_id, "profile")
.unwrap()
.is_none()
);
}
+109 -16
View File
@@ -7,11 +7,13 @@ mod jam;
mod library;
mod media;
mod player;
mod podcasts;
mod share;
mod similarity;
mod status;
mod streaming;
mod ui;
mod updater;
mod visualizer;
use std::io;
@@ -60,7 +62,9 @@ fn main() -> Result<()> {
if let Err(err) = config::logging::init() {
startup_warning = Some(format!("logging disabled: {err:#}"));
}
capture_stderr();
// Restore stderr before main returns: Rust prints a returned error only
// after local guards have been dropped and the terminal has been restored.
let _stderr_capture = capture_stderr();
let (keymap, keymap_warning) = config::keymap::Keymap::load();
let startup_warning = keymap_warning.or(startup_warning);
@@ -131,25 +135,46 @@ fn run_app(
/// every line into tracing — it lands in the Logs tab and the log file
/// instead of the screen.
#[cfg(unix)]
fn capture_stderr() {
struct StderrCapture {
original: std::os::fd::RawFd,
}
#[cfg(unix)]
impl Drop for StderrCapture {
fn drop(&mut self) {
// SAFETY: original is our owned duplicate, kept open for this scope.
unsafe {
libc::dup2(self.original, libc::STDERR_FILENO);
libc::close(self.original);
}
}
}
#[cfg(unix)]
fn capture_stderr() -> Option<StderrCapture> {
use std::io::BufRead as _;
use std::os::fd::FromRawFd as _;
let mut fds = [0i32; 2];
// SAFETY: plain pipe/dup2 syscalls on freshly created fds.
unsafe {
let original = libc::dup(libc::STDERR_FILENO);
if original == -1 {
return None;
}
let capture = StderrCapture { original };
if libc::pipe(fds.as_mut_ptr()) != 0 {
return;
return None;
}
let [read_fd, write_fd] = fds;
if libc::dup2(write_fd, libc::STDERR_FILENO) == -1 {
libc::close(read_fd);
libc::close(write_fd);
return;
return None;
}
libc::close(write_fd);
let reader = std::fs::File::from_raw_fd(read_fd);
std::thread::Builder::new()
let reader_thread = std::thread::Builder::new()
.name("stderr".to_string())
.spawn(move || {
for line in std::io::BufReader::new(reader).lines() {
@@ -158,33 +183,65 @@ fn capture_stderr() {
tracing::warn!(target: "stderr", "{line}");
}
}
})
.ok();
});
if reader_thread.is_err() {
return None;
}
Some(capture)
}
}
#[cfg(windows)]
fn capture_stderr() {
struct StderrCapture {
original: windows_sys::Win32::Foundation::HANDLE,
writer: windows_sys::Win32::Foundation::HANDLE,
}
#[cfg(windows)]
impl Drop for StderrCapture {
fn drop(&mut self) {
// SAFETY: original is borrowed from the process; writer is owned by
// this guard. Restoring the original also preserves shell redirection.
unsafe {
windows_sys::Win32::System::Console::SetStdHandle(
windows_sys::Win32::System::Console::STD_ERROR_HANDLE,
self.original,
);
windows_sys::Win32::Foundation::CloseHandle(self.writer);
}
}
}
#[cfg(windows)]
fn capture_stderr() -> Option<StderrCapture> {
use std::io::BufRead as _;
use std::os::windows::io::FromRawHandle as _;
use windows_sys::Win32::Foundation::CloseHandle;
use windows_sys::Win32::System::Console::{STD_ERROR_HANDLE, SetStdHandle};
use windows_sys::Win32::Foundation::{CloseHandle, INVALID_HANDLE_VALUE};
use windows_sys::Win32::System::Console::{GetStdHandle, STD_ERROR_HANDLE, SetStdHandle};
use windows_sys::Win32::System::Pipes::CreatePipe;
unsafe {
let original = GetStdHandle(STD_ERROR_HANDLE);
if original.is_null() || original == INVALID_HANDLE_VALUE {
return None;
}
let mut read = core::ptr::null_mut();
let mut write = core::ptr::null_mut();
if CreatePipe(&mut read, &mut write, core::ptr::null(), 0) == 0 {
return;
return None;
}
if SetStdHandle(STD_ERROR_HANDLE, write) == 0 {
CloseHandle(read);
CloseHandle(write);
return;
return None;
}
let capture = StderrCapture {
original,
writer: write,
};
let reader = std::fs::File::from_raw_handle(read);
std::thread::Builder::new()
let reader_thread = std::thread::Builder::new()
.name("stderr".to_string())
.spawn(move || {
for line in std::io::BufReader::new(reader).lines() {
@@ -193,13 +250,49 @@ fn capture_stderr() {
tracing::warn!(target: "stderr", "{line}");
}
}
})
.ok();
});
if reader_thread.is_err() {
return None;
}
Some(capture)
}
}
#[cfg(not(any(unix, windows)))]
fn capture_stderr() {}
fn capture_stderr() -> Option<()> {
None
}
#[cfg(test)]
mod stderr_tests {
#[test]
fn error_output_is_restored_after_capture() {
const CHILD: &str = "FURUMI_STDERR_CAPTURE_TEST";
if std::env::var_os(CHILD).is_some() {
{
let _capture = super::capture_stderr().expect("capture stderr");
}
eprintln!("furumi-test: visible error after terminal shutdown");
return;
}
// Run in another process: redirecting global stderr inside a parallel
// test suite would interfere with unrelated tests and panic reporting.
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"stderr_tests::error_output_is_restored_after_capture",
"--nocapture",
])
.env(CHILD, "1")
.output()
.unwrap();
assert!(output.status.success());
assert!(
String::from_utf8_lossy(&output.stderr)
.contains("furumi-test: visible error after terminal shutdown")
);
}
}
/// Kitty keyboard protocol, where supported, disambiguates Esc from alt-keys
/// and modifier combos. The flags are popped on exit and on panic — leaving
+155 -56
View File
@@ -5,13 +5,14 @@
//! the UI or app state.
mod analyzer;
mod opus;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender};
use std::time::Duration;
use rodio::{Decoder, DeviceSinkBuilder, Player, stream::MixerDeviceSink};
use rodio::{Decoder, DeviceSinkBuilder, Player, Source, stream::MixerDeviceSink};
pub use analyzer::AudioAnalysisSnapshot;
@@ -50,6 +51,12 @@ enum Command {
seekable: bool,
volume: f32,
},
/// Play a source that was already probed and decoded off this thread
/// (remote podcast streams, whose probing waits on the network).
PlaySource {
source: DecodedSource,
volume: f32,
},
/// Append the next track behind the current one without interrupting
/// playback — rodio switches sources back to back (gapless-ish).
Enqueue {
@@ -89,9 +96,18 @@ impl Shared {
pub struct Controller {
tx: Sender<Command>,
pub shared: Arc<Shared>,
playback_allowed: Arc<AtomicBool>,
}
impl Controller {
/// Revoke the output at the audio thread boundary, including work queued
/// by background decoders before an ownership transfer.
pub fn set_playback_allowed(&self, allowed: bool) {
if self.playback_allowed.swap(allowed, Ordering::AcqRel) && !allowed {
self.stop();
}
}
pub fn play(&self, reader: TrackReader, byte_len: Option<u64>, volume: f32) {
let _ = self.tx.send(Command::Play {
reader,
@@ -112,6 +128,12 @@ impl Controller {
});
}
/// Play an already decoded source, built with `decode_source_with`
/// outside the audio thread.
pub fn play_source(&self, source: DecodedSource, volume: f32) {
let _ = self.tx.send(Command::PlaySource { source, volume });
}
pub fn enqueue(&self, reader: TrackReader, byte_len: Option<u64>) {
let _ = self.tx.send(Command::Enqueue { reader, byte_len });
}
@@ -141,11 +163,17 @@ pub fn spawn(on_event: impl Fn(PlayerEvent) + Send + 'static) -> Controller {
let (tx, rx) = std::sync::mpsc::channel();
let shared = Arc::new(Shared::default());
let thread_shared = Arc::clone(&shared);
let playback_allowed = Arc::new(AtomicBool::new(true));
let thread_allowed = Arc::clone(&playback_allowed);
std::thread::Builder::new()
.name("audio".to_string())
.spawn(move || run(rx, thread_shared, on_event))
.spawn(move || run(rx, thread_shared, thread_allowed, on_event))
.expect("spawning the audio thread cannot fail");
Controller { tx, shared }
Controller {
tx,
shared,
playback_allowed,
}
}
struct Output {
@@ -154,7 +182,12 @@ struct Output {
player: Player,
}
fn run(rx: Receiver<Command>, shared: Arc<Shared>, on_event: impl Fn(PlayerEvent)) {
fn run(
rx: Receiver<Command>,
shared: Arc<Shared>,
playback_allowed: Arc<AtomicBool>,
on_event: impl Fn(PlayerEvent),
) {
let mut output: Option<Output> = None;
let mut track_loaded = false;
let mut last_len = 0usize;
@@ -162,6 +195,17 @@ fn run(rx: Receiver<Command>, shared: Arc<Shared>, on_event: impl Fn(PlayerEvent
loop {
match rx.recv_timeout(Duration::from_millis(100)) {
Ok(command) => {
if !playback_allowed.load(Ordering::Acquire)
&& matches!(
command,
Command::Play { .. }
| Command::PlaySource { .. }
| Command::Enqueue { .. }
| Command::Resume
)
{
continue;
}
// Commands change the source queue legitimately; resync the
// length so the next tick doesn't read it as a track ending.
handle(command, &shared, &mut output, &mut track_loaded, &on_event);
@@ -207,67 +251,28 @@ fn handle(
seekable,
volume,
} => {
// The device is opened lazily on first playback so the app works
// on machines with no audio output until you actually press play.
if output.is_none() {
match DeviceSinkBuilder::open_default_sink() {
Ok(device) => {
let player = Player::connect_new(device.mixer());
*output = Some(Output {
_device: device,
player,
});
}
Err(err) => {
on_event(PlayerEvent::Failed(format!("no audio device: {err}")));
return;
}
}
}
let out = output.as_ref().expect("output opened above");
let mut builder = Decoder::builder()
.with_data(reader)
.with_seekable(seekable)
.with_gapless(true);
if let Some(len) = byte_len {
builder = builder.with_byte_len(len);
}
if let Some(mime_type) = mime_type.as_deref() {
builder = builder.with_mime_type(mime_type);
}
match builder.build() {
Ok(decoder) => {
shared.analysis.clear();
out.player.stop();
out.player.set_volume(volume);
out.player.append(analyzer::AnalyzedSource::new(
decoder,
Arc::clone(&shared.analysis),
));
out.player.play();
*track_loaded = true;
on_event(PlayerEvent::Started);
}
let Some(out) = ensure_output(output, on_event) else {
return;
};
match decode_source(reader, byte_len, mime_type.as_deref(), seekable) {
Ok(source) => start_source(out, source, volume, shared, track_loaded, on_event),
Err(err) => {
on_event(PlayerEvent::Failed(format!("cannot decode track: {err}")));
}
}
}
Command::PlaySource { source, volume } => {
if let Some(out) = ensure_output(output, on_event) {
start_source(out, source, volume, shared, track_loaded, on_event);
}
}
Command::Enqueue { reader, byte_len } => {
let Some(out) = output.as_ref() else {
return;
};
let mut builder = Decoder::builder()
.with_data(reader)
.with_seekable(true)
.with_gapless(true);
if let Some(len) = byte_len {
builder = builder.with_byte_len(len);
}
match builder.build() {
Ok(decoder) => out.player.append(analyzer::AnalyzedSource::new(
decoder,
match decode_source(reader, byte_len, None, true) {
Ok(source) => out.player.append(analyzer::AnalyzedSource::new(
source,
Arc::clone(&shared.analysis),
)),
Err(err) => {
@@ -309,3 +314,97 @@ fn handle(
}
}
}
/// The device is opened lazily on first playback so the app works on
/// machines with no audio output until you actually press play.
fn ensure_output<'a>(
output: &'a mut Option<Output>,
on_event: &impl Fn(PlayerEvent),
) -> Option<&'a Output> {
if output.is_none() {
match DeviceSinkBuilder::open_default_sink() {
Ok(device) => {
let player = Player::connect_new(device.mixer());
*output = Some(Output {
_device: device,
player,
});
}
Err(err) => {
on_event(PlayerEvent::Failed(format!("no audio device: {err}")));
return None;
}
}
}
output.as_ref()
}
/// Replace whatever is playing with `source`.
fn start_source(
out: &Output,
source: DecodedSource,
volume: f32,
shared: &Shared,
track_loaded: &mut bool,
on_event: &impl Fn(PlayerEvent),
) {
shared.analysis.clear();
out.player.stop();
out.player.set_volume(volume);
out.player.append(analyzer::AnalyzedSource::new(
source,
Arc::clone(&shared.analysis),
));
out.player.play();
*track_loaded = true;
on_event(PlayerEvent::Started);
}
pub(crate) type DecodedSource = Box<dyn Source + Send>;
pub(crate) fn decode_source(
reader: TrackReader,
byte_len: Option<u64>,
mime_type: Option<&str>,
seekable: bool,
) -> Result<DecodedSource, String> {
decode_source_with(reader, byte_len, mime_type, seekable, false)
}
/// `decode_source` with optional coarse (byte-estimate) seeking, which
/// remote streams need: an accurate MP3 seek decodes every frame before the
/// target, i.e. downloads it.
pub(crate) fn decode_source_with(
mut reader: TrackReader,
byte_len: Option<u64>,
mime_type: Option<&str>,
seekable: bool,
coarse_seek: bool,
) -> Result<DecodedSource, String> {
let mime_is_opus = mime_type.is_some_and(|mime| {
let mime = mime.to_ascii_lowercase();
mime == "audio/opus" || mime.contains("codecs=opus") || mime.contains("codecs=\"opus\"")
});
let ogg_is_opus = opus::is_ogg_opus(&mut reader)
.map_err(|error| format!("cannot inspect audio stream: {error}"))?;
if mime_is_opus || ogg_is_opus {
return opus::OggOpusSource::new(reader, byte_len, seekable)
.map(|source| Box::new(source) as DecodedSource);
}
let mut builder = Decoder::builder()
.with_data(reader)
.with_seekable(seekable)
.with_coarse_seek(coarse_seek)
.with_gapless(true);
if let Some(len) = byte_len {
builder = builder.with_byte_len(len);
}
if let Some(mime_type) = mime_type {
builder = builder.with_mime_type(mime_type);
}
builder
.build()
.map(|decoder| Box::new(decoder) as DecodedSource)
.map_err(|error| error.to_string())
}
+433
View File
@@ -0,0 +1,433 @@
//! Ogg/Opus decoding for rodio.
//!
//! Rodio 0.22 can demux Ogg and decode Vorbis, but its Symphonia version does
//! not include an Opus decoder. This source keeps Symphonia's mature Ogg
//! demuxing and feeds the packets into the pure-Rust `rusty-opus` decoder.
use std::io::{self, Read, Seek, SeekFrom};
use std::sync::Arc;
use std::time::Duration;
use rodio::source::SeekError;
use rodio::{ChannelCount, SampleRate, Source};
use rusty_opus::OpusDecoder;
use symphonia::core::codecs::{CODEC_TYPE_OPUS, CodecParameters};
use symphonia::core::errors::Error as SymphoniaError;
use symphonia::core::formats::{FormatOptions, FormatReader, SeekMode, SeekTo};
use symphonia::core::io::{MediaSource, MediaSourceStream};
use symphonia::core::meta::MetadataOptions;
use symphonia::core::probe::Hint;
use super::TrackReader;
const OPUS_SAMPLE_RATE: u32 = 48_000;
const OPUS_SEEK_PREROLL_FRAMES: u64 = OPUS_SAMPLE_RATE as u64 * 80 / 1_000;
const SNIFF_BYTES: usize = 512;
/// Inspect the Ogg identification page without changing the reader position.
pub(super) fn is_ogg_opus(reader: &mut TrackReader) -> io::Result<bool> {
let position = reader.stream_position()?;
let mut header = [0; SNIFF_BYTES];
let read_result = reader.read(&mut header);
let rewind_result = reader.seek(SeekFrom::Start(position));
let read = read_result?;
rewind_result?;
Ok(header[..read].starts_with(b"OggS")
&& header[..read]
.windows(b"OpusHead".len())
.any(|window| window == b"OpusHead"))
}
struct ReaderMediaSource {
reader: TrackReader,
byte_len: Option<u64>,
seekable: bool,
}
impl Read for ReaderMediaSource {
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
self.reader.read(buffer)
}
}
impl Seek for ReaderMediaSource {
fn seek(&mut self, position: SeekFrom) -> io::Result<u64> {
self.reader.seek(position)
}
}
impl MediaSource for ReaderMediaSource {
fn is_seekable(&self) -> bool {
self.seekable
}
fn byte_len(&self) -> Option<u64> {
self.byte_len
}
}
pub(super) struct OggOpusSource {
format: Box<dyn FormatReader>,
decoder: OpusDecoder,
track_id: u32,
channels: u16,
pre_skip: u64,
playable_frames: Option<u64>,
output_position_frames: u64,
discard_frames: u64,
output_gain: f32,
buffer: Vec<f32>,
scratch: Vec<f32>,
buffer_position: usize,
seekable: bool,
done: bool,
}
impl OggOpusSource {
pub(super) fn new(
reader: TrackReader,
byte_len: Option<u64>,
seekable: bool,
) -> Result<Self, String> {
let source = ReaderMediaSource {
reader,
byte_len,
seekable,
};
let stream = MediaSourceStream::new(Box::new(source), Default::default());
let mut hint = Hint::new();
hint.with_extension("ogg");
hint.mime_type("audio/ogg");
let format_options = FormatOptions {
enable_gapless: false,
..Default::default()
};
let probed = symphonia::default::get_probe()
.format(&hint, stream, &format_options, &MetadataOptions::default())
.map_err(|error| format!("cannot read Ogg container: {error}"))?;
let format = probed.format;
let (track_id, channels, pre_skip, playable_frames, output_gain) = {
let track = format
.default_track()
.ok_or_else(|| "Ogg container has no audio track".to_string())?;
if track.codec_params.codec != CODEC_TYPE_OPUS {
return Err("Ogg track is not Opus".to_string());
}
let channels = track
.codec_params
.channels
.map(|channels| channels.count() as u16)
.ok_or_else(|| "Opus track has no channel layout".to_string())?;
if !(1..=2).contains(&channels) {
return Err(format!(
"Opus track has {channels} channels; only mono and stereo are supported"
));
}
let extra_data = track.codec_params.extra_data.as_deref();
let pre_skip = opus_pre_skip(extra_data);
let playable_frames = opus_playable_frames(&track.codec_params, pre_skip);
let output_gain = opus_output_gain(extra_data);
(track.id, channels, pre_skip, playable_frames, output_gain)
};
let decoder = OpusDecoder::new(OPUS_SAMPLE_RATE as i32, usize::from(channels))
.map_err(|error| format!("cannot initialize Opus decoder: {error}"))?;
let mut source = Self {
format,
decoder,
track_id,
channels,
pre_skip,
playable_frames,
output_position_frames: 0,
discard_frames: pre_skip,
output_gain,
buffer: Vec::new(),
scratch: Vec::new(),
buffer_position: 0,
seekable,
done: false,
};
source.fill_buffer();
if source.buffer.is_empty() {
return Err("Opus track contains no decodable audio".to_string());
}
Ok(source)
}
fn fill_buffer(&mut self) {
self.buffer.clear();
self.buffer_position = 0;
while self.buffer.is_empty() && !self.done {
let packet = match self.format.next_packet() {
Ok(packet) => packet,
Err(SymphoniaError::IoError(error))
if error.kind() == io::ErrorKind::UnexpectedEof =>
{
self.done = true;
break;
}
Err(error) => {
tracing::warn!(%error, "Ogg/Opus demux failed");
self.done = true;
break;
}
};
if packet.track_id() != self.track_id {
continue;
}
let Ok(decoded_frames) = usize::try_from(packet.dur) else {
tracing::warn!("Ogg/Opus packet duration is too large");
self.done = true;
break;
};
if decoded_frames == 0 {
continue;
}
let sample_count = match decoded_frames.checked_mul(usize::from(self.channels)) {
Some(sample_count) => sample_count,
None => {
tracing::warn!("Ogg/Opus packet sample count overflow");
self.done = true;
break;
}
};
self.scratch.resize(sample_count, 0.0);
let frames = match self
.decoder
.decode(&packet.data, decoded_frames, &mut self.scratch)
{
Ok(frames) => frames,
Err(error) => {
tracing::warn!(%error, "Opus packet decode failed");
continue;
}
};
let discarded = self.discard_frames.min(frames as u64) as usize;
self.discard_frames -= discarded as u64;
let start_frame = discarded;
let remaining = self
.playable_frames
.map(|total| total.saturating_sub(self.output_position_frames))
.unwrap_or(u64::MAX);
let end_frame = frames
.min(start_frame.saturating_add(usize::try_from(remaining).unwrap_or(usize::MAX)));
if start_frame >= end_frame {
if self
.playable_frames
.is_some_and(|total| self.output_position_frames >= total)
{
self.done = true;
}
continue;
}
let channels = usize::from(self.channels);
let start = start_frame * channels;
let end = end_frame.saturating_mul(channels).min(self.scratch.len());
self.output_position_frames = self
.output_position_frames
.saturating_add((end_frame - start_frame) as u64);
self.buffer.extend(
self.scratch[start..end]
.iter()
.map(|sample| (sample * self.output_gain).clamp(-1.0, 1.0)),
);
if self
.playable_frames
.is_some_and(|total| self.output_position_frames >= total)
{
self.done = true;
}
}
}
fn reset_decoder(&mut self) -> Result<(), SeekError> {
self.decoder = OpusDecoder::new(OPUS_SAMPLE_RATE as i32, usize::from(self.channels))
.map_err(|error| seek_error(format!("cannot reset Opus decoder: {error}")))?;
Ok(())
}
}
impl Iterator for OggOpusSource {
type Item = f32;
fn next(&mut self) -> Option<Self::Item> {
loop {
if self.buffer_position < self.buffer.len() {
let sample = self.buffer[self.buffer_position];
self.buffer_position += 1;
return Some(sample);
}
if self.done {
return None;
}
self.fill_buffer();
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.buffer.len().saturating_sub(self.buffer_position), None)
}
}
impl Source for OggOpusSource {
fn current_span_len(&self) -> Option<usize> {
Some(self.buffer.len())
}
fn channels(&self) -> ChannelCount {
ChannelCount::new(self.channels).expect("Opus channel count was validated")
}
fn sample_rate(&self) -> SampleRate {
SampleRate::new(OPUS_SAMPLE_RATE).expect("Opus sample rate is non-zero")
}
fn total_duration(&self) -> Option<Duration> {
self.playable_frames
.map(|frames| Duration::from_secs_f64(frames as f64 / f64::from(OPUS_SAMPLE_RATE)))
}
fn try_seek(&mut self, position: Duration) -> Result<(), SeekError> {
if !self.seekable {
return Err(SeekError::NotSupported {
underlying_source: std::any::type_name::<Self>(),
});
}
let requested = duration_frames(position);
let target = self
.playable_frames
.map_or(requested, |total| requested.min(total));
let raw_target = target.saturating_add(self.pre_skip);
let preroll = raw_target.saturating_sub(OPUS_SEEK_PREROLL_FRAMES);
let seeked = self
.format
.seek(
SeekMode::Accurate,
SeekTo::TimeStamp {
ts: preroll,
track_id: self.track_id,
},
)
.map_err(|error| seek_error(format!("Ogg seek failed: {error}")))?;
self.reset_decoder()?;
self.buffer.clear();
self.buffer_position = 0;
self.output_position_frames = target;
self.discard_frames = raw_target.saturating_sub(seeked.actual_ts);
self.done = false;
self.fill_buffer();
Ok(())
}
}
fn duration_frames(duration: Duration) -> u64 {
let frames = duration.as_secs_f64() * f64::from(OPUS_SAMPLE_RATE);
frames.round().clamp(0.0, u64::MAX as f64) as u64
}
fn opus_pre_skip(extra_data: Option<&[u8]>) -> u64 {
extra_data
.filter(|header| header.len() >= 12)
.map(|header| u64::from(u16::from_le_bytes([header[10], header[11]])))
.unwrap_or(0)
}
fn opus_playable_frames(params: &CodecParameters, pre_skip: u64) -> Option<u64> {
let encoded_frames = params.n_frames?;
let padding = u64::from(params.padding.unwrap_or(0));
// Symphonia normally leaves the OpusHead pre-skip in `delay`. For a very
// short stream whose first audio page is also its last page, it instead
// reports the page's trailing padding there. Preserve that information so
// both one-page and ordinary Ogg/Opus streams end on the correct sample.
let one_page_padding = params
.delay
.map(u64::from)
.filter(|delay| *delay != pre_skip)
.unwrap_or(0);
Some(
encoded_frames
.saturating_sub(pre_skip)
.saturating_sub(padding)
.saturating_sub(one_page_padding),
)
}
fn opus_output_gain(extra_data: Option<&[u8]>) -> f32 {
let Some(header) = extra_data.filter(|header| header.len() >= 18) else {
return 1.0;
};
let gain_q8 = i16::from_le_bytes([header[16], header[17]]);
10.0_f32.powf(f32::from(gain_q8) / (20.0 * 256.0))
}
fn seek_error(message: String) -> SeekError {
SeekError::Other(Arc::new(io::Error::other(message)))
}
#[cfg(test)]
mod tests {
use std::io::Cursor;
use base64::Engine as _;
use super::*;
// 30 ms mono Ogg/Opus sine, encoded by FFmpeg/libopus. Keeping a real
// interoperable stream here catches container and codec regressions.
const OGG_OPUS: &str = "T2dnUwACAAAAAAAAAABkiBtVAAAAAFZb1toBE09wdXNIZWFkAQE4AYC7AAAAAABPZ2dTAAAAAAAAAAAAAGSIG1UBAAAAq+b2jQE+T3B1c1RhZ3MNAAAATGF2ZjYyLjEyLjEwMgEAAAAdAAAAZW5jb2Rlcj1MYXZjNjIuMjguMTAyIGxpYm9wdXNPZ2dTAATYBgAAAAAAAGSIG1UCAAAAEkNnrgT/NP8x+HJJRycQ5MhbeCeLhvfY79vodePsNuYR8hAn2iaItTjKzvL0UULTvai7BFd4FJ2BZdtZQn4K2vCS9rjadnLmp+u4QMCAJiL3MXbVJBVHjtDhATQq5rg5ZlscpBXWk/NqnQE/QaT/nhMi/VqnLKwRCFcpFnH/NqPx7RhjpvzicAu/blC/sRygALOf6blR6HYkOb7BLn/Vn3ijqdTVwCzwtcpWU2hbCXUOWnTEuXUOWaOmQKy8zmqYgzM7aOueo/2siHPpomLeiHPp4NH0cpD63qkSv4/XxX8HWdBtg9IvLpQ42ch0PrqI7BR4ZhvJoDmg45q/177KfOUxlfoG/8GpJWKTCpaN1u64it+1vkpE7sfaLIuSVNbz6BDOIREzM5pPkdIyBzqThkbRkGJr8KDGTyQyJfi0JXhIR5fGM2RptXd5mTRUZHmCz9dCMyKay/nzCir7oSDyEbWNlfaV7qzrxPXTNJRpJ/6GgfI3Ht2Y9jKPV8WkGzGO/Pyz+hznJTgJdGOpxRmzAWXmcwcSdI0TjPVLIs8SIBtipLhr0R+yDe2ar7ZQR3hxQzoIn5ydLO7mk8QZvr+4gAAAAAAAAAAAAAAAAAAAAAAAAE+e11+HmY5uncnz1m153WYm1urhAnMTa3qa3G/MIezSH+urKDn4eZAnEgz8PMnPgAOL22PlLS7647fYrYbJfnfYvLgwLpXSlHeh7VHf+GQd99vAzM/086lHsksCvwXkd12GFX2BTQbtW/3wPMmi4zL6VsKJUH/Q5ZQ00cvMtFDgSw5CLIg8nHONgH2/XVeZC+VwngKBeuzHHK4=";
fn fixture() -> Vec<u8> {
base64::engine::general_purpose::STANDARD
.decode(OGG_OPUS)
.unwrap()
}
#[test]
fn detects_and_decodes_ogg_opus() {
let bytes = fixture();
let len = bytes.len() as u64;
let mut reader: TrackReader = Box::new(Cursor::new(bytes));
assert!(is_ogg_opus(&mut reader).unwrap());
let source = OggOpusSource::new(reader, Some(len), true).unwrap();
assert_eq!(source.channels().get(), 1);
assert_eq!(source.sample_rate().get(), OPUS_SAMPLE_RATE);
let samples: Vec<_> = source.collect();
assert_eq!(samples.len(), 1_440);
assert!(samples.iter().any(|sample| sample.abs() > 0.001));
}
#[test]
fn seeks_within_ogg_opus() {
let bytes = fixture();
let len = bytes.len() as u64;
let reader: TrackReader = Box::new(Cursor::new(bytes));
let mut source = OggOpusSource::new(reader, Some(len), true).unwrap();
assert_eq!(source.by_ref().count(), 1_440);
source.try_seek(Duration::from_millis(15)).unwrap();
let samples: Vec<_> = source.collect();
assert_eq!(samples.len(), 720);
assert!(samples.iter().any(|sample| sample.abs() > 0.001));
}
#[test]
fn ogg_vorbis_header_is_not_misclassified_as_opus() {
let mut bytes =
b"OggS\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\x1e\x01vorbis".to_vec();
bytes.resize(SNIFF_BYTES, 0);
let mut reader: TrackReader = Box::new(Cursor::new(bytes));
assert!(!is_ogg_opus(&mut reader).unwrap());
}
}
+1576
View File
File diff suppressed because it is too large. Load diff
+207
View File
@@ -0,0 +1,207 @@
//! Async HTTP for podcast feeds and cover images.
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use anyhow::{Context, Result, anyhow, bail};
use reqwest::header::{ACCEPT, ETAG, IF_MODIFIED_SINCE, IF_NONE_MATCH, LAST_MODIFIED};
use reqwest::{StatusCode, Url};
use super::feed::{ParsedFeed, parse_feed_with_base};
/// Hard cap on a feed document (large catalogs are a few MiB).
pub const MAX_FEED_BYTES: usize = 32 * 1024 * 1024;
/// Hard cap on a cover image.
pub const MAX_COVER_BYTES: usize = 10 * 1024 * 1024;
const FEED_ACCEPT: &str =
"application/rss+xml, application/atom+xml, application/xml;q=0.9, text/xml;q=0.9, */*;q=0.5";
pub enum FeedFetch {
NotModified,
Fetched {
feed: ParsedFeed,
etag: Option<String>,
last_modified: Option<String>,
},
}
pub struct Fetcher {
client: reqwest::Client,
covers_dir: PathBuf,
}
impl Fetcher {
pub fn new(covers_dir: PathBuf) -> Result<Fetcher> {
let _ = rustls::crypto::ring::default_provider().install_default();
let client = reqwest::Client::builder()
.connect_timeout(Duration::from_secs(15))
.timeout(Duration::from_secs(60))
.redirect(reqwest::redirect::Policy::limited(10))
.user_agent(concat!("furumi/", env!("CARGO_PKG_VERSION")))
.build()
.context("building the podcast HTTP client")?;
Ok(Fetcher { client, covers_dir })
}
/// Conditional GET of a feed. Parsing runs on the blocking pool.
pub async fn fetch_feed(
&self,
url: &str,
etag: Option<&str>,
last_modified: Option<&str>,
) -> Result<FeedFetch> {
let mut request = self.client.get(url).header(ACCEPT, FEED_ACCEPT);
if let Some(etag) = etag {
request = request.header(IF_NONE_MATCH, etag);
}
if let Some(last_modified) = last_modified {
request = request.header(IF_MODIFIED_SINCE, last_modified);
}
let response = request
.send()
.await
.with_context(|| format!("fetching feed {url}"))?;
let status = response.status();
if status == StatusCode::NOT_MODIFIED {
return Ok(FeedFetch::NotModified);
}
if !status.is_success() {
bail!("feed request failed: HTTP {status}");
}
let header = |name| {
response
.headers()
.get(name)
.and_then(|value: &reqwest::header::HeaderValue| value.to_str().ok())
.map(str::to_string)
};
let etag = header(ETAG);
let last_modified = header(LAST_MODIFIED);
// Relative enclosure URLs resolve against where the feed really is
// (after redirects).
let base = response.url().to_string();
let body = read_capped(response, MAX_FEED_BYTES, "feed").await?;
let feed = tokio::task::spawn_blocking(move || parse_feed_with_base(&body, Some(&base)))
.await
.map_err(|err| anyhow!("feed parser task failed: {err}"))??;
Ok(FeedFetch::Fetched {
feed,
etag,
last_modified,
})
}
/// Downloads a cover image into the covers directory and returns its path.
pub async fn fetch_cover(&self, podcast_id: i64, image_url: &str) -> Result<PathBuf> {
let response = self
.client
.get(image_url)
.send()
.await
.with_context(|| format!("fetching cover {image_url}"))?;
let status = response.status();
if !status.is_success() {
bail!("cover request failed: HTTP {status}");
}
let body = read_capped(response, MAX_COVER_BYTES, "cover image").await?;
let format = image::guess_format(&body).context("cover is not a supported image")?;
let extension = format.extensions_str().first().copied().unwrap_or("img");
let hash = blake3::hash(image_url.as_bytes()).to_hex();
let name = format!("{podcast_id}-{}.{extension}", &hash.as_str()[..16]);
let dir = self.covers_dir.clone();
tokio::task::spawn_blocking(move || write_atomically(&dir, &name, &body))
.await
.map_err(|err| anyhow!("cover writer task failed: {err}"))?
}
}
async fn read_capped(mut response: reqwest::Response, cap: usize, what: &str) -> Result<Vec<u8>> {
if let Some(len) = response.content_length()
&& len > cap as u64
{
bail!("{what} is too large ({len} bytes, limit {cap})");
}
let mut body = Vec::new();
while let Some(chunk) = response
.chunk()
.await
.with_context(|| format!("reading {what}"))?
{
if body.len() + chunk.len() > cap {
bail!("{what} is too large (over {cap} bytes)");
}
body.extend_from_slice(&chunk);
}
Ok(body)
}
fn write_atomically(dir: &Path, name: &str, bytes: &[u8]) -> Result<PathBuf> {
static COUNTER: AtomicU64 = AtomicU64::new(0);
std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
let target = dir.join(name);
let temp = dir.join(format!(
".{name}.{}-{}.tmp",
std::process::id(),
COUNTER.fetch_add(1, Ordering::Relaxed)
));
let result = std::fs::write(&temp, bytes)
.and_then(|()| std::fs::rename(&temp, &target))
.with_context(|| format!("writing {}", target.display()));
if result.is_err() {
let _ = std::fs::remove_file(&temp);
}
result?;
Ok(target)
}
/// Whether `text` starts with `<scheme>://`. Only a prefix counts: a URL
/// embedded in the query (`a.test/rss?src=https://b.test`) is not a scheme.
fn has_scheme(text: &str) -> bool {
let Some((scheme, rest)) = text.split_once(':') else {
return false;
};
let mut chars = scheme.chars();
chars.next().is_some_and(|c| c.is_ascii_alphabetic())
&& chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
&& rest.starts_with("//")
}
/// Normalizes user input into a feed URL: trims, maps `feed://`,
/// `podcast://`, `pcast://` and `itpc://` to `https://`, defaults to
/// `https://` without a scheme and accepts only http(s) URLs with a host.
pub fn normalize_feed_url(input: &str) -> Result<String> {
let trimmed = input.trim();
if trimmed.is_empty() {
bail!("feed URL is empty");
}
if trimmed.chars().any(char::is_whitespace) {
bail!("feed URL must not contain whitespace");
}
let lower = trimmed.to_ascii_lowercase();
let mut candidate = trimmed.to_string();
for scheme in ["feed:", "podcast:", "pcast:", "itpc:"] {
if lower.starts_with(scheme) {
let rest = &trimmed[scheme.len()..];
let rest_lower = rest.to_ascii_lowercase();
candidate = if rest_lower.starts_with("http://") || rest_lower.starts_with("https://") {
rest.to_string()
} else {
format!("https://{}", rest.trim_start_matches('/'))
};
break;
}
}
if !has_scheme(&candidate) {
candidate = format!("https://{candidate}");
}
let url = Url::parse(&candidate).with_context(|| format!("invalid feed URL {trimmed:?}"))?;
if !matches!(url.scheme(), "http" | "https") {
bail!("unsupported feed URL scheme {:?}", url.scheme());
}
if url.host_str().is_none_or(str::is_empty) {
bail!("feed URL has no host");
}
Ok(url.to_string())
}
+344
View File
@@ -0,0 +1,344 @@
//! Podcast RSS/Atom subscriptions.
//!
//! Furumi stores only subscription and episode *metadata* (SQLite, see
//! [`store`]); audio stays on the publisher's host and is streamed on demand
//! through [`stream`] when an episode is played. Adding a subscription never
//! downloads episodes.
//!
//! * [`feed`] parses RSS 2.0 / RSS 1.0 / Atom documents.
//! * [`fetch`] downloads feeds (conditional GET) and cover images.
//! * [`store`] persists podcasts and episodes, deduplicated by GUID.
//! * [`stream`] plays a remote episode through a bounded read-ahead buffer.
//! * [`subscribe`] / [`refresh`] orchestrate the above without blocking
//! Tokio workers.
pub mod feed;
pub mod fetch;
pub mod store;
pub mod stream;
#[cfg(test)]
mod test_server;
#[cfg(test)]
mod tests;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use anyhow::{Result, anyhow, bail};
use crate::library::models::{ArtistRef, TrackItem};
pub use fetch::{FeedFetch, Fetcher, normalize_feed_url};
pub use store::Store;
// Convenience re-exports used by the module's tests.
#[cfg(test)]
pub use feed::{ParsedEpisode, ParsedFeed, parse_feed, parse_feed_with_base};
#[cfg(test)]
pub use stream::HttpStream;
/// Track ids of podcast episodes are `-(EPISODE_TRACK_ID_BASE + episode.id)`
/// for episode ids in `1..EPISODE_TRACK_ID_BASE`, i.e. the narrow band
/// `-(2^41 - 1)..=-(2^40 + 1)`, far away from library rows (positive) and
/// other synthetic ids.
pub const EPISODE_TRACK_ID_BASE: i64 = 1 << 40;
#[derive(Debug, Clone, PartialEq)]
pub struct Podcast {
pub id: i64,
pub feed_url: String,
pub title: String,
pub author: Option<String>,
/// Plain text (HTML stripped, entities decoded, whitespace collapsed),
/// at most 4000 characters.
pub description: Option<String>,
pub link: Option<String>,
pub image_url: Option<String>,
/// Absolute path of the locally cached cover image, if downloaded.
pub cover_path: Option<String>,
pub episode_count: usize,
/// Unix seconds, max over episodes.
pub latest_published_at: Option<i64>,
/// Unix seconds of the last refresh attempt.
pub last_checked_at: Option<i64>,
/// Error of the last refresh attempt; `None` if it succeeded.
pub last_error: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Episode {
/// SQLite rowid, stable across refreshes.
pub id: i64,
pub podcast_id: i64,
pub guid: String,
pub title: String,
/// Plain text, at most 4000 characters.
pub description: Option<String>,
/// Unix seconds.
pub published_at: Option<i64>,
pub duration_seconds: Option<f64>,
pub audio_url: String,
pub mime_type: Option<String>,
pub byte_len: Option<u64>,
pub link: Option<String>,
pub image_url: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct RefreshSummary {
pub new_episodes: usize,
pub updated_episodes: usize,
pub not_modified: bool,
}
/// Runs blocking store/filesystem work off the Tokio workers.
async fn blocking<T, F>(work: F) -> Result<T>
where
T: Send + 'static,
F: FnOnce() -> Result<T> + Send + 'static,
{
tokio::task::spawn_blocking(work)
.await
.map_err(|err| anyhow!("background task failed: {err}"))?
}
/// Subscribes to a feed: normalizes the URL, rejects duplicates, fetches and
/// parses the feed (nothing is stored if it is not a feed), stores the
/// podcast with all episode metadata, then downloads the cover (best effort).
pub async fn subscribe(
store: Arc<Store>,
fetcher: &Fetcher,
input_url: &str,
now: i64,
) -> Result<i64> {
let feed_url = normalize_feed_url(input_url)?;
let existing = {
let store = Arc::clone(&store);
let feed_url = feed_url.clone();
blocking(move || store.podcast_by_feed_url(&feed_url)).await?
};
if let Some(podcast) = existing {
bail!("already subscribed to {}", podcast.title);
}
let (feed, etag, last_modified) = match fetcher.fetch_feed(&feed_url, None, None).await? {
FeedFetch::Fetched {
feed,
etag,
last_modified,
} => (feed, etag, last_modified),
FeedFetch::NotModified => {
bail!("server answered 304 Not Modified to an unconditional request")
}
};
let image_url = feed.image_url.clone();
let podcast_id = {
let store = Arc::clone(&store);
blocking(move || {
let id = store.add_podcast(&feed_url, &feed, now)?;
store.set_http_validators(id, etag.as_deref(), last_modified.as_deref())?;
Ok(id)
})
.await?
};
if let Some(image_url) = image_url {
update_cover(&store, fetcher, podcast_id, &image_url).await;
}
Ok(podcast_id)
}
/// Refreshes one subscription with a conditional request. Failures are
/// recorded on the podcast (`last_error`) and returned.
pub async fn refresh(
store: Arc<Store>,
fetcher: &Fetcher,
podcast_id: i64,
now: i64,
) -> Result<RefreshSummary> {
let (podcast, (etag, last_modified)) = {
let store = Arc::clone(&store);
blocking(move || {
let podcast = store
.podcast(podcast_id)?
.ok_or_else(|| anyhow!("podcast {podcast_id} not found"))?;
Ok((podcast, store.http_validators(podcast_id)?))
})
.await?
};
let fetched = fetcher
.fetch_feed(&podcast.feed_url, etag.as_deref(), last_modified.as_deref())
.await;
let (summary, image_url) = match fetched {
Err(err) => {
let message = format!("{err:#}");
let store = Arc::clone(&store);
if let Err(record_err) =
blocking(move || store.record_check(podcast_id, now, Some(&message))).await
{
tracing::warn!("recording podcast refresh error failed: {record_err:#}");
}
return Err(err);
}
Ok(FeedFetch::NotModified) => {
let store = Arc::clone(&store);
blocking(move || store.record_check(podcast_id, now, None)).await?;
let summary = RefreshSummary {
not_modified: true,
..RefreshSummary::default()
};
(summary, podcast.image_url)
}
Ok(FeedFetch::Fetched {
feed,
etag,
last_modified,
}) => {
let image_url = feed.image_url.clone();
let store = Arc::clone(&store);
let applied = blocking(move || {
let summary = store.apply_feed(podcast_id, &feed, now);
match &summary {
Ok(_) => store.set_http_validators(
podcast_id,
etag.as_deref(),
last_modified.as_deref(),
)?,
Err(err) => {
store.record_check(podcast_id, now, Some(&format!("{err:#}")))?;
}
}
summary
})
.await?;
(applied, image_url)
}
};
if let Some(image_url) = image_url {
refresh_cover(&store, fetcher, podcast_id, &image_url).await;
}
Ok(summary)
}
/// Re-downloads the cover when the feed image changed or the cached file is
/// gone. Best effort: failures are only logged.
async fn refresh_cover(store: &Arc<Store>, fetcher: &Fetcher, podcast_id: i64, image_url: &str) {
let source = {
let store = Arc::clone(store);
blocking(move || {
let (url, path) = store.cover_source(podcast_id)?;
let missing = path
.as_deref()
.is_none_or(|path| !Path::new(path).is_file());
Ok((url, missing))
})
.await
};
match source {
Ok((url, missing)) if url.as_deref() == Some(image_url) && !missing => {}
Ok(_) => update_cover(store, fetcher, podcast_id, image_url).await,
Err(err) => tracing::warn!("reading podcast cover state failed: {err:#}"),
}
}
/// Downloads `image_url` as the podcast cover, records it and deletes a
/// superseded cover file. Best effort.
async fn update_cover(store: &Arc<Store>, fetcher: &Fetcher, podcast_id: i64, image_url: &str) {
let path = match fetcher.fetch_cover(podcast_id, image_url).await {
Ok(path) => path,
Err(err) => {
tracing::warn!("downloading podcast cover {image_url} failed: {err:#}");
return;
}
};
let store = Arc::clone(store);
let image_url = image_url.to_string();
let result = blocking(move || {
let new_path = path_string(&path);
if store.podcast(podcast_id)?.is_none() {
// Unsubscribed while the cover was downloading.
let _ = std::fs::remove_file(&path);
return Ok(());
}
let (_, old_path) = store.cover_source(podcast_id)?;
store.set_cover(podcast_id, Some(&image_url), Some(&new_path))?;
if let Some(old_path) = old_path.filter(|old| *old != new_path) {
let _ = std::fs::remove_file(old_path);
}
Ok(())
})
.await;
if let Err(err) = result {
tracing::warn!("recording podcast cover failed: {err:#}");
}
}
fn path_string(path: &Path) -> String {
let absolute: PathBuf = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf());
absolute.to_string_lossy().into_owned()
}
/// Whether a track path is a remote (http/https) URL rather than a file.
pub fn is_remote_url(value: &str) -> bool {
let value = value.as_bytes();
let has_prefix = |prefix: &[u8]| {
value.len() >= prefix.len() && value[..prefix.len()].eq_ignore_ascii_case(prefix)
};
has_prefix(b"http://") || has_prefix(b"https://")
}
/// The player-facing track for an episode. `file_path` is the remote audio
/// URL; see [`is_remote_url`] and [`stream::open`].
pub fn episode_track(podcast: &Podcast, episode: &Episode) -> TrackItem {
TrackItem {
id: -(EPISODE_TRACK_ID_BASE + episode.id),
title: episode.title.clone(),
track_number: None,
disc_number: None,
duration_seconds: episode.duration_seconds.unwrap_or(0.0),
artists: vec![ArtistRef {
id: -1,
name: podcast.title.clone(),
}],
featured_artists: Vec::new(),
release_id: -1,
release_title: podcast.title.clone(),
release_year: episode.published_at.map(feed::year_from_unix),
file_path: episode.audio_url.clone(),
content_id: None,
cover_path: podcast.cover_path.clone(),
audio_format: episode.mime_type.as_deref().and_then(audio_format),
audio_bitrate: None,
audio_sample_rate: None,
audio_bit_depth: None,
file_size_bytes: episode
.byte_len
.map(|len| i64::try_from(len).unwrap_or(i64::MAX)),
play_count: 0,
fed: None,
}
}
/// Inverse of the id mapping in [`episode_track`]. Only the band reserved
/// for episodes maps back, so hash-derived negative ids (for example device
/// sync placeholders spread over the whole negative range) are almost never
/// mistaken for episodes; callers should still confirm with a store lookup.
pub fn episode_id_from_track_id(track_id: i64) -> Option<i64> {
let episode_id = track_id.checked_neg()?.checked_sub(EPISODE_TRACK_ID_BASE)?;
(episode_id > 0 && episode_id < EPISODE_TRACK_ID_BASE).then_some(episode_id)
}
/// Display format from a MIME type: `audio/mpeg` -> `MP3`, otherwise the
/// upper-cased subtype without an `x-` prefix.
fn audio_format(mime: &str) -> Option<String> {
let (kind, subtype) = mime.split(';').next()?.trim().split_once('/')?;
let subtype = subtype.strip_prefix("x-").unwrap_or(subtype);
if subtype.is_empty() {
return None;
}
let audio = kind.eq_ignore_ascii_case("audio");
Some(match subtype.to_ascii_lowercase().as_str() {
"mpeg" | "mp3" | "mpeg3" | "mpg" if audio => "MP3".to_string(),
"mp4" | "m4a" if audio => "M4A".to_string(),
"vorbis" => "OGG".to_string(),
_ => subtype.to_ascii_uppercase(),
})
}
+504
View File
@@ -0,0 +1,504 @@
//! SQLite persistence for podcast subscriptions and episode metadata.
//!
//! Only metadata is stored; audio stays on the publisher's host. Episodes are
//! unique per `(podcast_id, guid)` and their row ids stay stable across
//! refreshes, so they can be referenced from playback state. Both tables use
//! `AUTOINCREMENT`, so ids of removed podcasts and episodes are never handed
//! out again (a stale reference or an in-flight refresh of a removed podcast
//! can never hit a different subscription).
use std::path::Path;
use std::sync::{Mutex, MutexGuard, PoisonError};
use std::time::Duration;
use anyhow::{Context, Result, anyhow, bail};
use rusqlite::{Connection, OptionalExtension, Row, params};
use super::feed::{ParsedEpisode, ParsedFeed};
use super::{Episode, Podcast, RefreshSummary};
/// Schema migrations; `PRAGMA user_version` records how many have run.
/// Every statement is idempotent so a partially applied step can be re-run.
const MIGRATIONS: &[&str] = &[
// v1: subscriptions and episode metadata.
"
CREATE TABLE IF NOT EXISTS podcasts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
feed_url TEXT NOT NULL UNIQUE,
title TEXT NOT NULL,
author TEXT,
description TEXT,
link TEXT,
image_url TEXT,
cover_url TEXT,
cover_path TEXT,
etag TEXT,
last_modified TEXT,
added_at INTEGER NOT NULL,
last_checked_at INTEGER,
last_error TEXT
);
CREATE TABLE IF NOT EXISTS podcast_episodes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
podcast_id INTEGER NOT NULL REFERENCES podcasts(id) ON DELETE CASCADE,
guid TEXT NOT NULL,
title TEXT NOT NULL,
description TEXT,
published_at INTEGER,
duration_seconds REAL,
audio_url TEXT NOT NULL,
mime_type TEXT,
byte_len INTEGER,
link TEXT,
image_url TEXT,
first_seen_at INTEGER NOT NULL,
UNIQUE (podcast_id, guid)
);
CREATE INDEX IF NOT EXISTS podcast_episodes_by_date
ON podcast_episodes (podcast_id, published_at DESC, id DESC);
",
];
const PODCAST_COLUMNS: &str = "
p.id, p.feed_url, p.title, p.author, p.description, p.link, p.image_url,
p.cover_path, p.last_checked_at, p.last_error,
(SELECT COUNT(*) FROM podcast_episodes e WHERE e.podcast_id = p.id),
(SELECT MAX(e.published_at) FROM podcast_episodes e WHERE e.podcast_id = p.id)";
const EPISODE_COLUMNS: &str = "
id, podcast_id, guid, title, description, published_at, duration_seconds,
audio_url, mime_type, byte_len, link, image_url";
pub struct Store {
conn: Mutex<Connection>,
}
impl Store {
/// Opens (creating if needed) the podcast database at `path`.
pub fn open(path: &Path) -> Result<Store> {
if let Some(parent) = path.parent()
&& !parent.as_os_str().is_empty()
{
std::fs::create_dir_all(parent)
.with_context(|| format!("creating {}", parent.display()))?;
}
let conn = Connection::open(path)
.with_context(|| format!("opening podcast database {}", path.display()))?;
Self::init(conn, true)
}
#[cfg(test)]
pub fn open_in_memory() -> Result<Store> {
Self::init(Connection::open_in_memory()?, false)
}
fn init(conn: Connection, wal: bool) -> Result<Store> {
conn.busy_timeout(Duration::from_secs(5))?;
conn.pragma_update(None, "foreign_keys", true)?;
if wal {
// Returns the resulting mode as a row, hence query instead of execute.
let _: String = conn.query_row("PRAGMA journal_mode=WAL", [], |row| row.get(0))?;
}
migrate(&conn)?;
Ok(Store {
conn: Mutex::new(conn),
})
}
fn conn(&self) -> MutexGuard<'_, Connection> {
self.conn.lock().unwrap_or_else(PoisonError::into_inner)
}
pub fn list_podcasts(&self) -> Result<Vec<Podcast>> {
let conn = self.conn();
let mut stmt = conn.prepare(&format!(
"SELECT {PODCAST_COLUMNS} FROM podcasts p ORDER BY p.title COLLATE NOCASE, p.id"
))?;
let podcasts = stmt
.query_map([], podcast_from_row)?
.collect::<rusqlite::Result<Vec<_>>>()?;
Ok(podcasts)
}
pub fn podcast(&self, id: i64) -> Result<Option<Podcast>> {
let conn = self.conn();
Ok(conn
.query_row(
&format!("SELECT {PODCAST_COLUMNS} FROM podcasts p WHERE p.id = ?1"),
[id],
podcast_from_row,
)
.optional()?)
}
pub fn podcast_by_feed_url(&self, feed_url: &str) -> Result<Option<Podcast>> {
let conn = self.conn();
Ok(conn
.query_row(
&format!("SELECT {PODCAST_COLUMNS} FROM podcasts p WHERE p.feed_url = ?1"),
[feed_url],
podcast_from_row,
)
.optional()?)
}
/// Inserts a subscription and all of its episodes in one transaction.
pub fn add_podcast(&self, feed_url: &str, feed: &ParsedFeed, now: i64) -> Result<i64> {
let mut conn = self.conn();
let tx = conn.transaction()?;
let exists: bool = tx.query_row(
"SELECT EXISTS (SELECT 1 FROM podcasts WHERE feed_url = ?1)",
[feed_url],
|row| row.get(0),
)?;
if exists {
bail!("already subscribed");
}
tx.execute(
"INSERT INTO podcasts
(feed_url, title, author, description, link, image_url, added_at, last_checked_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?7)",
params![
feed_url,
feed.title,
feed.author,
feed.description,
feed.link,
feed.image_url,
now
],
)
.map_err(|err| match err {
rusqlite::Error::SqliteFailure(code, _)
if code.extended_code == rusqlite::ffi::SQLITE_CONSTRAINT_UNIQUE =>
{
anyhow!("already subscribed")
}
other => other.into(),
})?;
let podcast_id = tx.last_insert_rowid();
upsert_episodes(&tx, podcast_id, &feed.episodes, now)?;
tx.commit()?;
Ok(podcast_id)
}
/// Applies a freshly fetched feed: updates channel metadata and upserts
/// episodes by guid. Episodes no longer in the feed are kept.
pub fn apply_feed(
&self,
podcast_id: i64,
feed: &ParsedFeed,
now: i64,
) -> Result<RefreshSummary> {
let mut conn = self.conn();
let tx = conn.transaction()?;
let updated = tx.execute(
"UPDATE podcasts
SET title = ?2, author = ?3, description = ?4, link = ?5, image_url = ?6,
last_checked_at = ?7, last_error = NULL
WHERE id = ?1",
params![
podcast_id,
feed.title,
feed.author,
feed.description,
feed.link,
feed.image_url,
now
],
)?;
if updated == 0 {
bail!("podcast {podcast_id} not found");
}
let summary = upsert_episodes(&tx, podcast_id, &feed.episodes, now)?;
tx.commit()?;
Ok(summary)
}
pub fn record_check(&self, podcast_id: i64, now: i64, error: Option<&str>) -> Result<()> {
self.conn().execute(
"UPDATE podcasts SET last_checked_at = ?2, last_error = ?3 WHERE id = ?1",
params![podcast_id, now, error],
)?;
Ok(())
}
pub fn http_validators(&self, podcast_id: i64) -> Result<(Option<String>, Option<String>)> {
Ok(self
.conn()
.query_row(
"SELECT etag, last_modified FROM podcasts WHERE id = ?1",
[podcast_id],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.optional()?
.unwrap_or((None, None)))
}
pub fn set_http_validators(
&self,
podcast_id: i64,
etag: Option<&str>,
last_modified: Option<&str>,
) -> Result<()> {
self.conn().execute(
"UPDATE podcasts SET etag = ?2, last_modified = ?3 WHERE id = ?1",
params![podcast_id, etag, last_modified],
)?;
Ok(())
}
/// Records the cached cover file and the image URL it was downloaded from.
pub fn set_cover(
&self,
podcast_id: i64,
cover_url: Option<&str>,
cover_path: Option<&str>,
) -> Result<()> {
self.conn().execute(
"UPDATE podcasts SET cover_url = ?2, cover_path = ?3 WHERE id = ?1",
params![podcast_id, cover_url, cover_path],
)?;
Ok(())
}
/// `(cover_url, cover_path)`: the image URL the cached cover came from
/// and the cached file.
pub fn cover_source(&self, podcast_id: i64) -> Result<(Option<String>, Option<String>)> {
Ok(self
.conn()
.query_row(
"SELECT cover_url, cover_path FROM podcasts WHERE id = ?1",
[podcast_id],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.optional()?
.unwrap_or((None, None)))
}
/// Episodes of a podcast, newest first (undated episodes last).
pub fn episodes(&self, podcast_id: i64) -> Result<Vec<Episode>> {
let conn = self.conn();
let mut stmt = conn.prepare(&format!(
"SELECT {EPISODE_COLUMNS} FROM podcast_episodes
WHERE podcast_id = ?1
ORDER BY published_at IS NULL, published_at DESC, id DESC"
))?;
let episodes = stmt
.query_map([podcast_id], episode_from_row)?
.collect::<rusqlite::Result<Vec<_>>>()?;
Ok(episodes)
}
#[cfg(test)]
pub fn episode(&self, episode_id: i64) -> Result<Option<Episode>> {
Ok(self
.conn()
.query_row(
&format!("SELECT {EPISODE_COLUMNS} FROM podcast_episodes WHERE id = ?1"),
[episode_id],
episode_from_row,
)
.optional()?)
}
/// Deletes a subscription and its episodes; returns the cached cover path
/// so the caller can delete the file.
pub fn remove_podcast(&self, podcast_id: i64) -> Result<Option<String>> {
let mut conn = self.conn();
let tx = conn.transaction()?;
let cover_path: Option<Option<String>> = tx
.query_row(
"SELECT cover_path FROM podcasts WHERE id = ?1",
[podcast_id],
|row| row.get(0),
)
.optional()?;
// Explicit delete keeps this correct even on a connection where the
// foreign-key cascade is somehow disabled.
tx.execute(
"DELETE FROM podcast_episodes WHERE podcast_id = ?1",
[podcast_id],
)?;
tx.execute("DELETE FROM podcasts WHERE id = ?1", [podcast_id])?;
tx.commit()?;
Ok(cover_path.flatten())
}
#[cfg(test)]
pub(super) fn schema_version(&self) -> Result<i64> {
Ok(self
.conn()
.query_row("PRAGMA user_version", [], |row| row.get(0))?)
}
}
fn migrate(conn: &Connection) -> Result<()> {
let version: i64 = conn.query_row("PRAGMA user_version", [], |row| row.get(0))?;
let target = MIGRATIONS.len() as i64;
if version > target {
bail!("podcast database schema v{version} is newer than this Furumi (v{target})");
}
for (index, sql) in MIGRATIONS.iter().enumerate().skip(version as usize) {
let next = index as i64 + 1;
conn.execute_batch(&format!(
"BEGIN IMMEDIATE; {sql}; PRAGMA user_version = {next}; COMMIT;"
))
.inspect_err(|_| {
let _ = conn.execute_batch("ROLLBACK");
})
.with_context(|| format!("migrating podcast database to v{next}"))?;
}
Ok(())
}
fn podcast_from_row(row: &Row<'_>) -> rusqlite::Result<Podcast> {
let episode_count: i64 = row.get(10)?;
Ok(Podcast {
id: row.get(0)?,
feed_url: row.get(1)?,
title: row.get(2)?,
author: row.get(3)?,
description: row.get(4)?,
link: row.get(5)?,
image_url: row.get(6)?,
cover_path: row.get(7)?,
last_checked_at: row.get(8)?,
last_error: row.get(9)?,
episode_count: usize::try_from(episode_count).unwrap_or(0),
latest_published_at: row.get(11)?,
})
}
fn episode_from_row(row: &Row<'_>) -> rusqlite::Result<Episode> {
let byte_len: Option<i64> = row.get(9)?;
Ok(Episode {
id: row.get(0)?,
podcast_id: row.get(1)?,
guid: row.get(2)?,
title: row.get(3)?,
description: row.get(4)?,
published_at: row.get(5)?,
duration_seconds: row.get(6)?,
audio_url: row.get(7)?,
mime_type: row.get(8)?,
byte_len: byte_len.and_then(|len| u64::try_from(len).ok()),
link: row.get(10)?,
image_url: row.get(11)?,
})
}
/// The mutable columns of an episode row, for change detection.
#[derive(PartialEq)]
struct EpisodeFields {
title: String,
description: Option<String>,
published_at: Option<i64>,
duration_seconds: Option<f64>,
audio_url: String,
mime_type: Option<String>,
byte_len: Option<i64>,
link: Option<String>,
image_url: Option<String>,
}
impl EpisodeFields {
fn from_parsed(episode: &ParsedEpisode) -> EpisodeFields {
EpisodeFields {
title: episode.title.clone(),
description: episode.description.clone(),
published_at: episode.published_at,
duration_seconds: episode.duration_seconds,
audio_url: episode.audio_url.clone(),
mime_type: episode.mime_type.clone(),
byte_len: episode
.byte_len
.map(|len| i64::try_from(len).unwrap_or(i64::MAX)),
link: episode.link.clone(),
image_url: episode.image_url.clone(),
}
}
}
fn upsert_episodes(
tx: &rusqlite::Transaction<'_>,
podcast_id: i64,
episodes: &[ParsedEpisode],
now: i64,
) -> Result<RefreshSummary> {
let mut summary = RefreshSummary::default();
let mut select = tx.prepare_cached(
"SELECT id, title, description, published_at, duration_seconds, audio_url,
mime_type, byte_len, link, image_url
FROM podcast_episodes WHERE podcast_id = ?1 AND guid = ?2",
)?;
let mut insert = tx.prepare_cached(
"INSERT INTO podcast_episodes
(podcast_id, guid, title, description, published_at, duration_seconds,
audio_url, mime_type, byte_len, link, image_url, first_seen_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)",
)?;
let mut update = tx.prepare_cached(
"UPDATE podcast_episodes
SET title = ?2, description = ?3, published_at = ?4, duration_seconds = ?5,
audio_url = ?6, mime_type = ?7, byte_len = ?8, link = ?9, image_url = ?10
WHERE id = ?1",
)?;
for episode in episodes {
let fields = EpisodeFields::from_parsed(episode);
let existing = select
.query_row(params![podcast_id, episode.guid], |row| {
Ok((
row.get::<_, i64>(0)?,
EpisodeFields {
title: row.get(1)?,
description: row.get(2)?,
published_at: row.get(3)?,
duration_seconds: row.get(4)?,
audio_url: row.get(5)?,
mime_type: row.get(6)?,
byte_len: row.get(7)?,
link: row.get(8)?,
image_url: row.get(9)?,
},
))
})
.optional()?;
match existing {
None => {
insert.execute(params![
podcast_id,
episode.guid,
fields.title,
fields.description,
fields.published_at,
fields.duration_seconds,
fields.audio_url,
fields.mime_type,
fields.byte_len,
fields.link,
fields.image_url,
now
])?;
summary.new_episodes += 1;
}
Some((id, stored)) if stored != fields => {
update.execute(params![
id,
fields.title,
fields.description,
fields.published_at,
fields.duration_seconds,
fields.audio_url,
fields.mime_type,
fields.byte_len,
fields.link,
fields.image_url
])?;
summary.updated_episodes += 1;
}
Some(_) => {}
}
}
Ok(summary)
}
+794
View File
@@ -0,0 +1,794 @@
//! Seekable playback of a remote episode without downloading it first.
//!
//! [`open`] issues one `Range: bytes=0-` request and hands the response to a
//! dedicated downloader thread. The thread fills a bounded in-memory window
//! around the reader position:
//!
//! * up to [`READ_AHEAD`] bytes ahead of the reader (then it pauses),
//! * up to [`KEEP_BEHIND`] bytes behind it (cheap small backward seeks),
//! * plus the first [`HEAD_KEEP`] bytes of the file, kept permanently because
//! decoders re-read the container header while probing.
//!
//! The window buffer is allocated once at its maximum size and never grows,
//! so memory stays bounded (and no large reallocation happens under the lock
//! the audio callback reads through).
//!
//! A seek inside the window is free. A read outside it restarts the
//! downloader at the new position with a fresh `Range` request; a generation
//! counter makes sure bytes from the superseded response are dropped. Servers
//! that ignore `Range` are still supported: forward seeks read and discard,
//! backward seeks restart from the beginning.
//!
//! Network errors: the downloader never gives up on a transient error by
//! itself. It resumes from its current offset with capped exponential backoff
//! (shortened while the reader is waiting for data), so a short outage is
//! bridged by the buffered window. The decision to fail belongs to the
//! reader: a `read` that is blocked on missing data and sees no download
//! progress for [`STARVE_TIMEOUT`] returns `io::ErrorKind::TimedOut`; the next
//! `read` starts a fresh attempt. Client errors (HTTP 4xx other than 408/429,
//! e.g. an expired signed URL) are reported to the reader immediately.
//!
//! Timeouts: `reqwest::blocking` applies `ClientBuilder::timeout` to waiting
//! for the response head and then *separately to every `Response::read`
//! call* (verified in reqwest 0.12.28: `blocking::Response::read` wraps each
//! read in `wait::timeout(.., self.timeout)`, and the blocking client does not
//! forward that value to the async client's total timeout). So
//! `timeout(STALL_TIMEOUT)` is a per-read stall timeout, not a cap on the whole
//! body, and long episodes are fine. The request-level
//! `RequestBuilder::timeout` (which *is* a total timeout) is never used.
//!
//! Decoder note for the player integration: symphonia's MP3 demuxer in
//! accurate seek mode never seeks by byte offset (a backward seek re-parses
//! from the first frame), which would re-download the episode. Build rodio
//! decoders for these streams with `.with_coarse_seek(true)` (and
//! `.with_byte_len(..)`), so a seek maps to a single range request.
use std::collections::VecDeque;
use std::io::{self, Read, Seek, SeekFrom};
use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
use std::time::{Duration, Instant};
use anyhow::{Context, Result, anyhow, bail};
use reqwest::StatusCode;
use reqwest::blocking::{Client, Response};
use reqwest::header::{CONTENT_RANGE, CONTENT_TYPE, RANGE};
/// Maximum bytes buffered ahead of the reader position.
pub const READ_AHEAD: u64 = 8 * 1024 * 1024;
/// Bytes kept behind the reader position for small backward seeks.
pub const KEEP_BEHIND: u64 = 1024 * 1024;
/// Leading bytes of the file kept for the lifetime of the stream.
pub const HEAD_KEEP: usize = 256 * 1024;
/// Size of a single network read.
const CHUNK: usize = 64 * 1024;
/// Largest the window can get: the read-ahead may overshoot by less than one
/// chunk before the downloader pauses.
const WINDOW_CAP: usize = READ_AHEAD as usize + KEEP_BEHIND as usize + CHUNK;
/// A forward gap up to this size is bridged by waiting for the downloader
/// instead of issuing a new range request.
const NEAR_GAP: u64 = 512 * 1024;
/// A restart this close ahead of the current response offset keeps the
/// response and discards bytes instead of reconnecting.
const DISCARD_LIMIT: u64 = 256 * 1024;
const CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
/// No progress for this long on one read (or the response head) is an error.
const STALL_TIMEOUT: Duration = Duration::from_secs(30);
/// First retry delay; doubles per consecutive failure.
const RETRY_BASE: Duration = Duration::from_millis(250);
/// Longest delay between attempts while the reader still has buffered data.
const MAX_BACKOFF: Duration = Duration::from_secs(10);
/// Longest delay between attempts while the reader is waiting for data.
const STARVED_BACKOFF: Duration = Duration::from_secs(1);
/// A `read` blocked on missing data fails after this long without any
/// download progress. Shorter in tests to keep failure tests fast.
#[cfg(not(test))]
pub const STARVE_TIMEOUT: Duration = Duration::from_secs(30);
#[cfg(test)]
pub const STARVE_TIMEOUT: Duration = Duration::from_secs(4);
pub struct OpenedStream {
pub reader: HttpStream,
pub byte_len: Option<u64>,
pub mime_type: Option<String>,
pub seekable: bool,
}
/// `Read + Seek` view of a remote file; see the module docs.
pub struct HttpStream {
pos: u64,
shared: Arc<Shared>,
}
struct Shared {
state: Mutex<State>,
/// Signalled when data, EOF, an error or downloader exit is available to
/// the reader.
data_ready: Condvar,
/// Signalled when the reader moved, requested a restart, started waiting
/// or went away.
space_ready: Condvar,
}
struct State {
/// Bumped on every restart; downloader output for older generations is
/// discarded.
generation: u64,
/// Start offset requested for the current generation.
target: u64,
/// File offset of `window[0]`.
window_start: u64,
window: VecDeque<u8>,
/// Bytes `0..head.len()` of the file.
head: Vec<u8>,
/// Last position the reader consumed from the window.
reader_pos: u64,
total: Option<u64>,
ranges: bool,
/// The current generation reached the end of the body.
done: bool,
/// The current generation was abandoned (fatal error, or the reader gave
/// up waiting); the downloader idles until the reader restarts it.
failed: bool,
/// Fatal error to report to the reader once.
error: Option<(io::ErrorKind, String)>,
/// Most recent transient failure, for the reader's timeout message.
last_error: Option<String>,
/// Bytes received from the network for the current generation
/// (including discarded ones); the reader's stall detection watches it.
progress: u64,
/// The reader is blocked waiting for data.
reader_waiting: bool,
/// The reader went away.
closed: bool,
/// The downloader thread exited.
exited: bool,
peak_buffered: usize,
peak_capacity: usize,
}
impl State {
fn window_end(&self) -> u64 {
self.window_start + self.window.len() as u64
}
fn buffered(&self) -> usize {
self.window.len() + self.head.len()
}
fn restart_at(&mut self, pos: u64) {
self.generation = self.generation.wrapping_add(1);
self.target = pos;
self.window_start = pos;
// Keeps the allocation: the window never needs more than WINDOW_CAP.
self.window.clear();
self.done = false;
self.failed = false;
self.error = None;
self.last_error = None;
self.progress = 0;
}
fn trim_behind(&mut self) {
let keep_from = self.reader_pos.saturating_sub(KEEP_BEHIND);
if keep_from > self.window_start {
let drop = (keep_from - self.window_start).min(self.window.len() as u64) as usize;
self.window.drain(..drop);
self.window_start += drop as u64;
}
}
}
impl Shared {
fn lock(&self) -> MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(PoisonError::into_inner)
}
}
/// Opens a remote audio file for streaming playback. Blocking: call it from
/// `spawn_blocking` or a plain thread, never directly on a Tokio worker.
pub fn open(url: &str) -> Result<OpenedStream> {
let _ = rustls::crypto::ring::default_provider().install_default();
let client = Client::builder()
.connect_timeout(CONNECT_TIMEOUT)
// Per-read stall timeout, see the module docs.
.timeout(STALL_TIMEOUT)
.redirect(reqwest::redirect::Policy::limited(10))
.user_agent(concat!("furumi/", env!("CARGO_PKG_VERSION")))
.build()
.context("building the streaming HTTP client")?;
let response = client
.get(url)
.header(RANGE, "bytes=0-")
.send()
.with_context(|| format!("requesting {url}"))?;
let opened = classify(&response, 0).with_context(|| format!("requesting {url}"))?;
let mime_type = response
.headers()
.get(CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.split(';').next())
.map(|value| value.trim().to_ascii_lowercase())
.filter(|value| !value.is_empty());
let window = VecDeque::with_capacity(WINDOW_CAP);
let peak_capacity = window.capacity();
let shared = Arc::new(Shared {
state: Mutex::new(State {
generation: 0,
target: 0,
window_start: 0,
window,
head: Vec::with_capacity(HEAD_KEEP),
reader_pos: 0,
total: opened.total,
ranges: opened.ranges,
done: false,
failed: false,
error: None,
last_error: None,
progress: 0,
reader_waiting: false,
closed: false,
exited: false,
peak_buffered: 0,
peak_capacity,
}),
data_ready: Condvar::new(),
space_ready: Condvar::new(),
});
let downloader = Downloader {
shared: Arc::clone(&shared),
client,
url: url.to_string(),
};
std::thread::Builder::new()
.name("podcast-stream".to_string())
.spawn(move || downloader.run(response))
.context("spawning the podcast stream thread")?;
Ok(OpenedStream {
reader: HttpStream { pos: 0, shared },
byte_len: opened.total,
mime_type,
seekable: opened.total.is_some(),
})
}
struct Classified {
/// Offset of the first body byte.
start: u64,
total: Option<u64>,
ranges: bool,
}
/// Interprets a response to `Range: bytes=<requested>-` (or a plain GET).
fn classify(response: &Response, requested: u64) -> Result<Classified> {
let status = response.status();
if status == StatusCode::PARTIAL_CONTENT {
let range = response
.headers()
.get(CONTENT_RANGE)
.and_then(|value| value.to_str().ok())
.and_then(parse_content_range);
let Some((start, total)) = range else {
bail!("HTTP 206 without a usable Content-Range");
};
if start != requested {
bail!("server returned range starting at {start}, expected {requested}");
}
return Ok(Classified {
start,
total,
ranges: true,
});
}
if status.is_success() {
return Ok(Classified {
start: 0,
total: response.content_length(),
ranges: false,
});
}
bail!("HTTP {status}");
}
/// Parses `bytes <start>-<end>/<total|*>` into `(start, total)`.
fn parse_content_range(value: &str) -> Option<(u64, Option<u64>)> {
let value = value.trim();
let rest = value
.get(..6)
.filter(|unit| unit.eq_ignore_ascii_case("bytes "))
.map(|_| &value[6..])?;
let (range, total) = rest.split_once('/')?;
let (start, _end) = range.trim().split_once('-')?;
let start = start.trim().parse().ok()?;
let total = match total.trim() {
"*" => None,
total => Some(total.parse().ok()?),
};
Some((start, total))
}
/// Why a (re)connection attempt failed.
enum Failure {
/// Worth retrying (network error, 5xx, 408/429, odd range answer).
Transient(String),
/// Retrying will not help (4xx such as 403/404/410).
Fatal(String),
}
struct Downloader {
shared: Arc<Shared>,
client: Client,
url: String,
}
/// Marks the downloader as exited however `run` returns (including a panic).
struct ExitFlag(Arc<Shared>);
impl Drop for ExitFlag {
fn drop(&mut self) {
// Set under the lock so a reader that checked `exited` and is about
// to wait cannot miss the notification.
let mut state = self.0.lock();
state.exited = true;
drop(state);
self.0.data_ready.notify_all();
}
}
impl Downloader {
fn run(self, first: Response) {
let _exit = ExitFlag(Arc::clone(&self.shared));
let mut generation = 0u64;
let mut response = Some(first);
// File offset of the next byte `response` yields, or where the next
// request should start when there is no response.
let mut offset = 0u64;
// Consecutive failures of the current generation (drives backoff).
let mut failures = 0u32;
let mut buf = vec![0u8; CHUNK];
loop {
if !self.wait_for_work(&mut generation, &mut response, &mut offset, &mut failures) {
return;
}
if response.is_none() {
match self.reopen(offset) {
Ok(Some((next, start))) => {
response = Some(next);
offset = start;
}
Ok(None) => {
// Nothing left at this offset (HTTP 416).
self.mark_done(generation, offset, false);
continue;
}
Err(Failure::Fatal(message)) => {
failures = 0;
self.fail(generation, message);
continue;
}
Err(Failure::Transient(message)) => {
if !self.retry(generation, &mut failures, &message) {
return;
}
continue;
}
}
}
let Some(body) = response.as_mut() else {
continue;
};
match body.read(&mut buf) {
Ok(0) => {
response = None;
let total = self.shared.lock().total;
if total.is_some_and(|total| offset < total) {
// Connection closed early: resume from `offset`.
let message = format!("connection closed at byte {offset}");
if !self.retry(generation, &mut failures, &message) {
return;
}
continue;
}
self.mark_done(generation, offset, true);
}
Ok(read) => {
failures = 0;
let start = offset;
offset += read as u64;
self.publish(generation, start, &buf[..read]);
}
Err(err) if err.kind() == io::ErrorKind::Interrupted => {}
Err(err) => {
response = None;
if !self.retry(generation, &mut failures, &err.to_string()) {
return;
}
}
}
}
}
/// Blocks until there is something to download, adopting restarts
/// requested by the reader. Returns `false` when the reader is gone.
fn wait_for_work(
&self,
generation: &mut u64,
response: &mut Option<Response>,
offset: &mut u64,
failures: &mut u32,
) -> bool {
let mut state = self.shared.lock();
loop {
if state.closed {
return false;
}
if state.generation != *generation {
*generation = state.generation;
// A new generation gets a fresh backoff.
*failures = 0;
// Keep the open response when the new target is a little
// ahead of it (or anywhere ahead if ranges are unsupported):
// discarding is cheaper than reconnecting.
let keep = response.is_some()
&& state.target >= *offset
&& (!state.ranges || state.target - *offset <= DISCARD_LIMIT);
if !keep {
*response = None;
*offset = state.target;
}
continue;
}
let paused = state.done
|| state.failed
|| state.window_end() >= state.reader_pos.saturating_add(READ_AHEAD);
if !paused {
return true;
}
state = self
.shared
.space_ready
.wait(state)
.unwrap_or_else(PoisonError::into_inner);
}
}
/// Requests the file from `offset` (from 0 when ranges are unsupported).
/// `Ok(None)` means the server has nothing at that offset.
fn reopen(&self, offset: u64) -> std::result::Result<Option<(Response, u64)>, Failure> {
let ranges = self.shared.lock().ranges;
let mut request = self.client.get(&self.url);
if ranges {
request = request.header(RANGE, format!("bytes={offset}-"));
}
let response = request
.send()
.map_err(|err| Failure::Transient(format!("{:#}", anyhow!(err))))?;
let status = response.status();
if ranges && status == StatusCode::RANGE_NOT_SATISFIABLE {
return Ok(None);
}
if status.is_client_error()
&& status != StatusCode::REQUEST_TIMEOUT
&& status != StatusCode::TOO_MANY_REQUESTS
{
return Err(Failure::Fatal(format!("HTTP {status}")));
}
let classified = classify(&response, if ranges { offset } else { 0 })
.map_err(|err| Failure::Transient(format!("{err:#}")))?;
let mut state = self.shared.lock();
if !classified.ranges && state.ranges {
// The server stopped honouring Range: read from 0 and discard.
state.ranges = false;
}
if state.total.is_none() {
state.total = classified.total;
}
Ok(Some((response, classified.start)))
}
fn mark_done(&self, generation: u64, offset: u64, learn_total: bool) {
let mut state = self.shared.lock();
if state.generation != generation {
return;
}
state.done = true;
if learn_total && state.total.is_none() {
state.total = Some(offset.max(state.window_end()));
}
self.shared.data_ready.notify_all();
}
/// Abandons the current generation with an error for the reader.
fn fail(&self, generation: u64, message: String) {
let mut state = self.shared.lock();
if state.generation != generation {
return;
}
tracing::warn!("podcast stream failed: {message}");
state.failed = true;
state.error = Some((io::ErrorKind::Other, message));
self.shared.data_ready.notify_all();
}
/// Records a transient failure and backs off before the next attempt.
/// Returns `false` when the thread should exit.
fn retry(&self, generation: u64, failures: &mut u32, message: &str) -> bool {
let mut state = self.shared.lock();
if state.closed {
return false;
}
if state.generation != generation {
// The reader moved on; `wait_for_work` adopts the new target.
return true;
}
*failures = failures.saturating_add(1);
state.last_error = Some(message.to_string());
tracing::debug!("podcast stream attempt failed ({failures} in a row): {message}");
let delay = RETRY_BASE
.saturating_mul(1 << (*failures - 1).min(6))
.min(MAX_BACKOFF);
let started = Instant::now();
// Wakes early when the reader goes away, seeks elsewhere or starts
// waiting for data (which shortens the delay).
loop {
if state.closed {
return false;
}
if state.generation != generation {
return true;
}
let limit = if state.reader_waiting {
delay.min(STARVED_BACKOFF)
} else {
delay
};
let elapsed = started.elapsed();
if elapsed >= limit {
return true;
}
state = self
.shared
.space_ready
.wait_timeout(state, limit - elapsed)
.unwrap_or_else(PoisonError::into_inner)
.0;
}
}
fn publish(&self, generation: u64, start: u64, bytes: &[u8]) {
let mut state = self.shared.lock();
if state.generation != generation {
return;
}
state.progress = state.progress.wrapping_add(bytes.len() as u64);
state.last_error = None;
let end = start + bytes.len() as u64;
// Permanent copy of the file head for format probing.
let head_len = state.head.len() as u64;
if start <= head_len && end > head_len && head_len < HEAD_KEEP as u64 {
let from = (head_len - start) as usize;
let to = (HEAD_KEEP as u64).min(end) - start;
state.head.extend_from_slice(&bytes[from..to as usize]);
}
// Only bytes at or after the window end are new; earlier ones are
// being skipped (Range-less seek or resume) or are already buffered.
let want = state.window_end();
if end > want {
if start > want {
// A gap should not happen; resynchronise rather than corrupt.
state.window.clear();
state.window_start = start;
state.window.extend(bytes);
} else {
state.window.extend(&bytes[(want - start) as usize..]);
}
state.trim_behind();
}
state.peak_buffered = state.peak_buffered.max(state.buffered());
state.peak_capacity = state.peak_capacity.max(state.window.capacity());
self.shared.data_ready.notify_all();
}
}
fn downloader_stopped() -> io::Error {
io::Error::other("podcast stream downloader stopped")
}
impl Read for HttpStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if buf.is_empty() {
return Ok(0);
}
let shared = &*self.shared;
let state = shared.lock();
let (mut state, result) = read_locked(shared, &mut self.pos, state, buf);
state.reader_waiting = false;
result
}
}
/// The body of [`HttpStream::read`]; hands the guard back so the caller can
/// clear `reader_waiting` on every exit path.
fn read_locked<'a>(
shared: &Shared,
pos: &mut u64,
mut state: MutexGuard<'a, State>,
buf: &mut [u8],
) -> (MutexGuard<'a, State>, io::Result<usize>) {
// When this read started waiting, and the download progress then.
let mut starved: Option<(Instant, u64)> = None;
loop {
if state.total.is_some_and(|total| *pos >= total) {
return (state, Ok(0));
}
if *pos < state.head.len() as u64 {
let from = *pos as usize;
let len = buf.len().min(state.head.len() - from);
buf[..len].copy_from_slice(&state.head[from..from + len]);
*pos += len as u64;
return (state, Ok(len));
}
let window_end = state.window_end();
if *pos >= state.window_start && *pos < window_end {
let from = (*pos - state.window_start) as usize;
let len = buf.len().min(state.window.len() - from);
let (front, back) = state.window.as_slices();
copy_from_ring(front, back, from, &mut buf[..len]);
*pos += len as u64;
state.reader_pos = *pos;
// Free what fell out of the keep-behind range even when the
// downloader is idle (paused or done).
state.trim_behind();
shared.space_ready.notify_all();
return (state, Ok(len));
}
// Not buffered. Wait for the current generation if it is heading
// here, otherwise restart the downloader at the reader position.
let reachable = *pos >= state.window_start
&& (!state.ranges || *pos <= window_end.saturating_add(NEAR_GAP));
if reachable && !state.failed {
if state.done {
return (state, Ok(0));
}
if state.exited {
return (state, Err(downloader_stopped()));
}
if state.reader_pos != *pos {
state.reader_pos = *pos;
state.trim_behind();
shared.space_ready.notify_all();
}
let now = Instant::now();
let since = match starved {
Some((since, seen)) if seen == state.progress => since,
_ => {
starved = Some((now, state.progress));
now
}
};
let waited = now.saturating_duration_since(since);
if waited >= STARVE_TIMEOUT {
// Give up on this generation; the next read retries.
state.failed = true;
state.error = None;
let cause = state
.last_error
.take()
.unwrap_or_else(|| "no data received".to_string());
let error = io::Error::new(
io::ErrorKind::TimedOut,
format!(
"podcast stream stalled for {}s: {cause}",
STARVE_TIMEOUT.as_secs()
),
);
return (state, Err(error));
}
if !state.reader_waiting {
// Lets a downloader in backoff retry sooner.
state.reader_waiting = true;
shared.space_ready.notify_all();
}
state = shared
.data_ready
.wait_timeout(state, STARVE_TIMEOUT - waited)
.unwrap_or_else(PoisonError::into_inner)
.0;
continue;
}
if let Some((kind, message)) = state.error.take() {
// Report once; the next read retries from scratch.
return (state, Err(io::Error::new(kind, message)));
}
if state.exited {
return (state, Err(downloader_stopped()));
}
state.restart_at(*pos);
state.reader_pos = *pos;
starved = None;
shared.space_ready.notify_all();
}
}
fn copy_from_ring(front: &[u8], back: &[u8], from: usize, out: &mut [u8]) {
let mut written = 0;
if from < front.len() {
let len = out.len().min(front.len() - from);
out[..len].copy_from_slice(&front[from..from + len]);
written = len;
}
if written < out.len() {
let back_from = from + written - front.len();
let len = out.len() - written;
out[written..].copy_from_slice(&back[back_from..back_from + len]);
}
}
impl Seek for HttpStream {
fn seek(&mut self, position: SeekFrom) -> io::Result<u64> {
let target = match position {
SeekFrom::Start(offset) => i128::from(offset),
SeekFrom::Current(delta) => i128::from(self.pos) + i128::from(delta),
SeekFrom::End(delta) => {
let Some(total) = self.shared.lock().total else {
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"stream length is unknown",
));
};
i128::from(total) + i128::from(delta)
}
};
let target = u64::try_from(target).map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidInput,
"invalid seek to a negative or overflowing position",
)
})?;
// Lazy: the downloader is only redirected when a read needs data.
self.pos = target;
Ok(target)
}
}
impl Drop for HttpStream {
fn drop(&mut self) {
let mut state = self.shared.lock();
state.closed = true;
state.window = VecDeque::new();
state.head = Vec::new();
drop(state);
self.shared.space_ready.notify_all();
self.shared.data_ready.notify_all();
}
}
#[cfg(test)]
impl HttpStream {
/// Bytes currently held in memory (window + head).
pub(super) fn buffered_bytes(&self) -> usize {
self.shared.lock().buffered()
}
/// Highest number of bytes ever held in memory at once.
pub(super) fn peak_buffered_bytes(&self) -> usize {
self.shared.lock().peak_buffered
}
/// Largest allocation (window + head capacity) ever held.
pub(super) fn peak_allocated_bytes(&self) -> usize {
let state = self.shared.lock();
state.peak_capacity + state.head.capacity()
}
/// Flag the downloader thread sets when it exits.
pub(super) fn exit_probe(&self) -> impl Fn() -> bool + Send + 'static {
let shared = Arc::clone(&self.shared);
move || shared.lock().exited
}
}
/// Upper bound of [`HttpStream`] memory use, for tests.
#[cfg(test)]
pub(super) const MAX_BUFFERED: usize = WINDOW_CAP + HEAD_KEEP;
+260
View File
@@ -0,0 +1,260 @@
//! Minimal HTTP/1.1 server on 127.0.0.1 for podcast tests (no internet).
//! One request per connection (`Connection: close`).
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;
#[derive(Clone)]
pub(super) struct Route {
pub status: u16,
pub body: Arc<Vec<u8>>,
pub content_type: String,
pub etag: Option<String>,
/// Honour `Range` requests with 206 responses.
pub ranges: bool,
/// Send `Content-Length` (otherwise the body is close-delimited).
pub send_length: bool,
/// Close the connection once the body reaches this absolute file offset,
/// on the first `fail_times` responses (simulates a flaky network).
pub cut_after: Option<usize>,
pub fail_times: usize,
}
impl Route {
pub fn new(body: impl Into<Vec<u8>>, content_type: &str) -> Route {
Route {
status: 200,
body: Arc::new(body.into()),
content_type: content_type.to_string(),
etag: None,
ranges: true,
send_length: true,
cut_after: None,
fail_times: 0,
}
}
pub fn status(mut self, status: u16) -> Route {
self.status = status;
self
}
pub fn etag(mut self, etag: &str) -> Route {
self.etag = Some(etag.to_string());
self
}
pub fn no_ranges(mut self) -> Route {
self.ranges = false;
self
}
pub fn no_length(mut self) -> Route {
self.send_length = false;
self
}
pub fn cut(mut self, after: usize, times: usize) -> Route {
self.cut_after = Some(after);
self.fail_times = times;
self
}
}
#[derive(Default)]
struct ServerState {
routes: HashMap<String, Route>,
/// Per path: the `Range` header of every request (None if absent).
requests: HashMap<String, Vec<Option<String>>>,
failures: HashMap<String, usize>,
}
pub(super) struct TestServer {
base: String,
state: Arc<Mutex<ServerState>>,
}
impl TestServer {
pub fn start() -> TestServer {
let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server");
let base = format!("http://{}", listener.local_addr().expect("local addr"));
let state = Arc::new(Mutex::new(ServerState::default()));
let accept_state = Arc::clone(&state);
std::thread::spawn(move || {
for stream in listener.incoming() {
let Ok(stream) = stream else { continue };
let state = Arc::clone(&accept_state);
std::thread::spawn(move || handle(stream, &state));
}
});
TestServer { base, state }
}
pub fn url(&self, path: &str) -> String {
format!("{}{path}", self.base)
}
pub fn set(&self, path: &str, route: Route) {
self.lock().routes.insert(path.to_string(), route);
}
pub fn requests(&self, path: &str) -> Vec<Option<String>> {
self.lock().requests.get(path).cloned().unwrap_or_default()
}
fn lock(&self) -> std::sync::MutexGuard<'_, ServerState> {
self.state.lock().unwrap_or_else(PoisonError::into_inner)
}
}
fn handle(stream: TcpStream, state: &Mutex<ServerState>) {
let _ = stream.set_read_timeout(Some(Duration::from_secs(10)));
let mut reader = BufReader::new(match stream.try_clone() {
Ok(stream) => stream,
Err(_) => return,
});
let mut request_line = String::new();
if reader.read_line(&mut request_line).is_err() {
return;
}
let path = request_line
.split_whitespace()
.nth(1)
.unwrap_or("/")
.to_string();
let mut headers = HashMap::new();
loop {
let mut line = String::new();
match reader.read_line(&mut line) {
Ok(0) | Err(_) => return,
Ok(_) => {}
}
let line = line.trim_end();
if line.is_empty() {
break;
}
if let Some((name, value)) = line.split_once(':') {
headers.insert(name.trim().to_ascii_lowercase(), value.trim().to_string());
}
}
let range_header = headers.get("range").cloned();
let (route, cut) = {
let mut state = state.lock().unwrap_or_else(PoisonError::into_inner);
state
.requests
.entry(path.clone())
.or_default()
.push(range_header.clone());
let route = state.routes.get(&path).cloned();
let cut = match &route {
Some(route) if route.cut_after.is_some() => {
let failures = state.failures.entry(path.clone()).or_default();
if *failures < route.fail_times {
*failures += 1;
route.cut_after
} else {
None
}
}
_ => None,
};
(route, cut)
};
let mut out = stream;
let Some(route) = route else {
let _ = out
.write_all(b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");
return;
};
if let (Some(etag), Some(sent)) = (&route.etag, headers.get("if-none-match"))
&& etag == sent
{
let _ = write!(
out,
"HTTP/1.1 304 Not Modified\r\nETag: {etag}\r\nConnection: close\r\n\r\n"
);
return;
}
let total = route.body.len();
let mut status = route.status;
let mut start = 0usize;
let mut end = total;
let mut extra = String::new();
if route.status == 200
&& route.ranges
&& let Some(range) = range_header
.as_deref()
.and_then(|r| r.strip_prefix("bytes="))
&& let Some((from, to)) = range.split_once('-')
&& let Ok(from) = from.trim().parse::<usize>()
{
if from >= total {
let _ = write!(
out,
"HTTP/1.1 416 Range Not Satisfiable\r\nContent-Range: bytes */{total}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
);
return;
}
let to = to
.trim()
.parse::<usize>()
.map_or(total, |to| (to + 1).min(total));
status = 206;
start = from;
end = to;
extra.push_str(&format!(
"Content-Range: bytes {start}-{}/{total}\r\n",
end - 1
));
}
if route.ranges {
extra.push_str("Accept-Ranges: bytes\r\n");
}
if let Some(etag) = &route.etag {
extra.push_str(&format!("ETag: {etag}\r\n"));
}
if route.send_length {
extra.push_str(&format!("Content-Length: {}\r\n", end - start));
}
let reason = match status {
200 => "OK",
206 => "Partial Content",
404 => "Not Found",
500 => "Internal Server Error",
_ => "Status",
};
if write!(
out,
"HTTP/1.1 {status} {reason}\r\nContent-Type: {}\r\n{extra}Connection: close\r\n\r\n",
route.content_type
)
.is_err()
{
return;
}
let limit = cut.map_or(end, |cut| cut.clamp(start, end));
for chunk in route.body[start..limit].chunks(64 * 1024) {
if out.write_all(chunk).is_err() {
return;
}
}
let _ = out.flush();
let _ = out.shutdown(std::net::Shutdown::Both);
}
/// Deterministic pseudo-random bytes.
pub(super) fn noise(len: usize, seed: u64) -> Vec<u8> {
let mut state = seed | 1;
let mut out = Vec::with_capacity(len);
while out.len() < len {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
out.extend_from_slice(&state.to_le_bytes());
}
out.truncate(len);
out
}
File diff suppressed because it is too large. Load diff
+190 -41
View File
@@ -7,13 +7,13 @@
use std::collections::{HashMap, VecDeque};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use std::time::Instant;
use anyhow::{Context as _, Result};
use futures_util::StreamExt as _;
use rodio::{Decoder, Source as _};
use rodio::Source as _;
use rustfft::FftPlanner;
use rustfft::num_complex::Complex;
use sha2::{Digest as _, Sha256};
@@ -36,7 +36,7 @@ const EMBEDDING_DIMENSIONS: usize = 1280;
const MODEL_BATCH: usize = 8;
const MAX_MODEL_BYTES: usize = 64 * 1024 * 1024;
const RESULT_LIMIT: usize = 50;
const MAX_PER_ARTIST: usize = 3;
const PEER_CANDIDATE_MAX_PER_ARTIST: usize = 10;
const NEAR_DUPLICATE_COSINE: f32 = 0.995;
const FULL_TRACK_MAX_SECONDS: u32 = 5 * 60;
const LONG_TRACK_WINDOW_SECONDS: u32 = 60;
@@ -101,7 +101,7 @@ impl Phase {
}
}
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SimilarityStatus {
pub phase: Phase,
pub active_profile: Option<String>,
@@ -144,6 +144,8 @@ pub struct Manager {
settings: Mutex<SimilaritySettings>,
workers: AtomicUsize,
generation: AtomicU64,
pipeline_running: AtomicBool,
rescan_requested: AtomicBool,
status: Mutex<SimilarityStatus>,
index: RwLock<Index>,
model: Mutex<Option<(String, RunnableModel)>>,
@@ -169,13 +171,20 @@ impl Manager {
Err(err) => tracing::warn!(%err, "similarity index restore failed"),
}
}
let target_profile = model_by_id(&settings.model)
.filter(|_| profile_by_id(&settings.profile).is_some())
.map(|model| profile_fingerprint(model, &settings.profile));
let restored_profile_is_current = index.profile_id == target_profile;
let status = SimilarityStatus {
phase: if settings.enabled {
Phase::Loading
} else {
phase: if !settings.enabled {
Phase::Disabled
} else if restored_profile_is_current {
Phase::Ready
} else {
Phase::Loading
},
active_profile: index.profile_id.clone(),
target_profile,
model: settings.model.clone(),
..SimilarityStatus::default()
};
@@ -184,6 +193,8 @@ impl Manager {
event_tx,
workers: AtomicUsize::new(settings.workers.clamp(1, 16)),
generation: AtomicU64::new(0),
pipeline_running: AtomicBool::new(false),
rescan_requested: AtomicBool::new(false),
settings: Mutex::new(settings),
status: Mutex::new(status),
index: RwLock::new(index),
@@ -223,23 +234,51 @@ impl Manager {
|| previous.model != settings.model
|| previous.profile != settings.profile
{
self.generation.fetch_add(1, Ordering::AcqRel);
self.start();
}
}
/// Requests a scan without cancelling useful work already in progress.
/// Bursts of library-change notifications collapse into one follow-up
/// pass, so metadata refreshes cannot repeatedly restart the model.
pub fn start(self: &Arc<Self>) {
let generation = self.generation.fetch_add(1, Ordering::AcqRel) + 1;
self.rescan_requested.store(true, Ordering::Release);
if self.pipeline_running.swap(true, Ordering::AcqRel) {
return;
}
let this = Arc::clone(self);
tokio::spawn(async move {
if let Err(err) = this.run_pipeline(generation).await
&& this.generation.load(Ordering::Acquire) == generation
{
tracing::error!(%err, "similarity pipeline failed");
this.update_status(|status| {
status.phase = Phase::Error;
status.current_track = None;
status.last_error = Some(format!("{err:#}"));
});
loop {
// This pass covers every notification received before it
// starts. A notification during the pass requests one more.
this.rescan_requested.store(false, Ordering::Release);
let generation = this.generation.load(Ordering::Acquire);
if let Err(err) = this.run_pipeline(generation).await
&& this.generation.load(Ordering::Acquire) == generation
{
tracing::error!(%err, "similarity pipeline failed");
this.update_status(|status| {
status.phase = Phase::Error;
status.current_track = None;
status.last_error = Some(format!("{err:#}"));
});
}
if this.rescan_requested.load(Ordering::Acquire) {
continue;
}
this.pipeline_running.store(false, Ordering::Release);
// Close the small race between checking the request flag and
// releasing ownership of the worker. If another worker has
// already claimed it, that worker owns the pending pass.
if this.rescan_requested.swap(false, Ordering::AcqRel)
&& !this.pipeline_running.swap(true, Ordering::AcqRel)
{
continue;
}
break;
}
});
}
@@ -329,6 +368,52 @@ impl Manager {
exclude_track_id: Option<i64>,
exclude_content_id: Option<&str>,
limit: usize,
) -> Result<Vec<SimilarTrack>> {
let settings = lock(&self.settings);
let minimum_score = settings.minimum_score;
let max_tracks_per_artist = settings.max_tracks_per_artist;
drop(settings);
self.search_vector_with_policy(
profile_id,
vector,
exclude_track_id,
exclude_content_id,
limit,
minimum_score,
max_tracks_per_artist,
)
}
/// Returns a wider, policy-neutral candidate set to a remote requester.
/// The requester applies its own score threshold and artist diversity
/// limit; neither value is part of embedding compatibility.
pub(crate) fn search_vector_for_peer(
&self,
profile_id: &str,
vector: &[f32],
limit: usize,
) -> Result<Vec<SimilarTrack>> {
self.search_vector_with_policy(
profile_id,
vector,
None,
None,
limit,
-1.0,
PEER_CANDIDATE_MAX_PER_ARTIST,
)
}
#[allow(clippy::too_many_arguments)]
fn search_vector_with_policy(
&self,
profile_id: &str,
vector: &[f32],
exclude_track_id: Option<i64>,
exclude_content_id: Option<&str>,
limit: usize,
minimum_score: f32,
max_tracks_per_artist: usize,
) -> Result<Vec<SimilarTrack>> {
anyhow::ensure!(
!vector.is_empty() && vector.len() <= 4096,
@@ -357,7 +442,7 @@ impl Manager {
entry.vector.as_slice(),
)
})
.filter(|(_, score, _, _)| score.is_finite())
.filter(|(_, score, _, _)| score.is_finite() && *score >= minimum_score)
.collect();
scores.sort_by(|left, right| right.1.total_cmp(&left.1));
@@ -371,7 +456,7 @@ impl Manager {
continue;
}
let count = artist_counts.entry(artist.to_string()).or_default();
if !artist.is_empty() && *count >= MAX_PER_ARTIST {
if !artist.is_empty() && *count >= max_tracks_per_artist {
continue;
}
*count += 1;
@@ -431,7 +516,6 @@ impl Manager {
)?;
let stats = self.library.similarity_storage_stats(&profile_id)?;
self.update_status(|status| {
status.phase = Phase::Downloading;
status.target_profile = Some(profile_id.clone());
status.model = spec.id.to_string();
status.total_tracks = stats.total_tracks;
@@ -442,21 +526,18 @@ impl Manager {
status.current_track = None;
status.last_error = None;
});
let model_path = self.ensure_model(spec, generation).await?;
self.ensure_generation(generation)?;
self.update_status(|status| status.phase = Phase::Loading);
let model = self.load_model(&profile_id, &model_path).await?;
self.ensure_generation(generation)?;
let mut pending: VecDeque<_> = self.library.pending_similarity_tracks(&profile_id)?.into();
let pending_total = pending.len();
self.update_status(|status| {
status.phase = if pending_total == 0 {
Phase::Loading
} else {
Phase::Processing
};
});
if pending.is_empty() {
self.ensure_generation(generation)?;
return self.activate_profile(profile_id);
}
let model_path = self.ensure_model(spec, generation).await?;
self.ensure_generation(generation)?;
let model = self.load_model(&profile_id, &model_path).await?;
self.ensure_generation(generation)?;
self.update_status(|status| status.phase = Phase::Processing);
let mut jobs = tokio::task::JoinSet::new();
while !pending.is_empty() || !jobs.is_empty() {
@@ -506,6 +587,10 @@ impl Manager {
}
}
self.ensure_generation(generation)?;
self.activate_profile(profile_id)
}
fn activate_profile(&self, profile_id: String) -> Result<()> {
let entries = self.library.load_similarity_index(&profile_id)?;
let total_tracks = self
.library
@@ -519,7 +604,12 @@ impl Manager {
profile_id: Some(profile_id.clone()),
entries,
};
lock(&self.settings).active_profile = Some(profile_id.clone());
let profile_changed = {
let mut settings = lock(&self.settings);
let changed = settings.active_profile.as_deref() != Some(&profile_id);
settings.active_profile = Some(profile_id.clone());
changed
};
let stats = self.library.similarity_storage_stats(&profile_id)?;
self.update_status(|status| {
status.phase = Phase::Ready;
@@ -531,9 +621,11 @@ impl Manager {
status.stored_bytes = stats.stored_bytes;
status.current_track = None;
});
let _ = self
.event_tx
.send(AppEvent::SimilarityProfileActivated(Some(profile_id)));
if profile_changed {
let _ = self
.event_tx
.send(AppEvent::SimilarityProfileActivated(Some(profile_id)));
}
Ok(())
}
@@ -561,6 +653,7 @@ impl Manager {
tokio::fs::remove_file(&path).await?;
}
self.update_status(|status| status.phase = Phase::Downloading);
let response = reqwest::get(spec.url).await?.error_for_status()?;
let tmp = path.with_extension(format!("part-{}-{generation}", std::process::id()));
let mut file = tokio::fs::File::create(&tmp).await?;
@@ -603,6 +696,7 @@ impl Manager {
{
return Ok(Arc::clone(model));
}
self.update_status(|status| status.phase = Phase::Loading);
let path = path.to_path_buf();
let model = tokio::task::spawn_blocking(move || load_onnx(&path))
.await
@@ -614,10 +708,13 @@ impl Manager {
fn update_status(&self, update: impl FnOnce(&mut SimilarityStatus)) {
let snapshot = {
let mut status = lock(&self.status);
let previous = status.clone();
update(&mut status);
status.clone()
(*status != previous).then(|| status.clone())
};
let _ = self.event_tx.send(AppEvent::SimilarityStatus(snapshot));
if let Some(snapshot) = snapshot {
let _ = self.event_tx.send(AppEvent::SimilarityStatus(snapshot));
}
}
}
@@ -750,8 +847,15 @@ fn decode_mono_window(
length_seconds: Option<f64>,
) -> Result<Vec<f32>> {
let file = File::open(path).with_context(|| format!("opening {}", path.display()))?;
let mut decoder =
Decoder::try_from(file).with_context(|| format!("decoding {}", path.display()))?;
let byte_len = file.metadata().ok().map(|metadata| metadata.len());
let mut decoder = crate::player::decode_source(
Box::new(std::io::BufReader::new(file)),
byte_len,
None,
true,
)
.map_err(anyhow::Error::msg)
.with_context(|| format!("decoding {}", path.display()))?;
let channels = decoder.channels().get() as usize;
let source_rate = decoder.sample_rate().get() as usize;
if start_seconds > 0.0 {
@@ -977,6 +1081,14 @@ fn write<T>(lock: &RwLock<T>) -> std::sync::RwLockWriteGuard<'_, T> {
mod tests {
use super::*;
fn unique_test_dir(label: &str) -> PathBuf {
let unique = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
std::env::temp_dir().join(format!("furumi-{label}-{}-{unique}", std::process::id()))
}
#[test]
fn profile_fingerprint_changes_with_contract() {
let model = &MODELS[0];
@@ -1010,6 +1122,43 @@ mod tests {
assert!(!is_near_duplicate(&distinct, &[&query]));
}
#[tokio::test]
async fn repeated_rescans_keep_an_up_to_date_profile_ready() {
let directory = unique_test_dir("similarity-stable-status");
let library = Arc::new(Library::open(&directory.join("library.db")).unwrap());
let profile_id = profile_fingerprint(&MODELS[0], DEFAULT_PROFILE_ID);
let settings = SimilaritySettings {
enabled: true,
active_profile: Some(profile_id),
..SimilaritySettings::default()
};
let (event_tx, mut event_rx) = tokio::sync::mpsc::unbounded_channel();
let manager = Manager::new(Arc::clone(&library), event_tx, settings);
assert_eq!(manager.status().phase, Phase::Ready);
for _ in 0..32 {
manager.start();
}
tokio::time::timeout(std::time::Duration::from_secs(2), async {
while manager.pipeline_running.load(Ordering::Acquire) {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
assert_eq!(manager.status().phase, Phase::Ready);
while let Ok(event) = event_rx.try_recv() {
if let AppEvent::SimilarityStatus(status) = event {
assert_eq!(status.phase, Phase::Ready);
}
}
drop(manager);
drop(library);
std::fs::remove_dir_all(directory).unwrap();
}
#[test]
fn resampling_keeps_a_constant_signal() {
let output = resample_sinc(&vec![0.25; 441], 44_100, 16_000);
+30 -5
View File
@@ -101,11 +101,30 @@ impl Read for GrowingFileReader {
}
impl Seek for GrowingFileReader {
fn seek(&mut self, _: SeekFrom) -> io::Result<u64> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"streaming playback is not seekable yet",
))
fn seek(&mut self, position: SeekFrom) -> io::Result<u64> {
// Random access beyond the downloaded prefix is intentionally not
// exposed, but decoders may inspect and rewind the available header.
let available = self
.shared
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.available;
let target = match position {
SeekFrom::Start(position) => i128::from(position),
SeekFrom::Current(offset) => i128::from(self.pos) + i128::from(offset),
SeekFrom::End(offset) => i128::from(available) + i128::from(offset),
};
if target < 0 || target > i128::from(available) {
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"cannot seek outside the downloaded audio prefix",
));
}
let target = target as u64;
self.file.seek(SeekFrom::Start(target))?;
self.pos = target;
Ok(target)
}
}
@@ -129,6 +148,12 @@ mod tests {
reader.read_exact(&mut first).unwrap();
assert_eq!(&first, b"fur");
reader.seek(SeekFrom::Start(0)).unwrap();
let mut rewind = [0u8; 3];
reader.read_exact(&mut rewind).unwrap();
assert_eq!(&rewind, b"fur");
assert!(reader.seek(SeekFrom::Start(4)).is_err());
output.write_all(b"umi").unwrap();
writer.add_available(3);
writer.finish();
+191
View File
@@ -0,0 +1,191 @@
//! Secondary settings window; child dialogs are rendered above it.
use ratatui::{
Frame,
layout::{Constraint, Layout, Rect},
text::Line,
widgets::{Block, Clear, List, ListItem, ListState, Paragraph, Wrap},
};
use super::theme;
use crate::app::state::{AppState, SettingsRow, additional_settings_rows};
pub fn draw(frame: &mut Frame, state: &AppState) {
let screen = frame.area();
let width = screen.width.saturating_sub(2).min(90);
let height = screen.height.saturating_sub(2).min(26);
let area = Rect::new(
screen.x + (screen.width - width) / 2,
screen.y + (screen.height - height) / 2,
width,
height,
);
let block = Block::bordered()
.title(" Additional settings ")
.title_style(theme::header_for(state))
.border_style(theme::strong_border_for(state));
let inner = block.inner(area);
frame.render_widget(Clear, area);
frame.render_widget(block, area);
let [list_area, status_area, footer] = Layout::vertical([
Constraint::Min(1),
Constraint::Length(3),
Constraint::Length(1),
])
.areas(inner);
let rows = additional_settings_rows(state);
let mut items = Vec::new();
let mut selected = 0;
for (index, row) in rows.iter().enumerate() {
let section = match row {
SettingsRow::MusicDirectory => Some("Library"),
SettingsRow::CheckUpdate => Some("Updates"),
SettingsRow::VisualizationClock => Some("Visualizations"),
_ => None,
};
if let Some(section) = section {
items.push(ListItem::new(Line::styled(
section,
theme::header_for(state),
)));
}
if index
== state
.additional_settings_cursor
.min(rows.len().saturating_sub(1))
{
selected = items.len();
}
let (label, value, enabled) = match row {
SettingsRow::MusicDirectory => (
"Music save directory".into(),
if state.music_dir_changing {
"checking/changing...".into()
} else {
state.music_dir.to_string_lossy().into_owned()
},
!state.music_dir_changing,
),
SettingsRow::CheckUpdate => (
"Check for updates".into(),
format!("v{}", env!("CARGO_PKG_VERSION")),
!state.updater.busy && !state.updater.installed,
),
SettingsRow::InstallUpdate => (
"Install update".into(),
state
.updater
.available
.as_ref()
.map(|u| format!("v{}", u.version))
.unwrap_or_else(|| "check for updates first".into()),
state.updater.available.is_some()
&& !state.updater.busy
&& !state.updater.installed,
),
SettingsRow::VisualizationClock => (
"Show clock".into(),
if state.visualizer.config.show_clock {
"on"
} else {
"off"
}
.into(),
true,
),
SettingsRow::VisualizationScript(index) => {
let script = &state.visualizer.scripts[*index];
let mark = if state.visualizer.selected_script_index() == Some(*index) {
"* "
} else {
""
};
(
format!("{mark}{}", script.name),
script
.path
.file_name()
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default(),
true,
)
}
SettingsRow::VisualizationNew => {
("+ New visualization script".into(), "enter".into(), true)
}
SettingsRow::VisualizationEdit => {
("Edit selected visualization".into(), "enter".into(), true)
}
_ => continue,
};
let item = ListItem::new(format!("{label}: {value}"));
items.push(if enabled {
item
} else {
item.style(theme::dim())
});
}
frame.render_stateful_widget(
List::new(items)
.highlight_symbol("> ")
.highlight_style(theme::selection_for(state)),
list_area,
&mut ListState::default().with_selected(Some(selected)),
);
let message = state
.status_message
.as_deref()
.filter(|message| !message.is_empty())
.unwrap_or(&state.updater.message);
frame.render_widget(
Paragraph::new(message)
.wrap(Wrap { trim: true })
.style(theme::dim()),
status_area,
);
frame.render_widget(
Paragraph::new("Up/Down: navigate | Enter: select | Esc: back").style(theme::dim()),
footer,
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn window_renders_controls_and_scrolls_to_last_row_in_small_terminal() {
for (width, height) in [(80, 30), (45, 14)] {
let mut terminal =
ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, height)).unwrap();
let mut state = AppState::default();
state.updater.message = "No newer stable release".into();
terminal.draw(|frame| draw(frame, &state)).unwrap();
let text: String = terminal
.backend()
.buffer()
.content
.iter()
.map(|c| c.symbol())
.collect();
assert!(text.contains("Additional settings"));
assert!(text.contains("Music save directory"));
if height >= 30 {
assert!(text.contains("Check for updates"));
assert!(text.contains("Install update"));
assert!(text.contains("Visualizations"));
}
state.additional_settings_cursor = additional_settings_rows(&state).len() - 1;
terminal.draw(|frame| draw(frame, &state)).unwrap();
let text: String = terminal
.backend()
.buffer()
.content
.iter()
.map(|c| c.symbol())
.collect();
assert!(text.contains("New visualization script"));
assert!(text.contains("No newer stable release"));
}
}
}
+217 -218
View File
@@ -33,7 +33,7 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &AppState) {
}
let rows_height =
(settings_rows(state).len() + 10 + device_presence_sections(state).len()) as u16;
(settings_rows(state).len() + 6 + device_presence_sections(state).len()) as u16;
let [rows_area, _, status_area] = Layout::vertical([
Constraint::Length(rows_height.min(inner.height)),
Constraint::Length(1),
@@ -69,7 +69,6 @@ fn draw_settings_rows(frame: &mut Frame, area: Rect, state: &AppState) {
let mut y = area.y;
let mut cursor = 0usize;
draw_section(frame, area, state, &mut y, "Library");
draw_row(
frame,
area,
@@ -77,48 +76,22 @@ fn draw_settings_rows(frame: &mut Frame, area: Rect, state: &AppState) {
&mut y,
cursor,
state.settings_cursor,
"Music save directory",
if state.music_dir_changing {
format!("{} checking/changing…", state.spinner())
} else {
state.music_dir.to_string_lossy().into_owned()
},
"Additional settings",
"enter".into(),
);
cursor += 1;
draw_row(
frame,
area,
state,
&mut y,
cursor,
state.settings_cursor,
"Full status details",
"enter".to_string(),
);
y = y.saturating_add(1);
draw_section(frame, area, state, &mut y, "Similarity Search");
let similarity = &state.similarity.settings;
for row in SimilarityRow::ALL {
let (label, value) = match row {
SimilarityRow::Toggle => ("Similarity search", on_off(similarity.enabled).to_string()),
SimilarityRow::Model => (
"Embedding model",
crate::similarity::model_by_id(&similarity.model)
.map(|model| format!("{} · {}", model.id, model.license))
.unwrap_or_else(|| similarity.model.clone()),
),
SimilarityRow::Profile => (
"Preprocessing profile",
format!("{} (enter for details)", similarity.profile),
),
SimilarityRow::Workers => ("Background workers", similarity.workers.to_string()),
SimilarityRow::Clear => ("Clear all stored embeddings", "↵".to_string()),
};
draw_row(
frame,
area,
state,
&mut y,
cursor,
state.settings_cursor,
label,
value,
);
cursor += 1;
}
cursor += 1;
y = y.saturating_add(1);
draw_section(frame, area, state, &mut y, "Federation");
@@ -306,39 +279,32 @@ fn draw_settings_rows(frame: &mut Frame, area: Rect, state: &AppState) {
}
y = y.saturating_add(1);
draw_section(frame, area, state, &mut y, "Visualizations");
draw_row(
frame,
area,
state,
&mut y,
cursor,
state.settings_cursor,
"Show clock",
if state.visualizer.config.show_clock {
"[x]".to_string()
} else {
"[ ]".to_string()
},
);
cursor += 1;
for (index, script) in state.visualizer.scripts.iter().enumerate() {
let selected_script = state
.visualizer
.selected_script_index()
.is_some_and(|selected| selected == index);
let label = if selected_script {
format!("* {}", script.name)
} else {
format!(" {}", script.name)
draw_section(frame, area, state, &mut y, "Similarity Search");
let similarity = &state.similarity.settings;
for row in SimilarityRow::ALL {
let (label, value) = match row {
SimilarityRow::Toggle => ("Similarity search", on_off(similarity.enabled).to_string()),
SimilarityRow::Model => (
"Embedding model",
crate::similarity::model_by_id(&similarity.model)
.map(|model| format!("{} · {}", model.id, model.license))
.unwrap_or_else(|| similarity.model.clone()),
),
SimilarityRow::Profile => (
"Preprocessing profile",
format!("{} (enter for details)", similarity.profile),
),
SimilarityRow::MinimumScore => (
"Minimum similarity",
format!("{:.2}", similarity.minimum_score),
),
SimilarityRow::MaxTracksPerArtist => (
"Tracks per artist",
similarity.max_tracks_per_artist.to_string(),
),
SimilarityRow::Workers => ("Background workers", similarity.workers.to_string()),
SimilarityRow::Clear => ("Clear all stored embeddings", "↵".to_string()),
};
let value = script
.path
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("")
.to_string();
draw_row(
frame,
area,
@@ -346,49 +312,11 @@ fn draw_settings_rows(frame: &mut Frame, area: Rect, state: &AppState) {
&mut y,
cursor,
state.settings_cursor,
&label,
label,
value,
);
cursor += 1;
}
draw_row(
frame,
area,
state,
&mut y,
cursor,
state.settings_cursor,
"+ New visualization script",
"↵".to_string(),
);
cursor += 1;
if state.visualizer.selected_script().is_some() {
draw_row(
frame,
area,
state,
&mut y,
cursor,
state.settings_cursor,
"Edit selected visualization",
"↵".to_string(),
);
cursor += 1;
}
y = y.saturating_add(1);
draw_row(
frame,
area,
state,
&mut y,
cursor,
state.settings_cursor,
"Full status details",
"enter".to_string(),
);
}
fn protocol_label(id: &str) -> &str {
@@ -400,6 +328,7 @@ fn protocol_label(id: &str) -> &str {
"catalog" => "Catalog",
"audio" => "Audio transfer",
"similarity" => "Similarity search",
"similarity_dht" => "Similarity DHT",
"device_sync" => "Device sync",
"jam" => "Jam",
other => other,
@@ -540,21 +469,20 @@ fn short_id(id: &str) -> String {
}
fn draw_status_column(frame: &mut Frame, area: Rect, state: &AppState) {
if area.height < 12 {
draw_status(frame, area, state);
return;
}
let [similarity_area, _, federation_area] = Layout::vertical([
Constraint::Length(8),
Constraint::Length(1),
Constraint::Min(0),
])
.areas(area);
draw_similarity_status(frame, similarity_area, state);
draw_status(frame, federation_area, state);
draw_status(frame, area, state);
}
fn draw_similarity_status(frame: &mut Frame, area: Rect, state: &AppState) {
draw_summary_card(
frame,
area,
state,
" Similarity Processing ",
similarity_summary_lines(state),
);
}
fn similarity_summary_lines(state: &AppState) -> Vec<Line<'static>> {
let status = &state.similarity.status;
let progress = if status.total_tracks == 0 {
"0 / 0".to_string()
@@ -571,34 +499,28 @@ fn draw_similarity_status(frame: &mut Frame, area: Rect, state: &AppState) {
.as_deref()
.map(short_id)
.unwrap_or_else(|| "—".to_string());
draw_summary_card(
frame,
area,
state,
" Similarity Processing ",
vec![
status_line("State", status.phase.label().to_string()),
status_line("Progress", progress),
status_line("Active", active),
status_line("Processing", target),
status_line(
"Stored",
format!(
"{} vectors / {}",
status.stored_vectors,
short_bytes_label(status.stored_bytes)
),
vec![
summary_line("State", status.phase.label().to_string()),
summary_line("Progress", progress),
summary_line("Active", active),
summary_line("Processing", target),
summary_line(
"Stored",
format!(
"{} vectors / {}",
status.stored_vectors,
short_bytes_label(status.stored_bytes)
),
status_line(
"Current / errors",
status
.current_track
.clone()
.or_else(|| status.last_error.clone())
.unwrap_or_else(|| format!("{} errors", status.failed_tracks)),
),
],
);
),
summary_line(
"Current",
status
.current_track
.clone()
.or_else(|| status.last_error.clone())
.unwrap_or_else(|| format!("{} errors", status.failed_tracks)),
),
]
}
fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
@@ -615,69 +537,81 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
return;
}
if area.width >= 60 && area.height >= 20 {
let protocols_height =
protocol_card_height(state, area.width.saturating_sub(2), area.height);
let [top_area, _, bottom_area, _, protocols_area, _] = Layout::vertical([
Constraint::Length(7),
Constraint::Length(1),
Constraint::Length(7),
Constraint::Length(1),
Constraint::Length(protocols_height),
Constraint::Min(0),
])
.areas(area);
let [status_area, _, local_area] = Layout::horizontal([
Constraint::Percentage(50),
Constraint::Length(1),
Constraint::Percentage(50),
])
.areas(top_area);
let [transport_area, _, devices_area] = Layout::horizontal([
Constraint::Percentage(50),
Constraint::Length(1),
Constraint::Percentage(50),
])
.areas(bottom_area);
draw_summary_card(
frame,
status_area,
state,
" Status ",
node_summary_lines(state),
);
draw_summary_card(
frame,
local_area,
state,
" Local Data ",
local_data_summary_lines(state),
);
draw_summary_card(
frame,
transport_area,
state,
" Iroh Transport ",
transport_summary_lines(state),
);
draw_summary_card(
frame,
devices_area,
state,
" Connected Devices ",
device_summary_lines(state),
);
draw_summary_card(
frame,
protocols_area,
state,
" Protocol Versions ",
protocol_summary_lines(state, protocols_area.width.saturating_sub(2)),
);
return;
if area.width >= 60 {
let paired_width = area.width.saturating_sub(1) / 2;
let paired_height =
protocol_card_height(state, paired_width.saturating_sub(2), area.height).max(8);
if area.height >= 16 + paired_height {
let [top_area, _, middle_area, _, paired_area, _] = Layout::vertical([
Constraint::Length(7),
Constraint::Length(1),
Constraint::Length(7),
Constraint::Length(1),
Constraint::Length(paired_height),
Constraint::Min(0),
])
.areas(area);
let [status_area, _, local_area] = Layout::horizontal([
Constraint::Percentage(50),
Constraint::Length(1),
Constraint::Percentage(50),
])
.areas(top_area);
let [transport_area, _, devices_area] = Layout::horizontal([
Constraint::Percentage(50),
Constraint::Length(1),
Constraint::Percentage(50),
])
.areas(middle_area);
let [similarity_area, _, protocols_area] = Layout::horizontal([
Constraint::Percentage(50),
Constraint::Length(1),
Constraint::Percentage(50),
])
.areas(paired_area);
draw_summary_card(
frame,
status_area,
state,
" Status ",
node_summary_lines(state),
);
draw_summary_card(
frame,
local_area,
state,
" Local Data ",
local_data_summary_lines(state),
);
draw_summary_card(
frame,
transport_area,
state,
" Iroh Transport ",
transport_summary_lines(state),
);
draw_summary_card(
frame,
devices_area,
state,
" Connected Devices ",
device_summary_lines(state),
);
draw_similarity_status(frame, similarity_area, state);
draw_summary_card(
frame,
protocols_area,
state,
" Protocol Versions ",
protocol_summary_lines(state, protocols_area.width.saturating_sub(2)),
);
return;
}
}
if area.height < 39 {
let protocols_height = protocol_card_height(state, area.width.saturating_sub(2), area.height);
let required_vertical_height = 41u16.saturating_add(protocols_height);
if area.height < required_vertical_height {
frame.render_widget(
Paragraph::new(compact_status_lines(state))
.wrap(ratatui::widgets::Wrap { trim: false }),
@@ -695,6 +629,8 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
_,
local_area,
_,
similarity_area,
_,
protocols_area,
_,
] = Layout::vertical([
@@ -706,11 +642,9 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
Constraint::Length(1),
Constraint::Length(7),
Constraint::Length(1),
Constraint::Length(protocol_card_height(
state,
area.width.saturating_sub(2),
area.height,
)),
Constraint::Length(8),
Constraint::Length(1),
Constraint::Length(protocols_height),
Constraint::Min(0),
])
.areas(area);
@@ -743,6 +677,7 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
" Local Data ",
local_data_summary_lines(state),
);
draw_similarity_status(frame, similarity_area, state);
draw_summary_card(
frame,
protocols_area,
@@ -766,6 +701,12 @@ fn compact_status_lines(state: &AppState) -> Vec<Line<'static>> {
lines.push(Line::styled("Connected Devices", theme::header_for(state)));
lines.extend(device_summary_lines(state).into_iter().take(2));
lines.push(Line::default());
lines.push(Line::styled(
"Similarity Processing",
theme::header_for(state),
));
lines.extend(similarity_summary_lines(state).into_iter().take(3));
lines.push(Line::default());
lines.push(Line::styled("Protocol Versions", theme::header_for(state)));
lines.extend(protocol_summary_lines(state, 0).into_iter().take(3));
lines
@@ -940,7 +881,10 @@ fn node_summary_lines(state: &AppState) -> Vec<Line<'static>> {
]
}
Some(status) => vec![
summary_line("Node", format!("running on {}", status.network)),
summary_line(
"Node",
format!("running on {} · {}", status.network, status.network_health),
),
summary_line(
"Peers",
format!(
@@ -1194,6 +1138,17 @@ fn status_detail_status_lines(state: &AppState, status_cursor: usize) -> Vec<Lin
status.known_contacts
),
));
lines.push(status_line(
"Discovery",
format!(
"{} · {} DHT / {} dial failures · {} rebuilds · {} full recoveries",
status.network_health,
status.rendezvous_failures,
status.peer_dial_failures,
status.rendezvous_restarts,
status.recovery_count
),
));
if !status.connected_peers.is_empty() {
let mut peers: Vec<String> = status
.connected_peers
@@ -1239,6 +1194,9 @@ fn status_detail_status_lines(state: &AppState, status_cursor: usize) -> Vec<Lin
if let Some(error) = &status.last_error {
lines.push(status_line("Error", first_line(error)));
}
if let Some(error) = &status.last_rendezvous_error {
lines.push(status_line("Discovery error", first_line(error)));
}
push_transport_summary_status(&mut lines, state, status);
}
}
@@ -1540,3 +1498,44 @@ fn relative_time_label(value_ms: Option<i64>, now_ms: i64) -> String {
format!("{}d ago", seconds / 60 / 60 / 24)
}
}
#[cfg(test)]
mod update_ui_tests {
use super::*;
#[test]
fn update_controls_and_existing_sections_render_in_both_layouts() {
for width in [80, 160] {
let mut terminal =
ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, 55)).unwrap();
let mut state = AppState::default();
state.updater.message = "No newer stable release".into();
terminal
.draw(|frame| draw(frame, frame.area(), &state))
.unwrap();
let text: String = terminal
.backend()
.buffer()
.content
.iter()
.map(|cell| cell.symbol())
.collect();
let mut previous = 0;
for expected in [
"Additional settings",
"Full status details",
"Federation",
"Connected Devices",
"Similarity Search",
] {
assert!(
text.contains(expected),
"missing {expected} at width {width}"
);
let position = text.find(expected).unwrap();
assert!(position >= previous, "incorrect order for {expected}");
previous = position;
}
}
}
}
+202 -9
View File
@@ -537,6 +537,17 @@ fn draw_artist(frame: &mut Frame, area: Rect, state: &AppState, id: i64, cursor:
about.push_str(&format!(" · appears on {}", detail.featured_tracks.len()));
}
let federation_line = match state.artist_fed_views.get(&id) {
_ if state.federation.settings.enabled
&& !state.global.filters.source_mode.includes_network() =>
{
Some(Line::styled(
"federation: hidden by Local filter",
theme::dim(),
))
}
_ if state.artist_federation_loading(id) => {
Some(super::loading_line(state, "searching peers…"))
}
Some(Loadable::Loading) => Some(super::loading_line(state, "searching peers…")),
Some(Loadable::Ready(card)) => {
let tracks: usize = card
@@ -837,29 +848,58 @@ fn draw_release(frame: &mut Frame, area: Rect, state: &AppState, id: i64, cursor
let artists: Vec<&str> = detail.artists.iter().map(|a| a.name.as_str()).collect();
let year = detail.year.map(|y| format!(" · {y}")).unwrap_or_default();
let info = vec![
let tracks = crate::app::state::release_tracks(state, id, detail);
let federation_line = match state.release_fed_views.get(&id) {
_ if state.federation.settings.enabled
&& !state.global.filters.source_mode.includes_network() =>
{
Some(Line::styled(
"federation: hidden by Local filter",
theme::dim(),
))
}
_ if state.release_federation_loading(id) => {
Some(super::loading_line(state, "searching peers…"))
}
Some(Loadable::Loading) => Some(super::loading_line(state, "searching peers…")),
Some(Loadable::Ready(release)) if !release.tracks.is_empty() => Some(Line::styled(
format!("federation: +{} tracks", release.tracks.len()),
theme::dim(),
)),
Some(Loadable::Ready(_)) => Some(Line::styled(
"federation: no additional tracks",
theme::dim(),
)),
Some(Loadable::Failed(_)) => Some(Line::styled("federation: unavailable", theme::dim())),
_ if state.federation.settings.enabled
&& state.global.filters.source_mode.includes_network() =>
{
Some(Line::styled("federation: pending", theme::dim()))
}
_ => None,
};
let mut info = vec![
Line::default(),
Line::styled(detail.title.clone(), theme::header_for(state)),
Line::raw(artists.join(", ")),
Line::default(),
Line::styled(
format!(
"{}{year} · {} tracks",
detail.release_type,
detail.tracks.len()
),
format!("{}{year} · {} tracks", detail.release_type, tracks.len()),
theme::dim(),
),
];
if let Some(line) = federation_line {
info.push(line);
}
frame.render_widget(Paragraph::new(info), info_area);
// Track list with centered scrolling.
let visible = usize::from(tracks_area.height.max(1));
let total = detail.tracks.len();
let total = tracks.len();
let first = cursor
.saturating_sub(visible / 2)
.min(total.saturating_sub(visible));
for (offset, track) in detail.tracks.iter().enumerate().skip(first).take(visible) {
for (offset, track) in tracks.iter().enumerate().skip(first).take(visible) {
let rect = Rect {
x: tracks_area.x,
y: tracks_area.y + (offset - first) as u16,
@@ -900,6 +940,9 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
title.push_str("· searching… ");
}
let inner = bordered(frame, area, state, title);
if search.similarity_source.is_some() {
return draw_similarity_search(frame, inner, state, cursor);
}
let empty_results = SearchResults::default();
let results = match &search.results {
@@ -924,7 +967,7 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
return centered_line(frame, inner, line);
}
};
if results.len() == 0
if results.is_empty()
&& state.search.fed_tracks.is_empty()
&& state.search.fed_artists.is_empty()
&& !state.search.fed_loading
@@ -1102,6 +1145,156 @@ fn draw_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
}
}
fn draw_similarity_search(frame: &mut Frame, area: Rect, state: &AppState, cursor: usize) {
let search = &state.search;
let status = if search.fed_loading {
super::loading_line(state, "searching federation…")
} else if let Some(stats) = &search.similarity_stats {
let elapsed = if stats.elapsed_ms < 1_000 {
format!("{} ms", stats.elapsed_ms)
} else {
format!("{:.2} s", stats.elapsed_ms as f64 / 1_000.0)
};
Line::from(vec![
Span::styled("Federation · ", theme::accent_for(state)),
Span::styled(
format!(
"{} tracks · {} artists · {} peers queried · {elapsed}",
stats.tracks, stats.artists, stats.peers_queried
),
theme::dim(),
),
])
} else if search.similarity_error.is_some() {
Line::styled(
"Federation search failed · showing local results",
error_style(),
)
} else if search.loading {
super::loading_line(state, "preparing local similarity search…")
} else {
Line::styled("Federation disabled · showing local results", theme::dim())
};
let [status_area, content] =
Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).areas(area);
frame.render_widget(Paragraph::new(status), status_area);
let mut rows: Vec<(Line, Option<String>, Option<usize>)> = Vec::new();
rows.push((Line::styled("Tracks", theme::header_for(state)), None, None));
if let Some(track) = &search.similarity_source_track {
let heart = if state.track_liked(track) {
Span::styled("♥ ", theme::accent_for(state))
} else {
Span::raw(" ")
};
rows.push((
Line::from(vec![
heart,
Span::raw(track.title.clone()),
Span::styled(
format!(" {} · {}", track.artist_line(), track.release_title),
theme::dim(),
),
]),
Some(super::track_meta_suffix(track, true)),
Some(0),
));
}
for (offset, hit) in search.similarity_tracks.iter().enumerate() {
let index = offset + 1;
match hit {
crate::app::state::SimilaritySearchHit::Local { track, .. } => {
let heart = if state.track_liked(track) {
Span::styled("♥ ", theme::accent_for(state))
} else {
Span::raw(" ")
};
rows.push((
Line::from(vec![
heart,
Span::raw(track.title.clone()),
Span::styled(
format!(" {} · {}", track.artist_line(), track.release_title),
theme::dim(),
),
]),
Some(super::track_meta_suffix(track, true)),
Some(index),
));
}
crate::app::state::SimilaritySearchHit::Federated { track, .. } => {
let heart = if state.fed_track_liked(track) {
Span::styled("♥ ", theme::accent_for(state))
} else {
Span::raw(" ")
};
let origin = if track.own {
"your library".to_string()
} else {
format!("peer {}…", track.owner_short())
};
let mut meta = track.duration_label();
if let Some(year) = track.year {
if !meta.is_empty() {
meta.push_str(" · ");
}
meta.push_str(&year.to_string());
}
rows.push((
Line::from(vec![
heart,
fed_track_availability_prefix(state, track),
Span::raw(track.title.clone()),
Span::styled(
format!(" {} · {origin}", track.artist_line()),
theme::dim(),
),
]),
Some(meta),
Some(index),
));
}
}
}
let scope = crate::app::state::TrackSelectionScope::SimilaritySearch;
let mut selected = std::collections::HashSet::new();
if state.track_selection.is_active_for(&scope)
&& let Some(indices) = state
.track_selection
.indices(&scope, search.similarity_len())
{
selected.extend(indices);
}
let cursor_row = rows
.iter()
.position(|(_, _, row_cursor)| *row_cursor == Some(cursor))
.unwrap_or(0);
let visible = usize::from(content.height.max(1));
let first = cursor_row
.saturating_sub(visible / 2)
.min(rows.len().saturating_sub(visible));
for (offset, (line, right, row_cursor)) in
rows.into_iter().enumerate().skip(first).take(visible)
{
let rect = Rect {
x: content.x,
y: content.y + (offset - first) as u16,
width: content.width,
height: 1,
};
if let Some(row_index) = row_cursor
&& selected.contains(&row_index)
&& row_index != cursor
{
frame
.buffer_mut()
.set_style(rect, theme::selection_for(state));
}
draw_row(frame, rect, state, line, right, row_cursor == Some(cursor));
}
}
// ---------------------------------------------------------------------------
// Federated artist card (assembled from peer catalogs)
// ---------------------------------------------------------------------------
+117 -20
View File
@@ -1,9 +1,13 @@
mod additional_settings;
pub mod art;
mod federation;
mod global;
pub(crate) mod global;
mod logs;
mod playlists;
mod podcasts;
mod popup;
#[cfg(test)]
mod status_tests;
pub mod theme;
use ratatui::Frame;
@@ -72,9 +76,14 @@ pub fn draw(frame: &mut Frame, state: &AppState, keymap: &Keymap) {
Tab::Queue => draw_queue(frame, main_area, state),
Tab::Federation => federation::draw(frame, main_area, state),
Tab::Logs => logs::draw(frame, main_area, state),
Tab::Podcasts => podcasts::draw(frame, main_area, state),
}
draw_status(frame, status_area, state);
if state.additional_settings_open {
additional_settings::draw(frame, state);
}
if state.help_visible {
draw_help(frame, keymap, state);
}
@@ -133,13 +142,22 @@ fn is_waiting_message(message: &str) -> bool {
}
fn draw_tabs(frame: &mut Frame, area: Rect, state: &AppState) {
let titles = Tab::ALL
.iter()
.map(|tab| format!(" {} {} ", tab.index() + 1, tab.title()));
// Titles carry their own spacing. When all of them do not fit, inactive
// tabs collapse to their number so the active one is never cut off.
let full: usize = Tab::ALL.iter().map(|tab| tab.title().len() + 4).sum();
let compact = full > usize::from(area.width);
let titles = Tab::ALL.iter().map(|tab| {
if compact && *tab != state.active_tab {
format!(" {} ", tab.index() + 1)
} else {
format!(" {} {} ", tab.index() + 1, tab.title())
}
});
let tabs = Tabs::new(titles)
.select(state.active_tab.index())
.style(theme::dim())
.highlight_style(theme::tab_active_for(state))
.padding("", "")
.divider("");
frame.render_widget(tabs, area);
}
@@ -329,7 +347,7 @@ fn format_secs(secs: f64) -> String {
/// Playback time, progress bar, queue position, volume and mode flags.
/// Wider consoles get a longer bar and full flags; narrow ones drop pieces.
fn player_right_line(state: &AppState, width: u16) -> Line<'static> {
fn player_right_line(state: &AppState, width: u16) -> (Line<'static>, u16) {
let player = &state.player;
let mut spans: Vec<Span<'static>> = Vec::new();
if let Some(track) = &player.current
@@ -359,6 +377,7 @@ fn player_right_line(state: &AppState, width: u16) -> Line<'static> {
));
}
}
let volume_offset = spans.iter().map(Span::width).sum::<usize>() + 2;
if width >= 80 {
let volume_cells = usize::from(player.volume / 10);
spans.extend([
@@ -392,29 +411,99 @@ fn player_right_line(state: &AppState, width: u16) -> Line<'static> {
format!(" {} ", state.device_playback.role.label()),
theme::role_pill(state.device_playback.role),
));
spans.push(Span::styled(
format!(" · online {}", state.device_playback.online_devices.max(1)),
theme::dim(),
));
if width >= 160 {
spans.push(Span::styled(
format!(" · online {}", state.device_playback.online_devices.max(1)),
theme::dim(),
));
}
}
// Keep a gap between the flags and the username block to the right.
spans.push(Span::raw(" "));
Line::from(spans)
(
Line::from(spans),
volume_offset.min(usize::from(u16::MAX)) as u16,
)
}
/// Reserve the source-mode badge first so playback details cannot clip it.
fn player_row_areas(row: Rect, state: &AppState) -> [Rect; 3] {
let badge_width = (format!(" federation: {} ", state.global.filters.source_mode.label()).len()
as u16)
.min(row.width);
let content = Rect::new(
row.x,
row.y,
row.width.saturating_sub(badge_width),
row.height,
);
let badge = Rect::new(content.right(), row.y, badge_width, row.height);
let (line, _) = player_right_line(state, content.width);
let right_width = line.width().min(usize::from(content.width)) as u16;
let [title, right] = Layout::horizontal([
Constraint::Min(8.min(content.width)),
Constraint::Length(right_width),
])
.areas(content);
[title, right, badge]
}
/// Shared geometry for rendering and background visualization requests.
fn footer_areas(area: Rect, state: &AppState) -> [Rect; 3] {
let [player_row, message_row] =
Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area);
let [_, right, badge] = player_row_areas(player_row, state);
let (_, volume_offset) = player_right_line(state, area.width.saturating_sub(badge.width));
let version_width = (env!("CARGO_PKG_VERSION").len() as u16 + 1).min(message_row.width);
let version = Rect::new(
message_row.x + message_row.width.saturating_sub(version_width) / 2,
message_row.y,
version_width,
message_row.height,
);
let message = Rect::new(
message_row.x,
message_row.y,
version.x.saturating_sub(message_row.x).saturating_sub(1),
message_row.height,
);
// On wide terminals align exactly with "vol". On narrow ones keep the
// centered version readable, shrinking the histogram into the remainder.
let start = right
.x
.saturating_add(volume_offset)
.max(version.right().saturating_add(1))
.min(message_row.right());
let visual = Rect::new(
start,
message_row.y,
message_row.right().saturating_sub(start),
message_row.height,
);
[message, version, visual]
}
pub(crate) fn status_visualizer_area(screen: Rect, state: &AppState) -> Rect {
let [_, _, status] = Layout::vertical([
Constraint::Length(1),
Constraint::Min(0),
Constraint::Length(2),
])
.areas(screen);
footer_areas(status, state)[2]
}
fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
let [player_row, message_row] =
let [player_row, _] =
Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area);
let [message_row, version_area, visual_area] = footer_areas(area, state);
let player = &state.player;
// Layout: track title left, time/progress/flags on the right. The
// right block is built first and gets a fixed width; the title
// truncates into whatever is left.
let center = player_right_line(state, area.width);
let center_width = (center.width() as u16).min(area.width);
let [title_area, right_area] =
Layout::horizontal([Constraint::Min(8), Constraint::Length(center_width)])
.areas(player_row);
let [title_area, right_area, mode_area] = player_row_areas(player_row, state);
let (center, _) = player_right_line(state, area.width.saturating_sub(mode_area.width));
let mut spans = Vec::new();
match &player.current {
@@ -439,6 +528,16 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
}
frame.render_widget(Paragraph::new(Line::from(spans)), title_area);
frame.render_widget(Paragraph::new(center), right_area);
let mode = state.global.filters.source_mode;
frame.render_widget(
Paragraph::new(format!(" federation: {} ", mode.label()))
.style(theme::source_mode_pill(mode)),
mode_area,
);
draw_version(frame, version_area);
if crate::visualizer::status::visible(state) {
state.status_visualizer.draw(frame, visual_area);
}
if state.cmdline.active {
// Vim-style command line takes over the message row.
@@ -448,7 +547,6 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
usize::from(message_row.width.saturating_sub(2)),
));
frame.render_widget(Paragraph::new(Line::from(spans)), message_row);
draw_version(frame, message_row);
return;
}
@@ -470,7 +568,6 @@ fn draw_status(frame: &mut Frame, area: Rect, state: &AppState) {
},
};
frame.render_widget(Paragraph::new(message), message_row);
draw_version(frame, message_row);
if let Some(pending) = &state.pending_keys {
let pending = Paragraph::new(Line::styled(
@@ -489,7 +586,7 @@ fn active_artist_peer_search(state: &AppState) -> Option<String> {
let Some(GlobalView::Artist { id, .. }) = state.global.stack.last() else {
return None;
};
if !matches!(state.artist_fed_views.get(id), Some(Loadable::Loading)) {
if !state.artist_federation_loading(*id) {
return None;
}
let name = match state.artist_views.get(id) {
@@ -502,7 +599,7 @@ fn active_artist_peer_search(state: &AppState) -> Option<String> {
fn draw_version(frame: &mut Frame, area: Rect) {
let version = format!("v{}", env!("CARGO_PKG_VERSION"));
frame.render_widget(
Paragraph::new(Line::styled(version, theme::dim())).alignment(Alignment::Right),
Paragraph::new(Line::styled(version, theme::dim())).alignment(Alignment::Center),
area,
);
}
+493
View File
@@ -0,0 +1,493 @@
//! Podcasts tab: subscriptions with their cover, and the episode catalog of
//! one podcast with title search, date sort and the selected episode's notes.
use ratatui::Frame;
use ratatui::layout::{Alignment, Constraint, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph, Wrap};
use unicode_width::UnicodeWidthStr;
use super::{loading_line, theme};
use crate::app::state::{
ART_HEADER_HEIGHT, ART_HEADER_WIDTH, AppState, ArtState, Loadable, PodcastListRow,
};
use crate::podcasts::{Episode, Podcast};
/// Width below which the side panel with the cover is dropped.
const SIDE_PANEL_MIN_TOTAL_WIDTH: u16 = 72;
const SIDE_PANEL_WIDTH: u16 = ART_HEADER_WIDTH + 2;
pub fn draw(frame: &mut Frame, area: Rect, state: &AppState) {
match state.podcasts.opened {
Some(opened) => draw_episodes(frame, area, state, opened.id, opened.cursor),
None => draw_list(frame, area, state),
}
}
fn bordered(frame: &mut Frame, area: Rect, state: &AppState, title: String) -> Rect {
let block = Block::bordered()
.title(title)
.title_style(theme::header_for(state))
.border_style(theme::border_for(state));
let inner = block.inner(area);
frame.render_widget(block, area);
inner
}
fn centered_line(frame: &mut Frame, area: Rect, line: Line) {
if area.height == 0 {
return;
}
let middle = Rect {
y: area.y + area.height / 2,
height: 1,
..area
};
frame.render_widget(Paragraph::new(line).alignment(Alignment::Center), middle);
}
fn error_style() -> Style {
Style::new().fg(Color::Red)
}
fn draw_list(frame: &mut Frame, area: Rect, state: &AppState) {
let tab = &state.podcasts;
let inner = bordered(frame, area, state, " Podcasts ".to_string());
match &tab.list {
Some(Loadable::Failed(error)) => {
return centered_line(frame, inner, Line::styled(error.clone(), error_style()));
}
None | Some(Loadable::Loading) => {
return centered_line(frame, inner, loading_line(state, "loading podcasts…"));
}
Some(Loadable::Ready(_)) => {}
}
let (list_area, side_area) = split_side_panel(inner);
if let Some(side) = side_area {
draw_side_panel(frame, side, state, tab.current_podcast());
}
let rows = tab.list_rows();
let visible = usize::from(list_area.height.max(1));
let first = tab
.selected
.saturating_sub(visible / 2)
.min(rows.len().saturating_sub(visible));
for (index, row) in rows.iter().enumerate().skip(first).take(visible) {
let area = Rect {
x: list_area.x,
y: list_area.y + (index - first) as u16,
width: list_area.width,
height: 1,
};
let (left, right) = list_row_lines(state, *row, list_area.width);
frame.render_widget(Paragraph::new(left), area);
if let Some(right) = right {
frame.render_widget(Paragraph::new(right).alignment(Alignment::Right), area);
}
if index == tab.selected {
frame
.buffer_mut()
.set_style(area, theme::tab_active_for(state));
}
}
if tab.podcasts().is_empty() {
let hint_y = list_area.y + rows.len() as u16 + 1;
if hint_y < list_area.y + list_area.height {
frame.render_widget(
Paragraph::new(Line::styled(
"No subscriptions yet. Choose \"Add podcast\" and paste an RSS feed URL.",
theme::dim(),
))
.wrap(Wrap { trim: true }),
Rect {
y: hint_y,
height: list_area.y + list_area.height - hint_y,
..list_area
},
);
}
}
}
fn list_row_lines(
state: &AppState,
row: PodcastListRow,
width: u16,
) -> (Line<'static>, Option<Line<'static>>) {
let tab = &state.podcasts;
match row {
PodcastListRow::Add => {
let label = if tab.subscribing {
format!("{} Adding podcast…", state.spinner())
} else {
"+ Add podcast (RSS feed URL)…".to_string()
};
(Line::styled(label, theme::accent_for(state)), None)
}
PodcastListRow::RefreshAll => {
let busy = !tab.refreshing.is_empty();
let label = if busy {
format!(
"{} Checking {} feed(s) for new episodes…",
state.spinner(),
tab.refreshing.len()
)
} else {
"↻ Check all podcasts for new episodes".to_string()
};
(Line::styled(label, theme::accent_for(state)), None)
}
PodcastListRow::Podcast(index) => {
let Some(podcast) = tab.podcasts().get(index) else {
return (Line::raw(""), None);
};
let marker = if tab.refreshing.contains(&podcast.id) {
Span::styled(format!("{} ", state.spinner()), theme::accent_for(state))
} else if podcast.last_error.is_some() {
Span::styled("! ", error_style())
} else {
Span::raw(" ")
};
let mut meta = format!("{} ep", podcast.episode_count);
if let Some(latest) = podcast.latest_published_at {
meta.push_str(&format!(" · {}", format_date(latest)));
}
meta.push(' ');
// Marker (2), the title, one gap column, then the meta.
let title_width =
usize::from(width).saturating_sub(2 + UnicodeWidthStr::width(meta.as_str()) + 1);
let left = Line::from(vec![
marker,
Span::raw(fit_width(&podcast.title, title_width)),
]);
(left, Some(Line::styled(meta, theme::dim())))
}
}
}
/// Main area plus an optional right-hand panel for the cover and details.
fn split_side_panel(area: Rect) -> (Rect, Option<Rect>) {
if area.width < SIDE_PANEL_MIN_TOTAL_WIDTH {
return (area, None);
}
let [main, side] =
Layout::horizontal([Constraint::Min(0), Constraint::Length(SIDE_PANEL_WIDTH)]).areas(area);
(main, Some(side))
}
fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Option<&Podcast>) {
let block = Block::default()
.borders(Borders::LEFT)
.border_style(theme::border_for(state));
let inner = block.inner(area);
frame.render_widget(block, area);
let Some(podcast) = podcast else {
return;
};
let inner = Rect {
x: inner.x + 1,
width: inner.width.saturating_sub(1),
..inner
};
let art_height = ART_HEADER_HEIGHT.min(inner.height);
draw_cover(
frame,
Rect {
height: art_height,
width: ART_HEADER_WIDTH.min(inner.width),
..inner
},
state,
podcast,
);
let text_area = Rect {
y: inner.y + art_height + 1,
height: inner.height.saturating_sub(art_height + 1),
..inner
};
if text_area.height == 0 {
return;
}
let mut lines = vec![Line::styled(
podcast.title.clone(),
Style::new().add_modifier(Modifier::BOLD),
)];
if let Some(author) = &podcast.author {
lines.push(Line::styled(author.clone(), theme::dim()));
}
lines.push(Line::styled(
format!("{} episodes", podcast.episode_count),
theme::dim(),
));
lines.extend(check_status_line(state, podcast));
if let Some(description) = &podcast.description {
lines.push(Line::raw(""));
lines.push(Line::raw(description.clone()));
}
frame.render_widget(Paragraph::new(lines).wrap(Wrap { trim: true }), text_area);
}
fn check_status_line(state: &AppState, podcast: &Podcast) -> Option<Line<'static>> {
if state.podcasts.refreshing.contains(&podcast.id) {
return Some(loading_line(state, "checking for new episodes…"));
}
if let Some(error) = &podcast.last_error {
return Some(Line::styled(
format!("last check failed: {error}"),
error_style(),
));
}
podcast
.last_checked_at
.map(|at| Line::styled(format!("checked {}", format_date(at)), theme::dim()))
}
fn draw_cover(frame: &mut Frame, area: Rect, state: &AppState, podcast: &Podcast) {
let art = podcast.cover_path.as_ref().and_then(|path| {
state.art.get(&crate::art::cache_key(
path,
ART_HEADER_WIDTH,
ART_HEADER_HEIGHT,
))
});
match art {
Some(ArtState::Ready(image)) => {
frame.render_widget(Paragraph::new(super::art::to_text(image)), area);
}
Some(ArtState::Loading) => centered_line(frame, area, Line::styled("…", theme::dim())),
_ => centered_line(frame, area, Line::styled("🎙", theme::dim())),
}
}
fn draw_episodes(frame: &mut Frame, area: Rect, state: &AppState, id: i64, cursor: usize) {
let tab = &state.podcasts;
let podcast = tab.podcast(id);
let title = podcast.map_or("…".to_string(), |podcast| podcast.title.clone());
let mut header = format!(" Podcasts ▸ {title} · {} ", tab.sort.label());
if !tab.filter.is_empty() {
header.push_str(&format!("· search: \"{}\" ", tab.filter));
}
let inner = bordered(frame, area, state, header);
let episodes = match &tab.episodes {
Some((loaded, Loadable::Ready(episodes))) if *loaded == id => episodes,
Some((loaded, Loadable::Failed(error))) if *loaded == id => {
return centered_line(frame, inner, Line::styled(error.clone(), error_style()));
}
_ => return centered_line(frame, inner, loading_line(state, "loading episodes…")),
};
let visible_indices = tab.visible_episode_indices();
let (main, side) = split_side_panel(inner);
if let Some(side) = side {
draw_side_panel(frame, side, state, podcast);
}
// Episode notes below the list when there is room for them.
let notes_height = if main.height >= 16 { 6 } else { 0 };
let [list_area, notes_area] =
Layout::vertical([Constraint::Min(1), Constraint::Length(notes_height)]).areas(main);
let [summary_area, list_area] =
Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).areas(list_area);
let summary = if tab.filter.is_empty() {
format!("{} episodes", episodes.len())
} else {
format!(
"{} of {} episodes match \"{}\"",
visible_indices.len(),
episodes.len(),
tab.filter
)
};
let mut summary_spans = vec![Span::styled(summary, theme::dim())];
if tab.refreshing.contains(&id) {
summary_spans.push(Span::raw(" "));
summary_spans.extend(loading_line(state, "checking for new episodes…").spans);
}
frame.render_widget(Paragraph::new(Line::from(summary_spans)), summary_area);
if visible_indices.is_empty() {
let message = if episodes.is_empty() {
"this feed has no playable episodes"
} else {
"no episode title matches the search"
};
return centered_line(frame, list_area, Line::styled(message, theme::dim()));
}
let cursor = cursor.min(visible_indices.len() - 1);
let playing_id = state
.player
.current
.as_ref()
.and_then(|track| crate::podcasts::episode_id_from_track_id(track.id));
let visible = usize::from(list_area.height.max(1));
let first = cursor
.saturating_sub(visible / 2)
.min(visible_indices.len().saturating_sub(visible));
for (row_index, &episode_index) in visible_indices.iter().enumerate().skip(first).take(visible)
{
let episode = &episodes[episode_index];
let area = Rect {
x: list_area.x,
y: list_area.y + (row_index - first) as u16,
width: list_area.width,
height: 1,
};
draw_episode_row(
frame,
area,
state,
episode,
playing_id == Some(episode.id),
row_index == cursor,
);
}
if notes_height > 0
&& let Some(episode) = visible_indices
.get(cursor)
.and_then(|&index| episodes.get(index))
{
draw_notes(frame, notes_area, state, episode);
}
}
fn draw_episode_row(
frame: &mut Frame,
area: Rect,
state: &AppState,
episode: &Episode,
playing: bool,
selected: bool,
) {
let date = episode
.published_at
.map(format_date)
.unwrap_or_else(|| " ".to_string());
let marker = if playing {
Span::styled("▶ ", theme::accent_for(state))
} else {
Span::raw(" ")
};
let duration = episode
.duration_seconds
.map(format_duration)
.unwrap_or_default();
let title_width = usize::from(area.width)
.saturating_sub(2 + 10 + 2 + UnicodeWidthStr::width(duration.as_str()) + 2);
let line = Line::from(vec![
marker,
Span::styled(date, theme::dim()),
Span::raw(" "),
Span::raw(fit_width(&episode.title, title_width)),
]);
frame.render_widget(Paragraph::new(line), area);
if !duration.is_empty() {
frame.render_widget(
Paragraph::new(Line::styled(format!("{duration} "), theme::dim()))
.alignment(Alignment::Right),
area,
);
}
if selected {
frame
.buffer_mut()
.set_style(area, theme::tab_active_for(state));
}
}
fn draw_notes(frame: &mut Frame, area: Rect, state: &AppState, episode: &Episode) {
let block = Block::default()
.borders(Borders::TOP)
.border_style(theme::border_for(state))
.title(Span::styled(" Episode notes ", theme::dim()));
let inner = block.inner(area);
frame.render_widget(block, area);
let text = episode
.description
.clone()
.unwrap_or_else(|| "No description in the feed.".to_string());
frame.render_widget(
Paragraph::new(Line::raw(text)).wrap(Wrap { trim: true }),
inner,
);
}
/// Truncate to a display width, adding an ellipsis when cut.
fn fit_width(text: &str, width: usize) -> String {
if UnicodeWidthStr::width(text) <= width {
return text.to_string();
}
if width == 0 {
return String::new();
}
let mut out = String::new();
let mut used = 0;
for ch in text.chars() {
let w = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);
if used + w + 1 > width {
break;
}
out.push(ch);
used += w;
}
out.push('…');
out
}
fn format_duration(seconds: f64) -> String {
let total = seconds.max(0.0).round() as u64;
let (hours, minutes, secs) = (total / 3600, total / 60 % 60, total % 60);
if hours > 0 {
format!("{hours}:{minutes:02}:{secs:02}")
} else {
format!("{minutes}:{secs:02}")
}
}
/// `YYYY-MM-DD` (UTC) for unix seconds, without a date library: Howard
/// Hinnant's days-to-civil algorithm.
fn format_date(unix_seconds: i64) -> String {
let days = unix_seconds.div_euclid(86_400);
let z = days + 719_468;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097);
let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = doy - (153 * mp + 2) / 5 + 1;
let month = if mp < 10 { mp + 3 } else { mp - 9 };
let year = yoe + era * 400 + i64::from(month <= 2);
format!("{year:04}-{month:02}-{day:02}")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn formats_dates_in_utc() {
assert_eq!(format_date(0), "1970-01-01");
assert_eq!(format_date(951_782_400), "2000-02-29");
assert_eq!(format_date(1_718_409_600), "2024-06-15");
assert_eq!(format_date(-86_400), "1969-12-31");
}
#[test]
fn formats_durations() {
assert_eq!(format_duration(59.6), "1:00");
assert_eq!(format_duration(754.0), "12:34");
assert_eq!(format_duration(3_723.0), "1:02:03");
}
#[test]
fn fits_display_width_with_ellipsis() {
assert_eq!(fit_width("Выпуск 650", 20), "Выпуск 650");
assert_eq!(fit_width("Выпуск 650", 5), "Выпу…");
assert_eq!(fit_width("abc", 0), "");
}
}
+31 -9
View File
@@ -24,7 +24,9 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
error,
..
}) => draw_edit(frame, state, title, fields, *focus, error.as_deref()),
Some(Popup::ConfirmDelete { label, .. }) => draw_confirm_delete(frame, state, label),
Some(Popup::ConfirmDelete { target, label }) => {
draw_confirm_delete(frame, state, *target, label)
}
Some(Popup::SimilarityPrivacyConsent { .. }) => {
draw_similarity_privacy_consent(frame, state)
}
@@ -43,7 +45,12 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
}) => draw_track_artists(frame, state, tracks, *cursor, *selected),
Some(Popup::LogDetail(entry)) => draw_log_detail(frame, state, entry),
Some(Popup::FedInput { field, input }) => {
draw_fed_input(frame, state, field.title(), field.help(), input)
let keys = if *field == crate::app::state::FedInputField::PodcastFilter {
"enter: keep · esc: clear"
} else {
"enter: apply · esc: cancel"
};
draw_fed_input(frame, state, field.title(), field.help(), keys, input)
}
Some(Popup::FedText { title, text }) => draw_fed_text(frame, state, title, text),
Some(Popup::FedCopyText {
@@ -891,6 +898,7 @@ fn draw_fed_input(
state: &AppState,
title: &str,
help: &str,
keys: &str,
input: &crate::app::input::LineEdit,
) {
let area = centered(frame.area(), 72, 8);
@@ -916,8 +924,7 @@ fn draw_fed_input(
let spans = super::line_edit_spans(input, usize::from(entry_area.width.saturating_sub(1)));
frame.render_widget(Paragraph::new(Line::from(spans)), entry_area);
frame.render_widget(
Paragraph::new(Line::styled("enter: apply · esc: cancel", theme::dim()))
.alignment(Alignment::Center),
Paragraph::new(Line::styled(keys.to_string(), theme::dim())).alignment(Alignment::Center),
hint_area,
);
}
@@ -1241,11 +1248,23 @@ fn draw_edit(
frame.render_widget(Paragraph::new(hint).alignment(Alignment::Center), footer);
}
fn draw_confirm_delete(frame: &mut Frame, state: &AppState, label: &str) {
fn draw_confirm_delete(
frame: &mut Frame,
state: &AppState,
target: crate::app::state::DeleteTarget,
label: &str,
) {
// Unsubscribing only drops the feed's stored metadata, not music.
let (title, verb, key) = match target {
crate::app::state::DeleteTarget::Podcast(_) => {
(" Unsubscribe? ", "Unsubscribe from", "unsubscribe")
}
_ => (" Delete? ", "Delete", "delete"),
};
let width = 64.min(frame.area().width.saturating_sub(4)).max(30);
let area = centered(frame.area(), width, 7);
let block = Block::bordered()
.title(" Delete? ")
.title(title)
.title_style(theme::header_for(state))
.border_style(theme::strong_border_for(state));
let inner = block.inner(area);
@@ -1254,14 +1273,17 @@ fn draw_confirm_delete(frame: &mut Frame, state: &AppState, label: &str) {
let [body, footer] = Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(inner);
frame.render_widget(
Paragraph::new(format!("Delete {label}?"))
Paragraph::new(format!("{verb} {label}?"))
.wrap(Wrap { trim: false })
.alignment(Alignment::Center),
body,
);
frame.render_widget(
Paragraph::new(Line::styled("enter/y delete · esc/n cancel", theme::dim()))
.alignment(Alignment::Center),
Paragraph::new(Line::styled(
format!("enter/y {key} · esc/n cancel"),
theme::dim(),
))
.alignment(Alignment::Center),
footer,
);
}
+142
View File
@@ -0,0 +1,142 @@
use super::*;
use ratatui::{Terminal, backend::TestBackend};
fn playing_state() -> AppState {
let mut state = AppState::default();
state.player.current = Some(crate::library::models::TrackItem {
id: 1,
title: "Песня 界".into(),
track_number: None,
disc_number: None,
duration_seconds: 240.0,
artists: vec![],
featured_artists: vec![],
release_id: 1,
release_title: "Release".into(),
release_year: None,
cover_path: None,
file_path: "/unused/test.flac".into(),
content_id: None,
audio_format: None,
audio_bitrate: None,
audio_sample_rate: None,
audio_bit_depth: None,
file_size_bytes: None,
play_count: 0,
fed: None,
});
state.player.playing = true;
state.player.position_secs = 12.0;
state
}
#[test]
fn footer_centers_version_and_aligns_visualization_under_volume() {
let mut state = playing_state();
for width in [160, 200] {
for shuffle in [false, true] {
state.player.shuffle = shuffle;
let mut terminal = Terminal::new(TestBackend::new(width, 2)).unwrap();
terminal
.draw(|frame| draw_status(frame, frame.area(), &state))
.unwrap();
let buffer = terminal.backend().buffer();
let version = format!("v{}", env!("CARGO_PKG_VERSION"));
// Count display cells, not UTF-8 bytes: the title is non-ASCII.
let volume_x = (0..width)
.find(|x| {
buffer[(*x, 0)].symbol() == "v"
&& *x + 2 < width
&& buffer[(*x + 1, 0)].symbol() == "o"
&& buffer[(*x + 2, 0)].symbol() == "l"
})
.unwrap();
let areas = footer_areas(Rect::new(0, 0, width, 2), &state);
assert_eq!(areas[2].x, volume_x);
assert_eq!(areas[2].y, 1);
assert_eq!(areas[2].right(), width);
let version_x = (width - version.len() as u16) / 2;
for (offset, ch) in version.chars().enumerate() {
assert_eq!(
buffer[(version_x + offset as u16, 1)].symbol(),
ch.to_string()
);
}
assert_eq!(
status_visualizer_area(Rect::new(0, 0, width, 24), &state).x,
volume_x
);
}
}
}
#[test]
fn footer_regions_do_not_overlap_on_narrow_terminals_or_command_input() {
let mut state = playing_state();
state.status_message = Some("long status message ".repeat(20));
for width in [1, 5, 8, 20, 40, 80, 120, 200] {
let areas = footer_areas(Rect::new(0, 0, width, 2), &state);
assert!(areas[0].right() <= areas[1].x);
assert!(areas[1].right() <= areas[2].x);
assert!(areas[2].right() <= width);
for command in [false, true] {
state.cmdline.active = command;
let mut terminal = Terminal::new(TestBackend::new(width, 2)).unwrap();
terminal
.draw(|frame| draw_status(frame, frame.area(), &state))
.unwrap();
// Neither messages nor the command prompt may paint the graph.
for x in areas[2].x..areas[2].right() {
assert_eq!(terminal.backend().buffer()[(x, 1)].symbol(), " ");
}
}
}
}
#[test]
fn status_animation_requires_local_unpaused_playback() {
let mut state = playing_state();
assert!(crate::visualizer::status::playing(&state));
state.player.paused = true;
assert!(!crate::visualizer::status::playing(&state));
assert!(crate::visualizer::status::visible(&state));
state.player.paused = false;
state.player.playing = false;
assert!(!crate::visualizer::status::playing(&state));
assert!(!crate::visualizer::status::visible(&state));
state.player.playing = true;
state.device_playback.role = crate::app::state::DevicePlaybackRole::Control;
assert!(!crate::visualizer::status::playing(&state));
}
#[test]
fn federation_mode_badge_stays_visible_at_the_right_edge() {
use crate::config::settings::LibrarySourceMode;
use ratatui::style::Color;
let mut state = playing_state();
for (mode, background) in [
(LibrarySourceMode::Local, Color::Yellow),
(LibrarySourceMode::My, Color::Cyan),
(LibrarySourceMode::Global, Color::Green),
] {
state.global.filters.source_mode = mode;
for width in [20, 40, 80, 160, 200] {
for playing in [false, true] {
state.player.playing = playing;
state.cmdline.active = !playing;
let mut terminal = Terminal::new(TestBackend::new(width, 2)).unwrap();
terminal
.draw(|frame| draw_status(frame, frame.area(), &state))
.unwrap();
let label = format!(" federation: {} ", mode.label());
let start = width - label.len() as u16;
for (offset, ch) in label.chars().enumerate() {
let cell = &terminal.backend().buffer()[(start + offset as u16, 0)];
assert_eq!(cell.symbol(), ch.to_string());
assert_eq!(cell.bg, background);
assert_eq!(cell.fg, Color::Black);
}
}
}
}
}
+13
View File
@@ -73,6 +73,19 @@ pub fn role_pill(role: DevicePlaybackRole) -> Style {
.add_modifier(Modifier::BOLD)
}
pub fn source_mode_pill(mode: crate::config::settings::LibrarySourceMode) -> Style {
use crate::config::settings::LibrarySourceMode;
let bg = match mode {
LibrarySourceMode::Local => Color::Yellow,
LibrarySourceMode::My => Color::Cyan,
LibrarySourceMode::Global => Color::Green,
};
Style::new()
.fg(Color::Black)
.bg(bg)
.add_modifier(Modifier::BOLD)
}
fn accent_color_for(state: &AppState) -> Color {
match state.device_playback.role {
DevicePlaybackRole::Jam => JAM_ACCENT,
+432
View File
@@ -0,0 +1,432 @@
//! Manual portable-binary updates. All network/disk work runs off the UI thread.
use std::{
fs::File,
io::{Read, Write},
path::Path,
time::Duration,
};
use anyhow::{Context, Result, bail, ensure};
use object::Object;
use reqwest::blocking::Client;
use semver::Version;
use serde::Deserialize;
use sha2::{Digest, Sha256};
const REPOSITORY: &str = "https://api.github.com/repos/house-of-vanity/furumi_tui";
const MAX_ARCHIVE: u64 = 512 * 1024 * 1024;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Update {
pub version: String,
asset: Asset,
checksums: Asset,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
struct Asset {
name: String,
browser_download_url: String,
}
#[derive(Deserialize)]
struct Release {
tag_name: String,
draft: bool,
prerelease: bool,
assets: Vec<Asset>,
}
#[derive(Debug, Default)]
pub struct State {
pub busy: bool,
pub installed: bool,
pub available: Option<Update>,
pub message: String,
}
fn client() -> Result<Client> {
// Reuse the ring backend already used by federation; respect an existing provider.
let _ = rustls::crypto::ring::default_provider().install_default();
Ok(Client::builder()
.user_agent(concat!("furumi/", env!("CARGO_PKG_VERSION")))
.https_only(true)
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(300))
.build()?)
}
fn read_limited(mut reader: impl Read, limit: u64) -> Result<Vec<u8>> {
let mut data = Vec::new();
reader.by_ref().take(limit + 1).read_to_end(&mut data)?;
ensure!(data.len() as u64 <= limit, "download exceeds size limit");
Ok(data)
}
fn select_release(release: Release, current: &str, os: &str, arch: &str) -> Result<Option<Update>> {
let version = release
.tag_name
.strip_prefix('v')
.unwrap_or(&release.tag_name);
let next = Version::parse(version).context("invalid release version")?;
if release.draft
|| release.prerelease
|| !next.pre.is_empty()
|| next <= Version::parse(current)?
{
return Ok(None);
}
let platform = match os {
"linux" => "linux",
"macos" => "macos",
"windows" => "windows",
_ => bail!("self-update is unsupported on {os}"),
};
let extension = if os == "windows" { "zip" } else { "tar.gz" };
let name = format!("furumi-{platform}-{arch}-{version}.{extension}");
let find = |name: &str| -> Result<Asset> {
let matches: Vec<_> = release
.assets
.iter()
.filter(|asset| asset.name == name)
.collect();
ensure!(matches.len() == 1, "release has no unique {name} asset");
Ok(matches[0].clone())
};
Ok(Some(Update {
version: version.to_owned(),
asset: find(&name)?,
checksums: find("SHA256SUMS")?,
}))
}
pub fn check() -> Result<Option<Update>> {
let response = client()?
.get(format!("{REPOSITORY}/releases/latest"))
.timeout(Duration::from_secs(20))
.send()?;
if response.status() == reqwest::StatusCode::NOT_FOUND {
return Ok(None);
}
let release = serde_json::from_slice(&read_limited(
response.error_for_status()?,
2 * 1024 * 1024,
)?)?;
select_release(
release,
env!("CARGO_PKG_VERSION"),
std::env::consts::OS,
std::env::consts::ARCH,
)
}
fn checksum(text: &str, name: &str) -> Result<String> {
let mut found = None;
for line in text.lines() {
let Some((hash, filename)) = line.split_once(' ') else {
continue;
};
if filename.trim_start().trim_start_matches('*') != name {
continue;
}
ensure!(found.is_none(), "duplicate checksum for {name}");
ensure!(
hash.len() == 64 && hash.bytes().all(|b| b.is_ascii_hexdigit()),
"invalid SHA-256 for {name}"
);
found = Some(hash.to_ascii_lowercase());
}
found.context("release does not contain a checksum for the selected archive")
}
fn extract(archive: &Path, name: &str, output: &mut File) -> Result<()> {
let binary = if cfg!(windows) {
"furumi.exe"
} else {
"furumi"
};
let root = name
.strip_suffix(".tar.gz")
.or_else(|| name.strip_suffix(".zip"))
.context("unsupported archive")?;
// Existing release archives omit the version in their inner directory.
let root = root.rsplit_once('-').context("invalid archive name")?.0;
let expected = format!("{root}/{binary}");
let mut count = 0;
if name.ends_with(".zip") {
let mut archive = zip::ZipArchive::new(File::open(archive)?)?;
for index in 0..archive.len() {
let mut entry = archive.by_index(index)?;
if entry.name() != expected {
continue;
}
ensure!(
entry.is_file() && !entry.is_symlink(),
"binary is not a regular file"
);
count += 1;
ensure!(count == 1, "duplicate binary in archive");
output.write_all(&read_limited(&mut entry, MAX_ARCHIVE)?)?;
}
} else {
let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(File::open(archive)?));
for entry in archive.entries()? {
let mut entry = entry?;
if entry.path()?.as_ref() != Path::new(&expected) {
continue;
}
ensure!(
entry.header().entry_type().is_file(),
"binary is not a regular file"
);
count += 1;
ensure!(count == 1, "duplicate binary in archive");
output.write_all(&read_limited(&mut entry, MAX_ARCHIVE)?)?;
}
}
ensure!(count == 1, "archive does not contain {expected}");
output.sync_all()?;
Ok(())
}
fn validate_binary(current: &[u8], candidate: &[u8]) -> Result<()> {
let current = object::File::parse(current).context("cannot inspect installed binary")?;
let candidate = object::File::parse(candidate).context("invalid downloaded binary")?;
ensure!(
candidate.kind() == object::ObjectKind::Executable
|| candidate.kind() == object::ObjectKind::Dynamic,
"download is not an executable"
);
ensure!(
candidate.format() == current.format()
&& candidate.architecture() == current.architecture()
&& candidate.is_64() == current.is_64()
&& candidate.is_little_endian() == current.is_little_endian(),
"downloaded binary has incompatible platform or architecture"
);
Ok(())
}
pub fn install(update: &Update, mut progress: impl FnMut(String)) -> Result<()> {
let exe = std::env::current_exe()?.canonicalize()?;
let parent = exe.parent().context("executable has no parent directory")?;
let lock = File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(parent.join(".furumi-update.lock"))
.context("cannot write to installation directory")?;
fs2::FileExt::try_lock_exclusive(&lock).context("another furumi instance is updating")?;
// Keep staging on the destination filesystem and never overwrite a running image.
let staging = tempfile::Builder::new()
.prefix(".furumi-update-")
.tempdir_in(parent)
.context("cannot create update staging directory")?;
let client = client()?;
let sums = read_limited(
client
.get(&update.checksums.browser_download_url)
.send()?
.error_for_status()?,
1024 * 1024,
)?;
let expected = checksum(std::str::from_utf8(&sums)?, &update.asset.name)?;
let mut response = client
.get(&update.asset.browser_download_url)
.send()?
.error_for_status()?;
let total = response.content_length();
ensure!(
total.is_none_or(|size| size <= MAX_ARCHIVE),
"archive exceeds size limit"
);
let archive = staging.path().join("download");
let mut file = File::create(&archive)?;
let mut hash = Sha256::new();
let mut buffer = [0u8; 64 * 1024];
let mut downloaded = 0u64;
let mut reported = u64::MAX;
loop {
let count = response.read(&mut buffer)?;
if count == 0 {
break;
}
downloaded += count as u64;
ensure!(downloaded <= MAX_ARCHIVE, "archive exceeds size limit");
file.write_all(&buffer[..count])?;
hash.update(&buffer[..count]);
let mb = downloaded / (1024 * 1024);
if mb != reported {
progress(format!("Downloading: {mb} MiB"));
reported = mb;
}
}
file.sync_all()?;
drop(file);
ensure!(
format!("{:x}", hash.finalize()) == expected,
"SHA-256 mismatch; update was not installed"
);
progress("Verifying binary...".into());
let binary = staging.path().join(if cfg!(windows) {
"furumi.exe"
} else {
"furumi"
});
let mut output = File::create(&binary)?;
extract(&archive, &update.asset.name, &mut output)?;
drop(output);
validate_binary(&std::fs::read(&exe)?, &std::fs::read(&binary)?)?;
std::fs::set_permissions(&binary, std::fs::metadata(&exe)?.permissions())?;
progress("Installing...".into());
self_replace::self_replace(&binary).context("could not replace executable")?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn https_client_can_be_constructed() {
client().unwrap();
}
#[test]
fn extracts_only_expected_binary_from_release_archives() {
let temp = tempfile::tempdir().unwrap();
let binary = if cfg!(windows) {
"furumi.exe"
} else {
"furumi"
};
let expected = format!("furumi-windows-x86_64/{binary}");
let payload = b"test executable";
let zip_path = temp.path().join("test.zip");
let mut zip = zip::ZipWriter::new(File::create(&zip_path).unwrap());
zip.start_file("README.md", zip::write::SimpleFileOptions::default())
.unwrap();
zip.write_all(b"readme").unwrap();
zip.start_file(&expected, zip::write::SimpleFileOptions::default())
.unwrap();
zip.write_all(payload).unwrap();
zip.finish().unwrap();
let output = temp.path().join("output");
extract(
&zip_path,
"furumi-windows-x86_64-0.1.6.zip",
&mut File::create(&output).unwrap(),
)
.unwrap();
assert_eq!(std::fs::read(&output).unwrap(), payload);
assert!(
extract(
&zip_path,
"furumi-linux-x86_64-0.1.6.zip",
&mut File::create(&output).unwrap()
)
.is_err()
);
let tar_path = temp.path().join("test.tar.gz");
let gzip = flate2::write::GzEncoder::new(
File::create(&tar_path).unwrap(),
flate2::Compression::default(),
);
let mut tar = tar::Builder::new(gzip);
let mut header = tar::Header::new_gnu();
header.set_size(payload.len() as u64);
header.set_mode(0o755);
header.set_cksum();
tar.append_data(&mut header, &expected, payload.as_slice())
.unwrap();
tar.into_inner().unwrap().finish().unwrap();
extract(
&tar_path,
"furumi-windows-x86_64-0.1.6.tar.gz",
&mut File::create(&output).unwrap(),
)
.unwrap();
assert_eq!(std::fs::read(&output).unwrap(), payload);
}
#[test]
fn binary_validation_rejects_wrong_architecture() {
let current = std::fs::read(std::env::current_exe().unwrap()).unwrap();
let mut other = current.clone();
// Change only the machine field, preserving an otherwise valid executable.
if current.starts_with(b"MZ") {
let pe = u32::from_le_bytes(current[0x3c..0x40].try_into().unwrap()) as usize;
let machine: u16 = if current[pe + 4..pe + 6] == [0x64, 0x86] {
0xaa64
} else {
0x8664
};
other[pe + 4..pe + 6].copy_from_slice(&machine.to_le_bytes());
} else if current.starts_with(b"\x7fELF") {
let machine: u16 = if current[18..20] == [62, 0] { 183 } else { 62 };
other[18..20].copy_from_slice(&machine.to_le_bytes());
} else if current.starts_with(&[0xcf, 0xfa, 0xed, 0xfe]) {
let cpu: u32 = if current[4] == 7 {
0x0100000c
} else {
0x01000007
};
other[4..8].copy_from_slice(&cpu.to_le_bytes());
} else {
panic!("unsupported test executable format");
}
assert!(validate_binary(&current, &other).is_err());
}
#[test]
fn checksums_require_exact_unique_valid_entry() {
let hash = "ab".repeat(32);
assert_eq!(
checksum(&format!("{hash} app.zip\n"), "app.zip").unwrap(),
hash
);
assert!(checksum(&format!("{hash} other.zip"), "app.zip").is_err());
assert!(checksum("broken app.zip", "app.zip").is_err());
assert!(checksum(&format!("{hash} app.zip\n{hash} app.zip"), "app.zip").is_err());
}
#[test]
fn release_selection_respects_semver_and_assets() {
let release = |version: &str| Release {
tag_name: version.into(),
draft: false,
prerelease: false,
assets: vec![
Asset {
name: "furumi-linux-x86_64-0.1.10.tar.gz".into(),
browser_download_url: String::new(),
},
Asset {
name: "SHA256SUMS".into(),
browser_download_url: String::new(),
},
],
};
assert!(
select_release(release("v0.1.10"), "0.1.9", "linux", "x86_64")
.unwrap()
.is_some()
);
assert!(
select_release(release("v0.1.8"), "0.1.9", "linux", "x86_64")
.unwrap()
.is_none()
);
assert!(
select_release(release("v0.2.0-beta.1"), "0.1.9", "linux", "x86_64")
.unwrap()
.is_none()
);
assert!(select_release(release("v0.1.10"), "0.1.9", "linux", "aarch64").is_err());
}
#[test]
fn binary_validation_rejects_corrupt_download() {
let current = std::fs::read(std::env::current_exe().unwrap()).unwrap();
validate_binary(&current, &current).unwrap();
assert!(validate_binary(&current, b"not a binary").is_err());
}
}
+279
View File
@@ -0,0 +1,279 @@
// name: Morphing cube
// protocol: furumi-visualizer-v2
// bundle-version: 6
//
// Audio-reactive neon wireframe, echo contours, core, shock rings, sparks
// and a bounded particle field. No native 3D API or retained state required.
// Echoes sample earlier rotation times with CURRENT audio; they are analytic
// trails, not stored frames. Particle lifetimes use time, rings also expand
// as the smoothed beat envelope decays. No beat history is available in v2.
//
// Fixed budgets: 8 mesh vertices, at most 48 particles + 24 short tails,
// 20 background stars, 3 rings, 2 echoes. Edges/rings use bulk trace commands.
// Smaller terminals omit echoes/rings and reduce particle counts.
fn clamp(v, lo, hi) {
if v < lo { lo } else if v > hi { hi } else { v }
}
fn min(a, b) { if a < b { a } else { b } }
fn rgb(r, g, b) {
to_int(clamp(r, 0.0, 255.0)) * 65536
+ to_int(clamp(g, 0.0, 255.0)) * 256
+ to_int(clamp(b, 0.0, 255.0))
}
// trace runs left-to-right. Swap endpoints for reversed edges and use
// vline for vertical ones (including an edge projected to a single cell).
fn edge(ax, ay, bx, by, fg, bg) {
if ax > bx {
let tx = ax; ax = bx; bx = tx;
let ty = ay; ay = by; by = ty;
}
if ax == bx {
return #{ op: "vline", x: ax, y: min(ay, by),
h: abs(to_float(by - ay)).to_int() + 1, ch: "|", fg: fg, bg: bg };
}
let dx = bx - ax;
let dy = by - ay;
let ch = if abs(to_float(dy)) * 3.0 < to_float(dx) { "-" }
else if abs(to_float(dy)) > to_float(dx) { "|" }
else if dy < 0 { "/" } else { "\\" };
let ys = [];
let slope = to_float(dy) / to_float(dx);
for x in 0..=dx {
ys.push(to_int(to_float(ay) + to_float(x) * slope + 0.5));
}
#{ op: "trace", x: ax, ys: ys, ch: ch, line_ch: ch,
fg: fg, line_fg: fg, bg: bg }
}
// Lift quieter passages without a hard gain/clipping threshold.
fn response(level) { 1.0 - pow(1.0 - clamp(level, 0.0, 1.0), 1.6) }
fn palette(t, heat, light) {
rgb((95.0 + 125.0 * heat + 35.0 * sin(t * 0.43)) * light,
(185.0 - 95.0 * heat + 55.0 * sin(t * 0.31 + 1.3)) * light,
255.0 * light)
}
// A single projected mesh, fitted uniformly: never clamp individual corners.
fn mesh(t, energy, bass, mid, treble, beat, width, height) {
let pulse = 0.70 + energy * 0.45 + beat * 0.85;
let sx = pulse * (1.0 + bass * 0.55);
let sy = pulse * (1.0 - bass * 0.42 + mid * 0.15);
let sz = pulse * (1.0 + treble * 0.22);
// A bounded beat kick, not time * instantaneous_energy (which jumps).
let ax = t * 0.47 + 0.35 + beat * 0.22 * sin(t * 1.3);
let ay = t * 0.63 + 0.55;
let az = t * 0.23 + beat * 0.16 * cos(t * 0.7);
let cx = cos(ax); let snx = sin(ax);
let cy = cos(ay); let sny = sin(ay);
let cz = cos(az); let snz = sin(az);
let twist = mid * 0.95 * sin(t * 0.83);
let ct = cos(twist); let st = sin(twist);
let vertices = [
[-1.0, -1.0, -1.0], [1.0, -1.0, -1.0],
[1.0, 1.0, -1.0], [-1.0, 1.0, -1.0],
[-1.0, -1.0, 1.0], [1.0, -1.0, 1.0],
[1.0, 1.0, 1.0], [-1.0, 1.0, 1.0]
];
let projected = [];
let extent = 0.01;
for i in 0..8 {
let v = vertices[i];
let pull = 1.0 + treble * 0.38 * sin(t * 2.3 + to_float(i) * 2.4)
+ beat * 0.14 * cos(to_float(i) * 1.7 + t);
let x = v[0] * sx * pull;
let y = v[1] * sy * pull;
let z = v[2] * sz * pull;
let xt = x * ct + z * st * v[1];
let zt = z * ct - x * st * v[1];
let y1 = y * cx - zt * snx;
let z1 = y * snx + zt * cx;
let x2 = xt * cy + z1 * sny;
let z2 = z1 * cy - xt * sny;
let x3 = x2 * cz - y1 * snz;
let y3 = x2 * snz + y1 * cz;
// Even the largest deformed vertex stays < 6.4 from the origin.
let perspective = 10.0 / (10.0 + z2);
let px = x3 * perspective;
let py = y3 * perspective;
if abs(px) > extent { extent = abs(px); }
if abs(py) > extent { extent = abs(py); }
projected.push([px, py, z2]);
}
// CELL_ASPECT is cell height / width; adjust for a different font.
let CELL_ASPECT = 2.0;
let span = min(to_float(width - 4) / CELL_ASPECT, to_float(height - 4));
let scale = span * min(0.23, 0.46 / extent);
let center_x = to_float(width - 1) * 0.5;
let center_y = to_float(height - 1) * 0.5;
let points = [];
for p in projected {
points.push([to_int(center_x + p[0] * scale * CELL_ASPECT + 0.5),
to_int(center_y + p[1] * scale + 0.5), p[2]]);
}
points
}
// Method calls mutate the command array in place; don't copy it into helpers.
fn dot(x, y, ch, fg, bg, width, height) {
let px = to_int(x + 0.5); let py = to_int(y + 0.5);
if x >= 0.0 && y >= 1.0 && px < width && py < height - 1 {
this.push(#{ op: "cell", x: px, y: py, ch: ch, fg: fg, bg: bg });
}
}
fn render(input) {
let bg = 0x020610;
let cmds = [#{ op: "clear", bg: bg }];
if input.width < 12 || input.height < 8 { return cmds; }
let w = input.width; let h = input.height;
let t = input.time;
let energy = response(input.energy);
let bass = response(input.bass);
let mid = response(input.mid);
let treble = response(input.treble);
let beat = response(input.beat);
let drive = clamp(energy * 0.4 + treble * 0.3 + beat * 0.8, 0.0, 1.0);
let small = w < 40 || h < 16;
let center_x = to_float(w - 1) * 0.5;
let center_y = to_float(h - 1) * 0.5;
let span = min(to_float(w - 4) * 0.5, to_float(h - 4));
let points = mesh(t, energy, bass, mid, treble, beat, w, h);
let edges = [[0,1], [1,2], [2,3], [3,0], [4,5], [5,6],
[6,7], [7,4], [0,4], [1,5], [2,6], [3,7]];
// Sparse drifting starfield; deterministic, independent of audio hardware.
let stars = if small { 8 } else { 20 };
for i in 0..stars {
let phase = to_float(i);
let x = (phase * 37.71 + t * (0.3 + to_float(i % 3) * 0.2)) % to_float(w - 1);
let y = 1.0 + (phase * 13.37) % to_float(h - 3);
let light = 0.12 + treble * 0.16 * (0.5 + 0.5 * sin(t * 2.0 + phase));
cmds.dot(x, y, ".", palette(t, 0.0, light), bg, w, h);
}
// Three elliptical shock fronts. On a transient they brighten and
// spread; release of the beat envelope carries them farther outward.
if !small && drive > 0.04 {
for ring in 0..3 {
let phase = (t * 0.45 + to_float(ring) / 3.0) % 1.0;
let radius = 0.22 + phase * 0.55 + (1.0 - beat) * 0.18;
let rx = span * radius;
let ry = span * radius * (0.22 + mid * 0.10);
let left = to_int(center_x - rx);
let right = to_int(center_x + rx);
let top = []; let bottom = [];
for x in left..=right {
let nx = clamp((to_float(x) - center_x) / rx, -1.0, 1.0);
let dy = sqrt(1.0 - nx * nx) * ry;
top.push(to_int(center_y - dy + 0.5));
bottom.push(to_int(center_y + dy + 0.5));
}
let fg = palette(t + to_float(ring), beat, (1.0 - phase) * (0.12 + beat * 0.45 + bass * 0.12));
for ys in [top, bottom] {
cmds.push(#{ op: "trace", x: left, ys: ys, ch: ".", line_ch: ".",
fg: fg, line_fg: fg, bg: bg });
}
}
}
// Particles launch from the eight corners with staggered lifetimes.
// Speed/age depend on time; audio changes reach and brightness instead
// of rescaling time and teleporting particles on each audio update.
if drive > 0.04 {
let particles = if small { 16 } else { 48 };
for i in 0..particles {
let fi = to_float(i);
let age = (t * (0.27 + to_float(i % 5) * 0.025) + fi * 0.618034) % 1.0;
let angle = fi * 2.39996 + t * 0.16;
let dx = cos(angle); let dy = sin(angle);
let origin = points[i % 8];
let travel = span * (0.35 + energy * 0.55 + beat * 0.8);
let distance = age * travel;
let light = sin(age * 3.14159) * (0.15 + drive * 0.85);
let fg = palette(t + fi * 0.13, beat, light);
let ch = if beat > 0.45 && i % 3 == 0 { "*" } else if treble > 0.35 { "+" } else { "." };
if !small && i % 2 == 0 {
let tail = distance - span * 0.04;
cmds.dot(to_float(origin[0]) + dx * tail * 2.0,
to_float(origin[1]) + dy * tail, ".", palette(t, beat, light * 0.3), bg, w, h);
}
cmds.dot(to_float(origin[0]) + dx * distance * 2.0,
to_float(origin[1]) + dy * distance, ch, fg, bg, w, h);
}
}
// Analytic motion echoes. These are recomputed poses, not frame history.
if !small && drive > 0.04 {
for echo in [2, 1] {
let lag = to_float(echo) * (0.14 + beat * 0.10);
let ghost = mesh(t - lag, energy, bass, mid, treble, beat, w, h);
let fg = palette(t + to_float(echo), 1.0 - beat, (0.08 + drive * 0.16) / to_float(echo));
for e in edges {
let a = ghost[e[0]]; let b = ghost[e[1]];
cmds.push(edge(a[0], a[1], b[0], b[1], fg, bg));
}
}
}
// A warm inner cage gives depth and a second pulse inside the main mesh.
if !small {
let inner = [];
let size = 0.35 + beat * 0.25;
for p in points {
inner.push([to_int(center_x + (to_float(p[0]) - center_x) * size + 0.5),
to_int(center_y + (to_float(p[1]) - center_y) * size + 0.5)]);
}
let fg = rgb(100.0 + beat * 155.0, 45.0 + energy * 75.0, 65.0 + treble * 70.0);
for e in edges {
let a = inner[e[0]]; let b = inner[e[1]];
cmds.push(edge(a[0], a[1], b[0], b[1], fg, bg));
}
}
let ordered = [];
for e in edges {
let depth = (points[e[0]][2] + points[e[1]][2]) * 0.5;
let at = 0;
while at < ordered.len() && ordered[at][2] > depth { at += 1; }
ordered.insert(at, [e[0], e[1], depth]);
}
for e in ordered {
let a = points[e[0]]; let b = points[e[1]];
let light = clamp(0.78 - e[2] * 0.14 + beat * 0.12, 0.3, 1.0);
cmds.push(edge(a[0], a[1], b[0], b[1], palette(t, beat, light), bg));
}
// Sparks travel along edges; no more than twelve extra cells.
if treble > 0.08 || beat > 0.08 {
for i in 0..12 {
let e = edges[i]; let a = points[e[0]]; let b = points[e[1]];
let phase = (t * 0.8 + to_float(i) * 0.137) % 1.0;
cmds.dot(to_float(a[0]) + to_float(b[0] - a[0]) * phase,
to_float(a[1]) + to_float(b[1] - a[1]) * phase,
"*", 0xffe8c0, bg, w, h);
}
}
for p in points {
if !small && beat > 0.45 {
let glow = palette(t, beat, beat * 0.45);
for offset in [[-1,0], [1,0], [0,-1], [0,1]] {
cmds.dot(to_float(p[0] + offset[0]), to_float(p[1] + offset[1]), "+", glow, bg, w, h);
}
}
cmds.push(#{ op: "cell", x: p[0], y: p[1], ch: "o", fg: 0xe8f6ff, bg: bg });
}
if input.show_clock {
cmds.push(#{ op: "text", x: 1, y: 0, text: input.clock, fg: 0xa6cde0, bg: bg });
}
if input.paused {
cmds.push(#{ op: "text", x: w - 7, y: 0, text: "paused", fg: 0xa6cde0, bg: bg });
}
if w >= 16 {
cmds.push(#{ op: "text", x: 1, y: h - 1, text: "MORPHING CUBE", fg: 0x527b98, bg: bg });
}
cmds
}
+57
View File
@@ -0,0 +1,57 @@
// name: Status braille histogram
// protocol: furumi-visualizer-v2
// surface: status
// revision: 2
//
// One-row waveform histogram. Input is decoded audio BEFORE output volume.
// The host freezes the last frame on pause, including custom scripts.
// Edit this file to customize it; only untouched bundled defaults upgrade.
// Each Braille character packs two independent four-dot vertical columns.
// "default" background retains the terminal's background color.
fn clamp(value, low, high) {
if value < low { low } else if value > high { high } else { value }
}
// Local RMS for one slice of the recent waveform. Method syntax avoids
// copying the complete sample array for every column.
fn level(index, columns, reference) {
let count = this.len();
if count == 0 { return 0; }
let start = index * count / columns;
let end = (index + 1) * count / columns;
let square = 0.0;
let total = 0;
for i in start..end {
square += this[i] * this[i];
total += 1;
}
let rms = if total > 0 { sqrt(square / to_float(total)) }
else { abs(this[start]) };
// Remove the near-silent baseline and spread useful levels over 0..4.
// No additive bass/energy/beat floor: quiet columns really become empty.
let normalized = clamp((rms / reference - 0.12) / 0.72, 0.0, 1.0);
to_int(normalized * 4.0 + 0.5)
}
fn render(input) {
let commands = [#{ op: "clear", bg: "default" }];
if input.width <= 0 || input.height <= 0 { return commands; }
let samples = input.samples;
let square = 0.0;
for sample in samples { square += sample * sample; }
let rms = if samples.len() > 0 { sqrt(square / to_float(samples.len())) } else { 0.0 };
// Adapt headroom to the current recording, with an absolute noise floor
// so silence/very quiet passages are never normalized into a full row.
let reference = if rms * 1.8 > 0.10 { rms * 1.8 } else { 0.10 };
let dots = ["⠀⢀⢠⢰⢸", "⡀⣀⣠⣰⣸", "⡄⣄⣤⣴⣼", "⡆⣆⣦⣶⣾", "⡇⣇⣧⣷⣿"];
let columns = input.width * 2;
for x in 0..input.width {
let left = samples.level(x * 2, columns, reference);
let right = samples.level(x * 2 + 1, columns, reference);
let glyph = dots[left].sub_string(right, 1);
commands.push(#{ op: "cell", x: x, y: 0, ch: glyph,
fg: 0x48c9c0, bg: "default" });
}
commands
}
+353 -32
View File
@@ -1,4 +1,4 @@
//! Script-backed fullscreen visualizers.
//! Script-backed fullscreen and status-bar visualizers.
//!
//! Rust owns the audio tap, script loading, sandboxed execution limits and
//! terminal drawing primitives. Visual math lives in `.rhai` files: every
@@ -11,6 +11,9 @@ use std::time::{Instant, SystemTime};
use anyhow::{Context as _, Result};
use ratatui::Frame;
use ratatui::buffer::Buffer;
pub mod status;
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::widgets::{Clear, Paragraph};
@@ -20,12 +23,12 @@ use serde::{Deserialize, Serialize};
use crate::app::state::AppState;
const PRIMARY_SCRIPT_ID: &str = "scope_spectrum";
const BUNDLE_VERSION: u32 = 5;
struct BundledScript {
id: &'static str,
file_name: &'static str,
display_name: &'static str,
version: u32,
source: &'static str,
}
@@ -34,14 +37,23 @@ const BUNDLED_SCRIPTS: &[BundledScript] = &[
id: PRIMARY_SCRIPT_ID,
file_name: "scope_spectrum.rhai",
display_name: "Scope spectrum",
version: 5,
source: include_str!("visualizations/scope_spectrum.rhai"),
},
BundledScript {
id: "pulsing_sphere",
file_name: "pulsing_sphere.rhai",
display_name: "Pulsing sphere",
version: 5,
source: include_str!("visualizations/pulsing_sphere.rhai"),
},
BundledScript {
id: "morphing_cube",
file_name: "morphing_cube.rhai",
display_name: "Morphing cube",
version: 6,
source: include_str!("visualizations/morphing_cube.rhai"),
},
];
#[derive(Debug, Clone)]
@@ -114,6 +126,9 @@ impl VisualizerState {
}
pub fn load_library(&mut self) -> Result<()> {
if let Err(err) = status::ensure_script() {
tracing::warn!(%err, "status visualization initialization failed");
}
ensure_bundled_scripts()?;
self.config = load_config().unwrap_or_else(|err| {
tracing::warn!(%err, "visualization settings failed to load; using defaults");
@@ -284,7 +299,10 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
}
fn input_map(area: Rect, state: &AppState) -> Map {
let features = features_from_state(state);
input_map_with_features(area, state, features_from_state(state, true))
}
fn input_map_with_features(area: Rect, state: &AppState, features: AudioFeatures) -> Map {
let mut input = Map::new();
insert_int(&mut input, "width", i64::from(area.width));
insert_int(&mut input, "height", i64::from(area.height));
@@ -329,7 +347,7 @@ fn input_map(area: Rect, state: &AppState) -> Map {
input
}
fn features_from_state(state: &AppState) -> AudioFeatures {
fn features_from_state(state: &AppState, scale_output: bool) -> AudioFeatures {
let elapsed_secs = state
.visualizer
.started_at
@@ -355,12 +373,18 @@ fn features_from_state(state: &AppState) -> AudioFeatures {
.unwrap_or(0.17);
let analysis = &state.player.audio_analysis;
let volume = f64::from(state.player.volume.min(100)) / 100.0;
let output_scale = if state.player.volume == 0 {
let output_scale = if !scale_output {
1.0
} else if state.player.volume == 0 {
0.0
} else {
0.35 + volume * 0.65
};
let active_scale = if state.player.paused { 0.12 } else { 1.0 };
let active_scale = if scale_output && state.player.paused {
0.12
} else {
1.0
};
let scale = output_scale * active_scale;
AudioFeatures {
@@ -373,7 +397,7 @@ fn features_from_state(state: &AppState) -> AudioFeatures {
bass: (analysis.bass * scale).clamp(0.0, 1.0),
mid: (analysis.mid * scale).clamp(0.0, 1.0),
treble: (analysis.treble * scale).clamp(0.0, 1.0),
beat: if state.player.paused {
beat: if scale_output && state.player.paused {
0.0
} else {
(analysis.beat * output_scale).clamp(0.0, 1.0)
@@ -423,6 +447,10 @@ impl Default for ScriptRuntime {
.set_max_string_size(1_000_000)
.set_max_array_size(120_000)
.set_max_map_size(1_000_000);
engine.on_print(|message| tracing::debug!(message, "visualizer script"));
engine.on_debug(|message, source, position| {
tracing::debug!(message, ?source, ?position, "visualizer script")
});
engine.disable_symbol("import");
engine.disable_symbol("export");
engine.register_fn("sin", |value: FLOAT| value.sin());
@@ -606,11 +634,15 @@ impl DrawCommand {
}
fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
render_commands_to_buffer(frame.buffer_mut(), area, commands);
}
fn render_commands_to_buffer(buffer: &mut Buffer, area: Rect, commands: &[DrawCommand]) {
for command in commands {
match command {
DrawCommand::Clear { bg } => fill_rect(frame, area, " ", Style::new().bg(*bg)),
DrawCommand::Clear { bg } => fill_rect(buffer, area, " ", Style::new().bg(*bg)),
DrawCommand::Cell { x, y, ch, fg, bg } => {
write_cell(frame, area, *x, *y, ch, Style::new().fg(*fg).bg(*bg));
write_cell(buffer, area, *x, *y, ch, Style::new().fg(*fg).bg(*bg));
}
DrawCommand::HLine {
x,
@@ -621,8 +653,8 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
bg,
} => {
let style = Style::new().fg(*fg).bg(*bg);
for offset in 0..*w {
write_cell(frame, area, x.saturating_add(offset), *y, ch, style);
for offset in 0..(*w).min(area.width.saturating_sub(*x)) {
write_cell(buffer, area, x.saturating_add(offset), *y, ch, style);
}
}
DrawCommand::VLine {
@@ -634,8 +666,8 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
bg,
} => {
let style = Style::new().fg(*fg).bg(*bg);
for offset in 0..*h {
write_cell(frame, area, *x, y.saturating_add(offset), ch, style);
for offset in 0..(*h).min(area.height.saturating_sub(*y)) {
write_cell(buffer, area, *x, y.saturating_add(offset), ch, style);
}
}
DrawCommand::Rect {
@@ -648,10 +680,10 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
bg,
} => {
let style = Style::new().fg(*fg).bg(*bg);
for row in 0..*h {
for col in 0..*w {
for row in 0..(*h).min(area.height.saturating_sub(*y)) {
for col in 0..(*w).min(area.width.saturating_sub(*x)) {
write_cell(
frame,
buffer,
area,
x.saturating_add(col),
y.saturating_add(row),
@@ -673,7 +705,12 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
let line_style = Style::new().fg(*line_fg).bg(*bg);
let point_style = Style::new().fg(*fg).bg(*bg);
let mut previous_y: Option<u16> = None;
for (index, y) in ys.iter().copied().enumerate() {
for (index, y) in ys
.iter()
.copied()
.take(usize::from(area.width.saturating_sub(*x)))
.enumerate()
{
let Ok(offset) = u16::try_from(index) else {
break;
};
@@ -681,21 +718,25 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
if let Some(previous) = previous_y {
let from = previous.min(y);
let to = previous.max(y);
for y in from..=to {
write_cell(frame, area, x, y, line_ch, line_style);
for y in from..=to.min(area.height.saturating_sub(1)) {
write_cell(buffer, area, x, y, line_ch, line_style);
}
}
write_cell(frame, area, x, y, ch, point_style);
write_cell(buffer, area, x, y, ch, point_style);
previous_y = Some(y);
}
}
DrawCommand::Text { x, y, text, fg, bg } => {
for (offset, ch) in text.chars().enumerate() {
for (offset, ch) in text
.chars()
.take(usize::from(area.width.saturating_sub(*x)))
.enumerate()
{
let Ok(offset) = u16::try_from(offset) else {
break;
};
write_cell(
frame,
buffer,
area,
x.saturating_add(offset),
*y,
@@ -708,24 +749,21 @@ fn render_commands(frame: &mut Frame, area: Rect, commands: &[DrawCommand]) {
}
}
fn fill_rect(frame: &mut Frame, area: Rect, symbol: &'static str, style: Style) {
fn fill_rect(buffer: &mut Buffer, area: Rect, symbol: &'static str, style: Style) {
for y in area.y..area.y + area.height {
for x in area.x..area.x + area.width {
if let Some(cell) = frame.buffer_mut().cell_mut((x, y)) {
if let Some(cell) = buffer.cell_mut((x, y)) {
cell.set_symbol(symbol).set_style(style);
}
}
}
}
fn write_cell(frame: &mut Frame, area: Rect, x: u16, y: u16, symbol: &str, style: Style) {
fn write_cell(buffer: &mut Buffer, area: Rect, x: u16, y: u16, symbol: &str, style: Style) {
if x >= area.width || y >= area.height {
return;
}
if let Some(cell) = frame
.buffer_mut()
.cell_mut((area.x.saturating_add(x), area.y.saturating_add(y)))
{
if let Some(cell) = buffer.cell_mut((area.x.saturating_add(x), area.y.saturating_add(y))) {
cell.set_symbol(symbol).set_style(style);
}
}
@@ -774,6 +812,9 @@ fn rgb_color(value: INT) -> Color {
}
fn parse_color(text: &str) -> Option<Color> {
if text == "default" {
return Some(Color::Reset);
}
let hex = text.trim().trim_start_matches('#').trim_start_matches("0x");
let value = u32::from_str_radix(hex, 16).ok()?;
Some(Color::Rgb(
@@ -850,7 +891,7 @@ fn ensure_bundled_scripts() -> Result<()> {
fn should_replace_bundled_script(existing: &str, script: &BundledScript) -> bool {
let name_marker = format!("// name: {}", script.display_name);
let version_marker = format!("// bundle-version: {BUNDLE_VERSION}");
let version_marker = format!("// bundle-version: {}", script.version);
existing
.lines()
.take(3)
@@ -918,9 +959,9 @@ mod tests {
insert_int(&mut input, "analysis_sequence", 1);
input.insert(
"samples".into(),
(0..128)
(0..256)
.map(|index| {
let sample = ((index as f64 / 128.0) * std::f64::consts::TAU).sin();
let sample = ((index as f64 / 256.0) * std::f64::consts::TAU).sin();
Dynamic::from_float(sample as FLOAT)
})
.collect::<Array>()
@@ -991,4 +1032,284 @@ mod tests {
scope,
));
}
fn cube_file() -> tempfile::NamedTempFile {
let file = tempfile::NamedTempFile::new().unwrap();
std::fs::write(
file.path(),
include_str!("visualizations/morphing_cube.rhai"),
)
.unwrap();
file
}
#[test]
fn cube_update_does_not_upgrade_other_presets() {
let cube = BUNDLED_SCRIPTS
.iter()
.find(|script| script.id == "morphing_cube")
.unwrap();
let old_cube = cube
.source
.replacen("// bundle-version: 6", "// bundle-version: 5", 1);
assert!(should_replace_bundled_script(&old_cube, cube));
for script in BUNDLED_SCRIPTS {
assert!(!should_replace_bundled_script(script.source, script));
}
let custom = old_cube.replacen("// name: Morphing cube", "// name: My cube", 1);
assert!(!should_replace_bundled_script(&custom, cube));
}
#[test]
fn cube_music_adds_particles_echoes_and_a_larger_pulse() {
let file = cube_file();
let mut runtime = ScriptRuntime::default();
let mut input = test_input();
for band in ["energy", "bass", "mid", "treble", "beat"] {
insert_float(&mut input, band, 0.0);
}
let quiet = runtime.render(file.path(), input.clone()).unwrap();
for band in ["energy", "bass", "mid", "treble", "beat"] {
insert_float(&mut input, band, 0.8);
}
let loud = runtime.render(file.path(), input).unwrap();
let effects = |commands: &[DrawCommand]| {
commands
.iter()
.filter(|command| matches!(command, DrawCommand::Cell { ch, .. } if ch != "o"))
.count()
};
assert!(
effects(&loud) > effects(&quiet) + 30,
"music should visibly populate the scene"
);
let lines = |commands: &[DrawCommand]| {
commands
.iter()
.filter(|command| {
matches!(
command,
DrawCommand::Trace { .. } | DrawCommand::VLine { .. }
)
})
.count()
};
assert_eq!(
lines(&loud),
lines(&quiet) + 30,
"two echoes and three rings"
);
let footprint = |commands: &[DrawCommand]| {
let vertices = cube_vertices(commands);
let xs: Vec<_> = vertices.iter().map(|p| p.0).collect();
let ys: Vec<_> = vertices.iter().map(|p| p.1).collect();
u32::from(xs.iter().max().unwrap() - xs.iter().min().unwrap())
* u32::from(ys.iter().max().unwrap() - ys.iter().min().unwrap())
};
assert!(
footprint(&loud) > footprint(&quiet) * 2,
"pulse should be substantially larger"
);
}
fn cube_vertices(commands: &[DrawCommand]) -> Vec<(u16, u16)> {
commands
.iter()
.filter_map(|command| match command {
DrawCommand::Cell { x, y, ch, .. } if ch == "o" => Some((*x, *y)),
_ => None,
})
.collect()
}
#[test]
fn cube_rotates_and_each_audio_control_deforms_geometry() {
let file = cube_file();
let mut runtime = ScriptRuntime::default();
let mut input = test_input();
insert_int(&mut input, "width", 160);
insert_int(&mut input, "height", 60);
for band in ["energy", "bass", "mid", "treble", "beat"] {
insert_float(&mut input, band, 0.0);
}
let quiet = cube_vertices(&runtime.render(file.path(), input.clone()).unwrap());
assert_eq!(quiet.len(), 8);
for band in ["energy", "bass", "mid", "treble", "beat"] {
let mut loud = input.clone();
insert_float(&mut loud, band, 1.0);
let deformed = cube_vertices(&runtime.render(file.path(), loud).unwrap());
assert_ne!(
quiet, deformed,
"{band} should move vertices, not just colors"
);
}
insert_float(&mut input, "time", 3.0);
let rotated = cube_vertices(&runtime.render(file.path(), input).unwrap());
assert_ne!(quiet, rotated, "rotation should continue in silence");
}
#[test]
fn cube_stays_bounded_across_sizes_times_and_audio_extremes() {
let file = cube_file();
let mut runtime = ScriptRuntime::default();
// Much tighter than the production two-million-operation ceiling.
runtime.engine.set_max_operations(200_000);
for (width, height) in [
(1, 1),
(11, 7),
(12, 8),
(24, 80),
(80, 24),
(160, 50),
(320, 100),
] {
for level in [0.0, 0.5, 1.0] {
for tick in 0..12 {
let mut input = test_input();
insert_int(&mut input, "width", width);
insert_int(&mut input, "height", height);
insert_float(&mut input, "time", f64::from(tick) * 1.37);
for band in ["energy", "bass", "mid", "treble", "beat"] {
insert_float(&mut input, band, level);
}
let commands = runtime.render(file.path(), input).unwrap();
assert!(commands.len() <= 220);
if width < 12 || height < 8 {
assert_eq!(commands.len(), 1);
continue;
}
assert_eq!(cube_vertices(&commands).len(), 8);
let mut edges = 0;
for command in &commands {
match command {
DrawCommand::Trace { x, ys, .. } => {
edges += 1;
assert!(usize::from(*x) + ys.len() <= width as usize);
assert!(ys.iter().all(|y| i64::from(*y) < height));
}
DrawCommand::VLine { x, y, h, .. } => {
edges += 1;
assert!(i64::from(*x) < width);
assert!(i64::from(*y) + i64::from(*h) <= height);
}
DrawCommand::Cell { x, y, .. } => {
assert!(i64::from(*x) < width && i64::from(*y) < height);
}
_ => {}
}
}
assert!((12..=54).contains(&edges));
}
}
}
}
#[test]
fn cube_edges_handle_reversed_vertical_and_collapsed_projections() {
let runtime = ScriptRuntime::default();
let ast = runtime
.engine
.compile(include_str!("visualizations/morphing_cube.rhai"))
.unwrap();
for (ax, ay, bx, by) in [
(2_i64, 3_i64, 8_i64, 9_i64),
(8, 9, 2, 3),
(2, 9, 8, 3),
(8, 3, 2, 9),
(4, 9, 4, 3),
(4, 3, 4, 3),
] {
let output = runtime
.engine
.call_fn::<Dynamic>(
&mut Scope::new(),
&ast,
"edge",
(ax, ay, bx, by, 0xffffff_i64, 0_i64),
)
.unwrap();
match DrawCommand::from_dynamic(output).unwrap() {
DrawCommand::Trace { x, ys, .. } => {
assert_eq!(i64::from(x), ax.min(bx));
assert_eq!(ys.len(), (ax - bx).unsigned_abs() as usize + 1);
let (left_y, right_y) = if ax < bx { (ay, by) } else { (by, ay) };
assert_eq!(i64::from(ys[0]), left_y);
assert_eq!(i64::from(*ys.last().unwrap()), right_y);
}
DrawCommand::VLine { x, y, h, .. } => {
assert_eq!(i64::from(x), ax);
assert_eq!(i64::from(y), ay.min(by));
assert_eq!(u64::from(h), (ay - by).unsigned_abs() + 1);
}
other => panic!("unexpected edge: {other:?}"),
}
}
}
/// Explicit, reproducible CPU measurement; no audio hardware or user config.
/// TestBackend includes buffer drawing/diff, but not terminal I/O/emulation.
#[test]
#[ignore = "manual benchmark: cargo test --release benchmark_visualizers -- --ignored --nocapture"]
fn benchmark_visualizers() {
use ratatui::{Terminal, backend::TestBackend};
for script in BUNDLED_SCRIPTS {
let file = tempfile::NamedTempFile::new().unwrap();
std::fs::write(file.path(), script.source).unwrap();
let mut runtime = ScriptRuntime::default();
for (width, height) in [(80_u16, 24_u16), (160, 50), (240, 80)] {
let mut terminal = Terminal::new(TestBackend::new(width, height)).unwrap();
let mut script_times = Vec::new();
let mut frame_times = Vec::new();
let mut max_commands = 0;
for tick in 0..230 {
let mut input = test_input();
insert_int(&mut input, "width", i64::from(width));
insert_int(&mut input, "height", i64::from(height));
let time = f64::from(tick) * 0.05;
insert_float(&mut input, "time", time);
for (i, band) in ["energy", "bass", "mid", "treble", "beat"]
.iter()
.enumerate()
{
insert_float(&mut input, band, (time * (i + 1) as f64).sin() * 0.5 + 0.5);
}
let started = Instant::now();
let commands = runtime.render(file.path(), input).unwrap();
let script_elapsed = started.elapsed();
terminal
.draw(|frame| render_commands(frame, frame.area(), &commands))
.unwrap();
if tick >= 30 {
script_times.push(script_elapsed.as_secs_f64() * 1000.0);
frame_times.push(started.elapsed().as_secs_f64() * 1000.0);
max_commands = max_commands.max(commands.len());
}
}
script_times.sort_by(f64::total_cmp);
frame_times.sort_by(f64::total_cmp);
println!(
"{} {width}x{height}: script p95={:.3}ms; frame median={:.3}ms p95={:.3}ms max={:.3}ms; commands<={max_commands}",
script.id,
script_times[189],
frame_times[99],
frame_times[189],
frame_times[199]
);
if script.id == "morphing_cube" && width == 80 {
for row in terminal
.backend()
.buffer()
.content
.chunks(usize::from(width))
{
println!(
"{}",
row.iter().map(|cell| cell.symbol()).collect::<String>()
);
}
}
}
}
}
}
@@ -0,0 +1,57 @@
// name: Status braille histogram
// protocol: furumi-visualizer-v2
// surface: status
//
// Always-on, one-row visualization. Edit this file to customize it; saved
// changes reload during playback. The host never overwrites an existing file.
// Same render(input) and draw commands as fullscreen scripts; height is 1.
// Audio bands and waveform amplitudes drive the bars (not a full FFT).
// Each Braille character packs two independent four-dot vertical columns.
// "default" background retains the terminal's background color.
// A method reads the waveform through `this`, avoiding an array copy for
// every column in this continuously running visualization.
fn level(index, columns, band, energy, beat) {
let wave = 0.0;
let count = this.len();
if count > 0 {
let start = index * count / columns;
let end = (index + 1) * count / columns;
let total = 0;
for i in start..end {
wave += abs(this[i]);
total += 1;
}
if total > 0 { wave /= to_float(total); }
else { wave = abs(this[start]); }
}
let value = band * 0.55 + wave * 0.8 + energy * 0.12 + beat * 0.18;
if value <= 0.01 { 0 }
else if value < 0.20 { 1 }
else if value < 0.40 { 2 }
else if value < 0.65 { 3 }
else { 4 }
}
fn render(input) {
let commands = [#{ op: "clear", bg: "default" }];
if input.width <= 0 || input.height <= 0 || input.paused { return commands; }
// Lookup table indexed by left/right bar heights, from the bottom up.
let dots = ["⠀⢀⢠⢰⢸", "⡀⣀⣠⣰⣸", "⡄⣄⣤⣴⣼", "⡆⣆⣦⣶⣾", "⡇⣇⣧⣷⣿"];
let columns = input.width * 2;
let samples = input.samples;
for x in 0..input.width {
let levels = [];
for side in 0..2 {
let index = x * 2 + side;
let phase = to_float(index) / to_float(columns);
let band = if phase < 0.33 { input.bass }
else if phase < 0.67 { input.mid } else { input.treble };
levels.push(samples.level(index, columns, band, input.energy, input.beat));
}
let glyph = dots[levels[0]].sub_string(levels[1], 1);
commands.push(#{ op: "cell", x: x, y: 0, ch: glyph,
fg: 0x48c9c0, bg: "default" });
}
commands
}
+583
View File
@@ -0,0 +1,583 @@
//! One-row surface: blocking preparation runs off the UI thread; views copy
//! only the prepared buffer. Uses the fullscreen engine and drawing commands.
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use ratatui::{
Frame,
buffer::Buffer,
layout::Rect,
style::{Color, Style},
widgets::Paragraph,
};
use tokio::sync::mpsc;
use unicode_width::UnicodeWidthStr;
use super::{
ScriptRuntime, features_from_state, input_map_with_features, render_commands_to_buffer,
};
use crate::app::{event::AppEvent, state::AppState};
const SOURCE: &str = include_str!("../visualizations/status/braille.rhai");
const LEGACY_SOURCE: &str = include_str!("fixtures/status_braille_v1.rhai");
pub fn script_path() -> anyhow::Result<PathBuf> {
Ok(super::visualizations_dir()?
.join("status")
.join("braille.rhai"))
}
pub(super) fn ensure_script() -> anyhow::Result<()> {
install_script_at(&script_path()?)
}
fn install_script_at(path: &Path) -> anyhow::Result<()> {
ensure_script_at(path)?;
// Upgrade only a byte-for-byte stock script, never a user's edited one.
if std::fs::metadata(path)?.len() == LEGACY_SOURCE.len() as u64
&& std::fs::read(path)? == LEGACY_SOURCE.as_bytes()
{
let mut replacement =
tempfile::NamedTempFile::new_in(path.parent().unwrap_or(Path::new(".")))?;
replacement.write_all(SOURCE.as_bytes())?;
replacement.as_file().sync_all()?;
if std::fs::read(path)? == LEGACY_SOURCE.as_bytes() {
replacement.persist(path)?;
}
}
Ok(())
}
fn ensure_script_at(path: &Path) -> anyhow::Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
match std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path)
{
Ok(mut file) => file.write_all(SOURCE.as_bytes())?,
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {}
Err(err) => return Err(err.into()),
}
Ok(())
}
pub fn visible(state: &AppState) -> bool {
state.player.playing && state.player.current.is_some() && state.device_playback.is_audio_owner()
}
pub fn playing(state: &AppState) -> bool {
visible(state) && !state.player.paused
}
fn source_input(width: u16, state: &AppState, elapsed: f64) -> rhai::Map {
// AnalyzedSource taps decoded audio before rodio's output volume. Keep
// those source levels intact; input.volume remains available as metadata.
let mut features = features_from_state(state, false);
features.elapsed_secs = elapsed;
input_map_with_features(Rect::new(0, 0, width, 1), state, features)
}
#[derive(Debug, Default)]
pub struct State {
target: Option<(u16, String)>,
generation: u64,
frame: Option<Buffer>,
error: Option<String>,
paused: bool,
}
impl State {
/// Invalidates in-flight results. A resize keeps the last frame until a
/// replacement arrives, including when playback is paused.
pub fn set_target(&mut self, target: Option<(u16, String)>) {
if self.target != target {
let same_track = self
.target
.as_ref()
.zip(target.as_ref())
.is_some_and(|(old, new)| old.1 == new.1);
self.target = target;
self.generation = self.generation.wrapping_add(1);
if !same_track {
self.frame = None;
}
self.error = None;
}
}
pub fn set_paused(&mut self, paused: bool) {
if self.paused != paused {
self.paused = paused;
self.generation = self.generation.wrapping_add(1);
}
}
pub fn apply(&mut self, ready: Ready) {
if ready.generation != self.generation || self.target.is_none() || self.paused {
return;
}
match ready.result {
Ok(frame) => {
self.frame = Some(frame);
self.error = None;
}
Err(error) => {
if self.error.as_ref() != Some(&error) {
tracing::warn!(%error, "status visualization failed");
}
self.frame = None;
self.error = Some(error);
}
}
}
pub fn draw(&self, frame: &mut Frame, area: Rect) {
if area.is_empty() {
return;
}
if let Some(buffer) = &self.frame {
if buffer.area.width == 0 {
return;
}
for x in 0..area.width {
let source_x =
usize::from(x) * usize::from(buffer.area.width) / usize::from(area.width);
if let Some(target) = frame.buffer_mut().cell_mut((area.x + x, area.y)) {
*target = buffer.content[source_x].clone();
}
}
} else if let Some(error) = &self.error {
frame.render_widget(
Paragraph::new(format!("viz: {error}")).style(Style::new().fg(Color::Red)),
area,
);
}
}
}
#[derive(Debug)]
pub struct Ready {
generation: u64,
result: Result<Buffer, String>,
}
/// One in-flight job at most: slow custom scripts drop ticks, never queue
/// frames or block input. AST caches are independent of fullscreen selection.
pub struct Renderer {
runtime: Arc<Mutex<ScriptRuntime>>,
started: Instant,
pub busy: bool,
}
impl Default for Renderer {
fn default() -> Self {
let mut runtime = ScriptRuntime::default();
runtime
.engine
.set_max_operations(200_000)
.set_max_array_size(8192)
.set_max_map_size(4096)
.set_max_string_size(32_768);
Self {
runtime: Arc::new(Mutex::new(runtime)),
started: Instant::now(),
busy: false,
}
}
}
impl Renderer {
pub fn request(&mut self, state: &AppState, tx: mpsc::UnboundedSender<AppEvent>) {
let Some((width, _)) = &state.status_visualizer.target else {
return;
};
if self.busy || !playing(state) || state.visualizer.active {
return;
}
self.busy = true;
let width = *width;
let generation = state.status_visualizer.generation;
let input = source_input(width, state, self.started.elapsed().as_secs_f64());
let runtime = Arc::clone(&self.runtime);
tokio::spawn(async move {
let result = tokio::task::spawn_blocking(move || {
let path = script_path().map_err(|e| e.to_string())?;
let mut runtime = runtime
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if runtime.cached_path.is_none() {
// Retry initialization after a transient startup error.
install_script_at(&path).map_err(|e| e.to_string())?;
}
prepare(&mut runtime, &path, input, width)
})
.await
.unwrap_or_else(|err| Err(err.to_string()));
let _ = tx.send(AppEvent::StatusVisualizationReady(Ready {
generation,
result,
}));
});
}
}
fn prepare(
runtime: &mut ScriptRuntime,
path: &Path,
input: rhai::Map,
width: u16,
) -> Result<Buffer, String> {
if std::fs::metadata(path).map_err(|e| e.to_string())?.len() > 65_536 {
return Err("status script exceeds 64 KiB".into());
}
let commands = runtime.render(path, input)?;
if commands.len() > 4096 {
return Err("too many status drawing commands".into());
}
let area = Rect::new(0, 0, width, 1);
let mut buffer = Buffer::empty(area);
render_commands_to_buffer(&mut buffer, area, &commands);
// A user-supplied cell string must never spill into neighboring UI areas.
for cell in &mut buffer.content {
if cell.symbol().width() != 1 || cell.symbol().chars().any(char::is_control) {
cell.set_symbol(" ");
}
}
Ok(buffer)
}
#[cfg(test)]
mod tests {
use super::super::insert_float;
use super::*;
use ratatui::{Terminal, backend::TestBackend};
use rhai::{Array, Dynamic};
fn input(width: u16, level: f64) -> rhai::Map {
let mut input = source_input(width, &AppState::default(), 0.0);
for band in ["energy", "bass", "mid", "treble", "beat"] {
insert_float(&mut input, band, level);
}
input.insert(
"samples".into(),
(0..256)
.map(|index| {
Dynamic::from_float(level * (index as f64 * std::f64::consts::TAU / 64.0).sin())
})
.collect::<Array>()
.into(),
);
input
}
#[test]
fn histogram_uses_braille_and_real_levels_in_one_row() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("status/braille.rhai");
ensure_script_at(&path).unwrap();
let renderer = Renderer::default();
let mut runtime = renderer.runtime.lock().unwrap();
for width in [0, 1, 8, 60, 200] {
let quiet = prepare(&mut runtime, &path, input(width, 0.0), width).unwrap();
let loud = prepare(&mut runtime, &path, input(width, 1.0), width).unwrap();
assert_eq!(loud.area, Rect::new(0, 0, width, 1));
assert!(quiet.content.iter().all(|cell| cell.symbol() == "⠀"));
assert!(loud.content.iter().all(|cell| {
cell.symbol()
.chars()
.all(|ch| ('\u{2800}'..='\u{28ff}').contains(&ch))
&& cell.bg == Color::Reset
}));
if width > 0 {
assert_ne!(loud, quiet);
}
let mut paused = input(width, 1.0);
paused.insert("paused".into(), true.into());
assert_eq!(prepare(&mut runtime, &path, paused, width).unwrap(), loud);
}
}
#[test]
fn existing_user_script_survives_and_hot_reload_recovers_from_errors() {
let file = tempfile::NamedTempFile::new().unwrap();
let path = file.path();
let mut runtime = ScriptRuntime::default();
for (revision, symbol) in [(1, "A"), (2, "B"), (3, "bad"), (4, "C")] {
let source = if symbol == "bad" {
"fn render(".into()
} else {
format!(
"fn render(input) {{ [#{{ op: \"text\", x: 0, y: 0, text: \"{symbol}\" }}] }}"
)
};
std::fs::write(path, &source).unwrap();
file.as_file()
.set_times(std::fs::FileTimes::new().set_modified(
std::time::UNIX_EPOCH
+ std::time::Duration::from_secs(1_700_000_000 + revision),
))
.unwrap();
install_script_at(path).unwrap();
assert_eq!(std::fs::read_to_string(path).unwrap(), source);
let frame = prepare(&mut runtime, path, input(8, 0.0), 8);
if symbol == "bad" {
assert!(frame.is_err());
} else {
assert_eq!(frame.unwrap().content[0].symbol(), symbol);
}
}
}
#[test]
fn status_commands_are_clipped_and_cannot_spill_into_the_ui() {
let file = tempfile::NamedTempFile::new().unwrap();
std::fs::write(
file.path(),
r##"fn render(input) { [
#{ op: "rect", x: 0, y: 0, w: 65535, h: 65535, ch: "#" },
#{ op: "text", x: 0, y: 1, text: "outside" },
#{ op: "trace", x: 6, ys: [65535, 0, 65535], ch: "*" },
#{ op: "cell", x: 0, y: 0, ch: "界" },
#{ op: "cell", x: 1, y: 0, ch: "\n" },
#{ op: "cell", x: 2, y: 0, ch: "too long" }
] }"##,
)
.unwrap();
let buffer = prepare(&mut ScriptRuntime::default(), file.path(), input(8, 0.0), 8).unwrap();
assert!(buffer.content[..3].iter().all(|cell| cell.symbol() == " "));
let state = State {
frame: Some(buffer),
..State::default()
};
let mut terminal = Terminal::new(TestBackend::new(20, 4)).unwrap();
terminal
.draw(|frame| {
for cell in &mut frame.buffer_mut().content {
cell.set_symbol("x");
}
state.draw(frame, Rect::new(11, 3, 8, 1));
})
.unwrap();
let buffer = terminal.backend().buffer();
for y in 0..4 {
for x in 0..20 {
if y != 3 || !(11..19).contains(&x) {
assert_eq!(buffer[(x, y)].symbol(), "x");
}
}
}
}
#[test]
fn stale_frames_are_discarded_after_resize_track_change_or_stop() {
let mut state = State::default();
state.set_target(Some((8, "track-a".into())));
let generation = state.generation;
state.apply(Ready {
generation,
result: Ok(Buffer::empty(Rect::new(0, 0, 8, 1))),
});
assert!(state.frame.is_some());
for target in [
Some((12, "track-a".into())),
Some((12, "track-b".into())),
None,
] {
state.set_target(target);
let previous_frame = state.frame.clone();
state.apply(Ready {
generation,
result: Ok(Buffer::empty(Rect::new(0, 0, 8, 1))),
});
assert_eq!(state.frame, previous_frame);
}
assert!(state.frame.is_none());
}
#[test]
fn pause_freezes_the_last_frame_and_rejects_in_flight_work() {
let mut state = State::default();
state.set_target(Some((8, "track".into())));
let mut buffer = Buffer::empty(Rect::new(0, 0, 8, 1));
for cell in &mut buffer.content {
cell.set_symbol("⣤");
}
let generation = state.generation;
state.apply(Ready {
generation,
result: Ok(buffer.clone()),
});
state.set_paused(true);
state.apply(Ready {
generation,
result: Err("late error".into()),
});
state.apply(Ready {
generation: state.generation,
result: Ok(Buffer::empty(buffer.area)),
});
assert_eq!(state.frame.as_ref(), Some(&buffer));
// A paused resize adapts the frozen row instead of erasing it.
state.set_target(Some((12, "track".into())));
let mut terminal = Terminal::new(TestBackend::new(12, 1)).unwrap();
terminal
.draw(|frame| state.draw(frame, frame.area()))
.unwrap();
assert!(
terminal
.backend()
.buffer()
.content
.iter()
.all(|cell| cell.symbol() == "⣤")
);
let paused_generation = state.generation;
state.set_paused(false);
state.apply(Ready {
generation: paused_generation,
result: Err("stale pause".into()),
});
assert_eq!(state.frame.as_ref(), Some(&buffer));
state.apply(Ready {
generation: state.generation,
result: Ok(Buffer::empty(Rect::new(0, 0, 12, 1))),
});
assert_ne!(state.frame.as_ref(), Some(&buffer));
state.set_target(None);
assert!(state.frame.is_none());
}
#[test]
fn source_audio_input_is_independent_of_output_volume_and_pause() {
let file = tempfile::NamedTempFile::new().unwrap();
std::fs::write(file.path(), SOURCE).unwrap();
let mut state = AppState::default();
state.player.audio_analysis = crate::player::AudioAnalysisSnapshot {
energy: 0.8,
bass: 0.9,
mid: 0.6,
treble: 0.3,
beat: 0.5,
scope: (0..256).map(|i| (i as f64 * 0.13).sin() * 0.7).collect(),
..Default::default()
};
let mut runtime = ScriptRuntime::default();
let expected =
prepare(&mut runtime, file.path(), source_input(64, &state, 1.0), 64).unwrap();
for volume in [0, 1, 25, 80, 100] {
for paused in [false, true] {
state.player.volume = volume;
state.player.paused = paused;
let map = source_input(64, &state, 1.0);
for (key, level) in [
("energy", 0.8),
("bass", 0.9),
("mid", 0.6),
("treble", 0.3),
("beat", 0.5),
] {
assert_eq!(map[key].as_float().unwrap(), level);
}
assert_eq!(map["volume"].as_float().unwrap(), f64::from(volume) / 100.0);
assert_eq!(
prepare(&mut runtime, file.path(), map, 64).unwrap(),
expected
);
}
}
}
#[test]
fn loud_recordings_use_multiple_heights_instead_of_a_solid_row() {
let file = tempfile::NamedTempFile::new().unwrap();
let mut runtime = ScriptRuntime::default();
std::fs::write(file.path(), LEGACY_SOURCE).unwrap();
let old = prepare(&mut runtime, file.path(), input(64, 1.0), 64).unwrap();
assert!(old.content.iter().all(|cell| cell.symbol() == "⣿"));
install_script_at(file.path()).unwrap();
// A fresh runtime also models a restart installing the new default.
let mut runtime = ScriptRuntime::default();
let new = prepare(&mut runtime, file.path(), input(64, 1.0), 64).unwrap();
let symbols: std::collections::HashSet<_> =
new.content.iter().map(|cell| cell.symbol()).collect();
assert!(symbols.len() >= 4, "{symbols:?}");
assert!(
new.content
.iter()
.filter(|cell| cell.symbol() == "⣿")
.count()
< 32
);
assert!(new.content.iter().any(|cell| cell.symbol() == "⠀"));
// Compressed band/beat values alone must not create a filled baseline.
let mut silence = input(64, 1.0);
silence.insert(
"samples".into(),
(0..256)
.map(|_| Dynamic::from_float(0.001))
.collect::<Array>()
.into(),
);
let silent = prepare(&mut runtime, file.path(), silence, 64).unwrap();
assert!(silent.content.iter().all(|cell| cell.symbol() == "⠀"));
}
#[test]
fn only_the_untouched_previous_default_is_upgraded() {
let file = tempfile::NamedTempFile::new().unwrap();
std::fs::write(file.path(), LEGACY_SOURCE).unwrap();
install_script_at(file.path()).unwrap();
assert_eq!(std::fs::read_to_string(file.path()).unwrap(), SOURCE);
install_script_at(file.path()).unwrap();
assert_eq!(std::fs::read_to_string(file.path()).unwrap(), SOURCE);
for stock in [LEGACY_SOURCE, SOURCE] {
let custom = format!("{stock}\n// user customization\n");
std::fs::write(file.path(), &custom).unwrap();
install_script_at(file.path()).unwrap();
assert_eq!(std::fs::read_to_string(file.path()).unwrap(), custom);
}
}
#[test]
fn status_script_infinite_loop_hits_the_operation_limit() {
let file = tempfile::NamedTempFile::new().unwrap();
std::fs::write(file.path(), "fn render(input) { loop {} [] }").unwrap();
let renderer = Renderer::default();
let mut runtime = renderer.runtime.lock().unwrap();
let err = prepare(&mut runtime, file.path(), input(8, 0.0), 8).unwrap_err();
assert!(err.contains("operations"), "{err}");
}
#[test]
#[ignore = "manual benchmark: cargo test --release benchmark_status_visualizer -- --ignored --nocapture"]
fn benchmark_status_visualizer() {
let file = tempfile::NamedTempFile::new().unwrap();
std::fs::write(file.path(), SOURCE).unwrap();
let renderer = Renderer::default();
let mut runtime = renderer.runtime.lock().unwrap();
for width in [32, 64, 128] {
let mut times = Vec::new();
for tick in 0..230 {
let level = (f64::from(tick) * 0.13).sin() * 0.5 + 0.5;
let input = input(width, level);
let start = Instant::now();
let frame = prepare(&mut runtime, file.path(), input, width).unwrap();
std::hint::black_box(frame);
if tick >= 30 {
times.push(start.elapsed().as_secs_f64() * 1000.0);
}
}
times.sort_by(f64::total_cmp);
println!(
"status {width}x1: median={:.3}ms p95={:.3}ms max={:.3}ms",
times[99], times[189], times[199]
);
}
}
}