nixamp 0.28.35 → 0.28.37

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 (41) hide show
  1. package/dist/audio.d.ts +1 -1
  2. package/dist/audio.js +2 -2
  3. package/dist/channels.d.ts +10 -17
  4. package/dist/channels.js +93 -85
  5. package/dist/fragments.d.ts +5 -0
  6. package/dist/fragments.js +51 -0
  7. package/dist/playlist-input.d.ts +26 -0
  8. package/dist/playlist-input.js +95 -0
  9. package/dist/server.js +6 -7
  10. package/package.json +1 -1
  11. package/src/audio.ts +2 -2
  12. package/src/channels.ts +88 -85
  13. package/src/fragments.ts +46 -0
  14. package/src/playlist-input.ts +99 -0
  15. package/src/server.ts +6 -6
  16. package/web/dist/assets/{ar-B8r60Lgd.js → ar-DhdXPO3p.js} +1 -1
  17. package/web/dist/assets/{cs-C_jKpXHz.js → cs-CoTzYnZt.js} +1 -1
  18. package/web/dist/assets/{da-qhb4qzGL.js → da-M4GDhrcC.js} +1 -1
  19. package/web/dist/assets/{de-B1U3oniY.js → de-D7yI8cd_.js} +1 -1
  20. package/web/dist/assets/{es-jT9qQhJM.js → es-BMGb7XlT.js} +1 -1
  21. package/web/dist/assets/{fi-CD8a3RyK.js → fi-CXNsmIrh.js} +1 -1
  22. package/web/dist/assets/{fr-CBYEPF15.js → fr-BpvyrESQ.js} +1 -1
  23. package/web/dist/assets/{hi-DS7Jlgq3.js → hi-DO7CO-fI.js} +1 -1
  24. package/web/dist/assets/{hls-3VKVEQE3-BVtRqG0G.js → hls-3VKVEQE3-BCxdTCEU.js} +1 -1
  25. package/web/dist/assets/{hu-BZ__rJ87.js → hu-CosB3vlS.js} +1 -1
  26. package/web/dist/assets/{id-DkM6FK8S.js → id-DNumDlwq.js} +1 -1
  27. package/web/dist/assets/index-CY3lAwM2.js +3 -0
  28. package/web/dist/assets/index-jLCo7PIA.css +1 -0
  29. package/web/dist/assets/{it-DmhEUJ4D.js → it-IJKhyCGw.js} +1 -1
  30. package/web/dist/assets/{mpegts-DGMTjRf5.js → mpegts-DsEoUEPH.js} +1 -1
  31. package/web/dist/assets/{mpegts-LO6RVLD6-Cct2UCqZ.js → mpegts-LO6RVLD6-CC8jMmPy.js} +1 -1
  32. package/web/dist/assets/{nl-D-gXjVr5.js → nl-BU0Mn-e-.js} +1 -1
  33. package/web/dist/assets/{ru-BbqAccEb.js → ru-ClqNCOTW.js} +1 -1
  34. package/web/dist/assets/{sv-BMdjgsVR.js → sv-Df_aXTD6.js} +1 -1
  35. package/web/dist/assets/{uk-DLs9Y4CC.js → uk-K5yQBF0h.js} +1 -1
  36. package/web/dist/assets/{vi-_Y4nCAgc.js → vi-CYfcOUeA.js} +1 -1
  37. package/web/dist/assets/{zh-DIJTp7RL.js → zh-a2v2x-3e.js} +1 -1
  38. package/web/dist/index.html +2 -2
  39. package/web/dist/sw.js +23 -23
  40. package/web/dist/assets/index-BaDD_PRM.css +0 -1
  41. package/web/dist/assets/index-Dkxbx4QN.js +0 -3
