直播

Centos6.5+Nginx+RTMP流媒体服务(直播、点播)

2017-05-27  本文已影响366人  代码坊

安装前的准备

sudo yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel -y
sudo yum install gcc gcc-c++ make libtool -y

下载Nginx

wget http://nginx.org/download/nginx-1.12.0.tar.gz
tar -xvf nginx-1.12.0.tar.gz

下载nginx-rtmp-module

wget https://github.com/arut/nginx-rtmp-module/archive/v1.1.11.tar.gz
tar -xvf v1.1.11.tar.gz

编译安装,安装教程参见Getting started with nginx rtmp

cd nginx-1.12.0
sudo ./configure --add-module=../nginx-rtmp-module-1.1.11/
sudo make
sudo make install

添加防火墙白名单,局域网其它机器可以通过ip访问Nginx

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 8000 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

sudo service iptables save

sudo service iptables restart

查看防火墙配置

sudo cat /etc/sysconfig/iptables

或者

sudo iptables -L -n

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       8000;
        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;
        #}
    }

    server {
        listen       8080;
        server_name  localhost;

        # sample handlers
        #location /on_play {
        #    if ($arg_pageUrl ~* localhost) {
        #        return 201;
        #    }
        #    return 202;
        #}
        #location /on_publish {
        #    return 201;
        #}

        #提供vod点播服务
        location /vod {
            alias /var/flvs;
        }

        # 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 html;
        }

        # rtmp control
        location /control {
            rtmp_control all;
        }

    location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


    # 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;
    #    }
    #}

}

rtmp {
    server {
        listen 1935;
        ping 30s;
        notify_method get;

        application myapp {
            live on;

            # sample play/publish handlers
            #on_play http://localhost:8080/on_play;
            #on_publish http://localhost:8080/on_publish;

            # sample recorder
            #recorder rec1 {
            #    record all;
            #    record_interval 30s;
            #    record_path /tmp;
            #    record_unique on;
            #}

            # sample HLS
            #hls on;
            #hls_path /tmp/hls;
            #hls_sync 100ms;
        }

        # Video on demand
        application vod {
            #play /home/cmcc/test;
        play /var/flvs;
        }

        # Video on demand over HTTP
        application vod_http {
            play http://localhost:8080/vod/;
        }

    application hls{
        live on;
        hls on;
        hls_path /tmp/hls;
        hls_fragment 5s;
    }        

    }
}

将nginx-rtmp-module-1.1.11目录下的stat.xls放到nginx配置的stat文件所在目录,通过访问http://localhost:8080/stat 来查看Nginx RTMP状态

sudo cp stat.xsl /usr/local/nginx/html/

配置nginx开机自启动
使用官方脚本:Red Hat NGINX Init Script
配置教程:Linux(CentOS)下设置nginx开机自动启动和chkconfig管理

RTMP测试方法一,安装VLC播放器
教程:Install VLC on CentOS/RHEL 6.5

RTMP测试方法二,使用JWPlayer JS库,这里需要注意,需要在jwplayer官网注册之后,获得一个key之后,在加载jwplayer.js的页面将key一起配置,才可以使用。然后新建一个play.html,代码如下:

<html>
  <head>
    <script src="./jwplayer/jwplayer.js"></script>
    <script>jwplayer.key="LxctqkZ2gRUEaCXwBPcw7xhlN2PqOcYO+IqfkA==";</script>
  </head>

<body>
  <div>点播:rtmp://10.2.44.216/vod/mp4:large.mp4</div>
  <div id='my-video'></div>
  <script type='text/javascript'>
    jwplayer('my-video').setup({
      file:'rtmp://10.2.44.216/vod/mp4:large.mp4'
    });
  </script>
  <div>直播:rtmp://10.2.44.216/myapp/live</div>
  <div id='my-video1'></div>
  <script type='text/javascript'>
    jwplayer('my-video1').setup({
      file:'rtmp://10.2.44.216/myapp/live'
    });
  </script>
</body>
</html>

点播功能效果:
在/var/flvs/目录下放入一个h264编码的视频文件large.mp4,然后在play.html中设置rtmp地址为rtmp://10.2.44.216/vod/mp4:large.mp4

点播

此时stat页面:


http://10.2.44.216:8080/stat

直播功能效果:
需要Android手机一部,使用yasea作为推流的客户端APP,修改开源项目中的代码,设置rtmp地址为"rtmp://10.2.44.216/myapp/live",效果如下

直播 直播stat

参考
使用 nginx 和 rtmp 插件搭建视频直播和点播服务器
Centos6.5 nginx+nginx-rtmp配置流媒体服务器

上一篇下一篇

猜你喜欢

热点阅读