PlayServiceImpl.java 86 KB

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