@fonoster/streams
v0.17.1
Published
Core support for Fonoster Streams
Readme
streams
This is a NodeJS implementation of the AudioSocket protocol, which is a simple protocol for accessing bidirectional audio streams from Asterisk.
Installation
$ npm install --save @fonoster/streamsExample
While the AudioSocket is a utility for Fonoster Streams, it can be used as a standalone module. To use this library with Asterisk, you must configure your dialplan to use the AudioSocket application. Here is an example of how to use the AudioSocket with Asterisk:
exten = 100,1,Verbose("Call to AudioSocket via Dialplan Application")
same = n,Answer()
same = n,AudioSocket(40325ec2-5efd-4bd3-805f-53576e581d13,server.example.com:9092)
same = n,Hangup()Or with the Dial application:
exten = 100,1,Verbose("Call to AudioSocket via Dialplan Application")
same = n,Answer()
same = n,Dial(SIP/100,30,A(AudioSocket(40325ec2-5efd-4bd3-805f-53576e581d13,server.example.com:9092)))
same = n,Hangup()Connecting to the AudioSocket server using ARI with the externalMedia endpoint is also possible. You must ensure you set the transport to TCP the UUID in the data field.
Currently, the payload towards Asterisk is limited to signed linear, 16-bit, 8kHz, mono PCM (little-endian). However, the payload from Asterisk can be changed with the
formatparameter when using ARI. Please see the AudioSocket Server implementation, for interesting notes about AudioSocket.
Once Asterisk is configured, you can use the AudioSocket class to create a server that listens for connections. Here is an example of how to use the AudioSocket class:
const { AudioSocket } = require("@fonoster/streams");
const audioSocket = new AudioSocket();
audioSocket.onConnection(async (req, res) => {
console.log("new connection from:", req.ref);
res.on("data", (data) => {
// Do something with the audio data
);
res.on("end", () => {
// Do something when the stream ends
});
res.on("error", (err) => {
// Do something when an error occurs
});
// Utility for playing audio files
await res.play("/path/to/audio/file");
});
audioSocket.listen(9092, () => {
console.log("server listening on port 9092");
});APIs
AudioSocket
A NodeJS implementation of the AudioSocket protocol. The AudioSocket protocol is a simple protocol for streaming audio from Asterisk to a NodeJS application. The protocol is based on the I/O multiplexing model and uses
Kind: global class
See: AudioStream
new AudioSocket()
Constructs a new AudioSocket instance.
Example
const { AudioSocket } = require("@fonoster/streams");
const audioSocket = new AudioSocket();
audioSocket.onConnection(async (req, res) => {
console.log("new connection from:", req.ref);
res.on("data", (data) => {
// Do something with the audio data
);
res.on("end", () => {
// Do something when the stream ends
});
res.on("error", (err) => {
// Do something when an error occurs
});
// Utility for playing audio files
await res.play("/path/to/audio/file");
});
audioSocket.listen(9092, () => {
console.log("server listening on port 9092");
});audioSocket.onConnection(handler)
Sets the handler to be called when a new connection is established.
Kind: instance method of AudioSocket
| Param | Type | Description | | --- | --- | --- | | handler | function | The handler to call when a new connection is established |
Example
audioSocket.onConnection(async (req, res) => {
console.log("new connection from:", req.ref);
await res.play("/path/to/audio/file");
});audioSocket.close()
Closes the server and stops listening for connections.
Kind: instance method of AudioSocket
AudioStream
Object representing a stream of bidirectional audio data and control messages.
Kind: global class
new AudioStream(stream, socket)
Creates a new AudioStream.
| Param | Type | Description | | --- | --- | --- | | stream | Readable | A readable stream | | socket | net.Socket | A TCP socket |
audioStream.write(data)
Writes media data to the stream.
Kind: instance method of AudioStream
| Param | Type | Description | | --- | --- | --- | | data | Buffer | The data to write |
audioStream.hangup()
Sends a hangup message to the stream and closes the connection.
Kind: instance method of AudioStream
audioStream.play(filePath) ⇒ Promise.<void>
Utility for playing audio files.
Kind: instance method of AudioStream
| Param | Type | Description | | --- | --- | --- | | filePath | string | The path to the audio file |
audioStream.onData(callback) ⇒ AudioStream
Adds a listener for the data event.
Kind: instance method of AudioStream
Returns: AudioStream - The AudioStream instance
See: EventType.DATA
| Param | Type | Description | | --- | --- | --- | | callback | function | The callback to be executed |
audioStream.onClose(callback) ⇒ AudioStream
Adds a listener for the end event.
Kind: instance method of AudioStream
Returns: AudioStream - The AudioStream instance
See: EventType.END
| Param | Type | Description | | --- | --- | --- | | callback | function | The callback to be executed |
audioStream.onError(callback) ⇒ AudioStream
Adds a listener for the error event.
Kind: instance method of AudioStream
Returns: AudioStream - The AudioStream instance
See: EventType.ERROR
| Param | Type | Description | | --- | --- | --- | | callback | function | The callback to be executed |
