标签:spec 自己的 adobe stop replace exti overflow frame aac
来自: Delivery HLS
SRS支持HLS/RTMP两种成熟而且广泛应用的流媒体分发方式。
RTMP指Adobe的RTMP(Realtime Message Protocol),广泛应用于低延时直播,也是编码器和服务器对接的实际标准协议,在PC(Flash)上有最佳观看体验和最佳稳定性。
HLS指Apple的HLS(Http Live Streaming),本身就是Live(直播)的,不过Vod(点播)也能支持。HLS是Apple平台的标准流媒体协议,和RTMP在PC上一样支持得天衣无缝。
HLS和RTMP两种分发方式,就可以支持所有的终端。RTMP参考 RTMP分发
RTMP和HLS的比较参考: RTMP PK HLS
部署分发HLS的实例,参考: Usage: HLS
HLS是提供一个m3u8地址,Apple的Safari浏览器直接就能打开m3u8地址,譬如:
http://demo.srs.com/live/livestream.m3u8
Android不能直接打开,需要使用html5的video标签,然后在浏览器中打开这个页面即可,譬如:
<!-- livestream.html -->
<video width="640" height="360"
autoplay controls autobuffer
src="http://demo.srs.com/live/livestream.m3u8"
type="application/vnd.apple.mpegurl">
</video>
HLS的m3u8,是一个ts的列表,也就是告诉浏览器可以播放这些ts文件,譬如:
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:64
#EXT-X-TARGETDURATION:12
#EXTINF:11.550
livestream-64.ts
#EXTINF:5.250
livestream-65.ts
#EXTINF:7.700
livestream-66.ts
#EXTINF:6.850
livestream-67.ts
有几个关键的参数,这些参数在SRS的配置文件中都有配置项:
譬如,每个ts切片为10秒,窗口为60秒,那么m3u8中会保存6个ts切片。
注意:SRS 只需要在 Vhost 上配置 HLS,会自动根据流的 app 创建目录,但是配置的 hls_path 必须自己创建。
conf/full.conf中的with-hls.vhost.com是HLS配置的实例,可以拷贝到默认的Vhost,例如:
vhost __defaultVhost__ {
hls {
# whether the hls is enabled.
# if off, donot write hls(ts and m3u8) when publish.
# default: off
enabled on;
# the hls fragment in seconds, the duration of a piece of ts.
# default: 10
hls_fragment 10;
# the hls m3u8 target duration ratio,
# EXT-X-TARGETDURATION = hls_td_ratio * hls_fragment // init
# EXT-X-TARGETDURATION = max(ts_duration, EXT-X-TARGETDURATION) // for each
# @see https://github.com/ossrs/srs/issues/304#issuecomment-74000081
# default: 1.5
hls_td_ratio 1.5;
# the audio overflow ratio.
# for pure audio, the duration to reap the segment.
# for example, the hls_fragment is 10s, hls_aof_ratio is 2.0,
# the segment will reap to 20s for pure audio.
# default: 2.0
hls_aof_ratio 2.0;
# the hls window in seconds, the number of ts in m3u8.
# defualt: 60
hls_window 60;
# the error strategy. canbe:
# ignore, disable the hls.
# disconnect, require encoder republish.
# continue, ignore failed try to continue output hls.
# @see https://github.com/ossrs/srs/issues/264
# default: continue
hls_on_error continue;
# the hls output path.
# the m3u8 file is configed by hls_path/hls_m3u8_file, the defualt is:
# ./objs/nginx/html/[app]/[stream].m3u8
# the ts file is cofiged by hls_path/hls_ts_file, the defualt is:
# ./objs/nginx/html/[app]/[stream]-[seq].ts
# @remark, the hls_path is compatible with srs v1 config.
# default: ./objs/nginx/html
hls_path ./objs/nginx/html;
# the hls m3u8 file name.
# we supports some variables to generate the filename.
# [vhost], the vhost of stream.
# [app], the app of stream.
# [stream], the stream name of stream.
# default: [app]/[stream].m3u8
hls_m3u8_file [app]/[stream].m3u8;
# the hls ts file name.
# we supports some variables to generate the filename.
# [vhost], the vhost of stream.
# [app], the app of stream.
# [stream], the stream name of stream.
# [2006], replace this const to current year.
# [01], replace this const to current month.
# [02], replace this const to current date.
# [15], replace this const to current hour.
# [04], repleace this const to current minute.
# [05], repleace this const to current second.
# [999], repleace this const to current millisecond.
# [timestamp],replace this const to current UNIX timestamp in ms.
# [seq], the sequence number of ts.
# @see https://github.com/ossrs/srs/wiki/v2_CN_DVR#custom-path
# @see https://github.com/ossrs/srs/wiki/v2_CN_DeliveryHLS#hls-config
# default: [app]/[stream]-[seq].ts
hls_ts_file [app]/[stream]-[seq].ts;
# whether use floor for the hls_ts_file path generation.
# if on, use floor(timestamp/hls_fragment) as the variable [timestamp],
# and use enahanced algorithm to calc deviation for segment.
# @remark, when floor on, recommend the hls_segment >= 2*gop.
# defualt: off
hls_ts_floor off;
# the hls entry prefix, which is base url of ts url.
# if specified, the ts path in m3u8 will be like:
# http://your-server/live/livestream-0.ts
# http://your-server/live/livestream-1.ts
# ...
# optional, defualt to empty string.
hls_entry_prefix http://your-server;
# the default audio codec of hls.
# when codec changed, write the PAT/PMT table, but maybe ok util next ts.
# so user can set the default codec for mp3.
# the available audio codec:
# aac, mp3, an
# default: aac
hls_acodec aac;
# the default video codec of hls.
# when codec changed, write the PAT/PMT table, but maybe ok util next ts.
# so user can set the default codec for pure audio(without video) to vn.
# the available video codec:
# h264, vn
# default: h264
hls_vcodec h264;
# whether cleanup the old expired ts files.
# default: on
hls_cleanup on;
# the timeout in seconds to dispose the hls,
# dispose is to remove all hls files, m3u8 and ts files.
# when publisher timeout dispose hls.
# @remark 0 to disable dispose for publisher.
# @remark apply for publisher timeout only, while "etc/init.d/srs stop"
# always dispose hls.
# default: 0
hls_dispose 0;
# the max size to notify hls,
# to read max bytes from ts of specified cdn network,
# @remark only used when on_hls_notify is config.
# default: 64
hls_nb_notify 64;
# whether wait keyframe to reap segment,
# if off, reap segment when duration exceed the fragment,
# if on, reap segment when duration exceed and got keyframe.
# default: on
hls_wait_keyframe on;
# on_hls, never config in here, should config in http_hooks.
# for the hls http callback, @see http_hooks.on_hls of vhost
# hooks.callback.srs.com
# @read https://github.com/ossrs/srs/wiki/v2_CN_DeliveryHLS#http-callback
# @read https://github.com/ossrs/srs/wiki/v2_CN_DeliveryHLS#http-callback
# on_hls_notify, never config in here, should config in http_hooks.
# we support the variables to generate the notify url:
# [app], replace with the app.
# [stream], replace with the stream.
# [ts_url], replace with the ts url.
# for the hls http callback, @see http_hooks.on_hls_notify of
# vhost hooks.callback.srs.com
# @read https://github.com/ossrs/srs/wiki/v2_CN_DeliveryHLS#on-hls-notify
# @read https://github.com/ossrs/srs/wiki/v2_CN_DeliveryHLS#on-hls-notify
}
}
其中 hls 配置就是 HLS 的配置,主要配置项如下:
hls_fragment:秒,指定 ts 切片的最小长度。实际上 ts 文件的长度由以下公式决定:
ts 文件时长 = max(hls_fragment, gop_size)
hls_fragment:配置文件中的长度。譬如:5秒。
gop_size:编码器配置的 gop 的长度,譬如 ffmpeg 指定fps为20帧/秒,gop为200帧,则gop_size=gop/fps=10秒。
那么,最终ts的时长为max(5, 10) = 10秒。这也是为什么有些流配置了hls_fragment,
但是ts时长仍然比这个大的原因。
hls_window:秒,指定 HLS 窗口大小,即 m3u8 中 ts 文件的时长之和,超过总时长后,丢弃第一个 m3u8 中的第一个切片,直到 ts 的总时长在这个配置项范围之内。即 SRS 保证下面的公式:
hls_window >= sum(m3u8中每个ts的时长)
hls_ts_file:HLS的ts文件名,包含可替换的一系列变量,参考 dvr variables,另外,[seq] 是 ts 的 sequence number。
对于RTMP流:rtmp://localhost/live/livestream
HLS配置路径:
hls_path /data/nginx/html;
hls_m3u8_file [app]/[stream].m3u8;
hls_ts_file [app]/[stream]-[seq].ts;
那么会生成以下文件:
/data/nginx/html/live/livestream.m3u8
/data/nginx/html/live/livestream-0.ts
/data/nginx/html/live/livestream-1.ts
/data/nginx/html/live/livestream-2.ts
最后的HLS地址为:http://localhost/live/livestream.m3u8
hls_entry_prefix:TS 的 base url。可选默认为空字符串;非空时加在 ts 前面作为 base url。
对于ts切片:live/livestream-0.ts
若配置为:hls_entry_prefix http://your-server;
则最后的TS的URL是:http://your-server/live/livestream-0.ts
on_hls_notify:当切片生成时,回调这个 url,使用 GET 回调。用来和系统集成,可以使用 [ts_url] 变量,实现预分发(即下载一次 ts 片)。
部署分发HLS的实例,参考: Usage: HLS
可以配置 on_hls 实现回调,应该在 http_hooks 中配置,而不是在 hls 中配置。
备注:HLS 热备可以基于这个回调实现,参考 #351。
备注:HLS 热备必须保证两个服务器的切片完全一样,因为负载均衡器或者边缘可能从两个服务器取切片,必须完全一样。因此在切片上保证两个服务器切片完全一致,是一个非常非常复杂的流媒体问题;但是通过业务系统和回调,通过选择两个服务器的切片的方式,可以做到非常简单可靠的 HLS 热备系统。
可以配置 on_hls_notify 实现 CDN 预分发,应该在 http_hooks 中配置,而不是在 hls 中配置。
SRS 支持分发 HLS 纯音频,当 RTMP 流没有视频,且音频为 aac(可以使用转码转为 aac,参考 Usage: Transcode2HLS),SRS 只切片音频。
若 RTMP 流中已经有视频和音频,需要支持纯音频 HLS 流,可以用转码将视频去掉,参考: 转码: 禁用流。然后分发音频流。
分发纯音频流不需要特殊配置,和 HLS 分发一样,参考: Usage: HLS
Forward 的流和普通流不做区分,若 forward 的流所在的 VHOST 配置了 HLS,一样会应用 HLS 配置进行切片。
因此,可以对原始流进行 Transcode 之后,保证流符合 h.264/aac 的规范,然后 forward 到多个配置了 HLS 的 vhost 进行切片。支持多个源站的热备。
HLS 要求 RTMP 流的编码为 h.264+aac/mp3,否则会自动禁用 HLS,会出现 RTMP 流能看 HLS 流不能看(或者看到的 HLS 是之前的流)。
Transcode 将 RTMP 流转码后,可以让 SRS 接入任何编码的 RTMP 流,然后转换成 HLS 要求的 h.264/aac/mp3 编码方式。
配置 Transcode 时,若需要控制 ts 长度,需要配置 配置ffmpeg编码的gop,譬如:
vhost hls.transcode.vhost.com {
transcode {
enabled on;
ffmpeg ./objs/ffmpeg/bin/ffmpeg;
engine hls {
enabled on;
vfilter {
}
vcodec libx264;
vbitrate 500;
vfps 20;
vwidth 768;
vheight 320;
vthreads 2;
vprofile baseline;
vpreset superfast;
vparams {
g 100;
}
acodec libaacplus;
abitrate 45;
asample_rate 44100;
achannels 2;
aparams {
}
output rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_[engine];
}
}
}
该 FFMPEG 转码参数,指定 gop 时长为 100/20 = 5秒,fps 帧率(vfps=20),gop 帧数(g=100)。
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:13
#EXT-X-MEDIA-SEQUENCE:430
#EXTINF:11.800
news-430.ts
#EXTINF:10.120
news-431.ts
#EXTINF:11.160
news-432.ts
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:13
#EXT-X-MEDIA-SEQUENCE:430
#EXT-X-PLAYLIST-TYPE:EVENT
#EXTINF:11.800
news-430.ts
#EXTINF:10.120
news-431.ts
#EXTINF:11.160
news-432.ts
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-ALLOW-CACHE:YES
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-TARGETDURATION:12
#EXTINF:10.120,
livestream-184.ts
#EXTINF:10.029,
livestream-185.ts
#EXTINF:10.206,
livestream-186.ts
#EXTINF:10.160,
livestream-187.ts
#EXTINF:11.360,
livestream-188.ts
#EXTINF:9.782,
livestream-189.ts
#EXT-X-ENDLIST
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:13
#EXT-X-MEDIA-SEQUENCE:430
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:11.800
news-430.ts
#EXTINF:10.120
news-431.ts
#EXT-X-DISCONTINUITY
#EXTINF:11.952
news-430.ts
#EXTINF:12.640
news-431.ts
#EXTINF:11.160
news-432.ts
#EXT-X-DISCONTINUITY
#EXTINF:11.751
news-430.ts
#EXTINF:2.040
news-431.ts
#EXT-X-ENDLIST
https://developer.apple.com/library/ios/technotes/tn2288/_index.html
标签:spec 自己的 adobe stop replace exti overflow frame aac
原文地址:https://www.cnblogs.com/jimodetiantang/p/9135343.html