Skip to content

Commit

Permalink
Fixed segfault in ffmpeg_wirter
Browse files Browse the repository at this point in the history
  • Loading branch information
getroot committed Aug 23, 2024
1 parent 6d20bc8 commit 872ea01
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
58 changes: 26 additions & 32 deletions src/projects/modules/ffmpeg/ffmpeg_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace ffmpeg
std::lock_guard<std::shared_mutex> mlock(_track_map_lock);
// MediaTrackID -> AVStream, MediaTrack
// AVStream doesn't need to be released. It will be released when AVFormatContext is released.
std::shared_ptr<AVStream> av_stream_ptr(av_stream);
std::shared_ptr<AVStream> av_stream_ptr(av_stream, [](AVStream *av_stream) { });
_track_map[media_track->GetId()] = std::make_pair(av_stream_ptr, media_track);
}

Expand Down Expand Up @@ -219,28 +219,7 @@ namespace ffmpeg

bool Writer::Stop()
{
auto av_format = GetAVFormatContext();
if (av_format)
{
auto av_format_ptr = av_format.get();
// Write trailer
if (_need_to_flush)
{
av_write_trailer(av_format_ptr);
}

// Close file
if (_need_to_close)
{
avformat_close_input(&av_format_ptr);
}

// Free context
avformat_free_context(av_format_ptr);

ReleaseAVFormatContext();
}

ReleaseAVFormatContext();
SetState(WriterStateClosed);

return true;
Expand Down Expand Up @@ -410,27 +389,42 @@ namespace ffmpeg
return _last_packet_sent_time;
}

std::shared_ptr<AVFormatContext> Writer::GetAVFormatContext() const
std::shared_ptr<AVFormatContext> Writer::GetAVFormatContext() const
{
std::shared_lock<std::shared_mutex> mlock(_av_format_lock);
return _av_format_ptr;
return _av_format;
}

void Writer::SetAVFormatContext(AVFormatContext *av_format)
{
std::lock_guard<std::shared_mutex> mlock(_av_format_lock);
_av_format_ptr.reset(av_format);
// forward _need_to_flush and _need_to_close to lambda
_av_format.reset(av_format, [&need_to_flush = _need_to_flush, &need_to_close = _need_to_close](AVFormatContext *av_format_ptr) {
if (av_format_ptr == nullptr)
{
return;
}

if (need_to_flush)
{
av_write_trailer(av_format_ptr);
}

if (need_to_close)
{
avformat_close_input(&av_format_ptr);
}

avformat_free_context(av_format_ptr);
});
}

void Writer::ReleaseAVFormatContext()
{
std::lock_guard<std::shared_mutex> mlock(_av_format_lock);
if (_av_format_ptr)
{
avformat_free_context(_av_format_ptr.get());
_av_format_ptr.reset();
;
}
_av_format = nullptr;
_need_to_flush = false;
_need_to_close = false;
}

std::pair<std::shared_ptr<AVStream>, std::shared_ptr<MediaTrack>> Writer::GetTrack(int32_t track_id) const
Expand Down
2 changes: 1 addition & 1 deletion src/projects/modules/ffmpeg/ffmpeg_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace ffmpeg
std::map<int32_t, std::pair<std::shared_ptr<AVStream>, std::shared_ptr<MediaTrack>>> _track_map;
mutable std::shared_mutex _track_map_lock;

std::shared_ptr<AVFormatContext> _av_format_ptr = nullptr;
std::shared_ptr<AVFormatContext> _av_format = nullptr;
mutable std::shared_mutex _av_format_lock;

ov::String _output_format_name;
Expand Down

0 comments on commit 872ea01

Please sign in to comment.