src/jlibrtp/RtcpPkt.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.InetAddress;
       
    22 
       
    23 /**
       
    24  * Common RTCP packet headers.
       
    25  * 
       
    26  * @author Arne Kepp
       
    27  */
       
    28 public class RtcpPkt {
       
    29 	/** Whether a problem has been encountered during parsing */
       
    30 	protected int problem = 0;
       
    31 	/** The version, always 2, 2 bits */
       
    32 	protected int version = 2;
       
    33 	/** Padding , 1 bit */
       
    34 	protected int padding = 0;
       
    35 	/** Number of items, e.g. receiver report blocks. Usage may vary. 5 bits */
       
    36 	protected int itemCount = 0;
       
    37 	/** The type of RTCP packet, 8 bits */
       
    38 	protected int packetType = -1;
       
    39 	/** The length of the RTCP packet, in 32 bit blocks minus 1. 16 bits */
       
    40 	protected int length = -1;
       
    41 	/** The ssrc that sent this, usually dictated by RTP Session */
       
    42 	protected long ssrc = -1;
       
    43 
       
    44 	/** Contains the actual data (eventually) */
       
    45 	protected byte[] rawPkt = null;
       
    46 
       
    47 	/** Only used for feedback messages: Time message was generated */
       
    48 	protected long time = -1;
       
    49 	/** Only used for feedback message: Whether this packet was received */
       
    50 	protected boolean received = false;
       
    51 
       
    52 	/**
       
    53 	 * Parses the common header of an RTCP packet
       
    54 	 * 
       
    55 	 * @param start
       
    56 	 *            where in this.rawPkt the headers start
       
    57 	 * @return true if parsing succeeded and header cheks
       
    58 	 */
       
    59 	protected boolean parseHeaders(int start) {
       
    60 		version = ((rawPkt[start + 0] & 0xC0) >>> 6);
       
    61 		padding = ((rawPkt[start + 0] & 0x20) >>> 5);
       
    62 		itemCount = (rawPkt[start + 0] & 0x1F);
       
    63 		packetType = (int) rawPkt[start + 1];
       
    64 		if (packetType < 0) {
       
    65 			packetType += 256;
       
    66 		}
       
    67 		length = StaticProcs.bytesToUIntInt(rawPkt, start + 2);
       
    68 
       
    69 		if (RTPSession.rtpDebugLevel > 9) {
       
    70 			System.out.println(" <-> RtcpPkt.parseHeaders() version:" + version
       
    71 					+ " padding:" + padding + " itemCount:" + itemCount
       
    72 					+ " packetType:" + packetType + " length:" + length);
       
    73 		}
       
    74 
       
    75 		if (packetType > 207 || packetType < 200)
       
    76 			System.out
       
    77 					.println("RtcpPkt.parseHeaders problem discovered, packetType "
       
    78 							+ packetType);
       
    79 
       
    80 		if (version == 2 && length < 65536) {
       
    81 			return true;
       
    82 		} else {
       
    83 			System.out
       
    84 					.println("RtcpPkt.parseHeaders() failed header checks, check size and version");
       
    85 			this.problem = -1;
       
    86 			return false;
       
    87 		}
       
    88 	}
       
    89 
       
    90 	/**
       
    91 	 * Writes the common header of RTCP packets. The values should be filled in
       
    92 	 * when the packet is initiliazed and this function called at the very end
       
    93 	 * of .encode()
       
    94 	 */
       
    95 	protected void writeHeaders() {
       
    96 		byte aByte = 0;
       
    97 		aByte |= (version << 6);
       
    98 		aByte |= (padding << 5);
       
    99 		aByte |= (itemCount);
       
   100 		rawPkt[0] = aByte;
       
   101 		aByte = 0;
       
   102 		aByte |= packetType;
       
   103 		rawPkt[1] = aByte;
       
   104 		if (rawPkt.length % 4 != 0)
       
   105 			System.out
       
   106 					.println("!!!! RtcpPkt.writeHeaders() rawPkt was not a multiple of 32 bits / 4 octets!");
       
   107 		StaticProcs.uIntIntToByteWord((rawPkt.length / 4) - 1, rawPkt, 2);
       
   108 	}
       
   109 
       
   110 	/**
       
   111 	 * This is just a dummy to make Eclipse complain less.
       
   112 	 */
       
   113 	protected void encode() {
       
   114 		System.out.println("RtcpPkt.encode() should never be invoked!! "
       
   115 				+ this.packetType);
       
   116 	}
       
   117 
       
   118 	/**
       
   119 	 * Check whether this packet came from the source we expected.
       
   120 	 * 
       
   121 	 * Not currently used!
       
   122 	 * 
       
   123 	 * @param adr
       
   124 	 *            address that packet came from
       
   125 	 * @param partDb
       
   126 	 *            the participant database for the session
       
   127 	 * @return true if this packet came from the expected source
       
   128 	 */
       
   129 	protected boolean check(InetAddress adr, ParticipantDatabase partDb) {
       
   130 		// Multicast -> We have to be naive
       
   131 		if (partDb.rtpSession.mcSession
       
   132 				&& adr.equals(partDb.rtpSession.mcGroup))
       
   133 			return true;
       
   134 
       
   135 		// See whether this participant is known
       
   136 		Participant part = partDb.getParticipant(this.ssrc);
       
   137 		if (part != null && part.rtcpAddress.getAddress().equals(adr))
       
   138 			return true;
       
   139 
       
   140 		// If not, we should look for someone without SSRC with his ip-address?
       
   141 		return false;
       
   142 	}
       
   143 }