|  |  | 
 |  |  | package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.alibaba.fastjson2.JSONObject; | 
 |  |  | import com.genersoft.iot.vmp.conf.DynamicTask; | 
 |  |  | import com.genersoft.iot.vmp.conf.UserSetting; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.bean.*; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.event.SipSubscribe; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.session.SsrcConfig; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.SIPSender; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.event.request.ISIPRequestProcessor; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.utils.SipUtils; | 
 |  |  | import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe; | 
 |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMMediaListManager; | 
 |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory; | 
 |  |  | import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe; | 
 |  |  | import com.genersoft.iot.vmp.media.zlm.dto.*; | 
 |  |  | import com.genersoft.iot.vmp.service.IMediaServerService; | 
 |  |  | import com.genersoft.iot.vmp.service.IPlayService; | 
 |  |  | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  |  | 
 |  |  | import javax.sdp.*; | 
 |  |  | import javax.sip.*; | 
 |  |  | import javax.sip.InvalidArgumentException; | 
 |  |  | import javax.sip.RequestEvent; | 
 |  |  | import javax.sip.SipException; | 
 |  |  | import javax.sip.header.CallIdHeader; | 
 |  |  | import javax.sip.message.Request; | 
 |  |  | import javax.sip.message.Response; | 
 |  |  | import java.text.ParseException; | 
 |  |  | import java.time.Instant; | 
 |  |  | import java.util.Random; | 
 |  |  | import java.util.Vector; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  | 
 |  |  |                 StreamProxyItem proxyByAppAndStream =null; | 
 |  |  |                 // 不是通道可能是直播流 | 
 |  |  |                 if (channel != null && gbStream == null) { | 
 |  |  | //                    if (channel.getStatus() == 0) { | 
 |  |  | //                        logger.info("通道离线,返回400"); | 
 |  |  | //                        responseAck(request, Response.BAD_REQUEST, "channel [" + channel.getChannelId() + "] offline"); | 
 |  |  | //                        return; | 
 |  |  | //                    } | 
 |  |  |                     // 通道存在,发100,TRYING | 
 |  |  |                     try { | 
 |  |  |                         responseAck(request, Response.TRYING); | 
 |  |  | 
 |  |  |                     } | 
 |  |  |                     return; | 
 |  |  |                 } else { | 
 |  |  |                     logger.info("通道不存在,返回404"); | 
 |  |  |                     logger.info("通道不存在,返回404: {}", channelId); | 
 |  |  |                     try { | 
 |  |  |                         // 通道不存在,发404,资源不存在 | 
 |  |  |                         responseAck(request, Response.NOT_FOUND); | 
 |  |  | 
 |  |  |                     return; | 
 |  |  |                 } | 
 |  |  |                 String username = sdp.getOrigin().getUsername(); | 
 |  |  |                 String addressStr = sdp.getOrigin().getAddress(); | 
 |  |  |                 String addressStr = sdp.getConnection().getAddress(); | 
 |  |  |  | 
 |  |  |                 logger.info("[上级点播]用户:{}, 通道:{}, 地址:{}:{}, ssrc:{}", username, channelId, addressStr, port, ssrc); | 
 |  |  |                 Device device = null; | 
 |  |  | 
 |  |  |                         return; | 
 |  |  |                     } | 
 |  |  |                     SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(mediaServerItem, addressStr, port, ssrc, requesterId, | 
 |  |  |                             device.getDeviceId(), channelId, | 
 |  |  |                             mediaTransmissionTCP); | 
 |  |  |                             device.getDeviceId(), channelId, mediaTransmissionTCP, platform.isRtcp()); | 
 |  |  |  | 
 |  |  |                     if (tcpActive != null) { | 
 |  |  |                         sendRtpItem.setTcpActive(tcpActive); | 
 |  |  | 
 |  |  |                         } else { | 
 |  |  |                             content.append("t=0 0\r\n"); | 
 |  |  |                         } | 
 |  |  |                         content.append("m=video " + sendRtpItem.getLocalPort() + " RTP/AVP 96\r\n"); | 
 |  |  |                         int localPort = sendRtpItem.getLocalPort(); | 
 |  |  |                         if (localPort == 0) { | 
 |  |  |                             // 非严格模式端口不统一, 增加兼容性,修改为一个不为0的端口 | 
 |  |  |                             localPort = new Random().nextInt(65535) + 1; | 
 |  |  |                         } | 
 |  |  |                         content.append("m=video " + localPort + " RTP/AVP 96\r\n"); | 
 |  |  |                         content.append("a=sendonly\r\n"); | 
 |  |  |                         content.append("a=rtpmap:96 PS/90000\r\n"); | 
 |  |  |                         content.append("y=" + sendRtpItem.getSsrc() + "\r\n"); | 
 |  |  | 
 |  |  |                         // 未知错误。直接转发设备点播的错误 | 
 |  |  |                         try { | 
 |  |  |                             Response response = getMessageFactory().createResponse(event.statusCode, evt.getRequest()); | 
 |  |  |                             sipSender.transmitRequest(response); | 
 |  |  |                             sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), response); | 
 |  |  |                         } catch (ParseException | SipException  e) { | 
 |  |  |                             e.printStackTrace(); | 
 |  |  |                         } | 
 |  |  | 
 |  |  |  | 
 |  |  |                             // 写入redis, 超时时回复 | 
 |  |  |                             redisCatchStorage.updateSendRTPSever(sendRtpItem); | 
 |  |  |                             MediaServerItem finalMediaServerItem = mediaServerItem; | 
 |  |  |                             playService.play(mediaServerItem, ssrcInfo, device, channelId, hookEvent, errorEvent, (code, msg) -> { | 
 |  |  |                                 logger.info("[上级点播]超时, 用户:{}, 通道:{}", username, channelId); | 
 |  |  |                                 redisCatchStorage.deleteSendRTPServer(platform.getServerGBId(), channelId, callIdHeader.getCallId(), null); | 
 |  |  |                             }, null); | 
 |  |  |                             }); | 
 |  |  |                         } else { | 
 |  |  |                             sendRtpItem.setStreamId(playTransaction.getStream()); | 
 |  |  |                             // 写入redis, 超时时回复 | 
 |  |  | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } else if (gbStream != null) { | 
 |  |  |                     if(ssrc.equals(ssrcDefault)) | 
 |  |  |                     { | 
 |  |  |                         SsrcConfig ssrcConfig = mediaServerItem.getSsrcConfig(); | 
 |  |  |                         if(ssrcConfig != null) | 
 |  |  |                         { | 
 |  |  |                             ssrc = ssrcConfig.getPlaySsrc(); | 
 |  |  |                             ssrcConfig.releaseSsrc(ssrc); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                     if("push".equals(gbStream.getStreamType())) { | 
 |  |  |                         if (streamPushItem != null && streamPushItem.isPushIng()) { | 
 |  |  |                             // 推流状态 | 
 |  |  | 
 |  |  |                                     mediaTransmissionTCP, channelId, addressStr, ssrc, requesterId); | 
 |  |  |                         } | 
 |  |  |                     }else if ("proxy".equals(gbStream.getStreamType())){ | 
 |  |  |                         if(null != proxyByAppAndStream &&proxyByAppAndStream.isStatus()){ | 
 |  |  |                             pushProxyStream(evt, request, gbStream,  platform, callIdHeader, mediaServerItem, port, tcpActive, | 
 |  |  |                                     mediaTransmissionTCP, channelId, addressStr, ssrc, requesterId); | 
 |  |  |                         }else{ | 
 |  |  |                             //开启代理拉流 | 
 |  |  |                             boolean start1 = streamProxyService.start(gbStream.getApp(), gbStream.getStream()); | 
 |  |  |                             if(start1) { | 
 |  |  |                         if (null != proxyByAppAndStream) { | 
 |  |  |                             if(proxyByAppAndStream.isStatus()){ | 
 |  |  |                                 pushProxyStream(evt, request, gbStream,  platform, callIdHeader, mediaServerItem, port, tcpActive, | 
 |  |  |                                         mediaTransmissionTCP, channelId, addressStr, ssrc, requesterId); | 
 |  |  |                             }else{ | 
 |  |  |                                 //失败后通知 | 
 |  |  |                                 //开启代理拉流 | 
 |  |  |                                 notifyStreamOnline(evt, request,gbStream, null, platform, callIdHeader, mediaServerItem, port, tcpActive, | 
 |  |  |                                         mediaTransmissionTCP, channelId, addressStr, ssrc, requesterId); | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  | 
 |  |  |             if (streamReady) { | 
 |  |  |                 // 自平台内容 | 
 |  |  |                 SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(mediaServerItem, addressStr, port, ssrc, requesterId, | 
 |  |  |                         gbStream.getApp(), gbStream.getStream(), channelId, | 
 |  |  |                         mediaTransmissionTCP); | 
 |  |  |                         gbStream.getApp(), gbStream.getStream(), channelId, mediaTransmissionTCP, platform.isRtcp()); | 
 |  |  |  | 
 |  |  |                 if (sendRtpItem == null) { | 
 |  |  |                     logger.warn("服务器端口资源不足"); | 
 |  |  | 
 |  |  |             if (streamReady) { | 
 |  |  |                 // 自平台内容 | 
 |  |  |                 SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(mediaServerItem, addressStr, port, ssrc, requesterId, | 
 |  |  |                         gbStream.getApp(), gbStream.getStream(), channelId, | 
 |  |  |                         mediaTransmissionTCP); | 
 |  |  |                         gbStream.getApp(), gbStream.getStream(), channelId, mediaTransmissionTCP, platform.isRtcp()); | 
 |  |  |  | 
 |  |  |                 if (sendRtpItem == null) { | 
 |  |  |                     logger.warn("服务器端口资源不足"); | 
 |  |  | 
 |  |  |         if ("proxy".equals(gbStream.getStreamType())) { | 
 |  |  |             // TODO 控制启用以使设备上线 | 
 |  |  |             logger.info("[ app={}, stream={} ]通道未推流,启用流后开始推流", gbStream.getApp(), gbStream.getStream()); | 
 |  |  |             try { | 
 |  |  |                 responseAck(request, Response.BAD_REQUEST, "channel [" + gbStream.getGbId() + "] offline"); | 
 |  |  |             } catch (SipException | InvalidArgumentException | ParseException e) { | 
 |  |  |                 logger.error("[命令发送失败] invite 通道未推流: {}", e.getMessage()); | 
 |  |  |             // 监听流上线 | 
 |  |  |             HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed(gbStream.getApp(), gbStream.getStream(), true, "rtsp", mediaServerItem.getId()); | 
 |  |  |             zlmHttpHookSubscribe.addSubscribe(hookSubscribe, (mediaServerItemInUSe, responseJSON) -> { | 
 |  |  |                 String app = responseJSON.getString("app"); | 
 |  |  |                 String stream = responseJSON.getString("stream"); | 
 |  |  |                 logger.info("[上级点播]拉流代理已经就绪, {}/{}", app, stream); | 
 |  |  |                 dynamicTask.stop(callIdHeader.getCallId()); | 
 |  |  |                 pushProxyStream(evt, request, gbStream,  platform, callIdHeader, mediaServerItem, port, tcpActive, | 
 |  |  |                         mediaTransmissionTCP, channelId, addressStr, ssrc, requesterId); | 
 |  |  |             }); | 
 |  |  |             dynamicTask.startDelay(callIdHeader.getCallId(), () -> { | 
 |  |  |                 logger.info("[ app={}, stream={} ] 等待拉流代理流超时", gbStream.getApp(), gbStream.getStream()); | 
 |  |  |                 zlmHttpHookSubscribe.removeSubscribe(hookSubscribe); | 
 |  |  |             }, userSetting.getPlatformPlayTimeout()); | 
 |  |  |             boolean start = streamProxyService.start(gbStream.getApp(), gbStream.getStream()); | 
 |  |  |             if (!start) { | 
 |  |  |                 try { | 
 |  |  |                     responseAck(request, Response.BUSY_HERE, "channel [" + gbStream.getGbId() + "] offline"); | 
 |  |  |                 } catch (SipException | InvalidArgumentException | ParseException e) { | 
 |  |  |                     logger.error("[命令发送失败] invite 通道未推流: {}", e.getMessage()); | 
 |  |  |                 } | 
 |  |  |                 zlmHttpHookSubscribe.removeSubscribe(hookSubscribe); | 
 |  |  |                 dynamicTask.stop(callIdHeader.getCallId()); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         } else if ("push".equals(gbStream.getStreamType())) { | 
 |  |  |             if (!platform.isStartOfflinePush()) { | 
 |  |  |                 // 平台设置中关闭了拉起离线的推流则直接回复 | 
 |  |  |                 try { | 
 |  |  |                     logger.info("[上级点播] 失败,推流设备未推流,channel: {}, app: {}, stream: {}", gbStream.getGbId(), gbStream.getApp(), gbStream.getStream()); | 
 |  |  |                     responseAck(request, Response.TEMPORARILY_UNAVAILABLE, "channel stream not pushing"); | 
 |  |  |                 } catch (SipException | InvalidArgumentException | ParseException e) { | 
 |  |  |                     logger.error("[命令发送失败] invite 通道未推流: {}", e.getMessage()); | 
 |  |  | 
 |  |  |                 dynamicTask.stop(callIdHeader.getCallId()); | 
 |  |  |                 if (serverId.equals(userSetting.getServerId())) { | 
 |  |  |                     SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(mediaServerItem, addressStr, finalPort, ssrc, requesterId, | 
 |  |  |                             app, stream, channelId, mediaTransmissionTCP); | 
 |  |  |                             app, stream, channelId, mediaTransmissionTCP, platform.isRtcp()); | 
 |  |  |  | 
 |  |  |                     if (sendRtpItem == null) { | 
 |  |  |                         logger.warn("上级点时创建sendRTPItem失败,可能是服务器端口资源不足"); | 
 |  |  | 
 |  |  |         // 发送redis消息 | 
 |  |  |         redisGbPlayMsgListener.sendMsg(streamPushItem.getServerId(), streamPushItem.getMediaServerId(), | 
 |  |  |                 streamPushItem.getApp(), streamPushItem.getStream(), addressStr, port, ssrc, requesterId, | 
 |  |  |                 channelId, mediaTransmissionTCP, null, responseSendItemMsg -> { | 
 |  |  |                 channelId, mediaTransmissionTCP, platform.isRtcp(),null, responseSendItemMsg -> { | 
 |  |  |                     SendRtpItem sendRtpItem = responseSendItemMsg.getSendRtpItem(); | 
 |  |  |                     if (sendRtpItem == null || responseSendItemMsg.getMediaServerItem() == null) { | 
 |  |  |                         logger.warn("服务器端口资源不足"); | 
 |  |  | 
 |  |  |         content.append("s=Play\r\n"); | 
 |  |  |         content.append("c=IN IP4 " + mediaServerItem.getSdpIp() + "\r\n"); | 
 |  |  |         content.append("t=0 0\r\n"); | 
 |  |  |         content.append("m=video " + sendRtpItem.getLocalPort() + " RTP/AVP 96\r\n"); | 
 |  |  |         // 非严格模式端口不统一, 增加兼容性,修改为一个不为0的端口 | 
 |  |  |         int localPort = sendRtpItem.getLocalPort(); | 
 |  |  |         if(localPort == 0) | 
 |  |  |         { | 
 |  |  |             localPort = new Random().nextInt(65535) + 1; | 
 |  |  |         } | 
 |  |  |         content.append("m=video " + localPort + " RTP/AVP 96\r\n"); | 
 |  |  |         content.append("a=sendonly\r\n"); | 
 |  |  |         content.append("a=rtpmap:96 PS/90000\r\n"); | 
 |  |  |         if (sendRtpItem.isTcp()) { | 
 |  |  | 
 |  |  |                     return; | 
 |  |  |                 } | 
 |  |  |                 String username = sdp.getOrigin().getUsername(); | 
 |  |  |                 String addressStr = sdp.getOrigin().getAddress(); | 
 |  |  |                 String addressStr = sdp.getConnection().getAddress(); | 
 |  |  |                 logger.info("设备{}请求语音流,地址:{}:{},ssrc:{}", username, addressStr, port, ssrc); | 
 |  |  |             } catch (SdpException e) { | 
 |  |  |                 logger.error("[SDP解析异常]", e); |