PlayServiceImpl.java 88 KB

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