| | |
| | | import com.genersoft.iot.vmp.conf.UserSetting; |
| | | import com.genersoft.iot.vmp.conf.exception.ControllerException; |
| | | import com.genersoft.iot.vmp.conf.security.JwtUtils; |
| | | import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem; |
| | | import com.genersoft.iot.vmp.media.event.hook.Hook; |
| | | import com.genersoft.iot.vmp.media.event.hook.HookType; |
| | | import com.genersoft.iot.vmp.media.zlm.SendRtpPortManager; |
| | | import com.genersoft.iot.vmp.media.zlm.ZLMServerFactory; |
| | | import com.genersoft.iot.vmp.media.event.hook.HookSubscribe; |
| | | import com.genersoft.iot.vmp.media.bean.MediaServer; |
| | | import com.genersoft.iot.vmp.media.service.IMediaServerService; |
| | | import com.genersoft.iot.vmp.service.bean.SSRCInfo; |
| | | import com.genersoft.iot.vmp.utils.redis.RedisUtil; |
| | | import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; |
| | | import com.genersoft.iot.vmp.vmanager.bean.OtherRtpSendInfo; |
| | |
| | | @RestController |
| | | @RequestMapping("/api/rtp") |
| | | public class RtpController { |
| | | |
| | | @Autowired |
| | | private ZLMServerFactory zlmServerFactory; |
| | | |
| | | @Autowired |
| | | private SendRtpPortManager sendRtpPortManager; |
| | |
| | | logger.info("[第三方服务对接->开启收流和获取发流信息] isSend->{}, ssrc->{}, callId->{}, stream->{}, tcpMode->{}, callBack->{}", |
| | | isSend, ssrc, callId, stream, tcpMode==0?"UDP":"TCP被动", callBack); |
| | | |
| | | MediaServer mediaServerItem = mediaServerService.getDefaultMediaServer(); |
| | | if (mediaServerItem == null) { |
| | | MediaServer mediaServer = mediaServerService.getDefaultMediaServer(); |
| | | if (mediaServer == null) { |
| | | throw new ControllerException(ErrorCode.ERROR100.getCode(),"没有可用的MediaServer"); |
| | | } |
| | | if (stream == null) { |
| | |
| | | } |
| | | } |
| | | String receiveKey = VideoManagerConstants.WVP_OTHER_RECEIVE_RTP_INFO + userSetting.getServerId() + "_" + callId + "_" + stream; |
| | | int localPortForVideo = zlmServerFactory.createRTPServer(mediaServerItem, stream, ssrcInt, null, false, false, tcpMode); |
| | | int localPortForAudio = zlmServerFactory.createRTPServer(mediaServerItem, stream + "_a" , ssrcInt, null, false, false, tcpMode); |
| | | if (localPortForVideo == 0 || localPortForAudio == 0) { |
| | | SSRCInfo ssrcInfoForVideo = mediaServerService.openRTPServer(mediaServer, stream, ssrcInt + "",false,false, null, false, false, false, tcpMode); |
| | | SSRCInfo ssrcInfoForAudio = mediaServerService.openRTPServer(mediaServer, stream + "_a", ssrcInt + "", false, false, null, false,false,false, tcpMode); |
| | | if (ssrcInfoForVideo.getPort() == 0 || ssrcInfoForAudio.getPort() == 0) { |
| | | throw new ControllerException(ErrorCode.ERROR100.getCode(), "获取端口失败"); |
| | | } |
| | | // 注册回调如果rtp收流超时则通过回调发送通知 |
| | | if (callBack != null) { |
| | | Hook hook = Hook.getInstance(HookType.on_rtp_server_timeout, "rtp", stream, mediaServerItem.getId()); |
| | | Hook hook = Hook.getInstance(HookType.on_rtp_server_timeout, "rtp", stream, mediaServer.getId()); |
| | | // 订阅 zlm启动事件, 新的zlm也会从这里进入系统 |
| | | hookSubscribe.addSubscribe(hook, |
| | | (hookData)->{ |
| | |
| | | } |
| | | String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + "_" + callId; |
| | | OtherRtpSendInfo otherRtpSendInfo = new OtherRtpSendInfo(); |
| | | otherRtpSendInfo.setReceiveIp(mediaServerItem.getSdpIp()); |
| | | otherRtpSendInfo.setReceivePortForVideo(localPortForVideo); |
| | | otherRtpSendInfo.setReceivePortForAudio(localPortForAudio); |
| | | otherRtpSendInfo.setReceiveIp(mediaServer.getSdpIp()); |
| | | otherRtpSendInfo.setReceivePortForVideo(ssrcInfoForVideo.getPort()); |
| | | otherRtpSendInfo.setReceivePortForAudio(ssrcInfoForAudio.getPort()); |
| | | otherRtpSendInfo.setCallId(callId); |
| | | otherRtpSendInfo.setStream(stream); |
| | | |
| | |
| | | redisTemplate.opsForValue().set(receiveKey, otherRtpSendInfo); |
| | | if (isSend != null && isSend) { |
| | | // 预创建发流信息 |
| | | int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem); |
| | | int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem); |
| | | int portForVideo = sendRtpPortManager.getNextPort(mediaServer); |
| | | int portForAudio = sendRtpPortManager.getNextPort(mediaServer); |
| | | |
| | | otherRtpSendInfo.setSendLocalIp(mediaServerItem.getSdpIp()); |
| | | otherRtpSendInfo.setSendLocalIp(mediaServer.getSdpIp()); |
| | | otherRtpSendInfo.setSendLocalPortForVideo(portForVideo); |
| | | otherRtpSendInfo.setSendLocalPortForAudio(portForAudio); |
| | | // 将信息写入redis中,以备后用 |
| | |
| | | public void closeRtpServer(String stream) { |
| | | logger.info("[第三方服务对接->关闭收流] stream->{}", stream); |
| | | MediaServer mediaServerItem = mediaServerService.getDefaultMediaServer(); |
| | | zlmServerFactory.closeRtpServer(mediaServerItem,stream); |
| | | zlmServerFactory.closeRtpServer(mediaServerItem,stream + "_a"); |
| | | mediaServerService.closeRTPServer(mediaServerItem, stream); |
| | | mediaServerService.closeRTPServer(mediaServerItem, stream+ "_a"); |
| | | String receiveKey = VideoManagerConstants.WVP_OTHER_RECEIVE_RTP_INFO + userSetting.getServerId() + "_*_" + stream; |
| | | List<Object> scan = RedisUtil.scan(redisTemplate, receiveKey); |
| | | if (scan.size() > 0) { |
| | |
| | | sendInfo.setPushStream(stream); |
| | | sendInfo.setPushSSRC(ssrc); |
| | | |
| | | Map<String, Object> paramForAudio; |
| | | Map<String, Object> paramForVideo; |
| | | |
| | | SendRtpItem sendRtpItemForVideo; |
| | | SendRtpItem sendRtpItemForAudio; |
| | | if (!ObjectUtils.isEmpty(dstIpForAudio) && dstPortForAudio > 0) { |
| | | paramForAudio = new HashMap<>(); |
| | | paramForAudio.put("vhost","__defaultVhost__"); |
| | | paramForAudio.put("app",app); |
| | | paramForAudio.put("stream",stream); |
| | | paramForAudio.put("ssrc", ssrc); |
| | | |
| | | paramForAudio.put("dst_url", dstIpForAudio); |
| | | paramForAudio.put("dst_port", dstPortForAudio); |
| | | String is_Udp = isUdp ? "1" : "0"; |
| | | paramForAudio.put("is_udp", is_Udp); |
| | | paramForAudio.put("src_port", sendInfo.getSendLocalPortForAudio()); |
| | | paramForAudio.put("only_audio", "1"); |
| | | if (ptForAudio != null) { |
| | | paramForAudio.put("pt", ptForAudio); |
| | | } |
| | | |
| | | sendRtpItemForAudio = SendRtpItem.getInstance(app, stream, ssrc, dstIpForAudio, dstPortForAudio, !isUdp, sendInfo.getSendLocalPortForAudio(), ptForAudio); |
| | | } else { |
| | | paramForAudio = null; |
| | | sendRtpItemForAudio = null; |
| | | } |
| | | if (!ObjectUtils.isEmpty(dstIpForVideo) && dstPortForVideo > 0) { |
| | | paramForVideo = new HashMap<>(); |
| | | paramForVideo.put("vhost","__defaultVhost__"); |
| | | paramForVideo.put("app",app); |
| | | paramForVideo.put("stream",stream); |
| | | paramForVideo.put("ssrc", ssrc); |
| | | |
| | | paramForVideo.put("dst_url", dstIpForVideo); |
| | | paramForVideo.put("dst_port", dstPortForVideo); |
| | | String is_Udp = isUdp ? "1" : "0"; |
| | | paramForVideo.put("is_udp", is_Udp); |
| | | paramForVideo.put("src_port", sendInfo.getSendLocalPortForVideo()); |
| | | paramForVideo.put("only_audio", "0"); |
| | | if (ptForVideo != null) { |
| | | paramForVideo.put("pt", ptForVideo); |
| | | } |
| | | |
| | | sendRtpItemForVideo = SendRtpItem.getInstance(app, stream, ssrc, dstIpForAudio, dstPortForAudio, !isUdp, sendInfo.getSendLocalPortForVideo(), ptForVideo); |
| | | } else { |
| | | paramForVideo = null; |
| | | sendRtpItemForVideo = null; |
| | | } |
| | | |
| | | Boolean streamReady = mediaServerService.isStreamReady(mediaServer, app, stream); |
| | | if (streamReady) { |
| | | if (paramForVideo != null) { |
| | | JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServer, paramForVideo); |
| | | if (jsonObject.getInteger("code") == 0) { |
| | | logger.info("[第三方服务对接->发送流] 视频流发流成功,callId->{},param->{}", callId, paramForVideo); |
| | | redisTemplate.opsForValue().set(key, sendInfo); |
| | | }else { |
| | | redisTemplate.delete(key); |
| | | logger.info("[第三方服务对接->发送流] 视频流发流失败,callId->{}, {}", callId, jsonObject.getString("msg")); |
| | | throw new ControllerException(ErrorCode.ERROR100.getCode(), "[视频流发流失败] " + jsonObject.getString("msg")); |
| | | } |
| | | if (sendRtpItemForVideo != null) { |
| | | mediaServerService.startSendRtp(mediaServer, null, sendRtpItemForVideo); |
| | | logger.info("[第三方服务对接->发送流] 视频流发流成功,callId->{},param->{}", callId, sendRtpItemForVideo); |
| | | redisTemplate.opsForValue().set(key, sendInfo); |
| | | } |
| | | if(paramForAudio != null) { |
| | | JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServer, paramForAudio); |
| | | if (jsonObject.getInteger("code") == 0) { |
| | | logger.info("[第三方服务对接->发送流] 音频流发流成功,callId->{},param->{}", callId, paramForAudio); |
| | | redisTemplate.opsForValue().set(key, sendInfo); |
| | | }else { |
| | | redisTemplate.delete(key); |
| | | logger.info("[第三方服务对接->发送流] 音频流发流失败,callId->{}, {}", callId, jsonObject.getString("msg")); |
| | | throw new ControllerException(ErrorCode.ERROR100.getCode(), "[音频流发流失败] " + jsonObject.getString("msg")); |
| | | } |
| | | if(sendRtpItemForAudio != null) { |
| | | mediaServerService.startSendRtp(mediaServer, null, sendRtpItemForAudio); |
| | | logger.info("[第三方服务对接->发送流] 音频流发流成功,callId->{},param->{}", callId, sendRtpItemForAudio); |
| | | redisTemplate.opsForValue().set(key, sendInfo); |
| | | } |
| | | }else { |
| | | logger.info("[第三方服务对接->发送流] 流不存在,等待流上线,callId->{}", callId); |
| | |
| | | }, 10000); |
| | | |
| | | // 订阅 zlm启动事件, 新的zlm也会从这里进入系统 |
| | | OtherRtpSendInfo finalSendInfo = sendInfo; |
| | | hookSubscribe.removeSubscribe(hook); |
| | | OtherRtpSendInfo finalSendInfo = sendInfo; |
| | | hookSubscribe.addSubscribe(hook, |
| | | (hookData)->{ |
| | | dynamicTask.stop(uuid); |
| | |
| | | } catch (InterruptedException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | if (paramForVideo != null) { |
| | | JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServer, paramForVideo); |
| | | if (jsonObject.getInteger("code") == 0) { |
| | | logger.info("[第三方服务对接->发送流] 视频流发流成功,callId->{},param->{}", callId, paramForVideo); |
| | | redisTemplate.opsForValue().set(key, finalSendInfo); |
| | | }else { |
| | | redisTemplate.delete(key); |
| | | logger.info("[第三方服务对接->发送流] 视频流发流失败,callId->{}, {}", callId, jsonObject.getString("msg")); |
| | | throw new ControllerException(ErrorCode.ERROR100.getCode(), "[视频流发流失败] " + jsonObject.getString("msg")); |
| | | } |
| | | if (sendRtpItemForVideo != null) { |
| | | mediaServerService.startSendRtp(mediaServer, null, sendRtpItemForVideo); |
| | | logger.info("[第三方服务对接->发送流] 视频流发流成功,callId->{},param->{}", callId, sendRtpItemForVideo); |
| | | redisTemplate.opsForValue().set(key, finalSendInfo); |
| | | } |
| | | if(paramForAudio != null) { |
| | | JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServer, paramForAudio); |
| | | if (jsonObject.getInteger("code") == 0) { |
| | | logger.info("[第三方服务对接->发送流] 音频流发流成功,callId->{},param->{}", callId, paramForAudio); |
| | | redisTemplate.opsForValue().set(key, finalSendInfo); |
| | | }else { |
| | | redisTemplate.delete(key); |
| | | logger.info("[第三方服务对接->发送流] 音频流发流失败,callId->{}, {}", callId, jsonObject.getString("msg")); |
| | | throw new ControllerException(ErrorCode.ERROR100.getCode(), "[音频流发流失败] " + jsonObject.getString("msg")); |
| | | } |
| | | if(sendRtpItemForAudio != null) { |
| | | mediaServerService.startSendRtp(mediaServer, null, sendRtpItemForAudio); |
| | | logger.info("[第三方服务对接->发送流] 音频流发流成功,callId->{},param->{}", callId, sendRtpItemForAudio); |
| | | redisTemplate.opsForValue().set(key, finalSendInfo); |
| | | } |
| | | hookSubscribe.removeSubscribe(hook); |
| | | }); |
| | |
| | | if (sendInfo == null){ |
| | | throw new ControllerException(ErrorCode.ERROR100.getCode(), "未开启发流"); |
| | | } |
| | | Map<String, Object> param = new HashMap<>(); |
| | | param.put("vhost","__defaultVhost__"); |
| | | param.put("app",sendInfo.getPushApp()); |
| | | param.put("stream",sendInfo.getPushStream()); |
| | | param.put("ssrc",sendInfo.getPushSSRC()); |
| | | MediaServer mediaServerItem = mediaServerService.getDefaultMediaServer(); |
| | | Boolean result = zlmServerFactory.stopSendRtpStream(mediaServerItem, param); |
| | | if (!result) { |
| | | logger.info("[第三方服务对接->关闭发送流] 失败 callId->{}", callId); |
| | | throw new ControllerException(ErrorCode.ERROR100.getCode(), "停止发流失败"); |
| | | }else { |
| | | logger.info("[第三方服务对接->关闭发送流] 成功 callId->{}", callId); |
| | | } |
| | | mediaServerService.stopSendRtp(mediaServerItem, sendInfo.getPushApp(), sendInfo.getPushStream(), sendInfo.getPushSSRC()); |
| | | logger.info("[第三方服务对接->关闭发送流] 成功 callId->{}", callId); |
| | | redisTemplate.delete(key); |
| | | } |
| | | |