SdpFactory { public static SessionDescription buildSdp(boolean offer, String localAddress, String externalAddress, MediaChannel... channels) { SessionDescription sd = new SessionDescription(); sd.setVersion(new VersionField((short) 0)); String originAddress = (externalAddress == null || externalAddress.isEmpty()) ? localAddress : externalAddress; sd.setOrigin(new OriginField("-", String.valueOf(System.currentTimeMillis()), "1", "IN", "IP4", originAddress)); sd.setSessionName(new SessionNameField("Mobicents Media Server")); sd.setConnection(new ConnectionField("IN", "IP4", originAddress)); sd.setTiming(new TimingField(0, 0)); boolean ice = false; for (MediaChannel channel : channels) { MediaDescriptionField md = buildMediaDescription(channel, offer); md.setSession(sd); sd.addMediaDescription(md); if(md.containsIce()) { sd.getConnection().setAddress(md.getConnection().getAddress()); ice = true; } } if(ice) { sd.setIceLite(new IceLiteAttribute()); } return sd; }  static SessionDescription buildSdp(boolean offer, String localAddress, String externalAddress, MediaChannel... channels); static void rejectMediaField(SessionDescription answer, MediaDescriptionField media); static MediaDescriptionField buildMediaDescription(MediaChannel channel, boolean offer);  }
@Test public void testValidExternalAddress() { AudioChannel audioChannel = mock(AudioChannel.class); when(audioChannel.getExternalAddress()).thenReturn(this.externalAddress); when(audioChannel.getRtpAddress()).thenReturn(this.rtpAddress); when(audioChannel.getRtcpAddress()).thenReturn(this.rtcpAddress); when(audioChannel.isIceEnabled()).thenReturn(false); when(audioChannel.isRtcpMux()).thenReturn(false); when(audioChannel.getFormats()).thenReturn(this.rtpFormats); when(audioChannel.getMediaType()).thenReturn(AudioChannel.MEDIA_TYPE); SessionDescription sd = SdpFactory.buildSdp(true, this.localAddress, this.externalAddress, audioChannel); Assert.assertTrue(sd.getConnection().getAddress().equals(this.externalAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getConnection().getAddress().equals(this.externalAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getRtcp().getAddress().equals(this.externalAddress)); }
@Test public void testValidExternalAddressWithIceRtcpMux() { AudioChannel audioChannel = mock(AudioChannel.class); when(audioChannel.getExternalAddress()).thenReturn(this.externalAddress); when(audioChannel.getRtpAddress()).thenReturn(this.rtpAddress); when(audioChannel.getRtcpAddress()).thenReturn(this.rtcpAddress); when(audioChannel.isIceEnabled()).thenReturn(true); when(audioChannel.isRtcpMux()).thenReturn(true); when(audioChannel.getFormats()).thenReturn(this.rtpFormats); when(audioChannel.getMediaType()).thenReturn(AudioChannel.MEDIA_TYPE); SessionDescription sd = SdpFactory.buildSdp(true, this.localAddress, this.externalAddress, audioChannel); Assert.assertTrue(sd.getConnection().getAddress().equals(this.externalAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getConnection().getAddress().equals(this.externalAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getRtcp().getAddress().equals(this.externalAddress)); }
@Test public void testEmptyExternallAddress() { AudioChannel audioChannel = mock(AudioChannel.class); when(audioChannel.getExternalAddress()).thenReturn(""); when(audioChannel.getRtpAddress()).thenReturn(this.rtpAddress); when(audioChannel.getRtcpAddress()).thenReturn(this.rtcpAddress); when(audioChannel.isIceEnabled()).thenReturn(false); when(audioChannel.isRtcpMux()).thenReturn(false); when(audioChannel.getFormats()).thenReturn(this.rtpFormats); when(audioChannel.getMediaType()).thenReturn(AudioChannel.MEDIA_TYPE); SessionDescription sd = SdpFactory.buildSdp(true, this.localAddress, "", audioChannel); Assert.assertTrue(sd.getConnection().getAddress().equals(this.localAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getConnection().getAddress().equals(this.rtpAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getRtcp().getAddress().equals(this.rtcpAddress)); }
@Test public void testEmptyExternalAddressWithIceRtcpMux() { AudioChannel audioChannel = mock(AudioChannel.class); when(audioChannel.getExternalAddress()).thenReturn(""); when(audioChannel.getRtpAddress()).thenReturn(this.rtpAddress); when(audioChannel.getRtcpAddress()).thenReturn(this.rtcpAddress); when(audioChannel.isIceEnabled()).thenReturn(true); when(audioChannel.isRtcpMux()).thenReturn(true); when(audioChannel.getFormats()).thenReturn(this.rtpFormats); when(audioChannel.getMediaType()).thenReturn(AudioChannel.MEDIA_TYPE); SessionDescription sd = SdpFactory.buildSdp(true, this.localAddress, "", audioChannel); Assert.assertTrue(sd.getConnection().getAddress().equals(this.rtpAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getConnection().getAddress().equals(this.rtpAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getRtcp().getAddress().equals(this.rtpAddress)); }
@Test public void testNullExternalAddress() { AudioChannel audioChannel = mock(AudioChannel.class); when(audioChannel.getExternalAddress()).thenReturn(null); when(audioChannel.getRtpAddress()).thenReturn(this.rtpAddress); when(audioChannel.getRtcpAddress()).thenReturn(this.rtcpAddress); when(audioChannel.isIceEnabled()).thenReturn(false); when(audioChannel.isRtcpMux()).thenReturn(false); when(audioChannel.getFormats()).thenReturn(this.rtpFormats); when(audioChannel.getMediaType()).thenReturn(AudioChannel.MEDIA_TYPE); SessionDescription sd = SdpFactory.buildSdp(true, this.localAddress, null, audioChannel); Assert.assertTrue(sd.getConnection().getAddress().equals(this.localAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getConnection().getAddress().equals(this.rtpAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getRtcp().getAddress().equals(this.rtcpAddress)); }
@Test public void testNullExternalAddressWithIceRtcpMux() { AudioChannel audioChannel = mock(AudioChannel.class); when(audioChannel.getExternalAddress()).thenReturn(null); when(audioChannel.getRtpAddress()).thenReturn(this.rtpAddress); when(audioChannel.getRtcpAddress()).thenReturn(this.rtcpAddress); when(audioChannel.isIceEnabled()).thenReturn(true); when(audioChannel.isRtcpMux()).thenReturn(true); when(audioChannel.getFormats()).thenReturn(this.rtpFormats); when(audioChannel.getMediaType()).thenReturn(AudioChannel.MEDIA_TYPE); SessionDescription sd = SdpFactory.buildSdp(true, this.localAddress, null, audioChannel); Assert.assertTrue(sd.getConnection().getAddress().equals(this.rtpAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getConnection().getAddress().equals(this.rtpAddress)); Assert.assertTrue(sd.getMediaDescription("audio").getRtcp().getAddress().equals(this.rtpAddress)); }