From dea44dcd78418ed3e7f191a73cee2b81a7a0019f Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期五, 13 一月 2023 16:21:17 +0800
Subject: [PATCH] Merge branch 'wvp-28181-2.0'

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java |  258 +++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 198 insertions(+), 60 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
index d925e4b..ba95972 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
@@ -1,41 +1,40 @@
 package com.genersoft.iot.vmp.media.zlm;
 
-import java.text.ParseException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
 import com.genersoft.iot.vmp.common.StreamInfo;
 import com.genersoft.iot.vmp.conf.UserSetting;
 import com.genersoft.iot.vmp.conf.exception.SsrcTransactionNotFoundException;
 import com.genersoft.iot.vmp.gb28181.bean.*;
 import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
+import com.genersoft.iot.vmp.gb28181.session.AudioBroadcastManager;
 import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
-import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
-import com.genersoft.iot.vmp.media.zlm.dto.*;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
+import com.genersoft.iot.vmp.media.zlm.dto.HookType;
+import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import com.genersoft.iot.vmp.media.zlm.dto.StreamAuthorityInfo;
+import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
 import com.genersoft.iot.vmp.media.zlm.dto.hook.*;
 import com.genersoft.iot.vmp.service.*;
 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
 import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
+import com.genersoft.iot.vmp.vmanager.bean.StreamContent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.util.ObjectUtils;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.bind.annotation.RestController;
-
-import com.alibaba.fastjson2.JSONObject;
-import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
+import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.sip.InvalidArgumentException;
 import javax.sip.SipException;
+import java.text.ParseException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**    
  * @description:閽堝 ZLMediaServer鐨刪ook浜嬩欢鐩戝惉
@@ -52,7 +51,13 @@
 	private SIPCommander cmder;
 
 	@Autowired
-	private SIPCommanderFroPlatform commanderFroPlatform;
+	private ISIPCommanderForPlatform commanderFroPlatform;
+
+	@Autowired
+	private AudioBroadcastManager audioBroadcastManager;
+
+	@Autowired
+	private ZLMRTPServerFactory zlmrtpServerFactory;
 
 	@Autowired
 	private IPlayService playService;
