徐高翔的个人网站

HLS视频服务器srs

2016-12-15

软硬件环境

  • ubuntu 16.04 64bit
  • srs 2.0release

SRS简介

SRS,即Simple-RTMP-Server,是一个开源的视频服务器,出自国人之手。release版本下载地址https://github.com/ossrs/srs/releases,目前的最高版本是v2.0-r2

源码编译

1
2
3
4
5
6
tar xvf v2.0-r2.tar.bz2
cd srs-2.0-r2/trunk
mkdir build
./configure --prefix=/home/longjing/srs/srs-2.0-r2/trunk/build --with-http-api --with-hls --with-http-server --with-ssl --with-stat --with-research --with-dvr --with-http-callback
make
make install

如果出现错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
rm -f LINUX_4.4.0-116-generic_DBG/md-pic.o; ln -s md.o LINUX_4.4.0-116-generic_DBG/md-pic.o
ld -shared -soname=libst.so.1 -lc LINUX_4.4.0-116-generic_DBG/sched-pic.o LINUX_4.4.0-116-generic_DBG/stk-pic.o LINUX_4.4.0-116-generic_DBG/sync-pic.o LINUX_4.4.0-116-generic_DBG/key-pic.o LINUX_4.4.0-116-generic_DBG/io-pic.o LINUX_4.4.0-116-generic_DBG/event-pic.o LINUX_4.4.0-116-generic_DBG/md-pic.o -o LINUX_4.4.0-116-generic_DBG/libst.so.1.9
LINUX_4.4.0-116-generic_DBG/sched-pic.o: In function `st_poll':
/root/srs/trunk/objs/st-1.9/sched.c:108: undefined reference to `__stack_chk_fail_local'
LINUX_4.4.0-116-generic_DBG/sync-pic.o: In function `st_utime':
/root/srs/trunk/objs/st-1.9/sync.c:70: undefined reference to `__stack_chk_fail_local'
LINUX_4.4.0-116-generic_DBG/io-pic.o: In function `_st_io_init':
/root/srs/trunk/objs/st-1.9/io.c:106: undefined reference to `__stack_chk_fail_local'
LINUX_4.4.0-116-generic_DBG/io-pic.o: In function `_st_netfd_new':
/root/srs/trunk/objs/st-1.9/io.c:166: undefined reference to `__stack_chk_fail_local'
LINUX_4.4.0-116-generic_DBG/io-pic.o: In function `st_netfd_poll':
/root/srs/trunk/objs/st-1.9/io.c:241: undefined reference to `__stack_chk_fail_local'
LINUX_4.4.0-116-generic_DBG/io-pic.o:/root/srs/trunk/objs/st-1.9/io.c:438: more undefined references to `__stack_chk_fail_local' follow
Makefile:356: recipe for target 'LINUX_4.4.0-116-generic_DBG/libst.so.1.9' failed
make[1]: *** [LINUX_4.4.0-116-generic_DBG/libst.so.1.9] Error 1
make[1]: Leaving directory '/root/srs/trunk/objs/st-1.9'
Makefile:441: recipe for target 'linux-debug' failed
make: *** [linux-debug] Error 2
build st-1.9 failed, ret=2

修改auto/depends.sh,搜索关键字st-1.9,找到

1
_ST_MAKE=linux-debug && _ST_EXTRA_CFLAGS="-DMD_HAVE_EPOLL

并将其修改为

1
_ST_MAKE=linux-debug && _ST_EXTRA_CFLAGS="-DMD_HAVE_EPOLL -fno-stack-protector"

再重新configuremake即可。

HLS配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
listen              2935;
max_connections 1000;
daemon on;
srs_log_tank file;
srs_log_level error;
srs_log_file ./objs/srs.log;

http_server {
enabled on;
listen 8081;
dir ./objs/nginx/html;
}

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;
hls_cleanup on;
hls_dispose 30;
hls_on_error continue;
hls_storage disk;
hls_wait_keyframe on;
hls_acodec aac;
hls_vcodec h264;
}
}

启动服务

1
./objs/srs -c conf/http.hls.conf

reload

1
killall -1 srs

开机自动启动

1
sudo ln -sf /home/xugaoxiang/build/etc/init.d/srs /etc/init.d/srs

修改srs中的ROOT和CONFIG

1
2
ROOT="/home/xugaoxiang/build"
CONFIG="./conf/http.hls.conf"

然后在命令行下执行

1
sudo update-rc.d srs defaults

可以先用

1
sudo /etc/init.d/srs start/stop/restart/status/reload

测试一下,正确无误后再重启系统。需要注意的是,只有runlevel是2、3、5、6才可以启动。需要的话,可以到/etc/rc*.d/下进行相应的修改。

如果想要删除srs服务,可以执行

1
sudo update-rc.d -f srs remove

javascript访问出现的跨域问题

修改源码src/app/srs_app_http_conn.cpp,大概是292行

1
2
3
hdr->set("Access-Control-Allow-Origin", "*");
hdr->set("Access-Control-Allow-Methods", "GET, POST, HEAD, PUT, DELETE");
hdr->set("Access-Control-Allow-Headers", "Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type");

并发测试工具

srs-bench是专门用来测试srs并发的工具,基于state-threads

1
2
3
4
git clone https://github.com/ossrs/srs-bench.git
cd srs-bench
./configure
make

参考资料

推荐文章(由hexo文章推荐插件驱动)

使用支付宝打赏
使用微信打赏

请博主喝咖啡!