Open thebetterjort opened 7 years ago
Hey Tyler,
If you install the package from npm, you will receive the compiled JS in the package. It can be used out-of-the-box with plain JavaScript, just install and do EvenNicercast = require('even-nicercast')
.
In the most basic example, we just create a new server and pipe an mp3 stream to it. If you have an existing express app, using EvenNicercast looks something like this:
EvenNicercast = require('even-nicercast');
var app = express(); // your express app
var advertise = "http://your-domain-that-will-be-sent-in-the-playlist.com"
var port = 80; // probably 80 if you are using an existing express app,
// port should match the express app for advertising in the playlist
var server = new EvenNicercast {app, advertise, port};
app.use('/source', function(req, res, next) { // listen for your broadcast source to connect
req.pipe(server, {end: false}); // don't end the server stream when the source dies
// don't forget to unpipe on connection end
});
app.listen()
This assumes you're encoding the mp3 stream before sending it to EvenNicercast. EvenNicercast doesn't re-encode the stream, so in theory it could pass through .ogg or other formats, but right now I only send the headers for audio/mpeg
. If you need to re-encode it's possible to use even-nicercast/encoding-server
for that.
I should mention, express does not do custom HTTP methods, and the common icecast source clients open with SOURCE ... ICE/1.0
.
If you're using a client that does that, instead of a standard POST (or PUT or PATCH), then you'll have to intercept the server connection to grab the socket before express can process it. Here is such an implementation, though slightly outdated: from http://stackoverflow.com/a/24298059
server.on('connection', function (socket) {
var originalOnDataFunction = socket.ondata;
var newLineOffset;
var receiveBuffer = new Buffer(0);
socket.ondata = function (d, start, end) {
receiveBuffer = Buffer.concat([receiveBuffer, d.slice(start, end)]);
if ((newLineOffset = receiveBuffer.toString('ascii').indexOf('\n')) > -1) {
var firstLineParts = receiveBuffer.slice(0, newLineOffset).toString().split(' ');
firstLineParts[0] = firstLineParts[0].replace(/^SOURCE$/ig, 'PUT');
firstLineParts[2] = firstLineParts[2].replace(/^ICE\//ig, 'HTTP/');
receiveBuffer = Buffer.concat([
new Buffer(
firstLineParts.join(' ') + '\r\n' +
'Content-Length: 9007199254740992\r\n'
),
receiveBuffer.slice(newLineOffset +1)
]);
socket.ondata = originalOnDataFunction;
socket.ondata.apply(this, [receiveBuffer, 0, receiveBuffer.length]);
}
};
}
Thank you! I'm going to mess around. This looks to be the missing puzzle piece.
I think this is what I've been looking for.. I'm not a coffee master in the lang only in the brew.
I don't want to install icecast server. I want my broadcast to point to a nodejs express server instead.
Could you give me a 5 minute instructions or example of how to use this?
Sincerely yours, Tyler