教你从零搭建RMTP直播推流服务

之前由于向同学分享内容的时候发现手机端没有屏幕共享的应用,而直播软件大都需要实名注册,并且没有房间密码,隐私性很低,不符合需求,于是想自己开发一款,但苦于时间不允许,由此开个坑,慢慢填。

市面上的第三方直播推流供应商很多,集成起来也很方便,但是大都需要付费,而且价格不菲,于是乎决定自行搭建服务,同时加深对推流服务的理解。

开发环境

Linux,Nginx(OpenResty),nginx-rtmp-module

什么是OpenResty

基于官网的介绍,OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,详细介绍大家去官网看吧,这里就不多做解释了。

卸载或替换之前的nginx版本

如果你已经有安装过nginx,那么请备份好自己的数据,在此我将安装nginx,并展示如何卸载它,由于我自己的nginx里什么内容都没有,所以我进行完全卸载(以下命令全部在root下进行)。

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
31
32
33
34
35
36
37
38
39
#安装启动过程省略
//停止nginx
root@VM-53-84-ubuntu:/home/ubuntu# nginx -s stop

//查找nginx位置
root@VM-53-84-ubuntu:/home/ubuntu# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx

//使用apt移除卸载nginx
root@VM-53-84-ubuntu:/home/ubuntu# apt --purge remove nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'nginx' is not installed, so not removed
The following packages were automatically installed and are no longer required:
libgd3 libvpx3 libxpm4 libxslt1.1 linux-headers-4.4.0-124 linux-headers-4.4.0-124-generic linux-headers-4.4.0-127
linux-headers-4.4.0-127-generic linux-image-4.4.0-124-generic linux-image-4.4.0-127-generic linux-image-extra-4.4.0-124-generic
linux-image-extra-4.4.0-127-generic nginx-common nginx-core
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 25 not upgraded.

//查找nginx位置
root@VM-53-84-ubuntu:/home/ubuntu# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx

//删除/usr/sbin/nginx
root@VM-53-84-ubuntu:/home/ubuntu# rm -r /usr/sbin/nginx

//删除/etc/nginx
root@VM-53-84-ubuntu:/home/ubuntu# rm -r /etc/nginx

//删除/usr/share/nginx/
root@VM-53-84-ubuntu:/home/ubuntu# rm -r /usr/share/nginx/
root@VM-53-84-ubuntu:/home/ubuntu# whereis nginx
nginx:

//查看nginx进程
root@VM-53-84-ubuntu:/home/ubuntu# ps -ef | grep nginx
root 7970 7499 0 19:18 pts/0 00:00:00 grep --color=auto nginx

下载安装OpenResty

到官网下载最新的安装包并安装,具体过程请参考OpenResty-安装

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//下载nginx压缩包
root@VM-53-84-ubuntu:/home/ubuntu/RMTP# wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
--2018-07-18 19:21:35-- https://openresty.org/download/openresty-1.13.6.2.tar.gz
Resolving openresty.org (openresty.org)... 120.24.93.123
Connecting to openresty.org (openresty.org)|120.24.93.123|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4635916 (4.4M) [application/x-gzip]
Saving to: ‘openresty-1.13.6.2.tar.gz’

openresty-1.13.6.2.tar.gz 100%[====================================================================>] 4.42M 5.13MB/s in 0.9s

2018-07-18 19:21:36 (5.13 MB/s) - ‘openresty-1.13.6.2.tar.gz’ saved [4635916/4635916]

//解压
root@VM-53-84-ubuntu:/home/ubuntu/RMTP# tar -zxf openresty-1.13.6.2.tar.gz

//安装必要类库
root@VM-53-84-ubuntu:/home/ubuntu/RMTP# apt-get install libpcre3-dev \
> libssl-dev perl make build-essential curl
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version (12.1ubuntu2).
libpcre3-dev is already the newest version (2:8.38-3.1).
make is already the newest version (4.1-6).
curl is already the newest version (7.47.0-1ubuntu2.8).
libssl-dev is already the newest version (1.0.2g-1ubuntu4.13).
perl is already the newest version (5.22.1-9ubuntu0.5).
The following packages were automatically installed and are no longer required:
libgd3 libvpx3 libxpm4 libxslt1.1 linux-headers-4.4.0-124 linux-headers-4.4.0-124-generic linux-headers-4.4.0-127
linux-headers-4.4.0-127-generic linux-image-4.4.0-124-generic linux-image-4.4.0-127-generic linux-image-extra-4.4.0-124-generic
linux-image-extra-4.4.0-127-generic nginx-common nginx-core
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 25 not upgraded.

//进入openresty的文件夹
root@VM-53-84-ubuntu:/home/ubuntu/RMTP# cd openresty-1.13.6.2

