Search code examples
iosobjective-cffmpegrtsp

FFMPEG: closing RTSP stream cleanly -- av_read_frame crash on avformat_close_input


I'm using KxMovie: https://github.com/kolyvan/kxmovie

It appears to stop a stream and close the view controller one should use [pause]; However, I'm trying to receive a stream from a version of gstreamer that has a memory leak if a stream isn't closed properly (it's just left hanging).

So, just [pause]ing isn't an option for me.

I'm trying to use [closeFile] in the KxMovie decoder:

-(void) closeFile
{

[self closeAudioStream];
[self closeVideoStream];
[self closeSubtitleStream];

_videoStreams = nil;
_audioStreams = nil;
_subtitleStreams = nil;

     if (_formatCtx) {
         _formatCtx->interrupt_callback.opaque = NULL;
         _formatCtx->interrupt_callback.callback = NULL;
         avformat_close_input(&_formatCtx);
         _formatCtx = NULL;
     }
}

However, I usually get a EXC_BAD_ACCESS from av_read_frame after [closeFile] issues avformat_close_input.

Can anyone give me some advice on how to cleanly shutdown an RTSP stream using ffmpeg?

Thanks!


Solution

  • Needed to use the interrupt callbacks to interrupt av_read_frame

            _formatCtx->interrupt_callback.opaque
            _formatCtx->interrupt_callback.callback 
    

    Wait for the callback to be called and return non zero. After the callback has returned an interrupt value av_close_input can safely be called (after closing any codecs used).