PlayServiceImpl.java 96 KB

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