Allows JingleUtils to be used as a utility class outside of OSGi.

cusax-fix
Lyubomir Marinov 14 years ago
parent d113358cd6
commit 3b2b980504

@ -129,11 +129,15 @@ public synchronized InetAddress getLocalHost(
InetAddress intendedDestination)
{
InetAddress localHost = null;
String osVersion = System.getProperty("os.version");
if(logger.isTraceEnabled())
{
logger.trace(
"Querying a localhost addr for dst=" + intendedDestination);
"Querying for a localhost address"
+ " for intended destination '"
+ intendedDestination
+ "'");
}
/* use native code (JNI) to find source address for a specific
* destination address on Windows XP SP1 and over.
@ -143,14 +147,18 @@ public synchronized InetAddress getLocalHost(
* on Windows is because its socket implementation returns the any
* address...
*/
if(OSUtils.IS_WINDOWS &&
!osVersion.startsWith("4") && /* 95/98/Me/NT */
!osVersion.startsWith("5.0")) /* 2000 */
String osVersion;
if (OSUtils.IS_WINDOWS
&& !(osVersion = System.getProperty("os.version")).startsWith(
"4") /* 95/98/Me/NT */
&& !osVersion.startsWith("5.0")) /* 2000 */
{
byte[] src = Win32LocalhostRetriever.getSourceForDestination(
intendedDestination.getAddress());
byte[] src
= Win32LocalhostRetriever.getSourceForDestination(
intendedDestination.getAddress());
if(src == null)
if (src == null)
{
logger.warn("Failed to get localhost ");
}
@ -160,9 +168,9 @@ public synchronized InetAddress getLocalHost(
{
localHost = InetAddress.getByAddress(src);
}
catch(UnknownHostException e)
catch(UnknownHostException uhe)
{
logger.warn("Failed to get localhost ", e);
logger.warn("Failed to get localhost", uhe);
}
}
}
@ -176,10 +184,10 @@ public synchronized InetAddress getLocalHost(
localHost = localHostFinderSocket.getLocalAddress();
localHostFinderSocket.disconnect();
}
//windows socket implementations return the any address so we need to
//find something else here ... InetAddress.getLocalHost seems to work
//better on windows so lets hope it'll do the trick.
//better on windows so let's hope it'll do the trick.
if (localHost == null)
{
try
@ -191,11 +199,14 @@ public synchronized InetAddress getLocalHost(
logger.warn("Failed to get localhost ", e);
}
}
if( localHost.isAnyLocalAddress())
if (localHost.isAnyLocalAddress())
{
if (logger.isTraceEnabled())
logger.trace("Socket returned the AnyLocalAddress. "+
"Trying a workaround.");
{
logger.trace(
"Socket returned the ANY local address."
+ " Trying a workaround.");
}
try
{
//all that's inside the if is an ugly IPv6 hack
@ -204,32 +215,28 @@ public synchronized InetAddress getLocalHost(
{
//return the first globally routable ipv6 address we find
//on the machine (and hope it's a good one)
Enumeration<NetworkInterface> interfaces
boolean done = false;
Enumeration<NetworkInterface> ifaces
= NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements())
while (!done && ifaces.hasMoreElements())
{
NetworkInterface iface = interfaces.nextElement();
Enumeration<InetAddress> addresses =
iface.getInetAddresses();
while(addresses.hasMoreElements())
Enumeration<InetAddress> addresses
= ifaces.nextElement().getInetAddresses();
while (addresses.hasMoreElements())
{
InetAddress address
= addresses.nextElement();
if(address instanceof Inet6Address)
{
if(!address.isAnyLocalAddress()
InetAddress address = addresses.nextElement();
if ((address instanceof Inet6Address)
&& !address.isAnyLocalAddress()
&& !address.isLinkLocalAddress()
&& !address.isSiteLocalAddress()
&& !address.isLoopbackAddress())
{
if(logger.isTraceEnabled())
{
logger.trace("will return ipv6 addr "
+ address);
}
return address;
}
&& !address.isLoopbackAddress()
&& !address.isSiteLocalAddress())
{
localHost = address;
done = true;
break;
}
}
}
@ -243,47 +250,41 @@ public synchronized InetAddress getLocalHost(
// Make sure we got an IPv4 address.
if (!(localHost instanceof Inet4Address))
{
// return the first non localhost interface we find.
Enumeration<NetworkInterface> interfaces =
NetworkInterface.getNetworkInterfaces();
// return the first non-loopback interface we find.
boolean done = false;
Enumeration<NetworkInterface> ifaces
= NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements())
while (!done && ifaces.hasMoreElements())
{
NetworkInterface iface = interfaces.nextElement();
Enumeration<InetAddress> addresses = iface
.getInetAddresses();
Enumeration<InetAddress> addresses
= ifaces.nextElement().getInetAddresses();
while (addresses.hasMoreElements())
{
InetAddress address = addresses.nextElement();
if (address instanceof Inet4Address)
if ((address instanceof Inet4Address)
&& !address.isLoopbackAddress())
{
if (!address.isLoopbackAddress())
{
if (logger.isTraceEnabled())
{
logger.trace(
"will return ipv6 addr "
+ address);
}
return address;
}
localHost = address;
done = true;
break;
}
}
}
}
}
}
catch (Exception ex)
catch (Exception e)
{
//sigh ... ok return 0.0.0.0
logger.warn("Failed to get localhost ", ex);
logger.warn("Failed to get localhost", e);
}
}
if(logger.isTraceEnabled())
{
logger.trace("Will return the following localhost address: "
+ localHost);
}
if (logger.isTraceEnabled())
logger.trace("Returning the localhost address '" + localHost + "'");
return localHost;
}

