Skip to content

Commit 46848b3

Browse files
fix: bound AACDecoderStream pendingChunks, optimize _tightBuffer, and
fix DASH stuck loops Cap AACDecoderStream pendingChunks at 200, drop oldest when full to prevent unbounded growth Clear pendingChunks on destroy to release held buffers and callbacks Optimize _tightBuffer to skip Buffer.from() when buffer is already tight Clean up audioStream data listener on close/end to prevent leak during reconnects DASHHandler: increase prefetch from 1 to 4 and cap segment pace at 5s to prevent stuck recovery loops
1 parent 063560f commit 46848b3

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/playback/dash/DASHHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
} from '../../typings/playback/dash.types.ts'
66
import { logger, makeRequest } from '../../utils.ts'
77

8-
const PREFETCH_COUNT = 1
8+
const PREFETCH_COUNT = 4
99
const MAX_BUFFERED = 16 * 1024
1010

1111
/**
@@ -185,7 +185,7 @@ export class DASHHandler extends Transform {
185185
pushIndex++
186186

187187
if (segmentDuration > 0 && pushIndex < totalSegments) {
188-
const paceMs = segmentDuration * 1000 * 0.8
188+
const paceMs = Math.min(segmentDuration * 1000 * 0.8, 5000)
189189
await this._sleepOrStop(paceMs)
190190
}
191191
}

src/playback/player.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,18 @@ export class Player {
333333
this._onError(err)
334334
})
335335
this.connection.on('audioStream', (audioStream: VoiceAudioStream) => {
336-
audioStream.on('data', () => {
336+
const dataHandler = () => {
337337
this._lastStreamDataTime = Date.now()
338338
if (this.isLyricsSubscribed && !this.isPaused && this.track) {
339339
this._syncLyrics()
340340
}
341+
}
342+
audioStream.on('data', dataHandler)
343+
audioStream.once('close', () => {
344+
audioStream.off('data', dataHandler)
345+
})
346+
audioStream.once('end', () => {
347+
audioStream.off('data', dataHandler)
341348
})
342349
})
343350

src/playback/processing/streamProcessor.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ const _isWebmFormat = (type: string): boolean =>
264264
type.includes('webm') || type.includes('weba')
265265

266266
const _isFlvFormat = (type: string): boolean => type.indexOf('flv') !== -1
267-
const _tightBuffer = (buf: Buffer): Buffer => Buffer.from(buf)
267+
const _tightBuffer = (buf: Buffer): Buffer =>
268+
buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength
269+
? buf
270+
: Buffer.from(buf)
268271
const _toTightArrayBuffer = (buf: Buffer): ArrayBuffer => {
269272
const backing = buf.buffer
270273
if (
@@ -1270,6 +1273,7 @@ class AACDecoderStream extends Transform {
12701273
private ringBuffer: RingBufferLike
12711274
private resamplingQuality: string
12721275
private resamplerCreationPromise: Promise<ResamplerLike> | null
1276+
private static readonly MAX_PENDING_CHUNKS = 200
12731277

12741278
constructor(options: AACDecoderStreamOptions) {
12751279
super({
@@ -1300,6 +1304,7 @@ class AACDecoderStream extends Transform {
13001304
cb: (error?: Error | null) => void
13011305
): void {
13021306
this.ringBuffer.dispose()
1307+
this.pendingChunks.length = 0
13031308
if (this.decoder) this.decoder.free?.()
13041309
if (this.resampler) this.resampler.destroy?.()
13051310
super._destroy(err, cb)
@@ -1429,6 +1434,9 @@ class AACDecoderStream extends Transform {
14291434
callback: TransformCallback
14301435
): void {
14311436
if (!this.isDecoderReady || this.pendingChunks.length > 0) {
1437+
if (this.pendingChunks.length >= AACDecoderStream.MAX_PENDING_CHUNKS) {
1438+
this.pendingChunks.shift()
1439+
}
14321440
this.pendingChunks.push({ chunk, encoding, callback })
14331441
return
14341442
}

0 commit comments

Comments
 (0)