web: watch-party layer for the Discord Activity

party.js is inert unless a transport attaches. The host publishes route changes
and playback snapshots; viewers mirror the route, load the host's stream without
scraping and follow its clock. Also: forceProxy playback, bug-tv-tmdb/bug-tv-img
meta overrides, relative API bases (/tv), trailer hidden in party mode, and the
web package no longer ships web/test.
This commit is contained in:
4DBug committed 2026-10-27 10:00:34 -05:00
1 parent 5ab3683f44
commit ee0e36d2c7
14 files changed
+800 -17

No files matched your search

+35
View File
@@ -221,3 +221,38 @@ Player workstream owns `web/js/api.js` (bug-tv server client: `streams(params, {
- `/v1/remux/<token>/probe?start=<sec>` and `/v1/torrent/<token>/probe?start=<sec>` add `keyframe`: the time the remux really starts at. The player uses it as its clock offset because a `<video>` element can't read `X-Bug-Tv-Offset`.
- Extensionless stream URLs are sniffed by their first 1 KB (`src/media/sniff.js`) and typed m3u8/mp4/webm/mkv before `playbackFor` runs.
- The Swagger UI moved to `src/public/index.html`, `mod/` and `rest/` are removed, and webtorrent is bumped to ^3.0.21.
## Party (watch-together hooks)
`web/js/party.js` is inert unless a transport attaches, so the public site is unaffected. tv-discord's Activity overlay is the transport.
```js
import { party } from './party.js'; // also exposed as window.bugtvParty (dispatches 'bugtv:party-ready')
party.mode // 'solo' | 'host' | 'viewer'
party.attach(transport) // transport = { send(type, data) }; the transport then calls:
party.receive(msg) // feed server messages: welcome / host / members / route / play / pong
party.now() // server-synced ms (falls back to Date.now())
party.publish(type, data) // host only: 'route' {path} | 'play' {snapshot}
party.on(type, fn) → off // 'mode' | 'members' | 'route' | 'play'
```
- **Router:** in host mode, every route change publishes `route`. In viewer mode, an incoming `route` calls `navigate(path, {replace:true})`.
- **Watch view, host:** publishes a `play` snapshot on load/source/subtitle/quality/audio/episode change, play/pause/seeked/ended, and every 2s while on the watch route. The snapshot shape is in tv-discord `docs/protocol.md`.
- **Watch view, viewer:**
- Never scrapes. It loads `snapshot.stream` directly through `playStream` and follows the clock: over 1.5s drift it seeks, and at 0.3–1.5s it sets `playbackRate` to 0.95/1.05.
- It applies the subtitle (by lang+label, index as fallback), the quality and the audio track.
- It hides every control except volume, mute and fullscreen.
- **Player:** `createPlayer(video, { forceProxy })`, or `<meta name="bug-tv-force-proxy" content="1">`, always plays `playback.fallback` when present. This is needed because the Activity CSP only allows mapped origins.
- **TMDB bases:** `<meta name="bug-tv-tmdb">` (API base) and `<meta name="bug-tv-img">` (image base) override `https://api.themoviedb.org/3` and `https://image.tmdb.org/t/p/`. In the Activity they are `/tmdb/3` and `/img/t/p/`.
- **Party mode** (`party.mode !== 'solo'`) hides the trailer button, because the YouTube iframe can't be mapped.
### Deviations / details (party layer)
- **Wire shape:** party calls `transport.send(type, data)` with `('route', {path})`, `('play', {snapshot})` (`{snapshot: null}` when the host leaves the watch page) and `('ping', {at})`; the transport sends `{t: type, ...data}`. party pings by itself (5× at 1s, then every 15s) and takes the median of the last 5 pong offsets; `welcome.now` seeds the offset before the first pong.
- **Extra API:** `party.resend()` (host: re-publish current route + snapshot, for the overlay after a reconnect; emits `'resend'`), `party.lastRoute` / `party.lastPlay`, `party.hostName()`, `attach()` returns a detach function. `on('error')` relays server errors. A `welcome.play` whose path differs from `welcome.route` is ignored.
- **Route privacy:** host route messages drop session params (`api`, `frame_id`, `instance_id`, `platform`, `guild_id`, `channel_id`, …); viewers keep their own.
- **Viewer UI:** controls other than volume/mute/fullscreen/PiP get `.pl-host-only` and are hidden under `.pl.is-viewer`; the seek bar stays visible but inert. Those controls and the autoplay fallback button `.pl-join` ("Click to join", local playback only) carry `data-party-allow` for the overlay's input blocking. Viewers never auto-advance to the next episode; a viewer-side source failure shows "Waiting for <host>…" and the next heartbeat reloads the host's stream.
- **Drift:** paused snapshots seek when |drift| > 0.5s; hls/file seeks are rate-limited to 1 per 1.5s, remux to 1 per 3s; a viewer stuck seeking/buffering for 5s (12s remux) is re-seeked. Playback speed is not mirrored.
- **Quality:** `snapshot.quality` is the host's explicit menu pick (`chooseLevel`), reset to auto on source change, not hls.js's momentary level.
- **Watch handle:** `views/watch.js` exports `active()` → `{player, state, snapshot(), playStream, switchTo, switchEpisode, chooseSub, chooseLevel, role}` for tooling/tests.
- **Player:** `player.original` is the Stream passed to `load()`; `player.stream` is the proxied copy under forceProxy. `proxiedStream(s)` is exported.
- **API base:** a relative `bug-tv-api` / `?api=` / stored base such as `/tv` resolves against `location.origin` (it used to become `https://tv`).
- **Trailer:** hidden by CSS (`html.party-host|party-viewer .js-trailer`), and `trailerModal()` is a no-op outside solo.
- **Tests:** `node --test web/test/party.test.mjs`.