Compare commits
4
Commits
f6e2b7edda
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
87286c748b | ||
|
|
91e96894b7 | ||
|
|
06760a46b9 | ||
|
|
1da2f71e4b |
@@ -1,6 +1,5 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
data/recent-urls.json
|
data/recent-urls.json
|
||||||
data/webtransport-*.pem
|
|
||||||
.env
|
.env
|
||||||
.DS_Store
|
.DS_Store
|
||||||
npm-debug.log*
|
npm-debug.log*
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ The app is plain Node/Express plus browser JavaScript:
|
|||||||
|
|
||||||
- `server/index.js`: API, WebSocket, source proxy/relay, ffmpeg process lifecycle, recent URL and favorites persistence.
|
- `server/index.js`: API, WebSocket, source proxy/relay, ffmpeg process lifecycle, recent URL and favorites persistence.
|
||||||
- `public/index.html`: frontend markup.
|
- `public/index.html`: frontend markup.
|
||||||
- `public/app.js`: URL submission, WebSocket/WebTransport frame receiving, audio element coordination, canvas drawing, overlay controls.
|
- `public/app.js`: URL submission, WebSocket frame receiving, audio element coordination, canvas drawing, overlay controls.
|
||||||
- `public/styles.css`: two-screen player UI.
|
- `public/styles.css`: two-screen player UI.
|
||||||
- `Dockerfile`: production image with Node and ffmpeg.
|
- `Dockerfile`: production image with Node and ffmpeg.
|
||||||
- `docker-compose-example.yml`: operational example and default env knobs.
|
- `docker-compose-example.yml`: operational example and default env knobs.
|
||||||
@@ -34,7 +34,6 @@ Main public endpoints:
|
|||||||
- `PUT /api/favorites`: replaces the global favorites list. Each favorite has a user-provided `title` and stream `url`.
|
- `PUT /api/favorites`: replaces the global favorites list. Each favorite has a user-provided `title` and stream `url`.
|
||||||
- `GET /audio/:sessionId`: serves MP3 audio to the browser audio element.
|
- `GET /audio/:sessionId`: serves MP3 audio to the browser audio element.
|
||||||
- `WS /frames/:sessionId`: sends timed JPEG frame packets to the browser.
|
- `WS /frames/:sessionId`: sends timed JPEG frame packets to the browser.
|
||||||
- `WebTransport /frames/:sessionId`: optional QUIC frame path on the separate WebTransport UDP listener.
|
|
||||||
- `GET /api/health`: exposes basic health and active playback connection mode.
|
- `GET /api/health`: exposes basic health and active playback connection mode.
|
||||||
|
|
||||||
Internal endpoint:
|
Internal endpoint:
|
||||||
@@ -43,13 +42,11 @@ Internal endpoint:
|
|||||||
|
|
||||||
## Browser Playback Model
|
## Browser Playback Model
|
||||||
|
|
||||||
Audio is the playback clock. The server sends JPEG frames over WebSocket by default, or over WebTransport when explicitly enabled. Each binary frame packet is:
|
Audio is the playback clock. The server sends JPEG frames over WebSocket. Each binary frame packet is:
|
||||||
|
|
||||||
- First 8 bytes: little-endian float64 timestamp in seconds.
|
- First 8 bytes: little-endian float64 timestamp in seconds.
|
||||||
- Remaining bytes: one complete JPEG image.
|
- Remaining bytes: one complete JPEG image.
|
||||||
|
|
||||||
WebSocket sends one binary frame packet per message. WebTransport uses one persistent server-to-client unidirectional frame stream: the stream begins with the single byte `WT_STREAM_FRAME`, then repeats records of 4-byte little-endian uint32 packet length followed by one complete binary frame packet. Do not open one WebTransport stream per JPEG frame; that caused poor device performance at normal frame rates.
|
|
||||||
|
|
||||||
The frontend decodes JPEGs with browser image APIs, queues frames, and paints frames whose timestamps are due relative to `audio.currentTime`. This means the browser decodes only audio and still images, not video.
|
The frontend decodes JPEGs with browser image APIs, queues frames, and paints frames whose timestamps are due relative to `audio.currentTime`. This means the browser decodes only audio and still images, not video.
|
||||||
|
|
||||||
## Why JPEG Frames
|
## Why JPEG Frames
|
||||||
@@ -80,7 +77,13 @@ The regression history matters:
|
|||||||
|
|
||||||
Default code behavior is `split` when no mode is set. The Compose example uses `relay` because it is the mode to try for IPTV streams.
|
Default code behavior is `split` when no mode is set. The Compose example uses `relay` because it is the mode to try for IPTV streams.
|
||||||
|
|
||||||
Finite-duration sessions are treated as recorded video and seekable when metadata probing is enabled. Metadata probing is enabled by default except in `relay`, where it defaults off to preserve the one-upstream-connection behavior for IPTV-style streams. When the configured mode is `relay` and duration metadata is known, recorded sessions switch to the seek-capable `split` path; live/unknown-duration streams stay in relay mode.
|
Finite-duration sessions are treated as recorded video and seekable once duration metadata is available. Metadata probing defaults off for unknown/live URLs in `relay` to preserve one upstream connection, but local files, YouTube and X/Twitter URLs without resolved duration metadata, recognized media file extensions, and Jellyfin download/stream URLs are probed automatically. Likely recorded sources retry failed probes up to three times in the background, keeping metadata pending until they finish. The timeout grows by a factor of three per attempt, capped at 30 seconds (default 4, 12, then 30 seconds), with 1- and 2-second retry delays. The frontend polls for the full pending period, including after stream reconnects.
|
||||||
|
|
||||||
|
When configured for `relay`, recognized recorded files use `split` immediately, before duration is known. HTTP inputs for these files allow automatic range seeking from startup, including when `FFMPEG_INPUT_SEEKABLE=0` is configured for live streams. This avoids reading an entire MP4 through a pipe to find trailing metadata. Other sessions with known duration also use `split`; unknown/live streams stay in relay mode.
|
||||||
|
|
||||||
|
YouTube and X/Twitter sessions use the shared yt-dlp resolver and `split`, with separate resolved video and audio URLs when needed, subject to `SINGLE_CONNECTION_HOSTS` protections. Recognize `x.com` and `twitter.com`, including their `www`, `m`, and `mobile` variants. X posts can return playlists even with `--no-playlist`; use `--playlist-items 1` and unwrap the first entry, while preserving explicit `/video/N` selections. X/Twitter defaults to direct HTTPS formats (preferring combined MP4s), since the source proxy does not rewrite relative HLS segment URLs. Direct MP4 formats may omit codec names: only the explicit value `none` means a stream is absent. The audio proxy must use the audio URL and its headers; frames and probes use the video URL and its headers. Enable yt-dlp's Node runtime explicitly with `--js-runtimes node:<process.execPath>`. Use yt-dlp's finite, non-live duration metadata directly to avoid an extra ffprobe connection.
|
||||||
|
|
||||||
|
IPTV subscriptions such as Oceanstream permit only one stream per customer. `SINGLE_CONNECTION_HOSTS` lists protected hostnames, including their subdomains; these sessions always use `relay`, never metadata probing or `split`, even for recorded-file URLs or when global probing/split is enabled. Seeking and recovery must not bypass this rule. Unknown live streams with probing disabled must not gain a metadata probe during recovery. The deployed list also includes the configured IPTV aliases `line.oscreen.cc` and `yeahtv.live`.
|
||||||
|
|
||||||
## ffmpeg Pipelines
|
## ffmpeg Pipelines
|
||||||
|
|
||||||
@@ -178,31 +181,23 @@ Runtime:
|
|||||||
- `PORT`: HTTP port, default `3000`.
|
- `PORT`: HTTP port, default `3000`.
|
||||||
- `FFMPEG_PATH`: ffmpeg binary path, default `ffmpeg`.
|
- `FFMPEG_PATH`: ffmpeg binary path, default `ffmpeg`.
|
||||||
- `YT_DLP_PATH`: yt-dlp binary path, default `yt-dlp`.
|
- `YT_DLP_PATH`: yt-dlp binary path, default `yt-dlp`.
|
||||||
- `YT_DLP_FORMAT`: yt-dlp format selector for YouTube URLs, default `best[ext=mp4][vcodec!=none][acodec!=none]/best[vcodec!=none][acodec!=none]/best`.
|
- `YT_DLP_FORMAT`: format selector override for YouTube and X/Twitter. YouTube defaults to `bestvideo[protocol=https]+bestaudio[protocol=https]/bestvideo+bestaudio/best`; X/Twitter defaults to `best[protocol=https]/bestvideo[protocol=https]+bestaudio[protocol=https]`. Separate audio/video inputs and combined sources are supported.
|
||||||
- `YT_DLP_TIMEOUT_MS`: yt-dlp resolution timeout, default `45000`.
|
- `YT_DLP_TIMEOUT_MS`: yt-dlp resolution timeout, default `45000`.
|
||||||
- `FFMPEG_LOG_LEVEL`: ffmpeg log level, default `warning`.
|
- `FFMPEG_LOG_LEVEL`: ffmpeg log level, default `warning`.
|
||||||
- `FFMPEG_INPUT_SEEKABLE`: HTTP input seekable option, default `0`.
|
- `FFMPEG_INPUT_SEEKABLE`: HTTP input seekable option for unknown/live sources, default `0`; recognized recorded sources use automatic range seeking.
|
||||||
- `FFMPEG_HTTP_RECONNECT`: enable ffmpeg HTTP reconnect options for HTTP inputs, default `1`.
|
- `FFMPEG_HTTP_RECONNECT`: enable ffmpeg HTTP reconnect options for HTTP inputs, default `1`.
|
||||||
- `FFMPEG_HTTP_RECONNECT_DELAY_MAX`: max ffmpeg reconnect delay, default `2`.
|
- `FFMPEG_HTTP_RECONNECT_DELAY_MAX`: max ffmpeg reconnect delay, default `2`.
|
||||||
- `FFMPEG_HTTP_RECONNECT_MAX_RETRIES`: max ffmpeg reconnect retries, default `4`, applied only when the installed ffmpeg supports `reconnect_max_retries`.
|
- `FFMPEG_HTTP_RECONNECT_MAX_RETRIES`: max ffmpeg reconnect retries, default `4`, applied only when the installed ffmpeg supports `reconnect_max_retries`.
|
||||||
- `FFMPEG_HTTP_RECONNECT_ON_HTTP_ERROR`: HTTP status list for reconnect, default `5xx`.
|
- `FFMPEG_HTTP_RECONNECT_ON_HTTP_ERROR`: HTTP status list for reconnect, default `5xx`.
|
||||||
- `METADATA_PROBE_ENABLED`: probe session duration with ffprobe, default `1` except in `relay`, where default is `0`.
|
- `METADATA_PROBE_ENABLED`: probe session duration with ffprobe, default `1` except in `relay`, where default is `0`.
|
||||||
- `METADATA_PROBE_TIMEOUT_MS`: ffprobe duration timeout, default `4000`.
|
- `METADATA_PROBE_TIMEOUT_MS`: initial ffprobe duration timeout, default `4000`; recorded-source retry timeouts increase up to `30000`.
|
||||||
- `PLAYBACK_CONNECTION_MODE`: `split`, `relay`, or `single`.
|
- `PLAYBACK_CONNECTION_MODE`: `split`, `relay`, or `single`.
|
||||||
|
- `SINGLE_CONNECTION_HOSTS`: comma-separated subscription hosts that must use relay with no metadata probes or seeking; also matches their subdomains.
|
||||||
- `RECENT_URLS_PATH`: recent URL JSON path.
|
- `RECENT_URLS_PATH`: recent URL JSON path.
|
||||||
- `RECENT_URL_LIMIT`: recent URL count, default `12`.
|
- `RECENT_URL_LIMIT`: recent URL count, default `12`.
|
||||||
- `FAVORITES_PATH`: favorites JSON path.
|
- `FAVORITES_PATH`: favorites JSON path.
|
||||||
- `FAVORITES_LIMIT`: favorites count, default `50`.
|
- `FAVORITES_LIMIT`: favorites count, default `50`.
|
||||||
- `LOCAL_VIDEOS`: optional local video directory. When set, the UI shows `Play Local` and lists regular files under this directory recursively.
|
- `LOCAL_VIDEOS`: optional local video directory. When set, the UI shows `Play Local` and lists regular files under this directory recursively.
|
||||||
- `FRAME_TRANSPORT`: `websocket`, `webtransport`, or `auto`, default `websocket`.
|
|
||||||
- `WEBTRANSPORT_ENABLED`: enables the WebTransport listener, default true only when `FRAME_TRANSPORT` is `webtransport` or `auto`.
|
|
||||||
- `WEBTRANSPORT_HOST`: WebTransport UDP bind host, default `0.0.0.0`.
|
|
||||||
- `WEBTRANSPORT_PORT`: WebTransport UDP port, default `PORT + 1`.
|
|
||||||
- `WEBTRANSPORT_PUBLIC_HOST`: optional host advertised to browsers for WebTransport.
|
|
||||||
- `WEBTRANSPORT_PUBLIC_PORT`: port advertised to browsers for WebTransport, default `WEBTRANSPORT_PORT`.
|
|
||||||
- `WEBTRANSPORT_CERT_PATH`: optional WebTransport certificate path. If unset, the server generates a short-lived ECDSA cert under `data/`.
|
|
||||||
- `WEBTRANSPORT_KEY_PATH`: optional WebTransport private key path. Must be set together with `WEBTRANSPORT_CERT_PATH`.
|
|
||||||
- Browser WebTransport requires a secure context. Localhost is usually treated as secure, but plain HTTP over a LAN address may not expose `window.WebTransport`; the frontend must fall back to WebSocket in that case.
|
|
||||||
- `DEFAULT_FPS`: default frame rate, fallback `24`, clamped `1..30`.
|
- `DEFAULT_FPS`: default frame rate, fallback `24`, clamped `1..30`.
|
||||||
- `DEFAULT_FRAME_WIDTH`: default maximum frame width, fallback `960`, clamped `160..1920`.
|
- `DEFAULT_FRAME_WIDTH`: default maximum frame width, fallback `960`, clamped `160..1920`.
|
||||||
- `JPEG_QUALITY`: default JPEG quality, fallback `7`, clamped `2..18`; lower is better for ffmpeg `-q:v`.
|
- `JPEG_QUALITY`: default JPEG quality, fallback `7`, clamped `2..18`; lower is better for ffmpeg `-q:v`.
|
||||||
@@ -239,6 +234,7 @@ Basic validation:
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
node --check server/index.js
|
node --check server/index.js
|
||||||
|
npm test
|
||||||
docker compose -f docker-compose-example.yml config
|
docker compose -f docker-compose-example.yml config
|
||||||
docker build -t frame-stream-player .
|
docker build -t frame-stream-player .
|
||||||
```
|
```
|
||||||
|
|||||||
+2
-3
@@ -1,4 +1,4 @@
|
|||||||
FROM node:22-trixie-slim
|
FROM node:22-bookworm-slim
|
||||||
|
|
||||||
ENV NODE_ENV=production
|
ENV NODE_ENV=production
|
||||||
ENV PORT=3000
|
ENV PORT=3000
|
||||||
@@ -10,7 +10,7 @@ WORKDIR /app
|
|||||||
ADD https://api.github.com/repos/yt-dlp/yt-dlp/commits/master /tmp/yt-dlp-master.json
|
ADD https://api.github.com/repos/yt-dlp/yt-dlp/commits/master /tmp/yt-dlp-master.json
|
||||||
|
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get install -y --no-install-recommends ca-certificates ffmpeg openssl python3 python3-pip \
|
&& apt-get install -y --no-install-recommends ca-certificates ffmpeg python3 python3-pip \
|
||||||
&& python3 -m pip install --no-cache-dir --break-system-packages --upgrade "$YT_DLP_PIP_SPEC" \
|
&& python3 -m pip install --no-cache-dir --break-system-packages --upgrade "$YT_DLP_PIP_SPEC" \
|
||||||
&& yt-dlp --version \
|
&& yt-dlp --version \
|
||||||
&& apt-get purge -y --auto-remove python3-pip \
|
&& apt-get purge -y --auto-remove python3-pip \
|
||||||
@@ -30,7 +30,6 @@ RUN mkdir -p /app/data \
|
|||||||
USER node
|
USER node
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
EXPOSE 3001/udp
|
|
||||||
|
|
||||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
||||||
CMD node -e "fetch('http://127.0.0.1:' + (process.env.PORT || 3000) + '/api/health').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
|
CMD node -e "fetch('http://127.0.0.1:' + (process.env.PORT || 3000) + '/api/health').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
A small web app that plays a remote video stream without using browser video decoding. The server uses `ffmpeg` to decode the input URL into:
|
A small web app that plays a remote video stream without using browser video decoding. The server uses `ffmpeg` to decode the input URL into:
|
||||||
|
|
||||||
- an MP3 audio stream served to a normal `<audio>` element
|
- an MP3 audio stream served to a normal `<audio>` element
|
||||||
- timed JPEG image frames sent over WebSocket, or optionally WebTransport/QUIC, and painted onto a `<canvas>`
|
- timed JPEG image frames sent over a WebSocket and painted onto a `<canvas>`
|
||||||
|
|
||||||
This is meant for machines where image and audio decoding work but browser video decoding is unavailable or unreliable.
|
This is meant for machines where image and audio decoding work but browser video decoding is unavailable or unreliable.
|
||||||
|
|
||||||
@@ -14,11 +14,11 @@ npm install
|
|||||||
npm start
|
npm start
|
||||||
```
|
```
|
||||||
|
|
||||||
Open `http://localhost:3000`, paste a direct HTTP(S) stream URL, and click `Play`. Use `Queue` to add a URL to Recents without starting playback.
|
Open `http://localhost:3000`, paste a direct HTTP(S) stream URL, YouTube link, or X/Twitter video post URL, and click `Play`. Use `Queue` to add a URL to Recents without starting playback.
|
||||||
|
|
||||||
Set `LOCAL_VIDEOS=/path/to/videos` before starting the server to enable `Play Local`. The picker lets you navigate directories under that root, search the library, and play selected files through the server-side ffmpeg pipeline as MP3 audio plus JPEG canvas frames.
|
Set `LOCAL_VIDEOS=/path/to/videos` before starting the server to enable `Play Local`. The picker lets you navigate directories under that root, search the library, and play selected files through the server-side ffmpeg pipeline as MP3 audio plus JPEG canvas frames.
|
||||||
|
|
||||||
You need an `ffmpeg` binary with decoders for the stream's video and audio codecs. YouTube page links also need `yt-dlp`; Docker installs it automatically from the upstream master branch at image build time. If your stream is H.264 or HEVC, make sure your installed `ffmpeg` actually includes those decoders. You can point the app at different binaries with:
|
You need an `ffmpeg` binary with decoders for the stream's video and audio codecs. YouTube and X/Twitter page links also need `yt-dlp`; Docker installs it automatically from the upstream master branch at image build time. If your stream is H.264 or HEVC, make sure your installed `ffmpeg` actually includes those decoders. You can point the app at different binaries with:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
FFMPEG_PATH=/path/to/ffmpeg npm start
|
FFMPEG_PATH=/path/to/ffmpeg npm start
|
||||||
@@ -52,13 +52,13 @@ To use local playback in Docker, mount a host video directory into the container
|
|||||||
docker logs -f frame-stream-player
|
docker logs -f frame-stream-player
|
||||||
```
|
```
|
||||||
|
|
||||||
The app sets `FFMPEG_INPUT_SEEKABLE=0` by default so `ffmpeg` reads stream inputs sequentially and avoids extra HTTP range connections. If a specific VOD file requires seeking for metadata, set `FFMPEG_INPUT_SEEKABLE=-1` to restore ffmpeg's automatic behavior. For ffmpeg-owned HTTP inputs, reconnect handling is enabled by default with `FFMPEG_HTTP_RECONNECT=1`. `FFMPEG_HTTP_RECONNECT_MAX_RETRIES` is applied only when the installed `ffmpeg` supports that HTTP protocol option.
|
The app sets `FFMPEG_INPUT_SEEKABLE=0` by default so `ffmpeg` reads unknown/live stream inputs sequentially and avoids extra HTTP range connections. Recognized recorded files and sessions with a known duration automatically allow HTTP range requests from the start, so ffmpeg can jump to MP4 metadata instead of downloading the entire file first. For ffmpeg-owned HTTP inputs, reconnect handling is enabled by default with `FFMPEG_HTTP_RECONNECT=1`. `FFMPEG_HTTP_RECONNECT_MAX_RETRIES` is applied only when the installed `ffmpeg` supports that HTTP protocol option.
|
||||||
|
|
||||||
YouTube URLs are resolved server-side with `yt-dlp` before they enter the existing ffmpeg pipeline. Recents and favorites keep the original YouTube URL, while the short-lived playback session uses the resolved media URL and headers returned by `yt-dlp`. Tune the selected format with `YT_DLP_FORMAT` and the resolver timeout with `YT_DLP_TIMEOUT_MS`.
|
YouTube and X/Twitter URLs are resolved server-side with `yt-dlp`, with the app's Node runtime explicitly enabled for JavaScript challenges. Both `x.com` and `twitter.com` links work, including their `www`, `m`, and `mobile` variants. For posts with multiple videos, the first video plays unless the URL selects one with `/video/N`. The YouTube default selector is `bestvideo[protocol=https]+bestaudio[protocol=https]/bestvideo+bestaudio/best`. X/Twitter defaults to `best[protocol=https]/bestvideo[protocol=https]+bestaudio[protocol=https]`, preferring progressive MP4s because the source proxy does not rewrite relative HLS segment URLs. Audio and video can have separate URLs and request headers: each is proxied to its corresponding ffmpeg worker in `split` mode. Combined audio/video URLs also work. Finite, non-live duration metadata from yt-dlp enables seeking without another source probe. Recents and favorites keep the original page URL. Tune the selected format with `YT_DLP_FORMAT` and the resolver timeout with `YT_DLP_TIMEOUT_MS`.
|
||||||
|
|
||||||
JPEG frames are dropped when the browser frame transport falls behind instead of letting stale frames queue indefinitely. Tune the server-side backlog cap with `MAX_WS_BUFFER_BYTES`; the default is `2097152`.
|
Set `SINGLE_CONNECTION_HOSTS` to a comma-separated list of IPTV subscription hosts, for example `oceanstream.me`. Matching hosts and their subdomains always use `relay`, skip metadata probes, and disable seeking and recovery probes, even if the URL looks like a recorded file or the global mode is `split`. This protects subscriptions that permit only one upstream stream. Include any alternate provider hostnames in the list.
|
||||||
|
|
||||||
WebSocket remains the default frame transport. To try WebTransport/QUIC for frame delivery, set `FRAME_TRANSPORT=webtransport` or `FRAME_TRANSPORT=auto` and expose the WebTransport UDP port. The WebSocket path sends one binary frame packet per message. The WebTransport path uses one persistent server-to-client unidirectional stream with repeated length-prefixed frame packets. The default WebTransport port is `PORT + 1`; override it with `WEBTRANSPORT_PORT` and, when needed behind Docker or a proxy, `WEBTRANSPORT_PUBLIC_HOST` and `WEBTRANSPORT_PUBLIC_PORT`. The server generates a short-lived local ECDSA certificate under `data/` unless `WEBTRANSPORT_CERT_PATH` and `WEBTRANSPORT_KEY_PATH` are both set. Browser WebTransport requires a secure context: localhost is usually allowed, but plain HTTP over a LAN address may not expose the API. Browsers that cannot connect with WebTransport fall back to WebSocket.
|
JPEG frames are dropped when the browser WebSocket falls behind instead of letting stale frames queue indefinitely. Tune the server-side backlog cap with `MAX_WS_BUFFER_BYTES`; the default is `2097152`.
|
||||||
|
|
||||||
In single mode, audio output from `ffmpeg` is buffered before it is written to the browser so short HTTP backpressure pauses are less likely to stall frame generation. Tune the cap with `MAX_AUDIO_QUEUE_BYTES`; the default is `4194304`.
|
In single mode, audio output from `ffmpeg` is buffered before it is written to the browser so short HTTP backpressure pauses are less likely to stall frame generation. Tune the cap with `MAX_AUDIO_QUEUE_BYTES`; the default is `4194304`.
|
||||||
|
|
||||||
@@ -70,7 +70,9 @@ Available playback modes:
|
|||||||
- `relay`: One source connection from the backend, then the compressed input bytes are teed into separate audio and frame `ffmpeg` workers. This is intended for IPTV hosts that stop early or reject multiple active connections.
|
- `relay`: One source connection from the backend, then the compressed input bytes are teed into separate audio and frame `ffmpeg` workers. This is intended for IPTV hosts that stop early or reject multiple active connections.
|
||||||
- `single`: One source connection and one `ffmpeg` worker with both audio and frame outputs. This is the simplest one-connection fallback, but audio and frame delivery can affect each other.
|
- `single`: One source connection and one `ffmpeg` worker with both audio and frame outputs. This is the simplest one-connection fallback, but audio and frame delivery can affect each other.
|
||||||
|
|
||||||
Finite-duration streams are treated as recorded video and become seekable once metadata is available. Metadata probing is enabled by default except in `relay`, where `METADATA_PROBE_ENABLED=0` avoids extra upstream connections. Set `METADATA_PROBE_ENABLED=1` if you want recorded relay sessions to become seekable and switch to the seek-capable `split` path.
|
Finite-duration streams are treated as recorded video and become seekable once metadata is available. Metadata probing is enabled by default except in `relay`, where it is disabled for unknown/live URLs to avoid extra upstream connections. Local files, YouTube and X/Twitter URLs without resolved duration metadata, recognized media file extensions, and Jellyfin download/stream URLs are probed automatically. Set `METADATA_PROBE_ENABLED=1` to probe other URLs as well.
|
||||||
|
|
||||||
|
Recognized recorded files use `split` immediately when the configured mode is `relay`, even while duration detection is pending. Other relay sessions switch to `split` once a finite duration is known. Failed probes for likely recorded sources retry up to three times in the background, with increasing timeouts (by default 4, 12, then 30 seconds) and short delays. `METADATA_PROBE_TIMEOUT_MS` sets the initial timeout; each attempt is capped at 30 seconds. Seeking becomes available when the probe succeeds, without restarting playback.
|
||||||
|
|
||||||
Relay mode uses bounded per-worker input queues so one branch can briefly lag without immediately stalling the other. Tune the cap with `MAX_RELAY_BRANCH_QUEUE_BYTES`; the default is `8388608`.
|
Relay mode uses bounded per-worker input queues so one branch can briefly lag without immediately stalling the other. Tune the cap with `MAX_RELAY_BRANCH_QUEUE_BYTES`; the default is `8388608`.
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- "3000:3000"
|
- "3000:3000"
|
||||||
# Uncomment to try WebTransport frame delivery over QUIC/UDP.
|
|
||||||
# - "3001:3001/udp"
|
|
||||||
environment:
|
environment:
|
||||||
PORT: "3000"
|
PORT: "3000"
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
@@ -16,6 +14,9 @@ services:
|
|||||||
# relay: one upstream connection, separate audio/frame ffmpeg workers.
|
# relay: one upstream connection, separate audio/frame ffmpeg workers.
|
||||||
# single: one upstream connection and one ffmpeg worker; fallback only.
|
# single: one upstream connection and one ffmpeg worker; fallback only.
|
||||||
PLAYBACK_CONNECTION_MODE: relay
|
PLAYBACK_CONNECTION_MODE: relay
|
||||||
|
# Comma-separated subscription hosts (including subdomains): force relay,
|
||||||
|
# skip metadata probes, and disable seeks that would open extra streams.
|
||||||
|
# SINGLE_CONNECTION_HOSTS: oceanstream.me
|
||||||
FFMPEG_LOG_LEVEL: warning
|
FFMPEG_LOG_LEVEL: warning
|
||||||
FFMPEG_INPUT_SEEKABLE: "0"
|
FFMPEG_INPUT_SEEKABLE: "0"
|
||||||
FFMPEG_HTTP_RECONNECT: "1"
|
FFMPEG_HTTP_RECONNECT: "1"
|
||||||
@@ -33,10 +34,6 @@ services:
|
|||||||
MAX_WS_BUFFER_BYTES: "2097152"
|
MAX_WS_BUFFER_BYTES: "2097152"
|
||||||
MAX_AUDIO_QUEUE_BYTES: "4194304"
|
MAX_AUDIO_QUEUE_BYTES: "4194304"
|
||||||
MAX_RELAY_BRANCH_QUEUE_BYTES: "8388608"
|
MAX_RELAY_BRANCH_QUEUE_BYTES: "8388608"
|
||||||
# websocket: default. webtransport: try QUIC frames first. auto: try QUIC when available, fall back to WebSocket.
|
|
||||||
FRAME_TRANSPORT: websocket
|
|
||||||
WEBTRANSPORT_PORT: "3001"
|
|
||||||
WEBTRANSPORT_PUBLIC_PORT: "3001"
|
|
||||||
RECENT_URLS_PATH: /app/data/recent-urls.json
|
RECENT_URLS_PATH: /app/data/recent-urls.json
|
||||||
FAVORITES_PATH: /app/data/favorites.json
|
FAVORITES_PATH: /app/data/favorites.json
|
||||||
# Set this and mount a host directory below to enable Play Local.
|
# Set this and mount a host directory below to enable Play Local.
|
||||||
|
|||||||
Generated
+18
-98
@@ -9,89 +9,10 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@moq/web-transport": "^0.1.3",
|
|
||||||
"express": "^5.2.1",
|
"express": "^5.2.1",
|
||||||
"ws": "^8.20.0"
|
"ws": "^8.20.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@moq/web-transport": {
|
|
||||||
"version": "0.1.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/@moq/web-transport/-/web-transport-0.1.3.tgz",
|
|
||||||
"integrity": "sha512-hg3mKWUJaEwtJDGf8Ck62XP8ya+7wyMV/9ycV1A+M3iVly3xBOTPHpT8BrpOLomsBIB9iXQY6Z85MjknhUJW8w==",
|
|
||||||
"license": "(MIT OR Apache-2.0)",
|
|
||||||
"optionalDependencies": {
|
|
||||||
"@moq/web-transport-darwin-arm64": "0.1.3",
|
|
||||||
"@moq/web-transport-darwin-x64": "0.1.3",
|
|
||||||
"@moq/web-transport-linux-arm64-gnu": "0.1.3",
|
|
||||||
"@moq/web-transport-linux-x64-gnu": "0.1.3",
|
|
||||||
"@moq/web-transport-win32-x64-msvc": "0.1.3"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/@moq/web-transport-darwin-arm64": {
|
|
||||||
"version": "0.1.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/@moq/web-transport-darwin-arm64/-/web-transport-darwin-arm64-0.1.3.tgz",
|
|
||||||
"integrity": "sha512-MSKd2lmgjABS6A4CoaYA2m5XibJFUi3hzz/5TTrWAiRHU8t6WV5iHcPKLMblgXi+QSZ7iLlEL0tXzmN9u0qkJw==",
|
|
||||||
"cpu": [
|
|
||||||
"arm64"
|
|
||||||
],
|
|
||||||
"license": "(MIT OR Apache-2.0)",
|
|
||||||
"optional": true,
|
|
||||||
"os": [
|
|
||||||
"darwin"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"node_modules/@moq/web-transport-darwin-x64": {
|
|
||||||
"version": "0.1.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/@moq/web-transport-darwin-x64/-/web-transport-darwin-x64-0.1.3.tgz",
|
|
||||||
"integrity": "sha512-Kml/EUk8EWiKNkBRcLR8JEpMOqxEpDEHHaDlLd+ro84BY/MAgiJGiVKebGZI6QP2C2OalfAqZZQ0Eh8bB6w62Q==",
|
|
||||||
"cpu": [
|
|
||||||
"x64"
|
|
||||||
],
|
|
||||||
"license": "(MIT OR Apache-2.0)",
|
|
||||||
"optional": true,
|
|
||||||
"os": [
|
|
||||||
"darwin"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"node_modules/@moq/web-transport-linux-arm64-gnu": {
|
|
||||||
"version": "0.1.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/@moq/web-transport-linux-arm64-gnu/-/web-transport-linux-arm64-gnu-0.1.3.tgz",
|
|
||||||
"integrity": "sha512-Y3/Uko9N3X1b68hTgKVrLwpbMgtbvert0ADvIoziE1wczMcoORsys6dE9APH1jeZaXYgLAN5sVf7fdSMFI6HAQ==",
|
|
||||||
"cpu": [
|
|
||||||
"arm64"
|
|
||||||
],
|
|
||||||
"license": "(MIT OR Apache-2.0)",
|
|
||||||
"optional": true,
|
|
||||||
"os": [
|
|
||||||
"linux"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"node_modules/@moq/web-transport-linux-x64-gnu": {
|
|
||||||
"version": "0.1.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/@moq/web-transport-linux-x64-gnu/-/web-transport-linux-x64-gnu-0.1.3.tgz",
|
|
||||||
"integrity": "sha512-WVFRgrhrQUWEKM4uBgBwfz1vGmGOU7DtM38VqlW1+6+bEm6+a0jMJ/6HUBz1bEdzzKZsVcVoT66cZoymyvzjHA==",
|
|
||||||
"cpu": [
|
|
||||||
"x64"
|
|
||||||
],
|
|
||||||
"license": "(MIT OR Apache-2.0)",
|
|
||||||
"optional": true,
|
|
||||||
"os": [
|
|
||||||
"linux"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"node_modules/@moq/web-transport-win32-x64-msvc": {
|
|
||||||
"version": "0.1.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/@moq/web-transport-win32-x64-msvc/-/web-transport-win32-x64-msvc-0.1.3.tgz",
|
|
||||||
"integrity": "sha512-yPWBiujSW4bOByb1kfVyBd28MWoLpKneqel4k5KqW941Ofc6H/czegvtmDiFswx1KH531EjKljmuyilH+2g8Rw==",
|
|
||||||
"cpu": [
|
|
||||||
"x64"
|
|
||||||
],
|
|
||||||
"license": "(MIT OR Apache-2.0)",
|
|
||||||
"optional": true,
|
|
||||||
"os": [
|
|
||||||
"win32"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"node_modules/accepts": {
|
"node_modules/accepts": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz",
|
||||||
@@ -281,9 +202,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/es-object-atoms": {
|
"node_modules/es-object-atoms": {
|
||||||
"version": "1.1.2",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz",
|
||||||
"integrity": "sha512-HWcBoN6NileqtSydK2FqHbS/LoDd2pqrnQHLyJzBj4kOp/ky2MWMN694xOfkK8/SnUsW2DH7EfyVlydKCsm1Zw==",
|
"integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"es-errors": "^1.3.0"
|
"es-errors": "^1.3.0"
|
||||||
@@ -460,9 +381,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/hasown": {
|
"node_modules/hasown": {
|
||||||
"version": "2.0.4",
|
"version": "2.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.3.tgz",
|
||||||
"integrity": "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==",
|
"integrity": "sha512-ej4AhfhfL2Q2zpMmLo7U1Uv9+PyhIZpgQLGT1F9miIGmiCJIoCgSmczFdrc97mWT4kVY72KA+WnnhJ5pghSvSg==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"function-bind": "^1.1.2"
|
"function-bind": "^1.1.2"
|
||||||
@@ -664,13 +585,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/qs": {
|
"node_modules/qs": {
|
||||||
"version": "6.15.3",
|
"version": "6.15.1",
|
||||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.15.3.tgz",
|
"resolved": "https://registry.npmjs.org/qs/-/qs-6.15.1.tgz",
|
||||||
"integrity": "sha512-O9gl3zCl5h5blw1KGUzQKhA5oUXSl8rwUIM5o0S3nCXMliSvy5Dzx7/DJcI+SwgICv+IneSZwhBh1oSyEHA71A==",
|
"integrity": "sha512-6YHEFRL9mfgcAvql/XhwTvf5jKcOiiupt2FiJxHkiX1z4j7WL8J/jRHYLluORvc1XxB5rV20KoeK00gVJamspg==",
|
||||||
"license": "BSD-3-Clause",
|
"license": "BSD-3-Clause",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"es-define-property": "^1.0.1",
|
"side-channel": "^1.1.0"
|
||||||
"side-channel": "^1.1.1"
|
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.6"
|
"node": ">=0.6"
|
||||||
@@ -777,14 +697,14 @@
|
|||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
},
|
},
|
||||||
"node_modules/side-channel": {
|
"node_modules/side-channel": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz",
|
||||||
"integrity": "sha512-6x6dK6zJdpTzF4sQeNYxwtvBzf6Eg4GtlesS94HOvTudUeyK2WXAaIfmDgsyslYrRBeFIlsi54AYsFGUuhmvrQ==",
|
"integrity": "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"es-errors": "^1.3.0",
|
"es-errors": "^1.3.0",
|
||||||
"object-inspect": "^1.13.4",
|
"object-inspect": "^1.13.3",
|
||||||
"side-channel-list": "^1.0.1",
|
"side-channel-list": "^1.0.0",
|
||||||
"side-channel-map": "^1.0.1",
|
"side-channel-map": "^1.0.1",
|
||||||
"side-channel-weakmap": "^1.0.2"
|
"side-channel-weakmap": "^1.0.2"
|
||||||
},
|
},
|
||||||
@@ -905,9 +825,9 @@
|
|||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
},
|
},
|
||||||
"node_modules/ws": {
|
"node_modules/ws": {
|
||||||
"version": "8.21.0",
|
"version": "8.20.0",
|
||||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.21.0.tgz",
|
"resolved": "https://registry.npmjs.org/ws/-/ws-8.20.0.tgz",
|
||||||
"integrity": "sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==",
|
"integrity": "sha512-sAt8BhgNbzCtgGbt2OxmpuryO63ZoDk/sqaB/znQm94T4fCEsy/yV+7CdC1kJhOU9lboAEU7R3kquuycDoibVA==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10.0.0"
|
"node": ">=10.0.0"
|
||||||
|
|||||||
+1
-1
@@ -6,13 +6,13 @@
|
|||||||
"main": "server/index.js",
|
"main": "server/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server/index.js",
|
"start": "node server/index.js",
|
||||||
|
"test": "node --test",
|
||||||
"dev": "node --watch server/index.js"
|
"dev": "node --watch server/index.js"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@moq/web-transport": "^0.1.3",
|
|
||||||
"express": "^5.2.1",
|
"express": "^5.2.1",
|
||||||
"ws": "^8.20.0"
|
"ws": "^8.20.0"
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-313
@@ -68,17 +68,7 @@ const FRAME_UNPAINTED_PACKET_RESET_MS = 20000;
|
|||||||
const PLAYBACK_RESTART_COOLDOWN_MS = 15000;
|
const PLAYBACK_RESTART_COOLDOWN_MS = 15000;
|
||||||
const PLAYBACK_RESTART_DELAY_MS = 750;
|
const PLAYBACK_RESTART_DELAY_MS = 750;
|
||||||
const MIN_RECOVERY_RESUME_SECONDS = 2;
|
const MIN_RECOVERY_RESUME_SECONDS = 2;
|
||||||
const METADATA_REFRESH_ATTEMPTS = 20;
|
|
||||||
const METADATA_REFRESH_INTERVAL_MS = 650;
|
const METADATA_REFRESH_INTERVAL_MS = 650;
|
||||||
const WT_STREAM_CONTROL_TO_CLIENT = 1;
|
|
||||||
const WT_STREAM_FRAME = 2;
|
|
||||||
const WT_STREAM_CONTROL_TO_SERVER = 3;
|
|
||||||
const WT_FRAME_RECORD_HEADER_BYTES = 4;
|
|
||||||
const MAX_FRAME_PACKET_BYTES = 16 * 1024 * 1024;
|
|
||||||
const FRAME_CONNECTION_OPEN = 1;
|
|
||||||
const FRAME_CONNECTION_CLOSING = 2;
|
|
||||||
const FRAME_CONNECTION_CLOSED = 3;
|
|
||||||
const textEncoder = new TextEncoder();
|
|
||||||
|
|
||||||
const state = {
|
const state = {
|
||||||
generation: 0,
|
generation: 0,
|
||||||
@@ -496,7 +486,6 @@ function startSession(session) {
|
|||||||
|
|
||||||
connectPlaybackStreams();
|
connectPlaybackStreams();
|
||||||
startFrameWatchdog();
|
startFrameWatchdog();
|
||||||
void refreshSessionMetadata(session.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function connectPlaybackStreams() {
|
function connectPlaybackStreams() {
|
||||||
@@ -517,26 +506,10 @@ function connectPlaybackStreams() {
|
|||||||
state.websocket = null;
|
state.websocket = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void openPlaybackFrameConnection(session, streamGeneration);
|
const websocketProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||||
}
|
const websocketUrl = `${websocketProtocol}//${window.location.host}/frames/${session.id}?g=${session.seekGeneration ?? 0}`;
|
||||||
|
const websocket = new WebSocket(websocketUrl);
|
||||||
async function openPlaybackFrameConnection(session, streamGeneration) {
|
websocket.binaryType = 'arraybuffer';
|
||||||
let websocket;
|
|
||||||
|
|
||||||
try {
|
|
||||||
websocket = await createFrameConnection(session);
|
|
||||||
} catch (error) {
|
|
||||||
console.warn('Frame transport failed', error);
|
|
||||||
showPlayerMessage('Stream failed');
|
|
||||||
setControlsVisible(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (streamGeneration !== state.streamGeneration || state.session?.id !== session.id) {
|
|
||||||
websocket.close(1000, 'stale frame connection');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
state.websocket = websocket;
|
state.websocket = websocket;
|
||||||
|
|
||||||
websocket.addEventListener('message', (event) => {
|
websocket.addEventListener('message', (event) => {
|
||||||
@@ -571,278 +544,7 @@ async function openPlaybackFrameConnection(session, streamGeneration) {
|
|||||||
elements.audio.load();
|
elements.audio.load();
|
||||||
void playAudio();
|
void playAudio();
|
||||||
startRenderLoop();
|
startRenderLoop();
|
||||||
}
|
void refreshSessionMetadata(session.id);
|
||||||
|
|
||||||
async function createFrameConnection(session) {
|
|
||||||
const preferred = session.frameTransport?.preferred ?? 'websocket';
|
|
||||||
const webTransportConfig = session.frameTransport?.webTransport;
|
|
||||||
const shouldTryWebTransport = (preferred === 'webtransport' || preferred === 'auto') && webTransportConfig && 'WebTransport' in window;
|
|
||||||
|
|
||||||
if (shouldTryWebTransport) {
|
|
||||||
try {
|
|
||||||
return await createWebTransportFrameConnection(session, webTransportConfig);
|
|
||||||
} catch (error) {
|
|
||||||
console.warn('WebTransport frame connection failed; falling back to WebSocket', error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return createWebSocketFrameConnection(session);
|
|
||||||
}
|
|
||||||
|
|
||||||
function createWebSocketFrameConnection(session) {
|
|
||||||
const websocketProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
||||||
const websocketUrl = `${websocketProtocol}//${window.location.host}/frames/${session.id}?g=${session.seekGeneration ?? 0}`;
|
|
||||||
const websocket = new WebSocket(websocketUrl);
|
|
||||||
websocket.binaryType = 'arraybuffer';
|
|
||||||
return websocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createWebTransportFrameConnection(session, config) {
|
|
||||||
const url = buildWebTransportFrameUrl(session, config);
|
|
||||||
const options = {
|
|
||||||
allowPooling: false,
|
|
||||||
congestionControl: 'low-latency',
|
|
||||||
};
|
|
||||||
|
|
||||||
if (config.certificateHash) {
|
|
||||||
options.serverCertificateHashes = [{
|
|
||||||
algorithm: config.certificateHashAlgorithm ?? 'sha-256',
|
|
||||||
value: base64ToArrayBuffer(config.certificateHash),
|
|
||||||
}];
|
|
||||||
}
|
|
||||||
|
|
||||||
const transport = new WebTransport(url, options);
|
|
||||||
await transport.ready;
|
|
||||||
return createWebTransportFrameConnectionAdapter(transport);
|
|
||||||
}
|
|
||||||
|
|
||||||
function createWebTransportFrameConnectionAdapter(transport) {
|
|
||||||
const events = new EventTarget();
|
|
||||||
let readyState = FRAME_CONNECTION_OPEN;
|
|
||||||
let clientControlWriterPromise = null;
|
|
||||||
let clientControlWrite = Promise.resolve();
|
|
||||||
|
|
||||||
const connection = {
|
|
||||||
get readyState() {
|
|
||||||
return readyState;
|
|
||||||
},
|
|
||||||
addEventListener(type, listener, options) {
|
|
||||||
events.addEventListener(type, listener, options);
|
|
||||||
},
|
|
||||||
removeEventListener(type, listener, options) {
|
|
||||||
events.removeEventListener(type, listener, options);
|
|
||||||
},
|
|
||||||
send(data) {
|
|
||||||
if (readyState !== FRAME_CONNECTION_OPEN) {
|
|
||||||
throw new Error('Frame connection is closed.');
|
|
||||||
}
|
|
||||||
|
|
||||||
clientControlWriterPromise ??= openWebTransportClientControlWriter(transport);
|
|
||||||
const payload = textEncoder.encode(`${String(data)}\n`);
|
|
||||||
clientControlWrite = clientControlWrite.then(async () => {
|
|
||||||
const writer = await clientControlWriterPromise;
|
|
||||||
await writer.write(payload);
|
|
||||||
}).catch((error) => {
|
|
||||||
dispatchFrameConnectionError(events, error);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
close(code = 1000, reason = '') {
|
|
||||||
if (readyState === FRAME_CONNECTION_CLOSING || readyState === FRAME_CONNECTION_CLOSED) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
readyState = FRAME_CONNECTION_CLOSING;
|
|
||||||
transport.close({ closeCode: code, reason });
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
void readWebTransportIncomingStreams(transport, events);
|
|
||||||
transport.closed.then(() => {
|
|
||||||
readyState = FRAME_CONNECTION_CLOSED;
|
|
||||||
events.dispatchEvent(new Event('close'));
|
|
||||||
}).catch(() => {
|
|
||||||
readyState = FRAME_CONNECTION_CLOSED;
|
|
||||||
events.dispatchEvent(new Event('close'));
|
|
||||||
});
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function openWebTransportClientControlWriter(transport) {
|
|
||||||
const stream = await transport.createUnidirectionalStream();
|
|
||||||
const writer = stream.getWriter();
|
|
||||||
await writer.write(new Uint8Array([WT_STREAM_CONTROL_TO_SERVER]));
|
|
||||||
return writer;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function readWebTransportIncomingStreams(transport, events) {
|
|
||||||
const reader = transport.incomingUnidirectionalStreams.getReader();
|
|
||||||
|
|
||||||
try {
|
|
||||||
while (true) {
|
|
||||||
const { value: stream, done } = await reader.read();
|
|
||||||
|
|
||||||
if (done) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleWebTransportIncomingStream(stream, events);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
dispatchFrameConnectionError(events, error);
|
|
||||||
} finally {
|
|
||||||
reader.releaseLock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleWebTransportIncomingStream(stream, events) {
|
|
||||||
const reader = stream.getReader();
|
|
||||||
|
|
||||||
try {
|
|
||||||
const first = await reader.read();
|
|
||||||
|
|
||||||
if (first.done || !first.value || first.value.byteLength === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const chunk = first.value;
|
|
||||||
const streamType = chunk[0];
|
|
||||||
const initialPayload = chunk.subarray(1);
|
|
||||||
|
|
||||||
if (streamType === WT_STREAM_CONTROL_TO_CLIENT) {
|
|
||||||
await readWebTransportControlStream(reader, initialPayload, events);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (streamType === WT_STREAM_FRAME) {
|
|
||||||
await readWebTransportFrameStream(reader, initialPayload, events);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
dispatchFrameConnectionError(events, error);
|
|
||||||
} finally {
|
|
||||||
reader.releaseLock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function readWebTransportControlStream(reader, initialPayload, events) {
|
|
||||||
const decoder = new TextDecoder();
|
|
||||||
let pending = initialPayload.byteLength > 0 ? decoder.decode(initialPayload, { stream: true }) : '';
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
pending = dispatchWebTransportControlLines(pending, events);
|
|
||||||
const { value, done } = await reader.read();
|
|
||||||
|
|
||||||
if (done) {
|
|
||||||
pending += decoder.decode();
|
|
||||||
dispatchWebTransportControlLines(`${pending}\n`, events);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pending += decoder.decode(value, { stream: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function readWebTransportFrameStream(reader, initialPayload, events) {
|
|
||||||
let pending = new Uint8Array(0);
|
|
||||||
|
|
||||||
if (initialPayload.byteLength > 0) {
|
|
||||||
pending = appendWebTransportFrameChunk(pending, initialPayload);
|
|
||||||
pending = dispatchWebTransportFrameRecords(pending, events);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
const { value, done } = await reader.read();
|
|
||||||
|
|
||||||
if (done) {
|
|
||||||
if (pending.byteLength > 0) {
|
|
||||||
throw new Error('WebTransport frame stream ended with a partial frame record.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pending = appendWebTransportFrameChunk(pending, value);
|
|
||||||
pending = dispatchWebTransportFrameRecords(pending, events);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function appendWebTransportFrameChunk(pending, chunk) {
|
|
||||||
if (pending.byteLength === 0) {
|
|
||||||
return chunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
const merged = new Uint8Array(pending.byteLength + chunk.byteLength);
|
|
||||||
merged.set(pending, 0);
|
|
||||||
merged.set(chunk, pending.byteLength);
|
|
||||||
return merged;
|
|
||||||
}
|
|
||||||
|
|
||||||
function dispatchWebTransportFrameRecords(buffer, events) {
|
|
||||||
let offset = 0;
|
|
||||||
|
|
||||||
while (buffer.byteLength - offset >= WT_FRAME_RECORD_HEADER_BYTES) {
|
|
||||||
const packetLength = new DataView(buffer.buffer, buffer.byteOffset + offset, WT_FRAME_RECORD_HEADER_BYTES).getUint32(0, true);
|
|
||||||
|
|
||||||
if (packetLength <= 8 || packetLength > MAX_FRAME_PACKET_BYTES) {
|
|
||||||
throw new Error(`Invalid WebTransport frame packet length: ${packetLength}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const recordBytes = WT_FRAME_RECORD_HEADER_BYTES + packetLength;
|
|
||||||
|
|
||||||
if (buffer.byteLength - offset < recordBytes) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const packet = buffer.slice(offset + WT_FRAME_RECORD_HEADER_BYTES, offset + recordBytes);
|
|
||||||
events.dispatchEvent(new MessageEvent('message', { data: packet.buffer }));
|
|
||||||
offset += recordBytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
return offset === buffer.byteLength ? new Uint8Array(0) : buffer.slice(offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
function dispatchWebTransportControlLines(text, events) {
|
|
||||||
const lines = text.split(/\n/);
|
|
||||||
const pending = lines.pop() ?? '';
|
|
||||||
|
|
||||||
for (const line of lines) {
|
|
||||||
if (line.trim()) {
|
|
||||||
events.dispatchEvent(new MessageEvent('message', { data: line }));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return pending;
|
|
||||||
}
|
|
||||||
|
|
||||||
function dispatchFrameConnectionError(events, error) {
|
|
||||||
if (isExpectedWebTransportCloseError(error)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.warn('Frame connection error', error);
|
|
||||||
events.dispatchEvent(new Event('error'));
|
|
||||||
}
|
|
||||||
|
|
||||||
function isExpectedWebTransportCloseError(error) {
|
|
||||||
return error && String(error.message ?? error).toLowerCase().includes('session is closed');
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildWebTransportFrameUrl(session, config) {
|
|
||||||
const host = config.host || window.location.hostname;
|
|
||||||
const port = config.port || window.location.port;
|
|
||||||
const urlHost = host.includes(':') && !host.startsWith('[') ? `[${host}]` : host;
|
|
||||||
return `https://${urlHost}:${port}/frames/${session.id}?g=${session.seekGeneration ?? 0}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function base64ToArrayBuffer(value) {
|
|
||||||
const binary = atob(value);
|
|
||||||
const bytes = new Uint8Array(binary.length);
|
|
||||||
|
|
||||||
for (let index = 0; index < binary.length; index += 1) {
|
|
||||||
bytes[index] = binary.charCodeAt(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bytes.buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function playAudio() {
|
async function playAudio() {
|
||||||
@@ -1653,23 +1355,25 @@ async function seekTo(value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function refreshSessionMetadata(sessionId) {
|
async function refreshSessionMetadata(sessionId) {
|
||||||
const generation = state.generation;
|
const streamGeneration = state.streamGeneration;
|
||||||
|
|
||||||
for (let attempt = 0; attempt < METADATA_REFRESH_ATTEMPTS; attempt += 1) {
|
|
||||||
if (generation !== state.generation || state.session?.id !== sessionId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// The server bounds probe retries. Keep polling for their full lifetime,
|
||||||
|
// including after reconnects, instead of giving up before a slow probe ends.
|
||||||
|
while (streamGeneration === state.streamGeneration && state.session?.id === sessionId && state.session.metadataStatus === 'pending') {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/session/${encodeURIComponent(sessionId)}`, { cache: 'no-store' });
|
const response = await fetch(`/api/session/${encodeURIComponent(sessionId)}`, { cache: 'no-store' });
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
if (response.status === 404) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new Error('Metadata temporarily unavailable.');
|
||||||
|
}
|
||||||
|
|
||||||
const payload = await response.json();
|
const payload = await response.json();
|
||||||
|
|
||||||
if (generation !== state.generation || state.session?.id !== sessionId) {
|
if (streamGeneration !== state.streamGeneration || state.session?.id !== sessionId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1680,7 +1384,7 @@ async function refreshSessionMetadata(sessionId) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
return;
|
// A transient network failure should not leave seeking disabled either.
|
||||||
}
|
}
|
||||||
|
|
||||||
await delay(METADATA_REFRESH_INTERVAL_MS);
|
await delay(METADATA_REFRESH_INTERVAL_MS);
|
||||||
@@ -1688,11 +1392,14 @@ async function refreshSessionMetadata(sessionId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updateSessionMetadata(payload) {
|
function updateSessionMetadata(payload) {
|
||||||
if ('duration' in payload) {
|
// A delayed WebSocket ready message can still contain the initial null
|
||||||
|
// duration after polling has already received the completed probe result.
|
||||||
|
const hasDuration = normalizeDuration(payload.duration) !== null;
|
||||||
|
if ('duration' in payload && (hasDuration || state.duration === null)) {
|
||||||
state.duration = normalizeDuration(payload.duration);
|
state.duration = normalizeDuration(payload.duration);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('seekable' in payload) {
|
if ('seekable' in payload && (hasDuration || state.duration === null)) {
|
||||||
state.seekable = Boolean(payload.seekable);
|
state.seekable = Boolean(payload.seekable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+160
-500
@@ -1,21 +1,17 @@
|
|||||||
import { execFile, spawn } from 'node:child_process';
|
import { spawn } from 'node:child_process';
|
||||||
import { createHash, randomUUID, X509Certificate } from 'node:crypto';
|
import { randomUUID } from 'node:crypto';
|
||||||
import { EventEmitter } from 'node:events';
|
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import { createServer } from 'node:http';
|
import { createServer } from 'node:http';
|
||||||
import { createRequire } from 'node:module';
|
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { Readable } from 'node:stream';
|
import { Readable } from 'node:stream';
|
||||||
|
import { setTimeout as delay } from 'node:timers/promises';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { promisify } from 'node:util';
|
|
||||||
|
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { WebSocket, WebSocketServer } from 'ws';
|
import { WebSocket, WebSocketServer } from 'ws';
|
||||||
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
const publicDir = path.join(__dirname, '..', 'public');
|
const publicDir = path.join(__dirname, '..', 'public');
|
||||||
const requireFromHere = createRequire(import.meta.url);
|
|
||||||
const execFileAsync = promisify(execFile);
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const server = createServer(app);
|
const server = createServer(app);
|
||||||
@@ -25,7 +21,10 @@ const PORT = Number(process.env.PORT ?? 3000);
|
|||||||
const FFMPEG_PATH = process.env.FFMPEG_PATH ?? 'ffmpeg';
|
const FFMPEG_PATH = process.env.FFMPEG_PATH ?? 'ffmpeg';
|
||||||
const FFPROBE_PATH = process.env.FFPROBE_PATH ?? 'ffprobe';
|
const FFPROBE_PATH = process.env.FFPROBE_PATH ?? 'ffprobe';
|
||||||
const YT_DLP_PATH = process.env.YT_DLP_PATH ?? 'yt-dlp';
|
const YT_DLP_PATH = process.env.YT_DLP_PATH ?? 'yt-dlp';
|
||||||
const YT_DLP_FORMAT = process.env.YT_DLP_FORMAT ?? 'best[ext=mp4][vcodec!=none][acodec!=none]/best[vcodec!=none][acodec!=none]/best';
|
const YT_DLP_FORMAT = process.env.YT_DLP_FORMAT ?? 'bestvideo[protocol=https]+bestaudio[protocol=https]/bestvideo+bestaudio/best';
|
||||||
|
// X's HLS playlists contain relative segment URLs, which the source proxy does
|
||||||
|
// not rewrite. Prefer its progressive MP4s; keep separate HTTPS inputs usable.
|
||||||
|
const YT_DLP_TWITTER_FORMAT = process.env.YT_DLP_FORMAT ?? 'best[protocol=https]/bestvideo[protocol=https]+bestaudio[protocol=https]';
|
||||||
const YT_DLP_TIMEOUT_MS = clampInteger(process.env.YT_DLP_TIMEOUT_MS, 45 * 1000, 5 * 1000, 120 * 1000);
|
const YT_DLP_TIMEOUT_MS = clampInteger(process.env.YT_DLP_TIMEOUT_MS, 45 * 1000, 5 * 1000, 120 * 1000);
|
||||||
const FFMPEG_LOG_LEVEL = process.env.FFMPEG_LOG_LEVEL ?? 'warning';
|
const FFMPEG_LOG_LEVEL = process.env.FFMPEG_LOG_LEVEL ?? 'warning';
|
||||||
const FFMPEG_INPUT_SEEKABLE = process.env.FFMPEG_INPUT_SEEKABLE ?? '0';
|
const FFMPEG_INPUT_SEEKABLE = process.env.FFMPEG_INPUT_SEEKABLE ?? '0';
|
||||||
@@ -35,22 +34,16 @@ const FFMPEG_HTTP_RECONNECT_MAX_RETRIES = clampInteger(process.env.FFMPEG_HTTP_R
|
|||||||
const FFMPEG_HTTP_RECONNECT_ON_HTTP_ERROR = process.env.FFMPEG_HTTP_RECONNECT_ON_HTTP_ERROR ?? '5xx';
|
const FFMPEG_HTTP_RECONNECT_ON_HTTP_ERROR = process.env.FFMPEG_HTTP_RECONNECT_ON_HTTP_ERROR ?? '5xx';
|
||||||
const FFMPEG_CAPABILITY_TIMEOUT_MS = 3000;
|
const FFMPEG_CAPABILITY_TIMEOUT_MS = 3000;
|
||||||
const PLAYBACK_CONNECTION_MODE = parsePlaybackConnectionMode(process.env.PLAYBACK_CONNECTION_MODE ?? process.env.PLAYBACK_MODE);
|
const PLAYBACK_CONNECTION_MODE = parsePlaybackConnectionMode(process.env.PLAYBACK_CONNECTION_MODE ?? process.env.PLAYBACK_MODE);
|
||||||
const FRAME_TRANSPORT = parseFrameTransport(process.env.FRAME_TRANSPORT);
|
const SINGLE_CONNECTION_HOSTS = (process.env.SINGLE_CONNECTION_HOSTS ?? '').split(',')
|
||||||
|
.map((host) => host.trim().toLowerCase().replace(/\.$/, '')).filter(Boolean);
|
||||||
const METADATA_PROBE_ENABLED = parseBoolean(process.env.METADATA_PROBE_ENABLED, PLAYBACK_CONNECTION_MODE !== 'relay');
|
const METADATA_PROBE_ENABLED = parseBoolean(process.env.METADATA_PROBE_ENABLED, PLAYBACK_CONNECTION_MODE !== 'relay');
|
||||||
const METADATA_PROBE_TIMEOUT_MS = clampInteger(process.env.METADATA_PROBE_TIMEOUT_MS, 4 * 1000, 1000, 30 * 1000);
|
const METADATA_PROBE_TIMEOUT_MS = clampInteger(process.env.METADATA_PROBE_TIMEOUT_MS, 4 * 1000, 1000, 30 * 1000);
|
||||||
|
const METADATA_PROBE_MAX_ATTEMPTS = 3;
|
||||||
const RECENT_URLS_PATH = process.env.RECENT_URLS_PATH ?? path.join(__dirname, '..', 'data', 'recent-urls.json');
|
const RECENT_URLS_PATH = process.env.RECENT_URLS_PATH ?? path.join(__dirname, '..', 'data', 'recent-urls.json');
|
||||||
const RECENT_URL_LIMIT = clampInteger(process.env.RECENT_URL_LIMIT, 12, 1, 50);
|
const RECENT_URL_LIMIT = clampInteger(process.env.RECENT_URL_LIMIT, 12, 1, 50);
|
||||||
const FAVORITES_PATH = process.env.FAVORITES_PATH ?? path.join(__dirname, '..', 'data', 'favorites.json');
|
const FAVORITES_PATH = process.env.FAVORITES_PATH ?? path.join(__dirname, '..', 'data', 'favorites.json');
|
||||||
const FAVORITES_LIMIT = clampInteger(process.env.FAVORITES_LIMIT, 50, 1, 200);
|
const FAVORITES_LIMIT = clampInteger(process.env.FAVORITES_LIMIT, 50, 1, 200);
|
||||||
const LOCAL_VIDEOS_ROOT = parseLocalVideosRoot(process.env.LOCAL_VIDEOS);
|
const LOCAL_VIDEOS_ROOT = parseLocalVideosRoot(process.env.LOCAL_VIDEOS);
|
||||||
const WEBTRANSPORT_ENABLED = parseBoolean(process.env.WEBTRANSPORT_ENABLED, FRAME_TRANSPORT === 'webtransport' || FRAME_TRANSPORT === 'auto');
|
|
||||||
const WEBTRANSPORT_HOST = process.env.WEBTRANSPORT_HOST || '0.0.0.0';
|
|
||||||
const WEBTRANSPORT_PORT = clampInteger(process.env.WEBTRANSPORT_PORT, Math.min(PORT + 1, 65535), 1, 65535);
|
|
||||||
const WEBTRANSPORT_PUBLIC_HOST = process.env.WEBTRANSPORT_PUBLIC_HOST || '';
|
|
||||||
const WEBTRANSPORT_PUBLIC_PORT = clampInteger(process.env.WEBTRANSPORT_PUBLIC_PORT, WEBTRANSPORT_PORT, 1, 65535);
|
|
||||||
const WEBTRANSPORT_CERT_PATH = process.env.WEBTRANSPORT_CERT_PATH ?? path.join(__dirname, '..', 'data', 'webtransport-cert.pem');
|
|
||||||
const WEBTRANSPORT_KEY_PATH = process.env.WEBTRANSPORT_KEY_PATH ?? path.join(__dirname, '..', 'data', 'webtransport-key.pem');
|
|
||||||
const WEBTRANSPORT_CERT_RENEW_MS = 12 * 24 * 60 * 60 * 1000;
|
|
||||||
const SESSION_TTL_MS = 60 * 60 * 1000;
|
const SESSION_TTL_MS = 60 * 60 * 1000;
|
||||||
const PLAYBACK_READY_TIMEOUT_MS = 15 * 1000;
|
const PLAYBACK_READY_TIMEOUT_MS = 15 * 1000;
|
||||||
const CLIENT_CLOCK_FRAME_LATE_GRACE_SECONDS = 0.25;
|
const CLIENT_CLOCK_FRAME_LATE_GRACE_SECONDS = 0.25;
|
||||||
@@ -62,10 +55,6 @@ const MAX_YT_DLP_OUTPUT_BYTES = 8 * 1024 * 1024;
|
|||||||
const RELAY_BRANCH_PAUSE_BYTES = Math.floor(MAX_RELAY_BRANCH_QUEUE_BYTES / 2);
|
const RELAY_BRANCH_PAUSE_BYTES = Math.floor(MAX_RELAY_BRANCH_QUEUE_BYTES / 2);
|
||||||
const JPEG_SOI = Buffer.from([0xff, 0xd8]);
|
const JPEG_SOI = Buffer.from([0xff, 0xd8]);
|
||||||
const JPEG_EOI = Buffer.from([0xff, 0xd9]);
|
const JPEG_EOI = Buffer.from([0xff, 0xd9]);
|
||||||
const WT_STREAM_CONTROL_TO_CLIENT = 1;
|
|
||||||
const WT_STREAM_FRAME = 2;
|
|
||||||
const WT_STREAM_CONTROL_TO_SERVER = 3;
|
|
||||||
const WT_FRAME_RECORD_HEADER_BYTES = 4;
|
|
||||||
const BEST_EFFORT_RESUME_MAX_SECONDS = 30 * 24 * 60 * 60;
|
const BEST_EFFORT_RESUME_MAX_SECONDS = 30 * 24 * 60 * 60;
|
||||||
const RECORDED_MEDIA_EXTENSIONS = new Set([
|
const RECORDED_MEDIA_EXTENSIONS = new Set([
|
||||||
'.avi',
|
'.avi',
|
||||||
@@ -99,23 +88,13 @@ let favorites = [];
|
|||||||
let favoritesWrite = Promise.resolve();
|
let favoritesWrite = Promise.resolve();
|
||||||
let ffmpegSupportsReconnectMaxRetries = false;
|
let ffmpegSupportsReconnectMaxRetries = false;
|
||||||
let localVideosRealRootPromise = null;
|
let localVideosRealRootPromise = null;
|
||||||
let webTransportServer = null;
|
|
||||||
let webTransportClientConfig = null;
|
|
||||||
|
|
||||||
app.disable('x-powered-by');
|
app.disable('x-powered-by');
|
||||||
app.use(express.json({ limit: '256kb' }));
|
app.use(express.json({ limit: '256kb' }));
|
||||||
app.use(express.static(publicDir));
|
app.use(express.static(publicDir));
|
||||||
|
|
||||||
app.get('/api/health', (_request, response) => {
|
app.get('/api/health', (_request, response) => {
|
||||||
response.json({
|
response.json({ ok: true, ffmpeg: FFMPEG_PATH, playbackConnectionMode: PLAYBACK_CONNECTION_MODE });
|
||||||
ok: true,
|
|
||||||
ffmpeg: FFMPEG_PATH,
|
|
||||||
playbackConnectionMode: PLAYBACK_CONNECTION_MODE,
|
|
||||||
frameTransport: FRAME_TRANSPORT,
|
|
||||||
webTransport: webTransportClientConfig
|
|
||||||
? { enabled: true, port: webTransportClientConfig.port }
|
|
||||||
: { enabled: false },
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/api/recent-urls', (_request, response) => {
|
app.get('/api/recent-urls', (_request, response) => {
|
||||||
@@ -230,20 +209,23 @@ app.post('/api/session', async (request, response) => {
|
|||||||
|
|
||||||
const options = parsePlaybackOptions(request.body);
|
const options = parsePlaybackOptions(request.body);
|
||||||
const id = randomUUID();
|
const id = randomUUID();
|
||||||
const shouldProbeMetadata = METADATA_PROBE_ENABLED || canBestEffortResumeWithoutDuration({
|
const duration = Number.isFinite(source.duration) && source.duration > 0 ? source.duration : null;
|
||||||
|
const shouldProbeMetadata = !source.singleConnection && duration === null && (METADATA_PROBE_ENABLED || canBestEffortResumeWithoutDuration({
|
||||||
sourceKind: source.kind,
|
sourceKind: source.kind,
|
||||||
originalUrl: source.originalUrl,
|
originalUrl: source.originalUrl,
|
||||||
url: source.url,
|
url: source.url,
|
||||||
});
|
}));
|
||||||
const metadataStatus = shouldProbeMetadata ? 'pending' : 'disabled';
|
const metadataStatus = duration !== null ? 'ready' : shouldProbeMetadata ? 'pending' : 'disabled';
|
||||||
const session = {
|
const session = {
|
||||||
id,
|
id,
|
||||||
url: source.url,
|
url: source.url,
|
||||||
originalUrl: source.originalUrl,
|
originalUrl: source.originalUrl,
|
||||||
sourceKind: source.kind,
|
sourceKind: source.kind,
|
||||||
sourceHeaders: source.headers,
|
sourceHeaders: source.headers,
|
||||||
|
audioSource: source.audioSource ?? null,
|
||||||
|
singleConnection: source.singleConnection ?? false,
|
||||||
options,
|
options,
|
||||||
duration: null,
|
duration,
|
||||||
metadataStatus,
|
metadataStatus,
|
||||||
metadataProbePromise: null,
|
metadataProbePromise: null,
|
||||||
seekSeconds: 0,
|
seekSeconds: 0,
|
||||||
@@ -255,7 +237,7 @@ app.post('/api/session', async (request, response) => {
|
|||||||
|
|
||||||
sessions.set(id, session);
|
sessions.set(id, session);
|
||||||
|
|
||||||
logInfo(`session created id=${shortId(id)} source=${source.kind} mode=${getSessionPlaybackConnectionMode(session)} fps=${options.fps} width=${options.width} quality=${options.quality}`);
|
logInfo(`session created id=${shortId(id)} source=${source.kind} mode=${getSessionPlaybackConnectionMode(session)} metadata=${metadataStatus} fps=${options.fps} width=${options.width} quality=${options.quality}`);
|
||||||
|
|
||||||
if (shouldProbeMetadata) {
|
if (shouldProbeMetadata) {
|
||||||
startSessionMetadataProbe(session);
|
startSessionMetadataProbe(session);
|
||||||
@@ -300,7 +282,7 @@ app.post('/api/session/:sessionId/seek', async (request, response) => {
|
|||||||
|
|
||||||
const allowBestEffort = request.body?.allowBestEffort === true;
|
const allowBestEffort = request.body?.allowBestEffort === true;
|
||||||
|
|
||||||
if (!isSessionSeekable(session) && allowBestEffort && session.metadataStatus !== 'unavailable') {
|
if (!isSessionSeekable(session) && allowBestEffort && canBestEffortResumeWithoutDuration(session) && session.metadataStatus !== 'unavailable') {
|
||||||
await ensureSessionDuration(session);
|
await ensureSessionDuration(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,7 +344,9 @@ app.all('/_source/:token', async (request, response) => {
|
|||||||
response.on('close', cleanup);
|
response.on('close', cleanup);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const headers = createSourceRequestHeaders(session);
|
const input = source.kind === 'audio' && session.audioSource
|
||||||
|
? session.audioSource : { url: session.url, headers: session.sourceHeaders };
|
||||||
|
const headers = createSourceRequestHeaders(input.headers);
|
||||||
const range = request.get('range');
|
const range = request.get('range');
|
||||||
|
|
||||||
if (range) {
|
if (range) {
|
||||||
@@ -371,7 +355,7 @@ app.all('/_source/:token', async (request, response) => {
|
|||||||
|
|
||||||
headers.set('accept-encoding', 'identity');
|
headers.set('accept-encoding', 'identity');
|
||||||
|
|
||||||
const upstream = await fetch(session.url, {
|
const upstream = await fetch(input.url, {
|
||||||
method: request.method === 'HEAD' ? 'HEAD' : 'GET',
|
method: request.method === 'HEAD' ? 'HEAD' : 'GET',
|
||||||
headers,
|
headers,
|
||||||
redirect: 'follow',
|
redirect: 'follow',
|
||||||
@@ -491,205 +475,12 @@ await Promise.all([
|
|||||||
loadRecentUrls(),
|
loadRecentUrls(),
|
||||||
loadFavorites(),
|
loadFavorites(),
|
||||||
detectFfmpegHttpReconnectSupport(),
|
detectFfmpegHttpReconnectSupport(),
|
||||||
initializeWebTransportServer(),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
server.listen(PORT, () => {
|
server.listen(PORT, () => {
|
||||||
const webTransportStatus = webTransportClientConfig ? ` webTransportPort=${webTransportClientConfig.port}` : '';
|
console.log(`Frame stream app listening at http://localhost:${PORT} mode=${PLAYBACK_CONNECTION_MODE}`);
|
||||||
console.log(`Frame stream app listening at http://localhost:${PORT} mode=${PLAYBACK_CONNECTION_MODE} frameTransport=${FRAME_TRANSPORT}${webTransportStatus}`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
async function initializeWebTransportServer() {
|
|
||||||
if (!WEBTRANSPORT_ENABLED) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { certPem, keyPem, certificateHash } = await loadWebTransportCertificate();
|
|
||||||
const { NapiServer } = loadWebTransportNativeBinding();
|
|
||||||
const bindAddress = formatHostPort(WEBTRANSPORT_HOST, WEBTRANSPORT_PORT);
|
|
||||||
|
|
||||||
webTransportServer = NapiServer.bind(bindAddress, certPem, keyPem);
|
|
||||||
webTransportClientConfig = {
|
|
||||||
host: WEBTRANSPORT_PUBLIC_HOST || null,
|
|
||||||
port: WEBTRANSPORT_PUBLIC_PORT,
|
|
||||||
certificateHash,
|
|
||||||
certificateHashAlgorithm: 'sha-256',
|
|
||||||
};
|
|
||||||
|
|
||||||
logInfo(`webtransport listening address=${bindAddress} publicPort=${WEBTRANSPORT_PUBLIC_PORT}`);
|
|
||||||
void acceptWebTransportFrameSessions();
|
|
||||||
} catch (error) {
|
|
||||||
webTransportServer = null;
|
|
||||||
webTransportClientConfig = null;
|
|
||||||
logWarn(`webtransport disabled error=${oneLine(error.message)}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadWebTransportCertificate() {
|
|
||||||
const certPathConfigured = Boolean(process.env.WEBTRANSPORT_CERT_PATH);
|
|
||||||
const keyPathConfigured = Boolean(process.env.WEBTRANSPORT_KEY_PATH);
|
|
||||||
|
|
||||||
if (certPathConfigured !== keyPathConfigured) {
|
|
||||||
throw new Error('WEBTRANSPORT_CERT_PATH and WEBTRANSPORT_KEY_PATH must be set together.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!certPathConfigured && !(await isAutoWebTransportCertificateFresh())) {
|
|
||||||
await generateWebTransportCertificate();
|
|
||||||
}
|
|
||||||
|
|
||||||
const [certPem, keyPem] = await Promise.all([
|
|
||||||
fs.readFile(WEBTRANSPORT_CERT_PATH),
|
|
||||||
fs.readFile(WEBTRANSPORT_KEY_PATH),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return {
|
|
||||||
certPem,
|
|
||||||
keyPem,
|
|
||||||
certificateHash: getCertificateSha256Base64(certPem),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function isAutoWebTransportCertificateFresh() {
|
|
||||||
try {
|
|
||||||
const [certStat, keyStat, certPem] = await Promise.all([
|
|
||||||
fs.stat(WEBTRANSPORT_CERT_PATH),
|
|
||||||
fs.stat(WEBTRANSPORT_KEY_PATH),
|
|
||||||
fs.readFile(WEBTRANSPORT_CERT_PATH),
|
|
||||||
]);
|
|
||||||
const certificate = new X509Certificate(certPem);
|
|
||||||
const expiresAt = Date.parse(certificate.validTo);
|
|
||||||
const newestMtime = Math.max(certStat.mtimeMs, keyStat.mtimeMs);
|
|
||||||
const now = Date.now();
|
|
||||||
|
|
||||||
return Number.isFinite(expiresAt) && expiresAt - now > 24 * 60 * 60 * 1000 && now - newestMtime < WEBTRANSPORT_CERT_RENEW_MS;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function generateWebTransportCertificate() {
|
|
||||||
const directory = path.dirname(WEBTRANSPORT_CERT_PATH);
|
|
||||||
const configPath = path.join(directory, `webtransport-openssl-${process.pid}.cnf`);
|
|
||||||
const opensslConfig = [
|
|
||||||
'[req]',
|
|
||||||
'distinguished_name=req_distinguished_name',
|
|
||||||
'x509_extensions=v3_req',
|
|
||||||
'prompt=no',
|
|
||||||
'[req_distinguished_name]',
|
|
||||||
'CN=localhost',
|
|
||||||
'[v3_req]',
|
|
||||||
'basicConstraints=critical,CA:FALSE',
|
|
||||||
'keyUsage=critical,digitalSignature',
|
|
||||||
'extendedKeyUsage=serverAuth',
|
|
||||||
'subjectAltName=@alt_names',
|
|
||||||
'[alt_names]',
|
|
||||||
'DNS.1=localhost',
|
|
||||||
'IP.1=127.0.0.1',
|
|
||||||
'IP.2=::1',
|
|
||||||
'',
|
|
||||||
].join('\n');
|
|
||||||
|
|
||||||
await fs.mkdir(directory, { recursive: true });
|
|
||||||
await fs.writeFile(configPath, opensslConfig, { mode: 0o600 });
|
|
||||||
|
|
||||||
try {
|
|
||||||
await execFileAsync('openssl', [
|
|
||||||
'ecparam',
|
|
||||||
'-name',
|
|
||||||
'prime256v1',
|
|
||||||
'-genkey',
|
|
||||||
'-noout',
|
|
||||||
'-out',
|
|
||||||
WEBTRANSPORT_KEY_PATH,
|
|
||||||
]);
|
|
||||||
await execFileAsync('openssl', [
|
|
||||||
'req',
|
|
||||||
'-new',
|
|
||||||
'-x509',
|
|
||||||
'-key',
|
|
||||||
WEBTRANSPORT_KEY_PATH,
|
|
||||||
'-out',
|
|
||||||
WEBTRANSPORT_CERT_PATH,
|
|
||||||
'-days',
|
|
||||||
'13',
|
|
||||||
'-config',
|
|
||||||
configPath,
|
|
||||||
]);
|
|
||||||
await fs.chmod(WEBTRANSPORT_KEY_PATH, 0o600);
|
|
||||||
logInfo(`webtransport generated short-lived ECDSA certificate path=${WEBTRANSPORT_CERT_PATH}`);
|
|
||||||
} finally {
|
|
||||||
await fs.rm(configPath, { force: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadWebTransportNativeBinding() {
|
|
||||||
const packageEntry = requireFromHere.resolve('@moq/web-transport');
|
|
||||||
const packageRoot = path.resolve(path.dirname(packageEntry), '..');
|
|
||||||
return requireFromHere(path.join(packageRoot, 'napi.cjs'));
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCertificateSha256Base64(certPem) {
|
|
||||||
const certificate = new X509Certificate(certPem);
|
|
||||||
return createHash('sha256').update(Buffer.from(certificate.raw)).digest('base64');
|
|
||||||
}
|
|
||||||
|
|
||||||
async function acceptWebTransportFrameSessions() {
|
|
||||||
while (webTransportServer) {
|
|
||||||
let request = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
request = await webTransportServer.accept();
|
|
||||||
} catch (error) {
|
|
||||||
logWarn(`webtransport accept failed error=${oneLine(error.message)}`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!request) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleWebTransportFrameRequest(request);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleWebTransportFrameRequest(request) {
|
|
||||||
let rawUrl = '';
|
|
||||||
|
|
||||||
try {
|
|
||||||
rawUrl = await request.url;
|
|
||||||
const url = new URL(rawUrl, `https://localhost:${WEBTRANSPORT_PORT}`);
|
|
||||||
const match = url.pathname.match(/^\/frames\/([0-9a-f-]+)$/i);
|
|
||||||
const session = match ? getSession(match[1]) : null;
|
|
||||||
|
|
||||||
if (!session) {
|
|
||||||
await request.reject(404);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const wtSession = await request.ok();
|
|
||||||
const connection = new WebTransportFrameConnection(wtSession, `webtransport:${shortId(session.id)}`);
|
|
||||||
const playbackMode = getSessionPlaybackConnectionMode(session);
|
|
||||||
logInfo(`webtransport frames attached id=${shortId(session.id)} mode=${playbackMode}`);
|
|
||||||
|
|
||||||
if (playbackMode === 'single' || playbackMode === 'relay') {
|
|
||||||
getOrCreatePlayback(session, playbackMode).attachFrames(connection);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
stopSharedPlaybackIfNeeded(session, playbackMode);
|
|
||||||
streamSplitFrames(connection, session);
|
|
||||||
} catch (error) {
|
|
||||||
logWarn(`webtransport request failed url=${oneLine(rawUrl)} error=${oneLine(error.message)}`);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await request.reject(500);
|
|
||||||
} catch {
|
|
||||||
// The request may already have been accepted.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseSessionSource(body) {
|
function parseSessionSource(body) {
|
||||||
if (typeof body?.localPath === 'string' && body.localPath.trim()) {
|
if (typeof body?.localPath === 'string' && body.localPath.trim()) {
|
||||||
return {
|
return {
|
||||||
@@ -925,44 +716,75 @@ function parseResolvedMediaUrl(value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function resolvePlaybackSource(url) {
|
async function resolvePlaybackSource(url) {
|
||||||
if (!isYouTubeUrl(url)) {
|
const singleConnection = requiresSingleConnection(url);
|
||||||
|
const kind = singleConnection ? null : getYtDlpSourceKind(url);
|
||||||
|
|
||||||
|
if (!kind) {
|
||||||
return {
|
return {
|
||||||
kind: 'direct',
|
kind: 'direct',
|
||||||
url,
|
url,
|
||||||
originalUrl: url,
|
originalUrl: url,
|
||||||
headers: {},
|
headers: {},
|
||||||
|
singleConnection,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolved = await resolveYouTubeSource(url);
|
const resolved = await resolveYtDlpSource(url, kind);
|
||||||
|
const resolvedSingleConnection = requiresSingleConnection(resolved.url)
|
||||||
|
|| requiresSingleConnection(resolved.audioSource.url);
|
||||||
|
|
||||||
|
if (resolvedSingleConnection && resolved.url !== resolved.audioSource.url) {
|
||||||
|
throw new Error('Resolved audio and video require separate connections to a single-connection host.');
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
kind: 'youtube',
|
kind,
|
||||||
url: resolved.url,
|
url: resolved.url,
|
||||||
originalUrl: url,
|
originalUrl: url,
|
||||||
headers: resolved.headers,
|
headers: resolved.headers,
|
||||||
|
audioSource: resolved.audioSource,
|
||||||
|
duration: resolvedSingleConnection ? null : resolved.duration,
|
||||||
|
singleConnection: resolvedSingleConnection,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function isYouTubeUrl(value) {
|
function requiresSingleConnection(value) {
|
||||||
|
const hostname = new URL(value).hostname.toLowerCase().replace(/\.$/, '');
|
||||||
|
return SINGLE_CONNECTION_HOSTS.some((host) => hostname === host || hostname.endsWith(`.${host}`));
|
||||||
|
}
|
||||||
|
|
||||||
|
function getYtDlpSourceKind(value) {
|
||||||
let parsed;
|
let parsed;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
parsed = new URL(value);
|
parsed = new URL(value);
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const hostname = parsed.hostname.toLowerCase();
|
const hostname = parsed.hostname.toLowerCase();
|
||||||
|
|
||||||
return hostname === 'youtu.be'
|
if (hostname === 'youtu.be'
|
||||||
|| hostname.endsWith('.youtu.be')
|
|| hostname.endsWith('.youtu.be')
|
||||||
|| hostname === 'youtube.com'
|
|| hostname === 'youtube.com'
|
||||||
|| hostname.endsWith('.youtube.com')
|
|| hostname.endsWith('.youtube.com')
|
||||||
|| hostname === 'youtube-nocookie.com'
|
|| hostname === 'youtube-nocookie.com'
|
||||||
|| hostname.endsWith('.youtube-nocookie.com');
|
|| hostname.endsWith('.youtube-nocookie.com')) {
|
||||||
|
return 'youtube';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolveYouTubeSource(url) {
|
if (/^(?:(?:www|m|mobile)\.)?(?:x|twitter)\.com$/.test(hostname)) {
|
||||||
|
return 'twitter';
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isYtDlpSourceKind(kind) {
|
||||||
|
return kind === 'youtube' || kind === 'twitter';
|
||||||
|
}
|
||||||
|
|
||||||
|
async function resolveYtDlpSource(url, kind) {
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
logInfo(`yt-dlp resolving source host=${new URL(url).hostname}`);
|
logInfo(`yt-dlp resolving source host=${new URL(url).hostname}`);
|
||||||
|
|
||||||
@@ -971,29 +793,52 @@ async function resolveYouTubeSource(url) {
|
|||||||
try {
|
try {
|
||||||
payload = await runYtDlpJson([
|
payload = await runYtDlpJson([
|
||||||
'--no-playlist',
|
'--no-playlist',
|
||||||
|
// Twitter can return a playlist even with --no-playlist unless the URL
|
||||||
|
// selects /video/N. Resolve only the first video of an unselected post.
|
||||||
|
...(kind === 'twitter' ? ['--playlist-items', '1'] : []),
|
||||||
'--no-warnings',
|
'--no-warnings',
|
||||||
'--no-progress',
|
'--no-progress',
|
||||||
'--dump-single-json',
|
'--dump-single-json',
|
||||||
|
'--js-runtimes',
|
||||||
|
`node:${process.execPath}`,
|
||||||
'--format',
|
'--format',
|
||||||
YT_DLP_FORMAT,
|
kind === 'twitter' ? YT_DLP_TWITTER_FORMAT : YT_DLP_FORMAT,
|
||||||
url,
|
url,
|
||||||
]);
|
]);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`yt-dlp failed to resolve YouTube URL: ${error.message}`);
|
const detail = oneLine(error.message).replace(/https?:\/\/[^\s"'<>]+/g, '<url>');
|
||||||
|
logWarn(`yt-dlp resolve failed host=${new URL(url).hostname} durationMs=${Date.now() - startedAt} error=${detail}`);
|
||||||
|
throw new Error(`yt-dlp failed to resolve ${kind === 'youtube' ? 'YouTube' : 'X/Twitter'} URL: ${error.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const mediaUrl = typeof payload?.url === 'string' ? payload.url : '';
|
if (Array.isArray(payload?.entries)) {
|
||||||
|
payload = payload.entries[0];
|
||||||
if (!mediaUrl) {
|
|
||||||
throw new Error('yt-dlp did not return a playable media URL.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = parseResolvedMediaUrl(mediaUrl);
|
const formats = Array.isArray(payload?.requested_formats) && payload.requested_formats.length > 0
|
||||||
logInfo(`yt-dlp resolved source host=${new URL(parsed).hostname} durationMs=${Date.now() - startedAt}`);
|
? payload.requested_formats : [payload];
|
||||||
|
// Like yt-dlp's format selector, treat an unspecified codec as unknown;
|
||||||
|
// Twitter's direct MP4 formats may omit both. Only "none" means absent.
|
||||||
|
const video = formats.find((format) => format?.url && format.vcodec !== 'none');
|
||||||
|
const audio = formats.find((format) => format?.url && format.acodec !== 'none');
|
||||||
|
|
||||||
|
if (!video || !audio) {
|
||||||
|
throw new Error('yt-dlp did not return playable video and audio streams.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsed = parseResolvedMediaUrl(video.url);
|
||||||
|
const audioUrl = parseResolvedMediaUrl(audio.url);
|
||||||
|
const duration = Number(payload.duration);
|
||||||
|
logInfo(`yt-dlp resolved source host=${new URL(parsed).hostname} separateAudio=${parsed !== audioUrl} durationMs=${Date.now() - startedAt}`);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
url: parsed,
|
url: parsed,
|
||||||
headers: normalizeSourceHeaders(payload?.http_headers),
|
headers: normalizeSourceHeaders({ ...payload.http_headers, ...video.http_headers }),
|
||||||
|
audioSource: {
|
||||||
|
url: audioUrl,
|
||||||
|
headers: normalizeSourceHeaders({ ...payload.http_headers, ...audio.http_headers }),
|
||||||
|
},
|
||||||
|
duration: !payload.is_live && payload.live_status !== 'is_live' && Number.isFinite(duration) && duration > 0 ? duration : null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1135,23 +980,6 @@ function parsePlaybackConnectionMode(value) {
|
|||||||
return 'split';
|
return 'split';
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseFrameTransport(value) {
|
|
||||||
if (value === 'webtransport' || value === 'quic') {
|
|
||||||
return 'webtransport';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value === 'auto') {
|
|
||||||
return 'auto';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value === 'websocket' || value === 'ws' || value === undefined || value === '') {
|
|
||||||
return 'websocket';
|
|
||||||
}
|
|
||||||
|
|
||||||
console.warn(`Unknown FRAME_TRANSPORT "${value}", using websocket.`);
|
|
||||||
return 'websocket';
|
|
||||||
}
|
|
||||||
|
|
||||||
function clampInteger(value, fallback, min, max) {
|
function clampInteger(value, fallback, min, max) {
|
||||||
const parsed = Number(value);
|
const parsed = Number(value);
|
||||||
|
|
||||||
@@ -1199,15 +1027,11 @@ function formatSessionPayload(session) {
|
|||||||
seekable: isSessionSeekable(session),
|
seekable: isSessionSeekable(session),
|
||||||
seekSeconds: session.seekSeconds,
|
seekSeconds: session.seekSeconds,
|
||||||
seekGeneration: session.seekGeneration,
|
seekGeneration: session.seekGeneration,
|
||||||
frameTransport: {
|
|
||||||
preferred: FRAME_TRANSPORT,
|
|
||||||
webTransport: webTransportClientConfig,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function isSessionSeekable(session) {
|
function isSessionSeekable(session) {
|
||||||
return hasRecordedDuration(session);
|
return !session.singleConnection && hasRecordedDuration(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasRecordedDuration(session) {
|
function hasRecordedDuration(session) {
|
||||||
@@ -1215,8 +1039,11 @@ function hasRecordedDuration(session) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function canBestEffortResumeWithoutDuration(session) {
|
function canBestEffortResumeWithoutDuration(session) {
|
||||||
|
return !session.singleConnection && (isYtDlpSourceKind(session.sourceKind) || isLikelyRecordedSession(session));
|
||||||
|
}
|
||||||
|
|
||||||
|
function isLikelyRecordedSession(session) {
|
||||||
return session.sourceKind === 'local'
|
return session.sourceKind === 'local'
|
||||||
|| session.sourceKind === 'youtube'
|
|
||||||
|| isLikelyRecordedMediaUrl(session.originalUrl)
|
|| isLikelyRecordedMediaUrl(session.originalUrl)
|
||||||
|| isLikelyRecordedMediaUrl(session.url);
|
|| isLikelyRecordedMediaUrl(session.url);
|
||||||
}
|
}
|
||||||
@@ -1224,14 +1051,26 @@ function canBestEffortResumeWithoutDuration(session) {
|
|||||||
function isLikelyRecordedMediaUrl(value) {
|
function isLikelyRecordedMediaUrl(value) {
|
||||||
try {
|
try {
|
||||||
const parsed = new URL(value);
|
const parsed = new URL(value);
|
||||||
return RECORDED_MEDIA_EXTENSIONS.has(path.extname(parsed.pathname).toLowerCase());
|
return RECORDED_MEDIA_EXTENSIONS.has(path.extname(parsed.pathname).toLowerCase())
|
||||||
|
|| isJellyfinRecordedMediaPath(parsed.pathname);
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isJellyfinRecordedMediaPath(pathname) {
|
||||||
|
return /^\/items\/[^/]+\/download\/?$/i.test(pathname)
|
||||||
|
|| /^\/videos\/[^/]+\/stream\/?$/i.test(pathname);
|
||||||
|
}
|
||||||
|
|
||||||
function getSessionPlaybackConnectionMode(session) {
|
function getSessionPlaybackConnectionMode(session) {
|
||||||
if (session.sourceKind === 'local') {
|
// Subscription hosts must never gain a second source connection, even for
|
||||||
|
// files, metadata probing, recovery, or a globally configured split mode.
|
||||||
|
if (session.singleConnection) {
|
||||||
|
return 'relay';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (session.sourceKind === 'local' || isYtDlpSourceKind(session.sourceKind)) {
|
||||||
return 'split';
|
return 'split';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1239,7 +1078,9 @@ function getSessionPlaybackConnectionMode(session) {
|
|||||||
return 'split';
|
return 'split';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PLAYBACK_CONNECTION_MODE === 'relay' && hasRecordedDuration(session)) {
|
// Files need random access even before their duration is known. A pipe can
|
||||||
|
// otherwise make ffmpeg read an entire MP4 before reaching its metadata.
|
||||||
|
if (PLAYBACK_CONNECTION_MODE === 'relay' && (hasRecordedDuration(session) || isLikelyRecordedSession(session))) {
|
||||||
return 'split';
|
return 'split';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1251,6 +1092,10 @@ function startSessionMetadataProbe(session) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function ensureSessionDuration(session) {
|
function ensureSessionDuration(session) {
|
||||||
|
if (session.singleConnection) {
|
||||||
|
return Promise.resolve(null);
|
||||||
|
}
|
||||||
|
|
||||||
if (hasRecordedDuration(session)) {
|
if (hasRecordedDuration(session)) {
|
||||||
return Promise.resolve(session.duration);
|
return Promise.resolve(session.duration);
|
||||||
}
|
}
|
||||||
@@ -1261,7 +1106,7 @@ function ensureSessionDuration(session) {
|
|||||||
|
|
||||||
session.metadataStatus = 'pending';
|
session.metadataStatus = 'pending';
|
||||||
|
|
||||||
const probePromise = probeSessionDuration(session).then((duration) => {
|
const probePromise = probeSessionDurationWithRetry(session).then((duration) => {
|
||||||
const current = sessions.get(session.id);
|
const current = sessions.get(session.id);
|
||||||
|
|
||||||
if (current !== session) {
|
if (current !== session) {
|
||||||
@@ -1270,6 +1115,9 @@ function ensureSessionDuration(session) {
|
|||||||
|
|
||||||
session.duration = duration;
|
session.duration = duration;
|
||||||
session.metadataStatus = Number.isFinite(duration) ? 'ready' : 'unavailable';
|
session.metadataStatus = Number.isFinite(duration) ? 'ready' : 'unavailable';
|
||||||
|
if (Number.isFinite(duration)) {
|
||||||
|
logInfo(`duration ready id=${shortId(session.id)} seconds=${duration.toFixed(3)}`);
|
||||||
|
}
|
||||||
return duration;
|
return duration;
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
const current = sessions.get(session.id);
|
const current = sessions.get(session.id);
|
||||||
@@ -1291,7 +1139,28 @@ function ensureSessionDuration(session) {
|
|||||||
return probePromise;
|
return probePromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function probeSessionDuration(session) {
|
async function probeSessionDurationWithRetry(session) {
|
||||||
|
const maxAttempts = canBestEffortResumeWithoutDuration(session) ? METADATA_PROBE_MAX_ATTEMPTS : 1;
|
||||||
|
|
||||||
|
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
||||||
|
if (sessions.get(session.id) !== session) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await probeSessionDuration(session, Math.min(METADATA_PROBE_TIMEOUT_MS * (3 ** (attempt - 1)), 30 * 1000));
|
||||||
|
} catch (error) {
|
||||||
|
if (attempt === maxAttempts) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
logWarn(`duration probe retry id=${shortId(session.id)} attempt=${attempt}/${maxAttempts} error=${oneLine(error.message)}`);
|
||||||
|
await delay(attempt * 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function probeSessionDuration(session, timeoutMs) {
|
||||||
const source = createSourceInput(session.id, 'probe');
|
const source = createSourceInput(session.id, 'probe');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -1303,7 +1172,7 @@ async function probeSessionDuration(session) {
|
|||||||
'-of',
|
'-of',
|
||||||
'json',
|
'json',
|
||||||
source.url,
|
source.url,
|
||||||
]);
|
], timeoutMs);
|
||||||
const payload = JSON.parse(output);
|
const payload = JSON.parse(output);
|
||||||
const duration = Number(payload?.format?.duration);
|
const duration = Number(payload?.format?.duration);
|
||||||
|
|
||||||
@@ -1313,7 +1182,7 @@ async function probeSessionDuration(session) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function runFfprobe(args) {
|
function runFfprobe(args, timeoutMs) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const ffprobe = spawn(FFPROBE_PATH, args, {
|
const ffprobe = spawn(FFPROBE_PATH, args, {
|
||||||
stdio: ['ignore', 'pipe', 'pipe'],
|
stdio: ['ignore', 'pipe', 'pipe'],
|
||||||
@@ -1336,7 +1205,7 @@ function runFfprobe(args) {
|
|||||||
const timer = setTimeout(() => {
|
const timer = setTimeout(() => {
|
||||||
timedOut = true;
|
timedOut = true;
|
||||||
stopProcess(ffprobe);
|
stopProcess(ffprobe);
|
||||||
}, METADATA_PROBE_TIMEOUT_MS);
|
}, timeoutMs);
|
||||||
timer.unref();
|
timer.unref();
|
||||||
|
|
||||||
ffprobe.stdout.on('data', (chunk) => {
|
ffprobe.stdout.on('data', (chunk) => {
|
||||||
@@ -2027,7 +1896,7 @@ function createRelayPlayback(session) {
|
|||||||
sourceController = new AbortController();
|
sourceController = new AbortController();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const headers = createSourceRequestHeaders(session);
|
const headers = createSourceRequestHeaders(session.sourceHeaders);
|
||||||
headers.set('accept-encoding', 'identity');
|
headers.set('accept-encoding', 'identity');
|
||||||
|
|
||||||
const upstream = await fetch(session.url, {
|
const upstream = await fetch(session.url, {
|
||||||
@@ -2515,212 +2384,6 @@ function isWebSocketOpen(websocket) {
|
|||||||
return websocket?.readyState === WebSocket.OPEN;
|
return websocket?.readyState === WebSocket.OPEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
class WebTransportFrameConnection extends EventEmitter {
|
|
||||||
constructor(session, label) {
|
|
||||||
super();
|
|
||||||
this.session = session;
|
|
||||||
this.label = label;
|
|
||||||
this.readyState = WebSocket.OPEN;
|
|
||||||
this._bufferedAmount = 0;
|
|
||||||
this._closedEmitted = false;
|
|
||||||
this._controlStreamPromise = this.openControlStream();
|
|
||||||
this._controlWrite = Promise.resolve();
|
|
||||||
this._frameStreamPromise = null;
|
|
||||||
this._frameWrite = Promise.resolve();
|
|
||||||
|
|
||||||
this.session.closed().then((info) => {
|
|
||||||
this.markClosed(info?.closeCode ?? 1000, info?.reason ?? '');
|
|
||||||
}).catch((error) => {
|
|
||||||
this.emitError(error);
|
|
||||||
this.markClosed(1011, 'webtransport closed');
|
|
||||||
});
|
|
||||||
|
|
||||||
void this.acceptClientControlStreams();
|
|
||||||
}
|
|
||||||
|
|
||||||
get bufferedAmount() {
|
|
||||||
return this._bufferedAmount;
|
|
||||||
}
|
|
||||||
|
|
||||||
send(data, options, callback) {
|
|
||||||
const done = typeof options === 'function' ? options : callback;
|
|
||||||
|
|
||||||
if (typeof data === 'string') {
|
|
||||||
this._controlWrite = this._controlWrite
|
|
||||||
.then(() => this.writeControl(data))
|
|
||||||
.then(() => done?.())
|
|
||||||
.catch((error) => {
|
|
||||||
this.emitError(error);
|
|
||||||
done?.(error);
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const packet = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
||||||
this._frameWrite = this._frameWrite
|
|
||||||
.catch(() => {})
|
|
||||||
.then(() => this.writeFrame(packet));
|
|
||||||
|
|
||||||
this._frameWrite.then(() => {
|
|
||||||
done?.();
|
|
||||||
}).catch((error) => {
|
|
||||||
this.emitError(error);
|
|
||||||
done?.(error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
close(code = 1000, reason = '') {
|
|
||||||
if (this.readyState === WebSocket.CLOSED || this.readyState === WebSocket.CLOSING) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.readyState = WebSocket.CLOSING;
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.session.close(code, reason);
|
|
||||||
} catch (error) {
|
|
||||||
this.emitError(error);
|
|
||||||
this.markClosed(code, reason);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async openControlStream() {
|
|
||||||
const stream = await this.session.openUni();
|
|
||||||
await stream.write(Buffer.from([WT_STREAM_CONTROL_TO_CLIENT]));
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
async openFrameStream() {
|
|
||||||
const stream = await this.session.openUni();
|
|
||||||
await stream.write(Buffer.from([WT_STREAM_FRAME]));
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
getFrameStream() {
|
|
||||||
this._frameStreamPromise ??= this.openFrameStream();
|
|
||||||
return this._frameStreamPromise;
|
|
||||||
}
|
|
||||||
|
|
||||||
async writeControl(data) {
|
|
||||||
if (this.readyState !== WebSocket.OPEN) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const stream = await this._controlStreamPromise;
|
|
||||||
await stream.write(Buffer.from(`${data}\n`, 'utf8'));
|
|
||||||
}
|
|
||||||
|
|
||||||
async writeFrame(packet) {
|
|
||||||
if (this.readyState !== WebSocket.OPEN) {
|
|
||||||
throw new Error('WebTransport frame connection is closed.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (packet.length > 0xffffffff) {
|
|
||||||
throw new Error('WebTransport frame packet is too large.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const recordHeader = Buffer.allocUnsafe(WT_FRAME_RECORD_HEADER_BYTES);
|
|
||||||
recordHeader.writeUInt32LE(packet.length, 0);
|
|
||||||
this._bufferedAmount += WT_FRAME_RECORD_HEADER_BYTES + packet.length;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const stream = await this.getFrameStream();
|
|
||||||
await stream.write(recordHeader);
|
|
||||||
await stream.write(packet);
|
|
||||||
} finally {
|
|
||||||
this._bufferedAmount = Math.max(0, this._bufferedAmount - WT_FRAME_RECORD_HEADER_BYTES - packet.length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async acceptClientControlStreams() {
|
|
||||||
while (this.readyState === WebSocket.OPEN || this.readyState === WebSocket.CLOSING) {
|
|
||||||
let stream;
|
|
||||||
|
|
||||||
try {
|
|
||||||
stream = await this.session.acceptUni();
|
|
||||||
} catch (error) {
|
|
||||||
if (this.readyState === WebSocket.OPEN) {
|
|
||||||
this.emitError(error);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void this.readClientControlStream(stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async readClientControlStream(stream) {
|
|
||||||
let buffer = '';
|
|
||||||
let sawHeader = false;
|
|
||||||
|
|
||||||
try {
|
|
||||||
while (this.readyState === WebSocket.OPEN || this.readyState === WebSocket.CLOSING) {
|
|
||||||
const chunk = await stream.read(64 * 1024);
|
|
||||||
|
|
||||||
if (!chunk) {
|
|
||||||
this.flushClientControlBuffer(buffer);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let data = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
||||||
|
|
||||||
if (!sawHeader) {
|
|
||||||
sawHeader = true;
|
|
||||||
|
|
||||||
if (data[0] !== WT_STREAM_CONTROL_TO_SERVER) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
data = data.subarray(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer = this.consumeClientControlText(buffer + data.toString('utf8'));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
if (this.readyState === WebSocket.OPEN) {
|
|
||||||
this.emitError(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
consumeClientControlText(text) {
|
|
||||||
const lines = text.split(/\n/);
|
|
||||||
const pending = lines.pop() ?? '';
|
|
||||||
|
|
||||||
for (const line of lines) {
|
|
||||||
if (line.trim()) {
|
|
||||||
this.emit('message', Buffer.from(line, 'utf8'), false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return pending;
|
|
||||||
}
|
|
||||||
|
|
||||||
flushClientControlBuffer(text) {
|
|
||||||
if (text.trim()) {
|
|
||||||
this.emit('message', Buffer.from(text, 'utf8'), false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
emitError(error) {
|
|
||||||
if (this.listenerCount('error') > 0) {
|
|
||||||
this.emit('error', error);
|
|
||||||
} else {
|
|
||||||
logWarn(`webtransport frame connection error kind=${this.label} error=${oneLine(error.message)}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
markClosed(code, reason) {
|
|
||||||
if (this._closedEmitted) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._closedEmitted = true;
|
|
||||||
this.readyState = WebSocket.CLOSED;
|
|
||||||
this.emit('close', code, reason);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createJpegFrameParser(onFrame) {
|
function createJpegFrameParser(onFrame) {
|
||||||
let collecting = false;
|
let collecting = false;
|
||||||
let pendingMarkerByte = false;
|
let pendingMarkerByte = false;
|
||||||
@@ -3200,7 +2863,7 @@ function buildRelayFrameArgs(session) {
|
|||||||
|
|
||||||
function buildPlaybackArgs(session, inputUrl) {
|
function buildPlaybackArgs(session, inputUrl) {
|
||||||
return [
|
return [
|
||||||
...buildInputArgs(inputUrl, { startTime: session.seekSeconds }),
|
...buildInputArgs(session, inputUrl),
|
||||||
'-map',
|
'-map',
|
||||||
'0:a:0?',
|
'0:a:0?',
|
||||||
'-vn',
|
'-vn',
|
||||||
@@ -3223,7 +2886,7 @@ function buildPlaybackArgs(session, inputUrl) {
|
|||||||
|
|
||||||
function buildAudioArgs(session, inputUrl, inputOptions) {
|
function buildAudioArgs(session, inputUrl, inputOptions) {
|
||||||
return [
|
return [
|
||||||
...buildInputArgs(inputUrl, { ...inputOptions, startTime: inputOptions?.startTime ?? session.seekSeconds }),
|
...buildInputArgs(session, inputUrl, inputOptions),
|
||||||
'-map',
|
'-map',
|
||||||
'0:a:0?',
|
'0:a:0?',
|
||||||
'-vn',
|
'-vn',
|
||||||
@@ -3243,14 +2906,14 @@ function buildAudioArgs(session, inputUrl, inputOptions) {
|
|||||||
|
|
||||||
function buildFrameArgs(session, inputUrl, inputOptions) {
|
function buildFrameArgs(session, inputUrl, inputOptions) {
|
||||||
return [
|
return [
|
||||||
...buildInputArgs(inputUrl, { ...inputOptions, startTime: inputOptions?.startTime ?? session.seekSeconds }),
|
...buildInputArgs(session, inputUrl, inputOptions),
|
||||||
'-map',
|
'-map',
|
||||||
'0:v:0',
|
'0:v:0',
|
||||||
...buildFrameOutputArgs(session, 'pipe:1'),
|
...buildFrameOutputArgs(session, 'pipe:1'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildInputArgs(inputUrl, { seekable = true, startTime = 0 } = {}) {
|
function buildInputArgs(session, inputUrl, { seekable = true, startTime = session.seekSeconds } = {}) {
|
||||||
const isHttpInput = isHttpInputUrl(inputUrl);
|
const isHttpInput = isHttpInputUrl(inputUrl);
|
||||||
const args = [
|
const args = [
|
||||||
'-hide_banner',
|
'-hide_banner',
|
||||||
@@ -3261,7 +2924,8 @@ function buildInputArgs(inputUrl, { seekable = true, startTime = 0 } = {}) {
|
|||||||
];
|
];
|
||||||
|
|
||||||
if (seekable && isHttpInput) {
|
if (seekable && isHttpInput) {
|
||||||
args.push('-seekable', startTime > 0 ? '1' : FFMPEG_INPUT_SEEKABLE);
|
const inputSeekable = hasRecordedDuration(session) || isLikelyRecordedSession(session) ? '-1' : FFMPEG_INPUT_SEEKABLE;
|
||||||
|
args.push('-seekable', startTime > 0 ? '1' : inputSeekable);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (startTime > 0) {
|
if (startTime > 0) {
|
||||||
@@ -3562,10 +3226,10 @@ function createLineLogger(callback) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSourceRequestHeaders(session) {
|
function createSourceRequestHeaders(sourceHeaders) {
|
||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
|
|
||||||
for (const [name, value] of Object.entries(session.sourceHeaders ?? {})) {
|
for (const [name, value] of Object.entries(sourceHeaders ?? {})) {
|
||||||
headers.set(name, value);
|
headers.set(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3597,10 +3261,6 @@ function sendJson(websocket, payload) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatHostPort(host, port) {
|
|
||||||
return host.includes(':') && !host.startsWith('[') ? `[${host}]:${port}` : `${host}:${port}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function appendTail(current, chunk) {
|
function appendTail(current, chunk) {
|
||||||
const next = current + chunk.toString('utf8');
|
const next = current + chunk.toString('utf8');
|
||||||
return next.length > 4000 ? next.slice(-4000) : next;
|
return next.length > 4000 ? next.slice(-4000) : next;
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import fs from 'node:fs/promises';
|
||||||
|
import { test } from 'node:test';
|
||||||
|
import vm from 'node:vm';
|
||||||
|
|
||||||
|
const source = await fs.readFile(new URL('../public/app.js', import.meta.url), 'utf8');
|
||||||
|
|
||||||
|
function loadPlayer(metadataFetch) {
|
||||||
|
const nodes = new Map();
|
||||||
|
const createElement = () => ({
|
||||||
|
hidden: true, disabled: true, value: '', currentTime: 0,
|
||||||
|
addEventListener() {}, replaceChildren() {}, setAttribute() {},
|
||||||
|
getContext() { return {}; }, style: { setProperty() {} },
|
||||||
|
});
|
||||||
|
const context = vm.createContext({
|
||||||
|
document: {
|
||||||
|
querySelector(selector) {
|
||||||
|
if (!nodes.has(selector)) nodes.set(selector, createElement());
|
||||||
|
return nodes.get(selector);
|
||||||
|
},
|
||||||
|
createElement, addEventListener() {},
|
||||||
|
},
|
||||||
|
window: {
|
||||||
|
addEventListener() {},
|
||||||
|
// Advance polling without waiting in real time.
|
||||||
|
setTimeout(callback) { queueMicrotask(callback); },
|
||||||
|
},
|
||||||
|
fetch: async url => url.startsWith('/api/session/') ? metadataFetch(url) : ({ ok: true, json: async () => ({}) }),
|
||||||
|
console,
|
||||||
|
});
|
||||||
|
vm.runInContext(source, context);
|
||||||
|
vm.runInContext("state.session = { id: 'recorded', metadataStatus: 'pending' };", context);
|
||||||
|
return { nodes, run: code => vm.runInContext(code, context) };
|
||||||
|
}
|
||||||
|
|
||||||
|
test('seeking becomes enabled after a slow probe and transient metadata request errors', async () => {
|
||||||
|
let polls = 0;
|
||||||
|
const player = loadPlayer(async () => {
|
||||||
|
polls += 1;
|
||||||
|
assert.ok(polls <= 25, 'polling must stop once duration is available');
|
||||||
|
if (polls === 1) throw new Error('Temporary network failure');
|
||||||
|
if (polls === 2) return { ok: false, status: 503 };
|
||||||
|
const ready = polls === 25;
|
||||||
|
return { ok: true, json: async () => ({
|
||||||
|
id: 'recorded', metadataStatus: ready ? 'ready' : 'pending',
|
||||||
|
duration: ready ? 7783 : null, seekable: ready,
|
||||||
|
}) };
|
||||||
|
});
|
||||||
|
await player.run("refreshSessionMetadata('recorded')");
|
||||||
|
assert.equal(polls, 25, 'must continue beyond the old twenty-poll limit');
|
||||||
|
assert.equal(player.nodes.get('#seek').disabled, false);
|
||||||
|
assert.equal(player.nodes.get('#seek').max, '7783');
|
||||||
|
assert.equal(player.nodes.get('#total-time').textContent, '2:09:43');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('metadata responses from an old stream cannot overwrite a restarted player', async () => {
|
||||||
|
let resolveMetadata;
|
||||||
|
const player = loadPlayer(() => new Promise(resolve => { resolveMetadata = resolve; }));
|
||||||
|
const pending = player.run("refreshSessionMetadata('recorded')");
|
||||||
|
player.run("state.streamGeneration += 1; state.duration = 60; state.seekable = true; syncPlayhead();");
|
||||||
|
resolveMetadata({ ok: true, json: async () => ({ metadataStatus: 'unavailable', duration: null, seekable: false }) });
|
||||||
|
await pending;
|
||||||
|
assert.equal(player.nodes.get('#seek').disabled, false);
|
||||||
|
assert.equal(player.nodes.get('#seek').max, '60');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('metadata polling stops when probing finishes without a duration', async () => {
|
||||||
|
let polls = 0;
|
||||||
|
const player = loadPlayer(async () => {
|
||||||
|
polls += 1;
|
||||||
|
assert.equal(polls, 1);
|
||||||
|
return { ok: true, json: async () => ({ metadataStatus: 'unavailable', duration: null, seekable: false }) };
|
||||||
|
});
|
||||||
|
await player.run("refreshSessionMetadata('recorded')");
|
||||||
|
assert.equal(player.nodes.get('#seek').disabled, true);
|
||||||
|
assert.equal(polls, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a delayed ready message cannot disable seeking after duration is discovered', () => {
|
||||||
|
const player = loadPlayer(() => { throw new Error('Unexpected metadata request'); });
|
||||||
|
player.run('updateSessionMetadata({ duration: 7783, seekable: true });');
|
||||||
|
player.run('handleControlMessage(JSON.stringify({ type: "ready", duration: null, seekable: false }));');
|
||||||
|
assert.equal(player.nodes.get('#seek').disabled, false);
|
||||||
|
assert.equal(player.nodes.get('#seek').max, '7783');
|
||||||
|
});
|
||||||
@@ -0,0 +1,447 @@
|
|||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { execFileSync, spawn } from 'node:child_process';
|
||||||
|
import { once } from 'node:events';
|
||||||
|
import fs from 'node:fs/promises';
|
||||||
|
import { createServer } from 'node:http';
|
||||||
|
import os from 'node:os';
|
||||||
|
import path from 'node:path';
|
||||||
|
import { setTimeout as delay } from 'node:timers/promises';
|
||||||
|
import { after, before, test } from 'node:test';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
|
||||||
|
import WebSocket from 'ws';
|
||||||
|
|
||||||
|
const root = fileURLToPath(new URL('..', import.meta.url));
|
||||||
|
let directory;
|
||||||
|
let movie;
|
||||||
|
let live;
|
||||||
|
let videoOnly;
|
||||||
|
let audioOnly;
|
||||||
|
let combined;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
directory = await fs.mkdtemp(path.join(os.tmpdir(), 'carplay-playback-test-'));
|
||||||
|
const inputs = ['-y', '-hide_banner', '-loglevel', 'error',
|
||||||
|
'-f', 'lavfi', '-i', 'testsrc2=size=160x90:rate=12',
|
||||||
|
'-f', 'lavfi', '-i', 'sine=frequency=440:sample_rate=48000', '-t', '8'];
|
||||||
|
execFileSync('ffmpeg', [...inputs, '-c:v', 'mpeg4', '-c:a', 'aac', `${directory}/movie.mp4`]);
|
||||||
|
execFileSync('ffmpeg', [...inputs, '-c:v', 'mpeg2video', '-c:a', 'mp2', '-f', 'mpegts', `${directory}/live.ts`]);
|
||||||
|
const file = await fs.readFile(`${directory}/movie.mp4`);
|
||||||
|
combined = file;
|
||||||
|
execFileSync('ffmpeg', ['-y', '-hide_banner', '-loglevel', 'error', '-i', `${directory}/movie.mp4`,
|
||||||
|
'-map', '0:v', '-c', 'copy', '-an', `${directory}/video.mp4`,
|
||||||
|
'-map', '0:a', '-c', 'copy', '-vn', `${directory}/audio.m4a`]);
|
||||||
|
videoOnly = await fs.readFile(`${directory}/video.mp4`);
|
||||||
|
audioOnly = await fs.readFile(`${directory}/audio.m4a`);
|
||||||
|
let offset = 0;
|
||||||
|
while (file.toString('ascii', offset + 4, offset + 8) !== 'moov') {
|
||||||
|
const size = file.readUInt32BE(offset);
|
||||||
|
assert.ok(size >= 8 && offset + size < file.length);
|
||||||
|
offset += size;
|
||||||
|
}
|
||||||
|
// Leave media chunk offsets intact, but put metadata beyond a large padding
|
||||||
|
// box. HTTP seeking should skip it instead of downloading it through a pipe.
|
||||||
|
const padding = Buffer.alloc(16 * 1024 * 1024);
|
||||||
|
padding.writeUInt32BE(padding.length);
|
||||||
|
padding.write('free', 4);
|
||||||
|
movie = Buffer.concat([file.subarray(0, offset), padding, file.subarray(offset)]);
|
||||||
|
live = await fs.readFile(`${directory}/live.ts`);
|
||||||
|
});
|
||||||
|
|
||||||
|
after(async () => { await fs.rm(directory, { recursive: true, force: true }); });
|
||||||
|
|
||||||
|
async function until(check, timeoutMs = 10000) {
|
||||||
|
const deadline = Date.now() + timeoutMs;
|
||||||
|
while (Date.now() < deadline) {
|
||||||
|
const result = await check();
|
||||||
|
if (result) return result;
|
||||||
|
await delay(50);
|
||||||
|
}
|
||||||
|
throw new Error('Timed out waiting for playback condition');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function startApp(t, { mode = 'relay', probe = 'real', resolver, resolverMetadata = {}, resolverFormat, singleConnectionHosts = '', metadataEnabled = '0' } = {}) {
|
||||||
|
const temp = await fs.mkdtemp(path.join(directory, 'app-'));
|
||||||
|
const requests = [];
|
||||||
|
const upstream = createServer((request, response) => {
|
||||||
|
const pathname = new URL(request.url, 'http://localhost').pathname;
|
||||||
|
const isLive = pathname === '/live' || pathname === '/iptv.mp4';
|
||||||
|
const data = isLive ? live : pathname === '/video.mp4' ? videoOnly
|
||||||
|
: pathname === '/audio.m4a' ? audioOnly : pathname === '/combined.mp4' ? combined : movie;
|
||||||
|
const branch = pathname === '/video.mp4' ? 'video' : pathname === '/audio.m4a' ? 'audio' : null;
|
||||||
|
if (branch && (request.headers['x-branch'] !== branch || request.headers['x-common'] !== 'shared')) {
|
||||||
|
response.writeHead(403).end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const match = /^bytes=(\d+)-(\d*)$/.exec(request.headers.range ?? '');
|
||||||
|
const start = match ? Number(match[1]) : 0;
|
||||||
|
const end = match?.[2] ? Math.min(Number(match[2]), data.length - 1) : data.length - 1;
|
||||||
|
requests.push({ url: pathname, start, range: Boolean(match), branch: request.headers['x-branch'] });
|
||||||
|
response.writeHead(match ? 206 : 200, {
|
||||||
|
'content-type': isLive ? 'video/mp2t' : 'video/mp4',
|
||||||
|
'content-length': end - start + 1,
|
||||||
|
'accept-ranges': 'bytes',
|
||||||
|
...(match ? { 'content-range': `bytes ${start}-${end}/${data.length}` } : {}),
|
||||||
|
});
|
||||||
|
let position = start;
|
||||||
|
const timer = setInterval(() => {
|
||||||
|
if (response.writableNeedDrain) return;
|
||||||
|
const next = Math.min(position + 64 * 1024, end + 1);
|
||||||
|
response.write(data.subarray(position, next));
|
||||||
|
position = next;
|
||||||
|
if (position > end) response.end();
|
||||||
|
}, isLive ? 10 : 50);
|
||||||
|
response.on('close', () => clearInterval(timer));
|
||||||
|
});
|
||||||
|
await new Promise(resolve => upstream.listen(0, '127.0.0.1', resolve));
|
||||||
|
const sourceBase = `http://127.0.0.1:${upstream.address().port}`;
|
||||||
|
let ytDlp = 'yt-dlp';
|
||||||
|
if (resolver) {
|
||||||
|
ytDlp = `${temp}/yt-dlp`;
|
||||||
|
let payload = {
|
||||||
|
duration: 8, http_headers: { 'X-Common': 'shared' }, ...resolverMetadata,
|
||||||
|
...(['combined', 'unknown-codecs', 'protected-combined'].includes(resolver) ? {
|
||||||
|
url: `${sourceBase}/${resolver === 'protected-combined' ? 'iptv.mp4' : 'combined.mp4'}?token=combined-secret`,
|
||||||
|
...(resolver === 'unknown-codecs' ? {} : { vcodec: 'mpeg4', acodec: 'aac' }),
|
||||||
|
} : { requested_formats: [
|
||||||
|
...(resolver === 'missing-video' ? [] : [
|
||||||
|
{ url: `${sourceBase}/video.mp4?token=video-secret`, vcodec: 'mpeg4', acodec: 'none', http_headers: { 'X-Branch': 'video' } },
|
||||||
|
]),
|
||||||
|
...(resolver === 'missing-audio' ? [] : [
|
||||||
|
{ url: `${sourceBase}/audio.m4a?token=audio-secret`, vcodec: 'none', acodec: 'aac', http_headers: { 'X-Branch': 'audio' } },
|
||||||
|
]),
|
||||||
|
] }),
|
||||||
|
};
|
||||||
|
if (resolver === 'playlist' || resolver === 'empty-playlist') {
|
||||||
|
payload = { _type: 'playlist', duration: 999, entries: resolver === 'playlist' ? [payload] : [] };
|
||||||
|
}
|
||||||
|
await fs.writeFile(ytDlp, `#!/usr/bin/env node
|
||||||
|
require('node:fs').writeFileSync(${JSON.stringify(`${temp}/yt-dlp-args.json`)}, JSON.stringify(process.argv.slice(2)));
|
||||||
|
if (${JSON.stringify(resolver)} === 'fail') {
|
||||||
|
process.stderr.write('Extraction failed for https://example.test/video?token=resolver-secret');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
process.stdout.write(${JSON.stringify(JSON.stringify(payload))});
|
||||||
|
`, { mode: 0o755 });
|
||||||
|
}
|
||||||
|
const reservation = createServer();
|
||||||
|
await new Promise(resolve => reservation.listen(0, '127.0.0.1', resolve));
|
||||||
|
const port = reservation.address().port;
|
||||||
|
await new Promise(resolve => reservation.close(resolve));
|
||||||
|
let ffprobe = 'ffprobe';
|
||||||
|
if (probe !== 'real') {
|
||||||
|
ffprobe = `${temp}/ffprobe`;
|
||||||
|
await fs.writeFile(ffprobe, `#!/usr/bin/env node
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const counter = ${JSON.stringify(`${temp}/attempts`)};
|
||||||
|
const attempt = Number(fs.existsSync(counter) ? fs.readFileSync(counter, 'utf8') : 0) + 1;
|
||||||
|
fs.writeFileSync(counter, String(attempt));
|
||||||
|
if (${JSON.stringify(probe)} === 'fail') {
|
||||||
|
process.stderr.write('Temporary upstream failure'); process.exit(1);
|
||||||
|
} else if (attempt === 1) {
|
||||||
|
setInterval(() => {}, 1000);
|
||||||
|
} else {
|
||||||
|
const child = require('node:child_process').spawn('ffprobe', process.argv.slice(2), {stdio: 'inherit'});
|
||||||
|
process.on('SIGTERM', () => child.kill('SIGTERM'));
|
||||||
|
child.on('exit', code => process.exit(code ?? 1));
|
||||||
|
}
|
||||||
|
`, { mode: 0o755 });
|
||||||
|
}
|
||||||
|
const child = spawn(process.execPath, ['server/index.js'], {
|
||||||
|
cwd: root, detached: true,
|
||||||
|
env: { ...process.env, PORT: String(port), PLAYBACK_CONNECTION_MODE: mode,
|
||||||
|
METADATA_PROBE_ENABLED: metadataEnabled, METADATA_PROBE_TIMEOUT_MS: '1000', FFPROBE_PATH: ffprobe,
|
||||||
|
SINGLE_CONNECTION_HOSTS: singleConnectionHosts, YT_DLP_PATH: ytDlp, YT_DLP_FORMAT: resolverFormat,
|
||||||
|
FFMPEG_INPUT_SEEKABLE: '0', FFMPEG_HTTP_RECONNECT: '0', LOCAL_VIDEOS: '',
|
||||||
|
RECENT_URLS_PATH: `${temp}/recent.json`, FAVORITES_PATH: `${temp}/favorites.json` },
|
||||||
|
stdio: ['ignore', 'pipe', 'pipe'],
|
||||||
|
});
|
||||||
|
let logs = '';
|
||||||
|
child.stdout.on('data', chunk => logs += chunk);
|
||||||
|
child.stderr.on('data', chunk => logs += chunk);
|
||||||
|
t.after(async () => {
|
||||||
|
try { process.kill(-child.pid, 'SIGTERM'); } catch {}
|
||||||
|
if (child.exitCode === null && child.signalCode === null) await once(child, 'close');
|
||||||
|
// Include any worker still shutting down in this isolated process group.
|
||||||
|
try { process.kill(-child.pid, 'SIGKILL'); } catch {}
|
||||||
|
upstream.closeAllConnections();
|
||||||
|
await new Promise(resolve => upstream.close(resolve));
|
||||||
|
});
|
||||||
|
const base = `http://127.0.0.1:${port}`;
|
||||||
|
await until(async () => {
|
||||||
|
try { return (await fetch(`${base}/api/health`)).ok; } catch { return false; }
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
base, requests, logs: () => logs,
|
||||||
|
attempts: async () => Number(await fs.readFile(`${temp}/attempts`, 'utf8')),
|
||||||
|
ytDlpArgs: async () => JSON.parse(await fs.readFile(`${temp}/yt-dlp-args.json`, 'utf8')),
|
||||||
|
create: async (pathname = '/movie.mp4') => fetch(`${base}/api/session`, {
|
||||||
|
method: 'POST', headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify({ url: pathname.startsWith('http') ? pathname : `${sourceBase}${pathname}`, fps: 12, width: 160 }),
|
||||||
|
}).then(response => response.json()),
|
||||||
|
metadata: id => fetch(`${base}/api/session/${id}`).then(response => response.json()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function connect(t, base, id) {
|
||||||
|
const abort = new AbortController();
|
||||||
|
const socket = new WebSocket(`${base.replace('http:', 'ws:')}/frames/${id}`);
|
||||||
|
const close = () => { abort.abort(); socket.terminate(); };
|
||||||
|
t.after(close);
|
||||||
|
const timer = setTimeout(close, 10000);
|
||||||
|
const frame = new Promise((resolve, reject) => {
|
||||||
|
socket.on('message', (data, binary) => { if (binary) resolve(data); });
|
||||||
|
socket.on('error', reject);
|
||||||
|
socket.on('close', () => reject(new Error('Frames closed before first image')));
|
||||||
|
});
|
||||||
|
const audio = new Promise((resolve, reject) => {
|
||||||
|
void (async () => {
|
||||||
|
const response = await fetch(`${base}/audio/${id}`, { signal: abort.signal });
|
||||||
|
assert.equal(response.status, 200);
|
||||||
|
let bytes = 0;
|
||||||
|
for await (const chunk of response.body) {
|
||||||
|
bytes += chunk.length;
|
||||||
|
if (bytes >= 8192) resolve(bytes);
|
||||||
|
}
|
||||||
|
reject(new Error('Audio ended before enough data arrived'));
|
||||||
|
})().catch(reject);
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
ready: Promise.all([frame, audio]).finally(() => clearTimeout(timer)),
|
||||||
|
close,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
test('recorded MP4 starts with random access and recovers seeking after a probe timeout', { timeout: 20000 }, async t => {
|
||||||
|
const app = await startApp(t, { probe: 'timeout-once' });
|
||||||
|
const session = await app.create();
|
||||||
|
assert.equal(session.metadataStatus, 'pending');
|
||||||
|
const streams = connect(t, app.base, session.id);
|
||||||
|
const [firstFrame] = await streams.ready;
|
||||||
|
assert.equal(firstFrame.readDoubleLE(0), 0);
|
||||||
|
assert.equal(firstFrame.readUInt16BE(8), 0xffd8);
|
||||||
|
assert.match(app.logs(), /source=direct mode=split metadata=pending/);
|
||||||
|
assert.ok(app.requests.some(request => request.start > 16 * 1024 * 1024), 'ffmpeg must skip to metadata at EOF');
|
||||||
|
const metadata = await until(async () => {
|
||||||
|
const value = await app.metadata(session.id);
|
||||||
|
return value.metadataStatus === 'ready' && value;
|
||||||
|
});
|
||||||
|
assert.equal(metadata.seekable, true);
|
||||||
|
assert.ok(metadata.duration >= 8 && metadata.duration < 9);
|
||||||
|
assert.equal(await app.attempts(), 2);
|
||||||
|
assert.match(app.logs(), /duration probe retry .*ffprobe timed out/);
|
||||||
|
streams.close();
|
||||||
|
await until(() => (app.logs().match(/ffmpeg exited/g) ?? []).length === 2);
|
||||||
|
const response = await fetch(`${app.base}/api/session/${session.id}/seek`, {
|
||||||
|
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ time: 4 }),
|
||||||
|
});
|
||||||
|
assert.equal(response.status, 200);
|
||||||
|
const sought = await response.json();
|
||||||
|
assert.equal(sought.seekSeconds, 4);
|
||||||
|
assert.equal(sought.seekGeneration, 1);
|
||||||
|
const resumed = connect(t, app.base, session.id);
|
||||||
|
const [nextFrame] = await resumed.ready;
|
||||||
|
assert.notDeepEqual(nextFrame.subarray(8), firstFrame.subarray(8), 'seek must change the video content');
|
||||||
|
resumed.close();
|
||||||
|
await until(() => (app.logs().match(/ffmpeg exited/g) ?? []).length === 4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failed metadata retries are bounded and finish as unavailable', { timeout: 12000 }, async t => {
|
||||||
|
const app = await startApp(t, { probe: 'fail' });
|
||||||
|
const session = await app.create();
|
||||||
|
await until(async () => (await app.metadata(session.id)).metadataStatus === 'unavailable');
|
||||||
|
assert.equal(await app.attempts(), 3);
|
||||||
|
for (let i = 0; i < 3; i++) assert.equal((await app.metadata(session.id)).seekable, false);
|
||||||
|
assert.equal(await app.attempts(), 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('live relay still uses exactly one upstream connection and no metadata probe', { timeout: 15000 }, async t => {
|
||||||
|
const app = await startApp(t, { probe: 'fail' });
|
||||||
|
const session = await app.create('/live');
|
||||||
|
assert.equal(session.metadataStatus, 'disabled');
|
||||||
|
const streams = connect(t, app.base, session.id);
|
||||||
|
await streams.ready;
|
||||||
|
assert.equal(app.requests.length, 1);
|
||||||
|
assert.equal(app.requests[0].range, false);
|
||||||
|
const recovery = await fetch(`${app.base}/api/session/${session.id}/seek`, {
|
||||||
|
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ time: 5, allowBestEffort: true }),
|
||||||
|
});
|
||||||
|
assert.equal(recovery.status, 409);
|
||||||
|
assert.equal(app.requests.length, 1);
|
||||||
|
await assert.rejects(app.attempts(), { code: 'ENOENT' });
|
||||||
|
streams.close();
|
||||||
|
await until(() => app.logs().includes('relay closed'));
|
||||||
|
assert.equal((app.logs().match(/ffmpeg exited/g) ?? []).length, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('subscription hosts stay on one connection even for MP4 URLs with global split and probing enabled', { timeout: 15000 }, async t => {
|
||||||
|
const app = await startApp(t, { mode: 'split', probe: 'fail', singleConnectionHosts: '127.0.0.1', metadataEnabled: '1' });
|
||||||
|
const session = await app.create('/iptv.mp4');
|
||||||
|
assert.equal(session.metadataStatus, 'disabled');
|
||||||
|
assert.equal(session.seekable, false);
|
||||||
|
const streams = connect(t, app.base, session.id);
|
||||||
|
await streams.ready;
|
||||||
|
assert.match(app.logs(), /source=direct mode=relay metadata=disabled/);
|
||||||
|
for (const allowBestEffort of [false, true]) {
|
||||||
|
const result = await fetch(`${app.base}/api/session/${session.id}/seek`, {
|
||||||
|
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ time: 5, allowBestEffort }),
|
||||||
|
});
|
||||||
|
assert.equal(result.status, 409);
|
||||||
|
}
|
||||||
|
assert.equal(app.requests.length, 1);
|
||||||
|
assert.equal(app.requests[0].range, false);
|
||||||
|
await assert.rejects(app.attempts(), { code: 'ENOENT' });
|
||||||
|
streams.close();
|
||||||
|
await until(() => app.logs().includes('relay closed'));
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const { label, url, kind, resolver, mode = 'single' } of [
|
||||||
|
...['separate', 'combined'].map(resolver => ({ label: 'YouTube', url: 'https://www.youtube.com/watch?v=test', kind: 'youtube', resolver })),
|
||||||
|
...['separate', 'combined', 'unknown-codecs', 'playlist'].map(resolver => ({ label: 'X', url: 'https://x.com/example/status/123?s=20', kind: 'twitter', resolver, mode: 'relay' })),
|
||||||
|
{ label: 'Twitter', url: 'https://twitter.com/example/status/123/video/2', kind: 'twitter', resolver: 'combined' },
|
||||||
|
]) {
|
||||||
|
test(`${label} ${resolver} sources play and seek with Node enabled and no extra probe`, { timeout: 15000 }, async t => {
|
||||||
|
const app = await startApp(t, { mode, probe: 'fail', resolver });
|
||||||
|
const session = await app.create(url);
|
||||||
|
assert.equal(session.duration, 8);
|
||||||
|
assert.equal(session.metadataStatus, 'ready');
|
||||||
|
assert.equal(session.seekable, true);
|
||||||
|
const args = await app.ytDlpArgs();
|
||||||
|
assert.equal(args[args.indexOf('--js-runtimes') + 1], `node:${process.execPath}`);
|
||||||
|
assert.match(args[args.indexOf('--format') + 1], /bestvideo.*\+bestaudio/);
|
||||||
|
if (kind === 'twitter') {
|
||||||
|
assert.equal(args[args.indexOf('--format') + 1], 'best[protocol=https]/bestvideo[protocol=https]+bestaudio[protocol=https]');
|
||||||
|
}
|
||||||
|
assert.equal(args.at(-1), url, 'preserve the selected video and query parameters');
|
||||||
|
assert.ok(args.includes('--no-playlist'));
|
||||||
|
if (kind === 'twitter') assert.equal(args[args.indexOf('--playlist-items') + 1], '1');
|
||||||
|
const recents = await fetch(`${app.base}/api/recent-urls`).then(response => response.json());
|
||||||
|
assert.equal(recents.urls[0].url, url);
|
||||||
|
const streams = connect(t, app.base, session.id);
|
||||||
|
const [frame] = await streams.ready;
|
||||||
|
assert.ok(Number.isFinite(frame.readDoubleLE(0)));
|
||||||
|
assert.equal(frame.readUInt16BE(8), 0xffd8, 'frames must remain JPEGs');
|
||||||
|
assert.equal(frame.readUInt16BE(frame.length - 2), 0xffd9);
|
||||||
|
streams.close();
|
||||||
|
await until(() => (app.logs().match(/ffmpeg exited/g) ?? []).length === 2);
|
||||||
|
const result = await fetch(`${app.base}/api/session/${session.id}/seek`, {
|
||||||
|
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ time: 4 }),
|
||||||
|
});
|
||||||
|
assert.equal(result.status, 200);
|
||||||
|
assert.equal((await result.json()).seekSeconds, 4);
|
||||||
|
const resumed = connect(t, app.base, session.id);
|
||||||
|
await resumed.ready;
|
||||||
|
resumed.close();
|
||||||
|
await until(() => (app.logs().match(/ffmpeg exited/g) ?? []).length === 4);
|
||||||
|
if (resolver === 'separate' || resolver === 'playlist') {
|
||||||
|
assert.ok(app.requests.some(request => request.url === '/video.mp4' && request.branch === 'video'));
|
||||||
|
assert.ok(app.requests.some(request => request.url === '/audio.m4a' && request.branch === 'audio'));
|
||||||
|
} else {
|
||||||
|
assert.ok(app.requests.every(request => request.url === '/combined.mp4'));
|
||||||
|
}
|
||||||
|
assert.ok(app.logs().includes(`source=${kind} mode=split metadata=ready`));
|
||||||
|
assert.doesNotMatch(app.logs(), /(?:video|audio|combined)-secret/);
|
||||||
|
await assert.rejects(app.attempts(), { code: 'ENOENT' });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const host of ['youtu.be', 'x.com', 'twitter.com']) {
|
||||||
|
test(`${host} resolution failures are logged without exposing source URLs`, async t => {
|
||||||
|
const app = await startApp(t, { resolver: 'fail' });
|
||||||
|
const session = await app.create(`https://${host}/example/status/123`);
|
||||||
|
assert.match(session.error, /yt-dlp failed to resolve/);
|
||||||
|
assert.ok(app.logs().includes(`yt-dlp resolve failed host=${host}`));
|
||||||
|
assert.doesNotMatch(app.logs(), /resolver-secret|example\.test/);
|
||||||
|
if (host !== 'youtu.be') assert.match(session.error, /X\/Twitter URL/);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test('audio-less YouTube results are rejected before any playback worker starts', async t => {
|
||||||
|
const app = await startApp(t, { resolver: 'missing-audio' });
|
||||||
|
const session = await app.create('https://youtu.be/test');
|
||||||
|
assert.match(session.error, /did not return playable video and audio streams/);
|
||||||
|
assert.equal(app.requests.length, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('X/Twitter desktop and mobile URLs use the resolver, with hostname boundaries enforced', async t => {
|
||||||
|
const app = await startApp(t, { resolver: 'combined', probe: 'fail' });
|
||||||
|
for (const host of ['x.com', 'www.x.com', 'mobile.x.com', 'm.x.com', 'twitter.com', 'www.twitter.com', 'mobile.twitter.com', 'm.twitter.com']) {
|
||||||
|
const url = `https://${host}/i/web/status/123`;
|
||||||
|
assert.equal((await app.create(url)).metadataStatus, 'ready');
|
||||||
|
assert.equal((await app.ytDlpArgs()).at(-1), url);
|
||||||
|
}
|
||||||
|
const lastArgs = await app.ytDlpArgs();
|
||||||
|
for (const host of ['notx.com', 'nottwitter.com', 'x.com.example.test', 'twitter.com.example.test']) {
|
||||||
|
assert.equal((await app.create(`https://${host}/example/status/123`)).metadataStatus, 'disabled');
|
||||||
|
assert.deepEqual(await app.ytDlpArgs(), lastArgs, 'unrelated hosts must not invoke yt-dlp');
|
||||||
|
}
|
||||||
|
await assert.rejects(app.attempts(), { code: 'ENOENT' });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('X preserves an explicitly configured yt-dlp format selector', async t => {
|
||||||
|
const resolverFormat = 'best[protocol=https][height<=720]';
|
||||||
|
const app = await startApp(t, { resolver: 'combined', resolverFormat });
|
||||||
|
assert.equal((await app.create('https://x.com/example/status/123')).metadataStatus, 'ready');
|
||||||
|
const args = await app.ytDlpArgs();
|
||||||
|
assert.equal(args[args.indexOf('--format') + 1], resolverFormat);
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const resolver of ['missing-audio', 'missing-video', 'empty-playlist']) {
|
||||||
|
test(`X ${resolver} results are rejected before playback starts`, async t => {
|
||||||
|
const app = await startApp(t, { resolver });
|
||||||
|
const session = await app.create('https://x.com/example/status/123');
|
||||||
|
assert.match(session.error, /did not return playable video and audio streams/);
|
||||||
|
assert.equal(app.requests.length, 0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const resolverMetadata of [{ duration: null }, { duration: 0 }, { is_live: true }, { live_status: 'is_live' }]) {
|
||||||
|
test(`X metadata ${JSON.stringify(resolverMetadata)} does not enable seeking`, async t => {
|
||||||
|
const app = await startApp(t, { resolver: 'combined', resolverMetadata, probe: 'fail', mode: 'single' });
|
||||||
|
const session = await app.create('https://x.com/example/status/123');
|
||||||
|
assert.equal(session.duration, null);
|
||||||
|
assert.equal(session.seekable, false);
|
||||||
|
assert.equal(session.metadataStatus, 'pending');
|
||||||
|
assert.match(app.logs(), /source=twitter mode=split metadata=pending/);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test('protected page hosts bypass yt-dlp, probing, and recovery seeking', async t => {
|
||||||
|
const app = await startApp(t, { resolver: 'fail', probe: 'fail', mode: 'split', metadataEnabled: '1', singleConnectionHosts: 'X.COM, twitter.com, youtube.com' });
|
||||||
|
for (const host of ['x.com', 'mobile.x.com', 'twitter.com', 'www.youtube.com']) {
|
||||||
|
const session = await app.create(`https://${host}/example/status/123`);
|
||||||
|
assert.equal(session.metadataStatus, 'disabled');
|
||||||
|
assert.equal(session.seekable, false);
|
||||||
|
const recovery = await fetch(`${app.base}/api/session/${session.id}/seek`, {
|
||||||
|
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ time: 5, allowBestEffort: true }),
|
||||||
|
});
|
||||||
|
assert.equal(recovery.status, 409);
|
||||||
|
}
|
||||||
|
assert.match(app.logs(), /source=direct mode=relay metadata=disabled/);
|
||||||
|
await assert.rejects(app.ytDlpArgs(), { code: 'ENOENT' });
|
||||||
|
await assert.rejects(app.attempts(), { code: 'ENOENT' });
|
||||||
|
assert.equal(app.requests.length, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resolved combined sources on protected hosts use one relay connection', { timeout: 15000 }, async t => {
|
||||||
|
const app = await startApp(t, { resolver: 'protected-combined', probe: 'fail', mode: 'split', metadataEnabled: '1', singleConnectionHosts: '127.0.0.1' });
|
||||||
|
const session = await app.create('https://x.com/example/status/123');
|
||||||
|
assert.equal(session.metadataStatus, 'disabled');
|
||||||
|
assert.equal(session.duration, null);
|
||||||
|
assert.equal(session.seekable, false);
|
||||||
|
const streams = connect(t, app.base, session.id);
|
||||||
|
await streams.ready;
|
||||||
|
assert.equal(app.requests.length, 1);
|
||||||
|
assert.equal(app.requests[0].range, false);
|
||||||
|
assert.match(app.logs(), /source=twitter mode=relay metadata=disabled/);
|
||||||
|
await assert.rejects(app.attempts(), { code: 'ENOENT' });
|
||||||
|
streams.close();
|
||||||
|
await until(() => app.logs().includes('relay closed'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resolved separate sources cannot bypass single-connection hosts', async t => {
|
||||||
|
const app = await startApp(t, { resolver: 'separate', singleConnectionHosts: '127.0.0.1' });
|
||||||
|
const session = await app.create('https://x.com/example/status/123');
|
||||||
|
assert.match(session.error, /separate connections to a single-connection host/);
|
||||||
|
assert.equal(app.requests.length, 0);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user