技术分享| 如何搭建直播场景下的推拉流媒体服务器

介绍

本文使用的流媒体服务器的搭建是基于rtmp(Real Time Message Protocol)协议的,rtmp协议是应用层的协议,要依靠底层的传输层协议,比如tcp协议来保证信息传输的可靠性。

相关服务: Nginx、srs、MediaServer等三种推流服务

系统: CentOS Linux release 7.9.2009

推流工具:OBS 27.1.3

拉流工具:PotPlayer 1.7.21564

注意事项:由于内网测试 关闭防火墙与SElinux

Nginx部署

Nginx需要nginx-rtmp-module模块,yum安装没有此模块,所以本次使用源码安装

## 安装Nginx所需依赖
[root@localhost ~]# yum install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel git

## 下载所需安装包
[root@localhost ~]# cd  /home/artc
[root@localhost artc]# mkdir Nginx #创建源码目录 后面的源码都放在这个目录
[root@localhost artc]# cd Nginx
[root@localhost Nginx]# git clone https://github.com/nginx/nginx.git #从github服务器上将nginx的源代码下载下来
[root@localhost Nginx]# git clone https://github.com/arut/nginx-rtmp-module.git #将rtmp模块的源码下载下来
复制代码
## 编译安装Nginx
[root@localhost ~]# cd /home/artc/Nginx/nginx
[root@localhost nginx]# ./auto/configure --prefix=/usr/local/nginx \
        --with-http_v2_module \
        --with-http_flv_module \
        --with-http_mp4_module \
        --add-module=../nginx-rtmp-module/
[root@localhost nginx]# make && make install 
复制代码
## 更改配置文件
[root@localhost nginx]# cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
#在http模块外添加
rtmp  {
    server  {
        listen 1915;
        timeout 20s;

        application live  {
            live on;
            record off;
        }

## 一个音视频模块
        application anyrtc  {
            live on;
            hls  on;
            hls_path temp/zyh;
            hls_playlist_length 5s;
            hls_fragment 1s;
        }

        application vod  {
            play /var/flvs;
        }

        application vod_http  {
            play http://服务器IP/vod;
        }

## 一个音视频模块
        application hls  {
            live on;
            hls on;
            record off;
            hls_path /tmp/hls;
            hls_playlist_length 5s;
            hls_fragment 1s;
        }
    }
}

## 启动Nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx
复制代码

rtmp地址: rtmp://192.168.1.13:1915/anyrtc

OSB推流

在这里插入图片描述

确定———>开始推流

PotPlayer拉流

右键——>打开链接

在这里插入图片描述

拉取成功

在这里插入图片描述

SRS部署

下载地址:ossrs.net/releases/do…

在这里插入图片描述

## 安装依赖
[root@localhost artc]# yum install -y redhat-lsb

## 部署
[root@localhost artc]# unzip SRS-CentOS7-x86_64-3.0.168
[root@localhost artc]# cd SRS-CentOS7-x86_64-3.0.168/
[root@localhost SRS-CentOS7-x86_64-3.0.168]# ./INSTALL 
[root@localhost SRS-CentOS7-x86_64-3.0.168]# systemctl start srs.service

## 配置文件在/usr/local/srs
[root@localhost SRS-CentOS7-x86_64-3.0.168]# cd /usr/local/srs/conf
[root@localhost conf]# vim srs.conf
# main config for srs.
# @see full.conf for detail config.

listen              1935;    			##rtmp默认端口
max_connections     1000;
srs_log_tank        file;
srs_log_file        ./objs/srs.log;
daemon              on;
http_api {
    enabled         on;
    listen          1985;
}
http_server {
    enabled         on;
    listen          9090;				## srs管理平台端口  默认是8080 这里端口冲突改为9090
    dir             ./objs/nginx/html;
}
stats {
    network         0;
    disk            sda sdb xvda xvdb;
}
vhost __defaultVhost__ {
    hls {
        enabled         on;
    }
    http_remux {
        enabled     on;
        mount       [vhost]/[app]/[stream].flv;
    }
}
复制代码

rtmp地址: rtmp://192.168.1.13:1935

OSB推流

在这里插入图片描述

确定———>开始推流

PotPlayer拉流

右键——>打开链接

在这里插入图片描述

拉取成功

在这里插入图片描述

ZLMediaKit部署

下载地址: git clone --depth 1 gitee.com/xia-chu/ZLM…

## 准备环境
[root@localhost artc]# yum -y update

## gcc和yasm需要高版本  小编用的是gcc4.8.5 yasm1.2.0
[root@localhost artc]# yum -y update   yasm  gcc* 
[root@localhost artc]# yum -y install  openssl-devel  SDL-devel

## 编译安装cmake 3.8.2
[root@localhost ~]# cd /home/artc
[root@localhost artc]# wget https://cmake.org/files/v3.8/cmake-3.8.2.tar.gz
[root@localhost artc]# tar zxvf cmake-3.8.2.tar.gz
[root@localhost artc]# cd cmake-3.8.2
[root@localhost cmake-3.8.2]# ./bootstrap
[root@localhost cmake-3.8.2]# gmake
[root@localhost cmake-3.8.2]# gmake install

## 国内库比较快
[root@localhost ~]# cd  /home/artc 
[root@localhost artc]# git clone --depth 1 https://gitee.com/xia-chu/ZLMediaKit
[root@localhost artc]# cd ZLMediaKit
#千万不要忘记执行这句命令
[root@localhost ZLMediaKit]# git submodule update --init

## 编译安装
[root@localhost ZLMediaKit]# mkdir build
[root@localhost ZLMediaKit]# cmake .
[root@localhost ZLMediaKit]# make -j4

## ZLMediaKit服务目录
[root@localhost ZLMediaKit]# cd release/linux/Debug

## 配置文件
[root@localhost Debug]# vim config.ini 
[rtmp]  ##228行左右
handshakeSecond=15
keepAliveSecond=15
modifyStamp=0
port=1955     				## 默认1935   端口冲突 修改了1955
sslport=0

## 程序启动
[root@localhost Debug]# ./MediaServer	   ## 前台启动
[root@localhost Debug]# ./MediaServer -d & ## 后台启动

## 软连接
[root@localhost Debug]# ln -sf /home/ZLMediaKit/release/linux/Debug/MediaServer /usr/bin/
复制代码

rtmp地址: rtmp://192.168.1.13:1955

OSB推流

在这里插入图片描述

确定———>开始推流

PotPlayer拉流

右键——>打开链接

在这里插入图片描述

拉取成功

在这里插入图片描述

rtsp推流地址: rtsp://192.168.1.13

ffmpeg推流

ffmpeg    -re  -i  yace.mp4    -vcodec h264 -acodec aac -strict -2   -f rtsp -rtsp_transport tcp  rtsp://192.168.1.13/anyrtc/ZLMediaKit
复制代码

在这里插入图片描述

PotPlayer拉流

右键——>打开链接

在这里插入图片描述

拉取成功

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自juejin.im/post/7075541316611866637