node-av 6.2.0-beta.24 → 6.2.0-beta.26
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/README.md +1 -1
- package/dist/api/audio-frame-buffer.d.ts +378 -19
- package/dist/api/audio-frame-buffer.js +715 -101
- package/dist/api/audio-frame-buffer.js.map +1 -1
- package/dist/api/demuxer.d.ts +7 -3
- package/dist/api/demuxer.js +19 -7
- package/dist/api/demuxer.js.map +1 -1
- package/dist/api/encoder.d.ts +40 -3
- package/dist/api/encoder.js +78 -29
- package/dist/api/encoder.js.map +1 -1
- package/dist/api/fmp4-stream.d.ts +103 -3
- package/dist/api/fmp4-stream.js +151 -28
- package/dist/api/fmp4-stream.js.map +1 -1
- package/dist/api/index.d.ts +1 -1
- package/dist/api/index.js.map +1 -1
- package/dist/api/io-stream.js +21 -6
- package/dist/api/io-stream.js.map +1 -1
- package/dist/api/muxer.d.ts +275 -7
- package/dist/api/muxer.js +569 -62
- package/dist/api/muxer.js.map +1 -1
- package/dist/api/rtp-stream.d.ts +19 -2
- package/dist/api/rtp-stream.js +62 -15
- package/dist/api/rtp-stream.js.map +1 -1
- package/dist/api/scaler.d.ts +19 -4
- package/dist/api/scaler.js +21 -6
- package/dist/api/scaler.js.map +1 -1
- package/dist/api/utilities/packet-age-watchdog.d.ts +198 -0
- package/dist/api/utilities/packet-age-watchdog.js +594 -0
- package/dist/api/utilities/packet-age-watchdog.js.map +1 -0
- package/dist/lib/binding.d.ts +9 -1
- package/dist/lib/binding.js +13 -0
- package/dist/lib/binding.js.map +1 -1
- package/dist/lib/format-context.d.ts +7 -0
- package/dist/lib/format-context.js +7 -0
- package/dist/lib/format-context.js.map +1 -1
- package/dist/lib/frame.d.ts +6 -2
- package/dist/lib/frame.js +11 -3
- package/dist/lib/frame.js.map +1 -1
- package/dist/lib/native-types.d.ts +3 -1
- package/package.json +19 -19
|
@@ -1,8 +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
|
+
import { FFmpegError } from '../lib/error.js';
|
|
3
4
|
import { Frame } from '../lib/frame.js';
|
|
4
5
|
import { Rational } from '../lib/rational.js';
|
|
5
|
-
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
|
+
}
|
|
6
68
|
/**
|
|
7
69
|
* Audio frame buffering utility for encoders with fixed frame size requirements.
|
|
8
70
|
*
|
|
@@ -10,6 +72,16 @@ import { avGetBytesPerSample, avSampleFmtIsPlanar } from '../lib/utilities.js';
|
|
|
10
72
|
* of samples (frame_size). This class buffers incoming frames and outputs frames
|
|
11
73
|
* with exactly the required size.
|
|
12
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
|
+
*
|
|
13
85
|
* Uses FFmpeg's AVAudioFifo internally for efficient sample buffering.
|
|
14
86
|
*
|
|
15
87
|
* @example
|
|
@@ -43,15 +115,37 @@ import { avGetBytesPerSample, avSampleFmtIsPlanar } from '../lib/utilities.js';
|
|
|
43
115
|
* tail.free();
|
|
44
116
|
* }
|
|
45
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
|
+
* ```
|
|
46
124
|
*/
|
|
47
125
|
export class AudioFrameBuffer {
|
|
48
126
|
fifo;
|
|
49
127
|
frame;
|
|
50
128
|
frameSize;
|
|
51
|
-
sampleFormat;
|
|
52
129
|
channels;
|
|
53
|
-
|
|
54
|
-
|
|
130
|
+
sampleStride;
|
|
131
|
+
staging;
|
|
132
|
+
stagingPlanes;
|
|
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;
|
|
55
149
|
/**
|
|
56
150
|
* @param fifo - Underlying AudioFifo instance
|
|
57
151
|
*
|
|
@@ -63,12 +157,13 @@ export class AudioFrameBuffer {
|
|
|
63
157
|
*
|
|
64
158
|
* @param channelLayout - Channel layout
|
|
65
159
|
*
|
|
160
|
+
* @param options - Timing options
|
|
161
|
+
*
|
|
66
162
|
* @internal
|
|
67
163
|
*/
|
|
68
|
-
constructor(fifo, frameSize, sampleFormat, sampleRate, channelLayout) {
|
|
164
|
+
constructor(fifo, frameSize, sampleFormat, sampleRate, channelLayout, options) {
|
|
69
165
|
this.fifo = fifo;
|
|
70
166
|
this.frameSize = frameSize;
|
|
71
|
-
this.sampleFormat = sampleFormat;
|
|
72
167
|
this.channels = channelLayout.nbChannels;
|
|
73
168
|
this.frame = new Frame();
|
|
74
169
|
this.frame.alloc();
|
|
@@ -76,10 +171,27 @@ export class AudioFrameBuffer {
|
|
|
76
171
|
this.frame.format = sampleFormat;
|
|
77
172
|
this.frame.sampleRate = sampleRate;
|
|
78
173
|
this.frame.channelLayout = channelLayout;
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
this.frame.timeBase =
|
|
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;
|
|
82
177
|
this.frame.getBuffer(0); // Allocate buffer once
|
|
178
|
+
// Frame.data is a copy instead of a view where external buffers are not allowed (Electron),
|
|
179
|
+
// so samples are staged in JS memory and copied into the frame with fromBuffer()
|
|
180
|
+
const planar = avSampleFmtIsPlanar(sampleFormat);
|
|
181
|
+
const planeCount = planar ? this.channels : 1;
|
|
182
|
+
this.sampleStride = avGetBytesPerSample(sampleFormat) * (planar ? 1 : this.channels);
|
|
183
|
+
const planeSize = frameSize * this.sampleStride;
|
|
184
|
+
this.staging = Buffer.alloc(planeSize * planeCount);
|
|
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);
|
|
83
195
|
}
|
|
84
196
|
/**
|
|
85
197
|
* Create an audio frame buffer.
|
|
@@ -94,23 +206,30 @@ export class AudioFrameBuffer {
|
|
|
94
206
|
*
|
|
95
207
|
* @param channels - Number of audio channels
|
|
96
208
|
*
|
|
209
|
+
* @param options - Timing options
|
|
210
|
+
*
|
|
97
211
|
* @returns Configured audio frame buffer
|
|
98
212
|
*
|
|
213
|
+
* @throws {RangeError} If an option is out of range
|
|
214
|
+
*
|
|
99
215
|
* @example
|
|
100
216
|
* ```typescript
|
|
101
217
|
* // For Opus encoder at 48kHz with 20ms frames
|
|
102
218
|
* const buffer = AudioFrameBuffer.create(960, AV_SAMPLE_FMT_FLT, 48000, 'mono', 1);
|
|
103
219
|
* ```
|
|
104
220
|
*/
|
|
105
|
-
static create(frameSize, sampleFormat, sampleRate, channelLayout, channels) {
|
|
221
|
+
static create(frameSize, sampleFormat, sampleRate, channelLayout, channels, options = {}) {
|
|
222
|
+
assertAudioFrameBufferOptions(options);
|
|
106
223
|
const fifo = new AudioFifo();
|
|
107
224
|
// Allocate FIFO with capacity for multiple frames
|
|
108
225
|
fifo.alloc(sampleFormat, channels, frameSize * 4);
|
|
109
|
-
return new AudioFrameBuffer(fifo, frameSize, sampleFormat, sampleRate, channelLayout);
|
|
226
|
+
return new AudioFrameBuffer(fifo, frameSize, sampleFormat, sampleRate, channelLayout, options);
|
|
110
227
|
}
|
|
111
228
|
/**
|
|
112
229
|
* Get number of samples currently in buffer.
|
|
113
230
|
*
|
|
231
|
+
* Includes silence inserted for gaps in the input.
|
|
232
|
+
*
|
|
114
233
|
* @returns Number of buffered samples
|
|
115
234
|
*
|
|
116
235
|
* @example
|
|
@@ -119,12 +238,12 @@ export class AudioFrameBuffer {
|
|
|
119
238
|
* ```
|
|
120
239
|
*/
|
|
121
240
|
get size() {
|
|
122
|
-
return this.
|
|
241
|
+
return this.available();
|
|
123
242
|
}
|
|
124
243
|
/**
|
|
125
244
|
* Check if a complete frame is available.
|
|
126
245
|
*
|
|
127
|
-
* Returns true if the
|
|
246
|
+
* Returns true if the buffer holds at least frameSize samples.
|
|
128
247
|
*
|
|
129
248
|
* @returns True if a full frame can be pulled
|
|
130
249
|
*
|
|
@@ -137,7 +256,7 @@ export class AudioFrameBuffer {
|
|
|
137
256
|
* ```
|
|
138
257
|
*/
|
|
139
258
|
hasFrame() {
|
|
140
|
-
return this.
|
|
259
|
+
return this.available() >= this.frameSize;
|
|
141
260
|
}
|
|
142
261
|
/**
|
|
143
262
|
* Push an audio frame into the buffer asynchronously.
|
|
@@ -145,8 +264,23 @@ export class AudioFrameBuffer {
|
|
|
145
264
|
* The frame's samples are added to the internal FIFO.
|
|
146
265
|
* Call hasFrame() and pull() to retrieve fixed-size output frames.
|
|
147
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
|
+
*
|
|
148
278
|
* @param frame - Audio frame to buffer
|
|
149
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
|
+
*
|
|
150
284
|
* @example
|
|
151
285
|
* ```typescript
|
|
152
286
|
* await buffer.push(filterFrame);
|
|
@@ -155,17 +289,24 @@ export class AudioFrameBuffer {
|
|
|
155
289
|
* @see {@link pushSync} For synchronous version
|
|
156
290
|
*/
|
|
157
291
|
async push(frame) {
|
|
158
|
-
|
|
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)) {
|
|
159
296
|
throw new Error('AudioFrameBuffer.push() requires an audio frame');
|
|
160
297
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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;
|
|
166
309
|
}
|
|
167
|
-
// Write frame data to FIFO
|
|
168
|
-
await this.fifo.write(frame.data, frame.nbSamples);
|
|
169
310
|
}
|
|
170
311
|
/**
|
|
171
312
|
* Push an audio frame into the buffer synchronously.
|
|
@@ -173,9 +314,14 @@ export class AudioFrameBuffer {
|
|
|
173
314
|
*
|
|
174
315
|
* The frame's samples are added to the internal FIFO.
|
|
175
316
|
* Call hasFrame() and pullSync() to retrieve fixed-size output frames.
|
|
317
|
+
* Timestamps are handled as in push().
|
|
176
318
|
*
|
|
177
319
|
* @param frame - Audio frame to buffer
|
|
178
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
|
+
*
|
|
179
325
|
* @example
|
|
180
326
|
* ```typescript
|
|
181
327
|
* buffer.pushSync(filterFrame);
|
|
@@ -184,27 +330,35 @@ export class AudioFrameBuffer {
|
|
|
184
330
|
* @see {@link push} For async version
|
|
185
331
|
*/
|
|
186
332
|
pushSync(frame) {
|
|
187
|
-
|
|
333
|
+
const nbSamples = frame.nbSamples;
|
|
334
|
+
if (!(frame.sampleRate > 0 && nbSamples > 0)) {
|
|
188
335
|
throw new Error('AudioFrameBuffer.pushSync() requires an audio frame');
|
|
189
336
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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;
|
|
195
348
|
}
|
|
196
|
-
// Write frame data to FIFO
|
|
197
|
-
this.fifo.writeSync(frame.data, frame.nbSamples);
|
|
198
349
|
}
|
|
199
350
|
/**
|
|
200
351
|
* Pull a fixed-size audio frame from the buffer asynchronously.
|
|
201
352
|
*
|
|
202
|
-
* 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.
|
|
203
355
|
* Returns null if not enough samples are available.
|
|
204
356
|
* Reuses internal frame buffer for efficiency (like Decoder does).
|
|
205
357
|
*
|
|
206
358
|
* @returns Audio frame with exactly frameSize samples, or null if insufficient samples
|
|
207
359
|
*
|
|
360
|
+
* @throws {FFmpegError} If the FIFO cannot be read or the frame cannot be filled
|
|
361
|
+
*
|
|
208
362
|
* @throws {Error} If frame cloning fails (out of memory)
|
|
209
363
|
*
|
|
210
364
|
* @example
|
|
@@ -221,29 +375,25 @@ export class AudioFrameBuffer {
|
|
|
221
375
|
if (!this.hasFrame()) {
|
|
222
376
|
return null;
|
|
223
377
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
this.nextPts += BigInt(this.frameSize);
|
|
230
|
-
// Clone frame for user (like Decoder does)
|
|
231
|
-
const cloned = this.frame.clone();
|
|
232
|
-
if (!cloned) {
|
|
233
|
-
throw new Error('Failed to clone frame (out of memory)');
|
|
234
|
-
}
|
|
235
|
-
return cloned;
|
|
378
|
+
const pts = this.headPts();
|
|
379
|
+
// Read samples from FIFO and copy them into the reusable frame
|
|
380
|
+
await this.readStaging(this.frameSize);
|
|
381
|
+
this.commitStaging();
|
|
382
|
+
return this.emitFrame(pts);
|
|
236
383
|
}
|
|
237
384
|
/**
|
|
238
385
|
* Pull a fixed-size audio frame from the buffer synchronously.
|
|
239
386
|
* Synchronous version of pull.
|
|
240
387
|
*
|
|
241
|
-
* 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.
|
|
242
390
|
* Returns null if not enough samples are available.
|
|
243
391
|
* Reuses internal frame buffer for efficiency (like Decoder does).
|
|
244
392
|
*
|
|
245
393
|
* @returns Audio frame with exactly frameSize samples, or null if insufficient samples
|
|
246
394
|
*
|
|
395
|
+
* @throws {FFmpegError} If the FIFO cannot be read or the frame cannot be filled
|
|
396
|
+
*
|
|
247
397
|
* @throws {Error} If frame cloning fails (out of memory)
|
|
248
398
|
*
|
|
249
399
|
* @example
|
|
@@ -260,18 +410,11 @@ export class AudioFrameBuffer {
|
|
|
260
410
|
if (!this.hasFrame()) {
|
|
261
411
|
return null;
|
|
262
412
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
this.
|
|
267
|
-
|
|
268
|
-
this.nextPts += BigInt(this.frameSize);
|
|
269
|
-
// Clone frame for user (like Decoder does)
|
|
270
|
-
const cloned = this.frame.clone();
|
|
271
|
-
if (!cloned) {
|
|
272
|
-
throw new Error('Failed to clone frame (out of memory)');
|
|
273
|
-
}
|
|
274
|
-
return cloned;
|
|
413
|
+
const pts = this.headPts();
|
|
414
|
+
// Read samples from FIFO and copy them into the reusable frame
|
|
415
|
+
this.readStagingSync(this.frameSize);
|
|
416
|
+
this.commitStaging();
|
|
417
|
+
return this.emitFrame(pts);
|
|
275
418
|
}
|
|
276
419
|
/**
|
|
277
420
|
* Pull the final partial frame from the buffer asynchronously.
|
|
@@ -281,10 +424,13 @@ export class AudioFrameBuffer {
|
|
|
281
424
|
* Intended for end-of-stream flushing only - without it the tail (up to
|
|
282
425
|
* frameSize - 1 samples) would be dropped. Returns null if the buffer is
|
|
283
426
|
* empty, or while a complete frame is still available (drain those with
|
|
284
|
-
* pull() first).
|
|
427
|
+
* pull() first). The padding counts as audio, so frames pushed afterwards are
|
|
428
|
+
* timed after it.
|
|
285
429
|
*
|
|
286
430
|
* @returns Silence-padded audio frame with frameSize samples, or null if nothing to drain
|
|
287
431
|
*
|
|
432
|
+
* @throws {FFmpegError} If the FIFO cannot be read or the frame cannot be filled
|
|
433
|
+
*
|
|
288
434
|
* @throws {Error} If frame cloning fails (out of memory)
|
|
289
435
|
*
|
|
290
436
|
* @example
|
|
@@ -300,24 +446,19 @@ export class AudioFrameBuffer {
|
|
|
300
446
|
* @see {@link pull} For pulling complete frames
|
|
301
447
|
*/
|
|
302
448
|
async pullPartial() {
|
|
303
|
-
const remaining = this.
|
|
449
|
+
const remaining = this.available();
|
|
304
450
|
if (remaining <= 0 || remaining >= this.frameSize) {
|
|
305
451
|
return null;
|
|
306
452
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
await this.fifo.read(this.frame.data, remaining);
|
|
453
|
+
const pts = this.headPts();
|
|
454
|
+
// Read the remaining samples into the head of the staging buffer
|
|
455
|
+
await this.readStaging(remaining);
|
|
311
456
|
// Pad the tail with silence up to frameSize
|
|
312
|
-
this.
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
if (!cloned) {
|
|
318
|
-
throw new Error('Failed to clone frame (out of memory)');
|
|
319
|
-
}
|
|
320
|
-
return cloned;
|
|
457
|
+
this.fillSilence(remaining, this.frameSize);
|
|
458
|
+
this.commitStaging();
|
|
459
|
+
const frame = this.emitFrame(pts);
|
|
460
|
+
this.advanceTailToHead();
|
|
461
|
+
return frame;
|
|
321
462
|
}
|
|
322
463
|
/**
|
|
323
464
|
* Pull the final partial frame from the buffer synchronously.
|
|
@@ -330,6 +471,8 @@ export class AudioFrameBuffer {
|
|
|
330
471
|
*
|
|
331
472
|
* @returns Silence-padded audio frame with frameSize samples, or null if nothing to drain
|
|
332
473
|
*
|
|
474
|
+
* @throws {FFmpegError} If the FIFO cannot be read or the frame cannot be filled
|
|
475
|
+
*
|
|
333
476
|
* @throws {Error} If frame cloning fails (out of memory)
|
|
334
477
|
*
|
|
335
478
|
* @example
|
|
@@ -345,19 +488,492 @@ export class AudioFrameBuffer {
|
|
|
345
488
|
* @see {@link pullSync} For pulling complete frames
|
|
346
489
|
*/
|
|
347
490
|
pullPartialSync() {
|
|
348
|
-
const remaining = this.
|
|
491
|
+
const remaining = this.available();
|
|
349
492
|
if (remaining <= 0 || remaining >= this.frameSize) {
|
|
350
493
|
return null;
|
|
351
494
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
this.fifo.readSync(this.frame.data, remaining);
|
|
495
|
+
const pts = this.headPts();
|
|
496
|
+
// Read the remaining samples into the head of the staging buffer
|
|
497
|
+
this.readStagingSync(remaining);
|
|
356
498
|
// Pad the tail with silence up to frameSize
|
|
357
|
-
this.
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
499
|
+
this.fillSilence(remaining, this.frameSize);
|
|
500
|
+
this.commitStaging();
|
|
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)
|
|
361
977
|
const cloned = this.frame.clone();
|
|
362
978
|
if (!cloned) {
|
|
363
979
|
throw new Error('Failed to clone frame (out of memory)');
|
|
@@ -365,37 +981,35 @@ export class AudioFrameBuffer {
|
|
|
365
981
|
return cloned;
|
|
366
982
|
}
|
|
367
983
|
/**
|
|
368
|
-
* Fill the
|
|
984
|
+
* Fill the staged sample range [fromSample, toSample) with silence.
|
|
369
985
|
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
* 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.
|
|
373
988
|
*
|
|
374
989
|
* @param fromSample - First sample index to silence
|
|
375
990
|
*
|
|
991
|
+
* @param toSample - Sample index after the last one to silence
|
|
992
|
+
*
|
|
376
993
|
* @internal
|
|
377
994
|
*/
|
|
378
|
-
|
|
379
|
-
const
|
|
380
|
-
|
|
381
|
-
const stride = avSampleFmtIsPlanar(this.sampleFormat) ? bytesPerSample : bytesPerSample * this.channels;
|
|
382
|
-
const planes = this.frame.data ?? [];
|
|
383
|
-
for (const plane of planes) {
|
|
384
|
-
plane.fill(silence, fromSample * stride, this.frameSize * stride);
|
|
995
|
+
fillSilence(fromSample, toSample) {
|
|
996
|
+
for (const plane of this.stagingPlanes) {
|
|
997
|
+
plane.fill(this.silenceValue, fromSample * this.sampleStride, toSample * this.sampleStride);
|
|
385
998
|
}
|
|
386
999
|
}
|
|
387
1000
|
/**
|
|
388
|
-
*
|
|
1001
|
+
* Copy the staged samples into the reusable frame.
|
|
389
1002
|
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
*
|
|
1003
|
+
* A frame pulled earlier may still share the frame's buffer, so the buffer is
|
|
1004
|
+
* made writable first instead of overwriting the samples under that frame.
|
|
1005
|
+
*
|
|
1006
|
+
* @throws {FFmpegError} If the frame buffer cannot be made writable or filled
|
|
1007
|
+
*
|
|
1008
|
+
* @internal
|
|
394
1009
|
*/
|
|
395
|
-
|
|
396
|
-
this.
|
|
397
|
-
this.
|
|
398
|
-
this.firstFramePts = null;
|
|
1010
|
+
commitStaging() {
|
|
1011
|
+
FFmpegError.throwIfError(this.frame.makeWritable(), 'Failed to make frame writable');
|
|
1012
|
+
FFmpegError.throwIfError(this.frame.fromBuffer(this.staging), 'Failed to copy samples into frame');
|
|
399
1013
|
}
|
|
400
1014
|
/**
|
|
401
1015
|
* Free the buffer and all resources.
|