@mediabunny/aac-encoder 1.65.1 → 1.66.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/bundles/mediabunny-aac-encoder.js +1210 -1029
- package/dist/bundles/mediabunny-aac-encoder.min.js +1004 -1004
- package/dist/bundles/mediabunny-aac-encoder.min.mjs +1004 -1004
- package/dist/bundles/mediabunny-aac-encoder.mjs +1210 -1029
- package/dist/modules/build/aac.js +0 -0
- package/dist/modules/src/encode.worker.js +4 -2
- package/dist/modules/src/encoder.d.ts.map +1 -1
- package/dist/modules/src/encoder.js +21 -17
- package/dist/modules/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/bridge.c +71 -9
- package/src/encode.worker.ts +5 -3
- package/src/encoder.ts +26 -19
package/src/encoder.ts
CHANGED
|
@@ -50,6 +50,7 @@ class AacEncoder extends CustomAudioEncoder {
|
|
|
50
50
|
private pendingBuffer = new Float32Array(2 ** 16);
|
|
51
51
|
private pendingFrames = 0;
|
|
52
52
|
private nextSampleTimestampInSamples: number | null = null;
|
|
53
|
+
private inFlightEncode: Promise<void> | null = null;
|
|
53
54
|
|
|
54
55
|
static override supports(codec: AudioCodec, config: AudioEncoderConfig): boolean {
|
|
55
56
|
return codec === 'aac'
|
|
@@ -171,9 +172,14 @@ class AacEncoder extends CustomAudioEncoder {
|
|
|
171
172
|
this.pendingBuffer.set(incomingData, this.pendingFrames * channels);
|
|
172
173
|
this.pendingFrames += incomingFrames;
|
|
173
174
|
|
|
174
|
-
|
|
175
|
-
|
|
175
|
+
let encodedFrames = 0;
|
|
176
|
+
while (this.pendingFrames - encodedFrames >= this.encoderFrameSize) {
|
|
177
|
+
await this.encodeOneFrame(encodedFrames);
|
|
178
|
+
encodedFrames += this.encoderFrameSize;
|
|
176
179
|
}
|
|
180
|
+
|
|
181
|
+
this.pendingBuffer.copyWithin(0, encodedFrames * channels, this.pendingFrames * channels);
|
|
182
|
+
this.pendingFrames -= encodedFrames;
|
|
177
183
|
}
|
|
178
184
|
|
|
179
185
|
async flush() {
|
|
@@ -185,11 +191,14 @@ class AacEncoder extends CustomAudioEncoder {
|
|
|
185
191
|
const frameSamples = frameSize * channels;
|
|
186
192
|
|
|
187
193
|
this.pendingBuffer.fill(0, usedSamples, frameSamples);
|
|
188
|
-
this.pendingFrames =
|
|
194
|
+
this.pendingFrames = 0;
|
|
189
195
|
|
|
190
|
-
await this.encodeOneFrame();
|
|
196
|
+
await this.encodeOneFrame(0);
|
|
191
197
|
}
|
|
192
198
|
|
|
199
|
+
await this.inFlightEncode;
|
|
200
|
+
this.inFlightEncode = null;
|
|
201
|
+
|
|
193
202
|
const result = await this.sendCommand({ type: 'flush', data: { ctx: this.ctx } });
|
|
194
203
|
this.emitPackets(result.packets);
|
|
195
204
|
|
|
@@ -200,34 +209,32 @@ class AacEncoder extends CustomAudioEncoder {
|
|
|
200
209
|
this.worker?.terminate();
|
|
201
210
|
}
|
|
202
211
|
|
|
203
|
-
private async encodeOneFrame() {
|
|
212
|
+
private async encodeOneFrame(startFrame: number) {
|
|
204
213
|
assert(this.nextSampleTimestampInSamples !== null);
|
|
205
214
|
|
|
206
215
|
const channels = this.numberOfChannels;
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
// Shift remaining using copyWithin
|
|
213
|
-
this.pendingFrames -= frameSize;
|
|
214
|
-
if (this.pendingFrames > 0) {
|
|
215
|
-
this.pendingBuffer.copyWithin(0, frameSamples, frameSamples + this.pendingFrames * channels);
|
|
216
|
-
}
|
|
216
|
+
const frameData = this.pendingBuffer.slice(
|
|
217
|
+
startFrame * channels,
|
|
218
|
+
(startFrame + this.encoderFrameSize) * channels,
|
|
219
|
+
);
|
|
217
220
|
|
|
218
221
|
const audioData = frameData.buffer;
|
|
219
|
-
const
|
|
222
|
+
const encode = this.sendCommand({
|
|
220
223
|
type: 'encode',
|
|
221
224
|
data: {
|
|
222
225
|
ctx: this.ctx,
|
|
223
226
|
audioData,
|
|
224
227
|
timestamp: this.nextSampleTimestampInSamples,
|
|
225
228
|
},
|
|
226
|
-
}, [audioData]);
|
|
229
|
+
}, [audioData]).then(result => this.emitPackets(result.packets));
|
|
230
|
+
encode.catch(() => {}); // Surfaces through whoever awaits it next
|
|
227
231
|
|
|
228
|
-
this.nextSampleTimestampInSamples +=
|
|
232
|
+
this.nextSampleTimestampInSamples += this.encoderFrameSize;
|
|
229
233
|
|
|
230
|
-
|
|
234
|
+
// Keep one frame in flight, so the worker already has the next frame by the time it's done with the current one
|
|
235
|
+
const previousEncode = this.inFlightEncode;
|
|
236
|
+
this.inFlightEncode = encode;
|
|
237
|
+
await previousEncode;
|
|
231
238
|
}
|
|
232
239
|
|
|
233
240
|
private emitPackets(packets: PacketInfo[]) {
|