123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- package com.sckj.camera.service;
- import com.sckj.camera.common.FlowUtils;
- import com.sckj.camera.common.HikCameraUtils;
- import com.sckj.camera.manager.HikCameraManager;
- import com.sckj.camera.model.dto.CameraDTO;
- import com.sckj.camera.model.dto.ResultDTO;
- import com.sckj.camera.model.entity.Camera;
- import com.sckj.camera.model.entity.CameraFile;
- import com.sckj.camera.model.entity.CameraFlow;
- import com.sckj.camera.hik.HCNetTools;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import java.io.File;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- @Service
- public class HikCameraServiceImpl {
- private final Logger logger = LoggerFactory.getLogger(HikCameraServiceImpl.class);
- @Autowired
- private DeviceServiceImpl deviceService;
- @Autowired
- private FlowServiceImpl flowService;
- @Autowired
- private FFmpegServiceImpl ffmpegService;
- @Autowired
- private CameraFileServiceImpl cameraFileService;
- @Autowired
- private HikCameraManager hikCameraManager;
- @Value("${app.filepath}")
- private String rootPath;
- @Value("${rtmp.rtmphost}")
- private String rtmpServer;
- @Value("${rtmp.httphost}")
- private String httpServer;
- public ResultDTO getChannel(CameraDTO cameraDTO) {
- ResultDTO resultDTO = new ResultDTO();
- StringBuilder str = new StringBuilder();
- for (int i = 0;i < cameraDTO.getChannelList().size();i++) {
- if (i == cameraDTO.getChannelList().size() - 1) {
- str.append(cameraDTO.getChannelList().get(i));
- } else {
- str.append(cameraDTO.getChannelList().get(i)).append("&");
- }
- }
- resultDTO.setMsg(str.toString());
- return resultDTO;
- }
- /**
- * 开始推流(rtsp->rtmp)
- * @param cameraDTO 相机信息
- * @return ResultDTO
- */
- public ResultDTO startTranscode(CameraDTO cameraDTO) {
- ResultDTO resultDTO = new ResultDTO();
- Camera camera = deviceService.getById(cameraDTO.getCameraId());
- if(ObjectUtils.isNotEmpty(camera)){
- BeanUtils.copyProperties(camera,cameraDTO);
- }
- cameraDTO.setChannelList(Arrays.asList("1"));
- if (cameraDTO != null) {
- String appName;
- int channelNumber;
- if (cameraDTO.getType() == 0) {
- //摄像机
- appName = cameraDTO.getAccount() + cameraDTO.getIp().replace(".","");
- channelNumber = HikCameraUtils.analyzeChannel(cameraDTO.getChannelList().get(0));
- } else {
- //NVR
- appName = cameraDTO.getAccount() + cameraDTO.getIp().replace(".","") + "to" + cameraDTO.getIpcAddress().replace(".","");
- channelNumber = HikCameraUtils.getIpcChannel(cameraDTO.getChannelList(),cameraDTO.getIpcAddress());
- }
- if (channelNumber == -1) {
- logger.info("实时流-未搜索到ip:" + cameraDTO.getIp() + ", ipc:" + cameraDTO.getIpcAddress() + " 的设备");
- }
- String rtmpUrl = "rtmp://"+ rtmpServer + "/live/" + appName; //rtmp链
- String flvUrl = "http://" + httpServer + "/live/" + appName + ".flv"; //http-flv链
- boolean runFlag = ffmpegService.taskerIsRun(appName);
- CameraFlow cameraFlow = flowService.findByRtmpUrl(rtmpUrl);
- if (cameraFlow != null && !runFlag) {
- flowService.removeById(cameraFlow.getId());
- }
- if (runFlag) {
- //已推流
- if (cameraFlow != null) {
- resultDTO.setTasker(appName);
- resultDTO.setMsg(HikCameraUtils.resultNames[0]);
- resultDTO.setRtmpUrl(rtmpUrl);
- resultDTO.setFlvUrl(flvUrl);
- cameraFlow.setClient(cameraFlow.getClient() + 1);
- flowService.updateById(cameraFlow);
- } else {
- //停止推流
- flowService.stopByTasker(appName);
- resultDTO.setMsg("推流失败");
- return resultDTO;
- }
- } else {
- //未推流
- //String channelNumberStr = "ch"+channelNumber; //旧通道(2012年之前设备)
- channelNumber = 1;
- String channelNumberStr = channelNumber + "02"; //新通道(2012年之后设备,02代表子码流)
- //String rtspName = cameraDTO.getAccount() + ":" + cameraDTO.getPassword() + "@" + cameraDTO.getIp() + ":554/Streaming/Channels/" + channelNumberStr + "/sub/av_stream"); //旧码流(子码流)
- String rtspName = cameraDTO.getAccount() + ":" + cameraDTO.getPassword() + "@" + cameraDTO.getIp() + ":554/Streaming/Channels/" + channelNumberStr + "?transportmode=unicast"; //新码流
- String rtmpName = rtmpServer + "/live/"; //rtmp服务器推流入口
- String tasker= ffmpegService.startTranscoding(appName,rtspName,rtmpName);
- if (StringUtils.isNotEmpty(tasker)) {
- resultDTO.setTasker(tasker);
- resultDTO.setMsg(HikCameraUtils.resultNames[0]);
- resultDTO.setRtmpUrl(rtmpUrl);
- resultDTO.setFlvUrl(flvUrl);
- //添加流记录
- cameraFlow = new CameraFlow();
- cameraFlow.setCameraId(cameraDTO.getCameraId());
- if (cameraDTO.getType() == 0) {
- cameraFlow.setCameraIpc(cameraDTO.getIp());
- } else {
- cameraFlow.setCameraIpc(cameraDTO.getIpcAddress());
- }
- cameraFlow.setTasker(tasker);
- cameraFlow.setFlowType(FlowUtils.FlowType.HIK.ordinal());
- cameraFlow.setRtsp("rtsp://" + rtspName);
- cameraFlow.setRtmp(rtmpUrl);
- cameraFlow.setFlv(flvUrl);
- cameraFlow.setClient(1);
- cameraFlow.setCreateTime(new Date());
- cameraFlow.setUpdateTime(new Date());
- flowService.save(cameraFlow);
- } else {
- resultDTO.setMsg("推流失败");
- return resultDTO;
- }
- }
- return resultDTO;
- } else {
- resultDTO.setMsg("未找到设备");
- return resultDTO;
- }
- }
- /**
- * 回放推流
- * @param cameraDTO 相机信息
- * @return ResultDTO
- */
- public ResultDTO startBackTranscode(CameraDTO cameraDTO) {
- ResultDTO resultDTO = new ResultDTO();
- if (cameraDTO != null) {
- String startTime,endTime;
- DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyyMMdd't'HHmmss'z'");
- if (cameraDTO.getHistoryDTO().getEndTime() != null) {
- startTime = sdf.format(cameraDTO.getHistoryDTO().getStartTime());
- endTime = sdf.format(cameraDTO.getHistoryDTO().getEndTime());
- } else {
- startTime = sdf.format(cameraDTO.getHistoryDTO().getStartTime());
- endTime = null;
- }
- String appName;
- int channelNumber;
- if (cameraDTO.getType() == 0) {
- //摄像机
- appName = "history" + cameraDTO.getAccount() + cameraDTO.getIp().replace(".", "") + "start" + startTime + "end" + endTime;
- channelNumber = HikCameraUtils.analyzeChannel(cameraDTO.getChannelList().get(0));
- } else {
- //NVR
- appName = "history" + cameraDTO.getAccount() + cameraDTO.getIp().replace(".", "") + "to" + cameraDTO.getIpcAddress().replace(".", "") + "start" + startTime + "end" + endTime;
- channelNumber = HikCameraUtils.getIpcChannel(cameraDTO.getChannelList(), cameraDTO.getIpcAddress());
- }
- if (channelNumber == -1) {
- logger.info("历史流-未搜索到ip:" + cameraDTO.getIp() + ", ipc:" + cameraDTO.getIpcAddress() + " 的设备");
- }
- String rtmpUrl = "rtmp://"+ rtmpServer + "/live/" + appName; //rtmp链
- String flvUrl = "http://" + httpServer + "/live/" + appName + ".flv"; //http-flv链
- boolean runFlag = ffmpegService.taskerIsRun(appName);
- CameraFlow cameraFlow = flowService.findByRtmpUrl(rtmpUrl);
- if (cameraFlow != null && !runFlag) {
- flowService.removeById(cameraFlow.getId());
- }
- if (runFlag) {
- //已推流
- if (cameraFlow != null) {
- resultDTO.setTasker(appName);
- resultDTO.setMsg(HikCameraUtils.resultNames[0]);
- resultDTO.setRtmpUrl(rtmpUrl);
- resultDTO.setFlvUrl(flvUrl);
- cameraFlow.setClient(cameraFlow.getClient() + 1);
- flowService.updateById(cameraFlow);
- } else {
- //停止推流
- flowService.stopByTasker(appName);
- resultDTO.setMsg("推流失败");
- return resultDTO;
- }
- } else {
- //未推流
- String channelNumberStr = channelNumber + "01"; //回放流只有主码流
- String rtspName;
- if (endTime != null) {
- rtspName = cameraDTO.getAccount() + ":" + cameraDTO.getPassword() + "@" + cameraDTO.getIp() + ":554/Streaming/tracks/" + channelNumberStr + "?starttime=" + startTime + "&endtime=" + endTime;
- } else {
- rtspName = cameraDTO.getAccount() + ":" + cameraDTO.getPassword() + "@" + cameraDTO.getIp() + ":554/Streaming/tracks/" + channelNumberStr + "?starttime=" + startTime;
- }
- String rtmpName = rtmpServer + "/live/"; //rtmp服务器推流入口
- String tasker= ffmpegService.startTranscoding(appName,rtspName,rtmpName);
- if (StringUtils.isNotEmpty(tasker)) {
- resultDTO.setTasker(tasker);
- resultDTO.setMsg(HikCameraUtils.resultNames[0]);
- resultDTO.setRtmpUrl(rtmpUrl);
- resultDTO.setFlvUrl(flvUrl);
- //添加流记录
- cameraFlow = new CameraFlow();
- cameraFlow.setCameraId(cameraDTO.getCameraId());
- if (cameraDTO.getType() == 0) {
- cameraFlow.setCameraIpc(cameraDTO.getIp());
- } else {
- cameraFlow.setCameraIpc(cameraDTO.getIpcAddress());
- }
- cameraFlow.setTasker(tasker);
- cameraFlow.setFlowType(FlowUtils.FlowType.HIK.ordinal());
- cameraFlow.setRtsp("rtsp://" + rtspName);
- cameraFlow.setRtmp(rtmpUrl);
- cameraFlow.setFlv(flvUrl);
- cameraFlow.setClient(1);
- cameraFlow.setCreateTime(new Date());
- cameraFlow.setUpdateTime(new Date());
- flowService.save(cameraFlow);
- } else {
- resultDTO.setMsg("推流失败");
- return resultDTO;
- }
- }
- return resultDTO;
- } else {
- resultDTO.setMsg("未找到设备");
- return resultDTO;
- }
- }
- /**
- * 抓图
- * @param cameraDTO 相机信息
- */
- public ResultDTO catchPic(CameraDTO cameraDTO) {
- ResultDTO resultDTO = new ResultDTO();
- if (cameraDTO != null) {
- String pathName;
- int channelNumber;
- if (cameraDTO.getType() == 0) {
- //摄像机
- pathName = cameraDTO.getIp().replace(".", "");
- channelNumber = HikCameraUtils.analyzeChannel(cameraDTO.getChannelList().get(0));
- } else {
- //NVR
- resultDTO.setMsg("NVR设备不支持抓图");
- return resultDTO;
- }
- HCNetTools hcTool = cameraDTO.getHcTool();
- DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
- String name = dtf.format(LocalDateTime.now()) + ".jpg";
- String path = "/picture/" + pathName + "/";
- String filePath = rootPath + path + name;
- cameraFileService.pathCreator(rootPath + path);
- boolean success = hcTool.getDVRPic(channelNumber,filePath);
- if (success) {
- long size = new File(filePath).length();
- CameraFile cameraFile = cameraFileService.saveFileInfo(name,path,size);
- resultDTO.setMsg(cameraFile.getId().toString());
- }
- return resultDTO;
- } else {
- resultDTO.setMsg("未找到设备");
- return resultDTO;
- }
- }
- /**
- * 视频回放下载
- * @param cameraDTO
- */
- public ResultDTO downloadBack(CameraDTO cameraDTO) {
- ResultDTO resultDTO = new ResultDTO();
- if (cameraDTO != null) {
- String pathName;
- int channelNumber;
- if (cameraDTO.getType() == 0) {
- //摄像机
- pathName = cameraDTO.getIp().replace(".", "");
- channelNumber = HikCameraUtils.analyzeChannel(cameraDTO.getChannelList().get(0));
- } else {
- //NVR
- pathName = cameraDTO.getIpcAddress().replace(".", "");
- channelNumber = HikCameraUtils.getIpcChannel(cameraDTO.getChannelList(), cameraDTO.getIpcAddress());
- channelNumber = channelNumber + 32; //远程下载的通道号从32开始
- }
- DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
- String name = dtf.format(cameraDTO.getHistoryDTO().getStartTime()) + "TO" + dtf.format(cameraDTO.getHistoryDTO().getEndTime()) + ".mp4";
- String path = "/video/" + pathName + "/";
- cameraFileService.pathCreator(rootPath + path);
- Integer fileId = cameraFileService.findByPathAndName(path,name);
- if (fileId > 0) {
- resultDTO.setMsg(fileId.toString());
- return resultDTO;
- }
- hikCameraManager.downloadVideoAsync(cameraDTO,path,name,channelNumber);
- resultDTO.setMsg("Async");
- return resultDTO;
- } else {
- resultDTO.setMsg("未找到设备");
- return resultDTO;
- }
- }
- /**
- * 获取视频列表
- * @param cameraDTO
- * @return
- */
- public ResultDTO getVideoFileList(CameraDTO cameraDTO) {
- ResultDTO resultDTO = new ResultDTO();
- if (cameraDTO != null) {
- int channelNumber;
- if (cameraDTO.getType() == 0) {
- //摄像机
- channelNumber = HikCameraUtils.analyzeChannel(cameraDTO.getChannelList().get(0));
- } else {
- //NVR
- channelNumber = HikCameraUtils.getIpcChannel(cameraDTO.getChannelList(), cameraDTO.getIpcAddress());
- channelNumber = channelNumber + 32; //远程录像的通道号从33开始
- }
- HCNetTools hcTool = cameraDTO.getHcTool();
- List<HashMap<String, String>> map = hcTool.getVideoFileList(cameraDTO.getHistoryDTO().getStartTime(),cameraDTO.getHistoryDTO().getEndTime(),channelNumber);
- resultDTO.setMsg(map);
- return resultDTO;
- } else {
- resultDTO.setMsg("未找到设备");
- return resultDTO;
- }
- }
- /**
- * 云台控制
- * @param cameraDTO 相机信息
- */
- public ResultDTO playControl(CameraDTO cameraDTO) {
- ResultDTO resultDTO = new ResultDTO();
- if (cameraDTO != null) {
- int channelNumber;
- if (cameraDTO.getType() == 0) {
- //摄像机
- channelNumber = HikCameraUtils.analyzeChannel(cameraDTO.getChannelList().get(0));
- } else {
- //NVR
- resultDTO.setMsg("NVR设备不支持云台控制");
- return resultDTO;
- }
- hikCameraManager.playControlAsync(cameraDTO,channelNumber);
- resultDTO.setMsg("Async");
- return resultDTO;
- } else {
- resultDTO.setMsg("未找到设备");
- return resultDTO;
- }
- }
- /**
- * 停止推流
- * @param id 相机id
- */
- public Boolean stopRtmp(Integer id) {
- if (id == null) {
- return true;
- }
- Camera camera = deviceService.getById(id);
- if (camera != null) {
- String appName = camera.getAccount() + camera.getIp().replace(".","");
- return ffmpegService.stopTranscoding(appName);
- } else {
- return true;
- }
- }
- }
|