码迷,mamicode.com
首页 > 其他好文 > 详细

ffmpeg-hls实现分析

时间:2018-05-31 23:08:48      阅读:2907      评论:0      收藏:0      [点我收藏+]

标签:兴趣   interrupt   variant   根据   +=   直接   call   c语言   str   

[时间:2018-05] [状态:Open]
[关键词:流媒体,stream,HLS, ffmpeg, demuxer,avformat]

0 引言

本文将关注于FFmpeg中的HLS相关实现,相关代码在libavformat/hls.c中(我所使用的Ffmpeg版本是4.0的),分析hls_demuxer的主要实现逻辑。

本文作为我之前的HLS综述的后续文章,也是ffmpeg框架分析的后续文章。前者介绍了HLS协议相关的理论部分,后者介绍了FFmpeg主要框架分析(本文主要关注demuxer);要是你对此感兴趣建议了解下。

1 ff_hls_demuxer的主要对外接口

#define OFFSET(x) offsetof(HLSContext, x)
static const AVOption hls_options[] = {
    {"live_start_index", "segment index to start live streams at (negative values are from the end)",
        OFFSET(live_start_index), AV_OPT_TYPE_INT, {.i64 = -3}, INT_MIN, INT_MAX, FLAGS},
    // ... HLS demuxer支持的参数,有删减
    {NULL}
};

static const AVClass hls_class = {
    .class_name = "hls,applehttp",
    .item_name  = av_default_item_name,
    .option     = hls_options,
    .version    = LIBAVUTIL_VERSION_INT,
};

AVInputFormat ff_hls_demuxer = {
    .name           = "hls,applehttp",
    .long_name      = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
    .priv_class     = &hls_class,
    .priv_data_size = sizeof(HLSContext),
    .flags          = AVFMT_NOGENSEARCH,
    .read_probe     = hls_probe, // 媒体格式探测
    .read_header    = hls_read_header, // 读取协议头并获取节目信息
    .read_packet    = hls_read_packet, // 读取音视频包
    .read_close     = hls_close, // 关闭HLS通信
    .read_seek      = hls_read_seek, // 实现HLS的seek操作
};

通常分析FFmpeg中的demuxer,我们主要关注其read_probe、read_header、read_packet、read_close、read_seek这五个函数指针所对应的实现代码。FFmpeg在实现demuxer时将其协议解析的部分完整封装了demuxer中。
hls_demuxer中主要的结构是HLSContext,定义如下:

typedef struct HLSContext {
    AVClass *class;
    AVFormatContext *ctx;
    int n_variants;
    struct variant **variants;/* master playlist中有多个variants */
    int n_playlists;
    struct playlist **playlists;/* playlists中包含一个segment的列表 */
    int n_renditions;
    struct rendition **renditions;

    int cur_seq_no;
    int live_start_index;
    int first_packet;
    int64_t first_timestamp;
    int64_t cur_timestamp;
    AVIOInterruptCB *interrupt_callback;
    AVDictionary *avio_opts;
    int strict_std_compliance;
    char *allowed_extensions;
    int max_reload;
    int http_persistent;
    int http_multiple;
    AVIOContext *playlist_pb;
} HLSContext;

接下来我们将逐个函数查看。

2 hls_demuxer核心接口实现

2.1 read_probe -- hls_probe

这个函数主要是HLS格式探测,其实现代码如下:

static int hls_probe(AVProbeData *p)
{
    /* HLS协议要求必须以#EXTM3U打头,并至少有下面三个字段之一存在 */
    if (strncmp(p->buf, "#EXTM3U", 7))
        return 0;

    if (strstr(p->buf, "#EXT-X-STREAM-INF:")     ||
        strstr(p->buf, "#EXT-X-TARGETDURATION:") ||
        strstr(p->buf, "#EXT-X-MEDIA-SEQUENCE:"))
        return AVPROBE_SCORE_MAX;
    return 0;
}

这段代码逻辑比较简单,都是关于字符串匹配的处理,也是按照HLS协议规定的要求进行媒体格式探测的。

2.2 read_header -- hls_read_header

hls_read_header将是比较复杂的处理逻辑,因为这里涉及到master playlist解析、节目信息获取,同时初始化hls解析相关的结构。这个函数实现有300行左右。我们将分为三部分:playlist解析、hls相关初始化、提取节目信息。

2.2.1 playlist解析

hls_read_header第一部分代码如下:

