| | |
| | | package com.genersoft.iot.vmp.media.zlm; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.genersoft.iot.vmp.common.CommonCallback; |
| | | import com.genersoft.iot.vmp.conf.UserSetting; |
| | | import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForRtpServerTimeout; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.*; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | @Component |
| | | public class ZLMRTPServerFactory { |
| | |
| | | @Autowired |
| | | private ZlmHttpHookSubscribe hookSubscribe; |
| | | |
| | | private int[] portRangeArray = new int[2]; |
| | | @Autowired |
| | | private SendRtpPortManager sendRtpPortManager; |
| | | |
| | | public int getFreePort(MediaServerItem mediaServerItem, int startPort, int endPort, List<Integer> usedFreelist) { |
| | | if (endPort <= startPort) { |
| | | return -1; |
| | | } |
| | | if (usedFreelist == null) { |
| | | usedFreelist = new ArrayList<>(); |
| | | } |
| | | JSONObject listRtpServerJsonResult = zlmresTfulUtils.listRtpServer(mediaServerItem); |
| | | if (listRtpServerJsonResult != null) { |
| | | JSONArray data = listRtpServerJsonResult.getJSONArray("data"); |
| | | if (data != null) { |
| | | for (int i = 0; i < data.size(); i++) { |
| | | JSONObject dataItem = data.getJSONObject(i); |
| | | usedFreelist.add(dataItem.getInteger("port")); |
| | | } |
| | | } |
| | | } |
| | | |
| | | Map<String, Object> param = new HashMap<>(); |
| | | int result = -1; |
| | | // 设置推流端口 |
| | | if (startPort%2 == 1) { |
| | | startPort ++; |
| | | } |
| | | boolean checkPort = false; |
| | | for (int i = startPort; i < endPort + 1; i+=2) { |
| | | if (!usedFreelist.contains(i)){ |
| | | checkPort = true; |
| | | startPort = i; |
| | | break; |
| | | } |
| | | } |
| | | if (!checkPort) { |
| | | logger.warn("未找到节点{}上范围[{}-{}]的空闲端口", mediaServerItem.getId(), startPort, endPort); |
| | | return -1; |
| | | } |
| | | param.put("port", startPort); |
| | | String stream = UUID.randomUUID().toString(); |
| | | param.put("enable_tcp", 1); |
| | | param.put("stream_id", stream); |
| | | // param.put("port", 0); |
| | | JSONObject openRtpServerResultJson = zlmresTfulUtils.openRtpServer(mediaServerItem, param); |
| | | |
| | | if (openRtpServerResultJson != null) { |
| | | if (openRtpServerResultJson.getInteger("code") == 0) { |
| | | result= openRtpServerResultJson.getInteger("port"); |
| | | Map<String, Object> closeRtpServerParam = new HashMap<>(); |
| | | closeRtpServerParam.put("stream_id", stream); |
| | | zlmresTfulUtils.closeRtpServer(mediaServerItem, closeRtpServerParam); |
| | | }else { |
| | | usedFreelist.add(startPort); |
| | | startPort +=2; |
| | | result = getFreePort(mediaServerItem, startPort, endPort,usedFreelist); |
| | | } |
| | | }else { |
| | | // 检查ZLM状态 |
| | | logger.error("创建RTP Server 失败 {}: 请检查ZLM服务", param.get("port")); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public int createRTPServer(MediaServerItem mediaServerItem, String streamId, int ssrc, Integer port, Boolean onlyAuto) { |
| | | /** |
| | | * 开启rtpServer |
| | | * @param mediaServerItem zlm服务实例 |
| | | * @param streamId 流Id |
| | | * @param ssrc ssrc |
| | | * @param port 端口, 0/null为使用随机 |
| | | * @param reUsePort 是否重用端口 |
| | | * @param tcpMode 0/null udp 模式,1 tcp 被动模式, 2 tcp 主动模式。 |
| | | * @return |
| | | */ |
| | | public int createRTPServer(MediaServerItem mediaServerItem, String streamId, int ssrc, Integer port, Boolean onlyAuto, Boolean reUsePort, Integer tcpMode) { |
| | | int result = -1; |
| | | // 查询此rtp server 是否已经存在 |
| | | JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaServerItem, streamId); |
| | |
| | | JSONObject jsonObject = zlmresTfulUtils.closeRtpServer(mediaServerItem, param); |
| | | if (jsonObject != null ) { |
| | | if (jsonObject.getInteger("code") == 0) { |
| | | return createRTPServer(mediaServerItem, streamId, ssrc, port, onlyAuto); |
| | | return createRTPServer(mediaServerItem, streamId, ssrc, port,onlyAuto, reUsePort, tcpMode); |
| | | }else { |
| | | logger.warn("[开启rtpServer], 重启RtpServer错误"); |
| | | } |
| | |
| | | |
| | | Map<String, Object> param = new HashMap<>(); |
| | | |
| | | param.put("enable_tcp", 1); |
| | | if (tcpMode == null) { |
| | | tcpMode = 0; |
| | | } |
| | | param.put("tcp_mode", tcpMode); |
| | | param.put("stream_id", streamId); |
| | | if (reUsePort != null) { |
| | | param.put("re_use_port", reUsePort?"1":"0"); |
| | | } |
| | | // 推流端口设置0则使用随机端口 |
| | | if (port == null) { |
| | | param.put("port", 0); |
| | |
| | | Map<String, Object> param = new HashMap<>(); |
| | | param.put("stream_id", streamId); |
| | | JSONObject jsonObject = zlmresTfulUtils.closeRtpServer(serverItem, param); |
| | | logger.info("关闭RTP Server " + jsonObject); |
| | | if (jsonObject != null ) { |
| | | if (jsonObject.getInteger("code") == 0) { |
| | | result = jsonObject.getInteger("hit") == 1; |
| | | result = jsonObject.getInteger("hit") >= 1; |
| | | }else { |
| | | logger.error("关闭RTP Server 失败: " + jsonObject.getString("msg")); |
| | | } |
| | |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public void closeRtpServer(MediaServerItem serverItem, String streamId, CommonCallback<Boolean> callback) { |
| | | if (serverItem == null) { |
| | | callback.run(false); |
| | | return; |
| | | } |
| | | Map<String, Object> param = new HashMap<>(); |
| | | param.put("stream_id", streamId); |
| | | zlmresTfulUtils.closeRtpServer(serverItem, param, jsonObject -> { |
| | | if (jsonObject != null ) { |
| | | if (jsonObject.getInteger("code") == 0) { |
| | | callback.run(jsonObject.getInteger("hit") == 1); |
| | | return; |
| | | }else { |
| | | logger.error("关闭RTP Server 失败: " + jsonObject.getString("msg")); |
| | | } |
| | | }else { |
| | | // 检查ZLM状态 |
| | | logger.error("关闭RTP Server 失败: 请检查ZLM服务"); |
| | | } |
| | | callback.run(false); |
| | | }); |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | |
| | | * @param tcp 是否为tcp |
| | | * @return SendRtpItem |
| | | */ |
| | | public SendRtpItem createSendRtpItem(MediaServerItem serverItem, String ip, int port, String ssrc, String platformId, String deviceId, String channelId, boolean tcp, boolean rtcp){ |
| | | public SendRtpItem createSendRtpItem(MediaServerItem serverItem, String ip, int port, String ssrc, String platformId, |
| | | String deviceId, String channelId, boolean tcp, boolean rtcp){ |
| | | |
| | | // 默认为随机端口 |
| | | int localPort = 0; |
| | | if (userSetting.getGbSendStreamStrict()) { |
| | | if (userSetting.getGbSendStreamStrict()) { |
| | | localPort = keepPort(serverItem, ssrc); |
| | | if (localPort == 0) { |
| | | return null; |
| | | } |
| | | localPort = sendRtpPortManager.getNextPort(serverItem.getId()); |
| | | if (localPort == 0) { |
| | | return null; |
| | | } |
| | | } |
| | | SendRtpItem sendRtpItem = new SendRtpItem(); |
| | |
| | | * @param tcp 是否为tcp |
| | | * @return SendRtpItem |
| | | */ |
| | | public SendRtpItem createSendRtpItem(MediaServerItem serverItem, String ip, int port, String ssrc, String platformId, String app, String stream, String channelId, boolean tcp, boolean rtcp){ |
| | | public SendRtpItem createSendRtpItem(MediaServerItem serverItem, String ip, int port, String ssrc, String platformId, |
| | | String app, String stream, String channelId, boolean tcp, boolean rtcp){ |
| | | // 默认为随机端口 |
| | | int localPort = 0; |
| | | if (userSetting.getGbSendStreamStrict()) { |
| | | localPort = keepPort(serverItem, ssrc); |
| | | localPort = sendRtpPortManager.getNextPort(serverItem.getId()); |
| | | if (localPort == 0) { |
| | | return null; |
| | | } |
| | |
| | | sendRtpItem.setMediaServerId(serverItem.getId()); |
| | | sendRtpItem.setRtcp(rtcp); |
| | | return sendRtpItem; |
| | | } |
| | | |
| | | /** |
| | | * 保持端口,直到需要需要发流时再释放 |
| | | */ |
| | | public int keepPort(MediaServerItem serverItem, String ssrc) { |
| | | int localPort = 0; |
| | | Map<String, Object> param = new HashMap<>(3); |
| | | param.put("port", 0); |
| | | param.put("enable_tcp", 1); |
| | | param.put("stream_id", ssrc); |
| | | JSONObject jsonObject = zlmresTfulUtils.openRtpServer(serverItem, param); |
| | | if (jsonObject.getInteger("code") == 0) { |
| | | localPort = jsonObject.getInteger("port"); |
| | | HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(ssrc, null, serverItem.getId()); |
| | | // 订阅 zlm启动事件, 新的zlm也会从这里进入系统 |
| | | hookSubscribe.addSubscribe(hookSubscribeForRtpServerTimeout, |
| | | (MediaServerItem mediaServerItem, JSONObject response)->{ |
| | | logger.info("[保持端口] {}->监听端口到期继续保持监听", ssrc); |
| | | keepPort(serverItem, ssrc); |
| | | }); |
| | | } |
| | | logger.info("[保持端口] {}->监听端口: {}", ssrc, localPort); |
| | | return localPort; |
| | | } |
| | | |
| | | /** |
| | | * 释放保持的端口 |
| | | */ |
| | | public boolean releasePort(MediaServerItem serverItem, String ssrc) { |
| | | logger.info("[保持端口] {}->释放监听端口", ssrc); |
| | | boolean closeRTPServerResult = closeRtpServer(serverItem, ssrc); |
| | | HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(ssrc, null, serverItem.getId()); |
| | | // 订阅 zlm启动事件, 新的zlm也会从这里进入系统 |
| | | hookSubscribe.removeSubscribe(hookSubscribeForRtpServerTimeout); |
| | | return closeRTPServerResult; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public Boolean isRtpReady(MediaServerItem mediaServerItem, String streamId) { |
| | | JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo(mediaServerItem,"rtp", "rtsp", streamId); |
| | | if (mediaInfo.getInteger("code") == -2) { |
| | | return null; |
| | | } |
| | | return (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")); |
| | | } |
| | | |
| | |
| | | */ |
| | | public Boolean isStreamReady(MediaServerItem mediaServerItem, String app, String streamId) { |
| | | JSONObject mediaInfo = zlmresTfulUtils.getMediaList(mediaServerItem, app, streamId); |
| | | return mediaInfo != null && (mediaInfo.getInteger("code") == 0 |
| | | |
| | | if (mediaInfo == null || (mediaInfo.getInteger("code") == -2)) { |
| | | return null; |
| | | } |
| | | return (mediaInfo.getInteger("code") == 0 |
| | | && mediaInfo.getJSONArray("data") != null |
| | | && mediaInfo.getJSONArray("data").size() > 0); |
| | | } |
| | |
| | | // 如果是非严格模式,需要关闭端口占用 |
| | | JSONObject startSendRtpStreamResult = null; |
| | | if (sendRtpItem.getLocalPort() != 0) { |
| | | HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(sendRtpItem.getSsrc(), null, mediaInfo.getId()); |
| | | hookSubscribe.removeSubscribe(hookSubscribeForRtpServerTimeout); |
| | | if (releasePort(mediaInfo, sendRtpItem.getSsrc())) { |
| | | if (sendRtpItem.isTcpActive()) { |
| | | startSendRtpStreamResult = startSendRtpPassive(mediaInfo, param); |
| | | System.out.println(JSON.toJSON(param)); |
| | | }else { |
| | | param.put("is_udp", is_Udp); |
| | | param.put("dst_url", sendRtpItem.getIp()); |
| | | param.put("dst_port", sendRtpItem.getPort()); |
| | | startSendRtpStreamResult = startSendRtpStream(mediaInfo, param); |
| | | } |
| | | if (sendRtpItem.isTcpActive()) { |
| | | startSendRtpStreamResult = startSendRtpPassive(mediaInfo, param); |
| | | System.out.println(JSON.toJSON(param)); |
| | | }else { |
| | | param.put("is_udp", is_Udp); |
| | | param.put("dst_url", sendRtpItem.getIp()); |
| | | param.put("dst_port", sendRtpItem.getPort()); |
| | | startSendRtpStreamResult = startSendRtpStream(mediaInfo, param); |
| | | } |
| | | }else { |
| | | if (sendRtpItem.isTcpActive()) { |
| | |
| | | } |
| | | return startSendRtpStreamResult; |
| | | } |
| | | |
| | | public Boolean updateRtpServerSSRC(MediaServerItem mediaServerItem, String streamId, String ssrc) { |
| | | boolean result = false; |
| | | JSONObject jsonObject = zlmresTfulUtils.updateRtpServerSSRC(mediaServerItem, streamId, ssrc); |
| | | if (jsonObject == null) { |
| | | logger.error("[更新RTPServer] 失败: 请检查ZLM服务"); |
| | | } else if (jsonObject.getInteger("code") == 0) { |
| | | result= true; |
| | | logger.info("[更新RTPServer] 成功"); |
| | | } else { |
| | | logger.error("[更新RTPServer] 失败: {}, streamId:{},ssrc:{}->\r\n{}",jsonObject.getString("msg"), |
| | | streamId, ssrc, jsonObject); |
| | | } |
| | | return result; |
| | | } |
| | | } |