[Develope]/Multimedia

How to deploy RTMP-Push

하늘을닮은호수M 2020. 10. 20. 15:09
반응형

Step 1. Install

Step 2 - Configure Nginx as a Service

Step 3. Configure

Step 4 - Testing

Step 1. Install

Reference : https://www.howtoforge.com/tutorial/how-to-install-nginx-with-rtmp-module-on-centos/

Install Dependencies

 

In this tutorial, we will build the Nginx web server from source. We need to install all packages dependencies needed, including development tools, EPEL repository, and other packages.

Install CentOS 'Development Tools'.

> sudo yum -y groupinstall 'Development Tools'

 

Add the EPEL repository.

> sudo yum -y install epel-release

 

Install Nginx dependencies.

> sudo yum install -y  wget git unzip perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel pcre-devel GeoIP GeoIP-devel

 

Wait for all packages installed.

Download Nginx with Additional Package and RTMP Module

 

In this step, we will download nginx source code with the additional dependencies including pcre, zlib, and the OpenSSL.

Go to the '/home/gigasurv/Downloads/nginx/build' directory. (설치 Directory)

> cd /home/gigasurv/Downloads/nginx/build

Download Nginx 1.14.0 and extract it.

> wget https://nginx.org/download/nginx-1.18.0.tar.gz 
> tar -xzvf nginx-1.18.0.tar.gz

Download the pcre package and extract it.

> wget https://ftp.pcre.org/pub/pcre/pcre-8.42.zip 
> unzip pcre-8.42.zip

Download the zlib package and extract it.

> wget https://www.zlib.net/zlib-1.2.11.tar.gz 
> tar -xzvf zlib-1.2.11.tar.gz

Download the OpenSSL package and extract it.

> wget https://www.openssl.org/source/openssl-1.1.1h.tar.gz 
> tar -xzvf openssl-1.1.1h.tar.gz

Next, clone the Nginx RTMP Module source code using git command.

> git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git 
> sudo git clone git://github.com/arut/nginx-rtmp-module.git

And remove all compressed tar.gz and .zip files.

> rm -f *.tar.gz *.zip

Following are the directory details we have.

 

> ls -lah

 

 

Configure & Make

  • Configure
> ./configure --prefix=/etc/nginx 
			--sbin-path=/usr/sbin/nginx 
            --modules-path=/usr/lib64/nginx/modules 
            --conf-path=/etc/nginx/nginx.conf 
            --error-log-path=/var/log/nginx/error.log 
            --pid-path=/var/run/nginx.pid 
            --lock-path=/var/run/nginx.lock 
            --user=nginx 
            --group=nginx 
            --build=CentOS 
            --builddir=nginx-1.18.0 
            --with-select_module 
            --with-poll_module 
            --with-threads 
            --with-file-aio 
            --with-http_ssl_module 
            --with-http_v2_module 
            --with-http_realip_module 
            --with-http_addition_module 
            --with-http_xslt_module=dynamic 
            --with-http_image_filter_module=dynamic 
            --with-http_geoip_module=dynamic 
            --with-http_sub_module 
            --with-http_dav_module 
            --with-http_flv_module 
            --with-http_mp4_module 
            --with-http_gunzip_module 
            --with-http_gzip_static_module 
            --with-http_auth_request_module 
            --with-http_random_index_module 
            --with-http_secure_link_module
            --with-http_degradation_module 
            --with-http_slice_module 
            --with-http_stub_status_module
            --http-log-path=/var/log/nginx/access.log 
            --http-client-body-temp-path=/var/cache/nginx/client_temp 
            --http-proxy-temp-path=/var/cache/nginx/proxy_temp 
            --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 
            --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
            --http-scgi-temp-path=/var/cache/nginx/scgi_temp 
            --with-mail=dynamic 
            --with-mail_ssl_module 
            --with-stream=dynamic 
            --with-stream_ssl_module 
            --with-stream_realip_module 
            --with-stream_geoip_module=dynamic 
            --with-stream_ssl_preread_module 
            --with-compat 
            --with-pcre=../pcre-8.44 
            --with-pcre-jit 
            --with-zlib=../zlib-1.2.11 
            --with-openssl=../openssl-1.1.1h 
            --with-openssl-opt=no-nextprotoneg 
            --add-module=../nginx-rtmp-module 
            --with-debug

 

  • make
