流媒体服务器

Linux
16 0

准备 RTMP

下载 rtmp

cd /usr/local/src
git clone https://github.com/arut/nginx-rtmp-module.git
cd /usr/local/src/nginx

增加模块

./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module

编译

make
#make install

# 报错
../nginx-rtmp-module/ngx_rtmp_eval.c:160:17: error: this statement may fall through [-Werror=implicit-fallthrough=]

# 解决
make CFLAGS='-Wno-implicit-fallthrough'

备份 nginx

mv /usr/local/nginx/sbin/nginx{,_`date +%F`}

替换 nginx

cp objs/nginx /usr/local/nginx/sbin

验证

nginx -V

重启

service nginx restart

配置参数 nginx.conf

user www www;
worker_processes auto;

error_log /usr/local/nginx/logs/error_nginx.log;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
  use epoll;
  worker_connections 51200;
  multi_accept on;
}

http {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 1024m;
  client_body_buffer_size 10m;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 120;
  server_tokens off;
  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  fastcgi_intercept_errors on;

  #Gzip Compression
  gzip on;
  gzip_buffers 16 8k;
  gzip_comp_level 6;
  gzip_http_version 1.1;
  gzip_min_length 256;
  gzip_proxied any;
  gzip_vary on;
  gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  ##Brotli Compression
  #brotli on;
  #brotli_comp_level 6;
  #brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

  ##If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
  #open_file_cache max=1000 inactive=20s;
  #open_file_cache_valid 30s;
  #open_file_cache_min_uses 2;
  #open_file_cache_errors on;

  server {
        listen       80;
        server_name  rtmp;
      
        # rtmp stat
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            # 源文件绝对路径,必填。如:~/nginx-rtmp-module/stat.xsl
            root /mnt/usb/lnmp/nginx-rtmp-module;
        }
      
        # sample handlers
        #location /on_play {
        #    if ($arg_pageUrl ~* localhost) {
        #        return 201;
        #    }
        #    return 202;
        #}
        #location /on_publish {
        #    return 201;
        #}
      
        #location /vod {
        #    alias /var/myvideos;
        #}
      
        # rtmp control
        location /control {
            rtmp_control all;
        }
      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
  }
}

rtmp {
    server {
        listen 1935;
        ping 30s;
        notify_method get;
        chunk_size 4096;
        
            # 视频直播,live 应用名可自定义
        application live {
            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
        # rtmp://localhost/vod/girl.mp4
        application vod {
            # 视频存放路径
            play /var/vod;
        }

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

生效

service nginx restart

防火墙

路径

/etc/iptables.up.rules

增加

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80   -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 1935 -j ACCEPT

生效

iptables-restore < /etc/iptables.up.rules

应用

stat:
http://localhost/stat

live:
OBS 设置
# 地址
rtmp://localhost/live

# 密钥自定义,必填
test

# 播放
rtmp://localhost/live/test

RTMP:
rtmp://localhost/vod/girl.mp4

HTTP:
http://localhost/demo/demo.m3u8

其它

  • 这里只是简单搭建一个直播平台。
  • 整体推流、拉流间存在延迟现象严重,涉及主机处理流媒体能力。
  • 打开 m3u8 文件有一些参数可查看。

参数解释

Nginx RTMP 模块 nginx-rtmp-module 指令详解
视频直播点播nginx-rtmp开发手册中文版

格式兼容

https://segmentfault.com/a/1190000009121042

更新 2018-04-16
评论 ( 0 )
私信
pic
code
pre