Commit 8f7db635a31f2817fa920e4d43bc8718f1bc070d

Authored by chenjialing
1 parent 6d4312e3

优化----okhttp连接访问超时设置,访问异常code设置为-2

src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
... ... @@ -12,6 +12,7 @@ import org.springframework.stereotype.Component;
12 12  
13 13 import java.io.*;
14 14 import java.net.ConnectException;
  15 +import java.net.SocketTimeoutException;
15 16 import java.util.HashMap;
16 17 import java.util.Map;
17 18 import java.util.Objects;
... ... @@ -28,6 +29,9 @@ public class ZLMRESTfulUtils {
28 29  
29 30 private OkHttpClient getClient(){
30 31 OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
  32 + //todo 暂时写死超时时间 均为5s
  33 + httpClientBuilder.connectTimeout(5,TimeUnit.SECONDS); //设置连接超时时间
  34 + httpClientBuilder.readTimeout(5,TimeUnit.SECONDS); //设置读取超时时间
31 35 if (logger.isDebugEnabled()) {
32 36 HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
33 37 logger.debug("http请求参数:" + message);
... ... @@ -47,7 +51,10 @@ public class ZLMRESTfulUtils {
47 51 return null;
48 52 }
49 53 String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
50   - JSONObject responseJSON = null;
  54 + JSONObject responseJSON = new JSONObject();
  55 + //-2自定义流媒体 调用错误码
  56 + responseJSON.put("code",-2);
  57 + responseJSON.put("msg","流媒体调用失败");
51 58  
52 59 FormBody.Builder builder = new FormBody.Builder();
53 60 builder.add("secret",mediaServerItem.getSecret());
... ... @@ -78,11 +85,20 @@ public class ZLMRESTfulUtils {
78 85 response.close();
79 86 Objects.requireNonNull(response.body()).close();
80 87 }
81   - } catch (ConnectException e) {
82   - logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage()));
83   - logger.info("请检查media配置并确认ZLM已启动...");
84 88 }catch (IOException e) {
85 89 logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage()));
  90 +
  91 + if(e instanceof SocketTimeoutException){
  92 + //读取超时超时异常
  93 + logger.error(String.format("读取ZLM数据失败: %s, %s", url, e.getMessage()));
  94 + }
  95 + if(e instanceof ConnectException){
  96 + //判断连接异常,我这里是报Failed to connect to 10.7.5.144
  97 + logger.error(String.format("连接ZLM失败: %s, %s", url, e.getMessage()));
  98 + }
  99 +
  100 + }catch (Exception e){
  101 + logger.error(String.format("访问ZLM失败: %s, %s", url, e.getMessage()));
86 102 }
87 103 }else {
88 104 client.newCall(request).enqueue(new Callback(){
... ... @@ -105,8 +121,16 @@ public class ZLMRESTfulUtils {
105 121  
106 122 @Override
107 123 public void onFailure(@NotNull Call call, @NotNull IOException e) {
108   - logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage()));
109   - logger.info("请检查media配置并确认ZLM已启动...");
  124 + logger.error(String.format("连接ZLM失败: %s, %s", call.request().toString(), e.getMessage()));
  125 +
  126 + if(e instanceof SocketTimeoutException){
  127 + //读取超时超时异常
  128 + logger.error(String.format("读取ZLM数据失败: %s, %s", call.request().toString(), e.getMessage()));
  129 + }
  130 + if(e instanceof ConnectException){
  131 + //判断连接异常,我这里是报Failed to connect to 10.7.5.144
  132 + logger.error(String.format("连接ZLM失败: %s, %s", call.request().toString(), e.getMessage()));
  133 + }
110 134 }
111 135 });
112 136 }
... ...
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java
... ... @@ -87,10 +87,15 @@ public class ZLMRTPServerFactory {
87 87 int result = -1;
88 88 // 查询此rtp server 是否已经存在
89 89 JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaServerItem, streamId);
90   - if (rtpInfo != null && rtpInfo.getInteger("code") == 0 && rtpInfo.getBoolean("exist")) {
91   - result = rtpInfo.getInteger("local_port");
  90 + if(rtpInfo.getInteger("code") == 0){
  91 + if (rtpInfo.getBoolean("exist")) {
  92 + result = rtpInfo.getInteger("local_port");
  93 + return result;
  94 + }
  95 + }else if(rtpInfo.getInteger("code") == -2){
92 96 return result;
93 97 }
  98 +
94 99 Map<String, Object> param = new HashMap<>();
95 100 // 推流端口设置0则使用随机端口
96 101 param.put("enable_tcp", 1);
... ...
src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
... ... @@ -123,7 +123,7 @@ public class PlayServiceImpl implements IPlayService {
123 123 result.onCompletion(()->{
124 124 // 点播结束时调用截图接口
125 125 // TODO 应该在上流时调用更好,结束也可能是错误结束
126   - String path = "snap";
  126 + String path = "static/static/snap/";
127 127 String fileName = deviceId + "_" + channelId + ".jpg";
128 128 ResponseEntity responseEntity = (ResponseEntity)result.getResult();
129 129 if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
... ... @@ -152,24 +152,33 @@ public class PlayServiceImpl implements IPlayService {
152 152 MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
153 153  
154 154 JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaInfo, streamId);
155   - if (rtpInfo != null && rtpInfo.getBoolean("exist")) {
  155 + if(rtpInfo.getInteger("code") == 0){
  156 + if (rtpInfo.getBoolean("exist")) {
156 157  
157   - WVPResult wvpResult = new WVPResult();
158   - wvpResult.setCode(0);
159   - wvpResult.setMsg("success");
160   - wvpResult.setData(streamInfo);
161   - msg.setData(wvpResult);
  158 + WVPResult wvpResult = new WVPResult();
  159 + wvpResult.setCode(0);
  160 + wvpResult.setMsg("success");
  161 + wvpResult.setData(streamInfo);
  162 + msg.setData(wvpResult);
162 163  
163   - resultHolder.invokeAllResult(msg);
164   - if (hookEvent != null) {
165   - hookEvent.response(mediaServerItem, JSONObject.parseObject(JSON.toJSONString(streamInfo)));
  164 + resultHolder.invokeAllResult(msg);
  165 + if (hookEvent != null) {
  166 + hookEvent.response(mediaServerItem, JSONObject.parseObject(JSON.toJSONString(streamInfo)));
  167 + }
  168 + }else {
  169 + redisCatchStorage.stopPlay(streamInfo);
  170 + storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
  171 + streamInfo = null;
166 172 }
167 173 }else {
  174 + //zlm连接失败
168 175 redisCatchStorage.stopPlay(streamInfo);
169 176 storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
170 177 streamInfo = null;
  178 +
171 179 }
172 180  
  181 +
173 182 }
174 183 if (streamInfo == null) {
175 184 String streamId = null;
... ...