PlayServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.genersoft.iot.vmp.common.StreamInfo;
  6. import com.genersoft.iot.vmp.conf.UserSetup;
  7. import com.genersoft.iot.vmp.gb28181.bean.Device;
  8. import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  9. import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
  10. import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
  11. import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
  12. import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
  13. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  14. import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe;
  15. import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
  16. import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
  17. import com.genersoft.iot.vmp.service.IMediaServerService;
  18. import com.genersoft.iot.vmp.service.bean.SSRCInfo;
  19. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  20. import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
  21. import com.genersoft.iot.vmp.utils.redis.RedisUtil;
  22. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  23. import com.genersoft.iot.vmp.vmanager.gb28181.play.bean.PlayResult;
  24. import com.genersoft.iot.vmp.service.IMediaService;
  25. import com.genersoft.iot.vmp.service.IPlayService;
  26. import gov.nist.javax.sip.stack.SIPDialog;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.http.HttpStatus;
  31. import org.springframework.http.ResponseEntity;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.util.ResourceUtils;
  34. import org.springframework.web.context.request.async.DeferredResult;
  35. import java.io.FileNotFoundException;
  36. import java.util.Objects;
  37. import java.util.UUID;
  38. @SuppressWarnings(value = {"rawtypes", "unchecked"})
  39. @Service
  40. public class PlayServiceImpl implements IPlayService {
  41. private final static Logger logger = LoggerFactory.getLogger(PlayServiceImpl.class);
  42. @Autowired
  43. private IVideoManagerStorager storager;
  44. @Autowired
  45. private SIPCommander cmder;
  46. @Autowired
  47. private IRedisCatchStorage redisCatchStorage;
  48. @Autowired
  49. private RedisUtil redis;
  50. @Autowired
  51. private DeferredResultHolder resultHolder;
  52. @Autowired
  53. private ZLMRESTfulUtils zlmresTfulUtils;
  54. @Autowired
  55. private IMediaService mediaService;
  56. @Autowired
  57. private IMediaServerService mediaServerService;
  58. @Autowired
  59. private VideoStreamSessionManager streamSession;
  60. @Autowired
  61. private UserSetup userSetup;
  62. @Override
  63. public PlayResult play(MediaServerItem mediaServerItem, String deviceId, String channelId, ZLMHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent) {
  64. PlayResult playResult = new PlayResult();
  65. RequestMessage msg = new RequestMessage();
  66. String key = DeferredResultHolder.CALLBACK_CMD_PLAY + deviceId + channelId;
  67. msg.setKey(key);
  68. String uuid = UUID.randomUUID().toString();
  69. msg.setId(uuid);
  70. playResult.setUuid(uuid);
  71. DeferredResult<ResponseEntity<String>> result = new DeferredResult<>(userSetup.getPlayTimeout());
  72. playResult.setResult(result);
  73. // 录像查询以channelId作为deviceId查询
  74. resultHolder.put(key, uuid, result);
  75. if (mediaServerItem == null) {
  76. WVPResult wvpResult = new WVPResult();
  77. wvpResult.setCode(-1);
  78. wvpResult.setMsg("未找到可用的zlm");
  79. msg.setData(wvpResult);
  80. resultHolder.invokeResult(msg);
  81. return playResult;
  82. }
  83. Device device = storager.queryVideoDevice(deviceId);
  84. StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
  85. playResult.setDevice(device);
  86. // 超时处理
  87. result.onTimeout(()->{
  88. logger.warn(String.format("设备点播超时,deviceId:%s ,channelId:%s", deviceId, channelId));
  89. WVPResult wvpResult = new WVPResult();
  90. wvpResult.setCode(-1);
  91. SIPDialog dialog = streamSession.getDialog(deviceId, channelId);
  92. if (dialog != null) {
  93. wvpResult.setMsg("收流超时,请稍候重试");
  94. }else {
  95. wvpResult.setMsg("点播超时,请稍候重试");
  96. }
  97. msg.setData(wvpResult);
  98. // 点播超时回复BYE
  99. cmder.streamByeCmd(device.getDeviceId(), channelId);
  100. // 释放rtpserver
  101. mediaServerService.closeRTPServer(playResult.getDevice(), channelId);
  102. // 回复之前所有的点播请求
  103. resultHolder.invokeAllResult(msg);
  104. });
  105. result.onCompletion(()->{
  106. // 点播结束时调用截图接口
  107. try {
  108. String classPath = ResourceUtils.getURL("classpath:").getPath();
  109. // System.out.println(classPath);
  110. // 兼容打包为jar的class路径
  111. if(classPath.contains("jar")) {
  112. classPath = classPath.substring(0, classPath.lastIndexOf("."));
  113. classPath = classPath.substring(0, classPath.lastIndexOf("/") + 1);
  114. }
  115. if (classPath.startsWith("file:")) {
  116. classPath = classPath.substring(classPath.indexOf(":") + 1);
  117. }
  118. String path = classPath + "static/static/snap/";
  119. // 兼容Windows系统路径(去除前面的“/”)
  120. if(System.getProperty("os.name").contains("indows")) {
  121. path = path.substring(1);
  122. }
  123. String fileName = deviceId + "_" + channelId + ".jpg";
  124. ResponseEntity responseEntity = (ResponseEntity)result.getResult();
  125. if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
  126. WVPResult wvpResult = (WVPResult)responseEntity.getBody();
  127. if (Objects.requireNonNull(wvpResult).getCode() == 0) {
  128. StreamInfo streamInfoForSuccess = (StreamInfo)wvpResult.getData();
  129. MediaServerItem mediaInfo = mediaServerService.getOne(streamInfoForSuccess.getMediaServerId());
  130. String streamUrl = streamInfoForSuccess.getFmp4();
  131. // 请求截图
  132. zlmresTfulUtils.getSnap(mediaInfo, streamUrl, 15, 1, path, fileName);
  133. }
  134. }
  135. } catch (FileNotFoundException e) {
  136. e.printStackTrace();
  137. }
  138. });
  139. if (streamInfo == null) {
  140. SSRCInfo ssrcInfo;
  141. String streamId = null;
  142. if (mediaServerItem.isRtpEnable()) {
  143. streamId = String.format("%s_%s", device.getDeviceId(), channelId);
  144. }
  145. ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId);
  146. // 发送点播消息
  147. cmder.playStreamCmd(mediaServerItem, ssrcInfo, device, channelId, (MediaServerItem mediaServerItemInUse, JSONObject response) -> {
  148. logger.info("收到订阅消息: " + response.toJSONString());
  149. onPublishHandlerForPlay(mediaServerItemInUse, response, deviceId, channelId, uuid);
  150. if (hookEvent != null) {
  151. hookEvent.response(mediaServerItem, response);
  152. }
  153. }, (event) -> {
  154. WVPResult wvpResult = new WVPResult();
  155. wvpResult.setCode(-1);
  156. // 点播返回sip错误
  157. mediaServerService.closeRTPServer(playResult.getDevice(), channelId);
  158. wvpResult.setMsg(String.format("点播失败, 错误码: %s, %s", event.statusCode, event.msg));
  159. msg.setData(wvpResult);
  160. resultHolder.invokeAllResult(msg);
  161. if (errorEvent != null) {
  162. errorEvent.response(event);
  163. }
  164. });
  165. } else {
  166. String streamId = streamInfo.getStreamId();
  167. if (streamId == null) {
  168. WVPResult wvpResult = new WVPResult();
  169. wvpResult.setCode(-1);
  170. wvpResult.setMsg("点播失败, redis缓存streamId等于null");
  171. msg.setData(wvpResult);
  172. resultHolder.invokeAllResult(msg);
  173. return playResult;
  174. }
  175. String mediaServerId = streamInfo.getMediaServerId();
  176. MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
  177. JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaInfo, streamId);
  178. if (rtpInfo != null && rtpInfo.getBoolean("exist")) {
  179. WVPResult wvpResult = new WVPResult();
  180. wvpResult.setCode(0);
  181. wvpResult.setMsg("success");
  182. wvpResult.setData(streamInfo);
  183. msg.setData(wvpResult);
  184. resultHolder.invokeAllResult(msg);
  185. if (hookEvent != null) {
  186. hookEvent.response(mediaServerItem, JSONObject.parseObject(JSON.toJSONString(streamInfo)));
  187. }
  188. } else {
  189. // TODO 点播前是否重置状态
  190. redisCatchStorage.stopPlay(streamInfo);
  191. storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
  192. SSRCInfo ssrcInfo;
  193. String streamId2 = null;
  194. if (mediaServerItem.isRtpEnable()) {
  195. streamId2 = String.format("%s_%s", device.getDeviceId(), channelId);
  196. }
  197. ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId2);
  198. cmder.playStreamCmd(mediaServerItem, ssrcInfo, device, channelId, (MediaServerItem mediaServerItemInuse, JSONObject response) -> {
  199. logger.info("收到订阅消息: " + response.toJSONString());
  200. onPublishHandlerForPlay(mediaServerItemInuse, response, deviceId, channelId, uuid);
  201. }, (event) -> {
  202. mediaServerService.closeRTPServer(playResult.getDevice(), channelId);
  203. WVPResult wvpResult = new WVPResult();
  204. wvpResult.setCode(-1);
  205. wvpResult.setMsg(String.format("点播失败, 错误码: %s, %s", event.statusCode, event.msg));
  206. msg.setData(wvpResult);
  207. resultHolder.invokeAllResult(msg);
  208. });
  209. }
  210. }
  211. return playResult;
  212. }
  213. @Override
  214. public void onPublishHandlerForPlay(MediaServerItem mediaServerItem, JSONObject resonse, String deviceId, String channelId, String uuid) {
  215. RequestMessage msg = new RequestMessage();
  216. msg.setId(uuid);
  217. msg.setKey(DeferredResultHolder.CALLBACK_CMD_PLAY + deviceId + channelId);
  218. StreamInfo streamInfo = onPublishHandler(mediaServerItem, resonse, deviceId, channelId, uuid);
  219. if (streamInfo != null) {
  220. DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);
  221. if (deviceChannel != null) {
  222. deviceChannel.setStreamId(streamInfo.getStreamId());
  223. storager.startPlay(deviceId, channelId, streamInfo.getStreamId());
  224. }
  225. redisCatchStorage.startPlay(streamInfo);
  226. msg.setData(JSON.toJSONString(streamInfo));
  227. WVPResult wvpResult = new WVPResult();
  228. wvpResult.setCode(0);
  229. wvpResult.setMsg("success");
  230. wvpResult.setData(streamInfo);
  231. msg.setData(wvpResult);
  232. resultHolder.invokeAllResult(msg);
  233. } else {
  234. logger.warn("设备预览API调用失败!");
  235. msg.setData("设备预览API调用失败!");
  236. resultHolder.invokeAllResult(msg);
  237. }
  238. }
  239. @Override
  240. public MediaServerItem getNewMediaServerItem(Device device) {
  241. if (device == null) return null;
  242. String mediaServerId = device.getMediaServerId();
  243. MediaServerItem mediaServerItem;
  244. if (mediaServerId == null) {
  245. mediaServerItem = mediaServerService.getMediaServerForMinimumLoad();
  246. }else {
  247. mediaServerItem = mediaServerService.getOne(mediaServerId);
  248. }
  249. if (mediaServerItem == null) {
  250. logger.warn("点播时未找到可使用的ZLM...");
  251. }
  252. return mediaServerItem;
  253. }
  254. @Override
  255. public void onPublishHandlerForPlayBack(MediaServerItem mediaServerItem, JSONObject resonse, String deviceId, String channelId, String uuid) {
  256. RequestMessage msg = new RequestMessage();
  257. msg.setKey(DeferredResultHolder.CALLBACK_CMD_PLAYBACK + deviceId + channelId);
  258. msg.setId(uuid);
  259. StreamInfo streamInfo = onPublishHandler(mediaServerItem, resonse, deviceId, channelId, uuid);
  260. if (streamInfo != null) {
  261. redisCatchStorage.startPlayback(streamInfo);
  262. msg.setData(JSON.toJSONString(streamInfo));
  263. resultHolder.invokeResult(msg);
  264. } else {
  265. logger.warn("设备回放API调用失败!");
  266. msg.setData("设备回放API调用失败!");
  267. resultHolder.invokeResult(msg);
  268. }
  269. }
  270. @Override
  271. public void onPublishHandlerForDownload(MediaServerItem mediaServerItem, JSONObject response, String deviceId, String channelId, String uuid) {
  272. RequestMessage msg = new RequestMessage();
  273. msg.setKey(DeferredResultHolder.CALLBACK_CMD_DOWNLOAD + deviceId + channelId);
  274. msg.setId(uuid);
  275. StreamInfo streamInfo = onPublishHandler(mediaServerItem, response, deviceId, channelId, uuid);
  276. if (streamInfo != null) {
  277. redisCatchStorage.startDownload(streamInfo);
  278. msg.setData(JSON.toJSONString(streamInfo));
  279. resultHolder.invokeResult(msg);
  280. } else {
  281. logger.warn("设备预览API调用失败!");
  282. msg.setData("设备预览API调用失败!");
  283. resultHolder.invokeResult(msg);
  284. }
  285. }
  286. public StreamInfo onPublishHandler(MediaServerItem mediaServerItem, JSONObject resonse, String deviceId, String channelId, String uuid) {
  287. String streamId = resonse.getString("stream");
  288. JSONArray tracks = resonse.getJSONArray("tracks");
  289. StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(mediaServerItem,"rtp", streamId, tracks);
  290. streamInfo.setDeviceID(deviceId);
  291. streamInfo.setChannelId(channelId);
  292. return streamInfo;
  293. }
  294. }