From 00bf037003ea70870eee7127b8979f80f431a2bb Mon Sep 17 00:00:00 2001 From: emcho Date: Sun, 28 Feb 2010 09:19:48 +0000 Subject: [PATCH] Adds methods for prioritizing candidates and selecting a default one. --- src/org/ice4j/ice/Agent.java | 6 ++ src/org/ice4j/ice/Candidate.java | 28 ++++++++ src/org/ice4j/ice/Component.java | 112 ++++++++++++++++++++++++++++++- 3 files changed, 145 insertions(+), 1 deletion(-) diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index e0c22ed..6fd48f3 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -177,6 +177,12 @@ public class Agent //make sure we compute priorities only after we have all candidates component.prioritizeCandidates(); + + //eliminate redundant candidates + component.eliminateRedundantCandidates(); + + //select the candidate to put in the media line. + component.selectDefaultCandidate(); } /** diff --git a/src/org/ice4j/ice/Candidate.java b/src/org/ice4j/ice/Candidate.java index 2245b77..d338822 100644 --- a/src/org/ice4j/ice/Candidate.java +++ b/src/org/ice4j/ice/Candidate.java @@ -622,4 +622,32 @@ public abstract class Candidate return buff.toString(); } + /** + * Returns an integer indicating the preference that this Candidate + * should be considered with for becoming a default candidate. + * + * @return an integer indicating the preference that this Candidate + * should be considered with for becoming a default candidate. + */ + protected int getDefaultPreference() + { + if( getType() == CandidateType.RELAYED_CANDIDATE) + return 30; + + if( getType() == CandidateType.SERVER_REFLEXIVE_CANDIDATE) + return 20; + + if( getType() == CandidateType.HOST_CANDIDATE) + { + //prefer IPv4 as default since many servers would still freak out + //when seeing IPv6 address. + if(getTransportAddress().isIPv6()) + return 10; + else + return 15; + } + + //WTF? + return 5; + } } diff --git a/src/org/ice4j/ice/Component.java b/src/org/ice4j/ice/Component.java index 2033b03..4a92b97 100644 --- a/src/org/ice4j/ice/Component.java +++ b/src/org/ice4j/ice/Component.java @@ -8,6 +8,7 @@ package org.ice4j.ice; import java.util.*; +import java.util.logging.*; import org.ice4j.*; @@ -24,6 +25,12 @@ import org.ice4j.*; */ public class Component { + /** + * Our class logger. + */ + private static final Logger logger + = Logger.getLogger(Component.class.getName()); + /** * A component id is a positive integer between 1 and 256 which identifies * the specific component of the media stream for which this is a candidate. @@ -74,6 +81,12 @@ public class Component private CandidatePrioritizer candidatePrioritizer = new CandidatePrioritizer(); + /** + * The default Candidate for this component or in other words, the + * candidate that we would have used without ICE. + */ + private Candidate defaultCandidate = null; + /** * Creates a new Component with the specified componentID * as a child of the specified MediaStream. @@ -295,6 +308,8 @@ public class Component buff.append("\n" + cand.toString()); } } + + buff.append("\nDefault Candidate: " + getDefaultCandidate()); } else { @@ -328,7 +343,7 @@ public class Component * Computes the priorities of all Candidates and then sorts them * accordingly. */ - public void prioritizeCandidates() + protected void prioritizeCandidates() { synchronized(localCandidates) { @@ -407,4 +422,99 @@ public class Component return (obj instanceof CandidatePrioritizer); } } + + /** + * Eliminates redundant candidates, removing them from the specified + * component. A candidate is redundant if its transport address + * equals another candidate, and its base equals the base of that other + * candidate. Note that two candidates can have the same transport address + * yet have different bases, and these would not be considered redundant. + * Frequently, a server reflexive candidate and a host candidate will be + * redundant when the agent is not behind a NAT. The agent SHOULD eliminate + * the redundant candidate with the lower priority which is why we have to + * run this method only after prioritizing candidates. + */ + protected void eliminateRedundantCandidates() + { + List candidatesCopy = getLocalCandidates(); + + for (Candidate cand : candidatesCopy) + { + //find and remove all candidates that have the same address and + //base as cand and a lower priority. + synchronized(localCandidates) + { + Iterator localCandsIter = localCandidates.iterator(); + + while (localCandsIter.hasNext()) + { + Candidate cand2 = localCandsIter.next(); + + if( cand != cand2 + && cand.getTransportAddress() + .equals(cand2.getTransportAddress()) + && cand.getBase().equals(cand2.getBase()) + && cand.getPriority() >= cand2.getPriority() ) + { + localCandsIter.remove(); + if(logger.isLoggable(Level.FINEST)) + logger.finest("eliminating redundant cand: "+ cand2); + } + } + } + } + } + + /** + * Returns the Candidate that has been selected as the default + * for this Component or null if no such + * Candidate has been selected yet. A candidate is said to be + * default if it would be the target of media from a non-ICE peer; + * + * @return the Candidate that has been selected as the default for + * this Component or null if no such Candidate + * has been selected yet + */ + public Candidate getDefaultCandidate() + { + return defaultCandidate; + } + + /** + * Selects a Candidate that should be considered as the default + * for this Component. A candidate is said to be default if it + * would be the target of media from a non-ICE peer; + *

+ * The ICE specification RECOMMENDEDs that default candidates be chosen + * based on the likelihood of those candidates to work with the peer that is + * being contacted. It is RECOMMENDED that the default candidates are the + * relayed candidates (if relayed candidates are available), server + * reflexive candidates (if server reflexive candidates are available), and + * finally host candidates. + *

+ */ + protected void selectDefaultCandidate() + { + synchronized(localCandidates) + { + Iterator localCandsIter = localCandidates.iterator(); + + while (localCandsIter.hasNext()) + { + Candidate cand = localCandsIter.next(); + + if(this.defaultCandidate == null) + { + this.defaultCandidate = cand; + continue; + } + + if( defaultCandidate.getDefaultPreference() + < cand.getDefaultPreference()) + { + defaultCandidate = cand; + } + } + } + } }