@torrent-tv/proxy 2.107.84 → 2.107.85

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torrent-tv/proxy",
3
- "version": "2.107.84",
3
+ "version": "2.107.85",
4
4
  "description": "Torrent proxy client that exposes webseed-like HTTP stream endpoint.",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "repository": {
@@ -127,6 +127,14 @@ export class EncodeRun {
127
127
  #closedTimings = new Map();
128
128
  /** When the encoder named each closed piece on its channel, until it is published. */
129
129
  #namedAt = new Map();
130
+ /**
131
+ * What the run's own clock said when the encoder named each closed piece, until
132
+ * it is published: the moment, the run's own working time and the monotonic
133
+ * time. A piece's speed reading is taken from this, not from when it is
134
+ * published (torrent-tv/meta#414).
135
+ * @type {Map<string, { at: number, workingMs: number, monotonicAt: number }>}
136
+ */
137
+ #closedAt = new Map();
130
138
  #publicationError = null;
131
139
  #publicationCode = null;
132
140
  #inputTruncated = false;
@@ -186,14 +194,16 @@ export class EncodeRun {
186
194
  #stoppedBeforeFirstOutput = 0;
187
195
  #stoppedBeforeFirstNamed = 0;
188
196
 
189
- /** The film made and the run's own working time, as of the last progress report. */
190
- #workSample = null;
191
-
192
- /** Baseline work sample for this run's next speed reading. */
193
- #pieceSample = null;
197
+ /**
198
+ * Where the run's last speed reading ended: the film end of its piece on the
199
+ * output's timeline (the run's own start before any), the run's working time
200
+ * when that piece was named closed, and the monotonic time of that moment.
201
+ * The next reading is measured from here.
202
+ */
203
+ #lastReadingEnd = null;
194
204
 
195
- /** A closed, published piece is waiting for a usable progress sample. */
196
- #speedMeasurementPending = false;
205
+ /** When the run was built, which the idle time of the first reading is counted from. */
206
+ #builtAt = 0;
197
207
 
198
208
  /** @type {{ speed: number, at: number } | null} */
199
209
  #speedReading = null;
@@ -268,6 +278,10 @@ export class EncodeRun {
268
278
  * that writes it, so it arrives as a plain function rather than this class
269
279
  * knowing any naming.
270
280
  * @param {number} [params.startSeconds] - Where this run begins on the source timeline.
281
+ * @param {(index: number) => number | null} [params.pieceEndSeconds] - Where
282
+ * piece `index` ends on the same timeline `startSeconds` is taken from; its
283
+ * owner knows the timeline. A speed reading is the film between two such
284
+ * ends. Not finite: the piece gives no reading.
271
285
  * @param {number | null} [params.totalSeconds] - Source duration when known.
272
286
  */
273
287
  constructor({
@@ -293,6 +307,7 @@ export class EncodeRun {
293
307
  usesExplicitCuts = false,
294
308
  indexOfName,
295
309
  startSeconds = 0,
310
+ pieceEndSeconds,
296
311
  totalSeconds = null,
297
312
  monotonicNow,
298
313
  because = "no reason was given"
@@ -310,7 +325,9 @@ export class EncodeRun {
310
325
  this.progress = new RunProgress({ startSeconds, totalSeconds, now: this.now, monotonicNow: this.monotonicNow });
311
326
  /** Where this run's time went: its own work, or waiting, or stopped. */
312
327
  this.clock = new RunClock({ now: this.now });
313
- this.#pieceSample = { at: this.now(), monotonicAt: this.monotonicNow(), producedSeconds: 0, workingMs: 0 };
328
+ this.#builtAt = this.now();
329
+ this.#lastReadingEnd = { filmEndSeconds: startSeconds, workingMs: 0, monotonicAt: this.monotonicNow() };
330
+ this.pieceEndSeconds = typeof pieceEndSeconds === "function" ? pieceEndSeconds : () => null;
314
331
  this.onEnded = typeof onEnded === "function" ? onEnded : () => {};
315
332
  this.onProgress = typeof onProgress === "function" ? onProgress : () => {};
316
333
  this.onProduced = typeof onProduced === "function" ? onProduced : () => {};
@@ -461,17 +478,6 @@ export class EncodeRun {
461
478
  /** An input read of this run has its bytes. */
462
479
  inputWaitEnds() {
463
480
  this.clock.inputWaitEnds();
464
- if (this.#speedReading === null && this.#workSample !== null) {
465
- // Progress made while an input read was waiting belongs to the wait, not
466
- // to the encoder's rate. Start the first measurement from the latest
467
- // progress known when the input resumes.
468
- this.#pieceSample = {
469
- ...this.#workSample,
470
- at: this.now(),
471
- monotonicAt: this.monotonicNow(),
472
- workingMs: this.clock.workingMs()
473
- };
474
- }
475
481
  }
476
482
 
477
483
  /** @returns {boolean} Whether an input read of this run is waiting for bytes now. */
@@ -601,17 +607,9 @@ export class EncodeRun {
601
607
  }
602
608
 
603
609
  #reportProgress(report) {
610
+ // Progress reports tell how far the encoder is; they do not enter the speed
611
+ // reading, which is taken from the pieces it closed (torrent-tv/meta#414).
604
612
  this.progress.note(report);
605
- if (Number.isFinite(report.processedSeconds) || typeof report.outTime === "string") {
606
- const at = this.now();
607
- this.#workSample = {
608
- at,
609
- monotonicAt: this.progress.updatedAtMonotonic,
610
- producedSeconds: Math.max(0, this.progress.processedSeconds - this.progress.startPositionSeconds),
611
- workingMs: this.clock.workingMs(at)
612
- };
613
- if (this.#speedMeasurementPending) this.#measureSpeed();
614
- }
615
613
  this.onProgress(this.progress.snapshot());
616
614
  }
617
615
 
@@ -651,7 +649,9 @@ export class EncodeRun {
651
649
  if (name.length === 0) {
652
650
  continue;
653
651
  }
654
- this.#namedAt.set(name, this.now());
652
+ const namedAt = this.now();
653
+ this.#namedAt.set(name, namedAt);
654
+ this.#closedAt.set(name, { at: namedAt, workingMs: this.clock.workingMs(namedAt), monotonicAt: this.monotonicNow() });
655
655
  if (this.#publicationError) continue;
656
656
  if (this.#stopping) {
657
657
  // Flushed by our stop. Never published; it tells where the piece closed
@@ -704,6 +704,9 @@ export class EncodeRun {
704
704
  }
705
705
 
706
706
  #publishClosed(name, following = null, { quiet = false, tentative = false } = {}) {
707
+ // Read before the `finally` below deletes it: a reading is dated by the
708
+ // close of its piece, not by its publication.
709
+ const closedAt = this.#closedAt.get(name) ?? null;
707
710
  // ITS SERVED NAME, which is what whoever owns the disk gives it in answer.
708
711
  // ffmpeg writes a piece under a working name and reports that; the piece
709
712
  // becomes servable by being renamed, and everything below works in the
@@ -735,12 +738,12 @@ export class EncodeRun {
735
738
  if (!tentative || served) {
736
739
  this.#closedTimings.delete(name);
737
740
  this.#namedAt.delete(name);
741
+ this.#closedAt.delete(name);
738
742
  }
739
743
  }
740
744
 
741
745
  if (!served) return false;
742
- this.#speedMeasurementPending = true;
743
- this.#measureSpeed();
746
+ this.#measureSpeed(this.indexOfName(served), closedAt);
744
747
  if (!this.#stopping) {
745
748
  this.#provenName = served;
746
749
  }
@@ -760,27 +763,46 @@ export class EncodeRun {
760
763
  }
761
764
 
762
765
  /**
763
- * A published piece is measured against this run's last baseline over its
764
- * own working time. The initial baseline is the run start, advanced to the
765
- * latest known progress when input resumes, so a one-piece run can be
766
- * measured even when FFmpeg reports progress only once. Input waits remain
767
- * excluded by RunClock.
766
+ * A published piece gives a reading: the film between its end on the
767
+ * output's timeline and where the last reading ended (the run's start for
768
+ * the first), over the run's own working time between the moments the two
769
+ * pieces were named closed. Input waits and stops are not working time
770
+ * (`RunClock`); progress reports do not enter it. No reading when the end of
771
+ * the piece is unknown or no film or no working time lies between the two.
772
+ *
773
+ * @param {number | null} index - The published piece's number.
774
+ * @param {{ at: number, workingMs: number, monotonicAt: number } | null} closedAt - When it was named closed.
768
775
  */
769
- #measureSpeed() {
770
- const sample = this.#workSample;
771
- if (sample === null) {
776
+ #measureSpeed(index, closedAt) {
777
+ if (closedAt === null) {
778
+ return;
779
+ }
780
+ const filmEndSeconds = Number(this.pieceEndSeconds(index));
781
+ if (!Number.isFinite(filmEndSeconds)) {
772
782
  return;
773
783
  }
774
- const work = workDeltaBetween(this.#pieceSample, sample);
775
- if (work !== null) {
776
- this.#speedReading = { speed: work.workSeconds / work.workingSeconds, at: sample.at, ...work,
777
- captureOwner: "proxy-encoder", captureClock: "performance", captureClockEpoch: performance.timeOrigin,
778
- captureStartedAtMonotonic: this.#pieceSample.monotonicAt,
779
- captureCompletedAtMonotonic: sample.monotonicAt };
780
- this.#pieceSample = sample;
781
- this.#speedMeasurementPending = false;
782
- this.onSpeedMeasured(this.speedReading);
784
+ const last = this.#lastReadingEnd;
785
+ const work = workDeltaBetween(
786
+ { producedSeconds: last.filmEndSeconds, workingMs: last.workingMs },
787
+ { producedSeconds: filmEndSeconds, workingMs: closedAt.workingMs });
788
+ if (work === null) {
789
+ return;
790
+ }
791
+ const first = this.#speedReading === null;
792
+ this.#speedReading = { speed: work.workSeconds / work.workingSeconds, at: closedAt.at, ...work,
793
+ captureOwner: "proxy-encoder", captureClock: "performance", captureClockEpoch: performance.timeOrigin,
794
+ captureStartedAtMonotonic: last.monotonicAt,
795
+ captureCompletedAtMonotonic: closedAt.monotonicAt };
796
+ this.#lastReadingEnd = { filmEndSeconds, workingMs: closedAt.workingMs, monotonicAt: closedAt.monotonicAt };
797
+ if (first) {
798
+ const idleSeconds = Math.max(0, (closedAt.at - this.#builtAt) / 1000 - work.workingSeconds);
799
+ this.logger.info(
800
+ `encode-run #${this.from}..#${this.to} on ${this.address}: first speed reading ` +
801
+ `${this.#speedReading.speed.toFixed(2)}x — ${work.workSeconds.toFixed(3)}s of film to the close of #${index} ` +
802
+ `over ${work.workingSeconds.toFixed(3)}s of its own work, ${idleSeconds.toFixed(3)}s waiting for input or ` +
803
+ "stopped left out");
783
804
  }
805
+ this.onSpeedMeasured(this.speedReading);
784
806
  }
785
807
 
786
808
  /**
@@ -1154,6 +1154,11 @@ export class EncodeRuns {
1154
1154
  // judged finished — see getFileStream.
1155
1155
  usesExplicitCuts: Boolean(admittedInput || (cutTimes && cutTimes.length > 0)),
1156
1156
  startSeconds,
1157
+ // Where piece `index` ends on the timeline `startSeconds` is taken from:
1158
+ // the speed reading is the film between two such ends (torrent-tv/meta#414).
1159
+ pieceEndSeconds: (index) => Number.isInteger(index) && index >= 0 && index + 1 < session.timeline.published.length
1160
+ ? session.timeline.publishedStartOf(index + 1)
1161
+ : Number(session.file.durationSeconds) || null,
1157
1162
  totalSeconds: Number(session.file.durationSeconds) || null,
1158
1163
  spawn: (spawnArgs) =>
1159
1164
  spawn(this.#host.ffmpegBin, spawnArgs, {
@@ -11,8 +11,9 @@
11
11
  * A run's work sample states the film made and the milliseconds of the run's
12
12
  * OWN work, with input waits and stops taken out (`RunClock`). Two samples of
13
13
  * one run give its processing speed over the stretch between them; the run
14
- * takes them when it closes pieces (`EncodeRun.speedReading`), so there is no
15
- * window to choose.
14
+ * takes them at the moments it names pieces closed, with the end of each piece
15
+ * on the output's timeline as its film (`EncodeRun.speedReading`), so there is
16
+ * no window to choose and no progress report enters.
16
17
  */
17
18
 
18
19
  /**