Car-eye-http-flv-module 實現nginx-rtmp-mudule HTTP方式的FLV直播功能


nginx-rtmp-mudule RTMP 是一款優秀的Car-eye-http-flv-module 是在nginx-rtmp-mudule RTMP基礎上修改的流媒體服務器,除了支持flash播放器外,還支持現在常見的播放器。完美實現了HTTP方式的FLV直播功能。

本文簡單介紹下該模塊的主要功能和使用:

 

功能

  • nginx-rtmp-module提供的所有功能。

  • 基於HTTP協議的FLV直播流播放。

  • GOP緩存,降低播放延遲 (H.264視頻和AAC音頻)。

  • 支持Transfer-Encoding: chunked方式的HTTP回復。

  • rtmp配置的server塊中可以省略listen配置項。

  • 支持虛擬主機。

支持的系統

  • Linux(推薦)/FreeBSD/MacOS/Windows(受限)。

支持的播放器

依賴

  • 在類Unix系統上,需要GNU make,用於調用編譯器來編譯軟件。

  • 在類Unix系統上,需要GCC/在Windows上,需要MSVC,用於編譯軟件。

  • 在類Unix系統上,需要GDB,用於調試軟件(可選)。

  • FFmpeg,用於發布媒體流。

  • VLC播放器(推薦),用於播放媒體流。

  • 如果NGINX要支持正則表達式,需要PCRE庫。

  • 如果NGINX要支持加密訪問,需要OpenSSL庫。

  • 如果NGINX要支持壓縮,需要zlib庫。

創建

在Windows上

編譯步驟請參考Building nginx on the Win32 platform with Visual C,不要忘了在Run configure script步驟中添加--add-module=/path/to/nginx-http-flv-module

在類Unix系統上

下載NGINX和nginx-http-flv-module。

將它們解壓到某一路徑。

打開NGINX的源代碼路徑並執行:

將模塊編譯進NGINX

./configure --add-module=/path/to/nginx-http-flv-module
make
make install

將模塊編譯為動態模塊

./configure --add-dynamic-module=/path/to/nginx-http-flv-module
make
make install

注意

如果將模塊編譯為動態模塊,那么NGINX的版本號必須大於或者等於1.9.11。

 

發布

ffmpeg -re -i example.mp4 -vcodec copy -acodec copy -f flv rtmp://example.com[:port]/appname/streamname

appname用於匹配rtmp配置塊中的application塊(更多詳情見下文)。

streamname可以隨意指定。

RTMP默認端口為1935,如果要使用其他端口,必須指定:port

播放(HTTP)

http://example.com[:port]/dir?[port=xxx&]app=myapp&stream=mystream

參數dir用於匹配http配置塊中的location塊(更多詳情見下文)。

HTTP默認端口為80, 如果使用了其他端口,必須指定:port

RTMP默認端口為1935,如果使用了其他端口,必須指定port=xxx

參數app用來匹配application塊,但是如果請求的app出現在多個server塊中,並且這些server塊有相同的地址和端口配置,那么還需要用匹配主機名的server_name配置項來區分請求的是哪個application塊,否則,將匹配第一個application塊。

參數stream用來匹配發布流的streamname。

例子

假設在http配置塊中的listen配置項是:

http {
    ...
    server {
        listen 8080; #不是默認的80端口
        ...

        location /live {
            flv_live on;
        }
    }
}

rtmp配置塊中的listen配置項是:

rtmp {
    ...
    server {
        listen 1985; #不是默認的1935端口
        ...

        application myapp {
            live on;
        }
    }
}

那么基於HTTP的播放url是:

http://example.com:8080/live?port=1985&app=myapp&stream=mystream

注意

如果使用的是HTTP版本1.1(HTTP/1.1),chunked_transfer_encoding配置項默認是打開的。

由於一些播放器不支持HTTP塊傳輸,這種情況下最好在指定了flv_live on;的location中指定chunked_transfer_encoding off,否則播放會失敗。

nginx.conf實例

注意

配置項rtmp_auto_pushrtmp_auto_push_reconnectrtmp_socket_dir在Windows上不起作用,除了Windows 10 17063以及后續版本之外,因為多進程模式的relay需要Unix domain socket的支持,詳情請參考Unix domain socket on Windows 10

worker_processes  4; #運行在Windows上時,設置為1,因為Windows不支持Unix domain socket
worker_cpu_affinity  0001 0010 0100 1000; #運行在Windows上時,省略此配置項

error_log logs/error.log error;

#如果此模塊被編譯為動態模塊並且要使用與RTMP相關的功
#能時,必須指定下面的配置項並且它必須位於events配置
#項之前,否則NGINX啟動時不會加載此模塊或者加載失敗
#load_module modules/ngx_rtmp_module.so;

events {
    worker_connections  1024;
}

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

    keepalive_timeout  65;

    server {
        listen       80;

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /live {
            flv_live on; #打開HTTP播放FLV直播流功能
            chunked_transfer_encoding on; #支持'Transfer-Encoding: chunked'方式回復

            add_header 'Access-Control-Allow-Origin' '*'; #添加額外的HTTP頭
            add_header 'Access-Control-Allow-Credentials' 'true'; #添加額外的HTTP頭
        }

        location /stat {
            #push和pull狀態的配置

            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /var/www/rtmp; #指定stat.xsl的位置
        }
    }
}

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;

rtmp {
    out_queue   4096;
    out_cork    8;
    max_streams 64;

    server {
        listen 1935;
        server_name www.test.*; #用於虛擬主機名后綴通配

        application myapp {
            live on;
            gop_cache on; #打開GOP緩存,降低播放延遲
        }
    }

    server {
        listen 1935;
        server_name *.test.com; #用於虛擬主機名前綴通配

        application myapp {
            live on;
            gop_cache on; #打開GOP緩存,降低播放延遲
        }
    }

    server {
        listen 1935;
        server_name www.test.com; #用於虛擬主機名完全匹配

        application myapp {
            live on;
            gop_cache on; #打開GOP緩存,降低播放延遲
        }
    }
}

 

car-eye開源平台官方網址:www.car-eye.cn; car-eye開源平台源碼下載:https://github.com/Car-eye-team 有關car-eye 問題咨詢可以加QQ群590411159。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM