Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AudioConfig.fromReadableStream implementation #294

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/sdk/Audio/AudioConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { PathLike } from "fs";
import { Readable } from "stream";
import {
FileAudioSource,
MicAudioSource,
Expand Down Expand Up @@ -105,6 +106,32 @@ export abstract class AudioConfig {
throw new Error("Not Supported Type");
}

/**
* Creates an AudioConfig object from a standard Readable stream.
* Useful when streaming audio from external network sources (e.g., ffmpeg UDP stream into a Docker container).
* @member AudioConfig.fromReadableStream
* @function
* @public
* @param {Readable} audioStream - Custom audio input from a Readable stream. Currently, only WAV / PCM is supported.
* @param {AudioStreamFormat} format - The audio data format in which audio
* will be returned from the stream's read() method.
* @returns {AudioConfig} The audio output configuration being created.
*/
public static fromReadableStream(audioStream: Readable, format?: AudioStreamFormat): AudioConfig {
const pushStream = AudioInputStream.createPushStream(format);

audioStream.on('readable', () => {
const buffer = audioStream.read();
if (buffer === null) {
pushStream.close();
} else {
pushStream.write(buffer);
}
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing ever closes the push stream. That can lead to recognition hanging when the Readable runs out of data.

Copy link
Member Author

@SpaceKatt SpaceKatt Dec 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent point! I've updated the code to close the push stream when read() returns null.

Are there any other cases you believe should be handled? (e.g., do you have thoughts around .on('error', () => { ??? }), or should error handling be delegated to the client?)

return AudioConfig.fromStreamInput(pushStream);
}

/**
* Creates an AudioConfig object representing the default speaker.
* @member AudioConfig.fromDefaultSpeakerOutput
Expand Down