feat: configurable input with SRT caller mode (INPUT_URL)#11
Merged
Conversation
When INPUT_URL is set to an srt:// URL the encoder dials that source in caller mode and pulls the MPEG-TS feed into the existing filter graph and HLS output, instead of listening for an RTMP publisher. Unset keeps the current RTMP listener behaviour unchanged. A dial failure before the encoder reaches running is no longer terminal: it retries while staying in starting, bounded by the optional start-request timeout.
Contributor
Author
|
CI note: the |
Addresses review feedback on the SRT caller input: - Bound the dial with a deadline (per-request timeout if supplied, else INPUT_DIAL_TIMEOUT, default 300s) so a permanently unreachable source no longer re-dials forever pinned at starting. - On dial-deadline expiry land in error (a connect failure is an input failure); the RTMP listener keeps its original timeout-to-stopped path. - Clear stale HLS output before the first spawn and before each re-dial so a leftover index.m3u8 cannot flip status to running before a connection exists; also release the pull-push fetcher on the error path. - Fix ffmpeg exit-code precedence so a real nonzero exit is recorded. - Require host and port in the srt:// input URL, rejecting degenerate URLs at startup. - Redact URL query strings (which may carry a passphrase) from error messages and process-arg logging.
A failed srt caller dial echoes the full input URL (passphrase, streamid) in ffmpeg's stderr, and in caller mode failed dials are the common path, so the passphrase would otherwise hit the logs on every retry. Redact URL query strings in the stderr handler as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #9
Summary
Adds an optional
INPUT_URLenv var. When it is set to ansrt://URL the encoder dials that source in caller mode and pulls the MPEG-TS feed straight into the existing filter graph and HLS output, instead of listening for an RTMP publisher. When it is unset the behaviour is exactly today's RTMP listener, so this is fully backward compatible.This covers the two workflows in the issue: SRT contribution, and container platforms where no inbound UDP port can be exposed (the encoder dials out rather than listening).
What changed
src/encoder.tsgenerateInputtakes an optionalinputUrl. When present it emits-i <inputUrl>(no RTMP-listen); when absent it emits the unchanged RTMP listener args.EncoderOptsgainsinputUrl?: stringandinputDialTimeoutSec?: number.inputUrlis validated in the constructor: it must parse as a URL, use thesrt:scheme, and include a host and port, otherwise the process fails fast at startup with a clear (secret-redacted) message.errorwhen caller-mode input is configured and the encoder never reachedrunning. It re-dials while staying instarting, bounded by a dial deadline; on deadline it lands inerror. The RTMP path is untouched.src/server.tsreadsINPUT_URLandINPUT_DIAL_TIMEOUTinto the encoder options.SRT protocol knobs (
latency,passphrase,streamid,connect_timeout,mode, ...) travel as query parameters on the URL and are parsed by ffmpeg's srt reader directly. No srt option strings are hardcoded in this change, so the encoder does not need to track ffmpeg's srt option set.Backward compatibility
With
INPUT_URLunset,generateInputreturns byte-identical args to before and the monitor's RTMP timeout branch is unchanged (staysstopped). A regression test pins the default RTMP listener args and the RTMP timeout-to-stopped behaviour.Design choices flagged for maintainers
These came out of self-review; a different shape is fine, the capability is what matters.
errorfor SRT caller mode, notstopped. Rationale: a source that never connects is an input failure, and a supervising process should be able to tell that apart from a clean stop. The pre-existing RTMP listener timeout still goes tostopped(unchanged). This is a deliberate divergence between the two input modes.timeoutis given, overridable viaINPUT_DIAL_TIMEOUT. Without a bound, a wrong passphrase / NXDOMAIN / bad port would re-dial every 5 s forever pinned atstarting. Open to a different default or a dedicated max-attempts knob.start()now clears stale HLS output before spawning (both input modes). This fixes a latent bug where a leftoverindex.m3u8from a previous run could flip status torunningbefore a connection exists (and, for caller mode, defeat the re-dial). It is a small behaviour change for the RTMP path too.stopPullPush) before going toerror, which it previously did not.this.ffmpeg.exitCode = code || this.wantsToStop ? 0 : 1was mis-parenthesized (every nonzero exit recorded 0); fixed tocode || (this.wantsToStop ? 0 : 1).spawnargsexit log, and the ffmpeg stderr passthrough. The last one matters because a failed srt dial echoes the full input URL (passphrase, streamid) in ffmpeg's stderr, and failed dials are the common caller-mode path. Note stderrdataevents are chunked, so a URL split across chunks could in theory evade redaction; the normal single-line case is covered.Known and accepted (unchanged here): there is still no in-progress guard on
start()(pre-existing, no double-spawn path exists via the API), and aDELETEduring a re-dial gap reportsstartinguntil the next monitor tick (cosmetic lag).Merge coordination
PR #12 (WebVTT subtitle passthrough) also changes
generateInputin this repo. Whoever merges second must reconcile thegenerateInputsignature so SRT input and subtitle passthrough compose; a naive merge of both branches will break the combination.Dial-failure / retry semantics
ffmpeg's srt caller does not retry internally; on a failed dial it exits non-zero (see the probe below). The encoder re-dials on the next monitor tick (a natural ~5s backoff) while staying in
starting, until either the source connects or the dial deadline is reached (thenerror).Contract verification
Docker was used to probe the exact published image rather than coding option strings from generic ffmpeg docs.
Image probed:
eyevinntechnology/live-encoding:latestDigest:
sha256:a77339d6cea4d35f85b972d23b29b37285a0ffb570f2cb0257b8c4d999dd8d13(linux/amd64)ffmpeg build in that image:
ffmpeg -protocolslistssrt(andsrtp), confirming the binary speakssrt://natively with no image change.ffmpeg -h protocol=srtconfirms the knobs referenced in the issue are real AVOptions on this build (passed as URL query params):modedefaults tocaller, sosrt://host:portdials out as a caller with no extra option needed. Note the unit split surfaced by the probe and reflected in the readme:latencyis microseconds,connect_timeoutis milliseconds.Dial-failure behaviour (feeds the retry design). Dialing a port with no listener inside the image:
So the srt caller exits non-zero on dial failure and does not retry itself. A refused/absent local listener returns almost immediately; an unreachable (silently dropped) peer would instead wait up to
connect_timeout. The retry-in-startinglogic handles both cases, bounded by the dial deadline.Testing
npm run typecheck,npm run lint(0 errors),npm test(20 passing) all pass locally. Tests cover caller-mode vs RTMP arg generation,INPUT_URLvalidation (scheme, host/port, secret redaction), the redaction helper, stderr passthrough redaction, and the dial lifecycle via a child_process spawn stub: single re-dial after a pre-running exit, dial-deadline expiry toerror(notstopped), the default deadline engaging when no timeout is supplied, stale-index cleanup before spawn, and the RTMP timeout still landing instopped.An end-to-end live SRT test needs a reachable listener-mode source and is out of scope here; the arg generation, validation, dial-failure behaviour, and lifecycle transitions are verified as above.
Notes for maintainers
INPUT_URL/INPUT_DIAL_TIMEOUTmirror the existingOUTPUT_URL. Happy to rename.srt://since that is the requested case; broadening to other ffmpeg input URLs would be a one-line change to the scheme check.