648540858
2024-01-10 c25a99d60bef3d3bbd59fee895bd658928fd00db
src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java
old mode 100644 new mode 100755
@@ -6,12 +6,13 @@
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.conf.exception.ControllerException;
import com.genersoft.iot.vmp.media.zlm.SendRtpPortManager;
import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
import com.genersoft.iot.vmp.media.zlm.ZLMServerFactory;
import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe;
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.HookSubscribeForStreamChange;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import com.genersoft.iot.vmp.media.zlm.dto.hook.OnRtpServerTimeoutHookParam;
import com.genersoft.iot.vmp.service.IMediaServerService;
import com.genersoft.iot.vmp.utils.redis.RedisUtil;
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
@@ -42,10 +43,13 @@
@RequestMapping("/api/rtp")
public class RtpController {
    private final static Logger logger = LoggerFactory.getLogger(RtpController.class);
    @Autowired
    private ZLMServerFactory zlmServerFactory;
    @Autowired
    private ZLMRTPServerFactory zlmServerFactory;
    private SendRtpPortManager sendRtpPortManager;
    private final static Logger logger = LoggerFactory.getLogger(RtpController.class);
    @Autowired
    private ZlmHttpHookSubscribe hookSubscribe;
@@ -54,14 +58,10 @@
    private IMediaServerService mediaServerService;
    @Autowired
    private SendRtpPortManager sendRtpPortManager;
    @Autowired
    private UserSetting userSetting;
    @Autowired
    private DynamicTask dynamicTask;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
@@ -91,10 +91,10 @@
        if (isSend != null && isSend && callId == null) {
            throw new ControllerException(ErrorCode.ERROR100.getCode(),"isSend为true时,CallID不能为空");
        }
        int ssrcInt = 0;
        long ssrcInt = 0;
        if (ssrc != null) {
            try {
                ssrcInt = Integer.parseInt(ssrc);
                ssrcInt = Long.parseLong(ssrc);
            }catch (NumberFormatException e) {
                throw new ControllerException(ErrorCode.ERROR100.getCode(),"ssrc格式错误");
            }
@@ -110,11 +110,10 @@
            HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(stream, String.valueOf(ssrcInt), mediaServerItem.getId());
            // 订阅 zlm启动事件, 新的zlm也会从这里进入系统
            hookSubscribe.addSubscribe(hookSubscribeForRtpServerTimeout,
                    (mediaServerItemInUse, response)->{
                        if (stream.equals(response.getString("stream_id"))) {
                            logger.info("[第三方服务对接->开启收流和获取发流信息] 等待收流超时 callId->{}, 发送回调", callId);
                            // 将信息写入redis中,以备后用
                            redisTemplate.delete(receiveKey);
                    (mediaServerItemInUse, hookParam)->{
                        OnRtpServerTimeoutHookParam serverTimeoutHookParam = (OnRtpServerTimeoutHookParam) hookParam;
                        if (stream.equals(serverTimeoutHookParam.getStream_id())) {
                            logger.info("[开启收流和获取发流信息] 等待收流超时 callId->{}, 发送回调", callId);
                            OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
                            OkHttpClient client = httpClientBuilder.build();
                            String url = callBack + "?callId="  + callId;
@@ -128,6 +127,7 @@
                        }
                    });
        }
        String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + "_"  + callId;
        OtherRtpSendInfo otherRtpSendInfo = new OtherRtpSendInfo();
        otherRtpSendInfo.setReceiveIp(mediaServerItem.getSdpIp());
        otherRtpSendInfo.setReceivePortForVideo(localPortForVideo);
@@ -138,17 +138,19 @@
        // 将信息写入redis中,以备后用
        redisTemplate.opsForValue().set(receiveKey, otherRtpSendInfo);
        if (isSend != null && isSend) {
            String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + "_"  + callId;
            // 预创建发流信息
            int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem.getId());
            int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem.getId());
            // 将信息写入redis中,以备后用
            redisTemplate.opsForValue().set(key, otherRtpSendInfo, 300, TimeUnit.SECONDS);
            int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem);
            int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem);
            otherRtpSendInfo.setSendLocalIp(mediaServerItem.getSdpIp());
            otherRtpSendInfo.setSendLocalPortForVideo(portForVideo);
            otherRtpSendInfo.setSendLocalPortForAudio(portForAudio);
            // 将信息写入redis中,以备后用
            redisTemplate.opsForValue().set(key, otherRtpSendInfo, 300, TimeUnit.SECONDS);
            logger.info("[第三方服务对接->开启收流和获取发流信息] 结果,callId->{}, {}", callId, otherRtpSendInfo);
        }
        // 将信息写入redis中,以备后用
        redisTemplate.opsForValue().set(key, otherRtpSendInfo, 300, TimeUnit.SECONDS);
        return otherRtpSendInfo;
    }
@@ -245,7 +247,6 @@
            String is_Udp = isUdp ? "1" : "0";
            paramForAudio.put("is_udp", is_Udp);
            paramForAudio.put("src_port", sendInfo.getSendLocalPortForAudio());
            paramForAudio.put("use_ps", "0");
            paramForAudio.put("only_audio", "1");
            if (ptForAudio != null) {
                paramForAudio.put("pt", ptForAudio);
@@ -266,7 +267,6 @@
            String is_Udp = isUdp ? "1" : "0";
            paramForVideo.put("is_udp", is_Udp);
            paramForVideo.put("src_port", sendInfo.getSendLocalPortForVideo());
            paramForVideo.put("use_ps", "0");
            paramForVideo.put("only_audio", "0");
            if (ptForVideo != null) {
                paramForVideo.put("pt", ptForVideo);