static int hls_read_header(AVFormatContext *s)
{
    HLSContext *c = s->priv_data;
    int ret = 0, i;

    c->ctx                = s;
    c->interrupt_callback = &s->interrupt_callback;
    c->strict_std_compliance = s->strict_std_compliance;
    c->first_packet = 1;
    c->first_timestamp = AV_NOPTS_VALUE;
    c->cur_timestamp = AV_NOPTS_VALUE;
    
    /* 解析m3u8 */
    if ((ret = parse_playlist(c, s->url, NULL, s->pb)) < 0)
        goto fail;

    if (c->n_variants == 0) {
        av_log(NULL, AV_LOG_WARNING, "Empty playlist\n");
        ret = AVERROR_EOF;
        goto fail;
    }
    /* 对于master playlist,逐个解析其中的playlist */
    if (c->n_playlists > 1 || c->playlists[0]->n_segments == 0) {
        for (i = 0; i < c->n_playlists; i++) {
            struct playlist *pls = c->playlists[i];
            if ((ret = parse_playlist(c, pls->url, pls, NULL)) < 0)
                goto fail;
        }
    }
    /* 必须有至少一个variant */
    if (c->variants[0]->playlists[0]->n_segments == 0) {
        av_log(NULL, AV_LOG_WARNING, "Empty playlist\n");
        ret = AVERROR_EOF;
        goto fail;
    }
    // ... 部分代码,有删减

从上面代码来看最主要的逻辑就是调用parse_playlist函数来解析m3u8文件。解析部分主要参考HLS协议就可以。其代码如下:

static int parse_playlist(HLSContext *c, const char *url,
                          struct playlist *pls, AVIOContext *in)
{
    int ret = 0, is_segment = 0, is_variant = 0;
    int64_t duration = 0;
    char line[MAX_URL_SIZE];
    const char *ptr;
    int64_t seg_offset = 0;
    int64_t seg_size = -1;
    uint8_t *new_url = NULL;
    struct variant_info variant_info;
    char tmp_str[MAX_URL_SIZE];
    struct segment *cur_init_section = NULL;

    if (!in) { /* 创建用于HTTP请求的AVIO */
        AVDictionary *opts = NULL;
        av_dict_copy(&opts, c->avio_opts, 0);

        ret = c->ctx->io_open(c->ctx, &in, url, AVIO_FLAG_READ, &opts);
        av_dict_free(&opts);
        if (ret < 0)
            return ret;
    }
    /* HTTP-URL重定向 */
    if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0)
        url = new_url;

    ff_get_chomp_line(in, line, sizeof(line));
    if (strcmp(line, "#EXTM3U")) {/* HLS协议标志起始头 */
        ret = AVERROR_INVALIDDATA;
        goto fail;
    }
    /* 释放已经存在的pls及segment */
    if (pls) {
        free_segment_list(pls);
        pls->finished = 0;
        pls->type = PLS_TYPE_UNSPECIFIED;
    }/* 以下是具体协议的解析 */
    while (!avio_feof(in)) {
        ff_get_chomp_line(in, line, sizeof(line));
        if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
            is_variant = 1;
            memset(&variant_info, 0, sizeof(variant_info));
            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
                               &variant_info);
        } else if (av_strstart(line, "#EXT-X-MEDIA:", &ptr)) {
            struct rendition_info info = {{0}};
            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_rendition_args,
                               &info);
            new_rendition(c, &info, url);
        } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
            ret = ensure_playlist(c, &pls, url);
            if (ret < 0)
                goto fail; /* 最大分片时长 */
            pls->target_duration = strtoll(ptr, NULL, 10) * AV_TIME_BASE;
        } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
            ret = ensure_playlist(c, &pls, url);
            if (ret < 0)
                goto fail;
            pls->start_seq_no = atoi(ptr); /* 起始segment number */
        } else if (av_strstart(line, "#EXT-X-PLAYLIST-TYPE:", &ptr)) {
            ret = ensure_playlist(c, &pls, url);
            if (ret < 0)
                goto fail;
            if (!strcmp(ptr, "EVENT"))/* HLS类型:VOD/EVENT */
                pls->type = PLS_TYPE_EVENT;
            else if (!strcmp(ptr, "VOD"))
                pls->type = PLS_TYPE_VOD;
        } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
            if (pls) /* playlist结束标志,表示VOD */
                pls->finished = 1;
        } else if (av_strstart(line, "#EXTINF:", &ptr)) {
            is_segment = 1;
            duration   = atof(ptr) * AV_TIME_BASE;
        } else if (av_strstart(line, "#EXT-X-BYTERANGE:", &ptr)) {
            seg_size = strtoll(ptr, NULL, 10);
            ptr = strchr(ptr, ‘@‘);
            if (ptr)/* 使用字节划分的m3u8 */
                seg_offset = strtoll(ptr+1, NULL, 10);
        } else if (av_strstart(line, "#", NULL)) {
            continue; /* 忽略无法识别的字段 */
        } else if (line[0]) {
            if (is_variant) { /* 针对variant的处理,下一行一般是URL */
                if (!new_variant(c, &variant_info, line, url)) {
                    ret = AVERROR(ENOMEM);
                    goto fail;
                }
                is_variant = 0;
            }
            if (is_segment) { /* 针对segment的处理,需要拼接segment的URL */
                struct segment *seg;
                if (!pls) {
                    if (!new_variant(c, 0, url, NULL)) {
                        ret = AVERROR(ENOMEM);
                        goto fail;
                    }
                    pls = c->playlists[c->n_playlists - 1];
                }
                seg = av_malloc(sizeof(struct segment));
                if (!seg) {
                    ret = AVERROR(ENOMEM);
                    goto fail;
                }
                seg->duration = duration;
                seg->key_type = KEY_NONE;
                seg->key = NULL;

                ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, line);
                seg->url = av_strdup(tmp_str);
                if (!seg->url) {
                    av_free(seg->key);
                    av_free(seg);
                    ret = AVERROR(ENOMEM);
                    goto fail;
                }

                dynarray_add(&pls->segments, &pls->n_segments, seg);
                is_segment = 0;

                seg->size = seg_size;
                if (seg_size >= 0) {
                    seg->url_offset = seg_offset;
                    seg_offset += seg_size;
                    seg_size = -1;
                } else {
                    seg->url_offset = 0;
                    seg_offset = 0;
                }

                seg->init_section = cur_init_section;
            }
        }
    }
    if (pls)
        pls->last_load_time = av_gettime_relative();

