ZLMRESTfulUtils.java 11 KB

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