查看: 4679|回复: 5

【BPI-M2Berry试用】搭建直播服务器平台

[复制链接]
  • TA的每日心情
    开心
    2017-12-20 11:14
  • 签到天数: 5 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    发表于 2017-10-7 16:26:16 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 手手挤挤 于 2017-11-9 21:49 编辑

    目录
    1、准备
    2、编译
    3、配置

    4、推流、拉流



    一、准备

    下载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   
    复制代码
    159.png
    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
    复制代码
    再一执行上条命令可以看到以下端口已绑定
    2017-10-07_15-50-01.gif
    四、推流、拉流
    推流方式,有三种
    1、OBS推流

    2、FMLE推流

    135.png
    3、FFMPEG推流



    为了方便,直接使用obs进行推流,那有个问题是推流的地址是啥呢?
    根据上面的配置文件rtmp采用端口1935,并且配置了两个发布点carlpc和carlpc_hls,一个是用来rtmp推流拉流,一个则是rtmp推流后经过切片,以hls的协议进行拉流。
    因此,两个发布点的推流地址为(test为自定义的流名)
    1. rtmp://192.168.137.21:1935/carlpc

    2. rtmp://192.168.137.21:1935/carlpc_hls/test
    复制代码
    =============================================
    rtmp推流、拉流
    178.png
    设置后进行推流,接着进行拉流
    拉流地址与推流地址一样

    456.png
    =====================================================
    rtmp推流,hls拉流
    17.png

    这里拉流地址与推流地址不一样了,配置中可以看到,要想通过此拉流,需要走http协议的80端口,来读取/home/pi/hls目录下的切片文件,因此拉流地址使用的端口是80端口。
    先通过观看/home/pi/hls是否有切片文件
    2017-10-07_162148.png
    那怎么读取这些文件呢?比如要读取第4个切片文件就可以通过以下URL进行读取
    1. http://192.168.137.21:80/carlpc_hls/test-4.ts
    复制代码
    2017-10-07_162300.png

    还有最重要的一个问题,很多人反映不能读取m3U8文件,那是因为m3u8里面没有配置路径的关系,
    像如下,指定hls的基础url就能解决这个问题
    1. hls_base_url http://192.168.137.21/hls/;  
    复制代码
    更多的配置选项请参阅
    1. https://github.com/arut/nginx-rtmp-module/wiki/Directives#hls_base_url
    复制代码
    最后,祝大家双节快乐,今天先写这些啦
    回复

    使用道具 举报

    该用户从未签到

    发表于 2017-10-7 19:10:13 | 显示全部楼层
    挤挤 ,明天是节日的最后一天,小尾巴
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2017-12-20 11:14
  • 签到天数: 5 天

    连续签到: 1 天

    [LV.2]偶尔看看I

     楼主| 发表于 2017-10-7 20:06:45 | 显示全部楼层
    ky123 发表于 2017-10-7 19:10
    挤挤 ,明天是节日的最后一天,小尾巴

    今天发个帖,让可依觉得我天天都在发帖
    回复 支持 反对

    使用道具 举报

  • TA的每日心情

    2021-12-7 12:35
  • 签到天数: 1354 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2017-10-8 21:41:26 | 显示全部楼层
    参考和学习
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2017-10-9 08:55:05 | 显示全部楼层
    手手挤挤 发表于 2017-10-7 20:06
    今天发个帖,让可依觉得我天天都在发帖

    哈哈哈,你说的对
    早呀
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    难过
    2021-2-27 22:16
  • 签到天数: 1568 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2017-12-19 16:31:40 | 显示全部楼层
    写的很流畅
    回复 支持 反对

    使用道具 举报

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

    本版积分规则



    手机版|小黑屋|与非网

    GMT+8, 2024-4-23 23:34 , Processed in 0.165048 second(s), 25 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.