Docker 实战系列之 SRS 流媒体服务器

导读:本篇文章讲解 Docker 实战系列之 SRS 流媒体服务器,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1、简介

1.1、官网概述

SRS定位是运营级的互联网直播服务器集群,追求更好的概念完整性和最简单实现的代码。SRS提供了丰富的接入方案将RTMP流接入SRS, 包括推送RTMP到SRS、推送RTSP/UDP/FLV到SRS、拉取流到SRS。 SRS还支持将接入的RTMP流进行各种变换,譬如将RTMP流转码、流截图、 转发给其他服务器、转封装成HTTP-FLV流、转封装成HLS、 转封装成HDS、转封装成DASH、录制成FLV/MP4。SRS包含支大规模集群如CDN业务的关键特性, 譬如RTMP多级集群、源站集群、VHOST虚拟服务器 、 无中断服务Reload、HTTP-FLV集群。此外,SRS还提供丰富的应用接口, 包括HTTP回调、安全策略Security、HTTP API接口、 RTMP测速。SRS在源站和CDN集群中都得到了广泛的应用Applications。

1.2、官网资料

SRS 官网地址
SRS WIKI 地址
SRS HTTP 回调
SRS DRM 防盗链

1.3、镜像列表

SRS Docker Hub
在这里插入图片描述
在这里插入图片描述

2、准备工作

2.1、安装 Docker

CentOS7 安装 Docker

2.2、创建 bridge 网络并指定 IP 区间

#创建自定义网络
docker network create --driver bridge --subnet 172.0.0.0/16 woniu_network
 
#查看已存在网络
docker network ls

2.3、创建数据目录

# 创建数据目录
mkdir -p /home/docker/srs4

3、安装 SRS

3.1、创建配置文件

# 安装并启动 srs
docker run -p 1935:1935 -p 1985:1985 -p 8080:8080 \
--name srs \
ossrs/srs:v4.0.85

3.2、复制配置文件

# 把容器中的配置文件复制出来
docker cp -a srs:/usr/local/srs/conf /home/docker/srs4/conf

# 把容器中的数据文件复制出来
docker cp -a srs:/usr/local/srs/objs /home/docker/srs4/objs

# 删除 srs 容器
docker rm -f srs

3.3、启动 SRS

docker run -p 1935:1935 -p 1985:1985 -p 8080:8080 \
--name srs \
--network woniu_network \
--ip 172.0.0.35 \
--restart=always \
-v /home/docker/srs4/conf/:/usr/local/srs/conf/ \
-v /home/docker/srs4/objs/:/usr/local/srs/objs/ \
ossrs/srs:v4.0.85 

5、SRS 控制台

# SRS 控制台访问地址
http://服务器 IP 地址:8080

在这里插入图片描述
在这里插入图片描述

6、实战配置

6.1、自定义配置文件

# SRS 参考配置

listen              1935;
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          8080;
    dir             ./objs/nginx/html;
	# 开启 https 支持,需要开放 8088端口
	# https {
        # enabled on;
        # listen 8088;
        # key ./conf/woniu.key;
        # cert ./conf/woniu.crt;
    # }
}
vhost __defaultVhost__ {
    # http-flv设置
    http_remux{
        enabled    on;
        mount      [vhost]/[app]/[stream].flv;
        hstrs      on;
    }
 
    # hls设置
    hls {
        enabled         on;
        hls_fragment    1;
        hls_window      2;
        hls_path        ./objs/nginx/html;
        hls_m3u8_file   [app]/[stream].m3u8;
        hls_ts_file     [app]/[stream]-[seq].ts;
    }
	
	# dvr设置
	dvr {
        enabled             off;
        dvr_path            ./objs/nginx/html/[app]/[stream]/[2006]/[01]/[02]/[timestamp].flv;
        dvr_plan            segment;
        dvr_duration        30;
        dvr_wait_keyframe   on;
    }
	
	# rtc 设置
	rtc {
		enabled     on;
		bframe      discard;
    }
	
	# SRS支持refer防盗链:检查用户从哪个网站过来的。譬如不是从公司的页面过来的人都不让看。
    refer {
        # whether enable the refer hotlink-denial.
        # default: off.
        enabled         off;
        # the common refer for play and publish.
        # if the page url of client not in the refer, access denied.
        # if not specified this field, allow all.
        # default: not specified.
        all           github.com github.io;
        # refer for publish clients specified.
        # the common refer is not overrided by this.
        # if not specified this field, allow all.
        # default: not specified.
        publish   github.com github.io;
        # refer for play clients specified.
        # the common refer is not overrided by this.
        # if not specified this field, allow all.
        # default: not specified.
        play      github.com github.io;
    }
	
	# http 回调
	http_hooks {
	
		# 事件:发生该事件时,即回调指定的HTTP地址。
		# HTTP地址:可以支持多个,以空格分隔,SRS会依次回调这些接口。
		# 数据:SRS将数据POST到HTTP接口。
		# 返回值:SRS要求HTTP服务器返回HTTP200并且response内容为整数错误码(0表示成功),其他错误码会断开客户端连接。
		
        # whether the http hooks enable.
        # default off.
        enabled         on;
        
		# 当客户端连接到指定的vhost和app时
        on_connect      http://127.0.0.1:8085/api/v1/clients http://localhost:8085/api/v1/clients;
        
		# 当客户端关闭连接,或者SRS主动关闭连接时
        on_close        http://127.0.0.1:8085/api/v1/clients http://localhost:8085/api/v1/clients;
       
		# 当客户端发布流时,譬如flash/FMLE方式推流到服务器
        on_publish      http://127.0.0.1:8085/api/v1/streams http://localhost:8085/api/v1/streams;
        
		# 当客户端停止发布流时
        on_unpublish    http://127.0.0.1:8085/api/v1/streams http://localhost:8085/api/v1/streams;
        
		# 当客户端开始播放流时
        on_play         http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions;
        
		# 当客户端停止播放时。备注:停止播放可能不会关闭连接,还能再继续播放。
        on_stop         http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions;
        
		# 当DVR录制关闭一个flv文件时
        on_dvr          http://127.0.0.1:8085/api/v1/dvrs http://localhost:8085/api/v1/dvrs;
		
        # 当HLS生成一个ts文件时
        on_hls          http://127.0.0.1:8085/api/v1/hls http://localhost:8085/api/v1/hls;
		
        # when srs reap a ts file of hls, call this hook,
        on_hls_notify   http://127.0.0.1:8085/api/v1/hls/[app]/[stream]/[ts_url][param];
    }
}

6.2、指定配置文件启动 SRS

docker run -p 1935:1935 -p 1985:1985 -p 8080:8080 \
--name srs \
--network woniu_network \
--ip 172.0.0.35 \
--restart=always \
-v /home/docker/srs4/conf/:/usr/local/srs/conf/ \
-v /home/docker/srs4/objs/:/usr/local/srs/objs/ \
ossrs/srs:v4.0.85 \
./objs/srs -c conf/srs.woniu.conf

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/78038.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!