MediaDescriptionFieldParser implements SdpParser<MediaDescriptionField> { @Override public MediaDescriptionField parse(String sdp) throws SdpException { try { String[] values = sdp.trim().substring(2).split(" "); int index = 0; int maxIndex = values.length - 1; String media = values[index++]; int port = Integer.parseInt(values[index++]); String protocol = values[index++]; String[] formats = null; if (maxIndex - index >= 0) { int numFormats = maxIndex - index + 1; formats = new String[numFormats]; for (int i = 0; i < numFormats; i++) { formats[i] = values[i + index]; } } MediaDescriptionField md = new MediaDescriptionField(); md.setMedia(media); md.setPort(port); md.setProtocol(protocol); md.setPayloadTypes(formats); return md; } catch (Exception e) { throw new SdpException(PARSE_ERROR + sdp, e); } }  @Override boolean canParse(String sdp); @Override MediaDescriptionField parse(String sdp); @Override void parse(MediaDescriptionField field, String sdp);  }
@Test public void testParse() throws SdpException { String line = "m=video 64534 RTP/AVPF 0 101 120\n\r"; MediaDescriptionField md = parser.parse(line); Assert.assertEquals("video", md.getMedia()); Assert.assertEquals(64534, md.getPort()); Assert.assertEquals("RTP/AVPF", md.getProtocol()); Assert.assertTrue(md.containsPayloadType("0")); Assert.assertTrue(md.containsPayloadType("101")); Assert.assertTrue(md.containsPayloadType("120")); }
@Test public void testParseImageType() throws SdpException { String line = "m=image 52550 udptl t38\n\r"; MediaDescriptionField md = parser.parse(line); Assert.assertEquals("image", md.getMedia()); Assert.assertEquals(52550, md.getPort()); Assert.assertEquals("udptl", md.getProtocol()); Assert.assertTrue(md.containsPayloadType("t38")); }
@Test public void testParseOverwrite() throws SdpException { String line1 = "m=video 64534 RTP/AVPF 0 101 120\n\r"; String line2 = "m=audio 65000 RTP/SAVPF 0 101 180\n\r"; MediaDescriptionField md = parser.parse(line1); parser.parse(md, line2); Assert.assertEquals("audio", md.getMedia()); Assert.assertEquals(65000, md.getPort()); Assert.assertEquals("RTP/SAVPF", md.getProtocol()); Assert.assertTrue(md.containsPayloadType("0")); Assert.assertTrue(md.containsPayloadType("101")); Assert.assertTrue(md.containsPayloadType("180")); Assert.assertFalse(md.containsPayloadType("120")); }
@Test(expected=SdpException.class) public void testParseMissingMedia() throws SdpException { String line = "m=64534 RTP/AVPF 0 101 120\n\r"; parser.parse(line); }
@Test(expected=SdpException.class) public void testParseInvalidPortFormat() throws SdpException { String line = "m=64534 xyz RTP/AVPF 0 101 120\n\r"; parser.parse(line); }
@Ignore @Test(expected=SdpException.class) public void testParseMissingProtocol() throws SdpException { String line = "m=64534 63000 0 101 120\n\r"; parser.parse(line); }