日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SRS流媒体服务器——Forward集群搭建和源码分析

發布時間:2024/4/11 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SRS流媒体服务器——Forward集群搭建和源码分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SRS流媒體服務器——Forward集群搭建和源碼分析

目錄

  • Forward集群原理
  • RTMP流轉發(Forward)部署實例
  • Forward集群源碼分析
  • 1. Forward集群原理

  • Forward 表示向前、前頭的、發送等意思。
  • 在SRS中可以理解為把Master節點獲得直播流?播(轉發)給所有的Slave節點,master節點由多少路直播流,那么在每個slave節點也會多少路直播流。
  • 注:在SRS中還有另外?種集群?式,edge?式。注意兩種?式的?詞不同。
    a. 在Forward模式中,中?節點叫Master,邊緣節點叫Slave。
    b. 在edge模式中,中?節點叫origin(源站),邊緣節點叫做edge。
  • 1. 適用場景

  • Forward適合搭建?型集群。
  • 推流者推流給master,那么master就會Forward到每?個Slave,那么在slave節點上不論需不需要都會有master過來的流。
  • 如果推流者的數量為10,那么master到slave之間的帶寬就是:帶寬=10 *slave的個數 *直播流碼率,隨著slave的增多,master的出?帶寬會不斷提?。
  • ?現實是,在某些slave節點其實沒有?看,這樣就造成了master到slave之間的帶寬浪費。
  • 所以說Forward適合?于搭建?型集群。
  • 2. RTMP流轉發(Forward)部署實例

  • SRS可以將送到SRS的流轉發給其他RTMP服務器,實現簡單集群/熱備功能,也可以實現?路流熱備
    a. 如編碼器由于帶寬限制,只能送?路流到RTMP服務器,要求RTMP服務器能將這路流也轉發給其他RTMP備?服務器,實現主備容錯集群。
  • 假設服務器的IP是:8.141.75.248
  • Forward就是SRS將流拷?輸出給其他的RTMP服務器,以SRS轉發給SRS為例:
  • 主SRS:Master,編碼器推流到主SRS,主SRS將流處理的同時,將流轉發到備SRS。
  • 備SRS:Slave,主SRS轉發流到備SRS,就像編碼器推送流到備?SRS?樣。
  • 測試部署的實例中,主SRS偵聽1935端?,備SRS偵聽19350和19351端?。
  • 1. 編寫主SRS配置?件

  • vim conf/forward.master.conf
  • # the config for srs to forward # @see https://github.com/ossrs/srs/wiki/v1_CN_SampleForward # @see full.conf for detail config.listen 1935; max_connections 1000; pid ./objs/srs.master.pid; vhost __defaultVhost__ {forward {enabled on;destination 127.0.0.1:19350 127.0.0.1:19351; #forward目的地址,增加一個19351端口} }
  • 啟動srs服務器。
  • ./objs/srs -c conf/forward.master.conf

    監聽日志信息:tail -f ./objs/srs.log

    2. 編寫從SRS配置文件

  • 復制conf/forward.slave.conf到conf/forward.slave1.conf,conf/forward.slave2.conf。
  • cp conf/forward.slave.conf conf/forward.slave1.conf cp conf/forward.slave.conf conf/forward.slave2.conf
  • 修改conf/forward.slave1.conf配置文件。
  • # the config for srs to forward # @see https://github.com/ossrs/srs/wiki/v1_CN_SampleForward # @see full.conf for detail config.listen 19350; #注意端口 max_connections 1000; pid ./objs/srs.slave1.pid; #./objs/srs.slave.pid改為./objs/srs.slave1.pid srs_log_tank file; srs_log_file ./objs/srs.slave1.log; vhost __defaultVhost__ { }
  • 修改conf/forward.slave2.conf配置文件。
  • # the config for srs to forward # @see https://github.com/ossrs/srs/wiki/v1_CN_SampleForward # @see full.conf for detail config.listen 19351; max_connections 1000; pid ./objs/srs.slave2.pid; srs_log_tank file; srs_log_file ./objs/srs.slave2.log; vhost __defaultVhost__ { }
  • 啟動slave1和slave2
  • ./objs/srs -c conf/forward.slave1.conf ./objs/srs -c conf/forward.slave2.conf監聽日志信息:tail -f ./objs/srs.log
  • 啟動srs后查看srs是否啟動成功:netstat -anp|grep srs
  • 3. 驗證是否部署成功

    1. 啟動推流編碼器

  • 使用FFmpeg進行推流
  • ffmpeg -re -i source.200kbps.768x320.flv -vcodec copy -acodec copy -f flv -y rtmp://8.141.75.248/live/livestream
  • 涉及到的流包括:
  • 編碼器推送的流:rtmp://8.141.75.248/live/livestream主SRS轉發的流:rtmp://8.141.75.248:19350/live/livestream 主SRS轉發的流:rtmp://8.141.75.248:19351/live/livestream觀看主SRS的流:rtmp://8.141.75.248/live/livestream 觀看從1 SRS的流:rtmp://8.141.75.248:19350/live/livestream 觀看從2 SRS的流:rtmp://8.141.75.248:19351/live/livestream

    2. 觀看主從SRS的RTMP流

  • 主RTMP流地址為:rtmp://8.141.75.248/live/livestream
  • 從1 SRS的流:rtmp://8.141.75.248:19350/live/livestream
  • 從2 SRS的流:rtmp://8.141.75.248:19351/live/livestream
  • 可以使用VLC觀看或者SRS播放器播放:srs播放器
  • 注意:19350和19351端口需要在服務器開放,不然從節點無法拉流
  • 3. Forward集群源碼分析

  • 從原理上來分析,要實現forward功能:
  • 讀取配置?件獲取forward server的地址
  • 創建RTMP推流客戶端
  • 從source??拉取消息,然后推送給forward server
  • 1. 從配置文件開始分析

  • 打開 conf/forward.master.conf 配置文件。
  • listen 1935; max_connections 1000; pid ./objs/srs.master.pid; srs_log_tank file; srs_log_file ./objs/srs.master.log; vhost __defaultVhost__ {forward {enabled on;destination 127.0.0.1:19350 127.0.0.1:19351;} }日志打印在終端設置方法: #srs_log_tank file; #srs_log_file ./objs/srs.log; daemon off; srs_log_tank console;
  • 在srs_app_config.cpp中搜索“forward”即可發現讀取“forward”的代碼。
  • bool SrsConfig::get_forward_enabled(string vhost) {static bool DEFAULT = false;SrsConfDirective* conf = get_vhost(vhost);if (!conf) {return DEFAULT;}conf = conf->get("forward");if (!conf) {return DEFAULT;}conf = conf->get("enabled");if (!conf || conf->arg0().empty()) {return DEFAULT;}return SRS_CONF_PERFER_FALSE(conf->arg0()); }SrsConfDirective* SrsConfig::get_forwards(string vhost) {SrsConfDirective* conf = get_vhost(vhost);if (!conf) {return NULL;}conf = conf->get("forward");if (!conf) {return NULL;}return conf->get("destination"); }

    2. 使用gdb打斷點進行分析:

  • 使用gdb打斷點進行分析:
  • gdb ./objs/srs(gdb) set args -c conf/forward.master.conf (gdb) b SrsConfig::get_forward_enabled(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) Breakpoint 1 at 0x53701d: file src/app/srs_app_config.cpp, line 4837. (gdb) b SrsConfig::get_forwards(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) Breakpoint 2 at 0x5372df: file src/app/srs_app_config.cpp, line 4859.
  • 推流后才能進行調試:
  • ffmpeg -re -i source.200kbps.768x320.flv -vcodec copy -acodec copy -f flv -y rtmp://8.141.75.248/live/livestream
  • SrsConfig::get_forward_enabled 對應調試信息:
  • Breakpoint 1, SrsConfig::get_forward_enabled (this=0xa0fcf0, vhost="__defaultVhost__") at src/app/srs_app_config.cpp:4837 4837 { (gdb) bt #0 SrsConfig::get_forward_enabled (this=0xa0fcf0, vhost="__defaultVhost__") at src/app/srs_app_config.cpp:4837 #1 0x00000000004e277a in SrsOriginHub::create_forwarders (this=0xab8000) at src/app/srs_app_source.cpp:1467 #2 0x00000000004e1214 in SrsOriginHub::on_publish (this=0xab8000) at src/app/srs_app_source.cpp:1120 #3 0x00000000004e76ce in SrsSource::on_publish (this=0xab7cd0) at src/app/srs_app_source.cpp:2457 #4 0x00000000004d96ca in SrsRtmpConn::acquire_publish (this=0xa9be50, source=0xab7cd0) at src/app/srs_app_rtmp_conn.cpp:940 #5 0x00000000004d874c in SrsRtmpConn::publishing (this=0xa9be50, source=0xab7cd0) at src/app/srs_app_rtmp_conn.cpp:822 #6 0x00000000004d5ee7 in SrsRtmpConn::stream_service_cycle (this=0xa9be50) at src/app/srs_app_rtmp_conn.cpp:534 #7 0x00000000004d4ddf in SrsRtmpConn::service_cycle (this=0xa9be50) at src/app/srs_app_rtmp_conn.cpp:388 #8 0x00000000004d3ba7 in SrsRtmpConn::do_cycle (this=0xa9be50) at src/app/srs_app_rtmp_conn.cpp:209 #9 0x00000000004d1d99 in SrsConnection::cycle (this=0xa9bec8) at src/app/srs_app_conn.cpp:171 #10 0x000000000050ab08 in SrsSTCoroutine::cycle (this=0xa9c130) at src/app/srs_app_st.cpp:198 #11 0x000000000050ab7d in SrsSTCoroutine::pfn (arg=0xa9c130) at src/app/srs_app_st.cpp:213 #12 0x00000000005bed1a in _st_thread_main () at sched.c:337 #13 0x00000000005bf492 in st_thread_create (start=0x5be696 <_st_vp_schedule+170>, arg=0x700000001, joinable=1, stk_size=1) at sched.c:616 Backtrace stopped: previous frame inner to this frame (corrupt stack?)
  • forward metadata,video,audio數據。
  • b SrsForwarder::on_meta_data(SrsSharedPtrMessage*)(gdb) bt #0 SrsForwarder::on_meta_data (this=0xab9e60, shared_metadata=0xb28490) at src/app/srs_app_forward.cpp:114 #1 0x00000000004df741 in SrsOriginHub::on_meta_data (this=0xab8000, shared_metadata=0xb28490, packet=0xb283f0) at src/app/srs_app_source.cpp:924 #2 0x00000000004e5e23 in SrsSource::on_meta_data (this=0xab7cd0, msg=0xb28210, metadata=0xb283f0) at src/app/srs_app_source.cpp:2113 #3 0x00000000004d9e93 in SrsRtmpConn::process_publish_message (this=0xa9be50, source=0xab7cd0, msg=0xb28210) at src/app/srs_app_rtmp_conn.cpp:1045 #4 0x00000000004d9aa6 in SrsRtmpConn::handle_publish_message (this=0xa9be50, source=0xab7cd0, msg=0xb28210) at src/app/srs_app_rtmp_conn.cpp:993 #5 0x0000000000582720 in SrsPublishRecvThread::consume (this=0xab6480, msg=0xb28210) at src/app/srs_app_recv_thread.cpp:389 #6 0x000000000058123e in SrsRecvThread::do_cycle (this=0xab6488) at src/app/srs_app_recv_thread.cpp:146 #7 0x000000000058108f in SrsRecvThread::cycle (this=0xab6488) at src/app/srs_app_recv_thread.cpp:115 #8 0x000000000050ab08 in SrsSTCoroutine::cycle (this=0xb024f0) at src/app/srs_app_st.cpp:198 #9 0x000000000050ab7d in SrsSTCoroutine::pfn (arg=0xb024f0) at src/app/srs_app_st.cpp:213 #10 0x00000000005bed1a in _st_thread_main () at sched.c:337 #11 0x00000000005bf492 in st_thread_create (start=0xab8290, arg=0xab61b0, joinable=0, stk_size=11231648) at sched.c:616 Backtrace stopped: previous frame inner to this frame (corrupt stack?) b SrsForwarder::on_video(SrsSharedPtrMessage*)(gdb) bt #0 SrsForwarder::on_video (this=0xab9e60, shared_video=0xb13080) at src/app/srs_app_forward.cpp:155 #1 0x00000000004e1037 in SrsOriginHub::on_video (this=0xab8000, shared_video=0xb13080, is_sequence_header=true) at src/app/srs_app_source.cpp:1106 #2 0x00000000004e6c59 in SrsSource::on_video_imp (this=0xab7cd0, msg=0xb13080) at src/app/srs_app_source.cpp:2303 #3 0x00000000004e68ad in SrsSource::on_video (this=0xab7cd0, shared_video=0xb28210) at src/app/srs_app_source.cpp:2258 #4 0x00000000004d9c7f in SrsRtmpConn::process_publish_message (this=0xa9be50, source=0xab7cd0, msg=0xb28210) at src/app/srs_app_rtmp_conn.cpp:1021 #5 0x00000000004d9aa6 in SrsRtmpConn::handle_publish_message (this=0xa9be50, source=0xab7cd0, msg=0xb28210) at src/app/srs_app_rtmp_conn.cpp:993 #6 0x0000000000582720 in SrsPublishRecvThread::consume (this=0xab6480, msg=0xb28210) at src/app/srs_app_recv_thread.cpp:389 #7 0x000000000058123e in SrsRecvThread::do_cycle (this=0xab6488) at src/app/srs_app_recv_thread.cpp:146 #8 0x000000000058108f in SrsRecvThread::cycle (this=0xab6488) at src/app/srs_app_recv_thread.cpp:115 #9 0x000000000050ab08 in SrsSTCoroutine::cycle (this=0xb024f0) at src/app/srs_app_st.cpp:198 #10 0x000000000050ab7d in SrsSTCoroutine::pfn (arg=0xb024f0) at src/app/srs_app_st.cpp:213 #11 0x00000000005bed1a in _st_thread_main () at sched.c:337 #12 0x00000000005bf492 in st_thread_create (start=0xab8290, arg=0xab61b0, joinable=0, stk_size=11231648) at sched.c:616 Backtrace stopped: previous frame inner to this frame (corrupt stack?) b SrsForwarder::on_audio(SrsSharedPtrMessage*)(gdb) bt #0 SrsForwarder::on_audio (this=0xab9e60, shared_audio=0xb13090) at src/app/srs_app_forward.cpp:132 #1 0x00000000004e02d9 in SrsOriginHub::on_audio (this=0xab8000, shared_audio=0xb13090) at src/app/srs_app_source.cpp:1013 #2 0x00000000004e644e in SrsSource::on_audio_imp (this=0xab7cd0, msg=0xb13090) at src/app/srs_app_source.cpp:2188 #3 0x00000000004e6051 in SrsSource::on_audio (this=0xab7cd0, shared_audio=0xb28210) at src/app/srs_app_source.cpp:2138 #4 0x00000000004d9c00 in SrsRtmpConn::process_publish_message (this=0xa9be50, source=0xab7cd0, msg=0xb28210) at src/app/srs_app_rtmp_conn.cpp:1014 #5 0x00000000004d9aa6 in SrsRtmpConn::handle_publish_message (this=0xa9be50, source=0xab7cd0, msg=0xb28210) at src/app/srs_app_rtmp_conn.cpp:993 #6 0x0000000000582720 in SrsPublishRecvThread::consume (this=0xab6480, msg=0xb28210) at src/app/srs_app_recv_thread.cpp:389 #7 0x000000000058123e in SrsRecvThread::do_cycle (this=0xab6488) at src/app/srs_app_recv_thread.cpp:146 #8 0x000000000058108f in SrsRecvThread::cycle (this=0xab6488) at src/app/srs_app_recv_thread.cpp:115 #9 0x000000000050ab08 in SrsSTCoroutine::cycle (this=0xb024f0) at src/app/srs_app_st.cpp:198 #10 0x000000000050ab7d in SrsSTCoroutine::pfn (arg=0xb024f0) at src/app/srs_app_st.cpp:213 #11 0x00000000005bed1a in _st_thread_main () at sched.c:337 #12 0x00000000005bf492 in st_thread_create (start=0xab8290, arg=0xab61b0, joinable=0, stk_size=11231648) at sched.c:616 Backtrace stopped: previous frame inner to this frame (corrupt stack?)
  • SrsForwarder::do_cycle() 完成建聯,推流到slave操作。
  • 主要邏輯在:SrsForwarder::forward()
  • b SrsForwarder::forward()(gdb) bt #0 SrsForwarder::forward (this=0xab9e60) at src/app/srs_app_forward.cpp:248 #1 0x00000000004f8648 in SrsForwarder::do_cycle (this=0xab9e60) at src/app/srs_app_forward.cpp:237 #2 0x00000000004f7fd7 in SrsForwarder::cycle (this=0xab9e60) at src/app/srs_app_forward.cpp:190 #3 0x000000000050ab08 in SrsSTCoroutine::cycle (this=0xab9c20) at src/app/srs_app_st.cpp:198 #4 0x000000000050ab7d in SrsSTCoroutine::pfn (arg=0xab9c20) at src/app/srs_app_st.cpp:213 #5 0x00000000005bed1a in _st_thread_main () at sched.c:337 #6 0x00000000005bf492 in st_thread_create (start=0xab8290, arg=0xab61b0, joinable=0, stk_size=11231648) at sched.c:616 Backtrace stopped: previous frame inner to this frame (corrupt stack?)
  • 后續會補充更詳細的代碼分析和流程圖,現在先自行根據調用stack看源碼。
  • 總結

    以上是生活随笔為你收集整理的SRS流媒体服务器——Forward集群搭建和源码分析的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。