From 713aa00acc398e44cb2a59e4f5c3f55b4e0784d8 Mon Sep 17 00:00:00 2001
From: xiaoQQya <xiaoQQya@126.com>
Date: 星期三, 22 三月 2023 15:21:22 +0800
Subject: [PATCH] perf(snap): 优化点播成功后截图使用 rtsp 流, 新增对 H265 编码视频截图的支持

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java |   44 +++++++++++++++++++++++++-------------------
 1 files changed, 25 insertions(+), 19 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 bf9551d..a289197 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
@@ -23,27 +23,34 @@
 
     private final static Logger logger = LoggerFactory.getLogger(ZLMRESTfulUtils.class);
 
-
-
+    private OkHttpClient client;
 
     public interface RequestCallback{
         void run(JSONObject response);
     }
 
     private OkHttpClient getClient(){
-        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
-        //todo 鏆傛椂鍐欐瓒呮椂鏃堕棿 鍧囦负5s
-        httpClientBuilder.connectTimeout(5,TimeUnit.SECONDS);  //璁剧疆杩炴帴瓒呮椂鏃堕棿
-        httpClientBuilder.readTimeout(5,TimeUnit.SECONDS);     //璁剧疆璇诲彇瓒呮椂鏃堕棿
-        if (logger.isDebugEnabled()) {
-            HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
-                logger.debug("http璇锋眰鍙傛暟锛�" + message);
-            });
-            logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
-            // OkHttp閫茶娣诲姞鏀旀埅鍣╨oggingInterceptor
-            httpClientBuilder.addInterceptor(logging);
+        if (client == null) {
+            OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
+            //todo 鏆傛椂鍐欐瓒呮椂鏃堕棿 鍧囦负5s
+            // 璁剧疆杩炴帴瓒呮椂鏃堕棿
+            httpClientBuilder.connectTimeout(5,TimeUnit.SECONDS);
+            // 璁剧疆璇诲彇瓒呮椂鏃堕棿
+            httpClientBuilder.readTimeout(10,TimeUnit.SECONDS);
+            // 璁剧疆杩炴帴姹�
+            httpClientBuilder.connectionPool(new ConnectionPool(16, 5, TimeUnit.MINUTES));
+            if (logger.isDebugEnabled()) {
+                HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
+                    logger.debug("http璇锋眰鍙傛暟锛�" + message);
+                });
+                logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
+                // OkHttp閫茶娣诲姞鏀旀埅鍣╨oggingInterceptor
+                httpClientBuilder.addInterceptor(logging);
+            }
+            client = httpClientBuilder.build();
         }
-        return httpClientBuilder.build();
+        return client;
+
     }
 
 
@@ -164,9 +171,7 @@
                 .build();
         logger.info(request.toString());
         try {
-            OkHttpClient client = new OkHttpClient.Builder()
-                    .readTimeout(10, TimeUnit.SECONDS)
-                    .build();
+            OkHttpClient client = getClient();
             Response response = client.newCall(request).execute();
             if (response.isSuccessful()) {
                 if (targetPath != null) {
@@ -181,6 +186,7 @@
                     FileOutputStream outStream = new FileOutputStream(snapFile);
 
                     outStream.write(Objects.requireNonNull(response.body()).bytes());
+                    outStream.flush();
                     outStream.close();
                 } else {
                     logger.error(String.format("[ %s ]璇锋眰澶辫触: %s %s", url, response.code(), response.message()));
@@ -317,9 +323,9 @@
         sendPost(mediaServerItem, "kick_sessions",param, null);
     }
 
-    public void getSnap(MediaServerItem mediaServerItem, String flvUrl, int timeout_sec, int expire_sec, String targetPath, String fileName) {
+    public void getSnap(MediaServerItem mediaServerItem, String streamUrl, int timeout_sec, int expire_sec, String targetPath, String fileName) {
         Map<String, Object> param = new HashMap<>(3);
-        param.put("url", flvUrl);
+        param.put("url", streamUrl);
         param.put("timeout_sec", timeout_sec);
         param.put("expire_sec", expire_sec);
         sendGetForImg(mediaServerItem, "getSnap", param, targetPath, fileName);

--
Gitblit v1.8.0