diff --git a/src/main/java/org/ice4j/StackProperties.java b/src/main/java/org/ice4j/StackProperties.java index da5b11d..13fe0b5 100644 --- a/src/main/java/org/ice4j/StackProperties.java +++ b/src/main/java/org/ice4j/StackProperties.java @@ -232,6 +232,19 @@ public class StackProperties public static final String USE_DYNAMIC_HOST_HARVESTER = "org.ice4j.ice.harvest.USE_DYNAMIC_HOST_HARVESTER"; + /** + * The name of the property which specifies whether the virtual socket + * layer should be enabled. + * WARNING: enabling the virtual socket layer will break applications which + * use sockets from specific {@code CandidatePair}s in subtle and + * potentially hard to debug ways. This should only be used by applications + * that specifically want to use the virtual socket layer (i.e. use the + * socket instance provided by the {@code Component}, and not a specific + * {@code CandidatePair}). + */ + public static final String ENABLE_VIRTUAL_SOCKET_LAYER + = "org.ice4j.ice.harvest.ENABLE_VIRTUAL_SOCKET_LAYER"; + /** * Returns the String value of the specified property (minus all * encompassing whitespaces)and null in case no property value was mapped diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java index 09ebef7..1ece7ae 100644 --- a/src/main/java/org/ice4j/ice/Agent.java +++ b/src/main/java/org/ice4j/ice/Agent.java @@ -114,6 +114,15 @@ public class Agent public static final String PROPERTY_ICE_PROCESSING_STATE = "IceProcessingState"; + /** + * The flag which indicates whether the virtual socket layer should be + * enabled or not. + */ + public static boolean ENABLE_VIRTUAL_SOCKET_LAYER + = StackProperties.getBoolean( + StackProperties.ENABLE_VIRTUAL_SOCKET_LAYER, + false); + /** * The LinkedHashMap used to store the media streams * This map preserves the insertion order of the media streams. @@ -373,6 +382,12 @@ public class Agent { addCandidateHarvester(harvester); } + + if (logger.isLoggable(Level.FINE)) + { + logger.fine("Created a new Agent, ufrag=" + ufrag + + ", VSL=" + ENABLE_VIRTUAL_SOCKET_LAYER); + } } /** diff --git a/src/main/java/org/ice4j/ice/Component.java b/src/main/java/org/ice4j/ice/Component.java index ad45f11..e268ab4 100644 --- a/src/main/java/org/ice4j/ice/Component.java +++ b/src/main/java/org/ice4j/ice/Component.java @@ -17,10 +17,12 @@ */ package org.ice4j.ice; +import java.net.*; import java.util.*; import java.util.logging.*; import org.ice4j.*; +import org.ice4j.socket.*; import org.ice4j.util.Logger; //Disambiguation /** @@ -121,6 +123,20 @@ public class Component */ private final Logger logger; + /** + * The {@link MergingDatagramSocket} instance which, if the virtual socket + * layer is enabled (see + * {@link StackProperties#ENABLE_VIRTUAL_SOCKET_LAYER}), will serve as the + * single socket instance for this {@link Component}, merging received + * packets from all of its candidates. + */ + private final MergingDatagramSocket mergingDatagramSocket; + + /** + * A wrapper around {@link #mergingDatagramSocket}. + */ + private final IceUdpSocketWrapper mergingDatagramSocketWrapper; + /** * Creates a new Component with the specified componentID * as a child of the specified IceMediaStream. @@ -136,6 +152,27 @@ public class Component this.componentID = componentID; this.parentStream = mediaStream; + if (Agent.ENABLE_VIRTUAL_SOCKET_LAYER) + { + try + { + mergingDatagramSocket = new MergingDatagramSocket(); + mergingDatagramSocketWrapper + = new IceUdpSocketWrapper( + new MultiplexingDatagramSocket( + mergingDatagramSocket)); + } + catch (SocketException se) + { + throw new RuntimeException(se); + } + } + else + { + mergingDatagramSocket = null; + mergingDatagramSocketWrapper = null; + } + logger = new Logger(classLogger, mediaStream.getParentAgent().getLogger()); } @@ -911,4 +948,29 @@ public class Component return new Component(componentID, mediaStream); } + /** + * @return the single socket for this {@link Component} which should be + * used for reading and writing data, if the virtual socket layer is enabled + * (see {@link StackProperties#ENABLE_VIRTUAL_SOCKET_LAYER}), and + * {@code null} if it is not enabled. + */ + public MergingDatagramSocket getSocket() + { + return mergingDatagramSocket; + } + + /** + * @return an {@link IceSocketWrapper} instance wrapping the socket for this + * candidate (see {@link #getSocket()}), if the virtual socket layer is + * enabled (see {@link StackProperties#ENABLE_VIRTUAL_SOCKET_LAYER}), or + * {@code null} if it is not enabled. + * @deprecated Use {@link #getSocket()} directly. This is only introduced + * to ease the transition of applications which are already written to use + * a {@link IceSocketWrapper} instance. + */ + @Deprecated + public IceSocketWrapper getSocketWrapper() + { + return mergingDatagramSocketWrapper; + } }