在redis中添加wvp存活依据,添加推流变化消息

This commit is contained in:
648540858
2021-11-23 14:51:37 +08:00
parent 6282c81bc5
commit b1c92cf4e8
16 changed files with 108 additions and 44 deletions

View File

@@ -0,0 +1,33 @@
package com.genersoft.iot.vmp.web.gb28181;
import com.genersoft.iot.vmp.common.StreamInfo;
import com.genersoft.iot.vmp.service.IMediaService;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@CrossOrigin
@RestController
public class ApiCompatibleController {
private final static Logger logger = LoggerFactory.getLogger(ApiCompatibleController.class);
@Autowired
private IMediaService mediaService;
@GetMapping(value = "/api/v1/stream_info_by_app_and_stream")
@ResponseBody
public WVPResult<StreamInfo> getStreamInfoByAppAndStream(HttpServletRequest request, @RequestParam String app, @RequestParam String stream){
String localAddr = request.getLocalAddr();
StreamInfo streamINfo = mediaService.getStreamInfoByAppAndStreamWithCheck(app, stream, localAddr);
WVPResult<StreamInfo> wvpResult = new WVPResult<>();
wvpResult.setCode(0);
wvpResult.setMsg("success");
wvpResult.setData(streamINfo);
return wvpResult;
}
}

View File

@@ -0,0 +1,93 @@
package com.genersoft.iot.vmp.web.gb28181;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* API兼容设备控制
*/
@CrossOrigin
@RestController
@RequestMapping(value = "/api/v1/control")
public class ApiControlController {
private final static Logger logger = LoggerFactory.getLogger(ApiControlController.class);
@Autowired
private SIPCommander cmder;
@Autowired
private IVideoManagerStorager storager;
/**
* 设备控制 - 云台控制
* @param serial 设备编号
* @param command 控制指令 允许值: left, right, up, down, upleft, upright, downleft, downright, zoomin, zoomout, stop
* @param channel 通道序号
* @param code 通道编号
* @param speed 速度(0~255) 默认值: 129
* @return
*/
@RequestMapping(value = "/ptz")
private JSONObject list(String serial,String command,
@RequestParam(required = false)Integer channel,
@RequestParam(required = false)String code,
@RequestParam(required = false)Integer speed){
if (logger.isDebugEnabled()) {
logger.debug(String.format("模拟接口> 设备云台控制 API调用deviceId%s channelId%s command%d speed%d ",
serial, code, command, speed));
}
Device device = storager.queryVideoDevice(serial);
int leftRight = 0;
int upDown = 0;
int inOut = 0;
switch (command) {
case "left":
leftRight = 1;
break;
case "right":
leftRight = 2;
break;
case "up":
upDown = 1;
break;
case "down":
upDown = 2;
break;
case "upleft":
upDown = 1;
leftRight = 1;
case "upright":
upDown = 1;
leftRight = 2;
break;
case "downleft":
upDown = 2;
leftRight = 1;
break;
case "downright":
upDown = 2;
leftRight = 2;
break;
case "zoomin":
inOut = 2;
break;
case "zoomout":
inOut = 1;
break;
case "stop":
break;
}
// 默认值 50
cmder.ptzCmd(device, code, leftRight, upDown, inOut, speed==0 ? 129 : speed, 50);
return null;
}
}

View File

