blob: a6281c778780d47cb2c93b4e9f7221fffe272cad [file] [log] [blame]
henrik.lundin@webrtc.org83317142014-12-01 11:25:04 +00001/*
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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "test/rtp_file_writer.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stdint.h>
henrik.lundin@webrtc.org83317142014-12-01 11:25:04 +000014#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
kwibergbfefb032016-05-01 14:53:46 -070016#include <memory>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "test/gtest.h"
19#include "test/rtp_file_reader.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "test/testsupport/file_utils.h"
henrik.lundin@webrtc.org83317142014-12-01 11:25:04 +000021
22namespace webrtc {
23
24class RtpFileWriterTest : public ::testing::Test {
25 public:
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
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
44 void CloseOutputFile() { rtp_writer_.reset(); }
45
46 void VerifyFileContents(int expected_packets) {
47 ASSERT_TRUE(rtp_writer_.get() == NULL)
48 << "Must call CloseOutputFile before VerifyFileContents";
kwibergbfefb032016-05-01 14:53:46 -070049 std::unique_ptr<test::RtpFileReader> rtp_reader(
henrik.lundin@webrtc.org83317142014-12-01 11:25:04 +000050 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:
kwibergbfefb032016-05-01 14:53:46 -070067 std::unique_ptr<test::RtpFileWriter> rtp_writer_;
henrik.lundin@webrtc.org83317142014-12-01 11:25:04 +000068 std::string filename_;
69};
70
71TEST_F(RtpFileWriterTest, WriteToRtpDump) {
72 Init("test_rtp_file_writer.rtp");
73 WriteRtpPackets(10);
74 CloseOutputFile();
75 VerifyFileContents(10);
76}
77
78} // namespace webrtc