c++ffmpeglibavformat

How do I make libavformat reconnect when connection is reset by peer?


I am opening a network file via HTTPS like this:

avformat_open_input(&m_format, "https://www.example.com/file.mp4", nullptr, nullptr);

It works, however sometimes connection is closed and this is logged:

[tls @ 0x7f600ba690] Error in the pull function.
[tls @ 0x7f600ba690] IO error: Connection reset by peer
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f6007e450] Packet corrupt (stream = 0, dts = 24188928).
[aac @ 0x7f60324800] Input buffer exhausted before END element found

I found it possible to setup reconnection by logs from this post. Here are their logs:

[tls @ 0x6e3b440] Error in the pull function.
[tls @ 0x6e3b440] IO error: Connection reset by peer
[https @ 0x6e37900] Will reconnect at 720896 in 0 second(s), error=Connection reset by peer.

Here libavformat does not give up and it tries to reconnect because -reconnect argument was passed in FFmpeg CLI. How can I do the same in code with FFmpeg's libavformat?


Solution

  • Found it out myself. Turns out that commandline options can be directly passed to the function:

    AVDictionary* options = nullptr;
    av_dict_set(&options, "reconnect", "1", 0);
    av_dict_set(&options, "reconnect_streamed", "1", 0);
    av_dict_set(&options, "reconnect_delay_max", "30", 0);
    avformat_open_input(&m_format, "https://www.example.com/file.mp4", nullptr, &options);