Skip to content

Commit

Permalink
fix: AudioDecoder output may be fewer than the input chunk count, lea…
Browse files Browse the repository at this point in the history
…ding to decoding timeouts. #71
  • Loading branch information
hughfenghen committed Apr 10, 2024
1 parent 2882571 commit eac0e53
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions packages/av-cliper/src/clips/mp4-clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ class AudioFrameFinder {
this.#decCusorIdx = i;
const s = this.samples[i];
if (s.deleted) continue;
if (samples.length > 10) break;
if (samples.length >= 10) break;
samples.push(s);
}

Expand Down Expand Up @@ -679,24 +679,28 @@ function createAudioChunksDecoder(
) {
type OutputHandle = (pcm: Float32Array[], done: boolean) => void;

let curCb: ((pcm: Float32Array[]) => void) | null = null;
let curCb: ((pcm: Float32Array[], done: boolean) => void) | null = null;
const needResample = resampleRate !== decoderConf.sampleRate;
const resampleQ = createPromiseQueue<Float32Array[]>((resampedPCM) => {
curCb?.(resampedPCM);
});
const resampleQ = createPromiseQueue<[Float32Array[], boolean]>(
([resampedPCM, done]) => {
curCb?.(resampedPCM, done);
},
);

const adec = new AudioDecoder({
output: (ad) => {
const pcm = extractPCM4AudioData(ad);
const done = adec.decodeQueueSize === 0;
if (needResample) {
resampleQ(() =>
audioResample(pcm, ad.sampleRate, {
resampleQ(async () => [
await audioResample(pcm, ad.sampleRate, {
rate: resampleRate,
chanCount: ad.numberOfChannels,
}),
);
done,
]);
} else {
curCb?.(pcm);
curCb?.(pcm, done);
}
ad.close();
},
Expand All @@ -716,10 +720,7 @@ function createAudioChunksDecoder(
return;
}

let i = 0;
curCb = (pcm) => {
i += 1;
const done = i >= t.chunks.length;
curCb = (pcm, done) => {
t.cb(pcm, done);
if (done) {
curCb = null;
Expand Down

0 comments on commit eac0e53

Please sign in to comment.