StreamProxyServiceImpl.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.baomidou.dynamic.datasource.annotation.DS;
  4. import com.genersoft.iot.vmp.common.GeneralCallback;
  5. import com.genersoft.iot.vmp.common.StreamInfo;
  6. import com.genersoft.iot.vmp.conf.DynamicTask;
  7. import com.genersoft.iot.vmp.conf.UserSetting;
  8. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  9. import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
  10. import com.genersoft.iot.vmp.media.bean.MediaInfo;
  11. import com.genersoft.iot.vmp.media.event.MediaArrivalEvent;
  12. import com.genersoft.iot.vmp.media.event.MediaDepartureEvent;
  13. import com.genersoft.iot.vmp.media.event.MediaNotFoundEvent;
  14. import com.genersoft.iot.vmp.media.service.IMediaServerService;
  15. import com.genersoft.iot.vmp.media.zlm.ZLMServerFactory;
  16. import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe;
  17. import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory;
  18. import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForStreamChange;
  19. import com.genersoft.iot.vmp.media.zlm.dto.MediaServer;
  20. import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
  21. import com.genersoft.iot.vmp.media.zlm.dto.hook.HookResult;
  22. import com.genersoft.iot.vmp.media.zlm.dto.hook.OnStreamChangedHookParam;
  23. import com.genersoft.iot.vmp.service.IGbStreamService;
  24. import com.genersoft.iot.vmp.service.IMediaService;
  25. import com.genersoft.iot.vmp.service.IStreamProxyService;
  26. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  27. import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
  28. import com.genersoft.iot.vmp.storager.dao.GbStreamMapper;
  29. import com.genersoft.iot.vmp.storager.dao.PlatformGbStreamMapper;
  30. import com.genersoft.iot.vmp.storager.dao.StreamProxyMapper;
  31. import com.genersoft.iot.vmp.utils.DateUtil;
  32. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  33. import com.genersoft.iot.vmp.vmanager.bean.ResourceBaseInfo;
  34. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  35. import com.github.pagehelper.PageInfo;
  36. import org.slf4j.Logger;
  37. import org.slf4j.LoggerFactory;
  38. import org.springframework.beans.factory.annotation.Autowired;
  39. import org.springframework.context.event.EventListener;
  40. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  41. import org.springframework.scheduling.annotation.Async;
  42. import org.springframework.scheduling.annotation.Scheduled;
  43. import org.springframework.stereotype.Service;
  44. import org.springframework.transaction.TransactionDefinition;
  45. import org.springframework.transaction.TransactionStatus;
  46. import org.springframework.util.CollectionUtils;
  47. import org.springframework.util.ObjectUtils;
  48. import org.springframework.web.context.request.async.DeferredResult;
  49. import java.util.HashMap;
  50. import java.util.List;
  51. import java.util.Map;
  52. import java.util.UUID;
  53. import java.util.function.Function;
  54. import java.util.stream.Collectors;
  55. /**
  56. * 视频代理业务
  57. */
  58. @Service
  59. @DS("master")
  60. public class StreamProxyServiceImpl implements IStreamProxyService {
  61. private final static Logger logger = LoggerFactory.getLogger(StreamProxyServiceImpl.class);
  62. @Autowired
  63. private IVideoManagerStorage videoManagerStorager;
  64. @Autowired
  65. private IMediaService mediaService;
  66. @Autowired
  67. private ZLMServerFactory zlmServerFactory;
  68. @Autowired
  69. private StreamProxyMapper streamProxyMapper;
  70. @Autowired
  71. private IRedisCatchStorage redisCatchStorage;
  72. @Autowired
  73. private IVideoManagerStorage storager;
  74. @Autowired
  75. private UserSetting userSetting;
  76. @Autowired
  77. private GbStreamMapper gbStreamMapper;
  78. @Autowired
  79. private PlatformGbStreamMapper platformGbStreamMapper;
  80. @Autowired
  81. private IGbStreamService gbStreamService;
  82. @Autowired
  83. private IMediaServerService mediaServerService;
  84. @Autowired
  85. private ZlmHttpHookSubscribe hookSubscribe;
  86. @Autowired
  87. private DynamicTask dynamicTask;
  88. @Autowired
  89. DataSourceTransactionManager dataSourceTransactionManager;
  90. @Autowired
  91. TransactionDefinition transactionDefinition;
  92. /**
  93. * 流到来的处理
  94. */
  95. @Async("taskExecutor")
  96. @org.springframework.context.event.EventListener
  97. public void onApplicationEvent(MediaArrivalEvent event) {
  98. if ("rtsp".equals(event.getSchema())) {
  99. updateStatus(true, event.getApp(), event.getStream());
  100. }
  101. }
  102. /**
  103. * 流离开的处理
  104. */
  105. @Async("taskExecutor")
  106. @EventListener
  107. public void onApplicationEvent(MediaDepartureEvent event) {
  108. if ("rtsp".equals(event.getSchema())) {
  109. updateStatus(false, event.getApp(), event.getStream());
  110. }
  111. }
  112. /**
  113. * 流离开的处理
  114. */
  115. @Async("taskExecutor")
  116. @EventListener
  117. public void onApplicationEvent(MediaNotFoundEvent event) {
  118. if ("rtp".equals(event.getApp())) {
  119. return;
  120. }
  121. // 拉流代理
  122. StreamProxyItem streamProxyByAppAndStream = getStreamProxyByAppAndStream(event.getApp(), event.getStream());
  123. if (streamProxyByAppAndStream != null && streamProxyByAppAndStream.isEnableDisableNoneReader()) {
  124. start(event.getApp(), event.getStream());
  125. }
  126. }
  127. @Override
  128. public void save(StreamProxyItem param, GeneralCallback<StreamInfo> callback) {
  129. MediaServer mediaServer;
  130. if (ObjectUtils.isEmpty(param.getMediaServerId()) || "auto".equals(param.getMediaServerId())){
  131. mediaServer = mediaServerService.getMediaServerForMinimumLoad(null);
  132. }else {
  133. mediaServer = mediaServerService.getOne(param.getMediaServerId());
  134. }
  135. if (mediaServer == null) {
  136. logger.warn("保存代理未找到在线的ZLM...");
  137. throw new ControllerException(ErrorCode.ERROR100.getCode(), "保存代理未找到在线的ZLM");
  138. }
  139. String dstUrl;
  140. if ("ffmpeg".equalsIgnoreCase(param.getType())) {
  141. String ffmpegCmd = mediaServerService.getFfmpegCmd(mediaServer, param.getFfmpegCmdKey());
  142. if (ffmpegCmd == null) {
  143. throw new ControllerException(ErrorCode.ERROR100.getCode(), "ffmpeg拉流代理无法获取ffmpeg cmd");
  144. }
  145. String schema = getSchemaFromFFmpegCmd(ffmpegCmd);
  146. if (schema == null) {
  147. throw new ControllerException(ErrorCode.ERROR100.getCode(), "ffmpeg拉流代理无法从ffmpeg cmd中获取到输出格式");
  148. }
  149. int port;
  150. String schemaForUri;
  151. if (schema.equalsIgnoreCase("rtsp")) {
  152. port = mediaServer.getRtspPort();
  153. schemaForUri = schema;
  154. }else if (schema.equalsIgnoreCase("flv")) {
  155. port = mediaServer.getRtmpPort();
  156. schemaForUri = schema;
  157. }else {
  158. port = mediaServer.getRtmpPort();
  159. schemaForUri = schema;
  160. }
  161. dstUrl = String.format("%s://%s:%s/%s/%s", schemaForUri, "127.0.0.1", port, param.getApp(),
  162. param.getStream());
  163. }else {
  164. dstUrl = String.format("rtsp://%s:%s/%s/%s", "127.0.0.1", mediaServer.getRtspPort(), param.getApp(),
  165. param.getStream());
  166. }
  167. param.setDstUrl(dstUrl);
  168. logger.info("[拉流代理] 输出地址为:{}", dstUrl);
  169. param.setMediaServerId(mediaServer.getId());
  170. boolean saveResult;
  171. // 更新
  172. if (videoManagerStorager.queryStreamProxy(param.getApp(), param.getStream()) != null) {
  173. saveResult = updateStreamProxy(param);
  174. }else { // 新增
  175. saveResult = addStreamProxy(param);
  176. }
  177. if (!saveResult) {
  178. callback.run(ErrorCode.ERROR100.getCode(), "保存失败", null);
  179. return;
  180. }
  181. HookSubscribeForStreamChange hookSubscribeForStreamChange = HookSubscribeFactory.on_stream_changed(param.getApp(), param.getStream(), true, "rtsp", mediaServer.getId());
  182. hookSubscribe.addSubscribe(hookSubscribeForStreamChange, (mediaServerItem, response) -> {
  183. StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(
  184. mediaServer, param.getApp(), param.getStream(), null, null);
  185. callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), streamInfo);
  186. });
  187. if (param.isEnable()) {
  188. String talkKey = UUID.randomUUID().toString();
  189. String delayTalkKey = UUID.randomUUID().toString();
  190. dynamicTask.startDelay(delayTalkKey, ()->{
  191. StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStreamWithCheck(param.getApp(), param.getStream(), mediaServer.getId(), false);
  192. if (streamInfo != null) {
  193. callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), streamInfo);
  194. }else {
  195. dynamicTask.stop(talkKey);
  196. callback.run(ErrorCode.ERROR100.getCode(), "超时", null);
  197. }
  198. }, 7000);
  199. WVPResult<String> result = addStreamProxyToZlm(param);
  200. if (result != null && result.getCode() == 0) {
  201. hookSubscribe.removeSubscribe(hookSubscribeForStreamChange);
  202. dynamicTask.stop(talkKey);
  203. StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(
  204. mediaServer, param.getApp(), param.getStream(), null, null);
  205. callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), streamInfo);
  206. }else {
  207. param.setEnable(false);
  208. // 直接移除
  209. if (param.isEnableRemoveNoneReader()) {
  210. del(param.getApp(), param.getStream());
  211. }else {
  212. updateStreamProxy(param);
  213. }
  214. if (result == null){
  215. callback.run(ErrorCode.ERROR100.getCode(), "记录已保存,启用失败", null);
  216. }else {
  217. callback.run(ErrorCode.ERROR100.getCode(), result.getMsg(), null);
  218. }
  219. }
  220. }
  221. else{
  222. StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(
  223. mediaServer, param.getApp(), param.getStream(), null, null);
  224. callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), streamInfo);
  225. }
  226. }
  227. private String getSchemaFromFFmpegCmd(String ffmpegCmd) {
  228. ffmpegCmd = ffmpegCmd.replaceAll(" + ", " ");
  229. String[] paramArray = ffmpegCmd.split(" ");
  230. if (paramArray.length == 0) {
  231. return null;
  232. }
  233. for (int i = 0; i < paramArray.length; i++) {
  234. if (paramArray[i].equalsIgnoreCase("-f")) {
  235. if (i + 1 < paramArray.length - 1) {
  236. return paramArray[i+1];
  237. }else {
  238. return null;
  239. }
  240. }
  241. }
  242. return null;
  243. }
  244. /**
  245. * 新增代理流
  246. * @param streamProxyItem
  247. * @return
  248. */
  249. private boolean addStreamProxy(StreamProxyItem streamProxyItem) {
  250. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  251. boolean result = false;
  252. streamProxyItem.setStreamType("proxy");
  253. streamProxyItem.setStatus(true);
  254. String now = DateUtil.getNow();
  255. streamProxyItem.setCreateTime(now);
  256. try {
  257. if (streamProxyMapper.add(streamProxyItem) > 0) {
  258. if (!ObjectUtils.isEmpty(streamProxyItem.getGbId())) {
  259. if (gbStreamMapper.add(streamProxyItem) < 0) {
  260. //事务回滚
  261. dataSourceTransactionManager.rollback(transactionStatus);
  262. return false;
  263. }
  264. }
  265. }else {
  266. //事务回滚
  267. dataSourceTransactionManager.rollback(transactionStatus);
  268. return false;
  269. }
  270. result = true;
  271. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  272. }catch (Exception e) {
  273. logger.error("向数据库添加流代理失败:", e);
  274. dataSourceTransactionManager.rollback(transactionStatus);
  275. }
  276. return result;
  277. }
  278. /**
  279. * 更新代理流
  280. * @param streamProxyItem
  281. * @return
  282. */
  283. @Override
  284. public boolean updateStreamProxy(StreamProxyItem streamProxyItem) {
  285. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  286. boolean result = false;
  287. streamProxyItem.setStreamType("proxy");
  288. try {
  289. if (streamProxyMapper.update(streamProxyItem) > 0) {
  290. if (!ObjectUtils.isEmpty(streamProxyItem.getGbId())) {
  291. if (gbStreamMapper.updateByAppAndStream(streamProxyItem) == 0) {
  292. //事务回滚
  293. dataSourceTransactionManager.rollback(transactionStatus);
  294. return false;
  295. }
  296. }
  297. } else {
  298. //事务回滚
  299. dataSourceTransactionManager.rollback(transactionStatus);
  300. return false;
  301. }
  302. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  303. result = true;
  304. }catch (Exception e) {
  305. logger.error("未处理的异常 ", e);
  306. dataSourceTransactionManager.rollback(transactionStatus);
  307. }
  308. return result;
  309. }
  310. @Override
  311. public WVPResult<String> addStreamProxyToZlm(StreamProxyItem param) {
  312. WVPResult<String> result = null;
  313. MediaServer mediaServer = null;
  314. if (param.getMediaServerId() == null) {
  315. logger.warn("添加代理时MediaServerId 为null");
  316. return null;
  317. }else {
  318. mediaServer = mediaServerService.getOne(param.getMediaServerId());
  319. }
  320. if (mediaServer == null) {
  321. return null;
  322. }
  323. if (zlmServerFactory.isStreamReady(mediaServer, param.getApp(), param.getStream())) {
  324. mediaServerService.closeStreams(mediaServer, param.getApp(), param.getStream());
  325. }
  326. String msgResult;
  327. if ("ffmpeg".equalsIgnoreCase(param.getType())){
  328. result = mediaServerService.addFFmpegSource(mediaServer, param.getSrcUrl().trim(), param.getDstUrl(),
  329. param.getTimeoutMs(), param.isEnableAudio(), param.isEnableMp4(),
  330. param.getFfmpegCmdKey());
  331. }else {
  332. result = mediaServerService.addStreamProxy(mediaServer, param.getApp(), param.getStream(), param.getUrl().trim(),
  333. param.isEnableAudio(), param.isEnableMp4(), param.getRtpType());
  334. }
  335. System.out.println("addStreamProxyToZlm====");
  336. System.out.println(result);
  337. if (result != null && result.getCode() == 0) {
  338. String key = result.getData();
  339. if (key == null) {
  340. logger.warn("[获取拉流代理的结果数据Data] 失败: {}", result );
  341. return result;
  342. }
  343. param.setStreamKey(key);
  344. streamProxyMapper.update(param);
  345. }
  346. return result;
  347. }
  348. @Override
  349. public Boolean removeStreamProxyFromZlm(StreamProxyItem param) {
  350. if (param ==null) {
  351. return null;
  352. }
  353. MediaServer mediaServer = mediaServerService.getOne(param.getMediaServerId());
  354. Boolean result = false;
  355. if ("ffmpeg".equalsIgnoreCase(param.getType())){
  356. result = mediaServerService.delFFmpegSource(mediaServer, param.getStreamKey());
  357. }else {
  358. result = mediaServerService.delStreamProxy(mediaServer, param.getStreamKey());
  359. }
  360. return result;
  361. }
  362. @Override
  363. public PageInfo<StreamProxyItem> getAll(Integer page, Integer count) {
  364. return videoManagerStorager.queryStreamProxyList(page, count);
  365. }
  366. @Override
  367. public void del(String app, String stream) {
  368. StreamProxyItem streamProxyItem = videoManagerStorager.queryStreamProxy(app, stream);
  369. if (streamProxyItem != null) {
  370. gbStreamService.sendCatalogMsg(streamProxyItem, CatalogEvent.DEL);
  371. // 如果关联了国标那么移除关联
  372. platformGbStreamMapper.delByAppAndStream(app, stream);
  373. gbStreamMapper.del(app, stream);
  374. videoManagerStorager.deleteStreamProxy(app, stream);
  375. redisCatchStorage.removeStream(streamProxyItem.getMediaServerId(), "PULL", app, stream);
  376. Boolean result = removeStreamProxyFromZlm(streamProxyItem);
  377. if (result != null && result) {
  378. logger.info("[移除代理]: 代理: {}/{}, 从zlm移除成功", app, stream);
  379. }else {
  380. logger.info("[移除代理]: 代理: {}/{}, 从zlm移除失败", app, stream);
  381. }
  382. }
  383. }
  384. @Override
  385. public boolean start(String app, String stream) {
  386. boolean result = false;
  387. StreamProxyItem streamProxy = videoManagerStorager.queryStreamProxy(app, stream);
  388. if (streamProxy != null && !streamProxy.isEnable() ) {
  389. WVPResult<String> wvpResult = addStreamProxyToZlm(streamProxy);
  390. if (wvpResult == null) {
  391. return false;
  392. }
  393. if (wvpResult.getCode() == 0) {
  394. result = true;
  395. streamProxy.setEnable(true);
  396. updateStreamProxy(streamProxy);
  397. }else {
  398. logger.info("启用代理失败: {}/{}->{}({})", app, stream, wvpResult.getMsg(),
  399. streamProxy.getSrcUrl() == null? streamProxy.getUrl():streamProxy.getSrcUrl());
  400. }
  401. } else if (streamProxy != null && streamProxy.isEnable()) {
  402. return true ;
  403. }
  404. return result;
  405. }
  406. @Override
  407. public boolean stop(String app, String stream) {
  408. boolean result = false;
  409. StreamProxyItem streamProxyDto = videoManagerStorager.queryStreamProxy(app, stream);
  410. if (streamProxyDto != null && streamProxyDto.isEnable()) {
  411. Boolean removed = removeStreamProxyFromZlm(streamProxyDto);
  412. if (removed != null && removed) {
  413. streamProxyDto.setEnable(false);
  414. result = updateStreamProxy(streamProxyDto);
  415. }
  416. }
  417. return result;
  418. }
  419. @Override
  420. public Map<String, String> getFFmpegCMDs(MediaServer mediaServer) {
  421. return mediaServerService.getFFmpegCMDs(mediaServer);
  422. }
  423. @Override
  424. public StreamProxyItem getStreamProxyByAppAndStream(String app, String streamId) {
  425. return videoManagerStorager.getStreamProxyByAppAndStream(app, streamId);
  426. }
  427. @Override
  428. public void zlmServerOnline(String mediaServerId) {
  429. // 移除开启了无人观看自动移除的流
  430. List<StreamProxyItem> streamProxyItemList = streamProxyMapper.selectAutoRemoveItemByMediaServerId(mediaServerId);
  431. if (streamProxyItemList.size() > 0) {
  432. gbStreamMapper.batchDel(streamProxyItemList);
  433. }
  434. streamProxyMapper.deleteAutoRemoveItemByMediaServerId(mediaServerId);
  435. // 移除拉流代理生成的流信息
  436. syncPullStream(mediaServerId);
  437. // 恢复流代理, 只查找这个这个流媒体
  438. List<StreamProxyItem> streamProxyListForEnable = storager.getStreamProxyListForEnableInMediaServer(
  439. mediaServerId, true);
  440. for (StreamProxyItem streamProxyDto : streamProxyListForEnable) {
  441. logger.info("恢复流代理," + streamProxyDto.getApp() + "/" + streamProxyDto.getStream());
  442. WVPResult<String> wvpResult = addStreamProxyToZlm(streamProxyDto);
  443. if (wvpResult == null) {
  444. // 设置为离线
  445. logger.info("恢复流代理失败" + streamProxyDto.getApp() + "/" + streamProxyDto.getStream());
  446. updateStatus(false, streamProxyDto.getApp(), streamProxyDto.getStream());
  447. }else {
  448. updateStatus(true, streamProxyDto.getApp(), streamProxyDto.getStream());
  449. }
  450. }
  451. }
  452. @Override
  453. public void zlmServerOffline(String mediaServerId) {
  454. // 移除开启了无人观看自动移除的流
  455. List<StreamProxyItem> streamProxyItemList = streamProxyMapper.selectAutoRemoveItemByMediaServerId(mediaServerId);
  456. if (streamProxyItemList.size() > 0) {
  457. gbStreamMapper.batchDel(streamProxyItemList);
  458. }
  459. streamProxyMapper.deleteAutoRemoveItemByMediaServerId(mediaServerId);
  460. // 其他的流设置离线
  461. streamProxyMapper.updateStatusByMediaServerId(mediaServerId, false);
  462. String type = "PULL";
  463. // 发送redis消息
  464. List<OnStreamChangedHookParam> onStreamChangedHookParams = redisCatchStorage.getStreams(mediaServerId, type);
  465. if (onStreamChangedHookParams.size() > 0) {
  466. for (OnStreamChangedHookParam onStreamChangedHookParam : onStreamChangedHookParams) {
  467. JSONObject jsonObject = new JSONObject();
  468. jsonObject.put("serverId", userSetting.getServerId());
  469. jsonObject.put("app", onStreamChangedHookParam.getApp());
  470. jsonObject.put("stream", onStreamChangedHookParam.getStream());
  471. jsonObject.put("register", false);
  472. jsonObject.put("mediaServerId", mediaServerId);
  473. redisCatchStorage.sendStreamChangeMsg(type, jsonObject);
  474. // 移除redis内流的信息
  475. redisCatchStorage.removeStream(mediaServerId, type, onStreamChangedHookParam.getApp(), onStreamChangedHookParam.getStream());
  476. }
  477. }
  478. }
  479. @Override
  480. public void clean() {
  481. }
  482. @Override
  483. public int updateStatus(boolean status, String app, String stream) {
  484. return streamProxyMapper.updateStatus(app, stream, status);
  485. }
  486. private void syncPullStream(String mediaServerId){
  487. MediaServer mediaServer = mediaServerService.getOne(mediaServerId);
  488. if (mediaServer != null) {
  489. List<OnStreamChangedHookParam> allPullStream = redisCatchStorage.getStreams(mediaServerId, "PULL");
  490. if (!allPullStream.isEmpty()) {
  491. List<StreamInfo> mediaList = mediaServerService.getMediaList(mediaServer, null, null, null);
  492. Map<String, StreamInfo> stringStreamInfoMap = new HashMap<>();
  493. if (mediaList != null && !mediaList.isEmpty()) {
  494. for (StreamInfo streamInfo : mediaList) {
  495. stringStreamInfoMap.put(streamInfo.getApp() + streamInfo.getStream(), streamInfo);
  496. }
  497. }
  498. if (stringStreamInfoMap.isEmpty()) {
  499. redisCatchStorage.removeStream(mediaServerId, "PULL");
  500. }else {
  501. for (String key : stringStreamInfoMap.keySet()) {
  502. StreamInfo streamInfo = stringStreamInfoMap.get(key);
  503. if (stringStreamInfoMap.get(streamInfo.getApp() + streamInfo.getStream()) == null) {
  504. redisCatchStorage.removeStream(mediaServerId, "PULL", streamInfo.getApp(),
  505. streamInfo.getStream());
  506. }
  507. }
  508. }
  509. }
  510. }
  511. }
  512. @Override
  513. public ResourceBaseInfo getOverview() {
  514. int total = streamProxyMapper.getAllCount();
  515. int online = streamProxyMapper.getOnline();
  516. return new ResourceBaseInfo(total, online);
  517. }
  518. @Scheduled(cron = "* 0/10 * * * ?")
  519. public void asyncCheckStreamProxyStatus() {
  520. List<MediaServer> all = mediaServerService.getAllOnline();
  521. if (CollectionUtils.isEmpty(all)){
  522. return;
  523. }
  524. Map<String, MediaServer> serverItemMap = all.stream().collect(Collectors.toMap(MediaServer::getId, Function.identity(), (m1, m2) -> m1));
  525. List<StreamProxyItem> list = videoManagerStorager.getStreamProxyListForEnable(true);
  526. if (CollectionUtils.isEmpty(list)){
  527. return;
  528. }
  529. for (StreamProxyItem streamProxyItem : list) {
  530. MediaServer mediaServerItem = serverItemMap.get(streamProxyItem.getMediaServerId());
  531. MediaInfo mediaInfo = mediaServerService.getMediaInfo(mediaServerItem, streamProxyItem.getApp(), streamProxyItem.getStream());
  532. if (mediaInfo == null){
  533. streamProxyItem.setStatus(false);
  534. } else {
  535. if (mediaInfo.getOnline() != null && mediaInfo.getOnline()) {
  536. streamProxyItem.setStatus(true);
  537. } else {
  538. streamProxyItem.setStatus(false);
  539. }
  540. }
  541. updateStreamProxy(streamProxyItem);
  542. }
  543. }
  544. }