node-av 6.2.0-beta.25 → 6.2.0-beta.27

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 (40) hide show
  1. package/README.md +1 -1
  2. package/dist/api/audio-frame-buffer.d.ts +368 -22
  3. package/dist/api/audio-frame-buffer.js +687 -100
  4. package/dist/api/audio-frame-buffer.js.map +1 -1
  5. package/dist/api/demuxer.d.ts +7 -3
  6. package/dist/api/demuxer.js +19 -7
  7. package/dist/api/demuxer.js.map +1 -1
  8. package/dist/api/encoder.d.ts +40 -3
  9. package/dist/api/encoder.js +61 -23
  10. package/dist/api/encoder.js.map +1 -1
  11. package/dist/api/fmp4-stream.d.ts +103 -3
  12. package/dist/api/fmp4-stream.js +151 -28
  13. package/dist/api/fmp4-stream.js.map +1 -1
  14. package/dist/api/index.d.ts +1 -1
  15. package/dist/api/index.js.map +1 -1
  16. package/dist/api/io-stream.js +21 -6
  17. package/dist/api/io-stream.js.map +1 -1
  18. package/dist/api/muxer.d.ts +275 -7
  19. package/dist/api/muxer.js +569 -62
  20. package/dist/api/muxer.js.map +1 -1
  21. package/dist/api/rtp-stream.d.ts +19 -2
  22. package/dist/api/rtp-stream.js +62 -15
  23. package/dist/api/rtp-stream.js.map +1 -1
  24. package/dist/api/scaler.d.ts +19 -4
  25. package/dist/api/scaler.js +21 -6
  26. package/dist/api/scaler.js.map +1 -1
  27. package/dist/api/utilities/packet-age-watchdog.d.ts +198 -0
  28. package/dist/api/utilities/packet-age-watchdog.js +594 -0
  29. package/dist/api/utilities/packet-age-watchdog.js.map +1 -0
  30. package/dist/lib/binding.d.ts +9 -1
  31. package/dist/lib/binding.js +13 -0
  32. package/dist/lib/binding.js.map +1 -1
  33. package/dist/lib/format-context.d.ts +7 -0
  34. package/dist/lib/format-context.js +7 -0
  35. package/dist/lib/format-context.js.map +1 -1
  36. package/dist/lib/frame.d.ts +6 -2
  37. package/dist/lib/frame.js +11 -3
  38. package/dist/lib/frame.js.map +1 -1
  39. package/dist/lib/native-types.d.ts +3 -1
  40. package/package.json +12 -12
@@ -1,9 +1,70 @@
1
- import { AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8P } from '../constants/constants.js';
1
+ import { AV_NOPTS_VALUE, AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8P } from '../constants/constants.js';
2
2
  import { AudioFifo } from '../lib/audio-fifo.js';
3
3
  import { FFmpegError } from '../lib/error.js';
4
4
  import { Frame } from '../lib/frame.js';
5
5
  import { Rational } from '../lib/rational.js';
6
- import { avGetBytesPerSample, avSampleFmtIsPlanar } from '../lib/utilities.js';
6
+ import { avGetBytesPerSample, avRescaleQ, avSampleFmtIsPlanar } from '../lib/utilities.js';
7
+ /**
8
+ * Deviation (seconds) between an input frame's timestamp and the sample count that still counts as continuous input.
9
+ *
10
+ * @internal
11
+ */
12
+ const RESYNC_TOLERANCE = 0.02;
13
+ /**
14
+ * Input (seconds) over which a larger deviation must persist before it is compensated.
15
+ *
16
+ * @internal
17
+ */
18
+ const CONFIRM_DURATION = 0.2;
19
+ /**
20
+ * Frames over which a larger deviation must persist before it is compensated.
21
+ *
22
+ * @internal
23
+ */
24
+ const CONFIRM_FRAMES = 3;
25
+ /**
26
+ * Largest overlap (seconds) dropped from the input instead of restarting the timeline.
27
+ *
28
+ * @internal
29
+ */
30
+ const MAX_TRIM = 1;
31
+ /**
32
+ * Default largest gap (seconds) filled with silence instead of restarting the timeline.
33
+ *
34
+ * @internal
35
+ */
36
+ const DEFAULT_MAX_GAP_FILL = 1;
37
+ /**
38
+ * Check the options of an audio frame buffer.
39
+ *
40
+ * @param options - Options to check
41
+ *
42
+ * @throws {RangeError} If an option is out of range
43
+ *
44
+ * @internal
45
+ */
46
+ export function assertAudioFrameBufferOptions(options) {
47
+ const { timestamps, maxGapFill } = options;
48
+ if (timestamps !== undefined && timestamps !== 'input' && timestamps !== 'samples') {
49
+ throw new RangeError(`timestamps must be 'input' or 'samples', got ${String(timestamps)}`);
50
+ }
51
+ if (maxGapFill !== undefined && !(Number.isFinite(maxGapFill) && maxGapFill >= 0)) {
52
+ throw new RangeError(`maxGapFill must be a finite number of seconds >= 0, got ${maxGapFill}`);
53
+ }
54
+ }
55
+ /**
56
+ * Middle value of a list of timestamps.
57
+ *
58
+ * @param values - Non-empty list of timestamps
59
+ *
60
+ * @returns The value in the middle after sorting
61
+ *
62
+ * @internal
63
+ */
64
+ function median(values) {
65
+ const sorted = [...values].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
66
+ return sorted[sorted.length >> 1];
67
+ }
7
68
  /**
8
69
  * Audio frame buffering utility for encoders with fixed frame size requirements.
9
70
  *
@@ -11,6 +72,16 @@ import { avGetBytesPerSample, avSampleFmtIsPlanar } from '../lib/utilities.js';
11
72
  * of samples (frame_size). This class buffers incoming frames and outputs frames
12
73
  * with exactly the required size.
13
74
  *
75
+ * Output frames keep the input timing: each carries the timestamp of its first
76
+ * sample, in the time base of the pushed frames, and counts on by samples while the
77
+ * input is continuous. Deviations of up to 20 ms count as continuous, and larger ones
78
+ * only once they persist over 0.2 s of input, so jitter and single frames with a wrong
79
+ * timestamp leave the audio untouched. A persistent gap of up to a second is then
80
+ * filled with silence and an overlap of up to a second is dropped, which keeps the
81
+ * audio contiguous and in sync; a larger jump restarts the timeline at the new
82
+ * timestamp on the next frame boundary. Frames without a timestamp or time base
83
+ * continue the previous one.
84
+ *
14
85
  * Uses FFmpeg's AVAudioFifo internally for efficient sample buffering.
15
86
  *
16
87
  * @example
@@ -44,17 +115,37 @@ import { avGetBytesPerSample, avSampleFmtIsPlanar } from '../lib/utilities.js';
44
115
  * tail.free();
45
116
  * }
46
117
  * ```
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * // Real-time output: input gaps become timestamp jumps instead of silence
122
+ * using buffer = AudioFrameBuffer.create(960, AV_SAMPLE_FMT_FLT, 48000, 'mono', 1, { maxGapFill: 0 });
123
+ * ```
47
124
  */
