PlayServiceImpl.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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.DynamicTask;
  7. import com.genersoft.iot.vmp.conf.UserSetting;
  8. import com.genersoft.iot.vmp.gb28181.bean.*;
  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.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
  15. import com.genersoft.iot.vmp.utils.DateUtil;
  16. import com.genersoft.iot.vmp.media.zlm.AssistRESTfulUtils;
  17. import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe;
  18. import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
  19. import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
  20. import com.genersoft.iot.vmp.service.IMediaServerService;
  21. import com.genersoft.iot.vmp.service.IMediaService;
  22. import com.genersoft.iot.vmp.service.IPlayService;
  23. import com.genersoft.iot.vmp.service.bean.InviteTimeOutCallback;
  24. import com.genersoft.iot.vmp.service.bean.PlayBackCallback;
  25. import com.genersoft.iot.vmp.service.bean.PlayBackResult;
  26. import com.genersoft.iot.vmp.service.bean.SSRCInfo;
  27. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  28. import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
  29. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  30. import com.genersoft.iot.vmp.vmanager.gb28181.play.bean.PlayResult;
  31. import gov.nist.javax.sip.stack.SIPDialog;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.http.HttpStatus;
  36. import org.springframework.http.ResponseEntity;
  37. import org.springframework.stereotype.Service;
  38. import org.springframework.util.ResourceUtils;
  39. import org.springframework.web.context.request.async.DeferredResult;
  40. import javax.sip.ResponseEvent;
  41. import java.io.FileNotFoundException;
  42. import java.math.BigDecimal;
  43. import java.math.RoundingMode;
  44. import java.util.*;
  45. @SuppressWarnings(value = {"rawtypes", "unchecked"})
  46. @Service
  47. public class PlayServiceImpl implements IPlayService {
  48. private final static Logger logger = LoggerFactory.getLogger(PlayServiceImpl.class);
  49. @Autowired
  50. private IVideoManagerStorage storager;
  51. @Autowired
  52. private SIPCommander cmder;
  53. @Autowired
  54. private SIPCommanderFroPlatform sipCommanderFroPlatform;
  55. @Autowired
  56. private IRedisCatchStorage redisCatchStorage;
  57. @Autowired
  58. private DeferredResultHolder resultHolder;
  59. @Autowired
  60. private ZLMRESTfulUtils zlmresTfulUtils;
  61. @Autowired
  62. private AssistRESTfulUtils assistRESTfulUtils;
  63. @Autowired
  64. private IMediaService mediaService;
  65. @Autowired
  66. private IMediaServerService mediaServerService;
  67. @Autowired
  68. private VideoStreamSessionManager streamSession;
  69. @Autowired
  70. private UserSetting userSetting;
  71. @Autowired
  72. private DynamicTask dynamicTask;
  73. @Autowired
  74. private ZLMHttpHookSubscribe subscribe;
  75. @Override
  76. public PlayResult play(MediaServerItem mediaServerItem, String deviceId, String channelId,
  77. ZLMHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent,
  78. Runnable timeoutCallback) {
  79. PlayResult playResult = new PlayResult();
  80. RequestMessage msg = new RequestMessage();
  81. String key = DeferredResultHolder.CALLBACK_CMD_PLAY + deviceId + channelId;
  82. msg.setKey(key);
  83. String uuid = UUID.randomUUID().toString();
  84. msg.setId(uuid);
  85. playResult.setUuid(uuid);
  86. DeferredResult<ResponseEntity<String>> result = new DeferredResult<>(userSetting.getPlayTimeout().longValue());
  87. playResult.setResult(result);
  88. // 录像查询以channelId作为deviceId查询
  89. resultHolder.put(key, uuid, result);
  90. if (mediaServerItem == null) {
  91. WVPResult wvpResult = new WVPResult();
  92. wvpResult.setCode(-1);
  93. wvpResult.setMsg("未找到可用的zlm");
  94. msg.setData(wvpResult);
  95. resultHolder.invokeResult(msg);
  96. return playResult;
  97. }
  98. Device device = redisCatchStorage.getDevice(deviceId);
  99. StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
  100. playResult.setDevice(device);
  101. result.onCompletion(()->{
  102. // 点播结束时调用截图接口
  103. // TODO 应该在上流时调用更好,结束也可能是错误结束
  104. String path = "static/static/snap/";
  105. String fileName = deviceId + "_" + channelId + ".jpg";
  106. ResponseEntity responseEntity = (ResponseEntity)result.getResult();
  107. if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
  108. WVPResult wvpResult = (WVPResult)responseEntity.getBody();
  109. if (Objects.requireNonNull(wvpResult).getCode() == 0) {
  110. StreamInfo streamInfoForSuccess = (StreamInfo)wvpResult.getData();
  111. MediaServerItem mediaInfo = mediaServerService.getOne(streamInfoForSuccess.getMediaServerId());
  112. String streamUrl = streamInfoForSuccess.getFmp4();
  113. // 请求截图
  114. logger.info("[请求截图]: " + fileName);
  115. zlmresTfulUtils.getSnap(mediaInfo, streamUrl, 15, 1, path, fileName);
  116. }
  117. }
  118. });
  119. if (streamInfo != null) {
  120. String streamId = streamInfo.getStream();
  121. if (streamId == null) {
  122. WVPResult wvpResult = new WVPResult();
  123. wvpResult.setCode(-1);
  124. wvpResult.setMsg("点播失败, redis缓存streamId等于null");
  125. msg.setData(wvpResult);
  126. resultHolder.invokeAllResult(msg);
  127. return playResult;
  128. }
  129. String mediaServerId = streamInfo.getMediaServerId();
  130. MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
  131. JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaInfo, streamId);
  132. if(rtpInfo.getInteger("code") == 0){
  133. if (rtpInfo.getBoolean("exist")) {
  134. WVPResult wvpResult = new WVPResult();
  135. wvpResult.setCode(0);
  136. wvpResult.setMsg("success");
  137. wvpResult.setData(streamInfo);
  138. msg.setData(wvpResult);
  139. resultHolder.invokeAllResult(msg);
  140. if (hookEvent != null) {
  141. hookEvent.response(mediaServerItem, JSONObject.parseObject(JSON.toJSONString(streamInfo)));
  142. }
  143. }else {
  144. redisCatchStorage.stopPlay(streamInfo);
  145. storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
  146. streamInfo = null;
  147. }
  148. }else {
  149. //zlm连接失败
  150. redisCatchStorage.stopPlay(streamInfo);
  151. storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
  152. streamInfo = null;
  153. }
  154. }
  155. if (streamInfo == null) {
  156. String streamId = null;
  157. if (mediaServerItem.isRtpEnable()) {
  158. streamId = String.format("%s_%s", device.getDeviceId(), channelId);
  159. }
  160. SSRCInfo ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId, device.isSsrcCheck(), false);
  161. play(mediaServerItem, ssrcInfo, device, channelId, (mediaServerItemInUse, response)->{
  162. if (hookEvent != null) {
  163. hookEvent.response(mediaServerItem, response);
  164. }
  165. }, event -> {
  166. // sip error错误
  167. WVPResult wvpResult = new WVPResult();
  168. wvpResult.setCode(-1);
  169. wvpResult.setMsg(String.format("点播失败, 错误码: %s, %s", event.statusCode, event.msg));
  170. msg.setData(wvpResult);
  171. resultHolder.invokeAllResult(msg);
  172. if (errorEvent != null) {
  173. errorEvent.response(event);
  174. }
  175. }, (code, msgStr)->{
  176. // invite点播超时
  177. WVPResult wvpResult = new WVPResult();
  178. wvpResult.setCode(-1);
  179. if (code == 0) {
  180. wvpResult.setMsg("点播超时,请稍候重试");
  181. }else if (code == 1) {
  182. wvpResult.setMsg("收流超时,请稍候重试");
  183. }
  184. msg.setData(wvpResult);
  185. // 回复之前所有的点播请求
  186. resultHolder.invokeAllResult(msg);
  187. }, uuid);
  188. }
  189. return playResult;
  190. }
  191. @Override
  192. public void play(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, Device device, String channelId,
  193. ZLMHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent,
  194. InviteTimeOutCallback timeoutCallback, String uuid) {
  195. String streamId = null;
  196. if (mediaServerItem.isRtpEnable()) {
  197. streamId = String.format("%s_%s", device.getDeviceId(), channelId);
  198. }
  199. if (ssrcInfo == null) {
  200. ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId, device.isSsrcCheck(), false);
  201. }
  202. logger.info("[点播开始] deviceId: {}, channelId: {}, SSRC: {}", device.getDeviceId(), channelId, ssrcInfo.getSsrc() );
  203. // 超时处理
  204. String timeOutTaskKey = UUID.randomUUID().toString();
  205. SSRCInfo finalSsrcInfo = ssrcInfo;
  206. dynamicTask.startDelay( timeOutTaskKey,()->{
  207. SIPDialog dialog = streamSession.getDialogByStream(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  208. if (dialog != null) {
  209. logger.info("[点播超时] 收流超时 deviceId: {}, channelId: {}", device.getDeviceId(), channelId);
  210. timeoutCallback.run(1, "收流超时");
  211. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  212. cmder.streamByeCmd(device.getDeviceId(), channelId, finalSsrcInfo.getStream(), null);
  213. }else {
  214. logger.info("[点播超时] 消息未响应 deviceId: {}, channelId: {}", device.getDeviceId(), channelId);
  215. timeoutCallback.run(0, "点播超时");
  216. mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  217. mediaServerService.closeRTPServer(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  218. streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  219. }
  220. }, userSetting.getPlayTimeout());
  221. final String ssrc = ssrcInfo.getSsrc();
  222. final String stream = ssrcInfo.getStream();
  223. //端口获取失败的ssrcInfo 没有必要发送点播指令
  224. if(ssrcInfo.getPort() <= 0){
  225. logger.info("[点播端口分配异常],deviceId={},channelId={},ssrcInfo={}", device.getDeviceId(), channelId, ssrcInfo);
  226. return;
  227. }
  228. cmder.playStreamCmd(mediaServerItem, ssrcInfo, device, channelId, (MediaServerItem mediaServerItemInuse, JSONObject response) -> {
  229. logger.info("收到订阅消息: " + response.toJSONString());
  230. dynamicTask.stop(timeOutTaskKey);
  231. // hook响应
  232. onPublishHandlerForPlay(mediaServerItemInuse, response, device.getDeviceId(), channelId, uuid);
  233. hookEvent.response(mediaServerItemInuse, response);
  234. logger.info("[点播成功] deviceId: {}, channelId: {}", device.getDeviceId(), channelId);
  235. }, (event) -> {
  236. ResponseEvent responseEvent = (ResponseEvent)event.event;
  237. String contentString = new String(responseEvent.getResponse().getRawContent());
  238. // 获取ssrc
  239. int ssrcIndex = contentString.indexOf("y=");
  240. // 检查是否有y字段
  241. if (ssrcIndex >= 0) {
  242. //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容
  243. String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
  244. // 查询到ssrc不一致且开启了ssrc校验则需要针对处理
  245. if (ssrc.equals(ssrcInResponse)) {
  246. return;
  247. }
  248. logger.info("[点播消息] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse );
  249. if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
  250. logger.info("[SIP 消息] SSRC修正 {}->{}", ssrc, ssrcInResponse);
  251. if (!mediaServerItem.getSsrcConfig().checkSsrc(ssrcInResponse)) {
  252. // ssrc 不可用
  253. // 释放ssrc
  254. mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  255. streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  256. event.msg = "下级自定义了ssrc,但是此ssrc不可用";
  257. event.statusCode = 400;
  258. errorEvent.response(event);
  259. return;
  260. }
  261. // 单端口模式streamId也有变化,需要重新设置监听
  262. if (!mediaServerItem.isRtpEnable()) {
  263. // 添加订阅
  264. JSONObject subscribeKey = new JSONObject();
  265. subscribeKey.put("app", "rtp");
  266. subscribeKey.put("stream", stream);
  267. subscribeKey.put("regist", true);
  268. subscribeKey.put("schema", "rtmp");
  269. subscribeKey.put("mediaServerId", mediaServerItem.getId());
  270. subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed,subscribeKey);
  271. subscribeKey.put("stream", String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase());
  272. subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey,
  273. (MediaServerItem mediaServerItemInUse, JSONObject response)->{
  274. logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + response.toJSONString());
  275. dynamicTask.stop(timeOutTaskKey);
  276. // hook响应
  277. onPublishHandlerForPlay(mediaServerItemInUse, response, device.getDeviceId(), channelId, uuid);
  278. hookEvent.response(mediaServerItemInUse, response);
  279. });
  280. }
  281. // 关闭rtp server
  282. mediaServerService.closeRTPServer(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  283. // 重新开启ssrc server
  284. mediaServerService.openRTPServer(mediaServerItem, finalSsrcInfo.getStream(), ssrcInResponse, device.isSsrcCheck(), false);
  285. }
  286. }
  287. }, (event) -> {
  288. dynamicTask.stop(timeOutTaskKey);
  289. mediaServerService.closeRTPServer(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  290. // 释放ssrc
  291. mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  292. streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  293. errorEvent.response(event);
  294. });
  295. }
  296. @Override
  297. public void onPublishHandlerForPlay(MediaServerItem mediaServerItem, JSONObject response, String deviceId, String channelId, String uuid) {
  298. RequestMessage msg = new RequestMessage();
  299. if (uuid != null) {
  300. msg.setId(uuid);
  301. }
  302. msg.setKey(DeferredResultHolder.CALLBACK_CMD_PLAY + deviceId + channelId);
  303. StreamInfo streamInfo = onPublishHandler(mediaServerItem, response, deviceId, channelId);
  304. if (streamInfo != null) {
  305. DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);
  306. if (deviceChannel != null) {
  307. deviceChannel.setStreamId(streamInfo.getStream());
  308. storager.startPlay(deviceId, channelId, streamInfo.getStream());
  309. }
  310. redisCatchStorage.startPlay(streamInfo);
  311. WVPResult wvpResult = new WVPResult();
  312. wvpResult.setCode(0);
  313. wvpResult.setMsg("success");
  314. wvpResult.setData(streamInfo);
  315. msg.setData(wvpResult);
  316. resultHolder.invokeAllResult(msg);
  317. } else {
  318. logger.warn("设备预览API调用失败!");
  319. msg.setData("设备预览API调用失败!");
  320. resultHolder.invokeAllResult(msg);
  321. }
  322. }
  323. @Override
  324. public MediaServerItem getNewMediaServerItem(Device device) {
  325. if (device == null) {
  326. return null;
  327. }
  328. String mediaServerId = device.getMediaServerId();
  329. MediaServerItem mediaServerItem;
  330. if (mediaServerId == null) {
  331. mediaServerItem = mediaServerService.getMediaServerForMinimumLoad();
  332. }else {
  333. mediaServerItem = mediaServerService.getOne(mediaServerId);
  334. }
  335. if (mediaServerItem == null) {
  336. logger.warn("点播时未找到可使用的ZLM...");
  337. }
  338. return mediaServerItem;
  339. }
  340. @Override
  341. public DeferredResult<ResponseEntity<String>> playBack(String deviceId, String channelId, String startTime,
  342. String endTime,InviteStreamCallback inviteStreamCallback,
  343. PlayBackCallback callback) {
  344. Device device = storager.queryVideoDevice(deviceId);
  345. if (device == null) {
  346. return null;
  347. }
  348. MediaServerItem newMediaServerItem = getNewMediaServerItem(device);
  349. SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, true, true);
  350. return playBack(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, inviteStreamCallback, callback);
  351. }
  352. @Override
  353. public DeferredResult<ResponseEntity<String>> playBack(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo,
  354. String deviceId, String channelId, String startTime,
  355. String endTime, InviteStreamCallback infoCallBack,
  356. PlayBackCallback playBackCallback) {
  357. if (mediaServerItem == null || ssrcInfo == null) {
  358. return null;
  359. }
  360. String uuid = UUID.randomUUID().toString();
  361. String key = DeferredResultHolder.CALLBACK_CMD_PLAYBACK + deviceId + channelId;
  362. DeferredResult<ResponseEntity<String>> result = new DeferredResult<>(30000L);
  363. Device device = storager.queryVideoDevice(deviceId);
  364. if (device == null) {
  365. result.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
  366. return result;
  367. }
  368. resultHolder.put(DeferredResultHolder.CALLBACK_CMD_PLAYBACK + deviceId + channelId, uuid, result);
  369. RequestMessage msg = new RequestMessage();
  370. msg.setId(uuid);
  371. msg.setKey(key);
  372. PlayBackResult<RequestMessage> playBackResult = new PlayBackResult<>();
  373. String playBackTimeOutTaskKey = UUID.randomUUID().toString();
  374. dynamicTask.startDelay(playBackTimeOutTaskKey, ()->{
  375. logger.warn(String.format("设备回放超时,deviceId:%s ,channelId:%s", deviceId, channelId));
  376. playBackResult.setCode(-1);
  377. playBackResult.setData(msg);
  378. playBackCallback.call(playBackResult);
  379. SIPDialog dialog = streamSession.getDialogByStream(deviceId, channelId, ssrcInfo.getStream());
  380. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  381. if (dialog != null) {
  382. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  383. cmder.streamByeCmd(device.getDeviceId(), channelId, ssrcInfo.getStream(), null);
  384. }else {
  385. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  386. mediaServerService.closeRTPServer(deviceId, channelId, ssrcInfo.getStream());
  387. streamSession.remove(deviceId, channelId, ssrcInfo.getStream());
  388. }
  389. cmder.streamByeCmd(device.getDeviceId(), channelId, ssrcInfo.getStream(), null);
  390. // 回复之前所有的点播请求
  391. playBackCallback.call(playBackResult);
  392. }, userSetting.getPlayTimeout());
  393. cmder.playbackStreamCmd(mediaServerItem, ssrcInfo, device, channelId, startTime, endTime, infoCallBack,
  394. (InviteStreamInfo inviteStreamInfo) -> {
  395. logger.info("收到订阅消息: " + inviteStreamInfo.getResponse().toJSONString());
  396. dynamicTask.stop(playBackTimeOutTaskKey);
  397. StreamInfo streamInfo = onPublishHandler(inviteStreamInfo.getMediaServerItem(), inviteStreamInfo.getResponse(), deviceId, channelId);
  398. if (streamInfo == null) {
  399. logger.warn("设备回放API调用失败!");
  400. msg.setData("设备回放API调用失败!");
  401. playBackResult.setCode(-1);
  402. playBackResult.setData(msg);
  403. playBackCallback.call(playBackResult);
  404. return;
  405. }
  406. redisCatchStorage.startPlayback(streamInfo, inviteStreamInfo.getCallId());
  407. msg.setData(JSON.toJSONString(streamInfo));
  408. playBackResult.setCode(0);
  409. playBackResult.setData(msg);
  410. playBackResult.setMediaServerItem(inviteStreamInfo.getMediaServerItem());
  411. playBackResult.setResponse(inviteStreamInfo.getResponse());
  412. playBackCallback.call(playBackResult);
  413. }, event -> {
  414. dynamicTask.stop(playBackTimeOutTaskKey);
  415. msg.setData(String.format("回放失败, 错误码: %s, %s", event.statusCode, event.msg));
  416. playBackResult.setCode(-1);
  417. playBackResult.setData(msg);
  418. playBackResult.setEvent(event);
  419. playBackCallback.call(playBackResult);
  420. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  421. });
  422. return result;
  423. }
  424. @Override
  425. public DeferredResult<ResponseEntity<String>> download(String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, InviteStreamCallback infoCallBack, PlayBackCallback hookCallBack) {
  426. Device device = storager.queryVideoDevice(deviceId);
  427. if (device == null) {
  428. return null;
  429. }
  430. MediaServerItem newMediaServerItem = getNewMediaServerItem(device);
  431. SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, true, true);
  432. return download(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, downloadSpeed,infoCallBack, hookCallBack);
  433. }
  434. @Override
  435. public DeferredResult<ResponseEntity<String>> download(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, InviteStreamCallback infoCallBack, PlayBackCallback hookCallBack) {
  436. if (mediaServerItem == null || ssrcInfo == null) {
  437. return null;
  438. }
  439. String uuid = UUID.randomUUID().toString();
  440. String key = DeferredResultHolder.CALLBACK_CMD_DOWNLOAD + deviceId + channelId;
  441. DeferredResult<ResponseEntity<String>> result = new DeferredResult<>(30000L);
  442. Device device = storager.queryVideoDevice(deviceId);
  443. if (device == null) {
  444. result.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
  445. return result;
  446. }
  447. resultHolder.put(key, uuid, result);
  448. RequestMessage msg = new RequestMessage();
  449. msg.setId(uuid);
  450. msg.setKey(key);
  451. WVPResult<StreamInfo> wvpResult = new WVPResult<>();
  452. msg.setData(wvpResult);
  453. PlayBackResult<RequestMessage> downloadResult = new PlayBackResult<>();
  454. downloadResult.setData(msg);
  455. String downLoadTimeOutTaskKey = UUID.randomUUID().toString();
  456. dynamicTask.startDelay(downLoadTimeOutTaskKey, ()->{
  457. logger.warn(String.format("录像下载请求超时,deviceId:%s ,channelId:%s", deviceId, channelId));
  458. wvpResult.setCode(-1);
  459. wvpResult.setMsg("录像下载请求超时");
  460. downloadResult.setCode(-1);
  461. hookCallBack.call(downloadResult);
  462. SIPDialog dialog = streamSession.getDialogByStream(deviceId, channelId, ssrcInfo.getStream());
  463. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  464. if (dialog != null) {
  465. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  466. cmder.streamByeCmd(device.getDeviceId(), channelId, ssrcInfo.getStream(), null);
  467. }else {
  468. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  469. mediaServerService.closeRTPServer(deviceId, channelId, ssrcInfo.getStream());
  470. streamSession.remove(deviceId, channelId, ssrcInfo.getStream());
  471. }
  472. cmder.streamByeCmd(device.getDeviceId(), channelId, ssrcInfo.getStream(), null);
  473. // 回复之前所有的点播请求
  474. hookCallBack.call(downloadResult);
  475. }, userSetting.getPlayTimeout());
  476. cmder.downloadStreamCmd(mediaServerItem, ssrcInfo, device, channelId, startTime, endTime, downloadSpeed, infoCallBack,
  477. inviteStreamInfo -> {
  478. logger.info("收到订阅消息: " + inviteStreamInfo.getResponse().toJSONString());
  479. dynamicTask.stop(downLoadTimeOutTaskKey);
  480. StreamInfo streamInfo = onPublishHandler(inviteStreamInfo.getMediaServerItem(), inviteStreamInfo.getResponse(), deviceId, channelId);
  481. streamInfo.setStartTime(startTime);
  482. streamInfo.setEndTime(endTime);
  483. if (streamInfo == null) {
  484. logger.warn("录像下载API调用失败!");
  485. wvpResult.setCode(-1);
  486. wvpResult.setMsg("录像下载API调用失败");
  487. downloadResult.setCode(-1);
  488. hookCallBack.call(downloadResult);
  489. return ;
  490. }
  491. redisCatchStorage.startDownload(streamInfo, inviteStreamInfo.getCallId());
  492. wvpResult.setCode(0);
  493. wvpResult.setMsg("success");
  494. wvpResult.setData(streamInfo);
  495. downloadResult.setCode(0);
  496. downloadResult.setMediaServerItem(inviteStreamInfo.getMediaServerItem());
  497. downloadResult.setResponse(inviteStreamInfo.getResponse());
  498. hookCallBack.call(downloadResult);
  499. }, event -> {
  500. dynamicTask.stop(downLoadTimeOutTaskKey);
  501. downloadResult.setCode(-1);
  502. wvpResult.setCode(-1);
  503. wvpResult.setMsg(String.format("录像下载失败, 错误码: %s, %s", event.statusCode, event.msg));
  504. downloadResult.setEvent(event);
  505. hookCallBack.call(downloadResult);
  506. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  507. });
  508. return result;
  509. }
  510. @Override
  511. public StreamInfo getDownLoadInfo(String deviceId, String channelId, String stream) {
  512. StreamInfo streamInfo = redisCatchStorage.queryDownload(deviceId, channelId, stream, null);
  513. if (streamInfo != null) {
  514. if (streamInfo.getProgress() == 1) {
  515. return streamInfo;
  516. }
  517. // 获取当前已下载时长
  518. String mediaServerId = streamInfo.getMediaServerId();
  519. MediaServerItem mediaServerItem = mediaServerService.getOne(mediaServerId);
  520. if (mediaServerItem == null) {
  521. logger.warn("查询录像信息时发现节点已离线");
  522. return null;
  523. }
  524. if (mediaServerItem.getRecordAssistPort() != 0) {
  525. JSONObject jsonObject = assistRESTfulUtils.fileDuration(mediaServerItem, streamInfo.getApp(), streamInfo.getStream(), null);
  526. if (jsonObject != null && jsonObject.getInteger("code") == 0) {
  527. long duration = jsonObject.getLong("data");
  528. if (duration == 0) {
  529. streamInfo.setProgress(0);
  530. }else {
  531. String startTime = streamInfo.getStartTime();
  532. String endTime = streamInfo.getEndTime();
  533. long start = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime);
  534. long end = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime);
  535. BigDecimal currentCount = new BigDecimal(duration/1000);
  536. BigDecimal totalCount = new BigDecimal(end-start);
  537. BigDecimal divide = currentCount.divide(totalCount,2, RoundingMode.HALF_UP);
  538. double process = divide.doubleValue();
  539. streamInfo.setProgress(process);
  540. }
  541. }
  542. }
  543. }
  544. return streamInfo;
  545. }
  546. @Override
  547. public void onPublishHandlerForDownload(InviteStreamInfo inviteStreamInfo, String deviceId, String channelId, String uuid) {
  548. RequestMessage msg = new RequestMessage();
  549. msg.setKey(DeferredResultHolder.CALLBACK_CMD_DOWNLOAD + deviceId + channelId);
  550. msg.setId(uuid);
  551. StreamInfo streamInfo = onPublishHandler(inviteStreamInfo.getMediaServerItem(), inviteStreamInfo.getResponse(), deviceId, channelId);
  552. if (streamInfo != null) {
  553. redisCatchStorage.startDownload(streamInfo, inviteStreamInfo.getCallId());
  554. msg.setData(JSON.toJSONString(streamInfo));
  555. resultHolder.invokeResult(msg);
  556. } else {
  557. logger.warn("设备预览API调用失败!");
  558. msg.setData("设备预览API调用失败!");
  559. resultHolder.invokeResult(msg);
  560. }
  561. }
  562. public StreamInfo onPublishHandler(MediaServerItem mediaServerItem, JSONObject resonse, String deviceId, String channelId) {
  563. String streamId = resonse.getString("stream");
  564. JSONArray tracks = resonse.getJSONArray("tracks");
  565. StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(mediaServerItem,"rtp", streamId, tracks);
  566. streamInfo.setDeviceID(deviceId);
  567. streamInfo.setChannelId(channelId);
  568. return streamInfo;
  569. }
  570. @Override
  571. public void zlmServerOffline(String mediaServerId) {
  572. // 处理正在向上推流的上级平台
  573. List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServer(null);
  574. if (sendRtpItems.size() > 0) {
  575. for (SendRtpItem sendRtpItem : sendRtpItems) {
  576. if (sendRtpItem.getMediaServerId().equals(mediaServerId)) {
  577. ParentPlatform platform = storager.queryParentPlatByServerGBId(sendRtpItem.getPlatformId());
  578. sipCommanderFroPlatform.streamByeCmd(platform, sendRtpItem.getCallId());
  579. }
  580. }
  581. }
  582. // 处理正在观看的国标设备
  583. List<SsrcTransaction> allSsrc = streamSession.getAllSsrc();
  584. if (allSsrc.size() > 0) {
  585. for (SsrcTransaction ssrcTransaction : allSsrc) {
  586. if(ssrcTransaction.getMediaServerId().equals(mediaServerId)) {
  587. cmder.streamByeCmd(ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId(),
  588. ssrcTransaction.getStream(), null);
  589. }
  590. }
  591. }
  592. }
  593. @Override
  594. public void zlmServerOnline(String mediaServerId) {
  595. // 似乎没啥需要做的
  596. }
  597. }