prismcast 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/README.md +44 -28
  2. package/dist/app.d.ts +5 -0
  3. package/dist/app.js +63 -5
  4. package/dist/app.js.map +1 -1
  5. package/dist/browser/channelSelection.js +14 -2
  6. package/dist/browser/channelSelection.js.map +1 -1
  7. package/dist/browser/index.d.ts +23 -9
  8. package/dist/browser/index.js +205 -88
  9. package/dist/browser/index.js.map +1 -1
  10. package/dist/browser/precaching.js +3 -3
  11. package/dist/browser/precaching.js.map +1 -1
  12. package/dist/browser/tuning/directv.js +13 -0
  13. package/dist/browser/tuning/directv.js.map +1 -1
  14. package/dist/browser/tuning/fox.js +12 -0
  15. package/dist/browser/tuning/fox.js.map +1 -1
  16. package/dist/browser/tuning/hbo.js +11 -0
  17. package/dist/browser/tuning/hbo.js.map +1 -1
  18. package/dist/browser/tuning/hulu.js +23 -0
  19. package/dist/browser/tuning/hulu.js.map +1 -1
  20. package/dist/browser/tuning/sling.js +34 -4
  21. package/dist/browser/tuning/sling.js.map +1 -1
  22. package/dist/browser/tuning/spectrum.js +12 -0
  23. package/dist/browser/tuning/spectrum.js.map +1 -1
  24. package/dist/browser/tuning/xfinity.d.ts +2 -0
  25. package/dist/browser/tuning/xfinity.js +607 -0
  26. package/dist/browser/tuning/xfinity.js.map +1 -0
  27. package/dist/browser/tuning/youtubeTv.js +14 -0
  28. package/dist/browser/tuning/youtubeTv.js.map +1 -1
  29. package/dist/browser/video.d.ts +2 -0
  30. package/dist/browser/video.js +99 -17
  31. package/dist/browser/video.js.map +1 -1
  32. package/dist/channels/index.js +173 -56
  33. package/dist/channels/index.js.map +1 -1
  34. package/dist/config/health.d.ts +28 -28
  35. package/dist/config/health.js +52 -52
  36. package/dist/config/health.js.map +1 -1
  37. package/dist/config/paths.d.ts +12 -0
  38. package/dist/config/paths.js +16 -0
  39. package/dist/config/paths.js.map +1 -1
  40. package/dist/config/profiles.d.ts +3 -2
  41. package/dist/config/profiles.js +45 -34
  42. package/dist/config/profiles.js.map +1 -1
  43. package/dist/config/providers.d.ts +7 -0
  44. package/dist/config/providers.js +14 -0
  45. package/dist/config/providers.js.map +1 -1
  46. package/dist/config/sites.d.ts +28 -0
  47. package/dist/config/sites.js +93 -122
  48. package/dist/config/sites.js.map +1 -1
  49. package/dist/config/userChannels.d.ts +3 -0
  50. package/dist/config/userChannels.js +10 -3
  51. package/dist/config/userChannels.js.map +1 -1
  52. package/dist/config/userConfig.d.ts +1 -0
  53. package/dist/config/userConfig.js +4 -0
  54. package/dist/config/userConfig.js.map +1 -1
  55. package/dist/config/userProfiles.d.ts +0 -1
  56. package/dist/config/userProfiles.js +18 -10
  57. package/dist/config/userProfiles.js.map +1 -1
  58. package/dist/index.js +16 -7
  59. package/dist/index.js.map +1 -1
  60. package/dist/native/decrypt.d.ts +32 -0
  61. package/dist/native/decrypt.js +85 -0
  62. package/dist/native/decrypt.js.map +1 -0
  63. package/dist/native/index.d.ts +58 -0
  64. package/dist/native/index.js +341 -0
  65. package/dist/native/index.js.map +1 -0
  66. package/dist/native/intercept.d.ts +35 -0
  67. package/dist/native/intercept.js +184 -0
  68. package/dist/native/intercept.js.map +1 -0
  69. package/dist/native/probe.d.ts +48 -0
  70. package/dist/native/probe.js +270 -0
  71. package/dist/native/probe.js.map +1 -0
  72. package/dist/native/proxy.d.ts +52 -0
  73. package/dist/native/proxy.js +904 -0
  74. package/dist/native/proxy.js.map +1 -0
  75. package/dist/routes/config/channels/table.js +19 -25
  76. package/dist/routes/config/channels/table.js.map +1 -1
  77. package/dist/routes/config/providers.js +4 -4
  78. package/dist/routes/config/providers.js.map +1 -1
  79. package/dist/routes/debug.js +5 -5
  80. package/dist/routes/debug.js.map +1 -1
  81. package/dist/routes/hls.js +12 -2
  82. package/dist/routes/hls.js.map +1 -1
  83. package/dist/routes/index.d.ts +1 -0
  84. package/dist/routes/index.js +3 -0
  85. package/dist/routes/index.js.map +1 -1
  86. package/dist/routes/root/content.js +80 -20
  87. package/dist/routes/root/content.js.map +1 -1
  88. package/dist/routes/root/scripts/status.js +14 -13
  89. package/dist/routes/root/scripts/status.js.map +1 -1
  90. package/dist/routes/root/styles.js +4 -2
  91. package/dist/routes/root/styles.js.map +1 -1
  92. package/dist/streaming/fmp4Segmenter.d.ts +3 -0
  93. package/dist/streaming/fmp4Segmenter.js +143 -42
  94. package/dist/streaming/fmp4Segmenter.js.map +1 -1
  95. package/dist/streaming/hls.d.ts +24 -19
  96. package/dist/streaming/hls.js +486 -261
  97. package/dist/streaming/hls.js.map +1 -1
  98. package/dist/streaming/hlsResume.d.ts +7 -0
  99. package/dist/streaming/hlsResume.js +22 -2
  100. package/dist/streaming/hlsResume.js.map +1 -1
  101. package/dist/streaming/hlsSegments.d.ts +42 -2
  102. package/dist/streaming/hlsSegments.js +109 -8
  103. package/dist/streaming/hlsSegments.js.map +1 -1
  104. package/dist/streaming/lifecycle.js +20 -2
  105. package/dist/streaming/lifecycle.js.map +1 -1
  106. package/dist/streaming/monitor.d.ts +1 -0
  107. package/dist/streaming/monitor.js +369 -21
  108. package/dist/streaming/monitor.js.map +1 -1
  109. package/dist/streaming/mp4Parser.d.ts +52 -6
  110. package/dist/streaming/mp4Parser.js +156 -30
  111. package/dist/streaming/mp4Parser.js.map +1 -1
  112. package/dist/streaming/mpegts.d.ts +2 -2
  113. package/dist/streaming/mpegts.js +156 -109
  114. package/dist/streaming/mpegts.js.map +1 -1
  115. package/dist/streaming/playlistBuilder.d.ts +38 -0
  116. package/dist/streaming/playlistBuilder.js +66 -0
  117. package/dist/streaming/playlistBuilder.js.map +1 -0
  118. package/dist/streaming/preroll.d.ts +106 -0
  119. package/dist/streaming/preroll.js +373 -0
  120. package/dist/streaming/preroll.js.map +1 -0
  121. package/dist/streaming/pretune.d.ts +8 -0
  122. package/dist/streaming/pretune.js +226 -0
  123. package/dist/streaming/pretune.js.map +1 -0
  124. package/dist/streaming/registry.d.ts +24 -3
  125. package/dist/streaming/registry.js +23 -1
  126. package/dist/streaming/registry.js.map +1 -1
  127. package/dist/streaming/setup.d.ts +7 -0
  128. package/dist/streaming/setup.js +27 -6
  129. package/dist/streaming/setup.js.map +1 -1
  130. package/dist/streaming/showInfo.d.ts +24 -0
  131. package/dist/streaming/showInfo.js +55 -3
  132. package/dist/streaming/showInfo.js.map +1 -1
  133. package/dist/streaming/statusEmitter.d.ts +6 -4
  134. package/dist/streaming/statusEmitter.js +9 -3
  135. package/dist/streaming/statusEmitter.js.map +1 -1
  136. package/dist/types/channels.d.ts +1 -0
  137. package/dist/types/index.d.ts +1 -1
  138. package/dist/types/profiles.d.ts +8 -1
  139. package/dist/types/selection.d.ts +4 -1
  140. package/dist/types/streaming.d.ts +7 -0
  141. package/dist/utils/chromeFetch.d.ts +20 -0
  142. package/dist/utils/chromeFetch.js +43 -0
  143. package/dist/utils/chromeFetch.js.map +1 -0
  144. package/dist/utils/debugFilter.js +15 -0
  145. package/dist/utils/debugFilter.js.map +1 -1
  146. package/dist/utils/delay.d.ts +16 -0
  147. package/dist/utils/delay.js +15 -1
  148. package/dist/utils/delay.js.map +1 -1
  149. package/dist/utils/ffmpeg.d.ts +1 -7
  150. package/dist/utils/ffmpeg.js +86 -127
  151. package/dist/utils/ffmpeg.js.map +1 -1
  152. package/dist/utils/index.d.ts +2 -0
  153. package/dist/utils/index.js +2 -0
  154. package/dist/utils/index.js.map +1 -1
  155. package/dist/utils/pid.d.ts +40 -0
  156. package/dist/utils/pid.js +83 -0
  157. package/dist/utils/pid.js.map +1 -0
  158. package/package.json +5 -5
