Nginx 搭建直播流服務

搭建直播流服務器

技術

  • Nginx
  • nginx-rtmp-module-1.1.11 (nginx 插件)

步驟

  1. 下載nginx 和 nginx-rtmp-module 源文件
  2. 解壓 nginx 和 nginx-rtmp-module
  3. ./configure --add-module=/home/shichangcheng/live/nginx-rtmp-module-1.1.11(將nginx第三方模塊加入nginx)
  4. make ; make install;
  5. 配置nginx.conf
http {
	include       mime.types;
	default_type  application/octet-stream;


	sendfile        on;

	keepalive_timeout  65;

	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   500 502 503 504  /50x.html;
	    location = /50x.html {
	        root   html;
	    }

		# hls living
	    location /stat {
	        rtmp_stat all;
	        rtmp_stat_stylesheet stat.xsl;
	    }

	    location /stat.xsl {
	        root /usr/local/nginx/html/nginx-rtmp-module/;
	    }

	    location /control {
	        rtmp_control all;
	    }
	    location /hls {
	       types{
	         application/vnd.apple.mpegurl m3u8;
	         video/mp2t ts;
	        }
	       root /home/shichangcheng/live/my-app;
	    }
	}
}

rtmp {
    server {
        listen 1935;

        application hls {
             live on;
             hls on;
             hls_path /home/shichangcheng/live/my-app/hls;
             hls_fragment 5s;
       }

        application live{
             live on;
             max_connections 1024;
             allow play all;

             record_path /home/shichangcheng/live/my-app/live;

             recorder audio
             {
                record audio;
                record_suffix -%d-%b-%y-%T.flv;
             }
             recorder chunked

             {
                record all;
                #record_max_size 5120K;  
                record_interval 15s;
                record_path /home/shichangcheng/live/my-app/live/chunked;
             }
        }
    }
}

遇到的問題

  1. liunx 的權限
  2. nginx user 修改爲root
  3. rtmp 中 record_path 保存客戶端採集視頻信息推動到服務器的地址,需要注意該文件的讀寫權限。
  4. 防火牆 iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 1935 -j ACCEPT

推流地址

rtmp://123.206.231.130/hls/(movie)
(movie)爲表示生成流文件的名字

播放地址

rtmp地址:rtmp://123.206.231.130/hls/(movie)
hvl 地址:http://123.206.231.138/hls/movie.m3u8

直播協議

  1. rtmp :rtmp 延時較低,需要使用flash,或者解碼器才能播放。
  2. hlv :hlv 延時較高,自己測未進過cdn一般優化,延時5。移動端網頁播放使用HLV協議。

客戶端

  1. 直播工具可以使用金山雲的DEMO
  2. 播放器
  • H5 只能使用 hlv 地址
  • 金山雲提供的播放SDK
  • PC客戶端 VLC Media Player

我的博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章