Reputation: 320
ffmpeg avformat_open_input()
needs a file name of stream to open.
But my program generates its own SDP file with all information needed into a string.
Is there any analogous function I could use with a string instead of a file? As I don't want to create a file for that.
Or I`ll have to create my own parser for it?
Upvotes: 5
Views: 3776
Reputation: 11
I found a more direct way here, media-receiver-v2/main.cpp, thanks to aliakesis. For example you can just use the code below, the sdpClause is the same in Dmitry's answer.
AVInputFormat *file_iformat = av_find_input_format("sdp");
AVDictionary *format_opts = NULL;
std::string url = "data:text/plain;charset=UTF-8," + sdpClause;
int error = avformat_open_input(&ic,
url.c_str(),
file_iformat,
&format_opts);
Upvotes: 1
Reputation: 320
Example from Russian StackOverflow (many thanks for Monah Tuk):
#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;
}
It fully works in VS2013 exept std::copy
.
Upvotes: 7
Reputation: 11184
You can create your own input protocol by setting AVFormatContext->pb before calling avformat_open_input
. The pb should be an AVIOContext that has a custom read
callback that reads from a buffer instead of a file.
Upvotes: 2