Skip to content

Latest commit

 

History

History
204 lines (134 loc) · 5.18 KB

README.CN.md

File metadata and controls

204 lines (134 loc) · 5.18 KB

nginx-http-flv-module

基于nginx-rtmp-module的流媒体服务器。

功能

  • 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和nginx-http-flv-module。

将它们解压到某一路径。

打开NGINX的源代码路径并执行:

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

使用方法

关于nginx-rtmp-module用法的详情,请参考README.md

发布

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块传输, 这种情况下最好不要在指定了flv_live on;的location中指定chunked on;,否则播放会失败。

nginx.conf实例

worker_processes  4;
worker_cpu_affinity  0001 0010 0100 1000;

error_log logs/error.log error;

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  on; #支持'Transfer-Encoding: chunked'方式回复
        }

        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缓存,降低播放延迟
        }
    }
}