ffmpeg: drain avcodec contexts on close, fixes leak

Fixes #398
This commit is contained in:
rdb 2018-11-28 16:14:45 +01:00
parent 32df05b528
commit 85cb742f79
2 changed files with 25 additions and 11 deletions

View File

@ -210,6 +210,23 @@ FfmpegAudioCursor::
*/
void FfmpegAudioCursor::
cleanup() {
if (_audio_ctx && _audio_ctx->codec) {
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 100)
// We need to drain the codec to prevent a memory leak.
avcodec_send_packet(_audio_ctx, nullptr);
while (avcodec_receive_frame(_audio_ctx, _frame) == 0) {}
avcodec_flush_buffers(_audio_ctx);
#endif
avcodec_close(_audio_ctx);
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 52, 0)
avcodec_free_context(&_audio_ctx);
#else
delete _audio_ctx;
#endif
}
_audio_ctx = nullptr;
if (_frame) {
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 45, 101)
av_frame_free(&_frame);
@ -237,16 +254,6 @@ cleanup() {
_buffer = nullptr;
}
if ((_audio_ctx)&&(_audio_ctx->codec)) {
avcodec_close(_audio_ctx);
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 52, 0)
avcodec_free_context(&_audio_ctx);
#else
delete _audio_ctx;
#endif
}
_audio_ctx = nullptr;
if (_format_ctx) {
_ffvfile.close();
_format_ctx = nullptr;

View File

@ -595,7 +595,14 @@ close_stream() {
// Hold the global lock while we free avcodec objects.
ReMutexHolder av_holder(_av_lock);
if ((_video_ctx)&&(_video_ctx->codec)) {
if (_video_ctx && _video_ctx->codec) {
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 100)
// We need to drain the codec to prevent a memory leak.
avcodec_send_packet(_video_ctx, nullptr);
while (avcodec_receive_frame(_video_ctx, _frame) == 0) {}
avcodec_flush_buffers(_video_ctx);
#endif
avcodec_close(_video_ctx);
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 52, 0)
avcodec_free_context(&_video_ctx);