• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "JitterCalc"
18 #include <utils/Log.h>
19 
20 #include "JitterCalculator.h"
21 
22 #include <stdlib.h>
23 
24 namespace android {
25 
JitterCalc(int32_t clockRate)26 JitterCalc::JitterCalc(int32_t clockRate)
27     : mClockRate(clockRate) {
28     init(0, 0, 0, 0);
29 }
30 
init(uint32_t rtpTime,int64_t arrivalTimeUs,int32_t base,int32_t inter)31 void JitterCalc::init(uint32_t rtpTime, int64_t arrivalTimeUs, int32_t base, int32_t inter) {
32     mFirstTimeStamp = rtpTime;
33     mLastTimeStamp = rtpTime;
34     mFirstArrivalTimeUs = arrivalTimeUs;
35     mLastArrivalTimeUs = arrivalTimeUs;
36 
37     mBaseJitterUs = base;
38     mInterArrivalJitterUs = inter;
39 }
40 
putBaseData(int64_t rtpTime,int64_t arrivalTimeUs)41 void JitterCalc::putBaseData(int64_t rtpTime, int64_t arrivalTimeUs) {
42     // A RTP time wraps around after UINT32_MAX. We must consider this case.
43     const int64_t UINT32_MSB = 0x80000000;
44     int64_t overflowMask = (mFirstTimeStamp & UINT32_MSB & ~rtpTime) << 1;
45     int64_t tempRtpTime = overflowMask | rtpTime;
46 
47     // Base jitter implementation can be various
48     int64_t scheduledTimeUs = (tempRtpTime - (int64_t)mFirstTimeStamp) * 1000000ll / mClockRate;
49     int64_t elapsedTimeUs = arrivalTimeUs - mFirstArrivalTimeUs;
50     int64_t correctionTimeUs = elapsedTimeUs - scheduledTimeUs; // additional propagation delay;
51     mBaseJitterUs = (mBaseJitterUs * 15 + correctionTimeUs) / 16;
52     ALOGV("BaseJitterUs : %lld \t\t correctionTimeUs : %lld",
53             (long long)mBaseJitterUs, (long long)correctionTimeUs);
54 }
55 
putInterArrivalData(int64_t rtpTime,int64_t arrivalTimeUs)56 void JitterCalc::putInterArrivalData(int64_t rtpTime, int64_t arrivalTimeUs) {
57     const int64_t UINT32_MSB = 0x80000000;
58     int64_t tempRtpTime = rtpTime;
59     int64_t tempLastTimeStamp = mLastTimeStamp;
60 
61     // A RTP time wraps around after UINT32_MAX. We must consider this case.
62     int64_t overflowMask = (mLastTimeStamp ^ rtpTime) & UINT32_MSB;
63     tempRtpTime |= ((overflowMask & ~rtpTime) << 1);
64     tempLastTimeStamp |= ((overflowMask & ~mLastTimeStamp) << 1);
65 
66     // 6.4.1 of RFC3550 defines this interarrival jitter value.
67     int64_t diffTimeStampUs = abs(tempRtpTime - tempLastTimeStamp) * 1000000ll / mClockRate;
68     int64_t diffArrivalUs = arrivalTimeUs - mLastArrivalTimeUs; // Can't be minus
69     ALOGV("diffTimeStampUs %lld \t\t diffArrivalUs %lld",
70             (long long)diffTimeStampUs, (long long)diffArrivalUs);
71 
72     int64_t varianceUs = diffArrivalUs - diffTimeStampUs;
73     mInterArrivalJitterUs = (mInterArrivalJitterUs * 15 + abs(varianceUs)) / 16;
74 
75     mLastTimeStamp = (uint32_t)rtpTime;
76     mLastArrivalTimeUs = arrivalTimeUs;
77 }
78 
getBaseJitterMs()79 int32_t JitterCalc::getBaseJitterMs() {
80     return mBaseJitterUs / 1000;
81 }
82 
getInterArrivalJitterMs()83 int32_t JitterCalc::getInterArrivalJitterMs() {
84     return mInterArrivalJitterUs / 1000;
85 }
86 
87 }   // namespace android
88 
89