ZLMRunner.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.genersoft.iot.vmp.media.zlm;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.genersoft.iot.vmp.conf.MediaConfig;
  6. import com.genersoft.iot.vmp.conf.SipConfig;
  7. import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
  8. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  9. //import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
  10. import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
  11. import com.genersoft.iot.vmp.service.IStreamProxyService;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.boot.CommandLineRunner;
  17. import org.springframework.core.annotation.Order;
  18. import org.springframework.stereotype.Component;
  19. import org.springframework.util.StringUtils;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. @Component
  24. @Order(value=1)
  25. public class ZLMRunner implements CommandLineRunner {
  26. private final static Logger logger = LoggerFactory.getLogger(ZLMRunner.class);
  27. @Autowired
  28. private IVideoManagerStorager storager;
  29. @Autowired
  30. private IRedisCatchStorage redisCatchStorage;
  31. @Autowired
  32. private MediaConfig mediaConfig;
  33. @Autowired
  34. private SipConfig sipConfig;
  35. @Value("${server.port}")
  36. private String serverPort;
  37. @Value("${server.ssl.enabled}")
  38. private boolean sslEnabled;
  39. private boolean startGetMedia = false;
  40. @Autowired
  41. private ZLMRESTfulUtils zlmresTfulUtils;
  42. @Autowired
  43. private ZLMMediaListManager zlmMediaListManager;
  44. @Autowired
  45. private ZLMHttpHookSubscribe hookSubscribe;
  46. @Autowired
  47. private ZLMServerManger zlmServerManger;
  48. @Autowired
  49. private IStreamProxyService streamProxyService;
  50. @Override
  51. public void run(String... strings) throws Exception {
  52. // 订阅 zlm启动事件
  53. hookSubscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_server_started,null,(response)->{
  54. ZLMServerConfig ZLMServerConfig = JSONObject.toJavaObject(response, ZLMServerConfig.class);
  55. zLmRunning(ZLMServerConfig);
  56. });
  57. // 获取zlm信息
  58. logger.info("等待zlm接入...");
  59. startGetMedia = true;
  60. ZLMServerConfig ZLMServerConfig = getMediaServerConfig();
  61. if (ZLMServerConfig != null) {
  62. zLmRunning(ZLMServerConfig);
  63. }
  64. }
  65. public ZLMServerConfig getMediaServerConfig() {
  66. if (!startGetMedia) return null;
  67. JSONObject responseJSON = zlmresTfulUtils.getMediaServerConfig();
  68. ZLMServerConfig ZLMServerConfig = null;
  69. if (responseJSON != null) {
  70. JSONArray data = responseJSON.getJSONArray("data");
  71. if (data != null && data.size() > 0) {
  72. ZLMServerConfig = JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class);
  73. }
  74. } else {
  75. logger.error("getMediaServerConfig失败, 1s后重试");
  76. try {
  77. Thread.sleep(1000);
  78. } catch (InterruptedException e) {
  79. e.printStackTrace();
  80. }
  81. ZLMServerConfig = getMediaServerConfig();
  82. }
  83. return ZLMServerConfig;
  84. }
  85. private void saveZLMConfig() {
  86. logger.info("设置zlm...");
  87. if (StringUtils.isEmpty(mediaConfig.getHookIp())) mediaConfig.setHookIp(sipConfig.getSipIp());
  88. String protocol = sslEnabled ? "https" : "http";
  89. String hookPrex = String.format("%s://%s:%s/index/hook", protocol, mediaConfig.getHookIp(), serverPort);
  90. Map<String, Object> param = new HashMap<>();
  91. param.put("api.secret",mediaConfig.getSecret()); // -profile:v Baseline
  92. param.put("ffmpeg.cmd","%s -fflags nobuffer -rtsp_transport tcp -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s");
  93. param.put("hook.enable","1");
  94. param.put("hook.on_flow_report","");
  95. param.put("hook.on_play",String.format("%s/on_play", hookPrex));
  96. param.put("hook.on_http_access","");
  97. param.put("hook.on_publish",String.format("%s/on_publish", hookPrex));
  98. param.put("hook.on_record_mp4","");
  99. param.put("hook.on_record_ts","");
  100. param.put("hook.on_rtsp_auth","");
  101. param.put("hook.on_rtsp_realm","");
  102. param.put("hook.on_server_started",String.format("%s/on_server_started", hookPrex));
  103. param.put("hook.on_shell_login",String.format("%s/on_shell_login", hookPrex));
  104. param.put("hook.on_stream_changed",String.format("%s/on_stream_changed", hookPrex));
  105. param.put("hook.on_stream_none_reader",String.format("%s/on_stream_none_reader", hookPrex));
  106. param.put("hook.on_stream_not_found",String.format("%s/on_stream_not_found", hookPrex));
  107. param.put("hook.timeoutSec","20");
  108. param.put("general.streamNoneReaderDelayMS",mediaConfig.getStreamNoneReaderDelayMS());
  109. JSONObject responseJSON = zlmresTfulUtils.setServerConfig(param);
  110. if (responseJSON != null && responseJSON.getInteger("code") == 0) {
  111. logger.info("设置zlm成功");
  112. }else {
  113. logger.info("设置zlm失败: " + responseJSON.getString("msg"));
  114. }
  115. }
  116. /**
  117. * zlm 连接成功或者zlm重启后
  118. */
  119. private void zLmRunning(ZLMServerConfig zlmServerConfig){
  120. logger.info( "[ id: " + zlmServerConfig.getGeneralMediaServerId() + "] zlm接入成功...");
  121. // 关闭循环获取zlm配置
  122. startGetMedia = false;
  123. if (mediaConfig.getAutoConfig()) saveZLMConfig();
  124. zlmServerManger.updateServerCatch(zlmServerConfig);
  125. // 清空所有session
  126. // zlmMediaListManager.clearAllSessions();
  127. // 更新流列表
  128. zlmMediaListManager.updateMediaList();
  129. // 恢复流代理
  130. List<StreamProxyItem> streamProxyListForEnable = storager.getStreamProxyListForEnable(true);
  131. for (StreamProxyItem streamProxyDto : streamProxyListForEnable) {
  132. logger.info("恢复流代理," + streamProxyDto.getApp() + "/" + streamProxyDto.getStream());
  133. JSONObject jsonObject = streamProxyService.addStreamProxyToZlm(streamProxyDto);
  134. if (jsonObject == null) {
  135. // 设置为未启用
  136. logger.info("恢复流代理失败,请检查流地址后重新启用" + streamProxyDto.getApp() + "/" + streamProxyDto.getStream());
  137. streamProxyService.stop(streamProxyDto.getApp(), streamProxyDto.getStream());
  138. }
  139. }
  140. }
  141. }