@@ -186,7 +191,7 @@
 
 		if (!"rtp".equals(param.getApp())) {
 			if (userSetting.getPushAuthority()) {
-// 鎺ㄦ祦閴存潈
+				// 鎺ㄦ祦閴存潈
 				if (param.getParams() == null) {
 					logger.info("鎺ㄦ祦閴存潈澶辫触锛� 缂哄皯涓嶈鍙傛暟锛歴ign=md5(user琛ㄧ殑pushKey)");
 					ret.put("code", 401);
@@ -285,9 +290,9 @@
 
 
 		JSONObject json = (JSONObject) JSON.toJSON(param);
-		taskExecutor.execute(()->{
+		taskExecutor.execute(()-> {
 			ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_stream_changed, json);
-			if (subscribe != null ) {
+			if (subscribe != null) {
 				MediaServerItem mediaInfo = mediaServerService.getOne(param.getMediaServerId());
 				if (mediaInfo != null) {
 					subscribe.response(mediaInfo, json);
@@ -303,43 +308,134 @@
 					StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(param.getApp(), param.getStream());
 					if (streamAuthorityInfo == null) {
 						streamAuthorityInfo = StreamAuthorityInfo.getInstanceByHook(param);
-					}else {
+					} else {
 						streamAuthorityInfo.setOriginType(param.getOriginType());
 						streamAuthorityInfo.setOriginTypeStr(param.getOriginTypeStr());
 					}
 					redisCatchStorage.updateStreamAuthorityInfo(param.getApp(), param.getStream(), streamAuthorityInfo);
 				}
-			}else {
+			} else {
 				redisCatchStorage.removeStreamAuthorityInfo(param.getApp(), param.getStream());
 			}
+		});
 
-			if ("rtsp".equals(param.getSchema())){
-				if (param.isRegist()) {
-					mediaServerService.addCount(param.getMediaServerId());
-				}else {
-					mediaServerService.removeCount(param.getMediaServerId());
+		if ("rtsp".equals(param.getSchema())){
+			logger.info("娴佸彉鍖栵細娉ㄥ唽->{}, app->{}, stream->{}", param.isRegist(), param.getApp(), param.getStream());
+			if (param.isRegist()) {
+				mediaServerService.addCount(param.getMediaServerId());
+			}else {
+				mediaServerService.removeCount(param.getMediaServerId());
+			}
+			if (param.getOriginType() == OriginType.PULL.ordinal()
+					|| param.getOriginType() == OriginType.FFMPEG_PULL.ordinal()) {
+				// 璁剧疆鎷夋祦浠g悊涓婄嚎/绂荤嚎
+				streamProxyService.updateStatus(param.isRegist(), param.getApp(), param.getStream());
+			}
+			if ("rtp".equals(param.getApp()) && !param.isRegist() ) {
+				StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(param.getStream());
+				if (streamInfo!=null){
+					redisCatchStorage.stopPlay(streamInfo);
+					storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
+				}else{
+					streamInfo = redisCatchStorage.queryPlayback(null, null, param.getStream(), null);
+					if (streamInfo != null) {
+						redisCatchStorage.stopPlayback(streamInfo.getDeviceID(), streamInfo.getChannelId(),
+								streamInfo.getStream(), null);
+					}
 				}
-				if (param.getOriginType() == OriginType.PULL.ordinal()
-						|| param.getOriginType() == OriginType.FFMPEG_PULL.ordinal()) {
-					// 璁剧疆鎷夋祦浠g悊涓婄嚎/绂荤嚎
-					streamProxyService.updateStatus(param.isRegist(), param.getApp(), param.getStream());
-				}
-				if ("rtp".equals(param.getApp()) && !param.isRegist() ) {
-					StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(param.getStream());
-					if (streamInfo!=null){
-						redisCatchStorage.stopPlay(streamInfo);
-						storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
-					}else{
-						streamInfo = redisCatchStorage.queryPlayback(null, null, param.getStream(), null);
-						if (streamInfo != null) {
-							redisCatchStorage.stopPlayback(streamInfo.getDeviceID(), streamInfo.getChannelId(),
-									streamInfo.getStream(), null);
+			}else if ("broadcast".equals(param.getApp())){
+				// 璇煶瀵硅鎺ㄦ祦  stream闇�瑕佹弧瓒虫牸寮廳eviceId_channelId
+				if (param.getStream().indexOf("_") > 0) {
+					String[] streamArray = param.getStream().split("_");
+					if (streamArray.length == 2) {
+						String deviceId = streamArray[0];
+						String channelId = streamArray[1];
+						Device device = deviceService.getDevice(deviceId);
+						if (device != null) {
+							if (param.isRegist()) {
+								if (audioBroadcastManager.exit(deviceId, channelId)) {
+									playService.stopAudioBroadcast(deviceId, channelId);
+								}
+								// 寮�鍚闊冲璁查�氶亾
+								try {
+									playService.audioBroadcastCmd(device, channelId, 60, (msg)->{
+										logger.info("[璇煶瀵硅] 閫氶亾寤虹珛鎴愬姛, device: {}, channel: {}", deviceId, channelId);
+									});
+								} catch (InvalidArgumentException | ParseException | SipException e) {
+									logger.error("[鍛戒护鍙戦�佸け璐 璇煶瀵硅: {}", e.getMessage());
+								}
+							}else {
+								// 娴佹敞閿�
+								playService.stopAudioBroadcast(deviceId, channelId);
+							}
+						} else{
+							logger.info("[璇煶瀵硅] 鏈壘鍒拌澶囷細{}", deviceId);
 						}
 					}
-				}else {
-					if (!"rtp".equals(param.getApp())){
-						String type = OriginType.values()[param.getOriginType()].getType();
-						MediaServerItem mediaServerItem = mediaServerService.getOne(param.getMediaServerId());
+				}
+			}else if ("talk".equals(param.getApp())){
+				// 璇煶瀵硅鎺ㄦ祦  stream闇�瑕佹弧瓒虫牸寮廳eviceId_channelId
+				if (param.isRegist() && param.getStream().indexOf("_") > 0) {
+					String[] streamArray = param.getStream().split("_");
+					if (streamArray.length == 2) {
+						String deviceId = streamArray[0];
+						String channelId = streamArray[1];
+						Device device = deviceService.getDevice(deviceId);
+						if (device != null) {
+							DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);
+							if (deviceChannel != null) {
+								if (audioBroadcastManager.exit(deviceId, channelId)) {
+									// 鐩存帴鎺ㄦ祦
+									SendRtpItem sendRtpItem =  redisCatchStorage.querySendRTPServer(null, null, param.getStream(), null);
+									if (sendRtpItem == null) {
+										// TODO 鍙兘鏁版嵁閿欒锛岄噸鏂板紑鍚闊抽�氶亾
+									}else {
+										MediaServerItem mediaInfo = mediaServerService.getOne(sendRtpItem.getMediaServerId());
+										logger.info("rtp/{}寮�濮嬪悜涓婄骇鎺ㄦ祦, 鐩爣={}:{}锛孲SRC={}", sendRtpItem.getStreamId(), sendRtpItem.getIp(), sendRtpItem.getPort(), sendRtpItem.getSsrc());
+										Map<String, Object> sendParam = new HashMap<>(12);
+										sendParam.put("vhost","__defaultVhost__");
+										sendParam.put("app",sendRtpItem.getApp());
+										sendParam.put("stream",sendRtpItem.getStreamId());
+										sendParam.put("ssrc", sendRtpItem.getSsrc());
+										sendParam.put("src_port", sendRtpItem.getLocalPort());
+										sendParam.put("pt", sendRtpItem.getPt());
+										sendParam.put("use_ps", sendRtpItem.isUsePs() ? "1" : "0");
+										sendParam.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0");
+
+										JSONObject jsonObject;
+										if (sendRtpItem.isTcpActive()) {
+											jsonObject = zlmrtpServerFactory.startSendRtpPassive(mediaInfo, sendParam);
+										} else {
+											sendParam.put("is_udp", sendRtpItem.isTcp() ? "0" : "1");
+											sendParam.put("dst_url", sendRtpItem.getIp());
+											sendParam.put("dst_port", sendRtpItem.getPort());
+											jsonObject = zlmrtpServerFactory.startSendRtpStream(mediaInfo, sendParam);
+										}
+										if (jsonObject != null && jsonObject.getInteger("code") == 0) {
+											logger.info("[璇煶瀵硅] 鑷姩鎺ㄦ祦鎴愬姛, device: {}, channel: {}", deviceId, channelId);
+										}
+									}
+								}else {
+									// 寮�鍚闊冲璁查�氶亾
+									MediaServerItem mediaServerItem = mediaServerService.getOne(param.getMediaServerId());
+									playService.talk(mediaServerItem, device, channelId, (mediaServer, jsonObject)->{
+										System.out.println("寮�濮嬫帹娴�");
+									}, eventResult -> {
+										System.out.println(eventResult.msg);
+									}, ()->{
+										System.out.println("瓒呮椂");
+									});
+								}
+
+							}
+						}
+					}
+				}
+
+			}else{
+				if (!"rtp".equals(param.getApp())){
+					String type = OriginType.values()[param.getOriginType()].getType();
+					MediaServerItem mediaServerItem = mediaServerService.getOne(param.getMediaServerId());
 
 						if (mediaServerItem != null){
 							if (param.isRegist()) {
@@ -349,8 +445,8 @@
 									callId = streamAuthorityInfo.getCallId();
 								}
 								StreamInfo streamInfoByAppAndStream = mediaService.getStreamInfoByAppAndStream(mediaServerItem,
-										param.getApp(), param.getStream(), tracks, callId);
-								param.setStreamInfo(streamInfoByAppAndStream);
+										param.getApp(), param.getStream(), param.getTracks(), callId);
+								param.setStreamInfo(new StreamContent(streamInfoByAppAndStream));
 								redisCatchStorage.addStream(mediaServerItem, type, param.getApp(), param.getStream(), param);
 								if (param.getOriginType() == OriginType.RTSP_PUSH.ordinal()
 										|| param.getOriginType() == OriginType.RTMP_PUSH.ordinal()
@@ -360,14 +456,15 @@
 								}
 							}else {
 								// 鍏煎娴佹敞閿�鏃剁被鍨嬩粠redis璁板綍鑾峰彇
-								OnStreamChangedHookParam onStreamChangedHookParam = redisCatchStorage.getStreamInfo(param.getApp(), param.getStream(), param.getMediaServerId());
+								OnStreamChangedHookParam onStreamChangedHookParam = redisCatchStorage.getStreamInfo(
+										param.getApp(), param.getStream(), param.getMediaServerId());
 								if (onStreamChangedHookParam != null) {
 									type = OriginType.values()[onStreamChangedHookParam.getOriginType()].getType();
 									redisCatchStorage.removeStream(mediaServerItem.getId(), type, param.getApp(), param.getStream());
 								}
 								GbStream gbStream = storager.getGbStream(param.getApp(), param.getStream());
 								if (gbStream != null) {
-//								eventPublisher.catalogEventPublishForStream(null, gbStream, CatalogEvent.OFF);
+//									eventPublisher.catalogEventPublishForStream(null, gbStream, CatalogEvent.OFF);
 								}
 								zlmMediaListManager.removeMedia(param.getApp(), param.getStream());
 							}
@@ -393,21 +490,33 @@
 								ParentPlatform platform = storager.queryParentPlatByServerGBId(platformId);
 								Device device = deviceService.getDevice(platformId);
 
-								try {
-									if (platform != null) {
-										commanderFroPlatform.streamByeCmd(platform, sendRtpItem);
-									}else {
-										cmder.streamByeCmd(device, sendRtpItem.getChannelId(), param.getStream(), sendRtpItem.getCallId());
+							try {
+								if (platform != null) {
+									commanderFroPlatform.streamByeCmd(platform, sendRtpItem);
+								}else {
+									if (sendRtpItem.isOnlyAudio()) {
+										AudioBroadcastCatch audioBroadcastCatch = audioBroadcastManager.get(sendRtpItem.getDeviceId(), sendRtpItem.getChannelId());
+										if (audioBroadcastCatch != null) {
+//											playService.stopAudioBroadcast(device.getDeviceId(), sendRtpItem.getChannelId());
+											if ("talk".equals(param.getApp())) {
+//												cmder.streamByeCmd(device, sendRtpItem.getChannelId(), audioBroadcastCatch.getSipTransactionInfo(), null);
+											}else {
+//												cmder.streamByeCmd(device, sendRtpItem.getChannelId(), audioBroadcastCatch.getSipTransactionInfo(), null);
+											}
+										}
 									}
-								} catch (SipException | InvalidArgumentException | ParseException | SsrcTransactionNotFoundException e) {
-									logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 鍙戦�丅YE: {}", e.getMessage());
+
+
+
 								}
+							} catch (SipException | InvalidArgumentException | ParseException e) {
+								logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 鍙戦�丅YE: {}", e.getMessage());
 							}
 						}
 					}
 				}
 			}
-		});
+		}
 
 		JSONObject ret = new JSONObject();
 		ret.put("code", 0);
@@ -426,7 +535,7 @@
 		logger.info("[ZLM HOOK]娴佹棤浜鸿鐪嬶細{]->{}->{}/{}" + param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());
 		JSONObject ret = new JSONObject();
 		ret.put("code", 0);
-		// 褰曞儚涓嬭浇
+		// 鍥芥爣绫诲瀷鐨勬祦
 		if ("rtp".equals(param.getApp())){
 			ret.put("close", userSetting.getStreamOnDemand());
 			// 鍥芥爣娴侊紝 鐐规挱/褰曞儚鍥炴斁/褰曞儚涓嬭浇
@@ -439,6 +548,9 @@
 					if (sendRtpItems.size() > 0) {
 						for (SendRtpItem sendRtpItem : sendRtpItems) {
 							ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(sendRtpItem.getPlatformId());
+							if (parentPlatform == null) {
+								continue;
+							}
 							try {
 								commanderFroPlatform.streamByeCmd(parentPlatform, sendRtpItem.getCallId());
 							} catch (SipException | InvalidArgumentException | ParseException e) {
@@ -508,6 +620,7 @@
 					// 淇敼鏁版嵁
 					streamProxyService.stop(param.getApp(), param.getStream());
 				}else {
+					// 鏃犱汉瑙傜湅涓嶅仛澶勭悊
 					ret.put("close", false);
 				}
 				return ret;
@@ -529,7 +642,7 @@
 	@ResponseBody
 	@PostMapping(value = "/on_stream_not_found", produces = "application/json;charset=UTF-8")
 	public JSONObject onStreamNotFound(@RequestBody OnStreamNotFoundHookParam param){
-		logger.info("[ZLM HOOK] 娴佹湭鎵惧埌锛歿}->{}->{}/{}" + param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());
+		logger.info("[ZLM HOOK] 娴佹湭鎵惧埌锛歿}->{}->{}/{}", param.getMediaServerId(), param.getSchema(), param.getApp(), param.getStream());
 		taskExecutor.execute(()->{
 			MediaServerItem mediaInfo = mediaServerService.getOne(param.getMediaServerId());
 			if (userSetting.isAutoApplyPlay() && mediaInfo != null) {
@@ -597,7 +710,7 @@
 	@PostMapping(value = "/on_send_rtp_stopped", produces = "application/json;charset=UTF-8")
 	public JSONObject onSendRtpStopped(HttpServletRequest request, @RequestBody OnSendRtpStoppedHookParam param){
 
-		logger.info("[ZLM HOOK] 鍙戦�乺tp琚姩鍏抽棴锛歿}->{}/{}", param.getMediaServerId(), param.getApp(), param.getStream());
+		logger.info("[ZLM HOOK] rtp鍙戦�佸叧闂細{}->{}/{}", param.getMediaServerId(), param.getApp(), param.getStream());
 
 		JSONObject ret = new JSONObject();
 		ret.put("code", 0);
@@ -627,6 +740,31 @@
 		return ret;
 	}
 
+	/**
+	 * rtpServer鏀舵祦瓒呮椂
+	 */
+	@ResponseBody
+	@PostMapping(value = "/on_rtp_server_timeout", produces = "application/json;charset=UTF-8")
+	public JSONObject onRtpServerTimeout(HttpServletRequest request, @RequestBody OnRtpServerTimeoutHookParam param){
+		logger.info("[ZLM HOOK] rtpServer rtp瓒呮椂锛歿}->{}({})", param.getMediaServerId(), param.getStream_id(), param.getSsrc());
+
+		JSONObject ret = new JSONObject();
+		ret.put("code", 0);
+		ret.put("msg", "success");
+
+		taskExecutor.execute(()->{
+			JSONObject json = (JSONObject) JSON.toJSON(param);
+			List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_rtp_server_timeout);
+			if (subscribes != null  && subscribes.size() > 0) {
+				for (ZlmHttpHookSubscribe.Event subscribe : subscribes) {
+					subscribe.response(null, json);
+				}
+			}
+		});
+
+		return ret;
+	}
+
 	private Map<String, String> urlParamToMap(String params) {
 		HashMap<String, String> map = new HashMap<>();
 		if (ObjectUtils.isEmpty(params)) {

--
Gitblit v1.8.0