| | |
| | | import com.genersoft.iot.vmp.conf.DynamicTask; |
| | | import com.genersoft.iot.vmp.conf.SipConfig; |
| | | import com.genersoft.iot.vmp.conf.UserSetting; |
| | | import com.genersoft.iot.vmp.conf.exception.ControllerException; |
| | | import com.genersoft.iot.vmp.gb28181.bean.*; |
| | | import com.genersoft.iot.vmp.gb28181.session.AudioBroadcastManager; |
| | | import com.genersoft.iot.vmp.gb28181.session.SSRCFactory; |
| | |
| | | import com.genersoft.iot.vmp.gb28181.utils.SipUtils; |
| | | import com.genersoft.iot.vmp.media.bean.MediaServer; |
| | | import com.genersoft.iot.vmp.media.event.hook.Hook; |
| | | import com.genersoft.iot.vmp.media.event.hook.HookSubscribe; |
| | | import com.genersoft.iot.vmp.media.event.hook.HookType; |
| | | import com.genersoft.iot.vmp.media.service.IMediaServerService; |
| | | import com.genersoft.iot.vmp.media.zlm.ZLMMediaListManager; |
| | | import com.genersoft.iot.vmp.media.zlm.ZLMServerFactory; |
| | | import com.genersoft.iot.vmp.media.event.hook.HookSubscribe; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.*; |
| | | import com.genersoft.iot.vmp.service.*; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem; |
| | | import com.genersoft.iot.vmp.service.IInviteStreamService; |
| | | import com.genersoft.iot.vmp.service.IPlayService; |
| | | import com.genersoft.iot.vmp.service.IStreamProxyService; |
| | | import com.genersoft.iot.vmp.service.IStreamPushService; |
| | | import com.genersoft.iot.vmp.service.bean.ErrorCallback; |
| | | import com.genersoft.iot.vmp.service.bean.InviteErrorCode; |
| | | import com.genersoft.iot.vmp.service.bean.MessageForPushChannel; |
| | |
| | | import gov.nist.javax.sdp.fields.URIField; |
| | | import gov.nist.javax.sip.message.SIPRequest; |
| | | import gov.nist.javax.sip.message.SIPResponse; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | import javax.sip.message.Response; |
| | | import java.text.ParseException; |
| | | import java.time.Instant; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Random; |
| | | import java.util.Vector; |
| | |
| | | |
| | | @Autowired |
| | | private AudioBroadcastManager audioBroadcastManager; |
| | | |
| | | @Autowired |
| | | private ZLMServerFactory zlmServerFactory; |
| | | |
| | | @Autowired |
| | | private IMediaServerService mediaServerService; |
| | |
| | | } else { |
| | | streamTypeStr = "UDP"; |
| | | } |
| | | logger.info("[上级Invite] {}, 平台:{}, 通道:{}, 收流地址:{}:{},收流方式:{}, ssrc:{}", sessionName, username, channelId, addressStr, port, streamTypeStr, ssrc); |
| | | SendRtpItem sendRtpItem = zlmServerFactory.createSendRtpItem(mediaServerItem, addressStr, port, ssrc, requesterId, |
| | | logger.info("[上级Invite] {}, 平台:{}, 通道:{}, 收流地址:{}:{},收流方式:{}, ssrc:{}", |
| | | sessionName, username, channelId, addressStr, port, streamTypeStr, ssrc); |
| | | SendRtpItem sendRtpItem = mediaServerService.createSendRtpItem(mediaServerItem, addressStr, port, ssrc, requesterId, |
| | | device.getDeviceId(), channelId, mediaTransmissionTCP, platform.isRtcp()); |
| | | |
| | | if (tcpActive != null) { |
| | |
| | | // * 2 推流中 |
| | | sendRtpItem.setStatus(1); |
| | | redisCatchStorage.updateSendRTPSever(sendRtpItem); |
| | | |
| | | String sdpIp = mediaServerItemInUSe.getSdpIp(); |
| | | if (!ObjectUtils.isEmpty(platform.getSendStreamIp())) { |
| | | sdpIp = platform.getSendStreamIp(); |
| | | } |
| | | StringBuffer content = new StringBuffer(200); |
| | | content.append("v=0\r\n"); |
| | | content.append("o=" + channelId + " 0 0 IN IP4 " + mediaServerItemInUSe.getSdpIp() + "\r\n"); |
| | | content.append("o=" + channelId + " 0 0 IN IP4 " + sdpIp + "\r\n"); |
| | | content.append("s=" + sessionName + "\r\n"); |
| | | content.append("c=IN IP4 " + mediaServerItemInUSe.getSdpIp() + "\r\n"); |
| | | content.append("c=IN IP4 " + sdpIp + "\r\n"); |
| | | if ("Playback".equalsIgnoreCase(sessionName)) { |
| | | content.append("t=" + finalStartTime + " " + finalStopTime + "\r\n"); |
| | | } else { |
| | |
| | | responseSdpAck(request, content.toString(), platform); |
| | | // tcp主动模式,回复sdp后开启监听 |
| | | if (sendRtpItem.isTcpActive()) { |
| | | MediaServer mediaInfo = mediaServerService.getOne(sendRtpItem.getMediaServerId()); |
| | | Map<String, Object> param = new HashMap<>(12); |
| | | param.put("vhost","__defaultVhost__"); |
| | | param.put("app",sendRtpItem.getApp()); |
| | | param.put("stream",sendRtpItem.getStream()); |
| | | param.put("ssrc", sendRtpItem.getSsrc()); |
| | | if (!sendRtpItem.isTcpActive()) { |
| | | param.put("dst_url",sendRtpItem.getIp()); |
| | | param.put("dst_port", sendRtpItem.getPort()); |
| | | } |
| | | String is_Udp = sendRtpItem.isTcp() ? "0" : "1"; |
| | | param.put("is_udp", is_Udp); |
| | | param.put("src_port", localPort); |
| | | param.put("pt", sendRtpItem.getPt()); |
| | | param.put("use_ps", sendRtpItem.isUsePs() ? "1" : "0"); |
| | | param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0"); |
| | | if (!sendRtpItem.isTcp()) { |
| | | // 开启rtcp保活 |
| | | param.put("udp_rtcp_timeout", sendRtpItem.isRtcp()? "1":"0"); |
| | | } |
| | | JSONObject startSendRtpStreamResult = zlmServerFactory.startSendRtpPassive(mediaInfo, param); |
| | | if (startSendRtpStreamResult != null) { |
| | | startSendRtpStreamHand(evt, sendRtpItem, null, startSendRtpStreamResult, param, callIdHeader); |
| | | } |
| | | MediaServer mediaServer = mediaServerService.getOne(sendRtpItem.getMediaServerId()); |
| | | try { |
| | | mediaServerService.startSendRtpPassive(mediaServer, platform, sendRtpItem, 5); |
| | | }catch (ControllerException e) {} |
| | | } |
| | | } catch (SipException | InvalidArgumentException | ParseException e) { |
| | | logger.error("[命令发送失败] 国标级联 回复SdpAck", e); |
| | | } |
| | | }; |
| | | ErrorCallback<Object> errorEvent = ((statusCode, msg, data) -> { |
| | | logger.info("[上级Invite] {}, 失败, 平台:{}, 通道:{}, code: {}, msg;{}", sessionName, username, channelId, statusCode, msg); |
| | | // 未知错误。直接转发设备点播的错误 |
| | | try { |
| | | if (statusCode > 0) { |
| | | Response response = getMessageFactory().createResponse(statusCode, evt.getRequest()); |
| | | sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), response); |
| | | } |
| | | Response response = getMessageFactory().createResponse(statusCode, evt.getRequest()); |
| | | sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), response); |
| | | } catch (ParseException | SipException e) { |
| | | logger.error("未处理的异常 ", e); |
| | | } |
| | |
| | | } |
| | | |
| | | if ("push".equals(gbStream.getStreamType())) { |
| | | if (streamPushItem != null && streamPushItem.isPushIng()) { |
| | | // 推流状态 |
| | | pushStream(evt, request, gbStream, streamPushItem, platform, callIdHeader, mediaServerItem, port, tcpActive, |
| | | mediaTransmissionTCP, channelId, addressStr, ssrc, requesterId); |
| | | } else { |
| | | // 未推流 拉起 |
| | | notifyStreamOnline(evt, request, gbStream, streamPushItem, platform, callIdHeader, mediaServerItem, port, tcpActive, |
| | | mediaTransmissionTCP, channelId, addressStr, ssrc, requesterId); |
| | | if (streamPushItem != null) { |
| | | // 从redis查询是否正在接收这个推流 |
| | | StreamPushItem pushListItem = redisCatchStorage.getPushListItem(gbStream.getApp(), gbStream.getStream()); |
| | | if (pushListItem != null) { |
| | | pushListItem.setSelf(userSetting.getServerId().equals(pushListItem.getServerId())); |
| | | // 推流状态 |
| | | pushStream(evt, request, gbStream, pushListItem, platform, callIdHeader, mediaServerItem, port, tcpActive, |
| | | mediaTransmissionTCP, channelId, addressStr, ssrc, requesterId); |
| | | }else { |
| | | // 未推流 拉起 |
| | | notifyStreamOnline(evt, request, gbStream, streamPushItem, platform, callIdHeader, mediaServerItem, port, tcpActive, |
| | | mediaTransmissionTCP, channelId, addressStr, ssrc, requesterId); |
| | | } |
| | | } |
| | | } else if ("proxy".equals(gbStream.getStreamType())) { |
| | | if (null != proxyByAppAndStream) { |
| | |
| | | * 安排推流 |
| | | */ |
| | | private void pushProxyStream(RequestEvent evt, SIPRequest request, GbStream gbStream, ParentPlatform platform, |
| | | CallIdHeader callIdHeader, MediaServer mediaServerItem, |
| | | CallIdHeader callIdHeader, MediaServer mediaServer, |
| | | int port, Boolean tcpActive, boolean mediaTransmissionTCP, |
| | | String channelId, String addressStr, String ssrc, String requesterId) { |
| | | Boolean streamReady = zlmServerFactory.isStreamReady(mediaServerItem, gbStream.getApp(), gbStream.getStream()); |
| | | Boolean streamReady = mediaServerService.isStreamReady(mediaServer, gbStream.getApp(), gbStream.getStream()); |
| | | if (streamReady != null && streamReady) { |
| | | |
| | | // 自平台内容 |
| | | SendRtpItem sendRtpItem = zlmServerFactory.createSendRtpItem(mediaServerItem, addressStr, port, ssrc, requesterId, |
| | | SendRtpItem sendRtpItem = mediaServerService.createSendRtpItem(mediaServer, addressStr, port, ssrc, requesterId, |
| | | gbStream.getApp(), gbStream.getStream(), channelId, mediaTransmissionTCP, platform.isRtcp()); |
| | | |
| | | if (sendRtpItem == null) { |
| | |
| | | sendRtpItem.setCallId(callIdHeader.getCallId()); |
| | | sendRtpItem.setFromTag(request.getFromTag()); |
| | | |
| | | SIPResponse response = sendStreamAck(mediaServerItem, request, sendRtpItem, platform, evt); |
| | | SIPResponse response = sendStreamAck(mediaServer, request, sendRtpItem, platform, evt); |
| | | if (response != null) { |
| | | sendRtpItem.setToTag(response.getToTag()); |
| | | } |
| | |
| | | String channelId, String addressStr, String ssrc, String requesterId) { |
| | | // 推流 |
| | | if (streamPushItem.isSelf()) { |
| | | Boolean streamReady = zlmServerFactory.isStreamReady(mediaServerItem, gbStream.getApp(), gbStream.getStream()); |
| | | Boolean streamReady = mediaServerService.isStreamReady(mediaServerItem, gbStream.getApp(), gbStream.getStream()); |
| | | if (streamReady != null && streamReady) { |
| | | // 自平台内容 |
| | | SendRtpItem sendRtpItem = zlmServerFactory.createSendRtpItem(mediaServerItem, addressStr, port, ssrc, requesterId, |
| | | SendRtpItem sendRtpItem = mediaServerService.createSendRtpItem(mediaServerItem, addressStr, port, ssrc, requesterId, |
| | | gbStream.getApp(), gbStream.getStream(), channelId, mediaTransmissionTCP, platform.isRtcp()); |
| | | |
| | | if (sendRtpItem == null) { |
| | |
| | | dynamicTask.stop(callIdHeader.getCallId()); |
| | | redisPushStreamResponseListener.removeEvent(gbStream.getApp(), gbStream.getStream()); |
| | | if (serverId.equals(userSetting.getServerId())) { |
| | | SendRtpItem sendRtpItem = zlmServerFactory.createSendRtpItem(mediaServerItem, addressStr, finalPort, ssrc, requesterId, |
| | | SendRtpItem sendRtpItem = mediaServerService.createSendRtpItem(mediaServerItem, addressStr, finalPort, ssrc, requesterId, |
| | | app, stream, channelId, mediaTransmissionTCP, platform.isRtcp()); |
| | | |
| | | if (sendRtpItem == null) { |
| | |
| | | |
| | | public SIPResponse sendStreamAck(MediaServer mediaServerItem, SIPRequest request, SendRtpItem sendRtpItem, ParentPlatform platform, RequestEvent evt) { |
| | | |
| | | String sdpIp = mediaServerItem.getSdpIp(); |
| | | if (!ObjectUtils.isEmpty(platform.getSendStreamIp())) { |
| | | sdpIp = platform.getSendStreamIp(); |
| | | } |
| | | StringBuffer content = new StringBuffer(200); |
| | | content.append("v=0\r\n"); |
| | | content.append("o=" + sendRtpItem.getChannelId() + " 0 0 IN IP4 " + mediaServerItem.getSdpIp() + "\r\n"); |
| | | content.append("o=" + sendRtpItem.getChannelId() + " 0 0 IN IP4 " + sdpIp + "\r\n"); |
| | | content.append("s=Play\r\n"); |
| | | content.append("c=IN IP4 " + mediaServerItem.getSdpIp() + "\r\n"); |
| | | content.append("c=IN IP4 " + sdpIp + "\r\n"); |
| | | content.append("t=0 0\r\n"); |
| | | // 非严格模式端口不统一, 增加兼容性,修改为一个不为0的端口 |
| | | int localPort = sendRtpItem.getLocalPort(); |
| | |
| | | mediaTransmissionTCP ? (tcpActive ? "TCP主动" : "TCP被动") : "UDP", sdp.getSessionName().getValue()); |
| | | CallIdHeader callIdHeader = (CallIdHeader) request.getHeader(CallIdHeader.NAME); |
| | | |
| | | SendRtpItem sendRtpItem = zlmServerFactory.createSendRtpItem(mediaServerItem, addressStr, port, gb28181Sdp.getSsrc(), requesterId, |
| | | SendRtpItem sendRtpItem = mediaServerService.createSendRtpItem(mediaServerItem, addressStr, port, gb28181Sdp.getSsrc(), requesterId, |
| | | device.getDeviceId(), broadcastCatch.getChannelId(), |
| | | mediaTransmissionTCP, false); |
| | | |
| | |
| | | |
| | | redisCatchStorage.updateSendRTPSever(sendRtpItem); |
| | | |
| | | Boolean streamReady = zlmServerFactory.isStreamReady(mediaServerItem, broadcastCatch.getApp(), broadcastCatch.getStream()); |
| | | Boolean streamReady = mediaServerService.isStreamReady(mediaServerItem, broadcastCatch.getApp(), broadcastCatch.getStream()); |
| | | if (streamReady) { |
| | | sendOk(device, sendRtpItem, sdp, request, mediaServerItem, mediaTransmissionTCP, gb28181Sdp.getSsrc()); |
| | | } else { |