PlayServiceImpl.java 81 KB

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