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