openvidu-server: Reduce number of candidates when MEDIA_NODES_PUBLIC_IPS is defined

pull/648/head
cruizba 2021-07-08 17:49:52 +02:00
parent 9fd690559e
commit 3265bf401d
2 changed files with 41 additions and 48 deletions

View File

@ -17,8 +17,6 @@
package io.openvidu.server.kurento.endpoint;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
@ -576,57 +574,61 @@ public abstract class MediaEndpoint {
if (this.openviduConfig.areMediaNodesPublicIpsDefined()) {
sendCandidatesWithConfiguredIp(senderPublicId, candidate);
} else {
gatheredCandidateList.add(candidate);
this.owner.logIceCandidate(new WebrtcDebugEvent(this.owner, this.streamId, WebrtcDebugEventIssuer.server,
this.getWebrtcDebugOperation(), WebrtcDebugEventType.iceCandidate,
gson.toJsonTree(candidate).toString()));
owner.sendIceCandidate(senderPublicId, endpointName, candidate);
sendCandidate(senderPublicId, candidate);
}
});
}
private void sendCandidatesWithConfiguredIp(String senderPublicId, IceCandidate candidate) {
// Get media node private IP
String kurentoPrivateIp = this.owner.getSession().getKms().getIp();
try {
// Get media node private IP
String kurentoPrivateIp = this.owner.getSession().getKms().getIp();
// Get Ip to be replaced
String ipToReplace = this.openviduConfig.getMediaNodesPublicIpsMap().get(kurentoPrivateIp);
// Get Ip to be replaced
String ipToReplace = this.openviduConfig.getMediaNodesPublicIpsMap().get(kurentoPrivateIp);
// If Ip is configured
if (ipToReplace != null && !ipToReplace.isEmpty()) {
// Create IceCandidateParser to modify original candidate information
IceCandidateDataParser candidateParser = new IceCandidateDataParser(candidate);
// If Ip is configured
if (ipToReplace != null && !ipToReplace.isEmpty()) {
// Create IceCandidateParser to modify original candidate information
IceCandidateDataParser candidateParser = new IceCandidateDataParser(candidate);
// Only create host candidates to increase priority
if (candidateParser.getType() == IceCandidateType.host) {
String originalIP = candidateParser.getIp();
// Send candidate with new configured IP
// get original IP
String originalIp = candidateParser.getIp();
// Replace all candidates with with new configured IP
IceCandidate candidateMaxPriority = new IceCandidate(candidate.getCandidate(), candidate.getSdpMid(),
candidate.getSdpMLineIndex());
candidateParser.setIp(ipToReplace);
candidateParser.setMaxPriority(); // Set max priority for this candidate
candidateParser.setMaxPriority();
candidateMaxPriority.setCandidate(candidateParser.toString());
gatheredCandidateList.add(candidateMaxPriority);
this.owner.logIceCandidate(new WebrtcDebugEvent(this.owner, this.streamId, WebrtcDebugEventIssuer.server,
this.getWebrtcDebugOperation(), WebrtcDebugEventType.iceCandidate,
gson.toJsonTree(candidateMaxPriority).toString()));
owner.sendIceCandidate(senderPublicId, endpointName, candidateMaxPriority);
sendCandidate(senderPublicId, candidateMaxPriority);
// Send candidate with original IP
IceCandidate candidateMinPriority = new IceCandidate(candidate.getCandidate(), candidate.getSdpMid(),
candidate.getSdpMLineIndex());
candidateParser.setIp(originalIP);
candidateParser.setMinPriority(); // Set min priority for private IP
candidateMinPriority.setCandidate(candidateParser.toString());
gatheredCandidateList.add(candidateMinPriority);
this.owner.logIceCandidate(new WebrtcDebugEvent(this.owner, this.streamId, WebrtcDebugEventIssuer.server,
this.getWebrtcDebugOperation(), WebrtcDebugEventType.iceCandidate,
gson.toJsonTree(candidateMinPriority).toString()));
owner.sendIceCandidate(senderPublicId, endpointName, candidateMinPriority);
// Resend old public IP next to the new one
if (candidateParser.isType(IceCandidateType.srflx)) {
// Send candidate with private ip
IceCandidate candidateMinPriority = new IceCandidate(candidate.getCandidate(), candidate.getSdpMid(),
candidate.getSdpMLineIndex());
candidateParser.setIp(originalIp);
candidateParser.setMinPriority(); // Set min priority for original public IP
candidateMinPriority.setCandidate(candidateParser.toString());
sendCandidate(senderPublicId, candidateMinPriority);
}
}
} catch (Exception e) {
log.error("Error on adding additional IP in candidates: {}", e.getMessage());
// On Exception, send candidate without any modification
sendCandidate(senderPublicId, candidate);
}
}
private void sendCandidate(String senderPublicId, IceCandidate candidate) {
gatheredCandidateList.add(candidate);
this.owner.logIceCandidate(new WebrtcDebugEvent(this.owner, this.streamId, WebrtcDebugEventIssuer.server,
this.getWebrtcDebugOperation(), WebrtcDebugEventType.iceCandidate,
gson.toJsonTree(candidate).toString()));
owner.sendIceCandidate(senderPublicId, endpointName, candidate);
}
/**
* If supported, it instructs the internal endpoint to start gathering
* {@link IceCandidate}s.

View File

@ -2,7 +2,6 @@ package io.openvidu.server.utils.ice;
import org.kurento.client.IceCandidate;
import java.security.SecureRandom;
import java.util.Objects;
/**
@ -14,10 +13,11 @@ public class IceCandidateDataParser {
* Max priority and Min priority possible defined in rfc5245 15.1
* "<priority>: is a positive integer between 1 and (2**31 - 1)"
* MAX_PRIORITY = (2^24)*126 + (2^8)*65535 + 255
* MIN_PRIORITY = (2^24)*126 + (2^8)*1 + 255
* MIN_PRIORITY = (2^24)*1 + (2^8)*1 + 255
*/
private final int MAX_PRIORITY = 2130706431;
private final int MIN_PRIORITY = 511;
private final int MIN_PRIORITY = 16777727;
/**
* Full string with the candidate
@ -32,15 +32,6 @@ public class IceCandidateDataParser {
this.candidate = iceCandidate.split(" ");
}
/**
* Following rfc5245, section-15.1, the candidate foundation id is the 1 th element
* @return
*/
public void setRandomFoundation() {
String prefix = candidate[0].split(":")[0];
candidate[0] = prefix + ":" + new SecureRandom().nextInt(Integer.MAX_VALUE - 1);
}
/**
* Following rfc5245, section-15.1, the priority is the 4th element
* @return The priority of the candidate