From e90c796d6d3184a7faf4a997465ce5d21e4cd809 Mon Sep 17 00:00:00 2001 From: Jonathan Lennox Date: Wed, 8 Apr 2020 12:48:48 -0400 Subject: [PATCH] Merge duplicate ICE candidate pairs. (#195) This can happen if a remote candidate is discovered as peer-reflexive and later received over signaling. --- src/main/java/org/ice4j/ice/CheckList.java | 19 +++++++++++++++++++ src/main/java/org/ice4j/ice/Component.java | 21 ++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/ice4j/ice/CheckList.java b/src/main/java/org/ice4j/ice/CheckList.java index cdfb4e0..4373929 100644 --- a/src/main/java/org/ice4j/ice/CheckList.java +++ b/src/main/java/org/ice4j/ice/CheckList.java @@ -209,6 +209,25 @@ public class CheckList return highestPriorityPair; //return even if null } + /** + * Returns a pair which matches the specified local and remote candidates' + * transport addresses, if any, otherwise null. + */ + public synchronized CandidatePair findPairMatching(LocalCandidate local, RemoteCandidate remote) + { + for (CandidatePair pair : this) + { + if (pair.getLocalCandidate() == local || + pair.getLocalCandidate().getTransportAddress().equals(local.getTransportAddress())) + { + if (pair.getRemoteCandidate() == remote || + pair.getRemoteCandidate().getTransportAddress().equals(remote.getTransportAddress())) + return pair; + } + } + return null; + } + /** * Determines whether this CheckList can be considered active. * RFC 5245 says: A check list with at least one pair that is Waiting is diff --git a/src/main/java/org/ice4j/ice/Component.java b/src/main/java/org/ice4j/ice/Component.java index 687e996..72e65f8 100644 --- a/src/main/java/org/ice4j/ice/Component.java +++ b/src/main/java/org/ice4j/ice/Component.java @@ -437,8 +437,6 @@ public class Component = getParentStream() .getParentAgent() .createCandidatePair(localCnd, remoteCnd); - logger.info("new Pair added: " + pair.toShortString() - + "."); checkList.add(pair); } } @@ -465,7 +463,24 @@ public class Component { for (CandidatePair pair : checkList) { - streamCheckList.add(pair); + /* Check whether the pair is already in the check list. + * (This can happen for pairs with remote peer-reflexive candidates, + * since those candidates aren't added to the candidate list even + * though the pair is added to the check list). + */ + CandidatePair existingPair = streamCheckList.findPairMatching(pair.getLocalCandidate(), pair.getRemoteCandidate()); + if (existingPair != null) { + logger.info("existing Pair updated: " + + existingPair.toShortString() + + " to " + pair.toShortString() + "."); + existingPair.setRemoteCandidate(pair.getRemoteCandidate()); + existingPair.computePriority(); + } + else { + streamCheckList.add(pair); + logger.info("new Pair added: " + pair.toShortString() + + "."); + } } } }