src/jlibrtp/RtcpPktRTPFB.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 RTP Feedback Messages 
       
    23  * 
       
    24  * In line with RFC 4585, this packet currently only supports NACKs
       
    25  * 
       
    26  * @author Arne Kepp
       
    27  */
       
    28 public class RtcpPktRTPFB extends RtcpPkt {
       
    29 	/** If this packet was for a different SSRC */
       
    30 	protected boolean notRelevant = false;
       
    31 	/** SSRC we are sending feeback to */
       
    32 	protected long ssrcMediaSource = -1;
       
    33 	/** RTP sequence numbers of lost packets */
       
    34 	protected int PID[];
       
    35 	/** bitmask of following lost packets, shared index with PID */
       
    36 	protected int BLP[];
       
    37 	
       
    38 	/**
       
    39 	 * Constructor for RTP Feedback Message
       
    40 	 * 
       
    41 	 * @param ssrcPacketSender SSRC of sender, taken from RTPSession
       
    42 	 * @param ssrcMediaSource SSRC of recipient of this message
       
    43 	 * @param FMT the Feedback Message Subtype
       
    44 	 * @param PID RTP sequence numbers of lost packets
       
    45 	 * @param BLP bitmask of following lost packets, shared index with PID 
       
    46 	 */
       
    47 	protected RtcpPktRTPFB(long ssrcPacketSender, long ssrcMediaSource, int FMT, int[] PID, int[] BLP) {
       
    48 		super.packetType = 205; //RTPFB
       
    49 		super.itemCount = FMT; 
       
    50 		this.PID = PID;
       
    51 		this.BLP = BLP;
       
    52 	}
       
    53 	
       
    54 	/**
       
    55 	 * Constructor that parses a raw packet to retrieve information
       
    56 	 * 
       
    57 	 * @param aRawPkt the raw packet to be parsed
       
    58 	 * @param start the start of the packet, in bytes
       
    59 	 * @param rtpSession the session on which the callback interface resides
       
    60 	 */
       
    61 	protected RtcpPktRTPFB(byte[] aRawPkt, int start, RTPSession rtpSession) {		
       
    62 		if(RTPSession.rtpDebugLevel > 8) {
       
    63 			System.out.println("  -> RtcpPktRTPFB(byte[], int start)");
       
    64 		}
       
    65 		
       
    66 		rawPkt = aRawPkt;
       
    67 
       
    68 		if(! super.parseHeaders(start) || packetType != 205 || super.length < 2) {
       
    69 			if(RTPSession.rtpDebugLevel > 2) {
       
    70 				System.out.println(" <-> RtcpPktRTPFB.parseHeaders() etc. problem");
       
    71 			}
       
    72 			super.problem = -205;
       
    73 		} else {
       
    74 			//FMT = super.itemCount;
       
    75 			
       
    76 			ssrcMediaSource = StaticProcs.bytesToUIntLong(aRawPkt,8+start);
       
    77 			
       
    78 			if(ssrcMediaSource == rtpSession.ssrc) {
       
    79 				super.ssrc = StaticProcs.bytesToUIntLong(aRawPkt,4+start);
       
    80 				int loopStop = super.length - 2;
       
    81 				PID = new int[loopStop];
       
    82 				BLP = new int[loopStop];
       
    83 				int curStart = 12;
       
    84 
       
    85 				// Loop over Feedback Control Information (FCI) fields
       
    86 				for(int i=0; i< loopStop; i++) {
       
    87 					PID[i] = StaticProcs.bytesToUIntInt(aRawPkt, curStart);
       
    88 					BLP[i] = StaticProcs.bytesToUIntInt(aRawPkt, curStart + 2);
       
    89 					curStart += 4;
       
    90 				}
       
    91 
       
    92 				rtpSession.rtcpAVPFIntf.RTPFBPktReceived(
       
    93 						super.ssrc, super.itemCount, PID, BLP);
       
    94 			}
       
    95 		}
       
    96 		
       
    97 
       
    98 		
       
    99 		if(RTPSession.rtpDebugLevel > 8) {
       
   100 			System.out.println("  <- RtcpPktRTPFB()");
       
   101 		}
       
   102 	}
       
   103 	
       
   104 	/**
       
   105 	 * Encode the packet into a byte[], saved in .rawPkt
       
   106 	 * 
       
   107 	 * CompRtcpPkt will call this automatically
       
   108 	 */
       
   109 	protected void encode() {
       
   110 		super.rawPkt = new byte[12 + this.PID.length*4];
       
   111 		
       
   112 		byte[] someBytes = StaticProcs.uIntLongToByteWord(super.ssrc);
       
   113 		System.arraycopy(someBytes, 0, super.rawPkt, 4, 4);
       
   114 		someBytes = StaticProcs.uIntLongToByteWord(this.ssrcMediaSource);
       
   115 		System.arraycopy(someBytes, 0, super.rawPkt, 8, 4);
       
   116 		
       
   117 		// Loop over Feedback Control Information (FCI) fields
       
   118 		int curStart = 12;
       
   119 		for(int i=0; i < this.PID.length; i++ ) {
       
   120 			someBytes = StaticProcs.uIntIntToByteWord(PID[i]);
       
   121 			super.rawPkt[curStart++] = someBytes[0];
       
   122 			super.rawPkt[curStart++] = someBytes[1];
       
   123 			someBytes = StaticProcs.uIntIntToByteWord(BLP[i]);
       
   124 			super.rawPkt[curStart++] = someBytes[0];
       
   125 			super.rawPkt[curStart++] = someBytes[1];
       
   126 		}
       
   127 		writeHeaders();
       
   128 	}
       
   129 	
       
   130 	/** 
       
   131 	 * Get the FMT (Feedback Message Type)
       
   132 	 * @return value stored in .itemcount, same field
       
   133 	 */
       
   134 	protected int getFMT() {
       
   135 		return this.itemCount;
       
   136 	}
       
   137 	
       
   138 	/**
       
   139 	 * Debug purposes only
       
   140 	 */
       
   141 	protected void debugPrint() {
       
   142 		System.out.println("->RtcpPktRTPFB.debugPrint() ");
       
   143 		System.out.println("  ssrcPacketSender: " + super.ssrc + "  ssrcMediaSource: " + ssrcMediaSource);
       
   144 		
       
   145 		if(this.PID != null && this.PID.length < 1) {
       
   146 			System.out.println("  No Feedback Control Information (FCI) fields");
       
   147 		}
       
   148 		
       
   149 		for(int i=0; i < this.PID.length; i++ ) {
       
   150 			System.out.println("  FCI -> PID: " + PID[i] + "  BLP: " + BLP[i]);
       
   151 		}
       
   152 		System.out.println("<-RtcpPktRTPFB.debugPrint() ");
       
   153 	}
       
   154 }