手手挤挤 发表于 2017-12-16 18:07:02

基于X3路由器和树莓派3的rtmp、HLS直播平台

本帖最后由 ky123 于 2018-2-5 10:57 编辑

目录
1、服务器搭建
2、频道配置
3、网站配置
4、跨域组网
5、视频演示
————————————————————————————————
1、服务器搭建
下载ngnix服务器源码wget http://nginx.org/download/nginx-1.13.5.tar.gz下载rtmp模块git clone https://github.com/arut/nginx-rtmp-module.git安装依赖sudo apt-get install openssl libssl-dev
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev解压nginx源码,并进入该目录
进行配置./configure --prefix=/usr/local/nginx--add-module=../nginx-rtmp-module--with-http_ssl_module   make && sudo make install因为需要写文件到/usr/local目录下,因此安装命令需要sudo权限,
安装完成了,接下来开始配置直播服务器

二、频道配置
rtmp是直播推流采用的协议,因此这里以rtmp推流、拉流的过程做个示范
更改配置文件,nano /usr/local/nginx/conf/nginx.conf这里给出我的配置文件,修改的地方已用+++++++++++++++++++标出#usernobody;
worker_processes1;

#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;

#pid      logs/nginx.pid;


events {
    worker_connections1024;
}


http {
    include       mime.types;
    default_typeapplication/octet-stream;

    #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_loglogs/access.logmain;

    sendfile      on;
    #tcp_nopush   on;

    #keepalive_timeout0;
    keepalive_timeout65;

    #gzipon;

    server {
      listen       80;
      server_namelocalhost;

      #charset koi8-r;

      #access_loglogs/host.access.logmain;

      location / {
            #root   html;
            #indexindex.html index.htm;
                root /home/pi/hhh/;
                index index.html index.htm;
      }

       #+++++++++++++++++++
       location /hls{
            types{
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root/home/pi/;
                add_header Cache-Control no-cache;
   #         expires -1;
      }
   # +++++++++++++++++++


      #error_page404            /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_indexindex.php;
      #    fastcgi_paramSCRIPT_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 {
      #    denyall;
      #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_namesomenamealiasanother.alias;

    #    location / {
    #      root   html;
    #      indexindex.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_namelocalhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_keycert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout5m;

    #    ssl_ciphersHIGH:!aNULL:!MD5;
    #    ssl_prefer_server_cipherson;

    #    location / {
    #      root   html;
    #      indexindex.html index.htm;
    #    }
    #}

}
#+++++++++++++++++++
rtmp {   
   
    server {   
   
      listen 1935;
          publish_time_fix on;
      chunk_size 4000;   
         

      application carlpc{
                live on;
                allow publish all; # control access privilege
             allow play all; # control access privilege
      }
            
      application carlpc_hls {
            live on;   
            hls on;   
            hls_path /home/pi/hls;   
            hls_fragment 5s;
            hls_base_url http://192.168.137.21/hls/;
                allow publish all;
                allow play all;
      }   
    }   
}
#+++++++++++++++++++
配置完成后,第一次启动nginx/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf再一执行上条命令可以看到以下端口已绑定


三、网站配置
网站环境的目的在于利用网页访问之前设置的hls索引和切片所在的文件夹。
配置也很简单,主要在于声明网页的location即可,笔者直接将其声明在根目录下,配置如下
[*]location / {
[*]#         root   html;
[*]#         indexindex.html index.htm;
[*]                root /home/pi/hhh;
[*]                index index.html index.htm;
[*]      }

复制代码
接下来,编辑index.html网页。这里需要网页播放器的支持,首选当然是videojs啦,所以采用videojs播放器来播放hls流。index。html的配置如下
[*]
[*]<!DOCTYPE html>
[*]<html>
[*]<head>
[*]<meta charset=utf-8 />
[*]<title>Carlpc的直播小站</title>
[*]
[*]
[*]
[*]<linkrel="stylesheet">
[*]<script src="https://unpkg.com/video.js/dist/video.js"></script>
[*]<script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"></script>
[*]
[*]</head>
[*]<body>
[*]<h1>直播站--HLS</h1>
[*]
[*]<video id="my_video_1" class="video-js vjs-default-skin" controls preload="auto" width="683" height="384"
[*]data-setup='{"autoplay": true}'>
[*]    <source src="http://192.168.137.66:80/hls/test.m3u8" type="application/x-mpegURL">
[*]</video>
[*]
[*]<script>
[*]</script>
[*]
[*]</body>
[*]</html>
[*]

复制代码
写完后,还没完记得给这个文件夹、父文件夹、以及该文件添加权限,权限掩码777,最后的效果动图如下。

https://www.cirmall.com/bbs/data/attachment/forum/201711/13/220547at7tt5d7yr7intss.gif
接下来是两个推流命令raspivid -t 0 -w 320 -h 240 -o - | ffmpeg-i --c copy -f flv rtmp://localhost:1935/carlpc

ffmpeg -re -stream_loop -1 -i /home/pi/ffmpeg/test*** -c copy -f flv rtmp://localhost:1935/carlpc_hls/test并设置成开机自启动。
4、跨域组网
打开云端管理界面,首先是添加成员,由于花生壳免费的只能有3个成员,因此我添加了路由器,手机,电脑三个设备。

https://www.cirmall.com/bbs/data/attachment/forum/201711/25/034000d9ndesrr3ydsyxqs.jpg
有了设备后,创建网络,将这些设备异地互联
https://www.cirmall.com/bbs/data/attachment/forum/201711/25/0340005staj5jmsakt7jka.jpg
https://www.cirmall.com/bbs/data/attachment/forum/201711/25/0340013kmy2x24qtxmwgm3.jpg

路由器所在的网络下可直接通过分配的ip进行访问,而其他两个设备,则需要安装蒲公英客户端
5、视频演示
http://v.youku.com/v_show/id_XMzIzNjcxODMyMA==.html
页: [1]
查看完整版本: 基于X3路由器和树莓派3的rtmp、HLS直播平台

Baidu
map