Home > Article > Operation and Maintenance > How does Nginx-rtmp realize the real-time streaming effect of live media?
0. Preface
During this time, I am building an ipcamera project server. For video point-to-point calls, the client will view the real-time audio and video stream on the device. In order to save traffic, penetration is through p2p. But due to the reasons of NAT equipment and the exhaustion of ipv4. Some devices are unable to transmit live streams point-to-point. So server forwarding is required. Here, in order to quickly implement the prototype, we also refer to the current mainstream streaming media protocols. I found that many use the rtmp protocol.
The picture below is the overall design diagram. In order to integrate multiple platforms, we will build our own rtmp streaming media server and use the rtmp streaming media service of cloud vendor saas. However, because sometimes some non-streaming media data is transmitted and some binary files need to be transmitted, a custom media forwarding service is required.
The following is the architecture implementation flow chart used in my actual project.
1. 客户端a无法进行p2p穿透,请求业务服务器要进行转发。 2. 业务服务器根据客户端a,请求类型,返回对应的转发服务器地址和对应的房间号roomid/token等信息 3. 上述请求类型,可以是请求自建rtmp流媒体服务,购买于云厂商rtmp流媒体服务或者自定义协议媒体转发服务 4. 客户端a得到业务服务器返回的媒体服务器地址和roomid/token 5. 通过信令服务器或者mqtt服务器,把对应的媒体服务器地址和roomid/token告诉另一端客户端b 6. 客户端a和客户端b同时进入相同房间room,客户端a进行推流,客户端b进行拉流 7. 其他媒体信息,如编解码格式,清晰度,播放,暂停,拍照等命令,通过上述信令或mqtt服务器进行命令控制
1. Compile nginx
rtmp streaming media server, there are many ready-made open source solutions, including srs, red5, wowoza , fms, etc. I am using nginx’s rtmp plug-in to implement real-time stream forwarding.
Download nginx-rtmp-module
Recompile nginx
--prefix=/opt/nginx --with-stream --with-http_ssl_module --with-stream_ssl_module --with-debug --add-module=../nginx-rtmp-module
2. Configure nginx.conf
Basic nginx configuration , I won’t introduce it here. If you need to know more, you can refer to my other blogs, which have introductions. Here we only introduce the definition of rtmp segment.
rtmp{ server{ listen 8081; access_log logs/rtmp_access.log; on_connect http://127.0.0.1:8080/v1/rtmp/on_connect; application rtmp { live on; notify_method get; on_play http://127.0.0.1:8080/v1/rtmp/on_play; on_publish http://127.0.0.1:8080/v1/rtmp/on_publish; on_done http://127.0.0.1:8080/v1/rtmp/on_done; on_play_done http://127.0.0.1:8080/v1/rtmp/on_play_done; on_publish_done http://127.0.0.1:8080/v1/rtmp/on_publish_done; on_record_done http://127.0.0.1:8080/v1/rtmp/on_record_done; on_update http://127.0.0.1:8080/v1/rtmp/on_update; notify_update_timeout 10s; } application vod { play /opt/openresty/video; } } }
3. http asynchronous notification callback
The nginx-rtmp-module plug-in implements event notification for some commands of the rtmp protocol. Here I use a simple springboot project to quickly build an http service to receive rtmp callbacks.
package com.wunaozai.rtmp.notify.controller; import javax.servlet.http.httpservletrequest; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller @requestmapping(value="/v1/rtmp/") public class rtmpnotifycontroller { @getmapping(value="/on_connect") public string onconnect(httpservletrequest request){ debug(request, "on_connect"); return "on_connect"; } @getmapping(value="/on_play") public string onplay(httpservletrequest request){ debug(request, "on_play"); return "on_play"; } @getmapping(value="/on_publish") public string onpublish(httpservletrequest request){ debug(request, "on_publish"); return "on_publish"; } @getmapping(value="/on_done") public string ondone(httpservletrequest request){ debug(request, "on_done"); return "on_done"; } @getmapping(value="/on_play_done") public string onplaydone(httpservletrequest request){ debug(request, "on_play_done"); return "on_play_done"; } @getmapping(value="/on_publish_done") public string onpublishdone(httpservletrequest request){ debug(request, "on_publish_done"); return "on_publish_done"; } @getmapping(value="/on_record_done") public string onrecorddone(httpservletrequest request){ debug(request, "on_record_done"); return "on_record_done"; } @getmapping(value="/on_update") public string onupdate(httpservletrequest request){ debug(request, "on_update"); return "on_update"; } private string debug(httpservletrequest request, string action){ string str = action + ": " + request.getrequesturi() + " " + request.getquerystring(); system.out.println(str); return str; } }
4. Running effect
(1) Start nginx and springboot
(2) The following is the springboot print information (you can briefly analyze it These logs)
on_connect: /v1/rtmp/on_connect app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&epoch=178269841&call=connect on_publish: /v1/rtmp/on_publish app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=541&call=publish&name=room&type=live on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=541&call=update_publish&time=10×tamp=3999&name=room on_done: /v1/rtmp/on_done app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=541&call=done&name=room on_publish_done: /v1/rtmp/on_publish_done app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=541&call=publish_done&name=room on_connect: /v1/rtmp/on_connect app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&epoch=178305623&call=connect on_publish: /v1/rtmp/on_publish app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=publish&name=room&type=live on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=10×tamp=7296&name=room on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=20×tamp=17248&name=room on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=30×tamp=27328&name=room on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=40×tamp=37280&name=room on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=50×tamp=47296&name=room on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=60×tamp=57312&name=room on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=70×tamp=67264&name=room on_connect: /v1/rtmp/on_connect app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&epoch=178380351&call=connect on_play: /v1/rtmp/on_play app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=557&call=play&name=room&start=4294966296&duration=0&reset=0&pass=12345 on_play_done: /v1/rtmp/on_play_done app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=557&call=play_done&name=room&pass=12345 on_done: /v1/rtmp/on_done app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=557&call=done&name=room&pass=12345 on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=80×tamp=77344&name=room on_connect: /v1/rtmp/on_connect app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&epoch=178388202&call=connect on_play: /v1/rtmp/on_play app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=563&call=play&name=room&start=4294966296&duration=0&reset=0&pass=12345 on_done: /v1/rtmp/on_done app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=563&call=done&name=room&pass=12345 on_play_done: /v1/rtmp/on_play_done app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=563&call=play_done&name=room&pass=12345 on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=90×tamp=87360&name=room on_connect: /v1/rtmp/on_connect app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&epoch=178396146&call=connect on_play: /v1/rtmp/on_play app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=569&call=play&name=room&start=4294966296&duration=0&reset=0&pass=12345 on_done: /v1/rtmp/on_done app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=569&call=done&name=room&pass=12345 on_play_done: /v1/rtmp/on_play_done app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=569&call=play_done&name=room&pass=12345 on_connect: /v1/rtmp/on_connect app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&epoch=178403666&call=connect on_play: /v1/rtmp/on_play app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=574&call=play&name=room&start=4294966296&duration=0&reset=0&pass=12345 on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=100×tamp=97311&name=room on_update: /v1/rtmp/on_update app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=574&call=update_play&time=10×tamp=105504&name=room&pass=12345 on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=110×tamp=107199&name=room on_done: /v1/rtmp/on_done app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=574&call=done&name=room&pass=12345 on_play_done: /v1/rtmp/on_play_done app=rtmp&flashver=&swfurl=&tcurl=rtmp://rtmp.wunaozai.com:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=574&call=play_done&name=room&pass=12345 on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=120×tamp=117344&name=room on_update: /v1/rtmp/on_update app=rtmp&flashver=fmle/3.0%20(compatible%3b%20fmsc/1.0)&swfurl=&tcurl=rtmp://120.24.210.62:8081/rtmp&pageurl=&addr=113.74.127.195&clientid=547&call=update_publish&time=130×tamp=122815&name=room
(3) The client performs push streaming. The push software here, I use this
(4) Mobile terminal , I use Tencent Video Cloud in the WeChat applet. This applet has rtmp test
## (5) nginx-rtmp log1 113.74.127.195 [05/aug/2018:16:18:08 +0800] publish "rtmp" "room" "" - 2646572 687 "" "fmle/3.0 (compatible; fmsc/1.0)" (1m 46s) 2 113.74.127.195 [05/aug/2018:16:19:49 +0800] play "rtmp" "room" "pass=12345" - 413 542 "" "" (4s) 3 113.74.127.195 [05/aug/2018:16:19:57 +0800] play "rtmp" "room" "pass=12345" - 413 542 "" "" (4s) 4 113.74.127.195 [05/aug/2018:16:20:05 +0800] play "rtmp" "room" "pass=12345" - 413 542 "" "" (4s) 5 113.74.127.195 [05/aug/2018:16:20:13 +0800] play "rtmp" "room" "pass=12345" - 413 542 "" "" (4s) 6 113.74.127.195 [05/aug/2018:16:30:39 +0800] play "rtmp" "room" "pass=12345" - 413 871 "" "" (4s) 7 113.74.127.195 [05/aug/2018:16:30:54 +0800] play "rtmp" "room" "pass=12345" - 413 647163 "" "" (12s) 8 113.74.127.195 [05/aug/2018:16:31:08 +0800] publish "rtmp" "room" "" - 4961955 409 "" "fmle/3.0 (compatible; fmsc/1.0)" (1m 30s) 9 113.74.127.195 [05/aug/2018:23:06:47 +0800] publish "rtmp" "room" "" - 425763 529 "" "fmle/3.0 (compatible; fmsc/1.0)" (13s) 10 113.74.127.195 [05/aug/2018:23:08:29 +0800] play "rtmp" "room" "pass=12345" - 413 871 "" "" (4s) 11 113.74.127.195 [05/aug/2018:23:08:37 +0800] play "rtmp" "room" "pass=12345" - 413 871 "" "" (4s) 12 113.74.127.195 [05/aug/2018:23:08:45 +0800] play "rtmp" "room" "pass=12345" - 413 871 "" "" (4s) 13 113.74.127.195 [05/aug/2018:23:09:05 +0800] play "rtmp" "room" "pass=12345" - 413 926026 "" "" (17s) 14 113.74.127.195 [05/aug/2018:23:09:30 +0800] publish "rtmp" "room" "" - 7061016 409 "" "fmle/3.0 (compatible; fmsc/1.0)" (2m 20s)
5. RTMP authentication method
For general commercial use, in order to prevent it from being used by others and for security reasons, RTMP needs to be authenticated. If the authentication is special, you can modify the source code of nginx-rtmp-module and then modify it. In fact, it means adding an auth function. This function can query the database and so on, and then decide whether to return 0 for success or -1 for failure. . In addition to the methods mentioned above, you can also use a simple method, which is the http callback mentioned above. If the http status code returned by the http callback is 2xx, it means success. If a 5xx status code is returned, it indicates failure. In that case, the server just disconnects the rtmp connection. It’s inrtmp://rtmp.wunaozai.com/rtmp_live/room?username=username&password=password
6. Others
Here are some configuration instructions and examplesapplication 创建一个rtmp应用,这里有点区别于http的location timeout 60s stocket超时,可以配合keepalive和ping值来实现不让服务器端长期处于监听连接客户端状态,实现快速关掉socket ping 3m ping_timeout 30s rtmp ping用于检查活动连接的协议。发送一个特殊的包远程连接,在ping_timeout指定时间内期待一个回复,如果没有收到回复,连接断开 max_streams 32 设置rtmp流的最大数目,单一流数据最大限制,一般默认的32就可以了 ack_window 5000000 设置rtmp窗口的大小 chunk_size 4096 数据块大小 设置值越大cpu负载就越小 max_queue 最大队列数,一般默认即可 max_message 1m 输入数据消息的最大大小。所有输入数据消息都会保存在内存中,等待完成流媒体转发。在理论上传入的消息可以是非常大,对服务器稳定性影响较大,所以一般默认即可。 out_queue out_cork buflen 5s 设置默认缓冲区长度。通常客户端发送播放前rtmp set_buflen命令并重置该设置 访问控制 access allow/deny 允许来自指定地址或者所有地址发布/播放 allow public 127.0.0.1 deny publish all; allow play 192.168.0.0/24 deny play all; exec命令 exce exec_options on; 启动一些exec指令选项,通过一些exec事件来干预整个rtmp流 可以仔细一些外部编解码功能 exec 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; exce_statc 类似exce,属于静态命令,不支持传递上下文参数 exec_kill_signal term; exec_kill_signal user1; exec_kill_signal 3; exec_pull exec_push exec_publish 指定与参数外部命令要在发布事件执行。 exec_play 指定与要在打开事件执行外部命令 exec_play_done 指定要在打开完成事件执行外部命令 exec_publish_done exec_record_done 例子 exec_play bash -c “echo $addr $pageurl >> /tmp/clients” exec_publish base -c “echo $addr $flashver >> /tmp/publishers” 转录 exec_record_done ffmpeg -y -i $path -acodec libmp31ame -ar 44100 -ac 1 -vcodec libx264 $dirname/$basename.mp4 live 模式 live on 切换直播模式,即一对多广播 meta on/copy/off 奇幻发送元数据到客户端 默认on interleave on/off 切换交叉模式。在该模式下,音视频会在同一个rtmpchunk流中传输。默认为off wait_key on|off 使视频流从一个关键帧开始,默认为off wait_video on|off 在一个视频帧发送前禁用音频。默认off 通过wait_key/wait_video进行组合以使客户端可以收到具有所有其他数据的视频关键帧。但这样会增加连接延迟。不过可以通过编解码器中调整关键帧间隔来减少延迟。 publish_notify on 发送netstream.publish.start和netstream.publish.stop给用户,默认off drop_idle_publisher 10s 终止指定时间内闲置(没有音频、视频)的发布连接,默认为off。注意这个仅仅对于发布模式的连接起作用(发送publish命令之后) sync 10ms 同步音视频流。如果用户带宽不足以接收发布率,服务器会丢弃一些帧。这将导致同步问题。当时间戳差超过sync指定值,将会发送一个绝对帧来解决这个问题,默认为300ms play_restart off 使nginx-rtmp能够在发布启动或者停止时发送netstream.play.start 和 netstrem.play.stop到每个用户。如果关闭的话,那么每个用户就只能在回放的开始结束时收到该通知了。默认为on record 模式 record off|all|audio|video|keyframes|manual 切换录制模式,流可以被记录到flv文件 off 不录制 all 录制音频和视频 audio video keyframes 只录制关键视频帧 manual 不自动启动录制,使用控制接口来进行启动和停止 record_path /tmp/rec 指定录制的flv文件存放目录 record_suffix -%d-%b-%y-%t.flv 录制后缀strftime格式 record_unique on|off 是否添加时间戳到录制文件,防止文件被覆盖,默认off record_append on|off 切换文件附加模式。开启后,录制时将新数据附加到旧文件后面。默认off record_lock on|off 锁定文件,调用系统的fcntl record_max_size 128k 设置录制文件的最大值 record_max_frames 2 设置每个录制文件的视频帧最大数量 record_interval 1s/15m 在这个指令指定的时间之后重启录制。默认off设置为0表示录制中无延迟。如果record_unique为off时所有的记录都会被写到同一个文件中。否则就会以时间戳区分在不同文件 record_notify on|off 奇幻当定义录制启动或者停止文件时发送netstream.record.start和netstream.record.stop状态信息onstatus到发布者。 应用 application rtmp{ live on; 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; } } 创建录制块。可以在单个application中创建多个记录 。 vod媒体 play dir|http://loc 播放指定目录或者http地址的flv或者mp4文件。注意http播放是要在整个文件下载完后才开始播放。同一个play可以播放多个视频地址(用于负载)。mp4格式要在编解码都被rtmp支持才可以播放。一般常见的就是h264/aac application vod{ play /var/flvs; } application vod_http{ play http://localhost/vod; } play_temp_path /www 设置远程vod文件完全下载之后复制于play_temp_path之后的路径。空值的话禁用此功能。 play_local_path dir 在播放前设置远程存储vod文件路径,默认/tmp play_local_path /tmp/videos; paly /tmp/videos http://localhost/videos 表示播放视频,先播放本地缓存,如果没有的话,从localhost/videos下载到本地/tmp/videos后,在进行播放 relay模式 pull url [key=value] 创建pull中继。主要是从远程服务器拉取流媒体。并进行重新发布。 url语法 [rtmp://]host[:port][/app[/playpath]] 如果application找不到那么将会使用本地application名,如果找不到playpath那么久用当前流名称。 参数如下(使用key=value方式) app 明确application名 name 捆绑到relay的bending流名称。如果为空,那么会使用application中所有本地流 tcurl pageurl swfurl flashver playpath live start stop static pull rtmp://cdn.example.com/main/ch?id=1234 name=channel; push url [key=value] 与pull类似,只是push推送发布流到远程服务器。 push_reconnect 1s 在断开连接后,在push重新连接钱等待的时间,默认3秒 session_relay on; 切换会话relay模式。在这种情况下关闭时relay销毁。 notify 模式 这个功能主要是提供http回调。当发送一些连接操作是,一个http请求异步发送。命令处理会被暂停挂起,知道它返回结果代码。当http返回2xx成功状态码时,rtmp会话继续。3xx状态码会使rtmp重定向到另一个从http返回头获取到的application,否则连接丢失。其他状态码,连接断开。目前用来做简单的鉴权。 on_connect url 设置http连接回调。当客户分发连接命令时。 例子: on_connect http://localhost/my_auth; location /on_connect{ if($arg_flashver != “my_secret_flashver”){ rewrite ^.*$ fallback?permanent; } } on_play url 设置http播放回调。分发客户分发播放命令时。 http { location /redirect { rewrite ^.*$ newname?permanent; } } rtmp{ application myqpp{ live on; on_play http://localhost/redirect; } } on_publish on_doone on_play_done on_publish_done on_record_done on_update notify_update_timeout 设置on_update回调时间 notify_update_strict on|off notify_relay_redirect on notify_method get 设置http方法通知,默认是application/x-www-form-urlencodeed 的post内容类型。有时候可能会需要get方法,在nginx的http{}部分处理调用。在这种情况下可以使用arg_*变量去访问参数。 例如如果是method为get时 location /on_play{ if($arg_pageurl ~* localhost){ return 200; } return 500; } hls 模式 hls on|off 使application 切换hls协议直播 hls_path /tmp/hls; 设置hls播放列表和分段目录。这一目录必须在nginx启动前就已经存在。 hls_fragment 15s; 设置hls分段长度,默认5秒,这个跟直播延迟有比较大的影响 hls_playlist_length 20m; 设置hls播放列表长度,默认30秒。这个跟直播缓存有关。 hls_sync time 设置hls时间戳同步阈值。默认2ms。这个功能防止由低分辨率rtmp(1khz)转换到高分辨率mpeg-ts(90khz)之后出现的噪音。 hls_continuous on|off 切换hls连续模式,默认off。 hls_nested on|off 切换hls嵌套模式。默认off。 hls_cleanup on|off; 切换hls清理。默认on accesslog日志 access_log off|path [format_name] log_format new_format ‘$remote_addr'; access_log logs/rtmp_access.log new_format; log_format 指定日志格式 创建指定的日志格式。日志格式看起来很像 nginx http 日志格式。日志格式里支持的几个变量有: * connection - 连接数。 * remote_addr - 客户端地址。 * app - application 名。 * name - 上一个流名。 * args - 上一个流播放/发布参数。 * flashver - 客户端 flash 版本。 * swfurl - 客户端 swf url。 * tcurl - 客户端 tcurl。 * pageurl - 客户端页面 url。 * command - 客户端发送的播放/发布命令:none、play、publish、play+publish。 * bytes_sent - 发送到客户端的字节数。 * bytes_received - 从客户端接收到的字节数。 * time_local - 客户端连接结束的本地时间。 * session_time - 持续连接的秒数。 * session_readable_time - 在可读格式下的持续时间。 默认的日志格式叫做 combined。这里是这一格式的定义: $remote_addr [$time_local] $command "$app" "$name" "$args" - $bytes_received $bytes_sent "$pageurl" "$flashver" ($session_readable_time) limits限制 max_connections number; 设置rtmp引擎最大连接数,默认off application hls{ live on; hls on; hls_path /tmp/hls; hls_fragment 15s; }
The above is the detailed content of How does Nginx-rtmp realize the real-time streaming effect of live media?. For more information, please follow other related articles on the PHP Chinese website!