C++ ffmpeg avformat_open_带字符串输入的模拟输入

C++ ffmpeg avformat_open_带字符串输入的模拟输入,c++,ffmpeg,C++,Ffmpeg,ffmpegavformat\u open\u input()需要流的文件名才能打开 但是我的程序会生成自己的SDP文件,并将所有需要的信息转换成字符串 有没有类似的函数可以用字符串代替文件?因为我不想为此创建一个文件 或者我必须为它创建自己的解析器?在调用avformat\u open\u input之前,您可以通过设置来创建自己的输入协议。pb应该是一个自定义的读取回调,从缓冲区而不是文件中读取。来自俄罗斯StackOverflow的示例(非常感谢Monah Tuk): #包括 #包括 #包

ffmpeg
avformat\u open\u input()
需要流的文件名才能打开

但是我的程序会生成自己的SDP文件,并将所有需要的信息转换成字符串

有没有类似的函数可以用字符串代替文件?因为我不想为此创建一个文件


或者我必须为它创建自己的解析器?

在调用
avformat\u open\u input
之前,您可以通过设置来创建自己的输入协议。pb应该是一个自定义的
读取
回调,从缓冲区而不是文件中读取。

来自俄罗斯StackOverflow的示例(非常感谢Monah Tuk):

#包括
#包括
#包括
外部“C”{包括}
使用名称空间std;
静态常量字符*SDP_数据=R“(
v=0
o=-1376063087593 1英寸IP4 127.0.0.1
=-
t=0
m=音频50008 RTP/AVP 0
c=在IP4 192.168.2.196中
a=rtcp:50009在IP4 192.168.2.196中
a=rtpmap:0 PCMU/8000
a=sendrecv
m=视频50010 RTP/AVP 120
c=在IP4 192.168.2.196中
a=rtcp:50011在IP4 192.168.2.196中
a=rtpmap:120 VP8/90000
a=sendrecv
a=rtcp fb:*nack
a=rtcp fb:*ccm fir
)";
结构SdpOpaque
{
使用Vector=std::Vector;
矢量数据;
向量::迭代器位置;
};
int sdp_读取(无效*不透明,uint8_t*buf,int大小)
{
断言(不透明);
断言(buf);
auto octx=静态投影(不透明);
如果(octx->pos==octx->data.end()){
返回0;
}
auto dist=static_cast(std::distance(octx->pos,octx->data.end());
自动计数=标准::最小值(大小、距离);
std::复制(octx->pos,octx->pos+计数,基本单位);
octx->pos+=计数;
返回计数;
}
int sdp_打开(AVFormatContext**pctx、常量字符*数据、AVDictionary**选项)
{
断言(pctx);
*pctx=avformat_alloc_context();
断言(*pctx);
const size_t avioBufferSize=4096;
自动avioBuffer=静态(av_malloc(avioBufferSize));
自动不透明=新的SdpOpaque();
不透明->数据=SdpOpaque::向量(数据,数据+strlen(数据));
不透明->位置=不透明->数据.begin();
自动pbctx=avio_alloc_上下文(avioBuffer、avioBufferSize、0、不透明、sdp_读取、nullptr、nullptr);
断言(pbctx);
(*pctx)->pb=pbctx;
自动输入=自动查找输入格式(“sdp”);
返回avformat\u open\u输入(pctx,“memory.sdp”,infmt,选项);
}
作废sdp_关闭(AVFormatContext**fctx)
{
断言(fctx);
自动ctx=*fctx;
//不透明可以是非POD类型,在之前释放它并分配给null
自动不透明=静态投影(ctx->pb->不透明);
删除不透明;
ctx->pb->不透明=空PTR;
avio_关闭(ctx->pb);
AVU格式\关闭\输入(fctx);
}
int main()
{
av_寄存器_all();
avformat_network_init();
AVFormatContext*sdpctx=nullptr;
sdp_打开(&sdpctx、sdp_数据、空PTR);
av_转储_格式(sdpctx,0,“memory.sdp”,0);
//将设置从SDP上下文复制到目标上下文:
/*
对于(大小i=0;inb\u流;++i){
AVStream*st=avformat\U new\U stream(其他CTX、nullptr);
st->id=i;
avcodec_copy_上下文(st->codec,sdpctx->streams[i]->codec);
st->时基=sdpctx->streams[i]->时基;
}
*/
sdp_关闭(&sdpctx);
返回0;
}
它在VS2013 exept
std::copy
中完全工作

#include <iostream>
#include <vector>
#include <cassert>

extern "C" {#include <libavformat/avformat.h>}

using namespace std;

static const char* SDP_DATA = R"(
v=0
o=- 1376063087593 1 IN IP4 127.0.0.1
s=-
t=0 0
m=audio 50008 RTP/AVP 0
c=IN IP4 192.168.2.196
a=rtcp:50009 IN IP4 192.168.2.196
a=rtpmap:0 PCMU/8000
a=sendrecv
m=video 50010 RTP/AVP 120
c=IN IP4 192.168.2.196
a=rtcp:50011 IN IP4 192.168.2.196
a=rtpmap:120 VP8/90000
a=sendrecv
a=rtcp-fb:* nack
a=rtcp-fb:* ccm fir
)";

struct SdpOpaque
{
    using Vector = std::vector<uint8_t>;
    Vector           data;
    Vector::iterator pos;
};

int sdp_read(void *opaque, uint8_t *buf, int size)
{
    assert(opaque);
    assert(buf);
    auto octx = static_cast<SdpOpaque*>(opaque);

    if (octx->pos == octx->data.end()) {
        return 0;
    }

    auto dist = static_cast<int>(std::distance(octx->pos, octx->data.end()));
    auto count = std::min(size, dist);

    std::copy(octx->pos, octx->pos + count, buf);
    octx->pos += count;

    return count;
}

int sdp_open(AVFormatContext **pctx, const char *data, AVDictionary **options)
{
    assert(pctx);
    *pctx = avformat_alloc_context();
    assert(*pctx);

    const size_t avioBufferSize = 4096;
    auto avioBuffer = static_cast<uint8_t*>(av_malloc(avioBufferSize));
    auto opaque = new SdpOpaque();

    opaque->data = SdpOpaque::Vector(data, data + strlen(data));
    opaque->pos  = opaque->data.begin();

    auto pbctx = avio_alloc_context(avioBuffer, avioBufferSize, 0, opaque, sdp_read, nullptr, nullptr);
    assert(pbctx);

    (*pctx)->pb = pbctx;

    auto infmt = av_find_input_format("sdp");

    return avformat_open_input(pctx, "memory.sdp", infmt, options);
}

void sdp_close(AVFormatContext **fctx)
{
    assert(fctx);
    auto ctx = *fctx;

    // Opaque can be non-POD type, free it before and assign to null
    auto opaque = static_cast<SdpOpaque*>(ctx->pb->opaque);
    delete opaque;
    ctx->pb->opaque = nullptr;

    avio_close(ctx->pb);
    avformat_close_input(fctx);
}

int main()
{
    av_register_all();
    avformat_network_init();

    AVFormatContext *sdpctx = nullptr;
    sdp_open(&sdpctx, SDP_DATA, nullptr);

    av_dump_format(sdpctx, 0, "memory.sdp", 0);
    // Copy settings to target context from SDP context:
    /*
    for (size_t i = 0; i < sdpctx->nb_streams; ++i) {
        AVStream *st = avformat_new_stream(otherctx, nullptr);
        st->id = i;
        avcodec_copy_context(st->codec, sdpctx->streams[i]->codec);
        st->time_base = sdpctx->streams[i]->time_base;
    }
    */

    sdp_close(&sdpctx);
    return 0;
}