[Libav-user] timeout on av_read_frame()

luke lclemens at gmail.com
Thu Jul 7 19:46:07 CEST 2011

There is one catch... the avio_set_interrupt_cb() function doesn't have any
parameters. So if you're using a C++ class you won't be able to have
multiple instances of it because they'll all point to the same global
callback. When the callback is invoked by an av_read_frame(), there will be
no way for the callback to know which instance of the class invoked it or
which timer to use! You don't need C++ for you to run into this - you could
have several threads reading different files and you'd have the same

Ideally, avio_set_interrupt_cb() would have a mechanism to let you pass in a
void pointer or some other identifier that you could use later to figure out
who's invoking the callback. So I had to look at other options.

Option 2) There is a format context flag for AVFMT_FLAG_NONBLOCK, but at
least with the http mjpeg parser that i'm using, it doesn't seem to be
implemented. So unless the format you have supports that flag, it's not very

Option 3) Hard Terminate the thread when a read takes too long. When a
thread gets hung in av_read_frame(), there isn't much you can do to get it
out of that state. I tried closing the file from another thread, but even
that doesn't release it. You can periodically check the amount of time stuck
in av_read_frame() using another thread and call a function such as
TerminateThread() in windows or pthread_cancel() in *nix. The downside to
this is that it's not good practice to hard terminate threads - ideally they
should be exited gracefully. It could cause unpredictable results if other
threads interact with the thread that gets hard terminated.

I'm  using option 3 at the moment. I first try to terminate the normal way
by giving the thread 3 seconds to exit, but if that doesn't work, I assume
that it's stuck in av_read_frame() and execute a hard terminate. It's ugly,
but I don't know of a workaround.

Does anyone have ideas on how to use avio_set_interrupt_cb() with multiple
instances of a class and/or multiple threads? It works fine with only one
instance, but having a restriction like that sorta defeats the whole purpose
of object orientated code.


View this message in context: http://libav-users.943685.n4.nabble.com/timeout-on-av-read-frame-tp3306682p3652197.html
Sent from the libav-users mailing list archive at Nabble.com.

More information about the Libav-user mailing list