src/jlibrtp/DataFrame.java
changeset 823 2036ebfaccda
equal deleted inserted replaced
536:537ddd8aa407 823:2036ebfaccda
       
     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 org.sipdroid.net.tools.DatagramPool;
       
    22 import org.sipdroid.net.tools.PktBufNodePool;
       
    23 import org.sipdroid.net.tools.RtpPktPool;
       
    24 
       
    25 /**
       
    26  * Data structure to hold a complete frame if frame reconstruction is enabled,
       
    27  * or the data from an individual packet if it is not
       
    28  * 
       
    29  * It also contains most of the data from the individual packets that it is
       
    30  * based on.
       
    31  * 
       
    32  * @author Arne Kepp
       
    33  */
       
    34 public class DataFrame {
       
    35 	/** The share RTP timestamp */
       
    36 	private long rtpTimestamp;
       
    37 	/** The calculated UNIX timestamp, guessed after 2 Sender Reports */
       
    38 	private long timestamp = -1;
       
    39 	/** the SSRC from which this frame originated */
       
    40 	private long SSRC;
       
    41 	/** contributing CSRCs, only read from the first packet */
       
    42 	private long[] CSRCs;
       
    43 	/** RTP payload type */
       
    44 	private int payloadType;
       
    45 	/** The marks on individual packets, ordered */
       
    46 	//private boolean[] marks;
       
    47 	/** Whether any packets were marked or not */
       
    48 	private boolean anyMarked = false;
       
    49 	/** Whether the frame contains the expected number of packets */
       
    50 	private int isComplete = 0;
       
    51 	// private int dataLength;
       
    52 	/** The data from the individual packets, ordered */
       
    53 	//private byte[][] data;
       
    54 	/** The sequence numbers of the individual packets, ordered */
       
    55 	//private int[] seqNum;
       
    56 	/** The total amount of data bytes in this frame */
       
    57 	private int totalLength = 0;
       
    58 	/** The last sequence number in this frame */
       
    59 	protected int lastSeqNum;
       
    60 	/** The first sequence number in this frame */
       
    61 	protected int firstSeqNum;
       
    62 	/** The number of packets expected for a complete frame */
       
    63 	protected int noPkts;
       
    64 	private RtpPkt[] pkts = new RtpPkt[5];
       
    65 
       
    66 	/**
       
    67 	 * The usual way to construct a frame is by giving it a PktBufNode, which
       
    68 	 * contains links to all the other pkts that make it up.
       
    69 	 */
       
    70 	protected DataFrame(PktBufNode aBufNode, Participant p, int noPkts) {
       
    71 		initDataFrame(aBufNode, p, noPkts);
       
    72 	}
       
    73 
       
    74 	protected void initDataFrame(PktBufNode aBufNode, Participant p, int noPkts) {
       
    75 		if (RTPSession.rtpDebugLevel > 6) {
       
    76 			System.out.println("-> DataFrame(PktBufNode, noPkts = " + noPkts
       
    77 					+ ")");
       
    78 		}
       
    79 		this.noPkts = noPkts;
       
    80 		RtpPkt aPkt = aBufNode.pkt;
       
    81 		int pktCount = aBufNode.pktCount;
       
    82 		firstSeqNum = aBufNode.pktCount;
       
    83 		PktBufNode tempNode;
       
    84 
       
    85 		// All this data should be shared, so we just get it from the first one
       
    86 		this.rtpTimestamp = aBufNode.timeStamp;
       
    87 		SSRC = aPkt.getSsrc();
       
    88 		CSRCs = aPkt.getCsrcArray();
       
    89 
       
    90 		// Check whether we can compute an NTPish timestamp? Requires two SR
       
    91 		// reports
       
    92 		if (p.ntpGradient > 0) {
       
    93 			// System.out.print(Long.toString(p.ntpOffset)+" "
       
    94 			timestamp = p.ntpOffset
       
    95 			+ (long) (p.ntpGradient * (double) (this.rtpTimestamp - p.lastSRRtpTs));
       
    96 		}
       
    97 
       
    98 		// Make data the right length
       
    99 		int payloadLength = aPkt.getPayloadLength();
       
   100 		//seqNum = new int[aBufNode.pktCount];
       
   101 		//marks = new boolean[aBufNode.pktCount];
       
   102 		if (pktCount > 5) {
       
   103 			System.out.println("PKT COUNT TOO HIGH " + pktCount);
       
   104 		}
       
   105 		// Concatenate the data of the packets
       
   106 		int i;
       
   107 		for (i = 0; i < pktCount; i++) {
       
   108 			aPkt = aBufNode.pkt;
       
   109 			pkts[i] = aPkt;
       
   110 			// System.out.println("i " + i + " seqNum[i] " + seqNum[i] +
       
   111 			// " aBufNode" + aBufNode);
       
   112 			//seqNum[i] = aBufNode.seqNum;
       
   113 			if (aBufNode.pkt.isMarked())
       
   114 				anyMarked = true;
       
   115 
       
   116 			// Get next node
       
   117 			tempNode = aBufNode;
       
   118 			aBufNode = aBufNode.nextFrameNode;
       
   119 			PktBufNodePool.getInstance().returnBufNode(tempNode);
       
   120 			lastSeqNum = aPkt.getSeqNumber();
       
   121 		}
       
   122 
       
   123 		if (noPkts > 0) {
       
   124 			int seqDiff = firstSeqNum - lastSeqNum;
       
   125 			if (seqDiff < 0)
       
   126 				seqDiff = (Integer.MAX_VALUE - firstSeqNum) + lastSeqNum;
       
   127 			if (seqDiff == pktCount && pktCount == noPkts)
       
   128 				isComplete = 1;
       
   129 		} else {
       
   130 			isComplete = -1;
       
   131 		}
       
   132 
       
   133 		if (RTPSession.rtpDebugLevel > 6) {
       
   134 			System.out.println("<- DataFrame(PktBufNode, noPkt), data length: "
       
   135 					+ pkts.length);
       
   136 		}
       
   137 	}
       
   138 
       
   139 	public DataFrame(){
       
   140 	}
       
   141 
       
   142 	/**
       
   143 	 * Returns a two dimensial array where the first dimension represents
       
   144 	 * individual packets, from which the frame is made up, in order of
       
   145 	 * increasing sequence number. These indeces can be matched to the sequence
       
   146 	 * numbers returned by sequenceNumbers().
       
   147 	 * 
       
   148 	 * @return 2-dim array with raw data from packets
       
   149 	 */
       
   150 	/*public byte[][] getData() {
       
   151 		return this.data;
       
   152 	}*/
       
   153 
       
   154 	public RtpPkt[] getPkt(){
       
   155 		return this.pkts ;
       
   156 	}
       
   157 
       
   158 	/**
       
   159 	 * Returns a concatenated version of the data from getData() It ignores
       
   160 	 * missing sequence numbers, but then isComplete() will return false
       
   161 	 * provided that RTPAppIntf.frameSize() provides a non-negative number for
       
   162 	 * this payload type.
       
   163 	 * 
       
   164 	 * @return byte[] with all the data concatenated
       
   165 	 */
       
   166 	/*public byte[] getConcatenatedData() {
       
   167 		if (this.noPkts < 2) {
       
   168 			byte[] ret = new byte[this.totalLength];
       
   169 			int pos = 0;
       
   170 
       
   171 			for (int i = 0; i < data.length; i++) {
       
   172 				int length = data[i].length;
       
   173 
       
   174 				// Last packet may be shorter
       
   175 				if (pos + length > totalLength)
       
   176 					length = totalLength - pos;
       
   177 
       
   178 				System.arraycopy(data[i], 0, ret, pos, length);
       
   179 				pos += data[i].length;
       
   180 			}
       
   181 			return ret;
       
   182 		} else {
       
   183 			return data[0];
       
   184 		}
       
   185 	}*/
       
   186 
       
   187 	/**
       
   188 	 * If two SR packet have been received jlibrtp will attempt to calculate the
       
   189 	 * local UNIX timestamp (in milliseconds) of all packets received.
       
   190 	 * 
       
   191 	 * This value should ideally correspond to the local time when the SSRC sent
       
   192 	 * the packet. Note that the source may not be reliable.
       
   193 	 * 
       
   194 	 * Returns -1 if less than two SRs have been received
       
   195 	 * 
       
   196 	 * @return the UNIX timestamp, similar to System.currentTimeMillis() or -1;
       
   197 	 */
       
   198 	public long timestamp() {
       
   199 		return this.timestamp;
       
   200 
       
   201 	}
       
   202 
       
   203 	/**
       
   204 	 * Returns the RTP timestamp of all the packets in the frame.
       
   205 	 * 
       
   206 	 * @return unmodified RTP timestamp
       
   207 	 */
       
   208 	public long rtpTimestamp() {
       
   209 		return this.rtpTimestamp;
       
   210 	}
       
   211 
       
   212 	/**
       
   213 	 * Returns the payload type of the packets
       
   214 	 * 
       
   215 	 * @return the payload type of the packets
       
   216 	 */
       
   217 	public int payloadType() {
       
   218 		return this.payloadType;
       
   219 	}
       
   220 
       
   221 	/**
       
   222 	 * Returns an array whose values, for the same index, correpond to the
       
   223 	 * sequence number of the packet from which the data came.
       
   224 	 * 
       
   225 	 * This information can be valuable in conjunction with getData(), to
       
   226 	 * identify what parts of a frame are missing.
       
   227 	 * 
       
   228 	 * @return array with sequence numbers
       
   229 	 */
       
   230 	/*public int[] sequenceNumbers() {
       
   231 		return seqNum;
       
   232 	}*/
       
   233 
       
   234 	/**
       
   235 	 * Returns an array whose values, for the same index, correpond to whether
       
   236 	 * the data was marked or not.
       
   237 	 * 
       
   238 	 * This information can be valuable in conjunction with getData().
       
   239 	 * 
       
   240 	 * @return array of booleans
       
   241 	 */
       
   242 	/*public boolean[] marks() {
       
   243 		return this.marks;
       
   244 	}*/
       
   245 
       
   246 	/**
       
   247 	 * Returns true if any packet in the frame was marked.
       
   248 	 * 
       
   249 	 * This function should be used if all your frames fit into single packets.
       
   250 	 * 
       
   251 	 * @return true if any packet was marked, false otherwise
       
   252 	 */
       
   253 	public boolean marked() {
       
   254 		return this.anyMarked;
       
   255 	}
       
   256 
       
   257 	/**
       
   258 	 * The SSRC associated with this frame.
       
   259 	 * 
       
   260 	 * @return the ssrc that created this frame
       
   261 	 */
       
   262 	public long ssrc() {
       
   263 		return this.SSRC;
       
   264 	}
       
   265 
       
   266 	/**
       
   267 	 * The SSRCs that contributed to this frame
       
   268 	 * 
       
   269 	 * @return an array of contributing SSRCs, or null
       
   270 	 */
       
   271 	public long[] csrcs() {
       
   272 		return this.CSRCs;
       
   273 	}
       
   274 
       
   275 	/**
       
   276 	 * Checks whether the difference in sequence numbers corresponds to the
       
   277 	 * number of packets received for the current timestamp, and whether this
       
   278 	 * value corresponds to the expected number of packets.
       
   279 	 * 
       
   280 	 * @return true if the right number of packets make up the frame
       
   281 	 */
       
   282 	public int complete() {
       
   283 		return this.isComplete;
       
   284 	}
       
   285 
       
   286 	public void release() {
       
   287 		for(RtpPkt pkt : this.pkts) {
       
   288 			if (pkt != null) {
       
   289 				if (pkt.getDatagramPacket() != null)
       
   290 					DatagramPool.getInstance().returnPacket(pkt.getDatagramPacket());
       
   291 				RtpPktPool.getInstance().returnPkt(pkt);
       
   292 			}
       
   293 		}
       
   294 	}
       
   295 }