From 421c2f56020bfe60b0e0c90e9d509abf03354dab Mon Sep 17 00:00:00 2001 From: panlinlin <648540858@qq.com> Date: 星期一, 01 三月 2021 18:12:56 +0800 Subject: [PATCH] 使用jainsip的方式解析sdp --- src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java | 216 ++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 162 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java index 58e11ad..7e9e626 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java @@ -1,5 +1,6 @@ package com.genersoft.iot.vmp.gb28181.transmit.request.impl; +import javax.sdp.*; import javax.sip.InvalidArgumentException; import javax.sip.RequestEvent; import javax.sip.SipException; @@ -11,20 +12,18 @@ import javax.sip.message.Response; import com.alibaba.fastjson.JSONObject; +import com.genersoft.iot.vmp.conf.MediaServerConfig; import com.genersoft.iot.vmp.gb28181.bean.Device; import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; -import com.genersoft.iot.vmp.gb28181.sdp.Codec; -import com.genersoft.iot.vmp.gb28181.sdp.MediaDescription; -import com.genersoft.iot.vmp.gb28181.sdp.SdpParser; -import com.genersoft.iot.vmp.gb28181.sdp.SessionDescription; -import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder; -import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage; 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.request.SIPRequestAbstractProcessor; +import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory; +import com.genersoft.iot.vmp.storager.IRedisCatchStorage; import com.genersoft.iot.vmp.storager.IVideoManagerStorager; import com.genersoft.iot.vmp.vmanager.play.bean.PlayResult; import com.genersoft.iot.vmp.vmanager.service.IPlayService; +import gov.nist.javax.sdp.fields.SDPFormat; import gov.nist.javax.sip.address.AddressImpl; import gov.nist.javax.sip.address.SipUri; import org.slf4j.Logger; @@ -34,6 +33,8 @@ import java.io.IOException; import java.text.ParseException; import java.util.List; +import java.util.UUID; +import java.util.Vector; /** * @Description:澶勭悊INVITE璇锋眰 @@ -48,9 +49,21 @@ private IVideoManagerStorager storager; + private IRedisCatchStorage redisCatchStorage; + private SIPCommander cmder; private IPlayService playService; + + private ZLMRTPServerFactory zlmrtpServerFactory; + + public ZLMRTPServerFactory getZlmrtpServerFactory() { + return zlmrtpServerFactory; + } + + public void setZlmrtpServerFactory(ZLMRTPServerFactory zlmrtpServerFactory) { + this.zlmrtpServerFactory = zlmrtpServerFactory; + } /** * 澶勭悊invite璇锋眰 @@ -98,64 +111,144 @@ }else { response100Ack(evt); // 閫氶亾瀛樺湪锛屽彂100锛宼rying } - // 瑙f瀽sdp娑堟伅 - SessionDescription sdp = new SdpParser().parse(request.getRawContent()); + // 瑙f瀽sdp娑堟伅, 浣跨敤jainsip 鑷甫鐨剆dp瑙f瀽鏂瑰紡 + String contentString = new String(request.getRawContent()); + + // jainSip涓嶆敮鎸亂=瀛楁锛� 绉婚櫎绉婚櫎浠ヨВ鏋愩�� + int ssrcIndex = contentString.indexOf("y="); + String ssrc = contentString.substring(ssrcIndex + 2, contentString.length()) + .replace("\r\n", "").replace("\n", ""); + + String substring = contentString.substring(0, contentString.indexOf("y=")); + SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(substring); + // 鑾峰彇鏀寔鐨勬牸寮� - List<MediaDescription> mediaDescriptions = sdp.getMediaDescriptions(); + Vector mediaDescriptions = sdp.getMediaDescriptions(true); // 鏌ョ湅鏄惁鏀寔PS 璐熻浇96 String ip = null; int port = -1; - for (MediaDescription mediaDescription : mediaDescriptions) { + boolean recvonly = false; + boolean mediaTransmissionTCP = false; + Boolean tcpActive = null; + for (int i = 0; i < mediaDescriptions.size(); i++) { + MediaDescription mediaDescription = (MediaDescription)mediaDescriptions.get(i); + Media media = mediaDescription.getMedia(); - List<Codec> codecs = mediaDescription.getCodecs(); - for (Codec codec : codecs) { - if("96".equals(codec.getPayloadType()) || "PS".equals(codec.getName()) || "ps".equals(codec.getName())) { - ip = mediaDescription.getIpAddress().getHostName(); - port = mediaDescription.getPort(); - break; + Vector mediaFormats = media.getMediaFormats(false); + if (mediaFormats.contains("96")) { + port = media.getMediaPort(); + String mediaType = media.getMediaType(); + String protocol = media.getProtocol(); + + // 鍖哄垎TCP鍙戞祦杩樻槸udp锛� 褰撳墠榛樿udp + if ("TCP/RTP/AVP".equals(protocol)) { + String setup = mediaDescription.getAttribute("setup"); + if (setup != null) { + mediaTransmissionTCP = true; + if ("active".equals(setup)) { + tcpActive = true; + }else if ("passive".equals(setup)) { + tcpActive = false; + } + } } +// Vector attributes = mediaDescription.getAttributes(false); +// for (Object attributeObj : attributes) { +// Attribute attribute = (Attribute)attributeObj; +// String name = attribute.getName(); +// switch (name){ +// case "recvonly": +// recvonly = true; +// break; +// case "rtpmap": +// case "connection": +// break; +// case "setup": +// mediaTransmissionTCP = true; +// if ("active".equals(attribute.getValue())) { // tcp涓诲姩妯″紡 +// tcpActive = true; +// }else if ("passive".equals(attribute.getValue())){ // tcp琚姩妯″紡 +// tcpActive = false; +// } +// break; +// +// } +// if ("recvonly".equals(name)) { +// recvonly = true; +// } +// +// String value = attribute.getValue(); +// } + break; } } - if (ip == null || port == -1) { // TODO 娌℃湁鍚堥�傜殑瑙嗛娴佹牸寮忥紝 鍙厤缃槸鍚︿娇鐢ㄧ涓�涓猰edia淇℃伅 - if (mediaDescriptions.size() > 0) { - ip = mediaDescriptions.get(0).getIpAddress().getHostName(); - port = mediaDescriptions.get(0).getPort(); - } - } - - if (ip == null || port == -1) { - response488Ack(evt); + if (port == -1) { + // 鍥炲涓嶆敮鎸佺殑鏍煎紡 + response415Ack(evt); // 涓嶆敮鎸佺殑鏍煎紡锛屽彂415 return; } - - - String ssrc = sdp.getSsrc(); - - Device device = storager.queryVideoDeviceByPlatformIdAndChannelId(platformId, channelId); - if (device == null) { - logger.warn("鐐规挱骞冲彴{}鐨勯�氶亾{}鏃舵湭鎵惧埌璁惧淇℃伅", platformId, channel); - response500Ack(evt); - return; - } - - // 閫氱煡涓嬬骇鎺ㄦ祦锛� - PlayResult playResult = playService.play(device.getDeviceId(), channelId, (response)->{ - // 鏀跺埌鎺ㄦ祦锛� 鍥炲200OK - - },(event -> { - // 鏈煡閿欒銆傜洿鎺ヨ浆鍙戣澶囩偣鎾殑閿欒 - Response response = null; - try { - response = getMessageFactory().createResponse(event.getResponse().getStatusCode(), evt.getRequest()); - getServerTransaction(evt).sendResponse(response); - - } catch (ParseException | SipException | InvalidArgumentException e) { - e.printStackTrace(); - } - })); - playResult.getResult(); + String username = sdp.getOrigin().getUsername(); + String addressStr = sdp.getOrigin().getAddress(); + String sessionName = sdp.getSessionName().getValue(); + logger.info("[涓婄骇鐐规挱]鐢ㄦ埛锛歿}锛� 鍦板潃锛歿}:{}锛� ssrc锛歿}", username, addressStr, port, ssrc); +// +// Device device = storager.queryVideoDeviceByPlatformIdAndChannelId(platformId, channelId); +// if (device == null) { +// logger.warn("鐐规挱骞冲彴{}鐨勯�氶亾{}鏃舵湭鎵惧埌璁惧淇℃伅", platformId, channel); +// response500Ack(evt); +// return; +// } +// +// // 閫氱煡涓嬬骇鎺ㄦ祦锛� +// PlayResult playResult = playService.play(device.getDeviceId(), channelId, (responseJSON)->{ +// // 鏀跺埌鎺ㄦ祦锛� 鍥炲200OK +// UUID uuid = UUID.randomUUID(); +// int rtpServer = zlmrtpServerFactory.createRTPServer(uuid.toString()); +// if (rtpServer == -1) { +// logger.error("涓鸿幏鍙栧埌鍙敤绔彛"); +// return; +// }else { +// zlmrtpServerFactory.closeRTPServer(uuid.toString()); +// } +// // TODO 娣诲姞瀵箃cp鐨勬敮鎸� +// MediaServerConfig mediaInfo = redisCatchStorage.getMediaInfo(); +// StringBuffer content = new StringBuffer(200); +// content.append("v=0\r\n"); +// content.append("o="+"00000"+" 0 0 IN IP4 "+mediaInfo.getWanIp()+"\r\n"); +// content.append("s=Play\r\n"); +// content.append("c=IN IP4 "+mediaInfo.getWanIp()+"\r\n"); +// content.append("t=0 0\r\n"); +// content.append("m=video "+ rtpServer+" RTP/AVP 96\r\n"); +// content.append("a=sendonly\r\n"); +// content.append("a=rtpmap:96 PS/90000\r\n"); +// content.append("y="+ ssrc + "\r\n"); +// content.append("f=\r\n"); +// +// try { +// responseAck(evt, content.toString()); +// } catch (SipException e) { +// e.printStackTrace(); +// } catch (InvalidArgumentException e) { +// e.printStackTrace(); +// } catch (ParseException e) { +// e.printStackTrace(); +// } +// +// // 鍐欏叆redis锛� 瓒呮椂鏃跺洖澶� +//// redisCatchStorage.waiteAck() +// },(event -> { +// // 鏈煡閿欒銆傜洿鎺ヨ浆鍙戣澶囩偣鎾殑閿欒 +// Response response = null; +// try { +// response = getMessageFactory().createResponse(event.getResponse().getStatusCode(), evt.getRequest()); +// getServerTransaction(evt).sendResponse(response); +// +// } catch (ParseException | SipException | InvalidArgumentException e) { +// e.printStackTrace(); +// } +// })); +// playResult.getResult(); // 鏌ユ壘鍚堥�傜殑绔彛鎺ㄦ祦锛� - // 鍙戦�� 200ok // 鏀跺埌ack鍚庤皟鐢ㄦ帹娴佹帴鍙� @@ -163,8 +256,11 @@ } catch (SipException | InvalidArgumentException | ParseException e) { e.printStackTrace(); - } catch (IOException e) { logger.warn("sdp瑙f瀽閿欒"); + e.printStackTrace(); + } catch (SdpParseException e) { + e.printStackTrace(); + } catch (SdpException e) { e.printStackTrace(); } @@ -221,6 +317,18 @@ } /*** + * 鍥炲415 涓嶆敮鎸佺殑濯掍綋绫诲瀷 + * @param evt + * @throws SipException + * @throws InvalidArgumentException + * @throws ParseException + */ + private void response415Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException { + Response response = getMessageFactory().createResponse(Response.UNSUPPORTED_MEDIA_TYPE, evt.getRequest()); + getServerTransaction(evt).sendResponse(response); + } + + /*** * 鍥炲488 * @param evt * @throws SipException -- Gitblit v1.8.0