nginx+rtmp实现直播服务端

目前直播系统从功能上分为两种,直播和互动。普通直播对播放延迟要求不高,推流端也能设置本地延迟推流时间。互动直播对低延时要求较高,主要应用在即时通讯和在线教育等行业,从推流端采集到拉流端播放一般延时不超过500ms,更高的延迟会让人感觉到明显的不适。今天开始搭建我的第一个互动直播系统,首先实现推拉流和服务器的流中转,有了基本环境研究推流降低延迟,比如编码,rtmp换udp,延迟足够低后对服务端进行优化。

1. 准备服务器

租了一台阿里云ecs,2核8G,公网1M带宽,足够测试使用。

打开安全组设置,在入口方向开放1935端口,1935端口是rtmp默认监听端口,必须在这里设置开放,否则在服务器中打开和监听1935端口后公网环境连接不到该端口。

 

nginx+rtmp实现直播服务端_第1张图片

2. 服务端nginx+rtmp环境

1. 安装nginx相关依赖包

yum -y install pcre-devel openssl openssl-devel 

2. 创建工作目录,下载nginx和nginx-rtmp-module源码

mkdir work
cd work
wget http://nginx.org/download/nginx-1.10.0.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

3. 安装zip,解压源码

yum -y install unzip
tar -zxvf nginx-1.7.5.tar.gz
unzip master.zip

4. 配置,编译安装nginx

cd nginx-1.10.0
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master
make 
make install

5. 查看nginx安装信息

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
nginx -V

6. 启动,测试nginx

nginx -s stop
nginx

 

win10打开telnet测试,应用->程序和功能->启用或关闭windows功能:

nginx+rtmp实现直播服务端_第2张图片

telnet ip 端口,发现连接不上。关闭contos防火墙:

systemctl stop firewalld
nginx -s reload

再次测试,连接成功: 

nginx+rtmp实现直播服务端_第3张图片

 7. 配置rtmp服务相关

 

vi /usr/local/nginx/conf/nginx.conf

PS:注意是在http{}外进行添加:

rtmp {
    server {
            listen 1935;
            chunk_size 4096;

            application live {
                    live on;
                    record off;
            }
    }
}

完整的配置如下:

#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 /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /usr/local/nginx/hls/;
        }

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

}

rtmp {
    server {
            listen 1935;
            chunk_size 4096;

            application live {
                    live on;
                    record off;
            }
    }
}

把nginx-rtmp-module文件夹中的stat.xsl复制到/usr/lcoal/nginx/hls/文件夹中

监控地址:http://39.105.161.30/stat

重启nginx。

 3. 推拉流测试

1. obs studio推流,设置推流地址:

nginx+rtmp实现直播服务端_第4张图片

点击开始推流,如果按钮显示为停止推流,则推流成功,也可在http://39.105.161.30/stat 进行监控

2. vlc播放器拉流

 nginx+rtmp实现直播服务端_第5张图片

 

至此服务端和推拉流测试完成。

 

 

 

你可能感兴趣的:(nginx+rtmp实现直播服务端)