//生成编译配置文件
root@VM-53-84-ubuntu:/home/ubuntu/RMTP/openresty-1.13.6.2# ./configure
platform: linux (linux)
cp -rp bundle/ build
cd build
cd LuaJIT-2.1-20180420
INFO: found -msse4.2 in cc.
make TARGET_STRIP=@: CCDEBUG=-g XCFLAGS='-DLUAJIT_ENABLE_LUA52COMPAT -msse4.2' CC=cc PREFIX=/usr/local/openresty/luajit
==== Building LuaJIT 2.1.0-beta3 ====
make -C src
make[1]: Entering directory '/home/ubuntu/RMTP/openresty-1.13.6.2/build/LuaJIT-2.1-20180420/src'
HOSTCC host/minilua.o
HOSTLINK host/minilua
DYNASM host/buildvm_arch.h
//省略一部分输出。。。。。
creating objs/Makefile

Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library

nginx path prefix: "/usr/local/openresty/nginx"
nginx binary file: "/usr/local/openresty/nginx/sbin/nginx"
nginx modules path: "/usr/local/openresty/nginx/modules"
nginx configuration prefix: "/usr/local/openresty/nginx/conf"
nginx configuration file: "/usr/local/openresty/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/openresty/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/openresty/nginx/logs/error.log"
nginx http access log file: "/usr/local/openresty/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

cd ../..
Type the following commands to build and install:
make
make install
root@VM-53-84-ubuntu:/home/ubuntu/RMTP/openresty-1.13.6.2# make //编译
//省略一部分输出。。。。。
objs/addon/src/ngx_stream_lua_ssl.o \
objs/addon/src/ngx_stream_lua_balancer.o \
objs/addon/src/ngx_stream_lua_logby.o \
objs/addon/src/ngx_stream_lua_prereadby.o \
objs/ngx_modules.o \
-L/home/ubuntu/RMTP/openresty-1.13.6.2/build/luajit-root/usr/local/openresty/luajit/lib -L/home/ubuntu/RMTP/openresty-1.13.6.2/build/luajit-root/usr/local/openresty/luajit/lib -Wl,-rpath,/usr/local/openresty/luajit/lib -Wl,-E -Wl,-E -ldl -lpthread -lcrypt -L/home/ubuntu/RMTP/openresty-1.13.6.2/build/luajit-root/usr/local/openresty/luajit/lib -lluajit-5.1 -lm -ldl -L/home/ubuntu/RMTP/openresty-1.13.6.2/build/luajit-root/usr/local/openresty/luajit/lib -lluajit-5.1 -lm -ldl -lpcre -lssl -lcrypto -ldl -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/openresty/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/openresty/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/openresty/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/openresty/nginx/logs/error.log|" \
< docs/man/nginx.8 > objs/nginx.8
make[2]: Leaving directory '/home/ubuntu/RMTP/openresty-1.13.6.2/build/nginx-1.13.6'
make[1]: Leaving directory '/home/ubuntu/RMTP/openresty-1.13.6.2/build/nginx-1.13.6'

//编译安装
root@VM-53-84-ubuntu:/home/ubuntu/RMTP/openresty-1.13.6.2# make install
|| mkdir -p '/usr/local/openresty/nginx/logs'
test -d '/usr/local/openresty/nginx/html' \
|| cp -R docs/html '/usr/local/openresty/nginx'
test -d '/usr/local/openresty/nginx/logs' \
|| mkdir -p '/usr/local/openresty/nginx/logs'
make[2]: Leaving directory '/home/ubuntu/RMTP/openresty-1.13.6.2/build/nginx-1.13.6'
make[1]: Leaving directory '/home/ubuntu/RMTP/openresty-1.13.6.2/build/nginx-1.13.6'
mkdir -p /usr/local/openresty/site/lualib /usr/local/openresty/site/pod /usr/local/openresty/site/manifest
ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty

//为了方便,软链接nginx
root@VM-53-84-ubuntu:/home/ubuntu/RMTP/openresty-1.13.6.2# ln -s /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx

//启动
root@VM-53-84-ubuntu:/home/ubuntu/RMTP/openresty-1.13.6.2# nginx

//验证是否启动成功
root@VM-53-84-ubuntu:/home/ubuntu/RMTP/openresty-1.13.6.2# curl 111.231.243.184
<!DOCTYPE html>
<html>
<head>
<title>Welcome to OpenResty!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to OpenResty!</h1>
<p>If you see this page, the OpenResty web platform is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="https://openresty.org/">openresty.org</a>.<br/></p>

<p><em>Thank you for flying OpenResty.</em></p>
</body>
</html>

下载安装nginx的RMTP模块nginx-rtmp-module

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
root@VM-53-84-ubuntu:/home/ubuntu/RMTP/openresty-1.13.6.2# nginx -s stop
root@VM-53-84-ubuntu:/home/ubuntu/RMTP/openresty-1.13.6.2# ./configure --add-module=/home/ubuntu/RTTMP/nginx-rtmp-module-master
platform: linux (linux)
cp -rp bundle/ build
cd build
cd LuaJIT-2.1-20180420
INFO: found -msse4.2 in cc.
make TARGET_STRIP=@: CCDEBUG=-g XCFLAGS='-DLUAJIT_ENABLE_LUA52COMPAT -msse4.2' CC=cc PREFIX=/usr/local/openresty/luajit
==== Building LuaJIT 2.1.0-beta3 ====
make -C src
make[1]: Entering directory '/home/ubuntu/RMTP/openresty-1.13.6.2/build/LuaJIT-2.1-20180420/src'
HOSTCC host/minilua.o
HOSTLINK host/minilua
DYNASM host/buildvm_arch.h
HOSTCC host/buildvm.o
HOSTCC host/buildvm_asm.o
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile
//省略一部分输出。。。。。
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library

