src/jlibrtp/RtcpPkt.java
author Da Risk <darisk972@gmail.com>
Tue, 14 Apr 2009 16:56:20 +0200
changeset 105 c6e4728ac9f7
parent 13 e684f11070d5
permissions -rw-r--r--
Passage sous cupcake :) Peu de modification de code, il faut juste creer des fichier aidl pour les classes parcelables. Sinon les fichier de build.xml ont ete completement modifiés, j'ai remplacé par les nouveaux. (il doit y avoir un manque de precision dans le fichier build.properties)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     1
/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     2
 * Java RTP Library (jlibrtp)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     3
 * Copyright (C) 2006 Arne Kepp
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     4
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     5
 * This library is free software; you can redistribute it and/or
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     6
 * modify it under the terms of the GNU Lesser General Public
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     7
 * License as published by the Free Software Foundation; either
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     8
 * version 2.1 of the License, or (at your option) any later version.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     9
 *
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    10
 * This library is distributed in the hope that it will be useful,
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    13
 * Lesser General Public License for more details.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    14
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    15
 * You should have received a copy of the GNU Lesser General Public
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    16
 * License along with this library; if not, write to the Free Software
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    18
 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    19
package jlibrtp;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    20
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    21
import java.net.InetAddress;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    22
/** 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    23
 * Common RTCP packet headers.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    24
 *
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    25
 * @author Arne Kepp
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    26
 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    27
public class RtcpPkt {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    28
	/** Whether a problem has been encountered during parsing */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    29
	protected int problem = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    30
	/** The version, always 2, 2 bits */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    31
	protected int version = 2;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    32
	/** Padding , 1 bit */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    33
	protected int padding = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    34
	/** Number of items, e.g. receiver report blocks. Usage may vary. 5 bits */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    35
	protected int itemCount = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    36
	/** The type of RTCP packet, 8 bits */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    37
	protected int packetType = -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    38
	/** The length of the RTCP packet, in 32 bit blocks minus 1. 16 bits*/
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    39
	protected int length = -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    40
	/** The ssrc that sent this, usually dictated by RTP Session */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    41
	protected long ssrc = -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    42
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    43
	/** Contains the actual data (eventually) */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    44
	protected byte[] rawPkt = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    45
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    46
	/** Only used for feedback messages: Time message was generated */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    47
	protected long time = -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    48
	/** Only used for feedback message: Whether this packet was received */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    49
	protected boolean received = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    50
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    51
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    52
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    53
	 * Parses the common header of an RTCP packet
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    54
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    55
	 * @param start where in this.rawPkt the headers start
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    56
	 * @return true if parsing succeeded and header cheks 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    57
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    58
	protected boolean parseHeaders(int start) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    59
		version = ((rawPkt[start+0] & 0xC0) >>> 6);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    60
		padding = ((rawPkt[start+0] & 0x20) >>> 5);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    61
		itemCount = (rawPkt[start+0] & 0x1F);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    62
		packetType = (int) rawPkt[start+1];
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    63
		if(packetType < 0) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    64
			packetType += 256;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    65
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    66
		length = StaticProcs.bytesToUIntInt(rawPkt, start+2);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    67
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    68
		if(RTPSession.rtpDebugLevel > 9) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    69
			System.out.println(" <-> RtcpPkt.parseHeaders() version:"+version+" padding:"+padding+" itemCount:"+itemCount
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    70
					+" packetType:"+packetType+" length:"+length);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    71
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    72
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    73
		if(packetType > 207 || packetType < 200) 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    74
			System.out.println("RtcpPkt.parseHeaders problem discovered, packetType " + packetType);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    75
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    76
		if(version == 2 && length < 65536) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    77
			return true;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    78
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    79
			System.out.println("RtcpPkt.parseHeaders() failed header checks, check size and version");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    80
			this.problem = -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    81
			return false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    82
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    83
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    84
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    85
	 * Writes the common header of RTCP packets. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    86
	 * The values should be filled in when the packet is initiliazed and this function
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    87
	 * called at the very end of .encode()
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    88
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    89
	protected void writeHeaders() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    90
		byte aByte = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    91
		aByte |=(version << 6);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    92
		aByte |=(padding << 5);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    93
		aByte |=(itemCount);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    94
		rawPkt[0] = aByte;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    95
		aByte = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    96
		aByte |= packetType;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    97
		rawPkt[1] = aByte;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    98
		if(rawPkt.length % 4 != 0)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    99
			System.out.println("!!!! RtcpPkt.writeHeaders() rawPkt was not a multiple of 32 bits / 4 octets!");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   100
		byte[] someBytes = StaticProcs.uIntIntToByteWord((rawPkt.length / 4) - 1);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   101
		rawPkt[2] = someBytes[0];
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   102
		rawPkt[3] = someBytes[1];
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   103
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   104
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   105
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   106
	 * This is just a dummy to make Eclipse complain less.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   107
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   108
	protected void encode() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   109
		System.out.println("RtcpPkt.encode() should never be invoked!! " + this.packetType);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   110
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   111
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   112
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   113
	 * Check whether this packet came from the source we expected.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   114
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   115
	 * Not currently used!
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   116
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   117
	 * @param adr address that packet came from
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   118
	 * @param partDb the participant database for the session
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   119
	 * @return true if this packet came from the expected source
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   120
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   121
	protected boolean check(InetAddress adr, ParticipantDatabase partDb) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   122
		//Multicast -> We have to be naive
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   123
		if (partDb.rtpSession.mcSession && adr.equals(partDb.rtpSession.mcGroup))
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   124
			return true;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   125
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   126
		//See whether this participant is known
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   127
		Participant part = partDb.getParticipant(this.ssrc);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   128
		if(part != null && part.rtcpAddress.getAddress().equals(adr))
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   129
			return true;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   130
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   131
		//If not, we should look for someone without SSRC with his ip-address?
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   132
		return false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   133
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   134
}