make 과정 중에 rtmp module의 switch case 문에서 implicit-fallthrough 에러가 발생하는데, 문법 버전 error로 보이는데 명확한 이유를 확인할 수 없어 우선 no-error처리하였다. 설치 home directory(나의 경우/home/gigasurv/Downloads/nginx/build) 하위의 nginx-1.18.0/nginx-1.18.0/Makefile CFLAGS에 -Wno-error=implicit-fallthrough 를 추가한다.
CC = cc CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -Wno-error=implicit-fallthrough CPP = cc -E LINK = $(CC) ...

 

  • make install

 

> make install

 

Create nginx symlink module to the '/etc/nginx' configuration directory.

 

> sudo ln -s /usr/lib64/nginx/modules /etc/nginx/modules

 

Create a new 'nginx' system user and group.

> sudo useradd -r -d /var/cache/nginx/ -s /sbin/nologin -U nginx

 

Now create a new Nginx cache directory '/var/cache/nginx' and change the owner of the directory to 'nginx' user and group.

> sudo mkdir -p /var/cache/nginx/ 
> sudo chown -R nginx:nginx /var/cache/nginx/

 

Test nginx configuration and the installed nginx version.

> nginx -t 
> nginx -V

And the following is the result.

The Nginx web server has been installed on CentOS 7 with the RTMP Module enabled.

Step 2 - Configure Nginx as a Service

In this tutorial, we will be running nginx as a service and we need to create a new nginx service file to the systemd service directory.

Go to the '/lib/systemd/system' directory and create a new 'nginx.service' file using vim.

> cd /lib/systemd/system/ 
> vim nginx.service

 

paste the configuration below.Advertisement

[Unit] 
Description=nginx - high performance web server 
Documentation=https://nginx.org/en/docs/ 
After=network-online.target remote-fs.target nss-lookup.target 
Wants=network-online.target  

[Service] 
Type=forking 
PIDFile=/var/run/nginx.pid 
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf 
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf 
ExecReload=/bin/kill -s HUP $MAINPID 
ExecStop=/bin/kill -s TERM $MAINPID  

[Install] 
WantedBy=multi-user.target

 

Save and exit.

Now reload the systemd system.

 

> systemctl daemon-reload

 

Start the nginx service and enable it to launch everytime at system boot.

> systemctl start nginx 
> systemctl enable nginx

The Nginx web server is up and running as a service on CentOS 7 system.

 

Step 3. Configure

Configure Nginx RTMP Module

In this step, we will create a new custom Nginx configuration for RTMP module.

Go to the '/etc/nginx' configuration directory and backup the original 'nginx.conf' file.

> cd /etc/nginx/ 
> mv nginx.conf nginx.conf.asli

Now create a custom configuration 'nginx.conf'.

> vim nginx.conf

Paste Nginx RTMP configuration below.

```
worker_processes  auto;
events {
    worker_connections  1024;
}

# RTMP configuration
rtmp {
    server {
        listen 1935; # Listen on standard RTMP port
        chunk_size 4000;

        application show {
            live on;
            # Turn on HLS
            hls on;
            hls_path /var/www/hls/;
            hls_fragment 3;
            hls_playlist_length 60;
            # disable consuming the stream from nginx as rtmp
            deny play all;
        }

        application live {
            live on;
        }

        # RTMP video on demand for mp4 files
        application vod {
            play /var/www/vod;
        }
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        #server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            root   /var/www;
            index  index.html index.htm;

            # Disable cache
            add_header 'Cache-Control' 'no-cache';

            # CORS setup
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length';

            # allow CORS preflight requests
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }

            types {
                application/dash+xml mpd;
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
        }

		} 

		# Another Virtual Server ...
}
```

 