fail:
    av_free(new_url);
    ff_format_io_close(c->ctx, &in);
    c->ctx->ctx_flags = c->ctx->ctx_flags & ~(unsigned)AVFMTCTX_UNSEEKABLE;
    if (!c->n_variants || !c->variants[0]->n_playlists ||
        !(c->variants[0]->playlists[0]->finished ||
          c->variants[0]->playlists[0]->type == PLS_TYPE_EVENT))
        c->ctx->ctx_flags |= AVFMTCTX_UNSEEKABLE;
    return ret;
}

这样通过对m3u8的parse之后,我们已经创建了好了playlist、segment、variant以及rendition。接下来是第二部分。

2.2.2 hls相关初始化

主要相关代码如下:

    /* 对于非直播流,根据segment的时长计算总的节目时长 */
    if (c->variants[0]->playlists[0]->finished) {
        int64_t duration = 0;
        for (i = 0; i < c->variants[0]->playlists[0]->n_segments; i++)
            duration += c->variants[0]->playlists[0]->segments[i]->duration;
        s->duration = duration;
    }

    /* 将renditions与variants关联起来 */
    for (i = 0; i < c->n_variants; i++) {
        struct variant *var = c->variants[i];

        if (var->audio_group[0])
            add_renditions_to_variant(c, var, AVMEDIA_TYPE_AUDIO, var->audio_group);
        if (var->video_group[0])
            add_renditions_to_variant(c, var, AVMEDIA_TYPE_VIDEO, var->video_group);
        if (var->subtitles_group[0])
            add_renditions_to_variant(c, var, AVMEDIA_TYPE_SUBTITLE, var->subtitles_group);
    }

    /* 为每个variant创建一个节目 */
    for (i = 0; i < c->n_variants; i++) {
        struct variant *v = c->variants[i];
        AVProgram *program = av_new_program(s, i);
        if (!program)
            goto fail;
        av_dict_set_int(&program->metadata, "variant_bitrate", v->bandwidth, 0);
    }

    /* 选择起始segment的索引 */
    for (i = 0; i < c->n_playlists; i++) {
        struct playlist *pls = c->playlists[i];

        if (pls->n_segments == 0)
            continue;

        pls->cur_seq_no = select_cur_seq_no(c, pls);
        highest_cur_seq_no = FFMAX(highest_cur_seq_no, pls->cur_seq_no);
    }

我们看一下如何选择起始segment的索引,代码如下:

