diff --git a/source/encoder.cpp b/source/encoder.cpp index 0b40fba..29e2720 100644 --- a/source/encoder.cpp +++ b/source/encoder.cpp @@ -603,24 +603,26 @@ obsffmpeg::encoder::encoder(obs_data_t* settings, obs_encoder_t* encoder) obsffmpeg::encoder::~encoder() { + if (this->context) { + // Flush encoders that require it. + if ((this->codec->capabilities & AV_CODEC_CAP_DELAY) != 0) { + avcodec_send_frame(this->context, nullptr); + while (avcodec_receive_packet(this->context, &this->current_packet) >= 0) { + avcodec_send_frame(this->context, nullptr); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } + + // Close and free context. + avcodec_close(this->context); + avcodec_free_context(&this->context); + } + av_packet_unref(&this->current_packet); this->frame_queue.clear(); this->frame_queue_used.clear(); this->swscale.finalize(); - - if (this->context) { - // Flush encoders that require it. - if ((this->codec->capabilities & AV_CODEC_CAP_DELAY) != 0) { - avcodec_send_frame(this->context, nullptr); - while (avcodec_receive_packet(this->context, &this->current_packet) != AVERROR(EOF)) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - } - - avcodec_close(this->context); - avcodec_free_context(&this->context); - } } void obsffmpeg::encoder::get_properties(obs_properties_t* props)