48
125
  export class AudioFrameBuffer {
49
126
  fifo;
50
127
  frame;
51
128
  frameSize;
52
- sampleFormat;
53
129
  channels;
130
+ sampleStride;
54
131
  staging;
55
132
  stagingPlanes;
56
- nextPts = 0n;
57
- firstFramePts = null;
133
+ silenceValue;
134
+ sampleTimeBase;
135
+ timeBase = null;
136
+ countsInSamples = true;
137
+ followInput;
138
+ tolerance;
139
+ confirmSamples;
140
+ maxFill;
141
+ maxTrim;
142
+ headIndex = 0;
143
+ tailIndex = 0;
144
+ segments = [{ index: 0, pts: 0n }];
145
+ silence = [];
146
+ timed = false;
147
+ trimPending = 0;
148
+ deviation = null;
58
149
  /**
59
150
  * @param fifo - Underlying AudioFifo instance
60
151
  *
@@ -66,12 +157,13 @@ export class AudioFrameBuffer {
66
157
  *
67
158
  * @param channelLayout - Channel layout
68
159
  *
160
+ * @param options - Timing options
161
+ *
69
162
  * @internal
70
163
  */
71
- constructor(fifo, frameSize, sampleFormat, sampleRate, channelLayout) {
164
+ constructor(fifo, frameSize, sampleFormat, sampleRate, channelLayout, options) {
72
165
  this.fifo = fifo;
73
166
  this.frameSize = frameSize;
74
- this.sampleFormat = sampleFormat;
75
167
  this.channels = channelLayout.nbChannels;
76
168
  this.frame = new Frame();
77
169
  this.frame.alloc();
@@ -79,17 +171,27 @@ export class AudioFrameBuffer {
79
171
  this.frame.format = sampleFormat;
80
172
  this.frame.sampleRate = sampleRate;
81
173
  this.frame.channelLayout = channelLayout;
82
- // Output PTS is a running sample counter, so the matching timebase is
83
- // 1/sample_rate; pulled frames go to the codec without further rescaling.
84
- this.frame.timeBase = new Rational(1, sampleRate);
174
+ // Until the first pushed frame brings a time base, timestamps count samples.
175
+ this.sampleTimeBase = new Rational(1, sampleRate);
176
+ this.frame.timeBase = this.sampleTimeBase;
85
177
  this.frame.getBuffer(0); // Allocate buffer once
86
178
  // Frame.data is a copy instead of a view where external buffers are not allowed (Electron),
87
179
  // so samples are staged in JS memory and copied into the frame with fromBuffer()
88
180
  const planar = avSampleFmtIsPlanar(sampleFormat);
89
181
  const planeCount = planar ? this.channels : 1;
90
- const planeSize = frameSize * avGetBytesPerSample(sampleFormat) * (planar ? 1 : this.channels);
182
+ this.sampleStride = avGetBytesPerSample(sampleFormat) * (planar ? 1 : this.channels);
183
+ const planeSize = frameSize * this.sampleStride;
91
184
  this.staging = Buffer.alloc(planeSize * planeCount);
92
185
  this.stagingPlanes = Array.from({ length: planeCount }, (_, i) => this.staging.subarray(i * planeSize, (i + 1) * planeSize));
186
+ // av_samples_set_silence() semantics: unsigned 8-bit formats are silent at 0x80
187
+ this.silenceValue = sampleFormat === AV_SAMPLE_FMT_U8 || sampleFormat === AV_SAMPLE_FMT_U8P ? 0x80 : 0;
188
+ this.followInput = options.timestamps !== 'samples';
189
+ this.tolerance = Math.round(sampleRate * RESYNC_TOLERANCE);
190
+ this.confirmSamples = Math.round(sampleRate * CONFIRM_DURATION);
191
+ // At least one frame: a restart pads the current frame with up to that much silence anyway,
192
+ // and the padding must never reach past the new timestamp
193
+ this.maxFill = Math.max(Math.round(sampleRate * (options.maxGapFill ?? DEFAULT_MAX_GAP_FILL)), frameSize);
194
+ this.maxTrim = Math.round(sampleRate * MAX_TRIM);
93
195
  }
94
196
  /**
95
197
  * Create an audio frame buffer.
@@ -104,23 +206,30 @@ export class AudioFrameBuffer {
104
206
  *
105
207
  * @param channels - Number of audio channels
106
208
  *
209
+ * @param options - Timing options
210
+ *
107
211
  * @returns Configured audio frame buffer
108
212
  *
213
+ * @throws {RangeError} If an option is out of range
214
+ *
109
215
  * @example
110
216
  * ```typescript
111
217
  * // For Opus encoder at 48kHz with 20ms frames
112
218
  * const buffer = AudioFrameBuffer.create(960, AV_SAMPLE_FMT_FLT, 48000, 'mono', 1);
113
219
  * ```
114
220
  */
115
- static create(frameSize, sampleFormat, sampleRate, channelLayout, channels) {
221
+ static create(frameSize, sampleFormat, sampleRate, channelLayout, channels, options = {}) {
222
+ assertAudioFrameBufferOptions(options);
116
223
  const fifo = new AudioFifo();
117
224
  // Allocate FIFO with capacity for multiple frames
118
225
  fifo.alloc(sampleFormat, channels, frameSize * 4);
119
- return new AudioFrameBuffer(fifo, frameSize, sampleFormat, sampleRate, channelLayout);
226
+ return new AudioFrameBuffer(fifo, frameSize, sampleFormat, sampleRate, channelLayout, options);
120
227
  }
121
228
  /**
122
229
  * Get number of samples currently in buffer.
123
230
  *
231
+ * Includes silence inserted for gaps in the input.
232
+ *
124
233
  * @returns Number of buffered samples
125
234
  *
126
235
  * @example
@@ -129,12 +238,12 @@ export class AudioFrameBuffer {
129
238
  * ```
130
239
  */
131
240
  get size() {
132
- return this.fifo.size;
241
+ return this.available();
133
242
  }
134
243
  /**
135
244
  * Check if a complete frame is available.
136
245
  *
137
- * Returns true if the FIFO contains at least frameSize samples.
246
+ * Returns true if the buffer holds at least frameSize samples.
138
247
  *
139
248
  * @returns True if a full frame can be pulled
140
249
  *
@@ -147,7 +256,7 @@ export class AudioFrameBuffer {
147
256
  * ```
148
257
  */
149
258
  hasFrame() {
150
- return this.fifo.size >= this.frameSize;
259
+ return this.available() >= this.frameSize;
151
260
  }
152
261
  /**
153
262
  * Push an audio frame into the buffer asynchronously.
@@ -155,8 +264,23 @@ export class AudioFrameBuffer {
155
264
  * The frame's samples are added to the internal FIFO.
156
265
  * Call hasFrame() and pull() to retrieve fixed-size output frames.
157
266
  *
267
+ * The first frame with a valid time base fixes the time base of the output frames.
268
+ * When the frame timestamps deviate from the buffered samples by more than 20 ms
269
+ * over at least 0.2 s of input (and 3 frames), a gap is filled with silence (up to
270
+ * `maxGapFill`) and an overlap of up to 1 s is dropped from the input; a larger jump
271
+ * restarts the timeline at the new timestamp after padding the current output frame
272
+ * with silence. The frames that arrive until then still continue the previous
273
+ * timeline. Each such correction is a short dropout or cut in the audio: input whose
274
+ * timestamps drift against its sample count, such as a capture device stamped from
275
+ * the system clock, gets one per 20 ms of drift, which keeps it within about 20 ms
276
+ * of its timestamps.
277
+ *
158
278
  * @param frame - Audio frame to buffer
159
279
  *
280
+ * @throws {Error} If the frame is not an audio frame
281
+ *
282
+ * @throws {FFmpegError} If the samples cannot be written to the FIFO
283
+ *
160
284
  * @example
161
285
  * ```typescript
162
286
  * await buffer.push(filterFrame);
@@ -165,17 +289,24 @@ export class AudioFrameBuffer {
165
289
  * @see {@link pushSync} For synchronous version
166
290
  */
167
291
  async push(frame) {
168
- if (!frame.isAudio()) {
292
+ // Every Frame getter is a call into the addon, so each one is read only once per push
293
+ // (this is frame.isAudio() without its second nbSamples read)
294
+ const nbSamples = frame.nbSamples;
295
+ if (!(frame.sampleRate > 0 && nbSamples > 0)) {
169
296
  throw new Error('AudioFrameBuffer.push() requires an audio frame');
170
297
  }
171
- if (this.firstFramePts === null) {
172
- // Always start PTS from 0 for encoder frames
173
- // Input frames may be in different timebase than encoder expects
174
- this.firstFramePts = 0n;
175
- this.nextPts = 0n;
298
+ const skip = this.planPush(frame, nbSamples);
299
+ const samples = nbSamples - skip;
300
+ if (samples <= 0) {
301
+ return;
302
+ }
303
+ try {
304
+ FFmpegError.throwIfError(await this.fifo.write(this.inputPlanes(frame, skip), samples), 'Failed to write samples to the audio FIFO');
305
+ }
306
+ catch (error) {
307
+ this.resyncTail();
308
+ throw error;
176
309
  }
177
- // Write frame data to FIFO
178
- await this.fifo.write(frame.data, frame.nbSamples);
179
310
  }
180
311
  /**
181
312
  * Push an audio frame into the buffer synchronously.
@@ -183,9 +314,14 @@ export class AudioFrameBuffer {
183
314
  *
184
315
  * The frame's samples are added to the internal FIFO.
185
316
  * Call hasFrame() and pullSync() to retrieve fixed-size output frames.
317
+ * Timestamps are handled as in push().
186
318
  *
187
319
  * @param frame - Audio frame to buffer
188
320
  *
321
+ * @throws {Error} If the frame is not an audio frame
322
+ *
323
+ * @throws {FFmpegError} If the samples cannot be written to the FIFO
324
+ *
189
325
  * @example
190
326
  * ```typescript
191
327
  * buffer.pushSync(filterFrame);
@@ -194,27 +330,35 @@ export class AudioFrameBuffer {
194
330
  * @see {@link push} For async version
195
331
  */
196
332
  pushSync(frame) {
197
- if (!frame.isAudio()) {
333
+ const nbSamples = frame.nbSamples;
334
+ if (!(frame.sampleRate > 0 && nbSamples > 0)) {
198
335
  throw new Error('AudioFrameBuffer.pushSync() requires an audio frame');
199
336
  }
200
- if (this.firstFramePts === null) {
201
- // Always start PTS from 0 for encoder frames
202
- // Input frames may be in different timebase than encoder expects
203
- this.firstFramePts = 0n;
204
- this.nextPts = 0n;
337
+ const skip = this.planPush(frame, nbSamples);
338
+ const samples = nbSamples - skip;
339
+ if (samples <= 0) {
340
+ return;
341
+ }
342
+ try {
343
+ FFmpegError.throwIfError(this.fifo.writeSync(this.inputPlanes(frame, skip), samples), 'Failed to write samples to the audio FIFO');
344
+ }
345
+ catch (error) {
346
+ this.resyncTail();
347
+ throw error;
205
348
  }
206
- // Write frame data to FIFO
207
- this.fifo.writeSync(frame.data, frame.nbSamples);
208
349
  }
209
350
  /**
210
351
  * Pull a fixed-size audio frame from the buffer asynchronously.
211
352
  *
212
- * Reads exactly frameSize samples from the FIFO and returns a cloned Frame.
353
+ * Reads exactly frameSize samples from the FIFO and returns a cloned Frame
354
+ * stamped with the timestamp of its first sample.
213
355
  * Returns null if not enough samples are available.
214
356
  * Reuses internal frame buffer for efficiency (like Decoder does).
215
357
  *
216
358
  * @returns Audio frame with exactly frameSize samples, or null if insufficient samples
217
359
  *
360
+ * @throws {FFmpegError} If the FIFO cannot be read or the frame cannot be filled
361
+ *
218
362
  * @throws {Error} If frame cloning fails (out of memory)
219
363
  *
220
364
  * @example
@@ -231,30 +375,25 @@ export class AudioFrameBuffer {
231
375
  if (!this.hasFrame()) {
232
376
  return null;
233
377
  }
234
- // Update PTS
235
- this.frame.pts = this.nextPts;
378
+ const pts = this.headPts();
236
379
  // Read samples from FIFO and copy them into the reusable frame
237
- await this.fifo.read(this.stagingPlanes, this.frameSize);
380
+ await this.readStaging(this.frameSize);
238
381
  this.commitStaging();
239
- // Update PTS for next frame
240
- this.nextPts += BigInt(this.frameSize);
241
- // Clone frame for user (like Decoder does)
242
- const cloned = this.frame.clone();
243
- if (!cloned) {
244
- throw new Error('Failed to clone frame (out of memory)');
245
- }
246
- return cloned;
382
+ return this.emitFrame(pts);
247
383
  }
248
384
  /**
249
385
  * Pull a fixed-size audio frame from the buffer synchronously.
250
386
  * Synchronous version of pull.
251
387
  *
252
- * Reads exactly frameSize samples from the FIFO and returns a cloned Frame.
388
+ * Reads exactly frameSize samples from the FIFO and returns a cloned Frame
389
+ * stamped with the timestamp of its first sample.
253
390
  * Returns null if not enough samples are available.
254
391
  * Reuses internal frame buffer for efficiency (like Decoder does).
255
392
  *
256
393
  * @returns Audio frame with exactly frameSize samples, or null if insufficient samples
257
394
  *
395
+ * @throws {FFmpegError} If the FIFO cannot be read or the frame cannot be filled
396
+ *
258
397
  * @throws {Error} If frame cloning fails (out of memory)
259
398
  *
260
399
  * @example
@@ -271,19 +410,11 @@ export class AudioFrameBuffer {
271
410
  if (!this.hasFrame()) {
272
411
  return null;
273
412
  }
274
- // Update PTS
275
- this.frame.pts = this.nextPts;
413
+ const pts = this.headPts();
276
414
  // Read samples from FIFO and copy them into the reusable frame
277
- this.fifo.readSync(this.stagingPlanes, this.frameSize);
415
+ this.readStagingSync(this.frameSize);
278
416
  this.commitStaging();
279
- // Update PTS for next frame
280
- this.nextPts += BigInt(this.frameSize);
281
- // Clone frame for user (like Decoder does)
282
- const cloned = this.frame.clone();
283
- if (!cloned) {
284
- throw new Error('Failed to clone frame (out of memory)');
285
- }
286
- return cloned;
417
+ return this.emitFrame(pts);
287
418
  }
288
419
  /**
289
420
  * Pull the final partial frame from the buffer asynchronously.
@@ -293,10 +424,13 @@ export class AudioFrameBuffer {
293
424
  * Intended for end-of-stream flushing only - without it the tail (up to
294
425
  * frameSize - 1 samples) would be dropped. Returns null if the buffer is
295
426
  * empty, or while a complete frame is still available (drain those with
296
- * pull() first).
427
+ * pull() first). The padding counts as audio, so frames pushed afterwards are
428
+ * timed after it.
297
429
  *
298
430
  * @returns Silence-padded audio frame with frameSize samples, or null if nothing to drain
299
431
  *
432
+ * @throws {FFmpegError} If the FIFO cannot be read or the frame cannot be filled
433
+ *
300
434
  * @throws {Error} If frame cloning fails (out of memory)
301
435
  *
302
436
  * @example
@@ -312,25 +446,19 @@ export class AudioFrameBuffer {
312
446
  * @see {@link pull} For pulling complete frames
313
447
  */
314
448
  async pullPartial() {
315
- const remaining = this.fifo.size;
449
+ const remaining = this.available();
316
450
  if (remaining <= 0 || remaining >= this.frameSize) {
317
451
  return null;
318
452
  }
319
- // Update PTS
320
- this.frame.pts = this.nextPts;
453
+ const pts = this.headPts();
321
454
  // Read the remaining samples into the head of the staging buffer
322
- await this.fifo.read(this.stagingPlanes, remaining);
455
+ await this.readStaging(remaining);
323
456
  // Pad the tail with silence up to frameSize
324
- this.padWithSilence(remaining);
457
+ this.fillSilence(remaining, this.frameSize);
325
458
  this.commitStaging();
326
- // Update PTS for next frame
327
- this.nextPts += BigInt(this.frameSize);
328
- // Clone frame for user (like Decoder does)
329
- const cloned = this.frame.clone();
330
- if (!cloned) {
331
- throw new Error('Failed to clone frame (out of memory)');
332
- }
333
- return cloned;
459
+ const frame = this.emitFrame(pts);
460
+ this.advanceTailToHead();
461
+ return frame;
334
462
  }
335
463
  /**
336
464
  * Pull the final partial frame from the buffer synchronously.
@@ -343,6 +471,8 @@ export class AudioFrameBuffer {
343
471
  *
344
472
  * @returns Silence-padded audio frame with frameSize samples, or null if nothing to drain
345
473
  *
474
+ * @throws {FFmpegError} If the FIFO cannot be read or the frame cannot be filled
475
+ *
346
476
  * @throws {Error} If frame cloning fails (out of memory)
347
477
  *
348
478
  * @example
@@ -358,20 +488,492 @@ export class AudioFrameBuffer {
358
488
  * @see {@link pullSync} For pulling complete frames
359
489
  */
360
490
  pullPartialSync() {
361
- const remaining = this.fifo.size;
491
+ const remaining = this.available();
362
492
  if (remaining <= 0 || remaining >= this.frameSize) {
363
493
  return null;
364
494
  }
365
- // Update PTS
366
- this.frame.pts = this.nextPts;
495
+ const pts = this.headPts();
367
496
  // Read the remaining samples into the head of the staging buffer
368
- this.fifo.readSync(this.stagingPlanes, remaining);
497
+ this.readStagingSync(remaining);
369
498
  // Pad the tail with silence up to frameSize
370
- this.padWithSilence(remaining);
499
+ this.fillSilence(remaining, this.frameSize);
371
500
  this.commitStaging();
372
- // Update PTS for next frame
373
- this.nextPts += BigInt(this.frameSize);
374
- // Clone frame for user
501
+ const frame = this.emitFrame(pts);
502
+ this.advanceTailToHead();
503
+ return frame;
504
+ }
505
+ /**
506
+ * Reset the buffer, discarding all buffered samples.
507
+ *
508
+ * The next pushed frame starts a new timeline, and its time base becomes
509
+ * the output time base.
510
+ *
511
+ * @example
512
+ * ```typescript
513
+ * buffer.reset();
514
+ * ```
515
+ */
516
+ reset() {
517
+ this.fifo.reset();
518
+ this.headIndex = 0;
519
+ this.tailIndex = 0;
520
+ this.segments = [{ index: 0, pts: 0n }];
521
+ this.silence = [];
522
+ this.timed = false;
523
+ this.trimPending = 0;
524
+ this.deviation = null;
525
+ this.timeBase = null;
526
+ this.countsInSamples = true;
527
+ this.frame.timeBase = this.sampleTimeBase;
528
+ }
529
+ /**
530
+ * Decide how a pushed frame continues the buffered samples.
531
+ *
532
+ * The only place that moves the write position: it records the silence before the
533
+ * frame and accounts for the samples the caller then writes, so the position always
534
+ * runs ahead of the buffered samples by exactly the writes still in flight.
535
+ *
536
+ * @param frame - Audio frame about to be written
537
+ *
538
+ * @param nbSamples - Number of samples in the frame
539
+ *
540
+ * @returns Samples to drop from the head of the frame
541
+ *
542
+ * @internal
543
+ */
544
+ planPush(frame, nbSamples) {
545
+ this.timeBase ??= this.lockTimeBase(frame);
546
+ const pts = this.followInput ? this.inputPts(frame) : AV_NOPTS_VALUE;
547
+ if (pts !== AV_NOPTS_VALUE && nbSamples > 0) {
548
+ this.addSilence(this.timed ? this.trackInput(pts, nbSamples) : this.anchorInput(pts));
549
+ }
550
+ const skip = Math.min(this.trimPending, nbSamples);
551
+ this.trimPending -= skip;
552
+ this.tailIndex += nbSamples - skip;
553
+ return skip;
554
+ }
555
+ /**
556
+ * Append silence at the write position.
557
+ *
558
+ * Silence only exists as a run of sample indices and is generated while pulling, so
559
+ * a long gap costs neither a buffer of its size nor a FIFO grown to hold it (an
560
+ * AVAudioFifo never shrinks again).
561
+ *
562
+ * @param samples - Number of silent samples
563
+ *
564
+ * @internal
565
+ */
566
+ addSilence(samples) {
567
+ if (samples <= 0) {
568
+ return;
569
+ }
570
+ const last = this.silence[this.silence.length - 1];
571
+ if (last && last.index + last.length === this.tailIndex) {
572
+ last.length += samples;
573
+ }
574
+ else {
575
+ this.silence.push({ index: this.tailIndex, length: samples });
576
+ }
577
+ this.tailIndex += samples;
578
+ }
579
+ /**
580
+ * Anchor the timeline at the first frame that carries a timestamp.
581
+ *
582
+ * @param pts - Timestamp of the frame in the output time base
583
+ *
584
+ * @returns Silence to write before the frame
585
+ *
586
+ * @internal
587
+ */
588
+ anchorInput(pts) {
589
+ this.timed = true;
590
+ const first = this.segments[0];
591
+ if (this.segments.length === 1 && this.headIndex === first.index) {
592
+ // Nothing pulled yet: samples buffered without a timestamp end where this frame starts
593
+ first.pts = pts - this.ticks(this.tailIndex - first.index);
594
+ return 0;
595
+ }
596
+ // Samples without a timestamp already went out on a made-up timeline, which this
597
+ // frame cannot be measured against
598
+ return this.restart(pts);
599
+ }
600
+ /**
601
+ * Compare a frame's timestamp with the buffered samples and compensate a persistent deviation.
602
+ *
603
+ * Within the tolerance the frame counts on by samples. A larger deviation is only
604
+ * acted on once it has lasted over enough input in the same direction: jitter and
605
+ * frames with a wrong timestamp come and go, while gaps, overlaps and clock steps
606
+ * persist. The correction is the median deviation of that run, so jitter on top of
607
+ * a real step does not leak into it.
608
+ *
609
+ * @param pts - Timestamp of the frame in the output time base
610
+ *
611
+ * @param nbSamples - Number of samples in the frame
612
+ *
613
+ * @returns Silence to write before the frame
614
+ *
615
+ * @internal
616
+ */
617
+ trackInput(pts, nbSamples) {
618
+ // Where this frame belongs: after the buffered samples, minus the input still to be dropped
619
+ const expected = this.trimPending > 0 ? this.tailPts() - this.ticks(this.trimPending) : this.tailPts();
620
+ const offset = pts - expected;
621
+ const drift = this.samples(offset);
622
+ if (Math.abs(drift) <= this.tolerance) {
623
+ this.deviation = null;
624
+ return 0;
625
+ }
626
+ const sign = drift > 0 ? 1 : -1;
627
+ if (this.deviation?.sign !== sign) {
628
+ this.deviation = { sign, frames: 0, samples: 0, offsets: [] };
629
+ }
630
+ const deviation = this.deviation;
631
+ deviation.frames++;
632
+ deviation.samples += nbSamples;
633
+ deviation.offsets.push(offset);
634
+ if (deviation.frames < CONFIRM_FRAMES || deviation.samples < this.confirmSamples) {
635
+ return 0;
636
+ }
637
+ this.deviation = null;
638
+ return this.compensate(median(deviation.offsets), expected);
639
+ }
640
+ /**
641
+ * Apply a confirmed deviation to the timeline.
642
+ *
643
+ * A gap is filled with silence and an overlap is dropped from the input, which keeps
644
+ * the output contiguous and on its frame grid. Beyond their limits the timeline
645
+ * restarts at the frame's timestamp instead.
646
+ *
647
+ * @param offset - Deviation of the frame in the output time base
648
+ *
649
+ * @param expected - Timestamp the frame would have without the deviation
650
+ *
651
+ * @returns Silence to write before the frame
652
+ *
653
+ * @internal
654
+ */
655
+ compensate(offset, expected) {
656
+ const drift = this.samples(offset);
657
+ if (drift > 0) {
658
+ // Input still to be dropped is ahead of the expectation by the same amount
659
+ const cancelled = Math.min(drift, this.trimPending);
660
+ this.trimPending -= cancelled;
661
+ const gap = drift - cancelled;
662
+ if (gap <= this.maxFill) {
663
+ return gap;
664
+ }
665
+ }
666
+ else if (this.trimPending - drift <= this.maxTrim) {
667
+ // Dropping keeps the output monotonic; following a small backward step would make
668
+ // the muxer clamp every packet of the overlap to one tick after the previous one
669
+ this.trimPending -= drift;
670
+ return 0;
671
+ }
672
+ return this.restart(expected + offset);
673
+ }
674
+ /**
675
+ * Start a new segment at the next output frame boundary.
676
+ *
677
+ * Every output frame then lies within one segment, so its timestamp stays well
678
+ * defined; the current partial frame is padded with silence to get there.
679
+ *
680
+ * @param pts - Timestamp of the frame about to be written
681
+ *
682
+ * @returns Silence to write before the frame
683
+ *
684
+ * @internal
685
+ */
686
+ restart(pts) {
687
+ const pad = (this.frameSize - (this.tailIndex % this.frameSize)) % this.frameSize;
688
+ const index = this.tailIndex + pad;
689
+ const last = this.segments[this.segments.length - 1];
690
+ if (last.index === index) {
691
+ last.pts = pts;
692
+ }
693
+ else {
694
+ this.segments.push({ index, pts });
695
+ }
696
+ this.trimPending = 0;
697
+ this.deviation = null;
698
+ return pad;
699
+ }
700
+ /**
701
+ * Fix the output time base from the first pushed frame.
702
+ *
703
+ * Frames without a valid time base leave the output counting samples (1/sample_rate).
704
+ *
705
+ * @param frame - First pushed frame
706
+ *
707
+ * @returns Output time base
708
+ *
709
+ * @internal
710
+ */
711
+ lockTimeBase(frame) {
712
+ const tb = frame.timeBase;
713
+ const timeBase = tb.num > 0 && tb.den > 0 ? tb : this.sampleTimeBase;
714
+ this.countsInSamples = timeBase.num * this.sampleTimeBase.den === timeBase.den;
715
+ this.frame.timeBase = timeBase;
716
+ return timeBase;
717
+ }
718
+ /**
719
+ * Read a frame's timestamp in the output time base.
720
+ *
721
+ * @param frame - Pushed frame
722
+ *
723
+ * @returns Timestamp, or AV_NOPTS_VALUE if the frame has no timestamp or time base
724
+ *
725
+ * @internal
726
+ */
727
+ inputPts(frame) {
728
+ const pts = frame.pts;
729
+ if (pts === AV_NOPTS_VALUE) {
730
+ return pts;
731
+ }
732
+ const tb = frame.timeBase;
733
+ if (tb.num <= 0 || tb.den <= 0) {
734
+ return AV_NOPTS_VALUE;
735
+ }
736
+ const out = this.timeBase;
737
+ return tb.num === out.num && tb.den === out.den ? pts : avRescaleQ(pts, tb, out);
738
+ }
739
+ /**
740
+ * Timestamp of the next sample to pull.
741
+ *
742
+ * Drops segments the head has reached the end of first, so the head lies in the first one.
743
+ *
744
+ * @returns Timestamp in the output time base
745
+ *
746
+ * @internal
747
+ */
748
+ headPts() {
749
+ while (this.segments.length > 1 && this.segments[1].index <= this.headIndex) {
750
+ this.segments.shift();
751
+ }
752
+ const segment = this.segments[0];
753
+ return segment.pts + this.ticks(this.headIndex - segment.index);
754
+ }
755
+ /**
756
+ * Timestamp right after the last buffered sample.
757
+ *
758
+ * @returns Timestamp in the output time base
759
+ *
760
+ * @internal
761
+ */
762
+ tailPts() {
763
+ const segment = this.segments[this.segments.length - 1];
764
+ return segment.pts + this.ticks(this.tailIndex - segment.index);
765
+ }
766
+ /**
767
+ * Treat the silence padding of a partial frame as written audio.
768
+ *
769
+ * @internal
770
+ */
771
+ advanceTailToHead() {
772
+ this.tailIndex = this.headIndex;
773
+ // Deviations seen so far were measured against the unpadded end
774
+ this.deviation = null;
775
+ }
776
+ /**
777
+ * Realign the write position with the FIFO after a failed write.
778
+ *
779
+ * @internal
780
+ */
781
+ resyncTail() {
782
+ this.tailIndex = this.headIndex + this.available();
783
+ while (this.silence.length > 0 && this.silence[this.silence.length - 1].index >= this.tailIndex) {
784
+ this.silence.pop();
785
+ }
786
+ while (this.segments.length > 1 && this.segments[this.segments.length - 1].index > this.tailIndex) {
787
+ this.segments.pop();
788
+ }
789
+ this.deviation = null;
790
+ }
791
+ /**
792
+ * Number of samples that can be pulled: buffered samples plus the silence between them.
793
+ *
794
+ * Stops at the first silence run that lies behind a write still in flight.
795
+ *
796
+ * @returns Number of samples available from the read position
797
+ *
798
+ * @internal
799
+ */
800
+ available() {
801
+ let buffered = this.fifo.size;
802
+ if (this.silence.length === 0) {
803
+ return buffered;
804
+ }
805
+ let index = this.headIndex;
806
+ for (const run of this.silence) {
807
+ // Negative while the read position lies inside the run
808
+ const before = run.index - index;
809
+ if (before > buffered) {
810
+ break;
811
+ }
812
+ buffered -= Math.max(before, 0);
813
+ index = run.index + run.length;
814
+ }
815
+ return index - this.headIndex + buffered;
816
+ }
817
+ /**
818
+ * Split the next samples to pull into FIFO reads and silence.
819
+ *
820
+ * @param count - Number of samples to pull
821
+ *
822
+ * @returns Parts in pull order
823
+ *
824
+ * @internal
825
+ */
826
+ readRuns(count) {
827
+ const runs = [];
828
+ const end = this.headIndex + count;
829
+ let index = this.headIndex;
830
+ for (const run of this.silence) {
831
+ if (run.index + run.length <= index) {
832
+ continue;
833
+ }
834
+ const start = Math.max(run.index, index);
835
+ if (start >= end) {
836
+ break;
837
+ }
838
+ if (start > index) {
839
+ runs.push({ length: start - index, silent: false });
840
+ }
841
+ index = Math.min(run.index + run.length, end);
842
+ runs.push({ length: index - start, silent: true });
843
+ }
844
+ if (index < end) {
845
+ runs.push({ length: end - index, silent: false });
846
+ }
847
+ return runs;
848
+ }
849
+ /**
850
+ * Read the next samples into the head of the staging buffer.
851
+ *
852
+ * @param count - Number of samples to read
853
+ *
854
+ * @throws {FFmpegError} If the FIFO cannot be read
855
+ *
856
+ * @internal
857
+ */
858
+ async readStaging(count) {
859
+ if (this.silence.length === 0) {
860
+ FFmpegError.throwIfError(await this.fifo.read(this.stagingPlanes, count), 'Failed to read samples from the audio FIFO');
861
+ return;
862
+ }
863
+ let offset = 0;
864
+ for (const run of this.readRuns(count)) {
865
+ if (run.silent) {
866
+ this.fillSilence(offset, offset + run.length);
867
+ }
868
+ else {
869
+ FFmpegError.throwIfError(await this.fifo.read(this.stagingAt(offset), run.length), 'Failed to read samples from the audio FIFO');
870
+ }
871
+ offset += run.length;
872
+ }
873
+ }
874
+ /**
875
+ * Read the next samples into the head of the staging buffer synchronously.
876
+ * Synchronous version of readStaging.
877
+ *
878
+ * @param count - Number of samples to read
879
+ *
880
+ * @throws {FFmpegError} If the FIFO cannot be read
881
+ *
882
+ * @internal
883
+ */
884
+ readStagingSync(count) {
885
+ if (this.silence.length === 0) {
886
+ FFmpegError.throwIfError(this.fifo.readSync(this.stagingPlanes, count), 'Failed to read samples from the audio FIFO');
887
+ return;
888
+ }
889
+ let offset = 0;
890
+ for (const run of this.readRuns(count)) {
891
+ if (run.silent) {
892
+ this.fillSilence(offset, offset + run.length);
893
+ }
894
+ else {
895
+ FFmpegError.throwIfError(this.fifo.readSync(this.stagingAt(offset), run.length), 'Failed to read samples from the audio FIFO');
896
+ }
897
+ offset += run.length;
898
+ }
899
+ }
900
+ /**
901
+ * Staging planes starting at a sample offset.
902
+ *
903
+ * @param offset - First sample to write to
904
+ *
905
+ * @returns Plane buffers for a FIFO read
906
+ *
907
+ * @internal
908
+ */
909
+ stagingAt(offset) {
910
+ if (offset === 0) {
911
+ return this.stagingPlanes;
912
+ }
913
+ const start = offset * this.sampleStride;
914
+ return this.stagingPlanes.map((plane) => plane.subarray(start));
915
+ }
916
+ /**
917
+ * Convert a sample count to output time base ticks.
918
+ *
919
+ * @param samples - Number of samples
920
+ *
921
+ * @returns Duration in the output time base
922
+ *
923
+ * @internal
924
+ */
925
+ ticks(samples) {
926
+ return this.countsInSamples ? BigInt(samples) : avRescaleQ(samples, this.sampleTimeBase, this.timeBase ?? this.sampleTimeBase);
927
+ }
928
+ /**
929
+ * Convert output time base ticks to a sample count.
930
+ *
931
+ * @param ticks - Duration in the output time base
932
+ *
933
+ * @returns Number of samples (rounded)
934
+ *
935
+ * @internal
936
+ */
937
+ samples(ticks) {
938
+ return Number(this.countsInSamples ? ticks : avRescaleQ(ticks, this.timeBase ?? this.sampleTimeBase, this.sampleTimeBase));
939
+ }
940
+ /**
941
+ * Plane buffers of a pushed frame, starting at a sample offset.
942
+ *
943
+ * @param frame - Pushed frame
944
+ *
945
+ * @param skip - Samples to leave out at the head
946
+ *
947
+ * @returns Plane buffers to write to the FIFO
948
+ *
949
+ * @internal
950
+ */
951
+ inputPlanes(frame, skip) {
952
+ const planes = frame.data;
953
+ if (skip === 0) {
954
+ return planes;
955
+ }
956
+ const offset = skip * this.sampleStride;
957
+ return planes.map((plane) => plane.subarray(offset));
958
+ }
959
+ /**
960
+ * Stamp the reusable frame, advance the read position by one frame and hand out a clone.
961
+ *
962
+ * @param pts - Timestamp of the frame's first sample
963
+ *
964
+ * @returns Cloned output frame
965
+ *
966
+ * @throws {Error} If frame cloning fails (out of memory)
967
+ *
968
+ * @internal
969
+ */
970
+ emitFrame(pts) {
971
+ this.frame.pts = pts;
972
+ this.headIndex += this.frameSize;
973
+ while (this.silence.length > 0 && this.silence[0].index + this.silence[0].length <= this.headIndex) {
974
+ this.silence.shift();
975
+ }
976
+ // Clone frame for user (like Decoder does)
375
977
  const cloned = this.frame.clone();
376
978
  if (!cloned) {
377
979
  throw new Error('Failed to clone frame (out of memory)');
@@ -379,22 +981,20 @@ export class AudioFrameBuffer {
379
981
  return cloned;
380
982
  }
381
983
  /**
382
- * Fill the staged sample range [fromSample, frameSize) with silence.
984
+ * Fill the staged sample range [fromSample, toSample) with silence.
383
985
  *
384
- * Follows av_samples_set_silence() semantics: unsigned 8-bit formats use 0x80
385
- * as the silence value, every other format uses 0. Planar frames carry one
386
- * channel per plane; packed frames interleave all channels in plane 0.
986
+ * Planar frames carry one channel per plane; packed frames interleave all
987
+ * channels in plane 0.
387
988
  *
388
989
  * @param fromSample - First sample index to silence
389
990
  *
991
+ * @param toSample - Sample index after the last one to silence
992
+ *
390
993
  * @internal
391
994
  */
392
- padWithSilence(fromSample) {
393
- const silence = this.sampleFormat === AV_SAMPLE_FMT_U8 || this.sampleFormat === AV_SAMPLE_FMT_U8P ? 0x80 : 0;
394
- const bytesPerSample = avGetBytesPerSample(this.sampleFormat);
395
- const stride = avSampleFmtIsPlanar(this.sampleFormat) ? bytesPerSample : bytesPerSample * this.channels;
995
+ fillSilence(fromSample, toSample) {
396
996
  for (const plane of this.stagingPlanes) {
397
- plane.fill(silence, fromSample * stride, this.frameSize * stride);
997
+ plane.fill(this.silenceValue, fromSample * this.sampleStride, toSample * this.sampleStride);
398
998
  }
399
999
  }
400
1000
  /**
@@ -411,19 +1011,6 @@ export class AudioFrameBuffer {
411
1011
  FFmpegError.throwIfError(this.frame.makeWritable(), 'Failed to make frame writable');
412
1012
  FFmpegError.throwIfError(this.frame.fromBuffer(this.staging), 'Failed to copy samples into frame');
413
1013
  }
414
- /**
415
- * Reset the buffer, discarding all buffered samples.
416
- *
417
- * @example
418
- * ```typescript
419
- * buffer.reset();
420
- * ```
421
- */
422
- reset() {
423
- this.fifo.reset();
424
- this.nextPts = 0n;
425
- this.firstFramePts = null;
426
- }
427
1014
  /**
428
1015
  * Free the buffer and all resources.
429
1016
  *