http-flv Live Module (nginx-http-flv-module) based on Nginx-rtmp-module module implementation

Source: Internet
Author: User
Tags unix domain socket cpu usage

The following content of this article will be updated here: "http-flv Live Module (nginx-http-flv-module) based on Nginx-rtmp-module module" continued. Note: Many of the configurations below are no longer available because the implementation is somewhat different from the earlier implementations. In order to see the history of the entire project, it has been retained, and the following update mentions why some of the configuration items are no longer available. The configuration you are using now looks for README.CN.MD in the following section.



In recent years, live industry hot, open source Live software solution has SRS (Simple-rtmp-server) and Nginx-rtmp-module, the former is a good open source project launched by the Chinese, many domestic companies are now using it as a live solution, written by C + + The latter relies on Nginx to provide the live broadcast function in the form of a third-party module, written by C. SRS using multi-threaded way (after the user reminds correction: is single-threaded + co-process mode), excellent performance, withstood the test of many scenes, but SRS3 has closed source (correction: there is a period of time closed source, and now open source); Nginx-rtmp-module is a multi-process approach, Nginx performance is excellent, but according to the Netizen test, nginx-rtmp-module performance is inferior to SRS, and the author of Nginx-rtmp-module has not updated the version for a long time, support function is also limited, for example, do not support HTTP mode of FLV Live, And this is the domestic broadcast industry is widely used in the way, such as the push stream does not support upstream, distributed deployment capabilities, there is a lot of criticism of the playback response delay time is very long (known as the non-second broadcast) and so on.



On the basis of nginx-rtmp-module, I realized the FLV live function based on HTTP, support GOP cache, reduce the delay time of playback response, and support streaming and transfer-encoding:chunked two kinds of HTTP response formats. Fix Nginx-rtmp-module No listen configuration item, push stream fails, fix nginx-rtmp-module known bug, see Nginx-http-flv-module, Welcome to download test and fix bug. There are questions or suggestions, you can add Q Group: 711969608 chat. There are already manufacturers ready to use this module for commercial, currently known to have 9, according to another manufacturer's employees said their customers including the mirror and micro-roar, the other is currently in the test, there are many manufacturers have feedback a lot of bugs, after the repair function has become more and more stable, in this expressed thanks.






Comparison of Nginx-http-flv-module and Nginx-rtmp-module features:


Function Nginx-http-flv-module Nginx-rtmp-module Note
http-flv X Nginx-http-flv-module Support https-flv
GOP cache X
Vhost X
Omit Listen configuration X
RTMP 302 Beta X Nginx-http-flv-module as a server or client


If you do not want to push the stream, you can use a ready-made live address rtmp://live.hkstv.hk.lxdns.com/live/hks.



The typical nginx.conf are as follows:


Worker_processes 4; #Nginx开启4个子进程, the number of child processes should be the same as the CPU cores
Worker_cpu_affinity 0001 0010 0100 1000; #CPU的mask, the child process uses it to bind the CPU core, avoiding the loss of performance due to process switching

Error_log Logs/error.log error; #错误日志位置和日志级别, if you use the default compilation option, the location of/usr/local/nginx/logs/error.log,error means that only the error log is printed

Events {
Worker_connections 1024; #Nginx处理的最大连接数
}

HTTP {
Include Mime.types;
Default_type Application/octet-stream;

Keepalive_timeout 65;

server {
Listen 80; #Nginx监听的HTTP请求端口

Location/{
root/var/www; #HTTP请求URL映射到服务器的位置
Index index.html index.htm; #HTTP请求优先请求的文件, such as http://localhost/, if there is index.html in the/var/www directory, then the request is/var/www/index.html
}

Error_page 502 503 504/50x.html; #如果遇到这些HTTP请求错误, Nginx returns the contents of 50x.html
Location =/50x.html {
root HTML; #因为/Configured Root/var/www, so here the HTML corresponds to/var/www/html, so the 50x.html path is/var/www/html/50x.html
}

location/live {
Flv_live on; #当HTTP请求以/live end, Match here, this option means that the FLV live play function is turned on
Chunked on; #HTTP协议开启Transfer-encoding:chunked; way to reply
}
}
}

Rtmp_auto_push on; #因为Nginx可能开启多个子进程, this option indicates that when a stream is pushed, the media stream is published to multiple child processes
Rtmp_auto_push_reconnect 1s;
rtmp_socket_dir/tmp; #多个子进程情况下, when pushing a stream, only one child process initially receives the data in the competition, and then it relay to the other child processes, transferring data between them through the UNIX domain socket, which represents the path of the UNIX domain socket

