Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package javax.sdp;
  
  
  import java.util.*;
  import java.net.*;
  import java.text.*;

The SdpFactory enables applications to encode and decode SDP messages. The SdpFactory can be used to construct a SessionDescription object programmatically. The SdpFactory can also be used to construct a SessionDescription based on the contents of a String. Acknowledgement: Bugs reported by Brian J. Collins <bjcollins@rockwellcollins.com>. and by Majdi Abuelbassal <majdi.abuelbassal@bbumail.com>. Please refer to IETF RFC 2327 for a description of SDP.

Author(s):
Olivier Deruelle <olivier.deruelle@nist.gov>
M. Ranganathan
Version:
1.0
 
 public class SdpFactory extends Object {
     private static final SdpFactory singletonInstance = new SdpFactory();

    
Creates new SdpFactory
 
     private SdpFactory() {
     }

    
Obtain an instance of an SdpFactory. This static method returns a factory instance. Once an application has obtained a reference to an SdpFactory it can use the factory to configure and obtain parser instances and to create SDP objects.

Returns:
a factory instance
 
     public static SdpFactory getInstance() {
         return ;
     }

    
Creates a new, empty SessionDescription. The session is set as follows: v=0 o=this.createOrigin ("user", InetAddress.getLocalHost().toString()); s=- t=0 0

Returns:
a new, empty SessionDescription.
Throws:
SdpException SdpException, - if there is a problem constructing the SessionDescription.
 
         SessionDescriptionImpl sessionDescriptionImpl = new SessionDescriptionImpl();
 
         ProtoVersionField ProtoVersionField = new ProtoVersionField();
         ProtoVersionField.setVersion(0);
         sessionDescriptionImpl.setVersion(ProtoVersionField);
 
         OriginField originImpl = null;
         try {
             originImpl = (OriginFieldthis.createOrigin("user", InetAddress
                     .getLocalHost().getHostAddress());
         } catch (UnknownHostException e) {
             e.printStackTrace();
         }
         sessionDescriptionImpl.setOrigin(originImpl);
 
         SessionNameField sessionNameImpl = new SessionNameField();
         sessionNameImpl.setValue("-");
         sessionDescriptionImpl.setSessionName(sessionNameImpl);
 
         TimeDescriptionImpl timeDescriptionImpl = new TimeDescriptionImpl();
         TimeField timeImpl = new TimeField();
         timeImpl.setZero();
         timeDescriptionImpl.setTime(timeImpl);
         Vector times = new Vector();
         times.addElement(timeDescriptionImpl);
         sessionDescriptionImpl.setTimeDescriptions(times);
 
         return sessionDescriptionImpl;
     }

    
Creates a new SessionDescription, deep copy of another SessionDescription.

Parameters:
otherSessionDescription - the SessionDescription to copy from.
Returns:
a new SessionDescription, exact and deep copy of the otherSessionDescription.
Throws:
SdpException - if there is a problem constructing the SessionDescription.
            SessionDescription otherSessionDescriptionthrows SdpException {
        return new SessionDescriptionImpl(otherSessionDescription);
    }

    
Creates a SessionDescription populated with the information contained within the string parameter. Note: unknown field types should not cause exceptions.

Parameters:
s s - the sdp message that is to be parsed.
Returns:
a populated SessionDescription object.
Throws:
SdpParseException SdpParseException - if there is a problem parsing the String.
            throws SdpParseException {
        try {
            SDPAnnounceParser sdpParser = new SDPAnnounceParser(s);
            return sdpParser.parse();
        } catch (ParseException e) {
            e.printStackTrace();
            throw new SdpParseException(0, 0, "Could not parse message");
        }
    }

    
Returns Bandwidth object with the specified values.

Parameters:
modifier modifier - the bandwidth type
value the bandwidth value measured in kilobits per second
Returns:
bandwidth
    public BandWidth createBandwidth(String modifierint value) {
        BandwidthField bandWidthImpl = new BandwidthField();
        try {
            bandWidthImpl.setType(modifier);
            bandWidthImpl.setValue(value);
        } catch (SdpException s) {
            s.printStackTrace();
        }
        return bandWidthImpl;
    }

    
Returns Attribute object with the specified values.

Parameters:
name the namee of the attribute
value the value of the attribute
Returns:
Attribute
    public Attribute createAttribute(String nameString value) {
        AttributeField attributeImpl = new AttributeField();
        try {
            attributeImpl.setName(name);
            attributeImpl.setValueAllowNull(value);
        } catch (SdpException s) {
            s.printStackTrace();
        }
        return attributeImpl;
    }

    
Returns Info object with the specified value.

Parameters:
value the string containing the description.
Returns:
Info
    public Info createInfo(String value) {
        InformationField infoImpl = new InformationField();
        try {
            infoImpl.setValue(value);
        } catch (SdpException s) {
            s.printStackTrace();
        }
        return infoImpl;
    }

    
Returns Phone object with the specified value.

Parameters:
value the string containing the description.
Returns:
Phone
    public Phone createPhone(String value) {
        PhoneField phoneImpl = new PhoneField();
        try {
            phoneImpl.setValue(value);
        } catch (SdpException s) {
            s.printStackTrace();
        }
        return phoneImpl;
    }

    
Returns EMail object with the specified value.

Parameters:
value the string containing the description.
Returns:
EMail
    public EMail createEMail(String value) {
        EmailField emailImpl = new EmailField();
        try {
            emailImpl.setValue(value);
        } catch (SdpException s) {
            s.printStackTrace();
        }
        return emailImpl;
    }

    
Returns URI object with the specified value.

Parameters:
value the URL containing the description.
Returns:
URI
Throws:
SdpException
    public javax.sdp.URI createURI(URL valuethrows SdpException {
        URIField uriImpl = new URIField();
        uriImpl.set(value);
        return uriImpl;
    }

    
Returns SessionName object with the specified name.

Parameters:
name the string containing the name of the session.
Returns:
SessionName
    public SessionName createSessionName(String name) {
        SessionNameField sessionNameImpl = new SessionNameField();
        try {
            sessionNameImpl.setValue(name);
        } catch (SdpException s) {
            s.printStackTrace();
        }
        return sessionNameImpl;
    }

    
Returns Key object with the specified value.

Parameters:
method the string containing the method type.
key the key to set
Returns:
Key
    public Key createKey(String methodString key) {
        KeyField keyImpl = new KeyField();
        try {
            keyImpl.setMethod(method);
            keyImpl.setKey(key);
        } catch (SdpException s) {
            s.printStackTrace();
            return null;
        }
        return keyImpl;
    }

    
Returns Version object with the specified values.

Parameters:
value the version number.
Returns:
Version
    public Version createVersion(int value) {
        ProtoVersionField protoVersionField = new ProtoVersionField();
        try {
            protoVersionField.setVersion(value);
        } catch (SdpException s) {
            s.printStackTrace();
            return null;
        }
        return protoVersionField;
    }

    
Returns Media object with the specified properties.

Parameters:
media the media type, eg "audio"
port port number on which to receive media
numPorts number of ports used for this media stream
transport transport type, eg "RTP/AVP"
staticRtpAvpTypes vector to set
Returns:
Media
Throws:
SdpException
    public Media createMedia(String mediaint portint numPorts,
            String transportVector staticRtpAvpTypesthrows SdpException {
        MediaField mediaImpl = new MediaField();
        mediaImpl.setMediaType(media);
        mediaImpl.setMediaPort(port);
        mediaImpl.setPortCount(numPorts);
        mediaImpl.setProtocol(transport);
        mediaImpl.setMediaFormats(staticRtpAvpTypes);
        return mediaImpl;
    }

    
Returns Origin object with the specified properties.

Parameters:
userName the user name.
address the IP4 encoded address.
Returns:
Origin
Throws:
SdpException if the parameters are null
    public Origin createOrigin(String userNameString address)
            throws SdpException {
        OriginField originImpl = new OriginField();
        originImpl.setUsername(userName);
        originImpl.setAddress(address);
        originImpl.setNetworkType(.);
        originImpl.setAddressType(.);
        return originImpl;
    }

    
Returns Origin object with the specified properties.

Parameters:
userName String containing the user that created the string.
sessionId long containing the session identifier.
sessionVersion long containing the session version.
networkType String network type for the origin (usually "IN").
addrType String address type (usually "IP4").
address String IP address usually the address of the host.
Returns:
Origin object with the specified properties.
Throws:
SdpException if the parameters are null
    public Origin createOrigin(String userNamelong sessionId,
            long sessionVersionString networkTypeString addrType,
            String addressthrows SdpException {
        OriginField originImpl = new OriginField();
        originImpl.setUsername(userName);
        originImpl.setAddress(address);
        originImpl.setSessionId(sessionId);
        originImpl.setSessionVersion(sessionVersion);
        originImpl.setAddressType(addrType);
        originImpl.setNetworkType(networkType);
        return originImpl;
    }

    
Returns MediaDescription object with the specified properties. The returned object will respond to Media.getMediaFormats(boolean) with a Vector of media formats.

Parameters:
media media -
port port number on which to receive media
numPorts number of ports used for this media stream
transport transport type, eg "RTP/AVP"
staticRtpAvpTypes list of static RTP/AVP media payload types which should be specified by the returned MediaDescription throws IllegalArgumentException if passed an invalid RTP/AVP payload type
Returns:
MediaDescription
Throws:
java.lang.IllegalArgumentException
SdpException
    public MediaDescription createMediaDescription(String mediaint port,
            int numPortsString transportint[] staticRtpAvpTypes)
            throws IllegalArgumentExceptionSdpException {
        MediaDescriptionImpl mediaDescriptionImpl = new MediaDescriptionImpl();
        MediaField mediaImpl = new MediaField();
        mediaImpl.setMediaType(media);
        mediaImpl.setMediaPort(port);
        mediaImpl.setPortCount(numPorts);
        mediaImpl.setProtocol(transport);
        mediaDescriptionImpl.setMedia(mediaImpl);
        // Bug fix contributed by Paloma Ortega.
        Vector payload = new Vector();
        for (int i = 0; i < staticRtpAvpTypes.lengthi++)
            payload.add(new Integer(staticRtpAvpTypes[i]).toString());
        mediaImpl.setMediaFormats(payload);
        return mediaDescriptionImpl;
    }

    
Returns MediaDescription object with the specified properties. The returned object will respond to Media.getMediaFormats(boolean) with a Vector of String objects specified by the 'formats argument.

Parameters:
media the media type, eg "audio"
port port number on which to receive media
numPorts number of ports used for this media stream
transport transport type, eg "RTP/AVP"
formats list of formats which should be specified by the returned MediaDescription
Returns:
MediaDescription
    public MediaDescription createMediaDescription(String mediaint port,
            int numPortsString transportString[] formats) {
        MediaDescriptionImpl mediaDescriptionImpl = new MediaDescriptionImpl();
        try {
            MediaField mediaImpl = new MediaField();
            mediaImpl.setMediaType(media);
            mediaImpl.setMediaPort(port);
            mediaImpl.setPortCount(numPorts);
            mediaImpl.setProtocol(transport);
            Vector formatsV = new Vector(formats.length);
            for (int i = 0; i < formats.lengthi++)
                formatsV.add(formats[i]);
            mediaImpl.setMediaFormats(formatsV);
            mediaDescriptionImpl.setMedia(mediaImpl);
        } catch (SdpException s) {
            s.printStackTrace();
        }
        return mediaDescriptionImpl;
    }

    
Returns TimeDescription object with the specified properties.

Parameters:
t the Time that the time description applies to. Returns TimeDescription object with the specified properties.
Returns:
TimeDescription
Throws:
SdpException
        TimeDescriptionImpl timeDescriptionImpl = new TimeDescriptionImpl();
        timeDescriptionImpl.setTime(t);
        return timeDescriptionImpl;
    }

    
Returns TimeDescription unbounded (i.e. "t=0 0");

Returns:
TimeDescription unbounded (i.e. "t=0 0");
Throws:
SdpException
        TimeDescriptionImpl timeDescriptionImpl = new TimeDescriptionImpl();
        TimeField timeImpl = new TimeField();
        timeImpl.setZero();
        timeDescriptionImpl.setTime(timeImpl);
        return timeDescriptionImpl;
    }

    
Returns TimeDescription object with the specified properties.

Parameters:
start start time.
stop stop time.
Returns:
TimeDescription
Throws:
SdpException if the parameters are null
    public TimeDescription createTimeDescription(Date startDate stop)
            throws SdpException {
        TimeDescriptionImpl timeDescriptionImpl = new TimeDescriptionImpl();
        TimeField timeImpl = new TimeField();
        timeImpl.setStart(start);
        timeImpl.setStop(stop);
        timeDescriptionImpl.setTime(timeImpl);
        return timeDescriptionImpl;
    }

    
Returns a String containing the computed form for a multi-connection address. Parameters: addr - connection address ttl - time to live (TTL) for multicast addresses numAddrs - number of addresses used by the connection Returns: a String containing the computed form for a multi-connection address.
    public String formatMulticastAddress(String addrint ttlint numAddrs) {
        String res = addr + "/" + ttl + "/" + numAddrs;
        return res;
    }

    
Returns a Connection object with the specified properties a

Parameters:
netType network type, eg "IN" for "Internet"
addrType address type, eg "IP4" for IPv4 type addresses
addr connection address
ttl time to live (TTL) for multicast addresses
numAddrs number of addresses used by the connection
Returns:
Connection
    public Connection createConnection(String netTypeString addrType,
            String addrint ttlint numAddrsthrows SdpException {
        ConnectionField connectionImpl = new ConnectionField();
        connectionImpl.setNetworkType(netType);
        connectionImpl.setAddressType(addrType);
        connectionImpl.setAddress(addr);
        return connectionImpl;
    }

    
Returns a Connection object with the specified properties and no TTL and a default number of addresses (1).

Parameters:
netType network type, eg "IN" for "Internet"
addrType address type, eg "IP4" for IPv4 type addresses
addr connection address
Returns:
Connection
Throws:
SdpException if the parameters are null
    public Connection createConnection(String netTypeString addrType,
            String addrthrows SdpException {
        ConnectionField connectionImpl = new ConnectionField();
        connectionImpl.setNetworkType(netType);
        connectionImpl.setAddressType(addrType);
        connectionImpl.setAddress(addr);
        return connectionImpl;
    }

    
Returns a Connection object with the specified properties and a network and address type of "IN" and "IP4" respectively.

Parameters:
addr connection address
ttl time to live (TTL) for multicast addresses
numAddrs number of addresses used by the connection
Returns:
Connection
    public Connection createConnection(String addrint ttlint numAddrs)
            throws SdpException {
        ConnectionField connectionImpl = new ConnectionField();
        connectionImpl.setAddress(addr);
        return connectionImpl;
    }

    
Returns a Connection object with the specified address. This is equivalent to createConnection("IN", "IP4", addr);

Parameters:
addr connection address
Returns:
Connection
Throws:
SdpException if the parameter is null
    public Connection createConnection(String addrthrows SdpException {
        return createConnection("IN""IP4"addr);
    }

    
Returns a Time specification with the specified start and stop times.

Parameters:
start start time
stop stop time
Returns:
a Time specification with the specified start and stop times.
Throws:
SdpException if the parameters are null
    public Time createTime(Date startDate stopthrows SdpException {
        TimeField timeImpl = new TimeField();
        timeImpl.setStart(start);
        timeImpl.setStop(stop);
        return timeImpl;
    }

    
Returns an unbounded Time specification (i.e., "t=0 0").

Returns:
an unbounded Time specification (i.e., "t=0 0").
Throws:
SdpException
    public Time createTime() throws SdpException {
        TimeField timeImpl = new TimeField();
        timeImpl.setZero();
        return timeImpl;
    }

    
Returns a RepeatTime object with the specified interval, duration, and time offsets.

Parameters:
repeatInterval the "repeat interval" in seconds
activeDuration the "active duration" in seconds
offsets the list of offsets relative to the start time of the Time object with which the returned RepeatTime will be associated
Returns:
RepeatTime
    public RepeatTime createRepeatTime(int repeatIntervalint activeDuration,
            int[] offsets) {
        RepeatField repeatTimeField = new RepeatField();
        try {
            repeatTimeField.setRepeatInterval(repeatInterval);
            repeatTimeField.setActiveDuration(activeDuration);
            repeatTimeField.setOffsetArray(offsets);
        } catch (SdpException s) {
            s.printStackTrace();
        }
        return repeatTimeField;
    }

    
Constructs a timezone adjustment record.

Parameters:
d the Date at which the adjustment is going to take place.
offset the adjustment in number of seconds relative to the start time of the SessionDescription with which this object is associated.
Returns:
TimeZoneAdjustment
    public TimeZoneAdjustment createTimeZoneAdjustment(Date dint offset) {
        ZoneField timeZoneAdjustmentImpl = new ZoneField();
        try {
            Hashtable map = new Hashtable();
            map.put(dnew Integer(offset));
            timeZoneAdjustmentImpl.setZoneAdjustments(map);
        } catch (SdpException s) {
            s.printStackTrace();
        }
        return timeZoneAdjustmentImpl;
    }

    
Test main.

Parameters:
args
Throws:
SdpException public static void main(String[] args) throws SdpException { }


    

Parameters:
ntpTime long to set
Returns:
Returns a Date object for a given NTP date value.
    public static Date getDateFromNtp(long ntpTime) {
        return new Date((ntpTime - .) * 1000);
    }

    
Returns a long containing the NTP value for a given Java Date.

Parameters:
d Date to set
Returns:
long
    public static long getNtpTime(Date dthrows SdpParseException {
        if (d == null)
            return -1;
        return ((d.getTime() / 1000) + .);
    }
    public static void main(String[] argsthrows SdpParseException,
            SdpException {
        String sdpFields = "v=0\r\n"
                + "o=CiscoSystemsSIP-GW-UserAgent 2578 3027 IN IP4 83.211.215.216\r\n"
                + "s=SIP Call\r\n" + "c=IN IP4 62.94.199.36\r\n" + "t=0 0\r\n"
                + "m=audio 62278 RTP/AVP 18 8 0 4 3 125 101 19\r\n"
                + "c=IN IP4 62.94.199.36\r\n" + "a=rtpmap:18 G729/8000\r\n"
                + "a=fmtp:18 annexb=yes\r\n" + "a=rtpmap:8 PCMA/8000\r\n"
                + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n"
                + "a=fmtp:4 bitrate=5.3;annexa=no\r\n"
                + "a=rtpmap:3 GSM/8000\r\n" + "a=rtpmap:125 X-CCD/8000\r\n"
                + "a=rtpmap:101 telephone-event/8000\r\n"
                + "a=fmtp:101 0-16\r\n" + "a=rtpmap:19 CN/8000\r\n"
                + "a=direction:passive\r\n";
        SdpFactory sdpFactory = new SdpFactory();
        SessionDescription sessionDescription = sdpFactory
                .createSessionDescription(sdpFields);
        ..println("sessionDescription = " + sessionDescription);
        Vector mediaDescriptions = sessionDescription
                .getMediaDescriptions(true);
        for (int i = 0; i < mediaDescriptions.size(); i++) {
            MediaDescription m = (MediaDescriptionmediaDescriptions
                    .elementAt(i);
            ((MediaDescriptionImplm).setDuplexity("sendrecv");
            ..println("m = " + m.toString());
            Media media = m.getMedia();
            Vector formats = media.getMediaFormats(false);
            ..println("formats = " + formats);
        }
    }
New to GrepCode? Check out our FAQ X