static int select_cur_seq_no(HLSContext *c, struct playlist *pls)
{
    int seq_no;
    /* 直播情况下,定期更新m3u8 */
    if (!pls->finished && !c->first_packet &&
        av_gettime_relative() - pls->last_load_time >= default_reload_interval(pls))
        parse_playlist(c, pls->url, pls, NULL);

    /* 对于非直播的情况,直接通过时长查找对应的segment索引号(seek时比较常用的逻辑) */
    if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) {
        find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no);
        return seq_no;
    }

    if (!pls->finished) {
        if (!c->first_packet && /* 是在播放中选择segment */
            c->cur_seq_no >= pls->start_seq_no &&
            c->cur_seq_no < pls->start_seq_no + pls->n_segments)
            return c->cur_seq_no;

        /* 直播情况下,需要参考live_start_index调整下 */
        if (c->live_start_index < 0)
            return pls->start_seq_no + FFMAX(pls->n_segments + c->live_start_index, 0);
        else
            return pls->start_seq_no + FFMIN(c->live_start_index, pls->n_segments - 1);
    }

    /* 其他情况直接返回起始segment索引号*/
    return pls->start_seq_no;
}

经过上面的处理,我们已经创建好FFmpeg框架上需要的AVProgram等信息了,接下来就需要填充AVProgram,并获得所有的AVStream信息。

2.2.3 提取节目信息

在开始介绍前,我们先看一下playlist结构体的定义:

/* 每个playlist都有自己的demuxer,如果该playlist是在用的,它还会有AVIOContext和AVPacket */
struct playlist {
    char url[MAX_URL_SIZE];
    AVIOContext pb;
    uint8_t* read_buffer;
    AVIOContext *input;
    int input_read_done;
    AVIOContext *input_next;
    int input_next_requested;
    AVFormatContext *parent;// 这个将指向公用的AVFormatContext
    int index;
    AVFormatContext *ctx; // 这个将用于解析当前playlist的所有segment
    AVPacket pkt;
    int has_noheader_flag;

    /* 当前playlist中包含的AVStream信息 */
    AVStream **main_streams;
    int n_main_streams;

    int finished; /* segment读取状态的相关参数 */
    enum PlaylistType type;
    int64_t target_duration;
    int start_seq_no;
    int n_segments; /* 当前playlist中的所有segment数组 */
    struct segment **segments;
    int needed;
    int cur_seq_no;
    int64_t cur_seg_offset;
    int64_t last_load_time;

    /* Media Initialization Section */
    struct segment *cur_init_section;
    uint8_t *init_sec_buf;
    unsigned int init_sec_buf_size;
    unsigned int init_sec_data_len;
    unsigned int init_sec_buf_read_offset;

    char key_url[MAX_URL_SIZE]; /* HLS解密密钥对应的URL */
    uint8_t key[16];

    int64_t seek_timestamp; /* seek相关的参数 */
    int seek_flags;
    int seek_stream_index; /* into subdemuxer stream array */

    /* 和当前playlist相关的Renditions(可选) */
    int n_renditions;
    struct rendition **renditions;

    /* Media Initialization Sections (EXT-X-MAP)(可选) */
    int n_init_sections;
    struct segment **init_sections;
};

