Commit c19ad94c3ec45245914a7682174db5fb650ee08e

Authored by 648540858
1 parent ebcd2320

优化目录更新,更新失败时只更新收到的内容不重置所有通道和自动拉流

src/main/java/com/genersoft/iot/vmp/gb28181/session/CatalogDataCatch.java
@@ -109,12 +109,18 @@ public class CatalogDataCatch { @@ -109,12 +109,18 @@ public class CatalogDataCatch {
109 109
110 for (String deviceId : keys) { 110 for (String deviceId : keys) {
111 CatalogData catalogData = data.get(deviceId); 111 CatalogData catalogData = data.get(deviceId);
112 - if ( catalogData.getLastTime().isBefore(instantBefore5S)) { // 超过五秒收不到消息任务超时, 只更新这一部分数据 112 + if ( catalogData.getLastTime().isBefore(instantBefore5S)) {
  113 + // 超过五秒收不到消息任务超时, 只更新这一部分数据, 收到数据与声明的总数一致,则重置通道数据,数据不全则只对收到的数据做更新操作
113 if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.runIng)) { 114 if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.runIng)) {
114 - storager.resetChannels(catalogData.getDevice().getDeviceId(), catalogData.getChannelList()); 115 + if (catalogData.getTotal() == catalogData.getChannelList().size()) {
  116 + storager.resetChannels(catalogData.getDevice().getDeviceId(), catalogData.getChannelList());
  117 + }else {
  118 + storager.updateChannels(catalogData.getDevice().getDeviceId(), catalogData.getChannelList());
  119 + }
  120 + String errorMsg = "更新成功,共" + catalogData.getTotal() + "条,已更新" + catalogData.getChannelList().size() + "条";
  121 + catalogData.setErrorMsg(errorMsg);
115 if (catalogData.getTotal() != catalogData.getChannelList().size()) { 122 if (catalogData.getTotal() != catalogData.getChannelList().size()) {
116 - String errorMsg = "更新成功,共" + catalogData.getTotal() + "条,已更新" + catalogData.getChannelList().size() + "条";  
117 - catalogData.setErrorMsg(errorMsg); 123 +
118 } 124 }
119 }else if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.ready)) { 125 }else if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.ready)) {
120 String errorMsg = "同步失败,等待回复超时"; 126 String errorMsg = "同步失败,等待回复超时";
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/SIPRequestProcessorParent.java
@@ -3,7 +3,6 @@ package com.genersoft.iot.vmp.gb28181.transmit.event.request; @@ -3,7 +3,6 @@ package com.genersoft.iot.vmp.gb28181.transmit.event.request;
3 import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; 3 import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
4 import com.genersoft.iot.vmp.gb28181.transmit.SIPSender; 4 import com.genersoft.iot.vmp.gb28181.transmit.SIPSender;
5 import com.genersoft.iot.vmp.gb28181.utils.SipUtils; 5 import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
6 -import gov.nist.javax.sip.SipProviderImpl;  
7 import gov.nist.javax.sip.message.SIPRequest; 6 import gov.nist.javax.sip.message.SIPRequest;
8 import gov.nist.javax.sip.message.SIPResponse; 7 import gov.nist.javax.sip.message.SIPResponse;
9 import org.apache.commons.lang3.ArrayUtils; 8 import org.apache.commons.lang3.ArrayUtils;
@@ -14,19 +13,17 @@ import org.dom4j.io.SAXReader; @@ -14,19 +13,17 @@ import org.dom4j.io.SAXReader;
14 import org.slf4j.Logger; 13 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory; 14 import org.slf4j.LoggerFactory;
16 import org.springframework.beans.factory.annotation.Autowired; 15 import org.springframework.beans.factory.annotation.Autowired;
17 -import org.springframework.beans.factory.annotation.Qualifier;  
18 16
19 import javax.sip.*; 17 import javax.sip.*;
20 import javax.sip.address.Address; 18 import javax.sip.address.Address;
21 -import javax.sip.address.AddressFactory;  
22 import javax.sip.address.SipURI; 19 import javax.sip.address.SipURI;
23 -import javax.sip.header.*; 20 +import javax.sip.header.ContentTypeHeader;
  21 +import javax.sip.header.ExpiresHeader;
  22 +import javax.sip.header.HeaderFactory;
24 import javax.sip.message.MessageFactory; 23 import javax.sip.message.MessageFactory;
25 import javax.sip.message.Request; 24 import javax.sip.message.Request;
26 import javax.sip.message.Response; 25 import javax.sip.message.Response;
27 import java.io.ByteArrayInputStream; 26 import java.io.ByteArrayInputStream;
28 -import java.nio.ByteBuffer;  
29 -import java.nio.charset.StandardCharsets;  
30 import java.text.ParseException; 27 import java.text.ParseException;
31 import java.util.ArrayList; 28 import java.util.ArrayList;
32 import java.util.Arrays; 29 import java.util.Arrays;
@@ -44,15 +41,6 @@ public abstract class SIPRequestProcessorParent { @@ -44,15 +41,6 @@ public abstract class SIPRequestProcessorParent {
44 @Autowired 41 @Autowired
45 private SIPSender sipSender; 42 private SIPSender sipSender;
46 43
47 - public AddressFactory getAddressFactory() {  
48 - try {  
49 - return SipFactory.getInstance().createAddressFactory();  
50 - } catch (PeerUnavailableException e) {  
51 - e.printStackTrace();  
52 - }  
53 - return null;  
54 - }  
55 -  
56 public HeaderFactory getHeaderFactory() { 44 public HeaderFactory getHeaderFactory() {
57 try { 45 try {
58 return SipFactory.getInstance().createHeaderFactory(); 46 return SipFactory.getInstance().createHeaderFactory();
@@ -93,53 +81,6 @@ public abstract class SIPRequestProcessorParent { @@ -93,53 +81,6 @@ public abstract class SIPRequestProcessorParent {
93 return responseAck(sipRequest, statusCode, msg, null); 81 return responseAck(sipRequest, statusCode, msg, null);
94 } 82 }
95 83
96 -// public SIPResponse responseAck(ServerTransaction serverTransaction, int statusCode, String msg, ResponseAckExtraParam responseAckExtraParam) throws SipException, InvalidArgumentException, ParseException {  
97 -// if (serverTransaction == null) {  
98 -// logger.warn("[回复消息] ServerTransaction 为null");  
99 -// return null;  
100 -// }  
101 -// ToHeader toHeader = (ToHeader) serverTransaction.getRequest().getHeader(ToHeader.NAME);  
102 -// if (toHeader.getTag() == null) {  
103 -// toHeader.setTag(SipUtils.getNewTag());  
104 -// }  
105 -// SIPResponse response = (SIPResponse)getMessageFactory().createResponse(statusCode, serverTransaction.getRequest());  
106 -// if (msg != null) {  
107 -// response.setReasonPhrase(msg);  
108 -// }  
109 -// if (responseAckExtraParam != null) {  
110 -// if (responseAckExtraParam.sipURI != null && serverTransaction.getRequest().getMethod().equals(Request.INVITE)) {  
111 -// logger.debug("responseSdpAck SipURI: {}:{}", responseAckExtraParam.sipURI.getHost(), responseAckExtraParam.sipURI.getPort());  
112 -// Address concatAddress = SipFactory.getInstance().createAddressFactory().createAddress(  
113 -// SipFactory.getInstance().createAddressFactory().createSipURI(responseAckExtraParam.sipURI.getUser(), responseAckExtraParam.sipURI.getHost()+":"+responseAckExtraParam.sipURI.getPort()  
114 -// ));  
115 -// response.addHeader(SipFactory.getInstance().createHeaderFactory().createContactHeader(concatAddress));  
116 -// }  
117 -// if (responseAckExtraParam.contentTypeHeader != null) {  
118 -// response.setContent(responseAckExtraParam.content, responseAckExtraParam.contentTypeHeader);  
119 -// }  
120 -//  
121 -// if (serverTransaction.getRequest().getMethod().equals(Request.SUBSCRIBE)) {  
122 -// if (responseAckExtraParam.expires == -1) {  
123 -// logger.error("[参数不全] 2xx的SUBSCRIBE回复,必须设置Expires header");  
124 -// }else {  
125 -// ExpiresHeader expiresHeader = SipFactory.getInstance().createHeaderFactory().createExpiresHeader(responseAckExtraParam.expires);  
126 -// response.addHeader(expiresHeader);  
127 -// }  
128 -// }  
129 -// }else {  
130 -// if (serverTransaction.getRequest().getMethod().equals(Request.SUBSCRIBE)) {  
131 -// logger.error("[参数不全] 2xx的SUBSCRIBE回复,必须设置Expires header");  
132 -// }  
133 -// }  
134 -// serverTransaction.sendResponse(response);  
135 -// if (statusCode >= 200 && !"NOTIFY".equalsIgnoreCase(serverTransaction.getRequest().getMethod())) {  
136 -// if (serverTransaction.getDialog() != null) {  
137 -// serverTransaction.getDialog().delete();  
138 -// }  
139 -// }  
140 -// return response;  
141 -// }  
142 -  
143 public SIPResponse responseAck(SIPRequest sipRequest, int statusCode, String msg, ResponseAckExtraParam responseAckExtraParam) throws SipException, InvalidArgumentException, ParseException { 84 public SIPResponse responseAck(SIPRequest sipRequest, int statusCode, String msg, ResponseAckExtraParam responseAckExtraParam) throws SipException, InvalidArgumentException, ParseException {
144 if (sipRequest.getToHeader().getTag() == null) { 85 if (sipRequest.getToHeader().getTag() == null) {
145 sipRequest.getToHeader().setTag(SipUtils.getNewTag()); 86 sipRequest.getToHeader().setTag(SipUtils.getNewTag());
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/InviteRequestProcessor.java
@@ -228,7 +228,7 @@ public class InviteRequestProcessor extends SIPRequestProcessorParent implements @@ -228,7 +228,7 @@ public class InviteRequestProcessor extends SIPRequestProcessorParent implements
228 } 228 }
229 return; 229 return;
230 } else { 230 } else {
231 - logger.info("通道不存在,返回404"); 231 + logger.info("通道不存在,返回404: {}", channelId);
232 try { 232 try {
233 // 通道不存在,发404,资源不存在 233 // 通道不存在,发404,资源不存在
234 responseAck(request, Response.NOT_FOUND); 234 responseAck(request, Response.NOT_FOUND);
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
@@ -8,6 +8,8 @@ import com.genersoft.iot.vmp.conf.exception.SsrcTransactionNotFoundException; @@ -8,6 +8,8 @@ import com.genersoft.iot.vmp.conf.exception.SsrcTransactionNotFoundException;
8 import com.genersoft.iot.vmp.gb28181.bean.*; 8 import com.genersoft.iot.vmp.gb28181.bean.*;
9 import com.genersoft.iot.vmp.gb28181.event.EventPublisher; 9 import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
10 import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; 10 import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
  11 +import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
  12 +import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
11 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander; 13 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
12 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform; 14 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
13 import com.genersoft.iot.vmp.media.zlm.dto.HookType; 15 import com.genersoft.iot.vmp.media.zlm.dto.HookType;
@@ -18,7 +20,10 @@ import com.genersoft.iot.vmp.media.zlm.dto.hook.*; @@ -18,7 +20,10 @@ import com.genersoft.iot.vmp.media.zlm.dto.hook.*;
18 import com.genersoft.iot.vmp.service.*; 20 import com.genersoft.iot.vmp.service.*;
19 import com.genersoft.iot.vmp.storager.IRedisCatchStorage; 21 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
20 import com.genersoft.iot.vmp.storager.IVideoManagerStorage; 22 import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
  23 +import com.genersoft.iot.vmp.vmanager.bean.DeferredResultEx;
  24 +import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
21 import com.genersoft.iot.vmp.vmanager.bean.StreamContent; 25 import com.genersoft.iot.vmp.vmanager.bean.StreamContent;
  26 +import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
22 import org.slf4j.Logger; 27 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory; 28 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.factory.annotation.Autowired; 29 import org.springframework.beans.factory.annotation.Autowired;
@@ -26,6 +31,7 @@ import org.springframework.beans.factory.annotation.Qualifier; @@ -26,6 +31,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
26 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 31 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
27 import org.springframework.util.ObjectUtils; 32 import org.springframework.util.ObjectUtils;
28 import org.springframework.web.bind.annotation.*; 33 import org.springframework.web.bind.annotation.*;
  34 +import org.springframework.web.context.request.async.DeferredResult;
29 35
30 import javax.servlet.http.HttpServletRequest; 36 import javax.servlet.http.HttpServletRequest;
31 import javax.sip.InvalidArgumentException; 37 import javax.sip.InvalidArgumentException;
@@ -34,638 +40,644 @@ import java.text.ParseException; @@ -34,638 +40,644 @@ import java.text.ParseException;
34 import java.util.HashMap; 40 import java.util.HashMap;
35 import java.util.List; 41 import java.util.List;
36 import java.util.Map; 42 import java.util.Map;
  43 +import java.util.UUID;
37 44
38 -/** 45 +/**
39 * @description:针对 ZLMediaServer的hook事件监听 46 * @description:针对 ZLMediaServer的hook事件监听
40 * @author: swwheihei 47 * @author: swwheihei
41 - * @date: 2020年5月8日 上午10:46:48 48 + * @date: 2020年5月8日 上午10:46:48
42 */ 49 */
43 @RestController 50 @RestController
44 @RequestMapping("/index/hook") 51 @RequestMapping("/index/hook")
45 public class ZLMHttpHookListener { 52 public class ZLMHttpHookListener {
46 53
47 - private final static Logger logger = LoggerFactory.getLogger(ZLMHttpHookListener.class); 54 + private final static Logger logger = LoggerFactory.getLogger(ZLMHttpHookListener.class);
48 55
49 - @Autowired  
50 - private SIPCommander cmder;  
51 -  
52 - @Autowired  
53 - private SIPCommanderFroPlatform commanderFroPlatform;  
54 -  
55 - @Autowired  
56 - private IPlayService playService;  
57 -  
58 - @Autowired  
59 - private IVideoManagerStorage storager;  
60 -  
61 - @Autowired  
62 - private IRedisCatchStorage redisCatchStorage;  
63 -  
64 - @Autowired  
65 - private IDeviceService deviceService;  
66 -  
67 - @Autowired  
68 - private IMediaServerService mediaServerService;  
69 -  
70 - @Autowired  
71 - private IStreamProxyService streamProxyService;  
72 -  
73 - @Autowired  
74 - private IStreamPushService streamPushService;  
75 -  
76 - @Autowired  
77 - private IMediaService mediaService;  
78 -  
79 - @Autowired  
80 - private EventPublisher eventPublisher;  
81 -  
82 - @Autowired  
83 - private ZLMMediaListManager zlmMediaListManager;  
84 -  
85 - @Autowired  
86 - private ZlmHttpHookSubscribe subscribe;  
87 -  
88 - @Autowired  
89 - private UserSetting userSetting;  
90 -  
91 - @Autowired  
92 - private IUserService userService;  
93 -  
94 - @Autowired  
95 - private VideoStreamSessionManager sessionManager;  
96 -  
97 - @Autowired  
98 - private AssistRESTfulUtils assistRESTfulUtils;  
99 -  
100 - @Qualifier("taskExecutor")  
101 - @Autowired  
102 - private ThreadPoolTaskExecutor taskExecutor;  
103 -  
104 - /**  
105 - * 服务器定时上报时间,上报间隔可配置,默认10s上报一次  
106 - *  
107 - */  
108 - @ResponseBody  
109 - @PostMapping(value = "/on_server_keepalive", produces = "application/json;charset=UTF-8")  
110 - public JSONObject onServerKeepalive(@RequestBody OnServerKeepaliveHookParam param){  
111 -  
112 - logger.info("[ZLM HOOK] 收到zlm心跳:" + param.getMediaServerId());  
113 -  
114 - taskExecutor.execute(()->{  
115 - List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_server_keepalive);  
116 - JSONObject json = (JSONObject) JSON.toJSON(param);  
117 - if (subscribes != null && subscribes.size() > 0) {  
118 - for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {  
119 - subscribe.response(null, json);  
120 - }  
121 - }  
122 - });  
123 - mediaServerService.updateMediaServerKeepalive(param.getMediaServerId(), param.getData());  
124 -  
125 - JSONObject ret = new JSONObject();  
126 - ret.put("code", 0);  
127 - ret.put("msg", "success");  
128 -  
129 - return ret;  
130 - }  
131 -  
132 - /**  
133 - * 播放器鉴权事件,rtsp/rtmp/http-flv/ws-flv/hls的播放都将触发此鉴权事件。  
134 - *  
135 - */  
136 - @ResponseBody  
137 - @PostMapping(value = "/on_play", produces = "application/json;charset=UTF-8")  
138 - public JSONObject onPlay(@RequestBody OnPlayHookParam param){  
139 - if (logger.isDebugEnabled()) {  
140 - logger.debug("[ZLM HOOK] 播放鉴权:{}->{}" + param.getMediaServerId(), param);  
141 - }  
142 - String mediaServerId = param.getMediaServerId();  
143 -  
144 - taskExecutor.execute(()->{  
145 - JSONObject json = (JSONObject) JSON.toJSON(param);  
146 - ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_play, json);  
147 - if (subscribe != null ) {  
148 - MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);  
149 - if (mediaInfo != null) {  
150 - subscribe.response(mediaInfo, json);  
151 - }  
152 - }  
153 - });  
154 - JSONObject ret = new JSONObject();  
155 - if (!"rtp".equals(param.getApp())) {  
156 - Map<String, String> paramMap = urlParamToMap(param.getParams());  
157 - StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(param.getApp(), param.getStream());  
158 - if (streamAuthorityInfo != null && streamAuthorityInfo.getCallId() != null && !streamAuthorityInfo.getCallId().equals(paramMap.get("callId"))) {  
159 - ret.put("code", 401);  
160 - ret.put("msg", "Unauthorized");  
161 - return ret;  
162 - }  
163 - }  
164 -  
165 - ret.put("code", 0);  
166 - ret.put("msg", "success");  
167 - return ret;  
168 - }  
169 -  
170 - /**  
171 - * rtsp/rtmp/rtp推流鉴权事件。  
172 - *  
173 - */  
174 - @ResponseBody  
175 - @PostMapping(value = "/on_publish", produces = "application/json;charset=UTF-8")  
176 - public JSONObject onPublish(@RequestBody OnPublishHookParam param) {  
177 -  
178 - JSONObject json = (JSONObject) JSON.toJSON(param);  
179 -  
180 - logger.info("[ZLM HOOK]推流鉴权:{}->{}", param.getMediaServerId(), param);  
181 - JSONObject ret = new JSONObject();  
182 - String mediaServerId = json.getString("mediaServerId");  
183 - MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);  
184 -  
185 - if (!"rtp".equals(param.getApp())) {  
186 - if (userSetting.getPushAuthority()) {  
187 - // 推流鉴权  
188 - if (param.getParams() == null) {  
189 - logger.info("推流鉴权失败: 缺少不要参数:sign=md5(user表的pushKey)");  
190 - ret.put("code", 401);  
191 - ret.put("msg", "Unauthorized");  
192 - return ret;  
193 - }  
194 - Map<String, String> paramMap = urlParamToMap(param.getParams());  
195 - String sign = paramMap.get("sign");  
196 - if (sign == null) {  
197 - logger.info("推流鉴权失败: 缺少不要参数:sign=md5(user表的pushKey)");  
198 - ret.put("code", 401);  
199 - ret.put("msg", "Unauthorized");  
200 - return ret;  
201 - }  
202 - // 推流自定义播放鉴权码  
203 - String callId = paramMap.get("callId");  
204 - // 鉴权配置  
205 - boolean hasAuthority = userService.checkPushAuthority(callId, sign);  
206 - if (!hasAuthority) {  
207 - logger.info("推流鉴权失败: sign 无权限: callId={}. sign={}", callId, sign);  
208 - ret.put("code", 401);  
209 - ret.put("msg", "Unauthorized");  
210 - return ret;  
211 - }  
212 - StreamAuthorityInfo streamAuthorityInfo = StreamAuthorityInfo.getInstanceByHook(param);  
213 - streamAuthorityInfo.setCallId(callId);  
214 - streamAuthorityInfo.setSign(sign);  
215 - // 鉴权通过  
216 - redisCatchStorage.updateStreamAuthorityInfo(param.getApp(), param.getStream(), streamAuthorityInfo);  
217 - // 通知assist新的callId  
218 - if (mediaInfo != null && mediaInfo.getRecordAssistPort() > 0) {  
219 - taskExecutor.execute(()->{  
220 - assistRESTfulUtils.addStreamCallInfo(mediaInfo, param.getApp(), param.getStream(), callId, null);  
221 - });  
222 - }  
223 - }  
224 - }else {  
225 - zlmMediaListManager.sendStreamEvent(param.getApp(),param.getStream(), param.getMediaServerId());  
226 - }  
227 -  
228 - ret.put("code", 0);  
229 - ret.put("msg", "success");  
230 -  
231 - if (!"rtp".equals(param.getApp())) {  
232 - ret.put("enable_audio", true);  
233 - }  
234 -  
235 - taskExecutor.execute(()->{  
236 - ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_publish, json);  
237 - if (subscribe != null) {  
238 - if (mediaInfo != null) {  
239 - subscribe.response(mediaInfo, json);  
240 - }else {  
241 - ret.put("code", 1);  
242 - ret.put("msg", "zlm not register");  
243 - }  
244 - }  
245 - });  
246 -  
247 - if ("rtp".equals(param.getApp())) {  
248 - ret.put("enable_mp4", userSetting.getRecordSip());  
249 - }else {  
250 - ret.put("enable_mp4", userSetting.isRecordPushLive());  
251 - }  
252 - List<SsrcTransaction> ssrcTransactionForAll = sessionManager.getSsrcTransactionForAll(null, null, null, param.getStream());  
253 - if (ssrcTransactionForAll != null && ssrcTransactionForAll.size() == 1) {  
254 - String deviceId = ssrcTransactionForAll.get(0).getDeviceId();  
255 - String channelId = ssrcTransactionForAll.get(0).getChannelId();  
256 - DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);  
257 - if (deviceChannel != null) {  
258 - ret.put("enable_audio", deviceChannel.isHasAudio());  
259 - }  
260 - // 如果是录像下载就设置视频间隔十秒  
261 - if (ssrcTransactionForAll.get(0).getType() == VideoStreamSessionManager.SessionType.download) {  
262 - ret.put("mp4_max_second", 10);  
263 - ret.put("enable_mp4", true);  
264 - ret.put("enable_audio", true);  
265 - }  
266 - }  
267 - return ret;  
268 - }  
269 -  
270 - /**  
271 - * rtsp/rtmp流注册或注销时触发此事件;此事件对回复不敏感。  
272 - *  
273 - */  
274 - @ResponseBody  
275 - @PostMapping(value = "/on_stream_changed", produces = "application/json;charset=UTF-8")  
276 - public JSONObject onStreamChanged(@RequestBody OnStreamChangedHookParam param){  
277 -  
278 - if (param.isRegist()) {  
279 - logger.info("[ZLM HOOK] 流注册, {}->{}->{}/{}", param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());  
280 - }else {  
281 - logger.info("[ZLM HOOK] 流注销, {}->{}->{}/{}", param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());  
282 - }  
283 -  
284 -  
285 - JSONObject json = (JSONObject) JSON.toJSON(param);  
286 - taskExecutor.execute(()->{  
287 - ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_stream_changed, json);  
288 - if (subscribe != null ) {  
289 - MediaServerItem mediaInfo = mediaServerService.getOne(param.getMediaServerId());  
290 - if (mediaInfo != null) {  
291 - subscribe.response(mediaInfo, json);  
292 - }  
293 - }  
294 - // 流消失移除redis play  
295 - List<OnStreamChangedHookParam.MediaTrack> tracks = param.getTracks();  
296 - if (param.isRegist()) {  
297 - if (param.getOriginType() == OriginType.RTMP_PUSH.ordinal()  
298 - || param.getOriginType() == OriginType.RTSP_PUSH.ordinal()  
299 - || param.getOriginType() == OriginType.RTC_PUSH.ordinal()) {  
300 -  
301 - StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(param.getApp(), param.getStream());  
302 - if (streamAuthorityInfo == null) {  
303 - streamAuthorityInfo = StreamAuthorityInfo.getInstanceByHook(param);  
304 - }else {  
305 - streamAuthorityInfo.setOriginType(param.getOriginType());  
306 - streamAuthorityInfo.setOriginTypeStr(param.getOriginTypeStr());  
307 - }  
308 - redisCatchStorage.updateStreamAuthorityInfo(param.getApp(), param.getStream(), streamAuthorityInfo);  
309 - }  
310 - }else {  
311 - redisCatchStorage.removeStreamAuthorityInfo(param.getApp(), param.getStream());  
312 - }  
313 -  
314 - if ("rtsp".equals(param.getSchema())){  
315 - if (param.isRegist()) {  
316 - mediaServerService.addCount(param.getMediaServerId());  
317 - }else {  
318 - mediaServerService.removeCount(param.getMediaServerId());  
319 - }  
320 - if (param.getOriginType() == OriginType.PULL.ordinal()  
321 - || param.getOriginType() == OriginType.FFMPEG_PULL.ordinal()) {  
322 - // 设置拉流代理上线/离线  
323 - streamProxyService.updateStatus(param.isRegist(), param.getApp(), param.getStream());  
324 - }  
325 - if ("rtp".equals(param.getApp()) && !param.isRegist() ) {  
326 - StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(param.getStream());  
327 - if (streamInfo!=null){  
328 - redisCatchStorage.stopPlay(streamInfo);  
329 - storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());  
330 - }else{  
331 - streamInfo = redisCatchStorage.queryPlayback(null, null, param.getStream(), null);  
332 - if (streamInfo != null) {  
333 - redisCatchStorage.stopPlayback(streamInfo.getDeviceID(), streamInfo.getChannelId(),  
334 - streamInfo.getStream(), null);  
335 - }  
336 - }  
337 - }else {  
338 - if (!"rtp".equals(param.getApp())){  
339 - String type = OriginType.values()[param.getOriginType()].getType();  
340 - MediaServerItem mediaServerItem = mediaServerService.getOne(param.getMediaServerId());  
341 -  
342 - if (mediaServerItem != null){  
343 - if (param.isRegist()) {  
344 - StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(param.getApp(), param.getStream());  
345 - String callId = null;  
346 - if (streamAuthorityInfo != null) {  
347 - callId = streamAuthorityInfo.getCallId();  
348 - }  
349 - StreamInfo streamInfoByAppAndStream = mediaService.getStreamInfoByAppAndStream(mediaServerItem,  
350 - param.getApp(), param.getStream(), tracks, callId);  
351 - param.setStreamInfo(new StreamContent(streamInfoByAppAndStream));  
352 - redisCatchStorage.addStream(mediaServerItem, type, param.getApp(), param.getStream(), param);  
353 - if (param.getOriginType() == OriginType.RTSP_PUSH.ordinal()  
354 - || param.getOriginType() == OriginType.RTMP_PUSH.ordinal()  
355 - || param.getOriginType() == OriginType.RTC_PUSH.ordinal() ) {  
356 - param.setSeverId(userSetting.getServerId());  
357 - zlmMediaListManager.addPush(param);  
358 - }  
359 - }else {  
360 - // 兼容流注销时类型从redis记录获取  
361 - OnStreamChangedHookParam onStreamChangedHookParam = redisCatchStorage.getStreamInfo(param.getApp(), param.getStream(), param.getMediaServerId());  
362 - if (onStreamChangedHookParam != null) {  
363 - type = OriginType.values()[onStreamChangedHookParam.getOriginType()].getType();  
364 - redisCatchStorage.removeStream(mediaServerItem.getId(), type, param.getApp(), param.getStream());  
365 - }  
366 - GbStream gbStream = storager.getGbStream(param.getApp(), param.getStream());  
367 - if (gbStream != null) { 56 + @Autowired
  57 + private SIPCommander cmder;
  58 +
  59 + @Autowired
  60 + private SIPCommanderFroPlatform commanderFroPlatform;
  61 +
  62 + @Autowired
  63 + private IPlayService playService;
  64 +
  65 + @Autowired
  66 + private IVideoManagerStorage storager;
  67 +
  68 + @Autowired
  69 + private IRedisCatchStorage redisCatchStorage;
  70 +
  71 + @Autowired
  72 + private IDeviceService deviceService;
  73 +
  74 + @Autowired
  75 + private IMediaServerService mediaServerService;
  76 +
  77 + @Autowired
  78 + private IStreamProxyService streamProxyService;
  79 +
  80 + @Autowired
  81 + private DeferredResultHolder resultHolder;
  82 +
  83 + @Autowired
  84 + private IMediaService mediaService;
  85 +
  86 + @Autowired
  87 + private EventPublisher eventPublisher;
  88 +
  89 + @Autowired
  90 + private ZLMMediaListManager zlmMediaListManager;
  91 +
  92 + @Autowired
  93 + private ZlmHttpHookSubscribe subscribe;
  94 +
  95 + @Autowired
  96 + private UserSetting userSetting;
  97 +
  98 + @Autowired
  99 + private IUserService userService;
  100 +
  101 + @Autowired
  102 + private VideoStreamSessionManager sessionManager;
  103 +
  104 + @Autowired
  105 + private AssistRESTfulUtils assistRESTfulUtils;
  106 +
  107 + @Qualifier("taskExecutor")
  108 + @Autowired
  109 + private ThreadPoolTaskExecutor taskExecutor;
  110 +
  111 + /**
  112 + * 服务器定时上报时间,上报间隔可配置,默认10s上报一次
  113 + */
  114 + @ResponseBody
  115 + @PostMapping(value = "/on_server_keepalive", produces = "application/json;charset=UTF-8")
  116 + public HookResult onServerKeepalive(@RequestBody OnServerKeepaliveHookParam param) {
  117 +
  118 + logger.info("[ZLM HOOK] 收到zlm心跳:" + param.getMediaServerId());
  119 +
  120 + taskExecutor.execute(() -> {
  121 + List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_server_keepalive);
  122 + JSONObject json = (JSONObject) JSON.toJSON(param);
  123 + if (subscribes != null && subscribes.size() > 0) {
  124 + for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {
  125 + subscribe.response(null, json);
  126 + }
  127 + }
  128 + });
  129 + mediaServerService.updateMediaServerKeepalive(param.getMediaServerId(), param.getData());
  130 +
  131 + return HookResult.SUCCESS();
  132 + }
  133 +
  134 + /**
  135 + * 播放器鉴权事件,rtsp/rtmp/http-flv/ws-flv/hls的播放都将触发此鉴权事件。
  136 + */
  137 + @ResponseBody
  138 + @PostMapping(value = "/on_play", produces = "application/json;charset=UTF-8")
  139 + public HookResult onPlay(@RequestBody OnPlayHookParam param) {
  140 + if (logger.isDebugEnabled()) {
  141 + logger.debug("[ZLM HOOK] 播放鉴权:{}->{}" + param.getMediaServerId(), param);
  142 + }
  143 + String mediaServerId = param.getMediaServerId();
  144 +
  145 + taskExecutor.execute(() -> {
  146 + JSONObject json = (JSONObject) JSON.toJSON(param);
  147 + ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_play, json);
  148 + if (subscribe != null) {
  149 + MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
  150 + if (mediaInfo != null) {
  151 + subscribe.response(mediaInfo, json);
  152 + }
  153 + }
  154 + });
  155 + if (!"rtp".equals(param.getApp())) {
  156 + Map<String, String> paramMap = urlParamToMap(param.getParams());
  157 + StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(param.getApp(), param.getStream());
  158 + if (streamAuthorityInfo != null && streamAuthorityInfo.getCallId() != null && !streamAuthorityInfo.getCallId().equals(paramMap.get("callId"))) {
  159 + return new HookResult(401, "Unauthorized");
  160 + }
  161 + }
  162 +
  163 + return HookResult.SUCCESS();
  164 + }
  165 +
  166 + /**
  167 + * rtsp/rtmp/rtp推流鉴权事件。
  168 + */
  169 + @ResponseBody
  170 + @PostMapping(value = "/on_publish", produces = "application/json;charset=UTF-8")
  171 + public HookResultForOnPublish onPublish(@RequestBody OnPublishHookParam param) {
  172 +
  173 + JSONObject json = (JSONObject) JSON.toJSON(param);
  174 +
  175 + logger.info("[ZLM HOOK]推流鉴权:{}->{}", param.getMediaServerId(), param);
  176 +
  177 + String mediaServerId = json.getString("mediaServerId");
  178 + MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
  179 +
  180 + if (!"rtp".equals(param.getApp())) {
  181 + if (userSetting.getPushAuthority()) {
  182 + // 推流鉴权
  183 + if (param.getParams() == null) {
  184 + logger.info("推流鉴权失败: 缺少不要参数:sign=md5(user表的pushKey)");
  185 + return new HookResultForOnPublish(401, "Unauthorized");
  186 + }
  187 + Map<String, String> paramMap = urlParamToMap(param.getParams());
  188 + String sign = paramMap.get("sign");
  189 + if (sign == null) {
  190 + logger.info("推流鉴权失败: 缺少不要参数:sign=md5(user表的pushKey)");
  191 + return new HookResultForOnPublish(401, "Unauthorized");
  192 + }
  193 + // 推流自定义播放鉴权码
  194 + String callId = paramMap.get("callId");
  195 + // 鉴权配置
  196 + boolean hasAuthority = userService.checkPushAuthority(callId, sign);
  197 + if (!hasAuthority) {
  198 + logger.info("推流鉴权失败: sign 无权限: callId={}. sign={}", callId, sign);
  199 + return new HookResultForOnPublish(401, "Unauthorized");
  200 + }
  201 + StreamAuthorityInfo streamAuthorityInfo = StreamAuthorityInfo.getInstanceByHook(param);
  202 + streamAuthorityInfo.setCallId(callId);
  203 + streamAuthorityInfo.setSign(sign);
  204 + // 鉴权通过
  205 + redisCatchStorage.updateStreamAuthorityInfo(param.getApp(), param.getStream(), streamAuthorityInfo);
  206 + // 通知assist新的callId
  207 + if (mediaInfo != null && mediaInfo.getRecordAssistPort() > 0) {
  208 + taskExecutor.execute(() -> {
  209 + assistRESTfulUtils.addStreamCallInfo(mediaInfo, param.getApp(), param.getStream(), callId, null);
  210 + });
  211 + }
  212 + }
  213 + } else {
  214 + zlmMediaListManager.sendStreamEvent(param.getApp(), param.getStream(), param.getMediaServerId());
  215 + }
  216 +
  217 +
  218 + HookResultForOnPublish result = HookResultForOnPublish.SUCCESS();
  219 + if (!"rtp".equals(param.getApp())) {
  220 + result.setEnable_audio(true);
  221 + }
  222 +
  223 + taskExecutor.execute(() -> {
  224 + ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_publish, json);
  225 + if (subscribe != null) {
  226 + if (mediaInfo != null) {
  227 + subscribe.response(mediaInfo, json);
  228 + } else {
  229 + new HookResultForOnPublish(1, "zlm not register");
  230 + }
  231 + }
  232 + });
  233 +
  234 + if ("rtp".equals(param.getApp())) {
  235 + result.setEnable_mp4(userSetting.getRecordSip());
  236 + } else {
  237 + result.setEnable_mp4(userSetting.isRecordPushLive());
  238 + }
  239 + List<SsrcTransaction> ssrcTransactionForAll = sessionManager.getSsrcTransactionForAll(null, null, null, param.getStream());
  240 + if (ssrcTransactionForAll != null && ssrcTransactionForAll.size() == 1) {
  241 + String deviceId = ssrcTransactionForAll.get(0).getDeviceId();
  242 + String channelId = ssrcTransactionForAll.get(0).getChannelId();
  243 + DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);
  244 + if (deviceChannel != null) {
  245 + result.setEnable_audio(deviceChannel.isHasAudio());
  246 + }
  247 + // 如果是录像下载就设置视频间隔十秒
  248 + if (ssrcTransactionForAll.get(0).getType() == VideoStreamSessionManager.SessionType.download) {
  249 + result.setMp4_max_second(10);
  250 + result.setEnable_audio(true);
  251 + result.setEnable_mp4(true);
  252 + }
  253 + }
  254 + return result;
  255 + }
  256 +
  257 + /**
  258 + * rtsp/rtmp流注册或注销时触发此事件;此事件对回复不敏感。
  259 + */
  260 + @ResponseBody
  261 + @PostMapping(value = "/on_stream_changed", produces = "application/json;charset=UTF-8")
  262 + public HookResult onStreamChanged(@RequestBody OnStreamChangedHookParam param) {
  263 +
  264 + if (param.isRegist()) {
  265 + logger.info("[ZLM HOOK] 流注册, {}->{}->{}/{}", param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());
  266 + } else {
  267 + logger.info("[ZLM HOOK] 流注销, {}->{}->{}/{}", param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());
  268 + }
  269 +
  270 +
  271 + JSONObject json = (JSONObject) JSON.toJSON(param);
  272 + taskExecutor.execute(() -> {
  273 + ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_stream_changed, json);
  274 + if (subscribe != null) {
  275 + MediaServerItem mediaInfo = mediaServerService.getOne(param.getMediaServerId());
  276 + if (mediaInfo != null) {
  277 + subscribe.response(mediaInfo, json);
  278 + }
  279 + }
  280 + // 流消失移除redis play
  281 + List<OnStreamChangedHookParam.MediaTrack> tracks = param.getTracks();
  282 + if (param.isRegist()) {
  283 + if (param.getOriginType() == OriginType.RTMP_PUSH.ordinal()
  284 + || param.getOriginType() == OriginType.RTSP_PUSH.ordinal()
  285 + || param.getOriginType() == OriginType.RTC_PUSH.ordinal()) {
  286 +
  287 + StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(param.getApp(), param.getStream());
  288 + if (streamAuthorityInfo == null) {
  289 + streamAuthorityInfo = StreamAuthorityInfo.getInstanceByHook(param);
  290 + } else {
  291 + streamAuthorityInfo.setOriginType(param.getOriginType());
  292 + streamAuthorityInfo.setOriginTypeStr(param.getOriginTypeStr());
  293 + }
  294 + redisCatchStorage.updateStreamAuthorityInfo(param.getApp(), param.getStream(), streamAuthorityInfo);
  295 + }
  296 + } else {
  297 + redisCatchStorage.removeStreamAuthorityInfo(param.getApp(), param.getStream());
  298 + }
  299 +
  300 + if ("rtsp".equals(param.getSchema())) {
  301 + if (param.isRegist()) {
  302 + mediaServerService.addCount(param.getMediaServerId());
  303 + } else {
  304 + mediaServerService.removeCount(param.getMediaServerId());
  305 + }
  306 + if (param.getOriginType() == OriginType.PULL.ordinal()
  307 + || param.getOriginType() == OriginType.FFMPEG_PULL.ordinal()) {
  308 + // 设置拉流代理上线/离线
  309 + streamProxyService.updateStatus(param.isRegist(), param.getApp(), param.getStream());
  310 + }
  311 + if ("rtp".equals(param.getApp()) && !param.isRegist()) {
  312 + StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(param.getStream());
  313 + if (streamInfo != null) {
  314 + redisCatchStorage.stopPlay(streamInfo);
  315 + storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
  316 + } else {
  317 + streamInfo = redisCatchStorage.queryPlayback(null, null, param.getStream(), null);
  318 + if (streamInfo != null) {
  319 + redisCatchStorage.stopPlayback(streamInfo.getDeviceID(), streamInfo.getChannelId(),
  320 + streamInfo.getStream(), null);
  321 + }
  322 + }
  323 + } else {
  324 + if (!"rtp".equals(param.getApp())) {
  325 + String type = OriginType.values()[param.getOriginType()].getType();
  326 + MediaServerItem mediaServerItem = mediaServerService.getOne(param.getMediaServerId());
  327 +
  328 + if (mediaServerItem != null) {
  329 + if (param.isRegist()) {
  330 + StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(param.getApp(), param.getStream());
  331 + String callId = null;
  332 + if (streamAuthorityInfo != null) {
  333 + callId = streamAuthorityInfo.getCallId();
  334 + }
  335 + StreamInfo streamInfoByAppAndStream = mediaService.getStreamInfoByAppAndStream(mediaServerItem,
  336 + param.getApp(), param.getStream(), tracks, callId);
  337 + param.setStreamInfo(new StreamContent(streamInfoByAppAndStream));
  338 + redisCatchStorage.addStream(mediaServerItem, type, param.getApp(), param.getStream(), param);
  339 + if (param.getOriginType() == OriginType.RTSP_PUSH.ordinal()
  340 + || param.getOriginType() == OriginType.RTMP_PUSH.ordinal()
  341 + || param.getOriginType() == OriginType.RTC_PUSH.ordinal()) {
  342 + param.setSeverId(userSetting.getServerId());
  343 + zlmMediaListManager.addPush(param);
  344 + }
  345 + } else {
  346 + // 兼容流注销时类型从redis记录获取
  347 + OnStreamChangedHookParam onStreamChangedHookParam = redisCatchStorage.getStreamInfo(param.getApp(), param.getStream(), param.getMediaServerId());
  348 + if (onStreamChangedHookParam != null) {
  349 + type = OriginType.values()[onStreamChangedHookParam.getOriginType()].getType();
  350 + redisCatchStorage.removeStream(mediaServerItem.getId(), type, param.getApp(), param.getStream());
  351 + }
  352 + GbStream gbStream = storager.getGbStream(param.getApp(), param.getStream());
  353 + if (gbStream != null) {
368 // eventPublisher.catalogEventPublishForStream(null, gbStream, CatalogEvent.OFF); 354 // eventPublisher.catalogEventPublishForStream(null, gbStream, CatalogEvent.OFF);
369 - }  
370 - zlmMediaListManager.removeMedia(param.getApp(), param.getStream());  
371 - }  
372 - if (type != null) {  
373 - // 发送流变化redis消息  
374 - JSONObject jsonObject = new JSONObject();  
375 - jsonObject.put("serverId", userSetting.getServerId());  
376 - jsonObject.put("app", param.getApp());  
377 - jsonObject.put("stream", param.getStream());  
378 - jsonObject.put("register", param.isRegist());  
379 - jsonObject.put("mediaServerId", param.getMediaServerId());  
380 - redisCatchStorage.sendStreamChangeMsg(type, jsonObject);  
381 - }  
382 - }  
383 - }  
384 - }  
385 - if (!param.isRegist()) {  
386 - List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServerByStream(param.getStream());  
387 - if (sendRtpItems.size() > 0) {  
388 - for (SendRtpItem sendRtpItem : sendRtpItems) {  
389 - if (sendRtpItem.getApp().equals(param.getApp())) {  
390 - String platformId = sendRtpItem.getPlatformId();  
391 - ParentPlatform platform = storager.queryParentPlatByServerGBId(platformId);  
392 - Device device = deviceService.getDevice(platformId);  
393 -  
394 - try {  
395 - if (platform != null) {  
396 - commanderFroPlatform.streamByeCmd(platform, sendRtpItem);  
397 - }else {  
398 - cmder.streamByeCmd(device, sendRtpItem.getChannelId(), param.getStream(), sendRtpItem.getCallId());  
399 - }  
400 - } catch (SipException | InvalidArgumentException | ParseException | SsrcTransactionNotFoundException e) {  
401 - logger.error("[命令发送失败] 国标级联 发送BYE: {}", e.getMessage());  
402 - }  
403 - }  
404 - }  
405 - }  
406 - }  
407 - }  
408 - });  
409 -  
410 - JSONObject ret = new JSONObject();  
411 - ret.put("code", 0);  
412 - ret.put("msg", "success");  
413 - return ret;  
414 - }  
415 -  
416 - /**  
417 - * 流无人观看时事件,用户可以通过此事件选择是否关闭无人看的流。  
418 - *  
419 - */  
420 - @ResponseBody  
421 - @PostMapping(value = "/on_stream_none_reader", produces = "application/json;charset=UTF-8")  
422 - public JSONObject onStreamNoneReader(@RequestBody OnStreamNoneReaderHookParam param){  
423 -  
424 - logger.info("[ZLM HOOK]流无人观看:{]->{}->{}/{}" + param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());  
425 - JSONObject ret = new JSONObject();  
426 - ret.put("code", 0);  
427 - // 国标类型的流  
428 - if ("rtp".equals(param.getApp())){  
429 - ret.put("close", userSetting.getStreamOnDemand());  
430 - // 国标流, 点播/录像回放/录像下载  
431 - StreamInfo streamInfoForPlayCatch = redisCatchStorage.queryPlayByStreamId(param.getStream());  
432 - // 点播  
433 - if (streamInfoForPlayCatch != null) {  
434 - // 收到无人观看说明流也没有在往上级推送  
435 - if (redisCatchStorage.isChannelSendingRTP(streamInfoForPlayCatch.getChannelId())) {  
436 - List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServerByChnnelId(streamInfoForPlayCatch.getChannelId());  
437 - if (sendRtpItems.size() > 0) {  
438 - for (SendRtpItem sendRtpItem : sendRtpItems) {  
439 - ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(sendRtpItem.getPlatformId());  
440 - try {  
441 - commanderFroPlatform.streamByeCmd(parentPlatform, sendRtpItem.getCallId());  
442 - } catch (SipException | InvalidArgumentException | ParseException e) {  
443 - logger.error("[命令发送失败] 国标级联 发送BYE: {}", e.getMessage());  
444 - }  
445 - redisCatchStorage.deleteSendRTPServer(parentPlatform.getServerGBId(), sendRtpItem.getChannelId(),  
446 - sendRtpItem.getCallId(), sendRtpItem.getStreamId());  
447 - }  
448 - }  
449 - }  
450 - Device device = deviceService.getDevice(streamInfoForPlayCatch.getDeviceID());  
451 - if (device != null) {  
452 - try {  
453 - cmder.streamByeCmd(device, streamInfoForPlayCatch.getChannelId(),  
454 - streamInfoForPlayCatch.getStream(), null);  
455 - } catch (InvalidArgumentException | ParseException | SipException | SsrcTransactionNotFoundException e) {  
456 - logger.error("[无人观看]点播, 发送BYE失败 {}", e.getMessage());  
457 - }  
458 - }  
459 -  
460 - redisCatchStorage.stopPlay(streamInfoForPlayCatch);  
461 - storager.stopPlay(streamInfoForPlayCatch.getDeviceID(), streamInfoForPlayCatch.getChannelId());  
462 - return ret;  
463 - }  
464 - // 录像回放  
465 - StreamInfo streamInfoForPlayBackCatch = redisCatchStorage.queryPlayback(null, null, param.getStream(), null);  
466 - if (streamInfoForPlayBackCatch != null ) {  
467 - if (streamInfoForPlayBackCatch.isPause()) {  
468 - ret.put("close", false);  
469 - }else {  
470 - Device device = deviceService.getDevice(streamInfoForPlayBackCatch.getDeviceID());  
471 - if (device != null) {  
472 - try {  
473 - cmder.streamByeCmd(device,streamInfoForPlayBackCatch.getChannelId(),  
474 - streamInfoForPlayBackCatch.getStream(), null);  
475 - } catch (InvalidArgumentException | ParseException | SipException |  
476 - SsrcTransactionNotFoundException e) {  
477 - logger.error("[无人观看]回放, 发送BYE失败 {}", e.getMessage());  
478 - }  
479 - }  
480 - redisCatchStorage.stopPlayback(streamInfoForPlayBackCatch.getDeviceID(),  
481 - streamInfoForPlayBackCatch.getChannelId(), streamInfoForPlayBackCatch.getStream(), null);  
482 - }  
483 - return ret;  
484 - }  
485 - // 录像下载  
486 - StreamInfo streamInfoForDownload = redisCatchStorage.queryDownload(null, null, param.getStream(), null);  
487 - // 进行录像下载时无人观看不断流  
488 - if (streamInfoForDownload != null) {  
489 - ret.put("close", false);  
490 - return ret;  
491 - }  
492 - }else {  
493 - // 非国标流 推流/拉流代理  
494 - // 拉流代理  
495 - StreamProxyItem streamProxyItem = streamProxyService.getStreamProxyByAppAndStream(param.getApp(), param.getStream());  
496 - if (streamProxyItem != null ) {  
497 - if (streamProxyItem.isEnable_remove_none_reader()) {  
498 - // 无人观看自动移除  
499 - ret.put("close", true);  
500 - streamProxyService.del(param.getApp(), param.getStream());  
501 - String url = streamProxyItem.getUrl() != null?streamProxyItem.getUrl():streamProxyItem.getSrc_url();  
502 - logger.info("[{}/{}]<-[{}] 拉流代理无人观看已经移除", param.getApp(), param.getStream(), url);  
503 - }else if (streamProxyItem.isEnable_disable_none_reader()) {  
504 - // 无人观看停用  
505 - ret.put("close", true);  
506 - // 修改数据  
507 - streamProxyService.stop(param.getApp(), param.getStream());  
508 - }else {  
509 - // 无人观看不做处理  
510 - ret.put("close", false);  
511 - }  
512 - return ret;  
513 - }  
514 - // 推流具有主动性,暂时不做处理 355 + }
  356 + zlmMediaListManager.removeMedia(param.getApp(), param.getStream());
  357 + }
  358 + if (type != null) {
  359 + // 发送流变化redis消息
  360 + JSONObject jsonObject = new JSONObject();
  361 + jsonObject.put("serverId", userSetting.getServerId());
  362 + jsonObject.put("app", param.getApp());
  363 + jsonObject.put("stream", param.getStream());
  364 + jsonObject.put("register", param.isRegist());
  365 + jsonObject.put("mediaServerId", param.getMediaServerId());
  366 + redisCatchStorage.sendStreamChangeMsg(type, jsonObject);
  367 + }
  368 + }
  369 + }
  370 + }
  371 + if (!param.isRegist()) {
  372 + List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServerByStream(param.getStream());
  373 + if (sendRtpItems.size() > 0) {
  374 + for (SendRtpItem sendRtpItem : sendRtpItems) {
  375 + if (sendRtpItem.getApp().equals(param.getApp())) {
  376 + String platformId = sendRtpItem.getPlatformId();
  377 + ParentPlatform platform = storager.queryParentPlatByServerGBId(platformId);
  378 + Device device = deviceService.getDevice(platformId);
  379 +
  380 + try {
  381 + if (platform != null) {
  382 + commanderFroPlatform.streamByeCmd(platform, sendRtpItem);
  383 + } else {
  384 + cmder.streamByeCmd(device, sendRtpItem.getChannelId(), param.getStream(), sendRtpItem.getCallId());
  385 + }
  386 + } catch (SipException | InvalidArgumentException | ParseException |
  387 + SsrcTransactionNotFoundException e) {
  388 + logger.error("[命令发送失败] 国标级联 发送BYE: {}", e.getMessage());
  389 + }
  390 + }
  391 + }
  392 + }
  393 + }
  394 + }
  395 + });
  396 +
  397 + return HookResult.SUCCESS();
  398 + }
  399 +
  400 + /**
  401 + * 流无人观看时事件,用户可以通过此事件选择是否关闭无人看的流。
  402 + */
  403 + @ResponseBody
  404 + @PostMapping(value = "/on_stream_none_reader", produces = "application/json;charset=UTF-8")
  405 + public JSONObject onStreamNoneReader(@RequestBody OnStreamNoneReaderHookParam param) {
  406 +
  407 + logger.info("[ZLM HOOK]流无人观看:{]->{}->{}/{}" + param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());
  408 + JSONObject ret = new JSONObject();
  409 + ret.put("code", 0);
  410 + // 国标类型的流
  411 + if ("rtp".equals(param.getApp())) {
  412 + ret.put("close", userSetting.getStreamOnDemand());
  413 + // 国标流, 点播/录像回放/录像下载
  414 + StreamInfo streamInfoForPlayCatch = redisCatchStorage.queryPlayByStreamId(param.getStream());
  415 + // 点播
  416 + if (streamInfoForPlayCatch != null) {
  417 + // 收到无人观看说明流也没有在往上级推送
  418 + if (redisCatchStorage.isChannelSendingRTP(streamInfoForPlayCatch.getChannelId())) {
  419 + List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServerByChnnelId(streamInfoForPlayCatch.getChannelId());
  420 + if (sendRtpItems.size() > 0) {
  421 + for (SendRtpItem sendRtpItem : sendRtpItems) {
  422 + ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(sendRtpItem.getPlatformId());
  423 + try {
  424 + commanderFroPlatform.streamByeCmd(parentPlatform, sendRtpItem.getCallId());
  425 + } catch (SipException | InvalidArgumentException | ParseException e) {
  426 + logger.error("[命令发送失败] 国标级联 发送BYE: {}", e.getMessage());
  427 + }
  428 + redisCatchStorage.deleteSendRTPServer(parentPlatform.getServerGBId(), sendRtpItem.getChannelId(),
  429 + sendRtpItem.getCallId(), sendRtpItem.getStreamId());
  430 + }
  431 + }
  432 + }
  433 + Device device = deviceService.getDevice(streamInfoForPlayCatch.getDeviceID());
  434 + if (device != null) {
  435 + try {
  436 + cmder.streamByeCmd(device, streamInfoForPlayCatch.getChannelId(),
  437 + streamInfoForPlayCatch.getStream(), null);
  438 + } catch (InvalidArgumentException | ParseException | SipException |
  439 + SsrcTransactionNotFoundException e) {
  440 + logger.error("[无人观看]点播, 发送BYE失败 {}", e.getMessage());
  441 + }
  442 + }
  443 +
  444 + redisCatchStorage.stopPlay(streamInfoForPlayCatch);
  445 + storager.stopPlay(streamInfoForPlayCatch.getDeviceID(), streamInfoForPlayCatch.getChannelId());
  446 + return ret;
  447 + }
  448 + // 录像回放
  449 + StreamInfo streamInfoForPlayBackCatch = redisCatchStorage.queryPlayback(null, null, param.getStream(), null);
  450 + if (streamInfoForPlayBackCatch != null) {
  451 + if (streamInfoForPlayBackCatch.isPause()) {
  452 + ret.put("close", false);
  453 + } else {
  454 + Device device = deviceService.getDevice(streamInfoForPlayBackCatch.getDeviceID());
  455 + if (device != null) {
  456 + try {
  457 + cmder.streamByeCmd(device, streamInfoForPlayBackCatch.getChannelId(),
  458 + streamInfoForPlayBackCatch.getStream(), null);
  459 + } catch (InvalidArgumentException | ParseException | SipException |
  460 + SsrcTransactionNotFoundException e) {
  461 + logger.error("[无人观看]回放, 发送BYE失败 {}", e.getMessage());
  462 + }
  463 + }
  464 + redisCatchStorage.stopPlayback(streamInfoForPlayBackCatch.getDeviceID(),
  465 + streamInfoForPlayBackCatch.getChannelId(), streamInfoForPlayBackCatch.getStream(), null);
  466 + }
  467 + return ret;
  468 + }
  469 + // 录像下载
  470 + StreamInfo streamInfoForDownload = redisCatchStorage.queryDownload(null, null, param.getStream(), null);
  471 + // 进行录像下载时无人观看不断流
  472 + if (streamInfoForDownload != null) {
  473 + ret.put("close", false);
  474 + return ret;
  475 + }
  476 + } else {
  477 + // 非国标流 推流/拉流代理
  478 + // 拉流代理
  479 + StreamProxyItem streamProxyItem = streamProxyService.getStreamProxyByAppAndStream(param.getApp(), param.getStream());
  480 + if (streamProxyItem != null) {
  481 + if (streamProxyItem.isEnable_remove_none_reader()) {
  482 + // 无人观看自动移除
  483 + ret.put("close", true);
  484 + streamProxyService.del(param.getApp(), param.getStream());
  485 + String url = streamProxyItem.getUrl() != null ? streamProxyItem.getUrl() : streamProxyItem.getSrc_url();
  486 + logger.info("[{}/{}]<-[{}] 拉流代理无人观看已经移除", param.getApp(), param.getStream(), url);
  487 + } else if (streamProxyItem.isEnable_disable_none_reader()) {
  488 + // 无人观看停用
  489 + ret.put("close", true);
  490 + // 修改数据
  491 + streamProxyService.stop(param.getApp(), param.getStream());
  492 + } else {
  493 + // 无人观看不做处理
  494 + ret.put("close", false);
  495 + }
  496 + return ret;
  497 + }
  498 + // 推流具有主动性,暂时不做处理
515 // StreamPushItem streamPushItem = streamPushService.getPush(app, streamId); 499 // StreamPushItem streamPushItem = streamPushService.getPush(app, streamId);
516 // if (streamPushItem != null) { 500 // if (streamPushItem != null) {
517 // // TODO 发送停止 501 // // TODO 发送停止
518 // 502 //
519 // } 503 // }
520 - }  
521 - return ret;  
522 - }  
523 -  
524 - /**  
525 - * 流未找到事件,用户可以在此事件触发时,立即去拉流,这样可以实现按需拉流;此事件对回复不敏感。  
526 - *  
527 - */  
528 - @ResponseBody  
529 - @PostMapping(value = "/on_stream_not_found", produces = "application/json;charset=UTF-8")  
530 - public JSONObject onStreamNotFound(@RequestBody OnStreamNotFoundHookParam param){  
531 - logger.info("[ZLM HOOK] 流未找到:{}->{}->{}/{}", param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());  
532 - taskExecutor.execute(()->{  
533 - MediaServerItem mediaInfo = mediaServerService.getOne(param.getMediaServerId());  
534 - if (userSetting.isAutoApplyPlay() && mediaInfo != null) {  
535 - if ("rtp".equals(param.getApp())) {  
536 - if (mediaInfo.isRtpEnable()) {  
537 - String[] s = param.getStream().split("_");  
538 - if (s.length == 2) {  
539 - String deviceId = s[0];  
540 - String channelId = s[1];  
541 - Device device = redisCatchStorage.getDevice(deviceId);  
542 - if (device != null) {  
543 - playService.play(mediaInfo,deviceId, channelId, null, null, null);  
544 - }  
545 - }  
546 - }  
547 - }else {  
548 - // 拉流代理  
549 - StreamProxyItem streamProxyByAppAndStream = streamProxyService.getStreamProxyByAppAndStream(param.getApp(), param.getStream());  
550 - if (streamProxyByAppAndStream != null && streamProxyByAppAndStream.isEnable_disable_none_reader()) {  
551 - streamProxyService.start(param.getApp(), param.getStream());  
552 - }  
553 - }  
554 - }  
555 - });  
556 -  
557 -  
558 - JSONObject ret = new JSONObject();  
559 - ret.put("code", 0);  
560 - ret.put("msg", "success");  
561 - return ret;  
562 - }  
563 -  
564 - /**  
565 - * 服务器启动事件,可以用于监听服务器崩溃重启;此事件对回复不敏感。  
566 - *  
567 - */  
568 - @ResponseBody  
569 - @PostMapping(value = "/on_server_started", produces = "application/json;charset=UTF-8")  
570 - public JSONObject onServerStarted(HttpServletRequest request, @RequestBody JSONObject jsonObject){  
571 -  
572 - jsonObject.put("ip", request.getRemoteAddr());  
573 - ZLMServerConfig zlmServerConfig = JSON.to(ZLMServerConfig.class, jsonObject);  
574 - zlmServerConfig.setIp(request.getRemoteAddr());  
575 - logger.info("[ZLM HOOK] zlm 启动 " + zlmServerConfig.getGeneralMediaServerId());  
576 - taskExecutor.execute(()->{  
577 - List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_server_started);  
578 - if (subscribes != null && subscribes.size() > 0) {  
579 - for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {  
580 - subscribe.response(null, jsonObject);  
581 - }  
582 - }  
583 - mediaServerService.zlmServerOnline(zlmServerConfig);  
584 - });  
585 -  
586 - JSONObject ret = new JSONObject();  
587 - ret.put("code", 0);  
588 - ret.put("msg", "success");  
589 - return ret;  
590 - }  
591 -  
592 - /**  
593 - * 发送rtp(startSendRtp)被动关闭时回调  
594 - */  
595 - @ResponseBody  
596 - @PostMapping(value = "/on_send_rtp_stopped", produces = "application/json;charset=UTF-8")  
597 - public JSONObject onSendRtpStopped(HttpServletRequest request, @RequestBody OnSendRtpStoppedHookParam param){  
598 -  
599 - logger.info("[ZLM HOOK] rtp发送关闭:{}->{}/{}", param.getMediaServerId(), param.getApp(), param.getStream());  
600 -  
601 - JSONObject ret = new JSONObject();  
602 - ret.put("code", 0);  
603 - ret.put("msg", "success");  
604 -  
605 - // 查找对应的上级推流,发送停止  
606 - if (!"rtp".equals(param.getApp())) {  
607 - return ret;  
608 - }  
609 - taskExecutor.execute(()->{  
610 - List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServerByStream(param.getStream());  
611 - if (sendRtpItems.size() > 0) {  
612 - for (SendRtpItem sendRtpItem : sendRtpItems) {  
613 - ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(sendRtpItem.getPlatformId());  
614 - try {  
615 - commanderFroPlatform.streamByeCmd(parentPlatform, sendRtpItem.getCallId());  
616 - } catch (SipException | InvalidArgumentException | ParseException e) {  
617 - logger.error("[命令发送失败] 国标级联 发送BYE: {}", e.getMessage());  
618 - }  
619 - redisCatchStorage.deleteSendRTPServer(parentPlatform.getServerGBId(), sendRtpItem.getChannelId(),  
620 - sendRtpItem.getCallId(), sendRtpItem.getStreamId());  
621 - }  
622 - }  
623 - });  
624 -  
625 -  
626 - return ret;  
627 - }  
628 -  
629 - /**  
630 - * rtpServer收流超时  
631 - */  
632 - @ResponseBody  
633 - @PostMapping(value = "/on_rtp_server_timeout", produces = "application/json;charset=UTF-8")  
634 - public JSONObject onRtpServerTimeout(HttpServletRequest request, @RequestBody OnRtpServerTimeoutHookParam param){  
635 - logger.info("[ZLM HOOK] rtpServer收流超时:{}->{}({})", param.getMediaServerId(), param.getStream_id(), param.getSsrc());  
636 -  
637 - JSONObject ret = new JSONObject();  
638 - ret.put("code", 0);  
639 - ret.put("msg", "success");  
640 -  
641 - taskExecutor.execute(()->{  
642 - JSONObject json = (JSONObject) JSON.toJSON(param);  
643 - List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_rtp_server_timeout);  
644 - if (subscribes != null && subscribes.size() > 0) {  
645 - for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {  
646 - subscribe.response(null, json);  
647 - }  
648 - }  
649 - });  
650 -  
651 - return ret;  
652 - }  
653 -  
654 - private Map<String, String> urlParamToMap(String params) {  
655 - HashMap<String, String> map = new HashMap<>();  
656 - if (ObjectUtils.isEmpty(params)) {  
657 - return map;  
658 - }  
659 - String[] paramsArray = params.split("&");  
660 - if (paramsArray.length == 0) {  
661 - return map;  
662 - }  
663 - for (String param : paramsArray) {  
664 - String[] paramArray = param.split("=");  
665 - if (paramArray.length == 2){  
666 - map.put(paramArray[0], paramArray[1]);  
667 - }  
668 - }  
669 - return map;  
670 - } 504 + }
  505 + return ret;
  506 + }
  507 +
  508 + /**
  509 + * 流未找到事件,用户可以在此事件触发时,立即去拉流,这样可以实现按需拉流;此事件对回复不敏感。
  510 + */
  511 + @ResponseBody
  512 + @PostMapping(value = "/on_stream_not_found", produces = "application/json;charset=UTF-8")
  513 + public DeferredResult<HookResult> onStreamNotFound(@RequestBody OnStreamNotFoundHookParam param) {
  514 + logger.info("[ZLM HOOK] 流未找到:{}->{}->{}/{}", param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());
  515 +
  516 + DeferredResult<HookResult> defaultResult = new DeferredResult<>();
  517 +
  518 + MediaServerItem mediaInfo = mediaServerService.getOne(param.getMediaServerId());
  519 + if (!userSetting.isAutoApplyPlay() || mediaInfo == null) {
  520 + defaultResult.setResult(new HookResult(ErrorCode.ERROR404.getCode(), ErrorCode.ERROR404.getMsg()));
  521 + return defaultResult;
  522 + }
  523 +
  524 + if ("rtp".equals(param.getApp())) {
  525 + String[] s = param.getStream().split("_");
  526 + if (!mediaInfo.isRtpEnable() || s.length != 2) {
  527 + defaultResult.setResult(HookResult.SUCCESS());
  528 + return defaultResult;
  529 + }
  530 + String deviceId = s[0];
  531 + String channelId = s[1];
  532 + Device device = redisCatchStorage.getDevice(deviceId);
  533 + if (device == null) {
  534 + defaultResult.setResult(new HookResult(ErrorCode.ERROR404.getCode(), ErrorCode.ERROR404.getMsg()));
  535 + return defaultResult;
  536 + }
  537 + DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);
  538 + if (deviceChannel == null) {
  539 + defaultResult.setResult(new HookResult(ErrorCode.ERROR404.getCode(), ErrorCode.ERROR404.getMsg()));
  540 + return defaultResult;
  541 + }
  542 + logger.info("[ZLM HOOK] 流未找到, 发起自动点播:{}->{}->{}/{}", param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());
  543 + RequestMessage msg = new RequestMessage();
  544 + String key = DeferredResultHolder.CALLBACK_CMD_PLAY + deviceId + channelId;
  545 + boolean exist = resultHolder.exist(key, null);
  546 + msg.setKey(key);
  547 + String uuid = UUID.randomUUID().toString();
  548 + msg.setId(uuid);
  549 + DeferredResult<HookResult> result = new DeferredResult<>(userSetting.getPlayTimeout().longValue());
  550 + DeferredResultEx<HookResult> deferredResultEx = new DeferredResultEx<>(result);
  551 +
  552 + result.onTimeout(() -> {
  553 + logger.info("点播接口等待超时");
  554 + // 释放rtpserver
  555 + msg.setData(new HookResult(ErrorCode.ERROR100.getCode(), "点播超时"));
  556 + resultHolder.invokeResult(msg);
  557 + });
  558 + // TODO 在点播未成功的情况下在此调用接口点播会导致返回的流地址ip错误
  559 + deferredResultEx.setFilter(result1 -> {
  560 + WVPResult<StreamInfo> wvpResult1 = (WVPResult<StreamInfo>) result1;
  561 + HookResult resultForEnd = new HookResult();
  562 + resultForEnd.setCode(wvpResult1.getCode());
  563 + resultForEnd.setMsg(wvpResult1.getMsg());
  564 + return resultForEnd;
  565 + });
  566 +
  567 + // 录像查询以channelId作为deviceId查询
  568 + resultHolder.put(key, uuid, deferredResultEx);
  569 +
  570 + if (!exist) {
  571 + playService.play(mediaInfo, deviceId, channelId, null, eventResult -> {
  572 + msg.setData(new HookResult(eventResult.statusCode, eventResult.msg));
  573 + resultHolder.invokeResult(msg);
  574 + }, null);
  575 + }
  576 + return result;
  577 + } else {
  578 + // 拉流代理
  579 + StreamProxyItem streamProxyByAppAndStream = streamProxyService.getStreamProxyByAppAndStream(param.getApp(), param.getStream());
  580 + if (streamProxyByAppAndStream != null && streamProxyByAppAndStream.isEnable_disable_none_reader()) {
  581 + streamProxyService.start(param.getApp(), param.getStream());
  582 + }
  583 + DeferredResult<HookResult> result = new DeferredResult<>();
  584 + result.setResult(HookResult.SUCCESS());
  585 + return result;
  586 + }
  587 + }
  588 +
  589 + /**
  590 + * 服务器启动事件,可以用于监听服务器崩溃重启;此事件对回复不敏感。
  591 + */
  592 + @ResponseBody
  593 + @PostMapping(value = "/on_server_started", produces = "application/json;charset=UTF-8")
  594 + public HookResult onServerStarted(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
  595 +
  596 + jsonObject.put("ip", request.getRemoteAddr());
  597 + ZLMServerConfig zlmServerConfig = JSON.to(ZLMServerConfig.class, jsonObject);
  598 + zlmServerConfig.setIp(request.getRemoteAddr());
  599 + logger.info("[ZLM HOOK] zlm 启动 " + zlmServerConfig.getGeneralMediaServerId());
  600 + taskExecutor.execute(() -> {
  601 + List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_server_started);
  602 + if (subscribes != null && subscribes.size() > 0) {
  603 + for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {
  604 + subscribe.response(null, jsonObject);
  605 + }
  606 + }
  607 + mediaServerService.zlmServerOnline(zlmServerConfig);
  608 + });
  609 +
  610 + return HookResult.SUCCESS();
  611 + }
  612 +
  613 + /**
  614 + * 发送rtp(startSendRtp)被动关闭时回调
  615 + */
  616 + @ResponseBody
  617 + @PostMapping(value = "/on_send_rtp_stopped", produces = "application/json;charset=UTF-8")
  618 + public HookResult onSendRtpStopped(HttpServletRequest request, @RequestBody OnSendRtpStoppedHookParam param) {
  619 +
  620 + logger.info("[ZLM HOOK] rtp发送关闭:{}->{}/{}", param.getMediaServerId(), param.getApp(), param.getStream());
  621 +
  622 + // 查找对应的上级推流,发送停止
  623 + if (!"rtp".equals(param.getApp())) {
  624 + return HookResult.SUCCESS();
  625 + }
  626 + taskExecutor.execute(() -> {
  627 + List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServerByStream(param.getStream());
  628 + if (sendRtpItems.size() > 0) {
  629 + for (SendRtpItem sendRtpItem : sendRtpItems) {
  630 + ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(sendRtpItem.getPlatformId());
  631 + try {
  632 + commanderFroPlatform.streamByeCmd(parentPlatform, sendRtpItem.getCallId());
  633 + } catch (SipException | InvalidArgumentException | ParseException e) {
  634 + logger.error("[命令发送失败] 国标级联 发送BYE: {}", e.getMessage());
  635 + }
  636 + redisCatchStorage.deleteSendRTPServer(parentPlatform.getServerGBId(), sendRtpItem.getChannelId(),
  637 + sendRtpItem.getCallId(), sendRtpItem.getStreamId());
  638 + }
  639 + }
  640 + });
  641 +
  642 + return HookResult.SUCCESS();
  643 + }
  644 +
  645 + /**
  646 + * rtpServer收流超时
  647 + */
  648 + @ResponseBody
  649 + @PostMapping(value = "/on_rtp_server_timeout", produces = "application/json;charset=UTF-8")
  650 + public HookResult onRtpServerTimeout(HttpServletRequest request, @RequestBody OnRtpServerTimeoutHookParam param) {
  651 + logger.info("[ZLM HOOK] rtpServer收流超时:{}->{}({})", param.getMediaServerId(), param.getStream_id(), param.getSsrc());
  652 +
  653 + taskExecutor.execute(() -> {
  654 + JSONObject json = (JSONObject) JSON.toJSON(param);
  655 + List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_rtp_server_timeout);
  656 + if (subscribes != null && subscribes.size() > 0) {
  657 + for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {
  658 + subscribe.response(null, json);
  659 + }
  660 + }
  661 + });
  662 +
  663 + return HookResult.SUCCESS();
  664 + }
  665 +
  666 + private Map<String, String> urlParamToMap(String params) {
  667 + HashMap<String, String> map = new HashMap<>();
  668 + if (ObjectUtils.isEmpty(params)) {
  669 + return map;
  670 + }
  671 + String[] paramsArray = params.split("&");
  672 + if (paramsArray.length == 0) {
  673 + return map;
  674 + }
  675 + for (String param : paramsArray) {
  676 + String[] paramArray = param.split("=");
  677 + if (paramArray.length == 2) {
  678 + map.put(paramArray[0], paramArray[1]);
  679 + }
  680 + }
  681 + return map;
  682 + }
