R2-D2 R2-D2 - 1 month ago 19
C++ Question

c++ ffmpeg "Starting new cluster" error

I want to encode a live stream into

webm
, but
ffmpeg
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
AVFormatContext
params

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
ffmpeg
hangs

[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
doc/examples/muxing.c
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;
avio_open_dyn_buf(&oc->pb);

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

avio_close_dyn_buf(oc->pb, &output_buf);
av_free(output_buf);


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

Answer

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;