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

plain js version? #1

Open
thebetterjort opened this issue Nov 18, 2016 · 3 comments
Open

plain js version? #1

thebetterjort opened this issue Nov 18, 2016 · 3 comments

Comments

@thebetterjort
Copy link

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

@doublerebel
Copy link
Member

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.

@doublerebel
Copy link
Member

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]);
        }
    };
}

@thebetterjort
Copy link
Author

thebetterjort commented Nov 18, 2016

Thank you! I'm going to mess around. This looks to be the missing puzzle piece.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants