ZLMRESTfulUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package com.genersoft.iot.vmp.media.zlm;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.genersoft.iot.vmp.conf.MediaConfig;
  5. import com.genersoft.iot.vmp.media.zlm.dto.IMediaServerItem;
  6. import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
  7. import okhttp3.*;
  8. import org.jetbrains.annotations.NotNull;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Component;
  13. import java.io.*;
  14. import java.net.ConnectException;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import java.util.Objects;
  18. @Component
  19. public class ZLMRESTfulUtils {
  20. private final static Logger logger = LoggerFactory.getLogger(ZLMRESTfulUtils.class);
  21. public interface RequestCallback{
  22. void run(JSONObject response);
  23. }
  24. public JSONObject sendPost(IMediaServerItem mediaServerItem, String api, Map<String, Object> param, RequestCallback callback) {
  25. OkHttpClient client = new OkHttpClient();
  26. String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
  27. JSONObject responseJSON = null;
  28. logger.debug(url);
  29. FormBody.Builder builder = new FormBody.Builder();
  30. builder.add("secret",mediaServerItem.getSecret());
  31. if (param != null && param.keySet().size() > 0) {
  32. for (String key : param.keySet()){
  33. if (param.get(key) != null) {
  34. builder.add(key, param.get(key).toString());
  35. }
  36. }
  37. }
  38. FormBody body = builder.build();
  39. Request request = new Request.Builder()
  40. .post(body)
  41. .url(url)
  42. .build();
  43. if (callback == null) {
  44. try {
  45. Response response = client.newCall(request).execute();
  46. if (response.isSuccessful()) {
  47. String responseStr = response.body().string();
  48. if (responseStr != null) {
  49. responseJSON = JSON.parseObject(responseStr);
  50. }
  51. }
  52. } catch (ConnectException e) {
  53. logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage()));
  54. logger.info("请检查media配置并确认ZLM已启动...");
  55. }catch (IOException e) {
  56. logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage()));
  57. }
  58. }else {
  59. client.newCall(request).enqueue(new Callback(){
  60. @Override
  61. public void onResponse(@NotNull Call call, @NotNull Response response){
  62. if (response.isSuccessful()) {
  63. try {
  64. String responseStr = Objects.requireNonNull(response.body()).string();
  65. callback.run(JSON.parseObject(responseStr));
  66. } catch (IOException e) {
  67. logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage()));
  68. }
  69. }
  70. }
  71. @Override
  72. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  73. logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage()));
  74. logger.info("请检查media配置并确认ZLM已启动...");
  75. }
  76. });
  77. }
  78. return responseJSON;
  79. }
  80. public void sendPostForImg(IMediaServerItem mediaServerItem, String api, Map<String, Object> param, String targetPath, String fileName) {
  81. OkHttpClient client = new OkHttpClient();
  82. String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
  83. JSONObject responseJSON = null;
  84. logger.debug(url);
  85. FormBody.Builder builder = new FormBody.Builder();
  86. builder.add("secret",mediaServerItem.getSecret());
  87. if (param != null && param.keySet().size() > 0) {
  88. for (String key : param.keySet()){
  89. if (param.get(key) != null) {
  90. builder.add(key, param.get(key).toString());
  91. }
  92. }
  93. }
  94. FormBody body = builder.build();
  95. Request request = new Request.Builder()
  96. .post(body)
  97. .url(url)
  98. .build();
  99. try {
  100. Response response = client.newCall(request).execute();
  101. if (response.isSuccessful()) {
  102. if (targetPath != null) {
  103. File snapFolder = new File(targetPath);
  104. if (!snapFolder.exists()) {
  105. snapFolder.mkdirs();
  106. }
  107. File snapFile = new File(targetPath + "/" + fileName);
  108. FileOutputStream outStream = new FileOutputStream(snapFile);
  109. outStream.write(response.body().bytes());
  110. outStream.close();
  111. }
  112. }
  113. } catch (ConnectException e) {
  114. logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage()));
  115. logger.info("请检查media配置并确认ZLM已启动...");
  116. }catch (IOException e) {
  117. logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage()));
  118. }
  119. }
  120. public JSONObject getMediaList(IMediaServerItem mediaServerItem,String app, String stream, String schema, RequestCallback callback){
  121. Map<String, Object> param = new HashMap<>();
  122. if (app != null) param.put("app",app);
  123. if (stream != null) param.put("stream",stream);
  124. if (schema != null) param.put("schema",schema);
  125. param.put("vhost","__defaultVhost__");
  126. return sendPost(mediaServerItem, "getMediaList",param, callback);
  127. }
  128. public JSONObject getMediaList(IMediaServerItem mediaServerItem,String app, String stream){
  129. return getMediaList(mediaServerItem, app, stream,null, null);
  130. }
  131. public JSONObject getMediaList(IMediaServerItem mediaServerItem,RequestCallback callback){
  132. return sendPost(mediaServerItem, "getMediaList",null, callback);
  133. }
  134. public JSONObject getMediaInfo(IMediaServerItem mediaServerItem,String app, String schema, String stream){
  135. Map<String, Object> param = new HashMap<>();
  136. param.put("app",app);
  137. param.put("schema",schema);
  138. param.put("stream",stream);
  139. param.put("vhost","__defaultVhost__");
  140. return sendPost(mediaServerItem, "getMediaInfo",param, null);
  141. }
  142. public JSONObject getRtpInfo(IMediaServerItem mediaServerItem,String stream_id){
  143. Map<String, Object> param = new HashMap<>();
  144. param.put("stream_id",stream_id);
  145. return sendPost(mediaServerItem, "getRtpInfo",param, null);
  146. }
  147. public JSONObject addFFmpegSource(IMediaServerItem mediaServerItem,String src_url, String dst_url, String timeout_ms,
  148. boolean enable_hls, boolean enable_mp4, String ffmpeg_cmd_key){
  149. logger.info(src_url);
  150. logger.info(dst_url);
  151. Map<String, Object> param = new HashMap<>();
  152. param.put("src_url", src_url);
  153. param.put("dst_url", dst_url);
  154. param.put("timeout_ms", timeout_ms);
  155. param.put("enable_hls", enable_hls);
  156. param.put("enable_mp4", enable_mp4);
  157. param.put("ffmpeg_cmd_key", ffmpeg_cmd_key);
  158. return sendPost(mediaServerItem, "addFFmpegSource",param, null);
  159. }
  160. public JSONObject delFFmpegSource(IMediaServerItem mediaServerItem,String key){
  161. Map<String, Object> param = new HashMap<>();
  162. param.put("key", key);
  163. return sendPost(mediaServerItem, "delFFmpegSource",param, null);
  164. }
  165. public JSONObject getMediaServerConfig(IMediaServerItem mediaServerItem){
  166. return sendPost(mediaServerItem, "getServerConfig",null, null);
  167. }
  168. public JSONObject setServerConfig(IMediaServerItem mediaServerItem, Map<String, Object> param){
  169. return sendPost(mediaServerItem,"setServerConfig",param, null);
  170. }
  171. public JSONObject openRtpServer(IMediaServerItem mediaServerItem,Map<String, Object> param){
  172. return sendPost(mediaServerItem, "openRtpServer",param, null);
  173. }
  174. public JSONObject closeRtpServer(IMediaServerItem mediaServerItem,Map<String, Object> param) {
  175. return sendPost(mediaServerItem, "closeRtpServer",param, null);
  176. }
  177. public JSONObject listRtpServer(IMediaServerItem mediaServerItem) {
  178. return sendPost(mediaServerItem, "listRtpServer",null, null);
  179. }
  180. public JSONObject startSendRtp(IMediaServerItem mediaServerItem,Map<String, Object> param) {
  181. return sendPost(mediaServerItem, "startSendRtp",param, null);
  182. }
  183. public JSONObject stopSendRtp(IMediaServerItem mediaServerItem,Map<String, Object> param) {
  184. return sendPost(mediaServerItem, "stopSendRtp",param, null);
  185. }
  186. public JSONObject addStreamProxy(IMediaServerItem mediaServerItem,String app, String stream, String url, boolean enable_hls, boolean enable_mp4, String rtp_type) {
  187. Map<String, Object> param = new HashMap<>();
  188. param.put("vhost", "__defaultVhost__");
  189. param.put("app", app);
  190. param.put("stream", stream);
  191. param.put("url", url);
  192. param.put("enable_hls", enable_hls?1:0);
  193. param.put("enable_mp4", enable_mp4?1:0);
  194. param.put("rtp_type", rtp_type);
  195. return sendPost(mediaServerItem, "addStreamProxy",param, null);
  196. }
  197. public JSONObject closeStreams(IMediaServerItem mediaServerItem,String app, String stream) {
  198. Map<String, Object> param = new HashMap<>();
  199. param.put("vhost", "__defaultVhost__");
  200. param.put("app", app);
  201. param.put("stream", stream);
  202. param.put("force", 1);
  203. return sendPost(mediaServerItem, "close_streams",param, null);
  204. }
  205. public JSONObject getAllSession(IMediaServerItem mediaServerItem) {
  206. return sendPost(mediaServerItem, "getAllSession",null, null);
  207. }
  208. public void kickSessions(IMediaServerItem mediaServerItem, String localPortSStr) {
  209. Map<String, Object> param = new HashMap<>();
  210. param.put("local_port", localPortSStr);
  211. sendPost(mediaServerItem, "kick_sessions",param, null);
  212. }
  213. public void getSnap(IMediaServerItem mediaServerItem, String flvUrl, int timeout_sec, int expire_sec, String targetPath, String fileName) {
  214. Map<String, Object> param = new HashMap<>();
  215. param.put("url", flvUrl);
  216. param.put("timeout_sec", timeout_sec);
  217. param.put("expire_sec", expire_sec);
  218. sendPostForImg(mediaServerItem, "getSnap",param, targetPath, fileName);
  219. }
  220. }