2016年8月18日12:42:35 參照官方文檔https://github.com/arut/nginx-rtmp-module/wiki/Directives
請注意這個是粗翻譯版,僅供參考,不是精校版 精校版后面才會更新
目錄
- 核心
- rtmp
- server
- listen
- application
- timeout
- ping
- ping_timeout
- max_streams
- ack_window
- chunk_size
- max_queue
- max_message
- buflen
- out_queue
- out_cork
- 訪問權限
- allow
- deny
Exec 一簇函數(進程往往要調用一種exec函數以執行另一個程序)
- exec_push
- exec_pull
- exec
- exec_options
- exec_static
- exec_kill_signal
- respawn
- respawn_timeout
- exec_publish
- exec_play
- exec_play_done
- exec_publish_done
- exec_record_done
- 直播
- live
- meta
- interleave
- wait_key
- wait_video
- publish_notify
- drop_idle_publisher
- sync
- play_restart
- idle_streams
- 錄像
- record
- record_path
- record_suffix
- record_unique
- record_append
- record_lock
- record_max_size
- record_max_frames
- record_interval
- recorder
- record_notify
- 視頻點播
- play
- play_temp_path
- play_local_path
- 中繼
- pull
- push
- push_reconnect
- session_relay
- 通知
- on_connect
- on_play
- on_publish
- on_done
- on_play_done
- on_publish_done
- on_record_done
- on_update
- notify_update_timeout
- notify_update_strict
- notify_relay_redirect
- notify_method
- HLS協議
- hls
- hls_path
- hls_fragment
- hls_playlist_length
- hls_sync
- hls_continuous
- hls_nested
- hls_base_url
- hls_cleanup
- hls_fragment_naming
- hls_fragment_naming_granularity
- hls_fragment_slicing
- hls_variant
- hls_type
- hls_keys
- hls_key_path
- hls_key_url
- hls_fragments_per_key
- MPEG-DASH(HTTP動態自適應流媒體)
- dash
- dash_path
- dash_fragment
- dash_playlist_length
- dash_nested
- dash_cleanup
- 訪問日志
- access_log
- log_format
- Limits 限制
- max_connections
- 數據統計
- rtmp_stat
- rtmp_stat_stylesheet
- Multi-worker live streaming 多線程直播流
- rtmp_auto_push
- rtmp_auto_push_reconnect
- rtmp_socket_dir
- Control 控制模塊
- rtmp_control
上下文可以理解為,默認配置文件或者參數,如果沒有就會報錯,因為每個函數都需一些定義好的參數和傳入函數的參數,這個有些可以少,但是有些是必須的
核心
語法: rtmp { ... }
上下文: root
聲明RTMP服務器實例
rtmp { server { } }
語法: listen (addr[:port]|port|unix:path) [bind] [ipv6only=on|off] [so_keepalive=on|off|keepidle:keepintvl:keepcnt|proxy_protocol]
上下文: server
添加監聽套接字NGINX接受RTMP連接
server {
listen 1935;
}
application
語法: application name { ... }
上下文: server
創建RTMP應用。不同於HTTP位置應用程序的名稱不能是一個模式。
server {
listen 1935;
application myapp {
}
}
timeout 超時
語法:超時值
上下文:RTMP,服務器
套接字超時。該值主要用於寫入。大多數時間RTMP模塊預計不會對除發行插座所有套接字的任何活動。如果你想斷插座得到迅速斷開連接采用主動式工具,如保活或RTMP平。默認為1分鍾。
timeout 60s;
ping
語法: ping value
上下文: rtmp, server
RTMP ping間隔。零變為平掉。 RTMP平是主動連接檢查的協議功能。一個特殊的數據包被發送到遠程對等體和答復有望與ping_timeout指令指定的超時時間內。
如果ping回復未在此時間內收到然后關閉連接。平安默認值為1分鍾。默認Ping超時是30秒。
ping 3m;
ping_timeout 30s;
ping_timeout
ping_timeout
語法: ping_timeout value
上下文: rtmp, server
見上面ping的描述
max_streams 單一流數據最大限制
語法: max_streams value
上下文: rtmp, server
設置RTMP流的最大數目。數據流被復用成一個單一的數據流。不同信道用於發送命令,音頻,視頻等的默認值是32,它通常是確定為許多情況。
max_streams 32;
ack_window
語法: ack_window value
上下文: rtmp, server
設置RTMP確認窗口的大小。它接收到的字節之后對等體應發送確認包到遠程側的次數。默認值是5000000。
ack_window 5000000;
chunk_size 數據塊大小
語法: chunk_size value
上下文: rtmp, server
最大的塊的大小為最大流復數。默認值是4096越大該值越低CPU開銷。這個值不能小於128。
chunk_size 4096;
max_queue 最大隊列數
語法: max_queue value
上下文: rtmp, server
照上面的語法習慣是這樣,官方平沒有給出文檔,應該是設置最大連接數的參數,具體想知道這個是做什么的請參看源代碼
max_message
語法: max_queue value
上下文: rtmp, server
輸入數據消息的最大大小。所有輸入數據來源(成塊,並進一步)分成的消息。的部分信息保存在內存中,而等待它完成。在理論上傳入消息可以是非常大的,可以是對於服務器的穩定性的問題。默認值是1M足夠許多情況下。
max_message 1M;
buflen 緩沖區
語法: buflen time
上下文: rtmp, server
設置默認的緩沖區長度。通常客戶端發送播放前RTMP set_buflen命令並重置該設置。默認值是1000毫秒。
buflen 5S;
out_queue
目前沒有文檔
out_cork
目前沒有文檔
Access 訪問控制
allow
語法: allow [play|publish] address|subnet|all
上下文: rtmp, server, application
允許發布/從指定的地址或所有地址播放。允許/拒絕指令出現的順序檢查。
allow publish 127.0.0.1;
deny publish all;
allow play 192.168.0.0/24;
deny play all;
deny
語法: deny [play|publish] address|subnet|all
上下文: rtmp, server, application
參見allow
說明
Exec
exec_push
語法: exec_push command arg*
上下文: rtmp, server, application
指定將公布每一個流執行帶參數的外部命令。當發布停止處理結束。二進制完整路徑應指定為第一個參數。沒有這個過程應該做的假設。然而,這功能與ffmpeg的對流轉換非常有用。
FFmpeg的應該連接到nginx的,RTMP作為客戶端和輸出轉換流回到nginx的,RTMP作為發行人。形式是$var / ${VAR}的可替換命令行中使用
$name - stream name
$app - application name
$addr - client address
$flashver - client flash version
$swfurl - client swf url
$tcurl - client tc url
$pageurl - client page url
shell風格重定向可以exec_push指令指定寫入輸出和接收輸入。支持的是
truncating output >file
appending output >>file
descriptor redirects like 1>&2
input <file
確保有重定向字符和流名稱/編號之間沒有空格。
您可以指定要執行的命令或短命令名的完整路徑。在后一種情況下,二進制文件由PATH環境變量指定的目錄中查找。默認情況下nginx的清除環境通常會使RTMP模塊只運行位於標准目錄如/ bin和/ USR / bin中的二進制文件。
為了使這個總是工作,請保持原來的PATH變量值具有以下nginx的指令。
env PATH;
下面的ffmpeg的通話呼入轉碼流HLS就緒流(H264 / AAC)。 FFmpeg的應libx264&libfaac支持被編譯在這個例子中工作。
application src { live on; exec_push ffmpeg -i rtmp://localhost/src/$name -vcodec libx264 -vprofile baseline -g 10 -s 300x200 -acodec libfaac -ar 44100 -ac 1 -f flv rtmp://localhost/hls/$name 2>>/var/log/ffmpeg-$name.log; } application hls { live on; hls on; hls_path /tmp/hls; hls_fragment 15s; }
exec_pull
語法: exec_pull command arg*
上下文: rtmp, server, application
指定與要在打事件中執行參數外部命令。當第一個客戶端連接到該流,當最后一個斷開被殺害時,執行命令。該指令使得可以在本地客戶端的任何格式拉遠程流。
該功能僅在單進程模式下可靠地工作。這樣做的原因是,我們不能確保外部進程始終連接到合適的工人。它顯然連接到一個隨機的。雖然這仍然會在大多數情況下這不是一個建議的體系結構,這將是不穩定和可能出現bug。
指令參數是相同exec_push
application myapp { live on; exec_pull ffmpeg -i http://example.com/video_$name.ts -c copy -f flv rtmp://localhost/$app/$name; }
在上述配置exec_pull指令提供所有數據流。這導致遠程流名稱格式一定的局限性。它應該能夠利用現有的變量,比如$應用程序構建遠程URL,$app等。
當這是不可能的,你可以添加指令exec_options允許設置在exec-family指令附加流選項。現在支持的唯一的選擇是名稱選項。
application myapp {
live on;
exec_options on;
exec_pull ffmpeg -i http://example.com/tv1.ts -c copy -f flv rtmp://localhost/$app/$name name=mystream;
exec_pull ffmpeg -i http://another.example.com/video_plus.ts -c copy -f flv rtmp://localhost/$app/$name name=anotherstream;
}
exec
語法: exec command arg*
上下文: rtmp, server, application
EXEC是exec_push的別名
exec_options
語法: exec_options on|off
上下文: rtmp, server, application
該指令切換Exec選模式。當被激活時,你可以添加一個exec家族指令選項。唯一支持的EXEC選項名稱。通過該選項,可以應用EXEC只有指定的流。默認為關閉狀態。
exec_options on;
# 調用on_publish只為“mystream”
exec_publish http://localhost/on_publish name=mystream;
#調用on_play只為“另一個”
exec_play http://localhost/on_play name=another;
#執行不同的ffmpeg對不同的流
exec_pull http://example.com/abc.ts -c copy -f flv rtmp://localhost/$name/$app name=mystream;
exec_pull http://my.example.com/tele.ts -c copy -f flv rtmp://localhost/$name/$app name=tv;
exec_pull http://enother.example.com/hello/f.ts -c copy -f flv rtmp://localhost/$name/$app name=fun;
exec_static
語法: exec_static command arg*
上下文: rtmp, server, application
類似給exec,但在運行nginx的啟動指定的命令。不支持替換,因為沒有session 環境。
exec_static ffmpeg -i http://example.com/video.ts -c copy -f flv rtmp://localhost/myapp/mystream;
exec_kill_signal
語法: exec_kill_signal signal
上下文: rtmp, server, application
exec_kill_signal term; exec_kill_signal usr1; exec_kill_signal 3;
respawn
語法: respawn on|off
上下文: rtmp, server, application
如果重生子進程打開的時候同時發布仍然在它的終止。默認為上;
respawn off;
respawn_timeout
語法: respawn_timeout timeout
上下文: rtmp, server, application
套重生超時開始新的子實例之前等待。默認值是5秒。
respawn_timeout 10s;
exec_publish
語法: exec_publish command arg*
上下文: rtmp, server, application
指定與參數外部命令要在發布事件執行。返回代碼不進行分析。 EXEC的替換是同時支持在這里。此外ARGS變量贊成舉行查詢字符串參數。
exec_play
語法: exec_play command arg*
上下文: rtmp, server, application
指定與要在打事件中執行參數外部命令。返回代碼不進行分析。替代列表和exec_publish相同
exec_play_done
語法: exec_play_done command arg*
上下文: rtmp, server, application
指定與參數外部命令要在play_done事件執行。返回代碼不進行分析. 替代列表和exec_publish相同
exec_publish_done
語法: exec_publish_done command arg*
上下文: rtmp, server, application
指定與參數外部命令要在publish_done事件執行。返回代碼不進行分析. 替代列表和exec_publish相同
exec_record_done
語法: exec_record_done command arg*
上下文: rtmp, server, application, recorder
指定錄音結束時要執行帶有參數的外部命令。 exec_publish的取代在這里支持以及附加變量
recorder
- recorder namepath
- recorded file path (/tmp/rec/mystream-1389499351.flv
)filename
- path with directory omitted (mystream-1389499351.flv
)basename
- file name with extension omitted (mystream-1389499351
)dirname
- directory path (/tmp/rec
)
例子
# 跟蹤客戶信息
exec_play bash -c "echo $addr $pageurl >> /tmp/clients";
exec_publish bash -c "echo $addr $flashver >> /tmp/publishers";
# 轉換錄制的文件格式的MP4
exec_record_done ffmpeg -y -i $path -acodec libmp3lame -ar 44100 -ac 1 -vcodec libx264 $dirname/$basename.mp4;
Live
live
語法: live on|off
上下文: rtmp, server, application
切換現場模式,即一對多的廣播。
live on;
meta
語法: meta on|copy|off
上下文: rtmp, server, application
集元數據發送模式。上的價值,使用戶收到一封包含像寬度,高度等的副本設定值,使客戶得到出版商的元數據塊的精確副本,包括標准和具體領域的預定義字段重建的元數據包。關的值將關閉發送任何RTMP元數據給用戶。默認為on。
meta copy;
interleave
語法: interleave on|off
上下文: rtmp, server, application
切換交錯模式。在這種模式下的音頻和視頻數據上相同的RTMP塊流傳輸。默認為off。
interleave on;
wait_key
語法: wait_key on|off
上下文: rtmp, server, application
使視頻流開始一個關鍵幀。默認為關閉。
wait_key on;
wait_video
語法: wait_video on|off
上下文: rtmp, server, application
直到第一個視頻幀發送禁用音頻。默認為關閉。可以用wait_key決定相結合,使客戶端接收視頻關鍵幀但其后所有其他數據。然而,這通常會增加連接延遲。你可以在你的編碼器,以減少延遲調整關鍵幀間隔
IE瀏覽器的最新版本需要這個選項正常播放啟用。
wait_video on;
publish_notify
語法: publish_notify on|off
上下文: rtmp, server, application
發送NetStream.Play.PublishNotify和NetStream.Play.UnpublishNotify給用戶。默認為關閉。
publish_notify on;
drop_idle_publisher
語法: drop_idle_publisher timeout
上下文: rtmp, server, application
丟棄在指定時間內發行者連接這一直空閑(無音頻/視頻數據)。默認是關閉的。注意:這只能當連接處於發布模式(發送發布命令后)。
drop_idle_publisher 10s;
sync
語法: sync timeout
上下文: rtmp, server, application
同步的音頻和視頻流。如果訂戶帶寬不夠以接收發布速率數據,某些幀被服務器丟棄。這將導致同步問題。當時間戳差值超過指定為同步參數的值絕對幀發送固定的。默認為300ms。
sync 10ms;
play_restart
語法: play_restart on|off
上下文: rtmp, server, application
如果啟用nginx的,RTMP發送NetStream.Play.Start和NetStream.Play.Stop到每個用戶每次發行啟動或停止發布。如果禁用每個用戶只在開始和重放結束接收這些通知。默認是off。
play_restart off;
idle_streams
語法: idle_streams on|off
上下文: rtmp, server, application
如果禁用nginx的,RTMP防止連接到空閑/不存在的實時流用戶,斷開所有用戶的數據流時,斷開publisher。默認為on。
idle_streams off;
record
語法: record [off|all|audio|video|keyframes|manual]*
上下文: rtmp, server, application, recorder
切換錄音模式。流可以被記錄在FLV文件。該指令指定正是應記錄哪些:
- off - 不記錄在所有
- all - 音頻和視頻(一切)
- audio -音頻
- video - 視頻
- keyframes -唯一的關鍵幀視頻
- manual - 從來沒有自動啟動記錄儀,使用控制界面來啟動/停止
可以有鍵,在一個記錄指令任何相容組合。
record all;
record audio keyframes;
record_path
語法: record_path path
上下文: rtmp, server, application, recorder
指定將錄制的FLV文件來記錄路徑。
record_path /tmp/rec;
record_suffix
語法: record_suffix value
上下文: rtmp, server, application, recorder
設置記錄文件的后綴。默認為'。FLV“。
record_suffix _recorded.flv;
記錄后綴可以是的strftime格式的模式。下面的指令
record_suffix -%d-%b-%y-%T.flv;
將產生的形式mystream-24-Apr-13-18:23:38.flv 所有支持的strftime格式選項可以的strftime手冊頁上找到。
record_unique
語法: record_unique on|off
上下文: rtmp, server, application, recorder
如果打開追加當前的時間戳,以錄制的文件。否則,同樣的文件,每次新的記錄發生重寫。默認是關閉的
record_unique on;
record_append
語法: record_append on|off
上下文: rtmp, server, application, recorder
切換文件追加模式。當錄音機打開追加新數據舊文件或創建它時,它的缺失。有舊的數據,並在文件中的新數據之間不存在時間間隙。默認是關閉的。
record_append on;
record_lock
語法: record_lock on|off
上下文: rtmp, server, application, recorder
當打開當前記錄的文件被鎖住的fcntl調用。這可以從其他地方進行檢查,以找出正在錄制的文件。默認是關閉的。
record_lock on;
在FreeBSD上可以使用的羊群工具來檢查。在Linux上的flock
和fcntl是無關的,所以你只剩下編寫一個簡單的腳本文件檢查鎖定狀態。下面是這種腳本isunlocked.py的一個例子。
#!/usr/bin/python
import fcntl, sys
sys.stderr.close()
fcntl.lockf(open(sys.argv[1], "a"), fcntl.LOCK_EX|fcntl.LOCK_NB)
record_max_size
語法: record_max_size size
上下文: rtmp, server, application, recorder
設置最大記錄文件大小
record_max_size 128K;
record_max_frames
語法: record_max_frames nframes
上下文: rtmp, server, application, recorder
設置每個錄像文件的視頻幀的最大數量。
record_max_frames 2;
record_interval
語法: record_interval time
上下文: rtmp, server, application, recorder
重新啟動該數字(毫)秒后拍攝。默認關閉。零表示記錄之間沒有延遲。如果record_unique是關閉的,那么所有的記錄片段被寫入到同一個文件。否則,時間戳就會追加這使得文件不同(因為record_interval大於1秒以上)。
record_interval 1s;
record_interval 15m;
recorder
語法: recorder name {...}
上下文: application
創建記錄塊。多個刻錄機可以withing單一的應用程序來創建。所有上述記錄相關指令可以在記錄{}塊中指定。所有的設置是從更高層次繼承。
application {
live on;
# default recorder
record all;
record_path /var/rec;
recorder audio {
record audio;
record_suffix .audio.flv;
}
recorder chunked {
record all;
record_interval 15s;
record_path /var/rec/chunked;
}
}
record_notify
語法: record_notify on|off
上下文: rtmp, server, application, recorder
切換發送NetStream.Record.Start和NetStream.Record.Stop狀態信息(的onStatus),以發行時的具體記錄開始或停止錄音文件。狀態描述字段保存記錄的域名(空默認記錄器)。默認關閉。
recorder myrec {
record all manual;
record_path /var/rec;
record_notify on;
}
Video on demand 視頻點播
play
語法: play dir|http://loc [dir|http://loc]*
上下文: rtmp, server, application
從指定目錄或HTTP位置播放FLV或MP4文件。如果參數前綴以http://那么假定文件應該從遠程HTTP位置播放前下載。注意打不啟動,直到整個文件下載。您可以使用nginx的本地緩存本地計算機上的文件。
多個播放位置可以在一個單一的播放指令指定。當多個播放指令指定的位置列被合並,並從更高的作用域繼承。發揮每個位置試圖直到成功定位被發現。如果沒有找到這樣的位置錯誤狀態被發送到客戶端
索引FLV是為了隨機搜索能力。未編入索引FLV是玩謀求/暫停禁用(僅重啟動模式)。使用的FLV索引(例如,yamdi)索引。
如果您播放錄制的記錄指令FLV文件,請不要打前忘記他們的索引。在創建未編入索引。
如果視頻和音頻編解碼器是由RTMP支持的mp4文件只能播放。最常見的情況是H264 / AAC。
application vod {
play /var/flvs;
}
application vod_http {
play http://myserver.com/vod;
}
application vod_mirror {
# try local location first, then access remote location
play /var/local_mirror http://myserver.com/vod;
}
播放 /var/flvs/dir/file.flv:
ffplay rtmp://localhost/vod//dir/file.flv
VOD后的兩條斜線使ffplay使用VOD和應用程序名稱和URL作為playpath的其余部分。
play_temp_path
語法: play_temp_path dir
上下文: rtmp, server, application
設置將在其中遠程VOD播文件的播放之前存儲位置。默認值是/ tmp;
play_temp_path /www;
play http://example.com/videos;
play_local_path
語法: play_local_path dir
上下文: rtmp, server, application
設置從哪里目錄play_temp_path復制遠程點播文件,他們完全下載后的位置。空值禁用該功能。默認情況下它是空的。該特征可用於在本地緩存遠程文件。
此路徑應該是相同的裝置,play_temp_path上。
# 在
/tmp/videos上搜索文件
# 如果沒有找到就找遠程的location上尋找
# 而且儲存在/tmp/videos
play_local_path /tmp/videos;
play /tmp/videos http://example.com/videos;
Relay 中繼
pull
語法: pull url [key=value]*
上下文: application
Creates pull relay. Stream is pulled from remote machine and becomes available locally. It only happens when at least one player is playing the stream locally.
創建拉流中繼 ,流從遠程計算機上拉,並且成為本地可用。它僅當至少一個播放流正在玩本地流發生。
Url syntax: [rtmp://]host[:port][/app[/playpath]]
.
如果應用程序丟失,則使用本地應用程序的名稱。如果playpath缺少那么當前流的名字來代替。
以下參數被支持:
- app - 明確應用程序的名稱
- name - 綁定繼電器當地流名;如果為空或不指定,則應用程序中的所有當地的溪流被拉到
- tcUrl - 如果空的自動構建
- pageUrl - 偽裝網頁網址
- swfUrl - SWF網址假裝
- flashVer - flash版本假裝,默認為“LNX.11,1,102,55”
- playPath - 遠程播放路徑
- live - 切換特殊行為直播,值:0,1
- start - 以秒為單位的開始時間
- stop - 在幾秒鍾內停止時間
- static - 使靜態拉流 如拉在nginx的啟動創建
If a value for a parameter contains spaces then you should use quotes around the WHOLEkey=value pair like this : 'pageUrl=FAKE PAGE URL'
.
如果參數的值包含空格,那么你應該使用引號圍繞整個key = value對這樣的:'pageUrl=FAKE PAGE URL“。
pull rtmp://cdn.example.com/main/ch?id=12563 name=channel_a;
pull rtmp://cdn2.example.com/another/a?b=1&c=d pageUrl=http://www.example.com/video.html swfUrl=http://www.example.com/player.swf live=1;
pull rtmp://cdn.example.com/main/ch?id=12563 name=channel_a static;
push
語法: push url [key=value]*
上下文: application
推動具有相同的語法拉動。不像推拉指令發布流遠程服務器。
push_reconnect
語法: push_reconnect time
上下文: rtmp, server, application
重新連接超時之前等待的推后斷開連接。默認設置為3秒。
push_reconnect 1s;
session_relay
語法: session_relay on|off
上下文: rtmp, server, application
切換會話中繼模式。在這種模式下中繼當連接關閉時被破壞。當設置為關閉時,流關閉,使得其他的中繼也可能會被以后創建的繼電器被破壞。默認是關閉的。
session_relay on;
Notify
on_connect
語法: on_connect url
上下文: rtmp, server
設置HTTP連接的回調。當客戶端連接問題的命令HTTP請求是異步發出命令和處理被掛起,直到它返回結果代碼。如果返回HTTP 2XX代碼,然后RTMP會話繼續。
3XX的代碼使RTMP重定向到另一個應用程序的名字是從位置HTTP響應頭拍攝。否則,連接被丟棄。
注意:這個指令不應用范圍允許的,因為應用還處於連接階段不明。
HTTP請求接收多個參數。 POST方法用於application/x-www-form-urlencoded MIME type ,下面的參數被傳遞給調用者:
- call=connect
- addr - client IP address
- app - application name
- flashVer - client flash version
- swfUrl - client swf url
- tcUrl - tcUrl
- pageUrl - client page url
除了上述項目明確地傳遞給connect命令的所有參數也與回調發送。您應當區分連接參數從播放/發布參數。玩家通常具有播放/流發布名設置連接字符串分開的一種特殊方式。作為一個例子下面是這些參數是如何在JWPlayer設置
...
streamer: "rtmp://localhost/myapp?connarg1=a&connarg2=b",
file: "mystream?strarg1=c&strarg2=d",
...
Ffplay (with librtmp) example
ffplay "rtmp://localhost app=myapp?connarg1=a&connarg2=b playpath=mystream?strarg1=c&strarg2=d"
Usage example
on_connect http://example.com/my_auth;
Redirect example
location /on_connect {
if ($arg_flashver != "my_secret_flashver") {
rewrite ^.*$ fallback? permanent;
}
return 200;
}
on_play
語法: on_play url
上下文: rtmp, server, application
Sets HTTP play callback. Each time a clients issues play command an HTTP request is issued asynchronously and command processing is suspended until it returns result code. HTTP result code is then analyzed.
- HTTP 2XX代碼繼續RTMP會議
- HTTP重定向3XX RTMP另一個流的名字是從位置HTTP響應頭拍攝。如果新的流名字開始與rtmp:// 代替創建,繼而遠程中繼會被創建。中繼需要IP地址被指定,而不是域名,只用nginx的版本低於1.3.10更大的工作。又見notify_relay_redirect。
- 否則,RTMP連接斷開
重定向的例如
http {
...
location /local_redirect {
rewrite ^.*$ newname? permanent;
}
location /remote_redirect {
# no domain name here, only ip
rewrite ^.*$ rtmp://192.168.1.123/someapp/somename? permanent;
}
...
}
rtmp {
...
application myapp1 {
live on;
# stream will be redirected to 'newname'
on_play http://localhost:8080/local_redirect;
}
application myapp2 {
live on;
# stream will be pulled from remote location
# requires nginx >= 1.3.10
on_play http://localhost:8080/remote_redirect;
}
...
}
HTTP請求接收多個參數。 POST方法用於application/x-www-form-urlencoded MIME type. 。下面的參數被傳遞給調用者:
- call=play
- addr - client IP address
- clientid - nginx client id (displayed in log and stat)
- app - application name
- flashVer - client flash version
- swfUrl - client swf url
- tcUrl - tcUrl
- pageUrl - client page url
- name - stream name
除了上述項目通過明確玩命令的所有參數也與回調發送。
例如,如果流與URL訪問 rtmp://localhost/app/movie?a=100&b=face&foo=bar
then a
, b
& foo
也會被發送回來
on_play http://example.com/my_callback;
on_publish
語法: on_publish url
上下文: rtmp, server, application
上面這個指令設置了發布命令回調,唯一的區別一樣on_play。代替遠程拉推在這種情況下進行的。
on_done
語法: on_done url
上下文: rtmp, server, application
設置 play/publish 終止回調。上述所有適用於此。然而HTTP狀態代碼不檢查此回調。
on_play_done
語法: on_play_done url
上下文: rtmp, server, application
和 on_done 一樣的行為
但只作用於 play和 event
on_publish_done
語法: on_publish_done url
上下文: rtmp, server, application
相同的行為on_done 但只作用於 publish 和 event
on_record_done
語法: on_record_done url
上下文: rtmp, server, application, recorder
設置record_done回調。除了常見的HTTP回調變量它接收到以下值
- recorder - recorder name in config or empty string for inline recorder 在配置或空字符串內聯錄音機錄像機名
- path - 錄制的文件路徑
Example
on_record_done http://example.com/recorded;
on_update
語法: on_update url
上下文: rtmp, server, application
Set update callback. This callback is called with period of notify_update_timeout
. If a request returns HTTP result other than 2xx connection is terminated. This can be used to synchronize expired sessions. Two additional arguments time
and timestamp
are passed to this handler:
設置更新回調。此回調調用notify_update_timeout的時期。如果請求返回比2XX連接其他HTTP結果被終止。這可用於同步過期會話。另外兩個參數的 時間和時間戳 傳遞到該處理程序
time
從 play/publish 回調開始的時間timestamp
is RTMP timestamp of the last audio/video packet sent to the client 發送到客戶端的最后的音頻/視頻分組的RTMP時間戳
您可以使用時間戳參數單獨限制播放時長為每個用戶。
on_update http://example.com/update;
notify_update_timeout
語法: notify_update_timeout timeout
上下文: rtmp, server, application
集on_update回調之間超時。默認值是30秒。
notify_update_timeout 10s;
on_update http://example.com/update;
notify_update_strict
語法: notify_update_strict on|off
上下文: rtmp, server, application
對於切換回調on_update嚴格模式。默認是關閉的。當所有的連接錯誤打開,超時以及HTTP分析錯誤和空的反應被視為更新失敗,並導致連接終止。當關閉唯一有效的HTTP響應代碼等的2XX導致失敗。
notify_update_strict on;
on_update http://example.com/update;
notify_relay_redirect
語法: notify_relay_redirect on|off
上下文: rtmp, server, application
啟用對on_play和on_publish遠程重定向本地流重定向。新的流名字是用於遠程重定向RTMP網址的MD5哈希值。默認是關閉的。
notify_relay_redirect on;
notify_method
語法: notify_method get|post
上下文: rtmp, server, application, recorder
設置通知的HTTP方法。默認值是POST與 application/x-www-form-urlencoded content type.在某些情況下得到的是可取的,例如,如果您打算處理Nginx的HTTP {}部分呼叫。在這種情況下,你可以使用arg_*變量訪問參數。
notify_method get;
在HTTP GET方法處理 http{}部分可以做這樣
location /on_play {
if ($arg_pageUrl ~* localhost) {
return 200;
}
return 500;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HLS
語法: hls on|off
上下文: rtmp, server, application
切換HLS上的應用。
hls on;
hls_path /tmp/hls;
hls_fragment 15s;
在HTTP {}部分客戶打HLS設置以下的位置。
http {
...
server {
...
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
}
root /tmp;
add_header Cache-Control no-cache;
# 為了避免與跨域的HTTP請求(例如,在開發過程中)的問題
add_header Access-Control-Allow-Origin *;
}
}
}
hls_path
語法: hls_path path
上下文: rtmp, server, application
設置HLS播放列表和片段目錄。如果該目錄不存在,將會被創建。
hls_fragment
語法: hls_fragment time
上下文: rtmp, server, application
設置HLS片段長度。默認為5秒。
hls_playlist_length
語法: hls_playlist_length time
上下文: rtmp, server, application
設置播放列表HLS長度。默認為30秒。
hls_playlist_length 10m;
hls_sync
語法: hls_sync time
上下文: rtmp, server, application
Sets HLS timestamp synchronization threshold. Default is 2ms. This feature prevents crackling noises after conversion from low-resolution RTMP (1KHz) to high-resolution MPEG-TS (90KHz).
設置HLS時間戳同步的門檻。默認為2ms。此功能可以防止噼啪的噪聲從低分辨率RTMP(1KHz的)轉換為高分辨率MPEG-TS(90KHz的)之后。
hls_sync 100ms;
hls_continuous
語法: hls_continuous on|off
上下文: rtmp, server, application
切換HLS連續模式。在這種模式下HLS序列號從它停止了最后一次啟動。舊片段依然古色古香。默認是關閉的。
hls_continuous on;
hls_nested
語法: hls_nested on|off
上下文: rtmp, server, application
切換HLS嵌套模式。在這種模式下hls_path的子目錄為每個數據流創建。播放列表和片段在子目錄中創建。默認是關閉的。
hls_nested on;
hls_base_url
語法: hls_base_url url
上下文: rtmp, server, application
設置HLS播放列表項基本URL。當空這些項目沒有前綴並且假設是在相同的位置父播放列表或低一個級別時使用hls_nested。此功能同時適用於主(變量)和從HLS播放列表。它可以讓你下載播放列表,因為它包含兒童播放列表或片段完全引用本地播放。默認為空。
hls_base_url http://myserver.com/hls/;
hls_cleanup
語法: hls_cleanup on|off
上下文: rtmp, server, application
切換HLS清理。默認情況下,該功能已開啟。在這種模式下nginx的緩存管理器進程會刪除HLS目錄老HLS片段和播放列表。
hls_cleanup off;
hls_fragment_naming
語法: hls_fragment_naming sequential|timestamp|system
上下文: rtmp, server, application
設置片段的命名方式
- sequential - 使用增加的整數
- timestamp - 使用流時間戳
- system -使用系統時間
默認值是連續的
hls_fragment_naming system;
hls_fragment_naming_granularity
語法: hls_fragment_naming_granularity number
上下文: rtmp, server, application
設置HLS片段IDS粒度。如果大於零,改變IDS來划分提供的價值。默認值是零。
# use system time rounded to 500ms as fragment names
hls_fragment_naming system;
hls_fragment_naming_granularity 500;
hls_fragment_slicing
語法: hls_fragment_slicing plain|aligned
上下文: rtmp, server, application
設置片段切片模式。
- plain - 當達到目標時的持續時間片段切換
- aligned -開關片段時傳入的時間戳片段持續時間的倍數。該模式使得能夠產生不同的nginx實例相同片段
默認值是 plain.
hls_fragment_slicing aligned;
hls_variant
語法: hls_variant suffix [param*]
上下文: rtmp, server, application
添加HLS變項(variant entry 變量條目,翻譯不准確)。當后綴是在流名稱匹配,然后,對用hls_variant指示當前應用程序中指定的所有條目當前流創建播放列表中的變體。不帶后綴名剝離用作變量流名稱。原來的流像往常一樣處理。
以下后綴可選參數追加到播放列表M3U8 EXT-X-STREAM-INF。見HLS規范。 3.3.10。 EXT-X-STREAM-INF支持的參數的完整列表。
rtmp {
server {
listen 1935;
application src {
live on;
exec ffmpeg -i rtmp://localhost/src/$name
-c:a libfdk_aac -b:a 32k -c:v libx264 -b:v 128K -f flv rtmp://localhost/hls/$name_low
-c:a libfdk_aac -b:a 64k -c:v libx264 -b:v 256k -f flv rtmp://localhost/hls/$name_mid
-c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 512K -f flv rtmp://localhost/hls/$name_hi;
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
hls_nested on;
hls_variant _low BANDWIDTH=160000;
hls_variant _mid BANDWIDTH=320000;
hls_variant _hi BANDWIDTH=640000;
}
}
}
hls_type
語法: hls_type live|event
上下文: rtmp, server, application
將X-PLAYLIST型播放列表指令指定HLS播放列表類型。直播HLS流通常是從當前實際位置是幾個片段到播放列表的末尾播放。事件HLS流總是從播放開始播放。當事件模式確保播放列表的長度足以讓整個事件。默認值是live
;
hls_type event;
hls_keys
語法: hls_keys on|off
上下文: rtmp, server, application
啟用HLS加密。 AES-128的方法用來加密整個HLS片段。默認關閉。
hls_keys on;
下面是一個使用HLS加密示例配置。此配置要求nginx的與--with-http_ssl_module建為HTTPS支持。
...
http {
...
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /var/ssl/example.com.cert;
ssl_certificate_key /var/ssl/example.com.key;
location /keys {
root /tmp;
}
}
server {
listen 80;
server_name example.com;
location /hls {
root /tmp;
}
}
}
rtmp {
server {
listen 1935;
application myapp {
live on;
hls on;
hls_path /tmp/hls;
hls_keys on;
hls_key_path /tmp/keys;
hls_key_url https://example.com/keys/;
hls_fragments_per_key 10;
}
}
}
hls_key_path
語法: hls_key_path path
Context: rtmp, server, application
設置將在其中自動生成HLS密鑰保存的目錄。密鑰文件與在OpenSSL RAND_bytes()例行程序創建的.key延伸和偽隨機的16字節的內容。如果該目錄不存在,它在運行時創建的。默認情況下,hls_path目錄用於密鑰文件。但是請記住,你通常應該限制訪問關鍵文件,這是容易當這些文件從播放列表和片段分開存放。
hls_key_path /tmp/keys;
hls_key_url
語法: hls_key_url url
上下文: rtmp, server, application
設置HLS密鑰文件條目的URL。當空這些項目沒有前綴和密鑰被認為是在相同的位置播放列表。默認為空
hls_key_url https://myserver.com/keys/;
用上述設定例的播放列表條目
#EXT-X-KEY:METHOD=AES-128,URI="https://myserver.com/keys/337.key",IV=0x00000000000000000000000000000151
hls_fragments_per_key
語法: hls_fragments_per_key value
上下文: rtmp, server, application
設置具有相同的密鑰加密的HLS片段的數目。零意味着只有一個密鑰的發布開始創建,並在會話中的所有片段與此密鑰加密。默認值是零。
hls_fragments_per_key 10;
MPEG-DASH MPEG組織批准了MPEG-DASH(HTTP動態自適應流媒體)標准
dash
語法: dash on|off
上下文: rtmp, server, application
應用程序切換MPEG-DASH。
dash on;
dash_path /tmp/dash;
dash_fragment 15s;
在HTTP {}部分設置以下的位置,為客戶播放MPEG-DASH。
http {
...
server {
...
location /dash {
root /tmp;
add_header Cache-Control no-cache;
# #為了避免與跨域的HTTP請求(例如,在開發過程中)的問題
add_header Access-Control-Allow-Origin *;
}
}
}
dash_path
語法: dash_path path
上下文: rtmp, server, application
設置MPEG-DASH播放列表,片段目錄。如果該目錄不存在,它將被創建。
dash_fragment
語法: dash_fragment time
上下文: rtmp, server, application
設置MPEG-DASH片段長度。默認為5秒。
dash_playlist_length
語法: dash_playlist_length time
上下文: rtmp, server, application
設置MPEG-DASH播放列表的長度。默認為30秒。
dash_playlist_length 10m;
dash_nested
語法: dash_nested on|off
上下文: rtmp, server, application
切換MPEG-DASH嵌套模式。在這種模式下dash_path的子目錄為每個數據流創建。播放列表和片段在子目錄中創建。默認是關閉的。
dash_nested on;
dash_cleanup
語法: dash_cleanup on|off
上下文: rtmp, server, application
切換MPEG-DASH清理。默認情況下,該功能已開啟。在這種模式下nginx的緩存管理器進程從MPEG-DASH目錄中刪除舊的MPEG-DASH碎片和體現。流清單被刪除后,初始化片段被刪除。
dash_cleanup off;
Access log
access_log
語法: access_log off|path [format_name]
上下文: rtmp, server, application
設置訪問日志參數。日志默認情況下開啟。要關閉它使用ACCESS_LOG關閉指令。默認情況下訪問日志記錄是為了同一個文件的HTTP訪問記錄器(log/ access.log)。您可以指定訪問日志指令另一個日志文件的路徑。第二個參數是可選的。它可用於通過名稱來指定記錄格式。見log_format指令有關格式的詳細信息。
log_format new '$remote_addr';
access_log logs/rtmp_access.log new;
access_log logs/rtmp_access.log;
access_log off;
log_format
語法: log_format format_name format
上下文: rtmp
創建一個名為日志格式。日志格式看起來非常相同的nginx的HTTP日志格式。幾個變量的日志格式中支持:
connection
- connection numberremote_addr
- client addressapp
- application namename
- last stream nameargs
- last stream play/publish argumentsflashver
- client flashVerswfurl
- client swfUrltcurl
- client tcUrlpageurl
- client pageUrlcommand
- play/publish commands sent by client:NONE
,PLAY
,PUBLISH
,PLAY+PUBLISH
bytes_sent
- number of bytes sent to clientbytes_received
- number of bytes received from clienttime_local
- local time at the end of client connectionsession_time
- connection duration in secondssession_readable_time
- connection duration in human-readable formatmsec
- current unix timestamp in SEC.MSEC format
默認的日志格式有名稱相結合。下面是這種格式的定義
$remote_addr [$time_local] $command "$app" "$name" "$args" -
$bytes_received $bytes_sent "$pageurl" "$flashver" ($session_readable_time)
Limits
max_connections
語法: max_connections number
上下文: rtmp, server, application
設置RTMP發動機的最大連接數。默認關閉
max_connections 100;
Statistics
統計模塊是不同於此處列出的所有其他模塊NGINX HTTP模塊。因此,統計指令應位於HTTP {}塊之內
rtmp_stat
語法: rtmp_stat all
上下文: http, server, location
設置RTMP統計處理當前的HTTP位置。 RTMP統計是動態的XML文檔。要觀看在瀏覽器XHTML頁面使用rtmp_stat_stylesheet指令這份文件。
http {
server {
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /path/to/stat/xsl/file;
}
}
}
rtmp_stat_stylesheet
語法: rtmp_stat_stylesheet path
上下文: http, server, location
添加XML樣式表引用統計XML,使其在瀏覽器查看。見rtmp_stat說明和示例的詳細信息。
Multi-worker live streaming
多進程直播通過推流剩余nginx的工人執行。
rtmp_auto_push
語法: rtmp_auto_push on|off
上下文: root
切換自動推(多進程直播)模式。默認是關閉的。
rtmp_auto_push_reconnect
語法: rtmp_auto_push_reconnect timeout
上下文: root
設置自動推送重新連接超時,當工作進程被殺害。默認值是100毫秒。
rtmp_socket_dir
語法: rtmp_socket_dir dir
上下文: root
設置用於流推動UNIX域套接字目錄。默認值是/ tmp。
rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /var/sock;
rtmp {
server {
listen 1935;
application myapp {
live on;
}
}
}
Control 控制模塊
控制模塊是HTTP模塊,這使得它可以從使用HTTP協議之外的控制設rtmp模塊。下面是如何使能控制一個例子。
http { ... server { listen 8080; server_name localhost; .... location /control { rtmp_control all; } } }
有控制模塊內的幾個子模塊的每個控制不同的功能。
錄制
該子模塊啟動和停止與手動標志創建記錄。語法:
http://server.com/control/record/start|stop?srv=SRV&app=APP&name=NAME&rec=REC
SRV = SRV - 可選的服務器{}塊RTMP {}塊中號,默認為第一個服務器{}塊
應用= APP - 所需的應用程序名稱
NAME =名稱 - 需要流名稱
REC = REC - 可選的記錄名稱,默認為根(未命名)記錄
rtmp { server { listen 1935; application myapp { live on; recorder rec1 { record all manual; record_suffix all.flv; record_path /tmp/rec; record_unique on; } } } }
用下面的命令發布該流
ffmpeg -i http://someserver.com/mychannel.ts -c:v copy -c:a nellymoser -ar 44100 -ac 1 -f flv rtmp://localhost/myapp/mystream
使用下面的命令來啟動和停止錄音
curl "http://localhost:8080/control/record/start?app=myapp&name=mystream&rec=rec1"
curl "http://localhost:8080/control/record/stop?app=myapp&name=mystream&rec=rec1"
如果記錄開始/停止請求有時會返回什么都沒有,你應該檢查,如果你使用多進程,單個進程的比較好
刪除 drop
該子模塊提供了一種簡單的方法來刪除客戶端連接。語法:
http://server.com/control/drop/publisher|subscriber|client?srv=SRV&app=APP&name=NAME&addr=ADDR&clientid=CLIENTID
srv, app, name -相同
addr - 可選的客戶端地址(同樣由rtmp_stat返回)
clientid - 可選nginx的客戶端ID(日志和統計顯示)
第一種方法 刪除/發布者 發行下降的連接。第二方法是 刪除或者客戶端刪除,如果沒有指定地址每連接匹配addr參數或所有客戶端(包括出版商)。
curl http://localhost:8080/control/drop/publisher?app=myapp&name=mystream
curl http://localhost:8080/control/drop/client?app=myapp&name=mystream
curl http://localhost:8080/control/drop/client?app=myapp&name=mystream&addr=192.168.0.1
curl http://localhost:8080/control/drop/client?app=myapp&name=mystream&clientid=1
Redirect 重定向
重定向播放/客戶端發布到一個新的數據流。語法:
http://server.com/control/redirect/publisher|subscriber|client?srv=SRV&app=APP&name=NAME&addr=ADDR&clientid=CLIENTID&newname=NEWNAME
srv, app, name, addr, clients - 和上面相同
newname - 新的流名重定向到