ijkplayer和ffplay在播放ts流时起播慢的原因分析
1.前言
在优化ijkplayer起播时间的过程中,发现设置probesize和analyzeduration可以减少起播时间,在播放4k ts流时,发现需要将probesize和analyzeduration设置的很大才能找到音视频信息(通过av_dump_format)并播放,并且发现设置的很大的时候播放1080P的时候就变得很慢,发是在avformat_find_stream_info函数中很耗时,probesize和analyzeduration这两个值是用于探测数据里面的音视频流信息,一个做了大小限制一个做了时间限制,只要其中一个超出,则退出avformat_find_stream_info中的一个循环,我就在想,按照ffmpeg的逻辑,找到了音视频信息就会退出,那么probesize和analyzeduration这两个值应该和起播时间没有关系才对,为什么这两个值设置过大会导致起播时间延长呢?
2.排查过程
2.1 avformat_find_stream_info函数
先看一下avformat_find_stream_info函数中最主要的一个循环(精简了一下):
for (;;) {
int analyzed_all_streams;
/* check if one codec still needs to be handled */
for (i = 0; i < ic->nb_streams; i++) {
int fps_analyze_framecount = 20;
st = ic->streams[i];
char *errmsg = NULL;
if (!has_codec_parameters(st, errmsg)) {
av_log(ic, AV_LOG_INFO, "stream_index:%d %s\n", errmsg);
break;
}
analyzed_all_streams = 0;
if (i == ic->nb_streams) {
analyzed_all_streams = 1;
/* NOTE: If the format has no header, then we need to read some
* packets to get most of the streams, so we cannot stop here. */
if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
/* If we found the info for all the codecs, we can stop. */
ret = count;
av_log(ic, AV_LOG_DEBUG, "All info found\n");
flush_codecs = 0;
break;
}
}
/* We did not get all the codec info, but we read too much data. */
if (read_size >= probesize) {
ret = count;
av_log(ic, AV_LOG_DEBUG,
"Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
for (i = 0; i < ic->nb_streams; i++)
if (!ic->streams[i]->r_frame_rate.num &&
ic->streams[i]->info->duration_count <= 1 &&
ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
strcmp(ic->iformat->name, "image2"))
av_log(ic, AV_LOG_WARNING,
"Stream #%d: not enough frames to estimate rate; "
"consider increasing probesize\n", i);
break;
}
/* NOTE: A new stream can be added there if no header in file
* (AVFMTCTX_NOHEADER). */
ret = read_frame_internal(ic, &pkt1);
if (ret == AVERROR(EAGAIN))
continue;
if (ret < 0) {
/* EOF or error*/
eof_reached = 1;
break;
}
pkt = &pkt1;
st = ic->streams[pkt->stream_index];
if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
read_size += pkt->size;
}
if (st->codec_info_nb_frames>1) {
int64_t t = 0;
int64_t limit;
if (st->time_base.den > 0)
t = av_rescale_q(st->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
if (st->avg_frame_rate.num > 0)
t = FFMAX(t, av_rescale_q(st->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
if ( t == 0
&& st->codec_info_nb_frames>30
&& st->info->fps_first_dts != AV_NOPTS_VALUE
&& st->info->fps_last_dts != AV_NOPTS_VALUE)
t = FFMAX(t, av_rescale_q(st->info->fps_last_dts - st->info->fps_first_dts, st->time_base, AV_TIME_BASE_Q));
if (analyzed_all_streams) limit = max_analyze_duration;
else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
else limit = max_stream_analyze_duration;
if (t >= limit) {
av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
limit,
t, pkt->stream_index);
if (ic->flags & AVFMT_FLAG_NOBUFFER)
av_packet_unref(pkt);
break;
}
if (pkt->duration) {
if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && pkt->pts >= st->start_time) {
st->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, st->info->codec_info_duration + pkt->duration);
} else
st->info->codec_info_duration += pkt->duration;
st->info->codec_info_duration_fields += st->parser && st->need_parsing && avctx->ticks_per_frame ==2 ? st->parser->repeat_pict + 1 : 2;
}
}
/* If still no information, we try to open the codec and to
* decompress the frame. We try to avoid that in most cases as
* it takes longer and uses more memory. For MPEG-4, we need to
* decompress for QuickTime.
*
* If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
* least one frame of codec data, this makes sure the codec initializes
* the channel configuration and does not only trust the values from
* the container. */
try_decode_frame(ic, st, pkt,
(options && i < orig_nb_streams) ? &options[i] : NULL);
if (ic->flags & AVFMT_FLAG_NOBUFFER)
av_packet_unref(pkt);
st->codec_info_nb_frames++;
count++;
}
要跳出最外层的for循环,三个条件满足一个即可:
① i == ic->nb_streams 并且 !(ic->ctx_flags & AVFMTCTX_NOHEADER)
② read_size >= probesize
③ t >= limit
2和3就是上面说的一个大小和一个时间限制,然后查看之前的起播日志,发现都是在2或者3条件满足下跳出循环的,也就是说每次都是大小超出或者时间超出才退出循环的。就感觉很奇怪,为什么每次都要超时才能退出循环呢?查看条件1下有条注释和日志输出:
analyzed_all_streams = 1;
/* NOTE: If the format has no header, then we need to read some
* packets to get most of the streams, so we cannot stop here. */
if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
/* If we found the info for all the codecs, we can stop. */
ret = count;
av_log(ic, AV_LOG_DEBUG, "All info found\n");
flush_codecs = 0;
break;
}
看这个意思就是如果流是没有header的,就会继续读更多的包,然后加日志发现,i == ic->nb_streams这个条件在解码几帧视频后满足,但是ic->ctx_flags这个值一直都为1,导致这里永远不会走到break,看下i == ic->nb_streams这个条件是如何满足的:
for (i = 0; i < ic->nb_streams; i++) {
int fps_analyze_framecount = 20;
st = ic->streams[i];
char *errmsg = NULL;
if (!has_codec_parameters(st, errmsg)) {
av_log(ic, AV_LOG_INFO, "stream_index:%d %s\n", errmsg);
break;
}
上面的日志是我加的,原生ffmpeg没有这个日志,这里主要判断每一路流,有没有相应的解码信息,这个函数定义如下:
static int has_codec_parameters(AVStream *st, const char **errmsg_ptr)
{
AVCodecContext *avctx = st->internal->avctx;
#define FAIL(errmsg) do { \
if (errmsg_ptr) \
*errmsg_ptr = errmsg; \
return 0; \
} while (0)
if ( avctx->codec_id == AV_CODEC_ID_NONE
&& avctx->codec_type != AVMEDIA_TYPE_DATA)
FAIL("unknown codec");
switch (avctx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
if (!avctx->frame_size && determinable_frame_size(avctx))
FAIL("unspecified frame size");
if (st->info->found_decoder >= 0 &&
avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
FAIL("unspecified sample format");
if (!avctx->sample_rate)
FAIL("unspecified sample rate");
if (!avctx->channels)
FAIL("unspecified number of channels");
if (st->info->found_decoder >= 0 && !st->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
FAIL("no decodable DTS frames");
break;
case AVMEDIA_TYPE_VIDEO:
if (!avctx->width)
FAIL("unspecified size");
if (st->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
FAIL("unspecified pixel format");
if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)
if (!st->sample_aspect_ratio.num && !st->codecpar->sample_aspect_ratio.num && !st->codec_info_nb_frames)
FAIL("no frame in rv30/40 and no sar");
break;
case AVMEDIA_TYPE_SUBTITLE:
if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
FAIL("unspecified size");
break;
case AVMEDIA_TYPE_DATA:
if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
}
return 1;
}
可以看到该函数就是检查音视频的一些参数,视频检查宽高,像素格式,音频检查帧大小,采样率采样格式等,只要有一项不满足,就返回0,就跳出该for循环,i就不会等于ic->nb_streams,就会继续解码音视频帧直到获取到上面这些参数,i就等于ic->nb_streams了,这里的含义清楚了,只要音视频的以上参数有了就会跳出循环,但是ic->ctx_flags一直为1,所以需要找到为什么ic->ctx_flags一直为1即可。
2.1 mpegts.c文件分析
在该文件中搜索ic->ctx_flags,发现有两个地方对其赋值,一个是mpegts_read_header中将其赋值为1:
if (s->iformat == &ff_mpegts_demuxer) {
/* normal demux */
/* first do a scan to get all the services */
seek_back(s, pb, pos);
mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
handle_packets(ts, probesize / ts->raw_packet_size);
/* if could not find service, enable auto_guess */
ts->auto_guess = 1;
av_log(ts->stream, AV_LOG_TRACE, "tuning done\n");
s->ctx_flags |= AVFMTCTX_NOHEADER;
}
该函数会在avformat_open_input中被调用(在找到相应的AVInpuFormat后):
if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->iformat->read_header)
if ((ret = s->iformat->read_header(s)) < 0)
goto fail;
还有一个是在handle_packet函数中对其赋值:
if (ts->stream->ctx_flags & AVFMTCTX_NOHEADER && ts->scan_all_pmts <= 0) {
int i;
for (i = 0; i < ts->nb_prg; i++) {
if (!ts->prg[i].pmt_found)
break;
}
if (i == ts->nb_prg && ts->nb_prg > 0) {
int types = 0;
for (i = 0; i < ts->stream->nb_streams; i++) {
AVStream *st = ts->stream->streams[i];
if (st->codecpar->codec_type >= 0)
types |= 1<<st->codecpar->codec_type;
}
if ((types & (1<<AVMEDIA_TYPE_AUDIO) && types & (1<<AVMEDIA_TYPE_VIDEO)) || pos > 100000) {
av_log(ts->stream, AV_LOG_DEBUG, "All programs have pmt, headers found\n");
ts->stream->ctx_flags &= ~AVFMTCTX_NOHEADER;
}
}
}
于是添加打印发现ts->scan_all_pmts为1,永远不会走进来,最终发现在ijkplayer中的read_thread函数中有一段代码(查看原生ffplay中也有该语句):
if (!av_dict_get(ffp->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
av_dict_set(&ffp->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
scan_all_pmts_set = 1;
}
这里将scan_all_pmts设置为了1,最终在avformat_open_input中设置到了mpegts里面MpegTSContext中的scan_all_pmts 变量中:
/* Allocate private data. */
if (s->iformat->priv_data_size > 0) {
if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
ret = AVERROR(ENOMEM);
goto fail;
}
if (s->iformat->priv_class) {
*(const AVClass **) s->priv_data = s->iformat->priv_class;
av_opt_set_defaults(s->priv_data);
if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
goto fail;
}
}
上面的av_opt_set_dict会将tmp字典中的"scan_all_pmts"对应字段的值设置到MpegTSContext中去,这是ffmpeg的AVClass结构和功能,很nice。
上面说到了scan_all_pmts这个值为1,导致ic->ctx_flags一直为1,一直没有跳出avformat_find_stream_info函数,导致起播变慢,将其改为0,起播时间果然变快了,但是为什么ijkplayer和ffplay要将其设置为1呢,分析了下mpegts.c,发现如果scan_all_pmts为1,会分析并合并ts流中所有的pmt,但是在直播中,ts会一直传输,并且其中隔段时间就会有pat和pmt,所以一直不会分析完,导致起播变慢。
如果scan_all_pmts为0的话,上面就会检查pat中的pmt是否都收到,并且音视频格式都有的话,就会赋值ic->ctx_flags为0,最终就会退出avformat_find_stream_info中的循环了。
经过以上分析,猜测一下代码作者加入scan_all_pmts变量的想法应该跟以前的dvb那种ts流有关,那个时候ts流中有很多节目表(pmt),这个时候设置scan_all_pmts为1,就可以找到这个流中的所有节目信息,而现在网络播放中,ts流一般就一个节目,所以需要设置scan_all_pmts为0,找到一个pat后,解析其中的pmt,后面收到该pmt后就退出了。
结论:
将scan_all_pmts设置为0后,在mpegts.c里面的pat_cb回调中,会检查pat中pmt表中的数量,并记录每个pmt对应的pid,在pmt_cb回调中,会设置对应Program中的pmt_found字段,在handle_packet中会检查如果pmt的pmt_found都置1了,并且stream中音视频已经找到,就会将ic->ctx_flags置为1,从而影响到avformat_find_stream_info中的循环。