@Test public void testSDPParser() { SDPScheme sdpScheme = SDP.parse(sdp); assertEquals(1, sdpScheme.getMediaLevel().size()); SDPMedia media = sdpScheme.getMediaLevel().get(0); assertEquals("audio", media.getType()); assertEquals(SDPMediaMode.SEND_RECEIVE, media.getMode()); assertEquals("UDP/TLS/RTP/SAVPF", media.getProtocol()); assertEquals(9, media.getCodecs().size()); assertEquals(111, media.getCodecs().get(0).getIndex()); assertEquals(103, media.getCodecs().get(1).getIndex()); assertEquals(9, media.getCodecs().get(2).getIndex()); assertEquals(0, media.getCodecs().get(3).getIndex()); assertEquals(8, media.getCodecs().get(4).getIndex()); assertEquals(106, media.getCodecs().get(5).getIndex()); assertEquals(105, media.getCodecs().get(6).getIndex()); assertEquals(13, media.getCodecs().get(7).getIndex()); assertEquals(126, media.getCodecs().get(8).getIndex()); }
public static SDPScheme parse(String sdp) { SDPReader reader = new SDPReader(sdp); SDPRawRecord record; int version = reader.readVersion(); String originator = reader.readRecord('o').getValue(); String sessionName = reader.readRecord('s').getValue(); SDPSession session = new SDPSession(version, originator, sessionName); while ((record = reader.readUntil('m')) != null) { session.getRecords().add(record); } ArrayList<SDPMedia> medias = new ArrayList<>(); SDPRawRecord mediaLine; while ((mediaLine = reader.readOptionalRecord('m')) != null) { String[] mediaDesc = mediaLine.getValue().split(" "); String mediaType = mediaDesc[0]; int port = Integer.parseInt(mediaDesc[1]); String protocol = mediaDesc[2]; ArrayList<Integer> codecIds = new ArrayList<>(); for (int i = 3; i < mediaDesc.length; i++) { codecIds.add(Integer.parseInt(mediaDesc[i])); } SDPMediaMode mode = SDPMediaMode.SEND_RECEIVE; ArrayList<SDPRawRecord> records = new ArrayList<>(); HashMap<Integer, SDPCodec> codecs = new HashMap<>(); HashMap<Integer, HashMap<String, String>> args = new HashMap<>(); HashMap<Integer, ArrayList<String>> codecFeedbackMessages = new HashMap<>(); while ((record = reader.readUntil('m')) != null) { if (record.getType() == 'a' && "sendrecv".equals(record.getValue())) { mode = SDPMediaMode.SEND_RECEIVE; } else if (record.getType() == 'a' && "inactive".equals(record.getValue())) { mode = SDPMediaMode.INACTIVE; } else if (record.getType() == 'a' && "recvonly".equals(record.getValue())) { mode = SDPMediaMode.RECEIVE_ONLY; } else if (record.getType() == 'a' && "sendonly".equals(record.getValue())) { mode = SDPMediaMode.SEND_ONLY; } else if (record.getType() == 'a' && record.getValue().startsWith("rtpmap:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("rtpmap:".length())); String[] codecDef = codecMap[1].split("/"); String codecName = codecDef[0]; int clockRate = Integer.parseInt(codecDef[1]); String codecArgs = codecDef.length >= 3 ? codecDef[2] : null; codecs.put(index, new SDPCodec(index, codecName, clockRate, codecArgs)); } else if (record.getType() == 'a' && record.getValue().startsWith("fmtp:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("fmtp:".length())); String params = codecMap[1]; String[] pLines = params.trim().split(";"); HashMap<String, String> p = new HashMap<>(); for (String s : pLines) { s = s.trim(); String[] v2 = s.split("=", 2); p.put(v2[0], v2[1]); } args.put(index, p); } else if (record.getType() == 'a' && record.getValue().startsWith("rtcp-fb:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("rtcp-fb:".length())); if (!codecFeedbackMessages.containsKey(index)) { codecFeedbackMessages.put(index, new ArrayList<String>()); } codecFeedbackMessages.get(index).add(codecMap[1]); } else { records.add(record); } } ArrayList<SDPCodec> codec = new ArrayList<>(); for (int index : codecIds) { SDPCodec sdpCodec = codecs.get(index); sdpCodec.setFormat(args.get(index)); sdpCodec.setCodecFeedback(codecFeedbackMessages.get(index)); codec.add(sdpCodec); } medias.add(new SDPMedia(mediaType, port, protocol, codec, mode, records)); } return new SDPScheme(session, medias); }
SDP { public static SDPScheme parse(String sdp) { SDPReader reader = new SDPReader(sdp); SDPRawRecord record; int version = reader.readVersion(); String originator = reader.readRecord('o').getValue(); String sessionName = reader.readRecord('s').getValue(); SDPSession session = new SDPSession(version, originator, sessionName); while ((record = reader.readUntil('m')) != null) { session.getRecords().add(record); } ArrayList<SDPMedia> medias = new ArrayList<>(); SDPRawRecord mediaLine; while ((mediaLine = reader.readOptionalRecord('m')) != null) { String[] mediaDesc = mediaLine.getValue().split(" "); String mediaType = mediaDesc[0]; int port = Integer.parseInt(mediaDesc[1]); String protocol = mediaDesc[2]; ArrayList<Integer> codecIds = new ArrayList<>(); for (int i = 3; i < mediaDesc.length; i++) { codecIds.add(Integer.parseInt(mediaDesc[i])); } SDPMediaMode mode = SDPMediaMode.SEND_RECEIVE; ArrayList<SDPRawRecord> records = new ArrayList<>(); HashMap<Integer, SDPCodec> codecs = new HashMap<>(); HashMap<Integer, HashMap<String, String>> args = new HashMap<>(); HashMap<Integer, ArrayList<String>> codecFeedbackMessages = new HashMap<>(); while ((record = reader.readUntil('m')) != null) { if (record.getType() == 'a' && "sendrecv".equals(record.getValue())) { mode = SDPMediaMode.SEND_RECEIVE; } else if (record.getType() == 'a' && "inactive".equals(record.getValue())) { mode = SDPMediaMode.INACTIVE; } else if (record.getType() == 'a' && "recvonly".equals(record.getValue())) { mode = SDPMediaMode.RECEIVE_ONLY; } else if (record.getType() == 'a' && "sendonly".equals(record.getValue())) { mode = SDPMediaMode.SEND_ONLY; } else if (record.getType() == 'a' && record.getValue().startsWith("rtpmap:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("rtpmap:".length())); String[] codecDef = codecMap[1].split("/"); String codecName = codecDef[0]; int clockRate = Integer.parseInt(codecDef[1]); String codecArgs = codecDef.length >= 3 ? codecDef[2] : null; codecs.put(index, new SDPCodec(index, codecName, clockRate, codecArgs)); } else if (record.getType() == 'a' && record.getValue().startsWith("fmtp:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("fmtp:".length())); String params = codecMap[1]; String[] pLines = params.trim().split(";"); HashMap<String, String> p = new HashMap<>(); for (String s : pLines) { s = s.trim(); String[] v2 = s.split("=", 2); p.put(v2[0], v2[1]); } args.put(index, p); } else if (record.getType() == 'a' && record.getValue().startsWith("rtcp-fb:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("rtcp-fb:".length())); if (!codecFeedbackMessages.containsKey(index)) { codecFeedbackMessages.put(index, new ArrayList<String>()); } codecFeedbackMessages.get(index).add(codecMap[1]); } else { records.add(record); } } ArrayList<SDPCodec> codec = new ArrayList<>(); for (int index : codecIds) { SDPCodec sdpCodec = codecs.get(index); sdpCodec.setFormat(args.get(index)); sdpCodec.setCodecFeedback(codecFeedbackMessages.get(index)); codec.add(sdpCodec); } medias.add(new SDPMedia(mediaType, port, protocol, codec, mode, records)); } return new SDPScheme(session, medias); } }
SDP { public static SDPScheme parse(String sdp) { SDPReader reader = new SDPReader(sdp); SDPRawRecord record; int version = reader.readVersion(); String originator = reader.readRecord('o').getValue(); String sessionName = reader.readRecord('s').getValue(); SDPSession session = new SDPSession(version, originator, sessionName); while ((record = reader.readUntil('m')) != null) { session.getRecords().add(record); } ArrayList<SDPMedia> medias = new ArrayList<>(); SDPRawRecord mediaLine; while ((mediaLine = reader.readOptionalRecord('m')) != null) { String[] mediaDesc = mediaLine.getValue().split(" "); String mediaType = mediaDesc[0]; int port = Integer.parseInt(mediaDesc[1]); String protocol = mediaDesc[2]; ArrayList<Integer> codecIds = new ArrayList<>(); for (int i = 3; i < mediaDesc.length; i++) { codecIds.add(Integer.parseInt(mediaDesc[i])); } SDPMediaMode mode = SDPMediaMode.SEND_RECEIVE; ArrayList<SDPRawRecord> records = new ArrayList<>(); HashMap<Integer, SDPCodec> codecs = new HashMap<>(); HashMap<Integer, HashMap<String, String>> args = new HashMap<>(); HashMap<Integer, ArrayList<String>> codecFeedbackMessages = new HashMap<>(); while ((record = reader.readUntil('m')) != null) { if (record.getType() == 'a' && "sendrecv".equals(record.getValue())) { mode = SDPMediaMode.SEND_RECEIVE; } else if (record.getType() == 'a' && "inactive".equals(record.getValue())) { mode = SDPMediaMode.INACTIVE; } else if (record.getType() == 'a' && "recvonly".equals(record.getValue())) { mode = SDPMediaMode.RECEIVE_ONLY; } else if (record.getType() == 'a' && "sendonly".equals(record.getValue())) { mode = SDPMediaMode.SEND_ONLY; } else if (record.getType() == 'a' && record.getValue().startsWith("rtpmap:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("rtpmap:".length())); String[] codecDef = codecMap[1].split("/"); String codecName = codecDef[0]; int clockRate = Integer.parseInt(codecDef[1]); String codecArgs = codecDef.length >= 3 ? codecDef[2] : null; codecs.put(index, new SDPCodec(index, codecName, clockRate, codecArgs)); } else if (record.getType() == 'a' && record.getValue().startsWith("fmtp:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("fmtp:".length())); String params = codecMap[1]; String[] pLines = params.trim().split(";"); HashMap<String, String> p = new HashMap<>(); for (String s : pLines) { s = s.trim(); String[] v2 = s.split("=", 2); p.put(v2[0], v2[1]); } args.put(index, p); } else if (record.getType() == 'a' && record.getValue().startsWith("rtcp-fb:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("rtcp-fb:".length())); if (!codecFeedbackMessages.containsKey(index)) { codecFeedbackMessages.put(index, new ArrayList<String>()); } codecFeedbackMessages.get(index).add(codecMap[1]); } else { records.add(record); } } ArrayList<SDPCodec> codec = new ArrayList<>(); for (int index : codecIds) { SDPCodec sdpCodec = codecs.get(index); sdpCodec.setFormat(args.get(index)); sdpCodec.setCodecFeedback(codecFeedbackMessages.get(index)); codec.add(sdpCodec); } medias.add(new SDPMedia(mediaType, port, protocol, codec, mode, records)); } return new SDPScheme(session, medias); }  }
SDP { public static SDPScheme parse(String sdp) { SDPReader reader = new SDPReader(sdp); SDPRawRecord record; int version = reader.readVersion(); String originator = reader.readRecord('o').getValue(); String sessionName = reader.readRecord('s').getValue(); SDPSession session = new SDPSession(version, originator, sessionName); while ((record = reader.readUntil('m')) != null) { session.getRecords().add(record); } ArrayList<SDPMedia> medias = new ArrayList<>(); SDPRawRecord mediaLine; while ((mediaLine = reader.readOptionalRecord('m')) != null) { String[] mediaDesc = mediaLine.getValue().split(" "); String mediaType = mediaDesc[0]; int port = Integer.parseInt(mediaDesc[1]); String protocol = mediaDesc[2]; ArrayList<Integer> codecIds = new ArrayList<>(); for (int i = 3; i < mediaDesc.length; i++) { codecIds.add(Integer.parseInt(mediaDesc[i])); } SDPMediaMode mode = SDPMediaMode.SEND_RECEIVE; ArrayList<SDPRawRecord> records = new ArrayList<>(); HashMap<Integer, SDPCodec> codecs = new HashMap<>(); HashMap<Integer, HashMap<String, String>> args = new HashMap<>(); HashMap<Integer, ArrayList<String>> codecFeedbackMessages = new HashMap<>(); while ((record = reader.readUntil('m')) != null) { if (record.getType() == 'a' && "sendrecv".equals(record.getValue())) { mode = SDPMediaMode.SEND_RECEIVE; } else if (record.getType() == 'a' && "inactive".equals(record.getValue())) { mode = SDPMediaMode.INACTIVE; } else if (record.getType() == 'a' && "recvonly".equals(record.getValue())) { mode = SDPMediaMode.RECEIVE_ONLY; } else if (record.getType() == 'a' && "sendonly".equals(record.getValue())) { mode = SDPMediaMode.SEND_ONLY; } else if (record.getType() == 'a' && record.getValue().startsWith("rtpmap:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("rtpmap:".length())); String[] codecDef = codecMap[1].split("/"); String codecName = codecDef[0]; int clockRate = Integer.parseInt(codecDef[1]); String codecArgs = codecDef.length >= 3 ? codecDef[2] : null; codecs.put(index, new SDPCodec(index, codecName, clockRate, codecArgs)); } else if (record.getType() == 'a' && record.getValue().startsWith("fmtp:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("fmtp:".length())); String params = codecMap[1]; String[] pLines = params.trim().split(";"); HashMap<String, String> p = new HashMap<>(); for (String s : pLines) { s = s.trim(); String[] v2 = s.split("=", 2); p.put(v2[0], v2[1]); } args.put(index, p); } else if (record.getType() == 'a' && record.getValue().startsWith("rtcp-fb:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("rtcp-fb:".length())); if (!codecFeedbackMessages.containsKey(index)) { codecFeedbackMessages.put(index, new ArrayList<String>()); } codecFeedbackMessages.get(index).add(codecMap[1]); } else { records.add(record); } } ArrayList<SDPCodec> codec = new ArrayList<>(); for (int index : codecIds) { SDPCodec sdpCodec = codecs.get(index); sdpCodec.setFormat(args.get(index)); sdpCodec.setCodecFeedback(codecFeedbackMessages.get(index)); codec.add(sdpCodec); } medias.add(new SDPMedia(mediaType, port, protocol, codec, mode, records)); } return new SDPScheme(session, medias); }  static SDPScheme parse(String sdp); }
SDP { public static SDPScheme parse(String sdp) { SDPReader reader = new SDPReader(sdp); SDPRawRecord record; int version = reader.readVersion(); String originator = reader.readRecord('o').getValue(); String sessionName = reader.readRecord('s').getValue(); SDPSession session = new SDPSession(version, originator, sessionName); while ((record = reader.readUntil('m')) != null) { session.getRecords().add(record); } ArrayList<SDPMedia> medias = new ArrayList<>(); SDPRawRecord mediaLine; while ((mediaLine = reader.readOptionalRecord('m')) != null) { String[] mediaDesc = mediaLine.getValue().split(" "); String mediaType = mediaDesc[0]; int port = Integer.parseInt(mediaDesc[1]); String protocol = mediaDesc[2]; ArrayList<Integer> codecIds = new ArrayList<>(); for (int i = 3; i < mediaDesc.length; i++) { codecIds.add(Integer.parseInt(mediaDesc[i])); } SDPMediaMode mode = SDPMediaMode.SEND_RECEIVE; ArrayList<SDPRawRecord> records = new ArrayList<>(); HashMap<Integer, SDPCodec> codecs = new HashMap<>(); HashMap<Integer, HashMap<String, String>> args = new HashMap<>(); HashMap<Integer, ArrayList<String>> codecFeedbackMessages = new HashMap<>(); while ((record = reader.readUntil('m')) != null) { if (record.getType() == 'a' && "sendrecv".equals(record.getValue())) { mode = SDPMediaMode.SEND_RECEIVE; } else if (record.getType() == 'a' && "inactive".equals(record.getValue())) { mode = SDPMediaMode.INACTIVE; } else if (record.getType() == 'a' && "recvonly".equals(record.getValue())) { mode = SDPMediaMode.RECEIVE_ONLY; } else if (record.getType() == 'a' && "sendonly".equals(record.getValue())) { mode = SDPMediaMode.SEND_ONLY; } else if (record.getType() == 'a' && record.getValue().startsWith("rtpmap:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("rtpmap:".length())); String[] codecDef = codecMap[1].split("/"); String codecName = codecDef[0]; int clockRate = Integer.parseInt(codecDef[1]); String codecArgs = codecDef.length >= 3 ? codecDef[2] : null; codecs.put(index, new SDPCodec(index, codecName, clockRate, codecArgs)); } else if (record.getType() == 'a' && record.getValue().startsWith("fmtp:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("fmtp:".length())); String params = codecMap[1]; String[] pLines = params.trim().split(";"); HashMap<String, String> p = new HashMap<>(); for (String s : pLines) { s = s.trim(); String[] v2 = s.split("=", 2); p.put(v2[0], v2[1]); } args.put(index, p); } else if (record.getType() == 'a' && record.getValue().startsWith("rtcp-fb:")) { String[] codecMap = record.getValue().split(" ", 2); int index = Integer.parseInt(codecMap[0].substring("rtcp-fb:".length())); if (!codecFeedbackMessages.containsKey(index)) { codecFeedbackMessages.put(index, new ArrayList<String>()); } codecFeedbackMessages.get(index).add(codecMap[1]); } else { records.add(record); } } ArrayList<SDPCodec> codec = new ArrayList<>(); for (int index : codecIds) { SDPCodec sdpCodec = codecs.get(index); sdpCodec.setFormat(args.get(index)); sdpCodec.setCodecFeedback(codecFeedbackMessages.get(index)); codec.add(sdpCodec); } medias.add(new SDPMedia(mediaType, port, protocol, codec, mode, records)); } return new SDPScheme(session, medias); }  static SDPScheme parse(String sdp);  }