OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 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 |
| 13 #include <memory> |
| 14 #include <utility> |
| 15 |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "webrtc/base/basictypes.h" |
| 18 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/random.h" |
| 20 #include "webrtc/base/scoped_ref_ptr.h" |
| 21 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 22 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h" |
| 23 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction_internal.h" |
| 24 #include "webrtc/modules/rtp_rtcp/source/ulpfec_header_reader_writer.h" |
| 25 |
| 26 namespace webrtc { |
| 27 |
| 28 namespace { |
| 29 |
| 30 using Packet = ::webrtc::ForwardErrorCorrection::Packet; |
| 31 using ReceivedFecPacket = ::webrtc::ForwardErrorCorrection::ReceivedFecPacket; |
| 32 |
| 33 constexpr uint32_t kMediaSsrc = 1254983; |
| 34 constexpr uint16_t kMediaStartSeqNum = 825; |
| 35 constexpr size_t kMediaPacketLength = 1234; |
| 36 |
| 37 constexpr size_t kUlpfecHeaderSizeLBitClear = 14; |
| 38 constexpr size_t kUlpfecHeaderSizeLBitSet = 18; |
| 39 constexpr size_t kUlpfecPacketMaskOffset = 12; |
| 40 |
| 41 std::unique_ptr<uint8_t[]> GeneratePacketMask(size_t packet_mask_size, |
| 42 uint64_t seed) { |
| 43 Random random(seed); |
| 44 std::unique_ptr<uint8_t[]> packet_mask(new uint8_t[packet_mask_size]); |
| 45 for (size_t i = 0; i < packet_mask_size; ++i) { |
| 46 packet_mask[i] = random.Rand<uint8_t>(); |
| 47 } |
| 48 return packet_mask; |
| 49 } |
| 50 |
| 51 std::unique_ptr<Packet> WriteHeader(const uint8_t* packet_mask, |
| 52 size_t packet_mask_size) { |
| 53 UlpfecHeaderWriter writer; |
| 54 std::unique_ptr<Packet> written_packet(new Packet()); |
| 55 written_packet->length = kMediaPacketLength; |
| 56 for (size_t i = 0; i < written_packet->length; ++i) { |
| 57 written_packet->data[i] = i; // Actual content doesn't matter. |
| 58 } |
| 59 writer.FinalizeFecHeader(kMediaStartSeqNum, packet_mask, packet_mask_size, |
| 60 written_packet.get()); |
| 61 return written_packet; |
| 62 } |
| 63 |
| 64 std::unique_ptr<ReceivedFecPacket> ReadHeader(const Packet& written_packet) { |
| 65 UlpfecHeaderReader reader; |
| 66 std::unique_ptr<ReceivedFecPacket> read_packet(new ReceivedFecPacket()); |
| 67 read_packet->ssrc = kMediaSsrc; |
| 68 read_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); |
| 69 memcpy(read_packet->pkt->data, written_packet.data, written_packet.length); |
| 70 read_packet->pkt->length = written_packet.length; |
| 71 EXPECT_TRUE(reader.ReadFecHeader(read_packet.get())); |
| 72 return read_packet; |
| 73 } |
| 74 |
| 75 void VerifyHeaders(size_t expected_fec_header_size, |
| 76 const uint8_t* expected_packet_mask, |
| 77 size_t expected_packet_mask_size, |
| 78 const Packet& written_packet, |
| 79 const ReceivedFecPacket& read_packet) { |
| 80 EXPECT_EQ(kMediaSsrc, read_packet.ssrc); |
| 81 EXPECT_EQ(expected_fec_header_size, read_packet.fec_header_size); |
| 82 EXPECT_EQ(kMediaSsrc, read_packet.protected_ssrc); |
| 83 EXPECT_EQ(kMediaStartSeqNum, read_packet.seq_num_base); |
| 84 EXPECT_EQ(kUlpfecPacketMaskOffset, read_packet.packet_mask_offset); |
| 85 ASSERT_EQ(expected_packet_mask_size, read_packet.packet_mask_size); |
| 86 EXPECT_EQ(written_packet.length - expected_fec_header_size, |
| 87 read_packet.protection_length); |
| 88 EXPECT_EQ(0, memcmp(expected_packet_mask, |
| 89 &read_packet.pkt->data[read_packet.packet_mask_offset], |
| 90 read_packet.packet_mask_size)); |
| 91 // Verify that the call to ReadFecHeader did not tamper with the payload. |
| 92 EXPECT_EQ(0, memcmp(&written_packet.data[expected_fec_header_size], |
| 93 &read_packet.pkt->data[expected_fec_header_size], |
| 94 written_packet.length - expected_fec_header_size)); |
| 95 } |
| 96 |
| 97 } // namespace |
| 98 |
| 99 TEST(UlpfecHeaderReaderTest, ReadsSmallHeader) { |
| 100 const uint8_t packet[] = { |
| 101 0x00, 0x12, 0xab, 0xcd, // L bit clear, "random" payload type and SN base |
| 102 0x12, 0x34, 0x56, 0x78, // "random" TS recovery |
| 103 0xab, 0xcd, 0x11, 0x22, // "random" length recovery and protection length |
| 104 0x33, 0x44, // "random" packet mask |
| 105 0x00, 0x00, 0x00, 0x00 // payload |
| 106 }; |
| 107 const size_t packet_length = sizeof(packet); |
| 108 ReceivedFecPacket read_packet; |
| 109 read_packet.pkt = rtc::scoped_refptr<Packet>(new Packet()); |
| 110 memcpy(read_packet.pkt->data, packet, packet_length); |
| 111 read_packet.pkt->length = packet_length; |
| 112 |
| 113 UlpfecHeaderReader reader; |
| 114 EXPECT_TRUE(reader.ReadFecHeader(&read_packet)); |
| 115 |
| 116 EXPECT_EQ(14U, read_packet.fec_header_size); |
| 117 EXPECT_EQ(0xabcdU, read_packet.seq_num_base); |
| 118 EXPECT_EQ(12U, read_packet.packet_mask_offset); |
| 119 EXPECT_EQ(2U, read_packet.packet_mask_size); |
| 120 EXPECT_EQ(0x1122U, read_packet.protection_length); |
| 121 } |
| 122 |
| 123 TEST(UlpfecHeaderReaderTest, ReadsLargeHeader) { |
| 124 const uint8_t packet[] = { |
| 125 0x40, 0x12, 0xab, 0xcd, // L bit set, "random" payload type and SN base |
| 126 0x12, 0x34, 0x56, 0x78, // "random" TS recovery |
| 127 0xab, 0xcd, 0x11, 0x22, // "random" length recovery and protection length |
| 128 0x33, 0x44, 0x55, 0x66, // "random" packet mask |
| 129 0x77, 0x88, // |
| 130 0x00, 0x00, 0x00, 0x00 // payload |
| 131 }; |
| 132 const size_t packet_length = sizeof(packet); |
| 133 ReceivedFecPacket read_packet; |
| 134 read_packet.pkt = rtc::scoped_refptr<Packet>(new Packet()); |
| 135 memcpy(read_packet.pkt->data, packet, packet_length); |
| 136 read_packet.pkt->length = packet_length; |
| 137 |
| 138 UlpfecHeaderReader reader; |
| 139 EXPECT_TRUE(reader.ReadFecHeader(&read_packet)); |
| 140 |
| 141 EXPECT_EQ(18U, read_packet.fec_header_size); |
| 142 EXPECT_EQ(0xabcdU, read_packet.seq_num_base); |
| 143 EXPECT_EQ(12U, read_packet.packet_mask_offset); |
| 144 EXPECT_EQ(6U, read_packet.packet_mask_size); |
| 145 EXPECT_EQ(0x1122U, read_packet.protection_length); |
| 146 } |
| 147 |
| 148 TEST(UlpfecHeaderWriterTest, FinalizesSmallHeader) { |
| 149 const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitClear; |
| 150 auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd); |
| 151 Packet written_packet; |
| 152 written_packet.length = kMediaPacketLength; |
| 153 for (size_t i = 0; i < written_packet.length; ++i) { |
| 154 written_packet.data[i] = i; |
| 155 } |
| 156 |
| 157 UlpfecHeaderWriter writer; |
| 158 writer.FinalizeFecHeader(kMediaStartSeqNum, packet_mask.get(), |
| 159 packet_mask_size, &written_packet); |
| 160 |
| 161 const uint8_t* packet = written_packet.data; |
| 162 EXPECT_EQ(0x00, packet[0] & 0x80); // E bit. |
| 163 EXPECT_EQ(0x00, packet[0] & 0x40); // L bit. |
| 164 EXPECT_EQ(kMediaStartSeqNum, ByteReader<uint16_t>::ReadBigEndian(packet + 2)); |
| 165 EXPECT_EQ( |
| 166 static_cast<uint16_t>(kMediaPacketLength - kUlpfecHeaderSizeLBitClear), |
| 167 ByteReader<uint16_t>::ReadBigEndian(packet + 10)); |
| 168 EXPECT_EQ(0, memcmp(packet + kUlpfecPacketMaskOffset, packet_mask.get(), |
| 169 packet_mask_size)); |
| 170 } |
| 171 |
| 172 TEST(UlpfecHeaderWriterTest, FinalizesLargeHeader) { |
| 173 const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet; |
| 174 auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd); |
| 175 Packet written_packet; |
| 176 written_packet.length = kMediaPacketLength; |
| 177 for (size_t i = 0; i < written_packet.length; ++i) { |
| 178 written_packet.data[i] = i; |
| 179 } |
| 180 |
| 181 UlpfecHeaderWriter writer; |
| 182 writer.FinalizeFecHeader(kMediaStartSeqNum, packet_mask.get(), |
| 183 packet_mask_size, &written_packet); |
| 184 |
| 185 const uint8_t* packet = written_packet.data; |
| 186 EXPECT_EQ(0x00, packet[0] & 0x80); // E bit. |
| 187 EXPECT_EQ(0x40, packet[0] & 0x40); // L bit. |
| 188 EXPECT_EQ(kMediaStartSeqNum, ByteReader<uint16_t>::ReadBigEndian(packet + 2)); |
| 189 EXPECT_EQ( |
| 190 static_cast<uint16_t>(kMediaPacketLength - kUlpfecHeaderSizeLBitSet), |
| 191 ByteReader<uint16_t>::ReadBigEndian(packet + 10)); |
| 192 EXPECT_EQ(0, memcmp(packet + kUlpfecPacketMaskOffset, packet_mask.get(), |
| 193 packet_mask_size)); |
| 194 } |
| 195 |
| 196 TEST(UlpfecHeaderWriterTest, CalculateSmallHeaderSize) { |
| 197 const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitClear; |
| 198 auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd); |
| 199 |
| 200 UlpfecHeaderWriter writer; |
| 201 size_t min_packet_mask_size = |
| 202 writer.MinPacketMaskSize(packet_mask.get(), packet_mask_size); |
| 203 |
| 204 EXPECT_EQ(kUlpfecPacketMaskSizeLBitClear, min_packet_mask_size); |
| 205 EXPECT_EQ(kUlpfecHeaderSizeLBitClear, |
| 206 writer.FecHeaderSize(min_packet_mask_size)); |
| 207 } |
| 208 |
| 209 TEST(UlpfecHeaderWriterTest, CalculateLargeHeaderSize) { |
| 210 const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet; |
| 211 auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd); |
| 212 |
| 213 UlpfecHeaderWriter writer; |
| 214 size_t min_packet_mask_size = |
| 215 writer.MinPacketMaskSize(packet_mask.get(), packet_mask_size); |
| 216 |
| 217 EXPECT_EQ(kUlpfecPacketMaskSizeLBitSet, min_packet_mask_size); |
| 218 EXPECT_EQ(kUlpfecHeaderSizeLBitSet, |
| 219 writer.FecHeaderSize(min_packet_mask_size)); |
| 220 } |
| 221 |
| 222 TEST(UlpfecHeaderReaderWriterTest, WriteAndReadSmallHeader) { |
| 223 const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitClear; |
| 224 auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd); |
| 225 |
| 226 auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size); |
| 227 auto read_packet = ReadHeader(*written_packet); |
| 228 |
| 229 VerifyHeaders(kUlpfecHeaderSizeLBitClear, packet_mask.get(), packet_mask_size, |
| 230 *written_packet, *read_packet); |
| 231 } |
| 232 |
| 233 TEST(UlpfecHeaderReaderWriterTest, WriteAndReadLargeHeader) { |
| 234 const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet; |
| 235 auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd); |
| 236 |
| 237 auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size); |
| 238 auto read_packet = ReadHeader(*written_packet); |
| 239 |
| 240 VerifyHeaders(kUlpfecHeaderSizeLBitSet, packet_mask.get(), packet_mask_size, |
| 241 *written_packet, *read_packet); |
| 242 } |
| 243 |
| 244 } // namespace webrtc |
OLD | NEW |