一、方案选型
- nginxGryphon
- nginx-rtmp-module
- ffmpeg
- vlc播放器(android)
二、搭建流程
-
下载nginxGryphon
nginx 1.7.11.3 Gryphon.zip
。 -
下载nginx-rtmp-module并拷贝到nginx 1.7.11.3 Gryphon解压所在的目录。没有怎么研究过该module,rtmp-module里面都是代码,猜测重要的应该是
stat.xsl
这个文件。 -
安装ffmpeg工具
-
在模拟器中下载vlc.apk 需要是x86版本的。
三、配置nginx
-
增加rtmp节点
rtmp { server { listen 1935;#监听端口,若被占用,可以更改 chunk_size 4000;#上传flv文件块儿的大小 application live { #创建一个叫live的应用 live on;#开启live的应用 allow publish 127.0.0.1;# #allow play all; } } }
-
在server节点中增加以下内容:
location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl ; } location /stat.xsl { root ./nginx-rtmp-module/ ; }
通过配置完这个后,就可以在 "localhost/stat",可以查看直播监控状态了。
整体的配置文件如下(nginx-win-rtmp.conf):
# user nobody;
<p class="mume-header " id="user--nobody"></p>
# multiple workers works !
<p class="mume-header " id="multiple-workers-works-"></p>
worker_processes 2;
# error_log logs/error.log;
<p class="mume-header " id="error_log--logserrorlog"></p>
# error_log logs/error.log notice;
<p class="mume-header " id="error_log--logserrorlog--notice"></p>
# error_log logs/error.log info;
<p class="mume-header " id="error_log--logserrorlog--info"></p>
# pid logs/nginx.pid;
<p class="mume-header " id="pid--------logsnginxpid"></p>
events {
worker_connections 8192;
# max value 32768, nginx recycling connections+registry optimization =
# this.value * 20 = max concurrent connections currently tested with one worker
# C1000K should be possible depending there is enough ram/cpu power
# multi_accept on;
}
rtmp {
server {
listen 1935;#监听端口,若被占用,可以更改
chunk_size 4000;#上传flv文件块儿的大小
application live { #创建一个叫live的应用
live on;#开启live的应用
allow publish 127.0.0.1;#
#allow play all;
}
}
}
http {
#include /nginx/conf/naxsi_core.rules;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr:$remote_port - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
# # loadbalancing PHP
<p class="mume-header " id="-loadbalancing-php"></p>
# upstream myLoadBalancer {
<p class="mume-header " id="upstream-myloadbalancer-"></p>
......
# least_conn;
<p class="mume-header " id="least_conn"></p>
# }
<p class="mume-header " id=""></p>
sendfile off;
#tcp_nopush on;
server_names_hash_bucket_size 128;
## Start: Timeouts ##
<p class="mume-header " id="start-timeouts-"></p>
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 30;
send_timeout 10;
keepalive_requests 10;
## End: Timeouts ##
<p class="mume-header " id="end-timeouts-"></p>
#gzip on;
server {
listen 9090;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
## Caching Static Files, put before first location
#location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
# expires 14d;
# add_header Vary Accept-Encoding;
#}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl ;
}
location /stat.xsl {
root ./nginx-rtmp-module/ ;
}
# For Naxsi remove the single # line for learn mode, or the ## lines for full WAF mode
<p class="mume-header " id="for-naxsi-remove-the-single--line-for-learn-mode-or-the--lines-for-full-waf-mode"></p>
location / {
#include /nginx/conf/mysite.rules; # see also http block naxsi include line
##SecRulesEnabled;
##DeniedUrl "/RequestDenied";
##CheckRule "$SQL >= 8" BLOCK;
##CheckRule "$RFI >= 8" BLOCK;
##CheckRule "$TRAVERSAL >= 4" BLOCK;
##CheckRule "$XSS >= 8" BLOCK;
root html;
index index.html index.htm;
}
# For Naxsi remove the ## lines for full WAF mode, redirect location block used by naxsi
<p class="mume-header " id="for-naxsi-remove-the--lines-for-full-waf-mode-redirect-location-block-used-by-naxsi"></p>
##location /RequestDenied {
## return 412;
##}
## Lua examples !
<p class="mume-header " id="lua-examples-"></p>
# location /robots.txt {
<p class="mume-header " id="location-robotstxt-"></p>
# rewrite_by_lua '
<p class="mume-header " id="rewrite_by_lua-"></p>
# if ngx.var.http_host ~= "localhost" then
<p class="mume-header " id="if-ngxvarhttp_host--localhost-then"></p>
# return ngx.exec("/robots_disallow.txt");
<p class="mume-header " id="return-ngxexecrobots_disallowtxt"></p>
# end
<p class="mume-header " id="end"></p>
# ';
<p class="mume-header " id="-1"></p>
# }
<p class="mume-header " id="-2"></p>
#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;
}
# proxy the PHP scripts to Apache listening
#
#location ~ \.php$ {
# proxy_pass
#}
# pass the PHP scripts to FastCGI server listening
#
#location ~ \.php$ {
# root html;
# fastcgi_pass # single backend process
# fastcgi_pass myLoadBalancer; # or multiple, see example above
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# server
#
#server {
# listen 443 ssl spdy;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_prefer_server_ciphers On;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:ECDH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!eNULL:!MD5:!DSS:!EXP:!ADH:!LOW:!MEDIUM;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
四、验证
- 启动nginx服务
命令行下进入到nginx.exe所在的目录,并执行$ ./nginx.exe -c conf/nginx-win-rtmp.conf
。 - 启动ffmpeg推流命令
ffmpeg -re -stream_loop -1 -i ./rtmp_demo.mp4 -vcodec libx264 -acodec aac -f flv + "设置好的服务器地址"
其中rtmp_demo是之前录制好的一个mp4的视频文件。 - 由于模拟器中andorid的系统无法直接访问windows的服务器地址,需要进行一次端口的转发。
adb reverse tcp:1935 tcp:1935
- 在模拟器中打开之前安装好的vlc播放器,并将播放地址设置成拉流的地址即可播放。
adb shell input text "设置好的服务器地址"