671 } 683 }
src/main/java/com/genersoft/iot/vmp/media/zlm/dto/hook/HookResult.java 0 → 100644
  1 +package com.genersoft.iot.vmp.media.zlm.dto.hook;
  2 +
  3 +public class HookResult {
  4 +
  5 + private int code;
  6 + private String msg;
  7 +
  8 +
  9 + public HookResult() {
  10 + }
  11 +
  12 + public HookResult(int code, String msg) {
  13 + this.code = code;
  14 + this.msg = msg;
  15 + }
  16 +
  17 + public static HookResult SUCCESS(){
  18 + return new HookResult(0, "success");
  19 + }
  20 +
  21 + public int getCode() {
  22 + return code;
  23 + }
  24 +
  25 + public void setCode(int code) {
  26 + this.code = code;
  27 + }
  28 +
  29 + public String getMsg() {
  30 + return msg;
  31 + }
  32 +
  33 + public void setMsg(String msg) {
  34 + this.msg = msg;
  35 + }
  36 +}
src/main/java/com/genersoft/iot/vmp/media/zlm/dto/hook/HookResultForOnPublish.java 0 → 100644
  1 +package com.genersoft.iot.vmp.media.zlm.dto.hook;
  2 +
  3 +public class HookResultForOnPublish extends HookResult{
  4 +
  5 + private boolean enable_audio;
  6 + private boolean enable_mp4;
  7 + private int mp4_max_second;
  8 +
  9 + public HookResultForOnPublish() {
  10 + }
  11 +
  12 + public static HookResultForOnPublish SUCCESS(){
  13 + return new HookResultForOnPublish(0, "success");
  14 + }
  15 +
  16 + public HookResultForOnPublish(int code, String msg) {
  17 + setCode(code);
  18 + setMsg(msg);
  19 + }
  20 +
  21 + public boolean isEnable_audio() {
  22 + return enable_audio;
  23 + }
  24 +
  25 + public void setEnable_audio(boolean enable_audio) {
  26 + this.enable_audio = enable_audio;
  27 + }
  28 +
  29 + public boolean isEnable_mp4() {
  30 + return enable_mp4;
  31 + }
  32 +
  33 + public void setEnable_mp4(boolean enable_mp4) {
  34 + this.enable_mp4 = enable_mp4;
  35 + }
  36 +
  37 + public int getMp4_max_second() {
  38 + return mp4_max_second;
  39 + }
  40 +
  41 + public void setMp4_max_second(int mp4_max_second) {
  42 + this.mp4_max_second = mp4_max_second;
  43 + }
  44 +}
