//The Ben function is used to calculate jitter, and this function is called to calculate jitter each time a RTP packet is received. voidRtpreceptionstats::noteincomingpacket (u_int16_t seqNum, u_int32_t rtptimestamp, unsigned Timestampfrequen CY, Boolean Useforjittercalculation,structtimeval&Resultpresentationtime, Boolean&resulthasbeensyncedusingrtcp, unsigned packetsize) { if(!fhaveseeninitialsequencenumber) Initseqnum (seqNum); ++fnumpacketsreceivedsincelastreset;//The number of RTP packets received after the last reset. ++ftotnumpacketsreceived;//the number of RTP packets received altogether. u_int32_t Prevtotbytesreceived_lo = Ftotbytesreceived_lo;//Ftotbytesreceived_lo, the total number of RTP bytes received. Ftotbytesreceived_lo + =PacketSize; if(Ftotbytesreceived_lo < Prevtotbytesreceived_lo) {//Wrap-around Ftotbytesreceived_lo is an unsigned 32-bit integer that is calculated from the new start when the maximum value is reached. ++ftotbytesreceived_hi;//The number of zeros. } //Check whether the new sequence number is the highest yet seen:unsigned oldseqnum = (fhighestextseqnumreceived&0xFFFF);//take 16-bit lower dataunsigned seqnumcycle = (fhighestextseqnumreceived&0xffff0000);//take 16-bit high dataunsigned seqnumdifference = (unsigned) ((int) seqnum-(int) oldseqnum); unsigned newseqnum=0; if(SEQNUMLT (u_int16_t) Oldseqnum, SeqNum)) {//The current serial number is greater than the previous serial number, which appears to be a legitimate RTP package. However, it is still possible to detect the occurrence of zero zeroing. //This packet is not a old packet received out of order, so check it: if(Seqnumdifference >=0x8000) {//In the case of zeroing, the 0x8000 highest (i.e. 16-bit) is 1, the remaining bits are 0, and the serial number gap is so, assuming zero-zeroing. //the sequence number wrapped around, so start a new cycle:Seqnumcycle + =0x10000; } newseqnum= Seqnumcycle|seqnum;//make a combination of the current sequence number and the number of bad times. if(Newseqnum > Fhighestextseqnumreceived) {//The if statement may be true when the new ordinal is more than the previous serial number hour. fhighestextseqnumreceived = Newseqnum;//The new combination is saved to the member variable. } } Else if(Ftotnumpacketsreceived >1) {//the RTP packet was previously received. There are RTP packets already present. //This packet is an old packet received out of order if((int) Seqnumdifference >=0x8000) {//In the case of zeroing, the 0x8000 highest (i.e. 16-bit) is 1, the remaining bits are 0, and the serial number gap is so, assuming zero-zeroing. //the sequence number wrapped around, so switch to a old cycle:Seqnumcycle-=0x10000;//The last time there was a zero, so we have to reduce the number of bad times. } //Here's the code, which appears above, see the explanation above. Newseqnum = seqnumcycle|SeqNum; if(Newseqnum <fbaseextseqnumreceived) {fbaseextseqnumreceived=Newseqnum; } } //Record the Inter-packet delay structTimeval TimeNow; Gettimeofday (&TimeNow, NULL); if(Flastpacketreceptiontime.tv_sec! =0|| Flastpacketreceptiontime.tv_usec! =0) {unsigned gap//calculates the time interval for RTP packets. Units: One out of 10,000 seconds, microseconds, 1/1000000 seconds. = (timenow.tv_sec-flastpacketreceptiontime.tv_sec) *MILLION+ Timenow.tv_usec-flastpacketreceptiontime.tv_usec; if(Gap > Fmaxinterpacketgapus) {//records the maximum RTP packet time interval that has occurred. Fmaxinterpacketgapus =Gap; } if(Gap < Fmininterpacketgapus) {//Record the minimum RTP packet interval that has occurred. Fmininterpacketgapus =Gap; } ftotalinterpacketgaps.tv_usec+ = gap;//Ftotalinterpacketgaps, the cumulative time interval of two RTP packets arriving. if(Ftotalinterpacketgaps.tv_usec >= MILLION) {//mathematical calculations, into 1. ++ftotalinterpacketgaps.tv_sec; Ftotalinterpacketgaps.tv_usec-=MILLION; }} flastpacketreceptiontime= TimeNow;//Flastpacketreceptiontime, record the time the RTP packet arrives. //Compute the current ' jitter ' using the Received packet ' s RTP timestamp,//and the RTP timestamp that would correspond to the current time. //(use the code from Appendix A.8 in the RTP spec.) //Note, however, that we don't use this packet if it timestamp is//The same as that's the previous packet (this indicates a multi-packet//fragment), or if we ' ve been explicitly told not the use of this packet. /** If the current RTP packet timestamp is the same as the previous RTP packet timestamp, this RTP packet is not used to calculate jitter. Fpreviouspacketrtptimestamp==rtptimestamp. */ if(Useforjittercalculation&& Rtptimestamp! = Fpreviouspacketrtptimestamp) {//RTPTIMESTAMP,RTP the timestamp of the packet header record. unsigned arrival = (timestampfrequency*timenow.tv_sec); Arrival+ = (unsigned)//calculates the theoretical arrival time of the RTP packet. ((2.0*timestampfrequency*timenow.tv_usec +1000000.0)/2000000); //note:rounding intTransit = Arrival-rtptimestamp;//the theoretical and actual time difference values. if(Flasttransit = = (~0)) Flasttransit = transit;//hack for first time//if it is the first RTP package. intD = transit-flasttransit;//The difference between this difference and the last difference is obtained. Flasttransit = Transit;//Put the current difference in the last difference member variable, the next use. if(D <0) d =-D;//take a positive number. Fjitter + = (1.0/16.0) * ((Double) d-fjitter);//calculates the jitter value. The weight of D will be lower each time, with the variant: ((double) d)/16 + (15.0/16.0) *fjitter. } //Return the ' presentation time ', corresponds to ' rtptimestamp '://calculates the apparent time based on the timestamp of the RTP header. if(Fsynctime.tv_sec = =0&& Fsynctime.tv_usec = =0) { //This is the first timestamp that we ve seen//' Wall clock ' time as the synchronization time. ( this would be//corrected later when we receive RTCP SRs.)Fsynctimestamp = Rtptimestamp;//Fsynctimestamp the last RTP header timestamp. Fsynctime = TimeNow;//Fsynctime the last computed rendering time, based on the local time of the receiving end, Gettimeofday (&timenow, NULL);. } intTimestampdiff = Rtptimestamp-fsynctimestamp;//the difference between the RTP head time stamp and the last RTP header timestamp. //Note:this Works even if the timestamp wraps around//(as long as "int" is + bits)//timestampfrequency, sample rate, number of samples per second. //Rtptimestamp, the device is sampled every time, and the timestamp increments by 1. //Divide the timestamp frequency to get real time: DoubleTimediff = timestampdiff/(Double) timestampfrequency;//Obtain the time difference between the RTP packets before and after the unit is wonderful. //Add this to the ' sync time ' to get our result:UnsignedConstmillion =1000000; unsigned seconds, useconds; if(Timediff >=0.0) {seconds= Fsynctime.tv_sec + (unsigned) (Timediff);//The number of seconds to take. Useconds =fsynctime.tv_usec+ (unsigned) ((Timediff-(unsigned) timediff) *million);//take a subtle number. if(Useconds >=million) {Useconds-=million; ++seconds; } } Else{//Timediff of the negative number of this case processing. //if the serial number of the current RTP package is less than the serial number of the previous RTP package, and their timestamps are different, there is a case of this negative. Timediff =-Timediff; Seconds= Fsynctime.tv_sec-(unsigned) (Timediff); Useconds=fsynctime.tv_usec-(unsigned) ((Timediff-(unsigned) timediff) *million); if((int) Useconds <0) {Useconds+=million; --seconds; } } //the time to render is calculated, and the time value is returned by Resultpresentationtime. Resultpresentationtime.tv_sec =seconds; Resultpresentationtime.tv_usec=Useconds; Resulthasbeensyncedusingrtcp= fhasbeensynchronized;//estimates are used for RTCP, indicating whether the current statistics are RTCP used. //record the time stamp and time of this synchronization. //Save These as the new synchronization timestamp & time:Fsynctimestamp =Rtptimestamp; Fsynctime=Resultpresentationtime; Fpreviouspacketrtptimestamp=Rtptimestamp;}
Information:
RTP Jitter--rfc3550, appendix A.8 Https://www.ietf.org/rfc/rfc3550.txt
Online book--"Rtp:audio and video for the Internet by Colin perkings", see: Baidu Network Disk sharing \G:\WORKSPACE\DOWNLOAD\RTP-Audio and video for The Internet by Colin Perkings.pdf
Finish.
LIVE555 Analysis: Rtpreceptionstats::noteincomingpacket function