不少人都在找FFmpeg中是否有hls(m3u8)解析的源码,其实是有的。就是ffmpeg/libavformat/hlsproto.c,它依赖的文件也在那个目录中。

如果要是单纯想解析HLS的话,建议参考https://github.com/winlinvip/srs-bench,这是一个http、hls、rtmp集合在一起的压测工具,里面的代码更专一,代码量也不大。

下面是ffmpeg/libavformat/hlsproto.c

/*
* Apple HTTP Live Streaming Protocol Handler
* Copyright (c) 2010 Martin Storsjo
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ /**
* @file
* Apple HTTP Live Streaming Protocol Handler
* http://tools.ietf.org/html/draft-pantos-http-live-streaming
*/ #include "libavutil/avstring.h"
#include "libavutil/time.h"
#include "avformat.h"
#include "internal.h"
#include "url.h"
#include "version.h" /*
* An apple http stream consists of a playlist with media segment files,
* played sequentially. There may be several playlists with the same
* video content, in different bandwidth variants, that are played in
* parallel (preferably only one bandwidth variant at a time). In this case,
* the user supplied the url to a main playlist that only lists the variant
* playlists.
*
* If the main playlist doesn't point at any variants, we still create
* one anonymous toplevel variant for this, to maintain the structure.
*/ struct segment {
int duration;
char url[MAX_URL_SIZE];
}; struct variant {
int bandwidth;
char url[MAX_URL_SIZE];
}; typedef struct HLSContext {
char playlisturl[MAX_URL_SIZE];
int target_duration;
int start_seq_no;
int finished;
int n_segments;
struct segment **segments;
int n_variants;
struct variant **variants;
int cur_seq_no;
URLContext *seg_hd;
int64_t last_load_time;
} HLSContext; static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
{
int len = ff_get_line(s, buf, maxlen);
while (len > && av_isspace(buf[len - ]))
buf[--len] = '\0';
return len;
} static void free_segment_list(HLSContext *s)
{
int i;
for (i = ; i < s->n_segments; i++)
av_free(s->segments[i]);
av_freep(&s->segments);
s->n_segments = ;
} static void free_variant_list(HLSContext *s)
{
int i;
for (i = ; i < s->n_variants; i++)
av_free(s->variants[i]);
av_freep(&s->variants);
s->n_variants = ;
} struct variant_info {
char bandwidth[];
}; static void handle_variant_args(struct variant_info *info, const char *key,
int key_len, char **dest, int *dest_len)
{
if (!strncmp(key, "BANDWIDTH=", key_len)) {
*dest = info->bandwidth;
*dest_len = sizeof(info->bandwidth);
}
} static int parse_playlist(URLContext *h, const char *url)
{
HLSContext *s = h->priv_data;
AVIOContext *in;
int ret = , duration = , is_segment = , is_variant = , bandwidth = ;
char line[];
const char *ptr; if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
&h->interrupt_callback, NULL)) < )
return ret; read_chomp_line(in, line, sizeof(line));
if (strcmp(line, "#EXTM3U"))
return AVERROR_INVALIDDATA; free_segment_list(s);
s->finished = ;
while (!url_feof(in)) {
read_chomp_line(in, line, sizeof(line));
if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
struct variant_info info = {{}};
is_variant = ;
ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
&info);
bandwidth = atoi(info.bandwidth);
} else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
s->target_duration = atoi(ptr);
} else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
s->start_seq_no = atoi(ptr);
} else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
s->finished = ;
} else if (av_strstart(line, "#EXTINF:", &ptr)) {
is_segment = ;
duration = atoi(ptr);
} else if (av_strstart(line, "#", NULL)) {
continue;
} else if (line[]) {
if (is_segment) {
struct segment *seg = av_malloc(sizeof(struct segment));
if (!seg) {
ret = AVERROR(ENOMEM);
goto fail;
}
seg->duration = duration;
ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
dynarray_add(&s->segments, &s->n_segments, seg);
is_segment = ;
} else if (is_variant) {
struct variant *var = av_malloc(sizeof(struct variant));
if (!var) {
ret = AVERROR(ENOMEM);
goto fail;
}
var->bandwidth = bandwidth;
ff_make_absolute_url(var->url, sizeof(var->url), url, line);
dynarray_add(&s->variants, &s->n_variants, var);
is_variant = ;
}
}
}
s->last_load_time = av_gettime(); fail:
avio_close(in);
return ret;
} static int hls_close(URLContext *h)
{
HLSContext *s = h->priv_data; free_segment_list(s);
free_variant_list(s);
ffurl_close(s->seg_hd);
return ;
} static int hls_open(URLContext *h, const char *uri, int flags)
{
HLSContext *s = h->priv_data;
int ret, i;
const char *nested_url; if (flags & AVIO_FLAG_WRITE)
return AVERROR(ENOSYS); h->is_streamed = ; if (av_strstart(uri, "hls+", &nested_url)) {
av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
} else if (av_strstart(uri, "hls://", &nested_url)) {
av_log(h, AV_LOG_ERROR,
"No nested protocol specified. Specify e.g. hls+http://%s\n",
nested_url);
ret = AVERROR(EINVAL);
goto fail;
} else {
av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
ret = AVERROR(EINVAL);
goto fail;
}
av_log(h, AV_LOG_WARNING,
"Using the hls protocol is discouraged, please try using the "
"hls demuxer instead. The hls demuxer should be more complete "
"and work as well as the protocol implementation. (If not, "
"please report it.) To use the demuxer, simply use %s as url.\n",
s->playlisturl); if ((ret = parse_playlist(h, s->playlisturl)) < )
goto fail; if (s->n_segments == && s->n_variants > ) {
int max_bandwidth = , maxvar = -;
for (i = ; i < s->n_variants; i++) {
if (s->variants[i]->bandwidth > max_bandwidth || i == ) {
max_bandwidth = s->variants[i]->bandwidth;
maxvar = i;
}
}
av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
sizeof(s->playlisturl));
if ((ret = parse_playlist(h, s->playlisturl)) < )
goto fail;
} if (s->n_segments == ) {
av_log(h, AV_LOG_WARNING, "Empty playlist\n");
ret = AVERROR(EIO);
goto fail;
}
s->cur_seq_no = s->start_seq_no;
if (!s->finished && s->n_segments >= )
s->cur_seq_no = s->start_seq_no + s->n_segments - ; return ; fail:
hls_close(h);
return ret;
} static int hls_read(URLContext *h, uint8_t *buf, int size)
{
HLSContext *s = h->priv_data;
const char *url;
int ret;
int64_t reload_interval; start:
if (s->seg_hd) {
ret = ffurl_read(s->seg_hd, buf, size);
if (ret > )
return ret;
}
if (s->seg_hd) {
ffurl_close(s->seg_hd);
s->seg_hd = NULL;
s->cur_seq_no++;
}
reload_interval = s->n_segments > ?
s->segments[s->n_segments - ]->duration :
s->target_duration;
reload_interval *= ;
retry:
if (!s->finished) {
int64_t now = av_gettime();
if (now - s->last_load_time >= reload_interval) {
if ((ret = parse_playlist(h, s->playlisturl)) < )
return ret;
/* If we need to reload the playlist again below (if
* there's still no more segments), switch to a reload
* interval of half the target duration. */
reload_interval = s->target_duration * 500000LL;
}
}
if (s->cur_seq_no < s->start_seq_no) {
av_log(h, AV_LOG_WARNING,
"skipping %d segments ahead, expired from playlist\n",
s->start_seq_no - s->cur_seq_no);
s->cur_seq_no = s->start_seq_no;
}
if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
if (s->finished)
return AVERROR_EOF;
while (av_gettime() - s->last_load_time < reload_interval) {
if (ff_check_interrupt(&h->interrupt_callback))
return AVERROR_EXIT;
av_usleep(*);
}
goto retry;
}
url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
ret = ffurl_open(&s->seg_hd, url, AVIO_FLAG_READ,
&h->interrupt_callback, NULL);
if (ret < ) {
if (ff_check_interrupt(&h->interrupt_callback))
return AVERROR_EXIT;
av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
s->cur_seq_no++;
goto retry;
}
goto start;
} URLProtocol ff_hls_protocol = {
.name = "hls",
.url_open = hls_open,
.url_read = hls_read,
.url_close = hls_close,
.flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
.priv_data_size = sizeof(HLSContext),
};

