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(uint32_t rtpTime,int64_t arrivalTimeUs)41 void JitterCalc::putBaseData(uint32_t rtpTime, int64_t arrivalTimeUs) {
42 // A RTP time wraps around after UINT32_MAX. Overflow can present.
43 uint32_t diff = 0;
44 __builtin_usub_overflow(rtpTime, mFirstTimeStamp, &diff);
45
46 // Base jitter implementation can be various
47 int64_t scheduledTimeUs = ((int32_t)diff) * 1000000ll / mClockRate;
48 int64_t elapsedTimeUs = arrivalTimeUs - mFirstArrivalTimeUs;
49 int64_t correctionTimeUs = elapsedTimeUs - scheduledTimeUs; // additional propagation delay;
50 mBaseJitterUs = (mBaseJitterUs * 15 + correctionTimeUs) / 16;
51 ALOGV("BaseJitterUs : %lld \t\t correctionTimeUs : %lld",
52 (long long)mBaseJitterUs, (long long)correctionTimeUs);
53 }
54
putInterArrivalData(uint32_t rtpTime,int64_t arrivalTimeUs)55 void JitterCalc::putInterArrivalData(uint32_t rtpTime, int64_t arrivalTimeUs) {
56 // A RTP time wraps around after UINT32_MAX. Overflow can present.
57 uint32_t diff = 0;
58 __builtin_usub_overflow(rtpTime, mLastTimeStamp, &diff);
59
60 // 6.4.1 of RFC3550 defines this interarrival jitter value.
61 int64_t diffTimeStampUs = abs((int32_t)diff) * 1000000ll / mClockRate;
62 int64_t diffArrivalUs = arrivalTimeUs - mLastArrivalTimeUs; // Can't be minus
63 ALOGV("diffTimeStampUs %lld \t\t diffArrivalUs %lld",
64 (long long)diffTimeStampUs, (long long)diffArrivalUs);
65
66 int64_t varianceUs = diffArrivalUs - diffTimeStampUs;
67 mInterArrivalJitterUs = (mInterArrivalJitterUs * 15 + abs(varianceUs)) / 16;
68
69 mLastTimeStamp = rtpTime;
70 mLastArrivalTimeUs = arrivalTimeUs;
71 }
72
getBaseJitterMs()73 int32_t JitterCalc::getBaseJitterMs() {
74 return mBaseJitterUs / 1000;
75 }
76
getInterArrivalJitterMs()77 int32_t JitterCalc::getInterArrivalJitterMs() {
78 return mInterArrivalJitterUs / 1000;
79 }
80
81 } // namespace android
82
83