1 /**
2  * Copyright (C) 2022 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 #ifndef JITTERNETWORKANALYSER_H_INCLUDED
18 #define JITTERNETWORKANALYSER_H_INCLUDED
19 
20 #include <stdint.h>
21 #include <list>
22 #include <map>
23 #include <mutex>
24 
25 enum NETWORK_STATUS
26 {
27     NETWORK_STATUS_BAD,
28     NETWORK_STATUS_NORMAL,
29     NETWORK_STATUS_GOOD
30 };
31 
32 class JitterNetworkAnalyser
33 {
34 public:
35     JitterNetworkAnalyser();
36     virtual ~JitterNetworkAnalyser();
37     void Reset();
38     // initialze network analyser
39     void SetMinMaxJitterBufferSize(uint32_t nMinBufferSize, uint32_t nMaxBufferSize);
40     void SetJitterOptions(
41             uint32_t incThreshold, uint32_t decThreshold, uint32_t stepSize, double zValue);
42 
43     /**
44      * @brief Get the next jitter buffer size
45      *
46      * @param nCurrJitterBufferSize The current jiter buffer size
47      * @param currentTime The current timestamp when invoked this method with milliseconds unit
48      * @return uint32_t The next jitter buffer size
49      */
50     uint32_t GetNextJitterBufferSize(uint32_t nCurrJitterBufferSize, uint32_t currentTime);
51 
52     /**
53      * @brief Calculate transit time difference
54      *
55      * @param timestamp The rtp timestamp of the packet in milliseconds
56      * @param arrivalTime The received timestamp of the packet in milliseconds
57      * @return int32_t The calculated transit time difference of the packet
58      */
59     int32_t CalculateTransitTimeDifference(uint32_t timestamp, uint32_t arrivalTime);
60 
61     /**
62      * @brief Add the late arrival packet time to the list to monitor the latest time of the late
63      * arrival packet
64      *
65      * @param time The time of current late arrival appears
66      */
67     void SetLateArrivals(uint32_t time);
68 
69 private:
70     double CalculateDeviation(double* pMean);
71     int32_t GetMaxJitterValue();
72 
73     std::mutex mMutex;
74     uint32_t mMinJitterBufferSize;
75     uint32_t mMaxJitterBufferSize;
76     uint32_t mPrevTimestamp;
77     uint32_t mPrevArrivalTime;
78     std::list<int32_t> mListAccumDeltas;
79     int32_t mPrevDelta;
80     int32_t minJitterInBeginning;
81     uint32_t mTimeLateArrivals;
82     NETWORK_STATUS mNetworkStatus;
83     uint32_t mGoodStatusEnteringTime;
84     uint32_t mBadStatusChangedTime;
85     uint32_t mBufferIncThreshold;
86     uint32_t mBufferDecThreshold;
87     uint32_t mBufferStepSize;
88     double mBufferWeight;
89 };
90 
91 #endif  // JITTERNETWORKANALYSER_H_INCLUDED
92