src/jlibrtp/RtcpPktSDES.java
changeset 13 e684f11070d5
equal deleted inserted replaced
12:c9ff263c29ad 13:e684f11070d5
       
     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:
       
    40 	 * Currently the added participants are not actually encoded
       
    41 	 * because the library lacks some support for acting as mixer or
       
    42 	 * relay in other areas.
       
    43 	 * 
       
    44 	 * @param reportThisSession include information from RTPSession as a participant
       
    45 	 * @param rtpSession the session itself
       
    46 	 * @param additionalParticipants additional participants to include
       
    47 	 */
       
    48 	protected RtcpPktSDES(boolean reportThisSession, RTPSession rtpSession, Participant[] additionalParticipants) {
       
    49 		super.packetType = 202;
       
    50 		// Fetch all the right stuff from the database
       
    51 		reportSelf = reportThisSession;
       
    52 		participants = additionalParticipants;
       
    53 		this.rtpSession = rtpSession; 
       
    54 	}
       
    55 	
       
    56 	/**
       
    57 	 * Constructor that parses a received packet
       
    58 	 * 
       
    59 	 * @param aRawPkt the byte[] containing the packet
       
    60 	 * @param start where in the byte[] this packet starts
       
    61 	 * @param socket the address from which the packet was received
       
    62 	 * @param partDb the participant database
       
    63 	 */
       
    64 	protected RtcpPktSDES(byte[] aRawPkt,int start, InetSocketAddress socket, ParticipantDatabase partDb) {
       
    65 		if(RTPSession.rtcpDebugLevel > 8) {
       
    66 			System.out.println("  -> RtcpPktSDES(byte[], ParticipantDabase)");
       
    67 		}
       
    68 		rawPkt = aRawPkt;
       
    69 
       
    70 		if(! super.parseHeaders(start) || packetType != 202 ) {
       
    71 			if(RTPSession.rtpDebugLevel > 2) {
       
    72 				System.out.println(" <-> RtcpPktSDES.parseHeaders() etc. problem");
       
    73 			}
       
    74 			super.problem = -202;
       
    75 		} else {
       
    76 			//System.out.println(" DECODE SIZE: " + super.length + " itemcount " + itemCount );
       
    77 			
       
    78 			int curPos = 4 + start;
       
    79 			int curLength;
       
    80 			int curType;
       
    81 			long ssrc;
       
    82 			boolean endReached = false;
       
    83 			boolean newPart;
       
    84 			this.participants = new Participant[itemCount];
       
    85 			
       
    86 			// Loop over SSRC SDES chunks
       
    87 			for(int i=0; i< itemCount; i++) {
       
    88 				ssrc = StaticProcs.bytesToUIntLong(aRawPkt, curPos);
       
    89 				Participant part = partDb.getParticipant(ssrc);
       
    90 				if(part == null) {
       
    91 					if(RTPSession.rtcpDebugLevel > 1) {
       
    92 						System.out.println("RtcpPktSDES(byte[], ParticipantDabase) adding new participant, ssrc:"+ssrc+" "+socket);
       
    93 					}
       
    94 					
       
    95 					part = new Participant(socket, socket , ssrc);
       
    96 					newPart = true;
       
    97 				} else {
       
    98 					newPart = false;
       
    99 				}
       
   100 				
       
   101 				curPos += 4;
       
   102 							
       
   103 				//System.out.println("PRE endReached " + endReached + " curPos: " + curPos + " length:" + this.length + (!endReached && (curPos/4) < this.length));
       
   104 				
       
   105 				while(!endReached && (curPos/4) <= this.length) {
       
   106 					//System.out.println("endReached " + endReached + " curPos: " + curPos + " length:" + this.length);
       
   107 					curType = (int) aRawPkt[curPos];
       
   108 					
       
   109 					if(curType == 0) {	
       
   110 						curPos += 4 - (curPos % 4);
       
   111 						endReached = true;
       
   112 					} else {
       
   113 						curLength  = (int) aRawPkt[curPos + 1];
       
   114 						//System.out.println("curPos:"+curPos+" curType:"+curType+" curLength:"+curLength+" read from:"+(curPos + 1));
       
   115 
       
   116 						if(curLength > 0) {
       
   117 							byte[] item = new byte[curLength];
       
   118 							//System.out.println("curPos:"+curPos+" arawPkt.length:"+aRawPkt.length+" curLength:"+curLength);
       
   119 							System.arraycopy(aRawPkt, curPos + 2, item, 0, curLength);
       
   120 							
       
   121 							switch(curType) {
       
   122 							case 1:  part.cname = new String(item); break;
       
   123 							case 2:  part.name = new String(item); break;
       
   124 							case 3:  part.email = new String(item); break;
       
   125 							case 4:  part.phone = new String(item); break;
       
   126 							case 5:  part.loc = new String(item); break;
       
   127 							case 6:  part.tool = new String(item); break;
       
   128 							case 7:  part.note = new String(item); break;
       
   129 							case 8:  part.priv = new String(item); break;
       
   130 							}
       
   131 							//System.out.println("TYPE " + curType + " value:" + new String(item) );
       
   132 							
       
   133 						} else {
       
   134 							switch(curType) {
       
   135 							case 1:  part.cname = null; break;
       
   136 							case 2:  part.name = null; break;
       
   137 							case 3:  part.email = null; break;
       
   138 							case 4:  part.phone = null; break;
       
   139 							case 5:  part.loc = null; break;
       
   140 							case 6:  part.tool = null; break;
       
   141 							case 7:  part.note = null; break;
       
   142 							case 8:  part.priv = null; break;
       
   143 							}
       
   144 							
       
   145 						}
       
   146 						curPos = curPos + curLength + 2;
       
   147 					}
       
   148 				}
       
   149 				
       
   150 				// Save the participant
       
   151 				this.participants[i] = part;
       
   152 				if(newPart)
       
   153 					partDb.addParticipant(2,part);
       
   154 				
       
   155 				//System.out.println("HEPPPPPP " + participants[i].cname );
       
   156 			}
       
   157 		}
       
   158 		if(RTPSession.rtcpDebugLevel > 8) {
       
   159 			System.out.println("  <- RtcpPktSDES()");
       
   160 		}
       
   161 	}
       
   162 
       
   163 	/**
       
   164 	 * Encode the packet into a byte[], saved in .rawPkt
       
   165 	 * 
       
   166 	 * CompRtcpPkt will call this automatically
       
   167 	 */
       
   168 	protected void encode() {	
       
   169 		byte[] temp = new byte[1450];
       
   170 		byte[] someBytes = StaticProcs.uIntLongToByteWord(this.rtpSession.ssrc);
       
   171 		System.arraycopy(someBytes, 0, temp, 4, 4);
       
   172 		int pos = 8;
       
   173 	
       
   174 		String tmpString = null;
       
   175 		for(int i=1; i<9;i++) {			
       
   176 			switch(i) {
       
   177 				case 1:  tmpString = this.rtpSession.cname; break;
       
   178 				case 2:  tmpString = this.rtpSession.name; break;
       
   179 				case 3:  tmpString = this.rtpSession.email; break;
       
   180 				case 4:  tmpString = this.rtpSession.phone; break;
       
   181 				case 5:  tmpString = this.rtpSession.loc; break;
       
   182 				case 6:  tmpString = this.rtpSession.tool; break;
       
   183 				case 7:  tmpString = this.rtpSession.note; break;
       
   184 				case 8:  tmpString = this.rtpSession.priv; break;
       
   185 			}
       
   186 			
       
   187 			if(tmpString != null) {
       
   188 				someBytes = tmpString.getBytes();
       
   189 				temp[pos] = (byte) i;
       
   190 				temp[pos+1] = (byte) someBytes.length;
       
   191 				System.arraycopy(someBytes, 0, temp, pos + 2, someBytes.length);
       
   192 				//System.out.println("i: "+i+" pos:"+pos+" someBytes.length:"+someBytes.length);
       
   193 				pos = pos + someBytes.length + 2;
       
   194 				//if(i == 1 ) {
       
   195 				//	System.out.println("trueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + tmpString);
       
   196 				//}
       
   197 			}
       
   198 		}
       
   199 		int leftover = pos % 4;
       
   200 		if(leftover == 1) {
       
   201 			temp[pos] = (byte) 0; 
       
   202 			temp[pos + 1] = (byte) 1; 
       
   203 			pos += 3;
       
   204 		} else if(leftover == 2) {
       
   205 			temp[pos] = (byte) 0; 
       
   206 			temp[pos + 1] = (byte) 0; 
       
   207 			pos += 2;
       
   208 		} else if(leftover == 3) {
       
   209 			temp[pos] = (byte) 0; 
       
   210 			temp[pos + 1] = (byte) 3; 
       
   211 			pos += 5;
       
   212 		}
       
   213 		
       
   214 		// TODO Here we ought to loop over participants, if we're doing SDES for other participants.
       
   215 		
       
   216 		super.rawPkt = new byte[pos];
       
   217 		itemCount = 1;
       
   218 		//This looks wrong, but appears to be fine..
       
   219 		System.arraycopy(temp, 0, super.rawPkt, 0, pos);
       
   220 		writeHeaders();
       
   221 	}
       
   222 	
       
   223 	/**
       
   224 	 * Debug purposes only
       
   225 	 */
       
   226 	public void debugPrint() {
       
   227 		System.out.println("RtcpPktSDES.debugPrint() ");
       
   228 		if(participants != null) {
       
   229 			for(int i= 0; i<participants.length; i++) {
       
   230 				Participant part = participants[i];
       
   231 				System.out.println("     part.ssrc: " + part.ssrc + "  part.cname: " + part.cname + " part.loc: " + part.loc);
       
   232 			}
       
   233 		} else {
       
   234 			System.out.println("     nothing to report (only valid for received packets)");
       
   235 		}
       
   236 	}
       
   237 }