0

I'm using the https://github.com/Ruslan-B/FFmpeg.AutoGen library to connect to an rtsp camera stream and save the frames as png images.

Testing with the FFMPEG.Autogen's example project present in the GitHub repository with no changes I've noticed that the memory usage seems to constantly grow in infinitely. I've made sure to dispose of all bitmaps, pointers etc after use but am unable to pinpoint the source of the issue.

It seems to be coming from their VideoStreamDecoder.TryDecodeNextFrame method as shown below:

public bool TryDecodeNextFrame(out AVFrame frame)
{
    ffmpeg.av_frame_unref(_pFrame);
    ffmpeg.av_frame_unref(_receivedFrame);
    int error;
    do
    {
        try
        {
            do
            {
                error = ffmpeg.av_read_frame(_pFormatContext, _pPacket);
                if (error == ffmpeg.AVERROR_EOF)
                {
                    frame = *_pFrame;
                    return false;
                }
                else if(error < 0)
                {
                }
                error.ThrowExceptionIfError();
            } while (_pPacket->stream_index != _streamIndex);

            ffmpeg.avcodec_send_packet(_pCodecContext, _pPacket).ThrowExceptionIfError();
        }
        finally
        {
            ffmpeg.av_packet_unref(_pPacket);
        }

        error = ffmpeg.avcodec_receive_frame(_pCodecContext, _pFrame);
    } while (error == ffmpeg.AVERROR(ffmpeg.EAGAIN));
    error.ThrowExceptionIfError();
    if (_pCodecContext->hw_device_ctx != null)
    {
        ffmpeg.av_hwframe_transfer_data(_receivedFrame, _pFrame, 0).ThrowExceptionIfError();
        frame = *_receivedFrame;
    }
    else
    {
        frame = *_pFrame;
    }
    return true;
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Deadmeat
  • 101
  • 1
  • 2
  • 6
  • 1
    _"memory seems to constantly grow in size infinitely..."_ - so did you actually get a out of memory error? Did you use any form of memory profiler? How long did your program run for? Claiming memory leaks is often a false positve –  Dec 22 '20 at 11:27
  • 1
    Left it for 13.5 hours and noticed a consistent upward trend. The memory usage grew from 15.6 MB to 1372 MB. When tested on a system with less usable Memory (16 GB), reached 100% usage requiring a hard reset To add to the above, I've also tried fetching the images from the same source via the command line with no issues. The memory usage tends to stabilize at around 28-30 MB – Deadmeat Dec 22 '20 at 13:36
  • 1
    If you are running the exact example provided by the repository, you should contact the author and open an issue. I have just visited the repository out of curiosity and recognized that some other user already has opened an issue, reporting the same behavior as observed by you. For (possible) bugs of public repositories SO is not the first place to go in order to seek bug fixes. Always contact the owner/author of the library first. He knows his code better than anybody else and might already have an unreleased fix you can test. – BionicCode Dec 22 '20 at 17:27
  • I’m voting to close this question because the OP has indicated that the problem is with [3rd party open source code](https://github.com/Ruslan-B/FFmpeg.AutoGen/tree/master/FFmpeg.AutoGen.Example), possibly even isolated to the project's _example code_ and not the library itself. –  Dec 23 '20 at 04:03
  • Thanks for the follow-up @Deadmeat. That's interesting to see the command-line tools work fine, it might indicate the problem is with their example code and not with the core library itself. I agree with **BionicCode** that you should contact the author. Especially when it seems the problem is with their example code. Consider comparing the code in the example with the command-line tools. Good luck! –  Dec 23 '20 at 04:06
  • After attempting to contact the author and scrolling through the issues section on Github, it look like this is a known issue. I found an amended version of the code that's been working well so far: https://github.com/Ruslan-B/FFmpeg.AutoGen/issues/112 in case anyone else comes across this issue – Deadmeat Dec 23 '20 at 08:08

0 Answers0