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