src/main/java/com/genersoft/iot/vmp/service/impl/GbStreamServiceImpl.java
@@ -152,6 +152,10 @@ public class GbStreamServiceImpl implements IGbStreamService { @@ -152,6 +152,10 @@ public class GbStreamServiceImpl implements IGbStreamService {
152 152
153 @Override 153 @Override
154 public void sendCatalogMsg(GbStream gbStream, String type) { 154 public void sendCatalogMsg(GbStream gbStream, String type) {
  155 + if (gbStream == null || type == null) {
  156 + logger.warn("[发送目录订阅]类型:流信息或类型为NULL");
  157 + return;
  158 + }
155 List<GbStream> gbStreams = new ArrayList<>(); 159 List<GbStream> gbStreams = new ArrayList<>();
156 if (gbStream.getGbId() != null) { 160 if (gbStream.getGbId() != null) {
157 gbStreams.add(gbStream); 161 gbStreams.add(gbStream);
src/main/java/com/genersoft/iot/vmp/service/impl/StreamPushServiceImpl.java
@@ -184,7 +184,9 @@ public class StreamPushServiceImpl implements IStreamPushService { @@ -184,7 +184,9 @@ public class StreamPushServiceImpl implements IStreamPushService {
184 @Override 184 @Override
185 public boolean stop(String app, String streamId) { 185 public boolean stop(String app, String streamId) {
186 StreamPushItem streamPushItem = streamPushMapper.selectOne(app, streamId); 186 StreamPushItem streamPushItem = streamPushMapper.selectOne(app, streamId);
187 - gbStreamService.sendCatalogMsg(streamPushItem, CatalogEvent.DEL); 187 + if (streamPushItem != null) {
  188 + gbStreamService.sendCatalogMsg(streamPushItem, CatalogEvent.DEL);
  189 + }
188 190
189 platformGbStreamMapper.delByAppAndStream(app, streamId); 191 platformGbStreamMapper.delByAppAndStream(app, streamId);
190 gbStreamMapper.del(app, streamId); 192 gbStreamMapper.del(app, streamId);
src/main/java/com/genersoft/iot/vmp/storager/IVideoManagerStorage.java
@@ -2,7 +2,6 @@ package com.genersoft.iot.vmp.storager; @@ -2,7 +2,6 @@ package com.genersoft.iot.vmp.storager;
2 2
3 import com.genersoft.iot.vmp.gb28181.bean.*; 3 import com.genersoft.iot.vmp.gb28181.bean.*;
4 import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem; 4 import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
5 -import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem;  
6 import com.genersoft.iot.vmp.service.bean.GPSMsgInfo; 5 import com.genersoft.iot.vmp.service.bean.GPSMsgInfo;
7 import com.genersoft.iot.vmp.storager.dao.dto.ChannelSourceInfo; 6 import com.genersoft.iot.vmp.storager.dao.dto.ChannelSourceInfo;
8 import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce; 7 import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce;
@@ -324,6 +323,8 @@ public interface IVideoManagerStorage { @@ -324,6 +323,8 @@ public interface IVideoManagerStorage {
324 */ 323 */
325 boolean resetChannels(String deviceId, List<DeviceChannel> deviceChannelList); 324 boolean resetChannels(String deviceId, List<DeviceChannel> deviceChannelList);
326 325
  326 + boolean updateChannels(String deviceId, List<DeviceChannel> deviceChannelList);
  327 +
327 /** 328 /**
328 * 获取目录信息 329 * 获取目录信息
329 * @param platformId 330 * @param platformId
src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStorageImpl.java
@@ -194,6 +194,119 @@ public class VideoManagerStorageImpl implements IVideoManagerStorage { @@ -194,6 +194,119 @@ public class VideoManagerStorageImpl implements IVideoManagerStorage {
194 194
195 } 195 }
196 196
  197 +
  198 + @Override
  199 + public boolean updateChannels(String deviceId, List<DeviceChannel> deviceChannelList) {
  200 + if (CollectionUtils.isEmpty(deviceChannelList)) {
  201 + return false;
  202 + }
  203 + List<DeviceChannel> allChannels = deviceChannelMapper.queryAllChannels(deviceId);
  204 + Map<String,DeviceChannel> allChannelMap = new ConcurrentHashMap<>();
  205 + if (allChannels.size() > 0) {
  206 + for (DeviceChannel deviceChannel : allChannels) {
  207 + allChannelMap.put(deviceChannel.getChannelId(), deviceChannel);
  208 + }
  209 + }
  210 + List<DeviceChannel> addChannels = new ArrayList<>();
  211 + List<DeviceChannel> updateChannels = new ArrayList<>();
  212 +
  213 +
  214 + TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  215 + // 数据去重
  216 + StringBuilder stringBuilder = new StringBuilder();
  217 + Map<String, Integer> subContMap = new HashMap<>();
  218 + if (deviceChannelList.size() > 0) {
  219 + // 数据去重
  220 + Set<String> gbIdSet = new HashSet<>();
  221 + for (DeviceChannel deviceChannel : deviceChannelList) {
  222 + if (!gbIdSet.contains(deviceChannel.getChannelId())) {
  223 + gbIdSet.add(deviceChannel.getChannelId());
  224 + if (allChannelMap.containsKey(deviceChannel.getChannelId())) {
  225 + deviceChannel.setStreamId(allChannelMap.get(deviceChannel.getChannelId()).getStreamId());
  226 + deviceChannel.setHasAudio(allChannelMap.get(deviceChannel.getChannelId()).isHasAudio());
  227 + updateChannels.add(deviceChannel);
  228 + }else {
  229 + addChannels.add(deviceChannel);
  230 + }
  231 + if (!ObjectUtils.isEmpty(deviceChannel.getParentId())) {
  232 + if (subContMap.get(deviceChannel.getParentId()) == null) {
  233 + subContMap.put(deviceChannel.getParentId(), 1);
  234 + }else {
  235 + Integer count = subContMap.get(deviceChannel.getParentId());
  236 + subContMap.put(deviceChannel.getParentId(), count++);
  237 + }
  238 + }
  239 + }else {
  240 + stringBuilder.append(deviceChannel.getChannelId()).append(",");
  241 + }
  242 + }
  243 + if (addChannels.size() > 0) {
  244 + for (DeviceChannel channel : addChannels) {
  245 + if (subContMap.get(channel.getChannelId()) != null){
  246 + channel.setSubCount(subContMap.get(channel.getChannelId()));
  247 + }
  248 + }
  249 + }
  250 + if (updateChannels.size() > 0) {
  251 + for (DeviceChannel channel : updateChannels) {
  252 + if (subContMap.get(channel.getChannelId()) != null){
  253 + channel.setSubCount(subContMap.get(channel.getChannelId()));
  254 + }
  255 + }
  256 + }
  257 +
  258 + }
  259 + if (stringBuilder.length() > 0) {
  260 + logger.info("[目录查询]收到的数据存在重复: {}" , stringBuilder);
  261 + }
  262 + if(CollectionUtils.isEmpty(updateChannels) && CollectionUtils.isEmpty(addChannels) ){
  263 + logger.info("通道更新,数据为空={}" , deviceChannelList);
  264 + return false;
  265 + }
  266 + try {
  267 + int limitCount = 300;
  268 + boolean result = false;
  269 + if (addChannels.size() > 0) {
  270 + if (addChannels.size() > limitCount) {
  271 + for (int i = 0; i < addChannels.size(); i += limitCount) {
  272 + int toIndex = i + limitCount;
  273 + if (i + limitCount > addChannels.size()) {
  274 + toIndex = addChannels.size();
  275 + }
  276 + result = result || deviceChannelMapper.batchAdd(addChannels.subList(i, toIndex)) < 0;
  277 + }
  278 + }else {
  279 + result = result || deviceChannelMapper.batchAdd(addChannels) < 0;
  280 + }
  281 + }
  282 + if (updateChannels.size() > 0) {
  283 + if (updateChannels.size() > limitCount) {
  284 + for (int i = 0; i < updateChannels.size(); i += limitCount) {
  285 + int toIndex = i + limitCount;
  286 + if (i + limitCount > updateChannels.size()) {
  287 + toIndex = updateChannels.size();
  288 + }
  289 + result = result || deviceChannelMapper.batchUpdate(updateChannels.subList(i, toIndex)) < 0;
  290 + }
  291 + }else {
  292 + result = result || deviceChannelMapper.batchUpdate(updateChannels) < 0;
  293 + }
  294 + }
  295 + if (result) {
  296 + //事务回滚
  297 + dataSourceTransactionManager.rollback(transactionStatus);
  298 + }else {
  299 + //手动提交
  300 + dataSourceTransactionManager.commit(transactionStatus);
  301 + }
  302 + return true;
  303 + }catch (Exception e) {
  304 + e.printStackTrace();
  305 + dataSourceTransactionManager.rollback(transactionStatus);
  306 + return false;
  307 + }
  308 + }
  309 +
197 @Override 310 @Override
198 public void deviceChannelOnline(String deviceId, String channelId) { 311 public void deviceChannelOnline(String deviceId, String channelId) {
199 deviceChannelMapper.online(deviceId, channelId); 312 deviceChannelMapper.online(deviceId, channelId);
src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java
@@ -11,17 +11,13 @@ import com.genersoft.iot.vmp.utils.DateUtil; @@ -11,17 +11,13 @@ import com.genersoft.iot.vmp.utils.DateUtil;
11 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; 11 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
12 import com.genersoft.iot.vmp.vmanager.bean.WVPResult; 12 import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
13 import com.github.pagehelper.PageInfo; 13 import com.github.pagehelper.PageInfo;
14 -  
15 import io.swagger.v3.oas.annotations.Operation; 14 import io.swagger.v3.oas.annotations.Operation;
16 import io.swagger.v3.oas.annotations.Parameter; 15 import io.swagger.v3.oas.annotations.Parameter;
17 import io.swagger.v3.oas.annotations.tags.Tag; 16 import io.swagger.v3.oas.annotations.tags.Tag;
18 import org.springframework.beans.factory.annotation.Autowired; 17 import org.springframework.beans.factory.annotation.Autowired;
19 -import org.springframework.http.HttpStatus;  
20 -import org.springframework.http.ResponseEntity;  
21 import org.springframework.security.authentication.AuthenticationManager; 18 import org.springframework.security.authentication.AuthenticationManager;
22 import org.springframework.util.DigestUtils; 19 import org.springframework.util.DigestUtils;
23 import org.springframework.util.ObjectUtils; 20 import org.springframework.util.ObjectUtils;
24 -import org.springframework.util.StringUtils;  
25 import org.springframework.web.bind.annotation.*; 21 import org.springframework.web.bind.annotation.*;
26 22
27 import javax.security.sasl.AuthenticationException; 23 import javax.security.sasl.AuthenticationException;
@@ -90,7 +86,7 @@ public class UserController { @@ -90,7 +86,7 @@ public class UserController {
90 86
91 87
92 @PostMapping("/add") 88 @PostMapping("/add")
93 - @Operation(summary = "停止视频回放") 89 + @Operation(summary = "添加用户")
94 @Parameter(name = "username", description = "用户名", required = true) 90 @Parameter(name = "username", description = "用户名", required = true)
95 @Parameter(name = "password", description = "密码(未md5加密的密码)", required = true) 91 @Parameter(name = "password", description = "密码(未md5加密的密码)", required = true)
96 @Parameter(name = "roleId", description = "角色ID", required = true) 92 @Parameter(name = "roleId", description = "角色ID", required = true)