@@ -0,0 +1,107 @@
package com.genersoft.iot.vmp.web.gb28181;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.conf.SipConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* API兼容系统接口
*/
@Controller
@CrossOrigin
@RequestMapping(value = "/api/v1")
public class ApiController {
private final static Logger logger = LoggerFactory.getLogger(ApiController.class);
@Autowired
private SipConfig sipConfig;
@RequestMapping("/getserverinfo")
private JSONObject getserverinfo(){
JSONObject result = new JSONObject();
result.put("Authorization","ceshi");
result.put("Hardware","");
result.put("InterfaceVersion","2.5.5");
result.put("IsDemo","");
result.put("Hardware","false");
result.put("APIAuth","false");
result.put("RemainDays","永久");
result.put("RunningTime","");
result.put("ServerTime","2020-09-02 1711");
result.put("StartUpTime","2020-09-02 1711");
result.put("Server","");
result.put("SIPSerial", sipConfig.getId());
result.put("SIPRealm", sipConfig.getDomain());
result.put("SIPHost", sipConfig.getIp());
result.put("SIPPort", sipConfig.getPort());
result.put("ChannelCount","1000");
result.put("VersionType","");
result.put("LogoMiniText","");
result.put("LogoText","");
result.put("CopyrightText","");
return result;
}
@RequestMapping(value = "/userinfo")
private JSONObject userinfo(){
// JSONObject result = new JSONObject();
// result.put("ID","ceshi");
// result.put("Hardware","");
// result.put("InterfaceVersion","2.5.5");
// result.put("IsDemo","");
// result.put("Hardware","false");
// result.put("APIAuth","false");
// result.put("RemainDays","永久");
// result.put("RunningTime","");
// result.put("ServerTime","2020-09-02 1711");
// result.put("StartUpTime","2020-09-02 1711");
// result.put("Server","");
// result.put("SIPSerial", sipConfig.getId());
// result.put("SIPRealm", sipConfig.getDomain());
// result.put("SIPHost", sipConfig.getIp());
// result.put("SIPPort", sipConfig.getPort());
// result.put("ChannelCount","1000");
// result.put("VersionType","");
// result.put("LogoMiniText","");
// result.put("LogoText","");
// result.put("CopyrightText","");
return null;
}
/**
* 系统接口 - 登录
* @param username 用户名
* @param password 密码(经过md5加密,32位长度,不带中划线,不区分大小写)
* @return
*/
@RequestMapping(value = "/login")
@ResponseBody
private JSONObject login(String username,String password ){
if (logger.isDebugEnabled()) {
logger.debug(String.format("模拟接口> 登录 API调用username%s password%s ",
username, password));
}
JSONObject result = new JSONObject();
result.put("CookieToken","ynBDDiKMg");
result.put("URLToken","MOBkORkqnrnoVGcKIAHXppgfkNWRdV7utZSkDrI448Q.oxNjAxNTM4NDk3LCJwIjoiZGJjODg5NzliNzVj" +
"Nzc2YmU5MzBjM2JjNjg1ZWFiNGI5ZjhhN2Y0N2RlZjg3NWUyOTJkY2VkYjkwYmEwMTA0NyIsInQiOjE2MDA5MzM2OTcsInUiOiI" +
"4ODlkZDYyM2ViIn0eyJlIj.GciOiJIUzI1NiIsInR5cCI6IkpXVCJ9eyJhb");
result.put("TokenTimeout",604800);
result.put("AuthToken","MOBkORkqnrnoVGcKIAHXppgfkNWRdV7utZSkDrI448Q.oxNjAxNTM4NDk3LCJwIjoiZGJjODg5NzliNzVj" +
"Nzc2YmU5MzBjM2JjNjg1ZWFiNGI5ZjhhN2Y0N2RlZjg3NWUyOTJkY2VkYjkwYmEwMTA0NyIsInQiOjE2MDA5MzM2OTcsInUiOiI" +
"4ODlkZDYyM2ViIn0eyJlIj.GciOiJIUzI1NiIsInR5cCI6IkpXVCJ9eyJhb");
result.put("Token","ynBDDiKMg");
return result;
}
}

View File

