iOS Tips 杂货铺码农的日常之Android开发

直播推流nginx-rtmp-module集成

2016-06-08  本文已影响7831人  shone

官方源码:https://github.com/arut/nginx-rtmp-module

战斗民族俄罗斯人民开发的一款NGINX的流媒体插件,除了直播发布音视频流之外具备流媒体服务器的常见功能
比如推拉流媒体资源
基于HTTP的FLV/MP4 VOD点播
HLS (HTTP Live Streaming) M3U8的支持
基于http的操作(发布、播放、录制)
可以很好的协同现有的流媒体服务器以及播放器一起工作
在线调用ffmpeg对流媒体进行转码
H264/AAC音视频编码格式的支持
linux/BSD/MAC系统的支持

编译方法:

Screenshot from 2016-06-08 14:00:01.png

在上一篇中,已经成功部署了nginx服务器,但是没有集成推流的功能.

1\下载该模块
2\进入nginx-1.10.1目录,重新配置configure
3\增加参数--add-module=/path/to/nginx-rtmp-module

sudo ./configure --prefix=/usr/local/nginx --with-pcre=/home/wangxiong/Soft/nginx_station/pcre-8.37 --with-zlib=/home/wangxiong/Soft/nginx_station/zlib-1.2.8 --with-openssl=/home/wangxiong/Soft/nginx_station/openssl-1.0.1t  --with-http_ssl_module --add-module=/home/wangxiong/Soft/nginx_station/nginx-rtmp-module

4\sudo make
5\sudo make install

安装完成后nginx目录:

Screenshot from 2016-06-08 14:10:28.png

服务器配置nginx.conf所在路径

Screenshot from 2016-06-08 14:21:00.png

可以用gedit或者vim打开>


#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       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$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;
    #    }
    #}


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

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

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

默认是支持http协议,然后增加对rtmp协议,在http{}外面增加,详细配置可看官方wiki: https://github.com/arut/nginx-rtmp-module/wiki/Directives

rtmp {  
     server {  
            listen 1935;  
      
            application myapp {  
                live on;  
            }  
            application hls {  
                live on;  
                hls on;  
                hls_path /tmp/hls;  
            }  
      }  
}  

配置好后重启nginx;
在本地可以用ffmpeg命令进行推流,然后用ffplayer进行播放

如果用ffmpeg命令出现.so共享库找不到的问题,解决方法配置全局共享
vim /etc/ld.so.conf
在后面添加一行/usr/local/ffmpeg/lib
按Esc->wq保存退出
sudo ldconfig,生效

./ffmpeg -re -i /home/wangxiong/Documents/VID_20160508_164615.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://localhost:1935/myapp/testav

./ffmpeg -re -i /home/wangxiong/Documents/VID_20160508_164615.mp4 -c copy -f flv rtmp://localhost:1935/myapp/testav

推流结果

Screenshot from 2016-06-08 16:59:05.png

为了方便,直接用雷博士的android推流客户端,github上克隆或下载simplest_ffmpeg_android_streamer!

接下来,查看本地nginx服务器的ip地址,这个就是安卓客户端要填写的流服务器地址.

Screenshot from 2016-06-08 17:51:19.png

我的ip地址是192.168.1.116
1\用Eclipse导入项目工程,配置好NDK自动编译.
2\打开MainActivity,填写输入文件路径,和流服务器地址,当然我只是做了判断,如果EditText不填写,我就给一个默认的值.

               String folderurl = Environment.getExternalStorageDirectory()
                        .getPath();

                String urltext_input = urlEdittext_input.getText().toString();

                if (TextUtils.isEmpty(urltext_input)) {
                    urltext_input = "sintel.mp4";
                }
                String inputurl = folderurl + "/" + urltext_input;

                String outputurl = urlEdittext_output.getText().toString();

                String info = "";
                if (TextUtils.isEmpty(outputurl)) {
                    outputurl = "rtmp://192.168.1.116:1935/myapp/livestream";
                    // 192.168.1.116
                }
                stream(inputurl, outputurl); //native方法
                Log.e("inputurl", inputurl);
                Log.e("outputurl", outputurl);
            

运行build程序,点击 Start 按钮,开始将SD卡下的视频推到服务器.

Screenshot from 2016-06-08 18:03:49.png

注意,因为直播,我这里是在安卓上点击推流,然后在ubuntu打开浏览器访问rtmp流.
ps:安装VLC播放器:sudo apt-get install vlc 一条命令搞定.

Screenshot from 2016-06-09 10:52:51.png

然后选择播放应用程序,我已经安装了VLC播放器,就用它了.

Screenshot from 2016-06-09 10:56:58.png Screenshot from 2016-06-09 11:03:46.png Screenshot from 2016-06-09 11:02:00.png

直播开始啦,只要手机一直有视频推上来,播放器可以一直播放!!!

上一篇下一篇

猜你喜欢

热点阅读