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