rtmp {
Out_queue 4096;
Out_cork 8;
Max_streams 64; #Nginx能接受的最大的推流数

server {
Listen 1935; #Nginx监听的RTMP推流/Lahue port, can be omitted, default listener 1935

Application MyApp {
Live on; #当推流时, the app in the rtmp path (a concept in rtmp) matches MyApp when the stream is turned on
Gop_cache on; #开启GOP (Group of picture) cache, when the player decodes, it receives a full GOP before it starts playing, this is the option to reduce the playback delay
Pull rtmp://live.hkstv.hk.lxdns.com/live/hks; #如果懒得推流, you can use this, live streaming from Hong Kong TV


}



#以下配置项已废弃



Application App1 {



Proxy_pass rtmp://host (IP or domain name) [: HOST]/APP2; #将推流反向代理到上游服务器, and convert App1 automatically to APP2



#proxy_pass Rtmp://backend; #将推流反向代理到上游服务器, see upstream configuration



}
}



server {



Listen 1935;



server_name *.test.com; #或者www. test.*/www.test.com



Application MyApp {



Live on;



Gop_cache on;



}



}



#以下配置项已废弃 for reasons in the following update



Upstream Backend {



#开启负载均衡



Server Host1:port1;



Server Host2:port2;



}
}

Start Nginx, in the VLC player to "network" mode to open the media, fill in http://localhost/live?stream=hks.



General url:http://example.com[:p Ort]/dir? [Srv=index&app=xxx&]stream=xxx.



If the listening port in the HTTP configuration block is not 80 (default), then you must add: port, for example: 8080.



If there are multiple server configuration blocks in the RTMP configuration block, if the configuration of the stream that you want to play is in the second server configuration block, then you must add srv=1 (counting starting from 0).



If there are multiple application configuration blocks under one of the server blocks in the RTMP configuration block, if the name of the app (a concept in rtmp) for the stream you want to play is test, you must indicate that the App=test,stream corresponds to the name of the push stream.



General Command for push flow: Ffmpeg-i-re xxx.mp4 (or rtmp-compatible media file)-vcodec copy-acodec copy-f flv rtmp://example.com[:p ort]/app/stream, The back can also be like the URL of http add parameters, not carefully studied, if you want to push to MyApp, then the app to Myapp,stream casually named, play when it is consistent with it can.



See the Nginx-rtmp-module wiki for other explanations.



The test is as follows:









2017-09-18 Update:



The functionality of reverse proxy and load balancing has been largely available, but previously, and given the number of push flows, such as 1000-way push flows, this could be a heavy burden on the server. So why does the HTTP protocol use reverse proxy and load balancing without this problem? That's because HTTP requests consume a limited amount of bandwidth, and the load can be instantaneous, but not too long.



2017-10-07 Update:



The virtual host function is basically available, that is, you can configure server_name as HTTP configuration, because the configuration can be found through the virtual host, so the parameter srv=index is no longer supported, add a parameter port, if not specified, the default is 1935, Used to specify the configuration to find the push stream in the port. Generic URL changed to: http://example.com[:p ort]/dir? [Port=xxx&]app=xxx&stream=xxx



2017-11-10 Update:



Rtmp's 302 redirect is basically available, but because many players do not support redirection, this feature is very limited, currently only the JW player test passes, VLC cannot parse the returned redirect information, other players have not been tested. For rtmp 302 redirects, refer to the application.redirectconnection section of Adobe's website for instructions: https://helpx.adobe.com/adobe-media-server/ Ssaslr/application-class.html.



The settings are as follows: Add a configuration to the server block or application block, assuming that the app for the push stream is MyApp, to redirect to test, to keep the name of the stream unchanged:



Rewrite ' ^/app/(. *) '/test/$1 ';



In this way, you can redirect the push stream or playback from the app to test on this computer.



If you want to push the flow to another host, you can set it to:



Rewrite ' ^/app/(. *) ' rtmp://otherhost:otherport/otherapp/$1;



This way, you can redirect the push stream or playback on the local computer to another host, which is also a load balancing method.



PS: Not very willing to rewrite branch merge to master, after all, limited too much, function a little chicken.



2017-11-12 Update:



Today in the notebook for stress testing, using the SRS to test the tool, and it does not support the push MP4 file stream, only support FLV format, the results of a test problem, HTTP playback does not work properly, checked the code, has fixed the bug.



2017-11-22 Update:



Some netizens mentioned that while using the HTTP and rtmp mode live, the way to stop rtmp playback will cause HTTP playback also stop, the bug a few days ago when the test has been found, but recently due to work relatively busy, no time to change, today fixed the bug.



