HwVideoApi.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.zhiqiyun.open.router.apis;
  2. import cn.hutool.http.HttpRequest;
  3. import cn.hutool.json.JSONUtil;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.dliyun.oap.framework.annotation.ServiceMethod;
  7. import com.dliyun.oap.framework.annotation.ServiceMethodBean;
  8. import com.dliyun.oap.framework.response.OapResponse;
  9. import com.google.common.collect.Maps;
  10. import com.zhiqiyun.open.camera.config.CameraConfig;
  11. import com.zhiqiyun.open.core.models.hwVideo.DeviceList;
  12. import com.zhiqiyun.open.core.video.VideoDockingService;
  13. import com.zhiqiyun.open.core.video.VideoDockingUrbanService;
  14. import com.zhiqiyun.open.router.request.hwVideo.LoadVideoPageRequest;
  15. import com.zhiqiyun.open.router.request.hwVideo.VideoRtsUrlRequest;
  16. import com.zhiqiyun.open.router.request.hwVideo.VideoRtsUrlResponse;
  17. import com.zhiqiyun.open.router.request.hwVideo.VideoStopPushRequest;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  22. import javax.validation.Valid;
  23. import java.net.URLEncoder;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * @author NINGMEI
  28. */
  29. @Slf4j
  30. @ServiceMethodBean
  31. public class HwVideoApi {
  32. @Autowired
  33. private VideoDockingService videoDockingService;
  34. @Autowired
  35. private VideoDockingUrbanService videoDockingUrbanService;
  36. @Autowired
  37. private ThreadPoolTaskExecutor taskExecutor;
  38. @ServiceMethod(method = "hw.video.find", title = "查询子设备列表")
  39. public OapResponse loadDeviceList(LoadVideoPageRequest request) throws Exception {
  40. try {
  41. Map<String, String> params = Maps.newHashMap();
  42. params.put("fromIndex", String.valueOf((request.getPage() - 1) * request.getLimit() + 1));
  43. params.put("toIndex", String.valueOf(request.getPage() * request.getLimit()));
  44. JSONObject jsonObject = request.getLoadType().compareTo(1) == 0 ? videoDockingService.loadDeviceList(params)
  45. : videoDockingUrbanService.loadDeviceList(params);
  46. // log.info("设备信息:{}", jsonObject.toJSONString());
  47. JSONObject cameraBriefExInfos = jsonObject.getJSONObject("cameraBriefExInfos");
  48. int total = cameraBriefExInfos.getIntValue("total");
  49. JSONObject cameraBriefInfoExList = cameraBriefExInfos.getJSONObject("cameraBriefInfoExList");
  50. List<DeviceList> cameraBriefInfoExes = cameraBriefInfoExList.getJSONArray("cameraBriefInfoExes").toJavaList(DeviceList.class);
  51. Page<DeviceList> resultData = new Page<>(request.getPage(), request.getLimit(), total);
  52. resultData.setRecords(cameraBriefInfoExes);
  53. return OapResponse.success().setBody(resultData);
  54. } catch (Exception ex) {
  55. log.error(ex.getMessage(), ex);
  56. }
  57. return OapResponse.success();
  58. }
  59. private final static String SRS_API = "https://10.35.5.111:8090";
  60. @ServiceMethod(method = "hw.video.url.find", title = "获取视频URL信息")
  61. public OapResponse loadVideoRtspUrl(@Valid VideoRtsUrlRequest request) throws Exception {
  62. JSONObject jsonObject = request.getLoadType().compareTo(1) == 0 ? videoDockingService.loadVideoRtspUrl(request.getCameraCode())
  63. : videoDockingUrbanService.loadVideoRtspUrl(request.getCameraCode());
  64. String rtspUri = jsonObject.getString("rtspURL");
  65. log.info("获取到监控rtspURI:{}", rtspUri);
  66. rtspUri = CameraConfig.replaceUri(rtspUri);
  67. log.info("获取到监控rtspURI:{}", rtspUri);
  68. if (StringUtils.isBlank(rtspUri)) {
  69. return OapResponse.fail("ERROR", "视频流地址获取失败");
  70. }
  71. String rtspId = request.getCameraCode().split("#")[0];
  72. String body = HttpRequest.get(SRS_API + "/api/v1/srs/push")
  73. .form("rtspId", rtspId)
  74. .form("ip", request.getIp())
  75. .form("rtspUri", URLEncoder.encode(rtspUri, "utf-8"))
  76. .execute().body();
  77. log.info("推流返回信息:{}", body);
  78. JSONObject obJson = JSONObject.parseObject(body);
  79. if (0 != obJson.getIntValue("code")) {
  80. return OapResponse.fail("ERROR", "视频流地址获取失败");
  81. }
  82. VideoRtsUrlResponse response = obJson.getJSONObject("data").toJavaObject(VideoRtsUrlResponse.class);
  83. // RtspTransfer.createRtspId(rtspUri, rtspId);
  84. // response.setRtspId(rtspId);
  85. // log.info("RtspTransfer.STREAM_MAP:{}", RtspTransfer.STREAM_MAP.get(rtspId));
  86. // if (StringUtils.isNotBlank(RtspTransfer.STREAM_MAP.get(rtspId))) {
  87. // response.setRtspUri(RtspTransfer.STREAM_MAP.get(rtspId));
  88. // } else {
  89. // rtspUri = String.format(RtspPushSrs.SRS_PUSH_ADDRESS, SecureUtil.md5(rtspId));
  90. // log.info("重新进行流推送:{}", rtspUri);
  91. // taskExecutor.execute(() -> {
  92. // try {
  93. // //停顿1秒在进行推流,已缓解频繁开启或关闭推流造成的问题
  94. // Thread.sleep(1000);
  95. // RtspTransfer.startPushRtmp(rtspId);
  96. // } catch (Exception e) {
  97. // log.info("============停止推流=======================");
  98. // RtspTransfer.removeRtsp(rtspId);
  99. // }
  100. // });
  101. // RtspTransfer.STREAM_MAP.put(rtspId, rtspUri);
  102. // response.setRtspUri(rtspUri);
  103. // }
  104. return OapResponse.success().setBody(response);
  105. }
  106. // @ServiceMethod(method = "hw.video.url.find", title = "获取视频URL信息")
  107. // public OapResponse loadVideoRtspUrl(@Valid VideoRtsUrlRequest request) throws Exception {
  108. // VideoRtsUrlResponse response = new VideoRtsUrlResponse();
  109. // JSONObject jsonObject = request.getLoadType().compareTo(1) == 0 ? videoDockingService.loadVideoRtspUrl(request.getCameraCode())
  110. // : videoDockingUrbanService.loadVideoRtspUrl(request.getCameraCode());
  111. // String rtspUri = jsonObject.getString("rtspURL");
  112. // log.info("获取到监控rtspURI:{}", rtspUri);
  113. // rtspUri = CameraConfig.replaceUri(rtspUri);
  114. // log.info("获取到监控rtspURI:{}", rtspUri);
  115. // if (StringUtils.isBlank(rtspUri)) return OapResponse.fail("ERROR", "视频流地址获取失败");
  116. // String rtspId = request.getCameraCode().split("#")[0];
  117. // RtspTransfer.createRtspId(rtspUri, rtspId);
  118. // response.setRtspId(rtspId);
  119. // log.info("RtspTransfer.STREAM_MAP:{}", RtspTransfer.STREAM_MAP.get(rtspId));
  120. // if (StringUtils.isNotBlank(RtspTransfer.STREAM_MAP.get(rtspId))) {
  121. // response.setRtspUri(RtspTransfer.STREAM_MAP.get(rtspId));
  122. // } else {
  123. // rtspUri = String.format(RtspPushSrs.SRS_PUSH_ADDRESS, SecureUtil.md5(rtspId));
  124. // log.info("重新进行流推送:{}", rtspUri);
  125. // taskExecutor.execute(() -> {
  126. // try {
  127. // //停顿1秒在进行推流,已缓解频繁开启或关闭推流造成的问题
  128. // Thread.sleep(1000);
  129. // RtspTransfer.startPushRtmp(rtspId);
  130. // } catch (Exception e) {
  131. // log.info("============停止推流=======================");
  132. // RtspTransfer.removeRtsp(rtspId);
  133. // }
  134. // });
  135. // RtspTransfer.STREAM_MAP.put(rtspId, rtspUri);
  136. // response.setRtspUri(rtspUri);
  137. // }
  138. // return OapResponse.success().setBody(response);
  139. // }
  140. @ServiceMethod(method = "hw.video.live.stop", title = "停止视频推流工作")
  141. public OapResponse videoStopPush(VideoStopPushRequest request) throws Exception {
  142. log.info("============停止推流=======================:{}", JSONUtil.toJsonStr(request));
  143. String body = HttpRequest.get(SRS_API + "/api/v1/srs/stop")
  144. .form("rtspId", request.getRtspId())
  145. .form("ip", request.getIp())
  146. .execute().body();
  147. log.info("============停止推流=======================:{}", body);
  148. // RtspTransfer.removeRtsp(request.getRtspId());
  149. return OapResponse.success();
  150. }
  151. }