@@ -0,0 +1,160 @@
package com.genersoft.iot.vmp.web.gb28181;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* API兼容设备信息
*/
@SuppressWarnings("unchecked")
@CrossOrigin
@RestController
@RequestMapping(value = "/api/v1/device")
public class ApiDeviceController {
private final static Logger logger = LoggerFactory.getLogger(ApiDeviceController.class);
@Autowired
private IVideoManagerStorager storager;
// @Autowired
// private SIPCommander cmder;
// @Autowired
// private DeferredResultHolder resultHolder;
// @Autowired
// private DeviceOffLineDetector offLineDetector;
/**
* 分页获取设备列表 TODO 现在直接返回,尚未实现分页
* @param start
* @param limit
* @param q
* @param online
* @return
*/
@RequestMapping(value = "/list")
public JSONObject list( @RequestParam(required = false)Integer start,
@RequestParam(required = false)Integer limit,
@RequestParam(required = false)String q,
@RequestParam(required = false)Boolean online ){
// if (logger.isDebugEnabled()) {
// logger.debug("查询所有视频设备API调用");
// }
JSONObject result = new JSONObject();
List<Device> devices;
if (start == null || limit ==null) {
devices = storager.queryVideoDeviceList();
result.put("DeviceCount", devices.size());
}else {
PageInfo<Device> deviceList = storager.queryVideoDeviceList(start/limit, limit);
result.put("DeviceCount", deviceList.getTotal());
devices = deviceList.getList();
}
JSONArray deviceJSONList = new JSONArray();
for (Device device : devices) {
JSONObject deviceJsonObject = new JSONObject();
deviceJsonObject.put("ID", device.getDeviceId());
deviceJsonObject.put("Name", device.getName());
deviceJsonObject.put("Type", "GB");
deviceJsonObject.put("ChannelCount", device.getChannelCount());
deviceJsonObject.put("RecvStreamIP", "");
deviceJsonObject.put("CatalogInterval", 3600); // 通道目录抓取周期
deviceJsonObject.put("SubscribeInterval", 0); // 订阅周期(秒), 0 表示后台不周期订阅
deviceJsonObject.put("Online", device.getOnline() == 1);
deviceJsonObject.put("Password", "");
deviceJsonObject.put("MediaTransport", device.getTransport());
deviceJsonObject.put("RemoteIP", device.getIp());
deviceJsonObject.put("RemotePort", device.getPort());
deviceJsonObject.put("LastRegisterAt", "");
deviceJsonObject.put("LastKeepaliveAt", "");
deviceJsonObject.put("UpdatedAt", "");
deviceJsonObject.put("CreatedAt", "");
deviceJSONList.add(deviceJsonObject);
}
result.put("DeviceList",deviceJSONList);
return result;
}
@RequestMapping(value = "/channellist")
public JSONObject channellist( String serial,
@RequestParam(required = false)String channel_type,
@RequestParam(required = false)String dir_serial ,
@RequestParam(required = false)Integer start,
@RequestParam(required = false)Integer limit,
@RequestParam(required = false)String q,
@RequestParam(required = false)Boolean online ){
// if (logger.isDebugEnabled()) {
// logger.debug("查询所有视频设备API调用");
// }
JSONObject result = new JSONObject();
// 查询设备是否存在
Device device = storager.queryVideoDevice(serial);
if (device == null) {
result.put("ChannelCount", 0);
result.put("ChannelList", "[]");
return result;
}
List<DeviceChannel> deviceChannels;
if (start == null || limit ==null) {
deviceChannels = storager.queryChannelsByDeviceId(serial);
result.put("ChannelCount", deviceChannels.size());
}else {
PageInfo<DeviceChannel> pageResult = storager.queryChannelsByDeviceId(serial, null, null, null,start/limit, limit);
result.put("ChannelCount", pageResult.getTotal());
deviceChannels = pageResult.getList();
}
JSONArray channleJSONList = new JSONArray();
for (DeviceChannel deviceChannel : deviceChannels) {
JSONObject deviceJOSNChannel = new JSONObject();
deviceJOSNChannel.put("ID", deviceChannel.getChannelId());
deviceJOSNChannel.put("DeviceID", device.getDeviceId());
deviceJOSNChannel.put("DeviceName", device.getName());
deviceJOSNChannel.put("DeviceOnline", device.getOnline() == 1);
deviceJOSNChannel.put("Channel", 0); // TODO 自定义序号
deviceJOSNChannel.put("Name", deviceChannel.getName());
deviceJOSNChannel.put("Custom", false);
deviceJOSNChannel.put("CustomName", "");
deviceJOSNChannel.put("SubCount", deviceChannel.getSubCount()); // TODO ? 子节点数, SubCount > 0 表示该通道为子目录
deviceJOSNChannel.put("SnapURL", "");
deviceJOSNChannel.put("Manufacturer ", deviceChannel.getManufacture());
deviceJOSNChannel.put("Model", deviceChannel.getModel());
deviceJOSNChannel.put("Owner", deviceChannel.getOwner());
deviceJOSNChannel.put("CivilCode", deviceChannel.getCivilCode());
deviceJOSNChannel.put("Address", deviceChannel.getAddress());
deviceJOSNChannel.put("Parental", deviceChannel.getParental()); // 当为通道设备时, 是否有通道子设备, 1-有,0-没有
deviceJOSNChannel.put("ParentID", deviceChannel.getParentId()); // 直接上级编号
deviceJOSNChannel.put("Secrecy", deviceChannel.getSecrecy());
deviceJOSNChannel.put("RegisterWay", 1); // 注册方式, 缺省为1, 允许值: 1, 2, 3
// 1-IETF RFC3261,
// 2-基于口令的双向认证,
// 3-基于数字证书的双向认证
deviceJOSNChannel.put("Status", deviceChannel.getStatus());
deviceJOSNChannel.put("Longitude", deviceChannel.getLongitude());
deviceJOSNChannel.put("Latitude", deviceChannel.getLatitude());
deviceJOSNChannel.put("PTZType ", deviceChannel.getPTZType()); // 云台类型, 0 - 未知, 1 - 球机, 2 - 半球,
// 3 - 固定枪机, 4 - 遥控枪机
deviceJOSNChannel.put("CustomPTZType", "");
deviceJOSNChannel.put("StreamID", deviceChannel.getStreamId()); // StreamID 直播流ID, 有值表示正在直播
deviceJOSNChannel.put("NumOutputs ", -1); // 直播在线人数
channleJSONList.add(deviceJOSNChannel);
}
result.put("ChannelList", channleJSONList);
return result;
}
}