@@ -1,20 +1,43 @@
1
- import { LOG, delay, formatError, runWithStreamContext, startTimer } from "../utils/index.js";
2
- import { StreamSetupError, createPageWithCapture, setupStream } from "./setup.js";
3
- import { createHLSState, getAllStreams, getStream, getStreamCount, registerStream, updateLastAccess } from "./registry.js";
1
+ import { LOG, formatError, runWithStreamContext, startTimer } from "../utils/index.js";
2
+ import { StreamSetupError, createPageWithCapture, generateStreamId, setupStream } from "./setup.js";
3
+ import { consumeResumeData, getResumeSegmentIndex } from "./hlsResume.js";
4
+ import { createHLSState, getAllStreams, getNextStreamId, getStream, getStreamCount, registerStream, updateLastAccess } from "./registry.js";
4
5
  import { createInitialStreamStatus, emitStreamAdded } from "./statusEmitter.js";
5
- import { deleteChannelStreamId, getChannelStreamId, isTerminationInitiated, setChannelStreamId, terminateStream } from "./lifecycle.js";
6
6
  import { emitCurrentSystemStatus, isLoginModeActive, unregisterManagedPage } from "../browser/index.js";
7
+ import { generatePrerollPlaylist, getPrerollSegmentCount, isPrerollReady } from "./preroll.js";
7
8
  import { getAllChannels, isPredefinedChannelDisabled } from "../config/userChannels.js";
8
- import { getInitSegment, getPlaylist, getSegment, waitForPlaylist } from "./hlsSegments.js";
9
- import { getProviderTagForChannel, getResolvedChannel, resolveProviderKey } from "../config/providers.js";
9
+ import { getAudioPlaylist, getAudioSegment, getInitSegment, getPlaylist, getSegment, getVideoPlaylist, waitForPlaylist } from "./hlsSegments.js";
10
+ import { getAuthDomainForChannel, getProviderTagForChannel, getResolvedChannel, resolveProviderKey } from "../config/providers.js";
11
+ import { getChannelStreamId, isTerminationInitiated, setChannelStreamId, terminateStream } from "./lifecycle.js";
10
12
  import { markChannelFailure, markChannelSuccess } from "../config/health.js";
11
13
  import { CONFIG } from "../config/index.js";
12
- import { consumeResumeData } from "./hlsResume.js";
14
+ import { attemptNativeStreaming } from "../native/index.js";
15
+ import { clearProbeCache } from "../native/probe.js";
13
16
  import { createFMP4Segmenter } from "./fmp4Segmenter.js";
14
17
  import { createHash } from "node:crypto";
15
18
  import { getProviderBySlug } from "../browser/channelSelection.js";
16
19
  import { registerClient } from "./clients.js";
17
20
  import { triggerShowNameUpdate } from "./showInfo.js";
21
+ /* This module handles HLS (HTTP Live Streaming) output using fMP4 (fragmented MP4) segments. HLS mode uses MP4/AAC capture from puppeteer-stream, which is then
22
+ * segmented natively without any external dependencies. The overall flow uses a two-phase "preroll-first" architecture:
23
+ *
24
+ * 1. Client requests playlist at /hls/:name/stream.m3u8 (predefined channel) or /play?url=...&profile=... (ad-hoc URL)
25
+ * 2. If no stream exists, a pending stream entry is registered immediately with a preroll playlist (black frame + silence). The client receives a valid, playable
26
+ * playlist on the first request — no blocking wait for the real stream.
27
+ * 3. Asynchronously, setupStream() creates the browser page, navigates, captures, and creates the fMP4 segmenter. The segmenter starts at segment index 10
28
+ * (after the preroll range) with a pending discontinuity tag.
29
+ * 4. When the segmenter produces its first real playlist, it overwrites the preroll playlist. The next client poll receives real content.
30
+ * 5. Client fetches init.mp4 once, then media segments at /hls/:name/segmentN.m4s
31
+ * 6. Idle timeout terminates streams with no recent segment requests
32
+ *
33
+ * Shared streams: If multiple clients request the same channel (or the same ad-hoc URL with the same profile), they share one segmenter. The first client triggers
34
+ * stream creation, and subsequent clients get the existing playlist and segments. Ad-hoc streams are identified by a synthetic key ("play-<hash>") derived from the
35
+ * URL and profile, allowing them to use the same channelToStreamId deduplication mechanism as predefined channels.
36
+ */
37
+ // Delay before seeding the preroll playlist in milliseconds. If stream setup completes before this timer fires, the preroll is skipped and the client receives real
38
+ // content directly. This ensures fast-tuning providers (native HLS at 2-3s, most capture providers at 4-7s) never see preroll, while slow providers (Xfinity at
39
+ // 13-15s) get preroll content after the delay to prevent HTTP timeouts.
40
+ const PREROLL_DELAY_MS = 9_000;
18
41
  // Login mode error body used by both validateChannel() and handlePlayStream() to ensure consistent response format.
19
42
  const LOGIN_MODE_BODY = { error: "Login in progress", message: "Please complete authentication before starting new streams." };
20
43
  /**
@@ -68,8 +91,8 @@ export function sendValidationError(validation, res) {
68
91
  }
69
92
  // Public Endpoint Handlers.
70
93
  /**
71
- * Ensures a stream is running for a channel. If no stream exists, starts one. If a stream startup is in progress (-1 sentinel), waits for it to complete. Returns
72
- * the stream ID if successful, or null if an error occurred (with the error response already sent to the client).
94
+ * Ensures a stream is running for a channel. If no stream exists, registers a pending stream with a preroll playlist (when available) and launches async setup. If a
95
+ * stream already exists (either fully set up or pending), returns its ID immediately.
73
96
  *
74
97
  * The existing-stream check runs first so that ad-hoc streams (registered under synthetic keys like "play-a1b2c3d4") can be served without failing the
75
98
  * "Channel not found" check.
@@ -80,21 +103,16 @@ export function sendValidationError(validation, res) {
80
103
  * @param channelName - The channel key (or synthetic ad-hoc key) to stream.
81
104
  * @param req - Express request object (for profile override and client IP).
82
105
  * @param res - Express response object (for error responses).
83
- * @returns The stream ID if a stream is running, or null if an error occurred.
106
+ * @returns The stream ID if a stream is running or pending, or null if an error occurred.
84
107
  */