2017-12-10 Update:



Comments in the Netizen pointed out that do not know how to use the HTTP mode to play live stream, you can view the README.CN.md on GitHub, this file is a Chinese description, readme.md is an English explanation. These two days specifically updated these two files, did not add new features. The test is as follows, where the webpage is played in rtmp mode and VLC is played in HTTP mode:






Insert with Flv.js (2018-04-06):






2017-12-30 Update:



2017 the last update, because previously mentioned why the reverse proxy and load balancing in real life is not practical, so the Readme file in the reverse proxy and load balancing instructions have been deleted, but the code has not been deleted, follow-up will continue to delete. For the comments have mentioned the question, some have not repaired, I am sorry, usually work more busy, the end of the year 12 consecutive days, night 1 nights, so too late to fix the problem. Interested netizens can own hack code, code style is strictly in accordance with the official requirements of Nginx written, I think I look at the line, as for some logic problems, I did not make too clear, only know that writing no problem.



Finally, recently rewritten the http-flv live feature, assemble the data and send all the frames using HTTP, no longer use some "bare" methods of assembly data, such as "http/1.1 OK" CRLF, send also use Ngx_http_send_header and NGX _http_output_filter complete, no longer use custom send function, why have this idea, Since Nginx from 1.3.9 version of the native support HTTP chunked transmission, there is no need to do a set of assembly and send chunked data, and for non-chunked transmission, Nginx HTTP module, so simply all with the Nginx HTTP framework.



Finally, the above code is not submitted, because I found that someone fork the code, and then delete the fork, and then in their own code to add some of my project code, despite changing the name of the variable or something, still see traces. Bsd-2-clause Open Source Agreement was originally required very simple, you modify, re-release even commercial, license files in the original author information can be. This is not possible, I can only narrow-minded.



2018-01-02 Update:



The reverse proxy and load-balanced code has been removed from the master branch, the Vhost branch is the same as the Master Branch code, the upstream branch retains the reverse proxy and load balancing code, there is a need to view this branch and no longer maintain these two functions.



2018-01-03 Update:



Thanks to some netizens pointed out that nginx-http-flv-module because Nginx version changes caused by the problem can not be compiled, has already found some of the compatibility issues have been fixed, Testing to the oldest Nginx version is 1.2.6, considering that nginx-1.2.6 is already a 2012 version, so in most cases it should not be used in an older version, so the compatibility of Nginx-http-flv-module and older Nginx versions is no longer tested.



2018-01-12 Update:



Recently, using the Srs-bench push flow test nginx-http-flv-module stability, found that the third time to play the test video (occasionally the first and second times) there will be a sudden increase in CPU usage, Nginx does not accept any services, The player screen still does not move the problem (I used the player will appear this problem, so it is not the problem of the player). After debugging, it is found that when the Used list is released (not freeing memory, it is putting the memory chain into a free pointer), the loop is infinite, that is, the linked list has been used to form a ring. Later confirmed to be repeated release of the used linked list caused problems, after modifying the code, play test video more than 10 times (more than half an hour) no more problems, the code has been updated. Careful guessing nginx-rtmp-module also has this problem, but it has not been tested.



2018-02-07 Update:



There are netizens to submit the code, including timed output logs and fcpublish, such as the processing of commands, the code has been merged. In addition there are netizens on the server trial, 32GB of memory 6 hours exhausted, there is obviously a memory leak, has been fixed. Have to admire the original author of Nginx-rtmp-module, the memory list uses a reference counter, allocation and release of the counter operation to avoid multiple releases caused by the formation of the chain ring. Also fixed a because the number of GOP cache is 2 o'clock, it will cause the rate of instantaneous transmission of data is too high, causing the player to receive data, resulting in playback lag bug. With the Wireshark grab bag can see the ' TCP Window full ' problem, the cause of this problem is the player is too late to receive data.



2018-02-27 Update:



Some netizens want to run the Nginx with Nginx-http-flv-module on Windows, and I have been focusing on Linux, and Mac OS X can be compiled, but not how to test, last night on the Windows compilation, A lot of compilation errors are found, and if "chunked on;" is turned on; Configuration items, playback will crash, and now fix these bugs, thank you for your test and suggestions.



As this article is already very long, the following content will be updated here: "http-flv Live Module (nginx-http-flv-module) based on Nginx-rtmp-module module" continued.






from:74910586



http-flv Live Module (nginx-http-flv-module) based on Nginx-rtmp-module module implementation


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.