Webcast.js
The Webcast Flowchart |
Description
The webcast library is designed to write browser-based clients to stream local files and live media (webcam video, microphone audio).
The API contains several classes:
-
Webcast.Encoder.Raw
: encoder returning raw s8 samples. -
Webcast.Encoder.Mp3
: encoder returning mp3 data. Requires libshine.js. -
Webcast.Encoder.Resample
: a wrapper to resample encoder's input. Requires libsamplerate.js. -
Webcast.Encoder.Asynchronous
: a wrapper to encode in a Web Worker -
Webcast.Socket
: a simple wrapper aroundWebSockets
that implements thewebcast
protocol. -
Webcast.Node
: a wrapper to create awebcast
node, in-par with the Web Audio API.
Here's the highlight of how to use the library:
var source = (...);
var encoder = new Webcast.Encoder.Mp3({
channels: 2,
samplerate: 44100,
bitrate: 128
});
if (inputSampleRate !== 44100) {
encoder = new Webcast.Encoder.Resample({
encoder: encoder,
samplerate: inputSampleRate
});
}
if (useWorker) {
encoder = new Webcast.Encoder.Asynchronous({
encoder: encoder,
scripts: [(...)], // full path to required scripts for the worker.
// usually includes requires encoders and webcast.js
});
}
var webcast = context.createWebcastSource(4096, 2);
source.connect(webcast);
webcast.connect(audioContext.destination);
webcast.sendMetadata({
title: "My Awesome Stream",
artist: "The Dude"
});
The library involves several cutting-edge technologies and, thus, require a fairly modern browser. Here's a quick summary of the required technologies:
- WebSocket API: This is the transport layer. It is readily available in most modern browsers.
- Web Audio API: This is the API used to manipulate audio and video data inside the browser.
- asm.js: This is the technology used to optimize the mp3 encoder.
How to test?
Client
A browser-based streaming client, sending mp3 encoded data, using libshine.js or raw PCM data is available at webcast/webcaster
Server
The webcast/webcast.js repository contains a demo server, written in NodeJS.
Alternatively, a fully functional implementation of the protocol is available in liquidsoap. To test it, you can simply run liquidsoap with the following command line:
liquidsoap 'output.ao(fallible=true,audio_to_stereo(input.harbor("mount",port=8080)))'