一、Nginx中加入nginx-rtmp-module
1.引入安装的依赖库
yum install build-essential libpcre3 libpcre3-dev libssl-dev
2. 开始编译
下载源码:(nginx-rtmp-module项目地址:https://github.com/arut/nginx-rtmp-module/)
wget http://nginx.org/download/nginx-1.4.2.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
解压:
tar -zxvf nginx-1.4.2.tar.gz
unzip master.zip
cd nginx-1.4.2
开始配置、编译与安装:
./configure --add-module=../nginx-rtmp-module-master
$ make
$ sudo make install
启动nginx:
/usr/local/nginx/sbin/nginx
检查nginx 是否启动成功: curl -l http://localhost 查看返回页
Nginx rtmp 配置
打开nginx.conf文件,开始配置rtmp:
http {
……
}
在http节点后面加上rtmp配置:
rtmp {
server{
listen1935;
application live1{
liveon;
record off;
}
}
}
附录里面有Nginx完整配置参考
保存退出后,告诉nginx重新读取配置:
sudo /usr/local/nginx/sbin/nginx -s reload
这里多提一句,如果想要重启nginx:
sudo /usr/local/nginx/sbin/nginx -s stop
sudo /usr/local/nginx/sbin/nginx
这时我们可以测试推送流了,这里由于我们还没有写推送代码,就先借助软件完成吧。
如果你有Windows环境,且是Vista以上系统,有独立显卡,使用Open Broadcaster Software
如果你有Mac环境,使用CocoaSplit
推送流简单来说就是上传,也被称为推送、发布。想要推送流,总要有一个目标地址吧。看看之前我们建立的RTMP服务器配置,里面规定了使用1935作为监听端口,配置了一个应用live1,通过这些信息,我们就能拼出可以使用的上传地址了,例如这样:
live1是应用名,对应我们在nginx中配置的应用。room1是一个应用的实例(或者叫结点,我是这么理解的),这个不需要在服务器做特别的配置,我们只需要在配置好的应用中选择一个实例名字(例如room1)就能使用了。
使用之前提到的软件,简单配置下上传地址就能够发布了。
发送上去了,那么我们如何接收呢?
使用JWPlayer在网页播放
这里又要使用外部工具了:jwplayer,一个flash的播放器,利用它可以在网页播放rtmp流。
jwplayer目前能够提供试用,但是使用需要先注册以获取key。这里有一个我已经注册好并下载到本地的播放器文件,你可以直接使用它们。
找到/usr/local/nginx/html/目录,在下面新建文件player.html
charset="utf-8"> JW Player Example
id="'playerWRYqPNErNtRf'>test
然后将jwplayer文件夹放在/usr/local/nginx/html/目录下,最后应该是这样:
/usr/local/nginx/html/player.html
/usr/local/nginx/html/jwplayer/jwplayer.js
/usr/local/nginx/html/jwplayer/jwplayer.flash.swf
然后重启nginx,在浏览器输入http://localhost/player.html,点击播放按钮,这时如果你正在使用软件进行推送,就应该可以看到视频了:]
使用VLC软件播放
这是更简单的一种方式,找一个支持rtmp的桌面软件,恩……之前为什么我要折腾jwplayer呢……
下载后打开VLC,在菜单选择file->open network->在弹出的新窗口中选择Network标签页,填入URL:rtmp://localhost:1935/live1->点击open,等待缓冲完成后,就自动开始播放了=。=
附录:
一、 Nginx 参考配置:
#user nobody;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# rtmp stat
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# you can move stat.xsl to a different location
root /usr/local/nginx/html;
}
# rtmp control
location /control {
rtmp_control all;
}
#error_page 404 /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;
}
}
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application live1 {
live on;
record off;
}
}
}
二、 Nginx配置出错
错误提示:./configure: error: the HTTP rewrite module requires the PCRE library.
解决:
yum install openssl libssl-dev
yum install libpcre3 libpcre3-dev