src/jlibrtp/RtcpPktSR.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 /**
       
    22  * RTCP packets for Sender Reports
       
    23  * 
       
    24  * @author Arne Kepp
       
    25  */
       
    26 public class RtcpPktSR extends RtcpPkt {
       
    27 	/** NTP timestamp, MSB */
       
    28 	protected long ntpTs1 = -1; // 32 bits
       
    29 	/** NTP timestamp, LSB */
       
    30 	protected long ntpTs2 = -1; // 32 bits
       
    31 	/** RTP timestamp */
       
    32 	protected long rtpTs = -1; // 32 bits
       
    33 	/** Senders packet count */
       
    34 	protected long sendersPktCount = -1; // 32 bits
       
    35 	/** Senders octet count */
       
    36 	protected long sendersOctCount = -1; // 32 bits
       
    37 	/** RR packet with receiver reports that we can append */
       
    38 	protected RtcpPktRR rReports = null;
       
    39 
       
    40 	/**
       
    41 	 * Constructor for a new Sender Report packet
       
    42 	 * 
       
    43 	 * @param ssrc
       
    44 	 *            the senders SSRC, presumably from RTPSession
       
    45 	 * @param pktCount
       
    46 	 *            packets sent in this session
       
    47 	 * @param octCount
       
    48 	 *            octets sent in this session
       
    49 	 * @param rReports
       
    50 	 *            receiver reports, as RR packets, to be included in this packet
       
    51 	 */
       
    52 	protected RtcpPktSR(long ssrc, long pktCount, long octCount,
       
    53 			RtcpPktRR rReports) {
       
    54 		// Fetch all the right stuff from the database
       
    55 		super.ssrc = ssrc;
       
    56 		super.packetType = 200;
       
    57 		sendersPktCount = pktCount;
       
    58 		sendersOctCount = octCount;
       
    59 		this.rReports = rReports;
       
    60 	}
       
    61 
       
    62 	/**
       
    63 	 * Constructor that parses a received packet
       
    64 	 * 
       
    65 	 * @param aRawPkt
       
    66 	 *            the raw packet
       
    67 	 * @param start
       
    68 	 *            the position at which SR starts
       
    69 	 * @param length
       
    70 	 *            used to determine number of included receiver reports
       
    71 	 */
       
    72 	protected RtcpPktSR(byte[] aRawPkt, int start, int length) {
       
    73 		if (RTPSession.rtpDebugLevel > 9) {
       
    74 			System.out.println("  -> RtcpPktSR(rawPkt)");
       
    75 		}
       
    76 
       
    77 		super.rawPkt = aRawPkt;
       
    78 
       
    79 		if (!super.parseHeaders(start) || packetType != 200) {
       
    80 			if (RTPSession.rtpDebugLevel > 2) {
       
    81 				System.out
       
    82 						.println(" <-> RtcpPktSR.parseHeaders() etc. problem: "
       
    83 								+ (!super.parseHeaders(start)) + " "
       
    84 								+ packetType + " " + super.length);
       
    85 			}
       
    86 			super.problem = -200;
       
    87 		} else {
       
    88 			super.ssrc = StaticProcs.bytesToUIntLong(aRawPkt, 4 + start);
       
    89 			if (length > 11)
       
    90 				ntpTs1 = StaticProcs.bytesToUIntLong(aRawPkt, 8 + start);
       
    91 			if (length > 15)
       
    92 				ntpTs2 = StaticProcs.bytesToUIntLong(aRawPkt, 12 + start);
       
    93 			if (length > 19)
       
    94 				rtpTs = StaticProcs.bytesToUIntLong(aRawPkt, 16 + start);
       
    95 			if (length > 23)
       
    96 				sendersPktCount = StaticProcs.bytesToUIntLong(aRawPkt,
       
    97 						20 + start);
       
    98 			if (length > 27)
       
    99 				sendersOctCount = StaticProcs.bytesToUIntLong(aRawPkt,
       
   100 						24 + start);
       
   101 
       
   102 			// RRs attached?
       
   103 			if (itemCount > 0) {
       
   104 				rReports = new RtcpPktRR(rawPkt, start, itemCount);
       
   105 			}
       
   106 		}
       
   107 
       
   108 		if (RTPSession.rtpDebugLevel > 9) {
       
   109 			System.out.println("  <- RtcpPktSR(rawPkt)");
       
   110 		}
       
   111 	}
       
   112 
       
   113 	/**
       
   114 	 * Encode the packet into a byte[], saved in .rawPkt
       
   115 	 * 
       
   116 	 * CompRtcpPkt will call this automatically
       
   117 	 */
       
   118 	protected void encode() {
       
   119 		if (RTPSession.rtpDebugLevel > 9) {
       
   120 			if (this.rReports != null) {
       
   121 				System.out
       
   122 						.println("  -> RtcpPktSR.encode() receptionReports.length: "
       
   123 								+ this.rReports.length);
       
   124 			} else {
       
   125 				System.out
       
   126 						.println("  -> RtcpPktSR.encode() receptionReports: null");
       
   127 			}
       
   128 		}
       
   129 
       
   130 		if (this.rReports != null) {
       
   131 			super.itemCount = this.rReports.reportees.length;
       
   132 
       
   133 			byte[] tmp = this.rReports.encodeRR();
       
   134 			super.rawPkt = new byte[tmp.length + 28];
       
   135 			// super.length = (super.rawPkt.length / 4) - 1;
       
   136 
       
   137 			System.arraycopy(tmp, 0, super.rawPkt, 28, tmp.length);
       
   138 
       
   139 		} else {
       
   140 			super.itemCount = 0;
       
   141 			super.rawPkt = new byte[28];
       
   142 			// super.length = 6;
       
   143 		}
       
   144 		// Write the common header
       
   145 		super.writeHeaders();
       
   146 
       
   147 		// Convert to NTP and chop up
       
   148 		long timeNow = System.currentTimeMillis();
       
   149 		ntpTs1 = 2208988800L + (timeNow / 1000);
       
   150 		long ms = timeNow % 1000;
       
   151 		double tmp = ((double) ms) / 1000.0;
       
   152 		tmp = tmp * (double) 4294967295L;
       
   153 		ntpTs2 = (long) tmp;
       
   154 		rtpTs = System.currentTimeMillis();
       
   155 
       
   156 		// Write SR stuff
       
   157 		StaticProcs.uIntLongToByteWord(super.ssrc, super.rawPkt, 4);
       
   158 		StaticProcs.uIntLongToByteWord(ntpTs1, super.rawPkt, 8);
       
   159 		StaticProcs.uIntLongToByteWord(ntpTs2, super.rawPkt, 12);
       
   160 		StaticProcs.uIntLongToByteWord(rtpTs, super.rawPkt, 16);
       
   161 		StaticProcs.uIntLongToByteWord(sendersPktCount, super.rawPkt, 20);
       
   162 		StaticProcs.uIntLongToByteWord(sendersOctCount, super.rawPkt, 24);
       
   163 
       
   164 		if (RTPSession.rtpDebugLevel > 9) {
       
   165 			System.out.println("  <- RtcpPktSR.encode() ntpTs1: "
       
   166 					+ Long.toString(ntpTs1) + " ntpTs2: "
       
   167 					+ Long.toString(ntpTs2));
       
   168 		}
       
   169 	}
       
   170 
       
   171 	/**
       
   172 	 * Debug purposes only
       
   173 	 */
       
   174 	public void debugPrint() {
       
   175 		System.out.println("RtcpPktSR.debugPrint() ");
       
   176 		System.out.println("  SSRC:" + Long.toString(super.ssrc) + " ntpTs1:"
       
   177 				+ Long.toString(ntpTs1) + " ntpTS2:" + Long.toString(ntpTs2)
       
   178 				+ " rtpTS:" + Long.toString(rtpTs) + " senderPktCount:"
       
   179 				+ Long.toString(sendersPktCount) + " sendersOctetCount:"
       
   180 				+ Long.toString(sendersOctCount));
       
   181 		if (this.rReports != null) {
       
   182 			System.out.print("  Part of Sender Report: ");
       
   183 			this.rReports.debugPrint();
       
   184 			System.out.println("  End Sender Report");
       
   185 		} else {
       
   186 			System.out
       
   187 					.println("No Receiver Reports associated with this Sender Report.");
       
   188 		}
       
   189 	}
       
   190 }