R2-D2 R2-D2 - 1 year ago 112
C++ Question

c++ ffmpeg "Starting new cluster" error

I want to encode a live stream into

, but
gets stuck in a live lock after 5 seconds stating

[webm @ 0x1d81940] Starting new cluster at offset 0 bytes, pts 5040dts 5040

I tried increasing the related

av_opt_set_int(oc->priv_data, "chunk_duration", INT_MAX, 0);
av_opt_set_int(oc->priv_data, "cluster_time_limit", INT_MAX, 0);
av_opt_set_int(oc->priv_data, "cluster_size_limit", INT_MAX, 0);

which avoids the error for about 30 seconds, but then again

[webm @ 0xbc9940] Starting new cluster due to timestamp
[webm @ 0xbc9940] Starting new cluster at offset 0 bytes, pts 32800dts 32800

The error can be reproduced with the official example
just by writing into a buffer instead of a file like this

oc = avformat_alloc_context();
oc->oformat = av_guess_format("webm", NULL, NULL);
oc->oformat->flags |= AVFMT_NOFILE;

and for actual writing

uint8_t *output_buf;

avformat_write_header(oc, &opt);
/* or */
av_interleaved_write_frame(fmt_ctx, pkt);

avio_close_dyn_buf(oc->pb, &output_buf);

How can I encode webm into a buffer?
(And why does it work for files?)

Answer Source

Probably, webm format does some kind of asynchronous write operations that are somehow detached from the av_write_frame calls. However, I was able to solve my issue by implementing a proper AVIOContext.

To set up a custom IO, you need a callback function, which should look something like this:

static int dispatch_output_packet(void* opaque, uint8_t* buffer, int buffer_size)
    YourOutputType *out = (YourOutputType*) opaque;
    int result = out->do_something(buffer, buffer_size);
    return 0;

Then create a AVIOContext and plug it in:

size_t io_buffer_size = 3 * 1024 * 1024;
io_buffer = new unsigned char[io_buffer_size];
AVIOContext* io_ctx = avio_alloc_context(io_buffer, io_buffer_size, AVIO_FLAG_WRITE, &your_output_object, NULL, dispatch_output_packet, NULL);
io_ctx->seekable = 0;
format_context_->oformat->flags |= AVFMT_FLAG_CUSTOM_IO;
format_context_->oformat->flags |= AVFMT_NOFILE;
format_context_->pb = io_ctx;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download