回答

收藏

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

Raspberry Pi Raspberry Pi 5937 人阅读 | 0 人回复 | 2017-12-16

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

目录
1、服务器搭建
2、频道配置
3、网站配置
4、跨域组网
5、视频演示

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

二、频道配置

rtmp是直播推流采用的协议,因此这里以rtmp推流、拉流的过程做个示范
更改配置文件,
  1. nano /usr/local/nginx/conf/nginx.conf
复制代码
这里给出我的配置文件,修改的地方已用+++++++++++++++++++标出
  1. #user  nobody;
  2. worker_processes  1;

  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;

  6. #pid        logs/nginx.pid;


  7. events {
  8.     worker_connections  1024;
  9. }


  10. http {
  11.     include       mime.types;
  12.     default_type  application/octet-stream;

  13.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  14.     #                  '$status $body_bytes_sent "$http_referer" '
  15.     #                  '"$http_user_agent" "$http_x_forwarded_for"';

  16.     #access_log  logs/access.log  main;

  17.     sendfile        on;
  18.     #tcp_nopush     on;

  19.     #keepalive_timeout  0;
  20.     keepalive_timeout  65;

  21.     #gzip  on;

  22.     server {
  23.         listen       80;
  24.         server_name  localhost;

  25.         #charset koi8-r;

  26.         #access_log  logs/host.access.log  main;

  27.         location / {
  28.             #root   html;
  29.             #index  index.html index.htm;
  30.                 root /home/pi/hhh/;
  31.                 index index.html index.htm;
  32.         }

  33.        #+++++++++++++++++++
  34.        location /hls  {
  35.             types{
  36.                 application/vnd.apple.mpegurl m3u8;
  37.                 video/mp2t ts;
  38.             }
  39.             root  /home/pi/;
  40.                 add_header Cache-Control no-cache;
  41.    #         expires -1;
  42.         }
  43.      # +++++++++++++++++++


  44.         #error_page  404              /404.html;

  45.         # redirect server error pages to the static page /50x.html
  46.         #
  47.         error_page   500 502 503 504  /50x.html;
  48.         location = /50x.html {
  49.             root   html;
  50.         }

  51.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  52.         #
  53.         #location ~ \.php$ {
  54.         #    proxy_pass   http://127.0.0.1;
  55.         #}

  56.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  57.         #
  58.         #location ~ \.php$ {
  59.         #    root           html;
  60.         #    fastcgi_pass   127.0.0.1:9000;
  61.         #    fastcgi_index  index.php;
  62.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  63.         #    include        fastcgi_params;
  64.         #}

  65.         # deny access to .htaccess files, if Apache's document root
  66.         # concurs with nginx's one
  67.         #
  68.         #location ~ /\.ht {
  69.         #    deny  all;
  70.         #}
  71.     }


  72.     # another virtual host using mix of IP-, name-, and port-based configuration
  73.     #
  74.     #server {
  75.     #    listen       8000;
  76.     #    listen       somename:8080;
  77.     #    server_name  somename  alias  another.alias;

  78.     #    location / {
  79.     #        root   html;
  80.     #        index  index.html index.htm;
  81.     #    }
  82.     #}


  83.     # HTTPS server
  84.     #
  85.     #server {
  86.     #    listen       443 ssl;
  87.     #    server_name  localhost;

  88.     #    ssl_certificate      cert.pem;
  89.     #    ssl_certificate_key  cert.key;

  90.     #    ssl_session_cache    shared:SSL:1m;
  91.     #    ssl_session_timeout  5m;

  92.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  93.     #    ssl_prefer_server_ciphers  on;

  94.     #    location / {
  95.     #        root   html;
  96.     #        index  index.html index.htm;
  97.     #    }
  98.     #}

  99. }
  100. #+++++++++++++++++++
  101. rtmp {   
  102.      
  103.     server {   
  104.      
  105.         listen 1935;
  106.           publish_time_fix on;
  107.         chunk_size 4000;   
  108.            

  109.         application carlpc{
  110.                 live on;
  111.                 allow publish all; # control access privilege
  112.              allow play all; # control access privilege
  113.         }
  114.             
  115.         application carlpc_hls {  
  116.             live on;   
  117.             hls on;   
  118.             hls_path /home/pi/hls;   
  119.             hls_fragment 5s;
  120.             hls_base_url http://192.168.137.21/hls/;  
  121.                 allow publish all;
  122.                 allow play all;
  123.         }   
  124.     }   
  125. }
  126. #+++++++++++++++++++
复制代码
配置完成后,第一次启动nginx
  1. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
复制代码
再一执行上条命令可以看到以下端口已绑定


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

[color=rgb(51, 102, 153) !important]复制代码

接下来,编辑index.html网页。这里需要网页播放器的支持,首选当然是videojs啦,所以采用videojs播放器来播放hls流。index。html的配置如下
  • <!DOCTYPE html>
  • <html>
  • <head>
  • <meta charset=utf-8 />
  • <title>Carlpc的直播小站</title>
  •   <link  rel="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>


[color=rgb(51, 102, 153) !important]复制代码

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


接下来是两个推流命令
  1. raspivid -t 0 -w 320 -h 240 -o - | ffmpeg  -i -  -c copy -f flv rtmp://localhost:1935/carlpc

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


有了设备后,创建网络,将这些设备异地互联



路由器所在的网络下可直接通过分配的ip进行访问,而其他两个设备,则需要安装蒲公英客户端
5、视频演示

关注下面的标签,发现更多相似文章
分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条