ZLMRESTfulUtils.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package com.genersoft.iot.vmp.media.zlm;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import okhttp3.*;
  5. import org.jetbrains.annotations.NotNull;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.stereotype.Component;
  10. import java.io.IOException;
  11. import java.net.ConnectException;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. @Component
  15. public class ZLMRESTfulUtils {
  16. private final static Logger logger = LoggerFactory.getLogger(ZLMRESTfulUtils.class);
  17. @Value("${media.ip}")
  18. private String mediaIp;
  19. @Value("${media.port}")
  20. private int mediaPort;
  21. @Value("${media.secret}")
  22. private String mediaSecret;
  23. public interface RequestCallback{
  24. void run(JSONObject response);
  25. }
  26. public JSONObject sendPost(String api, Map<String, Object> param, RequestCallback callback) {
  27. OkHttpClient client = new OkHttpClient();
  28. String url = String.format("http://%s:%s/index/api/%s", mediaIp, mediaPort, api);
  29. JSONObject responseJSON = null;
  30. logger.debug(url);
  31. FormBody.Builder builder = new FormBody.Builder();
  32. builder.add("secret",mediaSecret);
  33. if (param != null) {
  34. for (String key : param.keySet()){
  35. builder.add(key, param.get(key).toString());
  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. e.printStackTrace();
  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 = response.body().string();
  65. if (responseStr != null) {
  66. callback.run(JSON.parseObject(responseStr));
  67. }else {
  68. callback.run(null);
  69. }
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. }
  75. @Override
  76. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  77. logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage()));
  78. logger.info("请检查media配置并确认ZLM已启动...");
  79. }
  80. });
  81. }
  82. return responseJSON;
  83. }
  84. public JSONObject getMediaList(String app, String stream, String schema, RequestCallback callback){
  85. Map<String, Object> param = new HashMap<>();
  86. if (app != null) param.put("app",app);
  87. if (stream != null) param.put("stream",stream);
  88. if (schema != null) param.put("schema",schema);
  89. param.put("vhost","__defaultVhost__");
  90. return sendPost("getMediaList",param, callback);
  91. }
  92. public JSONObject getMediaList(String app, String stream){
  93. return getMediaList(app, stream,null, null);
  94. }
  95. public JSONObject getMediaList(RequestCallback callback){
  96. return sendPost("getMediaList",null, callback);
  97. }
  98. public JSONObject getMediaInfo(String app, String schema, String stream){
  99. Map<String, Object> param = new HashMap<>();
  100. param.put("app",app);
  101. param.put("schema",schema);
  102. param.put("stream",stream);
  103. param.put("vhost","__defaultVhost__");
  104. return sendPost("getMediaInfo",param, null);
  105. }
  106. public JSONObject getRtpInfo(String stream_id){
  107. Map<String, Object> param = new HashMap<>();
  108. param.put("stream_id",stream_id);
  109. return sendPost("getRtpInfo",param, null);
  110. }
  111. public JSONObject addFFmpegSource(String src_url, String dst_url, String timeout_ms){
  112. System.out.println(src_url);
  113. System.out.println(dst_url);
  114. Map<String, Object> param = new HashMap<>();
  115. param.put("src_url", src_url);
  116. param.put("dst_url", dst_url);
  117. param.put("timeout_ms", timeout_ms);
  118. return sendPost("addFFmpegSource",param, null);
  119. }
  120. public JSONObject delFFmpegSource(String key){
  121. Map<String, Object> param = new HashMap<>();
  122. param.put("key", key);
  123. return sendPost("delFFmpegSource",param, null);
  124. }
  125. public JSONObject getMediaServerConfig(){
  126. return sendPost("getServerConfig",null, null);
  127. }
  128. public JSONObject setServerConfig(Map<String, Object> param){
  129. return sendPost("setServerConfig",param, null);
  130. }
  131. public JSONObject openRtpServer(Map<String, Object> param){
  132. return sendPost("openRtpServer",param, null);
  133. }
  134. public JSONObject closeRtpServer(Map<String, Object> param) {
  135. return sendPost("closeRtpServer",param, null);
  136. }
  137. public JSONObject listRtpServer() {
  138. return sendPost("listRtpServer",null, null);
  139. }
  140. public JSONObject startSendRtp(Map<String, Object> param) {
  141. return sendPost("startSendRtp",param, null);
  142. }
  143. public JSONObject stopSendRtp(Map<String, Object> param) {
  144. return sendPost("stopSendRtp",param, null);
  145. }
  146. public JSONObject addStreamProxy(String app, String stream, String url, boolean enable_hls, boolean enable_mp4, String rtp_type) {
  147. Map<String, Object> param = new HashMap<>();
  148. param.put("vhost", "__defaultVhost__");
  149. param.put("app", app);
  150. param.put("stream", stream);
  151. param.put("url", url);
  152. param.put("enable_hls", enable_hls?1:0);
  153. param.put("enable_mp4", enable_mp4?1:0);
  154. param.put("rtp_type", rtp_type);
  155. return sendPost("addStreamProxy",param, null);
  156. }
  157. public JSONObject closeStreams(String app, String stream) {
  158. Map<String, Object> param = new HashMap<>();
  159. param.put("vhost", "__defaultVhost__");
  160. param.put("app", app);
  161. param.put("stream", stream);
  162. param.put("force", 1);
  163. return sendPost("close_streams",param, null);
  164. }
  165. }