001/** 002 * 003 * Copyright 2003-2005 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.jingleold.packet; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.Iterator; 022import java.util.List; 023import java.util.logging.Logger; 024 025import org.jivesoftware.smack.packet.ExtensionElement; 026 027import org.jivesoftware.smackx.jingleold.media.PayloadType; 028 029/** 030 * Jingle content description. 031 * 032 * @author Alvaro Saurin 033 */ 034public abstract class JingleDescription implements ExtensionElement { 035 036 private static final Logger LOGGER = Logger.getLogger(JingleDescription.class.getName()); 037 038 // static 039 040 public static final String NODENAME = "description"; 041 042 // non-static 043 044 private final List<PayloadType> payloads = new ArrayList<>(); 045 046 /** 047 * Creates a content description.. 048 */ 049 public JingleDescription() { 050 super(); 051 } 052 053 /** 054 * Returns the XML element name of the element. 055 * 056 * @return the XML element name of the element. 057 */ 058 @Override 059 public String getElementName() { 060 return NODENAME; 061 } 062 063 /** 064 * Return the namespace. 065 * 066 * @return The namespace 067 */ 068 @Override 069 public abstract String getNamespace(); 070 071 /** 072 * Adds a audio payload type to the packet. 073 * 074 * @param pt the audio payload type to add. 075 */ 076 public void addPayloadType(final PayloadType pt) { 077 synchronized (payloads) { 078 if (pt == null) { 079 LOGGER.severe("Null payload type"); 080 } else { 081 payloads.add(pt); 082 } 083 } 084 } 085 086 /** 087 * Adds a list of payloads to the packet. 088 * 089 * @param pts the payloads to add. 090 */ 091 public void addAudioPayloadTypes(final List<PayloadType> pts) { 092 synchronized (payloads) { 093 Iterator<PayloadType> ptIter = pts.iterator(); 094 while (ptIter.hasNext()) { 095 PayloadType.Audio pt = (PayloadType.Audio) ptIter.next(); 096 addPayloadType(new PayloadType.Audio(pt)); 097 } 098 } 099 } 100 101 /** 102 * Returns an Iterator for the audio payloads in the packet. 103 * 104 * @return an Iterator for the audio payloads in the packet. 105 */ 106 public Iterator<PayloadType> getPayloadTypes() { 107 return Collections.unmodifiableList(getPayloadTypesList()).iterator(); 108 } 109 110 /** 111 * Returns a list for the audio payloads in the packet. 112 * 113 * @return a list for the audio payloads in the packet. 114 */ 115 public List<PayloadType> getPayloadTypesList() { 116 synchronized (payloads) { 117 return new ArrayList<>(payloads); 118 } 119 } 120 121 /** 122 * Return the list of Payload types contained in the description. 123 * 124 * @return a list of PayloadType.Audio 125 */ 126 public List<PayloadType> getAudioPayloadTypesList() { 127 ArrayList<PayloadType> result = new ArrayList<>(); 128 Iterator<PayloadType> jinglePtsIter = getPayloadTypes(); 129 130 while (jinglePtsIter.hasNext()) { 131 PayloadType jpt = jinglePtsIter.next(); 132 if (jpt instanceof PayloadType.Audio) { 133 PayloadType.Audio jpta = (PayloadType.Audio) jpt; 134 result.add(jpta); 135 } 136 } 137 138 return result; 139 } 140 141 /** 142 * Returns a count of the audio payloads in the Jingle packet. 143 * 144 * @return the number of audio payloads in the Jingle packet. 145 */ 146 public int getPayloadTypesCount() { 147 synchronized (payloads) { 148 return payloads.size(); 149 } 150 } 151 152 /** 153 * Convert a Jingle description to XML. 154 * 155 * @return a string with the XML representation 156 */ 157 @Override 158 public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 159 StringBuilder buf = new StringBuilder(); 160 161 synchronized (payloads) { 162 if (payloads.size() > 0) { 163 buf.append('<').append(getElementName()); 164 buf.append(" xmlns=\"").append(getNamespace()).append("\" >"); 165 166 for (PayloadType payloadType : payloads) { 167 if (payloadType != null) { 168 buf.append(payloadType.toXML()); 169 } 170 } 171 buf.append("</").append(getElementName()).append('>'); 172 } 173 } 174 175 return buf.toString(); 176 } 177 178 /** 179 * Jingle audio description. 180 */ 181 public static class Audio extends JingleDescription { 182 183 public static final String NAMESPACE = "urn:xmpp:tmp:jingle:apps:rtp"; 184 185 public Audio() { 186 super(); 187 } 188 189 /** 190 * Utility constructor, with a PayloadType. 191 * 192 * @param pt the payload type. 193 */ 194 @SuppressWarnings("this-escape") 195 public Audio(final PayloadType pt) { 196 super(); 197 addPayloadType(pt); 198 } 199 200 @Override 201 public String getNamespace() { 202 return NAMESPACE; 203 } 204 } 205}