| | |
| | | package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.genersoft.iot.vmp.common.StreamInfo; |
| | | import com.genersoft.iot.vmp.gb28181.bean.*; |
| | | import com.genersoft.iot.vmp.gb28181.event.SipSubscribe; |
| | | import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver; |
| | | import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage; |
| | | import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander; |
| | | import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander; |
| | | 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.media.zlm.ZLMHttpHookSubscribe; |
| | | import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; |
| | | import com.genersoft.iot.vmp.service.IMediaServerService; |
| | | import com.genersoft.iot.vmp.service.IPlayService; |
| | | import com.genersoft.iot.vmp.service.bean.SSRCInfo; |
| | | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; |
| | | import com.genersoft.iot.vmp.storager.IVideoManagerStorager; |
| | | import com.genersoft.iot.vmp.vmanager.gb28181.play.bean.PlayResult; |
| | | import gov.nist.javax.sdp.TimeDescriptionImpl; |
| | | import gov.nist.javax.sdp.fields.TimeField; |
| | | import gov.nist.javax.sip.address.AddressImpl; |
| | | import gov.nist.javax.sip.address.SipUri; |
| | | import org.slf4j.Logger; |
| | |
| | | import javax.sip.ServerTransaction; |
| | | import javax.sip.SipException; |
| | | import javax.sip.address.SipURI; |
| | | import javax.sip.header.CallIdHeader; |
| | | import javax.sip.header.FromHeader; |
| | | import javax.sip.message.Request; |
| | | import javax.sip.message.Response; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Vector; |
| | | |
| | | /** |
| | |
| | | private IPlayService playService; |
| | | |
| | | @Autowired |
| | | private ISIPCommander commander; |
| | | |
| | | @Autowired |
| | | private ZLMRTPServerFactory zlmrtpServerFactory; |
| | | |
| | | @Autowired |
| | |
| | | |
| | | @Autowired |
| | | private SIPProcessorObserver sipProcessorObserver; |
| | | |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() throws Exception { |
| | |
| | | @Override |
| | | public void process(RequestEvent evt) { |
| | | // Invite Request消息实现,此消息一般为级联消息,上级给下级发送请求视频指令 |
| | | Long startTimeForInvite = System.currentTimeMillis(); |
| | | try { |
| | | Request request = evt.getRequest(); |
| | | SipURI sipURI = (SipURI) request.getRequestURI(); |
| | |
| | | String requesterId = null; |
| | | |
| | | FromHeader fromHeader = (FromHeader)request.getHeader(FromHeader.NAME); |
| | | CallIdHeader callIdHeader = (CallIdHeader)request.getHeader(CallIdHeader.NAME); |
| | | AddressImpl address = (AddressImpl) fromHeader.getAddress(); |
| | | SipUri uri = (SipUri) address.getURI(); |
| | | requesterId = uri.getUser(); |
| | |
| | | return; |
| | | } |
| | | |
| | | // 查询请求方是否上级平台 |
| | | // 查询请求是否来自上级平台\设备 |
| | | ParentPlatform platform = storager.queryParentPlatByServerGBId(requesterId); |
| | | if (platform != null) { |
| | | // 查询平台下是否有该通道 |
| | | DeviceChannel channel = storager.queryChannelInParentPlatform(requesterId, channelId); |
| | | GbStream gbStream = storager.queryStreamInParentPlatform(requesterId, channelId); |
| | | PlatformCatalog catalog = storager.getCatalog(channelId); |
| | | MediaServerItem mediaServerItem = null; |
| | | // 不是通道可能是直播流 |
| | | if (channel != null && gbStream == null ) { |
| | |
| | | return; |
| | | } |
| | | responseAck(evt, Response.CALL_IS_BEING_FORWARDED); // 通道存在,发181,呼叫转接中 |
| | | }else { |
| | | }else if (catalog != null) { |
| | | responseAck(evt, Response.BAD_REQUEST, "catalog channel can not play"); // 目录不支持点播 |
| | | return; |
| | | } else { |
| | | logger.info("通道不存在,返回404"); |
| | | responseAck(evt, Response.NOT_FOUND); // 通道不存在,发404,资源不存在 |
| | | return; |
| | |
| | | // 解析sdp消息, 使用jainsip 自带的sdp解析方式 |
| | | String contentString = new String(request.getRawContent()); |
| | | |
| | | // jainSip不支持y=字段, 移除移除以解析。 |
| | | // jainSip不支持y=字段, 移除以解析。 |
| | | int ssrcIndex = contentString.indexOf("y="); |
| | | //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 |
| | | String ssrc = contentString.substring(ssrcIndex + 2, ssrcIndex + 12); |
| | | String substring = contentString.substring(0, contentString.indexOf("y=")); |
| | | SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(substring); |
| | | // 检查是否有y字段 |
| | | String ssrcDefault = "0000000000"; |
| | | String ssrc; |
| | | SessionDescription sdp; |
| | | if (ssrcIndex >= 0) { |
| | | //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 |
| | | ssrc = contentString.substring(ssrcIndex + 2, ssrcIndex + 12); |
| | | String substring = contentString.substring(0, contentString.indexOf("y=")); |
| | | sdp = SdpFactory.getInstance().createSessionDescription(substring); |
| | | }else { |
| | | ssrc = ssrcDefault; |
| | | sdp = SdpFactory.getInstance().createSessionDescription(contentString); |
| | | } |
| | | String sessionName = sdp.getSessionName().getValue(); |
| | | |
| | | Long startTime = null; |
| | | Long stopTime = null; |
| | | Date start = null; |
| | | Date end = null; |
| | | if (sdp.getTimeDescriptions(false) != null && sdp.getTimeDescriptions(false).size() > 0) { |
| | | TimeDescriptionImpl timeDescription = (TimeDescriptionImpl)(sdp.getTimeDescriptions(false).get(0)); |
| | | TimeField startTimeFiled = (TimeField)timeDescription.getTime(); |
| | | startTime = startTimeFiled.getStartTime(); |
| | | stopTime = startTimeFiled.getStopTime(); |
| | | |
| | | start = new Date(startTime*1000); |
| | | end = new Date(stopTime*1000); |
| | | } |
| | | // 获取支持的格式 |
| | | Vector mediaDescriptions = sdp.getMediaDescriptions(true); |
| | | // 查看是否支持PS 负载96 |
| | |
| | | responseAck(evt, Response.BUSY_HERE); |
| | | return; |
| | | } |
| | | |
| | | sendRtpItem.setCallId(callIdHeader.getCallId()); |
| | | sendRtpItem.setPlay("Play".equals(sessionName)); |
| | | // 写入redis, 超时时回复 |
| | | redisCatchStorage.updateSendRTPSever(sendRtpItem); |
| | | // 通知下级推流, |
| | | PlayResult playResult = playService.play(mediaServerItem,device.getDeviceId(), channelId, (mediaServerItemInUSe, responseJSON)->{ |
| | | // 收到推流, 回复200OK, 等待ack |
| | | |
| | | Device finalDevice = device; |
| | | MediaServerItem finalMediaServerItem = mediaServerItem; |
| | | Long finalStartTime = startTime; |
| | | Long finalStopTime = stopTime; |
| | | ZLMHttpHookSubscribe.Event hookEvent = (mediaServerItemInUSe, responseJSON)->{ |
| | | logger.info("[上级点播]收到下级开始点播订阅, {}/{}", sendRtpItem.getApp(), sendRtpItem.getStreamId()); |
| | | // if (sendRtpItem == null) return; |
| | | sendRtpItem.setStatus(1); |
| | | redisCatchStorage.updateSendRTPSever(sendRtpItem); |
| | | // TODO 添加对tcp的支持 |
| | | |
| | | 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("s=Play\r\n"); |
| | | content.append("s=" + sessionName+"\r\n"); |
| | | content.append("c=IN IP4 "+mediaServerItemInUSe.getSdpIp()+"\r\n"); |
| | | content.append("t=0 0\r\n"); |
| | | if ("Playback".equals(sessionName)) { |
| | | content.append("t=" + finalStartTime + " " + finalStopTime + "\r\n"); |
| | | }else { |
| | | content.append("t=0 0\r\n"); |
| | | } |
| | | content.append("m=video "+ sendRtpItem.getLocalPort()+" RTP/AVP 96\r\n"); |
| | | content.append("a=sendonly\r\n"); |
| | | content.append("a=rtpmap:96 PS/90000\r\n"); |
| | |
| | | content.append("f=\r\n"); |
| | | |
| | | try { |
| | | responseAck(evt, content.toString()); |
| | | responseSdpAck(evt, content.toString(), platform); |
| | | } catch (SipException e) { |
| | | e.printStackTrace(); |
| | | } catch (InvalidArgumentException e) { |
| | |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } ,((event) -> { |
| | | if ("Playback".equals(sessionName) && responseJSON != null) { |
| | | playService.onPublishHandlerForPlayBack(finalMediaServerItem, responseJSON, finalDevice.getDeviceId(), channelId, null); |
| | | } |
| | | }; |
| | | SipSubscribe.Event errorEvent = ((event) -> { |
| | | // 未知错误。直接转发设备点播的错误 |
| | | Response response = null; |
| | | try { |
| | |
| | | } catch (ParseException | SipException | InvalidArgumentException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | })); |
| | | if (logger.isDebugEnabled()) { |
| | | logger.debug(playResult.getResult().toString()); |
| | | }); |
| | | if ("Playback".equals(sessionName)) { |
| | | sendRtpItem.setPlay(false); |
| | | SSRCInfo ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, sendRtpItem.getSsrc(), true); |
| | | sendRtpItem.setStreamId(ssrc); |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | commander.playbackStreamCmd(mediaServerItem, ssrcInfo, device, channelId, format.format(start), format.format(end), hookEvent, errorEvent); |
| | | }else { |
| | | sendRtpItem.setPlay(true); |
| | | StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(device.getDeviceId(), channelId); |
| | | if (streamInfo == null) { |
| | | if (mediaServerItem.isRtpEnable()) { |
| | | sendRtpItem.setStreamId(String.format("%s_%s", device.getDeviceId(), channelId)); |
| | | } |
| | | sendRtpItem.setPlay(false); |
| | | playService.play(mediaServerItem,device.getDeviceId(), channelId, hookEvent,errorEvent); |
| | | }else { |
| | | sendRtpItem.setStreamId(streamInfo.getStreamId()); |
| | | hookEvent.response(mediaServerItem, null); |
| | | } |
| | | } |
| | | |
| | | }else if (gbStream != null) { |
| | | SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(mediaServerItem, addressStr, port, ssrc, requesterId, |
| | | gbStream.getApp(), gbStream.getStream(), channelId, |
| | |
| | | |
| | | sendRtpItem.setStatus(1); |
| | | redisCatchStorage.updateSendRTPSever(sendRtpItem); |
| | | // TODO 添加对tcp的支持 |
| | | StringBuffer content = new StringBuffer(200); |
| | | content.append("v=0\r\n"); |
| | | content.append("o="+ channelId +" 0 0 IN IP4 "+mediaServerItem.getSdpIp()+"\r\n"); |
| | |
| | | content.append("f=\r\n"); |
| | | |
| | | try { |
| | | responseAck(evt, content.toString()); |
| | | responseSdpAck(evt, content.toString(), platform); |
| | | } catch (SipException e) { |
| | | e.printStackTrace(); |
| | | } catch (InvalidArgumentException e) { |
| | |
| | | |
| | | } else { |
| | | // 非上级平台请求,查询是否设备请求(通常为接收语音广播的设备) |
| | | Device device = storager.queryVideoDevice(requesterId); |
| | | Device device = redisCatchStorage.getDevice(requesterId); |
| | | if (device != null) { |
| | | logger.info("收到设备" + requesterId + "的语音广播Invite请求"); |
| | | responseAck(evt, Response.TRYING); |