ffmpegmp4libavformatfmp4

Fragmented mp4 broadcast with avformat


I want to broadcast a live stream (h264 -> fragmented mp4) with avformatfunctions.

Now basically I have a working example with this pseudo:

avformat_write_header
while (!end) {
  av_read_frame
  av_write_frame
}
avformat_write_tailer

I have this as output flags: movflags +frag_keyframe+empty_moov+default_base_moof.

I'm dispatching the output to a html5 video which plays fine and smooth. However I want to broadcast it, so whenever a new player comes into picture I want to stream the very same data to it:

avformat_write_header
while (!end) {
  av_read_frame
  av_write_frame (to multiple clients)
}
avformat_write_tailer

However the first client can play the stream but the rest of them can not.

This is because the fmp4's structure: the first client receives the correct ftyp and moov atoms, whereas the rest of the clients are not.

Now I can hack my code to provide the earlier ftype and moov atoms to the later clients but I think that is not wise - correct me if I'm wrong.

My quesions:

  1. How can I ask avformat during the stream to (calculate) and include the ftyp and moov atoms?
  2. Will including ftyp and moov during the stream ruin the first client? (Is it valid in fmp4 to have ftype and moov not just once in the beginning?)

Thank you


Solution

  • The ftyp and moov together is called the initialization fragment And the client should download and process it first, no matter what segment it intends to play next. It's not a hack, it's exactly how fmp4 is intended to work.