From f760134b39b411495322d2d8bda5dc345aae36d7 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 15 六月 2022 14:19:33 +0800
Subject: [PATCH] Merge pull request #517 from TristingChen/optimize-play-controller

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java |    9 +++-
 src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java  |   34 ++++++++++++-----
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java     |   36 +++++++++++++++---
 3 files changed, 61 insertions(+), 18 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 5caae66..5e0d317 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
@@ -12,6 +12,7 @@
 
 import java.io.*;
 import java.net.ConnectException;
+import java.net.SocketTimeoutException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
@@ -28,6 +29,9 @@
 
     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);
@@ -47,7 +51,10 @@
             return null;
         }
         String url = String.format("http://%s:%s/index/api/%s",  mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
-        JSONObject responseJSON = null;
+        JSONObject responseJSON = new JSONObject();
+        //-2鑷畾涔夋祦濯掍綋 璋冪敤閿欒鐮�
+        responseJSON.put("code",-2);
+        responseJSON.put("msg","娴佸獟浣撹皟鐢ㄥけ璐�");
 
         FormBody.Builder builder = new FormBody.Builder();
         builder.add("secret",mediaServerItem.getSecret());
@@ -78,11 +85,20 @@
                         response.close();
                         Objects.requireNonNull(response.body()).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()));
+
+                    if(e instanceof SocketTimeoutException){
+                        //璇诲彇瓒呮椂瓒呮椂寮傚父
+                        logger.error(String.format("璇诲彇ZLM鏁版嵁澶辫触: %s, %s", url, e.getMessage()));
+                    }
+                    if(e instanceof ConnectException){
+                        //鍒ゆ柇杩炴帴寮傚父锛屾垜杩欓噷鏄姤Failed to connect to 10.7.5.144
+                        logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", url, e.getMessage()));
+                    }
+
+                }catch (Exception e){
+                    logger.error(String.format("璁块棶ZLM澶辫触: %s, %s", url, e.getMessage()));
                 }
             }else {
                 client.newCall(request).enqueue(new Callback(){
@@ -105,8 +121,16 @@
 
                     @Override
                     public void onFailure(@NotNull Call call, @NotNull IOException e) {
-                        logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", e.getCause().getMessage(), e.getMessage()));
-                        logger.info("璇锋鏌edia閰嶇疆骞剁‘璁LM宸插惎鍔�...");
+                        logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", call.request().toString(), e.getMessage()));
+
+                        if(e instanceof SocketTimeoutException){
+                            //璇诲彇瓒呮椂瓒呮椂寮傚父
+                            logger.error(String.format("璇诲彇ZLM鏁版嵁澶辫触: %s, %s", call.request().toString(), e.getMessage()));
+                        }
+                        if(e instanceof ConnectException){
+                            //鍒ゆ柇杩炴帴寮傚父锛屾垜杩欓噷鏄姤Failed to connect to 10.7.5.144
+                            logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", call.request().toString(), e.getMessage()));
+                        }
                     }
                 });
             }
diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java
index 210aa72..48cdca3 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java
@@ -91,10 +91,15 @@
         int result = -1;
         // 鏌ヨ姝tp server 鏄惁宸茬粡瀛樺湪
         JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaServerItem, streamId);
-        if (rtpInfo != null && rtpInfo.getInteger("code") == 0 && rtpInfo.getBoolean("exist")) {
-            result = rtpInfo.getInteger("local_port");
+        if(rtpInfo.getInteger("code") == 0){
+            if (rtpInfo.getBoolean("exist")) {
+                result = rtpInfo.getInteger("local_port");
+                return result;
+            }
+        }else if(rtpInfo.getInteger("code") == -2){
             return result;
         }
+
         Map<String, Object> param = new HashMap<>();
         // 鎺ㄦ祦绔彛璁剧疆0鍒欎娇鐢ㄩ殢鏈虹鍙�
         param.put("enable_tcp", 1);
diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
index 200c731..41eca5d 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
@@ -123,7 +123,7 @@
         result.onCompletion(()->{
             // 鐐规挱缁撴潫鏃惰皟鐢ㄦ埅鍥炬帴鍙�
             // TODO 搴旇鍦ㄤ笂娴佹椂璋冪敤鏇村ソ锛岀粨鏉熶篃鍙兘鏄敊璇粨鏉�
-            String path =  "snap";
+            String path =  "static/static/snap/";
             String fileName =  deviceId + "_" + channelId + ".jpg";
             ResponseEntity responseEntity =  (ResponseEntity)result.getResult();
             if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
@@ -152,23 +152,32 @@
             MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
 
             JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaInfo, streamId);
-            if (rtpInfo != null && rtpInfo.getBoolean("exist")) {
+            if(rtpInfo.getInteger("code") == 0){
+                if (rtpInfo.getBoolean("exist")) {
 
-                WVPResult wvpResult = new WVPResult();
-                wvpResult.setCode(0);
-                wvpResult.setMsg("success");
-                wvpResult.setData(streamInfo);
-                msg.setData(wvpResult);
+                    WVPResult wvpResult = new WVPResult();
+                    wvpResult.setCode(0);
+                    wvpResult.setMsg("success");
+                    wvpResult.setData(streamInfo);
+                    msg.setData(wvpResult);
 
-                resultHolder.invokeAllResult(msg);
-                if (hookEvent != null) {
-                    hookEvent.response(mediaServerItem, JSONObject.parseObject(JSON.toJSONString(streamInfo)));
+                    resultHolder.invokeAllResult(msg);
+                    if (hookEvent != null) {
+                        hookEvent.response(mediaServerItem, JSONObject.parseObject(JSON.toJSONString(streamInfo)));
+                    }
+                }else {
+                    redisCatchStorage.stopPlay(streamInfo);
+                    storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
+                    streamInfo = null;
                 }
             }else {
+                //zlm杩炴帴澶辫触
                 redisCatchStorage.stopPlay(streamInfo);
                 storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
                 streamInfo = null;
+
             }
+
 
         }
         if (streamInfo == null) {
@@ -244,6 +253,11 @@
         }, userSetting.getPlayTimeout());
         final String ssrc = ssrcInfo.getSsrc();
         final String stream = ssrcInfo.getStream();
+        //绔彛鑾峰彇澶辫触鐨剆srcInfo 娌℃湁蹇呰鍙戦�佺偣鎾寚浠�
+        if(ssrcInfo.getPort() <= 0){
+            logger.info("[鐐规挱绔彛鍒嗛厤寮傚父]锛宒eviceId={},channelId={},ssrcInfo={}", device.getDeviceId(), channelId, ssrcInfo);
+            return;
+        }
         cmder.playStreamCmd(mediaServerItem, ssrcInfo, device, channelId, (MediaServerItem mediaServerItemInuse, JSONObject response) -> {
             logger.info("鏀跺埌璁㈤槄娑堟伅锛� " + response.toJSONString());
             dynamicTask.stop(timeOutTaskKey);

--
Gitblit v1.8.0