View File

@@ -0,0 +1,190 @@
package com.genersoft.iot.vmp.web.gb28181;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.common.StreamInfo;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import com.genersoft.iot.vmp.vmanager.gb28181.play.PlayController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
/**
* API兼容实时直播
*/
@SuppressWarnings(value = {"rawtypes", "unchecked"})
@CrossOrigin
@RestController
@RequestMapping(value = "/api/v1/stream")
public class ApiStreamController {
private final static Logger logger = LoggerFactory.getLogger(ApiStreamController.class);
@Autowired
private SIPCommander cmder;
@Autowired
private IVideoManagerStorager storager;
@Autowired
private IRedisCatchStorage redisCatchStorage;
// @Autowired
// private ZLMRESTfulUtils zlmresTfulUtils;
@Autowired
private PlayController playController;
/**
* 实时直播 - 开始直播
* @param serial 设备编号
* @param channel 通道序号 默认值: 1
* @param code 通道编号,通过 /api/v1/device/channellist 获取的 ChannelList.ID, 该参数和 channel 二选一传递即可
* @param cdn TODO 转推 CDN 地址, 形如: [rtmp|rtsp]://xxx, encodeURIComponent
* @param audio TODO 是否开启音频, 默认 开启
* @param transport 流传输模式, 默认 UDP
* @param checkchannelstatus TODO 是否检查通道状态, 默认 false, 表示 拉流前不检查通道状态是否在线
* @param transportmode TODO 当 transport=TCP 时有效, 指示流传输主被动模式, 默认被动
* @param timeout TODO 拉流超时(秒),
* @return
*/
@RequestMapping(value = "/start")
private DeferredResult<JSONObject> start(String serial ,
@RequestParam(required = false)Integer channel ,
@RequestParam(required = false)String code,
@RequestParam(required = false)String cdn,
@RequestParam(required = false)String audio,
@RequestParam(required = false)String transport,
@RequestParam(required = false)String checkchannelstatus ,
@RequestParam(required = false)String transportmode,
@RequestParam(required = false)String timeout
){
DeferredResult<JSONObject> resultDeferredResult = new DeferredResult<JSONObject>();
Device device = storager.queryVideoDevice(serial);
if (device == null ) {
JSONObject result = new JSONObject();
result.put("error","device[ " + serial + " ]未找到");
resultDeferredResult.setResult(result);
}else if (device.getOnline() == 0) {
JSONObject result = new JSONObject();
result.put("error","device[ " + code + " ]offline");
resultDeferredResult.setResult(result);
}
resultDeferredResult.onTimeout(()->{
logger.info("播放等待超时");
JSONObject result = new JSONObject();
result.put("error","timeout");
resultDeferredResult.setResult(result);
// 清理RTP server
});
DeviceChannel deviceChannel = storager.queryChannel(serial, code);
if (deviceChannel == null) {
JSONObject result = new JSONObject();
result.put("error","channel[ " + code + " ]未找到");
resultDeferredResult.setResult(result);
}else if (deviceChannel.getStatus() == 0) {
JSONObject result = new JSONObject();
result.put("error","channel[ " + code + " ]offline");
resultDeferredResult.setResult(result);
}
DeferredResult<ResponseEntity<String>> play = playController.play(serial, code);
play.setResultHandler((Object o)->{
ResponseEntity<String> responseEntity = (ResponseEntity)o;
StreamInfo streamInfo = JSON.parseObject(responseEntity.getBody(), StreamInfo.class);
JSONObject result = new JSONObject();
result.put("StreamID", streamInfo.getStreamId());
result.put("DeviceID", device.getDeviceId());
result.put("ChannelID", code);
result.put("ChannelName", deviceChannel.getName());
result.put("ChannelCustomName", "");
result.put("FLV", streamInfo.getFlv());
result.put("WS_FLV", streamInfo.getWs_flv());
result.put("RTMP", streamInfo.getRtmp());
result.put("HLS", streamInfo.getHls());
result.put("RTSP", streamInfo.getRtsp());
result.put("CDN", "");
result.put("SnapURL", "");
result.put("Transport", device.getTransport());
result.put("StartAt", "");
result.put("Duration", "");
result.put("SourceVideoCodecName", "");
result.put("SourceVideoWidth", "");
result.put("SourceVideoHeight", "");
result.put("SourceVideoFrameRate", "");
result.put("SourceAudioCodecName", "");
result.put("SourceAudioSampleRate", "");
result.put("AudioEnable", "");
result.put("Ondemand", "");
result.put("InBytes", "");
result.put("InBitRate", "");
result.put("OutBytes", "");
result.put("NumOutputs", "");
result.put("CascadeSize", "");
result.put("RelaySize", "");
result.put("ChannelPTZType", 0);
resultDeferredResult.setResult(result);
});
return resultDeferredResult;
}
/**
* 实时直播 - 直播流停止
* @param serial 设备编号
* @param channel 通道序号
* @param code 通道国标编号
* @param check_outputs
* @return
*/
@RequestMapping(value = "/stop")
@ResponseBody
private JSONObject stop(String serial ,
@RequestParam(required = false)Integer channel ,
@RequestParam(required = false)String code,
@RequestParam(required = false)String check_outputs
){
StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(serial, code);
if (streamInfo == null) {
JSONObject result = new JSONObject();
result.put("error","未找到流信息");
return result;
}
cmder.streamByeCmd(serial, code);
redisCatchStorage.stopPlay(streamInfo);
storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
return null;
}
/**
* 实时直播 - 直播流保活
* @param serial 设备编号
* @param channel 通道序号
* @param code 通道国标编号
* @return
*/
@RequestMapping(value = "/touch")
@ResponseBody
private JSONObject touch(String serial ,String t,
@RequestParam(required = false)Integer channel ,
@RequestParam(required = false)String code,
@RequestParam(required = false)String autorestart,
@RequestParam(required = false)String audio,
@RequestParam(required = false)String cdn
){
return null;
}
}

View File

@@ -0,0 +1,25 @@
package com.genersoft.iot.vmp.web.gb28181;
import com.genersoft.iot.vmp.service.IUserService;
import com.genersoft.iot.vmp.storager.dao.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin
@RestController
@RequestMapping(value = "/auth")
public class AuthController {
@Autowired
private IUserService userService;
@RequestMapping("/login")
public String devices(String name, String passwd){
User user = userService.getUser(name, passwd);
if (user != null) {
return "success";
}else {
return "fail";
}
}
}