package com.zhiqiyun.open.router.apis; import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.dliyun.oap.framework.annotation.ServiceMethod; import com.dliyun.oap.framework.annotation.ServiceMethodBean; import com.dliyun.oap.framework.response.OapResponse; import com.google.common.collect.Maps; import com.zhiqiyun.open.camera.config.CameraConfig; import com.zhiqiyun.open.core.models.hwVideo.DeviceList; import com.zhiqiyun.open.core.video.VideoDockingService; import com.zhiqiyun.open.core.video.VideoDockingUrbanService; import com.zhiqiyun.open.router.request.hwVideo.LoadVideoPageRequest; import com.zhiqiyun.open.router.request.hwVideo.VideoRtsUrlRequest; import com.zhiqiyun.open.router.request.hwVideo.VideoRtsUrlResponse; import com.zhiqiyun.open.router.request.hwVideo.VideoStopPushRequest; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import javax.validation.Valid; import java.net.URLEncoder; import java.util.List; import java.util.Map; /** * @author NINGMEI */ @Slf4j @ServiceMethodBean public class HwVideoApi { @Autowired private VideoDockingService videoDockingService; @Autowired private VideoDockingUrbanService videoDockingUrbanService; @Autowired private ThreadPoolTaskExecutor taskExecutor; @ServiceMethod(method = "hw.video.find", title = "查询子设备列表") public OapResponse loadDeviceList(LoadVideoPageRequest request) throws Exception { try { Map params = Maps.newHashMap(); params.put("fromIndex", String.valueOf((request.getPage() - 1) * request.getLimit() + 1)); params.put("toIndex", String.valueOf(request.getPage() * request.getLimit())); JSONObject jsonObject = request.getLoadType().compareTo(1) == 0 ? videoDockingService.loadDeviceList(params) : videoDockingUrbanService.loadDeviceList(params); // log.info("设备信息:{}", jsonObject.toJSONString()); JSONObject cameraBriefExInfos = jsonObject.getJSONObject("cameraBriefExInfos"); int total = cameraBriefExInfos.getIntValue("total"); JSONObject cameraBriefInfoExList = cameraBriefExInfos.getJSONObject("cameraBriefInfoExList"); List cameraBriefInfoExes = cameraBriefInfoExList.getJSONArray("cameraBriefInfoExes").toJavaList(DeviceList.class); Page resultData = new Page<>(request.getPage(), request.getLimit(), total); resultData.setRecords(cameraBriefInfoExes); return OapResponse.success().setBody(resultData); } catch (Exception ex) { log.error(ex.getMessage(), ex); } return OapResponse.success(); } private final static String SRS_API = "https://10.35.5.111:8090"; @ServiceMethod(method = "hw.video.url.find", title = "获取视频URL信息") public OapResponse loadVideoRtspUrl(@Valid VideoRtsUrlRequest request) throws Exception { JSONObject jsonObject = request.getLoadType().compareTo(1) == 0 ? videoDockingService.loadVideoRtspUrl(request.getCameraCode()) : videoDockingUrbanService.loadVideoRtspUrl(request.getCameraCode()); String rtspUri = jsonObject.getString("rtspURL"); log.info("获取到监控rtspURI:{}", rtspUri); rtspUri = CameraConfig.replaceUri(rtspUri); log.info("获取到监控rtspURI:{}", rtspUri); if (StringUtils.isBlank(rtspUri)) { return OapResponse.fail("ERROR", "视频流地址获取失败"); } String rtspId = request.getCameraCode().split("#")[0]; String body = HttpRequest.get(SRS_API + "/api/v1/srs/push") .form("rtspId", rtspId) .form("ip", request.getIp()) .form("rtspUri", URLEncoder.encode(rtspUri, "utf-8")) .execute().body(); log.info("推流返回信息:{}", body); JSONObject obJson = JSONObject.parseObject(body); if (0 != obJson.getIntValue("code")) { return OapResponse.fail("ERROR", "视频流地址获取失败"); } VideoRtsUrlResponse response = obJson.getJSONObject("data").toJavaObject(VideoRtsUrlResponse.class); // RtspTransfer.createRtspId(rtspUri, rtspId); // response.setRtspId(rtspId); // log.info("RtspTransfer.STREAM_MAP:{}", RtspTransfer.STREAM_MAP.get(rtspId)); // if (StringUtils.isNotBlank(RtspTransfer.STREAM_MAP.get(rtspId))) { // response.setRtspUri(RtspTransfer.STREAM_MAP.get(rtspId)); // } else { // rtspUri = String.format(RtspPushSrs.SRS_PUSH_ADDRESS, SecureUtil.md5(rtspId)); // log.info("重新进行流推送:{}", rtspUri); // taskExecutor.execute(() -> { // try { // //停顿1秒在进行推流,已缓解频繁开启或关闭推流造成的问题 // Thread.sleep(1000); // RtspTransfer.startPushRtmp(rtspId); // } catch (Exception e) { // log.info("============停止推流======================="); // RtspTransfer.removeRtsp(rtspId); // } // }); // RtspTransfer.STREAM_MAP.put(rtspId, rtspUri); // response.setRtspUri(rtspUri); // } return OapResponse.success().setBody(response); } // @ServiceMethod(method = "hw.video.url.find", title = "获取视频URL信息") // public OapResponse loadVideoRtspUrl(@Valid VideoRtsUrlRequest request) throws Exception { // VideoRtsUrlResponse response = new VideoRtsUrlResponse(); // JSONObject jsonObject = request.getLoadType().compareTo(1) == 0 ? videoDockingService.loadVideoRtspUrl(request.getCameraCode()) // : videoDockingUrbanService.loadVideoRtspUrl(request.getCameraCode()); // String rtspUri = jsonObject.getString("rtspURL"); // log.info("获取到监控rtspURI:{}", rtspUri); // rtspUri = CameraConfig.replaceUri(rtspUri); // log.info("获取到监控rtspURI:{}", rtspUri); // if (StringUtils.isBlank(rtspUri)) return OapResponse.fail("ERROR", "视频流地址获取失败"); // String rtspId = request.getCameraCode().split("#")[0]; // RtspTransfer.createRtspId(rtspUri, rtspId); // response.setRtspId(rtspId); // log.info("RtspTransfer.STREAM_MAP:{}", RtspTransfer.STREAM_MAP.get(rtspId)); // if (StringUtils.isNotBlank(RtspTransfer.STREAM_MAP.get(rtspId))) { // response.setRtspUri(RtspTransfer.STREAM_MAP.get(rtspId)); // } else { // rtspUri = String.format(RtspPushSrs.SRS_PUSH_ADDRESS, SecureUtil.md5(rtspId)); // log.info("重新进行流推送:{}", rtspUri); // taskExecutor.execute(() -> { // try { // //停顿1秒在进行推流,已缓解频繁开启或关闭推流造成的问题 // Thread.sleep(1000); // RtspTransfer.startPushRtmp(rtspId); // } catch (Exception e) { // log.info("============停止推流======================="); // RtspTransfer.removeRtsp(rtspId); // } // }); // RtspTransfer.STREAM_MAP.put(rtspId, rtspUri); // response.setRtspUri(rtspUri); // } // return OapResponse.success().setBody(response); // } @ServiceMethod(method = "hw.video.live.stop", title = "停止视频推流工作") public OapResponse videoStopPush(VideoStopPushRequest request) throws Exception { log.info("============停止推流=======================:{}", JSONUtil.toJsonStr(request)); String body = HttpRequest.get(SRS_API + "/api/v1/srs/stop") .form("rtspId", request.getRtspId()) .form("ip", request.getIp()) .execute().body(); log.info("============停止推流=======================:{}", body); // RtspTransfer.removeRtsp(request.getRtspId()); return OapResponse.success(); } }