Yokan / rebuild
Course 1 8624561

TORRENT_STREAMING.md

docs/features/TORRENT_STREAMING.md

documentation / design asset150 lines
1# Torrent streaming (TorrServer)23Animetail streams magnet and `.torrent` videos through an embedded TorrServer4(a Go BitTorrent engine with an HTTP API). Yokan talks to the same5[TorrServer API](https://github.com/YouROK/TorrServer) from shared code. On6desktop it can run TorrServer itself; on every platform it can use a TorrServer7the user runs. Settings live in `DbSettings.features.torrent` and the page is8Settings → Player → Torrent streaming. The feature is off by default.910## What is wired1112- **Playback.** `ProfileReaderContent.videos()` is the single source of player,13  quality-switch, failover, cast and external-player URLs.14  - A torrent library entry (Library → Torrent stream) is source-less and its15    chapter URL is a magnet link or a `.torrent` path/URL. It now resolves to16    TorrServer stream URLs. Local `.torrent` files are uploaded17    (`/torrent/upload`, 16 MiB cap) because a remote server cannot read them.18  - Extension video lists can contain magnet links or `.torrent` URLs, as in19    Animetail. These are expanded in place. Subtitle and audio tracks are kept,20    and the quality label becomes `<quality> · <file name>`.21- **File selection.** Yokan polls `get` until the torrent's metadata arrives22  (90 s limit). Each video file of a multi-file torrent becomes a separate23  stream, chosen in the player's quality list. Non-video files are listed only24  when the torrent has no video files. Animetail's `index=N` link parameter25  selects one file (TorrServer file ids start at 1).26- **Downloads.** The anime download resolver passes the chosen source video27  through the same expansion and downloads TorrServer's28  `/stream/<name>?link=<hash>&index=<id>&play` URL with the normal HTTP29  downloader. This is Animetail's `torrentDownload` path. Torrent library30  entries are not downloadable: they are local-archive titles, whose download31  deletion would remove the user's `.torrent` file.32- **Engine off.** Torrent entries are removed from mixed source lists.33  Torrent-only results and torrent library entries fail with a message that34  points to the setting.3536## Server lifecycle3738| Mode | Platforms | Behavior |39|---|---|---|40| Run TorrServer on this device | Desktop (macOS, Linux, Windows) | Supervised subprocess. Details below. |41| Remote TorrServer | All | Uses the configured URL and optional HTTP Basic credentials, which are sent only on API calls. `play` stream URLs need no credentials. |4243Details of the desktop subprocess:4445- **Binary.** On opt-in, Yokan downloads the official `MatriX.147` release46  binary. The SHA-256 digests are pinned in code. The binary is written to a47  `.part` file, verified, marked executable and moved atomically into48  `~/.local/share/yokan/torrserver-bin/`. A user-provided binary path can be49  used instead.50- **Launch.** `--port <port> --path <profile dir>/torrserver`, plus51  `--proxyurl/--proxymode` when a proxy is set. Output goes to52  `torrserver.log`.53- **Health.** Startup waits for `GET /echo`. It fails with a clear message if54  the process exits (including the log tail), if it does not answer within 20 s,55  or if another server already answers on the port.56- **Crash recovery.** An unexpected exit restarts the server with backoff (1,57  2, 4 s), up to 3 times in a row. After that, the next use restarts it.58  Stream URLs survive a restart because TorrServer re-adds a torrent from the59  `link` hash.60- **Yokan crash.** A pid file holding the pid and the process start time lets61  the next start kill a server left running by a crashed Yokan. The start time62  guards against pid reuse. A JVM shutdown hook stops the server on normal63  exit.64- **Option changes.** A new port, binary or proxy restarts the server on next65  use. Switching to remote mode stops the managed server.66- **Profiles.** Each profile has its own TorrServer state (`config.db`, cache)67  under its owned root, so profile deletion removes it. Session retirement68  (`YokanServices.closeAndJoin`) stops the server.6970## Trackers and proxy7172- The tracker list defaults to Animetail's anime list. It can be edited, and73  "Reset tracker list" restores the default.74- The list is pushed to the server's `/settings` API in `DefaultTrackers`,75  with `RetrackersMode = 1` (add to every torrent). Because `set` replaces all76  settings, Yokan reads the full `BTSets` and changes only these two fields.77- The list is pushed once per server per session, and again on78  "Check server and apply trackers" or a reset.79- Servers without `DefaultTrackers` are reported as too old. Older TorrServer80  builds only read a `trackers.txt` file.81- The proxy modes are None, Trackers, Peers+DHT and Full, as in Animetail.82  TorrServer accepts a proxy only as launch flags (`--proxyurl`,83  `--proxymode`), not through its settings API (checked in84  `server/settings/args.go` and `server/torr/btserver.go` at `a86d8fa`).85  - The desktop managed server receives the flags.86  - A remote server must be started with them where it runs; the settings page87    says so.88- Backups and sync exports clear the binary path, password and proxy URL89  (`DbTorrentPreferences.forExport`).9091## Platform support and blockers9293- **Desktop:** managed or remote. Casting a managed stream is refused because94  `127.0.0.1` is not reachable by a TV. A remote server configured with a LAN95  address can be cast.96- **Android:** remote only. The TorrServer app on the same phone counts97  (`http://127.0.0.1:8090`). Cleartext HTTP is already allowed. Yokan does not98  bundle or exec TorrServer:99  - `TorrServer-android-arm64` is 64,445,000 bytes, and each ABI adds about100    62–64 MB. That would roughly double the APK, and APK size is tracked for101    this project.102  - Android 10+ (W^X) refuses to exec files in app-writable storage, so a103    downloaded or user-provided binary cannot run. Only `nativeLibraryDir`104    (bundled) can.105  - Animetail instead links a JNI build (`libtorrserver.so`) into its APK.106- **iOS:** remote only. iOS forbids spawning processes. The official107  `TorrServerKit.xcframework` is 75 MB and GPL-3.0; GPL-3.0 terms conflict108  with App Store distribution terms. `NSAllowsLocalNetworking` is set so LAN109  servers work over HTTP. Public servers need HTTPS.110- **Web:** remote only. Browsers cannot spawn processes or accept BitTorrent111  peer connections.112  - TorrServer sends CORS headers for all origins: `AllowAllOrigins`,113    `AllowPrivateNetwork`, and an `Authorization` header allow-list114    (`server/web/server.go`, checked at `a86d8fa`). API calls and `<video>`115    playback therefore work cross-origin.116  - An HTTPS-hosted Yokan can reach only an HTTPS TorrServer or `localhost`,117    because of mixed-content blocking.118119## License120121TorrServer is GPL-3.0. The Yokan repository has no license file, so122compatibility with distributing TorrServer inside Yokan cannot be established.123Yokan therefore never ships TorrServer in its artifacts:124125- Desktop downloads the unmodified official binary only on explicit user126  opt-in and runs it as a separate process over HTTP.127- Other platforms use a server the user installs.128129## Verification130131- `TorrentStreamsTest` (common, run on desktop and Kotlin/Wasm) uses MockEngine132  fixtures of the TorrServer API. It covers:133  - remote/hostless selection and Basic auth134  - metadata polling and the timeout135  - multi-file listing, `index=` selection and `.torrent` upload136  - the tracker push preserving `BTSets`, done once per session137  - old-server detection, error bodies and URL validation138  - managed-host launch options, including the proxy, and stopping the host139    in remote mode140  - disabled-engine filtering141- `DesktopTorrServerHostTest` runs real subprocesses of a fake TorrServer (a142  Java program behind a shell script). It covers:143  - start/health, proxy flags, crash restart and stop with pid-file cleanup144  - restart on option change145  - orphan kill after an app crash146  - a foreign server on the port, a startup crash with the log tail, and a147    missing binary148  - SHA-256-verified install, including a rejected mismatch that leaves no149    file behind150- No real TorrServer, swarm or device playback was exercised.151

Search chapters, reference and source filenames.