From e48fa711a3664bece9b3e58840a75fe7c05bc47c Mon Sep 17 00:00:00 2001
From: panlinlin <648540858@qq.com>
Date: 星期六, 08 五月 2021 17:14:05 +0800
Subject: [PATCH] 添加截图(快照)功能

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java |  105 ++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 84 insertions(+), 21 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
index f62ad32..c8aa245 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
@@ -2,31 +2,31 @@
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.genersoft.iot.vmp.conf.MediaConfig;
 import okhttp3.*;
+import org.checkerframework.checker.units.qual.A;
 import org.jetbrains.annotations.NotNull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
-import java.io.IOException;
+import java.io.*;
 import java.net.ConnectException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Objects;
 
 @Component
 public class ZLMRESTfulUtils {
 
     private final static Logger logger = LoggerFactory.getLogger(ZLMRESTfulUtils.class);
 
-    @Value("${media.ip}")
-    private String mediaIp;
+    @Autowired
+    private MediaConfig mediaConfig;
 
-    @Value("${media.port}")
-    private int mediaPort;
 
-    @Value("${media.secret}")
-    private String mediaSecret;
 
     public interface RequestCallback{
         void run(JSONObject response);
@@ -34,15 +34,17 @@
 
     public JSONObject sendPost(String api, Map<String, Object> param, RequestCallback callback) {
         OkHttpClient client = new OkHttpClient();
-        String url = String.format("http://%s:%s/index/api/%s",  mediaIp, mediaPort, api);
+        String url = String.format("http://%s:%s/index/api/%s",  mediaConfig.getIp(), mediaConfig.getHttpPort(), api);
         JSONObject responseJSON = null;
         logger.debug(url);
 
         FormBody.Builder builder = new FormBody.Builder();
-        builder.add("secret",mediaSecret);
-        if (param != null) {
+        builder.add("secret",mediaConfig.getSecret());
+        if (param != null && param.keySet().size() > 0) {
             for (String key : param.keySet()){
-                builder.add(key, param.get(key).toString());
+                if (param.get(key) != null) {
+                    builder.add(key, param.get(key).toString());
+                }
             }
         }
 
@@ -65,7 +67,7 @@
                     logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", e.getCause().getMessage(), e.getMessage()));
                     logger.info("璇锋鏌edia閰嶇疆骞剁‘璁LM宸插惎鍔�...");
                 }catch (IOException e) {
-                    e.printStackTrace();
+                    logger.error(String.format("[ %s ]璇锋眰澶辫触: %s", url, e.getMessage()));
                 }
             }else {
                 client.newCall(request).enqueue(new Callback(){
@@ -74,14 +76,10 @@
                     public void onResponse(@NotNull Call call, @NotNull Response response){
                         if (response.isSuccessful()) {
                             try {
-                                String responseStr = response.body().string();
-                                if (responseStr != null) {
-                                    callback.run(JSON.parseObject(responseStr));
-                                }else {
-                                    callback.run(null);
-                                }
+                                String responseStr = Objects.requireNonNull(response.body()).string();
+                                callback.run(JSON.parseObject(responseStr));
                             } catch (IOException e) {
-                                e.printStackTrace();
+                                logger.error(String.format("[ %s ]璇锋眰澶辫触: %s", url, e.getMessage()));
                             }
                         }
                     }
@@ -98,6 +96,53 @@
 
         return responseJSON;
     }
+
+
+    public void sendPostForImg(String api, Map<String, Object> param, String targetPath, String fileName) {
+        OkHttpClient client = new OkHttpClient();
+        String url = String.format("http://%s:%s/index/api/%s",  mediaConfig.getIp(), mediaConfig.getHttpPort(), api);
+        JSONObject responseJSON = null;
+        logger.debug(url);
+
+        FormBody.Builder builder = new FormBody.Builder();
+        builder.add("secret",mediaConfig.getSecret());
+        if (param != null && param.keySet().size() > 0) {
+            for (String key : param.keySet()){
+                if (param.get(key) != null) {
+                    builder.add(key, param.get(key).toString());
+                }
+            }
+        }
+
+        FormBody body = builder.build();
+
+        Request request = new Request.Builder()
+                .post(body)
+                .url(url)
+                .build();
+        try {
+            Response response = client.newCall(request).execute();
+            if (response.isSuccessful()) {
+                if (targetPath != null) {
+                    File snapFolder = new File(targetPath);
+                    if (!snapFolder.exists()) {
+                        snapFolder.mkdirs();
+                    }
+                    File snapFile = new File(targetPath + "/" + fileName);
+                    FileOutputStream outStream = new FileOutputStream(snapFile);
+                    outStream.write(response.body().bytes());
+                    outStream.close();
+                }
+            }
+        } catch (ConnectException e) {
+            logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", e.getCause().getMessage(), e.getMessage()));
+            logger.info("璇锋鏌edia閰嶇疆骞剁‘璁LM宸插惎鍔�...");
+        }catch (IOException e) {
+            logger.error(String.format("[ %s ]璇锋眰澶辫触: %s", url, e.getMessage()));
+        }
+
+    }
+
 
     public JSONObject getMediaList(String app, String stream, String schema, RequestCallback callback){
         Map<String, Object> param = new HashMap<>();
@@ -132,8 +177,8 @@
     }
 
     public JSONObject addFFmpegSource(String src_url, String dst_url, String timeout_ms){
-        System.out.println(src_url);
-        System.out.println(dst_url);
+        logger.info(src_url);
+        logger.info(dst_url);
         Map<String, Object> param = new HashMap<>();
         param.put("src_url", src_url);
         param.put("dst_url", dst_url);
@@ -195,4 +240,22 @@
         param.put("force", 1);
         return sendPost("close_streams",param, null);
     }
+
+    public JSONObject getAllSession() {
+        return sendPost("getAllSession",null, null);
+    }
+
+    public void kickSessions(String localPortSStr) {
+        Map<String, Object> param = new HashMap<>();
+        param.put("local_port", localPortSStr);
+        sendPost("kick_sessions",param, null);
+    }
+
+    public void getSnap(String flvUrl, int timeout_sec, int expire_sec, String targetPath, String fileName) {
+        Map<String, Object> param = new HashMap<>();
+        param.put("url", flvUrl);
+        param.put("timeout_sec", timeout_sec);
+        param.put("expire_sec", expire_sec);
+        sendPostForImg("getSnap",param, targetPath, fileName);
+    }
 }

--
Gitblit v1.8.0