File tree Expand file tree Collapse file tree 2 files changed +15
-2
lines changed Expand file tree Collapse file tree 2 files changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -99,8 +99,18 @@ AVSampleFormat findBestOutputSampleFormat(const AVCodec& avCodec) {
99
99
} // namespace
100
100
101
101
AudioEncoder::~AudioEncoder () {
102
- if (avFormatContext_ && avFormatContext_->pb && !avioContextHolder_) {
103
- avio_close (avFormatContext_->pb );
102
+ close_avio ();
103
+ }
104
+
105
+ void AudioEncoder::close_avio () {
106
+ if (avFormatContext_ && avFormatContext_->pb ) {
107
+ avio_flush (avFormatContext_->pb );
108
+
109
+ if (!avioContextHolder_) {
110
+ avio_close (avFormatContext_->pb );
111
+ // avoids closing again in destructor, which would segfault.
112
+ avFormatContext_->pb = nullptr ;
113
+ }
104
114
}
105
115
}
106
116
@@ -308,6 +318,8 @@ void AudioEncoder::encode() {
308
318
status == AVSUCCESS,
309
319
" Error in: av_write_trailer" ,
310
320
getFFMPEGErrorStringFromErrorCode (status));
321
+
322
+ close_avio ();
311
323
}
312
324
313
325
UniqueAVFrame AudioEncoder::maybeConvertAVFrame (const UniqueAVFrame& avFrame) {
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ class AudioEncoder {
33
33
void encodeFrame (AutoAVPacket& autoAVPacket, const UniqueAVFrame& avFrame);
34
34
void maybeFlushSwrBuffers (AutoAVPacket& autoAVPacket);
35
35
void flushBuffers ();
36
+ void close_avio ();
36
37
37
38
UniqueEncodingAVFormatContext avFormatContext_;
38
39
UniqueAVCodecContext avCodecContext_;
You can’t perform that action at this time.
0 commit comments