hls_read_header后续逻辑与playlist结构体相关比较大,节目信息提取的代码如下:

    /* 对每个playlist打开其demuxer */
    for (i = 0; i < c->n_playlists; i++) {
        struct playlist *pls = c->playlists[i];
        AVInputFormat *in_fmt = NULL;

        if (!(pls->ctx = avformat_alloc_context())) {
            ret = AVERROR(ENOMEM);
            goto fail;
        }

        if (pls->n_segments == 0)
            continue;

        pls->index  = i;
        pls->needed = 1;
        pls->parent = s;

        /* 调整直播流的起播索引号,以保证所有playlist是同步的 */
        if (!pls->finished && pls->cur_seq_no == highest_cur_seq_no - 1 &&
            highest_cur_seq_no < pls->start_seq_no + pls->n_segments) {
            pls->cur_seq_no = highest_cur_seq_no;
        }

        pls->read_buffer = av_malloc(INITIAL_BUFFER_SIZE);
        if (!pls->read_buffer){
            ret = AVERROR(ENOMEM);
            avformat_free_context(pls->ctx);
            pls->ctx = NULL;
            goto fail;
        }/* 这里初始化了AVIOContext,留意read_data函数,这将是后续读包的核心 */
        ffio_init_context(&pls->pb, pls->read_buffer, INITIAL_BUFFER_SIZE, 0, pls,
                          read_data, NULL, NULL);
        pls->pb.seekable = 0;
        ret = av_probe_input_buffer(&pls->pb, &in_fmt, pls->segments[0]->url,
                                    NULL, 0, 0);
        if (ret < 0) {
            av_log(s, AV_LOG_ERROR, "Error when loading first segment ‘%s‘\n", pls->segments[0]->url);
            avformat_free_context(pls->ctx);
            pls->ctx = NULL;
            goto fail;
        }
        pls->ctx->pb       = &pls->pb;
        pls->ctx->io_open  = nested_io_open;
        pls->ctx->flags   |= s->flags & ~AVFMT_FLAG_CUSTOM_IO;

        /* 接下来就是打开流,获取其中AVStream信息了 */
        ret = avformat_open_input(&pls->ctx, pls->segments[0]->url, in_fmt, NULL);
        if (ret < 0)
            goto fail;

        ret = avformat_find_stream_info(pls->ctx, NULL);
        if (ret < 0)
            goto fail;

        pls->has_noheader_flag = !!(pls->ctx->ctx_flags & AVFMTCTX_NOHEADER);

        /* Create new AVStreams for each stream in this playlist */
        ret = update_streams_from_subdemuxer(s, pls);
        if (ret < 0)
            goto fail;

        if (pls->n_main_streams)
            av_dict_copy(&pls->main_streams[0]->metadata, pls->ctx->metadata, 0);

        add_metadata_from_renditions(s, pls, AVMEDIA_TYPE_AUDIO);
        add_metadata_from_renditions(s, pls, AVMEDIA_TYPE_VIDEO);
        add_metadata_from_renditions(s, pls, AVMEDIA_TYPE_SUBTITLE);
    }

    update_noheader_flag(s);

    return 0;
fail:
    hls_close(s);
    return ret;
}

2.3 read_close -- hls_close

鉴于read_close实现相对简单,我们先了解下该函数的实现代码:

static void free_playlist_list(HLSContext *c)
{
    int i; /* 针对每个playlist释放动态分配的内存 */
    for (i = 0; i < c->n_playlists; i++) {
        struct playlist *pls = c->playlists[i];
        free_segment_list(pls);
        free_init_section_list(pls);
        av_freep(&pls->main_streams);
        av_freep(&pls->renditions);
        av_freep(&pls->init_sec_buf);
        av_packet_unref(&pls->pkt);
        av_freep(&pls->pb.buffer);
        if (pls->input)
            ff_format_io_close(c->ctx, &pls->input);
        pls->input_read_done = 0;
        if (pls->input_next)
            ff_format_io_close(c->ctx, &pls->input_next);
        pls->input_next_requested = 0;
        if (pls->ctx) {
            pls->ctx->pb = NULL;
            avformat_close_input(&pls->ctx);
        }
        av_free(pls);
    }
    av_freep(&c->playlists);
    c->n_playlists = 0;
}

static void free_variant_list(HLSContext *c)
{
    int i;
    for (i = 0; i < c->n_variants; i++) {
        struct variant *var = c->variants[i];
        av_freep(&var->playlists);
        av_free(var);
    }
    av_freep(&c->variants);
    c->n_variants = 0;
}

static void free_rendition_list(HLSContext *c)
{
    int i;
    for (i = 0; i < c->n_renditions; i++)
        av_freep(&c->renditions[i]);
    av_freep(&c->renditions);
    c->n_renditions = 0;
}

static int hls_close(AVFormatContext *s)
{
    HLSContext *c = s->priv_data;
    /* 基本逻辑就是依次释放:playlist, variant, rendition */
    free_playlist_list(c); /* 最复杂的释放 */
    free_variant_list(c);
    free_rendition_list(c);

    av_dict_free(&c->avio_opts);
    ff_format_io_close(c->ctx, &c->playlist_pb);

    return 0;
}

2.4 read_packet -- hls_read_packet

该函数主要功能是读取原始的数据,并解析为AVPacket。代码大约有150行,主要逻辑如下:

