PlayServiceImpl.java 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. package com.genersoft.iot.vmp.service.impl;
  2. import java.math.BigDecimal;
  3. import java.math.RoundingMode;
  4. import java.text.ParseException;
  5. import java.util.*;
  6. import javax.sip.InvalidArgumentException;
  7. import javax.sip.ResponseEvent;
  8. import javax.sip.SipException;
  9. import com.genersoft.iot.vmp.gb28181.bean.*;
  10. import com.genersoft.iot.vmp.common.VideoManagerConstants;
  11. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  12. import com.genersoft.iot.vmp.conf.exception.ServiceException;
  13. import com.genersoft.iot.vmp.conf.exception.SsrcTransactionNotFoundException;
  14. import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
  15. import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
  16. import com.genersoft.iot.vmp.media.zlm.dto.MediaItem;
  17. import com.genersoft.iot.vmp.service.IDeviceService;
  18. import com.genersoft.iot.vmp.vmanager.bean.AudioBroadcastResult;
  19. import com.genersoft.iot.vmp.utils.redis.RedisUtil;
  20. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Qualifier;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.util.ObjectUtils;
  29. import org.springframework.util.StringUtils;
  30. import org.springframework.web.context.request.async.DeferredResult;
  31. import com.alibaba.fastjson.JSON;
  32. import com.alibaba.fastjson.JSONArray;
  33. import com.alibaba.fastjson.JSONObject;
  34. import com.genersoft.iot.vmp.common.StreamInfo;
  35. import com.genersoft.iot.vmp.conf.DynamicTask;
  36. import com.genersoft.iot.vmp.conf.SipConfig;
  37. import com.genersoft.iot.vmp.conf.UserSetting;
  38. import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
  39. import com.genersoft.iot.vmp.gb28181.session.AudioBroadcastManager;
  40. import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
  41. import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
  42. import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
  43. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  44. import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
  45. import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory;
  46. import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForStreamChange;
  47. import com.genersoft.iot.vmp.utils.DateUtil;
  48. import com.genersoft.iot.vmp.media.zlm.AssistRESTfulUtils;
  49. import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe;
  50. import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
  51. import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
  52. import com.genersoft.iot.vmp.service.IMediaServerService;
  53. import com.genersoft.iot.vmp.service.IMediaService;
  54. import com.genersoft.iot.vmp.service.IPlayService;
  55. import com.genersoft.iot.vmp.service.bean.InviteTimeOutCallback;
  56. import com.genersoft.iot.vmp.service.bean.PlayBackCallback;
  57. import com.genersoft.iot.vmp.service.bean.PlayBackResult;
  58. import com.genersoft.iot.vmp.service.bean.SSRCInfo;
  59. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  60. import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
  61. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  62. import com.genersoft.iot.vmp.vmanager.gb28181.play.bean.AudioBroadcastEvent;
  63. import com.genersoft.iot.vmp.vmanager.gb28181.play.bean.PlayResult;
  64. @SuppressWarnings(value = {"rawtypes", "unchecked"})
  65. @Service
  66. public class PlayServiceImpl implements IPlayService {
  67. private final static Logger logger = LoggerFactory.getLogger(PlayServiceImpl.class);
  68. @Autowired
  69. private IVideoManagerStorage storager;
  70. @Autowired
  71. private SIPCommander cmder;
  72. @Autowired
  73. private AudioBroadcastManager audioBroadcastManager;
  74. @Autowired
  75. private IDeviceService deviceService;
  76. @Autowired
  77. private ISIPCommanderForPlatform sipCommanderFroPlatform;
  78. @Autowired
  79. private IRedisCatchStorage redisCatchStorage;
  80. @Autowired
  81. private ZLMRTPServerFactory zlmrtpServerFactory;
  82. @Autowired
  83. private DeferredResultHolder resultHolder;
  84. @Autowired
  85. private ZLMRESTfulUtils zlmresTfulUtils;
  86. @Autowired
  87. private AssistRESTfulUtils assistRESTfulUtils;
  88. @Autowired
  89. private IMediaService mediaService;
  90. @Autowired
  91. private IMediaServerService mediaServerService;
  92. @Autowired
  93. private VideoStreamSessionManager streamSession;
  94. @Autowired
  95. private UserSetting userSetting;
  96. @Autowired
  97. private SipConfig sipConfig;
  98. @Autowired
  99. private DynamicTask dynamicTask;
  100. @Autowired
  101. private ZlmHttpHookSubscribe subscribe;
  102. @Qualifier("taskExecutor")
  103. @Autowired
  104. private ThreadPoolTaskExecutor taskExecutor;
  105. @Override
  106. public PlayResult play(MediaServerItem mediaServerItem, String deviceId, String channelId,
  107. ZlmHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent,
  108. Runnable timeoutCallback) {
  109. if (mediaServerItem == null) {
  110. throw new ControllerException(ErrorCode.ERROR100.getCode(), "未找到可用的zlm");
  111. }
  112. PlayResult playResult = new PlayResult();
  113. RequestMessage msg = new RequestMessage();
  114. String key = DeferredResultHolder.CALLBACK_CMD_PLAY + deviceId + channelId;
  115. msg.setKey(key);
  116. String uuid = UUID.randomUUID().toString();
  117. msg.setId(uuid);
  118. playResult.setUuid(uuid);
  119. DeferredResult<WVPResult<String>> result = new DeferredResult<>(userSetting.getPlayTimeout().longValue());
  120. playResult.setResult(result);
  121. // 录像查询以channelId作为deviceId查询
  122. resultHolder.put(key, uuid, result);
  123. Device device = redisCatchStorage.getDevice(deviceId);
  124. StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
  125. playResult.setDevice(device);
  126. result.onCompletion(() -> {
  127. // 点播结束时调用截图接口
  128. taskExecutor.execute(() -> {
  129. // TODO 应该在上流时调用更好,结束也可能是错误结束
  130. String path = "snap";
  131. String fileName = deviceId + "_" + channelId + ".jpg";
  132. WVPResult wvpResult = (WVPResult) result.getResult();
  133. if (Objects.requireNonNull(wvpResult).getCode() == 0) {
  134. StreamInfo streamInfoForSuccess = (StreamInfo) wvpResult.getData();
  135. MediaServerItem mediaInfo = mediaServerService.getOne(streamInfoForSuccess.getMediaServerId());
  136. String streamUrl = streamInfoForSuccess.getFmp4();
  137. // 请求截图
  138. logger.info("[请求截图]: " + fileName);
  139. zlmresTfulUtils.getSnap(mediaInfo, streamUrl, 15, 1, path, fileName);
  140. }
  141. });
  142. });
  143. if (streamInfo != null) {
  144. String streamId = streamInfo.getStream();
  145. if (streamId == null) {
  146. WVPResult wvpResult = new WVPResult();
  147. wvpResult.setCode(ErrorCode.ERROR100.getCode());
  148. wvpResult.setMsg("点播失败, redis缓存streamId等于null");
  149. msg.setData(wvpResult);
  150. resultHolder.invokeAllResult(msg);
  151. return playResult;
  152. }
  153. String mediaServerId = streamInfo.getMediaServerId();
  154. MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
  155. JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaInfo, streamId);
  156. if (rtpInfo.getInteger("code") == 0) {
  157. if (rtpInfo.getBoolean("exist")) {
  158. int localPort = rtpInfo.getInteger("local_port");
  159. if (localPort == 0) {
  160. logger.warn("[点播],点播时发现rtpServerC存在,但是尚未开始推流");
  161. // 此时说明rtpServer已经创建但是流还没有推上来
  162. WVPResult wvpResult = new WVPResult();
  163. wvpResult.setCode(ErrorCode.ERROR100.getCode());
  164. wvpResult.setMsg("点播已经在进行中,请稍候重试");
  165. msg.setData(wvpResult);
  166. resultHolder.invokeAllResult(msg);
  167. return playResult;
  168. } else {
  169. WVPResult wvpResult = new WVPResult();
  170. wvpResult.setCode(ErrorCode.SUCCESS.getCode());
  171. wvpResult.setMsg(ErrorCode.SUCCESS.getMsg());
  172. wvpResult.setData(streamInfo);
  173. msg.setData(wvpResult);
  174. resultHolder.invokeAllResult(msg);
  175. if (hookEvent != null) {
  176. hookEvent.response(mediaServerItem, JSONObject.parseObject(JSON.toJSONString(streamInfo)));
  177. }
  178. }
  179. } else {
  180. redisCatchStorage.stopPlay(streamInfo);
  181. storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
  182. streamInfo = null;
  183. }
  184. } else {
  185. //zlm连接失败
  186. redisCatchStorage.stopPlay(streamInfo);
  187. storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
  188. streamInfo = null;
  189. }
  190. }
  191. if (streamInfo == null) {
  192. String streamId = null;
  193. if (mediaServerItem.isRtpEnable()) {
  194. streamId = String.format("%s_%s", device.getDeviceId(), channelId);
  195. }
  196. SSRCInfo ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId, device.isSsrcCheck(), false);
  197. logger.info(JSONObject.toJSONString(ssrcInfo));
  198. play(mediaServerItem, ssrcInfo, device, channelId, (mediaServerItemInUse, response) -> {
  199. if (hookEvent != null) {
  200. hookEvent.response(mediaServerItem, response);
  201. }
  202. }, event -> {
  203. // sip error错误
  204. WVPResult wvpResult = new WVPResult();
  205. wvpResult.setCode(ErrorCode.ERROR100.getCode());
  206. wvpResult.setMsg(String.format("点播失败, 错误码: %s, %s", event.statusCode, event.msg));
  207. msg.setData(wvpResult);
  208. resultHolder.invokeAllResult(msg);
  209. if (errorEvent != null) {
  210. errorEvent.response(event);
  211. }
  212. }, (code, msgStr) -> {
  213. // invite点播超时
  214. WVPResult wvpResult = new WVPResult();
  215. wvpResult.setCode(ErrorCode.ERROR100.getCode());
  216. if (code == 0) {
  217. wvpResult.setMsg("点播超时,请稍候重试");
  218. } else if (code == 1) {
  219. wvpResult.setMsg("收流超时,请稍候重试");
  220. }
  221. msg.setData(wvpResult);
  222. // 回复之前所有的点播请求
  223. resultHolder.invokeAllResult(msg);
  224. }, uuid);
  225. }
  226. return playResult;
  227. }
  228. @Override
  229. public void talk(MediaServerItem mediaServerItem, Device device, String channelId,
  230. ZlmHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent,
  231. Runnable timeoutCallback) {
  232. String streamId = null;
  233. if (mediaServerItem.isRtpEnable()) {
  234. streamId = String.format("%s_%s", device.getDeviceId(), channelId);
  235. }
  236. SSRCInfo ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId, device.isSsrcCheck(), false);
  237. logger.info("[对讲开始] deviceId: {}, channelId: {},收流端口: {}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, ssrcInfo.getPort(), device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck());
  238. // 超时处理
  239. String timeOutTaskKey = UUID.randomUUID().toString();
  240. SSRCInfo finalSsrcInfo = ssrcInfo;
  241. System.out.println("设置超时任务: " + timeOutTaskKey);
  242. dynamicTask.startDelay(timeOutTaskKey, () -> {
  243. logger.info("[对讲超时] 收流超时 deviceId: {}, channelId: {},端口:{}, SSRC: {}", device.getDeviceId(), channelId, finalSsrcInfo.getPort(), finalSsrcInfo.getSsrc());
  244. timeoutCallback.run();
  245. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  246. try {
  247. cmder.streamByeCmd(device, channelId, finalSsrcInfo.getStream(), null);
  248. } catch (InvalidArgumentException | ParseException | SipException e) {
  249. logger.error("[对讲超时], 发送BYE失败 {}", e.getMessage());
  250. } catch (SsrcTransactionNotFoundException e) {
  251. timeoutCallback.run();
  252. mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  253. mediaServerService.closeRTPServer(mediaServerItem, finalSsrcInfo.getStream());
  254. streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  255. }
  256. }, userSetting.getPlayTimeout());
  257. final String ssrc = ssrcInfo.getSsrc();
  258. final String stream = ssrcInfo.getStream();
  259. //端口获取失败的ssrcInfo 没有必要发送点播指令
  260. if (ssrcInfo.getPort() <= 0) {
  261. logger.info("[对讲] 端口分配异常,deviceId={},channelId={},ssrcInfo={}", device.getDeviceId(), channelId, ssrcInfo);
  262. return;
  263. }
  264. String callId = SipUtils.getNewCallId();
  265. boolean pushing = false;
  266. // 查看设备是否已经在推流
  267. // MediaItem mediaItem = zlmrtpServerFactory.getMediaInfo(mediaServerItem, "rtp",ssrcInfo.getStream());
  268. // if (mediaItem != null) {
  269. // SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(mediaServerItem,
  270. // mediaItem.getOriginSock().getPeer_ip(), mediaItem.getOriginSock().getPeer_port(), ssrcInfo.getSsrc(), device.getDeviceId(),
  271. // device.getDeviceId(), channelId,
  272. // false);
  273. //
  274. // sendRtpItem.setTcpActive(false);
  275. // sendRtpItem.setCallId(callId);
  276. // sendRtpItem.setPlayType(InviteStreamType.TALK);
  277. // sendRtpItem.setStatus(1);
  278. // sendRtpItem.setIp(mediaItem.getOriginSock().getPeer_ip());
  279. // sendRtpItem.setPort(mediaItem.getOriginSock().getPeer_port());
  280. // sendRtpItem.setTcpActive(false);
  281. // sendRtpItem.setStreamId(ssrcInfo.getStream());
  282. // sendRtpItem.setApp("1000");
  283. // sendRtpItem.setStreamId("1000");
  284. // sendRtpItem.setSsrc(ssrc);
  285. // sendRtpItem.setOnlyAudio(true);
  286. // redisCatchStorage.updateSendRTPSever(sendRtpItem);
  287. //
  288. // Map<String, Object> param = new HashMap<>(12);
  289. // param.put("vhost","__defaultVhost__");
  290. // param.put("app",sendRtpItem.getApp());
  291. // param.put("stream",sendRtpItem.getStreamId());
  292. // param.put("ssrc", sendRtpItem.getSsrc());
  293. // param.put("dst_url", sendRtpItem.getIp());
  294. // param.put("dst_port", sendRtpItem.getPort());
  295. // param.put("src_port", sendRtpItem.getLocalPort());
  296. // param.put("pt", sendRtpItem.getPt());
  297. // param.put("use_ps", sendRtpItem.isUsePs() ? "1" : "0");
  298. // param.put("is_udp", sendRtpItem.isTcp() ? "0" : "1");
  299. // param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0");
  300. // JSONObject jsonObject = zlmrtpServerFactory.startSendRtpStream(mediaServerItem, param);
  301. // System.out.println(2222);
  302. // System.out.println(jsonObject);
  303. // }else {
  304. try {
  305. cmder.talkStreamCmd(mediaServerItem, ssrcInfo, device, channelId, callId, (MediaServerItem mediaServerItemInuse, JSONObject response) -> {
  306. logger.info("[对讲] 流已生成, 开始推流: " + response.toJSONString());
  307. dynamicTask.stop(timeOutTaskKey);
  308. // TODO 暂不做处理
  309. }, (MediaServerItem mediaServerItemInuse, JSONObject json) -> {
  310. logger.info("[对讲] 设备开始推流: " + json.toJSONString());
  311. dynamicTask.stop(timeOutTaskKey);
  312. // 获取远程IP端口 作为回复语音流的地址
  313. String ip = json.getString("ip");
  314. Integer port = json.getInteger("port");
  315. logger.info("[设备开始推流]{}/{}, 来自ip:{}, 端口:{}", device.getDeviceId(), channelId, ip, port);
  316. // 查看平台推流是否就绪
  317. // Boolean ready = zlmrtpServerFactory.isStreamReady(mediaServerItemInuse, "talk", stream);
  318. // if (!ready) {
  319. // try {
  320. // cmder.streamByeCmd(device, channelId, finalSsrcInfo.getStream(), null);
  321. // } catch (InvalidArgumentException | ParseException | SipException e) {
  322. // logger.error("[对讲超时], 发送BYE失败 {}", e.getMessage());
  323. // } catch (SsrcTransactionNotFoundException e) {
  324. // timeoutCallback.run();
  325. // mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  326. // mediaServerService.closeRTPServer(mediaServerItem, finalSsrcInfo.getStream());
  327. // streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  328. // }
  329. // }else {
  330. // try {
  331. // Thread.sleep(1000);
  332. // } catch (InterruptedException e) {
  333. // throw new RuntimeException(e);
  334. // }
  335. SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(mediaServerItem, ip, port, ssrcInfo.getSsrc(), device.getDeviceId(),
  336. device.getDeviceId(), channelId,
  337. false);
  338. // if (sendRtpItem.getLocalPort() == 0) {
  339. // logger.warn("服务器端口资源不足");
  340. // try {
  341. // cmder.streamByeCmd(device, channelId, finalSsrcInfo.getStream(), null);
  342. // } catch (InvalidArgumentException | ParseException | SipException e) {
  343. // logger.error("[对讲超时], 发送BYE失败 {}", e.getMessage());
  344. // } catch (SsrcTransactionNotFoundException e) {
  345. // timeoutCallback.run();
  346. // mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  347. // mediaServerService.closeRTPServer(mediaServerItem, finalSsrcInfo.getStream());
  348. // streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  349. // }
  350. // return;
  351. // }
  352. sendRtpItem.setTcpActive(false);
  353. sendRtpItem.setCallId(callId);
  354. sendRtpItem.setPlayType(InviteStreamType.TALK);
  355. sendRtpItem.setStatus(1);
  356. sendRtpItem.setIp(ip);
  357. sendRtpItem.setPort(port);
  358. sendRtpItem.setTcpActive(false);
  359. sendRtpItem.setApp("1000");
  360. sendRtpItem.setStreamId("1000");
  361. sendRtpItem.setSsrc(ssrc);
  362. sendRtpItem.setOnlyAudio(true);
  363. redisCatchStorage.updateSendRTPSever(sendRtpItem);
  364. Map<String, Object> param = new HashMap<>(12);
  365. param.put("vhost","__defaultVhost__");
  366. param.put("app",sendRtpItem.getApp());
  367. param.put("stream",sendRtpItem.getStreamId());
  368. param.put("ssrc", sendRtpItem.getSsrc());
  369. param.put("dst_url", sendRtpItem.getIp());
  370. param.put("dst_port", sendRtpItem.getPort());
  371. param.put("src_port", sendRtpItem.getLocalPort());
  372. param.put("pt", sendRtpItem.getPt());
  373. param.put("use_ps", sendRtpItem.isUsePs() ? "1" : "0");
  374. param.put("is_udp", sendRtpItem.isTcp() ? "0" : "1");
  375. param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0");
  376. JSONObject jsonObject = zlmrtpServerFactory.startSendRtpStream(mediaServerItemInuse, param);
  377. System.out.println(11111);
  378. System.out.println(sendRtpItem.getIp() + ":" + sendRtpItem.getPort());
  379. // System.out.println(jsonObject);
  380. // }
  381. }, (event) -> {
  382. }, (event) -> {
  383. dynamicTask.stop(timeOutTaskKey);
  384. mediaServerService.closeRTPServer(mediaServerItem, finalSsrcInfo.getStream());
  385. // 释放ssrc
  386. mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  387. streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  388. errorEvent.response(event);
  389. });
  390. } catch (InvalidArgumentException | SipException | ParseException e) {
  391. logger.error("[命令发送失败] 对讲消息: {}", e.getMessage());
  392. dynamicTask.stop(timeOutTaskKey);
  393. mediaServerService.closeRTPServer(mediaServerItem, finalSsrcInfo.getStream());
  394. // 释放ssrc
  395. mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  396. streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  397. SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult(new CmdSendFailEvent(null));
  398. eventResult.msg = "命令发送失败";
  399. errorEvent.response(eventResult);
  400. }
  401. // }
  402. }
  403. @Override
  404. public void play(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, Device device, String channelId,
  405. ZlmHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent,
  406. InviteTimeOutCallback timeoutCallback, String uuid) {
  407. String streamId = null;
  408. if (mediaServerItem.isRtpEnable()) {
  409. streamId = String.format("%s_%s", device.getDeviceId(), channelId);
  410. }
  411. if (ssrcInfo == null) {
  412. ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId, device.isSsrcCheck(), false);
  413. }
  414. logger.info("[点播开始] deviceId: {}, channelId: {},收流端口: {}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, ssrcInfo.getPort(), device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck());
  415. // 超时处理
  416. String timeOutTaskKey = UUID.randomUUID().toString();
  417. SSRCInfo finalSsrcInfo = ssrcInfo;
  418. System.out.println("设置超时任务: " + timeOutTaskKey);
  419. dynamicTask.startDelay(timeOutTaskKey, () -> {
  420. logger.info("[点播超时] 收流超时 deviceId: {}, channelId: {},端口:{}, SSRC: {}", device.getDeviceId(), channelId, finalSsrcInfo.getPort(), finalSsrcInfo.getSsrc());
  421. timeoutCallback.run(1, "收流超时");
  422. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  423. try {
  424. cmder.streamByeCmd(device, channelId, finalSsrcInfo.getStream(), null);
  425. } catch (InvalidArgumentException | ParseException | SipException e) {
  426. logger.error("[点播超时], 发送BYE失败 {}", e.getMessage());
  427. } catch (SsrcTransactionNotFoundException e) {
  428. timeoutCallback.run(0, "点播超时");
  429. mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  430. mediaServerService.closeRTPServer(mediaServerItem, finalSsrcInfo.getStream());
  431. streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  432. }
  433. }, userSetting.getPlayTimeout());
  434. final String ssrc = ssrcInfo.getSsrc();
  435. final String stream = ssrcInfo.getStream();
  436. //端口获取失败的ssrcInfo 没有必要发送点播指令
  437. if (ssrcInfo.getPort() <= 0) {
  438. logger.info("[点播端口分配异常],deviceId={},channelId={},ssrcInfo={}", device.getDeviceId(), channelId, ssrcInfo);
  439. return;
  440. }
  441. try {
  442. cmder.playStreamCmd(mediaServerItem, ssrcInfo, device, channelId, (MediaServerItem mediaServerItemInuse, JSONObject response) -> {
  443. logger.info("收到订阅消息: " + response.toJSONString());
  444. System.out.println("停止超时任务: " + timeOutTaskKey);
  445. dynamicTask.stop(timeOutTaskKey);
  446. // hook响应
  447. onPublishHandlerForPlay(mediaServerItemInuse, response, device.getDeviceId(), channelId, uuid);
  448. hookEvent.response(mediaServerItemInuse, response);
  449. logger.info("[点播成功] deviceId: {}, channelId: {}", device.getDeviceId(), channelId);
  450. }, (event) -> {
  451. ResponseEvent responseEvent = (ResponseEvent) event.event;
  452. String contentString = new String(responseEvent.getResponse().getRawContent());
  453. // 获取ssrc
  454. int ssrcIndex = contentString.indexOf("y=");
  455. // 检查是否有y字段
  456. if (ssrcIndex >= 0) {
  457. //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容
  458. String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
  459. // 查询到ssrc不一致且开启了ssrc校验则需要针对处理
  460. if (ssrc.equals(ssrcInResponse)) {
  461. return;
  462. }
  463. logger.info("[点播消息] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse);
  464. if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
  465. logger.info("[点播消息] SSRC修正 {}->{}", ssrc, ssrcInResponse);
  466. if (!mediaServerItem.getSsrcConfig().checkSsrc(ssrcInResponse)) {
  467. // ssrc 不可用
  468. // 释放ssrc
  469. mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  470. streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  471. event.msg = "下级自定义了ssrc,但是此ssrc不可用";
  472. event.statusCode = 400;
  473. errorEvent.response(event);
  474. return;
  475. }
  476. // 单端口模式streamId也有变化,需要重新设置监听
  477. if (!mediaServerItem.isRtpEnable()) {
  478. // 添加订阅
  479. HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp", stream, true, "rtsp", mediaServerItem.getId());
  480. subscribe.removeSubscribe(hookSubscribe);
  481. hookSubscribe.getContent().put("stream", String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase());
  482. subscribe.addSubscribe(hookSubscribe, (MediaServerItem mediaServerItemInUse, JSONObject response) -> {
  483. logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + response.toJSONString());
  484. dynamicTask.stop(timeOutTaskKey);
  485. // hook响应
  486. onPublishHandlerForPlay(mediaServerItemInUse, response, device.getDeviceId(), channelId, uuid);
  487. hookEvent.response(mediaServerItemInUse, response);
  488. });
  489. }
  490. // 关闭rtp server
  491. mediaServerService.closeRTPServer(mediaServerItem, finalSsrcInfo.getStream());
  492. // 重新开启ssrc server
  493. mediaServerService.openRTPServer(mediaServerItem, finalSsrcInfo.getStream(), ssrcInResponse, device.isSsrcCheck(), false, finalSsrcInfo.getPort());
  494. }
  495. }
  496. }, (event) -> {
  497. dynamicTask.stop(timeOutTaskKey);
  498. mediaServerService.closeRTPServer(mediaServerItem, finalSsrcInfo.getStream());
  499. // 释放ssrc
  500. mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  501. streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  502. errorEvent.response(event);
  503. });
  504. } catch (InvalidArgumentException | SipException | ParseException e) {
  505. logger.error("[命令发送失败] 点播消息: {}", e.getMessage());
  506. dynamicTask.stop(timeOutTaskKey);
  507. mediaServerService.closeRTPServer(mediaServerItem, finalSsrcInfo.getStream());
  508. // 释放ssrc
  509. mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc());
  510. streamSession.remove(device.getDeviceId(), channelId, finalSsrcInfo.getStream());
  511. SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult(new CmdSendFailEvent(null));
  512. eventResult.msg = "命令发送失败";
  513. errorEvent.response(eventResult);
  514. }
  515. }
  516. @Override
  517. public void onPublishHandlerForPlay(MediaServerItem mediaServerItem, JSONObject response, String deviceId, String channelId, String uuid) {
  518. RequestMessage msg = new RequestMessage();
  519. if (uuid != null) {
  520. msg.setId(uuid);
  521. }
  522. msg.setKey(DeferredResultHolder.CALLBACK_CMD_PLAY + deviceId + channelId);
  523. StreamInfo streamInfo = onPublishHandler(mediaServerItem, response, deviceId, channelId);
  524. if (streamInfo != null) {
  525. DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);
  526. if (deviceChannel != null) {
  527. deviceChannel.setStreamId(streamInfo.getStream());
  528. storager.startPlay(deviceId, channelId, streamInfo.getStream());
  529. }
  530. redisCatchStorage.startPlay(streamInfo);
  531. WVPResult wvpResult = new WVPResult();
  532. wvpResult.setCode(ErrorCode.SUCCESS.getCode());
  533. wvpResult.setMsg(ErrorCode.SUCCESS.getMsg());
  534. wvpResult.setData(streamInfo);
  535. msg.setData(wvpResult);
  536. resultHolder.invokeAllResult(msg);
  537. } else {
  538. logger.warn("设备预览API调用失败!");
  539. msg.setData(WVPResult.fail(ErrorCode.ERROR100.getCode(), "设备预览API调用失败!"));
  540. resultHolder.invokeAllResult(msg);
  541. }
  542. }
  543. @Override
  544. public MediaServerItem getNewMediaServerItem(Device device) {
  545. if (device == null) {
  546. return null;
  547. }
  548. MediaServerItem mediaServerItem;
  549. if (ObjectUtils.isEmpty(device.getMediaServerId()) || "auto".equals(device.getMediaServerId())) {
  550. mediaServerItem = mediaServerService.getMediaServerForMinimumLoad();
  551. } else {
  552. mediaServerItem = mediaServerService.getOne(mediaServerId);
  553. } else {
  554. mediaServerItem = mediaServerService.getOne(device.getMediaServerId());
  555. }
  556. if (mediaServerItem == null) {
  557. logger.warn("点播时未找到可使用的ZLM...");
  558. }
  559. return mediaServerItem;
  560. }
  561. @Override
  562. public DeferredResult<WVPResult<StreamInfo>> playBack(String deviceId, String channelId, String startTime,
  563. String endTime, InviteStreamCallback inviteStreamCallback,
  564. PlayBackCallback callback) {
  565. Device device = storager.queryVideoDevice(deviceId);
  566. if (device == null) {
  567. return null;
  568. }
  569. MediaServerItem newMediaServerItem = getNewMediaServerItem(device);
  570. SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, true, true);
  571. return playBack(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, inviteStreamCallback, callback);
  572. }
  573. @Override
  574. public DeferredResult<WVPResult<StreamInfo>> playBack(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo,
  575. String deviceId, String channelId, String startTime,
  576. String endTime, InviteStreamCallback infoCallBack,
  577. PlayBackCallback playBackCallback) {
  578. if (mediaServerItem == null || ssrcInfo == null) {
  579. return null;
  580. }
  581. String uuid = UUID.randomUUID().toString();
  582. String key = DeferredResultHolder.CALLBACK_CMD_PLAYBACK + deviceId + channelId;
  583. Device device = storager.queryVideoDevice(deviceId);
  584. if (device == null) {
  585. throw new ControllerException(ErrorCode.ERROR100.getCode(), "设备: " + deviceId + "不存在");
  586. }
  587. DeferredResult<WVPResult<StreamInfo>> result = new DeferredResult<>(30000L);
  588. resultHolder.put(DeferredResultHolder.CALLBACK_CMD_PLAYBACK + deviceId + channelId, uuid, result);
  589. RequestMessage requestMessage = new RequestMessage();
  590. requestMessage.setId(uuid);
  591. requestMessage.setKey(key);
  592. PlayBackResult<RequestMessage> playBackResult = new PlayBackResult<>();
  593. String playBackTimeOutTaskKey = UUID.randomUUID().toString();
  594. dynamicTask.startDelay(playBackTimeOutTaskKey, () -> {
  595. logger.warn(String.format("设备回放超时,deviceId:%s ,channelId:%s", deviceId, channelId));
  596. playBackResult.setCode(ErrorCode.ERROR100.getCode());
  597. playBackResult.setMsg("回放超时");
  598. playBackResult.setData(requestMessage);
  599. try {
  600. cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null);
  601. } catch (InvalidArgumentException | ParseException | SipException e) {
  602. logger.error("[录像流]回放超时 发送BYE失败 {}", e.getMessage());
  603. } catch (SsrcTransactionNotFoundException e) {
  604. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  605. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  606. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  607. streamSession.remove(deviceId, channelId, ssrcInfo.getStream());
  608. }
  609. // 回复之前所有的点播请求
  610. playBackCallback.call(playBackResult);
  611. result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), "回放超时"));
  612. resultHolder.exist(DeferredResultHolder.CALLBACK_CMD_PLAYBACK + deviceId + channelId, uuid);
  613. }, userSetting.getPlayTimeout());
  614. SipSubscribe.Event errorEvent = event -> {
  615. dynamicTask.stop(playBackTimeOutTaskKey);
  616. requestMessage.setData(WVPResult.fail(ErrorCode.ERROR100.getCode(), String.format("回放失败, 错误码: %s, %s", event.statusCode, event.msg)));
  617. playBackResult.setCode(ErrorCode.ERROR100.getCode());
  618. playBackResult.setMsg(String.format("回放失败, 错误码: %s, %s", event.statusCode, event.msg));
  619. playBackResult.setData(requestMessage);
  620. playBackResult.setEvent(event);
  621. playBackCallback.call(playBackResult);
  622. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  623. };
  624. InviteStreamCallback hookEvent = (InviteStreamInfo inviteStreamInfo) -> {
  625. logger.info("收到回放订阅消息: " + inviteStreamInfo.getResponse().toJSONString());
  626. dynamicTask.stop(playBackTimeOutTaskKey);
  627. StreamInfo streamInfo = onPublishHandler(inviteStreamInfo.getMediaServerItem(), inviteStreamInfo.getResponse(), deviceId, channelId);
  628. if (streamInfo == null) {
  629. logger.warn("设备回放API调用失败!");
  630. playBackResult.setCode(ErrorCode.ERROR100.getCode());
  631. playBackResult.setMsg("设备回放API调用失败!");
  632. playBackCallback.call(playBackResult);
  633. return;
  634. }
  635. redisCatchStorage.startPlayback(streamInfo, inviteStreamInfo.getCallId());
  636. WVPResult<StreamInfo> success = WVPResult.success(streamInfo);
  637. requestMessage.setData(success);
  638. playBackResult.setCode(ErrorCode.SUCCESS.getCode());
  639. playBackResult.setMsg(ErrorCode.SUCCESS.getMsg());
  640. playBackResult.setData(requestMessage);
  641. playBackResult.setMediaServerItem(inviteStreamInfo.getMediaServerItem());
  642. playBackResult.setResponse(inviteStreamInfo.getResponse());
  643. playBackCallback.call(playBackResult);
  644. };
  645. try {
  646. cmder.playbackStreamCmd(mediaServerItem, ssrcInfo, device, channelId, startTime, endTime, infoCallBack,
  647. hookEvent, eventResult -> {
  648. if (eventResult.type == SipSubscribe.EventResultType.response) {
  649. ResponseEvent responseEvent = (ResponseEvent) eventResult.event;
  650. String contentString = new String(responseEvent.getResponse().getRawContent());
  651. // 获取ssrc
  652. int ssrcIndex = contentString.indexOf("y=");
  653. // 检查是否有y字段
  654. if (ssrcIndex >= 0) {
  655. //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容
  656. String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
  657. // 查询到ssrc不一致且开启了ssrc校验则需要针对处理
  658. if (ssrcInfo.getSsrc().equals(ssrcInResponse)) {
  659. return;
  660. }
  661. logger.info("[回放消息] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse);
  662. if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
  663. logger.info("[回放消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
  664. if (!mediaServerItem.getSsrcConfig().checkSsrc(ssrcInResponse)) {
  665. // ssrc 不可用
  666. // 释放ssrc
  667. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  668. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  669. eventResult.msg = "下级自定义了ssrc,但是此ssrc不可用";
  670. eventResult.statusCode = 400;
  671. errorEvent.response(eventResult);
  672. return;
  673. }
  674. // 单端口模式streamId也有变化,需要重新设置监听
  675. if (!mediaServerItem.isRtpEnable()) {
  676. // 添加订阅
  677. HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp", ssrcInfo.getStream(), true, "rtsp", mediaServerItem.getId());
  678. subscribe.removeSubscribe(hookSubscribe);
  679. hookSubscribe.getContent().put("stream", String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase());
  680. subscribe.addSubscribe(hookSubscribe, (MediaServerItem mediaServerItemInUse, JSONObject response) -> {
  681. logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + response.toJSONString());
  682. dynamicTask.stop(playBackTimeOutTaskKey);
  683. // hook响应
  684. onPublishHandlerForPlay(mediaServerItemInUse, response, device.getDeviceId(), channelId, uuid);
  685. hookEvent.call(new InviteStreamInfo(mediaServerItem, null, eventResult.callId, "rtp", ssrcInfo.getStream()));
  686. });
  687. }
  688. // 关闭rtp server
  689. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  690. // 重新开启ssrc server
  691. mediaServerService.openRTPServer(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse, device.isSsrcCheck(), true, ssrcInfo.getPort());
  692. }
  693. }
  694. }
  695. }, errorEvent);
  696. } catch (InvalidArgumentException | SipException | ParseException e) {
  697. logger.error("[命令发送失败] 回放: {}", e.getMessage());
  698. SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult(new CmdSendFailEvent(null));
  699. eventResult.msg = "命令发送失败";
  700. errorEvent.response(eventResult);
  701. }
  702. return result;
  703. }
  704. @Override
  705. public DeferredResult<WVPResult<StreamInfo>> download(String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, InviteStreamCallback infoCallBack, PlayBackCallback hookCallBack) {
  706. Device device = storager.queryVideoDevice(deviceId);
  707. if (device == null) {
  708. return null;
  709. }
  710. MediaServerItem newMediaServerItem = getNewMediaServerItem(device);
  711. SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, true, true);
  712. return download(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, downloadSpeed, infoCallBack, hookCallBack);
  713. }
  714. @Override
  715. public DeferredResult<WVPResult<StreamInfo>> download(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, InviteStreamCallback infoCallBack, PlayBackCallback hookCallBack) {
  716. if (mediaServerItem == null || ssrcInfo == null) {
  717. return null;
  718. }
  719. String uuid = UUID.randomUUID().toString();
  720. String key = DeferredResultHolder.CALLBACK_CMD_DOWNLOAD + deviceId + channelId;
  721. DeferredResult<WVPResult<StreamInfo>> result = new DeferredResult<>(30000L);
  722. Device device = storager.queryVideoDevice(deviceId);
  723. if (device == null) {
  724. throw new ControllerException(ErrorCode.ERROR400.getCode(), "设备:" + deviceId + "不存在");
  725. }
  726. resultHolder.put(key, uuid, result);
  727. RequestMessage requestMessage = new RequestMessage();
  728. requestMessage.setId(uuid);
  729. requestMessage.setKey(key);
  730. WVPResult<StreamInfo> wvpResult = new WVPResult<>();
  731. requestMessage.setData(wvpResult);
  732. PlayBackResult<RequestMessage> downloadResult = new PlayBackResult<>();
  733. downloadResult.setData(requestMessage);
  734. String downLoadTimeOutTaskKey = UUID.randomUUID().toString();
  735. dynamicTask.startDelay(downLoadTimeOutTaskKey, () -> {
  736. logger.warn(String.format("录像下载请求超时,deviceId:%s ,channelId:%s", deviceId, channelId));
  737. wvpResult.setCode(ErrorCode.ERROR100.getCode());
  738. wvpResult.setMsg("录像下载请求超时");
  739. downloadResult.setCode(ErrorCode.ERROR100.getCode());
  740. downloadResult.setMsg("录像下载请求超时");
  741. hookCallBack.call(downloadResult);
  742. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  743. try {
  744. cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null);
  745. } catch (InvalidArgumentException | ParseException | SipException e) {
  746. logger.error("[录像流]录像下载请求超时, 发送BYE失败 {}", e.getMessage());
  747. } catch (SsrcTransactionNotFoundException e) {
  748. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  749. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  750. streamSession.remove(deviceId, channelId, ssrcInfo.getStream());
  751. }
  752. // 回复之前所有的点播请求
  753. hookCallBack.call(downloadResult);
  754. }, userSetting.getPlayTimeout());
  755. SipSubscribe.Event errorEvent = event -> {
  756. dynamicTask.stop(downLoadTimeOutTaskKey);
  757. downloadResult.setCode(ErrorCode.ERROR100.getCode());
  758. downloadResult.setMsg(String.format("录像下载失败, 错误码: %s, %s", event.statusCode, event.msg));
  759. wvpResult.setCode(ErrorCode.ERROR100.getCode());
  760. wvpResult.setMsg(String.format("录像下载失败, 错误码: %s, %s", event.statusCode, event.msg));
  761. downloadResult.setEvent(event);
  762. hookCallBack.call(downloadResult);
  763. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  764. };
  765. try {
  766. cmder.downloadStreamCmd(mediaServerItem, ssrcInfo, device, channelId, startTime, endTime, downloadSpeed, infoCallBack,
  767. inviteStreamInfo -> {
  768. logger.info("收到订阅消息: " + inviteStreamInfo.getResponse().toJSONString());
  769. dynamicTask.stop(downLoadTimeOutTaskKey);
  770. StreamInfo streamInfo = onPublishHandler(inviteStreamInfo.getMediaServerItem(), inviteStreamInfo.getResponse(), deviceId, channelId);
  771. streamInfo.setStartTime(startTime);
  772. streamInfo.setEndTime(endTime);
  773. redisCatchStorage.startDownload(streamInfo, inviteStreamInfo.getCallId());
  774. wvpResult.setCode(ErrorCode.SUCCESS.getCode());
  775. wvpResult.setMsg(ErrorCode.SUCCESS.getMsg());
  776. wvpResult.setData(streamInfo);
  777. downloadResult.setCode(ErrorCode.SUCCESS.getCode());
  778. downloadResult.setMsg(ErrorCode.SUCCESS.getMsg());
  779. downloadResult.setMediaServerItem(inviteStreamInfo.getMediaServerItem());
  780. downloadResult.setResponse(inviteStreamInfo.getResponse());
  781. hookCallBack.call(downloadResult);
  782. }, errorEvent);
  783. } catch (InvalidArgumentException | SipException | ParseException e) {
  784. logger.error("[命令发送失败] 录像下载: {}", e.getMessage());
  785. SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult(new CmdSendFailEvent(null));
  786. eventResult.msg = "命令发送失败";
  787. errorEvent.response(eventResult);
  788. }
  789. return result;
  790. }
  791. @Override
  792. public StreamInfo getDownLoadInfo(String deviceId, String channelId, String stream) {
  793. StreamInfo streamInfo = redisCatchStorage.queryDownload(deviceId, channelId, stream, null);
  794. if (streamInfo != null) {
  795. if (streamInfo.getProgress() == 1) {
  796. return streamInfo;
  797. }
  798. // 获取当前已下载时长
  799. String mediaServerId = streamInfo.getMediaServerId();
  800. MediaServerItem mediaServerItem = mediaServerService.getOne(mediaServerId);
  801. if (mediaServerItem == null) {
  802. logger.warn("查询录像信息时发现节点已离线");
  803. return null;
  804. }
  805. if (mediaServerItem.getRecordAssistPort() > 0) {
  806. JSONObject jsonObject = assistRESTfulUtils.fileDuration(mediaServerItem, streamInfo.getApp(), streamInfo.getStream(), null);
  807. if (jsonObject != null && jsonObject.getInteger("code") == 0) {
  808. long duration = jsonObject.getLong("data");
  809. if (duration == 0) {
  810. streamInfo.setProgress(0);
  811. } else {
  812. String startTime = streamInfo.getStartTime();
  813. String endTime = streamInfo.getEndTime();
  814. long start = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime);
  815. long end = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime);
  816. BigDecimal currentCount = new BigDecimal(duration / 1000);
  817. BigDecimal totalCount = new BigDecimal(end - start);
  818. BigDecimal divide = currentCount.divide(totalCount, 2, RoundingMode.HALF_UP);
  819. double process = divide.doubleValue();
  820. streamInfo.setProgress(process);
  821. }
  822. }
  823. }
  824. }
  825. return streamInfo;
  826. }
  827. @Override
  828. public void onPublishHandlerForDownload(InviteStreamInfo inviteStreamInfo, String deviceId, String channelId, String uuid) {
  829. RequestMessage msg = new RequestMessage();
  830. msg.setKey(DeferredResultHolder.CALLBACK_CMD_DOWNLOAD + deviceId + channelId);
  831. msg.setId(uuid);
  832. StreamInfo streamInfo = onPublishHandler(inviteStreamInfo.getMediaServerItem(), inviteStreamInfo.getResponse(), deviceId, channelId);
  833. if (streamInfo != null) {
  834. redisCatchStorage.startDownload(streamInfo, inviteStreamInfo.getCallId());
  835. msg.setData(JSON.toJSONString(streamInfo));
  836. resultHolder.invokeResult(msg);
  837. } else {
  838. logger.warn("设备预览API调用失败!");
  839. msg.setData(WVPResult.fail(ErrorCode.ERROR100.getCode(), "设备预览API调用失败!"));
  840. resultHolder.invokeResult(msg);
  841. }
  842. }
  843. public StreamInfo onPublishHandler(MediaServerItem mediaServerItem, JSONObject resonse, String deviceId, String channelId) {
  844. String streamId = resonse.getString("stream");
  845. JSONArray tracks = resonse.getJSONArray("tracks");
  846. StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(mediaServerItem, "rtp", streamId, tracks, null);
  847. streamInfo.setDeviceID(deviceId);
  848. streamInfo.setChannelId(channelId);
  849. return streamInfo;
  850. }
  851. @Override
  852. public void zlmServerOffline(String mediaServerId) {
  853. // 处理正在向上推流的上级平台
  854. List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServer(null);
  855. if (sendRtpItems.size() > 0) {
  856. for (SendRtpItem sendRtpItem : sendRtpItems) {
  857. if (sendRtpItem.getMediaServerId().equals(mediaServerId)) {
  858. ParentPlatform platform = storager.queryParentPlatByServerGBId(sendRtpItem.getPlatformId());
  859. try {
  860. sipCommanderFroPlatform.streamByeCmd(platform, sendRtpItem.getCallId());
  861. } catch (SipException | InvalidArgumentException | ParseException e) {
  862. logger.error("[命令发送失败] 国标级联 发送BYE: {}", e.getMessage());
  863. }
  864. }
  865. }
  866. }
  867. // 处理正在观看的国标设备
  868. List<SsrcTransaction> allSsrc = streamSession.getAllSsrc();
  869. if (allSsrc.size() > 0) {
  870. for (SsrcTransaction ssrcTransaction : allSsrc) {
  871. if (ssrcTransaction.getMediaServerId().equals(mediaServerId)) {
  872. Device device = deviceService.queryDevice(ssrcTransaction.getDeviceId());
  873. if (device == null) {
  874. continue;
  875. }
  876. try {
  877. cmder.streamByeCmd(device, ssrcTransaction.getChannelId(),
  878. ssrcTransaction.getStream(), null);
  879. } catch (InvalidArgumentException | ParseException | SipException |
  880. SsrcTransactionNotFoundException e) {
  881. logger.error("[zlm离线]为正在使用此zlm的设备, 发送BYE失败 {}", e.getMessage());
  882. }
  883. }
  884. }
  885. }
  886. }
  887. @Override
  888. public AudioBroadcastResult audioBroadcast(Device device, String channelId) {
  889. if (device == null || channelId == null) {
  890. return null;
  891. }
  892. logger.info("[语音喊话] device: {}, channel: {}", device.getDeviceId(), channelId);
  893. DeviceChannel deviceChannel = storager.queryChannel(device.getDeviceId(), channelId);
  894. if (deviceChannel == null) {
  895. logger.warn("开启语音广播的时候未找到通道: {}", channelId);
  896. return null;
  897. }
  898. MediaServerItem mediaServerItem = mediaServerService.getMediaServerForMinimumLoad();
  899. // String app = "broadcast";
  900. // TODO 从sip user agent中判断是什么品牌设备,大华默认使用talk模式,其他使用broadcast模式
  901. String app = "talk";
  902. String stream = device.getDeviceId() + "_" + channelId;
  903. StreamInfo broadcast = mediaService.getStreamInfoByAppAndStream(mediaServerItem, "broadcast", stream, null, null, null, false);
  904. AudioBroadcastResult audioBroadcastResult = new AudioBroadcastResult();
  905. audioBroadcastResult.setApp(app);
  906. audioBroadcastResult.setStream(stream);
  907. audioBroadcastResult.setStreamInfo(mediaService.getStreamInfoByAppAndStream(mediaServerItem, app, stream, null, null, null,false));
  908. audioBroadcastResult.setCodec("G.711");
  909. return audioBroadcastResult;
  910. }
  911. @Override
  912. public void audioBroadcastCmd(Device device, String channelId, int timeout, AudioBroadcastEvent event) throws InvalidArgumentException, ParseException, SipException {
  913. if (device == null || channelId == null) {
  914. return;
  915. }
  916. logger.info("[语音喊话] device: {}, channel: {}", device.getDeviceId(), channelId);
  917. DeviceChannel deviceChannel = storager.queryChannel(device.getDeviceId(), channelId);
  918. if (deviceChannel == null) {
  919. logger.warn("开启语音广播的时候未找到通道: {}", channelId);
  920. event.call("开启语音广播的时候未找到通道");
  921. return;
  922. }
  923. // 查询通道使用状态
  924. if (audioBroadcastManager.exit(device.getDeviceId(), channelId)) {
  925. SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(device.getDeviceId(), channelId, null, null);
  926. if (sendRtpItem != null && sendRtpItem.isOnlyAudio()) {
  927. // 查询流是否存在,不存在则认为是异常状态
  928. MediaServerItem mediaServerItem = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  929. Boolean streamReady = zlmrtpServerFactory.isStreamReady(mediaServerItem, sendRtpItem.getApp(), sendRtpItem.getStreamId());
  930. if (streamReady) {
  931. logger.warn("语音广播已经开启: {}", channelId);
  932. event.call("语音广播已经开启");
  933. return;
  934. } else {
  935. audioBroadcastManager.del(deviceChannel.getDeviceId(), channelId);
  936. redisCatchStorage.deleteSendRTPServer(device.getDeviceId(), channelId, sendRtpItem.getCallId(), sendRtpItem.getStreamId());
  937. }
  938. }
  939. }
  940. // 发送通知
  941. cmder.audioBroadcastCmd(device, channelId, eventResultForOk -> {
  942. // 发送成功
  943. AudioBroadcastCatch audioBroadcastCatch = new AudioBroadcastCatch(device.getDeviceId(), channelId, AudioBroadcastCatchStatus.Ready);
  944. audioBroadcastManager.add(audioBroadcastCatch);
  945. }, eventResultForError -> {
  946. // 发送失败
  947. logger.error("语音广播发送失败: {}:{}", channelId, eventResultForError.msg);
  948. event.call("语音广播发送失败");
  949. stopAudioBroadcast(device.getDeviceId(), channelId);
  950. });
  951. }
  952. @Override
  953. public void stopAudioBroadcast(String deviceId, String channelId) {
  954. AudioBroadcastCatch audioBroadcastCatch = audioBroadcastManager.get(deviceId, channelId);
  955. if (audioBroadcastCatch != null) {
  956. Device device = deviceService.queryDevice(deviceId);
  957. if (device == null) {
  958. return;
  959. }
  960. // if (audioBroadcastCatch.getStatus() == AudioBroadcastCatchStatus.Ok) {
  961. // cmder.streamByeCmd(device, audioBroadcastCatch.getChannelId(), null, audioBroadcastCatch.getSipTransactionInfo().getCallId());
  962. // }
  963. SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(deviceId, audioBroadcastCatch.getChannelId(), null, null);
  964. if (sendRtpItem != null) {
  965. redisCatchStorage.deleteSendRTPServer(deviceId, sendRtpItem.getChannelId(), null, null);
  966. MediaServerItem mediaInfo = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  967. Map<String, Object> param = new HashMap<>();
  968. param.put("vhost", "__defaultVhost__");
  969. param.put("app", sendRtpItem.getApp());
  970. param.put("stream", sendRtpItem.getStreamId());
  971. zlmresTfulUtils.stopSendRtp(mediaInfo, param);
  972. }
  973. audioBroadcastManager.del(deviceId, channelId);
  974. }
  975. }
  976. @Override
  977. public void zlmServerOnline(String mediaServerId) {
  978. // TODO 查找之前的点播,流如果不存在则给下级发送bye
  979. // MediaServerItem mediaServerItem = mediaServerService.getOne(mediaServerId);
  980. // zlmresTfulUtils.getMediaList(mediaServerItem, (mediaList ->{
  981. // Integer code = mediaList.getInteger("code");
  982. // if (code == 0) {
  983. // JSONArray data = mediaList.getJSONArray("data");
  984. // if (data == null || data.size() == 0) {
  985. // zlmServerOffline(mediaServerId);
  986. // }else {
  987. // Map<String, JSONObject> mediaListMap = new HashMap<>();
  988. // for (int i = 0; i < data.size(); i++) {
  989. // JSONObject json = data.getJSONObject(i);
  990. // String app = json.getString("app");
  991. // if ("rtp".equals(app)) {
  992. // String stream = json.getString("stream");
  993. // if (mediaListMap.get(stream) != null) {
  994. // continue;
  995. // }
  996. // mediaListMap.put(stream, json);
  997. // // 处理正在观看的国标设备
  998. // List<SsrcTransaction> ssrcTransactions = streamSession.getSsrcTransactionForAll(null, null, null, stream);
  999. // if (ssrcTransactions.size() > 0) {
  1000. // for (SsrcTransaction ssrcTransaction : ssrcTransactions) {
  1001. // if(ssrcTransaction.getMediaServerId().equals(mediaServerId)) {
  1002. // cmder.streamByeCmd(ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId(),
  1003. // ssrcTransaction.getStream(), null);
  1004. // }
  1005. // }
  1006. // }
  1007. // }
  1008. // }
  1009. // if (mediaListMap.size() > 0 ) {
  1010. // // 处理正在向上推流的上级平台
  1011. // List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServer(null);
  1012. // if (sendRtpItems.size() > 0) {
  1013. // for (SendRtpItem sendRtpItem : sendRtpItems) {
  1014. // if (sendRtpItem.getMediaServerId().equals(mediaServerId)) {
  1015. // if (mediaListMap.get(sendRtpItem.getStreamId()) == null) {
  1016. // ParentPlatform platform = storager.queryPlatformByServerGBId(sendRtpItem.getPlatformId());
  1017. // sipCommanderFroPlatform.streamByeCmd(platform, sendRtpItem.getCallId());
  1018. // }
  1019. // }
  1020. // }
  1021. // }
  1022. // }
  1023. // }
  1024. // }
  1025. // }));
  1026. }
  1027. @Override
  1028. public void pauseRtp(String streamId) throws ServiceException, InvalidArgumentException, ParseException, SipException {
  1029. String key = redisCatchStorage.queryPlaybackForKey(null, null, streamId, null);
  1030. StreamInfo streamInfo = redisCatchStorage.queryPlayback(null, null, streamId, null);
  1031. if (null == streamInfo) {
  1032. logger.warn("streamId不存在!");
  1033. throw new ServiceException("streamId不存在");
  1034. }
  1035. streamInfo.setPause(true);
  1036. RedisUtil.set(key, streamInfo);
  1037. MediaServerItem mediaServerItem = mediaServerService.getOne(streamInfo.getMediaServerId());
  1038. if (null == mediaServerItem) {
  1039. logger.warn("mediaServer 不存在!");
  1040. throw new ServiceException("mediaServer不存在");
  1041. }
  1042. // zlm 暂停RTP超时检查
  1043. JSONObject jsonObject = zlmresTfulUtils.pauseRtpCheck(mediaServerItem, streamId);
  1044. if (jsonObject == null || jsonObject.getInteger("code") != 0) {
  1045. throw new ServiceException("暂停RTP接收失败");
  1046. }
  1047. Device device = storager.queryVideoDevice(streamInfo.getDeviceID());
  1048. cmder.playPauseCmd(device, streamInfo);
  1049. }
  1050. @Override
  1051. public void resumeRtp(String streamId) throws ServiceException, InvalidArgumentException, ParseException, SipException {
  1052. String key = redisCatchStorage.queryPlaybackForKey(null, null, streamId, null);
  1053. StreamInfo streamInfo = redisCatchStorage.queryPlayback(null, null, streamId, null);
  1054. if (null == streamInfo) {
  1055. logger.warn("streamId不存在!");
  1056. throw new ServiceException("streamId不存在");
  1057. }
  1058. streamInfo.setPause(false);
  1059. RedisUtil.set(key, streamInfo);
  1060. MediaServerItem mediaServerItem = mediaServerService.getOne(streamInfo.getMediaServerId());
  1061. if (null == mediaServerItem) {
  1062. logger.warn("mediaServer 不存在!");
  1063. throw new ServiceException("mediaServer不存在");
  1064. }
  1065. // zlm 暂停RTP超时检查
  1066. JSONObject jsonObject = zlmresTfulUtils.resumeRtpCheck(mediaServerItem, streamId);
  1067. if (jsonObject == null || jsonObject.getInteger("code") != 0) {
  1068. throw new ServiceException("继续RTP接收失败");
  1069. }
  1070. Device device = storager.queryVideoDevice(streamInfo.getDeviceID());
  1071. cmder.playResumeCmd(device, streamInfo);
  1072. }
  1073. }