使用nginx在android手机上搭建rtmp服务器并测试

1,编译android上面可以使用的nginx,添加选项--add-module=/path/nginx-rtmp-module,准备android上面shell可以使用的ffmpeg和相关库文件以及m3u8的segmenter文件
2,在手机的data/data/android.nginx目录下把编译生成的nginx相关文件放到该目录下
3,编写配置文件
-----------------------------------nginx.conf start here----------------------------------------------------
user  root;
worker_processes  2;

error_log  /data/data/android.nginx/logs/error.log;
error_log  /data/data/android.nginx/logs/error.log  notice;
error_log  /data/data/android.nginx/logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;

        chunk_size 4096;

        application myapp {
            live on;
        }
    }
}


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      801;

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

        location /stat.xsl {
            root /data/data/android.rtmp;
        }

        location / {
            root /data/data/android.rtmp/test/rtmp-publisher;
        }
    }

    server {
        listen      80 default;
        server_name  localhost;
        autoindex            on;
        autoindex_exact_size on;
        root    /data/data/android.wwwroot;
        index   index.html index.htm index.php;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #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 on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~* \.php$ {
            fastcgi_index  index.php;
            client_max_body_size 64m;
            fastcgi_pass   unix:/data/data/android.php-fpm/tmp/php-fpm.socket;
            fastcgi_param  SCRIPT_FILENAME  /data/data/android.wwwroot$fastcgi_script_name;
            include        fastcgi_params;
        }

    location ~ \.flv$ {
                         flv;
                        }
       location ~ \.mp4$ {
                         mp4;
                        }


        # 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  default;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen     443 default;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

-----------------------------------nginx.conf end here----------------------------------------------------
4,adb push nginx.conf /data/data/android.nginx/conf/

5,启动nginx,在/data/data/android.nginx目录下,执行./nginx -c ./conf/nginx.conf

6,准备ffmpeg文件
adb shell
su
chmod 777 /system/bin/
adb push ffmpeg /system/bin/
adb shell
su
cd system/bin
chmod 777 ffmpeg
exit
exit
adb push libvorbisenc.so.2 /system/lib
adb push libvorbis.so.0 /system/lib
adb push libogg.so.0 /system/lib
adb push libtheoraenc.so.1 /system/lib
adb push libtheoradec.so.1 /system/lib
adb push libmp3lame.so.0 /system/lib
adb push libfdk-aac.so.0 /system/lib
adb push segmenter /system/bin
adb shell
su
cd /system/bin
chmod 777 segmenter
exit
exit


7,准备相关文件
adb push 1.flv /data/data/android.wwwroot/

8,准备rtmp相关文件
在/data/data/目录下建立rtmp目录,使用adb push的方法把nginx-rtmp-module目录下所有文件push到/data/data/rtmp目录下
修改该目录下的./test/rtmp-publisher/player.html
---------------------------player.html start here---------------------------------------------------------------
!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.2/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>
---------------------------player.html end here---------------------------------------------------------------

9,推流
"rtmp://192.168.1.2/myapp"表示url, "test1"表示stream

推流方式1:ffmpeg -re -i 1.flv -f flv rtmp://192.168.1.2/myapp/test1
注意:要把相关文件push到
我这里把这个简单写成pushrtmp的批处理
---------------------------------pushrtmp start here-----------------------------------------------------------
!/system/bin/sh
    file=$1
    file_name=`echo ${file} | busybox awk -F '.' '{ print $1 "" }'`  
    ffmpeg -re -i ./$1.flv -f flv rtmp://192.168.1.$2/myapp/test1
---------------------------------pushrtmp end here-----------------------------------------------------------
执行方式就是简单的./pushrtmp 1 2就可以了

10,测试
在另一台机器上访问http://192.168.1.2:801/player.html就可以了

你可能感兴趣的:(使用nginx在android手机上搭建rtmp服务器并测试)