static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    HLSContext *c = s->priv_data;
    int ret, i, minplaylist = -1;
    /* 这个函数会根据AVStream.discard标志来判断关闭不需要的HTTP下载 */
    recheck_discard_flags(s, c->first_packet);
    c->first_packet = 0;

    for (i = 0; i < c->n_playlists; i++) {
        struct playlist *pls = c->playlists[i];
        /* 从每一个已打开的playlist上读取一个AVPacket */
        if (pls->needed && !pls->pkt.data) {
            while (1) {
                int64_t ts_diff;
                AVRational tb;
                ret = av_read_frame(pls->ctx, &pls->pkt);
                if (ret < 0) {
                    if (!avio_feof(&pls->pb) && ret != AVERROR_EOF)
                        return ret;
                    reset_packet(&pls->pkt);
                    break;
                } else {
                    if (c->first_timestamp == AV_NOPTS_VALUE &&
                        pls->pkt.dts       != AV_NOPTS_VALUE)
                        c->first_timestamp = av_rescale_q(pls->pkt.dts,
                            get_timebase(pls), AV_TIME_BASE_Q);
                }
                /* 以下部分跟seek有关,无seek操作无需后续判断 */
                if (pls->seek_timestamp == AV_NOPTS_VALUE)
                    break;

                if (pls->seek_stream_index < 0 ||
                    pls->seek_stream_index == pls->pkt.stream_index) {

                    if (pls->pkt.dts == AV_NOPTS_VALUE) {
                        pls->seek_timestamp = AV_NOPTS_VALUE;
                        break;
                    }

                    tb = get_timebase(pls);
                    ts_diff = av_rescale_rnd(pls->pkt.dts, AV_TIME_BASE,
                                            tb.den, AV_ROUND_DOWN) -
                            pls->seek_timestamp;
                    if (ts_diff >= 0 && (pls->seek_flags  & AVSEEK_FLAG_ANY ||
                                        pls->pkt.flags & AV_PKT_FLAG_KEY)) {
                        pls->seek_timestamp = AV_NOPTS_VALUE;
                        break;
                    }
                }/* 到此,说面当前AVPacket是需要丢弃的,重新读取 */
                av_packet_unref(&pls->pkt);
                reset_packet(&pls->pkt);
            }
        }
        /* 从所有AVStream中找到dts最小的一个,记录该索引值 */
        if (pls->pkt.data) {
            struct playlist *minpls = minplaylist < 0 ?
                                     NULL : c->playlists[minplaylist];
            if (minplaylist < 0) {
                minplaylist = i;
            } else {
                int64_t dts     =    pls->pkt.dts;
                int64_t mindts  = minpls->pkt.dts;

                if (dts == AV_NOPTS_VALUE ||
                    (mindts != AV_NOPTS_VALUE && compare_ts_with_wrapdetect(dts, pls, mindts, minpls) < 0))
                    minplaylist = i;
            }
        }
    }

    /* 成功读取AVPacket,需要返回给上层调用者 */
    if (minplaylist >= 0) {
        struct playlist *pls = c->playlists[minplaylist];
        AVStream *ist;
        AVStream *st;

        /* 判断stream_index有效性 */
        if (pls->pkt.stream_index >= pls->n_main_streams) {
            av_packet_unref(&pls->pkt);
            reset_packet(&pls->pkt);
            return AVERROR_BUG;
        }

        ist = pls->ctx->streams[pls->pkt.stream_index];
        st = pls->main_streams[pls->pkt.stream_index];

        *pkt = pls->pkt;
        pkt->stream_index = st->index;
        reset_packet(&c->playlists[minplaylist]->pkt);
        /* 更新当前HLS的读取位置 */
        if (pkt->dts != AV_NOPTS_VALUE)
            c->cur_timestamp = av_rescale_q(pkt->dts,
                                            ist->time_base,
                                            AV_TIME_BASE_Q);

        return 0;
    }
    return AVERROR_EOF;
}

从这个代码上来看很奇怪,这里没有包含任何http请求、playlist更新、segment的处理逻辑,那么真正的逻辑隐藏在哪里呢?不知道大家还记得2.2节中hls_read_header函数中对AVIOContext的初始化逻辑,其实最终的解析主要部分就在其中一个不起眼的函数中——read_data。上面的av_read_frame通过FFmpeg提供的一系列机制,最终调用read_data(你要是对此感兴趣,建议研究下雷神的自定义AVIO的示例)。这个函数长度在150行左右,但其内部是一个带有多个标签的循环逻辑(实际上这部分代码是最复杂的,使用了c语言中不推荐使用的goto语句),详细代码如下:

