ZLMRESTfulUtils.java 11 KB

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