Adds a testing utility that allows running the ICE sample over two distinct stations.

This commit is contained in:
emcho 2010-05-18 07:24:23 +00:00
parent 69805233d9
commit 7c1781eb7d
7 changed files with 329 additions and 4 deletions

Binary file not shown.

View File

@ -68,4 +68,38 @@ public enum Transport
{ {
return transportName; return transportName;
} }
/**
* Returns a <tt>Transport</tt> instance corresponding to the specified
* <tt>transportName</tt>. For example, for name "udp", this method
* would return {@link #UDP}.
*
* @param transportName the name that we'd like to parse.
* @return a <tt>Transport</tt> instance corresponding to the specified
* <tt>transportName</tt>.
*
* @throws IllegalArgumentException in case <tt>transportName</tt> 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");
}
} }

View File

@ -331,6 +331,7 @@ public class Agent
{ {
synchronized(startLock) synchronized(startLock)
{ {
pruneNonMatchedStreams();
initCheckLists(); initCheckLists();
//change state before we actually send checks so that we don't //change state before we actually send checks so that we don't
@ -340,6 +341,29 @@ public class Agent
} }
} }
/**
* <tt>Free()</tt>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 * Indicates whether this {@link Agent} is currently in the process of
* running connectivity checks and establishing connectivity. Connectivity * running connectivity checks and establishing connectivity. Connectivity

View File

@ -77,4 +77,35 @@ public enum CandidateType
{ {
return typeName; return typeName;
} }
/**
* Returns a <tt>CandidateType</tt> instance corresponding to the specified
* <tt>candidateTypeName</tt>. For example, for name "host", this method
* would return {@link #HOST_CANDIDATE}.
*
* @param candidateTypeName the name that we'd like to parse.
* @return a <tt>CandidateType</tt> instance corresponding to the specified
* <tt>candidateTypeName</tt>.
*
* @throws IllegalArgumentException in case <tt>candidateTypeName</tt> 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");
}
} }

View File

@ -19,6 +19,10 @@ import org.ice4j.*;
public class RemoteCandidate public class RemoteCandidate
extends Candidate extends Candidate
{ {
/**
* The related address that the remote party sent in SDP, if it did.
*/
private TransportAddress raddr;
/** /**
* Creates a <tt>RemoteCandidate</tt> instance for the specified transport * Creates a <tt>RemoteCandidate</tt> instance for the specified transport
@ -78,4 +82,36 @@ public class RemoteCandidate
return equals(parentCmp.getDefaultRemoteCandidate()); return equals(parentCmp.getDefaultRemoteCandidate());
} }
/**
* Returns the <tt>TransportAddress</tt> that the remote party indicated
* as "related" for this <tt>Candidate</tt>.
* <p>
* 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 <tt>Candidate</tt>.
* 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
* <tt>null</tt>.
*
* @return the <tt>TransportAddress</tt> that the remote party indicated
* as "related" for this <tt>Candidate</tt> or <tt>null</tt> if they didn't
* include it in SDP.
*/
public TransportAddress getRelatedAddress()
{
return raddr;
}
/**
* Specifies the <tt>TransportAddress</tt> that the remote party indicated
* as "related" for this <tt>Candidate</tt>.
*
* @param relatedAddr the <tt>TransportAddress</tt> that the remote party
* indicated as "related" for this <tt>Candidate</tt>.
*/
public void setRelatedAddress(TransportAddress relatedAddr)
{
this.raddr = relatedAddr;
}
} }

View File

@ -8,8 +8,7 @@
package test; package test;
import java.beans.*; import java.beans.*;
import java.lang.reflect.*; import java.io.*;
import java.net.*;
import java.util.*; import java.util.*;
import org.ice4j.*; import org.ice4j.*;
@ -28,6 +27,14 @@ public class Ice
*/ */
static long startTime; 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 public static void main(String[] args) throws Throwable
{ {
startTime = System.currentTimeMillis(); startTime = System.currentTimeMillis();
@ -40,10 +47,48 @@ public class Ice
//let them fight ... fights forge character. //let them fight ... fights forge character.
localAgent.setControlling(true); 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();
} }
/** /**

View File

@ -10,11 +10,15 @@ import gov.nist.core.*;
import gov.nist.javax.sdp.fields.*; import gov.nist.javax.sdp.fields.*;
import java.util.*; import java.util.*;
import java.util.StringTokenizer;
import javax.sdp.*; import javax.sdp.*;
import org.ice4j.*; import org.ice4j.*;
import org.ice4j.ice.*; import org.ice4j.ice.*;
import org.ice4j.ice.Agent;
import sun.management.*;
/** /**
* Utilities for manipulating SDP. The utilities This method <b>do not</b> try * Utilities for manipulating SDP. The utilities This method <b>do not</b> try
@ -100,11 +104,162 @@ public class SdpUtils
return sdess.toString(); return sdess.toString();
} }
/**
* Configures <tt>localAgent</tt> the the remote peer streams, components,
* and candidates specified in <tt>sdp</tt>
*
* @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<MediaDescription> mdescs = sdess.getMediaDescriptions(true);
for (MediaDescription desc : mdescs)
{
String streamName = desc.getMedia().getMediaType();
IceMediaStream stream = localAgent.getStream(streamName);
if(stream == null)
continue;
Vector<Attribute> 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 <tt>attribute</tt>.
*
* @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 <tt>attribute</tt> or <tt>null</tt> 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 <tt>candidate</tt> SDP attribute. * An implementation of the <tt>candidate</tt> SDP attribute.
*/ */
private static class CandidateAttribute extends AttributeField private static class CandidateAttribute extends AttributeField
{ {
/**
* The SDP name of candidate attributes.
*/
public static final String NAME = "candidate";
/** /**
* This class's serial version uid. * This class's serial version uid.
*/ */
@ -131,7 +286,7 @@ public class SdpUtils
*/ */
public String getName() public String getName()
{ {
return "candidate"; return NAME;
} }
/** /**