From a59095db0c240987028d4d05ab561665ed8fe7ea Mon Sep 17 00:00:00 2001
From: mrjackwang <30337754+mrjackwang@users.noreply.github.com>
Date: 星期五, 29 七月 2022 15:16:39 +0800
Subject: [PATCH] Merge branch '648540858:wvp-28181-2.0' into wvp-28181-2.0
---
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java | 110 ++++++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 92 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 71bb317..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
@@ -4,6 +4,7 @@
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import okhttp3.*;
+import okhttp3.logging.HttpLoggingInterceptor;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -11,9 +12,11 @@
import java.io.*;
import java.net.ConnectException;
+import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
+import java.util.concurrent.TimeUnit;
@Component
public class ZLMRESTfulUtils {
@@ -24,11 +27,34 @@
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);
+ }
+ return httpClientBuilder.build();
+ }
+
+
public JSONObject sendPost(MediaServerItem mediaServerItem, String api, Map<String, Object> param, RequestCallback callback) {
- OkHttpClient client = new OkHttpClient();
+ OkHttpClient client = getClient();
+
+ if (mediaServerItem == null) {
+ return null;
+ }
String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
- JSONObject responseJSON = null;
- logger.debug(url);
+ JSONObject responseJSON = new JSONObject();
+ //-2鑷畾涔夋祦濯掍綋 璋冪敤閿欒鐮�
+ responseJSON.put("code",-2);
+ responseJSON.put("msg","娴佸獟浣撹皟鐢ㄥけ璐�");
FormBody.Builder builder = new FormBody.Builder();
builder.add("secret",mediaServerItem.getSecret());
@@ -50,16 +76,29 @@
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
- String responseStr = response.body().string();
- if (responseStr != null) {
+ ResponseBody responseBody = response.body();
+ if (responseBody != null) {
+ String responseStr = responseBody.string();
responseJSON = JSON.parseObject(responseStr);
}
+ }else {
+ 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(){
@@ -73,13 +112,25 @@
} catch (IOException e) {
logger.error(String.format("[ %s ]璇锋眰澶辫触: %s", url, e.getMessage()));
}
+
+ }else {
+ response.close();
+ Objects.requireNonNull(response.body()).close();
}
}
@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()));
+ }
}
});
}
@@ -92,7 +143,11 @@
public void sendGetForImg(MediaServerItem mediaServerItem, String api, Map<String, Object> params, String targetPath, String fileName) {
String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
logger.debug(url);
- HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder();
+ HttpUrl parseUrl = HttpUrl.parse(url);
+ if (parseUrl == null) {
+ return;
+ }
+ HttpUrl.Builder httpBuilder = parseUrl.newBuilder();
httpBuilder.addQueryParameter("secret", mediaServerItem.getSecret());
if (params != null) {
@@ -115,16 +170,20 @@
if (targetPath != null) {
File snapFolder = new File(targetPath);
if (!snapFolder.exists()) {
- snapFolder.mkdirs();
+ if (!snapFolder.mkdirs()) {
+ logger.warn("{}璺緞鍒涘缓澶辫触", snapFolder.getAbsolutePath());
+ }
+
}
- File snapFile = new File(targetPath + "/" + fileName);
+ File snapFile = new File(targetPath + File.separator + fileName);
FileOutputStream outStream = new FileOutputStream(snapFile);
- outStream.write(response.body().bytes());
+
+ outStream.write(Objects.requireNonNull(response.body()).bytes());
outStream.close();
} else {
logger.error(String.format("[ %s ]璇锋眰澶辫触: %s %s", url, response.code(), response.message()));
}
- response.body().close();
+ Objects.requireNonNull(response.body()).close();
} else {
logger.error(String.format("[ %s ]璇锋眰澶辫触: %s %s", url, response.code(), response.message()));
}
@@ -138,9 +197,15 @@
public JSONObject getMediaList(MediaServerItem mediaServerItem, String app, String stream, String schema, RequestCallback callback){
Map<String, Object> param = new HashMap<>();
- if (app != null) param.put("app",app);
- if (stream != null) param.put("stream",stream);
- if (schema != null) param.put("schema",schema);
+ if (app != null) {
+ param.put("app",app);
+ }
+ if (stream != null) {
+ param.put("stream",stream);
+ }
+ if (schema != null) {
+ param.put("schema",schema);
+ }
param.put("vhost","__defaultVhost__");
return sendPost(mediaServerItem, "getMediaList",param, callback);
}
@@ -216,6 +281,10 @@
return sendPost(mediaServerItem, "stopSendRtp",param, null);
}
+ public JSONObject restartServer(MediaServerItem mediaServerItem) {
+ return sendPost(mediaServerItem, "restartServer",null, null);
+ }
+
public JSONObject addStreamProxy(MediaServerItem mediaServerItem, String app, String stream, String url, boolean enable_hls, boolean enable_mp4, String rtp_type) {
Map<String, Object> param = new HashMap<>();
param.put("vhost", "__defaultVhost__");
@@ -224,6 +293,11 @@
param.put("url", url);
param.put("enable_hls", enable_hls?1:0);
param.put("enable_mp4", enable_mp4?1:0);
+ param.put("enable_rtmp", 1);
+ param.put("enable_fmp4", 1);
+ param.put("enable_audio", 1);
+ param.put("enable_rtsp", 1);
+ param.put("add_mute_audio", 1);
param.put("rtp_type", rtp_type);
return sendPost(mediaServerItem, "addStreamProxy",param, null);
}
--
Gitblit v1.8.0