PlayServiceImpl.java 92 KB

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