static int read_data(void *opaque, uint8_t *buf, int buf_size)
{
    struct playlist *v = opaque;
    HLSContext *c = v->parent->priv_data;
    int ret;
    int just_opened = 0;
    int reload_count = 0;
    struct segment *seg;

restart: /* 标签:重启新的下载 */
    if (!v->needed)
        return AVERROR_EOF;

    if (!v->input || (c->http_persistent && v->input_read_done)) {
        int64_t reload_interval;

        /* 检查playlist是否需要下载,如果不需要了,直接返回EOF */
        v->needed = playlist_needed(v);
        if (!v->needed) {
            return AVERROR_EOF;
        }

        /* 对于直播流,定期更新playlist */
        reload_interval = default_reload_interval(v);

reload: /* 标签:重新下载playlist */
        reload_count++;
        if (reload_count > c->max_reload)
            return AVERROR_EOF;
        if (!v->finished &&
            av_gettime_relative() - v->last_load_time >= reload_interval) {
            if ((ret = parse_playlist(c, v->url, v, NULL)) < 0)
                return ret;

            /* 按照HLS协议规定,如果还需要请求playlist,请求间隔可以设置为segment时长的一半 */
            reload_interval = v->target_duration / 2;
        }
        if (v->cur_seq_no < v->start_seq_no) {
            /* 这种情况下客户端因为某些原因下载过慢,直接调整cur_seq_no,跳过一些segment */
            v->cur_seq_no = v->start_seq_no;
        }
        if (v->cur_seq_no >= v->start_seq_no + v->n_segments) {
            if (v->finished) /* 点播的情况下,表示读取完成 */
                return AVERROR_EOF;
            while (av_gettime_relative() - v->last_load_time < reload_interval) {
                if (ff_check_interrupt(c->interrupt_callback))
                    return AVERROR_EXIT;
                av_usleep(100*1000);
            }
            /* 上面的循环用于等待给定的时间,然后重新加载playlist */
            goto reload;
        }
        /* 所有的前提都满足,可以开始读segment了 */
        v->input_read_done = 0;
        seg = current_segment(v);

        /* 如果存在,加载并更新Media Initialization Section */
        ret = update_init_section(v, seg);
        if (ret)
            return ret;
        
        if (c->http_multiple == 1 && v->input_next_requested) {
            FFSWAP(AVIOContext *, v->input, v->input_next);
            v->input_next_requested = 0;
            ret = 0;
        } else { /* 这里发起了新的HTTP请求 */
            ret = open_input(c, v, seg, &v->input);
        }
        if (ret < 0) { /* 请求失败的情况下的处理,默认直接跳过当前segment */
            if (ff_check_interrupt(c->interrupt_callback))
                return AVERROR_EXIT;
            v->cur_seq_no += 1;
            goto reload;
        }
        just_opened = 1;
    }

    if (c->http_multiple == -1) {
        uint8_t *http_version_opt = NULL;
        int r = av_opt_get(v->input, "http_version", AV_OPT_SEARCH_CHILDREN, &http_version_opt);
        if (r >= 0) {
            c->http_multiple = strncmp((const char *)http_version_opt, "1.1", 3) == 0;
            av_freep(&http_version_opt);
        }
    }

    seg = next_segment(v); /* 这是使用单HTTP发起请求的处理 */
    if (c->http_multiple == 1 && !v->input_next_requested &&
        seg && seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL)) {
        ret = open_input(c, v, seg, &v->input_next);
        if (ret < 0) {
            if (ff_check_interrupt(c->interrupt_callback))
                return AVERROR_EXIT;
            av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %d of playlist %d\n",
                   v->cur_seq_no + 1,
                   v->index);
        } else {
            v->input_next_requested = 1;
        }
    }

    if (v->init_sec_buf_read_offset < v->init_sec_data_len) {
        /* 在任何实际数据返回之前,首先返回init section(解码器可能会依赖这些数据做初始化) */
        int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
        memcpy(buf, v->init_sec_buf, copy_size);
        v->init_sec_buf_read_offset += copy_size;
        return copy_size;
    }

    seg = current_segment(v);
    /* 这是实际通过HTTP读取buf_size长度的数据,这个数据是交织后的数据,从server上获取的 */
    ret = read_from_url(v, seg, buf, buf_size);
    if (ret > 0)
        return ret;
    
    /* 读取完成了,可以关闭segment所使用的HTTP资源了 */
    if (c->http_persistent &&
        seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL)) {
        v->input_read_done = 1;
    } else {
        ff_format_io_close(v->parent, &v->input);
    }

    /* 更新当前读取的seg_no,同时更新全局的,保证流切换之后可以同步 */
    v->cur_seq_no++;

    c->cur_seq_no = v->cur_seq_no;

    goto restart; /* 回到上面继续循环了 */
}

这是需要强调下,read_data就是完成一个I/O该做的事情,从server上去数据,放到缓冲区中,真正的数据解析是由FFmpeg的通用框架实现的。换句话来说,HLS demuxer仅仅做了HLS协议相关的解析,关于mpeg-ts/mp4的解析实际上有它们各自的demuxer完成的。