自己用的到,先做个记录,到时候整理吧。

FFmpeg中HLS文件解析源码的更多相关文章

  1. RocketMQ中Broker的消息存储源码分析

    Broker和前面分析过的NameServer类似,需要在Pipeline责任链上通过NettyServerHandler来处理消息 [RocketMQ中NameServer的启动源码分析] 实际上就 ...

  2. Flink 源码解析 —— 源码编译运行

    更新一篇知识星球里面的源码分析文章,去年写的,周末自己录了个视频,大家看下效果好吗?如果好的话,后面补录发在知识星球里面的其他源码解析文章. 前言 之前自己本地 clone 了 Flink 的源码,编 ...

  3. Django的settings文件部分源码分析

    Django的settings文件部分源码分析 在编写Django项目的过程中, 其中一个非常强大的功能就是我们可以在settings文件配置许多选项来完成我们预期的功能, 并且这些配置还必须大写, ...

  4. spark读取文件机制 源码剖析

    Spark数据分区调研 Spark以textFile方式读取文件源码 textFile方法位于 spark-core_2.11/org.apache.spark.api.java/JavaSparkC ...

  5. Springboot中mybatis执行逻辑源码分析

    Springboot中mybatis执行逻辑源码分析 在上一篇springboot整合mybatis源码分析已经讲了我们的Mapper接口,userMapper是通过MapperProxy实现的一个动 ...

  6. EventBus源码解析 源码阅读记录

    EventBus源码阅读记录 repo地址: greenrobot/EventBus EventBus的构造 双重加锁的单例. static volatile EventBus defaultInst ...

  7. eclipse查看class文件的源码

    eclipse查看class文件的源码: 1.网上下载jadClipse的jar包和执行文件jad.exe和 net.sf.jadclipse_3.3.0.jar. 2.把上面下载的jar包放在ecp ...

  8. 在Xcode中使用Git进行源码版本控制

    http://www.cocoachina.com/ios/20140524/8536.html 资讯 论坛 代码 工具 招聘 CVP 外快 博客new 登录| 注册   iOS开发 Swift Ap ...

  9. Asp.Net Web Api 2 实现多文件打包并下载文件示例源码_转

    一篇关于Asp.Net Web Api下载文件的文章,之前我也写过类似的文章,请见:<ASP.NET(C#) Web Api通过文件流下载文件到本地实例>本文以这篇文章的基础,提供了Byt ...

随机推荐

  1. Safari 快捷键

    标签和网页导航快捷键 8 个 切换到下一个标签页 – Control+Tab 切换到上一个标签页 – Control+Shift+Tab 向下滚动一屏 – 空格 向上滚动一屏 – Shift+空格 焦 ...

  2. Html5 dataset--自定义属性

    dataset--自定义属性 HTMLElement.dataset data-*属性集 元素上保存数据 <div id="user" data-id="12345 ...

  3. XML 学习介绍 收藏

    XML学习总结(一)——XML介绍 一.XML概念 Extensible Markup Language,翻译过来为可扩展标记语言.Xml技术是w3c组织发布的,目前推荐遵循的是W3C组织于2000发 ...

  4. SQL Server 2012实施与管理实战指南(笔记)——Ch4数据库连接组件

    4.数据库连接组件 访问数据库有多种不同的技术,包括ADO,ODBC,OLEDB,ADO.NET等这些都有一些共性.首先要建立连接(Connection),然后通过命令(Command)对数据库进行访 ...

  5. 安装Mysql 5.7.1

    现在安装MySQL变成了一件非常人性化的事情,因为有了MySQL-installer这个工具,它可以帮助我们全程安装MySQL.     下面我来简单介绍一下如何使用,以供新手学习:     .首先下 ...

  6. .net 操作sftp服务器

    因为项目的需要,整理了一段C#操作sftp的方法. 依赖的第三方类库名称为:SharpSSH 1.1.1.13. 代码如下: 1: using System; 2: using System.Coll ...

  7. mysql 二进制文件增量备份

    1.首先在my.cnf下添加二进制文件路径(windows下文件名称为my.ini) 在[mysqld]下添加 log-bin=mysql-bin 2.centos下默认安装mysql 5.6,数据默 ...

  8. IOS中修改图片的大小:修改分辨率和裁剪

    在IOS开发中,经常有限制图片文件大小的,有的用户图片很大,导致上传时间慢,造成问题. 如:微信分享中,如果图片的大小好像大于50kbytes,就分享失败,而且没有任何提示. 所以,我添加了两个函数: ...

  9. 网络之Ip地址

    0.0.0.0---255.255.255.255 Ip地址分类(D.E)不对外开放 网络类别 最大网络数 IP地址范围(,唯一的,花钱的) 最大主机数 私有IP地址范围 (做内网ip,不可直接访问公 ...

  10. 08章 分组查询、子查询、原生SQL

    一.分组查询 使用group by关键字对数据分组,使用having关键字对分组数据设定约束条件,从而完成对数据分组和统计 1.1 聚合函数:常被用来实现数据统计功能 ① count() 统计记录条数 ...