Nginx+RTMP
直播服务器集群1 直播服务器搭建
1.1 系统环境
操作系统:Centos7.3
系统版本:CentOS Linux release 7.3.1611 (Core)
Nginx 版本:1.12
1.2 RTMP 模块安装
Git clone
添加到 Nginx 模块中编译 --add-module
例如:
![Nginx+RTMP 直播服务器集群](http://p2.ttnews.xyz/loading.gif)
上图表明系统 Nginx 已经加载了 RTMP 模块
注意,如果需要支持 H.264,需额外安装 nginx_mod_h264_streaming 模块
wget
需 要 注 释 掉 nginx_mod_h264_streaming-
2.2.7/src/ngx_http_streaming_module.c 的以下内容:
if (r->zero_in_uri)
{
return NGX_DECLINED;
}
![Nginx+RTMP 直播服务器集群](http://p2.ttnews.xyz/loading.gif)
如上图表明已经加载 h264 模块
1.3 配置 RTMP
在 Nginx 中配置
rtmp { server {
listen 1935; application myapp {
live on;
}
record all; record_path /tmp/av;
record_suffix %Y%m%d%H%M%S.mp4; record_unique on;
record_append on;
}
}
参数简要说明:
record
录制相关
语法: record [off|all|audio|video|keyframes|manual]*
· off - 不记录在所有
· all - 音频和视频(一切)
· audio -音频
· video - 视频
· keyframes -唯一的关键帧视频
· manual - 从来没有自动启动记录仪,使用控制界面来启动/停止
record_path
指定录制文件的路径
record_suffix
录制文件的格式
语法: record_suffix value
该参数默认为 Flv 格式,即不定义的话录制文件格式为.Flv
record_suffix %Y%m%d%H%M%S.mp4 表示录制文件为 mp4 格式
record_unique
是否打开追加当前的时间戳,默认为 off
record_append
切换文件追加模式以上
application appname {
……
}
配置完成后,重启 nginx,即可进行推拉流测试
例如:推流地址 rtmp://localhost:1935/myapp/teststream1
拉流地址 rtmp://localhost:1935/myapp/teststream1
1.4 配置 HLS
rtmp { server {
listen 1935;
application hls { live on;
hls on;
hls_path /tmp/hls;
record all;
record_path /usr/local/nginx/html/liverecord; record_suffix %Y%m%d%H%M%S.mp4; record_unique on;
hls_fragment 4s; hls_playlist_length 12s; hls_nested on; hls_cleanup off;
hls_fragment_naming system; wait_key on;
sync 10ms;
}
}
}
http{ server {
listen 8008; server_name localhost; location /hls {
types {
application/vnd.apple.mpegurl m3u8; video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
}
参数说明:
hls on; 表示打开回看(HLS)
hls_path; hls 文件路径,即.m3u8 以及 ts 切片文件路径
完成以上 rtmp{……} 及 http {……} 配置后,即可进行推拉流例如:
推流地址: rtmp://localhost:1935/hls/mystream1
拉流地址: rtmp://localhost:8008/hls/mystream1/index.m3u8
2 直播服务器转推集群
2.1 集群配置
Nginx+RTMP 可以实现直播、点播,但单机性能有限,就需要集群。本方案采用
RTMP 推流服务器公用,辅助多节点代理服务器,用户通过节点拉流请求代理转发到推流服务器。
配置示例如下:
推流服务器 A 配置(192.168.1.152)(192.168.1.182):
rtmp {
server {
listen 1935;
chunk_size 4000;
application myapp { live on;
record all; record_path /tmp/av; record_suffix .mp4; record_unique on; record_append on;
push rtmp://192.168.1.176:1935/myapp; push rtmp://192.168.1.183:1935/myapp;
}
application hls {
live on; hls on;
hls_path /tmp/hls; record all;
record_path /usr/local/nginx/html/liverecord; record_suffix %Y%m%d%H%M%S.mp4; record_unique on;
hls_fragment 4s; hls_playlist_length 12s; hls_nested on; hls_cleanup off;
hls_fragment_naming system; wait_key on;
sync 10ms;
push rtmp://192.168.1.176:1935/hls; push rtmp://192.168.1.183:1935/hls;
}
}
}
http{
server {
listen 8008; location /hls {
# Serve HLS fragments types {
application/vnd.apple.mpegurl m3u8; video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
}
}
拉流服务器(192.168.1.176)、(192.168.1.183):
rtmp {
server {
listen 1935;
chunk_size 4000;
application myapp {
# enable live streaming live on;
}
application hls { live on;
hls on;
hls_path /tmp/hls; hls_fragment 4s; hls_playlist_length 12s; hls_nested on; hls_cleanup off;
hls_fragment_naming system; wait_key on;
sync 10ms;
}
}
}
http {
server {
listen 8080; location /hls {
# Serve HLS fragments types {
application/vnd.apple.mpegurl m3u8; video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
}
}
RTMP 流:
推流:rtmp://192.168.1.152:1935/live/stream1
对应拉流地址:
rtmp://192.168.1.152:1935/live/stream1 (本地拉流)
rtmp://192.168.1.176:1935/live/stream1 (拉流)
HLS 流:
推流:rtmp://192.168.1.152:1935/hlslive/stream2
对应拉流地址:
http://192.168.1.152:8008/hlslive/stream2/index.m3u8 (本地拉流)
:8080/hlslive/stream2/index.m3u8 (代理节点拉流)
2.2 集群测试
在推流服务器上使用 OBS 推流,然后在拉流服务器上通过 VLC 拉流
RTMP 推流地址:
rtmp://192.168.1.152:1935/myapp/mystream1
RTMP 拉流地址:
rtmp:// 192.168.1.176:1935/myapp/mystream1 rtmp:// 192.168.1.183:1935/myapp/mystream1
HLS 推流地址:
rtmp://192.168.1.152:1935/hls/mystream2
拉流地址:
http://192.168.1.176:8008/hls/mystream2/index.m3u8 http://192.168.1.183:8008/hls/mystream2/index.m3u8
3 回调
3.1 RTMP 推流回调
rtmp {server {listen 1935;#notify_method get;application hls { live on;publish_notify on; hls on;hls_path /tmp/hls; hls_cleanup off;on_publish http://192.168.1.182:8080/publish; on_publish_done http://192.168.1.182:8080/publish_done;}}
3.1.1 on_pulish
推流开始时起效
"app" = "hls""flashver" = "FMLE/3.0 (compatible; FMSc/1.0)" "swfurl" = "rtmp://192.168.1.152:1935/hls" "tcurl" = "rtmp://192.168.1.152:1935/hls" "pageurl" = """addr" = "192.168.1.168""clientid" = "65" "call" = "publish" "name" = "11520301""type" = "live"参数:
3.1.2 on_publish_done
推流结束时起效
参数:
"app" = "hls""flashver" = "FMLE/3.0 (compatible; FMSc/1.0)" "swfurl" = "rtmp://192.168.1.152:1935/hls" "tcurl" = "rtmp://192.168.1.152:1935/hls" "pageurl" = """addr" = "192.168.1.168""clientid" = "68""call" = "publish_done" "name" = "11520301"
3.2 RTMP 拉流回调
rtmp {server {listen 1935;#notify_method get;application hls {live on;publish_notify on;#notify_method get; hls on;hls_path /tmp/hls; hls_cleanup off;on_play http://192.168.1.182:8080/play; on_play_done http://192.168.1.182:8080/play_done;}}}
3.2.1 on_play
拉流开始时起效
参数:
"app" = "hls""flashver" = "LNX 9,0,124,2" "swfurl" = """tcurl" = "rtmp://192.168.1.152:1935/hls" "pageurl" = """addr" = "192.168.1.168""clientid" = "127" "call" = "play" "name" = "myplay""start" = "4294965296""duration" = "0""reset" = "0"
3.2.2 on_play_done
拉流结束时起效
"app" = "hls""flashver" = "LNX 9,0,124,2" "swfurl" = """tcurl" = "rtmp://192.168.1.152:1935/hls" "pageurl" = """addr" = "192.168.1.168""clientid" = "127" "call" = "play_done" "name" = "myplay"参数:
3.3 录制结束回调
rtmp {server {listen 1935;#notify_method get;application live {live on;recorder myvideo { record all;record_path /tmp/hls;
record_suffix .mp4; record_notify on;on_record_done http://192.168.1.182:8080/record_done;}}}
3.3.1 on_record_done
在录制结束时起效
参数:
"app" = "live""flashver" = "FMLE/3.0 (compatible; FMSc/1.0)" "swfurl" = "rtmp://192.168.1.152:1935/live" "tcurl" = "rtmp://192.168.1.152:1935/live" "pageurl" = """addr" = "192.168.1.168""clientid" = "157" "call" = "record_done""recorder" = "myvideo""name" = "bbbbbb"
"path" = "/tmp/hls/bbbbbb.mp4""myvideo" = "bbbbbb"
根据目前业务看,主要考虑推流回调及录制结束回调,即需要业务处理推流开始、推流结束、录制结束的回调
on_publish on_publish_done on_record_done
閱讀更多 愛新覺羅張三丰 的文章