package com.inspect.tcpserver.sip.media;
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
public class SdpParser {
|
|
|
|
public static SdpInfo parse(String sdp) {
|
|
SdpInfo info = new SdpInfo();
|
|
|
|
String[] lines = sdp.split("\\r?\\n");
|
|
|
|
for (String line : lines) {
|
|
line = line.trim();
|
|
if (line.isEmpty()) continue;
|
|
|
|
// ---------- 会话级 ----------
|
|
if (line.startsWith("c=")) {
|
|
// c=IN IP4 192.168.1.116
|
|
String[] parts = line.split("\\s+");
|
|
if (parts.length >= 3) {
|
|
info.setRemoteIp(parts[2]);
|
|
}
|
|
}
|
|
|
|
// ---------- media ----------
|
|
else if (line.startsWith("m=video")) {
|
|
// m=video 50000 TCP/AVP 98
|
|
StringTokenizer st = new StringTokenizer(line);
|
|
st.nextToken(); // m=video
|
|
|
|
info.setMediaPort(Integer.parseInt(st.nextToken()));
|
|
|
|
String proto = st.nextToken();
|
|
if (proto.toUpperCase().contains("TCP")) {
|
|
info.setTransport(SdpInfo.Transport.RTP_TCP);
|
|
} else {
|
|
info.setTransport(SdpInfo.Transport.RTP_UDP);
|
|
}
|
|
|
|
info.setPayloadType(Integer.parseInt(st.nextToken()));
|
|
}
|
|
|
|
// ---------- rtpmap ----------
|
|
else if (line.startsWith("a=rtpmap")) {
|
|
// a=rtpmap:98 H264/90000
|
|
String value = line.substring("a=rtpmap:".length());
|
|
String[] parts = value.split("\\s+");
|
|
if (parts.length == 2) {
|
|
String[] codecInfo = parts[1].split("/");
|
|
info.setCodec(codecInfo[0]);
|
|
info.setClockRate(Integer.parseInt(codecInfo[1]));
|
|
}
|
|
}
|
|
|
|
// ---------- setup ----------
|
|
else if (line.startsWith("a=setup:")) {
|
|
info.setSetup(line.substring("a=setup:".length()));
|
|
}
|
|
|
|
// ---------- connection ----------
|
|
else if (line.startsWith("a=connection:")) {
|
|
info.setConnection(line.substring("a=connection:".length()));
|
|
}
|
|
|
|
// ---------- fmtp ----------
|
|
else if (line.startsWith("a=fmtp:")) {
|
|
// 可留作扩展
|
|
info.putAttribute("fmtp", line);
|
|
}
|
|
|
|
// ---------- y= (SSRC) ----------
|
|
else if (line.startsWith("y=")) {
|
|
// y=0700000038
|
|
info.setSsrc(Long.parseLong(line.substring(2)));
|
|
}
|
|
|
|
// ---------- 其他属性 ----------
|
|
else if (line.startsWith("a=")) {
|
|
int idx = line.indexOf(':');
|
|
if (idx > 0) {
|
|
info.putAttribute(
|
|
line.substring(2, idx),
|
|
line.substring(idx + 1)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
return info;
|
|
}
|
|
}
|
|
|