1.引言
大家好,距离上篇文章已经过去有一段时间了,主要是最近太忙了,一直没有更新。今天总算能抽出一点时间,说说ffmpeg源码级别的优化了,这块应该会连载,请大家持续关注。废话不多说,接下来就进入正题,说说怎么样进行优化。
2.问题引出及解决
使用ffmpeg接口去推流,当网络不稳定时,av_interleaved_write_frame有时候会出现长期无法返回,也就是卡死在里面,造成无法退出或不能进行下一步处理。下面我们就来分析下该接口究竟干了什么,揭开其神秘的面纱。
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
{
int ret, flush = 0;
ret = prepare_input_packet(s, pkt);
if (ret < 0)
goto fail;
if (pkt) {
AVStream *st = s->streams[pkt->stream_index];
ret = do_packet_auto_bsf(s, pkt);
if (ret == 0)
return 0;
else if (ret < 0)
goto fail;
if (s->debug & FF_FDEBUG_TS)
av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
goto fail;
#endif
if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
ret = AVERROR(EINVAL);
goto fail;
}
} else {
av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
flush = 1;
}
for (;; ) {
AVPacket opkt;
int ret = interleave_packet(s, &opkt, pkt, flush);
if (pkt) {
memset(pkt, 0, sizeof(*pkt));
av_init_packet(pkt);
pkt = NULL;
}
if (ret <= 0) //FIXME cleanup needed for ret<0 ?
return ret;
ret = write_packet(s, &opkt);
if (ret >= 0)
s->streams[opkt.stream_index]->nb_frames++;
av_packet_unref(&opkt);
if (ret < 0)
return ret;
if(s->pb && s->pb->error)
return s->pb->error;
}
fail:
av_packet_unref(pkt);
return ret;
}
上面就是发送封装包的源代码,下面来一一分析。
static int prepare_input_packet(AVFormatContext *s, AVPacket *pkt)
{
int ret;
/*数据包是否合格*/
ret = check_packet(s, pkt);
if (ret < 0)
return ret;
#if !FF_API_COMPUTE_PKT_FIELDS2 || !FF_API_LAVF_AVCTX
/* 清除时间戳 */
if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
AVStream *st = s->streams[pkt->stream_index];
/* 当没有重新排序时,也就是pts等于dts,则需要把pts和dts分别进行设置*/
if (!st->internal->reorder) {
if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE)
pkt->pts = pkt->dts;
if (pkt->dts == AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
pkt->dts = pkt->pts;
}
/* 检查是否设置了时间戳 */
if (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE) {
av_log(s, AV_LOG_ERROR,
"Timestamps are unset in a packet for stream %d\n", st->index);
return AVERROR(EINVAL);
}
/*检查dts是否增加,如果dts没有增加,则需要去检查是否在封包时有其它异常*/
if (st->cur_dts != AV_NOPTS_VALUE &&
((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && st->cur_dts >= pkt->dts) ||
st->cur_dts > pkt->dts)) {
av_log(s, AV_LOG_ERROR,
"Application provided invalid, non monotonically increasing "
"dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
st->index, st->cur_dts, pkt->dts);
return AVERROR(EINVAL);
}
/*
如果pts小于dts,则视为异常,因为dts是解码时间戳,pts是显示时间戳
正常情况是,先解码,后显示,所以pts应该是要大于dts,这样才会合理。
*/
if (pkt->pts < pkt->dts) {
av_log(s, AV_LOG_ERROR, "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
pkt->pts, pkt->dts, st->index);
return AVERROR(EINVAL);
}
}
#endif
return 0;
}
static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) {
AVStream *st = s->streams[pkt->stream_index];
int i, ret;
if (!(s->flags & AVFMT_FLAG_AUTO_BSF))
return 1;
/*检查是否设置了码流过滤,以及额外数据提取*/
if (s->oformat->check_bitstream) {
if (!st->internal->bitstream_checked) {
if ((ret = s->oformat->check_bitstream(s, pkt)) < 0)
return ret;
else if (ret == 1)
st->internal->bitstream_checked = 1;
}
}
#if FF_API_LAVF_MERGE_SD
FF_DISABLE_DEPRECATION_WARNINGS
if (st->internal->nb_bsfcs) {
/*分离出头部数据*/
ret = av_packet_split_side_data(pkt);
if (ret < 0)
av_log(s, AV_LOG_WARNING, "Failed to split side data before bitstream filter\n");
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif
for (i = 0; i < st->internal->nb_bsfcs; i++) {
AVBSFContext *ctx = st->internal->bsfcs[i];
/*当任何过滤器需要在流结束时刷新,则我们需要在刷新整个BSF数据链表*/
if ((ret = av_bsf_send_packet(ctx, pkt)) < 0) {
av_log(ctx, AV_LOG_ERROR,
"Failed to send packet to filter %s for stream %d\n",
ctx->filter->name, pkt->stream_index);
return ret;
}
/*当有多个码流过滤器,需要循环去生成,并调用*/
if ((ret = av_bsf_receive_packet(ctx, pkt)) < 0) {
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return 0;
av_log(ctx, AV_LOG_ERROR,
"Failed to send packet to filter %s for stream %d\n",
ctx->filter->name, pkt->stream_index);
return ret;
}
}
return 1;
}
下面就是开始循环发包的过程,详细分析如下:
/*
功能:间隔发送数据包,实际上是缓存了。这也是av_interleaved_write_frame与av_write_frame的区别之处
AVPacket *out:表述输出数据包
AVPacket *in:表示输入数据包
flush:如果为1表示输入没有更多数据包,应该输出剩余数据包
返回值:1表示输出数据包。0:表示没有数据包
<0,表示发生错误
*/
static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
{
if (s->oformat->interleave_packet) {
int ret = s->oformat->interleave_packet(s, out, in, flush);
if (in)
av_packet_unref(in);
return ret;
} else
/*把数据包缓存到队列中,那什么时候发送?也就是flush什么时候为1呢?*/
return ff_interleave_packet_per_dts(s, out, in, flush);
}
int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
AVPacket *pkt, int flush)
{
AVPacketList *pktl;
int stream_count = 0;
int noninterleaved_count = 0;
int i, ret;
int eof = flush;
if (pkt) {
/*比较数据包的dts添加数据包到想要队列中*/
if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
return ret;
}
for (i = 0; i < s->nb_streams; i++) {
if (s->streams[i]->last_in_packet_buffer) {
/*记录数据包个数*/
++stream_count;
} else if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
s->streams[i]->codecpar->codec_id != AV_CODEC_ID_VP8 &&
s->streams[i]->codecpar->codec_id != AV_CODEC_ID_VP9) {
++noninterleaved_count;
}
}
/*表示缓冲区已满,需要输出*/
if (s->internal->nb_interleaved_streams == stream_count)
flush = 1;
if (s->max_interleave_delta > 0 &&
s->internal->packet_buffer &&
!flush &&
s->internal->nb_interleaved_streams == stream_count+noninterleaved_count
) {
AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
int64_t delta_dts = INT64_MIN;
int64_t top_dts = av_rescale_q(top_pkt->dts,
s->streams[top_pkt->stream_index]->time_base,
AV_TIME_BASE_Q);
for (i = 0; i < s->nb_streams; i++) {
int64_t last_dts;
const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
if (!last)
continue;
last_dts = av_rescale_q(last->pkt.dts,
s->streams[i]->time_base,
AV_TIME_BASE_Q);
delta_dts = FFMAX(delta_dts, last_dts - top_dts);
}
/*缓冲区中最老与最新数据包的dts间隔,大于了最大间隔,则需要输出数据*/
if (delta_dts > s->max_interleave_delta) {
av_log(s, AV_LOG_DEBUG,
"Delay between the first packet and last packet in the "
"muxing queue is %"PRId64" > %"PRId64": forcing output\n",
delta_dts, s->max_interleave_delta);
flush = 1;
}
}
if (s->internal->packet_buffer &&
eof &&
(s->flags & AVFMT_FLAG_SHORTEST) &&
s->internal->shortest_end == AV_NOPTS_VALUE) {
AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
s->internal->shortest_end = av_rescale_q(top_pkt->dts,
s->streams[top_pkt->stream_index]->time_base,
AV_TIME_BASE_Q);
}
if (s->internal->shortest_end != AV_NOPTS_VALUE) {
while (s->internal->packet_buffer) {
AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
AVStream *st;
int64_t top_dts = av_rescale_q(top_pkt->dts,
s->streams[top_pkt->stream_index]->time_base,
AV_TIME_BASE_Q);
if (s->internal->shortest_end + 1 >= top_dts)
break;
pktl = s->internal->packet_buffer;
st = s->streams[pktl->pkt.stream_index];
s->internal->packet_buffer = pktl->next;
if (!s->internal->packet_buffer)
s->internal->packet_buffer_end = NULL;
if (st->last_in_packet_buffer == pktl)
st->last_in_packet_buffer = NULL;
av_packet_unref(&pktl->pkt);
av_freep(&pktl);
flush = 0;
}
}
if (stream_count && flush) {
AVStream *st;
pktl = s->internal->packet_buffer;
*out = pktl->pkt;
st = s->streams[out->stream_index];
s->internal->packet_buffer = pktl->next;
if (!s->internal->packet_buffer)
s->internal->packet_buffer_end = NULL;
if (st->last_in_packet_buffer == pktl)
st->last_in_packet_buffer = NULL;
av_freep(&pktl);
return 1;
} else {
av_init_packet(out);
return 0;
}
}
前面做了数据包的合格性检查,pts与dts的检查,缓冲到队列中,并在合适的时机,去发送数据包。接下来就该主角上场了。
/**
功能:发送数据包
AVFormatContext *s:封装上下文
AVPacket *pkt:需要发送的数据包
*/
static int write_packet(AVFormatContext *s, AVPacket *pkt)
{
int ret, did_split;
int64_t pts_backup, dts_backup;
pts_backup = pkt->pts;
dts_backup = pkt->dts;
/*调整pts与dts时间偏移,这个偏移值是可以灵活调整*/
if (s->output_ts_offset) {
AVStream *st = s->streams[pkt->stream_index];
int64_t offset = av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
if (pkt->dts != AV_NOPTS_VALUE)
pkt->dts += offset;
if (pkt->pts != AV_NOPTS_VALUE)
pkt->pts += offset;
}
/*根据时间戳的不同情况,调整时间戳,并进行时间基的转化*/
if (s->avoid_negative_ts > 0) {
AVStream *st = s->streams[pkt->stream_index];
int64_t offset = st->mux_ts_offset;
int64_t ts = s->internal->avoid_negative_ts_use_pts ? pkt->pts : pkt->dts;
if (s->internal->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
(ts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
s->internal->offset = -ts;
s->internal->offset_timebase = st->time_base;
}
if (s->internal->offset != AV_NOPTS_VALUE && !offset) {
offset = st->mux_ts_offset =
av_rescale_q_rnd(s->internal->offset,
s-