PlayServiceImpl.java 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.genersoft.iot.vmp.common.InviteInfo;
  4. import com.genersoft.iot.vmp.common.InviteSessionStatus;
  5. import com.genersoft.iot.vmp.common.InviteSessionType;
  6. import com.genersoft.iot.vmp.common.StreamInfo;
  7. import com.genersoft.iot.vmp.conf.DynamicTask;
  8. import com.genersoft.iot.vmp.conf.SipConfig;
  9. import com.genersoft.iot.vmp.conf.UserSetting;
  10. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  11. import com.genersoft.iot.vmp.conf.exception.ServiceException;
  12. import com.genersoft.iot.vmp.conf.exception.SsrcTransactionNotFoundException;
  13. import com.genersoft.iot.vmp.gb28181.bean.*;
  14. import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
  15. import com.genersoft.iot.vmp.gb28181.session.AudioBroadcastManager;
  16. import com.genersoft.iot.vmp.gb28181.session.SSRCFactory;
  17. import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
  18. import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
  19. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  20. import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
  21. import com.genersoft.iot.vmp.media.zlm.*;
  22. import com.genersoft.iot.vmp.media.zlm.AssistRESTfulUtils;
  23. import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
  24. import com.genersoft.iot.vmp.media.zlm.ZLMServerFactory;
  25. import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe;
  26. import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory;
  27. import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForStreamChange;
  28. import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
  29. import com.genersoft.iot.vmp.media.zlm.dto.hook.HookParam;
  30. import com.genersoft.iot.vmp.media.zlm.dto.hook.OnStreamChangedHookParam;
  31. import com.genersoft.iot.vmp.service.*;
  32. import com.genersoft.iot.vmp.service.bean.ErrorCallback;
  33. import com.genersoft.iot.vmp.service.bean.InviteErrorCode;
  34. import com.genersoft.iot.vmp.service.bean.RequestPushStreamMsg;
  35. import com.genersoft.iot.vmp.service.bean.SSRCInfo;
  36. import com.genersoft.iot.vmp.service.redisMsg.RedisGbPlayMsgListener;
  37. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  38. import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
  39. import com.genersoft.iot.vmp.utils.DateUtil;
  40. import com.genersoft.iot.vmp.vmanager.bean.AudioBroadcastResult;
  41. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  42. import com.genersoft.iot.vmp.vmanager.bean.StreamContent;
  43. import com.genersoft.iot.vmp.vmanager.gb28181.play.bean.AudioBroadcastEvent;
  44. import gov.nist.javax.sip.message.SIPResponse;
  45. import org.slf4j.Logger;
  46. import org.slf4j.LoggerFactory;
  47. import org.springframework.beans.factory.annotation.Autowired;
  48. import org.springframework.beans.factory.annotation.Qualifier;
  49. import org.springframework.data.redis.core.RedisTemplate;
  50. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  51. import org.springframework.stereotype.Service;
  52. import org.springframework.util.ObjectUtils;
  53. import javax.sdp.*;
  54. import javax.sip.InvalidArgumentException;
  55. import javax.sip.ResponseEvent;
  56. import javax.sip.SipException;
  57. import javax.sip.header.CallIdHeader;
  58. import java.io.File;
  59. import java.math.BigDecimal;
  60. import java.math.RoundingMode;
  61. import java.text.ParseException;
  62. import java.util.*;
  63. @SuppressWarnings(value = {"rawtypes", "unchecked"})
  64. @Service
  65. public class PlayServiceImpl implements IPlayService {
  66. private final static Logger logger = LoggerFactory.getLogger(PlayServiceImpl.class);
  67. @Autowired
  68. private IVideoManagerStorage storager;
  69. @Autowired
  70. private SIPCommander cmder;
  71. @Autowired
  72. private AudioBroadcastManager audioBroadcastManager;
  73. @Autowired
  74. private IDeviceService deviceService;
  75. @Autowired
  76. private ISIPCommanderForPlatform sipCommanderFroPlatform;
  77. @Autowired
  78. private IRedisCatchStorage redisCatchStorage;
  79. @Autowired
  80. private ZLMServerFactory zlmServerFactory;
  81. @Autowired
  82. private IInviteStreamService inviteStreamService;
  83. @Autowired
  84. private SendRtpPortManager sendRtpPortManager;
  85. @Autowired
  86. private ZLMRESTfulUtils zlmresTfulUtils;
  87. @Autowired
  88. private AssistRESTfulUtils assistRESTfulUtils;
  89. @Autowired
  90. private IMediaService mediaService;
  91. @Autowired
  92. private IMediaServerService mediaServerService;
  93. @Autowired
  94. private VideoStreamSessionManager streamSession;
  95. @Autowired
  96. private UserSetting userSetting;
  97. @Autowired
  98. private SipConfig sipConfig;
  99. @Autowired
  100. private DynamicTask dynamicTask;
  101. @Autowired
  102. private ZlmHttpHookSubscribe subscribe;
  103. @Autowired
  104. private ISIPCommanderForPlatform commanderForPlatform;
  105. @Qualifier("taskExecutor")
  106. @Autowired
  107. private ThreadPoolTaskExecutor taskExecutor;
  108. @Autowired
  109. private RedisGbPlayMsgListener redisGbPlayMsgListener;
  110. @Autowired
  111. private ZlmHttpHookSubscribe hookSubscribe;
  112. @Autowired
  113. private SSRCFactory ssrcFactory;
  114. @Autowired
  115. private RedisTemplate<Object, Object> redisTemplate;
  116. @Override
  117. public SSRCInfo play(MediaServerItem mediaServerItem, String deviceId, String channelId, String ssrc, ErrorCallback<Object> callback) {
  118. if (mediaServerItem == null) {
  119. throw new ControllerException(ErrorCode.ERROR100.getCode(), "未找到可用的zlm");
  120. }
  121. Device device = redisCatchStorage.getDevice(deviceId);
  122. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId);
  123. if (inviteInfo != null ) {
  124. if (inviteInfo.getStreamInfo() == null) {
  125. // 点播发起了但是尚未成功, 仅注册回调等待结果即可
  126. inviteStreamService.once(InviteSessionType.PLAY, deviceId, channelId, null, callback);
  127. return inviteInfo.getSsrcInfo();
  128. }else {
  129. StreamInfo streamInfo = inviteInfo.getStreamInfo();
  130. String streamId = streamInfo.getStream();
  131. if (streamId == null) {
  132. callback.run(InviteErrorCode.ERROR_FOR_CATCH_DATA.getCode(), "点播失败, redis缓存streamId等于null", null);
  133. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  134. InviteErrorCode.ERROR_FOR_CATCH_DATA.getCode(),
  135. "点播失败, redis缓存streamId等于null",
  136. null);
  137. return inviteInfo.getSsrcInfo();
  138. }
  139. String mediaServerId = streamInfo.getMediaServerId();
  140. MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
  141. Boolean ready = zlmServerFactory.isStreamReady(mediaInfo, "rtp", streamId);
  142. if (ready != null && ready) {
  143. callback.run(InviteErrorCode.SUCCESS.getCode(), InviteErrorCode.SUCCESS.getMsg(), streamInfo);
  144. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  145. InviteErrorCode.SUCCESS.getCode(),
  146. InviteErrorCode.SUCCESS.getMsg(),
  147. streamInfo);
  148. return inviteInfo.getSsrcInfo();
  149. }else {
  150. // 点播发起了但是尚未成功, 仅注册回调等待结果即可
  151. inviteStreamService.once(InviteSessionType.PLAY, deviceId, channelId, null, callback);
  152. storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
  153. inviteStreamService.removeInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId);
  154. }
  155. }
  156. }
  157. String streamId = null;
  158. if (mediaServerItem.isRtpEnable()) {
  159. streamId = String.format("%s_%s", device.getDeviceId(), channelId);
  160. }
  161. SSRCInfo ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId, null, device.isSsrcCheck(), false, 0, false, false,device.getStreamModeForParam());
  162. if (ssrcInfo == null) {
  163. callback.run(InviteErrorCode.ERROR_FOR_RESOURCE_EXHAUSTION.getCode(), InviteErrorCode.ERROR_FOR_RESOURCE_EXHAUSTION.getMsg(), null);
  164. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  165. InviteErrorCode.ERROR_FOR_RESOURCE_EXHAUSTION.getCode(),
  166. InviteErrorCode.ERROR_FOR_RESOURCE_EXHAUSTION.getMsg(),
  167. null);
  168. return null;
  169. }
  170. // TODO 记录点播的状态
  171. play(mediaServerItem, ssrcInfo, device, channelId, callback);
  172. return ssrcInfo;
  173. }
  174. private void talk(MediaServerItem mediaServerItem, Device device, String channelId, String stream,
  175. ZlmHttpHookSubscribe.Event hookEvent, SipSubscribe.Event errorEvent,
  176. Runnable timeoutCallback, AudioBroadcastEvent audioEvent) {
  177. String playSsrc = ssrcFactory.getPlaySsrc(mediaServerItem.getId());
  178. if (playSsrc == null) {
  179. audioEvent.call("ssrc已经用尽");
  180. return;
  181. }
  182. SendRtpItem sendRtpItem = new SendRtpItem();
  183. sendRtpItem.setApp("talk");
  184. sendRtpItem.setStream(stream);
  185. sendRtpItem.setSsrc(playSsrc);
  186. sendRtpItem.setDeviceId(device.getDeviceId());
  187. sendRtpItem.setPlatformId(device.getDeviceId());
  188. sendRtpItem.setChannelId(channelId);
  189. sendRtpItem.setRtcp(false);
  190. sendRtpItem.setMediaServerId(mediaServerItem.getId());
  191. sendRtpItem.setOnlyAudio(true);
  192. sendRtpItem.setPlayType(InviteStreamType.TALK);
  193. sendRtpItem.setPt(8);
  194. sendRtpItem.setStatus(1);
  195. sendRtpItem.setTcpActive(false);
  196. sendRtpItem.setTcp(true);
  197. sendRtpItem.setUsePs(false);
  198. sendRtpItem.setReceiveStream(stream + "_talk");
  199. String callId = SipUtils.getNewCallId();
  200. int port = sendRtpPortManager.getNextPort(mediaServerItem);
  201. //端口获取失败的ssrcInfo 没有必要发送点播指令
  202. if (port <= 0) {
  203. logger.info("[语音对讲] 端口分配异常,deviceId={},channelId={}", device.getDeviceId(), channelId);
  204. audioEvent.call("端口分配异常");
  205. return;
  206. }
  207. sendRtpItem.setLocalPort(port);
  208. sendRtpItem.setPort(port);
  209. logger.info("[语音对讲]开始 deviceId: {}, channelId: {},收流端口: {}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, sendRtpItem.getLocalPort(), device.getStreamMode(), sendRtpItem.getSsrc(), false);
  210. // 超时处理
  211. String timeOutTaskKey = UUID.randomUUID().toString();
  212. dynamicTask.startDelay(timeOutTaskKey, () -> {
  213. logger.info("[语音对讲] 收流超时 deviceId: {}, channelId: {},端口:{}, SSRC: {}", device.getDeviceId(), channelId, sendRtpItem.getPort(), sendRtpItem.getSsrc());
  214. timeoutCallback.run();
  215. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  216. try {
  217. cmder.streamByeCmd(device, channelId, sendRtpItem.getStream(), null);
  218. } catch (InvalidArgumentException | ParseException | SipException | SsrcTransactionNotFoundException e) {
  219. logger.error("[语音对讲]超时, 发送BYE失败 {}", e.getMessage());
  220. } finally {
  221. timeoutCallback.run();
  222. mediaServerService.releaseSsrc(mediaServerItem.getId(), sendRtpItem.getSsrc());
  223. streamSession.remove(device.getDeviceId(), channelId, sendRtpItem.getStream());
  224. }
  225. }, userSetting.getPlayTimeout());
  226. Map<String, Object> param = new HashMap<>(12);
  227. param.put("vhost","__defaultVhost__");
  228. param.put("app", sendRtpItem.getApp());
  229. param.put("stream", sendRtpItem.getStream());
  230. param.put("ssrc", sendRtpItem.getSsrc());
  231. param.put("src_port", sendRtpItem.getLocalPort());
  232. param.put("pt", sendRtpItem.getPt());
  233. param.put("use_ps", sendRtpItem.isUsePs() ? "1" : "0");
  234. param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0");
  235. param.put("is_udp", sendRtpItem.isTcp() ? "0" : "1");
  236. param.put("recv_stream_id", sendRtpItem.getReceiveStream());
  237. param.put("close_delay_ms", userSetting.getPlayTimeout() * 1000);
  238. zlmServerFactory.startSendRtpPassive(mediaServerItem, param, jsonObject -> {
  239. if (jsonObject == null || jsonObject.getInteger("code") != 0 ) {
  240. mediaServerService.releaseSsrc(mediaServerItem.getId(), sendRtpItem.getSsrc());
  241. logger.info("[语音对讲]失败 deviceId: {}, channelId: {}", device.getDeviceId(), channelId);
  242. audioEvent.call("失败, " + jsonObject.getString("msg"));
  243. // 查看是否已经建立了通道,存在则发送bye
  244. stopTalk(device, channelId);
  245. }
  246. });
  247. // 查看设备是否已经在推流
  248. try {
  249. cmder.talkStreamCmd(mediaServerItem, sendRtpItem, device, channelId, callId, (mediaServerItemInuse, hookParam) -> {
  250. logger.info("[语音对讲] 流已生成, 开始推流: " + hookParam);
  251. dynamicTask.stop(timeOutTaskKey);
  252. // TODO 暂不做处理
  253. }, (mediaServerItemInuse, hookParam) -> {
  254. logger.info("[语音对讲] 设备开始推流: " + hookParam);
  255. dynamicTask.stop(timeOutTaskKey);
  256. }, (event) -> {
  257. dynamicTask.stop(timeOutTaskKey);
  258. if (event.event instanceof ResponseEvent) {
  259. ResponseEvent responseEvent = (ResponseEvent) event.event;
  260. if (responseEvent.getResponse() instanceof SIPResponse) {
  261. SIPResponse response = (SIPResponse) responseEvent.getResponse();
  262. sendRtpItem.setFromTag(response.getFromTag());
  263. sendRtpItem.setToTag(response.getToTag());
  264. sendRtpItem.setCallId(response.getCallIdHeader().getCallId());
  265. redisCatchStorage.updateSendRTPSever(sendRtpItem);
  266. streamSession.put(device.getDeviceId(), channelId, "talk",
  267. sendRtpItem.getStream(), sendRtpItem.getSsrc(), sendRtpItem.getMediaServerId(),
  268. response, InviteSessionType.TALK);
  269. } else {
  270. logger.error("[语音对讲]收到的消息错误,response不是SIPResponse");
  271. }
  272. } else {
  273. logger.error("[语音对讲]收到的消息错误,event不是ResponseEvent");
  274. }
  275. }, (event) -> {
  276. dynamicTask.stop(timeOutTaskKey);
  277. mediaServerService.closeRTPServer(mediaServerItem, sendRtpItem.getStream());
  278. // 释放ssrc
  279. mediaServerService.releaseSsrc(mediaServerItem.getId(), sendRtpItem.getSsrc());
  280. streamSession.remove(device.getDeviceId(), channelId, sendRtpItem.getStream());
  281. errorEvent.response(event);
  282. });
  283. } catch (InvalidArgumentException | SipException | ParseException e) {
  284. logger.error("[命令发送失败] 对讲消息: {}", e.getMessage());
  285. dynamicTask.stop(timeOutTaskKey);
  286. mediaServerService.closeRTPServer(mediaServerItem, sendRtpItem.getStream());
  287. // 释放ssrc
  288. mediaServerService.releaseSsrc(mediaServerItem.getId(), sendRtpItem.getSsrc());
  289. streamSession.remove(device.getDeviceId(), channelId, sendRtpItem.getStream());
  290. SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult();
  291. eventResult.type = SipSubscribe.EventResultType.cmdSendFailEvent;
  292. eventResult.statusCode = -1;
  293. eventResult.msg = "命令发送失败";
  294. errorEvent.response(eventResult);
  295. }
  296. // }
  297. }
  298. @Override
  299. public void play(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, Device device, String channelId,
  300. ErrorCallback<Object> callback) {
  301. if (mediaServerItem == null || ssrcInfo == null) {
  302. callback.run(InviteErrorCode.ERROR_FOR_PARAMETER_ERROR.getCode(),
  303. InviteErrorCode.ERROR_FOR_PARAMETER_ERROR.getMsg(),
  304. null);
  305. return;
  306. }
  307. logger.info("[点播开始] deviceId: {}, channelId: {},码流类型:{}, 收流端口: {}, 收流模式:{}, SSRC: {}, SSRC校验:{}",
  308. device.getDeviceId(), channelId, device.isSwitchPrimarySubStream() ? "辅码流" : "主码流", ssrcInfo.getPort(),
  309. device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck());
  310. //端口获取失败的ssrcInfo 没有必要发送点播指令
  311. if (ssrcInfo.getPort() <= 0) {
  312. logger.info("[点播端口分配异常],deviceId={},channelId={},ssrcInfo={}", device.getDeviceId(), channelId, ssrcInfo);
  313. // 释放ssrc
  314. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  315. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  316. callback.run(InviteErrorCode.ERROR_FOR_RESOURCE_EXHAUSTION.getCode(), "点播端口分配异常", null);
  317. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  318. InviteErrorCode.ERROR_FOR_RESOURCE_EXHAUSTION.getCode(), "点播端口分配异常", null);
  319. return;
  320. }
  321. // 初始化redis中的invite消息状态
  322. InviteInfo inviteInfo = InviteInfo.getInviteInfo(device.getDeviceId(), channelId, ssrcInfo.getStream(), ssrcInfo,
  323. mediaServerItem.getSdpIp(), ssrcInfo.getPort(), device.getStreamMode(), InviteSessionType.PLAY,
  324. InviteSessionStatus.ready);
  325. inviteInfo.setSubStream(device.isSwitchPrimarySubStream());
  326. inviteStreamService.updateInviteInfo(inviteInfo);
  327. // 超时处理
  328. String timeOutTaskKey = UUID.randomUUID().toString();
  329. dynamicTask.startDelay(timeOutTaskKey, () -> {
  330. // 执行超时任务时查询是否已经成功,成功了则不执行超时任务,防止超时任务取消失败的情况
  331. InviteInfo inviteInfoForTimeOut = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, device.getDeviceId(), channelId);
  332. if (inviteInfoForTimeOut == null || inviteInfoForTimeOut.getStreamInfo() == null) {
  333. logger.info("[点播超时] 收流超时 deviceId: {}, channelId: {},码流类型:{},端口:{}, SSRC: {}",
  334. device.getDeviceId(), channelId, device.isSwitchPrimarySubStream() ? "辅码流" : "主码流",
  335. ssrcInfo.getPort(), ssrcInfo.getSsrc());
  336. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  337. // InviteInfo inviteInfoForTimeout = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.play, device.getDeviceId(), channelId);
  338. // if (inviteInfoForTimeout == null) {
  339. // return;
  340. // }
  341. // if (InviteSessionStatus.ok == inviteInfoForTimeout.getStatus() ) {
  342. // // TODO 发送bye
  343. // }else {
  344. // // TODO 发送cancel
  345. // }
  346. callback.run(InviteErrorCode.ERROR_FOR_STREAM_TIMEOUT.getCode(), InviteErrorCode.ERROR_FOR_STREAM_TIMEOUT.getMsg(), null);
  347. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  348. InviteErrorCode.ERROR_FOR_STREAM_TIMEOUT.getCode(), InviteErrorCode.ERROR_FOR_STREAM_TIMEOUT.getMsg(), null);
  349. inviteStreamService.removeInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, device.getDeviceId(), channelId);
  350. try {
  351. cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null);
  352. } catch (InvalidArgumentException | ParseException | SipException |
  353. SsrcTransactionNotFoundException e) {
  354. logger.error("[点播超时], 发送BYE失败 {}", e.getMessage());
  355. } finally {
  356. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  357. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  358. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  359. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  360. // 取消订阅消息监听
  361. HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp", ssrcInfo.getStream(), true, "rtsp", mediaServerItem.getId());
  362. subscribe.removeSubscribe(hookSubscribe);
  363. }
  364. }
  365. }, userSetting.getPlayTimeout());
  366. try {
  367. cmder.playStreamCmd(mediaServerItem, ssrcInfo, device, channelId, (mediaServerItemInuse, hookParam ) -> {
  368. logger.info("收到订阅消息: " + hookParam);
  369. dynamicTask.stop(timeOutTaskKey);
  370. // hook响应
  371. StreamInfo streamInfo = onPublishHandlerForPlay(mediaServerItemInuse, hookParam, device.getDeviceId(), channelId);
  372. if (streamInfo == null){
  373. callback.run(InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getCode(),
  374. InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getMsg(), null);
  375. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  376. InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getCode(),
  377. InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getMsg(), null);
  378. return;
  379. }
  380. callback.run(InviteErrorCode.SUCCESS.getCode(), InviteErrorCode.SUCCESS.getMsg(), streamInfo);
  381. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  382. InviteErrorCode.SUCCESS.getCode(),
  383. InviteErrorCode.SUCCESS.getMsg(),
  384. streamInfo);
  385. logger.info("[点播成功] deviceId: {}, channelId:{}, 码流类型:{}", device.getDeviceId(), channelId,
  386. device.isSwitchPrimarySubStream() ? "辅码流" : "主码流");
  387. snapOnPlay(mediaServerItemInuse, device.getDeviceId(), channelId, ssrcInfo.getStream());
  388. }, (event) -> {
  389. inviteInfo.setStatus(InviteSessionStatus.ok);
  390. ResponseEvent responseEvent = (ResponseEvent) event.event;
  391. String contentString = new String(responseEvent.getResponse().getRawContent());
  392. // 获取ssrc
  393. int ssrcIndex = contentString.indexOf("y=");
  394. // 检查是否有y字段
  395. if (ssrcIndex >= 0) {
  396. //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容
  397. String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12).trim();
  398. // 查询到ssrc不一致且开启了ssrc校验则需要针对处理
  399. if (ssrcInfo.getSsrc().equals(ssrcInResponse)) {
  400. if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
  401. String substring = contentString.substring(0, contentString.indexOf("y="));
  402. try {
  403. SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(substring);
  404. int port = -1;
  405. Vector mediaDescriptions = sdp.getMediaDescriptions(true);
  406. for (Object description : mediaDescriptions) {
  407. MediaDescription mediaDescription = (MediaDescription) description;
  408. Media media = mediaDescription.getMedia();
  409. Vector mediaFormats = media.getMediaFormats(false);
  410. if (mediaFormats.contains("96")) {
  411. port = media.getMediaPort();
  412. break;
  413. }
  414. }
  415. logger.info("[点播-TCP主动连接对方] deviceId: {}, channelId: {}, 连接对方的地址:{}:{}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, sdp.getConnection().getAddress(), port, device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck());
  416. JSONObject jsonObject = zlmresTfulUtils.connectRtpServer(mediaServerItem, sdp.getConnection().getAddress(), port, ssrcInfo.getStream());
  417. logger.info("[点播-TCP主动连接对方] 结果: {}", jsonObject);
  418. } catch (SdpException e) {
  419. logger.error("[点播-TCP主动连接对方] deviceId: {}, channelId: {}, 解析200OK的SDP信息失败", device.getDeviceId(), channelId, e);
  420. dynamicTask.stop(timeOutTaskKey);
  421. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  422. // 释放ssrc
  423. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  424. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  425. callback.run(InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getCode(),
  426. InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getMsg(), null);
  427. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  428. InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getCode(),
  429. InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getMsg(), null);
  430. }
  431. }
  432. return;
  433. }
  434. logger.info("[点播消息] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse);
  435. if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
  436. logger.info("[点播消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
  437. // 释放ssrc
  438. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  439. // 单端口模式streamId也有变化,重新设置监听即可
  440. if (!mediaServerItem.isRtpEnable()) {
  441. // 添加订阅
  442. HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp", ssrcInfo.getStream(), true, "rtsp", mediaServerItem.getId());
  443. subscribe.removeSubscribe(hookSubscribe);
  444. String stream = String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase();
  445. hookSubscribe.getContent().put("stream", stream);
  446. inviteStreamService.updateInviteInfoForStream(inviteInfo, stream);
  447. subscribe.addSubscribe(hookSubscribe, (mediaServerItemInUse, hookParam) -> {
  448. logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + hookParam);
  449. dynamicTask.stop(timeOutTaskKey);
  450. // hook响应
  451. StreamInfo streamInfo = onPublishHandlerForPlay(mediaServerItemInUse, hookParam, device.getDeviceId(), channelId);
  452. if (streamInfo == null){
  453. callback.run(InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getCode(),
  454. InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getMsg(), null);
  455. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  456. InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getCode(),
  457. InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getMsg(), null);
  458. return;
  459. }
  460. callback.run(InviteErrorCode.SUCCESS.getCode(),
  461. InviteErrorCode.SUCCESS.getMsg(), streamInfo);
  462. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  463. InviteErrorCode.SUCCESS.getCode(),
  464. InviteErrorCode.SUCCESS.getMsg(),
  465. streamInfo);
  466. snapOnPlay(mediaServerItemInUse, device.getDeviceId(), channelId, stream);
  467. });
  468. return;
  469. }
  470. // 更新ssrc
  471. Boolean result = mediaServerService.updateRtpServerSSRC(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse);
  472. if (!result) {
  473. try {
  474. logger.warn("[点播] 更新ssrc失败,停止点播 {}/{}", device.getDeviceId(), channelId);
  475. cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null, null);
  476. } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) {
  477. logger.error("[命令发送失败] 停止点播, 发送BYE: {}", e.getMessage());
  478. }
  479. dynamicTask.stop(timeOutTaskKey);
  480. // 释放ssrc
  481. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  482. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  483. callback.run(InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
  484. "下级自定义了ssrc,重新设置收流信息失败", null);
  485. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  486. InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
  487. "下级自定义了ssrc,重新设置收流信息失败", null);
  488. }else {
  489. if (ssrcInfo.getStream()!= null && !ssrcInfo.getStream().equals(inviteInfo.getStream())) {
  490. inviteStreamService.removeInviteInfo(inviteInfo);
  491. }
  492. ssrcInfo.setSsrc(ssrcInResponse);
  493. inviteInfo.setSsrcInfo(ssrcInfo);
  494. inviteInfo.setStream(ssrcInfo.getStream());
  495. }
  496. }else {
  497. logger.info("[点播消息] 收到invite 200, 下级自定义了ssrc, 但是当前模式无需修正");
  498. }
  499. }
  500. inviteStreamService.updateInviteInfo(inviteInfo);
  501. }, (event) -> {
  502. dynamicTask.stop(timeOutTaskKey);
  503. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  504. // 释放ssrc
  505. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  506. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  507. callback.run(InviteErrorCode.ERROR_FOR_SIGNALLING_ERROR.getCode(),
  508. String.format("点播失败, 错误码: %s, %s", event.statusCode, event.msg), null);
  509. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  510. InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
  511. String.format("点播失败, 错误码: %s, %s", event.statusCode, event.msg), null);
  512. inviteStreamService.removeInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, device.getDeviceId(), channelId);
  513. });
  514. } catch (InvalidArgumentException | SipException | ParseException e) {
  515. logger.error("[命令发送失败] 点播消息: {}", e.getMessage());
  516. dynamicTask.stop(timeOutTaskKey);
  517. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  518. // 释放ssrc
  519. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  520. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  521. callback.run(InviteErrorCode.ERROR_FOR_SIP_SENDING_FAILED.getCode(),
  522. InviteErrorCode.ERROR_FOR_SIP_SENDING_FAILED.getMsg(), null);
  523. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  524. InviteErrorCode.ERROR_FOR_SIP_SENDING_FAILED.getCode(),
  525. InviteErrorCode.ERROR_FOR_SIP_SENDING_FAILED.getMsg(), null);
  526. inviteStreamService.removeInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, device.getDeviceId(), channelId);
  527. }
  528. }
  529. /**
  530. * 点播成功时调用截图.
  531. *
  532. * @param mediaServerItemInuse media
  533. * @param deviceId 设备 ID
  534. * @param channelId 通道 ID
  535. * @param stream ssrc
  536. */
  537. private void snapOnPlay(MediaServerItem mediaServerItemInuse, String deviceId, String channelId, String stream) {
  538. String streamUrl;
  539. if (mediaServerItemInuse.getRtspPort() != 0) {
  540. streamUrl = String.format("rtsp://127.0.0.1:%s/%s/%s", mediaServerItemInuse.getRtspPort(), "rtp", stream);
  541. } else {
  542. streamUrl = String.format("http://127.0.0.1:%s/%s/%s.live.mp4", mediaServerItemInuse.getHttpPort(), "rtp", stream);
  543. }
  544. String path = "snap";
  545. String fileName = deviceId + "_" + channelId + ".jpg";
  546. // 请求截图
  547. logger.info("[请求截图]: " + fileName);
  548. zlmresTfulUtils.getSnap(mediaServerItemInuse, streamUrl, 15, 1, path, fileName);
  549. }
  550. public StreamInfo onPublishHandlerForPlay(MediaServerItem mediaServerItem, HookParam hookParam, String deviceId, String channelId) {
  551. StreamInfo streamInfo = null;
  552. Device device = redisCatchStorage.getDevice(deviceId);
  553. OnStreamChangedHookParam streamChangedHookParam = (OnStreamChangedHookParam)hookParam;
  554. streamInfo = onPublishHandler(mediaServerItem, streamChangedHookParam, deviceId, channelId);
  555. if (streamInfo != null) {
  556. DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);
  557. if (deviceChannel != null) {
  558. deviceChannel.setStreamId(streamInfo.getStream());
  559. storager.startPlay(deviceId, channelId, streamInfo.getStream());
  560. }
  561. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId);
  562. if (inviteInfo != null) {
  563. inviteInfo.setStatus(InviteSessionStatus.ok);
  564. inviteInfo.setStreamInfo(streamInfo);
  565. inviteStreamService.updateInviteInfo(inviteInfo);
  566. }
  567. }
  568. return streamInfo;
  569. }
  570. private StreamInfo onPublishHandlerForPlayback(MediaServerItem mediaServerItem, HookParam param, String deviceId, String channelId, String startTime, String endTime) {
  571. OnStreamChangedHookParam streamChangedHookParam = (OnStreamChangedHookParam) param;
  572. StreamInfo streamInfo = onPublishHandler(mediaServerItem, streamChangedHookParam, deviceId, channelId);
  573. if (streamInfo != null) {
  574. streamInfo.setStartTime(startTime);
  575. streamInfo.setEndTime(endTime);
  576. DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);
  577. if (deviceChannel != null) {
  578. deviceChannel.setStreamId(streamInfo.getStream());
  579. storager.startPlay(deviceId, channelId, streamInfo.getStream());
  580. }
  581. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAYBACK, deviceId, channelId);
  582. if (inviteInfo != null) {
  583. inviteInfo.setStatus(InviteSessionStatus.ok);
  584. inviteInfo.setStreamInfo(streamInfo);
  585. inviteStreamService.updateInviteInfo(inviteInfo);
  586. }
  587. }
  588. return streamInfo;
  589. }
  590. @Override
  591. public MediaServerItem getNewMediaServerItem(Device device) {
  592. if (device == null) {
  593. return null;
  594. }
  595. MediaServerItem mediaServerItem;
  596. if (ObjectUtils.isEmpty(device.getMediaServerId()) || "auto".equals(device.getMediaServerId())) {
  597. mediaServerItem = mediaServerService.getMediaServerForMinimumLoad(null);
  598. } else {
  599. mediaServerItem = mediaServerService.getOne(device.getMediaServerId());
  600. }
  601. if (mediaServerItem == null) {
  602. logger.warn("点播时未找到可使用的ZLM...");
  603. }
  604. return mediaServerItem;
  605. }
  606. @Override
  607. public MediaServerItem getNewMediaServerItemHasAssist(Device device) {
  608. if (device == null) {
  609. return null;
  610. }
  611. MediaServerItem mediaServerItem;
  612. if (ObjectUtils.isEmpty(device.getMediaServerId()) || "auto".equals(device.getMediaServerId())) {
  613. mediaServerItem = mediaServerService.getMediaServerForMinimumLoad(true);
  614. } else {
  615. mediaServerItem = mediaServerService.getOne(device.getMediaServerId());
  616. }
  617. if (mediaServerItem == null) {
  618. logger.warn("[获取可用的ZLM节点]未找到可使用的ZLM...");
  619. }
  620. return mediaServerItem;
  621. }
  622. @Override
  623. public void playBack(String deviceId, String channelId, String startTime,
  624. String endTime, ErrorCallback<Object> callback) {
  625. Device device = storager.queryVideoDevice(deviceId);
  626. if (device == null) {
  627. return;
  628. }
  629. MediaServerItem newMediaServerItem = getNewMediaServerItem(device);
  630. String stream = null;
  631. if (newMediaServerItem.isRtpEnable()) {
  632. String startTimeStr = startTime.replace("-", "")
  633. .replace(":", "")
  634. .replace(" ", "");
  635. System.out.println(startTimeStr);
  636. String endTimeTimeStr = endTime.replace("-", "")
  637. .replace(":", "")
  638. .replace(" ", "");
  639. System.out.println(endTimeTimeStr);
  640. stream = deviceId + "_" + channelId + "_" + startTimeStr + "_" + endTimeTimeStr;
  641. }
  642. SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, stream, null, device.isSsrcCheck(), true, 0, false,false, device.getStreamModeForParam());
  643. playBack(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, callback);
  644. }
  645. @Override
  646. public void playBack(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo,
  647. String deviceId, String channelId, String startTime,
  648. String endTime, ErrorCallback<Object> callback) {
  649. if (mediaServerItem == null || ssrcInfo == null) {
  650. callback.run(InviteErrorCode.ERROR_FOR_PARAMETER_ERROR.getCode(),
  651. InviteErrorCode.ERROR_FOR_PARAMETER_ERROR.getMsg(),
  652. null);
  653. return;
  654. }
  655. Device device = storager.queryVideoDevice(deviceId);
  656. if (device == null) {
  657. throw new ControllerException(ErrorCode.ERROR100.getCode(), "设备: " + deviceId + "不存在");
  658. }
  659. logger.info("[录像回放] deviceId: {}, channelId: {}, 开始时间: {}, 结束时间: {}, 收流端口:{}, 收流模式:{}, SSRC: {}, SSRC校验:{}",
  660. device.getDeviceId(), channelId, startTime, endTime, ssrcInfo.getPort(), device.getStreamMode(),
  661. ssrcInfo.getSsrc(), device.isSsrcCheck());
  662. // 初始化redis中的invite消息状态
  663. InviteInfo inviteInfo = InviteInfo.getInviteInfo(device.getDeviceId(), channelId, ssrcInfo.getStream(), ssrcInfo,
  664. mediaServerItem.getSdpIp(), ssrcInfo.getPort(), device.getStreamMode(), InviteSessionType.PLAYBACK,
  665. InviteSessionStatus.ready);
  666. inviteStreamService.updateInviteInfo(inviteInfo);
  667. String playBackTimeOutTaskKey = UUID.randomUUID().toString();
  668. dynamicTask.startDelay(playBackTimeOutTaskKey, () -> {
  669. logger.warn("[录像回放] 超时,deviceId:{} ,channelId:{}", deviceId, channelId);
  670. inviteStreamService.removeInviteInfo(inviteInfo);
  671. callback.run(InviteErrorCode.ERROR_FOR_SIGNALLING_TIMEOUT.getCode(), InviteErrorCode.ERROR_FOR_SIGNALLING_TIMEOUT.getMsg(), null);
  672. try {
  673. cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null);
  674. } catch (InvalidArgumentException | ParseException | SipException e) {
  675. logger.error("[录像回放] 超时 发送BYE失败 {}", e.getMessage());
  676. } catch (SsrcTransactionNotFoundException e) {
  677. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  678. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  679. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  680. streamSession.remove(deviceId, channelId, ssrcInfo.getStream());
  681. }
  682. }, userSetting.getPlayTimeout());
  683. SipSubscribe.Event errorEvent = event -> {
  684. logger.info("[录像回放] 失败,{} {}", event.statusCode, event.msg);
  685. dynamicTask.stop(playBackTimeOutTaskKey);
  686. callback.run(InviteErrorCode.ERROR_FOR_SIGNALLING_ERROR.getCode(),
  687. String.format("回放失败, 错误码: %s, %s", event.statusCode, event.msg), null);
  688. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  689. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  690. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  691. inviteStreamService.removeInviteInfo(inviteInfo);
  692. };
  693. ZlmHttpHookSubscribe.Event hookEvent = (mediaServerItemInuse, hookParam) -> {
  694. logger.info("收到回放订阅消息: " + hookParam);
  695. dynamicTask.stop(playBackTimeOutTaskKey);
  696. StreamInfo streamInfo = onPublishHandlerForPlayback(mediaServerItemInuse, hookParam, deviceId, channelId, startTime, endTime);
  697. if (streamInfo == null) {
  698. logger.warn("设备回放API调用失败!");
  699. callback.run(InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getCode(),
  700. InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getMsg(), null);
  701. return;
  702. }
  703. callback.run(InviteErrorCode.SUCCESS.getCode(), InviteErrorCode.SUCCESS.getMsg(), streamInfo);
  704. logger.info("[录像回放] 成功 deviceId: {}, channelId: {}, 开始时间: {}, 结束时间: {}", device.getDeviceId(), channelId, startTime, endTime);
  705. };
  706. try {
  707. cmder.playbackStreamCmd(mediaServerItem, ssrcInfo, device, channelId, startTime, endTime,
  708. hookEvent, eventResult -> {
  709. inviteInfo.setStatus(InviteSessionStatus.ok);
  710. ResponseEvent responseEvent = (ResponseEvent) eventResult.event;
  711. String contentString = new String(responseEvent.getResponse().getRawContent());
  712. // 获取ssrc
  713. int ssrcIndex = contentString.indexOf("y=");
  714. // 检查是否有y字段
  715. if (ssrcIndex >= 0) {
  716. //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容
  717. String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
  718. // 查询到ssrc不一致且开启了ssrc校验则需要针对处理
  719. if (ssrcInfo.getSsrc().equals(ssrcInResponse)) {
  720. if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
  721. String substring = contentString.substring(0, contentString.indexOf("y="));
  722. try {
  723. SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(substring);
  724. int port = -1;
  725. Vector mediaDescriptions = sdp.getMediaDescriptions(true);
  726. for (Object description : mediaDescriptions) {
  727. MediaDescription mediaDescription = (MediaDescription) description;
  728. Media media = mediaDescription.getMedia();
  729. Vector mediaFormats = media.getMediaFormats(false);
  730. if (mediaFormats.contains("96")) {
  731. port = media.getMediaPort();
  732. break;
  733. }
  734. }
  735. logger.info("[录像回放-TCP主动连接对方] deviceId: {}, channelId: {}, 连接对方的地址:{}:{}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, sdp.getConnection().getAddress(), port, device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck());
  736. JSONObject jsonObject = zlmresTfulUtils.connectRtpServer(mediaServerItem, sdp.getConnection().getAddress(), port, ssrcInfo.getStream());
  737. logger.info("[录像回放-TCP主动连接对方] 结果: {}", jsonObject);
  738. } catch (SdpException e) {
  739. logger.error("[录像回放-TCP主动连接对方] deviceId: {}, channelId: {}, 解析200OK的SDP信息失败", device.getDeviceId(), channelId, e);
  740. dynamicTask.stop(playBackTimeOutTaskKey);
  741. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  742. // 释放ssrc
  743. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  744. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  745. callback.run(InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getCode(),
  746. InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getMsg(), null);
  747. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  748. InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getCode(),
  749. InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getMsg(), null);
  750. }
  751. }
  752. return;
  753. }
  754. logger.info("[录像回放] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse);
  755. if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
  756. logger.info("[录像回放] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
  757. // 释放ssrc
  758. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  759. // 单端口模式streamId也有变化,需要重新设置监听
  760. if (!mediaServerItem.isRtpEnable()) {
  761. // 添加订阅
  762. HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp", ssrcInfo.getStream(), true, "rtsp", mediaServerItem.getId());
  763. subscribe.removeSubscribe(hookSubscribe);
  764. String stream = String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase();
  765. hookSubscribe.getContent().put("stream", stream);
  766. inviteStreamService.updateInviteInfoForStream(inviteInfo, stream);
  767. subscribe.addSubscribe(hookSubscribe, (mediaServerItemInUse, hookParam) -> {
  768. logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + hookParam);
  769. dynamicTask.stop(playBackTimeOutTaskKey);
  770. // hook响应
  771. hookEvent.response(mediaServerItemInUse, hookParam);
  772. });
  773. }
  774. // 更新ssrc
  775. Boolean result = mediaServerService.updateRtpServerSSRC(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse);
  776. if (!result) {
  777. try {
  778. logger.warn("[录像回放] 更新ssrc失败,停止录像回放 {}/{}", device.getDeviceId(), channelId);
  779. cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null, null);
  780. } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) {
  781. logger.error("[命令发送失败] 停止点播, 发送BYE: {}", e.getMessage());
  782. }
  783. dynamicTask.stop(playBackTimeOutTaskKey);
  784. // 释放ssrc
  785. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  786. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  787. callback.run(InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
  788. "下级自定义了ssrc,重新设置收流信息失败", null);
  789. }else {
  790. if (ssrcInfo.getStream()!= null && !ssrcInfo.getStream().equals(inviteInfo.getStream())) {
  791. inviteStreamService.removeInviteInfo(inviteInfo);
  792. }
  793. ssrcInfo.setSsrc(ssrcInResponse);
  794. inviteInfo.setSsrcInfo(ssrcInfo);
  795. inviteInfo.setStream(ssrcInfo.getStream());
  796. }
  797. }else {
  798. logger.info("[点播消息] 收到invite 200, 下级自定义了ssrc, 但是当前模式无需修正");
  799. }
  800. }
  801. inviteStreamService.updateInviteInfo(inviteInfo);
  802. }, errorEvent);
  803. } catch (InvalidArgumentException | SipException | ParseException e) {
  804. logger.error("[命令发送失败] 回放: {}", e.getMessage());
  805. SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult();
  806. eventResult.type = SipSubscribe.EventResultType.cmdSendFailEvent;
  807. eventResult.statusCode = -1;
  808. eventResult.msg = "命令发送失败";
  809. errorEvent.response(eventResult);
  810. }
  811. }
  812. @Override
  813. public void download(String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, ErrorCallback<Object> callback) {
  814. Device device = storager.queryVideoDevice(deviceId);
  815. if (device == null) {
  816. return;
  817. }
  818. MediaServerItem newMediaServerItem = getNewMediaServerItemHasAssist(device);
  819. if (newMediaServerItem == null) {
  820. callback.run(InviteErrorCode.ERROR_FOR_ASSIST_NOT_READY.getCode(),
  821. InviteErrorCode.ERROR_FOR_ASSIST_NOT_READY.getMsg(),
  822. null);
  823. return;
  824. }
  825. SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, null, device.isSsrcCheck(), true, 0, false,false, device.getStreamModeForParam());
  826. download(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, downloadSpeed, callback);
  827. }
  828. @Override
  829. public void download(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, ErrorCallback<Object> callback) {
  830. if (mediaServerItem == null || ssrcInfo == null) {
  831. callback.run(InviteErrorCode.ERROR_FOR_PARAMETER_ERROR.getCode(),
  832. InviteErrorCode.ERROR_FOR_PARAMETER_ERROR.getMsg(),
  833. null);
  834. return;
  835. }
  836. Device device = storager.queryVideoDevice(deviceId);
  837. if (device == null) {
  838. callback.run(InviteErrorCode.ERROR_FOR_PARAMETER_ERROR.getCode(),
  839. "设备:" + deviceId + "不存在",
  840. null);
  841. return;
  842. }
  843. logger.info("[录像下载] deviceId: {}, channelId: {}, 下载速度:{}, 收流端口:{}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, downloadSpeed, ssrcInfo.getPort(), device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck());
  844. // 初始化redis中的invite消息状态
  845. InviteInfo inviteInfo = InviteInfo.getInviteInfo(device.getDeviceId(), channelId, ssrcInfo.getStream(), ssrcInfo,
  846. mediaServerItem.getSdpIp(), ssrcInfo.getPort(), device.getStreamMode(), InviteSessionType.DOWNLOAD,
  847. InviteSessionStatus.ready);
  848. inviteStreamService.updateInviteInfo(inviteInfo);
  849. String downLoadTimeOutTaskKey = UUID.randomUUID().toString();
  850. dynamicTask.startDelay(downLoadTimeOutTaskKey, () -> {
  851. logger.warn(String.format("录像下载请求超时,deviceId:%s ,channelId:%s", deviceId, channelId));
  852. inviteStreamService.removeInviteInfo(inviteInfo);
  853. callback.run(InviteErrorCode.ERROR_FOR_SIGNALLING_TIMEOUT.getCode(),
  854. InviteErrorCode.ERROR_FOR_SIGNALLING_TIMEOUT.getMsg(), null);
  855. // 点播超时回复BYE 同时释放ssrc以及此次点播的资源
  856. try {
  857. cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null);
  858. } catch (InvalidArgumentException | ParseException | SipException e) {
  859. logger.error("[录像流]录像下载请求超时, 发送BYE失败 {}", e.getMessage());
  860. } catch (SsrcTransactionNotFoundException e) {
  861. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  862. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  863. streamSession.remove(deviceId, channelId, ssrcInfo.getStream());
  864. }
  865. }, userSetting.getPlayTimeout());
  866. SipSubscribe.Event errorEvent = event -> {
  867. dynamicTask.stop(downLoadTimeOutTaskKey);
  868. callback.run(InviteErrorCode.ERROR_FOR_SIGNALLING_TIMEOUT.getCode(),
  869. String.format("录像下载失败, 错误码: %s, %s", event.statusCode, event.msg), null);
  870. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  871. inviteStreamService.removeInviteInfo(inviteInfo);
  872. };
  873. ZlmHttpHookSubscribe.Event hookEvent = (mediaServerItemInuse, hookParam) -> {
  874. logger.info("[录像下载]收到订阅消息: " + hookParam);
  875. dynamicTask.stop(downLoadTimeOutTaskKey);
  876. StreamInfo streamInfo = onPublishHandlerForDownload(mediaServerItemInuse, hookParam, deviceId, channelId, startTime, endTime);
  877. if (streamInfo == null) {
  878. logger.warn("[录像下载] 获取流地址信息失败");
  879. callback.run(InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getCode(),
  880. InviteErrorCode.ERROR_FOR_STREAM_PARSING_EXCEPTIONS.getMsg(), null);
  881. return;
  882. }
  883. callback.run(InviteErrorCode.SUCCESS.getCode(), InviteErrorCode.SUCCESS.getMsg(), streamInfo);
  884. logger.info("[录像下载] 调用成功 deviceId: {}, channelId: {}, 开始时间: {}, 结束时间: {}", device.getDeviceId(), channelId, startTime, endTime);
  885. };
  886. try {
  887. cmder.downloadStreamCmd(mediaServerItem, ssrcInfo, device, channelId, startTime, endTime, downloadSpeed,
  888. hookEvent, errorEvent, eventResult ->{
  889. inviteInfo.setStatus(InviteSessionStatus.ok);
  890. ResponseEvent responseEvent = (ResponseEvent) eventResult.event;
  891. String contentString = new String(responseEvent.getResponse().getRawContent());
  892. // 获取ssrc
  893. int ssrcIndex = contentString.indexOf("y=");
  894. // 检查是否有y字段
  895. if (ssrcIndex >= 0) {
  896. //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容
  897. String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
  898. // 查询到ssrc不一致且开启了ssrc校验则需要针对处理
  899. if (ssrcInfo.getSsrc().equals(ssrcInResponse)) {
  900. if (device.getStreamMode().equalsIgnoreCase("TCP-ACTIVE")) {
  901. String substring = contentString.substring(0, contentString.indexOf("y="));
  902. try {
  903. SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(substring);
  904. int port = -1;
  905. Vector mediaDescriptions = sdp.getMediaDescriptions(true);
  906. for (Object description : mediaDescriptions) {
  907. MediaDescription mediaDescription = (MediaDescription) description;
  908. Media media = mediaDescription.getMedia();
  909. Vector mediaFormats = media.getMediaFormats(false);
  910. if (mediaFormats.contains("96")) {
  911. port = media.getMediaPort();
  912. break;
  913. }
  914. }
  915. logger.info("[录像下载-TCP主动连接对方] deviceId: {}, channelId: {}, 连接对方的地址:{}:{}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, sdp.getConnection().getAddress(), port, device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck());
  916. JSONObject jsonObject = zlmresTfulUtils.connectRtpServer(mediaServerItem, sdp.getConnection().getAddress(), port, ssrcInfo.getStream());
  917. logger.info("[录像下载-TCP主动连接对方] 结果: {}", jsonObject);
  918. } catch (SdpException e) {
  919. logger.error("[录像下载-TCP主动连接对方] deviceId: {}, channelId: {}, 解析200OK的SDP信息失败", device.getDeviceId(), channelId, e);
  920. dynamicTask.stop(downLoadTimeOutTaskKey);
  921. mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream());
  922. // 释放ssrc
  923. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  924. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  925. callback.run(InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getCode(),
  926. InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getMsg(), null);
  927. inviteStreamService.call(InviteSessionType.PLAY, device.getDeviceId(), channelId, null,
  928. InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getCode(),
  929. InviteErrorCode.ERROR_FOR_SDP_PARSING_EXCEPTIONS.getMsg(), null);
  930. }
  931. }
  932. return;
  933. }
  934. logger.info("[录像下载] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse);
  935. if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) {
  936. logger.info("[录像下载] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse);
  937. // 释放ssrc
  938. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  939. // 单端口模式streamId也有变化,需要重新设置监听
  940. if (!mediaServerItem.isRtpEnable()) {
  941. // 添加订阅
  942. HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp", ssrcInfo.getStream(), true, "rtsp", mediaServerItem.getId());
  943. subscribe.removeSubscribe(hookSubscribe);
  944. String stream = String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase();
  945. hookSubscribe.getContent().put("stream", stream);
  946. inviteStreamService.updateInviteInfoForStream(inviteInfo, stream);
  947. subscribe.addSubscribe(hookSubscribe, (mediaServerItemInUse, hookParam) -> {
  948. logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + hookParam);
  949. dynamicTask.stop(downLoadTimeOutTaskKey);
  950. hookEvent.response(mediaServerItemInUse, hookParam);
  951. });
  952. }
  953. // 更新ssrc
  954. Boolean result = mediaServerService.updateRtpServerSSRC(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse);
  955. if (!result) {
  956. try {
  957. logger.warn("[录像下载] 更新ssrc失败,停止录像回放 {}/{}", device.getDeviceId(), channelId);
  958. cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null, null);
  959. } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) {
  960. logger.error("[命令发送失败] 停止点播, 发送BYE: {}", e.getMessage());
  961. }
  962. dynamicTask.stop(downLoadTimeOutTaskKey);
  963. // 释放ssrc
  964. mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
  965. streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
  966. callback.run(InviteErrorCode.ERROR_FOR_RESET_SSRC.getCode(),
  967. "下级自定义了ssrc,重新设置收流信息失败", null);
  968. }else {
  969. if (ssrcInfo.getStream()!= null && !ssrcInfo.getStream().equals(inviteInfo.getStream())) {
  970. inviteStreamService.removeInviteInfo(inviteInfo);
  971. }
  972. ssrcInfo.setSsrc(ssrcInResponse);
  973. inviteInfo.setSsrcInfo(ssrcInfo);
  974. inviteInfo.setStream(ssrcInfo.getStream());
  975. }
  976. }else {
  977. logger.info("[录像下载] 收到invite 200, 下级自定义了ssrc, 但是当前模式无需修正");
  978. }
  979. }
  980. inviteStreamService.updateInviteInfo(inviteInfo);
  981. });
  982. } catch (InvalidArgumentException | SipException | ParseException e) {
  983. logger.error("[命令发送失败] 录像下载: {}", e.getMessage());
  984. SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult();
  985. eventResult.type = SipSubscribe.EventResultType.cmdSendFailEvent;
  986. eventResult.statusCode = -1;
  987. eventResult.msg = "命令发送失败";
  988. errorEvent.response(eventResult);
  989. }
  990. }
  991. @Override
  992. public StreamInfo getDownLoadInfo(String deviceId, String channelId, String stream) {
  993. InviteInfo inviteInfo = inviteStreamService.getInviteInfo(InviteSessionType.DOWNLOAD, deviceId, channelId, stream);
  994. if (inviteInfo != null && inviteInfo.getStreamInfo() != null) {
  995. if (inviteInfo.getStreamInfo().getProgress() == 1) {
  996. return inviteInfo.getStreamInfo();
  997. }
  998. // 获取当前已下载时长
  999. String mediaServerId = inviteInfo.getStreamInfo().getMediaServerId();
  1000. MediaServerItem mediaServerItem = mediaServerService.getOne(mediaServerId);
  1001. if (mediaServerItem == null) {
  1002. logger.warn("查询录像信息时发现节点已离线");
  1003. return null;
  1004. }
  1005. if (mediaServerItem.getRecordAssistPort() > 0) {
  1006. JSONObject jsonObject = assistRESTfulUtils.fileDuration(mediaServerItem, inviteInfo.getStreamInfo().getApp(), inviteInfo.getStreamInfo().getStream(), null);
  1007. if (jsonObject == null) {
  1008. throw new ControllerException(ErrorCode.ERROR100.getCode(), "连接Assist服务失败");
  1009. }
  1010. if (jsonObject.getInteger("code") == 0) {
  1011. long duration = jsonObject.getLong("data");
  1012. if (duration == 0) {
  1013. inviteInfo.getStreamInfo().setProgress(0);
  1014. } else {
  1015. String startTime = inviteInfo.getStreamInfo().getStartTime();
  1016. String endTime = inviteInfo.getStreamInfo().getEndTime();
  1017. long start = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime);
  1018. long end = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime);
  1019. BigDecimal currentCount = new BigDecimal(duration / 1000);
  1020. BigDecimal totalCount = new BigDecimal(end - start);
  1021. BigDecimal divide = currentCount.divide(totalCount, 2, RoundingMode.HALF_UP);
  1022. double process = divide.doubleValue();
  1023. inviteInfo.getStreamInfo().setProgress(process);
  1024. }
  1025. inviteStreamService.updateInviteInfo(inviteInfo);
  1026. }
  1027. }
  1028. return inviteInfo.getStreamInfo();
  1029. }
  1030. return null;
  1031. }
  1032. private StreamInfo onPublishHandlerForDownload(MediaServerItem mediaServerItemInuse, HookParam hookParam, String deviceId, String channelId, String startTime, String endTime) {
  1033. OnStreamChangedHookParam streamChangedHookParam = (OnStreamChangedHookParam) hookParam;
  1034. StreamInfo streamInfo = onPublishHandler(mediaServerItemInuse, streamChangedHookParam, deviceId, channelId);
  1035. if (streamInfo != null) {
  1036. streamInfo.setProgress(0);
  1037. streamInfo.setStartTime(startTime);
  1038. streamInfo.setEndTime(endTime);
  1039. InviteInfo inviteInfo = inviteStreamService.getInviteInfo(InviteSessionType.DOWNLOAD, deviceId, channelId, streamInfo.getStream());
  1040. if (inviteInfo != null) {
  1041. logger.info("[录像下载] 更新invite消息中的stream信息");
  1042. inviteInfo.setStatus(InviteSessionStatus.ok);
  1043. inviteInfo.setStreamInfo(streamInfo);
  1044. inviteStreamService.updateInviteInfo(inviteInfo);
  1045. }
  1046. }
  1047. return streamInfo;
  1048. }
  1049. public StreamInfo onPublishHandler(MediaServerItem mediaServerItem, OnStreamChangedHookParam hookParam, String deviceId, String channelId) {
  1050. StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(mediaServerItem, "rtp", hookParam.getStream(), hookParam.getTracks(), null);
  1051. streamInfo.setDeviceID(deviceId);
  1052. streamInfo.setChannelId(channelId);
  1053. return streamInfo;
  1054. }
  1055. @Override
  1056. public void zlmServerOffline(String mediaServerId) {
  1057. // 处理正在向上推流的上级平台
  1058. List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServer(null);
  1059. if (sendRtpItems.size() > 0) {
  1060. for (SendRtpItem sendRtpItem : sendRtpItems) {
  1061. if (sendRtpItem.getMediaServerId().equals(mediaServerId)) {
  1062. ParentPlatform platform = storager.queryParentPlatByServerGBId(sendRtpItem.getPlatformId());
  1063. try {
  1064. sipCommanderFroPlatform.streamByeCmd(platform, sendRtpItem.getCallId());
  1065. } catch (SipException | InvalidArgumentException | ParseException e) {
  1066. logger.error("[命令发送失败] 国标级联 发送BYE: {}", e.getMessage());
  1067. }
  1068. }
  1069. }
  1070. }
  1071. // 处理正在观看的国标设备
  1072. List<SsrcTransaction> allSsrc = streamSession.getAllSsrc();
  1073. if (allSsrc.size() > 0) {
  1074. for (SsrcTransaction ssrcTransaction : allSsrc) {
  1075. if (ssrcTransaction.getMediaServerId().equals(mediaServerId)) {
  1076. Device device = deviceService.getDevice(ssrcTransaction.getDeviceId());
  1077. if (device == null) {
  1078. continue;
  1079. }
  1080. try {
  1081. cmder.streamByeCmd(device, ssrcTransaction.getChannelId(),
  1082. ssrcTransaction.getStream(), null);
  1083. } catch (InvalidArgumentException | ParseException | SipException |
  1084. SsrcTransactionNotFoundException e) {
  1085. logger.error("[zlm离线]为正在使用此zlm的设备, 发送BYE失败 {}", e.getMessage());
  1086. }
  1087. }
  1088. }
  1089. }
  1090. }
  1091. @Override
  1092. public AudioBroadcastResult audioBroadcast(Device device, String channelId, Boolean broadcastMode) {
  1093. // TODO 必须多端口模式才支持语音喊话鹤语音对讲
  1094. if (device == null || channelId == null) {
  1095. return null;
  1096. }
  1097. logger.info("[语音喊话] device: {}, channel: {}", device.getDeviceId(), channelId);
  1098. DeviceChannel deviceChannel = storager.queryChannel(device.getDeviceId(), channelId);
  1099. if (deviceChannel == null) {
  1100. logger.warn("开启语音广播的时候未找到通道: {}", channelId);
  1101. return null;
  1102. }
  1103. MediaServerItem mediaServerItem = mediaServerService.getMediaServerForMinimumLoad(null);
  1104. if (broadcastMode == null) {
  1105. broadcastMode = true;
  1106. }
  1107. String app = broadcastMode?"broadcast":"talk";
  1108. String stream = device.getDeviceId() + "_" + channelId;
  1109. AudioBroadcastResult audioBroadcastResult = new AudioBroadcastResult();
  1110. audioBroadcastResult.setApp(app);
  1111. audioBroadcastResult.setStream(stream);
  1112. audioBroadcastResult.setStreamInfo(new StreamContent(mediaService.getStreamInfoByAppAndStream(mediaServerItem, app, stream, null, null, null, false)));
  1113. audioBroadcastResult.setCodec("G.711");
  1114. return audioBroadcastResult;
  1115. }
  1116. @Override
  1117. public boolean audioBroadcastCmd(Device device, String channelId, MediaServerItem mediaServerItem, String app, String stream, int timeout, boolean isFromPlatform, AudioBroadcastEvent event) throws InvalidArgumentException, ParseException, SipException {
  1118. if (device == null || channelId == null) {
  1119. return false;
  1120. }
  1121. logger.info("[语音喊话] device: {}, channel: {}", device.getDeviceId(), channelId);
  1122. DeviceChannel deviceChannel = storager.queryChannel(device.getDeviceId(), channelId);
  1123. if (deviceChannel == null) {
  1124. logger.warn("开启语音广播的时候未找到通道: {}", channelId);
  1125. event.call("开启语音广播的时候未找到通道");
  1126. return false;
  1127. }
  1128. // 查询通道使用状态
  1129. if (audioBroadcastManager.exit(device.getDeviceId(), channelId)) {
  1130. SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(device.getDeviceId(), channelId, null, null);
  1131. if (sendRtpItem != null && sendRtpItem.isOnlyAudio()) {
  1132. // 查询流是否存在,不存在则认为是异常状态
  1133. Boolean streamReady = zlmServerFactory.isStreamReady(mediaServerItem, sendRtpItem.getApp(), sendRtpItem.getStream());
  1134. if (streamReady) {
  1135. logger.warn("语音广播已经开启: {}", channelId);
  1136. event.call("语音广播已经开启");
  1137. return false;
  1138. } else {
  1139. stopAudioBroadcast(device.getDeviceId(), channelId);
  1140. }
  1141. }
  1142. }
  1143. SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(device.getDeviceId(), channelId, null, null);
  1144. if (sendRtpItem != null) {
  1145. MediaServerItem mediaServer = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  1146. Boolean streamReady = zlmServerFactory.isStreamReady(mediaServer, "rtp", sendRtpItem.getReceiveStream());
  1147. if (streamReady) {
  1148. logger.warn("[语音对讲] 进行中: {}", channelId);
  1149. event.call("语音对讲进行中");
  1150. return false;
  1151. } else {
  1152. stopTalk(device, channelId);
  1153. }
  1154. }
  1155. // 发送通知
  1156. cmder.audioBroadcastCmd(device, channelId, eventResultForOk -> {
  1157. // 发送成功
  1158. AudioBroadcastCatch audioBroadcastCatch = new AudioBroadcastCatch(device.getDeviceId(), channelId, mediaServerItem, app, stream, event, AudioBroadcastCatchStatus.Ready, isFromPlatform);
  1159. audioBroadcastManager.update(audioBroadcastCatch);
  1160. }, eventResultForError -> {
  1161. // 发送失败
  1162. logger.error("语音广播发送失败: {}:{}", channelId, eventResultForError.msg);
  1163. event.call("语音广播发送失败");
  1164. stopAudioBroadcast(device.getDeviceId(), channelId);
  1165. });
  1166. return true;
  1167. }
  1168. @Override
  1169. public boolean audioBroadcastInUse(Device device, String channelId) {
  1170. if (audioBroadcastManager.exit(device.getDeviceId(), channelId)) {
  1171. SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(device.getDeviceId(), channelId, null, null);
  1172. if (sendRtpItem != null && sendRtpItem.isOnlyAudio()) {
  1173. // 查询流是否存在,不存在则认为是异常状态
  1174. MediaServerItem mediaServerServiceOne = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  1175. Boolean streamReady = zlmServerFactory.isStreamReady(mediaServerServiceOne, sendRtpItem.getApp(), sendRtpItem.getStream());
  1176. if (streamReady) {
  1177. logger.warn("语音广播通道使用中: {}", channelId);
  1178. return true;
  1179. }
  1180. }
  1181. }
  1182. return false;
  1183. }
  1184. @Override
  1185. public void stopAudioBroadcast(String deviceId, String channelId) {
  1186. logger.info("[停止对讲] 设备:{}, 通道:{}", deviceId, channelId);
  1187. List<AudioBroadcastCatch> audioBroadcastCatchList = new ArrayList<>();
  1188. if (channelId == null) {
  1189. audioBroadcastCatchList.addAll(audioBroadcastManager.get(deviceId));
  1190. } else {
  1191. audioBroadcastCatchList.add(audioBroadcastManager.get(deviceId, channelId));
  1192. }
  1193. if (audioBroadcastCatchList.size() > 0) {
  1194. for (AudioBroadcastCatch audioBroadcastCatch : audioBroadcastCatchList) {
  1195. Device device = deviceService.getDevice(deviceId);
  1196. if (device == null || audioBroadcastCatch == null) {
  1197. return;
  1198. }
  1199. SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(deviceId, audioBroadcastCatch.getChannelId(), null, null);
  1200. if (sendRtpItem != null) {
  1201. redisCatchStorage.deleteSendRTPServer(deviceId, sendRtpItem.getChannelId(), null, null);
  1202. MediaServerItem mediaInfo = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  1203. Map<String, Object> param = new HashMap<>();
  1204. param.put("vhost", "__defaultVhost__");
  1205. param.put("app", sendRtpItem.getApp());
  1206. param.put("stream", sendRtpItem.getStream());
  1207. zlmresTfulUtils.stopSendRtp(mediaInfo, param);
  1208. try {
  1209. cmder.streamByeCmd(device, sendRtpItem.getChannelId(), audioBroadcastCatch.getSipTransactionInfo(), null);
  1210. } catch (InvalidArgumentException | ParseException | SipException |
  1211. SsrcTransactionNotFoundException e) {
  1212. logger.error("[消息发送失败] 发送语音喊话BYE失败");
  1213. }
  1214. }
  1215. audioBroadcastManager.del(deviceId, channelId);
  1216. }
  1217. }
  1218. }
  1219. @Override
  1220. public void zlmServerOnline(String mediaServerId) {
  1221. // TODO 查找之前的点播,流如果不存在则给下级发送bye
  1222. // MediaServerItem mediaServerItem = mediaServerService.getOne(mediaServerId);
  1223. // zlmresTfulUtils.getMediaList(mediaServerItem, (mediaList ->{
  1224. // Integer code = mediaList.getInteger("code");
  1225. // if (code == 0) {
  1226. // JSONArray data = mediaList.getJSONArray("data");
  1227. // if (data == null || data.size() == 0) {
  1228. // zlmServerOffline(mediaServerId);
  1229. // }else {
  1230. // Map<String, JSONObject> mediaListMap = new HashMap<>();
  1231. // for (int i = 0; i < data.size(); i++) {
  1232. // JSONObject json = data.getJSONObject(i);
  1233. // String app = json.getString("app");
  1234. // if ("rtp".equals(app)) {
  1235. // String stream = json.getString("stream");
  1236. // if (mediaListMap.get(stream) != null) {
  1237. // continue;
  1238. // }
  1239. // mediaListMap.put(stream, json);
  1240. // // 处理正在观看的国标设备
  1241. // List<SsrcTransaction> ssrcTransactions = streamSession.getSsrcTransactionForAll(null, null, null, stream);
  1242. // if (ssrcTransactions.size() > 0) {
  1243. // for (SsrcTransaction ssrcTransaction : ssrcTransactions) {
  1244. // if(ssrcTransaction.getMediaServerId().equals(mediaServerId)) {
  1245. // cmder.streamByeCmd(ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId(),
  1246. // ssrcTransaction.getStream(), null);
  1247. // }
  1248. // }
  1249. // }
  1250. // }
  1251. // }
  1252. // if (mediaListMap.size() > 0 ) {
  1253. // // 处理正在向上推流的上级平台
  1254. // List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServer(null);
  1255. // if (sendRtpItems.size() > 0) {
  1256. // for (SendRtpItem sendRtpItem : sendRtpItems) {
  1257. // if (sendRtpItem.getMediaServerId().equals(mediaServerId)) {
  1258. // if (mediaListMap.get(sendRtpItem.getStreamId()) == null) {
  1259. // ParentPlatform platform = storager.queryPlatformByServerGBId(sendRtpItem.getPlatformId());
  1260. // sipCommanderFroPlatform.streamByeCmd(platform, sendRtpItem.getCallId());
  1261. // }
  1262. // }
  1263. // }
  1264. // }
  1265. // }
  1266. // }
  1267. // }
  1268. // }));
  1269. }
  1270. @Override
  1271. public void pauseRtp(String streamId) throws ServiceException, InvalidArgumentException, ParseException, SipException {
  1272. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByStream(InviteSessionType.PLAYBACK, streamId);
  1273. if (null == inviteInfo || inviteInfo.getStreamInfo() == null) {
  1274. logger.warn("streamId不存在!");
  1275. throw new ServiceException("streamId不存在");
  1276. }
  1277. inviteInfo.getStreamInfo().setPause(true);
  1278. inviteStreamService.updateInviteInfo(inviteInfo);
  1279. MediaServerItem mediaServerItem = mediaServerService.getOne(inviteInfo.getStreamInfo().getMediaServerId());
  1280. if (null == mediaServerItem) {
  1281. logger.warn("mediaServer 不存在!");
  1282. throw new ServiceException("mediaServer不存在");
  1283. }
  1284. // zlm 暂停RTP超时检查
  1285. JSONObject jsonObject = zlmresTfulUtils.pauseRtpCheck(mediaServerItem, streamId);
  1286. if (jsonObject == null || jsonObject.getInteger("code") != 0) {
  1287. throw new ServiceException("暂停RTP接收失败");
  1288. }
  1289. Device device = storager.queryVideoDevice(inviteInfo.getDeviceId());
  1290. cmder.playPauseCmd(device, inviteInfo.getStreamInfo());
  1291. }
  1292. @Override
  1293. public void resumeRtp(String streamId) throws ServiceException, InvalidArgumentException, ParseException, SipException {
  1294. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByStream(InviteSessionType.PLAYBACK, streamId);
  1295. if (null == inviteInfo || inviteInfo.getStreamInfo() == null) {
  1296. logger.warn("streamId不存在!");
  1297. throw new ServiceException("streamId不存在");
  1298. }
  1299. inviteInfo.getStreamInfo().setPause(false);
  1300. inviteStreamService.updateInviteInfo(inviteInfo);
  1301. MediaServerItem mediaServerItem = mediaServerService.getOne(inviteInfo.getStreamInfo().getMediaServerId());
  1302. if (null == mediaServerItem) {
  1303. logger.warn("mediaServer 不存在!");
  1304. throw new ServiceException("mediaServer不存在");
  1305. }
  1306. // zlm 暂停RTP超时检查
  1307. JSONObject jsonObject = zlmresTfulUtils.resumeRtpCheck(mediaServerItem, streamId);
  1308. if (jsonObject == null || jsonObject.getInteger("code") != 0) {
  1309. throw new ServiceException("继续RTP接收失败");
  1310. }
  1311. Device device = storager.queryVideoDevice(inviteInfo.getDeviceId());
  1312. cmder.playResumeCmd(device, inviteInfo.getStreamInfo());
  1313. }
  1314. @Override
  1315. public void startPushStream(SendRtpItem sendRtpItem, SIPResponse sipResponse, ParentPlatform platform, CallIdHeader callIdHeader) {
  1316. // 开始发流
  1317. String is_Udp = sendRtpItem.isTcp() ? "0" : "1";
  1318. MediaServerItem mediaInfo = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  1319. logger.info("[开始推流] rtp/{}, 目标={}:{},SSRC={}, RTCP={}", sendRtpItem.getStream(),
  1320. sendRtpItem.getIp(), sendRtpItem.getPort(), sendRtpItem.getSsrc(), sendRtpItem.isRtcp());
  1321. Map<String, Object> param = new HashMap<>(12);
  1322. param.put("vhost", "__defaultVhost__");
  1323. param.put("app", sendRtpItem.getApp());
  1324. param.put("stream", sendRtpItem.getStream());
  1325. param.put("ssrc", sendRtpItem.getSsrc());
  1326. param.put("src_port", sendRtpItem.getLocalPort());
  1327. param.put("pt", sendRtpItem.getPt());
  1328. param.put("use_ps", sendRtpItem.isUsePs() ? "1" : "0");
  1329. param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0");
  1330. param.put("is_udp", is_Udp);
  1331. if (!sendRtpItem.isTcp()) {
  1332. // udp模式下开启rtcp保活
  1333. param.put("udp_rtcp_timeout", sendRtpItem.isRtcp() ? "1" : "0");
  1334. }
  1335. if (mediaInfo == null) {
  1336. RequestPushStreamMsg requestPushStreamMsg = RequestPushStreamMsg.getInstance(
  1337. sendRtpItem.getMediaServerId(), sendRtpItem.getApp(), sendRtpItem.getStream(),
  1338. sendRtpItem.getIp(), sendRtpItem.getPort(), sendRtpItem.getSsrc(), sendRtpItem.isTcp(),
  1339. sendRtpItem.getLocalPort(), sendRtpItem.getPt(), sendRtpItem.isUsePs(), sendRtpItem.isOnlyAudio());
  1340. redisGbPlayMsgListener.sendMsgForStartSendRtpStream(sendRtpItem.getServerId(), requestPushStreamMsg, json -> {
  1341. startSendRtpStreamHand(sendRtpItem, platform, json, param, callIdHeader);
  1342. });
  1343. } else {
  1344. // 如果是严格模式,需要关闭端口占用
  1345. JSONObject startSendRtpStreamResult = null;
  1346. if (sendRtpItem.getLocalPort() != 0) {
  1347. if (sendRtpItem.isTcpActive()) {
  1348. startSendRtpStreamResult = zlmServerFactory.startSendRtpPassive(mediaInfo, param);
  1349. } else {
  1350. param.put("dst_url", sendRtpItem.getIp());
  1351. param.put("dst_port", sendRtpItem.getPort());
  1352. startSendRtpStreamResult = zlmServerFactory.startSendRtpStream(mediaInfo, param);
  1353. }
  1354. } else {
  1355. if (sendRtpItem.isTcpActive()) {
  1356. startSendRtpStreamResult = zlmServerFactory.startSendRtpPassive(mediaInfo, param);
  1357. } else {
  1358. param.put("dst_url", sendRtpItem.getIp());
  1359. param.put("dst_port", sendRtpItem.getPort());
  1360. startSendRtpStreamResult = zlmServerFactory.startSendRtpStream(mediaInfo, param);
  1361. }
  1362. }
  1363. if (startSendRtpStreamResult != null) {
  1364. startSendRtpStreamHand(sendRtpItem, platform, startSendRtpStreamResult, param, callIdHeader);
  1365. }
  1366. }
  1367. }
  1368. @Override
  1369. public void startSendRtpStreamHand(SendRtpItem sendRtpItem, Object correlationInfo,
  1370. JSONObject jsonObject, Map<String, Object> param, CallIdHeader callIdHeader) {
  1371. if (jsonObject == null) {
  1372. logger.error("RTP推流失败: 请检查ZLM服务");
  1373. } else if (jsonObject.getInteger("code") == 0) {
  1374. logger.info("调用ZLM推流接口, 结果: {}", jsonObject);
  1375. logger.info("RTP推流成功[ {}/{} ],{}->{}, ", param.get("app"), param.get("stream"), jsonObject.getString("local_port"),
  1376. sendRtpItem.isTcpActive()?"被动发流": param.get("dst_url") + ":" + param.get("dst_port"));
  1377. } else {
  1378. logger.error("RTP推流失败: {}, 参数:{}", jsonObject.getString("msg"), JSONObject.toJSONString(param));
  1379. if (sendRtpItem.isOnlyAudio()) {
  1380. Device device = deviceService.getDevice(sendRtpItem.getDeviceId());
  1381. AudioBroadcastCatch audioBroadcastCatch = audioBroadcastManager.get(sendRtpItem.getDeviceId(), sendRtpItem.getChannelId());
  1382. if (audioBroadcastCatch != null) {
  1383. try {
  1384. cmder.streamByeCmd(device, sendRtpItem.getChannelId(), audioBroadcastCatch.getSipTransactionInfo(), null);
  1385. } catch (SipException | ParseException | InvalidArgumentException |
  1386. SsrcTransactionNotFoundException e) {
  1387. logger.error("[命令发送失败] 停止语音对讲: {}", e.getMessage());
  1388. }
  1389. }
  1390. } else {
  1391. // 向上级平台
  1392. if (correlationInfo instanceof ParentPlatform) {
  1393. try {
  1394. ParentPlatform parentPlatform = (ParentPlatform)correlationInfo;
  1395. commanderForPlatform.streamByeCmd(parentPlatform, callIdHeader.getCallId());
  1396. } catch (SipException | InvalidArgumentException | ParseException e) {
  1397. logger.error("[命令发送失败] 国标级联 发送BYE: {}", e.getMessage());
  1398. }
  1399. }
  1400. }
  1401. }
  1402. }
  1403. @Override
  1404. public void talkCmd(Device device, String channelId, MediaServerItem mediaServerItem, String stream, AudioBroadcastEvent event) {
  1405. if (device == null || channelId == null) {
  1406. return;
  1407. }
  1408. // TODO 必须多端口模式才支持语音喊话鹤语音对讲
  1409. logger.info("[语音对讲] device: {}, channel: {}", device.getDeviceId(), channelId);
  1410. DeviceChannel deviceChannel = storager.queryChannel(device.getDeviceId(), channelId);
  1411. if (deviceChannel == null) {
  1412. logger.warn("开启语音对讲的时候未找到通道: {}", channelId);
  1413. event.call("开启语音对讲的时候未找到通道");
  1414. return;
  1415. }
  1416. // 查询通道使用状态
  1417. if (audioBroadcastManager.exit(device.getDeviceId(), channelId)) {
  1418. SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(device.getDeviceId(), channelId, null, null);
  1419. if (sendRtpItem != null && sendRtpItem.isOnlyAudio()) {
  1420. // 查询流是否存在,不存在则认为是异常状态
  1421. MediaServerItem mediaServer = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  1422. Boolean streamReady = zlmServerFactory.isStreamReady(mediaServer, sendRtpItem.getApp(), sendRtpItem.getStream());
  1423. if (streamReady) {
  1424. logger.warn("[语音对讲] 正在语音广播,无法开启语音通话: {}", channelId);
  1425. event.call("正在语音广播");
  1426. return;
  1427. } else {
  1428. stopAudioBroadcast(device.getDeviceId(), channelId);
  1429. }
  1430. }
  1431. }
  1432. SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(device.getDeviceId(), channelId, stream, null);
  1433. if (sendRtpItem != null) {
  1434. MediaServerItem mediaServer = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  1435. Boolean streamReady = zlmServerFactory.isStreamReady(mediaServer, "rtp", sendRtpItem.getReceiveStream());
  1436. if (streamReady) {
  1437. logger.warn("[语音对讲] 进行中: {}", channelId);
  1438. event.call("语音对讲进行中");
  1439. return;
  1440. } else {
  1441. stopTalk(device, channelId);
  1442. }
  1443. }
  1444. talk(mediaServerItem, device, channelId, stream, (mediaServerItem1, hookParam) -> {
  1445. logger.info("[语音对讲] 收到设备发来的流");
  1446. }, eventResult -> {
  1447. logger.warn("[语音对讲] 失败,{}/{}, 错误码 {} {}", device.getDeviceId(), channelId, eventResult.statusCode, eventResult.msg);
  1448. event.call("失败,错误码 " + eventResult.statusCode + ", " + eventResult.msg);
  1449. }, () -> {
  1450. logger.warn("[语音对讲] 失败,{}/{} 超时", device.getDeviceId(), channelId);
  1451. event.call("失败,超时 ");
  1452. stopTalk(device, channelId);
  1453. }, errorMsg -> {
  1454. logger.warn("[语音对讲] 失败,{}/{} {}", device.getDeviceId(), channelId, errorMsg);
  1455. event.call(errorMsg);
  1456. stopTalk(device, channelId);
  1457. });
  1458. }
  1459. private void stopTalk(Device device, String channelId) {
  1460. stopTalk(device, channelId, null);
  1461. }
  1462. @Override
  1463. public void stopTalk(Device device, String channelId, Boolean streamIsReady) {
  1464. logger.info("[语音对讲] 停止, {}/{}", device.getDeviceId(), channelId);
  1465. SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(device.getDeviceId(), channelId, null, null);
  1466. if (sendRtpItem == null) {
  1467. logger.info("[语音对讲] 停止失败, 未找到发送信息,可能已经停止");
  1468. return;
  1469. }
  1470. // 停止向设备推流
  1471. String mediaServerId = sendRtpItem.getMediaServerId();
  1472. if (mediaServerId == null) {
  1473. return;
  1474. }
  1475. MediaServerItem mediaServer = mediaServerService.getOne(mediaServerId);
  1476. if (streamIsReady == null || streamIsReady) {
  1477. Map<String, Object> param = new HashMap<>();
  1478. param.put("vhost", "__defaultVhost__");
  1479. param.put("app", sendRtpItem.getApp());
  1480. param.put("stream", sendRtpItem.getStream());
  1481. param.put("ssrc", sendRtpItem.getSsrc());
  1482. zlmServerFactory.stopSendRtpStream(mediaServer, param);
  1483. }
  1484. ssrcFactory.releaseSsrc(mediaServerId, sendRtpItem.getSsrc());
  1485. SsrcTransaction ssrcTransaction = streamSession.getSsrcTransaction(device.getDeviceId(), channelId, null, sendRtpItem.getStream());
  1486. if (ssrcTransaction != null) {
  1487. try {
  1488. cmder.streamByeCmd(device, channelId, sendRtpItem.getStream(), null);
  1489. } catch (InvalidArgumentException | ParseException | SipException | SsrcTransactionNotFoundException e) {
  1490. logger.info("[语音对讲] 停止消息发送失败,可能已经停止");
  1491. }
  1492. }
  1493. redisCatchStorage.deleteSendRTPServer(device.getDeviceId(), channelId,null, null);
  1494. }
  1495. @Override
  1496. public void getSnap(String deviceId, String channelId, String fileName, ErrorCallback errorCallback) {
  1497. Device device = deviceService.getDevice(deviceId);
  1498. if (device == null) {
  1499. errorCallback.run(InviteErrorCode.ERROR_FOR_PARAMETER_ERROR.getCode(), InviteErrorCode.ERROR_FOR_PARAMETER_ERROR.getMsg(), null);
  1500. return;
  1501. }
  1502. InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId);
  1503. if (inviteInfo != null) {
  1504. if (inviteInfo.getStreamInfo() != null) {
  1505. // 已存在线直接截图
  1506. MediaServerItem mediaServerItemInuse = mediaServerService.getOne(inviteInfo.getStreamInfo().getMediaServerId());
  1507. String streamUrl;
  1508. if (mediaServerItemInuse.getRtspPort() != 0) {
  1509. streamUrl = String.format("rtsp://127.0.0.1:%s/%s/%s", mediaServerItemInuse.getRtspPort(), "rtp", inviteInfo.getStreamInfo().getStream());
  1510. }else {
  1511. streamUrl = String.format("http://127.0.0.1:%s/%s/%s.live.mp4", mediaServerItemInuse.getHttpPort(), "rtp", inviteInfo.getStreamInfo().getStream());
  1512. }
  1513. String path = "snap";
  1514. // 请求截图
  1515. logger.info("[请求截图]: " + fileName);
  1516. zlmresTfulUtils.getSnap(mediaServerItemInuse, streamUrl, 15, 1, path, fileName);
  1517. File snapFile = new File(path + File.separator + fileName);
  1518. if (snapFile.exists()) {
  1519. errorCallback.run(InviteErrorCode.SUCCESS.getCode(), InviteErrorCode.SUCCESS.getMsg(), snapFile.getAbsoluteFile());
  1520. }else {
  1521. errorCallback.run(InviteErrorCode.FAIL.getCode(), InviteErrorCode.FAIL.getMsg(), null);
  1522. }
  1523. return;
  1524. }
  1525. }
  1526. MediaServerItem newMediaServerItem = getNewMediaServerItem(device);
  1527. play(newMediaServerItem, deviceId, channelId, null, (code, msg, data)->{
  1528. if (code == InviteErrorCode.SUCCESS.getCode()) {
  1529. InviteInfo inviteInfoForPlay = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId);
  1530. if (inviteInfoForPlay != null && inviteInfoForPlay.getStreamInfo() != null) {
  1531. getSnap(deviceId, channelId, fileName, errorCallback);
  1532. }else {
  1533. errorCallback.run(InviteErrorCode.FAIL.getCode(), InviteErrorCode.FAIL.getMsg(), null);
  1534. }
  1535. }else {
  1536. errorCallback.run(InviteErrorCode.FAIL.getCode(), InviteErrorCode.FAIL.getMsg(), null);
  1537. }
  1538. });
  1539. }
  1540. }