SimpleSessionDescriptionpublic class SimpleSessionDescription extends Object An object used to manipulate messages of Session Description Protocol (SDP).
It is mainly designed for the uses of Session Initiation Protocol (SIP).
Therefore, it only handles connection addresses ("c="), bandwidth limits,
("b="), encryption keys ("k="), and attribute fields ("a="). Currently this
implementation does not support multicast sessions.
Here is an example code to create a session description.
SimpleSessionDescription description = new SimpleSessionDescription(
System.currentTimeMillis(), "1.2.3.4");
Media media = description.newMedia("audio", 56789, 1, "RTP/AVP");
media.setRtpPayload(0, "PCMU/8000", null);
media.setRtpPayload(8, "PCMA/8000", null);
media.setRtpPayload(127, "telephone-event/8000", "0-15");
media.setAttribute("sendrecv", "");
Invoking description.encode() will produce a result like the
one below.
v=0
o=- 1284970442706 1284970442709 IN IP4 1.2.3.4
s=-
c=IN IP4 1.2.3.4
t=0 0
m=audio 56789 RTP/AVP 0 8 127
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:127 telephone-event/8000
a=fmtp:127 0-15
a=sendrecv
|
Fields Summary |
---|
private final Fields | mFields | private final ArrayList | mMedia |
Constructors Summary |
---|
public SimpleSessionDescription(long sessionId, String address)Creates a minimal session description from the given session ID and
unicast address. The address is used in the origin field ("o=") and the
connection field ("c="). See {@link SimpleSessionDescription} for an
example of its usage.
address = (address.indexOf(':") < 0 ? "IN IP4 " : "IN IP6 ") + address;
mFields.parse("v=0");
mFields.parse(String.format(Locale.US, "o=- %d %d %s", sessionId,
System.currentTimeMillis(), address));
mFields.parse("s=-");
mFields.parse("t=0 0");
mFields.parse("c=" + address);
| public SimpleSessionDescription(String message)Creates a session description from the given message.
String[] lines = message.trim().replaceAll(" +", " ").split("[\r\n]+");
Fields fields = mFields;
for (String line : lines) {
try {
if (line.charAt(1) != '=") {
throw new IllegalArgumentException();
}
if (line.charAt(0) == 'm") {
String[] parts = line.substring(2).split(" ", 4);
String[] ports = parts[1].split("/", 2);
Media media = newMedia(parts[0], Integer.parseInt(ports[0]),
(ports.length < 2) ? 1 : Integer.parseInt(ports[1]),
parts[2]);
for (String format : parts[3].split(" ")) {
media.setFormat(format, null);
}
fields = media;
} else {
fields.parse(line);
}
} catch (Exception e) {
throw new IllegalArgumentException("Invalid SDP: " + line);
}
}
|
Methods Summary |
---|
public java.lang.String | encode()Encodes the session description and all its media descriptions in a
string. Note that the result might be incomplete if a required field
has never been added before.
StringBuilder buffer = new StringBuilder();
mFields.write(buffer);
for (Media media : mMedia) {
media.write(buffer);
}
return buffer.toString();
| public java.lang.String | getAddress()Returns the connection address or {@code null} if it is not present.
return mFields.getAddress();
| public java.lang.String | getAttribute(java.lang.String name)Returns the attribute of the given name or {@code null} if it is not
present.
return mFields.getAttribute(name);
| public java.lang.String[] | getAttributeNames()Returns the names of all the attributes.
return mFields.getAttributeNames();
| public int | getBandwidth(java.lang.String type)Returns the bandwidth limit of the given type or {@code -1} if it is not
present.
return mFields.getBandwidth(type);
| public java.lang.String[] | getBandwidthTypes()Returns the types of the bandwidth limits.
return mFields.getBandwidthTypes();
| public java.lang.String | getEncryptionKey()Returns the encryption key or {@code null} if it is not present.
return mFields.getEncryptionKey();
| public java.lang.String | getEncryptionMethod()Returns the encryption method or {@code null} if it is not present.
return mFields.getEncryptionMethod();
| public android.net.sip.SimpleSessionDescription$Media[] | getMedia()Returns all the media descriptions in this session description.
return mMedia.toArray(new Media[mMedia.size()]);
| public android.net.sip.SimpleSessionDescription$Media | newMedia(java.lang.String type, int port, int portCount, java.lang.String protocol)Creates a new media description in this session description.
Media media = new Media(type, port, portCount, protocol);
mMedia.add(media);
return media;
| public void | setAddress(java.lang.String address)Sets the connection address. The field will be removed if the address
is {@code null}.
mFields.setAddress(address);
| public void | setAttribute(java.lang.String name, java.lang.String value)Sets the attribute for the given name. The field will be removed if
the value is {@code null}. To set a binary attribute, use an empty
string as the value.
mFields.setAttribute(name, value);
| public void | setBandwidth(java.lang.String type, int value)Sets the bandwith limit for the given type. The field will be removed if
the value is negative.
mFields.setBandwidth(type, value);
| public void | setEncryption(java.lang.String method, java.lang.String key)Sets the encryption method and the encryption key. The field will be
removed if the method is {@code null}.
mFields.setEncryption(method, key);
|
|