How to setup RTMP server on NanoPC-T4

From FriendlyELEC WiKi
Revision as of 07:29, 16 May 2019 by Yftan (Talk | contribs)

Jump to: navigation, search

查看中文

1 Introduction

This article gives basic steps on how to use a RK3399 based board under Linux to collect real-time video stream data and push the data to a RTMP server which a user can visit and watch the video stream.
All the steps presented in this article are verifies with the following OS systems:

  • FriendlyCore
  • FriendlyDesktop

2 Setup a RTMP Server

You will use a RK3399 board as a RTMP server. You need to install the nginx and nginx-rtmp-module packages by running the following commands:

sudo apt-get update
sudo apt-get -y install nginx
sudo apt-get -y remove nginx
sudo apt-get clean
sudo rm -rf /etc/nginx/*
sudo apt-get install -y curl build-essential libpcre3 libpcre3-dev libpcre++-dev zlib1g-dev libcurl4-openssl-dev libssl-dev
sudo mkdir -p /var/www
rm -rf nginx_src
mkdir -p nginx_src
cd nginx_src
NGINXSRC=$PWD
wget http://nginx.org/download/nginx-1.13.8.tar.gz
git clone https://github.com/arut/nginx-rtmp-module.git
(cd nginx-rtmp-module/; git checkout -b ng1.13.8 791b6136f02bc9613daf178723ac09f4df5a3bbf)
tar -zxvf nginx-1.13.8.tar.gz
cd nginx-1.13.8
./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 --with-http_ssl_module --without-http_proxy_module --add-module=$NGINXSRC/nginx-rtmp-module
make
sudo make install

Check nginx's version and whether it is installed:

nginx -v

3 Configure a RTMP Server

Open and edit the nginx's configuration file:

sudo vim /etc/nginx/nginx.conf

Append the following lines in the file:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        application live {
            live on;
            record off;
        }
    }
}

Restart the nginx service and activate its configuration:

sudo systemctl restart nginx

4 Test RTMP Server

Before you begin to use your RTMP server to get stream data you need to make sure it works:

gst-launch-1.0 videotestsrc is-live=true ! videoconvert ! x264enc bitrate=1000 tune=zerolatency ! \
    video/x-h264 ! h264parse ! video/x-h264 ! queue ! flvmux streamable=true name=mux ! \
    rtmpsink sync=false location='rtmp://192.168.1.122:1935/live/test'

Under Ubuntu run the following rtmpdump command to retrieve stream data:

rtmpdump -r "rtmp://192.168.1.122/live/test" -V -z -o out.flv

If it can get data continuously and the output file out.flv can be played it means your RTMP server works.

5 Push Pictures Captured by OV13850 Camera to RMTP Server

There are two commands of which one only shows images but doesn't output audio and the other plays complete audio and video.
The one that only shows images but doesn't output audio:

gst-launch-1.0 rkisp io-mode=4 device=/dev/video0 ! \
    video/x-raw,format=NV12,width=1280,height=720,framerate=30/1 ! queue ! mpph264enc ! h264parse ! \
    flvmux streamable=true ! queue ! \
    rtmpsink sync=false location='rtmp://192.168.1.122:1935/live/test'

The other that plays both audio and video:

gst-launch-1.0 rkisp io-mode=4 device=/dev/video0 ! \
    video/x-raw,format=NV12,width=1280,height=720,framerate=30/1 ! queue ! mpph264enc ! h264parse ! \
    flvmux name=mux streamable=true alsasrc device=hw:0 ! queue ! \
    audioresample ! audio/x-raw,rate=48000 ! \
    queue ! voaacenc bitrate=32000 ! aacparse ! queue ! mux. mux. ! \
    rtmpsink sync=false location='rtmp://192.168.1.122:1935/live/test'

6 Push Pictures Captured by Logitech C922 Pro Camera to RMTP Server

6.1 Check USB Camera Node

Run the following command to check the USB camera device node:

uvcdynctrl -l

We used a Logitech C922 Pro camera and we can get the pictures captured by it from /dev/video8 :

  video8   C922 Pro Stream Webcam

6.2 Check Available Formats for USB Camera

Install necessary utilities:

sudo apt install v4l-utils

You can run the following command to check the available formats for a USB camera. For a RK3399 board you can focus on the MJPG format and its resolution and frame rate:

sudo v4l2-ctl --list-formats-ext -d /dev/video8

Note: the device node on your board might not be "/dev/video8" you need to make sure to use the node on your device.

6.3 Play Real-Time Video with USB Camera

Run the following command:

gst-launch-1.0 v4l2src device=/dev/video8 ! "image/jpeg,width=1280,height=720,framerate=30/1" ! jpegdec ! videoconvert ! queue ! mpph264enc ! queue ! h264parse ! flvmux streamable=true ! queue ! rtmpsink sync=false location='rtmp://192.168.1.122:1935/live/test'

7 Play Video Stream

7.1 Play on Smart Phone

If you use an iPhone you can install the SLDP Player utility which is free. Open this address "rtmp://192.168.1.122/live/test" with the utility and you will be able to view real-time video:
Rk3399-iphone-rtmp.jpg

7.2 Play on Web Page

We will create a web page and play a video on the page. Here are the steps:

mkdir -p ~/strobe_src
cd ~/strobe_src
wget http://downloads.sourceforge.net/project/smp.adobe/Strobe%20Media%20Playback%201.6%20Release%20%28source%20and%20binaries%29/StrobeMediaPlayback_1.6.328-full.zip
unzip StrobeMediaPlayback_1.6.328-full.zip
sudo cp -r for\ Flash\ Player\ 10.1 /var/www/html/strobe

Open and edit the /var/www/html/index.html file and add the following contents. Note: you need to replace the IP address with your actual IP address which can be obtained by running the ifconfig command:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>NanoPi Live</title>
    <script type="text/javascript" src="strobe/lib/swfobject.js"></script>
    <script type="text/javascript"> 
        // Create a StrobeMediaPlayback configuration 
        var parameters = {  
        // src: "http://players.edgesuite.net/videos/big_buck_bunny/bbb_448x252.mp4",  
        src: "rtmp://192.168.1.122/live/test", 
        autoPlay: true,  
            controlBarAutoHide: false,  
            playButtonOverlay: true,  
            showVideoInfoOverlayOnStartUp: false,  
            optimizeBuffering : false,  
            initialBufferTime : 0.1,  
            expandedBufferTime : 0.1,  
            minContinuousPlayback : 0.1,  
            poster: "strobe/images/poster.png"
        };  
 
        // Embed the player SWF:            
        swfobject.embedSWF
        ( "strobe/StrobeMediaPlayback.swf"
            , "strobeMediaPlayback"
            , 640
            , 480
            , "10.1.0"
            , {}
            , parameters
            , { allowFullScreen: "true"}
            , { name: "strobeMediaPlayback" }
        );
    </script>   
    </head>
    <body>
        <div id="strobeMediaPlayback">
          <p>Alternative content</p>
        </div>
    </body>
</html>

最后,在电脑上用浏览器打开网址 http://开发板的IP地址 即可进入播放界面,如下图所示:
Rk3399-rtmp.png

8 推送至斗鱼直播平台

GStreamer推送命令中的 location 参数是用来设定采集到的视频流推向哪里,通过设定这个参数可以将视频流推向任何支持 RTMP 协议的服务器。
斗鱼平台同样采用了 RTMP 协议传输直播视频,下面以斗鱼平台为例来说明一下推流到斗鱼的方法。

首先获取斗鱼的 RTMP 推流地址, 开启了直播室之后可以获得推流码,所下所示:
Douyu-location.png
把这两个参数组合起来(中间加上/)就是完整的地址:

rtmp://send3a.douyu.com/live/6012223rbUbZVVtN?wsSecret=221975cd093c763ab688f17ba8a7eb63&wsTime=5bf5315e&wsSeek=off&wm=0&tw=0

将完整的地址赋给location参数即可,命令示例如下:

gst-launch-1.0 rkisp io-mode=4 device=/dev/video8 ! \
    video/x-raw,format=NV12,width=1280,height=720,framerate=30/1 ! queue ! mpph264enc ! h264parse ! \
    flvmux name=mux streamable=true alsasrc device=hw:0 ! queue ! \
    audioresample ! audio/x-raw,rate=48000 ! \
    queue ! voaacenc bitrate=32000 ! aacparse ! queue ! mux. mux. ! \
    rtmpsink sync=false location='rtmp://send3a.douyu.com/live/6012223rbUbZVVtN?wsSecret=221975cd093c763ab688f17ba8a7eb63&wsTime=5bf5315e&wsSeek=off&wm=0&tw=0'

Douyu-live.png

9 其他

9.1 FriendlyDesktop取消自动锁屏

sudo apt-get -y remove light-locker

9.2 查看音频设备

aplay -L