Commit f760134b39b411495322d2d8bda5dc345aae36d7

Authored by 648540858
Committed by GitHub
2 parents a9c4638c 0e826e34

Merge pull request #517 from TristingChen/optimize-play-controller

Optimize play controller
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
... ... @@ -91,10 +91,15 @@ public class ZLMRTPServerFactory {
91 91 int result = -1;
92 92 // 查询此rtp server 是否已经存在
93 93 JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaServerItem, streamId);
94   - if (rtpInfo != null && rtpInfo.getInteger("code") == 0 && rtpInfo.getBoolean("exist")) {
95   - result = rtpInfo.getInteger("local_port");
  94 + if(rtpInfo.getInteger("code") == 0){
  95 + if (rtpInfo.getBoolean("exist")) {
  96 + result = rtpInfo.getInteger("local_port");
  97 + return result;
  98 + }
  99 + }else if(rtpInfo.getInteger("code") == -2){
96 100 return result;
97 101 }
  102 +
98 103 Map<String, Object> param = new HashMap<>();
99 104 // 推流端口设置0则使用随机端口
100 105 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;
... ... @@ -244,6 +253,11 @@ public class PlayServiceImpl implements IPlayService {
244 253 }, userSetting.getPlayTimeout());
245 254 final String ssrc = ssrcInfo.getSsrc();
246 255 final String stream = ssrcInfo.getStream();
  256 + //端口获取失败的ssrcInfo 没有必要发送点播指令
  257 + if(ssrcInfo.getPort() <= 0){
  258 + logger.info("[点播端口分配异常],deviceId={},channelId={},ssrcInfo={}", device.getDeviceId(), channelId, ssrcInfo);
  259 + return;
  260 + }
247 261 cmder.playStreamCmd(mediaServerItem, ssrcInfo, device, channelId, (MediaServerItem mediaServerItemInuse, JSONObject response) -> {
248 262 logger.info("收到订阅消息: " + response.toJSONString());
249 263 dynamicTask.stop(timeOutTaskKey);
... ...