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 模塊
注意,如果需要支持 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;
}
如上圖表明已經加載 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
閱讀更多 愛新覺羅張三丰 的文章