Search code examples
videovideo-streaminghtml5-videohttp-live-streaminglive-streaming

Live Stream Segmentation


If I do not need adaptive video then can I do without segmentation? For example, can I just open WebSocket connection and transmit the video file, client will play it as soon as new portion is received.Are there any cons in such approach?


Solution

  • It's even easier than that. You can do this type of streaming without Web Sockets and use normal HTTP. Client-side is something like:

    <video src="https://stream-server.example.com/stream"></video>
    

    Then, whatever you are using server-side just needs to generate a stream with the appropriate initialization information, followed by wherever you are in the live stream. The browser will begin playback as soon as it can, and all is well.

    This is straight up normal HTTP progressive streaming. It's used all the time with audio, which in most cases doesn't require adaptive bitrate, and can self-sync easily (MP3, ADTS, etc.). You can use it with video too if you don't require adaptive bitrate, and can send that initialization data and chunk the stream in the right place. (If you're unsure how to do this, get cozy with a hex editor and the specs for whichever container format you're using. I've found WebM/Matroska to be extremely easy to work with, and have streamed this way with some EBML NPM packages for a Node.js server.)

    There are a few things to keep in mind:

    • The server is responsible for assembling the stream, so you won't be able to use standard file/blob-based CDNs. For a lot of use cases, this is irrelevant, so you'll need to decide if it matters to you.
    • If the stream stalls, the browser will either wait for those packets to arrive, or in the extreme case it may reconnect. You need to be able to handle that reconnection logic by tracking the byte offsets that you've sent to the client. The client will make a ranged HTTP request to continue where it left off, and you'll need to be able to handle it. (Alternatively, fix with a couple lines of JavaScript to just reset the whole thing if it goes wrong.)
    • On the upside, this is a high quality and extremely simple way to stream!

    Also, look into using Icecast for your server. I used to send video through it this way... haven't tried in years, but I imagine it still works. If not, a simple Node.js server can do it.