因工作需要,及考虑成本因素,需要探索一套免费的移动端基于rtmp推流的直播解决方案,过程虽稍显曲折,但最终还是完成了目标。在这里将记录下来,以便日后查阅。
总体思路
移动端推流(RTMP) ---> 流媒体服务器 ---> web服务器 ---> 网页播放
移动端推流工具使用 Larixbroadcaster ,支持安卓和苹果,并且有提供SDK,方便集成与二次开发,app商店有载,https://softvelum.com/larix
流媒体服务器的搭建使用 nginx+rtmp 方案, nginx-rtmp模块github坐标,https://github.com/arut/nginx-rtmp-module
web容器直接使用 nginx ,http://nginx.org/en/download.html
网页播放,采用 ckplayer 作为web播放器,http://www.ckplayer.com
1. Larixbroadcaster的安装过程省略了
2. 流媒体服务器的搭建,nginx+rtmp
- 1,使用docker容器,公共仓库有大神制作好的镜像,适合测试使用,便捷快速。
Centos7环境下的安装启动脚本
#!/bin/sh
# Author: Alusss
###
##docker-compose版本,https://github.com/docker/compose/releases/查阅可用版本
dcv=1.25.1
###安装docker
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.nginx.bak -f
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak -f
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.bak -f
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
rm -rf /var/lib/docker
curl -sSL https://get.daocloud.io/docker | sh
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
##安装docker-compose(选装)
yum -y remove docker-compose
rm -rf /usr/local/bin/docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/$dcv/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
##检查版本
docker -v
docker-compose -v
- 2,linux环境,下载nginx源码和nginx-rtmp-module,自行编译安装。
编译安装脚本
#!/bin/sh
# Author: Alusss
###
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.nginx-rtmp -f
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.rtmp -f
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.rtmp -f
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
yum install -yq tar wget gcc gcc-c++ make automake autoconf pcre pcre-devel zlib zlib-devel openssl openssl-devel
#设置nginx版本,rtmp版本
nginx_ver="1.16.1"
rtmp_ver="1.2.1"
dir="/usr/local/nginx"
temp="/root/nginx-rtmp"
user="root"
group="root"
args="--user=$user --group=$group --prefix=$dir --conf-path=$dir/conf/nginx.conf --pid-path=$dir/conf/nginx.pid --lock-path=$dir/lock/nginx.lock --error-log-path=$dir/log/error.log --http-log-path=$dir/log/access.log --with-http_gzip_static_module --http-client-body-temp-path=$dir/temp/client --http-proxy-temp-path=$dir/temp/proxy --http-fastcgi-temp-path=$dir/temp/fastcgi --http-uwsgi-temp-path=$dir/temp/uwsgi --http-scgi-temp-path=$dir/temp/scgi --with-http_stub_status_module --with-http_ssl_module"
mkdir $temp
cd $temp
curl -o "nginx-$nginx_ver.tar.gz" http://nginx.org/download/"nginx-$nginx_ver.tar.gz"
tar -xzf "nginx-$nginx_ver.tar.gz"
rm -f "nginx-$nginx_ver.tar.gz"
curl -o "nginx-rtmp-module-$rtmp_ver.tar.gz" https://codeload.github.com/arut/nginx-rtmp-module/tar.gz/v"$rtmp_ver"
tar xzf "nginx-rtmp-module-$rtmp_ver.tar.gz"
rm -f "nginx-rtmp-module-$rtmp_ver.tar.gz"
useradd -s /sbin/nologin $user -M
cd nginx-$nginx_ver
./configure $args --add-module="$temp/nginx-rtmp-module-$rtmp_ver"
make -j8
make install
-
3,Windows平台,自行编译安装,可参考:https://www.cnblogs.com/KendoCross/p/7585654.html
-
4,分享一个Windows平台下编译好的版本,基于nginx-1.9.6, https://pan.baidu.com/s/1ffo0ZPMtNLgii3L_lS61mA
-
5,贴一下rtmp部分的配置
rtmp {
server {
listen 1935;
access_log logs/rtmp.access.log;
application live {
live on;
hls on;
hls_path html/hls;
hls_fragment 5s;
publish_notify on;
}
application hls {
live on;
hls on;
hls_path temp/hls;
hls_fragment 5s;
}
record all;
record_unique on;
record_path /data/rtmp/record;
record_suffix -%Y-%m-%d-%H_%M_%S.flv;
}
}
推流地址: rtmp://ip:1935/live/a
nginx下html/hls下会生成a.m3u8文件。
通过ckplayer插件,(ckplay已经放在上面nginx里面)在http://ip/index.html页面播放。