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 RTCP_XR_ANALYZER_H_INCLUDED
18 #define RTCP_XR_ANALYZER_H_INCLUDED
19 
20 #include <ImsMediaDefine.h>
21 #include <ImsMediaBitWriter.h>
22 #include <list>
23 #include <vector>
24 
25 #define MAX_BLOCK_LENGTH          220
26 #define BLOCK_LENGTH_STATISTICS   40
27 #define BLOCK_LENGTH_VOIP_METRICS 36
28 #define G_MIN_THRESHOLD           16
29 
30 struct tLossReport
31 {
tLossReporttLossReport32     tLossReport() :
33             beginSeq(-1),
34             endSeq(-1),
35             numLostPackets(-1),
36             numPacketsReceived(-1),
37             lossRate(-1)
38     {
39     }
40     int16_t beginSeq;
41     int16_t endSeq;
42     int32_t numLostPackets;
43     int32_t numPacketsReceived;
44     int32_t lossRate;
45 };
46 
47 struct tTTLReport
48 {
tTTLReporttTTLReport49     tTTLReport() :
50             beginSeq(-1),
51             endSeq(-1),
52             ipVersion(-1),
53             minTTL(-1),
54             meanTTL(-1),
55             maxTTL(-1),
56             devTTL(-1)
57     {
58     }
59     int16_t beginSeq;
60     int16_t endSeq;
61     uint8_t ipVersion;
62     int32_t minTTL;
63     int32_t meanTTL;
64     int32_t maxTTL;
65     int32_t devTTL;
66 };
67 
68 struct tDuplicateReport
69 {
tDuplicateReporttDuplicateReport70     tDuplicateReport() :
71             beginSeq(-1),
72             endSeq(-1),
73             numDuplicatedPackets(-1),
74             numPacketsReceived(-1)
75     {
76     }
77     int16_t beginSeq;
78     int16_t endSeq;
79     int32_t numDuplicatedPackets;
80     int32_t numPacketsReceived;
81 };
82 
83 struct tJitterReport
84 {
tJitterReporttJitterReport85     tJitterReport() :
86             beginSeq(-1),
87             endSeq(-1),
88             minJitter(-1),
89             meanJitter(-1),
90             maxJitter(-1),
91             devJitter(-1)
92     {
93     }
94     int16_t beginSeq;
95     int16_t endSeq;
96     int32_t minJitter;
97     int32_t meanJitter;
98     int32_t maxJitter;
99     int32_t devJitter;
100 };
101 
102 struct tVoIPMatricReport
103 {
tVoIPMatricReporttVoIPMatricReport104     tVoIPMatricReport() :
105             ssrc(0),
106             lossRate(0),
107             discardRate(0),
108             burstDensity(0),
109             gapDensity(0),
110             burstDuration(0),
111             gapDuration(0),
112             roundTripDelay(0),
113             endSystemDelay(0),
114             signalLevel(0),
115             noiseLevel(0),
116             rerl(0),
117             gMin(0),
118             rFactor(0),
119             extRFactor(0),
120             rxConfig(0),
121             jitterBufferNominal(0),
122             jitterBufferMaximum(0),
123             jitterBufferAbsMaximum(0)
124     {
125     }
126     uint32_t ssrc;
127     uint32_t lossRate;
128     uint32_t discardRate;
129     uint32_t burstDensity;
130     uint32_t gapDensity;
131     uint32_t burstDuration;
132     uint32_t gapDuration;
133     uint32_t roundTripDelay;
134     uint32_t endSystemDelay;
135     uint32_t signalLevel;
136     uint32_t noiseLevel;
137     uint32_t rerl;
138     uint32_t gMin;
139     uint32_t rFactor;
140     uint32_t extRFactor;
141     uint8_t rxConfig;
142     uint32_t jitterBufferNominal;
143     uint32_t jitterBufferMaximum;
144     uint32_t jitterBufferAbsMaximum;
145 };
146 
147 class RtcpXrEncoder
148 {
149 public:
150     RtcpXrEncoder();
151     virtual ~RtcpXrEncoder();
152 
153     /**
154      * @brief Set the receiving stream ssrc
155      */
156     void setSsrc(const uint32_t ssrc);
157 
158     /**
159      * @brief Set the sampling rate of audio codec in kHz unit
160      */
161     void setSamplingRate(const uint32_t rate);
162 
163     /**
164      * @brief Set the round trip delay in milliseconds unit
165      */
166     void setRoundTripDelay(const uint32_t delay);
167 
168     /**
169      * @brief Stack receiving rtp status
170      *
171      * @param status The status of the audio frame how it is processed in jitter buffer
172      * @param delay The delay of the audio frame from stacked in jitter buffer to play to the audio
173      * codec
174      */
175     void stackRxRtpStatus(const int32_t status, const uint32_t delay);
176 
177     /**
178      * @brief Set the jitter buffer size
179      *
180      * @param current The current jitter buffer size
181      * @param max The maximum jitter buffer size in worst condition
182      */
183     void setJitterBufferStatus(const uint32_t current, const uint32_t max);
184 
185     /**
186      * @brief Create a rtcp-xr report blocks
187      *
188      * @param nRtcpXrReport The bitmask of the enabled report block types
189      * @param packets The list of the packets received to use in statistic summary report block
190      * @param lostPackets The list of the lost packet counted to use in statistic summary report
191      * block
192      * @param beginSeq The beginning of the rtp sequence number to generate statistics summery
193      * report block
194      * @param endSeq The end of the rtp sequence number to generate statistics summery report block
195      * @param data The data buffer to store the report block
196      * @param size The size of the data buffer to stored
197      * @return true The report block generated without error
198      * @return false The bitmask of the report block types are zero or data buffer is null
199      */
200     bool createRtcpXrReport(const uint32_t nRtcpXrReport, std::list<RtpPacket*>* packets,
201             std::list<LostPacket*>* lostPackets, uint16_t beginSeq, uint16_t endSeq, uint8_t* data,
202             uint32_t& size);
203 
204 private:
205     tLossReport* createLossAnalysisReport(std::list<RtpPacket*>* packets,
206             std::list<LostPacket*>* lostPackets, uint16_t beginSeq, uint16_t endSeq);
207     tJitterReport* createJitterAnalysisReport(
208             std::list<RtpPacket*>* packets, uint16_t beginSeq, uint16_t endSeq);
209     tTTLReport* createTTLAnalysisReport(
210             std::list<RtpPacket*>* packets, uint16_t beginSeq, uint16_t endSeq);
211     tDuplicateReport* createDuplicateAnalysisReport(
212             std::list<RtpPacket*>* packets, uint16_t beginSeq, uint16_t endSeq);
213     tVoIPMatricReport* createVoIPMatricReport();
214     void encodeStatisticSummeryReport(tLossReport* lossReport, tJitterReport* jitterReport,
215             tTTLReport* ttlReport, tDuplicateReport* duplicateReport, uint8_t* data);
216     void encodeVoipMetricReport(tVoIPMatricReport* report, uint8_t* data);
217 
218     uint32_t mSsrc;
219     uint32_t mSamplingRate;
220     uint32_t mRoundTripDelay;
221     uint32_t mVoipLossCount;
222     uint32_t mVoipDiscardedCount;
223     uint32_t mVoipPktCount;
224     uint32_t mVoipLostCountInBurst;
225     uint32_t mJitterBufferNominal;
226     uint32_t mJitterBufferMax;
227     uint32_t mJitterBufferAbsMax;
228 
229     /**
230      * state 1 = received a packet during a gap
231      * state 2 = received a packet during a burst
232      * state 3 = lost a packet during a burst
233      * state 4 = lost an isolated packet during a gap
234      */
235     // calculated when rtp packet received
236     uint32_t mVoipC11;
237     uint32_t mVoipC13;
238     uint32_t mVoipC14;
239     uint32_t mVoipC22;
240     uint32_t mVoipC23;
241     uint32_t mVoipC33;
242     // calculate when create the voip report
243     uint32_t mVoipC31;
244     uint32_t mVoipC32;
245     ImsMediaBitWriter mBitWriter;
246 };
247 
248 #endif