001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: package gov.nist.javax.sdp;
026:
027: import gov.nist.javax.sdp.fields.*;
028: import java.util.*;
029: import gov.nist.core.*;
030:
031: /**
032: * Field implementation of Media Description interface.
033: * @version JSR141-PUBLIC-REVIEW (subject to change).
034: *
035: *
036: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
037: */
038: public class MediaDescriptionImpl {
039: /** Media type field. */
040: protected MediaField mediaField;
041: /** Extra information field. */
042: protected InformationField informationField;
043: /** Current connection. */
044: protected ConnectionField connectionField;
045: /** Vector of bandwidths. */
046: protected Vector bandwidthFields;
047: /** Key field. */
048: protected KeyField keyField;
049: /** Connection attributes. */
050: protected Vector attributeFields;
051:
052: /**
053: * Encodes to a canonical form.
054: * @return encoded string of object contents
055: * @since v1.0
056: */
057: public String encode() {
058: StringBuffer retval = new StringBuffer();
059:
060: if (mediaField != null)
061: retval.append(mediaField.encode());
062:
063: if (informationField != null)
064: retval.append(informationField.encode());
065:
066: if (connectionField != null)
067: retval.append(connectionField.encode());
068:
069: if (bandwidthFields != null) {
070: for (int i = 0; i < bandwidthFields.size(); i++) {
071: BandwidthField bandwidthField = (BandwidthField) bandwidthFields
072: .elementAt(i);
073: retval.append(bandwidthField.encode());
074: }
075: }
076:
077: if (keyField != null)
078: retval.append(keyField.encode());
079:
080: if (attributeFields != null) {
081: for (int i = 0; i < attributeFields.size(); i++)
082: retval.append(((SDPField) attributeFields.elementAt(i))
083: .encode());
084: }
085:
086: return retval.toString();
087: }
088:
089: /**
090: * Returns encoded text.
091: * @return encoded string of object contents
092: */
093: public String toString() {
094: return this .encode();
095: }
096:
097: /** Default constructor. */
098: public MediaDescriptionImpl() {
099: this .bandwidthFields = new Vector();
100: this .attributeFields = new Vector();
101: }
102:
103: /**
104: * Gets the media field
105: * @return the media field
106: */
107: public MediaField getMediaField() {
108: return mediaField;
109: }
110:
111: /**
112: * Gets the information field.
113: * @return the information field
114: */
115: public InformationField getInformationField() {
116: return informationField;
117: }
118:
119: /**
120: * Gets the connection filed
121: * @return the connection firld
122: */
123: public ConnectionField getConnectionField() {
124: return connectionField;
125: }
126:
127: /**
128: * Gets the key field.
129: * @return the key field
130: */
131: public KeyField getKeyField() {
132: return keyField;
133: }
134:
135: /**
136: * Gets the attributes filed.
137: * @return the vector of attributes
138: */
139: public Vector getAttributeFields() {
140: return attributeFields;
141: }
142:
143: /**
144: * Sets the media field member.
145: * @param m the new media field
146: */
147: public void setMediaField(MediaField m) {
148: mediaField = m;
149: }
150:
151: /**
152: * Sets the information field member.
153: * @param i the new information filed
154: */
155: public void setInformationField(InformationField i) {
156: informationField = i;
157: }
158:
159: /**
160: * Sets the connection field member.
161: * @param c the new connection field
162: */
163: public void setConnectionField(ConnectionField c) {
164: connectionField = c;
165: }
166:
167: /**
168: * Sets the bandwidth field member.
169: * @param b the new bandwidth field
170: */
171: public void addBandwidthField(BandwidthField b) {
172: bandwidthFields.addElement(b);
173: }
174:
175: /**
176: * Sets the key field member.
177: * @param k the new key field
178: */
179: public void setKeyField(KeyField k) {
180: keyField = k;
181: }
182:
183: /**
184: * Sets the attribute fields member.
185: * @param a the new vector of attributes
186: */
187: public void setAttributeFields(Vector a) {
188: attributeFields = a;
189: }
190:
191: /**
192: * Returns the Media field of the description.
193: * @return the Media field of the description.
194: */
195: public MediaField getMedia() {
196: return mediaField;
197:
198: }
199:
200: /**
201: * Adds a new attirbute to the list.
202: * @param af the attribute field to be processed
203: */
204: protected void addAttribute(AttributeField af) {
205: this .attributeFields.addElement(af);
206: }
207:
208: /**
209: * Checks if named attribute exists.
210: * @param name the attribute to check
211: * @return true is attribute is found
212: */
213: protected boolean hasAttribute(String name) {
214: for (int i = 0; i < this .attributeFields.size(); i++) {
215: AttributeField af = (AttributeField) this .attributeFields
216: .elementAt(i);
217: if (af.getAttribute().getName().equals(name))
218: return true;
219: }
220: return false;
221: }
222:
223: /**
224: * Sets the Media field of the description.
225: * @param media to set
226: * @throws SdpException if the media field is null
227: */
228: public void setMedia(MediaField media) throws SdpException {
229: if (media == null)
230: throw new SdpException("The media is null");
231: mediaField = media;
232: }
233:
234: /**
235: * Returns value of the info field (i=) of this object.
236: * @return value of the info field (i=) of this object.
237: */
238: public InformationField getInfo() {
239: return informationField;
240: }
241:
242: /**
243: * Sets the i= field of this object.
244: * @param i to set
245: * @throws SdpException if the info is null
246: */
247: public void setInfo(InformationField i) throws SdpException {
248: if (i == null)
249: throw new SdpException("The info is null");
250: this .informationField = i;
251: }
252:
253: /**
254: * Returns the connection information associated with this object. This may
255: * be null for SessionDescriptions if all Media
256: * objects have a connection object and may be null for Media
257: * objects if the corresponding session connection is non-null.
258: * @return connection
259: */
260: public ConnectionField getConnection() {
261:
262: return connectionField;
263:
264: }
265:
266: /**
267: * Sets the connection data for this entity.
268: * @param conn to set
269: * @throws SdpException if the connexion is null
270: */
271: public void setConnection(ConnectionField conn) throws SdpException {
272: if (conn == null)
273: throw new SdpException("The conn is null");
274: connectionField = conn;
275:
276: }
277:
278: /**
279: * Returns the Bandwidth of the specified type.
280: * @param create type of the Bandwidth to return
281: * @return the Bandwidth or null if undefined
282: */
283:
284: public Vector getBandwidths(boolean create) {
285: return bandwidthFields;
286: }
287:
288: /**
289: * Sets the value of the Bandwidth with the specified type.
290: * @param bandwidths type of the Bandwidth object whose value is requested
291: * @throws SdpException if vector is null
292: */
293: public void setBandwidths(Vector bandwidths) throws SdpException {
294: if (bandwidths == null)
295: throw new SdpException("The vector bandwidths is null");
296: this .bandwidthFields = bandwidths;
297: }
298:
299: /**
300: * Returns the integer value of the specified bandwidth name.
301: * @param name the name of the bandwidth type.
302: * @throws SdpParseException if a parsing error occurs
303: * @return the value of the named bandwidth
304: */
305: public int getBandwidth(String name) throws SdpParseException {
306:
307: if (name == null)
308: throw new NullPointerException("null parameter");
309: if (bandwidthFields == null)
310: return -1;
311: else {
312: for (int i = 0; i < bandwidthFields.size(); i++) {
313: BandwidthField bandwidthField = (BandwidthField) bandwidthFields
314: .elementAt(i);
315: String type = bandwidthField.getBwtype();
316: if (type != null && type.equals(name))
317: return bandwidthField.getBandwidth();
318: }
319: return -1;
320: }
321: }
322:
323: /**
324: * Sets the value of the specified bandwidth type.
325: * @param name the name of the bandwidth type.
326: * @param value the value of the named bandwidth type.
327: * @throws SdpException if the name is null
328: */
329: public void setBandwidth(String name, int value)
330: throws SdpException {
331: if (name == null)
332: throw new SdpException("The name is null");
333: else {
334: for (int i = 0; i < bandwidthFields.size(); i++) {
335: BandwidthField bandwidthField = (BandwidthField) bandwidthFields
336: .elementAt(i);
337: String type = bandwidthField.getBwtype();
338: if (type != null && type.equals(name))
339: bandwidthField.setBandwidth(value);
340: }
341:
342: }
343: }
344:
345: /**
346: * Removes the specified bandwidth type.
347: * @param name the name of the bandwidth type.
348: */
349: public void removeBandwidth(String name) {
350: if (name == null) {
351: throw new NullPointerException("null bandwidth type");
352: } else {
353: int i = 0;
354: for (i = 0; i < bandwidthFields.size(); i++) {
355: BandwidthField bandwidthField = (BandwidthField) bandwidthFields
356: .elementAt(i);
357: String type = bandwidthField.getBwtype();
358: if (type != null && type.equals(name))
359: break;
360:
361: }
362: if (i < bandwidthFields.size())
363: bandwidthFields.removeElementAt(i);
364: }
365: }
366:
367: /**
368: * Returns the key data.
369: * @return the key data.
370: */
371: public KeyField getKey() {
372: return keyField;
373: }
374:
375: /**
376: * Sets encryption key information. This consists of a method and an
377: * encryption key included inline.
378: * @param key the encryption key data; depending on method may be null
379: * @throws SdpException if the key is null
380: */
381: public void setKey(KeyField key) throws SdpException {
382: if (key == null)
383: throw new SdpException("The key is null");
384: setKeyField(key);
385: }
386:
387: /**
388: * Returns the set of attributes for this Description as a
389: * Vector of Attribute objects in the order they were parsed.
390: * @param create specifies whether to return null or a
391: * new empty Vector in case
392: * no attributes exists for this Description
393: * @return attributes for this Description
394: */
395: public Vector getAttributes(boolean create) {
396: return attributeFields;
397: }
398:
399: /**
400: * Adds the specified Attribute to this Description object.
401: * @param attributes the attribute to add
402: * @throws SdpException if the attributes is null
403: */
404: public void setAttributes(Vector attributes) throws SdpException {
405: this .attributeFields = attributes;
406: }
407:
408: /**
409: * Returns the value of the specified attribute.
410: * @param name the name of the attribute.
411: * @throws SdpParseException
412: * @return the value of the named attribute
413: */
414: public String getAttribute(String name) throws SdpParseException {
415: if (name != null) {
416: for (int i = 0; i < this .attributeFields.size(); i++) {
417: AttributeField af = (AttributeField) this .attributeFields
418: .elementAt(i);
419: if (name.equals(af.getAttribute().getName()))
420: return (String) af.getAttribute().getValue();
421: }
422: return null;
423: } else
424: throw new NullPointerException("null arg!");
425: }
426:
427: /**
428: * Sets the value of the specified attribute
429: * @param name the name of the attribute.
430: * @param value the value of the named attribute.
431: * @throws SdpException if the parameters are null
432: */
433: public void setAttribute(String name, String value)
434: throws SdpException {
435: if (name == null)
436: throw new SdpException("The parameters are null");
437: else {
438:
439: int i = 0;
440: for (i = 0; i < this .attributeFields.size(); i++) {
441: AttributeField af = (AttributeField) this .attributeFields
442: .elementAt(i);
443: if (af.getAttribute().getName().equals(name)) {
444: NameValue nv = af.getAttribute();
445: nv.setValue(value);
446: break;
447: }
448:
449: }
450:
451: if (i == this .attributeFields.size()) {
452: AttributeField af = new AttributeField();
453: NameValue nv = new NameValue(name, value);
454: af.setAttribute(nv);
455: this .attributeFields.addElement(af);
456: }
457:
458: }
459: }
460:
461: /**
462: * Removes the attribute specified by the value parameter.
463: * @param name the name of the attribute.
464: */
465: public void removeAttribute(String name) {
466: if (name == null)
467: throw new NullPointerException("null arg!");
468: if (name != null) {
469: int i = 0;
470: for (i = 0; i < this .attributeFields.size(); i++) {
471: AttributeField af = (AttributeField) this .attributeFields
472: .elementAt(i);
473: if (af.getAttribute().getName().equals(name))
474: break;
475: }
476: if (i < attributeFields.size())
477: attributeFields.removeElementAt(i);
478: }
479: }
480:
481: /**
482: * Returns a Vector containing a string indicating the MIME type
483: * for each of the codecs in this description.
484: * <p>
485: * A MIME value is computed for each codec in the media description.
486: * <p>
487: * The MIME type is computed in the following fashion:
488: * The type is the mediaType from the media field.
489: * The subType is determined by the protocol.
490: * <p>
491: * The result is computed as the string of the form:
492: * <pre>
493: * type + '/' + subType
494: * </pre>
495: * The subType portion is computed in the following fashion.
496: * RTP/AVP
497: * the subType is returned as the codec name. This will either
498: * be extracted from the rtpmap attribute or computed.
499: * other
500: * the protocol is returned as the subType.
501: * <p>
502: * If the protocol is RTP/AVP and the rtpmap attribute for a
503: * codec is absent, then the codec name will be computed in the
504: * following fashion.
505: * String indexed in table SdpConstants.avpTypeNames
506: * if the value is an int greater than or equal to 0 and less than
507: * AVP_DEFINED_STATIC_MAX, and has been assigned a
508: * value.
509: * SdpConstant.RESERVED
510: * if the value is an int greater than or equal to 0 and less than
511: * AVP_DEFINED_STATIC_MAX, and has not been
512: * assigned a value.
513: * SdpConstant.UNASSIGNED
514: * An int greater than or equal to AVP_DEFINED_STATIC_MAX
515: * and less than
516: * AVP_DYNAMIC_MIN - currently
517: * unassigned.
518: * SdpConstant.DYNAMIC
519: * Any int less than 0 or greater than or equal to AVP_DYNAMIC_MIN
520: * @throws SdpException if there is a problem extracting the parameters.
521: * @return a Vector containing a string indicating the MIME type for
522: * each of the codecs in this description
523: */
524: public Vector getMimeTypes() throws SdpException {
525: MediaField mediaField = (MediaField) getMedia();
526: String type = mediaField.getMediaType();
527: String protocol = mediaField.getProtocol();
528: Vector formats = mediaField.getMediaFormats(false);
529:
530: Vector v = new Vector();
531: for (int i = 0; i < formats.size(); i++) {
532: String result = null;
533: if (protocol.equals("RTP/AVP")) {
534: if (getAttribute(SdpConstants.RTPMAP) != null)
535: result = type + "/" + protocol;
536: else {
537:
538: }
539: } else
540: result = type + "/" + protocol;
541: v.addElement(result);
542: }
543: return v;
544: }
545:
546: /**
547: * Returns a Vector containing a string of parameters for each of
548: * the codecs in this description.
549: * <p>
550: * A parameter string is computed for each codec.
551: * <p>
552: * The parameter string is computed in the following fashion.
553: * <p>
554: * The rate is extracted from the rtpmap or static data.
555: * <p>
556: * The number of channels is extracted from the rtpmap or static data.
557: * <p>
558: * The ptime is extracted from the ptime attribute.
559: * <p>
560: * The maxptime is extracted from the maxptime attribute.
561: * <p>
562: * Any additional parameters are extracted from the ftmp attribute.
563: * @throws SdpException if there is a problem extracting the parameters.
564: * @return a Vector containing a string of parameters for each of the
565: * codecs in this description.
566: */
567: public Vector getMimeParameters() throws SdpException {
568: String rate = getAttribute("rate");
569: String ptime = getAttribute("ptime");
570: String maxptime = getAttribute("maxptime");
571: String ftmp = getAttribute("ftmp");
572: Vector result = new Vector();
573: result.addElement(rate);
574: result.addElement(ptime);
575: result.addElement(maxptime);
576: result.addElement(ftmp);
577: return result;
578: }
579:
580: /**
581: * Adds dynamic media types to the description.
582: * @param payloadNames a Vector of String - each one the name of
583: * a dynamic payload to be added (usually an integer larger
584: * than SdpConstants.AVP_DYNAMIC_MIN).
585: * @param payloadValues a Vector of String - each contains the
586: * value describing the correlated dynamic payloads to be added
587: * @throws SdpException if either vector is null or empty.
588: * if the vector sizes are unequal.
589: */
590: public void addDynamicPayloads(Vector payloadNames,
591: Vector payloadValues) throws SdpException {
592: MediaField mediaField = (MediaField) getMedia();
593: if (payloadNames == null || payloadValues == null)
594: throw new SdpException(" The vectors are null");
595: else {
596: if (payloadNames.isEmpty() || payloadValues.isEmpty())
597: throw new SdpException(" The vectors are empty");
598: else {
599: if (payloadNames.size() != payloadValues.size())
600: throw new SdpException(
601: " The vector sizes are unequal");
602: else {
603: for (int i = 0; i < payloadNames.size(); i++) {
604: String name = (String) payloadNames
605: .elementAt(i);
606: String value = (String) payloadValues
607: .elementAt(i);
608: setAttribute(name, value);
609: }
610: }
611: }
612: }
613: }
614:
615: }
|