樹莓派安裝 nginx 架設 RTMP做即時影像串流
1. 安裝nginx
先安裝nginx編譯時需要用到的套件
「sudo apt-get install -y curl build-essential libpcre3-dev libpcre++-dev zlib1g-dev libcurl4-openssl-dev libssl-dev」
2. 下載nginx跟nginx-rtmp-module原始碼
「git clone https://github.com/arut/nginx-rtmp-module.git」
「wget http://nginx.org/download/nginx-1.15.0.tar.gz」
3. 重新編譯
解壓縮.tar檔,並將command 路徑cd ./nginx-1.15.0 底下後,執行configure將rtmp module加入後執行make開始重新編譯,最後make install安裝
「./configure --add-module=/home/pi/nginx-rtmp-module」
「make」
「make install」
(如果想改變路徑,指令 「./configure --prefix=/var/www --sbin-path=/usr/sbin/nginx
--conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log --without-http_proxy_module --add-module=/home/pi/nginx_src/nginx-rtmp-module」)
4. 開啟nginx
安裝完的nginx預設路徑在/usr/local/nginx,接著執行 「/usr/local/nginx/sbin/nginx」 就開啟nginx了
如果沒問題的話,進入http://localhost就會有歡迎的畫面了
也可以 「netstat -ntlp | grep 80」 查看80port有無被nginx使用中
killall nginx就關閉了
「/usr/local/nginx/sbin/nginx –s stop」 也是關閉
「/usr/local/nginx/sbin/nginx –s reload」 重啟
5. 設定rtmp
1. 編輯/usr/local/nginx/conf/nginx.confog
加入rtmp、hls的設定 (hls是啥就自己google囉)
rtmp {
server {
listen 1935;
chunk_size 4096;
application demo{
live on;
}
application hls {
live on;
hls on;
hls_path /tmp/hls; #緩衝目錄
hls_fragment 5s; #HLS分段切片秒數設定5秒(會超卡,15秒好些)
hls_playlist_length 30s; #播放清單30秒
deny play all; #只能推送影像,連線請走http,不開放透過rtmp取得此服務
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile off;
tcp_nopush on;
directio 512; #直接存取
server {
listen 80; #port
server_name localhost;
location / {
root /usr/local/share/nginx/html;
index index.html index.htm;
}
location /hls {
# Disable cache
add_header 'Cache-Control' 'no-cache';
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
#add_header 'Access-Control-Allow-Headers' 'Range';
#add_header 'Content-Type' 'text/plain charset=UTF-8';
#add_header 'Content-Length' 0;
types {
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
}
}
}
6. 推送影像
1. 安裝串流編碼器 「apt-get install libav-tools」, 原本想安裝ffmpeg但跳出"ffmpeg is not avaliable...",avconv跟ffmpeg的愛恨情仇自己google
2. avconv推流到nginx,記得加上-vcodec libx264 用H,264編譯,流量小很多!
若選擇啟動rtmp
「avconv -f video4linux2 -framerate 24 -i /dev/video0 -s 640x480 -vcodec libx264 -f flv rtmp://localhost:1935/demo/test」
若選擇啟動hls
「avconv -f video4linux2 -framerate 24 -i /dev/video0 -s 640x480 -vcodec libx264 -f flv rtmp://localhost:1935/hls/test」
3.安裝個VLC測試有沒開通,檔案>開啟網路串流
若選擇啟動rtmp
rtmp://127.0.0.1:1935/demo/test
若選擇啟動hls
rtmp://127.0.0.1:1935/hls/test
http://127.0.0.1/hls/test.m3u8 (不穩,看到畫面後撐不到一分鐘就斷了Orz)
備註:
「sudo apt-get install htop」, 比較好觀察效能
「v4l2-ctl --all」 查camera資料
結論
1. 使用avconv解碼1080p/1fps時,cpu使用率100%...
2. 可以嘗試做硬體加速,參考資料8,改用gstreamer試試看
3. 用raspberry做stream有夠卡,但目前是靠wifi,還沒試過有線。不過pi 3B網路頻寬就10/100mbps,或許用pi 3B+ 10/100/1000mbps會好點(?)
後記
1. 安裝Gstreamer「sudo apt-get gstreamer1.0-tools」後,下指令
簡單版:
「gst-launch-1.0 -v v4l2src device=/dev/video0 ! 'video/x-raw, width=1920, height=1080, framerate=30/1' ! queue ! videoconvert ! omxh264enc ! h264parse ! flvmux ! rtmpsink location='rtmp://localhost:1935/demo/test live=1'」
或複雜版增加一些設定參數:
「gst-launch-1.0 -v v4l2src device=/dev/video0 ! 'video/x-raw, width=1920, height=1080, framerate=30/1' ! queue ! videoconvert ! omxh264enc target-bitrate=2000000 control-rate=variable ! video/x-h264, stream-format=byte-stream, width=1920, height=1080, framerate=30, profile=high ! h264parse ! flvmux ! rtmpsink location='rtmp://localhost:1935/demo/test live=1'」
加音源版:
「gst-launch-1.0 -v v4l2src device=/dev/video0 ! 'video/x-raw, width=1920, height=1080, framerate=30/1' ! omxh264enc target-bitrate=2000000 control-rate=variable ! video/x-h264, stream-format=byte-stream, width=1920, height=1080, framerate=30, profile=high ! h264parse ! flvmux name=mux alsasrc device=hw:1 ! audioresample ! audio/x-raw,rate=48000 ! voaacenc bitrate=32000 ! queue ! mux. mux. ! rtmpsink location='rtmp://localhost:1935/hls/demo live=1'」
※ omxh264enc在1080p/30fps時cpu使用率維持在20~25%,硬體加速真的有差!但解析度在1080p時,底下會出現"green band"。爬文顯示omxh264enc是以16的倍數設定解析度的,有人說改1920*1088可解,但還是一樣,嘗試換用x264enc、openh264enc。
2. x264enc藏在ugly分類下,先安裝「sudo install apt-get gstreamer1.0-plugins-ugly」,安裝完後「gst-inspect-1.0 | gerp "264"」看到有x264就代表成功了!
下指令「gst-launch-1.0 -v v4l2src device=/dev/video0 ! 'video/x-raw, width=1920, height=1080, framerate=30/1' ! x264enc tune=zerolatency ! video/x-h264,stream-format=byte-stream,width=1280,height=720,framerate=30/1,profile=high ! h264parse ! flvmux ! rtmpsink location='rtmp://localhost:1935/hls/test live=1'」
※ x264enc在1080p/30fps時cpu使用率維持在65~75%,運作正常,礙眼的「green band」消失了!確定是omxh264enc編碼器的問題!
不過呢,x264enc 就算降到480p使用率也維持在65~75%!相較之下omxh264enc是比較省CPU資源的
3. openh264enc根據參考資料[11],"Currently Debian does not package openh264 at all, so the plugin in the gst-plugins-bad package can also not be enabled.",要自己手動裝,就留著讓有心人去測試吧XD。
4. 比對延遲跟效能
avconv編碼 |
1920*1080/30fps |
1280*720/30fps |
640*480/3fps |
rtmp |
無法撥放 |
無法撥放 |
40s,80% |
rtmp/hls |
無法撥放 |
無法撥放 |
不想試了 |
gst-omxh264enc編碼 |
1920*1080/30fps |
1280*720/30fps |
640*480/30fps |
rtmp |
22s, 20%(green bar) |
13s, 20% |
20s, 4% |
rtmp/hls |
20s, 20%(green bar) |
13s, 20% |
15s, 4% |
gst-x264enc編碼 |
1920*1080/30fps |
1280*720/30fps |
640*480/30fps |
rtmp |
70s, 70% |
70s, 70% |
35s, 70% |
rtmp/hls |
45s, 70% |
65s, 70% |
35s, 70% |
※肉眼實際上沒有到30fps
5. ffmpeg用openMax h264硬體加速「ffmpeg -f video4linux2 -framerate 30 -i /dev/video0 -s 1280x720 -vcodec h264_omx -profile:v high -b:v 3000k -f flv rtmp://localhost:1935/hls/test」
若「ffmpeg -encoders」中沒有h264_omx則自行重編ffmpeg[ref]
6. 2021-03-12: v4l2h264dec instead of omxh264dec[ref] [ref]
其他參考資料
1. Nginx通过/etc/init.d/nginx方式启动或停止服务, https://blog.csdn.net/Aaroun/article/details/78218131
2. Ubuntu下Nginx安装, https://www.jianshu.com/p/7cb1a824333e
3. 树莓派nginx+rtmp搭建直播流媒体服务, https://my.oschina.net/yehun/blog/1633459
4. nginx-rtmp-module和ffmpeg搭建流媒体服务器, https://blog.csdn.net/happybird100/article/details/50742766
5. 樹莓派架設 RTMP 串流伺服器, https://blog.gtwang.org/iot/raspberry-pi-nginx-rtmp-server-live-streaming
6. RTMP直播服務(2/3) : 設定 NGINX HLS 直播服務, http://blakew88.blogspot.com/2017/04/rtmp23-nginx-hls.html
7. Raspberry Pi Camera RTSP 影像串流, http://blog.jex.tw/blog/2014/01/22/raspberry-pi-camera-rtsp-video-streaming/
8. Hardware-accelerated video playback on the Raspberry Pi, https://wiki.matthiasbock.net/index.php/Hardware-accelerated_video_playback_on_the_Raspberry_Pi
9. GStreamer指令, https://gstreamer.freedesktop.org/documentation/plugins.html
10. Live Video Streaming with Raspberry Pi camera module, https://wiki.jmk.hu/wiki/Live_Video_Streaming_with_Raspberry_Pi_camera_module
11. Missin openh264enc plugin in Linux, http://gstreamer-devel.966125.n4.nabble.com/Missin-openh264enc-plugin-in-Linux-td4679146.html
12. x264 API reference, https://thiblahute.github.io/GStreamer-doc/x264-1.0/index.html?gi-language=c#GstX264EncTune
13. Setting up HLS live streaming server using NGINX + nginx-rtmp-module on Ubuntu, https://docs.peer5.com/guides/setting-up-hls-live-streaming-server-using-nginx/ (<=基本上遵循這篇就很ok了!)