src/jlibrtp/RtcpPktBYE.java
changeset 823 2036ebfaccda
equal deleted inserted replaced
536:537ddd8aa407 823:2036ebfaccda
       
     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
       
    39 					.println("RtcpBYE.RtcpPktBYE(long[] ssrcs, byte[] aReason) requires at least one SSRC!");
       
    40 		}
       
    41 	}
       
    42 
       
    43 	protected RtcpPktBYE(byte[] aRawPkt, int start) {
       
    44 		rawPkt = aRawPkt;
       
    45 		if (!super.parseHeaders(start) || packetType != 203) {
       
    46 			if (RTPSession.rtpDebugLevel > 2) {
       
    47 				System.out
       
    48 						.println(" <-> RtcpPktBYE.parseHeaders() etc. problem");
       
    49 			}
       
    50 			super.problem = -203;
       
    51 		} else {
       
    52 			ssrcArray = new long[super.itemCount];
       
    53 
       
    54 			for (int i = 0; i < super.itemCount; i++) {
       
    55 				ssrcArray[i] = StaticProcs.bytesToUIntLong(aRawPkt, start
       
    56 						+ (i + 1) * 4);
       
    57 			}
       
    58 			if (super.length > (super.itemCount + 1)) {
       
    59 				int reasonLength = (int) aRawPkt[start + (super.itemCount + 1)
       
    60 						* 4];
       
    61 				// System.out.println("super.itemCount:"+super.itemCount+" reasonLength:"+reasonLength+" start:"+(super.itemCount*4
       
    62 				// + 4 + 1));
       
    63 				reason = new byte[reasonLength];
       
    64 				System.arraycopy(aRawPkt,
       
    65 						start + (super.itemCount + 1) * 4 + 1, reason, 0,
       
    66 						reasonLength);
       
    67 				// System.out.println("test:" + new String(reason));
       
    68 			}
       
    69 		}
       
    70 	}
       
    71 
       
    72 	protected void encode() {
       
    73 		itemCount = ssrcArray.length;
       
    74 		length = 4 * ssrcArray.length;
       
    75 
       
    76 		if (reason != null) {
       
    77 			length += (reason.length + 1) / 4;
       
    78 			if ((reason.length + 1) % 4 != 0) {
       
    79 				length += 1;
       
    80 			}
       
    81 		}
       
    82 		rawPkt = new byte[length * 4 + 4];
       
    83 
       
    84 		int i;
       
    85 		byte[] someBytes;
       
    86 
       
    87 		// SSRCs
       
    88 		for (i = 0; i < ssrcArray.length; i++) {
       
    89 			someBytes = StaticProcs.uIntLongToByteWord(ssrcArray[i]);
       
    90 			System.arraycopy(someBytes, 0, rawPkt, 4 + 4 * i, 4);
       
    91 		}
       
    92 
       
    93 		// Reason for leaving
       
    94 		if (reason != null) {
       
    95 			// System.out.println("Writing to:"+(4+4*ssrcArray.length)+
       
    96 			// " reason.length:"+reason.length );
       
    97 			rawPkt[(4 + 4 * ssrcArray.length)] = (byte) reason.length;
       
    98 			System.arraycopy(reason, 0, rawPkt, 4 + 4 * i + 1, reason.length);
       
    99 		}
       
   100 		super.writeHeaders();
       
   101 	}
       
   102 
       
   103 	public void debugPrint() {
       
   104 		System.out.println("RtcpPktBYE.debugPrint() ");
       
   105 		if (ssrcArray != null) {
       
   106 			for (int i = 0; i < ssrcArray.length; i++) {
       
   107 				long anSsrc = ssrcArray[i];
       
   108 				System.out.println("     ssrc: " + anSsrc);
       
   109 			}
       
   110 		}
       
   111 		if (reason != null) {
       
   112 			System.out.println("     Reason: " + new String(reason));
       
   113 		}
       
   114 	}
       
   115 }