⽤
SRS提供了⼀系列http回调,根据客户端连接服务器的不同状态触发该状态下⽤户指定的http请求,⽤户⾃定义服务接受SRS服务传递信息做⼀系列操作,⽐如客户端连接服务器时根据回调数据判断是否允许客户端连接服务器。
SRS服务提供了以下回调:
SRS服务版本:4.0.166
on_connect:当客户端连接到指定的vhost和app时触发
{
钟汉良老婆叫什么名"action": "on_connect",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "st",
"app": "live",
"tcUrl": "rtmp://st/live?key=d2fa801d08e3f90ed1e1670e6e52651a",
"pageUrl": "st/live.html",
"server_id": "vid-werty"
}
on_close:当客户端关闭连接,或者SRS主动关闭连接时触发
{
"action": "on_close",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "st",
"app": "live",
"send_bytes": 10240,
"recv_bytes": 10240,
"server_id": "vid-werty"
}
on_publish:当客户端发布流时,譬如flash/FMLE⽅式推流到服务器触发
{
"action": "on_publish",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "st",
"app": "live",
怎样提高英语成绩"stream": "livestream",
"param":"?token=xxx&salt=yyy",
"server_id": "vid-werty"
}
on_unpublish:当客户端停⽌发布流时触发
"action": "on_unpublish",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "st",
"app": "live",
"stream": "livestream",
"param":"?token=xxx&salt=yyy",
"server_id": "vid-werty"
}
on_play:当客户端开始播放流时触发
{
"action": "on_play",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "st",
"app": "live",
"stream": "livestream",
"param":"?token=xxx&salt=yyy",
"pageUrl": "st/live.html",
"server_id": "vid-werty"
}
on_stop:当客户端停⽌播放时触发。备注:停⽌播放可能不会关闭连接,还能再继续播放。
{
"action": "on_stop",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "st",
"app": "live",
"stream": "livestream",
"param":"?token=xxx&salt=yyy",
"server_id": "vid-werty"
}
on_dvr:当DVR录制关闭⼀个flv⽂件时触发
{
"action": "on_dvr",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "st",
"app": "live",
"stream": "livestream",
"param":"?token=xxx&salt=yyy",
"cwd": "/usr/local/srs",
"file": "./objs/nginx/html/live/livestream.1420254068776.flv",
"server_id": "vid-werty"
}
on_hls:当SRS获取HLS的ts⽂件时触发
"action": "on_hls",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "st",马丽娟个人资料简介
"app": "live",
"stream": "livestream",
"param":"?token=xxx&salt=yyy",
"duration": 9.36, // in seconds
"cwd": "/usr/local/srs",
"file": "./objs/nginx/html/live/livestream/2015-04-23/01/476584165.ts",
"url": "live/livestream/2015-04-23/01/476584165.ts",
"m3u8": "./objs/nginx/html/live/livestream/live.m3u8",
"m3u8_url": "live/livestream/live.m3u8",
"seq_no": 100, "server_id": "vid-werty"
}
on_hls_notify:当SRS获取HLS的ts⽂件时触发,⽤于将⽂件推送到CDN⽹络,通过从CDN⽹络获取ts⽂件。该请求是⼀个GET请求,请求地址为格式:
[server_id]服务器ID替换
[app]应⽤名称替换
[stream]流名称替换
[ts_url]url替换
[param]参数替换
马思纯白敬亭电视剧事件:发⽣该事件时,即回调指定的HTTP地址。
HTTP地址:可以⽀持多个,以空格分隔,SRS会依次回调这些接⼝。
数据:SRS将数据POST到HTTP接⼝。最后⼀个回调on_hls_notify为GET请求,在URL⾥拼接请求参数。
返回值:SRS要求HTTP服务器返回HTTP200并且response内容为整数错误码(0表⽰成功),其他错误码会断开客户端连接。
以上回调传递信息由官⽅GitHub⾥的提供,与实际获取到的信息⾥部分信息可能有区别,可在⾃定义服务接⼝⾥获取打印查看,开启回调并指定请求URL参考
回调请求格式,多个接⼝⽤空格隔开,分号结尾:
on_connect http(https)://ip:port/api http(https)://ip:port/api;
测试demo
服务器系统:CentOS7.4
VM15Pro虚拟机⽹络模式:桥接
服务器IP地址:192.168.5.105
老公鸡怎么吃好吃服务器端⼝和服务:
本机IP地址:192.168.5.104
⼀、开启SRS服务,进⼊安装⽬录
1.1、查看配置⽂件,设置回调函数
# main config for srs.
# @f for detail config.
listen 1935; //RTMP默认监听1935 TCP端⼝
max_connections 1000; //最⼤连接数
#srs_log_tank file;
#srs_log_file ./objs/srs.log;
daemon on; //开启后台模式
http_api {
enabled on; //⽀持外部程序管理SRS服务器,⽀持跨域请求,请求和响应数据只⽀持JSON
listen 1985; //
}
http_server {
enabled on; //开启SRS服务可视化界⾯查看系统信息和播放器推流器
listen 8080;
dir ./objs/nginx/html;
}
rtc_server {
enabled on; //开启RTC服务器
listen 8000; //RTC默认监听8000端⼝为UDP端⼝
candidate $CANDIDATE; //服务器提供服务的IP地址。开启RTC服务,这⾥⼀定不能错,云服务器这⾥可能读取不到可访问的外⽹IP或者错误IP地址,需要⼿动指定}
vhost __defaultVhost__ {
hls {
enabled on;
}
http_remux {
enabled on; //RTMP流转封装为http flv流分发
mount [vhost]/[app]/[stream].flv;
}
孙杨资料rtc {
enabled on;
#rtmp_to_rtc off; //RTMP推流转RTC拉流
#rtc_to_rtmp off; //RTC推流转RTMP拉流
}
http_hooks {
enabled on;
on_connect 192.168.5.102:400/connect;
on_close 192.168.5.102:400/close;
on_publish 192.168.5.102:400/publish;
on_unpublish 192.168.5.102:400/unpublish;
on_play 192.168.5.102:400/play;
on_stop 192.168.5.102:400/stop;
}
}
SRS4.0.166⽆法设置rtmp_to_rtc,否则启动服务报错⾮法,但可以RTMP推流RTC拉流。后续版本设置此项⽆报错,SRS4.0.X版本⽬
前属于开发版本
1.2、开启服务指定f配置启动
cd /usr/local/srs/SRS4.0/trunk //进⼊安装⽬录
./objs/srs -c f //启动服务指定配置⽂件为f
./etc/init.d/srs status //查看运⾏状态
./objs/srs -v //查看安装版本号
⼆、创建springboot项⽬,编写接⼝
配置类
fig;
import t.annotation.Configuration;
import org.springframework.fig.annotation.ResourceHandlerRegistry; import org.springframework.fig.annotation.ViewControllerRegistry; import org.springframework.fig.annotation.WebMvcConfigurer;
/*
* 配置类
* @author jiang
* @date 2021.10.17
* */
@Configuration
public class WebConfig implements WebMvcConfigurer {
//解决 No mapping for GET 访问静态资源
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/");
}
//http(https)://ip:port/访问本地址
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("client");
}
}
token⽣成与校验类
ample.live.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.ptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
stant.Constant;
import java.io.UnsupportedEncodingException;
import java.util.Date;
/*
* token⽣成、验证⼯具类
* @author jiang
* @date 2021.10.17
* */
public class TokenUtils {
//有效期 1000为1秒
private static final long EXPIRE_TIME = 60*60*1000;
//密钥
private static final String SECRET = Constant.SECRET;
/*
* 校验token是否正确
发布评论