detecting a timeout in ffmpeg

11,262

Solution 1

Found in the ffmpeg documentation:

During blocking operations, callback is called with opaque as parameter. If the callback returns 1, the blocking operation will be aborted.

Here is declaration int_cb variable of type AVIOInterruptCB struct from your code:

static const libffmpeg::AVIOInterruptCB int_cb = { interrupt_cb, NULL };

You declared opaque parameter as NULL.

I'd recommend to rewrite initialization code like this:

AVFormatContext* formatContext = libffmpeg::avformat_alloc_context( );
formatContext->interrupt_callback.callback = interrupt_cb;
formatContext->interrupt_callback.opaque = formatContext;

you will be able to access formatContext instance inside interrupt_cb:

static int interrupt_cb(void *ctx) 
{ 
    AVFormatContext* formatContext = reinterpret_cast<AVFormatContext*>(ctx);
// do something 
    return 0;
}

Solution 2

you can pass not only AVFormatContext* formatContext, but any other usefull pointer to some instance, which contains usefull data to determine, which thread timed out

Share:
11,262

Related videos on Youtube

Sean
Author by

Sean

Updated on September 16, 2022

Comments

  • Sean
    Sean over 1 year

    I am writing some software that uses ffmpeg extensively and it is multi threaded, with multiple class instances.

    If the network connection drops out ffmpeg hangs on reading. I found a method to assign a callback that ffmpeg fires periodically to check if it should abort or not:

    static int interrupt_cb(void *ctx) 
    { 
    
    // do something 
        return 0;
    } 
    
    static const libffmpeg::AVIOInterruptCB int_cb = { interrupt_cb, NULL }; 
    

    ...

    AVFormatContext* formatContext = libffmpeg::avformat_alloc_context( );
    formatContext->interrupt_callback = int_cb; 
    if ( libffmpeg::avformat_open_input( &formatContext, fileName, NULL, NULL ) !=0 ) {...}
    

    This is all fine but nowhere on the web can i find what *ctx contains and how to determine whether the callback should return 1 or 0. I can't assign a static "abort" flag as the class has many instances. I also can't debug the code as for some reason visual studio refuses to set a breakpoint on the return 0; line, claiming no executable code is associated with the location. Any ideas?

  • Sean
    Sean almost 12 years
    OK brilliant, thanks! With access to the formatContext how do I then detect that the stream has timed out?