package/dist/audio.d.ts CHANGED
@@ -137,7 +137,7 @@ export interface Codecs {
137
137
  * answering other requests, and a synchronous probe per media request is how
138
138
  * the whole library came to be tagged with the process wedged solid.
139
139
  */
140
- export declare function codecsOf(tools: Tools, path: string, input?: string[]): Promise<Codecs>;
140
+ export declare function codecsOf(tools: Tools, path: string, input?: string[], signal?: AbortSignal): Promise<Codecs>;
141
141
  /**
142
142
  * The title, artist and album a file carries in its tags, however the
143
143
  * container spells the keys (ID3 says TIT2 but ffprobe says title; MP4 and
package/dist/audio.js CHANGED
@@ -356,7 +356,7 @@ function readTags(stdout, fallback) {
356
356
  * answering other requests, and a synchronous probe per media request is how
357
357
  * the whole library came to be tagged with the process wedged solid.
358
358
  */
359
- export async function codecsOf(tools, path, input = []) {
359
+ export async function codecsOf(tools, path, input = [], signal) {
360
360
  const [cmd, ...rest] = tools.ffprobe;
361
361
  const empty = { video: "", audio: "", container: "" };
362
362
  if (!cmd)
@@ -380,7 +380,7 @@ export async function codecsOf(tools, path, input = []) {
380
380
  // Headers the source's site expects, for a link resolved by yt-dlp.
381
381
  ...input,
382
382
  path,
383
- ], { stdio: ["ignore", "pipe", "ignore"] });
383
+ ], { stdio: ["ignore", "pipe", "ignore"], signal });
384
384
  let out = "";
