src/jlibrtp/RtcpPktBYE.java
changeset 214 2bf440c54ca5
parent 213 9bdff6cbd120
child 215 5db64229be69
equal deleted inserted replaced
213:9bdff6cbd120 214:2bf440c54ca5
     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 /**
       
    22  * RTCP packets for sending Bye messages
       
    23  * 
       
    24  * @author Arne Kepp
       
    25  */
       
    26 public class RtcpPktBYE extends RtcpPkt {
       
    27 	/** SSRCs saying bye, 32xn bits, n<16 */
       
    28 	protected long[] ssrcArray = null;
       
    29 	/** Optional reason */
       
    30 	protected byte[] reason = null;
       
    31 	
       
    32 	protected RtcpPktBYE(long[] ssrcs,byte[] aReason) {
       
    33 		super.packetType = 203;
       
    34 		// Fetch all the right stuff from the database
       
    35 		reason = aReason;
       
    36 		ssrcArray = ssrcs;
       
    37 		if(ssrcs.length < 1) {
       
    38 			System.out.println("RtcpBYE.RtcpPktBYE(long[] ssrcs, byte[] aReason) requires at least one SSRC!");
       
    39 		}
       
    40 	}
       
    41 	
       
    42 	protected RtcpPktBYE(byte[] aRawPkt, int start) {
       
    43 		rawPkt = aRawPkt;
       
    44 		if(!super.parseHeaders(start) || packetType != 203 ) {
       
    45 			if(RTPSession.rtpDebugLevel > 2) {
       
    46 				System.out.println(" <-> RtcpPktBYE.parseHeaders() etc. problem");
       
    47 			}
       
    48 			super.problem = -203;
       
    49 		} else {
       
    50 			ssrcArray = new long[super.itemCount];
       
    51 			
       
    52 			for(int i=0; i<super.itemCount; i++) {
       
    53 				ssrcArray[i] = StaticProcs.bytesToUIntLong(aRawPkt, start + (i+1)*4);
       
    54 			}
       
    55 			if(super.length > (super.itemCount + 1)) {
       
    56 				int reasonLength = (int) aRawPkt[start + (super.itemCount+1)*4];
       
    57 				//System.out.println("super.itemCount:"+super.itemCount+" reasonLength:"+reasonLength+" start:"+(super.itemCount*4 + 4 + 1));
       
    58 				reason = new byte[reasonLength];
       
    59 				System.arraycopy(aRawPkt, start + (super.itemCount + 1)*4 + 1, reason, 0, reasonLength);
       
    60 				//System.out.println("test:" + new String(reason));
       
    61 			}
       
    62 		}
       
    63 	}
       
    64 	
       
    65 	protected void encode() {			
       
    66 		itemCount = ssrcArray.length;
       
    67 		length = 4*ssrcArray.length;
       
    68 		
       
    69 		if(reason != null) {
       
    70 			length += (reason.length + 1)/4;
       
    71 			if((reason.length + 1) % 4 != 0) {
       
    72 				length +=1;
       
    73 			}
       
    74 		}
       
    75 		rawPkt = new byte[length*4 + 4];
       
    76 		
       
    77 		int i;
       
    78 		byte[] someBytes;
       
    79 		
       
    80 		// SSRCs
       
    81 		for(i=0; i<ssrcArray.length; i++ ) {
       
    82 			someBytes = StaticProcs.uIntLongToByteWord(ssrcArray[i]);
       
    83 			System.arraycopy(someBytes, 0, rawPkt, 4 + 4*i, 4);			
       
    84 		}
       
    85 		
       
    86 		// Reason for leaving
       
    87 		if(reason != null) {
       
    88 			//System.out.println("Writing to:"+(4+4*ssrcArray.length)+ " reason.length:"+reason.length );
       
    89 			rawPkt[(4 + 4*ssrcArray.length)] = (byte) reason.length;
       
    90 			System.arraycopy(reason, 0, rawPkt, 4+4*i +1, reason.length);		
       
    91 		}
       
    92 		super.writeHeaders();
       
    93 	}
       
    94 	
       
    95 	public void debugPrint() {
       
    96 		System.out.println("RtcpPktBYE.debugPrint() ");
       
    97 		if(ssrcArray != null) {
       
    98 			for(int i= 0; i<ssrcArray.length; i++) {
       
    99 				long anSsrc = ssrcArray[i];
       
   100 				System.out.println("     ssrc: " + anSsrc);
       
   101 			}
       
   102 		}
       
   103 		if(reason != null) {
       
   104 			System.out.println("     Reason: " + new String(reason));
       
   105 		}
       
   106 	}
       
   107 }