支持 gb28181 sdp f= 字段解析

This commit is contained in:
shikong 2023-09-30 18:11:49 +08:00
parent fde9332d9b
commit 90d5661305
5 changed files with 165 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package cn.skcks.docking.gb28181.sdp;
import cn.skcks.docking.gb28181.sdp.field.ssrc.FormatField;
import cn.skcks.docking.gb28181.sdp.field.ssrc.SsrcField;
import gov.nist.javax.sdp.SessionDescriptionImpl;
import lombok.Getter;
@ -16,6 +17,7 @@ import javax.sdp.SessionDescription;
@Getter
public class GB28181Description extends SessionDescriptionImpl implements SessionDescription {
private SsrcField ssrcField;
private FormatField formatField = new FormatField();
private SessionDescriptionImpl sessionDescription;
public GB28181Description(){
@ -41,6 +43,7 @@ public class GB28181Description extends SessionDescriptionImpl implements Sessio
public String toString() {
StringBuilder sb = new StringBuilder(super.toString());
sb.append(getSsrcField() == null ? "" : getSsrcField().toString());
sb.append(getFormatField() == null ? "" : getFormatField().toString());
return sb.toString();
}
}

View File

@ -0,0 +1,76 @@
package cn.skcks.docking.gb28181.sdp.field.ssrc;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import gov.nist.core.Separators;
import gov.nist.javax.sdp.fields.SDPField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@Slf4j
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
public class FormatField extends SDPField {
public static final String FORMAT_FIELD_NAME = "f";
private static final String FORMAT_FIELD = FORMAT_FIELD_NAME + "=";
public FormatField() {
super(FORMAT_FIELD);
}
/**
* 视频编码格式
*/
private String videoFormat = "";
/**
* 视频分辨率
*/
private String videoRatio = "";
/**
* 视频帧率
*/
private String videoFrame = "";
/**
* 视频码率类型
*/
private String videoRateType = "";
/**
* 视频码率大小
*/
private String videoRateNum = "";
/**
* 音频编码格式
*/
private String audioFormat = "";
/**
* 音频码率大小
*/
private String audioRateNum = "";
/**
* 音频采样率
*/
private String audioSampling = "";
@SneakyThrows
@Override
public String encode() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(FORMAT_FIELD);
if(!StringUtils.isAllBlank(videoFormat, videoRatio, videoFrame, videoRateType, videoRateNum,audioFormat, audioRateNum, audioSampling)){
String video = StringUtils.joinWith("/", "v", videoFormat, videoRatio, videoFrame, videoRateType, videoRateNum);
String audio = StringUtils.joinWith("/", "a", audioFormat, audioRateNum, audioSampling);
stringBuilder.append(StringUtils.joinWith("/",video,audio));
}
stringBuilder.append(Separators.NEWLINE);
log.info("{}", new JsonMapper().writeValueAsString(this));
return stringBuilder.toString();
}
public String toString(){
return encode();
}
}

View File

@ -10,7 +10,8 @@ import lombok.EqualsAndHashCode;
@Data
@AllArgsConstructor
public class SsrcField extends SDPField {
private static final String SSRC_FIELD = "y=";
public static final String SSRC_FIELD_NAME = "y";
private static final String SSRC_FIELD = SSRC_FIELD_NAME + "=";
public SsrcField() {
super(SSRC_FIELD);
}

View File

@ -0,0 +1,77 @@
package cn.skcks.docking.gb28181.sdp.field.ssrc.parser;
import cn.skcks.docking.gb28181.sdp.field.ssrc.FormatField;
import cn.skcks.docking.gb28181.sdp.field.ssrc.SsrcField;
import gov.nist.javax.sdp.fields.SDPField;
import gov.nist.javax.sdp.parser.Lexer;
import gov.nist.javax.sdp.parser.SDPParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.text.ParseException;
@Slf4j
public class FormatFieldParser extends SDPParser {
public FormatFieldParser(String ssrcField) {
this.lexer = new Lexer("charLexer", ssrcField);
}
public FormatField formatField() throws ParseException {
try {
this.lexer.match('f');
this.lexer.SPorHT();
this.lexer.match('=');
this.lexer.SPorHT();
FormatField formatField = new FormatField();
String rest = lexer.getRest().trim();
String[] split = StringUtils.split(rest, 'a');
if(split.length == 0){
return formatField;
}
log.info("{}", (Object) split);
String video = split[0];
String[] videoParams = StringUtils.split(video,"/");
log.info("{}", (Object) videoParams);
if(videoParams.length > 1){
formatField.setVideoFormat(videoParams[1]);
}
if(videoParams.length > 2){
formatField.setVideoRatio(videoParams[2]);
}
if(videoParams.length > 3){
formatField.setVideoFrame(videoParams[3]);
}
if(videoParams.length > 4){
formatField.setVideoRateType(videoParams[4]);
}
if(videoParams.length > 5){
formatField.setVideoRateNum(videoParams[5]);
}
if(split.length < 2){
return formatField;
}
String audio = split[1];
String[] audioParams = audio.split("/");
if(audioParams.length > 0){
formatField.setAudioFormat(audioParams[0]);
}
if(audioParams.length > 1){
formatField.setAudioRateNum(audioParams[1]);
}
if(audioParams.length > 2){
formatField.setAudioSampling(audioParams[2]);
}
return formatField;
} catch (Exception e) {
e.printStackTrace();
throw lexer.createParseException();
}
}
public SDPField parse() throws ParseException {
return this.formatField();
}
}

View File

@ -1,5 +1,8 @@
package cn.skcks.docking.gb28181.sdp.parser;
import cn.skcks.docking.gb28181.sdp.field.ssrc.FormatField;
import cn.skcks.docking.gb28181.sdp.field.ssrc.SsrcField;
import cn.skcks.docking.gb28181.sdp.field.ssrc.parser.FormatFieldParser;
import cn.skcks.docking.gb28181.sdp.field.ssrc.parser.SsrcFieldParser;
import gov.nist.javax.sdp.parser.Lexer;
import gov.nist.javax.sdp.parser.ParserFactory;
@ -10,9 +13,12 @@ import java.text.ParseException;
public class GB28181DescriptionParserFactory {
public static SDPParser createParser(String field) throws ParseException {
String fieldName = Lexer.getFieldName(field);
if(fieldName.equalsIgnoreCase("y")){
if(fieldName.equalsIgnoreCase(SsrcField.SSRC_FIELD_NAME)){
return new SsrcFieldParser(field);
}
if(fieldName.equalsIgnoreCase(FormatField.FORMAT_FIELD_NAME)){
return new FormatFieldParser(field);
}
return ParserFactory.createParser(field);
}
}