2.5 read_seek -- hls_read_seek

seek的基本思路就是按照给定的时间点(timestamp)找到对应的流的读取位置,然后继续读取数据。所以在执行seek之前,需要清理下之前缓存的数据。实际代码如下:

static int hls_read_seek(AVFormatContext *s, int stream_index,
                               int64_t timestamp, int flags)
{
    HLSContext *c = s->priv_data;
    struct playlist *seek_pls = NULL;
    int i, seq_no;
    int j;
    int stream_subdemuxer_index;
    int64_t first_timestamp, seek_timestamp, duration;

    if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE))
        return AVERROR(ENOSYS);

    first_timestamp = c->first_timestamp == AV_NOPTS_VALUE ?
                      0 : c->first_timestamp;

    seek_timestamp = av_rescale_rnd(timestamp, AV_TIME_BASE,
                                    s->streams[stream_index]->time_base.den,
                                    flags & AVSEEK_FLAG_BACKWARD ?
                                    AV_ROUND_DOWN : AV_ROUND_UP);

    duration = s->duration == AV_NOPTS_VALUE ?
               0 : s->duration;
    /* 检查seek位置的有效性 */
    if (0 < duration && duration < seek_timestamp - first_timestamp)
        return AVERROR(EIO);

    /* 找到stream_index对应的playlist */
    for (i = 0; i < c->n_playlists; i++) {
        struct playlist *pls = c->playlists[i];
        for (j = 0; j < pls->n_main_streams; j++) {
            if (pls->main_streams[j] == s->streams[stream_index]) {
                seek_pls = pls;
                stream_subdemuxer_index = j;
                break;
            }
        }
    }
    /* 检查给定的seek timestamp对指定的流是否有效 */
    if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no))
        return AVERROR(EIO);

    /* 所有参数都有效了,这就可以设置目标位置了 */
    seek_pls->cur_seq_no = seq_no;
    seek_pls->seek_stream_index = stream_subdemuxer_index;
    /* 下面是对正在读取的流的处理 */
    for (i = 0; i < c->n_playlists; i++) {
        struct playlist *pls = c->playlists[i];
        if (pls->input)
            ff_format_io_close(pls->parent, &pls->input);
        pls->input_read_done = 0;
        if (pls->input_next)
            ff_format_io_close(pls->parent, &pls->input_next);
        pls->input_next_requested = 0;
        av_packet_unref(&pls->pkt);
        reset_packet(&pls->pkt);
        pls->pb.eof_reached = 0;
        /* 清空所有缓存的数据 */
        pls->pb.buf_end = pls->pb.buf_ptr = pls->pb.buffer;
        /* 重置读取位置,以确保demuxer知道需要seek */
        pls->pb.pos = 0;
        /* 清空subdemuxer缓存的所有AVPacket队列 */
        ff_read_frame_flush(pls->ctx);

        pls->seek_timestamp = seek_timestamp;
        pls->seek_flags = flags;

        if (pls != seek_pls) {
            /* 对于不是seek的playlist,将其读取位置设置到seek目标点的位置 */
            find_timestamp_in_playlist(c, pls, seek_timestamp, &pls->cur_seq_no);
            pls->seek_stream_index = -1; /* 这两个标志将在read_packet中使用 */
            pls->seek_flags |= AVSEEK_FLAG_ANY;
        }
    }
    /* 最后记录seek目标位置 */
    c->cur_timestamp = seek_timestamp;

    return 0;
}

读完seek的代码,基本上是处理了下正在读取的流,并没有处理新收到的数据。对新收到数据的过滤处理明显是在hls_read_packet中完成(通过seek_stream_index、seek_flags、seek_timestamp等参数)。需要说明的是seek操作和read_packet必须位于同一个线程,否则从上面的实现来看,明显存在多线程逻辑上的问题。

3 小结

本文主要参考FFmpeg/libavformat/hls.c,对其代码逻辑做了简单收集及整理。整体来说,本文总结了ffmpeg中hls_demxuer的实现逻辑,希望对读者有所帮助。

HLS协议中还涉及一些比较细节的部分,比如subtitle、rendetion、group、init_section、fragment mp4,对这些感兴趣的建议参考HLS官方标准。

ffmpeg-hls实现分析

标签:兴趣   interrupt   variant   根据   +=   直接   call   c语言   str   

原文地址:https://www.cnblogs.com/tocy/p/HLS-impl-in-ffmpeg-hls_demuxer.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!