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: /*
026: */
027: package gov.nist.javax.sdp.fields;
028:
029: import gov.nist.javax.sdp.*;
030: import gov.nist.core.*;
031: import java.util.*;
032:
033: /**
034: * Media field SDP header.
035: * @version JSR141-PUBLIC-REVIEW (subject to change).
036: *
037: *
038: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
039: *
040: */
041: public class MediaField extends SDPField {
042: /** Type of media. */
043: protected String media;
044: /** Current port number. */
045: protected int port;
046: /** Number of ports. */
047: protected int nports;
048: /** Protocol used. */
049: protected String proto;
050: /** Media formats. */
051: protected Vector formats;
052:
053: /**
054: * Copies the current instance.
055: * @return the copy of this object
056: */
057: public Object clone() {
058: MediaField retval = new MediaField();
059: retval.media = media;
060: retval.port = port;
061: retval.nports = nports;
062: retval.proto = proto;
063: for (int i = 0; i < formats.size(); i++) {
064: retval.formats.addElement(formats.elementAt(i));
065: }
066: return retval;
067: }
068:
069: /** Default constructor. */
070: public MediaField() {
071: super (SDPFieldNames.MEDIA_FIELD);
072: formats = new Vector();
073: }
074:
075: /**
076: * Gets the media type.
077: * @return the media type
078: */
079: public String getMedia() {
080: return media;
081: }
082:
083: /**
084: * Gets the port information.
085: * @return the current port number
086: */
087: public int getPort() {
088: return port;
089: }
090:
091: /**
092: * Gets the number of ports.
093: * @return the number of ports
094: */
095: public int getNports() {
096: return nports;
097: }
098:
099: /**
100: * Gets the protocl information.
101: * @return the protocol
102: */
103: public String getProto() {
104: return proto;
105: }
106:
107: /**
108: * Gets the vector of media formats
109: * @return the formats
110: */
111: public Vector getFormats() {
112: return formats;
113: }
114:
115: /**
116: * Sets the media member.
117: * @param m the new media type
118: */
119: public void setMedia(String m) {
120: media = m;
121: }
122:
123: /**
124: * Sets the port member.
125: * @param p the new port number
126: */
127: public void setPort(int p) {
128: port = p;
129: }
130:
131: /**
132: * Sets the number of ports member.
133: * @param n the new number of ports
134: */
135: public void setNports(int n) {
136: nports = n;
137: }
138:
139: /**
140: * Sets the protocol type member.
141: * @param p the protocol type
142: */
143: public void setProto(String p) {
144: proto = p;
145: }
146:
147: /**
148: * Sets the formats member.
149: * @param formats the new vector of media formats
150: */
151: public void setFormats(Vector formats) {
152: this .formats = formats;
153: }
154:
155: /**
156: * Returns the type (audio,video etc) of the
157: * media defined by this description.
158: * @throws SdpParseException if a parsing erro occurs
159: * @return the string media type.
160: */
161: public String getMediaType() throws SdpParseException {
162: return getMedia();
163: }
164:
165: /**
166: * Sets the type (audio,video etc) of the media defined by this description.
167: * @param mediaType to set
168: * @throws SdpException if mediaType is null
169: */
170: public void setMediaType(String mediaType) throws SdpException {
171: if (mediaType == null)
172: throw new SdpException("The mediaType is null");
173: else
174: setMedia(mediaType);
175: }
176:
177: /**
178: * Returns the port of the media defined by this description
179: * @throws SdpParseException if a parsing error occurs
180: * @return the integer media port.
181: */
182: public int getMediaPort() throws SdpParseException {
183: return getPort();
184: }
185:
186: /**
187: * Sets the port of the media defined by this description
188: * @param port to set
189: * @throws SdpException if th eport is null
190: */
191: public void setMediaPort(int port) throws SdpException {
192: if (port < 0)
193: throw new SdpException("The port is < 0");
194: else
195: setPort(port);
196: }
197:
198: /**
199: * Returns the number of ports associated with this media description
200: * @throws SdpParseException if a parsing error occurs
201: * @return the integer port count.
202: */
203: public int getPortCount() throws SdpParseException {
204: return getNports();
205: }
206:
207: /**
208: * Sets the number of ports associated with this media description.
209: * @param portCount portCount - the integer port count.
210: * @throws SdpException if the count is less than zero
211: */
212: public void setPortCount(int portCount) throws SdpException {
213: if (portCount < 0)
214: throw new SdpException("The port count is < 0");
215: else
216: setNports(portCount);
217: }
218:
219: /**
220: * Returns the protocol over which this media should be transmitted.
221: * @throws SdpParseException if a parsing error occurs
222: * @return the String protocol, e.g. RTP/AVP.
223: */
224: public String getProtocol() throws SdpParseException {
225: return getProto();
226: }
227:
228: /**
229: * Sets the protocol over which this media should be transmitted.
230: * @param protocol - the String protocol, e.g. RTP/AVP.
231: * @throws SdpException if the protocol is null
232: */
233: public void setProtocol(String protocol) throws SdpException {
234: if (protocol == null)
235: throw new SdpException("The protocol is null");
236: else
237: setProto(protocol);
238: }
239:
240: /**
241: * Returns an Vector of the media formats supported by this description.
242: * Each element in this Vector will be an String value which matches one of
243: * the a=rtpmap: attribute fields of the media description.
244: * @param create to set
245: * @throws SdpException
246: * @return the Vector.
247: */
248: public Vector getMediaFormats(boolean create)
249: throws SdpParseException {
250:
251: if (!create && formats.size() == 0)
252: return null;
253: else
254: return formats;
255: }
256:
257: /**
258: * Adds a media format to the media description.
259: * Each element in this Vector should be an String value which
260: * matches one of the
261: * a=rtpmap: attribute fields of the media description.
262: * @param mediaFormats the format to add.
263: * @throws SdpException if the vector is null
264: */
265: public void setMediaFormats(Vector mediaFormats)
266: throws SdpException {
267: if (mediaFormats == null)
268: throw new SdpException("The mediaFormats is null");
269: this .formats = mediaFormats;
270: }
271:
272: /**
273: * Gets the string encoded version of the media formats.
274: * @return encoded string of media formats contents
275: */
276: private String encodeFormats() {
277: String retval = "";
278: for (int i = 0; i < formats.size(); i++) {
279: retval += formats.elementAt(i);
280: if (i < formats.size() - 1)
281: retval += Separators.SP;
282: }
283: return retval;
284: }
285:
286: /**
287: * Gets the string encoded version of this object.
288: * @return encoded string of instance contents
289: * @since v1.0
290: */
291: public String encode() {
292: String encoded_string;
293: encoded_string = MEDIA_FIELD;
294: if (media != null)
295: encoded_string += media + Separators.SP + port;
296: // Leave out the nports parameter as this confuses the messenger.
297: if (nports > 1)
298: encoded_string += Separators.SLASH + nports;
299:
300: if (proto != null)
301: encoded_string += Separators.SP + proto;
302:
303: if (formats != null)
304: encoded_string += Separators.SP + encodeFormats();
305:
306: encoded_string += Separators.NEWLINE;
307: return encoded_string;
308: }
309: }
|