nginx path prefix: "/usr/local/openresty/nginx"
nginx binary file: "/usr/local/openresty/nginx/sbin/nginx"
nginx modules path: "/usr/local/openresty/nginx/modules"
nginx configuration prefix: "/usr/local/openresty/nginx/conf"
nginx configuration file: "/usr/local/openresty/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/openresty/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/openresty/nginx/logs/error.log"
nginx http access log file: "/usr/local/openresty/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

cd ../..
Type the following commands to build and install:
make
make install
root@VM-53-47-ubuntu:/home/ubuntu/RMTP/openresty-1.13.6.2# make
//省略一部分输出。。。。。
objs/addon/dash/ngx_rtmp_mp4.o \
objs/addon/nginx-rtmp-module-master/ngx_rtmp_stat_module.o \
objs/addon/nginx-rtmp-module-master/ngx_rtmp_control_module.o \
objs/ngx_modules.o \
-L/home/ubuntu/RMTP/openresty-1.13.6.2/build/luajit-root/usr/local/openresty/luajit/lib -L/home/ubuntu/RMTP/openresty-1.13.6.2/build/luajit-root/usr/local/openresty/luajit/lib -Wl,-rpath,/usr/local/openresty/luajit/lib -Wl,-E -Wl,-E -ldl -lpthread -lcrypt -L/home/ubuntu/RMTP/openresty-1.13.6.2/build/luajit-root/usr/local/openresty/luajit/lib -lluajit-5.1 -lm -ldl -L/home/ubuntu/RMTP/openresty-1.13.6.2/build/luajit-root/usr/local/openresty/luajit/lib -lluajit-5.1 -lm -ldl -lpcre -lssl -lcrypto -ldl -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/openresty/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/openresty/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/openresty/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/openresty/nginx/logs/error.log|" \
< docs/man/nginx.8 > objs/nginx.8
make[2]: Leaving directory '/home/ubuntu/RMTP/openresty-1.13.6.2/build/nginx-1.13.6'
make[1]: Leaving directory '/home/ubuntu/RMTP/openresty-1.13.6.2/build/nginx-1.13.6'

//拷贝新生成的nginx文件到OpenResty安装目录下
root@VM-53-47-ubuntu:/home/ubuntu/RMTP/openresty-1.13.6.2# cp build/nginx-1.13.6/objs/nginx /usr/local/openresty/nginx/sbin

接着修改nginx.conf配置文件,修改后的nginx.conf配置文件如下

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
root@ubuntu:/home/kaithmy/RTMP# cat /usr/local/openresty/nginx/conf/nginx.conf

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
use epoll;# 选择epoll模型可以达到最佳的IO性能
worker_connections 1024;
}

rtmp { #RTMP服务
server {
listen 1935; #//服务端口
chunk_size 4096; #//数据传输块的大小
application vod {
play /opt/video; #//视频文件存放位置。
}
application rtmplive {
live on;
#为 rtmp 引擎设置最大连接数。默认为 off
max_connections 1024;
}
application live{ #直播
live on;
hls on; #这个参数把直播服务器改造成实时回放服务器。
wait_key on; #对视频切片进行保护,这样就不会产生马赛克了。
hls_path /opt/video/hls; #切片视频文件存放位置。
hls_fragment 600s; #设置HLS片段长度。
hls_playlist_length 10m; #设置HLS播放列表长度,这里设置的是10分钟。
hls_continuous on; #连续模式。
hls_cleanup on; #对多余的切片进行删除。
hls_nested on; #嵌套模式。
}
}
}

http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}

location /stat.xsl {
root /home/kaithmy/RTMP/nginx-rtmp-module-master/;
}

location / {
root html;
index index.html index.htm;
}

location /live { #这里也是需要添加的字段。
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /opt/video/hls;
expires -1;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

测试

  1. 首先测试推流服务
    启动nginx后,我们往服务器的/opt/video目录下上传一个flv视频,如我上传的是01a.flv,接着我们使用potplayer打开我们的推流地址rtmp://119.29.162.191:1935/vod/01a.flv,接着我们就可以观看我们上传的视频了

  2. 接着测试直播功能
    测试直播功能需要用到OBS Studio,一款非常好用的软件,我们在软件的设置里设置好我们的推流地址,如我的是rtmp://119.29.162.191/live,点击开始推流后就可以开始直播了,其他人则可以通过地址http://119.29.162.191/live/index.m3u8观看我们的直播

    由于开启了回放,所以直播的会被缓存下来,并且以分片形式存放,具体参数与配置参考nginx.conf

参考:

  1. https://blog.52itstyle.com/archives/2254/
  2. https://blog.52itstyle.com/archives/1764/
博主 wechat
冰糖可乐柠檬茶,可爱的女士喝点啥?喝点啥?
喜欢的话就给予一点支持吧(*/ω\*)