This is the 26th day of my participation in the August Text Challenge.More challenges in August


My column

  1. Explore the underlying principles of iOS
  2. Summary of iOS underlying principles of exploration

preface

In fact, I have always wanted to do a simple set of live broadcast (including mobile terminal and service terminal) development test, but I was quite confused before. Recently, inspiration came to me by accident, which instantly relieved me of many doubts. I will share it and I hope you can study it together. I’ll post a full tutorial later when IT’s done. Today, we first set up the server for the live broadcast part.

1. Install Homebrow

Homebrew, short for BREW, is a software package management tool on Mac OSX, which can easily install or uninstall software in Mac. It can be said that Homebrew is apt-get and YUM magic tools under Mac

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Copy the code

If you’ve installed it and want to uninstall it

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
Copy the code

2. Install the Nginx server

brew tap homebrew/nginx
Copy the code

Added extensions to Nginx; Home-brew extension to nginxnx, downloaded from github.

3. Install the Nginx server and RTMP module

brew install nginx-full --with-rtmp-module
Copy the code

This installation, time-consuming is relatively long. Now that the Nginx and RTMP modules are installed, let’s configure the RTMP module for Nginx.

brew info nginx-full
Copy the code

Nginx installation location/usr/local/Cellar/nginx – full / 1.10.1 / bin/nginx nginx configuration file location/usr/local/etc/nginx/nginx. Conf nginx server root directory location /usr/local/var/www

Run the command to test whether the nginx service can be successfully started

nginx
Copy the code

Click http://localhost:8080 to see Welcome to Nginx! , the nginx installation is successful.

4. Configure RTMP

Open the nginx with Xcode. Conf, find/usr/local/etc/nginx/nginx. Conf file, dragged into the Dock in Xcode, you can open.

Description of document contents:

  • RTMP is the protocol name
  • Server Indicates internal configurations related to servers
  • Listen Listens to the port number. The default port number for the RTMP protocol is 1935
  • Application Access the application path is ZBCS
  • Live on starts real-time
  • Record off Does not record data

5. After saving the file, reload the Nginx configuration file

nginx -s reload
Copy the code

# This step is important

6. Install ffMEPG

brew install ffmpeg
Copy the code

Installing this will take a while, so you can prepare a video file to stream and install a video player that supports the RTMP protocol. You can use VLC on Mac.

7. Push the flow through ffmepg command

Ffmpeg - re - the absolute path I your video files (such as/Users/lideshan/Downloads/Demo. Mp4) - vcodec copy -f FLV RTMP: / / localhost: 1935 / ZBCS/roomCopy the code

Here ZBCS is the path name of the application configured in the configuration file above; I can write whatever room I want.

8. Verify the video

Then Open the player software VLC on the computer, click File—–Open Network, select Network from the box that pops up and enter the URL:

rtmp://localhost:1935/zbcs/room
Copy the code

Next the HLS live stream configuration

 sudo /usr/local/nginx/sbin/nginx -s stop
 sudo /usr/local/nginx/sbin/nginx
Copy the code

Locate HTTP —- Server and add it in curly braces

server {
    listen    8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location/ { root html; index index.html index.htm; } # HLS configuration, this configuration in order to "client" to the HTTP protocol for HLS flow location/HLS {# Serve HLS fragments could types {application/VND. Apple. Mpegurlm3u8; video/mp2t ts; } root html; add_ header Cache-Control no-cache; } #HLS configuration end #error_page404  /404.html;


    # redirect server error pages to the static page /
    50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
        root   html;
    }
    
    # proxy the PHP scripts to Apache listening on
    127.0. 01.:80
    #
    #location ~ \.php$ {
    #   proxy_pass   http:/ / 127.0.0.1;
    #}
Copy the code

Find server under RTMP added in curly braces

rtmp {
    server {
        listen 1935; application zbcs { live on; revord off; } application live { live on; revord off; } # add support for HLS. hls on; hls_path /usr/local/var/www/hls; hls_fragment 5s; } # add support for HLS end}}Copy the code

Description:

  • Live on Enables real-time
  • HLS on Enables HLS
  • Hls_path Path for storing TS files
  • Hls_fragment 5s Each TS file contains 5 seconds of video content

HLS live delay

We know that HLS protocol divides live stream into small video segments to download and play. Therefore, assuming that the list contains 5 TS files, and each TS file contains 5 seconds of video content, the overall delay is 25 seconds. Because when you see these videos, the host has already recorded the video and uploaded it, so the delay caused by this. Of course can shorten the length of the list and the size of a single ts file to reduce the delay, acme can cut list of length 1, and ts the length of the 1 s, but this will cause the request times increase, increase the pressure of the server, when low-bandwidth back to cause more buffer, so apple’s recommended the ts the length of 10 s, So this will be a big change with a delay of 30 seconds. The resources

Save the configuration file and reload the Nginx configuration

nginx -s reload
Copy the code

To push the flow

The ffmPEG push stream is the same as the previous one, however, we need to push it to the newly configured HLS, and the movie keyword can be replaced any way

ffmpeg -re -i /Users/jiangys/Documents/Document/demo.mp4 -vcodec copy -f flv rtmp://localhost:1935/hls/movie
Copy the code

Then we can go to this directory (which is also the default HTML configuration file for Nginx)

/usr/local/var/www/hls
Copy the code

If you see a ts file generated, you will also see a.m3u8 file generated

Test flow

With the above configuration, we can see the outgoing stream through both RTMP and HLS playback modes. Note that if you use HTTP, you are listening on port 8080, which is written in the configuration file

  • 1 use RTMP :(use VLC to verify playback)
RTMP: / / 192.168.1.109 / HLS/movieCopy the code
  • 2 Use HLS to play
http://192.168.1.109:8080/hls/movie.m3u8
Copy the code

Supplement:

/usr/local/var/ WWW = /usr/local/var/ WWW = /usr/local/var/ WWW = /usr/local/var/ WWW

Finally, we will use Nginx and RTMP to build live and on-demand video servers

Using the Nginx and RTMP modules, it is easy to set up a live and on-demand video server. Nginx and RTMP modules have been installed to enable Nginx live video streaming and on-demand services. If you need to support multiple streams for live video streaming, you can simply add several applications to the Nginx configuration file, as follows:

In this way, the live stream can be pushed through the following address, and the audience can also access the live stream through the following address:

RTMP: / / 192.168.31.185 / live/test RTMP: / / 192.168.31.185 live2 / testCopy the code

The test keyword, which is followed by the test keyword, can be changed side by side, as long as your push stream has the same address as the access stream.

HLS we have configured previously, push stream can use the following address

RTMP: / / 192.168.31.185 / HLS/movieCopy the code

For viewers, there are several ways to play: RTMP:

RTMP: / / 192.168.31.185 / HLS/movieCopy the code

Play with HLS:

http://192.168.31.185:8080/hls/movie.m3u8
Copy the code

This allows you to see the stream exiting from the anchor side. Note that if you use HTTP, you are listening on port 8080, which is written in the configuration file. The web player plugin, in the second part, allows you to view videos directly by opening urls in the browser, and to write a web page to achieve video-on-demand services like Youku. This can be done by using a third-party player, such as JWPlayer, to embed the player in the web page. Download the JWPlayer and place it in the root directory of the Nginx web service. Unzip JWPlayer into this directory and create a new play. HTML text file in this directory.