@ -306,7 +306,8 @@ protected ContentPacketExtension removeContent(
}
/**
* Close this transport manager and release resources.
* Releases the resources acquired by this <tt>TransportManager</tt> and
* prepares it for garbage collection.
*/
public void close()
{

@ -186,6 +186,21 @@ public static class Channel
*/
public static final String ELEMENT_NAME = "channel";
/**
* The XML name of the <tt>expire</tt> attribute of a <tt>channel</tt>
* of a <tt>content</tt> of a <tt>conference</tt> IQ which represents
* the value of the <tt>expire</tt> property of
* <tt>CobriConferenceIQ.Channel</tt>.
*/
public static final String EXPIRE_ATTR_NAME = "expire";
/**
* The value of the <tt>expire</tt> property of
* <tt>CobriConferenceIQ.Channel</tt> which indicates that no actual
* value has been specified for the property in question.
*/
public static final int EXPIRE_NOT_SPECIFIED = -1;
/**
* The XML name of the <tt>host</tt> attribute of a <tt>channel</tt> of
* a <tt>content</tt> of a <tt>conference</tt> IQ which represents the
@ -218,6 +233,12 @@ public static class Channel
*/
public static final String RTP_PORT_ATTR_NAME = "rtpport";
/**
* The number of seconds of inactivity after which the <tt>channel</tt>
* represented by this instance expires.
*/
private int expire = EXPIRE_NOT_SPECIFIED;
/**
* The host of the <tt>channel</tt> represented by this instance.
*/
@ -268,6 +289,18 @@ public boolean addPayloadType(PayloadTypePacketExtension payloadType)
: payloadTypes.add(payloadType);
}
/**
* Gets the number of seconds of inactivity after which the
* <tt>channel</tt> represented by this instance expires.
*
* @return the number of seconds of inactivity after which the
* <tt>channel</tt> represented by this instance expires
*/
public int getExpire()
{
return expire;
}
public String getHost()
{
return host;
@ -313,6 +346,24 @@ public boolean removePayloadType(PayloadTypePacketExtension payloadType)
return payloadTypes.remove(payloadType);
}
/**
* Sets the number of seconds of inactivity after which the
* <tt>channel</tt> represented by this instance expires.
*
* @param expire the number of seconds of activity after which the
* <tt>channel</tt> represented by this instance expires
* @throws IllegalArgumentException if the value of the specified
* <tt>expire</tt> is other than {@link #EXPIRE_NOT_SPECIFIED} and
* negative
*/
public void setExpire(int expire)
{
if ((expire != EXPIRE_NOT_SPECIFIED) && (expire < 0))
throw new IllegalArgumentException("expire");
this.expire = expire;
}
public void setHost(String host)
{
this.host = host;
@ -366,6 +417,12 @@ public void toXML(StringBuilder xml)
xml.append(' ').append(RTCP_PORT_ATTR_NAME).append("='")
.append(rtcpPort).append('\'');
int expire = getExpire();
if (expire >= 0)
xml.append(' ').append(EXPIRE_ATTR_NAME).append("='")
.append(expire).append('\'');
List<PayloadTypePacketExtension> payloadTypes = getPayloadTypes();
if (payloadTypes.size() == 0)

@ -121,6 +121,13 @@ else if (CobriConferenceIQ.Content.ELEMENT_NAME
if ((rtcpPort != null) && (rtcpPort.length() != 0))
channel.setRTCPPort(Integer.parseInt(rtcpPort));
String expire
= parser.getAttributeValue(
"",
CobriConferenceIQ.Channel.EXPIRE_ATTR_NAME);
if ((expire != null) && (expire.length() != 0))
channel.setExpire(Integer.parseInt(expire));
}
else if (CobriConferenceIQ.Content.ELEMENT_NAME
.equals(name))

@ -24,6 +24,7 @@
* transports.
*
* @author Emil Ivov
* @author Lyubomir Marinov
*/
public class JingleUtils
{
@ -99,16 +100,46 @@ public static List<MediaFormat> extractFormats(
* Returns the {@link MediaFormat} described in the <tt>payloadType</tt>
* extension or <tt>null</tt> if we don't recognize the format.
*
* @param payloadType the {@link PayloadTypePacketExtension} that we'd like
* to parse into a {@link MediaFormat}.
* @param payloadType the {@link PayloadTypePacketExtension} which is to be
* parsed into a {@link MediaFormat}.
* @param ptRegistry the {@link DynamicPayloadTypeRegistry} that we would
* use for the registration of possible dynamic payload types.
* use for the registration of possible dynamic payload types or
* <tt>null</tt> the returned <tt>MediaFormat</tt> is to not be registered
* into a <tt>DynamicPayloadTypeRegistry</tt>.
*
* @return the {@link MediaFormat} described in the <tt>payloadType</tt>
* extension or <tt>null</tt> if we don't recognize the format.
*/
public static MediaFormat payloadTypeToMediaFormat(
PayloadTypePacketExtension payloadType,
DynamicPayloadTypeRegistry ptRegistry)
{
return
payloadTypeToMediaFormat(
payloadType,
JabberActivator.getMediaService(),
ptRegistry);
}
/**
* Returns the {@link MediaFormat} described in the <tt>payloadType</tt>
* extension or <tt>null</tt> if we don't recognize the format.
*
* @param payloadType the {@link PayloadTypePacketExtension} which is to be
* parsed into a {@link MediaFormat}.
* @param mediaService the <tt>MediaService</tt> implementation which is to
* be used for <tt>MediaFormat</tt>-related factory methods
* @param ptRegistry the {@link DynamicPayloadTypeRegistry} that we would
* use for the registration of possible dynamic payload types or
* <tt>null</tt> the returned <tt>MediaFormat</tt> is to not be registered
* into a <tt>DynamicPayloadTypeRegistry</tt>.
*
* @return the {@link MediaFormat} described in the <tt>payloadType</tt>
* extension or <tt>null</tt> if we don't recognize the format.
*/
public static MediaFormat payloadTypeToMediaFormat(
PayloadTypePacketExtension payloadType,
MediaService mediaService,
DynamicPayloadTypeRegistry ptRegistry)
{
byte pt = (byte)payloadType.getID();
@ -127,19 +158,15 @@ public static MediaFormat payloadTypeToMediaFormat(
paramsMap.put(param.getName(), param.getValue());
}
// video related attribute in payload type element
// video-related attributes in payload-type element
for(String attr : payloadType.getAttributeNames())
{
if(attr.equals("width") || attr.equals("height"))
{
paramsMap.put(attr, payloadType.getAttributeAsString(
attr));
}
paramsMap.put(attr, payloadType.getAttributeAsString(attr));
}
//now create the format.
MediaFormatFactory formatFactory
= JabberActivator.getMediaService().getFormatFactory();
MediaFormatFactory formatFactory = mediaService.getFormatFactory();
MediaFormat format
= formatFactory.createMediaFormat(
pt,
@ -169,14 +196,15 @@ public static MediaFormat payloadTypeToMediaFormat(
* re-map a payloadType in its answer to a different MediaFormat
* than the one we've specified in our offer?
*/
if ((pt >= MediaFormat.MIN_DYNAMIC_PAYLOAD_TYPE)
if ((ptRegistry != null)
&& (pt >= MediaFormat.MIN_DYNAMIC_PAYLOAD_TYPE)
&& (pt <= MediaFormat.MAX_DYNAMIC_PAYLOAD_TYPE)
&& (ptRegistry.findFormat(pt) == null))
{
ptRegistry.addMapping(format, pt);
}
return (unknown == false) ? format : null;
return unknown ? null : format;
}
/**
@ -527,18 +555,19 @@ public static ContentPacketExtension createDescription(
}
/**
* Converts <tt>format</tt> into a {@link PayloadTypePacketExtension} instance.
* Converts a specific {@link MediaFormat} into a new
* {@link PayloadTypePacketExtension} instance.
*
* @param format the {@link MediaFormat} we'd like to convert.
* @param format the <tt>MediaFormat</tt> we'd like to convert.
* @param ptRegistry the {@link DynamicPayloadTypeRegistry} to use for
* formats that don't have a static pt number.
*
* @return the newly created {@link PayloadTypePacketExtension} that
* contains <tt>format</tt>'s parameters.
* @return the new <tt>PayloadTypePacketExtension</tt> which contains
* <tt>format</tt>'s parameters.
*/
public static PayloadTypePacketExtension formatToPayloadType(
MediaFormat format,
DynamicPayloadTypeRegistry ptRegistry)
MediaFormat format,
DynamicPayloadTypeRegistry ptRegistry)
{
PayloadTypePacketExtension ptExt = new PayloadTypePacketExtension();
@ -555,7 +584,10 @@ public static PayloadTypePacketExtension formatToPayloadType(
ptExt.setClockrate((int)format.getClockRate());
/* add parameters */
/*
* Add the format parameters and the advanced attributes (as parameter
* packet extensions).
*/
for(Map.Entry<String, String> entry :
format.getFormatParameters().entrySet())
{
@ -564,7 +596,6 @@ public static PayloadTypePacketExtension formatToPayloadType(
ext.setValue(entry.getValue());
ptExt.addParameter(ext);
}
for(Map.Entry<String, String> entry :
format.getAdvancedAttributes().entrySet())
{

Loading…
Cancel
Save