src/jlibrtp/RtcpPktSDES.java
changeset 834 e8d6255306f8
parent 833 f5a5d9237d69
child 835 4e40f3481f23
equal deleted inserted replaced
833:f5a5d9237d69 834:e8d6255306f8
     1 /**
       
     2  * Java RTP Library (jlibrtp)
       
     3  * Copyright (C) 2006 Arne Kepp
       
     4  * 
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Lesser General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2.1 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Lesser General Public License for more details.
       
    14  * 
       
    15  * You should have received a copy of the GNU Lesser General Public
       
    16  * License along with this library; if not, write to the Free Software
       
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
       
    18  */
       
    19 package jlibrtp;
       
    20 
       
    21 import java.net.InetSocketAddress;
       
    22 
       
    23 /**
       
    24  * RTCP packets for Source Descriptions
       
    25  * 
       
    26  * @author Arne Kepp
       
    27  */
       
    28 public class RtcpPktSDES extends RtcpPkt {
       
    29 	/** Whether the RTP Session object should be inclduded */
       
    30 	boolean reportSelf = true;
       
    31 	/** The parent RTP Session object, holds participant database */
       
    32 	RTPSession rtpSession = null;
       
    33 	/** The participants to create SDES packets for */
       
    34 	protected Participant[] participants = null;
       
    35 
       
    36 	/**
       
    37 	 * Constructor to create a new SDES packet
       
    38 	 * 
       
    39 	 * TODO: Currently the added participants are not actually encoded because
       
    40 	 * the library lacks some support for acting as mixer or relay in other
       
    41 	 * areas.
       
    42 	 * 
       
    43 	 * @param reportThisSession
       
    44 	 *            include information from RTPSession as a participant
       
    45 	 * @param rtpSession
       
    46 	 *            the session itself
       
    47 	 * @param additionalParticipants
       
    48 	 *            additional participants to include
       
    49 	 */
       
    50 	protected RtcpPktSDES(boolean reportThisSession, RTPSession rtpSession,
       
    51 			Participant[] additionalParticipants) {
       
    52 		super.packetType = 202;
       
    53 		// Fetch all the right stuff from the database
       
    54 		reportSelf = reportThisSession;
       
    55 		participants = additionalParticipants;
       
    56 		this.rtpSession = rtpSession;
       
    57 	}
       
    58 
       
    59 	/**
       
    60 	 * Constructor that parses a received packet
       
    61 	 * 
       
    62 	 * @param aRawPkt
       
    63 	 *            the byte[] containing the packet
       
    64 	 * @param start
       
    65 	 *            where in the byte[] this packet starts
       
    66 	 * @param socket
       
    67 	 *            the address from which the packet was received
       
    68 	 * @param partDb
       
    69 	 *            the participant database
       
    70 	 */
       
    71 	protected RtcpPktSDES(byte[] aRawPkt, int start, InetSocketAddress socket,
       
    72 			ParticipantDatabase partDb) {
       
    73 		if (RTPSession.rtcpDebugLevel > 8) {
       
    74 			System.out.println("  -> RtcpPktSDES(byte[], ParticipantDabase)");
       
    75 		}
       
    76 		rawPkt = aRawPkt;
       
    77 
       
    78 		if (!super.parseHeaders(start) || packetType != 202) {
       
    79 			if (RTPSession.rtpDebugLevel > 2) {
       
    80 				System.out
       
    81 						.println(" <-> RtcpPktSDES.parseHeaders() etc. problem");
       
    82 			}
       
    83 			super.problem = -202;
       
    84 		} else {
       
    85 			// System.out.println(" DECODE SIZE: " + super.length +
       
    86 			// " itemcount " + itemCount );
       
    87 
       
    88 			int curPos = 4 + start;
       
    89 			int curLength;
       
    90 			int curType;
       
    91 			long ssrc;
       
    92 			boolean endReached = false;
       
    93 			boolean newPart;
       
    94 			this.participants = new Participant[itemCount];
       
    95 
       
    96 			// Loop over SSRC SDES chunks
       
    97 			for (int i = 0; i < itemCount; i++) {
       
    98 				ssrc = StaticProcs.bytesToUIntLong(aRawPkt, curPos);
       
    99 				Participant part = partDb.getParticipant(ssrc);
       
   100 				if (part == null) {
       
   101 					if (RTPSession.rtcpDebugLevel > 1) {
       
   102 						System.out
       
   103 								.println("RtcpPktSDES(byte[], ParticipantDabase) adding new participant, ssrc:"
       
   104 										+ ssrc + " " + socket);
       
   105 					}
       
   106 
       
   107 					part = new Participant(socket, socket, ssrc);
       
   108 					newPart = true;
       
   109 				} else {
       
   110 					newPart = false;
       
   111 				}
       
   112 
       
   113 				curPos += 4;
       
   114 
       
   115 				// System.out.println("PRE endReached " + endReached +
       
   116 				// " curPos: " + curPos + " length:" + this.length +
       
   117 				// (!endReached && (curPos/4) < this.length));
       
   118 
       
   119 				while (!endReached && (curPos / 4) <= this.length) {
       
   120 					// System.out.println("endReached " + endReached +
       
   121 					// " curPos: " + curPos + " length:" + this.length);
       
   122 					curType = (int) aRawPkt[curPos];
       
   123 
       
   124 					if (curType == 0) {
       
   125 						curPos += 4 - (curPos % 4);
       
   126 						endReached = true;
       
   127 					} else {
       
   128 						curLength = (int) aRawPkt[curPos + 1];
       
   129 						// System.out.println("curPos:"+curPos+" curType:"+curType+" curLength:"+curLength+" read from:"+(curPos
       
   130 						// + 1));
       
   131 
       
   132 						if (curLength > 0) {
       
   133 							byte[] item = new byte[curLength];
       
   134 							// System.out.println("curPos:"+curPos+" arawPkt.length:"+aRawPkt.length+" curLength:"+curLength);
       
   135 							System.arraycopy(aRawPkt, curPos + 2, item, 0,
       
   136 									curLength);
       
   137 
       
   138 							switch (curType) {
       
   139 							case 1:
       
   140 								part.cname = new String(item);
       
   141 								break;
       
   142 							case 2:
       
   143 								part.name = new String(item);
       
   144 								break;
       
   145 							case 3:
       
   146 								part.email = new String(item);
       
   147 								break;
       
   148 							case 4:
       
   149 								part.phone = new String(item);
       
   150 								break;
       
   151 							case 5:
       
   152 								part.loc = new String(item);
       
   153 								break;
       
   154 							case 6:
       
   155 								part.tool = new String(item);
       
   156 								break;
       
   157 							case 7:
       
   158 								part.note = new String(item);
       
   159 								break;
       
   160 							case 8:
       
   161 								part.priv = new String(item);
       
   162 								break;
       
   163 							}
       
   164 							// System.out.println("TYPE " + curType + " value:"
       
   165 							// + new String(item) );
       
   166 
       
   167 						} else {
       
   168 							switch (curType) {
       
   169 							case 1:
       
   170 								part.cname = null;
       
   171 								break;
       
   172 							case 2:
       
   173 								part.name = null;
       
   174 								break;
       
   175 							case 3:
       
   176 								part.email = null;
       
   177 								break;
       
   178 							case 4:
       
   179 								part.phone = null;
       
   180 								break;
       
   181 							case 5:
       
   182 								part.loc = null;
       
   183 								break;
       
   184 							case 6:
       
   185 								part.tool = null;
       
   186 								break;
       
   187 							case 7:
       
   188 								part.note = null;
       
   189 								break;
       
   190 							case 8:
       
   191 								part.priv = null;
       
   192 								break;
       
   193 							}
       
   194 
       
   195 						}
       
   196 						curPos = curPos + curLength + 2;
       
   197 					}
       
   198 				}
       
   199 
       
   200 				// Save the participant
       
   201 				this.participants[i] = part;
       
   202 				if (newPart)
       
   203 					partDb.addParticipant(2, part);
       
   204 
       
   205 				// System.out.println("HEPPPPPP " + participants[i].cname );
       
   206 			}
       
   207 		}
       
   208 		if (RTPSession.rtcpDebugLevel > 8) {
       
   209 			System.out.println("  <- RtcpPktSDES()");
       
   210 		}
       
   211 	}
       
   212 
       
   213 	/**
       
   214 	 * Encode the packet into a byte[], saved in .rawPkt
       
   215 	 * 
       
   216 	 * CompRtcpPkt will call this automatically
       
   217 	 */
       
   218 	protected void encode() {
       
   219 		byte[] temp = new byte[1450];
       
   220 		byte[] someBytes = StaticProcs.uIntLongToByteWord(this.rtpSession.ssrc);
       
   221 		System.arraycopy(someBytes, 0, temp, 4, 4);
       
   222 		int pos = 8;
       
   223 
       
   224 		String tmpString = null;
       
   225 		for (int i = 1; i < 9; i++) {
       
   226 			switch (i) {
       
   227 			case 1:
       
   228 				tmpString = this.rtpSession.cname;
       
   229 				break;
       
   230 			case 2:
       
   231 				tmpString = this.rtpSession.name;
       
   232 				break;
       
   233 			case 3:
       
   234 				tmpString = this.rtpSession.email;
       
   235 				break;
       
   236 			case 4:
       
   237 				tmpString = this.rtpSession.phone;
       
   238 				break;
       
   239 			case 5:
       
   240 				tmpString = this.rtpSession.loc;
       
   241 				break;
       
   242 			case 6:
       
   243 				tmpString = this.rtpSession.tool;
       
   244 				break;
       
   245 			case 7:
       
   246 				tmpString = this.rtpSession.note;
       
   247 				break;
       
   248 			case 8:
       
   249 				tmpString = this.rtpSession.priv;
       
   250 				break;
       
   251 			}
       
   252 
       
   253 			if (tmpString != null) {
       
   254 				someBytes = tmpString.getBytes();
       
   255 				temp[pos] = (byte) i;
       
   256 				temp[pos + 1] = (byte) someBytes.length;
       
   257 				System.arraycopy(someBytes, 0, temp, pos + 2, someBytes.length);
       
   258 				// System.out.println("i: "+i+" pos:"+pos+" someBytes.length:"+someBytes.length);
       
   259 				pos = pos + someBytes.length + 2;
       
   260 				// if(i == 1 ) {
       
   261 				// System.out.println("trueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" +
       
   262 				// tmpString);
       
   263 				// }
       
   264 			}
       
   265 		}
       
   266 		int leftover = pos % 4;
       
   267 		if (leftover == 1) {
       
   268 			temp[pos] = (byte) 0;
       
   269 			temp[pos + 1] = (byte) 1;
       
   270 			pos += 3;
       
   271 		} else if (leftover == 2) {
       
   272 			temp[pos] = (byte) 0;
       
   273 			temp[pos + 1] = (byte) 0;
       
   274 			pos += 2;
       
   275 		} else if (leftover == 3) {
       
   276 			temp[pos] = (byte) 0;
       
   277 			temp[pos + 1] = (byte) 3;
       
   278 			pos += 5;
       
   279 		}
       
   280 
       
   281 		// TODO Here we ought to loop over participants, if we're doing SDES for
       
   282 		// other participants.
       
   283 
       
   284 		super.rawPkt = new byte[pos];
       
   285 		itemCount = 1;
       
   286 		// This looks wrong, but appears to be fine..
       
   287 		System.arraycopy(temp, 0, super.rawPkt, 0, pos);
       
   288 		writeHeaders();
       
   289 	}
       
   290 
       
   291 	/**
       
   292 	 * Debug purposes only
       
   293 	 */
       
   294 	public void debugPrint() {
       
   295 		System.out.println("RtcpPktSDES.debugPrint() ");
       
   296 		if (participants != null) {
       
   297 			for (int i = 0; i < participants.length; i++) {
       
   298 				Participant part = participants[i];
       
   299 				System.out.println("     part.ssrc: " + part.ssrc
       
   300 						+ "  part.cname: " + part.cname + " part.loc: "
       
   301 						+ part.loc);
       
   302 			}
       
   303 		} else {
       
   304 			System.out
       
   305 					.println("     nothing to report (only valid for received packets)");
       
   306 		}
       
   307 	}
       
   308 }