henrik.lundin@webrtc.org | 8331714 | 2014-12-01 11:25:04 +0000 | [diff] [blame] | 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 <string.h> |
| 12 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "test/gtest.h" |
| 16 | #include "test/rtp_file_reader.h" |
| 17 | #include "test/rtp_file_writer.h" |
| 18 | #include "test/testsupport/fileutils.h" |
henrik.lundin@webrtc.org | 8331714 | 2014-12-01 11:25:04 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | class RtpFileWriterTest : public ::testing::Test { |
| 23 | public: |
| 24 | void Init(const std::string& filename) { |
| 25 | filename_ = test::OutputPath() + filename; |
| 26 | rtp_writer_.reset( |
| 27 | test::RtpFileWriter::Create(test::RtpFileWriter::kRtpDump, filename_)); |
| 28 | } |
| 29 | |
| 30 | void WriteRtpPackets(int num_packets) { |
| 31 | ASSERT_TRUE(rtp_writer_.get() != NULL); |
| 32 | test::RtpPacket packet; |
| 33 | for (int i = 1; i <= num_packets; ++i) { |
| 34 | packet.length = i; |
| 35 | packet.original_length = i; |
| 36 | packet.time_ms = i; |
| 37 | memset(packet.data, i, packet.length); |
| 38 | EXPECT_TRUE(rtp_writer_->WritePacket(&packet)); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void CloseOutputFile() { rtp_writer_.reset(); } |
| 43 | |
| 44 | void VerifyFileContents(int expected_packets) { |
| 45 | ASSERT_TRUE(rtp_writer_.get() == NULL) |
| 46 | << "Must call CloseOutputFile before VerifyFileContents"; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 47 | std::unique_ptr<test::RtpFileReader> rtp_reader( |
henrik.lundin@webrtc.org | 8331714 | 2014-12-01 11:25:04 +0000 | [diff] [blame] | 48 | test::RtpFileReader::Create(test::RtpFileReader::kRtpDump, filename_)); |
| 49 | ASSERT_TRUE(rtp_reader.get() != NULL); |
| 50 | test::RtpPacket packet; |
| 51 | int i = 0; |
| 52 | while (rtp_reader->NextPacket(&packet)) { |
| 53 | ++i; |
| 54 | EXPECT_EQ(static_cast<size_t>(i), packet.length); |
| 55 | EXPECT_EQ(static_cast<size_t>(i), packet.original_length); |
| 56 | EXPECT_EQ(static_cast<uint32_t>(i), packet.time_ms); |
| 57 | for (int j = 0; j < i; ++j) { |
| 58 | EXPECT_EQ(i, packet.data[j]); |
| 59 | } |
| 60 | } |
| 61 | EXPECT_EQ(expected_packets, i); |
| 62 | } |
| 63 | |
| 64 | private: |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 65 | std::unique_ptr<test::RtpFileWriter> rtp_writer_; |
henrik.lundin@webrtc.org | 8331714 | 2014-12-01 11:25:04 +0000 | [diff] [blame] | 66 | std::string filename_; |
| 67 | }; |
| 68 | |
| 69 | TEST_F(RtpFileWriterTest, WriteToRtpDump) { |
| 70 | Init("test_rtp_file_writer.rtp"); |
| 71 | WriteRtpPackets(10); |
| 72 | CloseOutputFile(); |
| 73 | VerifyFileContents(10); |
| 74 | } |
| 75 | |
| 76 | } // namespace webrtc |