监听FFmpeg/Libav中的端口

Listening to port in FFmpeg/Libav

本文关键字:Libav FFmpeg 监听      更新时间:2023-10-16

在FFmpeg中,有一个参数"-listen"来监听指定的端口:

# 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

我想在c++和Libav中使用这个命令(因为FFMpeg已经移动到Libav中)。

对于侦听端口,我需要使用哪种Libav方法?

我解决了这个问题:

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