xref: /aosp_15_r20/external/webrtc/test/rtp_file_writer_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2014 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_writer.h"
12 
13 #include <stdint.h>
14 #include <string.h>
15 
16 #include <memory>
17 
18 #include "test/gtest.h"
19 #include "test/rtp_file_reader.h"
20 #include "test/testsupport/file_utils.h"
21 
22 namespace webrtc {
23 
24 class RtpFileWriterTest : public ::testing::Test {
25  public:
Init(const std::string & filename)26   void Init(const std::string& filename) {
27     filename_ = test::OutputPath() + filename;
28     rtp_writer_.reset(
29         test::RtpFileWriter::Create(test::RtpFileWriter::kRtpDump, filename_));
30   }
31 
WriteRtpPackets(int num_packets)32   void WriteRtpPackets(int num_packets) {
33     ASSERT_TRUE(rtp_writer_.get() != NULL);
34     test::RtpPacket packet;
35     for (int i = 1; i <= num_packets; ++i) {
36       packet.length = i;
37       packet.original_length = i;
38       packet.time_ms = i;
39       memset(packet.data, i, packet.length);
40       EXPECT_TRUE(rtp_writer_->WritePacket(&packet));
41     }
42   }
43 
CloseOutputFile()44   void CloseOutputFile() { rtp_writer_.reset(); }
45 
VerifyFileContents(int expected_packets)46   void VerifyFileContents(int expected_packets) {
47     ASSERT_TRUE(rtp_writer_.get() == NULL)
48         << "Must call CloseOutputFile before VerifyFileContents";
49     std::unique_ptr<test::RtpFileReader> rtp_reader(
50         test::RtpFileReader::Create(test::RtpFileReader::kRtpDump, filename_));
51     ASSERT_TRUE(rtp_reader.get() != NULL);
52     test::RtpPacket packet;
53     int i = 0;
54     while (rtp_reader->NextPacket(&packet)) {
55       ++i;
56       EXPECT_EQ(static_cast<size_t>(i), packet.length);
57       EXPECT_EQ(static_cast<size_t>(i), packet.original_length);
58       EXPECT_EQ(static_cast<uint32_t>(i), packet.time_ms);
59       for (int j = 0; j < i; ++j) {
60         EXPECT_EQ(i, packet.data[j]);
61       }
62     }
63     EXPECT_EQ(expected_packets, i);
64   }
65 
66  private:
67   std::unique_ptr<test::RtpFileWriter> rtp_writer_;
68   std::string filename_;
69 };
70 
TEST_F(RtpFileWriterTest,WriteToRtpDump)71 TEST_F(RtpFileWriterTest, WriteToRtpDump) {
72   Init("test_rtp_file_writer.rtp");
73   WriteRtpPackets(10);
74   CloseOutputFile();
75   VerifyFileContents(10);
76 }
77 
78 }  // namespace webrtc
79