Adds methods for prioritizing candidates and selecting a default one.
This commit is contained in:
parent
15e239de88
commit
00bf037003
@ -177,6 +177,12 @@ public class Agent
|
|||||||
|
|
||||||
//make sure we compute priorities only after we have all candidates
|
//make sure we compute priorities only after we have all candidates
|
||||||
component.prioritizeCandidates();
|
component.prioritizeCandidates();
|
||||||
|
|
||||||
|
//eliminate redundant candidates
|
||||||
|
component.eliminateRedundantCandidates();
|
||||||
|
|
||||||
|
//select the candidate to put in the media line.
|
||||||
|
component.selectDefaultCandidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -622,4 +622,32 @@ public abstract class Candidate
|
|||||||
return buff.toString();
|
return buff.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an integer indicating the preference that this <tt>Candidate</tt>
|
||||||
|
* should be considered with for becoming a default candidate.
|
||||||
|
*
|
||||||
|
* @return an integer indicating the preference that this <tt>Candidate</tt>
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
package org.ice4j.ice;
|
package org.ice4j.ice;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.logging.*;
|
||||||
|
|
||||||
import org.ice4j.*;
|
import org.ice4j.*;
|
||||||
|
|
||||||
@ -24,6 +25,12 @@ import org.ice4j.*;
|
|||||||
*/
|
*/
|
||||||
public class Component
|
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
|
* 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.
|
* the specific component of the media stream for which this is a candidate.
|
||||||
@ -74,6 +81,12 @@ public class Component
|
|||||||
private CandidatePrioritizer candidatePrioritizer
|
private CandidatePrioritizer candidatePrioritizer
|
||||||
= new CandidatePrioritizer();
|
= new CandidatePrioritizer();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default <tt>Candidate</tt> for this component or in other words, the
|
||||||
|
* candidate that we would have used without ICE.
|
||||||
|
*/
|
||||||
|
private Candidate defaultCandidate = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new <tt>Component</tt> with the specified <tt>componentID</tt>
|
* Creates a new <tt>Component</tt> with the specified <tt>componentID</tt>
|
||||||
* as a child of the specified <tt>MediaStream</tt>.
|
* as a child of the specified <tt>MediaStream</tt>.
|
||||||
@ -295,6 +308,8 @@ public class Component
|
|||||||
buff.append("\n" + cand.toString());
|
buff.append("\n" + cand.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buff.append("\nDefault Candidate: " + getDefaultCandidate());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -328,7 +343,7 @@ public class Component
|
|||||||
* Computes the priorities of all <tt>Candidate</tt>s and then sorts them
|
* Computes the priorities of all <tt>Candidate</tt>s and then sorts them
|
||||||
* accordingly.
|
* accordingly.
|
||||||
*/
|
*/
|
||||||
public void prioritizeCandidates()
|
protected void prioritizeCandidates()
|
||||||
{
|
{
|
||||||
synchronized(localCandidates)
|
synchronized(localCandidates)
|
||||||
{
|
{
|
||||||
@ -407,4 +422,99 @@ public class Component
|
|||||||
return (obj instanceof CandidatePrioritizer);
|
return (obj instanceof CandidatePrioritizer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Eliminates redundant candidates, removing them from the specified
|
||||||
|
* <tt>component</tt>. 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<Candidate> 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<Candidate> 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 <tt>Candidate</tt> that has been selected as the default
|
||||||
|
* for this <tt>Component</tt> or <tt>null</tt> if no such
|
||||||
|
* <tt>Candidate</tt> 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 <tt>Candidate</tt> that has been selected as the default for
|
||||||
|
* this <tt>Component</tt> or <tt>null</tt> if no such <tt>Candidate</tt>
|
||||||
|
* has been selected yet
|
||||||
|
*/
|
||||||
|
public Candidate getDefaultCandidate()
|
||||||
|
{
|
||||||
|
return defaultCandidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Selects a <tt>Candidate</tt> that should be considered as the default
|
||||||
|
* for this <tt>Component</tt>. A candidate is said to be default if it
|
||||||
|
* would be the target of media from a non-ICE peer;
|
||||||
|
* <p>
|
||||||
|
* 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.
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
protected void selectDefaultCandidate()
|
||||||
|
{
|
||||||
|
synchronized(localCandidates)
|
||||||
|
{
|
||||||
|
Iterator<Candidate> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user