部分代码显示
package com.xdnnaovideo.videoservice.listenermessage;
import com.google.gson.Gson;
import com.xdnnaovideo.videoservice.entity.AddAndUpdateRtspVo;
import com.xdnnaovideo.videoservice.entity.AjaxResult;
import com.xdnnaovideo.videoservice.rtsp.MediaConstant;
import com.xdnnaovideo.videoservice.rtsp.MediaStart;
import org.apache.commons.lang3.StringUtils;
import org.bytedeco.javacv.FrameGrabber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.TextMessage;
@Component
public class ActivemqListener {
@Autowired
MediaStart mediaStart;
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${webserviceluwairstp}")
String webserviceluwairstp;
@JmsListener(destination = "${webserviceluwairstp}")
public void webserviceluwairstp(TextMessage message) {
try {
AddAndUpdateRtspVo addAndUpdateRtspVo = new Gson().fromJson(message.getText(), AddAndUpdateRtspVo.class);
String type = addAndUpdateRtspVo.getType();
if("start".equals(type)){
convertexsetting(addAndUpdateRtspVo);
logger.info("设置数据rstp进来:" + addAndUpdateRtspVo);
}else if("stop".equals(type)){
stopconvertexsetting(addAndUpdateRtspVo);
logger.info("设置数据rstp进来:" + addAndUpdateRtspVo);
}
message.acknowledge();
} catch (Exception error) {
logger.error("发生错误:{}", error.getMessage());
}
}
private void convertexsetting(AddAndUpdateRtspVo addAndUpdateRtspVo) {
String linkId = addAndUpdateRtspVo.getLinkIdDeviceInfo();
String rtspPath = addAndUpdateRtspVo.getRtspPath();
boolean haveSameFFmpegFrameGrabberCache = MediaConstant.isHaveSameFFmpegFrameGrabberCache(linkId.trim(), rtspPath.trim());
boolean haveSamertspPath = MediaConstant.isHaveSamertspPath(rtspPath.trim());
String linkIdCopy = MediaConstant.getLinkId(rtspPath.trim());
// 当要修改的rtspPath地址不是在存在准备修改的linkId上,而是存在其他linkId上,则不允许修改,这样做是避免同时解析两个相同的rtsp地址导致流错乱
if (haveSameFFmpegFrameGrabberCache && haveSamertspPath) {
logger.info("输入参数存在有误,要更改的rtspPath地址已经存在于其他链路上,不允许在此链路修改,请移步先将链路:[" + linkIdCopy + "]的rtsp地址更改后再来更改");
return;
}
MediaConstant.FFmpegFrameGrabberCache fFmpegFrameGrabberCache1 = MediaConstant.getFFmpegFrameGrabberCache(linkId.trim());
String message = "";
// 如果为空,则说明没有添加过该链路的FFmpegFrameGrabberCache
if (fFmpegFrameGrabberCache1 == null) {
MediaConstant.FFmpegFrameGrabberCache fFmpegFrameGrabberCache =
new MediaConstant.FFmpegFrameGrabberCache()
.setLinkId(linkId.trim())
.setRtspPath(rtspPath.trim());
MediaConstant.fFmpegFrameGrabberCaches.put(linkId.trim(), fFmpegFrameGrabberCache);
message = "添加" + linkId + ":rtsp地址成功";
} else { // 如果不为空,则要将该链路对应的数据更改为初始化状态,添加新的信息
try {
fFmpegFrameGrabberCache1.getGrabber().stop();
} catch (FrameGrabber.Exception e) {
logger.error("FrameGrabber stop exception: ", e);
}
fFmpegFrameGrabberCache1.setRtspPath(rtspPath)
.setLinkId(linkId)
.setGrabber(null)
.setIsStart(false);
message = "修改" + linkId + ":rtsp地址成功";
}
//启动当前链路线程
mediaStart.addLinkThreadPool(linkId.trim());
}
private void stopconvertexsetting(AddAndUpdateRtspVo addAndUpdateRtspVo) {
String linkId = addAndUpdateRtspVo.getLinkIdDeviceInfo();
String rtspPath = addAndUpdateRtspVo.getRtspPath();
MediaConstant.FFmpegFrameGrabberCache fFmpegFrameGrabberCache1 = MediaConstant.getFFmpegFrameGrabberCache(linkId.trim());
// 如果为空,则说明没有添加过该链路的FFmpegFrameGrabberCache
if (fFmpegFrameGrabberCache1 != null) {
try {
fFmpegFrameGrabberCache1.getGrabber().stop();
} catch (FrameGrabber.Exception e) {
logger.error("FrameGrabber stop exception: ", e);
}
fFmpegFrameGrabberCache1.setRtspPath(rtspPath)
.setLinkId(linkId)
.setGrabber(null)
.setIsStart(false);
}
MediaConstant.removeisHaveSameFFmpegFrameGrabberCache(linkId.trim());
MediaConstant.removeisHaveSameFFmpegFrameGrabberCache(linkId.trim());
}
}