Skip to content

Commit

Permalink
Fix unref frames after decoding/encoding external output
Browse files Browse the repository at this point in the history
* Fix no bother rescale ts if packet will not be written.
* Set context it will be variable fps.
  • Loading branch information
zevarito committed May 31, 2018
1 parent 8f476c0 commit f6494e1
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions erizo/src/erizo/media/ExternalOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ void ExternalOutput::writeAudioData(char* buf, int len) {
if (av_packet.size > 0 && need_audio_transcode_) {
if (audio_decoder_.decode(audio_decoder_.frame_, &av_packet)) {
EncodeCB encode_callback = [this](AVPacket *pkt, AVFrame *frame, bool should_write){
av_frame_unref(frame);
this->writePacket(pkt, audio_st_, should_write);
};
if (need_audio_resample_) {
Expand All @@ -202,6 +203,7 @@ void ExternalOutput::writeAudioData(char* buf, int len) {
i += audio_encoder_.resampled_frame_->nb_samples;
audio_encoder_.encode(audio_encoder_.resampled_frame_, &av_packet, encode_callback);
}
av_frame_unref(audio_decoder_.frame_);
} else {
audio_encoder_.encode(audio_decoder_.frame_, &av_packet, encode_callback);
}
Expand Down Expand Up @@ -300,6 +302,7 @@ void ExternalOutput::maybeWriteVideoPacket(char* buf, int len) {
if (av_packet.size > 0 && need_video_transcode_) {
if (video_decoder_.decode(video_decoder_.frame_, &av_packet)) {
auto done_callback = [this](AVPacket *pkt, AVFrame *frame, bool should_write){
av_frame_unref(frame);
this->writePacket(pkt, video_st_, should_write);
};
video_encoder_.encode(video_decoder_.frame_, &av_packet, done_callback);
Expand All @@ -313,22 +316,23 @@ void ExternalOutput::maybeWriteVideoPacket(char* buf, int len) {
}

void ExternalOutput::writePacket(AVPacket *pkt, AVStream *st, bool should_write) {
const char *media_type;
if (st->id == 0) {
media_type = "video";
if (should_write) {
const char *media_type;

if (st->id == 0) {
media_type = "video";
av_packet_rescale_ts(pkt, AVRational{1, static_cast<int>(video_map_.clock_rate)},
video_st_->time_base);
} else {
media_type = "audio";
av_packet_rescale_ts(pkt, audio_encoder_.codec_context_->time_base, audio_st_->time_base);
}
pkt->stream_index = st->id;
video_st_->time_base);
} else {
media_type = "audio";
av_packet_rescale_ts(pkt, audio_encoder_.codec_context_->time_base, audio_st_->time_base);
}
pkt->stream_index = st->id;

int64_t pts = pkt->pts;
int64_t dts = pkt->dts;
int64_t dl = pkt->dts;
int64_t pts = pkt->pts;
int64_t dts = pkt->dts;
int64_t dl = pkt->dts;

if (should_write) {
int ret = av_interleaved_write_frame(context_, pkt);
if (ret != 0) {
ELOG_ERROR("av_interleaved_write_frame pts: %d failed with: %d %s",
Expand Down Expand Up @@ -614,6 +618,7 @@ AVStream * ExternalOutput::addOutputStream(int index, CoderCodec *coder_codec) {

bool ExternalOutput::writeContextHeader() {
AVDictionary *opt = NULL;
context_->oformat->flags |= AVFMT_VARIABLE_FPS;
int ret = avformat_write_header(context_, &opt);
av_dict_free(&opt);
if (ret < 0) {
Expand Down Expand Up @@ -664,6 +669,10 @@ void ExternalOutput::setupVideoEncodingParams(AVCodecContext *context, AVDiction
context->time_base = (AVRational){1, 25};
context->pix_fmt = AV_PIX_FMT_YUV420P;

if (context->codec->id == AV_CODEC_ID_H264) {
av_opt_set(context->priv_data, "profile", "main", 0);
}

if (context->codec->id == AV_CODEC_ID_VP8 || context->codec->id == AV_CODEC_ID_VP9) {
ELOG_DEBUG("Setting VPX params");
}
Expand Down

0 comments on commit f6494e1

Please sign in to comment.