destroy and recreate output stream on error

This commit is contained in:
Daniel Sommer 2022-05-02 15:56:51 +02:00
parent 3289bc651d
commit c1b7220399

View file

@ -14,8 +14,9 @@ class StreamBuffer extends EventEmitter {
paused: false,
hiccups: 0
};
this.audiosettings = settings?.audio;
this.#setupBuffer(settings?.threshold);
this.#setupStreams(stream, settings?.audio);
this.#setupStreams(stream);
}
play() {
@ -62,25 +63,32 @@ class StreamBuffer extends EventEmitter {
}
}
#setupStreams(stream, audioSettings) {
#setupStreams(stream) {
if (stream === undefined) {
return;
}
this.streams = {
input: stream,
output: new NodeSpeaker({
channels: audioSettings?.channels || 2,
bitDepth: audioSettings?.bitDepth || 16,
sampleRate: audioSettings?.sampleRate || 44100,
}),
buffer: new Readable()
};
this.streams.buffer._read = () => { };
this.#createOutputStream();
this.#handleBufferStream();
this.#handleInputStream();
this.#handleOutputStream();
}
#createOutputStream() {
if (this.streams.output !== undefined) {
this.streams.output.destroy();
}
this.streams.output = new NodeSpeaker({
channels: audioSettings?.channels || 2,
bitDepth: audioSettings?.bitDepth || 16,
sampleRate: audioSettings?.sampleRate || 44100,
});
}
#isThresholdReached() {
return this.streams.buffer.readableLength >= this.threshold.value;
}
@ -185,6 +193,7 @@ class StreamBuffer extends EventEmitter {
}
this.streams.output.on('error', (error) => {
logger.error('output stream encountered an error: ' + error);
this.#createOutputStream();
});
this.streams.output.on('written', (bytes) => {
this.#writeBufferedChunk(bytes);