hls简介
- hls是http live stream的简称,是通过http拉流的协议,由苹果开发。
- hls,client通过web http拉流指定m3u8文件,该文件包含ts列表,ts为实际的媒体文件,而对直播,client需要实时获取动态更新的m3u8文件。
https://zhuanlan.zhihu.com/p/355136397
rfc:
hls的优势:
It allows a receiver to adapt the bit rate of the media to the current
network conditions in order to maintain uninterrupted playback at the
best possible quality. It supports interstitial content boundaries.
It provides a flexible framework for media encryption. It can
efficiently offer multiple renditions of the same content, such as
audio translations. It offers compatibility with large-scale HTTP
caching infrastructure to support delivery to large audiences.
概述,playlist相关:
一个多媒体介绍被具体化,通过一个指定到一个playlist的uri
如:http://dlhls.cdn.zhanqi.tv/zqlive/30647_JIjP2.m3u8
该拉取到的m3u8文件就是一个playlist.
播放列表的编码: A Playlist is either a Media Playlist or a Master Playlist. Both are
UTF-8 text files containing URIs and descriptive tags.
一个示例和解释:
Here is an example of a Media Playlist:
1 | #EXTM3U |
媒体段: media segments即ts
1)媒体段是什么:
A Media Playlist contains a series of Media Segments that make up the
overall presentation. A Media Segment is specified by a URI and
optionally a byte range.
playlist包含了一系列的媒体段,组成了整个presentation. 一个媒体段由一个URI和可选的一个字节范围指定。
2)媒体段时长指定:
The duration of each Media Segment is indicated in the Media Playlist
by its EXTINF tag (Section 4.3.2.1).
3)媒体段的seq num: 每个seg 有一个唯一的seq,第一个为0或playlist指定,其他的都是前一个+1
Each segment in a Media Playlist has a unique integer Media Sequence Number.The Media Sequence Number of the first segment in the Media
Playlist is either 0 or declared in the Playlist (Section 4.3.3.2).
The Media Sequence Number of every other segment is equal to the
Media Sequence Number of the segment that precedes it plus one.
4)媒体段和连续性(dts连续和不连续等)
每个媒体段必须携带编码的延续
与以前媒体的段结尾的比特流
序列号,其中诸如时间戳等系列中的值
连续性计数器必须不间断地继续。唯一的例外
是媒体播放列表中的第一个媒体段和
明确地称为不连续性的媒体段
(第4.3.2.3节)。无标记的媒体不连续性可以触发
播放错误。
Each Media Segment MUST carry the continuation of the encoded
bitstream from the end of the segment with the previous Media
Sequence Number, where values in a series such as timestamps and
Continuity Counters MUST continue uninterrupted. The only exceptions
are the first Media Segment ever to appear in a Media Playlist and
Media Segments that are explicitly signaled as discontinuities
(Section 4.3.2.3). Unmarked media discontinuities can trigger
playback errors.
5) 媒体段和IDR:
任何媒体段包含视频都应该包含足够信息来初始化解码器和解码一个连续的帧序列包含在段的最后一个帧。
网络有效性被优化如果有足够的解码全部帧的信息在段中。 例如,任何包含H.264视频的媒体段应该包含一个IDR帧。 在第一个IDR帧之前的帧可能会被下载但是可能会被丢弃。
Any Media Segment that contains video SHOULD include enough
information to initialize a video decoder and decode a continuous set
of frames that includes the final frame in the Segment; network
efficiency is optimized if there is enough information in the Segment
to decode all frames in the Segment. For example, any Media Segment
containing H.264 video SHOULD contain an Instantaneous Decoding
Refresh (IDR); frames prior to the first IDR will be downloaded but
possibly discarded.
6)支持的Media segment 格式:
All Media Segments MUST be in a format described in this section.
Transport of other media file formats is not defined.
Some media formats require a common sequence of bytes to initialize a
parser before a Media Segment can be parsed. This format-specific
sequence is called the Media Initialization Section. The Media
Initialization Section can be specified by an EXT-X-MAP tag
(Section 4.3.2.5). The Media Initialization Section MUST NOT contain
sample data.
有哪些类型的媒体段打包方式?
3.2. MPEG-2 Transport Streams
3.3. Fragmented MPEG-4
3.4. Packed Audio
3.5. WebVTT
关于playlist:以及它的语法:
本节介绍 HTTP Live 使用的播放列表文件
流媒体。在本节中,“MUST”和“MUST NOT”指定规则
用于合法播放列表文件的语法和结构。播放列表
违反这些规则无效;客户端必须无法解析它们。
请参见第 6.3.2 节。
播放列表文件的格式源自 M3U [M3U]
播放列表文件格式,并从该早期文件继承两个标签
格式:EXTM3U(第 4.3.1.1 节)和 EXTINF(第 4.3.2.1 节)
在标签语法规范中,一个字符串由
标识标签参数;其具体格式在其
标签定义。如果一个参数进一步被 [] 包围,它是
可选的;否则,它是必需的。
每个播放列表文件必须可以通过路径组件识别
其 URI 或 HTTP Content-Type。在第一种情况下,路径必须
以 .m3u8 或 .m3u 结尾。二、HTTP Content-Type
必须是“application/vnd.apple.mpegurl”或“audio/mpegurl”。客户
应该拒绝解析未被识别的播放列表。
1)定义一个playlist:如何写?
2)属性列表:Attribute Lists
3) Playlist Tags 有很多
A basic Tags:These tags are allowed in both Media Playlists and Master Playlists.
EXTM3U
EXT-X-VERSION
B Media Segment Tags: 4.3.2
EXTINF
EXT-X-BYTERANGE
…
C Media Playlist Tags 4.3.3
…
D Master Playlist Tags 4.3.4
E Media or Master Playlist Tags 4.3.5
关于Key FIles:
带有 URI 属性的 EXT-X-KEY 标记标识一个密钥文件(key file)。Key file 包含一个密钥,可以解密媒体段中的
播放列表。
…
客户端/服务器职责
服务器的职责:
1)一般服务器职责
2)直播的playlists
3) Encrypting Media Segments
4) Providing Variant Streams
客户端的职责:
1)一般客户端职责
2)Loading the Media Playlist File
3) Playing the Media Playlist File
4) Reloading the Media Playlist File
5) Determining the Next Segment to Load
6)Decrypting Encrypted Media Segments
协议版本兼容性
协议兼容性由Ext-X-Version标记指定。一种
包含不兼容的标记或属性的播放列表
具有协议版本1必须包含ext-x-version标记。
如果它不支持,客户端不得尝试播放
ext-x-version标记指定的协议版本或意外
可能发生行为。
更多说明见协议
playlist 实例:
1)简单的media playlist
2)直播媒体使用https
3) playlist使用加密的media segments
4) Master Playlist
#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=1280000,AVERAGE-BANDWIDTH=1000000
http://example.com/low.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2560000,AVERAGE-BANDWIDTH=2000000
http://example.com/mid.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=7680000,AVERAGE-BANDWIDTH=6000000
http://example.com/hi.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=65000,CODECS=”mp4a.40.5”
http://example.com/audio-only.m3u8
5) Master Playlist with I-Frames
6) Master Playlist with Alternative Audio
7) Master Playlist with Alternative Video
8) Session Data in a Master Playlist
9) CHARACTERISTICS Attribute Containing Multiple Characteristics
10)EXT-X-DATERANGE Carrying SCTE-35 Tags
IANA 考虑事项
Internet分配的号码权限(IANA)负责维护一系列注册管理机构,这对于确保DNS根域,IP地址和其他Internet协议资源的全球协调至关重要。自1997年以来,该作用是由ICANN进行的,根据美国商务部的国家电信和信息管理局(NTIA)授予的合同。
更多见协议
安全性考虑
因为是http的,所以。。。
虽然可以对media seg加密解密,。加密密钥由 URI 指定。这些钥匙的交付
应该通过 HTTP Over TLS [RFC2818] 等机制来保护
(以前称为 SSL)与安全领域或会话令牌结合使用。
更多见协议
参考
hls支持的媒体类型:
1)支持mp3/aac/mp4/webvtt(字幕)/广告等
2)支持嵌套子m3u8
关于怎么用webvtt:https://aws.amazon.com/cn/blogs/china/encapsulation-of-hls-streaming-media-loaded-with-webvtt-subtitles-based-on-mediaconvert/
hls是由apple开发的,所以更多资料:https://developer.apple.com/streaming/