@mediabunny/aac-encoder 1.65.2 → 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/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
- while (this.pendingFrames >= this.encoderFrameSize) {
175
- await this.encodeOneFrame();
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 = frameSize;
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 frameSize = this.encoderFrameSize;
208
- const frameSamples = frameSize * channels;
209
-
210
- const frameData = this.pendingBuffer.slice(0, frameSamples);
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 result = await this.sendCommand({
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 += frameSize;
232
+ this.nextSampleTimestampInSamples += this.encoderFrameSize;
229
233
 
230
- this.emitPackets(result.packets);
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[]) {