Save and exit.

Next, we need to create a new directory for the HLS configuration and we've defined the web root directory is on the '/var/www/hls' directory.

Create the 'hls' directory under the '/var/www/hls' directory and change the owner of the directory to the nginx user and group.

> mkdir -p /var/www/hls 
> chown -R nginx:nginx /var/www/hls

 

Test the configuration and restart the nginx service.

> nginx -t 
> systemctl restart nginx

 

Setup First RTMP Live Stream

In this tutorial, we will create new RTMP stream video on demand using the mp4 videos on the server, and create a new live RTMP stream that will be broadcasted from the local computer using the OBS software.

Go to the '/etc/nginx' configuration directory and edit the 'nginx.conf' file.

> cd /etc/nginx/ 
> vim nginx.conf

 

Paste configurations below in to the 'rtmp { ... }' bracket.

```
        # RTMP video on demand for mp4 files
        application vod {
            play /var/www/vod;
        }

        # RTMP stream using OBS
        application stream {
            live on;
        }
```

 

Save and exit.

Now create a new directory 'vod' for storing all vod videos, and change the owner to the nginx user group.

> mkdir -p /var/www/vod 
> chown -R nginx:nginx /var/www/vod

 

Test nginx configuration and make sure there is no error, then restart the nginx service.

> nginx -t 
> systemctl restart nginx

Nginx configuration for the RTMP live stream and the vod stream has been completed.

Advertisement

 

 

Open port 80 and 443 using firewall-cmd

You must open and enable port 80 and 443 using the firewall-cmd command:

> sudo firewall-cmd --permanent --zone=public --add-service=http --add-service=https 
> sudo firewall-cmd --reload 
> sudo firewall-cmd --list-services --zone=public 
dhcpv6-client http https nfs ntp samba ssh

Step 4 - Testing

Test RTMP live stream and vod stream using the VLC player.

Video On Demand Stream

Open the VLC app on your computer.

Click the 'File' menu choose the 'Open Network' option.

Now type the RTMP URL for our vod stream.

rtmp://host_address:1935/vod/file.mp4

Click the 'Open' button.

And the following are the results of the video stream.

 

 

Live Stream

 

  • 영상 송출
> ffmpeg -re -i ~/Videos/seren.mp4 -loop -1 -c:v libx264 -b:v 3M -pix_fmt yuv420p -c:a:0 libfdk_aac -b:a:0 480k -f flv rtmp://host_address/show/stream
- i : Input. ~/Videos/seren.mp4
- loop : Movie 재생 횟수. -1은 infinitely
- c:v : Video Codec. libx264, H.264 Codec
- b:v : Video Bandwidth
- c:a : Audio Codec. libfdk_aac. AAC Codec
- b:a : Audio Bandwidth
- f : File Format or Container
- 출력 (rtmp push address) : rtmp://host_address/show/stream

 

영상이 제대로 송출되고 있음을 표시한다.

ffmpeg을 통한 RTMP-Push (to host_address)

nginx.conf에 설정한 위치 (/var/www/hls)에 정상 파일 저장되고 있음을 확인한다.

 

  • 영상 확인 (/w vlc client)

실시간 영상이 클라이언트에서 정상 재생됨을 확인한다.

재생 주소 : http://host_address:port/hls/stream.m3u8

 

 

반응형

'[Develope] > Multimedia' 카테고리의 다른 글

Wowza + 360 Giroptic Cam 연결  (0) 2017.02.28
ffmpeg 설치  (0) 2012.01.27
Gstreamer  (0) 2008.08.08
Getting Started with Adobe Flash Media Server 3  (0) 2008.06.09
Darwin Streaming Server  (0) 2007.12.20