385
385
  child.stdout.on("data", (chunk) => {
386
386
  out += chunk.toString("utf8");
@@ -35,9 +35,9 @@ export interface ChannelInfo {
35
35
  redials?: number;
36
36
  /**
37
37
  * For a channel that plays a list: the entries, in order, and which one is
38
- * on. When one ends the next is dialled at once, and the last is followed
39
- * by the first: a station, not a file. A restart picks up at the entry
40
- * that was on.
38
+ * on. Local files are normalized into one continuous stream. After the
39
+ * final entry the show ends. A restart picks up at the saved entry and
40
+ * position within it.
41
41
  */
42
42
  playlist?: string[];
43
43
  playlistAt?: number;
@@ -104,6 +104,7 @@ export interface PullResume {
104
104
  position: number;
105
105
  /** For a list of things rather than one: every entry, in order. */
106
106
  playlist?: string[];
107
+ playlistAt?: number;
107
108
  }
108
109
  /**
109
110
  * A source read by us rather than by ffmpeg: what to tell ffmpeg it is,
@@ -159,18 +160,9 @@ export declare const BACKLOG_AUDIO: number;
159
160
  * until the channel ended, however long that was.
160
161
  */
161
162
  export declare const LISTENER_QUEUE: number;
162
- /**
163
- * The backlog is really a number of seconds, and four megabytes was that
164
- * number for the stream we happened to have.
165
- *
166
- * Six seconds of 720p is about 4 MB. Six seconds of a 1080p transport stream
167
- * copied straight through is nearer 12, and of 4K nearer 30 -- so a fixed
168
- * 4 MB hands a 4K joiner under a second of video, which is the live edge with
169
- * no cushion, which is the play-wait-play loop the backlog exists to prevent.
170
- * So the cap follows the stream: seconds times the rate it is actually
171
- * running at, between the old floor and a ceiling that keeps a channel's
172
- * memory bounded whatever it is carrying.
173
- */
163
+ /** Media-time window for complete MP4 fragments. Byte limits are a separate
164
+ * memory ceiling; the measured byte rate is only a fallback for inputs with
165
+ * no usable fragment timestamps. It must never make low-bitrate video old. */
174
166
  export declare const BACKLOG_SECONDS = 6;
175
167
  export declare const BACKLOG_VIDEO_MAX: number;
176
168
  /**
@@ -187,6 +179,7 @@ export declare const RATE_WINDOW_MS = 5000;
187
179
  export declare function cleanId(value: unknown, fallback?: string): string;
188
180
  export interface ChannelOptions {
189
181
  ffmpeg: string[];
182
+ ffprobe?: string[];
190
183
  onStart?: (info: ChannelInfo) => void;
191
184
  onEnd?: (info: ChannelInfo) => void;
192
185
  /**
@@ -250,6 +243,8 @@ export declare class Channel {
250
243
  * can begin at; for MP3 any point will do, a frame announces itself.
251
244
  */
252
245
  private recent;
246
+ private fragmentClock;
247
+ private fragmentTimes;
253
248
  private recentBytes;
254
249
  /** The rate window: when it opened, what has arrived in it, and what the last closed one measured. */
255
250
  private rateStart;
@@ -266,8 +261,6 @@ export declare class Channel {
266
261
  private readonly sourceTaps;
267
262
  /** Pulls the plug on the current read-through, when there is one. */
268
263
  private throughAbort;
269
- /** ffmpeg concat manifest for a local multi-file live. */
270
- private concatListPath;
271
264
  constructor(info: ChannelInfo, options: ChannelOptions, onGone: (id: string) => void);
272
265
  start(format: string): void;
273
266
  /**
package/dist/channels.js CHANGED
@@ -16,10 +16,10 @@
16
16
  */
17
17
  import { spawn } from "node:child_process";
18
18
  import { randomBytes } from "node:crypto";
19
- import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
20
- import { dirname, join } from "node:path";
21
- import { tmpdir } from "node:os";
22
- import { Fragments, isOpening } from "./fragments.js";
19
+ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
20
+ import { join } from "node:path";
21
+ import { PlaylistInputStream } from "./playlist-input.js";
22
+ import { FragmentClock, Fragments, isOpening } from "./fragments.js";
23
23
  /**
24
24
  * How far back of the saved place a film is picked up from, in seconds. The
25
25
  * place is written down every so often and a restart lands between two
@@ -65,18 +65,9 @@ export const BACKLOG_AUDIO = 64 * 1024;
65
65
  * until the channel ended, however long that was.
66
66
  */
67
67
  export const LISTENER_QUEUE = 16 * 1024 * 1024;
68
- /**
69
- * The backlog is really a number of seconds, and four megabytes was that
70
- * number for the stream we happened to have.
71
- *
72
- * Six seconds of 720p is about 4 MB. Six seconds of a 1080p transport stream
73
- * copied straight through is nearer 12, and of 4K nearer 30 -- so a fixed
74
- * 4 MB hands a 4K joiner under a second of video, which is the live edge with
75
- * no cushion, which is the play-wait-play loop the backlog exists to prevent.
76
- * So the cap follows the stream: seconds times the rate it is actually
77
- * running at, between the old floor and a ceiling that keeps a channel's
78
- * memory bounded whatever it is carrying.
79
- */
68
+ /** Media-time window for complete MP4 fragments. Byte limits are a separate
69
+ * memory ceiling; the measured byte rate is only a fallback for inputs with
70
+ * no usable fragment timestamps. It must never make low-bitrate video old. */
80
71
  export const BACKLOG_SECONDS = 6;
81
72
  export const BACKLOG_VIDEO_MAX = 48 * 1024 * 1024;
82
73
  /**
@@ -166,6 +157,8 @@ export class Channel {
166
157
  * can begin at; for MP3 any point will do, a frame announces itself.
167
158
  */
168
159
  recent = [];
160
+ fragmentClock = new FragmentClock();
161
+ fragmentTimes = new WeakMap();
169
162
  recentBytes = 0;
170
163
  /** The rate window: when it opened, what has arrived in it, and what the last closed one measured. */
171
164
  rateStart = 0;
@@ -182,8 +175,6 @@ export class Channel {
182
175
  sourceTaps = new Set();
183
176
  /** Pulls the plug on the current read-through, when there is one. */
184
177
  throughAbort = null;
185
- /** ffmpeg concat manifest for a local multi-file live. */
186
- concatListPath = null;
187
178
  constructor(info, options, onGone) {
188
179
  this.info = info;
189
180
  this.options = options;
@@ -240,44 +231,29 @@ export class Channel {
240
231
  this.fragments = new Fragments();
241
232
  const [command, ...prefix] = this.options.ffmpeg;
242
233
  this.info.live = resume.live;
243
- if (!resume.live)
234
+ if (!resume.live || resume.playlist?.length)
244
235
  this.info.position = Math.max(0, resume.position);
245
236
  // A list plays entry by entry: the one that is on is what gets dialled,
246
237
  // and an entry that ended is followed by the next, at once.
247
238
  const list = resume.playlist && resume.playlist.length > 0 ? resume.playlist : null;
248
- // A local directory live must be one ffmpeg input. Starting a new ffmpeg
249
- // process for every file makes browsers see a new MP4 stream at each
250
- // boundary, even when the HTTP listener stays attached. The concat
251
- // demuxer keeps timestamps and the output pipe continuous.
252
- const concatList = list && list.length > 1 && list.every((one) => !/^https?:\/\//i.test(one))
253
- ? list
254
- : null;
255
- if (this.concatListPath) {
256
- rmSync(this.concatListPath, { force: true });
257
- this.concatListPath = null;
258
- }
259
- if (concatList) {
260
- const dir = mkdtempSync(join(tmpdir(), "nixamp-playlist-"));
261
- this.concatListPath = join(dir, "playlist.txt");
262
- const quote = (one) => one.replaceAll("'", "'\\''");
263
- writeFileSync(this.concatListPath, concatList.map((one) => `file '${quote(one)}'`).join("\n") + "\n");
264
- }
239
+ const localPlaylist = list && list.length > 1 && list.every(one => !/^https?:\/\//i.test(one)) ? list : null;
265
240
  let at = 0;
266
241
  if (list) {
267
242
  this.info.playlist = list;
268
- at = Math.min(Math.max(0, this.info.playlistAt ?? 0), list.length - 1);
243
+ at = Math.min(Math.max(0, resume.playlistAt ?? this.info.playlistAt ?? 0), list.length - 1);
269
244
  this.info.playlistAt = at;
270
245
  }
271
246
  let current = list ? list[at] : source;
272
247
  // The last entry is the end of the show, not a way back to the first:
273
248
  // a list that played through is over, and says so with the outro.
274
- this.advance = list && list.length > 1 && !concatList
249
+ this.advance = list && list.length > 1 && !localPlaylist
275
250
  ? () => {
276
251
  if (at + 1 >= list.length)
277
252
  return false;
278
253
  at += 1;
279
254
  this.info.playlistAt = at;
280
255
  current = list[at];
256
+ this.info.position = 0;
281
257
  return true;
282
258
  }
283
259
  : null;
@@ -296,7 +272,7 @@ export class Channel {
296
272
  // the place was written down a moment ago and a moment of it twice is
297
273
  // better than a moment of it missing. A live source is joined as is,
298
274
  // and a film that has barely started is started.
299
- const from = resume.live ? 0 : Math.max(0, Math.floor((this.info.position ?? 0) - REWIND));
275
+ const from = resume.live && !list ? 0 : Math.max(0, Math.floor((this.info.position ?? 0) - REWIND));
300
276
  const seek = from > 0 ? ["-ss", String(from)] : [];
301
277
  // Read the source here rather than in ffmpeg, when a policy wants the
302
278
  // original bytes and the source is the kind that can be. ffmpeg then
@@ -306,26 +282,22 @@ export class Channel {
306
282
  this.throughAbort?.abort();
307
283
  this.throughAbort = null;
308
284
  // The outro is a file read by ffmpeg itself: a pipe cannot loop.
309
- const through = this.outroOn ? null : this.options.through?.(this.info, from, input, audio) ?? null;
310
- this.info.teed = through !== null;
311
- const concatInput = this.concatListPath
312
- ? [
313
- ...(paced ? ["-re"] : []),
314
- // Directory lives may contain different codecs, dimensions, or
315
- // timestamp bases. Copying the first file's streams can make
316
- // ffmpeg reject later entries and leave the audience on entry 1.
317
- // The output settings below normalize the entire concat stream.
318
- "-f", "concat", "-safe", "0", "-i", this.concatListPath,
319
- ]
320
- : null;
321
- const concatEncode = this.concatListPath && this.info.kind === "video"
322
- ? [
323
- "-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
324
- "-pix_fmt", "yuv420p", "-g", "48", "-keyint_min", "48", "-sc_threshold", "0",
325
- "-c:a", "aac", "-b:a", "160k", "-ac", "2",
326
- "-f", "mp4", "-movflags", "frag_keyframe+empty_moov+default_base_moof",
327
- ]
328
- : null;
285
+ const playlistInput = localPlaylist ? new PlaylistInputStream({
286
+ ffmpeg: this.options.ffmpeg,
287
+ ffprobe: this.options.ffprobe ?? this.options.ffmpeg.map(one => one.replace(/ffmpeg(?=(?:\.exe)?$)/, "ffprobe")),
288
+ files: localPlaylist, at: this.info.playlistAt ?? 0, position: from,
289
+ video: this.info.kind === "video", width: this.info.codecs?.width, height: this.info.codecs?.height,
290
+ }) : null;
291
+ const through = this.outroOn ? null : playlistInput
292
+ ? { format: "mpegts", open: async (signal) => playlistInput.open(signal) }
293
+ : this.options.through?.(this.info, from, input, audio) ?? null;
294
+ this.info.teed = through !== null && !playlistInput;
295
+ // AAC's AudioSpecificConfig is produced by the bitstream filter on
296
+ // its first packet. Writing an empty moov earlier omits that config:
297
+ // HLS remuxing then emits AAC with no ADTS headers and browsers fail.
298
+ const playlistEncode = !playlistInput ? null : this.info.kind === "video"
299
+ ? ["-c", "copy", "-bsf:a", "aac_adtstoasc", "-f", "mp4", "-movflags", "frag_keyframe+empty_moov+default_base_moof+delay_moov"]
300
+ : ["-af", "aresample=async=1:first_pts=0", ...encode];
329
301
  const child = spawn(command, [
330
302
  ...prefix,
331
303
  "-hide_banner",
@@ -345,7 +317,7 @@ export class Channel {
345
317
  // headers, which only a source read through us leaves out,
346
318
  // are no use to a pipe and are not here (a source that needs
347
319
  // them is not read through us in the first place).
348
- ...input,
320
+ ...(playlistInput ? ["-probesize", "32768", "-analyzeduration", "1000000"] : input),
349
321
  "-f", through.format,
350
322
  ...(paced ? ["-re"] : []),
351
323
  "-i", "pipe:0",
@@ -366,12 +338,12 @@ export class Channel {
366
338
  // What the source's site expects on the request: a user agent, a
367
339
  // referer, a cookie. A link resolved by yt-dlp comes with these,
368
340
  // and a CDN that got them from yt-dlp and not from us answers 403.
369
- ...(concatInput ?? [...input, ...seek, "-i", current]),
341
+ ...input, ...seek, "-i", current,
370
342
  // The sound, when the site keeps it apart from the picture: a
371
343
  // second input, dialled the same way, that the encode maps in.
372
344
  ...(audio ? [...remoteArgs, ...(paced ? ["-re"] : []), ...input, ...seek, "-i", audio] : []),
373
345
  ]),
374
- ...(concatEncode ?? encode),
346
+ ...(playlistEncode ?? encode),
375
347
  "pipe:1",
376
348
  ], { stdio: [through ? "pipe" : "ignore", "pipe", "pipe", "pipe"] });
377
349
  let sent = false;
@@ -386,14 +358,22 @@ export class Channel {
386
358
  let progress = "";
387
359
  child.stdio[3]?.on("data", (chunk) => {
388
360
  progress = (progress + chunk.toString("utf8")).slice(-4000);
389
- if (this.child !== child || resume.live)
361
+ if (this.child !== child || (resume.live && !list))
390
362
  return;
391
363
  const lines = progress.split("\n");
392
364
  progress = lines.pop() ?? "";
393
365
  for (const line of lines) {
394
366
  const match = /^out_time_us=(\d+)/.exec(line.trim());
395
- if (match)
396
- this.info.position = from + Number(match[1]) / 1e6;
367
+ if (match) {
368
+ const seconds = Number(match[1]) / 1e6;
369
+ if (playlistInput) {
370
+ const place = playlistInput.place(seconds);
371
+ this.info.playlistAt = place.at;
372
+ this.info.position = place.position;
373
+ }
374
+ else
375
+ this.info.position = from + seconds;
376
+ }
397
377
  }
398
378
  });
399
379
  child.stdio[3]?.on("error", () => undefined);
@@ -401,6 +381,8 @@ export class Channel {
401
381
  // An ffmpeg that was replaced can still have a chunk in the pipe.
402
382
  if (this.child !== child)
403
383
  return;
384
+ if (!sent)
385
+ this.info.error = undefined;
404
386
  sent = true;
405
387
  this.info.bytes += chunk.byteLength;
406
388
  this.rearm(child);
@@ -411,9 +393,14 @@ export class Channel {
411
393
  // Only the ffmpeg we are currently running gets to say the source
412
394
  // dropped. One that was killed to make way for a restart is not news.
413
395
  child.on("error", () => { if (this.child === child)
414
- this.dropped(sent); });
415
- child.on("close", () => { if (this.child === child)
416
- this.dropped(sent); });
396
+ this.dropped(sent, false); });
397
+ child.on("close", (code, signal) => {
398
+ if (this.child !== child)
399
+ return;
400
+ if (playlistInput?.error)
401
+ this.stderr = playlistInput.error.message;
402
+ this.dropped(sent, code === 0 && !signal && (!playlistInput || playlistInput.complete));
403
+ });
417
404
  };
418
405
  this.redial = dial;
419
406
  dial();
@@ -435,6 +422,7 @@ export class Channel {
435
422
  const kind = this.info.kind ?? "audio";
436
423
  const old = this.child;
437
424
  this.child = null;
425
+ this.throughAbort?.abort();
438
426
  if (this.watchdog)
439
427
  clearTimeout(this.watchdog);
440
428
  this.watchdog = null;
@@ -485,7 +473,7 @@ export class Channel {
485
473
  this.info.playlistAt = 0;
486
474
  this.startOver();
487
475
  const show = this.dialed;
488
- this.pull(show.source, show.encode, show.paced, show.stall, show.input, show.audio, { ...show.resume, position: 0 });
476
+ this.pull(show.source, show.encode, show.paced, show.stall, show.input, show.audio, { ...show.resume, position: 0, playlistAt: 0 });
489
477
  return true;
490
478
  }
491
479
  const dial = this.redial;
@@ -522,6 +510,8 @@ export class Channel {
522
510
  if (this.info.kind === "video")
523
511
  this.fragments = new Fragments();
524
512
  this.recent = [];
513
+ this.fragmentClock = new FragmentClock();
514
+ this.fragmentTimes = new WeakMap();
525
515
  this.recentBytes = 0;
526
516
  // A new source may be a different size of stream, and the rate measured
527
517
  // off the old one is not evidence about this one.
@@ -581,7 +571,7 @@ export class Channel {
581
571
  * worth dialling again, and a URL that has never once produced a byte is a
582
572
  * mistake somebody made, and retrying it for ever helps nobody.
583
573
  */
584
- dropped(sent) {
574
+ dropped(sent, clean = false) {
585
575
  if (this.closing || !this.redial)
586
576
  return;
587
577
  // An outro that stopped is over; there is nothing after it.
@@ -590,28 +580,28 @@ export class Channel {
590
580
  return;
591
581
  }
592
582
  this.child = null;
583
+ this.throughAbort?.abort();
593
584
  if (this.watchdog)
594
585
  clearTimeout(this.watchdog);
595
586
  this.watchdog = null;
596
587
  const said = lastLine(this.stderr);
597
588
  if (said)
598
589
  this.info.error = said;
599
- this.failures = sent ? 0 : this.failures + 1;
590
+ this.failures = sent && clean ? 0 : this.failures + 1;
600
591
  if (this.failures >= GIVE_UP) {
601
592
  this.close();
602
593
  return;
603
594
  }
604
- // A list moves on. An entry that played to its end is not a source that
605
- // dropped: the next is dialled now, and it is not a redial. One that
606
- // gave nothing is skipped the same way, counted as the failure it was,
607
- // so a list of dead links gives up rather than cycling for ever.
608
- const moved = this.advance?.() ?? false;
595
+ // Only a clean EOF advances a per-URL playlist. Decoder errors, a
596
+ // watchdog kill, or a failed normalized input recover at the saved place
597
+ // with bounded retries; an MP4 header alone is not a completed show.
598
+ const moved = clean ? this.advance?.() ?? false : false;
609
599
  // A show that ended: a film or a podcast that played to its end, a
610
600
  // list whose last entry did. That is not a source that dropped, and it
611
601
  // is not dialled again from the top; it is over, and the outro says
612
602
  // so. A live feed that stopped is a feed that dropped, and is redialled.
613
603
  const list = (this.info.playlist?.length ?? 0) > 0;
614
- if (sent && !moved && (this.info.live === false || list)) {
604
+ if (clean && sent && !moved && (this.info.live === false || list)) {
615
605
  void this.endShow();
616
606
  return;
617
607
  }
@@ -649,8 +639,25 @@ export class Channel {
649
639
  }
650
640
  const cap = this.backlogCap();
651
641
  for (const box of this.fragments.push(chunk)) {
642
+ const time = this.fragmentClock.read(box);
643
+ if (time !== null)
644
+ this.fragmentTimes.set(box, time);
652
645
  if (!isOpening(boxType(box)))
653
- this.remember(box, cap, true);
646
+ this.remember(box, time === null ? cap : BACKLOG_VIDEO_MAX, true);
647
+ if (time !== null) {
648
+ // Drop complete old fragments even when six seconds occupy only a
649
+ // few kilobytes (slides, static cameras, or a paused game screen).
650
+ while (this.recent.length > 0) {
651
+ const oldest = this.fragmentTimes.get(this.recent[0]);
652
+ // Audio frame rounding and floating-point subtraction must not
653
+ // retain one extra two-second fragment at the window boundary.
654
+ if (oldest === undefined || time - oldest < BACKLOG_SECONDS - 0.05)
655
+ break;
656
+ do {
657
+ this.recentBytes -= this.recent.shift().byteLength;
658
+ } while (this.recent.length && boxType(this.recent[0]) !== "moof");
659
+ }
660
+ }
654
661
  this.send(box);
655
662
  }
656
663
  }
@@ -830,8 +837,13 @@ export class Channel {
830
837
  // Wait for ffmpeg to take it, or for the pipe to go: a pipe that
831
838
  // closed never drains, and waiting on it would hold the read open.
832
839
  await new Promise((done) => {
833
- stdin.once("drain", done);
834
- stdin.once("close", done);
840
+ const finish = () => {
841
+ stdin.off("drain", finish);
842
+ stdin.off("close", finish);
843
+ done();
844
+ };
845
+ stdin.once("drain", finish);
846
+ stdin.once("close", finish);
835
847
  });
836
848
  }
837
849
  }
@@ -929,10 +941,6 @@ export class Channel {
929
941
  this.child = null;
930
942
  this.throughAbort?.abort();
931
943
  this.throughAbort = null;
932
- if (this.concatListPath) {
933
- rmSync(dirname(this.concatListPath), { force: true, recursive: true });
934
- this.concatListPath = null;
935
- }
936
944
  this.endTaps();
937
945
  try {
938
946
  child?.stdin?.end();
@@ -1240,7 +1248,7 @@ export function rememberedNow(channels) {
1240
1248
  kept.codecs = one.codecs;
1241
1249
  if (typeof one.live === "boolean")
1242
1250
  kept.live = one.live;
1243
- if (!one.live && typeof one.position === "number" && one.position > 0)
1251
+ if ((!one.live || one.playlist?.length) && typeof one.position === "number" && one.position > 0)
1244
1252
  kept.position = Math.floor(one.position);
1245
1253
  if (one.startedBy)
1246
1254
  kept.startedBy = one.startedBy;
@@ -35,6 +35,11 @@ export declare function firstBox(buffer: Buffer): {
35
35
  } | null;
36
36
  /** The boxes that describe the stream rather than carry it. */
37
37
  export declare function isOpening(type: string): boolean;
38
+ /** Decode timestamps use each track's own timescale, never its byte rate. */
39
+ export declare class FragmentClock {
40
+ private scales;
41
+ read(box: Buffer): number | null;
42
+ }
38
43
  /**
39
44
  * A fragmented MP4 arriving in pieces, handed back a box at a time.
40
45
  *
package/dist/fragments.js CHANGED
@@ -60,6 +60,57 @@ export function firstBox(buffer) {
60
60
  export function isOpening(type) {
61
61
  return type === "ftyp" || type === "moov";
62
62
  }
63
+ function children(box) {
64
+ let rest = box.subarray(box.readUInt32BE(0) === 1 ? BIG : HEADER);
65
+ const found = [];
66
+ for (;;) {
67
+ const next = firstBox(rest);
68
+ if (!next)
69
+ return found;
70
+ found.push(next.box);
71
+ rest = next.rest;
72
+ }
73
+ }
74
+ /** Decode timestamps use each track's own timescale, never its byte rate. */
75
+ export class FragmentClock {
76
+ scales = new Map();
77
+ read(box) {
78
+ const type = box.toString("latin1", 4, 8);
79
+ if (type === "moov") {
80
+ for (const track of children(box).filter(one => one.type === "trak")) {
81
+ const parts = children(track.bytes);
82
+ const tkhd = parts.find(one => one.type === "tkhd")?.bytes;
83
+ const mdia = parts.find(one => one.type === "mdia")?.bytes;
84
+ const mdhd = mdia && children(mdia).find(one => one.type === "mdhd")?.bytes;
85
+ if (!tkhd || !mdhd)
86
+ continue;
87
+ const idAt = tkhd[8] === 1 ? 28 : 20;
88
+ const scaleAt = mdhd[8] === 1 ? 28 : 20;
89
+ if (tkhd.length < idAt + 4 || mdhd.length < scaleAt + 4)
90
+ continue;
91
+ const scale = mdhd.readUInt32BE(scaleAt);
92
+ if (scale)
93
+ this.scales.set(tkhd.readUInt32BE(idAt), scale);
94
+ }
95
+ }
96
+ if (type !== "moof")
97
+ return null;
98
+ const times = [];
99
+ for (const track of children(box).filter(one => one.type === "traf")) {
100
+ const parts = children(track.bytes);
101
+ const tfhd = parts.find(one => one.type === "tfhd")?.bytes;
102
+ const tfdt = parts.find(one => one.type === "tfdt")?.bytes;
103
+ if (!tfhd || tfhd.length < 16 || !tfdt || tfdt.length < (tfdt[8] === 1 ? 20 : 16))
104
+ continue;
105
+ const scale = this.scales.get(tfhd.readUInt32BE(12));
106
+ if (!scale)
107
+ continue;
108
+ const time = tfdt[8] === 1 ? Number(tfdt.readBigUInt64BE(12)) : tfdt.readUInt32BE(12);
109
+ times.push(time / scale);
110
+ }
111
+ return times.length ? Math.min(...times) : null;
112
+ }
113
+ }
63
114
  /**
64
115
  * A fragmented MP4 arriving in pieces, handed back a box at a time.
65
116
  *
@@ -0,0 +1,26 @@
1
+ export interface PlaylistInput {
2
+ ffmpeg: string[];
3
+ ffprobe: string[];
4
+ files: string[];
5
+ at: number;
6
+ position: number;
7
+ video: boolean;
8
+ width?: number;
9
+ height?: number;
10
+ }
11
+ /** Decode each file independently; feed one persistent muxer a fixed stream
12
+ * profile and continuous timestamps. A concat demuxer cannot decode a list
13
+ * whose input codecs differ. Only one entry is encoded at a time, with pipe
14
+ * backpressure bounding read-ahead independently of the directory's size. */
15
+ export declare class PlaylistInputStream {
16
+ private readonly options;
17
+ private entries;
18
+ error: Error | null;
19
+ complete: boolean;
20
+ constructor(options: PlaylistInput);
21
+ place(seconds: number): {
22
+ at: number;
23
+ position: number;
24
+ };
25
+ open(signal: AbortSignal): AsyncGenerator<Buffer>;
26
+ }