85
108
  export async function ensureChannelStream(channelName, req, res) {
86
109
  // Check for an existing stream first. This must happen before channel validation so that ad-hoc streams (registered under synthetic keys like "play-a1b2c3d4") can
87
110
  // be served by the standard HLS playlist handler without failing the "Channel not found" check. A stream in channelToStreamId was already validated when it was
88
- // started, so no re-validation is needed.
111
+ // started, so no re-validation is needed. With pending stream registration, this always returns a real stream ID (never a sentinel).
89
112
  const streamId = getChannelStreamId(channelName);
90
- // If a stream is already running (not a startup-in-progress sentinel), return it directly.
91
- if ((streamId !== undefined) && (streamId !== -1)) {
113
+ if (streamId !== undefined) {
92
114
  return streamId;
93
115
  }
94
- // If a startup is in progress (-1 sentinel), another request is already starting this stream. Poll until the real stream ID appears or we timeout.
95
- if (streamId === -1) {
96
- return awaitStreamReady(channelName, res);
97
- }
98
116
  // No existing stream — validate the channel and start a new one. Channel validation is only needed for new streams because existing streams were already validated
99
117
  // at startup time.
100
118
  const validation = validateChannel(channelName);
@@ -102,17 +120,42 @@ export async function ensureChannelStream(channelName, req, res) {
102
120
  sendValidationError(validation, res);
103
121
  return null;
104
122
  }
105
- // Start the stream using the resolved channel's URL. The stream is registered under channelName (canonical key) for deduplication, but uses the resolved
106
- // provider's definition.
107
- const newStreamId = await startHLSStream(channelName, validation.channel.url, req, res, validation.channel);
108
- if (newStreamId === null) {
109
- // Error response already sent by startHLSStream.
110
- return null;
123
+ // When the preroll is available, register a pending stream immediately with a preroll playlist so the client gets a valid playlist on the first request. The full
124
+ // stream setup runs asynchronously. When the preroll is not available (FFmpeg missing or failed), fall back to blocking initialization.
125
+ if (isPrerollReady()) {
126
+ // Check capacity before registering the pending entry. The pending entry occupies a registry slot, so registering it at max capacity would cause setupStream's
127
+ // capacity check to reject — after we've already returned a preroll playlist to the client. By gating here, we can still send a proper 503 error response.
128
+ if (getStreamCount() >= CONFIG.streaming.maxConcurrentStreams) {
129
+ if (!reclaimIdleStream()) {
130
+ res.setHeader("Retry-After", "10");
131
+ res.setHeader("X-HDHomeRun-Error", "All Tuners In Use");
132
+ res.status(503).send("Maximum concurrent streams (" + String(CONFIG.streaming.maxConcurrentStreams) + ") reached. Try again later.");
133
+ return null;
134
+ }
135
+ }
136
+ const clientAddress = req.ip ?? req.socket.remoteAddress ?? null;
137
+ const pending = registerPendingStream(channelName, validation.channel, clientAddress, req);
138
+ // Launch async setup. Errors are caught here to clean up the pending entry and prevent unhandled rejections.
139
+ void completeStreamSetup({
140
+ channel: validation.channel,
141
+ channelName,
142
+ clientAddress,
143
+ numericStreamId: pending.numericStreamId,
144
+ profileOverride: req.query.profile,
145
+ streamIdStr: pending.streamIdStr,
146
+ url: validation.channel.url
147
+ }).catch((error) => {
148
+ handleSetupFailure(pending.numericStreamId, channelName, validation.channel, error);
149
+ });
150
+ return pending.numericStreamId;
111
151
  }
112
- return newStreamId;
152
+ // Fallback: no preroll available. Block until the stream is fully set up.
153
+ return startHLSStream(channelName, validation.channel.url, req, res, validation.channel);
113
154
  }
114
155
  /**
115
- * Handles HLS playlist requests. Ensures a stream is running for the channel (blocking until ready if a new stream must start), then returns the playlist.
156
+ * Handles HLS playlist requests. If a stream exists (fully set up or pending with preroll), returns the playlist immediately. If no stream exists and the preroll is
157
+ * available, registers a pending stream with a preroll playlist and returns it immediately while setup runs asynchronously. Falls back to blocking initialization when
158
+ * the preroll is not available.
116
159
  *
117
160
  * Route: GET /hls/:name/stream.m3u8
118
161
  *
@@ -133,7 +176,8 @@ export async function handleHLSPlaylist(req, res) {
133
176
  await sendPlaylistResponse(streamId, clientAddress, res);
134
177
  }
135
178
  /**
136
- * Handles HLS segment requests. Returns the requested segment from memory. Supports both the fMP4 initialization segment (init.mp4) and media segments (.m4s).
179
+ * Handles HLS segment requests. Returns the requested segment from memory. Supports the fMP4 initialization segment (init.mp4), capture-mode media segments
180
+ * (.m4s), native-mode video segments (.ts), and audio segments for streams with separate audio renditions.
137
181
  *
138
182
  * Route: GET /hls/:name/:segment
139
183
  *
@@ -148,11 +192,11 @@ export function handleHLSSegment(req, res) {
148
192
  return;
149
193
  }
150
194
  const streamId = getChannelStreamId(channelName);
151
- if ((streamId === undefined) || (streamId === -1)) {
195
+ if (streamId === undefined) {
152
196
  res.status(404).send("Stream not found.");
153
197
  return;
154
198
  }
155
- // Handle init segment (init.mp4) separately from media segments (.m4s).
199
+ // Handle init segment (init.mp4) separately from media segments (.m4s, .ts).
156
200
  if (segmentName === "init.mp4") {
157
201
  const initSegment = getInitSegment(streamId);
158
202
  if (!initSegment) {
@@ -160,17 +204,55 @@ export function handleHLSSegment(req, res) {
160
204
  return;
161
205
  }
162
206
  updateLastAccess(streamId);
163
- sendSegment(initSegment, res);
207
+ sendSegment(initSegment, "init.mp4", res);
164
208
  return;
165
209
  }
166
- // Handle media segments (.m4s).
167
- const segment = getSegment(streamId, segmentName);
210
+ // Handle media segments (.m4s and .ts). Check both video and audio segment stores.
211
+ const segment = getSegment(streamId, segmentName) ?? getAudioSegment(streamId, segmentName);
168
212
  if (!segment) {
169
213
  res.status(404).send("Segment not found.");
170
214
  return;
171
215
  }
172
216
  updateLastAccess(streamId);
173
- sendSegment(segment, res);
217
+ sendSegment(segment, segmentName, res);
218
+ }
219
+ /**
220
+ * Handles HLS variant playlist requests for streams with separate audio renditions. Serves video.m3u8 or audio.m3u8 depending on the requested playlist name.
221
+ * Returns 404 for streams that don't have separate audio.
222
+ *
223
+ * Route: GET /hls/:name/video.m3u8 and GET /hls/:name/audio.m3u8
224
+ *
225
+ * @param req - Express request object.
226
+ * @param res - Express response object.
227
+ */
228
+ export function handleHLSVariantPlaylist(req, res) {
229
+ const channelName = req.params.name;
230
+ // Extract the playlist filename from the URL path. The route is registered as two explicit paths (/video.m3u8 and /audio.m3u8) rather than a parameterized route,
231
+ // so there is no :playlist param to read.
232
+ const lastSlash = req.path.lastIndexOf("/");
233
+ const playlistName = lastSlash >= 0 ? req.path.slice(lastSlash + 1) : undefined;
234
+ if (!channelName || !playlistName) {
235
+ res.status(400).send("Channel name and playlist name are required.");
236
+ return;
237
+ }
238
+ const streamId = getChannelStreamId(channelName);
239
+ if (streamId === undefined) {
240
+ res.status(404).send("Stream not found.");
241
+ return;
242
+ }
243
+ let playlist;
244
+ if (playlistName === "video.m3u8") {
245
+ playlist = getVideoPlaylist(streamId);
246
+ }
247
+ else if (playlistName === "audio.m3u8") {
248
+ playlist = getAudioPlaylist(streamId);
249
+ }
250
+ if (!playlist) {
251
+ res.status(404).send("Playlist not found.");
252
+ return;
253
+ }
254
+ updateLastAccess(streamId);
255
+ sendPlaylist(playlist, res);
174
256
  }
175
257
  // Ad-Hoc Streaming.
176
258
  /**
@@ -198,20 +280,9 @@ export async function handlePlayStream(req, res) {
198
280
  // Generate a deterministic synthetic key from the trimmed URL, profile, selector, clickToPlay, and clickSelector. Including these ensures that the same URL with
199
281
  // different options produces separate streams. The newline delimiter is safe since URLs cannot contain literal newlines.
200
282
  const channelName = "play-" + createHash("sha256").update(url + "\n" + (profileOverride ?? "") + "\n" + (selector ?? "") + "\n" + (clickToPlay ? "1" : "") + "\n" + (clickSelector ?? "")).digest("hex").slice(0, 8);
201
- // Check for an existing stream.
283
+ // Check for an existing stream (either fully set up or pending). With pending stream registration, the stream ID is always a real ID.
202
284
  const streamId = getChannelStreamId(channelName);
203
- // If a stream is already running, redirect immediately.
204
- if ((streamId !== undefined) && (streamId !== -1)) {
205
- res.redirect(302, "/hls/" + channelName + "/stream.m3u8");
206
- return;
207
- }
208
- // If a startup is in progress (-1 sentinel), another request is already starting this stream. Poll until the real stream ID appears or we timeout, then redirect.
209
- if (streamId === -1) {
210
- const resolvedId = await awaitStreamReady(channelName, res);
211
- if (resolvedId === null) {
212
- // Error response already sent by awaitStreamReady.
213
- return;
214
- }
285
+ if (streamId !== undefined) {
215
286
  res.redirect(302, "/hls/" + channelName + "/stream.m3u8");
216
287
  return;
217
288
  }
@@ -222,7 +293,7 @@ export async function handlePlayStream(req, res) {
222
293
  }
223
294
  // Capture client IP for Channels DVR API integration.
224
295
  const clientAddress = req.ip ?? req.socket.remoteAddress ?? null;
225
- // Start a new ad-hoc stream. initializeStream handles capture setup, segmenter creation, and event emission.
296
+ // Start a new ad-hoc stream. initializeStream handles pending registration, capture setup, segmenter creation, and event emission.
226
297
  try {
227
298
  const newStreamId = await initializeStream({ channelName, channelSelector: selector, clickSelector, clickToPlay, clientAddress, profileOverride, url });
228
299
  if (newStreamId === null) {
@@ -244,109 +315,46 @@ export async function handlePlayStream(req, res) {
244
315
  }
245
316
  res.redirect(302, "/hls/" + channelName + "/stream.m3u8");
246
317
  }
247
- // Startup Polling.
248
- /**
249
- * Polls for a stream startup to complete. The -1 sentinel in channelToStreamId signals that startup is in progress. Returns the resolved stream ID on success, null
250
- * if startup failed (sentinel removed), or undefined if the timeout expired while startup is still active.
251
- *
252
- * This is the shared inner polling loop used by both awaitStreamReady() (which sends error responses) and awaitStreamReadySilent() (which does not).
253
- *
254
- * @param channelName - The channel name (or synthetic ad-hoc key) to poll.
255
- * @returns The resolved stream ID, null if startup failed, or undefined if timed out.
256
- */
257
- async function pollStreamReady(channelName) {
258
- const pollInterval = 200;
259
- const deadline = Date.now() + CONFIG.streaming.navigationTimeout;
260
- while (Date.now() < deadline) {
261
- // eslint-disable-next-line no-await-in-loop
262
- await delay(pollInterval);
263
- const streamId = getChannelStreamId(channelName);
264
- // The startup failed and the sentinel was removed.
265
- if (streamId === undefined) {
266
- return null;
267
- }
268
- // Real stream ID is now available.
269
- if (streamId !== -1) {
270
- return streamId;
271
- }
272
- }
273
- // Timed out waiting for the startup to complete.
274
- return undefined;
275
- }
276
- /**
277
- * Waits for a stream startup to complete. This is used when a second request arrives while the first is still starting the stream. The -1 sentinel in
278
- * channelToStreamId signals that startup is in progress. We poll until the sentinel is replaced with a real stream ID, removed (startup failed), or the
279
- * timeout expires.
280
- *
281
- * On failure, the appropriate error response is sent to the client and null is returned.
282
- *
283
- * @param channelName - The channel name (or synthetic ad-hoc key) to poll.
284
- * @param res - Express response object for sending error responses on failure.
285
- * @returns The resolved stream ID on success, or null if startup failed or timed out (error response already sent).
286
- */
287
- async function awaitStreamReady(channelName, res) {
288
- const result = await pollStreamReady(channelName);
289
- // Startup failed (sentinel removed).
290
- if (result === null) {
291
- res.status(500).send("Stream startup failed.");
292
- return null;
293
- }
294
- // Timed out.
295
- if (result === undefined) {
296
- res.setHeader("Retry-After", "5");
297
- res.status(503).send("Stream is starting. Please retry.");
298
- return null;
299
- }
300
- return result;
301
- }
302
- /**
303
- * Waits for a stream startup to complete without sending any HTTP responses. Used by MPEG-TS when headers have already been flushed and error responses cannot be
304
- * sent.
305
- *
306
- * @param channelName - The channel name (or synthetic ad-hoc key) to poll.
307
- * @returns The resolved stream ID on success, or null if startup failed or timed out.
308
- */
309
- export async function awaitStreamReadySilent(channelName) {
310
- const result = await pollStreamReady(channelName);
311
- // Both null (failed) and undefined (timed out) map to null for the silent variant.
312
- if ((result === null) || (result === undefined)) {
313
- return null;
314
- }
315
- return result;
316
- }
317
318
  // Response Helpers.
318
319
  /**
319
- * Sends the playlist for a stream, waiting for the first playlist if needed. Handles client registration and access tracking. This is the shared pattern used by
320
- * multiple code paths in handleHLSPlaylist() to avoid duplicating the get-wait-check-register-send sequence.
320
+ * Sends the playlist for a stream. With the deferred preroll timer, the playlist may not be available immediately after stream registration — it arrives when either
321
+ * the timer fires (seeding preroll) or the segmenter produces real content, whichever comes first. This function awaits the playlistReady promise to handle that
322
+ * window. For the blocking fallback path (no FFmpeg / no preroll), the playlist is guaranteed to exist because initializeStream blocks until the segmenter produces
323
+ * it. Returns 404 only if the stream was terminated or the playlist wait timed out.
321
324
  * @param streamId - The numeric stream ID.
322
325
  * @param clientAddress - Client address for tracking.
323
326
  * @param res - Express response object.
324
327
  */
325
328
  async function sendPlaylistResponse(streamId, clientAddress, res) {
326
- // Try to get an existing playlist first.
327
329
  let playlist = getPlaylist(streamId);
328
- // If no playlist yet, wait for the first one.
330
+ // The playlist may not be populated yet if the deferred preroll timer hasn't fired and the segmenter hasn't produced content. Wait for the playlistReady promise
331
+ // which resolves when either source provides a playlist. The 30-second timeout covers pathological cases like setup hanging.
329
332
  if (!playlist) {
330
- const playlistReady = await waitForPlaylist(streamId, CONFIG.streaming.navigationTimeout);
331
- if (!playlistReady) {
332
- res.setHeader("Retry-After", "5");
333
- res.status(503).send("Stream is starting. Please retry.");
334
- return;
335
- }
336
- playlist = getPlaylist(streamId);
337
- if (!playlist) {
338
- res.status(500).send("Playlist not available.");
339
- return;
340
- }
341
- // Log the time from stream start to first playlist delivery. This only fires for the initial playlist wait, not for subsequent playlist polls.
342
- const stream = getStream(streamId);
343
- if (stream) {
344
- const elapsed = ((Date.now() - stream.startTime.getTime()) / 1000).toFixed(3);
345
- LOG.debug("timing:hls", "Playlist delivered to client in %ss.", elapsed);
333
+ const ready = await waitForPlaylist(streamId, 30_000);
334
+ if (ready) {
335
+ playlist = getPlaylist(streamId);
346
336
  }
347
337
  }
338
+ if (!playlist) {
339
+ res.status(404).send("Stream not found.");
340
+ return;
341
+ }
348
342
  updateLastAccess(streamId);
349
343
  registerClient(streamId, clientAddress, "hls");
344
+ // When still serving preroll, regenerate the progressive playlist on each poll so the sliding window advances based on elapsed wall-clock time. This simulates a
345
+ // live stream — the client sees new segments appear on each poll and keeps playing without stalling. Once real content arrives, hasRealPlaylist becomes true and
346
+ // the segmenter's playlist takes over.
347
+ const stream = getStream(streamId);
348
+ if (stream && !stream.hls.hasRealPlaylist) {
349
+ if (stream.hls.prerollBaseUrl && stream.hls.prerollStartTime) {
350
+ playlist = generatePrerollPlaylist(stream.hls.prerollBaseUrl, stream.hls.resumeSegmentIndex, stream.hls.prerollStartTime);
351
+ }
352
+ LOG.debug("streaming:preroll", "Serving preroll playlist for stream %d.", streamId);
353
+ }
354
+ if (stream?.preTuned) {
355
+ stream.preTuned = false;
356
+ LOG.debug("streaming:pretune", "Cleared pretune flag for stream %d on first client connection from %s.", streamId, clientAddress);
357
+ }
350
358
  sendPlaylist(playlist, res);
351
359
  }
352
360
  /**
@@ -360,13 +368,15 @@ function sendPlaylist(playlist, res) {
360
368
  res.send(playlist);
361
369
  }
362
370
  /**
363
- * Sends a segment buffer as a video/mp4 response with appropriate headers.
371
+ * Sends a segment buffer with appropriate Content-Type headers. MPEG-TS segments (.ts) get video/MP2T, fMP4 segments (.m4s and init.mp4) get video/mp4.
364
372
  * @param data - The segment data.
373
+ * @param segmentName - The segment filename (used to determine Content-Type).
365
374
  * @param res - Express response object.
366
375
  */
367
- function sendSegment(data, res) {
376
+ function sendSegment(data, segmentName, res) {
377
+ const contentType = segmentName.endsWith(".ts") ? "video/MP2T" : "video/mp4";
368
378
  res.setHeader("Cache-Control", "no-cache");
369
- res.setHeader("Content-Type", "video/mp4");
379
+ res.setHeader("Content-Type", contentType);
370
380
  res.send(data);
371
381
  }
372
382
  // Stream Lifecycle.
@@ -393,6 +403,9 @@ function cleanupOrphanedSetup(segmenter) {
393
403
  *
394
404
  * The handler preserves existing HLS segments and marks a discontinuity so clients know the stream parameters may have changed.
395
405
  *
406
+ * Note: This handler is only invoked for capture-mode streams. Native-mode streams bypass video element monitoring entirely (the monitor early-returns for native
407
+ * streams), so evaluate timeouts that trigger tab replacement cannot occur.
408
+ *
396
409
  * @param numericStreamId - The stream's numeric ID for registry lookups.
397
410
  * @param streamId - The stream's string ID for logging.
398
411
  * @param channelName - The channel name (or synthetic ad-hoc key like "play-a1b2c3d4") used as the store key for error callbacks and termination.
@@ -435,14 +448,16 @@ function createTabReplacementHandler(numericStreamId, streamId, channelName, url
435
448
  LOG.debug("recovery:tab", "Stopping FFmpeg process for tab replacement.");
436
449
  stream.ffmpegProcess.kill();
437
450
  }
438
- // Close the current page.
451
+ // Close the current page. The page may be null for pending entries whose async setup has not yet completed.
439
452
  const oldPage = stream.page;
440
- unregisterManagedPage(oldPage);
441
- if (!oldPage.isClosed()) {
442
- LOG.debug("recovery:tab", "Closing unresponsive page for tab replacement.");
443
- oldPage.close().catch((error) => {
444
- LOG.debug("recovery:tab", "Page close error during tab replacement: %s.", formatError(error));
445
- });
453
+ if (oldPage) {
454
+ unregisterManagedPage(oldPage);
455
+ if (!oldPage.isClosed()) {
456
+ LOG.debug("recovery:tab", "Closing unresponsive page for tab replacement.");
457
+ oldPage.close().catch((error) => {
458
+ LOG.debug("recovery:tab", "Page close error during tab replacement: %s.", formatError(error));
459
+ });
460
+ }
446
461
  }
447
462
  LOG.debug("timing:tab", "Old tab cleanup complete. (+%sms)", tabElapsed());
448
463
  // Create a new page with capture.
@@ -456,6 +471,7 @@ function createTabReplacementHandler(numericStreamId, streamId, channelName, url
456
471
  },
457
472
  profile,
458
473
  streamId,
474
+ tabReplacement: true,
459
475
  url
460
476
  });
461
477
  }
@@ -507,167 +523,367 @@ function createTabReplacementHandler(numericStreamId, streamId, channelName, url
507
523
  };
508
524
  }
509
525
  /**
510
- * Initializes a new HLS stream. This is the shared stream startup logic used by both channel-based and ad-hoc streams. It handles browser capture setup, segmenter
511
- * creation, stream registration, and event emission.
526
+ * Initializes a new HLS stream. This is the blocking wrapper used by callers that need to wait for the full setup to complete (pretune, MPEG-TS, ad-hoc play).
527
+ * Registers a pending stream entry with the channel-to-stream mapping, then awaits the full async setup.
512
528
  *
513
- * A -1 sentinel is set in channelToStreamId during setup to prevent duplicate stream starts. On success, the sentinel is replaced with the real stream ID. On
514
- * failure, the sentinel is removed and the error is re-thrown for the caller to handle HTTP error responses appropriately (channel-based streams need HDHomeRun
515
- * headers, ad-hoc streams do not).
529
+ * For the non-blocking path used by HLS playlist requests, see registerPendingStream() + completeStreamSetup() called from ensureChannelStream().
516
530
  *
517
531
  * @param options - Stream initialization options.
518
- * @returns The stream ID on success, or null if the stream was terminated during the narrow setup window (orphaned setup race condition).
532
+ * @returns The stream ID on success, or null if the stream was terminated during setup.
519
533
  * @throws StreamSetupError if setup fails, or Error for unexpected failures.
520
534
  */
521
535
  export async function initializeStream(options) {
522
- const { channel, channelName, channelSelector, clickSelector, clickToPlay, clientAddress, profileOverride, url } = options;
523
- // Set a -1 sentinel to prevent duplicate stream starts while we're setting up.
524
- const startupSentinel = -1;
525
- setChannelStreamId(channelName, startupSentinel);
526
- let setup;
536
+ const { channel, channelName, url } = options;
537
+ // Allocate stream IDs. For predefined channels, use the channel name for the stream ID prefix. For ad-hoc streams, omit it so generateStreamId derives a prefix
538
+ // from the URL (e.g., "foxsports-abc123"), which is more informative in logs.
539
+ const numericStreamId = getNextStreamId();
540
+ const streamIdStr = generateStreamId(channel ? channelName : undefined, url);
541
+ // Register a pending entry in the stream registry. This allows concurrent requests for the same channel to find the stream immediately.
542
+ createPendingEntry({ ...options, hls: createHLSState(), numericStreamId, streamIdStr });
543
+ try {
544
+ return await completeStreamSetup({ ...options, numericStreamId, streamIdStr });
545
+ }
546
+ catch (error) {
547
+ // Skip logging — callers (startHLSStream, handlePlayStream, handleMpegTsStream) handle the re-thrown error with their own error responses and logging.
548
+ handleSetupFailure(numericStreamId, channelName, channel, error, false);
549
+ throw error;
550
+ }
551
+ }
552
+ /**
553
+ * Registers a pending stream entry in the registry with deferred preroll. This is the synchronous Phase 1 of the two-phase stream initialization used by the HLS
554
+ * playlist handler. The pending entry has a real stream ID but no playlist yet — the response is held until either the preroll timer fires (after PREROLL_DELAY_MS)
555
+ * or real content arrives from the segmenter/native proxy. This ensures that fast-tuning streams (native, most capture providers) skip preroll entirely, while slow
556
+ * streams (Xfinity at 13-15s) get preroll content after the delay.
557
+ * @param channelName - The channel key for registration and deduplication.
558
+ * @param channel - The resolved channel definition.
559
+ * @param clientAddress - Client IP address for Channels DVR API integration.
560
+ * @param req - Express request object for deriving the base URL.
561
+ * @returns The allocated stream IDs.
562
+ */
563
+ function registerPendingStream(channelName, channel, clientAddress, req) {
564
+ const numericStreamId = getNextStreamId();
565
+ const streamIdStr = generateStreamId(channelName, channel.url);
566
+ // Derive the base URL from the request for absolute preroll segment URLs.
567
+ const protocol = req.protocol;
568
+ const forwardedHost = req.get("x-forwarded-host");
569
+ const host = forwardedHost ? forwardedHost.split(",")[0].trim() : req.get("host");
570
+ const fallbackHost = CONFIG.server.host + ":" + String(CONFIG.server.port);
571
+ const baseUrl = protocol + "://" + (host ?? fallbackHost);
572
+ // Create HLS state with a deferred preroll timer. The timer fires after PREROLL_DELAY_MS — if stream setup hasn't completed by then, preroll is seeded and the
573
+ // playlist response is unblocked. For resume streams, the preroll's MEDIA-SEQUENCE is offset by the saved segment index so it continues from the prior session's
574
+ // sequence range. The timer is cancelled by completeStreamSetup() when the segmenter or native proxy is created, preventing races.
575
+ const hls = createHLSState();
576
+ // Snapshot the resume segment index once at registration. Both the preroll timer callback and completeStreamSetup() use this single snapshot, eliminating the TTL
577
+ // race that would occur if each read the resume map independently at different times.
578
+ hls.resumeSegmentIndex = getResumeSegmentIndex(channelName) ?? 0;
579
+ // Capture the stream start time at registration. This timestamp is used for the registry's startTime field (stream age display, etc.).
580
+ const streamStartTime = new Date();
581
+ if (isPrerollReady()) {
582
+ // Set preroll metadata immediately at registration so the segmenter and native proxy can read it regardless of whether the deferred timer has fired. The segment
583
+ // count and base URL are structural properties of the preroll system — they determine segment index offsets and composite playlist behavior. The timer controls
584
+ // only the client-facing timing (when the standalone preroll playlist begins serving). Decoupling these ensures correct behavior for fast-tune/slow-proxy
585
+ // scenarios (e.g., Fox native at 5s tune but 15s proxy first poll) where setup completes before the timer fires.
586
+ hls.prerollBaseUrl = baseUrl;
587
+ hls.prerollSegmentCount = getPrerollSegmentCount();
588
+ hls.prerollTimer = setTimeout(() => {
589
+ // Record the preroll start time and seed the initial progressive playlist. On subsequent polls, sendPlaylistResponse() regenerates the playlist with an
590
+ // advancing window based on elapsed time from this start time, simulating a live stream.
591
+ hls.prerollStartTime = new Date();
592
+ hls.playlist = generatePrerollPlaylist(baseUrl, hls.resumeSegmentIndex, hls.prerollStartTime);
593
+ hls.signalPlaylistReady();
594
+ }, PREROLL_DELAY_MS);
595
+ }
596
+ // Register the pending entry.
597
+ createPendingEntry({ channel, channelName, clientAddress, hls, numericStreamId, preTuned: false, streamIdStr, streamStartTime, url: channel.url });
598
+ return { numericStreamId, streamIdStr };
599
+ }
600
+ /**
601
+ * Creates a pending stream entry in the registry and sets the channel-to-stream mapping. The entry has null page, profile, segmenter, and other browser-related
602
+ * fields that are filled in asynchronously by completeStreamSetup(). This is the shared core for both the non-blocking preroll path (registerPendingStream) and the
603
+ * blocking path (initializeStream).
604
+ * @param options - Pending entry options.
605
+ */
606
+ function createPendingEntry(options) {
607
+ const { channel, channelName, hls, numericStreamId, streamIdStr, url } = options;
608
+ registerStream({
609
+ channelName: channel?.name ?? null,
610
+ clientAddress: options.clientAddress ?? null,
611
+ ffmpegProcess: null,
612
+ hls,
613
+ id: numericStreamId,
614
+ info: {
615
+ lastPlaylistRequest: Date.now(),
616
+ storeKey: channelName
617
+ },
618
+ mpegTsClientCount: 0,
619
+ nativeProxy: null,
620
+ page: null,
621
+ preTuned: options.preTuned ?? false,
622
+ profile: null,
623
+ rawCaptureStream: null,
624
+ segmenter: null,
625
+ startTime: options.streamStartTime ?? new Date(),
626
+ stopMonitor: null,
627
+ streamIdStr,
628
+ streamingMode: "capture",
629
+ url
630
+ });
631
+ setChannelStreamId(channelName, numericStreamId);
632
+ }
633
+ /**
634
+ * Handles stream setup failure by marking channel health, terminating the pending entry, and optionally logging the error. The blocking path (initializeStream) skips
635
+ * logging because it re-throws for callers to handle. The non-blocking path (ensureChannelStream .catch) logs because there is no outer handler.
636
+ * @param numericStreamId - The pending entry's numeric stream ID.
637
+ * @param channelName - The channel key for health tracking and termination.
638
+ * @param channel - The channel definition, or undefined for ad-hoc streams (no health tracking).
639
+ * @param error - The error that caused the failure.
640
+ * @param logError - Whether to log the error. False when the caller will re-throw (blocking path), true when fire-and-forget (non-blocking path).
641
+ */
642
+ function handleSetupFailure(numericStreamId, channelName, channel, error, logError = true) {
643
+ // Mark channel health as failed. Only for predefined channels (channel is defined). Ad-hoc URL streams have no persistent channel identity.
644
+ if (channel) {
645
+ const failVariantKey = resolveProviderKey(channelName);
646
+ const failAuthDomain = getAuthDomainForChannel(failVariantKey);
647
+ markChannelFailure(channelName, failAuthDomain);
648
+ }
649
+ // Clean up the pending entry.
650
+ terminateStream(numericStreamId, channelName, "setup failed");
651
+ void emitCurrentSystemStatus();
652
+ // Only log when the error won't be caught and logged by an outer handler (non-blocking fire-and-forget path).
653
+ if (logError) {
654
+ if (error instanceof StreamSetupError) {
655
+ LOG.warn("Stream setup failed for %s: %s.", channelName, error.userMessage);
656
+ }
657
+ else {
658
+ LOG.error("Unexpected error during stream setup for %s: %s.", channelName, formatError(error));
659
+ }
660
+ }
661
+ }
662
+ /**
663
+ * Completes the async portion of stream initialization. Creates the browser page, navigates to the URL, sets up capture, creates the segmenter, and fills in the
664
+ * pending registry entry. On failure, cleans up the pending entry via terminateStream().
665
+ *
666
+ * This is the Phase 2 of the two-phase stream initialization. For the non-blocking HLS path, it runs as fire-and-forget via `void`. For the blocking path
667
+ * (initializeStream), it is awaited directly.
668
+ *
669
+ * @param options - Stream setup options including pre-allocated IDs.
670
+ * @returns The stream ID on success, or null if the stream was terminated during setup.
671
+ * @throws StreamSetupError if setup fails, or Error for unexpected failures.
672
+ */
673
+ async function completeStreamSetup(options) {
674
+ const { channel, channelName, channelSelector, clickSelector, clickToPlay, mpegTsClient, numericStreamId, profileOverride, streamIdStr, url } = options;
527
675
  // Circuit breaker callback — terminate the stream on unrecoverable errors.
528
676
  const onCircuitBreak = () => {
529
- const streamId = getChannelStreamId(channelName);
530
- if ((streamId !== undefined) && (streamId !== startupSentinel)) {
531
- terminateStream(streamId, channelName, "too many errors");
677
+ const currentStreamId = getChannelStreamId(channelName);
678
+ if (currentStreamId !== undefined) {
679
+ terminateStream(currentStreamId, channelName, "too many errors");
532
680
  void emitCurrentSystemStatus();
533
681
  }
534
682
  };
535
- // Factory to create the tab replacement handler. Called by setupStream after stream IDs are generated, allowing the handler to be created with access to those IDs.
536
- const tabReplacementFactory = (numericStreamId, streamId, profile, metadataComment) => {
683
+ // Factory to create the tab replacement handler. Called by setupStream after resolving the profile, allowing the handler to be created with access to all context.
684
+ const tabReplacementFactory = (_, streamId, profile, metadataComment) => {
537
685
  return createTabReplacementHandler(numericStreamId, streamId, channelName, url, profile, metadataComment, onCircuitBreak);
538
686
  };
539
687
  // If at capacity, try to reclaim an idle stream before starting setup. This avoids rejecting new requests when idle streams can be freed.
540
688
  if (getStreamCount() >= CONFIG.streaming.maxConcurrentStreams) {
541
689
  reclaimIdleStream();
542
690
  }
543
- try {
544
- // Pass channelName to setupStream only for predefined channels. For ad-hoc streams, omitting it causes generateStreamId to derive the stream ID string from the
545
- // URL (e.g., "foxsports-abc123"), which is more informative in logs than the synthetic hash key.
546
- setup = await setupStream({
547
- channel,
548
- channelName: channel ? channelName : undefined,
549
- channelSelector: channel ? undefined : channelSelector,
550
- clickSelector: channel ? undefined : clickSelector,
551
- clickToPlay: channel ? undefined : clickToPlay,
552
- onTabReplacementFactory: tabReplacementFactory,
553
- profileOverride,
554
- url
555
- }, onCircuitBreak);
556
- }
557
- catch (error) {
558
- // Mark channel health as failed. Only for predefined channels (channel is defined). Ad-hoc URL streams have no persistent channel identity.
559
- if (channel) {
560
- const failVariantKey = resolveProviderKey(channelName);
561
- const failProviderTag = getProviderTagForChannel(failVariantKey);
562
- markChannelFailure(channelName, failProviderTag);
563
- }
564
- // Remove startup sentinel on failure and re-throw for the caller to handle error responses.
565
- deleteChannelStreamId(channelName);
566
- throw error;
691
+ // Pass the pre-allocated IDs to setupStream so it uses them instead of generating new ones. This ensures the abort controller, health monitor, and tab replacement
692
+ // handler all reference the same stream identity as the pending registry entry. Pass channelName only for predefined channels — for ad-hoc streams, omitting it
693
+ // causes generateStreamId to derive the stream ID string from the URL (e.g., "foxsports-abc123"), which is more informative in logs.
694
+ const setup = await setupStream({
695
+ channel,
696
+ channelName: channel ? channelName : undefined,
697
+ channelSelector: channel ? undefined : channelSelector,
698
+ clickSelector: channel ? undefined : clickSelector,
699
+ clickToPlay: channel ? undefined : clickToPlay,
700
+ numericStreamId,
701
+ onTabReplacementFactory: tabReplacementFactory,
702
+ profileOverride,
703
+ streamId: streamIdStr,
704
+ url
705
+ }, onCircuitBreak);
706
+ // Fill in the pending registry entry with the real browser state. The entry was registered in Phase 1 (registerPendingStream or initializeStream).
707
+ const stream = getStream(numericStreamId);
708
+ if (!stream) {
709
+ // Stream was terminated during setup (e.g., idle timeout on the pending entry). Clean up setupStream resources.
710
+ setup.cleanup().catch((error) => {
711
+ LOG.debug("streaming:setup", "Cleanup error for terminated pending stream: %s.", formatError(error));
712
+ });
713
+ return null;
567
714
  }
568
- // Update the channel mapping with the real stream ID.
569
- setChannelStreamId(channelName, setup.numericStreamId);
715
+ stream.ffmpegProcess = setup.ffmpegProcess;
716
+ stream.page = setup.page;
717
+ stream.profile = setup.profile;
718
+ stream.rawCaptureStream = setup.rawCaptureStream;
719
+ stream.startTime = setup.startTime;
720
+ stream.stopMonitor = setup.stopMonitor;
721
+ stream.url = setup.url;
570
722
  // Continue within stream context for consistent logging.
571
- return runWithStreamContext({ channelName: channel?.name, streamId: setup.streamId, url: setup.url },
572
- // eslint-disable-next-line @typescript-eslint/require-await
573
- async () => {
574
- // Register with null segmenter first because segmenter callbacks (onError, onStop) need the stream to exist in the registry for cleanup logic. The segmenter is
575
- // assigned immediately after creation below.
576
- registerStream({
577
- channelName: channel?.name ?? null,
578
- clientAddress,
579
- ffmpegProcess: setup.ffmpegProcess,
580
- hls: createHLSState(),
581
- id: setup.numericStreamId,
582
- info: {
583
- lastPlaylistRequest: Date.now(),
584
- storeKey: channelName
585
- },
586
- mpegTsClientCount: 0,
587
- page: setup.page,
588
- profile: setup.profile,
589
- rawCaptureStream: setup.rawCaptureStream,
590
- segmenter: null,
591
- startTime: setup.startTime,
592
- stopMonitor: setup.stopMonitor,
593
- streamIdStr: setup.streamId,
594
- url: setup.url
595
- });
596
- // Check for resume data from a previous shutdown. If available, the segmenter will continue from the saved sequence numbers instead of starting at 0, preventing
597
- // HLS sequence resets that cause Channels DVR to produce broken recording timelines.
598
- const resumeData = consumeResumeData(channelName);
599
- // Create the native fMP4 segmenter to parse the MP4/AAC stream into HLS segments.
600
- const segmenter = createFMP4Segmenter({
601
- ...(resumeData ? {
602
- initialTrackTimestamps: resumeData.trackTimestamps,
603
- pendingDiscontinuity: true,
604
- previousInitSegment: resumeData.initSegment,
605
- startingInitVersion: resumeData.initVersion,
606
- startingSegmentIndex: resumeData.segmentIndex
607
- } : {}),
608
- onError: (error) => {
609
- // Skip error handling if termination was already initiated.
610
- if (isTerminationInitiated(setup.numericStreamId)) {
611
- return;
723
+ return runWithStreamContext({ channelName: channel?.name, streamId: setup.streamId, url: setup.url }, async () => {
724
+ // The deferred preroll timer is NOT cancelled here. It continues running until the first real playlist arrives (cancelled in updatePlaylist() in
725
+ // hlsSegments.ts). This is critical for native streams where the browser setup completes quickly but the native proxy's first poll cycle can take 10-15+
726
+ // seconds. If we cancelled the timer at setup completion, the client would have no playlist during the proxy's first poll — the preroll timer fires after
727
+ // PREROLL_DELAY_MS and provides content during that gap. For the capture path, the segmenter produces its first playlist within ~2 seconds of creation, so
728
+ // the timer is cancelled almost immediately after setup anyway.
729
+ // Attempt native streaming if a manifest interception handle is available. Signal finalize() to tell the interceptor that channel selection is complete and it
730
+ // should resolve with the most recently captured manifest URL. For direct-navigation sites, the manifest was captured during page load and finalize resolves
731
+ // immediately. For guide-based sites (Fox, Hulu, etc.), the manifest from the correct channel was captured after the strategy clicked the right entry.
732
+ let streamingMode = "capture";
733
+ if (setup.manifestInterception) {
734
+ setup.manifestInterception.finalize(setup.directTune);
735
+ // Read the preroll segment count from the pending entry to pass to the native coordinator. This value is set at registration time (not in the timer callback),
736
+ // so it's available regardless of whether the deferred preroll timer has fired. The proxy uses it for segment index offset. The base URL for composite playlists
737
+ // is read dynamically from the stream's HLS state at playlist generation time.
738
+ const pendingForNative = getStream(numericStreamId);
739
+ const nativePrerollSegmentCount = pendingForNative?.hls.prerollSegmentCount ?? 0;
740
+ const nativeResult = await attemptNativeStreaming({
741
+ channelName,
742
+ interceptionPromise: setup.manifestInterception.promise,
743
+ mpegTsClient,
744
+ onError: (error) => {
745
+ if (isTerminationInitiated(numericStreamId)) {
746
+ return;
747
+ }
748
+ // Log the error and clear the probe cache. The proxy has already stopped itself (set errorThresholdReached + stopped). The monitor detects
749
+ // hasErrored() on the next 2-second tick and triggers L3 fallback to capture mode, preserving the stream for the DVR client.
750
+ LOG.warn("Native proxy error for %s: %s. Falling back to capture.", channelName, error);
751
+ clearProbeCache(channelName);
752
+ },
753
+ page: setup.page,
754
+ prerollSegmentCount: nativePrerollSegmentCount,
755
+ streamId: numericStreamId,
756
+ streamIdStr: setup.streamId,
757
+ url
758
+ });
759
+ if (nativeResult) {
760
+ const currentStream = getStream(numericStreamId);
761
+ if (!currentStream) {
762
+ nativeResult.proxy.stop();
763
+ return null;
612
764
  }
613
- LOG.error("Segmenter error for %s: %s.", channelName, formatError(error));
614
- terminateStream(setup.numericStreamId, channelName, "stream processing error");
615
- void emitCurrentSystemStatus();
616
- },
617
- onStop: () => {
618
- // Skip handling if termination was already initiated.
619
- if (isTerminationInitiated(setup.numericStreamId)) {
620
- return;
765
+ // Stop the capture pipeline — native streaming replaces it entirely.
766
+ if (!setup.rawCaptureStream.destroyed) {
767
+ setup.rawCaptureStream.destroy();
621
768
  }
622
- LOG.error("Segmenter stopped unexpectedly for %s.", channelName);
623
- terminateStream(setup.numericStreamId, channelName, "stream ended unexpectedly");
624
- void emitCurrentSystemStatus();
625
- },
626
- streamId: setup.numericStreamId
627
- });
628
- // Pipe the capture stream to the segmenter.
629
- segmenter.pipe(setup.captureStream);
630
- // Store the segmenter reference in the registry.
631
- const stream = getStream(setup.numericStreamId);
632
- if (stream) {
633
- stream.segmenter = segmenter;
769
+ if (setup.ffmpegProcess) {
770
+ setup.ffmpegProcess.kill();
771
+ }
772
+ // Update the registry entry to reflect native mode. For streams with separate audio, clear preroll state — preroll is muxed video+audio and can't be
773
+ // split into separate renditions. For muxed-audio streams, preserve preroll state so the proxy can build composite playlists with preroll entries.
774
+ currentStream.ffmpegProcess = null;
775
+ currentStream.hls.hasAudio = nativeResult.hasAudio;
776
+ if (nativeResult.hasAudio) {
777
+ currentStream.hls.prerollBaseUrl = null;
778
+ currentStream.hls.prerollSegmentCount = 0;
779
+ }
780
+ currentStream.nativeProxy = nativeResult.proxy;
781
+ currentStream.rawCaptureStream = null;
782
+ currentStream.streamingMode = "native";
783
+ streamingMode = "native";
784
+ // Start the native proxy. Signal init segment readiness immediately — native MPEG-TS segments carry their own PAT/PMT codec configuration in every
785
+ // segment, so there is no separate init segment to wait for. Without this, MPEG-TS clients block on waitForInitSegment() and time out before the proxy's
786
+ // first poll cycle completes.
787
+ nativeResult.proxy.start();
788
+ currentStream.hls.signalInitSegmentReady();
789
+ LOG.debug("native:coordinator", "Capture pipeline stopped for %s. Native proxy active.", channelName);
790
+ }
634
791
  }
635
- else {
636
- // Stream was terminated during setup (rare race condition). Clean up the orphaned segmenter.
637
- cleanupOrphanedSetup(segmenter);
638
- return null;
792
+ // If native streaming was not viable or not attempted, create the fMP4 segmenter for capture mode.
793
+ if (streamingMode === "capture") {
794
+ // Check for resume data from a previous shutdown. If available, the segmenter continues from the saved sequence numbers instead of starting at 0,
795
+ // preventing HLS sequence resets that cause Channels DVR to produce broken recording timelines. Preroll and resume work together — the preroll playlist
796
+ // is already offset by the resume segment index (set in registerPendingStream), and the segmenter starts after the preroll range.
797
+ const resumeData = consumeResumeData(channelName);
798
+ const currentStream = getStream(numericStreamId);
799
+ const prerollSegmentCount = currentStream?.hls.prerollSegmentCount ?? 0;
800
+ // When preroll is active, use the snapshotted resume index (stored on HLS state at registration) so the segmenter's starting index is guaranteed to match
801
+ // the preroll playlist's MEDIA-SEQUENCE offset. When preroll is inactive, use the resume data directly — no preroll playlist to be consistent with.
802
+ const baseSegmentIndex = (prerollSegmentCount > 0) ? (currentStream?.hls.resumeSegmentIndex ?? 0) : (resumeData?.segmentIndex ?? 0);
803
+ // Create the fMP4 segmenter. The starting segment index accounts for both the resume offset and the preroll segment range. When preroll is active, the
804
+ // segmenter includes preroll entries in its sliding window via the compositor. The pending discontinuity at the preroll→real boundary is always needed —
805
+ // previousInitSegment is only passed without preroll, because the preroll init segment differs from the real init and the discontinuity must not be
806
+ // suppressed by an init-match comparison against the prior session.
807
+ const segmenter = createFMP4Segmenter({
808
+ ...(resumeData ? {
809
+ initialTrackTimestamps: resumeData.trackTimestamps,
810
+ ...((prerollSegmentCount === 0) ? { previousInitSegment: resumeData.initSegment } : {}),
811
+ startingInitVersion: resumeData.initVersion
812
+ } : {}),
813
+ ...((prerollSegmentCount > 0) ? {
814
+ prerollBaseUrl: currentStream?.hls.prerollBaseUrl ?? null,
815
+ prerollSegmentCount
816
+ } : {}),
817
+ ...((resumeData || (prerollSegmentCount > 0)) ? {
818
+ pendingDiscontinuity: true,
819
+ startingSegmentIndex: baseSegmentIndex + prerollSegmentCount
820
+ } : {}),
821
+ onError: (error) => {
822
+ // Skip error handling if termination was already initiated.
823
+ if (isTerminationInitiated(numericStreamId)) {
824
+ return;
825
+ }
826
+ LOG.error("Segmenter error for %s: %s.", channelName, formatError(error));
827
+ terminateStream(numericStreamId, channelName, "stream processing error");
828
+ void emitCurrentSystemStatus();
829
+ },
830
+ onStop: () => {
831
+ // Skip handling if termination was already initiated.
832
+ if (isTerminationInitiated(numericStreamId)) {
833
+ return;
834
+ }
835
+ LOG.error("Segmenter stopped unexpectedly for %s.", channelName);
836
+ terminateStream(numericStreamId, channelName, "stream ended unexpectedly");
837
+ void emitCurrentSystemStatus();
838
+ },
839
+ streamId: numericStreamId
840
+ });
841
+ // Pipe the capture stream to the segmenter.
842
+ segmenter.pipe(setup.captureStream);
843
+ // Store the segmenter reference in the registry.
844
+ const captureStream = getStream(numericStreamId);
845
+ if (captureStream) {
846
+ captureStream.segmenter = segmenter;
847
+ }
848
+ else {
849
+ // Stream was terminated during setup (rare race condition). Clean up the orphaned segmenter.
850
+ cleanupOrphanedSetup(segmenter);
851
+ return null;
852
+ }
639
853
  }
640
- const captureMode = CONFIG.streaming.captureMode === "ffmpeg" ? "FFmpeg" : "Native";
854
+ const captureMode = streamingMode === "native" ? "native HLS" : (CONFIG.streaming.captureMode === "ffmpeg" ? "FFmpeg" : "Native fMP4");
641
855
  const displayName = channel?.name ?? url;
642
856
  const tuneTime = ((Date.now() - setup.startTime.getTime()) / 1000).toFixed(1);
643
857
  LOG.info("Streaming %s (%s, %s). Tuned in %ss%s.", displayName, setup.profileName, captureMode, tuneTime, setup.directTune ? " (direct)" : "");
644
- // Mark channel health as successful. Only for predefined channels (channel is defined). Ad-hoc URL streams have no persistent channel identity. Provider
645
- // auth is conditionally skipped when the provider defines validateTune and the tuned channel does not prove paid access (e.g., Sling Freestream channels).
858
+ // Mark channel health as successful. Only for predefined channels (channel is defined). Ad-hoc URL streams have no persistent channel identity. Domain auth
859
+ // is conditionally skipped when the provider defines validateTune and the tuned channel does not prove paid access (e.g., Sling Freestream channels).
646
860
  if (channel) {
647
861
  const successVariantKey = resolveProviderKey(channelName);
862
+ const successAuthDomain = getAuthDomainForChannel(successVariantKey);
648
863
  const successProviderTag = getProviderTagForChannel(successVariantKey);
649
864
  const provider = getProviderBySlug(successProviderTag);
650
865
  const markAuth = !provider?.validateTune || provider.validateTune(channel.channelSelector ?? channelName);
651
- markChannelSuccess(channelName, successProviderTag, markAuth);
866
+ markChannelSuccess(channelName, successAuthDomain, markAuth);
652
867
  }
653
868
  // Emit stream added event.
654
869
  emitStreamAdded(createInitialStreamStatus({
655
870
  channelName: channel?.name ?? null,
656
- numericStreamId: setup.numericStreamId,
871
+ numericStreamId,
657
872
  providerName: setup.providerName,
658
873
  startTime: setup.startTime,
874
+ streamingMode,
659
875
  url: setup.url
660
876
  }));
661
877
  void emitCurrentSystemStatus();
662
878
  // Trigger show name lookup for the new stream.
663
879
  triggerShowNameUpdate();
664
- return setup.numericStreamId;
880
+ return numericStreamId;
665
881
  });
666
882
  }
667
883
  // Channel Stream Startup.
668
884
  /**
669
- * Starts a new HLS stream for a predefined channel. Delegates to initializeStream() for the actual setup. Error responses are sent directly to the client, including
670
- * HDHomeRun-specific headers for capacity errors.
885
+ * Starts a new HLS stream for a predefined channel using the blocking path. Used as a fallback when the preroll is not available (no FFmpeg). Delegates to
886
+ * initializeStream() for the actual setup. Error responses are sent directly to the client, including HDHomeRun-specific headers for capacity errors.
671
887
  *
672
888
  * @param channelName - The channel key (canonical key for stream registration and deduplication).
673
889
  * @param url - The URL to stream (from the resolved provider).
@@ -705,6 +921,11 @@ export function cleanupIdleStreams() {
705
921
  const now = Date.now();
706
922
  let terminatedCount = 0;
707
923
  for (const stream of streams) {
924
+ // Skip pretuned streams. These have no clients by design and must not be terminated by the idle timer. The pretune module manages their lifecycle via a
925
+ // safety timeout.
926
+ if (stream.preTuned) {
927
+ continue;
928
+ }
708
929
  // Skip streams with active MPEG-TS clients. These streams are still being consumed even if no HLS playlist requests have been made recently.
709
930
  if (stream.mpegTsClientCount > 0) {
710
931
  continue;
@@ -731,6 +952,10 @@ function reclaimIdleStream() {
731
952
  const now = Date.now();
732
953
  let oldest = null;
733
954
  for (const stream of streams) {
955
+ // Skip pretuned streams — they should not be reclaimed for new stream capacity.
956
+ if (stream.preTuned) {
957
+ continue;
958
+ }
734
959
  // Skip streams with active MPEG-TS clients.
735
960
  if (stream.mpegTsClientCount > 0) {
736
961
  continue;