diff --git a/lib/jain-sdp.jar b/lib/jain-sdp.jar index fd034e6..b61f636 100644 Binary files a/lib/jain-sdp.jar and b/lib/jain-sdp.jar differ diff --git a/src/org/ice4j/Transport.java b/src/org/ice4j/Transport.java index 852f835..05ad973 100644 --- a/src/org/ice4j/Transport.java +++ b/src/org/ice4j/Transport.java @@ -68,4 +68,38 @@ public enum Transport { return transportName; } + + /** + * Returns a Transport instance corresponding to the specified + * transportName. For example, for name "udp", this method + * would return {@link #UDP}. + * + * @param transportName the name that we'd like to parse. + * @return a Transport instance corresponding to the specified + * transportName. + * + * @throws IllegalArgumentException in case transportName is + * not a valid or currently supported transport. + */ + public static Transport parse(String transportName) + throws IllegalArgumentException + { + if(UDP.toString().equals(transportName)) + return UDP; + + if(TCP.toString().equals(transportName)) + return TCP; + + if(TLS.toString().equals(transportName)) + return TLS; + + if(SCTP.toString().equals(transportName)) + return SCTP; + + if(DTLS.toString().equals(transportName)) + return DTLS; + + throw new IllegalArgumentException( + transportName + " is not a currently supported Transport"); + } } diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index b191671..7c09b7e 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -331,6 +331,7 @@ public class Agent { synchronized(startLock) { + pruneNonMatchedStreams(); initCheckLists(); //change state before we actually send checks so that we don't @@ -340,6 +341,29 @@ public class Agent } } + /** + * Free()s and removes from this agent components or entire streams + * if they do not contain remote candidates. A possible reason for this + * could be the fact that the remote party canceled some of the streams. + */ + private void pruneNonMatchedStreams() + { + for (IceMediaStream stream : getStreams()) + { + for(Component cmp : stream.getComponents()) + { + if(cmp.getRemoteCandidateCount() == 0) + { + stream.removeComponent(cmp); + } + } + + if(stream.getComponentCount() == 0) + removeStream(stream); + } + + } + /** * Indicates whether this {@link Agent} is currently in the process of * running connectivity checks and establishing connectivity. Connectivity diff --git a/src/org/ice4j/ice/CandidateType.java b/src/org/ice4j/ice/CandidateType.java index c0ba658..9647dce 100644 --- a/src/org/ice4j/ice/CandidateType.java +++ b/src/org/ice4j/ice/CandidateType.java @@ -77,4 +77,35 @@ public enum CandidateType { return typeName; } + + /** + * Returns a CandidateType instance corresponding to the specified + * candidateTypeName. For example, for name "host", this method + * would return {@link #HOST_CANDIDATE}. + * + * @param candidateTypeName the name that we'd like to parse. + * @return a CandidateType instance corresponding to the specified + * candidateTypeName. + * + * @throws IllegalArgumentException in case candidateTypeName is + * not a valid or currently supported candidate type. + */ + public static CandidateType parse(String candidateTypeName) + throws IllegalArgumentException + { + if(PEER_REFLEXIVE_CANDIDATE.toString().equals(candidateTypeName)) + return PEER_REFLEXIVE_CANDIDATE; + + if(SERVER_REFLEXIVE_CANDIDATE.toString().equals(candidateTypeName)) + return SERVER_REFLEXIVE_CANDIDATE; + + if(RELAYED_CANDIDATE.toString().equals(candidateTypeName)) + return RELAYED_CANDIDATE; + + if(HOST_CANDIDATE.toString().equals(candidateTypeName)) + return HOST_CANDIDATE; + + throw new IllegalArgumentException( + candidateTypeName + " is not a currently supported CandidateType"); + } } diff --git a/src/org/ice4j/ice/RemoteCandidate.java b/src/org/ice4j/ice/RemoteCandidate.java index a907541..4f2ba33 100644 --- a/src/org/ice4j/ice/RemoteCandidate.java +++ b/src/org/ice4j/ice/RemoteCandidate.java @@ -19,6 +19,10 @@ import org.ice4j.*; public class RemoteCandidate extends Candidate { + /** + * The related address that the remote party sent in SDP, if it did. + */ + private TransportAddress raddr; /** * Creates a RemoteCandidate instance for the specified transport @@ -78,4 +82,36 @@ public class RemoteCandidate return equals(parentCmp.getDefaultRemoteCandidate()); } + + /** + * Returns the TransportAddress that the remote party indicated + * as "related" for this Candidate. + *

+ * Related addresses are present for server reflexive, peer reflexive and + * relayed candidates. If a candidate is server or peer reflexive, + * the related address is equal to the base of this Candidate. + * If the candidate is relayed, the returned address is equal to the mapped + * address. If the candidate is a host candidate then the method returns + * null. + * + * @return the TransportAddress that the remote party indicated + * as "related" for this Candidate or null if they didn't + * include it in SDP. + */ + public TransportAddress getRelatedAddress() + { + return raddr; + } + + /** + * Specifies the TransportAddress that the remote party indicated + * as "related" for this Candidate. + * + * @param relatedAddr the TransportAddress that the remote party + * indicated as "related" for this Candidate. + */ + public void setRelatedAddress(TransportAddress relatedAddr) + { + this.raddr = relatedAddr; + } } diff --git a/test/test/Ice.java b/test/test/Ice.java index 43237f9..c757cdb 100644 --- a/test/test/Ice.java +++ b/test/test/Ice.java @@ -8,8 +8,7 @@ package test; import java.beans.*; -import java.lang.reflect.*; -import java.net.*; +import java.io.*; import java.util.*; import org.ice4j.*; @@ -28,6 +27,14 @@ public class Ice */ static long startTime; + /** + * Runs a test application that allocates streams, generates an SDP, dumps + * it on stdout, waits for a remote peer SDP on stdin, then feeds that + * to our local agent and starts ICE processing. + * + * @param args none currently handled + * @throws Throwable every now and then. + */ public static void main(String[] args) throws Throwable { startTime = System.currentTimeMillis(); @@ -40,10 +47,48 @@ public class Ice //let them fight ... fights forge character. localAgent.setControlling(true); + String localSDP = SdpUtils.createSDPDescription(localAgent); + System.out.println(localSDP); - System.out.println(SdpUtils.createSDPDescription(localAgent)); + String sdp = readSDP(); + SdpUtils.parseSDP(localAgent, sdp); + System.out.println("Agent:"); + System.out.println(localAgent); + } + + /** + * Reads an SDP description from the standard input. We expect descriptions + * provided to this method to be originating from instances of this + * application running on remote computers. + * + * @return whatever we got on stdin (hopefully an SDP description. + * + * @throws Throwable if something goes wrong with console reading. + */ + private static String readSDP() throws Throwable + { + System.out.println("Paste SDP here. Enter an empty line to proceed:"); + System.out.println("(we don't mind the [java] prefix in SDP intput)"); + BufferedReader reader + = new BufferedReader(new InputStreamReader(System.in)); + + StringBuffer buff = new StringBuffer(); + String line = new String(); + + while ( (line = reader.readLine()) != null) + { + line = line.replace("[java]", ""); + line = line.trim(); + if(line.length() == 0) + break; + + buff.append(line); + buff.append("\r\n"); + } + + return buff.toString(); } /** diff --git a/test/test/SdpUtils.java b/test/test/SdpUtils.java index afcb3ff..0c03a32 100644 --- a/test/test/SdpUtils.java +++ b/test/test/SdpUtils.java @@ -10,11 +10,15 @@ import gov.nist.core.*; import gov.nist.javax.sdp.fields.*; import java.util.*; +import java.util.StringTokenizer; import javax.sdp.*; import org.ice4j.*; import org.ice4j.ice.*; +import org.ice4j.ice.Agent; + +import sun.management.*; /** * Utilities for manipulating SDP. The utilities This method do not try @@ -100,11 +104,162 @@ public class SdpUtils return sdess.toString(); } + /** + * Configures localAgent the the remote peer streams, components, + * and candidates specified in sdp + * + * @param localAgent the {@link Agent} that we'd like to configure. + * + * @param sdp the SDP string that the remote peer sent. + * + * @throws Exception for all sorts of reasons. + */ + @SuppressWarnings("unchecked") // jain-sdp legacy code. + public static void parseSDP(Agent localAgent, String sdp) + throws Exception + { + SdpFactory factory = SdpFactory.getInstance(); + SessionDescription sdess = factory.createSessionDescription(sdp); + + localAgent.setRemotePassword(sdess.getAttribute("ice-pwd")); + localAgent.setRemoteUfrag(sdess.getAttribute("ice-ufrag")); + + Connection globalConn = sdess.getConnection(); + String globalConnAddr = null; + if(globalConn != null) + globalConnAddr = globalConn.getAddress(); + + Vector mdescs = sdess.getMediaDescriptions(true); + + for (MediaDescription desc : mdescs) + { + String streamName = desc.getMedia().getMediaType(); + + IceMediaStream stream = localAgent.getStream(streamName); + + if(stream == null) + continue; + + Vector attributes = desc.getAttributes(true); + for( Attribute attribute : attributes) + { + if(!attribute.getName().equals(CandidateAttribute.NAME)) + continue; + + parseCandidate(attribute, stream); + } + + //set default candidates + Connection streamConn = desc.getConnection(); + String streamConnAddr = null; + if(streamConn != null) + streamConnAddr = streamConn.getAddress(); + else + streamConnAddr = globalConnAddr; + + int port = desc.getMedia().getMediaPort(); + + TransportAddress defaultRtpAddress = + new TransportAddress(streamConnAddr, port, Transport.UDP); + + int rtcpPort = port + 1; + String rtcpAttributeValue = desc.getAttribute("rtcp"); + + if (rtcpAttributeValue != null) + rtcpPort = Integer.parseInt(rtcpAttributeValue); + + TransportAddress defaultRtcpAddress = + new TransportAddress(streamConnAddr, rtcpPort, Transport.UDP); + + Component rtpComponent = stream.getComponent(Component.RTP); + Component rtcpComponent = stream.getComponent(Component.RTCP); + + Candidate defaultRtpCandidate + = rtpComponent.findRemoteCandidate(defaultRtpAddress); + Candidate defaultRtcpCandidate + = rtcpComponent.findRemoteCandidate(defaultRtcpAddress); + + rtpComponent.setDefaultRemoteCandidate(defaultRtpCandidate); + rtcpComponent.setDefaultRemoteCandidate(defaultRtcpCandidate); + } + } + + /** + * Parses the attribute. + * + * @param attribute the attribute that we need to parse. + * @param stream the {@link IceMediaStream} that the candidate is supposed + * to belong to. + * + * @return a newly created {@link RemoteCandidate} matching the + * content of the specified attribute or null if the + * candidate belonged to a component we don't have. + */ + private static RemoteCandidate parseCandidate(Attribute attribute, + IceMediaStream stream) + { + String value = null; + + try{ + value = attribute.getValue(); + }catch (Throwable t){}//can't happen + + StringTokenizer tokenizer = new StringTokenizer(value); + + //XXX add exception handling. + String foundation = tokenizer.nextToken(); + int componentID = Integer.parseInt( tokenizer.nextToken() ); + Transport transport = Transport.parse(tokenizer.nextToken()); + long priority = Long.parseLong(tokenizer.nextToken()); + String address = tokenizer.nextToken(); + int port = Integer.parseInt(tokenizer.nextToken()); + + TransportAddress transAddr + = new TransportAddress(address, port, transport); + + tokenizer.nextToken(); //skip the "typ" String + CandidateType type = CandidateType.parse(tokenizer.nextToken()); + + Component component = stream.getComponent(componentID); + + if(component == null) + return null; + + RemoteCandidate cand = new RemoteCandidate(transAddr, component, type, + foundation, priority); + + component.addRemoteCandidate(cand); + + // check if there's a related address property + + if (tokenizer.countTokens() >= 4) + { + tokenizer.nextToken(); // skip the raddr element + String relatedAddr = tokenizer.nextToken(); + tokenizer.nextToken(); // skip the rport element + int relatedPort = Integer.parseInt(tokenizer.nextToken()); + + TransportAddress raddr = new TransportAddress( + relatedAddr, relatedPort, Transport.UDP); + + cand.setRelatedAddress(raddr); + } + + return cand; + } + + + /** * An implementation of the candidate SDP attribute. */ private static class CandidateAttribute extends AttributeField { + /** + * The SDP name of candidate attributes. + */ + public static final String NAME = "candidate"; + /** * This class's serial version uid. */ @@ -131,7 +286,7 @@ public class SdpUtils */ public String getName() { - return "candidate"; + return NAME; } /**