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 #include <RtcpPacket.h>
18 #include <gtest/gtest.h>
19 
20 extern RtpDt_Void addSdesItem(
21         OUT RtcpConfigInfo* pobjRtcpCfgInfo, IN RtpDt_UChar* sdesName, IN RtpDt_UInt32 uiLength);
22 
23 class RtcpPacketTest : public ::testing::Test
24 {
25 public:
26 protected:
SetUp()27     virtual void SetUp() override {}
28 
TearDown()29     virtual void TearDown() override {}
30 };
31 
32 /**
33  * Test compound RTCP packet with one Sender-Report and SDES.
34  * SR has zero reports and SDES has one CNAME item.
35  */
TEST_F(RtcpPacketTest,DecodeCompoundSrSdesPacket)36 TEST_F(RtcpPacketTest, DecodeCompoundSrSdesPacket)
37 {
38     RtcpPacket rtcpPacket;
39 
40     /*
41      * Real-time Transport Control Protocol (Sender Report)
42      * 10.. .... = Version: RFC 1889 Version (2)
43      * ..0. .... = Padding: False
44      * ...0 0000 = Reception report count: 0
45      * Packet type: Sender Report (200)
46      * Length: 6 (28 bytes)
47      * Sender SSRC: 0xb1c8cb02 (2982726402)
48      * Timestamp, MSW: 3865027889 (0xe65fa531)
49      * Timestamp, LSW: 1402021058 (0x539124c2)
50      * [MSW and LSW as NTP timestamp: Jun 24, 2022 02:51:29.326433465 UTC]
51      * RTP timestamp: 262533
52      * Sender's packet count: 65
53      * Sender's octet count: 51283
54      *
55      * Real-time Transport Control Protocol (Source description)
56      * 10.. .... = Version: RFC 1889 Version (2)
57      * ..0. .... = Padding: False
58      * ...0 0001 = Source count: 1
59      * Packet type: Source description (202)
60      * Length: 10 (44 bytes)
61      * Chunk 1, SSRC/CSRC 0xB1C8CB02
62      *    Identifier: 0xb1c8cb02 (2982726402)
63      *    SDES items
64      *       Type: CNAME (user and domain) (1)
65      *       Length: 31
66      *       Text: 2600:100e:1008:af4f::1ebe:6851
67      *       Type: END (0)
68      */
69     uint8_t bufSrSdesPacket[] = {0x80, 0xc8, 0x00, 0x06, 0xb1, 0xc8, 0xcb, 0x02, 0xe6, 0x5f, 0xa5,
70             0x31, 0x53, 0x91, 0x24, 0xc2, 0x00, 0x04, 0x01, 0x85, 0x00, 0x00, 0x00, 0x41, 0x00,
71             0x00, 0xc8, 0x53, 0x81, 0xca, 0x00, 0x0a, 0xb1, 0xc8, 0xcb, 0x02, 0x01, 0x1f, 0x32,
72             0x36, 0x30, 0x30, 0x3a, 0x31, 0x30, 0x30, 0x65, 0x3a, 0x31, 0x30, 0x30, 0x38, 0x3a,
73             0x61, 0x66, 0x34, 0x66, 0x3a, 0x3a, 0x31, 0x65, 0x62, 0x65, 0x3a, 0x36, 0x38, 0x35,
74             0x31, 0x00, 0x00, 0x00, 0x00};
75 
76     RtpDt_UChar IPAddress[] = "2600:100e:1008:af4f::1ebe:6851";
77     RtcpConfigInfo rtcpConfigInfo;
78     addSdesItem(&rtcpConfigInfo, IPAddress, strlen(reinterpret_cast<char*>(IPAddress)));
79 
80     RtpBuffer rtpBuffer(72, bufSrSdesPacket);
81     eRTP_STATUS_CODE res = rtcpPacket.decodeRtcpPacket(&rtpBuffer, 0, &rtcpConfigInfo);
82     EXPECT_EQ(res, RTP_SUCCESS);
83 
84     std::list<RtcpSrPacket*> SrList = rtcpPacket.getSrPacketList();
85     ASSERT_TRUE(SrList.size() != 0);
86 
87     RtcpSrPacket* rtcpSrPacket = SrList.front();
88     ASSERT_TRUE(rtcpSrPacket != nullptr);
89 
90     RtcpHeader* pRtcpHeader = rtcpSrPacket->getRtcpHdrInfo();
91     ASSERT_TRUE(pRtcpHeader != nullptr);
92 
93     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
94     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_FALSE);
95     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 0);
96     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_SR);
97     EXPECT_EQ(pRtcpHeader->getLength(), 6 * RTP_WORD_SIZE);
98     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xb1c8cb02);
99 
100     tRTP_NTP_TIME* ntpTime = rtcpSrPacket->getNtpTime();
101     ASSERT_TRUE(ntpTime != nullptr);
102 
103     EXPECT_EQ(ntpTime->m_uiNtpHigh32Bits, 0xe65fa531);
104     EXPECT_EQ(ntpTime->m_uiNtpLow32Bits, 0x539124c2);
105     EXPECT_EQ(rtcpSrPacket->getRtpTimestamp(), 0x00040185);
106     EXPECT_EQ(rtcpSrPacket->getSendPktCount(), 65);
107     EXPECT_EQ(rtcpSrPacket->getSendOctetCount(), 0x0000c853);
108 
109     RtcpSdesPacket* pRtcpSdesPacket = rtcpPacket.getSdesPacket();
110     ASSERT_TRUE(pRtcpSdesPacket != nullptr);
111 
112     std::list<RtcpChunk*> pSdesChunks = pRtcpSdesPacket->getSdesChunkList();
113     EXPECT_EQ(pSdesChunks.size(), 1);
114     RtcpChunk* chunk = pSdesChunks.front();
115     ASSERT_TRUE(chunk != nullptr);
116 
117     std::list<tRTCP_SDES_ITEM*> sdesItemList = chunk->getSdesItemList();
118     EXPECT_EQ(sdesItemList.size(), 1);
119     tRTCP_SDES_ITEM* sdesItem = sdesItemList.front();
120     ASSERT_TRUE(sdesItem != nullptr);
121 
122     EXPECT_EQ(sdesItem->ucType, 1);
123     EXPECT_EQ(sdesItem->ucLength, 31);
124     const char* expectedpValue = "2600:100e:1008:af4f::1ebe:6851";
125     EXPECT_EQ(strncmp(reinterpret_cast<char*>(sdesItem->pValue), expectedpValue,
126                       strlen(expectedpValue)),
127             0);
128 
129     pRtcpHeader = pRtcpSdesPacket->getRtcpHdrInfo();
130     ASSERT_TRUE(pRtcpHeader != nullptr);
131 
132     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
133     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_FALSE);
134     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 1);
135     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_SDES);
136     EXPECT_EQ(pRtcpHeader->getLength(), 10 * RTP_WORD_SIZE);
137     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xb1c8cb02);
138 }
139 
140 /**
141  * Test RTCP packet with Sender Report and Receiver Report.
142  */
TEST_F(RtcpPacketTest,DecodeCompoundSrRrPacket)143 TEST_F(RtcpPacketTest, DecodeCompoundSrRrPacket)
144 {
145     RtcpPacket rtcpPacket;
146 
147     /*
148      *  Real-time Transport Control Protocol (Sender Report)
149      *       [Stream setup by SDP (frame 1)]
150      *       10.. .... = Version: RFC 1889 Version (2)
151      *       ..0. .... = Padding: False
152      *       ...0 0001 = Reception report count: 1
153      *       Packet type: Sender Report (200)
154      *       Length: 12 (52 bytes)
155      *       Sender SSRC: 0xd2bd4e3e (3535621694)
156      *       Timestamp, MSW: 3314714324 (0xc59286d4)
157      *       Timestamp, LSW: 3874060501 (0xe6e978d5)
158      *       [MSW and LSW as NTP timestamp: Jan 14, 2005 17:58:44.902000000 UTC]
159      *       RTP timestamp: 320
160      *       Sender's packet count: 2
161      *       Sender's octet count: 320
162      *       Source 1
163      *           Identifier: 0xd2bd4e3e (3535621694)
164      *           SSRC contents
165      *               Fraction lost: 0 / 256
166      *               Cumulative number of packets lost: 0
167      *           Extended highest sequence number received: 131074
168      *               Sequence number cycles count: 2
169      *               Highest sequence number received: 2
170      *           Interarrival jitter: 0
171      *           Last SR timestamp: 2262099689 (0x86d4e6e9)
172      *           Delay since last SR timestamp: 1 (0 milliseconds)
173      *   Real-time Transport Control Protocol (Receiver Report)
174      *       [Stream setup by SDP (frame 1)]
175      *       10.. .... = Version: RFC 1889 Version (2)
176      *       ..0. .... = Padding: False
177      *       ...0 0001 = Reception report count: 1
178      *       Packet type: Receiver Report (201)
179      *       Length: 7 (32 bytes)
180      *       Sender SSRC: 0xd2bd4e3e (3535621694)
181      *       Source 1
182      *           Identifier: 0x00000000 (0)
183      *           SSRC contents
184      *               Fraction lost: 0x10 / 256
185      *               Cumulative number of packets lost: 0x000020
186      *           Extended highest sequence number received: 0
187      *               Sequence number cycles count: 0
188      *               Highest sequence number received: 0
189      *           Interarrival jitter: 0
190      *           Last SR timestamp: 2262099689 (0x86d4e6e9)
191      *           Delay since last SR timestamp: 1 (0 milliseconds)
192      */
193     uint8_t bufSrSdesPacket[] = {0x81, 0xc8, 0x00, 0x0c, 0xd2, 0xbd, 0x4e, 0x3e, 0xc5, 0x92, 0x86,
194             0xd4, 0xe6, 0xe9, 0x78, 0xd5, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x00, 0x02, 0x00,
195             0x00, 0x01, 0x40, 0xd2, 0xbd, 0x4e, 0x3e, 0x10, 0x00, 0x00, 0x20, 0x00, 0x02, 0x00,
196             0x02, 0x00, 0x00, 0x00, 0x00, 0x86, 0xd4, 0xe6, 0xe9, 0x00, 0x00, 0x00, 0x01, 0x81,
197             0xc9, 0x00, 0x07, 0xd2, 0xbd, 0x4e, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86, 0xd4, 0xe6, 0xe9, 0x00,
199             0x00, 0x00, 0x01};
200 
201     RtcpConfigInfo rtcpConfigInfo;
202     RtpBuffer rtpBuffer(84, bufSrSdesPacket);
203     eRTP_STATUS_CODE res = rtcpPacket.decodeRtcpPacket(&rtpBuffer, 0, &rtcpConfigInfo);
204     EXPECT_EQ(res, RTP_SUCCESS);
205 
206     // Check SR packet.
207     std::list<RtcpSrPacket*> SrList = rtcpPacket.getSrPacketList();
208     ASSERT_TRUE(SrList.size() != 0);
209 
210     RtcpSrPacket* rtcpSrPacket = SrList.front();
211     ASSERT_TRUE(rtcpSrPacket != nullptr);
212 
213     RtcpHeader* pRtcpHeader = rtcpSrPacket->getRtcpHdrInfo();
214     ASSERT_TRUE(pRtcpHeader != nullptr);
215 
216     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
217     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_FALSE);
218     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 1);
219     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_SR);
220     EXPECT_EQ(pRtcpHeader->getLength(), 12 * RTP_WORD_SIZE);
221     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xd2bd4e3e);
222 
223     tRTP_NTP_TIME* ntpTime = rtcpSrPacket->getNtpTime();
224     ASSERT_TRUE(ntpTime != nullptr);
225 
226     EXPECT_EQ(ntpTime->m_uiNtpHigh32Bits, 3314714324);
227     EXPECT_EQ(ntpTime->m_uiNtpLow32Bits, 3874060501);
228     EXPECT_EQ(rtcpSrPacket->getRtpTimestamp(), 320);
229     EXPECT_EQ(rtcpSrPacket->getSendPktCount(), 2);
230     EXPECT_EQ(rtcpSrPacket->getSendOctetCount(), 320);
231 
232     RtcpRrPacket* pRRInfo = rtcpSrPacket->getRrPktInfo();
233     ASSERT_TRUE(pRRInfo != nullptr);
234 
235     std::list<RtcpReportBlock*> reports = pRRInfo->getReportBlockList();
236     ASSERT_TRUE(reports.size() != 0);
237 
238     RtcpReportBlock* report = reports.front();
239     ASSERT_TRUE(report != nullptr);
240 
241     EXPECT_EQ(report->getSsrc(), 0xd2bd4e3e);
242     EXPECT_EQ((int)report->getFracLost(), 0x10);
243     EXPECT_EQ((int)report->getCumNumPktLost(), 0x000020);
244     EXPECT_EQ(report->getExtHighSeqRcv(), 131074);
245     EXPECT_EQ(report->getJitter(), 0);
246     EXPECT_EQ(report->getLastSR(), 2262099689);
247     EXPECT_EQ(report->getDelayLastSR(), 1);
248 
249     // Check RR packet.
250     std::list<RtcpRrPacket*> RrList = rtcpPacket.getRrPacketList();
251     ASSERT_TRUE(RrList.size() != 0);
252 
253     RtcpRrPacket* pRrPkt = RrList.front();
254     ASSERT_TRUE(pRrPkt != nullptr);
255 
256     pRtcpHeader = pRrPkt->getRtcpHdrInfo();
257     ASSERT_TRUE(pRtcpHeader != nullptr);
258 
259     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
260     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_FALSE);
261     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 1);
262     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_RR);
263     EXPECT_EQ(pRtcpHeader->getLength(), 7 * RTP_WORD_SIZE);
264     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xd2bd4e3e);
265 
266     reports = pRrPkt->getReportBlockList();
267     ASSERT_TRUE(reports.size() != 0);
268 
269     report = reports.front();
270     ASSERT_TRUE(report != nullptr);
271 
272     EXPECT_EQ(report->getSsrc(), 0);
273     EXPECT_EQ((int)report->getFracLost(), 0);
274     EXPECT_EQ((int)report->getCumNumPktLost(), 0);
275     EXPECT_EQ(report->getExtHighSeqRcv(), 0);
276     EXPECT_EQ(report->getJitter(), 0);
277     EXPECT_EQ(report->getLastSR(), 2262099689);
278     EXPECT_EQ(report->getDelayLastSR(), 1);
279 }
280 
281 /**
282  * Test RTCP packet with Sender Report, Receiver Report and SDES.
283  */
TEST_F(RtcpPacketTest,DecodeCompoundSrRrSdesPacket)284 TEST_F(RtcpPacketTest, DecodeCompoundSrRrSdesPacket)
285 {
286     RtcpPacket rtcpPacket;
287 
288     /*
289      * Real-time Transport Control Protocol (Sender Report)
290      *   [Stream setup by SDP (frame 1)]
291      *   10.. .... = Version: RFC 1889 Version (2)
292      *   ..0. .... = Padding: False
293      *   ...0 0001 = Reception report count: 1
294      *   Packet type: Sender Report (200)
295      *   Length: 12 (52 bytes)
296      *   Sender SSRC: 0xd2bd4e3e (3535621694)
297      *   Timestamp, MSW: 3314714324 (0xc59286d4)
298      *   Timestamp, LSW: 4131758539 (0xf645a1cb)
299      *   [MSW and LSW as NTP timestamp: Jan 14, 2005 17:58:44.962000000 UTC]
300      *   RTP timestamp: 640
301      *   Sender's packet count: 4
302      *   Sender's octet count: 640
303      *   Source 1
304      *       Identifier: 0xd2bd4e3e (3535621694)
305      *       SSRC contents
306      *           Fraction lost: 0 / 256
307      *           Cumulative number of packets lost: 0
308      *       Extended highest sequence number received: 262148
309      *       Interarrival jitter: 0
310      *       Last SR timestamp: 2262103621 (0x86d4f645)
311      *       Delay since last SR timestamp: 1 (0 milliseconds)
312      * Real-time Transport Control Protocol (Receiver Report)
313      *   [Stream setup by SDP (frame 1)]
314      *   10.. .... = Version: RFC 1889 Version (2)
315      *   ..0. .... = Padding: False
316      *   ...0 0001 = Reception report count: 1
317      *   Packet type: Receiver Report (201)
318      *   Length: 7 (32 bytes)
319      *   Sender SSRC: 0xd2bd4e3e (3535621694)
320      *   Source 1
321      *      Identifier: 0x58f33dea (1492336106)
322      *       SSRC contents
323      *          Fraction lost: 0 / 256
324      *           Cumulative number of packets lost: 0
325      *       Extended highest sequence number received: 11332
326      *       Interarrival jitter: 0
327      *       Last SR timestamp: 2262103621 (0x86d4f645)
328      *       Delay since last SR timestamp: 1 (0 milliseconds)
329      * Real-time Transport Control Protocol (Source description)
330      *  [Stream setup by SDP (frame 1)]
331      *   10.. .... = Version: RFC 1889 Version (2)
332      *   ..0. .... = Padding: False
333      *   ...0 0001 = Source count: 1
334      *   Packet type: Source description (202)
335      *   Length: 7 (32 bytes)
336      *   Chunk 1, SSRC/CSRC 0xD2BD4E3E
337      *       Identifier: 0xd2bd4e3e (3535621694)
338      *       SDES items
339      *           Type: CNAME (user and domain) (1)
340      *           Length: 20
341      *           Text: [email protected]
342      *           Type: END (0)
343      */
344     uint8_t bufSrSdesPacket[] = {0x81, 0xc8, 0x00, 0x0c, 0xd2, 0xbd, 0x4e, 0x3e, 0xc5, 0x92, 0x86,
345             0xd4, 0xf6, 0x45, 0xa1, 0xcb, 0x00, 0x00, 0x02, 0x80, 0x00, 0x00, 0x00, 0x04, 0x00,
346             0x00, 0x02, 0x80, 0xd2, 0xbd, 0x4e, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
347             0x04, 0x00, 0x00, 0x00, 0x00, 0x86, 0xd4, 0xf6, 0x45, 0x00, 0x00, 0x00, 0x01, 0x81,
348             0xc9, 0x00, 0x07, 0xd2, 0xbd, 0x4e, 0x3e, 0x58, 0xf3, 0x3d, 0xea, 0x00, 0x00, 0x00,
349             0x00, 0x00, 0x00, 0x2c, 0x44, 0x00, 0x00, 0x00, 0x00, 0x86, 0xd4, 0xf6, 0x45, 0x00,
350             0x00, 0x00, 0x01, 0x81, 0xca, 0x00, 0x07, 0xd2, 0xbd, 0x4e, 0x3e, 0x01, 0x14, 0x75,
351             0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x40, 0x32, 0x30, 0x30, 0x2e, 0x35, 0x37, 0x2e,
352             0x37, 0x2e, 0x32, 0x30, 0x34, 0x00, 0x00, 0x00, 0x00};
353 
354     RtpBuffer rtpBuffer(118, bufSrSdesPacket);
355 
356     RtpDt_UChar IPAddress[] = "2600:100e:1008:af4f::1ebe:6851";
357     RtcpConfigInfo rtcpConfigInfo;
358     addSdesItem(&rtcpConfigInfo, IPAddress, strlen(reinterpret_cast<char*>(IPAddress)));
359 
360     eRTP_STATUS_CODE res = rtcpPacket.decodeRtcpPacket(&rtpBuffer, 0, &rtcpConfigInfo);
361     EXPECT_EQ(res, RTP_SUCCESS);
362 
363     // Check SR packet.
364     std::list<RtcpSrPacket*> SrList = rtcpPacket.getSrPacketList();
365     ASSERT_TRUE(SrList.size() != 0);
366 
367     RtcpSrPacket* rtcpSrPacket = SrList.front();
368     ASSERT_TRUE(rtcpSrPacket != nullptr);
369 
370     RtcpHeader* pRtcpHeader = rtcpSrPacket->getRtcpHdrInfo();
371     ASSERT_TRUE(pRtcpHeader != nullptr);
372 
373     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
374     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_FALSE);
375     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 1);
376     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_SR);
377     EXPECT_EQ(pRtcpHeader->getLength(), 12 * RTP_WORD_SIZE);
378     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xd2bd4e3e);
379 
380     tRTP_NTP_TIME* ntpTime = rtcpSrPacket->getNtpTime();
381     ASSERT_TRUE(ntpTime != nullptr);
382 
383     EXPECT_EQ(ntpTime->m_uiNtpHigh32Bits, 3314714324);
384     EXPECT_EQ(ntpTime->m_uiNtpLow32Bits, 4131758539);
385     EXPECT_EQ(rtcpSrPacket->getRtpTimestamp(), 640);
386     EXPECT_EQ(rtcpSrPacket->getSendPktCount(), 4);
387     EXPECT_EQ(rtcpSrPacket->getSendOctetCount(), 640);
388 
389     RtcpRrPacket* pRRInfo = rtcpSrPacket->getRrPktInfo();
390     ASSERT_TRUE(pRRInfo != nullptr);
391 
392     std::list<RtcpReportBlock*> reports = pRRInfo->getReportBlockList();
393     ASSERT_TRUE(reports.size() != 0);
394 
395     RtcpReportBlock* report = reports.front();
396     ASSERT_TRUE(report != nullptr);
397 
398     EXPECT_EQ(report->getSsrc(), 0xd2bd4e3e);
399     EXPECT_EQ((int)report->getFracLost(), 0);
400     EXPECT_EQ((int)report->getCumNumPktLost(), 0);
401     EXPECT_EQ(report->getExtHighSeqRcv(), 262148);
402     EXPECT_EQ(report->getJitter(), 0);
403     EXPECT_EQ(report->getLastSR(), 2262103621);
404     EXPECT_EQ(report->getDelayLastSR(), 1);
405 
406     // Check RR packet.
407     std::list<RtcpRrPacket*> RrList = rtcpPacket.getRrPacketList();
408     ASSERT_TRUE(RrList.size() != 0);
409 
410     RtcpRrPacket* pRrPkt = RrList.front();
411     ASSERT_TRUE(pRrPkt != nullptr);
412 
413     pRtcpHeader = pRrPkt->getRtcpHdrInfo();
414     ASSERT_TRUE(pRtcpHeader != nullptr);
415 
416     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
417     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_FALSE);
418     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 1);
419     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_RR);
420     EXPECT_EQ(pRtcpHeader->getLength(), 7 * RTP_WORD_SIZE);
421     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xd2bd4e3e);
422 
423     reports = pRrPkt->getReportBlockList();
424     ASSERT_TRUE(reports.size() != 0);
425 
426     report = reports.front();
427     ASSERT_TRUE(report != nullptr);
428 
429     EXPECT_EQ(report->getSsrc(), 0x58f33dea);
430     EXPECT_EQ((int)report->getFracLost(), 0);
431     EXPECT_EQ((int)report->getCumNumPktLost(), 0);
432     EXPECT_EQ(report->getExtHighSeqRcv(), 11332);
433     EXPECT_EQ(report->getJitter(), 0);
434     EXPECT_EQ(report->getLastSR(), 2262103621);
435     EXPECT_EQ(report->getDelayLastSR(), 1);
436 
437     // Check SDES
438     RtcpSdesPacket* pRtcpSdesPacket = rtcpPacket.getSdesPacket();
439     ASSERT_TRUE(pRtcpSdesPacket != nullptr);
440 
441     std::list<RtcpChunk*> pSdesChunks = pRtcpSdesPacket->getSdesChunkList();
442     EXPECT_EQ(pSdesChunks.size(), 1);
443     RtcpChunk* chunk = pSdesChunks.front();
444     ASSERT_TRUE(chunk != nullptr);
445 
446     std::list<tRTCP_SDES_ITEM*> sdesItemList = chunk->getSdesItemList();
447     EXPECT_EQ(sdesItemList.size(), 1);
448     tRTCP_SDES_ITEM* sdesItem = sdesItemList.front();
449     ASSERT_TRUE(sdesItem != nullptr);
450 
451     EXPECT_EQ(sdesItem->ucType, 1);
452     EXPECT_EQ(sdesItem->ucLength, 20);
453     const char* expectedpValue = "[email protected]";
454     EXPECT_EQ(strncmp(reinterpret_cast<char*>(sdesItem->pValue), expectedpValue,
455                       strlen(expectedpValue)),
456             0);
457 
458     pRtcpHeader = pRtcpSdesPacket->getRtcpHdrInfo();
459     ASSERT_TRUE(pRtcpHeader != nullptr);
460 
461     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
462     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_FALSE);
463     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 1);
464     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_SDES);
465     EXPECT_EQ(pRtcpHeader->getLength(), 7 * RTP_WORD_SIZE);
466     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xd2bd4e3e);
467 }
468 
469 /**
470  * Test RTCP BYE packet.
471  */
TEST_F(RtcpPacketTest,TestDecodeByePacket)472 TEST_F(RtcpPacketTest, TestDecodeByePacket)
473 {
474     RtcpPacket rtcpPacket;
475 
476     /*
477      * Real-time Transport Control Protocol (Sender Report)
478      * 10.. .... = Version: RFC 1889 Version (2)
479      * ..1. .... = Padding: True
480      * ...0 0011 = Reception report count: 3
481      * Packet type: Bye (203)
482      * Length: 6 (28 bytes)
483      * SSRC 1: 0xb1c8cb02 (2982726402)
484      * SSRC 1: 0xb1c8cb03 (2982726403)
485      * SSRC 1: 0xb1c8cb04 (2982726404)
486      * Length: 8
487      * Reason for leaving: teardown
488      * padding: 0x000003
489      */
490     uint8_t bufPacket[] = {0xA3, 0xcb, 0x00, 0x6, 0xb1, 0xc8, 0xcb, 0x02, 0xb1, 0xc8, 0xcb, 0x03,
491             0xb1, 0xc8, 0xcb, 0x04, 0x08, 0x74, 0x65, 0x61, 0x72, 0x64, 0x6F, 0x77, 0x6E, 0x00,
492             0x00, 0x03};
493 
494     RtcpConfigInfo rtcpConfigInfo;
495     RtpBuffer rtpBuffer(sizeof(bufPacket), bufPacket);
496     eRTP_STATUS_CODE res = rtcpPacket.decodeRtcpPacket(&rtpBuffer, 0, &rtcpConfigInfo);
497     EXPECT_EQ(res, RTP_SUCCESS);
498 
499     RtcpByePacket* pByePacket = rtcpPacket.getByePacket();
500     ASSERT_TRUE(pByePacket != nullptr);
501 
502     RtcpHeader* pRtcpHeader = pByePacket->getRtcpHdrInfo();
503     ASSERT_TRUE(pRtcpHeader != nullptr);
504 
505     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
506     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_TRUE);
507     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 3);
508     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_BYE);
509     EXPECT_EQ(pRtcpHeader->getLength(), 6 * RTP_WORD_SIZE);
510     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xb1c8cb02);
511 
512     std::list<RtpDt_UInt32*> ssrcList = pByePacket->getSsrcList();
513     EXPECT_TRUE(ssrcList.size() == 2);
514     EXPECT_EQ(*ssrcList.front(), (RtpDt_UInt32)0xb1c8cb03);
515     ssrcList.pop_front();
516     EXPECT_EQ(*ssrcList.front(), (RtpDt_UInt32)0xb1c8cb04);
517 
518     RtpBuffer* reason = pByePacket->getReason();
519     ASSERT_TRUE(reason != nullptr);
520 
521     const char* leaveReason = "teardown";
522     EXPECT_EQ(reason->getLength(), strlen(leaveReason));
523     EXPECT_EQ(memcmp(reason->getBuffer(), leaveReason, strlen(leaveReason)), 0);
524 }
525 
526 /**
527  * Test RTCP APP packet.
528  */
TEST_F(RtcpPacketTest,TestDecodeAppPacket)529 TEST_F(RtcpPacketTest, TestDecodeAppPacket)
530 {
531     RtcpPacket rtcpPacket;
532 
533     /*
534      * Real-time Transport Control Protocol (Sender Report)
535      * 10.. .... = Version: RFC 1889 Version (2)
536      * ..0. .... = Padding: False
537      * ...0 1111 = SubType
538      * Packet type: App (204)
539      * Length: 10 (40 bytes)
540      * SSRC : 0xb1c8cb02 (2982726402)
541      * App defined packet name: TEST
542      * Application data: This is a test application data.
543      */
544     uint8_t bufPacket[] = {0x8F, 0xcc, 0x00, 0x0a, 0xb1, 0xc8, 0xcb, 0x02, 0x54, 0x45, 0x53, 0x54,
545             0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74,
546             0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x64,
547             0x61, 0x74, 0x61, 0x2e};
548 
549     RtcpConfigInfo rtcpConfigInfo;
550     RtpBuffer rtpBuffer(44, bufPacket);
551     eRTP_STATUS_CODE res = rtcpPacket.decodeRtcpPacket(&rtpBuffer, 0, &rtcpConfigInfo);
552     EXPECT_EQ(res, RTP_SUCCESS);
553 
554     RtcpAppPacket* pAppPacket = rtcpPacket.getAppPacket();
555     ASSERT_TRUE(pAppPacket != nullptr);
556 
557     RtcpHeader* pRtcpHeader = pAppPacket->getRtcpHdrInfo();
558     ASSERT_TRUE(pRtcpHeader != nullptr);
559 
560     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
561     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_FALSE);
562     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 0x0f);
563     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_APP);
564     EXPECT_EQ(pRtcpHeader->getLength(), 10 * RTP_WORD_SIZE);
565     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xb1c8cb02);
566     RtpDt_UInt32 appPktName = pAppPacket->getName();
567     const char* pktName = "TEST";
568     EXPECT_EQ(memcmp(&appPktName, pktName, strlen(pktName)), 0);
569     RtpBuffer* pAppData = pAppPacket->getAppData();
570     ASSERT_TRUE(pAppData != nullptr);
571     const char* appData = "This is a test application data.";
572     EXPECT_EQ(memcmp(pAppData->getBuffer(), appData, strlen(appData)), 0);
573     EXPECT_EQ(pAppData->getLength(), strlen(appData));
574 }
575 
576 /**
577  * Test RTCP Feedback packet decoding.
578  */
TEST_F(RtcpPacketTest,TestDecodeFBPacket)579 TEST_F(RtcpPacketTest, TestDecodeFBPacket)
580 {
581     RtcpPacket rtcpPacket;
582 
583     /*
584      * Real-time Transport Control Protocol (Payload- specific)
585      * 10.. .... = Version: RFC 1889 Version (2)
586      * ..0. .... = Padding: False
587      * ...0 0001 = PLI: Picture Loss Indication.
588      * Packet type: PSFB (206)
589      * Length: 2 (8 bytes)
590      * Sender SSRC : 0xb1c8cb02 (2982726402)
591      * Media SSRC : 0xb1c8cb03 (2982726402)
592      *
593      * Real-time Transport Control Protocol (Generic RTP Feedback): TMMBR: 2097152
594      * 10.. .... = Version: RFC 1889 Version (2)
595      * ..0. .... = Padding: False
596      * ...0 0011 = PLI: TMMBR: Temp Max Media stream Bit Rate Request.
597      * Packet type: PSFB (205)
598      * Length: 4 (16 bytes)
599      * Sender SSRC : 0xb1c8cb02 (2982726402)
600      * Media SSRC : 0xb1c8cb03 (2982726402)
601      * 8bytes of test data: TMMBR***
602      */
603     uint8_t bufPacket[] = {0x81, 0xce, 0x00, 0x02, 0xb1, 0xc8, 0xcb, 0x02, 0xb1, 0xc8, 0xcb, 0x03,
604             0x83, 0xcd, 0x00, 0x04, 0xb1, 0xc8, 0xcb, 0x02, 0xb1, 0xc8, 0xcb, 0x03, 0x54, 0x4d,
605             0x4d, 0x42, 0x52, 0x2a, 0x2a, 0x2a};
606 
607     RtcpConfigInfo rtcpConfigInfo;
608     RtpBuffer rtpBuffer(32, bufPacket);
609     eRTP_STATUS_CODE res = rtcpPacket.decodeRtcpPacket(&rtpBuffer, 0, &rtcpConfigInfo);
610     EXPECT_EQ(res, RTP_SUCCESS);
611 
612     std::list<RtcpFbPacket*> fbpktList = rtcpPacket.getFbPacketList();
613     EXPECT_EQ(fbpktList.size(), 2);
614     RtcpFbPacket* fbpkt = fbpktList.front();
615     ASSERT_TRUE(fbpkt != nullptr);
616 
617     RtcpHeader* pRtcpHeader = fbpkt->getRtcpHdrInfo();
618     ASSERT_TRUE(pRtcpHeader != nullptr);
619 
620     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
621     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_FALSE);
622     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 1);
623     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_PSFB);
624     EXPECT_EQ(pRtcpHeader->getLength(), 2 * RTP_WORD_SIZE);
625     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xb1c8cb02);
626     EXPECT_EQ(fbpkt->getMediaSsrc(), 0xb1c8cb03);
627 
628     fbpktList.pop_front();
629     fbpkt = fbpktList.front();
630     ASSERT_TRUE(fbpkt != nullptr);
631 
632     pRtcpHeader = fbpkt->getRtcpHdrInfo();
633     ASSERT_TRUE(pRtcpHeader != nullptr);
634 
635     EXPECT_EQ(pRtcpHeader->getVersion(), RTP_VERSION_NUM);
636     EXPECT_EQ(pRtcpHeader->getPadding(), eRTP_FALSE);
637     EXPECT_EQ(pRtcpHeader->getReceptionReportCount(), 3);
638     EXPECT_EQ(pRtcpHeader->getPacketType(), RTCP_RTPFB);
639     EXPECT_EQ(pRtcpHeader->getLength(), 4 * RTP_WORD_SIZE);
640     EXPECT_EQ(pRtcpHeader->getSsrc(), 0xb1c8cb02);
641     EXPECT_EQ(fbpkt->getMediaSsrc(), 0xb1c8cb03);
642 }
643 
644 /**
645  * Test RTCP packet with only SR header.
646  */
TEST_F(RtcpPacketTest,DecodeOnlyRtcpSRHeader)647 TEST_F(RtcpPacketTest, DecodeOnlyRtcpSRHeader)
648 {
649     RtcpPacket rtcpPacket;
650 
651     /*
652      * Real-time Transport Control Protocol (Sender Report)
653      * 10.. .... = Version: RFC 1889 Version (2)
654      * ..0. .... = Padding: False
655      * ...0 0000 = Reception report count: 0
656      * Packet type: Sender Report (200)
657      * Length: 0 (0 bytes)
658      */
659     uint8_t bufPacket[] = {0x80, 0xc8, 0x00, 0x0};
660 
661     RtcpConfigInfo rtcpConfigInfo;
662     RtpBuffer rtpBuffer(4, bufPacket);
663     eRTP_STATUS_CODE res = rtcpPacket.decodeRtcpPacket(&rtpBuffer, 0, &rtcpConfigInfo);
664     EXPECT_EQ(res, RTP_SUCCESS);
665 
666     RtcpHeader pRtcpHeader = rtcpPacket.getHeader();
667     EXPECT_EQ(pRtcpHeader.getVersion(), RTP_VERSION_NUM);
668     EXPECT_EQ(pRtcpHeader.getPadding(), eRTP_FALSE);
669     EXPECT_EQ(pRtcpHeader.getReceptionReportCount(), 0);
670     EXPECT_EQ(pRtcpHeader.getPacketType(), RTCP_SR);
671     EXPECT_EQ(pRtcpHeader.getLength(), 0 * RTP_WORD_SIZE);
672 }
673 
674 /**
675  * Test RTCP XR packet.
676  */
TEST_F(RtcpPacketTest,TestDecodeRtcpXrPacket)677 TEST_F(RtcpPacketTest, TestDecodeRtcpXrPacket)
678 {
679     RtcpPacket rtcpPacket;
680 
681     /*
682      * Real-time Transport Control Protocol (Sender Report)
683      * 10.. .... = Version: RFC 1889 Version (2)
684      * ..1. .... = Padding: False
685      * ...0 0001 = Report count: 1
686      * Packet type: XR (207)
687      * Length: 5 (24 bytes)
688      * SSRC : 0xb1c8cb02 (2982726402)
689      * 0x00, 0x00, 0x00, 0x01, // XR block type: VoIP Metrics Report Block (207)
690      * 0x00, 0x0A,             // Length of the XR block in 32-bit words: 10
691      * 0x02, 0x01,             // Loss rate (packets lost per million packets sent): 2 bytes;
692      * Type-specific: 1 0x00, 0x64,             // Loss rate: 100 0x03, 0x01,             // Delay
693      * since last report (milliseconds): 2 bytes; Type-specific: 1 0x00, 0x3C,             // Delay:
694      * 60 milliseconds
695      */
696     uint8_t bufPacket[] = {0xa1, 0xcf, 0x00, 0x05, 0xb1, 0xc8, 0xcb, 0x02, 0x00, 0x00, 0x00, 0x01,
697             0x00, 0x0A, 0x02, 0x01, 0x00, 0x64, 0x03, 0x01, 0x00, 0x3C, 0x00, 0x02};
698 
699     RtcpConfigInfo rtcpConfigInfo;
700     RtpBuffer rtpBuffer(24, bufPacket);
701     eRTP_STATUS_CODE res = rtcpPacket.decodeRtcpPacket(&rtpBuffer, 0, &rtcpConfigInfo);
702     EXPECT_EQ(res, RTP_SUCCESS);
703 
704     // TODO: After Rtcp-Xr decoder function is implemented, add checks for each files in XR report.
705 }
706 
TEST_F(RtcpPacketTest,CheckAllGetSets)707 TEST_F(RtcpPacketTest, CheckAllGetSets)
708 {
709     RtcpPacket rtcpPacket;
710 
711     RtcpSdesPacket* sdesPacket = new RtcpSdesPacket();
712     rtcpPacket.setSdesPacketData(sdesPacket);
713     EXPECT_EQ(sdesPacket, rtcpPacket.getSdesPacket());
714 
715     RtcpByePacket* byePacket = new RtcpByePacket();
716     rtcpPacket.setByePacketData(byePacket);
717     EXPECT_EQ(byePacket, rtcpPacket.getByePacket());
718 
719     RtcpAppPacket* appPacket = new RtcpAppPacket();
720     rtcpPacket.setAppPktData(appPacket);
721     EXPECT_EQ(appPacket, rtcpPacket.getAppPacket());
722 
723     RtcpXrPacket* XrPacket = new RtcpXrPacket();
724     rtcpPacket.setXrPacket(XrPacket);
725     EXPECT_EQ(XrPacket, rtcpPacket.getXrPacket());
726 
727     RtcpSrPacket* srPacket1 = new RtcpSrPacket();
728     RtcpSrPacket* srPacket2 = new RtcpSrPacket();
729     rtcpPacket.addSrPacketData(srPacket1);
730     rtcpPacket.addSrPacketData(srPacket2);
731     std::list<RtcpSrPacket*> srList = rtcpPacket.getSrPacketList();
732     EXPECT_TRUE(srPacket1 == srList.front());
733     srList.pop_front();
734     EXPECT_TRUE(srPacket2 == srList.front());
735     srList.clear();
736 
737     RtcpRrPacket* RrPacket1 = new RtcpRrPacket();
738     RtcpRrPacket* RrPacket2 = new RtcpRrPacket();
739     rtcpPacket.addRrPacketData(RrPacket1);
740     rtcpPacket.addRrPacketData(RrPacket2);
741     std::list<RtcpRrPacket*> rrList = rtcpPacket.getRrPacketList();
742     EXPECT_TRUE(RrPacket1 == rrList.front());
743     rrList.pop_front();
744     EXPECT_TRUE(RrPacket2 == rrList.front());
745     rrList.clear();
746 
747     RtcpFbPacket* FbPacket1 = new RtcpFbPacket();
748     RtcpFbPacket* FbPacket2 = new RtcpFbPacket();
749     rtcpPacket.addFbPacketData(FbPacket1);
750     rtcpPacket.addFbPacketData(FbPacket2);
751     std::list<RtcpFbPacket*> fbList = rtcpPacket.getFbPacketList();
752     EXPECT_TRUE(FbPacket1 == fbList.front());
753     fbList.pop_front();
754     EXPECT_TRUE(FbPacket2 == fbList.front());
755     fbList.clear();
756 }