nginx的RTMP协议服务器

nginxRTMP协议服务器

by ahuner

通过以下的配置,可以使nginx接收RTMP流,并在web上播放实时视频。

1.openssl安装

nginx需要http_ssl_module模块,需要openssl库。

下载opensll:http://www.openssl.org/

最新稳定版本:openssl-1.0.1e

修改三个文件的代码,openssl-1.0.1e\test中的md2test.c、rc5test.c、jpaketest.c

将dummytest.c修改为#include "dummytest.c"。

编译:

chmod +x config

编译静态库

./config --prefix=/usr/local --openssldir=/usr/local/opensll

make && make install

编译动态库

./config shared --prefix=/usr/local --openssldir=/usr/local/opensll

make clean

make && make install

2.nginx的rtmp模块

下载nginx-rtmp-module

https://github.com/arut/nginx-rtmp-module

3.nginx安装

./configure \

--prefix=/usr/local/nginx \

--sbin-path=/usr/local/nginx/nginx \

--conf-path=/usr/local/nginx/nginx.conf \

--pid-path=/usr/local/nginx/nginx.pid \

--error-log-path=/usr/local/nginx/logs/error.log \

--with-pcre=../pcre-8.31 \

--with-zlib=../zlib-1.2.7 \

--with-http_dav_module \

--with-http_flv_module \

--with-http_stub_status_module \

--without-http_scgi_module \

--without-http_uwsgi_module \

--without-http_gzip_module \

--without-http_ssi_module \

--without-http_proxy_module \

--without-http_memcached_module \

--without-http_empty_gif_module \

--without-mail_pop3_module \

--without-mail_imap_module \

--without-mail_smtp_module \

--with-http_ssl_module \

--with-openssl=/project/openssl-1.0.1e \

--add-module=/project/nginx-rtmp-module-master

注意:/project/openssl-1.0.1e为openssl的源代码目录,编译nginx的时候会自动编译连接openssl.

make && make install

4.nginx.conf

配置文件:

#user  nobody;
worker_processes  1;

error_log  logs/error.log debug;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;

chunk_size 4096;

application myapp {
            live on;
        }
    }
}

http {
    server {
        listen      80;

location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

location /stat.xsl {
            root /project/nginx-rtmp-module-master/;
        }

location / {
            root /project/nginx-rtmp-module-master/test/rtmp-publisher;
        }
    }
}

5.推流

"rtmp://192.168.1.102/myapp"表示url, "test1"表示stream

推流方式1:ffmpeg -re -i ~/2012.flv -f flv rtmp://192.168.1.102/myapp/test1

推流方式2:Adobe Flash Media Live Encoder设置上述参数

6.直播播放

用nginx-rtmp-module自带的一个例子修改,在test/rtmp-publisher目录下player.html

<!DOCTYPE html>

<html>

<head>

<title>RTMP Player</title>

<script type="text/javascript" src="swfobject.js"></script>

<script type="text/javascript">

var flashVars = {

streamer: 'rtmp://192.168.1.102/myapp',

file:'test1'

};

swfobject.embedSWF("RtmpPlayer.swf", "rtmp-publisher", "500", "400", "9.0.0",

null, flashVars);

</script>

</head>

<body>

<div id="rtmp-publisher">

<p>Flash not installed</p>

</div>

</body>

</html>

访问http://192.168.1.102/player.html

参考

Nginx RTMP 功能研究:

http://blog.csdn.net/cccallen/article/details/8440191

上一篇:【转】Duff's Device


下一篇:Java面向对象之封装 入门实例(一)