码迷,mamicode.com
首页 > 其他好文 > 详细

Simple live system using nginx

时间:2019-11-20 14:11:14      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:false   record   ram   stat   als   gnu   keep   pes   control   

1. Install nginx

#compiler setup
if [[ $1 = arm ]]; then
compiler=`CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++`
elif [[ $1 = himix100 ]]; then
compiler=`CC=arm-himix100-linux-gcc CXX=arm-himix100-linux-g++`
elif [[ $1 = himix200 ]]; then
compiler=`CC=arm-himix200-linux-gcc CXX=arm-himix200-linux-g++`
else
:
fi

#Preinstalled directory
install=/usr/local/nginx

#Delete installed directory
rm -rf ${install}

#library path
pkg_config=${install}/lib/pkgconfig
lib_config=`CPPFLAGS=-I${install}/include CFLAGS=-I${install}/include LDFLAGS=-L${install}/lib`


#Delete compiled directory
if false;then
for i in `ls .`
    do
    if [ -d $i ];then
    rm -rf $i
    fi
done
fi

mkdir ${install}
cp nginx-http-flv-module-1.2.7.tar.gz ${install}
tar xvf ${install}/nginx-http-flv-module-1.2.7.tar.gz -C ${install}
tar xvf nginx-1.17.5.tar.gz
cd nginx-1.17.5
./configure --prefix=${install} --add-module=${install}/nginx-http-flv-module-1.2.7
make && make install
cd ..

 

2. Edit  nginx.conf

nginx-http-flv-module-1.2.7/test/nginx.conf

worker_processes  1;

error_log  logs/error.log debug;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;

        application myapp {
            live on;

            #record keyframes;
            #record_path /tmp;
            #record_max_size 128K;
            #record_interval 30s;
            #record_suffix .this.is.flv;

            #on_publish http://localhost:8080/publish;
            #on_play http://localhost:8080/play;
            #on_record_done http://localhost:8080/record_done;
        }
    }
}

http {
    server {
        listen      8002;

        location /live{
            flv_live on;
        }

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /usr/local/nginx/nginx-http-flv-module-1.2.7/;
        }

        location /control {
            rtmp_control all;
        }

        #location /publish {
        #    return 201;
        #}

        #location /play {
        #    return 202;
        #}

        #location /record_done {
        #    return 203;
        #}

        location /rtmp-publisher {
            root /usr/local/nginx/nginx-http-flv-module-1.2.7/test;
        }

        location / {
            root /usr/local/nginx/nginx-http-flv-module-1.2.7/test/www;
        }
    }
}

Reference example:

Example

Assume that listen directive specified in http block is:

http {
    ...
    server {
        listen 8080; #not default port 80
        ...

        location /live {
            flv_live on;
        }
    }
}

And listen directive specified in rtmp block is:

rtmp {
    ...
    server {
        listen 1985; #not default port 1935
        ...

        application myapp {
            live on;
        }
    }
}

 

3. Startup nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

#ps -ef|grep nginx
#kill -TERM 2132 or kill -INT 2132
#pkill -9 nginx
#./nginx -t
#./nginx -s reload

 

4. push rtmp stream

ffmpeg -re -i source.200kbps.768x320.flv -c copy -f flv rtmp://127.0.0.1:1935/myapp/mystream

 

5. player
rtmp://localhost:1935/myapp/mystream

http://localhost:8002/live?port=1935&app=myapp&stream=mystream

 

6. Other optimized configuration

worker_processes  10;
events {
    worker_connections  10240;
}
rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;
rtmp{
    out_queue 4096;
    out_cork 8;
    max_streams 128;
    timeout 15s;
    drop_idle_publisher 15s;
    log_interval 5s;
    log_size 1m;
    server{
     listen 9000;
     server_name 127.0.0.1;
     application myapp{
         live on;
         gop_cache on;
      }
     application hls{
      live on;
      hls on;
      hls_path /usr/local/nginx/html/hls; 
    }
     application dash{
       live on;
       dash on;
       dash_path /usr/local/nginx/html/dash;
     }
    
    }
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8002;
    server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    location /live{
        flv_live on;
        chunked_transfer_encoding  on;
        add_header ‘Access-Control-Allow-Origin‘ ‘*‘;
        add_header ‘Access-Control-Allow-Credentials‘ ‘true‘;
    }
    location /hls{
        types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
         }
         root /usr/local/nginx/html/hls;
         add_header ‘Cache-Control‘ ‘no-cache‘;
    }
     location /dash {
            root /usr/local/nginx/html/dash;
            add_header ‘Cache-Control‘ ‘no-cache‘;
        }
    
     location /stat {
            #configuration of push & pull status
              rtmp_stat all;
              rtmp_stat_stylesheet stat.xsl;
         }
    location /stat.xsl {
      root /usr/local/nginx/nginx-http-flv-module-1.2.7;
    }

     location /control {
            rtmp_control all; #configuration of control module of rtmp
        }    
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

rtmp

rtmp://example.com[:port]/myapp/mystream

hls
http://example.com[:port]/dir/streamname.m3u8

dash
http://example.com[:port]/dir/streamname.mpd

 

7. Reference design:

https://github.com/winshining/nginx-http-flv-module

https://blog.csdn.net/caowenjing123/article/details/94623466

Simple live system using nginx

标签:false   record   ram   stat   als   gnu   keep   pes   control   

原文地址:https://www.cnblogs.com/dong1/p/11897350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!