Search code examples
httphttp-live-streaming

How to implement http video streaming (HLS)


I want to implement a simple server (C++) that generates (captures) video stream and sends it to a browser.
The question is: how should the server interact with a browser ?

I am considering a "HTTP Live Streaming (HLS)" technology as the simplest, because I have mastered of network/http interaction is work.

If I understood correctly:

  1. the server should split video and prepare M3U8 file that describes the sequence of chunks - small video files, The following steps is not quite clear for me.
  2. the browser send GET request (?) to receive M3U8, gets a list of the chunks,
  3. the browser play downloaded chunks,
  4. the next step is unclear - in stream (live) video is continuing - how the browser can know - which names of the next chunks ?

I see I have lost logic here …

How to force the browser to get video chunks continuously ?
Maybe I am wrong about something above ? Could you explain me the basic steps ?


Solution

  • I am considering a "HTTP Live Streaming (HLS)" technology as the simplest, because I have mastered of network/http interaction is work.

    It definitely isn't the simplest, in most cases. Handling a simple HTTP request and streaming a continuous stream is far easier. And then, the client side is just a <video> element.

    1. the browser send GET request (?) to receive M3U8, gets a list of the chunks,

    Correct.

    1. the next step is unclear - in stream (live) video is continuing - how the browser can know - which names of the next chunks ?

    It downloads a fresh copy of the M3U8 playlist to figure out what's next.

    How to force the browser to get video chunks continuously ?

    HLS clients know to refresh the playlist. It's also possible to build a client to just "know" what segments will be next. For instance, if you had a sequential list, there's no reason for the client to keep requesting the playlist over and over again.

    Sequential playlists have some gotchas though, around negative caching. If you use a CDN, turn off caching for any non-ok response because someone might hit your segment URLs before they are ready, causing your CDN to assume they will never exist.