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 <media/stagefright/rtsp/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
51 // We want to approximate correctionTimeUs by slowly (1:15) averaging into jitter base, but
52 // both correction time and base jitter can roll over. Adjust correctionTime to be close to
53 // base jitter. Accomplish this by calculating the closest 32-bit delta (positive or
54 // negative) and applying 1/16th of it to the base jitter.
55 int32_t correctionDiff;
56 (void)__builtin_sub_overflow(correctionTimeUs, mBaseJitterUs, &correctionDiff);
57 mBaseJitterUs = int32_t(int64_t(mBaseJitterUs) + correctionDiff / 16);
58 ALOGV("BaseJitterUs : %lld \t\t correctionTimeUs : %lld",
59 (long long)mBaseJitterUs, (long long)correctionTimeUs);
60 }
61
putInterArrivalData(uint32_t rtpTime,int64_t arrivalTimeUs)62 void JitterCalc::putInterArrivalData(uint32_t rtpTime, int64_t arrivalTimeUs) {
63 // A RTP time wraps around after UINT32_MAX. Overflow can present.
64 uint32_t diff = 0;
65 __builtin_usub_overflow(rtpTime, mLastTimeStamp, &diff);
66
67 // 6.4.1 of RFC3550 defines this interarrival jitter value.
68 int64_t diffTimeStampUs = abs((int32_t)diff) * 1000000ll / mClockRate;
69 int64_t diffArrivalUs = arrivalTimeUs - mLastArrivalTimeUs; // Can't be minus
70 ALOGV("diffTimeStampUs %lld \t\t diffArrivalUs %lld",
71 (long long)diffTimeStampUs, (long long)diffArrivalUs);
72
73 int64_t varianceUs = diffArrivalUs - diffTimeStampUs;
74 mInterArrivalJitterUs = (mInterArrivalJitterUs * 15 + abs(varianceUs)) / 16;
75
76 mLastTimeStamp = rtpTime;
77 mLastArrivalTimeUs = arrivalTimeUs;
78 }
79
getBaseJitterMs()80 int32_t JitterCalc::getBaseJitterMs() {
81 return mBaseJitterUs / 1000;
82 }
83
getInterArrivalJitterMs()84 int32_t JitterCalc::getInterArrivalJitterMs() {
85 return mInterArrivalJitterUs / 1000;
86 }
87
88 } // namespace android
89
90