一、方案選型
- 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 "設置好的服務器地址"