Search code examples
node.jsgoogle-chromeffmpegmp3audio-streaming

Chrome "stalling" when streaming mp3 file from nodejs windows only


We've got a really annoying bug when trying to send mp3 data. We've got the following set up.

Web cam producing aac -> ffmpeg convert to adts -> send to nodejs server -> ffmpeg on server converts adts to mp3 -> mp3 then streamed to browser.

This works *perfectly" on Linux ( chrome with HTML5 and flash, firefox flash only )

However on windows the sound just "stalls", no matter what combination we use ( browser/html5/flash ). If however we shutdown the server the sound then immediately starts to play as we expect.

For some reason on windows based machines it's as if the sound is being buffered "waiting" for something but we don't know what that is.

Any help would be greatly appreciated.

Relevant code in node

    res.setHeader('Connection', 'Transfer-Encoding');
    res.setHeader('Content-Type', 'audio/mpeg');
    res.setHeader('Transfer-Encoding', 'chunked');
    res.writeHeader('206');

    that.eventEmitter.on('soundData', function (data) {
        debug("Got sound data" + data.cameraId + " " + req.params.camera_id);
        if (req.params.camera_id == data.cameraId) {
            debug("Sending data direct to browser");
            res.write(data.sound);
        }
    });

Code on browser

       soundManager.setup({
                            url: 'http://dashboard.agricamera.co.uk/themes/agricamv2/swf/soundmanager2.swf',
                            useHTML5Audio: false,
                            onready: function () {
                                that.log("Sound manager is now ready")
                                var mySound = soundManager.createSound({
                                    url: src,
                                    autoLoad: true,
                                    autoPlay: true,
                                    stream: true,
                                });
                            }
                        });

Solution

  • If however we shutdown the server the sound then immediately starts to play as we expect.

    For some reason on windows based machines it's as if the sound is being buffered "waiting" for something but we don't know what that is.

    That's exactly what's happening.

    First off, chrome can play ADTS streams so if possible, just use that directly and save yourself some audio quality by not having to use a second lossy codec in the chain.

    Next, don't use soundManager, or at least let it use HTML5 audio. You don't need the Flash fallback these days in most cases, and Chrome is perfectly capable of playing your streams. I suspect this is where your problem lies.

    Next, try disabling chunked transfer. Many clients don't like transfer encoding on streams.

    Finally, I have seen cases where Chrome's built-in media handling (which I believe varies from OS to OS) cannot sync to the stream. There are a few bug tickets out there for Chromium. If your playback timer isn't incrementing, this is likely your problem and you can simply try to reload the stream programmatically to work around it.