音视频流媒体开发【六十五】RTMP/HLS/HTTP-FLV流媒体服务器分8-SRS流媒体框架-HLS⽂件配置和测试

HLS原理简介

RTMP指Adobe的RTMP(Realtime Message Protocol),⼴泛应⽤于低延时直播,也是编码器和服务器对接的实际标准协议,在PC(Flash)上有最佳观看体验和最佳稳定性。

HLS指Apple的HLS(Http Live Streaming),本身就是Live(直播)的,不过Vod(点播)也能⽀持。
HLS是Apple平台的标准流媒体协议,和RTMP在PC上⼀样⽀持得天⾐⽆缝。
HLS和RTMP两种分发⽅式,就可以⽀持所有的终端。

应⽤场景

HLS主要的应⽤场景包括:

  1. 跨平台:PC主要的直播⽅案是RTMP,也有⼀些库能播放HLS,譬如jwplayer,基于osmf的hls插件也⼀⼤堆。所以实际上如果选⼀种协议能跨PC/Android/IOS,那就是HLS。
  2. iOS上苛刻的稳定性要求:IOS上最稳定的当然是HLS,稳定性不差于RTMP在PC-flash上的表现。
  3. 友好的CDN分发⽅式:⽬前CDN对于RTMP也是基本协议,但是HLS分发的基础是HTTP,所以CDN的接⼊和分发会⽐RTMP更加完善。能在各种CDN之间切换,RTMP也能,只是可能需要对接测试。
  4. 简单:HLS作为流媒体协议⾮常简单,apple⽀持得也很完善。Android对HLS的⽀持也会越来越完善。⾄于DASH/HDS,好像没有什么特别的理由,就像linux已经⼤⾏其道⽽且开放,其他的系统很难再⼴泛应⽤。

总之,SRS⽀持HLS主要是作为输出的分发协议,直播以RTMP+HLS分发,满⾜各种应⽤场景。点播以HLS为主。

分发⽅式⽐较

互联⽹上的两种主要的分发⽅式:HLSRTMP,什么时候⽤谁,完全决定于应⽤场景。

还有其他的分发⽅式,这些分发⽅式不属于互联⽹常⻅和通⽤的⽅式,不予以⽐较:
  1. UDP:譬如YY的实时应⽤,视频会议等等,或者RTSP之类。这类应⽤的特点就是实时性要求特别⾼,以毫秒计算。TCP家族协议根本就满⾜不了要求,所以HTTP/TCP都不靠谱。这类应⽤没有通⽤的⽅案,必须⾃⼰实现分发(服务端)和播放(客户端)。
  2. P2P:譬如RTMFP或者各家⾃⼰的协议。这类应⽤的特点是节省带宽。⽬前PC/flash上的RTMFP⽐较成熟,Android上的P2P属于起步群雄纷争标准不⼀,IOS上P2P应该没有听说过。
  3. RTSP:这种不是互联⽹上的主要应⽤,在其他领域譬如安防等有⼴泛应⽤。
另外,HTTP的也分为⼏种:
  1. HTTP progressive:早期流媒体服务器分发http⽂件时,以普通的http⽂件分发,这种叫做渐进式下载,意思就是如果⽂件很⼤譬如1⼩时时⻓1GB⼤⼩,想从中间开始播放是不⾏的。但这种⽅式已经是作古了,很多http服务器⽀持http⽂件的seek,就是从中间开始播放。
  2. HTTP stream:⽀持seek的HTTP流,譬如各家视频⽹站的点播分发⽅式。或者稍微复杂点的,譬如把⼀个⼤⽂件切⼏段之后分发。⽬前在pc/flash上点播国内的主流分发是这种⽅式。
  3. HLS:这种是现在适配⽅式最⼴(除了flash, 需要额外的as库⽀持),在PC上有vlc,Android/IOS原⽣播放器就⽀持播放HLS,HTML5⾥⾯的url可以写HLS地址。总之,在移动端是以HLS为主。
  4. HDS:adobe⾃⼰的HLS,⼀坨屎。
  5. DASH:各家提出的HLS,⽬前还没有⼴泛应⽤。
对⽐以下互联⽹上⽤的流媒体分发⽅式:
  1. HLS:apple的HLS,⽀持点播和直播。
  2. HTTP:即HTTP stream,各家⾃⼰定义的http流,应⽤于国内点播视频⽹站。
  3. RTMP:直播应⽤,对实时性有⼀定要求,以PC为主。

