destroy and recreate output stream on error
This commit is contained in:
parent
3289bc651d
commit
c1b7220399
1 changed files with 16 additions and 7 deletions
|
@ -14,8 +14,9 @@ class StreamBuffer extends EventEmitter {
|
||||||
paused: false,
|
paused: false,
|
||||||
hiccups: 0
|
hiccups: 0
|
||||||
};
|
};
|
||||||
|
this.audiosettings = settings?.audio;
|
||||||
this.#setupBuffer(settings?.threshold);
|
this.#setupBuffer(settings?.threshold);
|
||||||
this.#setupStreams(stream, settings?.audio);
|
this.#setupStreams(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
play() {
|
play() {
|
||||||
|
@ -62,25 +63,32 @@ class StreamBuffer extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#setupStreams(stream, audioSettings) {
|
#setupStreams(stream) {
|
||||||
if (stream === undefined) {
|
if (stream === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.streams = {
|
this.streams = {
|
||||||
input: stream,
|
input: stream,
|
||||||
output: new NodeSpeaker({
|
|
||||||
channels: audioSettings?.channels || 2,
|
|
||||||
bitDepth: audioSettings?.bitDepth || 16,
|
|
||||||
sampleRate: audioSettings?.sampleRate || 44100,
|
|
||||||
}),
|
|
||||||
buffer: new Readable()
|
buffer: new Readable()
|
||||||
};
|
};
|
||||||
this.streams.buffer._read = () => { };
|
this.streams.buffer._read = () => { };
|
||||||
|
this.#createOutputStream();
|
||||||
this.#handleBufferStream();
|
this.#handleBufferStream();
|
||||||
this.#handleInputStream();
|
this.#handleInputStream();
|
||||||
this.#handleOutputStream();
|
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() {
|
#isThresholdReached() {
|
||||||
return this.streams.buffer.readableLength >= this.threshold.value;
|
return this.streams.buffer.readableLength >= this.threshold.value;
|
||||||
}
|
}
|
||||||
|
@ -185,6 +193,7 @@ class StreamBuffer extends EventEmitter {
|
||||||
}
|
}
|
||||||
this.streams.output.on('error', (error) => {
|
this.streams.output.on('error', (error) => {
|
||||||
logger.error('output stream encountered an error: ' + error);
|
logger.error('output stream encountered an error: ' + error);
|
||||||
|
this.#createOutputStream();
|
||||||
});
|
});
|
||||||
this.streams.output.on('written', (bytes) => {
|
this.streams.output.on('written', (bytes) => {
|
||||||
this.#writeBufferedChunk(bytes);
|
this.#writeBufferedChunk(bytes);
|
||||||
|
|
Loading…
Reference in a new issue