xref: /aosp_15_r20/external/webrtc/test/rtp_file_reader_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "test/rtp_file_reader.h"
12 
13 #include <map>
14 #include <memory>
15 
16 #include "api/array_view.h"
17 #include "modules/rtp_rtcp/source/rtp_util.h"
18 #include "test/gtest.h"
19 #include "test/testsupport/file_utils.h"
20 
21 namespace webrtc {
22 
23 class TestRtpFileReader : public ::testing::Test {
24  public:
Init(const std::string & filename,bool headers_only_file)25   void Init(const std::string& filename, bool headers_only_file) {
26     std::string filepath =
27         test::ResourcePath("video_coding/" + filename, "rtp");
28     rtp_packet_source_.reset(
29         test::RtpFileReader::Create(test::RtpFileReader::kRtpDump, filepath));
30     ASSERT_TRUE(rtp_packet_source_.get() != NULL);
31     headers_only_file_ = headers_only_file;
32   }
33 
CountRtpPackets()34   int CountRtpPackets() {
35     test::RtpPacket packet;
36     int c = 0;
37     while (rtp_packet_source_->NextPacket(&packet)) {
38       if (headers_only_file_)
39         EXPECT_LT(packet.length, packet.original_length);
40       else
41         EXPECT_EQ(packet.length, packet.original_length);
42       c++;
43     }
44     return c;
45   }
46 
47  private:
48   std::unique_ptr<test::RtpFileReader> rtp_packet_source_;
49   bool headers_only_file_;
50 };
51 
TEST_F(TestRtpFileReader,Test60Packets)52 TEST_F(TestRtpFileReader, Test60Packets) {
53   Init("pltype103", false);
54   EXPECT_EQ(60, CountRtpPackets());
55 }
56 
TEST_F(TestRtpFileReader,Test60PacketsHeaderOnly)57 TEST_F(TestRtpFileReader, Test60PacketsHeaderOnly) {
58   Init("pltype103_header_only", true);
59   EXPECT_EQ(60, CountRtpPackets());
60 }
61 
62 typedef std::map<uint32_t, int> PacketsPerSsrc;
63 
64 class TestPcapFileReader : public ::testing::Test {
65  public:
Init(const std::string & filename)66   void Init(const std::string& filename) {
67     std::string filepath =
68         test::ResourcePath("video_coding/" + filename, "pcap");
69     rtp_packet_source_.reset(
70         test::RtpFileReader::Create(test::RtpFileReader::kPcap, filepath));
71     ASSERT_TRUE(rtp_packet_source_.get() != NULL);
72   }
73 
CountRtpPackets()74   int CountRtpPackets() {
75     int c = 0;
76     test::RtpPacket packet;
77     while (rtp_packet_source_->NextPacket(&packet)) {
78       EXPECT_EQ(packet.length, packet.original_length);
79       c++;
80     }
81     return c;
82   }
83 
CountRtpPacketsPerSsrc()84   PacketsPerSsrc CountRtpPacketsPerSsrc() {
85     PacketsPerSsrc pps;
86     test::RtpPacket packet;
87     while (rtp_packet_source_->NextPacket(&packet)) {
88       rtc::ArrayView<const uint8_t> raw(packet.data, packet.length);
89       if (IsRtpPacket(raw)) {
90         pps[ParseRtpSsrc(raw)]++;
91       }
92     }
93     return pps;
94   }
95 
96  private:
97   std::unique_ptr<test::RtpFileReader> rtp_packet_source_;
98 };
99 
TEST_F(TestPcapFileReader,TestEthernetIIFrame)100 TEST_F(TestPcapFileReader, TestEthernetIIFrame) {
101   Init("frame-ethernet-ii");
102   EXPECT_EQ(368, CountRtpPackets());
103 }
104 
TEST_F(TestPcapFileReader,TestLoopbackFrame)105 TEST_F(TestPcapFileReader, TestLoopbackFrame) {
106   Init("frame-loopback");
107   EXPECT_EQ(491, CountRtpPackets());
108 }
109 
TEST_F(TestPcapFileReader,TestTwoSsrc)110 TEST_F(TestPcapFileReader, TestTwoSsrc) {
111   Init("ssrcs-2");
112   PacketsPerSsrc pps = CountRtpPacketsPerSsrc();
113   EXPECT_EQ(2UL, pps.size());
114   EXPECT_EQ(370, pps[0x78d48f61]);
115   EXPECT_EQ(60, pps[0xae94130b]);
116 }
117 
TEST_F(TestPcapFileReader,TestThreeSsrc)118 TEST_F(TestPcapFileReader, TestThreeSsrc) {
119   Init("ssrcs-3");
120   PacketsPerSsrc pps = CountRtpPacketsPerSsrc();
121   EXPECT_EQ(3UL, pps.size());
122   EXPECT_EQ(162, pps[0x938c5eaa]);
123   EXPECT_EQ(113, pps[0x59fe6ef0]);
124   EXPECT_EQ(61, pps[0xed2bd2ac]);
125 }
126 }  // namespace webrtc
127