更进⼀步的⽐较继续参考 srs官⽹wiki:

https://github.com/ossrs/srs/wiki/v3_CN_RTMP.PK.HTTP

推荐的⽅式

  1. 编码器输出RTMP协议。
  2. 流媒体系统接⼊使⽤RTMP协议。
  3. 流媒体系统内部直播分发使⽤RTMP。
  4. PC+直播+实时性要求⾼:使⽤flash播放RTMP。
  5. PC+直播+没有实时性要求:使⽤RTMP或者HLS均可。
  6. PC+点播:使⽤HTTP或者HLS。
  7. Apple IOS/OSX:都使⽤HLS(实时性要求⾼得⾃⼰解析RTMP,或者使⽤外部库,譬如https://www.vitamio.org
  8. Andorid:和IOS⼀样,不过可以确定的是可以⾃⼰开发⽀持RTMP。

HLS部署

第⼀步,编写SRS配置⽂件。详细参考HLS分发

将以下内容保存为⽂件,譬如 conf/hls.conf ,服务器启动时指定该配置⽂件(srs的conf⽂件夹有该⽂件)。

# conf/hls.conf
listen 1935;
max_connections 1000;
http_server {
  enabled on;
  listen 8081;
  dir ./objs/nginx/html;
}
vhost __defaultVhost__ {
  hls {
    enabled on;
    hls_path ./objs/nginx/html;
    hls_fragment 10; # 分⽚时⻓
    hls_window 60; # 窗⼝时⻓
  }
}

备注:我们使⽤SRS内置的HTTP服务器分发HLS切⽚,也可以使⽤Nginx等Web服务器分发。
备注:hls_path必须存在,srs只会⾃动创建${hls_path}下的app的⽬录。参考:HLS分发流程

第⼆步,启动SRS。详细参考HLS分发
./objs/srs -c conf/hls.conf

完整的hls配置⽂件说明

# the vhost with hls specified.
vhost hls.srs.com {
  hls {
    # whether the hls is enabled.
    # if off, do not 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 ts
    # @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.
    # default: 60
    hls_window 60;
    # the error strategy. can be:
    # 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 configured by hls_path/hls_m3u8_file, the default is:
    # ./objs/nginx/html/[app]/[stream].m3u8
    # the ts file is configured by hls_path/hls_ts_file, the default 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], replace this const to current minute.
    # [05], replace this const to current second.
    # [999], replace this const to current millisecond.
    # [timestamp],replace this const to current UNIX timestamp in ms.
    # [seq], the sequence number of ts.
    # [duration], replace this const to current ts duration.
    # @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 enhanced algorithm to calc deviation for segment.
    # @remark when floor on, recommend the hls_segment>=2*gop.
    # default: off
    hls_ts_floor off;
    # the hls entry prefix, which is base url of ts url.
    # for example, the prefix is:
    # http://your-server/
    # then, the ts path in m3u8 will be like:
    # http://your-server/live/livestream-0.ts
    # http://your-server/live/livestream-1.ts
    # ...
    # optional, default 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;
    # If there is no incoming packets, dispose HLS in this timeout in seconds,
    # which removes all HLS files including m3u8 and ts files.
    # @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;
    # whether using AES encryption.
    # default: off
    hls_keys on;
    # the number of clear ts which one key can encrypt.
    # default: 5
    hls_fragments_per_key 5;
    # the hls key 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.
    # [seq], the sequence number of key corresponding to the ts.
    hls_key_file [app]/[stream]-[seq].key;
    # the key output path.
    # the key file is configed by hls_path/hls_key_file, the default is:
    # ./objs/nginx/html/[app]/[stream]-[seq].key
    hls_key_file_path ./objs/nginx/html;
    # the key root URL, use this can support https.
    # @remark It's optional.
    hls_key_url https://localhost:8080;
    # Special control controls.
    ###########################################
    # Whether calculate the DTS of audio frame directly.
    # If on, guess the specific DTS by AAC samples, please read https://github.com/ossrs/srs/issues/547#issuecomment-294350544
    # If off, directly turn the FLV timestamp to DTS, which might cause corrupt audio stream.
    # @remark Recommend to set to off, unless your audio stream sample-rate and timestamp is not correct.
    # Default: on
    hls_dts_directly 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_EN_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.
    # [param], replace with the param.
    # [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_EN_DeliveryHLS#on-hls-notify
  }
}

源码分析

数据从哪⾥来 怎么创建源
怎么切⽚
怎么更新数据
怎么提供分发

推流断点
SrsConfig::get_hls_fragment
没有推流的时候不会触发,rtmp推流后触发

#0 SrsConfig::get_hls_fragment (this=0xa0fcf0, vhost="__defaultVhost__") at
src/app/srs_app_config.cpp:6102
#1 0x00000000004f3ee9 in SrsHlsController::on_publish (this=0xa3b560, req=0xa3bcb0) at src/app/srs_app_hls.cpp:885
#2 0x00000000004f5be2 in SrsHls::on_publish (this=0xa3b030) at src/app/srs_app_hls.cpp:1181
#3 0x00000000004e0603 in SrsOriginHub::on_publish (this=0xa3b670) at src/app/srs_app_source.cpp:1129
#4 0x00000000004e6a0b in SrsSource::on_publish (this=0xa3b1e0) at src/app/srs_app_source.cpp:2460
#5 0x00000000004d89f2 in SrsRtmpConn::acquire_publish (this=0xa30d60, source=0xa3b1e0) at src/app/srs_app_rtmp_conn.cpp:940
#6 0x00000000004d7a74 in SrsRtmpConn::publishing (this=0xa30d60, source=0xa3b1e0) at src/app/srs_app_rtmp_conn.cpp:822
#7 0x00000000004d5229 in SrsRtmpConn::stream_service_cycle (this=0xa30d60) at src/app/srs_app_rtmp_conn.cpp:534
#8 0x00000000004d4141 in SrsRtmpConn::service_cycle (this=0xa30d60) at src/app/srs_app_rtmp_conn.cpp:388
#9 0x00000000004d2f09 in SrsRtmpConn::do_cycle (this=0xa30d60) at src/app/srs_app_rtmp_conn.cpp:209
#10 0x00000000004d10fb in SrsConnection::cycle (this=0xa30dd8) at src/app/srs_app_conn.cpp:171
#11 0x0000000000509c88 in SrsSTCoroutine::cycle (this=0xa31010) at src/app/srs_app_st.cpp:198
#12 0x0000000000509cfd in SrsSTCoroutine::pfn (arg=0xa31010) at src/app/srs_app_st.cpp:213
#13 0x00000000005bdd9d in _st_thread_main () at sched.c:337
#14 0x00000000005be515 in st_thread_create (start=0x5bd719 <_st_vp_schedule+170>,arg=0x700000001,

从这⾥可以看出来 hls相关的业务都在:srs_app_hls.cpp ⽂件,以及SrsHlsMuxer是核⼼所在。

SrsHlsMuxer::_refresh_m3u8

SrsHlsMuxer::update_config

配置hls

SrsTsMessageCache::cache_audio
#0 SrsTsMessageCache::cache_audio (this=0xa3b580, frame=0xa41640, dts=5130) at src/kernel/srs_kernel_ts.cpp:2716
#1 0x00000000004f4f18 in SrsHlsController::write_audio (this=0xa3b560, frame=0xa41640, pts=5130) at src/app/srs_app_hls.cpp:971
#2 0x00000000004f613c in SrsHls::on_audio (this=0xa3b030, shared_audio=0x7ffff7ee8b10,format=0xa3bad0) at src/app/srs_app_hls.cpp:1290
#3 0x00000000004deeaf in SrsOriginHub::on_audio (this=0xa3b670,shared_audio=0x7ffff7ee8b10)at src/app/srs_app_source.cpp:969
#4 0x00000000004e579a in SrsSource::on_audio_imp (this=0xa3b1e0, msg=0x7ffff7ee8b10)at src/app/srs_app_source.cpp:2191
#5 0x00000000004e539d in SrsSource::on_audio (this=0xa3b1e0, shared_audio=0xa3dfa0)at src/app/srs_app_source.cpp:2141
#6 0x00000000004d8f28 in SrsRtmpConn::process_publish_message (this=0xa30d60,source=0xa3b1e0,msg=0xa3dfa0) at src/app/srs_app_rtmp_conn.cpp:1014
#7 0x00000000004d8dce in SrsRtmpConn::handle_publish_message (this=0xa30d60,source=0xa3b1e0, msg=0xa3dfa0)at src/app/srs_app_rtmp_conn.cpp:993
#8 0x00000000005810b6 in SrsPublishRecvThread::consume (this=0x7ffff7f42800,msg=0xa3dfa0)at src/app/srs_app_recv_thread.cpp:389
#9 0x000000000057fbd4 in SrsRecvThread::do_cycle (this=0x7ffff7f42808)at src/app/srs_app_recv_thread.cpp:146
---Type <return> to continue, or q <return> to quit---
#10 0x000000000057fa25 in SrsRecvThread::cycle (this=0x7ffff7f42808) at src/app/srs_app_recv_thread.cpp:115
#11 0x0000000000509c88 in SrsSTCoroutine::cycle (this=0xa3de00) at src/app/srs_app_st.cpp:198
#12 0x0000000000509cfd in SrsSTCoroutine::pfn (arg=0xa3de00) at src/app/srs_app_st.cpp:213
#13 0x00000000005bdd9d in _st_thread_main () at sched.c:337
SrsTsContextWriter::SrsTsContextWriter

对应的writer 实际是SrsFileWriter ,也就是说SrsFileWriter 负责⽂件的写⼊

SrsTsContextWriter::write_audio 写⼊TS的audio
#0 SrsTsContextWriter::write_audio (this=0xa3c770, audio=0xa45bf0) at
src/kernel/srs_kernel_ts.cpp:2582
#1 0x00000000004f1858 in SrsHlsMuxer::flush_audio (this=0xa3b6f0, cache=0xa3b580)at src/app/srs_app_hls.cpp:552
#2 0x00000000004f5089 in SrsHlsController::write_audio (this=0xa3b560, frame=0xa41640,pts=3060)at src/app/srs_app_hls.cpp:1001
#3 0x00000000004f613c in SrsHls::on_audio (this=0xa3b030, shared_audio=0x7ffff7ee8b10,format=0xa3bad0)at src/app/srs_app_hls.cpp:1290
#4 0x00000000004deeaf in SrsOriginHub::on_audio (this=0xa3b670,shared_audio=0x7ffff7ee8b10)at src/app/srs_app_source.cpp:969
#5 0x00000000004e579a in SrsSource::on_audio_imp (this=0xa3b1e0, msg=0x7ffff7ee8b10)at src/app/srs_app_source.cpp:2191
#6 0x00000000004e539d in SrsSource::on_audio (this=0xa3b1e0, shared_audio=0xa3dfa0)at src/app/srs_app_source.cpp:2141
#7 0x00000000004d8f28 in SrsRtmpConn::process_publish_message (this=0xa30d60,source=0xa3b1e0,msg=0xa3dfa0) at src/app/srs_app_rtmp_conn.cpp:1014
#8 0x00000000004d8dce in SrsRtmpConn::handle_publish_message (this=0xa30d60,source=0xa3b1e0, msg=0xa3dfa0)at src/app/srs_app_rtmp_conn.cpp:993
#9 0x00000000005810b6 in SrsPublishRecvThread::consume (this=0x7ffff7f42800,msg=0xa3dfa0)at src/app/srs_app_recv_thread.cpp:389
#10 0x000000000057fbd4 in SrsRecvThread::do_cycle (this=0x7ffff7f42808)at src/app/srs_app_recv_thread.cpp:146
---Type <return> to continue, or q <return> to quit---
#11 0x000000000057fa25 in SrsRecvThread::cycle (this=0x7ffff7f42808) at src/app/srs_app_recv_thread.cpp:115
#12 0x0000000000509c88 in SrsSTCoroutine::cycle (this=0xa3de00) at src/app/srs_app_st.cpp:198
#13 0x0000000000509cfd in SrsSTCoroutine::pfn (arg=0xa3de00) at src/app/srs_app_st.cpp:213
SrsTsContextWriter::write_video 写⼊TS的video

在调⽤write_video之前是先要 SrsTsMessageCache::cache_video 缓存video

#0 SrsTsContextWriter::write_video (this=0xa3c770, video=0xa45bf0) atsrc/kernel/srs_kernel_ts.cpp:2597
#1 0x00000000004f1a67 in SrsHlsMuxer::flush_video (this=0xa3b6f0, cache=0xa3b580)at src/app/srs_app_hls.cpp:581
#2 0x00000000004f523a in SrsHlsController::write_video (this=0xa3b560, frame=0xa3fdb0,dts=0)at src/app/srs_app_hls.cpp:1031
#3 0x00000000004f63c3 in SrsHls::on_video (this=0xa3b030, shared_video=0x7ffff7ee8b00,format=0xa3bad0)at src/app/srs_app_hls.cpp:1340
#4 0x00000000004dfbef in SrsOriginHub::on_video (this=0xa3b670,shared_video=0x7ffff7ee8b00,is_sequence_header=false) at src/app/srs_app_source.cpp:1062
#5 0x00000000004e5fa5 in SrsSource::on_video_imp (this=0xa3b1e0, msg=0x7ffff7ee8b00)at src/app/srs_app_source.cpp:2306
#6 0x00000000004e5bf9 in SrsSource::on_video (this=0xa3b1e0, shared_video=0xa3dfa0)at src/app/srs_app_source.cpp:2261
#7 0x00000000004d8fa7 in SrsRtmpConn::process_publish_message (this=0xa30d60,source=0xa3b1e0,msg=0xa3dfa0) at src/app/srs_app_rtmp_conn.cpp:1021
#8 0x00000000004d8dce in SrsRtmpConn::handle_publish_message (this=0xa30d60,source=0xa3b1e0, msg=0xa3dfa0)at src/app/srs_app_rtmp_conn.cpp:993
#9 0x00000000005810b6 in SrsPublishRecvThread::consume (this=0x7ffff7f42800,msg=0xa3dfa0)at src/app/srs_app_recv_thread.cpp:389
#10 0x000000000057fbd4 in SrsRecvThread::do_cycle (this=0x7ffff7f42808)at src/app/srs_app_recv_thread.cpp:146
---Type <return> to continue, or q <return> to quit---
#11 0x000000000057fa25 in SrsRecvThread::cycle (this=0x7ffff7f42808) at src/app/srs_app_recv_thread.cpp:115
#12 0x0000000000509c88 in SrsSTCoroutine::cycle (this=0xa3de00) at src/app/srs_app_st.cpp:198
#13 0x0000000000509cfd in SrsSTCoroutine::pfn (arg=0xa3de00) at src/app/srs_app_st.cpp:213
SrsHlsMuxer::segment_open 开始⼀个segment
#0 SrsHlsMuxer::segment_open (this=0xa3b6d0) at src/app/srs_app_hls.cpp:353
#1 0x00000000004f5406 in SrsHlsController::reap_segment (this=0xa3b540) at src/app/srs_app_hls.cpp:1058
#2 0x00000000004f51d2 in SrsHlsController::write_video (this=0xa3b540, frame=0xa3fd90,dts=2379600)at src/app/srs_app_hls.cpp:1024
#3 0x00000000004f63c3 in SrsHls::on_video (this=0xa3b010, shared_video=0x7ffff7ee8b00,format=0xa3bab0)at src/app/srs_app_hls.cpp:1340
#4 0x00000000004dfbef in SrsOriginHub::on_video (this=0xa3b650,shared_video=0x7ffff7ee8b00,is_sequence_header=false) at src/app/srs_app_source.cpp:1062
#5 0x00000000004e5fa5 in SrsSource::on_video_imp (this=0xa3b1c0, msg=0x7ffff7ee8b00)at src/app/srs_app_source.cpp:2306
#6 0x00000000004e5bf9 in SrsSource::on_video (this=0xa3b1c0, shared_video=0xa3df80)at src/app/srs_app_source.cpp:2261
#7 0x00000000004d8fa7 in SrsRtmpConn::process_publish_message (this=0xa30d60,source=0xa3b1c0,msg=0xa3df80) at src/app/srs_app_rtmp_conn.cpp:1021
#8 0x00000000004d8dce in SrsRtmpConn::handle_publish_message (this=0xa30d60,source=0xa3b1c0, msg=0xa3df80)at src/app/srs_app_rtmp_conn.cpp:993
#9 0x00000000005810b6 in SrsPublishRecvThread::consume (this=0x7ffff7f42800,msg=0xa3df80)at src/app/srs_app_recv_thread.cpp:389
#10 0x000000000057fbd4 in SrsRecvThread::do_cycle (this=0x7ffff7f42808)at src/app/srs_app_recv_thread.cpp:146
---Type <return> to continue, or q <return> to quit---
#11 0x000000000057fa25 in SrsRecvThread::cycle (this=0x7ffff7f42808) at src/app/srs_app_recv_thread.cpp:115
#12 0x0000000000509c88 in SrsSTCoroutine::cycle (this=0xa3dde0) at src/app/srs_app_st.cpp:198
#13 0x0000000000509cfd in SrsSTCoroutine::pfn (arg=0xa3dde0) at src/app/srs_app_st.cpp:213
#14 0x00000000005bdd9d in _st_thread_main () at sched.c:337
SrsHlsMuxer::do_segment_close 结束⼀个segment的写⼊
#0 SrsHlsMuxer::do_segment_close (this=0xa3b6f0) at src/app/srs_app_hls.cpp:602
#1 0x00000000004f1b19 in SrsHlsMuxer::segment_close (this=0xa3b6f0) at src/app/srs_app_hls.cpp:593
#2 0x00000000004f52c4 in SrsHlsController::reap_segment (this=0xa3b560) at
src/app/srs_app_hls.cpp:1046
#3 0x00000000004f51d2 in SrsHlsController::write_video (this=0xa3b560, frame=0xa3fdb0,dts=1328400)at src/app/srs_app_hls.cpp:1024
#4 0x00000000004f63c3 in SrsHls::on_video (this=0xa3b030, shared_video=0x7ffff7ee8b00,format=0xa3bad0)at src/app/srs_app_hls.cpp:1340
#5 0x00000000004dfbef in SrsOriginHub::on_video (this=0xa3b670,shared_video=0x7ffff7ee8b00,is_sequence_header=false) at src/app/srs_app_source.cpp:1062
#6 0x00000000004e5fa5 in SrsSource::on_video_imp (this=0xa3b1e0, msg=0x7ffff7ee8b00)at src/app/srs_app_source.cpp:2306
#7 0x00000000004e5bf9 in SrsSource::on_video (this=0xa3b1e0, shared_video=0xa3dfa0)at src/app/srs_app_source.cpp:2261
#8 0x00000000004d8fa7 in SrsRtmpConn::process_publish_message (this=0xa30d60,source=0xa3b1e0,msg=0xa3dfa0) at src/app/srs_app_rtmp_conn.cpp:1021
#9 0x00000000004d8dce in SrsRtmpConn::handle_publish_message (this=0xa30d60,source=0xa3b1e0, msg=0xa3dfa0)at src/app/srs_app_rtmp_conn.cpp:993
#10 0x00000000005810b6 in SrsPublishRecvThread::consume (this=0x7ffff7f42800, msg=0xa3dfa0)at src/app/srs_app_recv_thread.cpp:389
#11 0x000000000057fbd4 in SrsRecvThread::do_cycle (this=0x7ffff7f42808)at src/app/srs_app_recv_thread.cpp:146
---Type <return> to continue, or q <return> to quit--- 
#12 0x000000000057fa25 in SrsRecvThread::cycle (this=0x7ffff7f42808) at src/app/srs_app_recv_thread.cpp:115
#13 0x0000000000509c88 in SrsSTCoroutine::cycle (this=0xa3de00) at src/app/srs_app_st.cpp:198
SrsAsyncCallWorker::execute push了什么任务

在SrsAsyncCallWorker::flush_tasks⾥⾯执⾏

SrsFragment::unlink_file删除⼀个segment

拉流断点

SrsFileReader::SrsFileReader 读取ts⽂件
394 if ((err = fs->open(fullpath)) != srs_success) {
(gdb) print fullpath
$1 = "./objs/nginx/html/live/livestream.m3u8"
请求m3u8⽂件
#0 SrsFileReader::SrsFileReader (this=0xa7ac50, __in_chrg=<optimized out>, __vtt_parm=<optimized out>)at src/kernel/srs_kernel_file.cpp:195
#1 0x0000000000459258 in ISrsFileReaderFactory::create_file_reader (this=0xa122f0)at src/kernel/srs_kernel_file.cpp:192
#2 0x0000000000499842 in SrsHttpFileServer::serve_file (this=0xa12310, w=0x7ffff7f0cbd0,r=0xa77130,fullpath="./objs/nginx/html/live/livestream.m3u8") at src/protocol/srs_http_stack.cpp:391
#3 0x000000000049957e in SrsHttpFileServer::serve_http (this=0xa12310, w=0x7ffff7f0cbd0,r=0xa77130)at src/protocol/srs_http_stack.cpp:384
#4 0x000000000049c86f in SrsHttpServeMux::serve_http (this=0xa10420, w=0x7ffff7f0cbd0,r=0xa77130)at src/protocol/srs_http_stack.cpp:711
#5 0x00000000005620a1 in SrsHttpServer::serve_http (this=0xa11e60, w=0x7ffff7f0cbd0,r=0xa77130)at src/app/srs_app_http_conn.cpp:303
#6 0x000000000049d6be in SrsHttpCorsMux::serve_http (this=0xa59bb0, w=0x7ffff7f0cbd0,r=0xa77130)at src/protocol/srs_http_stack.cpp:859
#7 0x0000000000561086 in SrsHttpConn::process_request (this=0xa76ea0, w=0x7ffff7f0cbd0,r=0xa77130)at src/app/srs_app_http_conn.cpp:161
#8 0x0000000000560ce8 in SrsHttpConn::do_cycle (this=0xa76ea0) at src/app/srs_app_http_conn.cpp:133
#9 0x00000000004d10fb in SrsConnection::cycle (this=0xa76ea0) at src/app/srs_app_conn.cpp:171
#10 0x0000000000509c88 in SrsSTCoroutine::cycle (this=0xa5aa20) at src/app/srs_app_st.cpp:198
#11 0x0000000000509cfd in SrsSTCoroutine::pfn (arg=0xa5aa20) at src/app/srs_app_st.cpp:213
---Type <return> to continue, or q <return> to quit---
#12 0x00000000005bdd9d in _st_thread_main () at sched.c:337
#13 0x00000000005be515 in st_thread_create (start=0x5bd719 <_st_vp_schedule+170>,arg=0x900000001,joinable=1, stk_size=1) at sched.c:616
请求ts⽂件
#0 SrsFileReader::SrsFileReader (this=0xa3e030, __in_chrg=<optimized out>, __vtt_parm=<optimized out>)at src/kernel/srs_kernel_file.cpp:195
#1 0x0000000000459258 in ISrsFileReaderFactory::create_file_reader (this=0xa122f0)at src/kernel/srs_kernel_file.cpp:192
#2 0x0000000000499842 in SrsHttpFileServer::serve_file (this=0xa12310, w=0x7ffff7f42bd0,r=0xa7c6d0,fullpath="./objs/nginx/html/live/livestream-31.ts") at src/protocol/srs_http_stack.cpp:391
#3 0x000000000049957e in SrsHttpFileServer::serve_http (this=0xa12310, w=0x7ffff7f42bd0,r=0xa7c6d0)at src/protocol/srs_http_stack.cpp:384
#4 0x000000000049c86f in SrsHttpServeMux::serve_http (this=0xa10420, w=0x7ffff7f42bd0,r=0xa7c6d0)at src/protocol/srs_http_stack.cpp:711
#5 0x00000000005620a1 in SrsHttpServer::serve_http (this=0xa11e60, w=0x7ffff7f42bd0,r=0xa7c6d0)at src/app/srs_app_http_conn.cpp:303
#6 0x000000000049d6be in SrsHttpCorsMux::serve_http (this=0xa77c70, w=0x7ffff7f42bd0,r=0xa7c6d0)at src/protocol/srs_http_stack.cpp:859
#7 0x0000000000561086 in SrsHttpConn::process_request (this=0xa7bbc0, w=0x7ffff7f42bd0,r=0xa7c6d0)at src/app/srs_app_http_conn.cpp:161
#8 0x0000000000560ce8 in SrsHttpConn::do_cycle (this=0xa7bbc0) at src/app/srs_app_http_conn.cpp:133
#9 0x00000000004d10fb in SrsConnection::cycle (this=0xa7bbc0) at src/app/srs_app_conn.cpp:171
#10 0x0000000000509c88 in SrsSTCoroutine::cycle (this=0xa30430) at src/app/srs_app_st.cpp:198
#11 0x0000000000509cfd in SrsSTCoroutine::pfn (arg=0xa30430) at src/app/srs_app_st.cpp:213

类解析

SrsHls 对于HLS业务的封装
SrsHlsController The hls stream cache
SrsHlsMuxer 负责⽣产m3u8和ts⽂件
SrsHlsSegment 负责分⽚,⽂件创建、删除等
SrsFragment 对⽂件的处理,是SrsHlsSegment 的基类
SrsFragmentWindow 管理segment

SrsAsyncCallWorker::cycle

SrsAsyncCallWorker::flush_tasks

SrsHlsController::write_audio

SrsHlsController::write_video

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容