henrik.lundin@webrtc.org | 8331714 | 2014-12-01 11:25:04 | [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 | |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 11 | #include "test/rtp_file_writer.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <stdint.h> |
henrik.lundin@webrtc.org | 8331714 | 2014-12-01 11:25:04 | [diff] [blame] | 14 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
kwiberg | bfefb03 | 2016-05-01 21:53:46 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 18 | #include "test/gtest.h" |
| 19 | #include "test/rtp_file_reader.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "test/testsupport/file_utils.h" |
henrik.lundin@webrtc.org | 8331714 | 2014-12-01 11:25:04 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | class 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"; |
kwiberg | bfefb03 | 2016-05-01 21:53:46 | [diff] [blame] | 49 | std::unique_ptr<test::RtpFileReader> rtp_reader( |
henrik.lundin@webrtc.org | 8331714 | 2014-12-01 11:25:04 | [diff] [blame] | 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: |
kwiberg | bfefb03 | 2016-05-01 21:53:46 | [diff] [blame] | 67 | std::unique_ptr<test::RtpFileWriter> rtp_writer_; |
henrik.lundin@webrtc.org | 8331714 | 2014-12-01 11:25:04 | [diff] [blame] | 68 | std::string filename_; |
| 69 | }; |
| 70 | |
| 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 |