简体   繁体   中英

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM