henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +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 | // Unit tests for test Packet class. |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_coding/neteq/tools/packet.h" |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "test/gtest.h" |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace test { |
| 19 | |
| 20 | namespace { |
| 21 | const int kHeaderLengthBytes = 12; |
| 22 | |
| 23 | void MakeRtpHeader(int payload_type, |
| 24 | int seq_number, |
| 25 | uint32_t timestamp, |
| 26 | uint32_t ssrc, |
| 27 | uint8_t* rtp_data) { |
| 28 | rtp_data[0] = 0x80; |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 29 | rtp_data[1] = static_cast<uint8_t>(payload_type); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 30 | rtp_data[2] = (seq_number >> 8) & 0xFF; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 31 | rtp_data[3] = (seq_number)&0xFF; |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 32 | rtp_data[4] = timestamp >> 24; |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 33 | rtp_data[5] = (timestamp >> 16) & 0xFF; |
| 34 | rtp_data[6] = (timestamp >> 8) & 0xFF; |
| 35 | rtp_data[7] = timestamp & 0xFF; |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 36 | rtp_data[8] = ssrc >> 24; |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 37 | rtp_data[9] = (ssrc >> 16) & 0xFF; |
| 38 | rtp_data[10] = (ssrc >> 8) & 0xFF; |
| 39 | rtp_data[11] = ssrc & 0xFF; |
| 40 | } |
| 41 | } // namespace |
| 42 | |
| 43 | TEST(TestPacket, RegularPacket) { |
| 44 | const size_t kPacketLengthBytes = 100; |
| 45 | uint8_t* packet_memory = new uint8_t[kPacketLengthBytes]; |
| 46 | const uint8_t kPayloadType = 17; |
| 47 | const uint16_t kSequenceNumber = 4711; |
| 48 | const uint32_t kTimestamp = 47114711; |
| 49 | const uint32_t kSsrc = 0x12345678; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 50 | MakeRtpHeader(kPayloadType, kSequenceNumber, kTimestamp, kSsrc, |
| 51 | packet_memory); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 52 | const double kPacketTime = 1.0; |
| 53 | // Hand over ownership of |packet_memory| to |packet|. |
| 54 | Packet packet(packet_memory, kPacketLengthBytes, kPacketTime); |
| 55 | ASSERT_TRUE(packet.valid_header()); |
| 56 | EXPECT_EQ(kPayloadType, packet.header().payloadType); |
| 57 | EXPECT_EQ(kSequenceNumber, packet.header().sequenceNumber); |
| 58 | EXPECT_EQ(kTimestamp, packet.header().timestamp); |
| 59 | EXPECT_EQ(kSsrc, packet.header().ssrc); |
| 60 | EXPECT_EQ(0, packet.header().numCSRCs); |
| 61 | EXPECT_EQ(kPacketLengthBytes, packet.packet_length_bytes()); |
| 62 | EXPECT_EQ(kPacketLengthBytes - kHeaderLengthBytes, |
| 63 | packet.payload_length_bytes()); |
| 64 | EXPECT_EQ(kPacketLengthBytes, packet.virtual_packet_length_bytes()); |
| 65 | EXPECT_EQ(kPacketLengthBytes - kHeaderLengthBytes, |
| 66 | packet.virtual_payload_length_bytes()); |
| 67 | EXPECT_EQ(kPacketTime, packet.time_ms()); |
| 68 | } |
| 69 | |
| 70 | TEST(TestPacket, DummyPacket) { |
| 71 | const size_t kPacketLengthBytes = kHeaderLengthBytes; // Only RTP header. |
| 72 | const size_t kVirtualPacketLengthBytes = 100; |
| 73 | uint8_t* packet_memory = new uint8_t[kPacketLengthBytes]; |
| 74 | const uint8_t kPayloadType = 17; |
| 75 | const uint16_t kSequenceNumber = 4711; |
| 76 | const uint32_t kTimestamp = 47114711; |
| 77 | const uint32_t kSsrc = 0x12345678; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 78 | MakeRtpHeader(kPayloadType, kSequenceNumber, kTimestamp, kSsrc, |
| 79 | packet_memory); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 80 | const double kPacketTime = 1.0; |
| 81 | // Hand over ownership of |packet_memory| to |packet|. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 82 | Packet packet(packet_memory, kPacketLengthBytes, kVirtualPacketLengthBytes, |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 83 | kPacketTime); |
| 84 | ASSERT_TRUE(packet.valid_header()); |
| 85 | EXPECT_EQ(kPayloadType, packet.header().payloadType); |
| 86 | EXPECT_EQ(kSequenceNumber, packet.header().sequenceNumber); |
| 87 | EXPECT_EQ(kTimestamp, packet.header().timestamp); |
| 88 | EXPECT_EQ(kSsrc, packet.header().ssrc); |
| 89 | EXPECT_EQ(0, packet.header().numCSRCs); |
| 90 | EXPECT_EQ(kPacketLengthBytes, packet.packet_length_bytes()); |
| 91 | EXPECT_EQ(kPacketLengthBytes - kHeaderLengthBytes, |
| 92 | packet.payload_length_bytes()); |
| 93 | EXPECT_EQ(kVirtualPacketLengthBytes, packet.virtual_packet_length_bytes()); |
| 94 | EXPECT_EQ(kVirtualPacketLengthBytes - kHeaderLengthBytes, |
| 95 | packet.virtual_payload_length_bytes()); |
| 96 | EXPECT_EQ(kPacketTime, packet.time_ms()); |
| 97 | } |
| 98 | |
| 99 | namespace { |
| 100 | // Writes one RED block header starting at |rtp_data|, according to RFC 2198. |
| 101 | // returns the number of bytes written (1 or 4). |
| 102 | // |
| 103 | // Format if |last_payoad| is false: |
| 104 | // 0 1 2 3 |
| 105 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 106 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 107 | // |1| block PT | timestamp offset | block length | |
| 108 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 109 | // |
| 110 | // Format if |last_payoad| is true: |
| 111 | // 0 1 2 3 4 5 6 7 |
| 112 | // +-+-+-+-+-+-+-+-+ |
| 113 | // |0| Block PT | |
| 114 | // +-+-+-+-+-+-+-+-+ |
| 115 | |
| 116 | int MakeRedHeader(int payload_type, |
| 117 | uint32_t timestamp_offset, |
| 118 | int block_length, |
| 119 | bool last_payload, |
| 120 | uint8_t* rtp_data) { |
| 121 | rtp_data[0] = 0x80 | (payload_type & 0x7F); // Set the first bit to 1. |
| 122 | if (last_payload) { |
| 123 | rtp_data[0] &= 0x7F; // Reset the first but to 0 to indicate last block. |
| 124 | return 1; |
| 125 | } |
| 126 | rtp_data[1] = timestamp_offset >> 6; |
| 127 | rtp_data[2] = (timestamp_offset & 0x3F) << 2; |
| 128 | rtp_data[2] |= block_length >> 8; |
| 129 | rtp_data[3] = block_length & 0xFF; |
| 130 | return 4; |
| 131 | } |
| 132 | } // namespace |
| 133 | |
| 134 | TEST(TestPacket, RED) { |
| 135 | const size_t kPacketLengthBytes = 100; |
| 136 | uint8_t* packet_memory = new uint8_t[kPacketLengthBytes]; |
| 137 | const uint8_t kRedPayloadType = 17; |
| 138 | const uint16_t kSequenceNumber = 4711; |
| 139 | const uint32_t kTimestamp = 47114711; |
| 140 | const uint32_t kSsrc = 0x12345678; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 141 | MakeRtpHeader(kRedPayloadType, kSequenceNumber, kTimestamp, kSsrc, |
| 142 | packet_memory); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 143 | // Create four RED headers. |
| 144 | // Payload types are just the same as the block index the offset is 100 times |
| 145 | // the block index. |
| 146 | const int kRedBlocks = 4; |
| 147 | uint8_t* payload_ptr = |
| 148 | &packet_memory[kHeaderLengthBytes]; // First byte after header. |
| 149 | for (int i = 0; i < kRedBlocks; ++i) { |
| 150 | int payload_type = i; |
| 151 | // Offset value is not used for the last block. |
| 152 | uint32_t timestamp_offset = 100 * i; |
| 153 | int block_length = 10 * i; |
| 154 | bool last_block = (i == kRedBlocks - 1) ? true : false; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 155 | payload_ptr += MakeRedHeader(payload_type, timestamp_offset, block_length, |
| 156 | last_block, payload_ptr); |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 157 | } |
| 158 | const double kPacketTime = 1.0; |
| 159 | // Hand over ownership of |packet_memory| to |packet|. |
| 160 | Packet packet(packet_memory, kPacketLengthBytes, kPacketTime); |
| 161 | ASSERT_TRUE(packet.valid_header()); |
| 162 | EXPECT_EQ(kRedPayloadType, packet.header().payloadType); |
| 163 | EXPECT_EQ(kSequenceNumber, packet.header().sequenceNumber); |
| 164 | EXPECT_EQ(kTimestamp, packet.header().timestamp); |
| 165 | EXPECT_EQ(kSsrc, packet.header().ssrc); |
| 166 | EXPECT_EQ(0, packet.header().numCSRCs); |
| 167 | EXPECT_EQ(kPacketLengthBytes, packet.packet_length_bytes()); |
| 168 | EXPECT_EQ(kPacketLengthBytes - kHeaderLengthBytes, |
| 169 | packet.payload_length_bytes()); |
| 170 | EXPECT_EQ(kPacketLengthBytes, packet.virtual_packet_length_bytes()); |
| 171 | EXPECT_EQ(kPacketLengthBytes - kHeaderLengthBytes, |
| 172 | packet.virtual_payload_length_bytes()); |
| 173 | EXPECT_EQ(kPacketTime, packet.time_ms()); |
| 174 | std::list<RTPHeader*> red_headers; |
| 175 | EXPECT_TRUE(packet.ExtractRedHeaders(&red_headers)); |
| 176 | EXPECT_EQ(kRedBlocks, static_cast<int>(red_headers.size())); |
| 177 | int block_index = 0; |
| 178 | for (std::list<RTPHeader*>::reverse_iterator it = red_headers.rbegin(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 179 | it != red_headers.rend(); ++it) { |
henrik.lundin@webrtc.org | 810acbc | 2014-04-14 18:42:23 +0000 | [diff] [blame] | 180 | // Reading list from the back, since the extraction puts the main payload |
| 181 | // (which is the last one on wire) first. |
| 182 | RTPHeader* red_block = *it; |
| 183 | EXPECT_EQ(block_index, red_block->payloadType); |
| 184 | EXPECT_EQ(kSequenceNumber, red_block->sequenceNumber); |
| 185 | if (block_index == kRedBlocks - 1) { |
| 186 | // Last block has zero offset per definition. |
| 187 | EXPECT_EQ(kTimestamp, red_block->timestamp); |
| 188 | } else { |
| 189 | EXPECT_EQ(kTimestamp - 100 * block_index, red_block->timestamp); |
| 190 | } |
| 191 | EXPECT_EQ(kSsrc, red_block->ssrc); |
| 192 | EXPECT_EQ(0, red_block->numCSRCs); |
| 193 | ++block_index; |
| 194 | } |
| 195 | Packet::DeleteRedHeaders(&red_headers); |
| 196 | } |
| 197 | |
| 198 | } // namespace test |
| 199 | } // namespace webrtc |