[Libav-user] Using custom asynchronous I/O with avio_alloc_context

Nicolas George nicolas.george at normalesup.org
Sun Mar 10 19:44:33 CET 2013


Le nonidi 19 ventôse, an CCXXI, Nicolas a écrit :
> I am using libav with custom I/O (with avio_alloc_context()) for reading
> various stream.
> 
> In my project, all my I/O are asynchronous but ffmpeg callbacks work in a
> synchronous way. So right now, what I am doing is to use an internal buffer
> 4 times bigger than my avio buffer and each time my read callback is called
> I copy a part of my buffer and get new data asynchronously, this works well
> for reading, but when I seek my media I don't have (yet) data in my
> internal buffer. In such case what should I return in my read callback?
> 
> Also, there is a flag AVIO_FLAG_NONBLOCKING that I could pass to
> avio_alloc_context but i couldn't figure how to use it (what should I
> return in my read callback?)
> 
> is there any way to use custom I/O in an asynchronous fashion?

Unfortunately, no, there is currently no way of working with asynchronous
and non-blocking I/O. Some of the network protocols can be set to work
non-blocking, but not all, and the demuxers themselves can not work in
non-blocking mode.

The usual solution for that is to use threads. You could, for example, run
the demuxer in a separate thread. When the demuxer calls the read function
in your custom I/O context, the thread becomes blocked waiting for data from
the main thread, and when it has demuxed a packet, it can wake the main
thread using some kind of signalfd/self-pipe. That way, the demuxing thread
do not force you to abandon your async model (using a few threads has a
tendency to force you to use threads everywhere).

Also note that since you are using the thread not for concurrency but only
to keep the local context of the demuxer, you do not have to use "real"
threads, you can consider using, for examples, GNU Pth, or libpcl.

Regards,

-- 
  Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20130310/2e264958/attachment.asc>


More information about the Libav-user mailing list