Skip to content

Commit 52025fb

Browse files
fix: race condition on finishBuffering
This commit fixes an issue where, if a small file gets inputed (example: 1MB) the downloads finish too fast, making the finishBuffering event be emitted even if the decoder was still processing data, Signed-off-by: toddynnn <86982643+ToddyTheNoobDud@users.noreply.github.com>
1 parent 2531ae5 commit 52025fb

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

src/playback/processing/streamProcessor.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,10 +1058,7 @@ class MP4ToAACStream extends Transform {
10581058
_emitSampleWithADTS(sample) {
10591059
const { profile, samplingIndex, channelCount } = this.audioConfig
10601060

1061-
const sampleData =
1062-
sample.data instanceof ArrayBuffer
1063-
? Buffer.from(sample.data)
1064-
: Buffer.from(sample.data.buffer || sample.data)
1061+
const sampleData = Buffer.from(sample.data)
10651062

10661063
this.push(
10671064
_createAdtsHeader(
@@ -1089,13 +1086,8 @@ class MP4ToAACStream extends Transform {
10891086
if (codecParts.length >= 3) {
10901087
const objectType = Number.parseInt(codecParts[2], 10)
10911088

1092-
if (objectType === 5) {
1093-
const coreSamplingIndex = SAMPLE_RATES.indexOf(
1094-
track.audio.sample_rate / 2
1095-
)
1096-
if (coreSamplingIndex !== -1) {
1097-
samplingIndex = coreSamplingIndex
1098-
}
1089+
if (objectType === 5 || objectType === 29) {
1090+
profile = 2
10991091
} else {
11001092
profile = objectType
11011093
}
@@ -1808,9 +1800,9 @@ class StreamAudioResource extends BaseAudioResource {
18081800
sampleRate: AUDIO_CONFIG.sampleRate,
18091801
channels: AUDIO_CONFIG.channels
18101802
})
1811-
1803+
18121804
const flowController = new FlowController(filters, volumeTransformer, fadeTransformer, audioMixer)
1813-
1805+
18141806
const opusEncoder = new OpusEncoder({
18151807
rate: AUDIO_CONFIG.sampleRate,
18161808
channels: AUDIO_CONFIG.channels,
@@ -1847,6 +1839,8 @@ class StreamAudioResource extends BaseAudioResource {
18471839
pipeline(streams, (err) => {
18481840
if (err && !this._destroyed) {
18491841
opusEncoder.emit('error', err)
1842+
} else if (!this._destroyed) {
1843+
this.stream?.emit('finishBuffering')
18501844
}
18511845
})
18521846

@@ -1872,7 +1866,7 @@ class StreamAudioResource extends BaseAudioResource {
18721866

18731867
_setupEventHandlers(inputStream) {
18741868
inputStream.on('finishBuffering', () => {
1875-
this.stream?.emit('finishBuffering')
1869+
// Waiting for the pipeline to finish
18761870
})
18771871

18781872
inputStream.on('error', (err) => {

0 commit comments

Comments
 (0)