美文网首页
音视频流媒体开发【五十八】SRS流媒体服务器1-SRS环境搭建

音视频流媒体开发【五十八】SRS流媒体服务器1-SRS环境搭建

作者: AlanGe | 来源:发表于2023-05-26 20:20 被阅读0次

    音视频流媒体开发-目录
    iOS知识点-目录
    Android-目录
    Flutter-目录
    数据结构与算法-目录
    uni-pp-目录

    SRS环境搭建

    srs官⽹:https://github.com/ossrs/srs

    码云的源速度快:https://gitee.com/winlinvip/srs.oschina.git
    github的源速度慢:https://github.com/ossrs/srs.git

    选择当前最新的release版本3.0

    第⼀步,获取SRS。详细参考GIT获取代码

    git clone https://gitee.com/winlinvip/srs.oschina.git srs.3.0-20200720
    cd srs.3.0-20200720
    #使⽤当前最新的3.0版本
    git checkout 3.0release
    cd trunk
    

    第⼆步,编译SRS。详细参考Build

    ./configure && make
    

    第三步,编写SRS配置⽂件。

    详细参考
    RTMP分发
    Delivery HLS
    Delivery HTTP FLV

    编辑 conf/srs.conf ,服务器启动时指定该配置⽂件(srs的conf⽂件夹有该⽂件)。

    listen 1935;
    max_connections 1000;
    srs_log_tank file;
    srs_log_file ./objs/srs.log;
    http_api {
        enabled on;
        listen 1985;
    }
    http_server {
        enabled on;
        listen 8081; # http监听端⼝,注意⾃⼰配置的端⼝
        dir ./objs/nginx/html;
    }
    stats {
        network 0;
        disk sda sdb xvda xvdb;
    }
    vhost __defaultVhost__ {
        # hls darren
        hls {
            enabled on;
            hls_path ./objs/nginx/html;
            hls_fragment 10;
            hls_window 60;
        }
        # http-flv darren
        http_remux {
            enabled on;
            mount [vhost]/[app]/[stream].flv;
            hstrs on;
        }
    }
    

    第四步,启动SRS。

    ./objs/srs -c conf/srs.conf
    
    ubuntu@VM-0-13-ubuntu:~/0voice/media/srs.3.0-20200720/trunk$ ./objs/srs -c conf/srs.conf
    后台运⾏结果
    [2020-07-20 17:34:48.061][Trace][30433][0] XCORE-SRS/3.0.141(OuXuli)
    [2020-07-20 17:34:48.061][Trace][30433][0] config parse complete
    [2020-07-20 17:34:48.061][Trace][30433][0] write log to file ./objs/srs.log
    [2020-07-20 17:34:48.061][Trace][30433][0] you can: tailf ./objs/srs.log
    [2020-07-20 17:34:48.061][Trace][30433][0] @see: [https://github.com/o](https://github.com/o)ssrs/srs/wiki/v1_CN_SrsLog
    
    确认是否已经正常启动
    ubuntu@VM-0-13-ubuntu:~/0voice/media/srs.3.0-20200720/trunk$ sudo ps-ef | grep srs
    ubuntu 30435 1 0 17:34 pts/0 00:00:00 ./objs/srs -c conf/srs.conf
    

    显示到ubuntu 30435 1 0 17:34 pts/0 00:00:00 ./objs/srs -c conf/srs.conf

    安全退出正在运⾏的srs

    sudo kill -SIGQUIT srs_pid

    默认是后台启动的⽅式,如果是要⽅便GDB调试则需要修改配置⽂件为前台启动。
    listen 1935;
    max_connections 1000;
    #srs_log_tank file;
    #srs_log_file ./objs/srs.log;
    # 前台运⾏
    daemon off;
    # 打印到终端控制台
    srs_log_tank console;
    http_api {
        enabled on;
        listen 1985;
    }
    http_server {
        enabled on;
        listen 8081; # http监听端⼝
    dir ./objs/nginx/html;
    }
    stats {
        network 0;
        disk sda sdb xvda xvdb;
    }
    vhost __defaultVhost__ {
        # hls darren
        hls {
            enabled on;
            hls_path ./objs/nginx/html;
            hls_fragment 10;
            hls_window 60;
        }
        # http-flv darren
        http_remux {
            enabled on;
            mount [vhost]/[app]/[stream].flv;
            hstrs on;
        }
    }
    

    执⾏⽅法:

    ./objs/srs -c conf/srs.conf
    

    在终端运⾏,log也在终端显示

    [2020-07-20 17:46:33.586][Trace][1533][0] system default latency(ms): mw(0-350) + mr(0-350) + play-queue(0-30000)
    [2020-07-20 17:46:33.586][Warn][1533][0][0] SRS/3.0.141 is beta
    [2020-07-20 17:46:33.586][Trace][1533][0] http flv live stream, vhost=__defaultVhost__, mount=[vhost]/[app]/[stream].flv
    [2020-07-20 17:46:33.586][Trace][1533][0] http: root mount to ./objs/nginx/html
    [2020-07-20 17:46:33.586][Trace][1533][0] st_init success, use epoll
    [2020-07-20 17:46:33.586][Trace][1533][380] server main cid=380, pid=1533, ppid=2337, asprocess=0
    [2020-07-20 17:46:33.586][Trace][1533][380] write pid=1533 to ./objs/srs.pid success!
    [2020-07-20 17:46:33.586][Trace][1533][380] RTMP listen at tcp://0.0.0.0:1935, fd=7
    [2020-07-20 17:46:33.586][Trace][1533][380] HTTP-API listen at tcp://0.0.0.0:1985, fd=8
    [2020-07-20 17:46:33.586][Trace][1533][380] HTTP-Server listen at tcp://0.0.0.0:8081, fd=9
    [2020-07-20 17:46:33.586][Trace][1533][380] signal installed, reload=1, reopen=10, fast_quit=15, grace_quit=3
    [2020-07-20 17:46:33.586][Trace][1533][380] http: api mount /consoleto ./objs/nginx/html/console
    

    SRS源码⽬录

    trunk⽬录

    3rdparty auto conf configure doc etc ide modules research scripts src usr

    image.png

    src下的源码

    app core kernel libs main protocol service utest

    image.png

    app应⽤.

    |── app
    │ ├── srs_app_async_call.cpp
    │ ├── srs_app_async_call.hpp 可以⽤来执⾏异步任务,通过execute()函数 push任务,然后在cycle()执⾏
    │ ├── srs_app_bandwidth.cpp
    │ ├── srs_app_bandwidth.hpp 提供带宽测试接⼝
    │ ├── srs_app_caster_flv.cpp
    │ ├── srs_app_caster_flv.hpp ⽀持POST⼀个flv流到服务器,类似相当于RTMP的publish
    │ ├── srs_app_config.cpp
    │ ├── srs_app_config.hpp 读取配置⽂件
    │ ├── srs_app_conn.cpp
    │ ├── srs_app_conn.hpp srs的基本连接,每个连接对应⼀个协程,所有的连接都被管理
    │ ├── srs_app_coworkers.cpp
    │ ├── srs_app_coworkers.hpp SrsCoWorkers For origin cluster
    │ ├── srs_app_dash.cpp
    │ ├── srs_app_dash.hpp SrsDash 流媒体DASH业务 The MPEG-DASH encoder,transmux RTMP to DASH.
    │ ├── srs_app_dvr.cpp
    │ ├── srs_app_dvr.hpp SrsDvr 录制RTMP流程flv或者mp4⽂件
    │ ├── srs_app_edge.cpp
    │ ├── srs_app_edge.hpp SrsEdgeRtmpUpstream 边缘节点业务,⽐如从源站拉流到边缘,边缘回溯到源站
    │ ├── srs_app_empty.cpp
    │ ├── srs_app_empty.hpp 没有内容
    │ ├── srs_app_encoder.cpp
    │ ├── srs_app_encoder.hpp SrsEncoder 可以使⽤多个ffmpeg来转换指定的流,最终调⽤SrsFFMPEG来转流
    │ ├── srs_app_ffmpeg.cpp
    │ ├── srs_app_ffmpeg.hpp SrsFFMPEG 使⽤ffmpeg来转换流
    │ ├── srs_app_forward.cpp
    │ ├── srs_app_forward.hpp SrsForwarder 将流转发到其他服务器
    │ ├── srs_app_fragment.cpp
    │ ├── srs_app_fragment.hpp SrsFragment 表示⼀个分⽚,如HLS分⽚、DVR分⽚或DASH分⽚。它是⼀个媒体⽂件,例如FLV或MP4,有持续时间。
    │ ├── srs_app_hds.cpp
    │ ├── srs_app_hds.hpp SrsHds 将RTMP转成Adobe HDS流
    │ ├── srs_app_heartbeat.cpp
    │ ├── srs_app_heartbeat.hpp SrsHttpHeartbeat HHTP⼼跳
    │ ├── srs_app_hls.cpp
    │ ├── srs_app_hls.hpp SrsHls HLS业务,Transmux RTMP stream to HLS(m3u8 andts).
    │ ├── srs_app_hourglass.cpp
    │ ├── srs_app_hourglass.hpp SrsHourGlass 滴答tick的处理程序
    │ ├── srs_app_http_api.cpp
    │ ├── srs_app_http_api.hpp SrsHttpApi HTTP业务API
    │ ├── srs_app_http_client.cpp
    │ ├── srs_app_http_client.hpp 没有内容
    │ ├── srs_app_http_conn.cpp
    │ ├── srs_app_http_conn.hpp SrsHttpConn,HTTP连接,继承于SrsConnection
    │ ├── srs_app_http_hooks.cpp
    │ ├── srs_app_http_hooks.hpp SrsHttpHooks HTTP勾⼦,HTTP回调API
    │ ├── srs_app_http_static.cpp
    │ ├── srs_app_http_static.hpp SrsHttpStaticServer HTTP静态服务器实例,为HTTP静态⽂件和FLV/MP4视频点播服务
    │ ├── srs_app_http_stream.cpp
    │ ├── srs_app_http_stream.hpp SrsHttpStreamServer HTTP直播流服务,⽀持FLV/TS/MP3/AAC流
    │ ├── srs_app_ingest.cpp
    │ ├── srs_app_ingest.hpp SrsIngester摄取⽂件/流/设备,⽤FFMPEG编码(可选), 通过RTMP推送到SRS(或其他RTMP服务器)
    │ ├── srs_app_listener.cpp
    │ ├── srs_app_listener.hpp SrsTcpListener SrsUdpListener TCP/UDP监听器
    │ ├── srs_app_log.cpp
    │ ├── srs_app_log.hpp SrsFastLog ⽇志
    │ ├── srs_app_mpegts_udp.cpp
    │ ├── srs_app_mpegts_udp.hpp SrsMpegtsOverUdpThe mpegts over udp stream caster
    │ ├── srs_app_ng_exec.cpp
    │ ├── srs_app_ng_exec.hpp SrsNgExec
    │ ├── srs_app_pithy_print.cpp
    │ ├── srs_app_pithy_print.hpp SrsPithyPrint 收集信息,然后打印
    │ ├── srs_app_process.cpp
    │ ├── srs_app_process.hpp SrsProcess启动和停⽌进程,当被终⽌时调⽤cycle重新启动进程
    │ ├── srs_app_recv_thread.cpp
    │ ├── srs_app_recv_thread.hpp SrsHttpRecvThread HTTP数据读取,SrsPublishRecvThread推流数据读取,SrsQueueRecvThread从队列读取;SrsRecvThread封装的协程
    │ ├── srs_app_refer.cpp
    │ ├── srs_app_refer.hpp SrsRefer
    │ ├── srs_app_reload.cpp
    │ ├── srs_app_reload.hpp ISrsReloadHandler 重新读取配置⽂件的处理
    │ ├── srs_app_rtmp_conn.cpp
    │ ├── srs_app_rtmp_conn.hpp SrsRtmpConn RTMP连接
    │ ├── srs_app_rtsp.cpp
    │ ├── srs_app_rtsp.hpp SrsRtpConn RTSP连接,SrsRtspCaster RTSP业务
    │ ├── srs_app_security.cpp
    │ ├── srs_app_security.hpp SrsSecurity 安全限制,主要是限制url
    │ ├── srs_app_server.cpp
    │ ├── srs_app_server.hpp SrsServer SRS服务,对应的rtmp、rtsp、http-flv等等业务在这⾥启动
    │ ├── srs_app_source.cpp
    │ ├── srs_app_source.hpp SrsSource 对应⼀个源,⽀持多个SrsConsumer来拉流,SrsSourceManager管理源, SrsMetaCache⽤于源缓存Meta数据,SrsConsumer源的消费者,SrsGopCache GOP缓存
    │ ├── srs_app_statistic.cpp
    │ ├── srs_app_statistic.hpp SrsStatistic流统计
    │ ├── srs_app_st.cpp
    │ ├── srs_app_st.hpp SrsSTCoroutine协程相关
    │ ├── srs_app_thread.cpp
    │ ├── srs_app_thread.hpp SrsCoroutineManager协程管理
    │ ├── srs_app_utility.cpp
    │ └── srs_app_utility.hpp ⼯具类:SrsPlatformInfo、SrsNetworkDevices、SrsMemInfo、SrsDiskStat等等
    |── core
    │ ├── srs_core_autofree.cpp
    │ ├── srs_core_autofree.hpp 通过栈上的⽅式构建⾃动释放堆申请的对象,这个设计还是⾮常值得我们学习
    │ ├── srs_core.cpp
    │ ├── srs_core.hpp 版本相关的⼀些信息
    │ ├── srs_core_mem_watch.cpp
    │ ├── srs_core_mem_watch.hpp 内存监测接⼝
    │ ├── srs_core_performance.cpp
    │ ├── srs_core_performance.hpp 性能测试相关
    │ ├── srs_core_time.cpp
    │ ├── srs_core_time.hpp 时间单位相关
    │ ├── srs_core_version3.cpp
    │ └── srs_core_version3.hpp 版本信息
    |── kernel        // ⾳视频格式相关的
    │ ├── srs_kernel_aac.cpp
    │ ├── srs_kernel_aac.hpp SrsAacTransmuxer 合成AAC⾳频流,带ADTS header
    │ ├── srs_kernel_balance.cpp
    │ ├── srs_kernel_balance.hpp SrsLbRoundRobin负载均衡,⽤于边缘节点拉流和其他多个服务器的功能
    │ ├── srs_kernel_buffer.cpp
    │ ├── srs_kernel_buffer.hpp SrsBuffer读取字节的实⽤类
    │ ├── srs_kernel_codec.cpp
    │ ├── srs_kernel_codec.hpp 编码器相关,包括视频和⾳频,⾮常核⼼的⽂件;SrsFlvVideo⽤来检测FLV的video tag对应内容;SrsFlvAudio⽤来检测FLV的audio tag对应内容;SrsMaxNbSamples 256表示video最⼤的NALUS个数,audio最⼤的packet数量;SrsFrame存储帧,SrsAudioFrame 存储AAC帧,SrsVideoFrame存储视频帧;SrsFormat编码器格式,包含了⼀个或者多个流,⽐如为RTMP format时,包含⼀个视频和⼀个⾳频帧。先猜测推流时的数据实例是保存在SrsFormat?
    │ ├── srs_kernel_consts.cpp
    │ ├── srs_kernel_consts.hpp SRS的常量定义,⽐如播放的标记#define SRS_CONSTS_LOG_PLAY "PLA";发布的标记#define SRS_CONSTS_LOG_CLIENT_PUBLISH "CPB";SRS_CONSTS_HTTP_XXX等HTTP响应码;SRS_CONSTS_RTSP_XXX响应码等等。
    │ ├── srs_kernel_error.cpp
    │ ├── srs_kernel_error.hpp 返回值常量定义,ERROR_XXX;SrsCplxError 异常类
    │ ├── srs_kernel_file.cpp
    │ ├── srs_kernel_file.hpp ⽂件的读写,SrsFileWriter⽂件写⼊器,SrsFileReader⽂件读取器
    │ ├── srs_kernel_flv.cpp
    │ ├── srs_kernel_flv.hpp FLV SrsFlvDecoder解析,SrsFlvTransmuxer将RTMP转成FLV流;SrsSharedPtrMessage对应RTMP的消息
    │ ├── srs_kernel_io.cpp
    │ ├── srs_kernel_io.hpp IO读写接⼝类
    │ ├── srs_kernel_log.cpp
    │ ├── srs_kernel_log.hpp ⽇志相关
    │ ├── srs_kernel_mp3.cpp
    │ ├── srs_kernel_mp3.hpp SrsMp3Transmuxer将RTMP转成MP3流
    │ ├── srs_kernel_mp4.cpp
    │ ├── srs_kernel_mp4.hpp SrsMp4Encoder MP4复⽤器;
    │ ├── srs_kernel_stream.cpp
    │ ├── srs_kernel_stream.hpp SrsSimpleStream⽤vector实现的简单的字节append类,主要在hls和http中使⽤,将来需要进⾏改进。
    │ ├── srs_kernel_ts.cpp
    │ ├── srs_kernel_ts.hpp SrsTsTransmuxer将RTMP流转成http-ts流,该⽂件实现了ts格式相关的接⼝
    │ ├── srs_kernel_utility.cpp
    │ └── srs_kernel_utility.hpp ⼯具函数,⽐如bool srs_string_ends_with(std::string str,std::string flag)
    |── libs
    │ ├── srs_lib_bandwidth.cpp
    │ ├── srs_lib_bandwidth.hpp SrsBandwidthClient srs-librtmp 客户端带宽统计
    │ ├── srs_librtmp.cpp
    │ ├── srs_librtmp.hpp srs提供的客户端rtmp库
    │ ├── srs_lib_simple_socket.cpp
    │ └── srs_lib_simple_socket.hpp SimpleSocketStream rtmp客户端的socket封装
    |── main
    │ ├── srs_main_ingest_hls.cpp 拉取hls发布到rtmp流媒体服务器
    │ ├── srs_main_mp4_parser.cpp MP4 box解析
    │ └── srs_main_server.cpp srs流媒体服务器主⼊⼝
    |── protocol 流媒体协议相关的协议都在这⾥
    │ ├── srs_http_stack.cpp
    │ ├── srs_http_stack.hpp HTTP协议
    │ ├── srs_protocol_amf0.cpp
    │ ├── srs_protocol_amf0.hpp Amf0解析
    │ ├── srs_protocol_format.cpp
    │ ├── srs_protocol_format.hpp SrsRtmpFormat继承了SrsFormat, 代表RTMP格式
    │ ├── srs_protocol_io.cpp
    │ ├── srs_protocol_io.hpp 协议数据读取的IO封装接⼝,⽐如ISrsProtocolReadWriter
    │ ├── srs_protocol_json.cpp
    │ ├── srs_protocol_json.hpp json类
    │ ├── srs_protocol_kbps.cpp
    │ ├── srs_protocol_kbps.hpp ⽐特率统计相关
    │ ├── srs_protocol_stream.cpp
    │ ├── srs_protocol_stream.hpp 流读取,从ISrsReader读取数据到buffer⾥⾯
    │ ├── srs_protocol_utility.cpp
    │ ├── srs_protocol_utility.hpp 协议⼯具函数
    │ ├── srs_raw_avc.cpp
    │ ├── srs_raw_avc.hpp SrsRawH264Stream H264裸流解析,SrsRawAacStream AAC 裸流解析
    │ ├── srs_rtmp_handshake.cpp
    │ ├── srs_rtmp_handshake.hpp RTMP握⼿,包括SrsSimpleHandshake和SrsComplexHandshake
    │ ├── srs_rtmp_msg_array.cpp
    │ ├── srs_rtmp_msg_array.hpp SrsMessageArray消息数组
    │ ├── srs_rtmp_stack.cpp
    │ ├── srs_rtmp_stack.hpp RTMP协议栈
    │ ├── srs_rtsp_stack.cpp
    │ └── srs_rtsp_stack.hpp RTSP协议栈
    |── service
    │ ├── srs_service_conn.cpp
    │ ├── srs_service_conn.hpp ISrsConnection HTTP/RTMP/RTSP等对象的连接接⼝;IConnectionManager管理连接接⼝
    │ ├── srs_service_http_client.cpp
    │ ├── srs_service_http_client.hpp SrsHttpClient HTTP客户端
    │ ├── srs_service_http_conn.cpp
    │ ├── srs_service_http_conn.hpp HTTP连接 SrsHttpParser,SrsHttpMessage,SrsHttpResponseWriter,SrsHttpResponseReader
    │ ├── srs_service_log.cpp
    │ ├── srs_service_log.hpp SrsConsoleLog⽇志相关
    │ ├── srs_service_rtmp_conn.cpp
    │ ├── srs_service_rtmp_conn.hpp SrsBasicRtmpClient RTMP客户端类
    │ ├── srs_service_st.cpp
    │ ├── srs_service_st.hpp 对st-thread协程的封装
    │ ├── srs_service_utility.cpp
    │ └── srs_service_utility.hpp service组件的⼯具类
    └── utest
    ├── srs_utest_amf0.cpp
    ├── srs_utest_amf0.hpp
    ├── srs_utest_app.cpp
    ├── srs_utest_app.hpp
    ├── srs_utest_avc.cpp
    ├── srs_utest_avc.hpp
    ├── srs_utest_config.cpp
    ├── srs_utest_config.hpp
    ........... 还有其他utest⽂件,这⾥忽略
    
    8 directories, 203 files
    

    相关文章

      网友评论

          本文标题:音视频流媒体开发【五十八】SRS流媒体服务器1-SRS环境搭建

          本文链接:https://www.haomeiwen.com/subject/eftledtx.html