使用开源的SRS 搭建一个直播服务

SRS 是一个开源的,国产的直播服务。可以去官网下载:
官网

环境

操作系统:centos 7
推流软件:obs 或 ffmpeg
拉流软件:ffplay 或 vlc

安装

如果是 centos 6 可以直接下载这个进行安装

# wget -c http://ossrs.net/srs.release/releases/files/SRS-CentOS6-x86_64-2.0.239.zip

# unzip SRS-CentOS6-x86_64-2.0.239.zip
# cd SRS-CentOS6-x86_64-2.0.239
# chmod +x INSTALL
# ./INSTALL

安装在/usr/local/srs目录中,可以直接使用

/etc/init.d/srs start

启动

centos 7 需要编译

[root@adsl-172-10-100-120 opt]# wget -c https://github.com/ossrs/srs/archive/v2.0-r1.tar.gz
[root@adsl-172-10-100-120 opt]# tar zxvf v2.0-r1.tar.gz
[root@adsl-172-10-100-120 opt]# cd srs-2.0-r1/

然后进入到 trunk 目录

[root@adsl-172-10-100-120 srs-2.0-r1]# cd trunk/
[root@adsl-172-10-100-120 trunk]# ls
3rdparty  auto  conf  configure  doc  etc  ide  modules  research  scripts  src

使用 help 可以查看编译帮助文档

[root@adsl-172-10-100-120 trunk]# ./configure --help

比如我这里编译全部的模块安装到/usr/local/srs目录

[root@adsl-172-10-100-120 trunk]# ./configure --full --prefix=/usr/local/srs

然后

make && make install

进入 /usr/local/srs

[root@adsl-172-10-100-120 srs]# pwd
/usr/local/srs
[root@adsl-172-10-100-120 srs]# ./objs/
nginx/ srs
[root@adsl-172-10-100-120 srs]# ./objs/srs -c ./conf/srs.conf
[2017-05-02 17:22:56.900][trace][1949][0] XCORE-SRS/2.0.239(ZhouGuowen)
[2017-05-02 17:22:56.900][trace][1949][0] config parse complete
[2017-05-02 17:22:56.900][trace][1949][0] write log to file ./objs/srs.log
[2017-05-02 17:22:56.900][trace][1949][0] you can: tailf ./objs/srs.log
[2017-05-02 17:22:56.900][trace][1949][0] @see: https://github.com/ossrs/srs/wiki/v1_CN_SrsLog

##推流

➜  Downloads ffmpeg -i gopro.mp4 -vcodec libx264 -acodec aac -f flv rtmp://172.10.100.120/live/lol

拉流

然后 ffplay 播放

➜  ~ ffplay rtmp://172.10.100.120/live/lol
ffplay version 3.3 Copyright (c) 2003-2017 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.42)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
  libavutil      55. 58.100 / 55. 58.100
  libavcodec     57. 89.100 / 57. 89.100
  libavformat    57. 71.100 / 57. 71.100
  libavdevice    57.  6.100 / 57.  6.100
  libavfilter     6. 82.100 /  6. 82.100
  libavresample   3.  5.  0 /  3.  5.  0
  libswscale      4.  6.100 /  4.  6.100
  libswresample   2.  7.100 /  2.  7.100
  libpostproc    54.  5.100 / 54.  5.100
Input #0, flv, from 'rtmp://172.10.100.120/live/lol':    0B f=0/0
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.71.100
    server          : SRS/2.0.239(ZhouGuowen)
    srs_primary     : SRS/1.0release
    srs_authors     : winlin,wenjie.zhao
    server_version  : 2.0.239
  Duration: N/A, start: 0.023000, bitrate: N/A
    Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 128 kb/s
    Stream #0:1: Video: h264 (High), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc
   3.96 A-V:  0.082 fd=  22 aq=    0KB vq=    0KB sq=    0B f=0/0

srs 配置

[root@adsl-172-10-100-120 srs]# ls
conf  etc  objs
[root@adsl-172-10-100-120 srs]# cd conf/
[root@adsl-172-10-100-120 conf]# ls
bandwidth.conf    edge.conf                 hls.conf                  http.hooks.callback.conf  push.flv.conf               transcode2hls.audio.only.conf
console.conf      edge.token.traverse.conf  http.aac.live.conf        http.mp3.live.conf        push.mpegts.over.udp.conf   transform.edge.conf
demo.19350.conf   ffmpeg.transcode.conf     http.flv.live.conf        http.server.conf          push.rtsp.conf
demo.conf         forward.master.conf       http.flv.live.edge1.conf  http.ts.live.conf         realtime.conf
dvr.path.conf     forward.slave.conf        http.flv.live.edge2.conf  ingest.conf               rtmp.conf
dvr.segment.conf  full.conf                 http.heartbeat.conf       mac.dev.conf              security.deny.publish.conf
dvr.session.conf  hds.conf                  http.hls.conf             origin.conf               srs.conf

默认的配置文件是 srs.conf

[root@adsl-172-10-100-120 conf]# cat srs.conf
# main config for srs.
# @see full.conf for detail config.

listen              1935;
max_connections     1000;
srs_log_tank        file;
srs_log_file        ./objs/srs.log;
http_api {
    enabled         on;
    listen          1985; #默认端口1985
}
http_server {
    enabled         on;
    listen          8080;
    dir             ./objs/nginx/html;
}
stats {
    network         0;
    disk            sda sdb xvda xvdb;
}
vhost __defaultVhost__ { #虚拟主机
}

如果你希望将推流的内容转换成 hls,可以参考

[root@adsl-172-10-100-120 conf]# cat hls.conf
# the config for srs to delivery hls
# @see https://github.com/ossrs/srs/wiki/v1_CN_SampleHLS
# @see full.conf for detail config.

listen              1935;
max_connections     1000;
daemon              off;
srs_log_tank        console;
vhost __defaultVhost__ {
    hls {
        enabled         on;
        hls_fragment    10;
        hls_window      60;
        hls_path        ./objs/nginx/html;
        hls_m3u8_file   [app]/[stream].m3u8;
        hls_ts_file     [app]/[stream]-[seq].ts;
    }
}

更多使用方法,参考[官方维基](https://github.com/ossrs/srs/wiki/v3_CN_Home)