Use nginx to build http-flv live streaming media server on virtual machine CentOS 7.0

References

Build a live streaming media server based on http-flv through nginx
CentOS 7 installation and configuration git
VMwear installation Centos7 ultra-detailed process
nginx +rtmp+nginx-http-flv-module environment build
CentOS7 view and close the firewall

I encountered some pitfalls when referring to the above materials, so I will skip the places without pitfalls (directly refer to the above materials), and I will explain in detail if there are pitfalls or different places.

basic workflow

  1. The video source (including any device for live broadcasting: computer, mobile phone, webcam) "pushes" the video data to a certain (port + address) on the streaming server
  2. The video receiver "pulls" video data through another (port + address) to watch live content

In this example:

text

flow media services

Install VMware and CentOS 7.0

Download and install VMware and CentOS 7.0, the steps refer to the above reference materials. Sign up and do it yourself.

After turning on the network card during the installation process, you need to write down the obtained IP address (you can also check it after the installation is complete), which will be used later.

Note that CentOS 7.0 installed in this way lacks some functions (mainly the c compiler). After logging in as root, execute the following command:

yum -y install unzip
yum -y install gcc-c++ 
yum -y install pcre pcre-devel  
yum -y install zlib zlib-devel 
yum -y install openssl openssl-devel

install git

Refer to the above information, and note that the user name and email address in the configuration information should be changed to your own.

Download Nginx and nginx-http-flv-module source code

We need to put all the source code in the temporary folder, enter the temporary folder

cd /tmp

Clone nginx-http-flv-module source code

git clone https://github.com/winshining/nginx-http-flv-module.git

Download Nginx source code

wget https://nginx.org/download/nginx-1.19.9.tar.gz

Note that the version number of nginx may be updated here (see the link above), decide by yourself, and the version number involved in the future needs to be consistent. Unzip
and enter the folder

tar -xzf nginx-1.19.9.tar.gz
cd nginx-1.19.9

Compile and install

Execute sequentially in the nginx folder

./configure --add-module=/tmp/nginx-http-flv-module --with-http_ssl_module
make
make install

After execution, nginx will be installed in /usr/local/nginx

Modify the nginx configuration file

In this example, CentOS is installed with a graphical interface. It is recommended to use the graphical interface for this step.

Log in as the root user, open the /usr/local/nginx/conf/nginx.conf file, see the first link in the reference for editing content

The part modified from the default configuration is

Added

rtmp {
    
    
    server {
    
    
        listen 9999;  # 接受推流的端口号
        chunk_size 8192; # 单一推流数据包的最大容量

        application myapp {
    
     # myapp 模块,可以自行更换名字
            live on; # 打开直播
            meta off; # 为了兼容网页前端的 flv.js,设置为 off 可以避免报错
            gop_cache on; # 支持GOP缓存,以减少首屏时间
            allow play all; # 允许来自任何 ip 的人拉流
        }
    }
}

Modify the listen port of http/server to 8080, and add it under http/server

location /live {
    
     # 拉流时的 uri ,可以自行修改
            flv_live on; # 打开 http-flv 服务
            chunked_transfer_encoding on;
            add_header 'Access-Control-Allow-Origin' '*'; # 允许跨域
            add_header 'Access-Control-Allow-Credentials' 'true';
        }

save and exit

Start Nginx

cd /usr/local/nginx/sbin
./nginx

The push and pull addresses after successful startup are:

  • Stream: rtmp://localhost:9999/myapp/mystream
  • 拉流:http://localhost:8080/live?port=9999&app=myapp&stream=mystream

Among them:
localhost needs to be replaced with the IP address of the CentOS system recorded or checked before.
live is the uri of the location added in the nginx configuration file.
myapp is the name of the app module specified in the nginx configuration file, which appears in configuration files, push streams, and pull streams. The names of
mystream push streams need to be consistent , and the names that appear in push streams and pull streams need to be consistent

firewall

Successful streaming needs to go through the Linux firewall. In this example, choose to close the firewall directly (see the reference at the beginning). In actual applications, you should modify the firewall configuration to open the ports used for pushing and pulling streams (9999 and 8080 in this example).

So far the server is ready

OBS thrust

Install and start OBS, click the plus sign at the bottom of the "source", add a "monitor capture", confirm directly, at this time you should see the real-time screen of the computer screen on the main interface of OBS

Then select: File - Settings - Streaming (on the left), in the right window

Select "Custom" for service, fill in rtmp://localhost:9999/myapp/ for "Server", fill in mystream for "Stream Key" (the actual content must be consistent with the previous one), confirm to exit

Click "Start streaming" in the lower right corner. After the streaming is successful, the button will change to "Stop streaming", and the real-time traffic data per second will appear in the lower right corner of the status bar

So far the streaming is complete

VLC, flv.js pull stream, play

VLC

Media - Open the network stream, fill in the streaming URL above (the actual content must be consistent with the previous article), play

flv.js

Open the demo page of flv.js and fill in the stream URL above in the Stream URL, and click Load below

Guess you like

Origin blog.csdn.net/hjg719/article/details/115347365