Search code examples
c++ffmpeglibav

Listening to port in FFmpeg/Libav


In FFmpeg, there is a parameter "-listen" in order to listen to a specified port:

# Server side (receiving):
ffmpeg -listen 1 -enter code herei http://server:port -c copy somefile.ogg

https://www.ffmpeg.org/ffmpeg-protocols.html#toc-http

I would like to use this command in C++ with Libav (as FFMpeg has been moved to Libav).

For listening to a port, which Libav method do I need to use?


Solution

  • I solved the problem by:

    void listen(const unsigned int port) {
    
    const int TIMEOUT = 600000;
    
    // check if webservice is already listening
    if (!m_listening) {
    
        m_listening = true;
    
        // Format specification: tcp://hostname:port[?options]
        // See: https://www.ffmpeg.org/ffmpeg-protocols.html#tcp
    
        std::stringstream ss;
        ss << "tcp://localhost:" << port << "?listen=1" << "?listen_timeout=" << TIMEOUT << "?timeout=" << TIMEOUT * 1000;
        const std::string publishingPointURI = ss.str();
        avformat_network_init();
        if (avformat_open_input(&m_stream, publishingPointURI.c_str(), NULL, NULL) != 0) {
                throw Exception(
                        "Unable to buffer stream received from " + publishingPointURI + "");
        }
    
        m_listening = false;
    }