Add FlexFEC header formatters.

Add classes that can read and finalize FlexFEC headers.

BUG=webrtc:5654

Review-Url: https://codereview.webrtc.org/2269903002
Cr-Commit-Position: refs/heads/master@{#14469}
This commit is contained in:
brandtr 2016-10-03 00:43:25 -07:00 committed by Commit bot
parent 8ff860a35d
commit 0496de298e
12 changed files with 1043 additions and 18 deletions

View File

@ -403,6 +403,7 @@ if (rtc_include_tests) {
"rtp_rtcp/source/fec_receiver_unittest.cc",
"rtp_rtcp/source/fec_test_helper.cc",
"rtp_rtcp/source/fec_test_helper.h",
"rtp_rtcp/source/flexfec_header_reader_writer_unittest.cc",
"rtp_rtcp/source/mock/mock_rtp_payload_strategy.h",
"rtp_rtcp/source/nack_rtx_unittest.cc",
"rtp_rtcp/source/packet_loss_stats_unittest.cc",

View File

@ -26,6 +26,8 @@ rtc_static_library("rtp_rtcp") {
"source/fec_private_tables_random.h",
"source/fec_receiver_impl.cc",
"source/fec_receiver_impl.h",
"source/flexfec_header_reader_writer.cc",
"source/flexfec_header_reader_writer.h",
"source/forward_error_correction.cc",
"source/forward_error_correction.h",
"source/forward_error_correction_internal.cc",

View File

@ -132,6 +132,8 @@
# Video Files
'source/fec_private_tables_random.h',
'source/fec_private_tables_bursty.h',
'source/flexfec_header_reader_writer.cc',
'source/flexfec_header_reader_writer.h',
'source/forward_error_correction.cc',
'source/forward_error_correction.h',
'source/forward_error_correction_internal.cc',

View File

@ -0,0 +1,311 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/modules/rtp_rtcp/source/flexfec_header_reader_writer.h"
#include <string.h>
#include <utility>
#include "webrtc/base/checks.h"
#include "webrtc/base/logging.h"
#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
#include "webrtc/modules/rtp_rtcp/source/forward_error_correction_internal.h"
namespace webrtc {
namespace {
// Maximum number of media packets that can be protected in one batch.
constexpr size_t kMaxMediaPackets = 48; // Since we are reusing ULPFEC masks.
// Maximum number of FEC packets stored inside ForwardErrorCorrection.
constexpr size_t kMaxFecPackets = kMaxMediaPackets;
// Size (in bytes) of packet masks, given number of K bits set.
constexpr size_t kFlexfecPacketMaskSizes[] = {2, 6, 14};
// Size (in bytes) of part of header which is not packet mask specific.
constexpr size_t kBaseHeaderSize = 12;
// Size (in bytes) of part of header which is stream specific.
constexpr size_t kStreamSpecificHeaderSize = 6;
// Size (in bytes) of header, given the single stream packet mask size, i.e.
// the number of K-bits set.
constexpr size_t kHeaderSizes[] = {
kBaseHeaderSize + kStreamSpecificHeaderSize + kFlexfecPacketMaskSizes[0],
kBaseHeaderSize + kStreamSpecificHeaderSize + kFlexfecPacketMaskSizes[1],
kBaseHeaderSize + kStreamSpecificHeaderSize + kFlexfecPacketMaskSizes[2]};
// We currently only support single-stream protection.
// TODO(brandtr): Update this when we support multistream protection.
constexpr uint8_t kSsrcCount = 1;
// There are three reserved bytes that MUST be set to zero in the header.
constexpr uint32_t kReservedBits = 0;
// TODO(brandtr): Update this when we support multistream protection.
constexpr size_t kPacketMaskOffset =
kBaseHeaderSize + kStreamSpecificHeaderSize;
// Here we count the K-bits as belonging to the packet mask.
// This can be used in conjunction with FlexfecHeaderWriter::MinPacketMaskSize,
// which calculates a bound on the needed packet mask size including K-bits,
// given a packet mask without K-bits.
size_t FlexfecHeaderSize(size_t packet_mask_size) {
RTC_DCHECK_LE(packet_mask_size, kFlexfecPacketMaskSizes[2]);
if (packet_mask_size <= kFlexfecPacketMaskSizes[0]) {
return kHeaderSizes[0];
} else if (packet_mask_size <= kFlexfecPacketMaskSizes[1]) {
return kHeaderSizes[1];
}
return kHeaderSizes[2];
}
} // namespace
FlexfecHeaderReader::FlexfecHeaderReader()
: FecHeaderReader(kMaxMediaPackets, kMaxFecPackets) {}
FlexfecHeaderReader::~FlexfecHeaderReader() = default;
// TODO(brandtr): Update this function when we support flexible masks,
// retransmissions, and/or several protected SSRCs.
bool FlexfecHeaderReader::ReadFecHeader(
ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const {
if (fec_packet->pkt->length <= kBaseHeaderSize + kStreamSpecificHeaderSize) {
LOG(LS_WARNING) << "Discarding truncated FlexFEC packet.";
return false;
}
bool f_bit = (fec_packet->pkt->data[0] & 0x80) != 0;
if (f_bit) {
LOG(LS_INFO) << "FlexFEC packet with inflexible generator matrix. We do "
"not yet support this, thus discarding packet.";
return false;
}
bool r_bit = (fec_packet->pkt->data[0] & 0x40) != 0;
if (r_bit) {
LOG(LS_INFO) << "FlexFEC packet with retransmission bit set. We do not yet "
"support this, thus discarding the packet.";
return false;
}
uint8_t ssrc_count =
ByteReader<uint8_t>::ReadBigEndian(&fec_packet->pkt->data[8]);
if (ssrc_count != 1) {
LOG(LS_INFO) << "FlexFEC packet protecting multiple media SSRCs. We do not "
"yet support this, thus discarding packet.";
return false;
}
uint32_t protected_ssrc =
ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[12]);
uint16_t seq_num_base =
ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[16]);
// Parse the FlexFEC packet mask and remove the interleaved K-bits.
// (See FEC header schematic in flexfec_header_reader_writer.h.)
// We store the packed packet mask in-band, which "destroys" the standards
// compliance of the header. That is fine though, since the code that
// reads from the header (from this point and onwards) is aware of this.
// TODO(brandtr): When the FEC packet classes have been refactored, store
// the packed packet masks out-of-band, thus leaving the FlexFEC header as is.
//
// We treat the mask parts as unsigned integers with host order endianness
// in order to simplify the bit shifting between bytes.
if (fec_packet->pkt->length < kHeaderSizes[0]) {
LOG(LS_WARNING) << "Discarding truncated FlexFEC packet.";
return false;
}
uint8_t* const packet_mask = fec_packet->pkt->data + kPacketMaskOffset;
bool k_bit0 = (packet_mask[0] & 0x80) != 0;
uint16_t mask_part0 = ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
// Shift away K-bit 0, implicitly clearing the last bit.
mask_part0 <<= 1;
ByteWriter<uint16_t>::WriteBigEndian(&packet_mask[0], mask_part0);
size_t packet_mask_size;
if (k_bit0) {
// The first K-bit is set, and the packet mask is thus only 2 bytes long.
// We have now read the entire FEC header, and the rest of the packet
// is payload.
packet_mask_size = kFlexfecPacketMaskSizes[0];
} else {
if (fec_packet->pkt->length < kHeaderSizes[1]) {
return false;
}
bool k_bit1 = (packet_mask[2] & 0x80) != 0;
// We have already shifted the first two bytes of the packet mask one step
// to the left, thus removing K-bit 0. We will now shift the next four bytes
// of the packet mask two steps to the left. (One step for the removed
// K-bit 0, and one step for the to be removed K-bit 1).
uint8_t bit15 = (packet_mask[2] >> 6) & 0x01;
packet_mask[1] |= bit15;
uint32_t mask_part1 = ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]);
// Shift away K-bit 1 and bit 15, implicitly clearing the last two bits.
mask_part1 <<= 2;
ByteWriter<uint32_t>::WriteBigEndian(&packet_mask[2], mask_part1);
if (k_bit1) {
// The first K-bit is clear, but the second K-bit is set. The packet
// mask is thus 6 bytes long. We have now read the entire FEC header,
// and the rest of the packet is payload.
packet_mask_size = kFlexfecPacketMaskSizes[1];
} else {
if (fec_packet->pkt->length < kHeaderSizes[2]) {
LOG(LS_WARNING) << "Discarding truncated FlexFEC packet.";
return false;
}
bool k_bit2 = (packet_mask[6] & 0x80) != 0;
if (k_bit2) {
// The first and second K-bits are clear, but the third K-bit is set.
// The packet mask is thus 14 bytes long. We have now read the entire
// FEC header, and the rest of the packet is payload.
packet_mask_size = kFlexfecPacketMaskSizes[2];
} else {
LOG(LS_WARNING) << "Discarding FlexFEC packet with malformed header.";
return false;
}
// At this point, K-bits 0 and 1 have been removed, and the front-most
// part of the FlexFEC packet mask has been packed accordingly. We will
// now shift the remaning part of the packet mask three steps to the left.
// This corresponds to the (in total) three K-bits, which have been
// removed.
uint8_t tail_bits = (packet_mask[6] >> 5) & 0x03;
packet_mask[5] |= tail_bits;
uint64_t mask_part2 =
ByteReader<uint64_t>::ReadBigEndian(&packet_mask[6]);
// Shift away K-bit 2, bit 46, and bit 47, implicitly clearing the last
// three bits.
mask_part2 <<= 3;
ByteWriter<uint64_t>::WriteBigEndian(&packet_mask[6], mask_part2);
}
}
// Store "ULPFECized" packet mask info.
fec_packet->fec_header_size = FlexfecHeaderSize(packet_mask_size);
fec_packet->protected_ssrc = protected_ssrc;
fec_packet->seq_num_base = seq_num_base;
fec_packet->packet_mask_offset = kPacketMaskOffset;
fec_packet->packet_mask_size = packet_mask_size;
// In FlexFEC, all media packets are protected in their entirety.
fec_packet->protection_length =
fec_packet->pkt->length - fec_packet->fec_header_size;
return true;
}
FlexfecHeaderWriter::FlexfecHeaderWriter()
: FecHeaderWriter(kMaxMediaPackets, kMaxFecPackets, kHeaderSizes[2]) {}
FlexfecHeaderWriter::~FlexfecHeaderWriter() = default;
size_t FlexfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask,
size_t packet_mask_size) const {
if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear &&
(packet_mask[1] & 0x01) == 0) {
// Packet mask is 16 bits long, with bit 15 clear.
// It can be used as is.
return kFlexfecPacketMaskSizes[0];
} else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
// Packet mask is 16 bits long, with bit 15 set.
// We must expand the packet mask with zeros in the FlexFEC header.
return kFlexfecPacketMaskSizes[1];
} else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet &&
(packet_mask[5] & 0x03) == 0) {
// Packet mask is 48 bits long, with bits 46 and 47 clear.
// It can be used as is.
return kFlexfecPacketMaskSizes[1];
} else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) {
// Packet mask is 48 bits long, with at least one of bits 46 and 47 set.
// We must expand it with zeros.
return kFlexfecPacketMaskSizes[2];
}
RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size << ".";
return kFlexfecPacketMaskSizes[2];
}
size_t FlexfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const {
return FlexfecHeaderSize(packet_mask_size);
}
// This function adapts the precomputed ULPFEC packet masks to the
// FlexFEC header standard. Note that the header size is computed by
// FecHeaderSize(), so in this function we can be sure that we are
// writing in space that is intended for the header.
//
// TODO(brandtr): Update this function when we support offset-based masks,
// retransmissions, and protecting multiple SSRCs.
void FlexfecHeaderWriter::FinalizeFecHeader(
uint32_t media_ssrc,
uint16_t seq_num_base,
const uint8_t* packet_mask,
size_t packet_mask_size,
ForwardErrorCorrection::Packet* fec_packet) const {
fec_packet->data[0] &= 0x7f; // Clear F bit.
fec_packet->data[0] &= 0xbf; // Clear R bit.
ByteWriter<uint8_t>::WriteBigEndian(&fec_packet->data[8], kSsrcCount);
ByteWriter<uint32_t, 3>::WriteBigEndian(&fec_packet->data[9], kReservedBits);
ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[12], media_ssrc);
ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[16], seq_num_base);
// Adapt ULPFEC packet mask to FlexFEC header.
//
// We treat the mask parts as unsigned integers with host order endianness
// in order to simplify the bit shifting between bytes.
uint8_t* const written_packet_mask = fec_packet->data + kPacketMaskOffset;
if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) {
// The packet mask is 48 bits long.
uint16_t tmp_mask_part0 =
ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
uint32_t tmp_mask_part1 =
ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]);
tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0.
ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0],
tmp_mask_part0);
tmp_mask_part1 >>= 2; // Shift, thus clearing K-bit 1 and bit 15.
ByteWriter<uint32_t>::WriteBigEndian(&written_packet_mask[2],
tmp_mask_part1);
bool bit15 = (packet_mask[1] & 0x01) != 0;
if (bit15)
written_packet_mask[2] |= 0x40; // Set bit 15.
bool bit46 = (packet_mask[5] & 0x02) != 0;
bool bit47 = (packet_mask[5] & 0x01) != 0;
if (!bit46 && !bit47) {
written_packet_mask[2] |= 0x80; // Set K-bit 1.
} else {
memset(&written_packet_mask[6], 0, 8); // Clear all trailing bits.
written_packet_mask[6] |= 0x80; // Set K-bit 2.
if (bit46)
written_packet_mask[6] |= 0x40; // Set bit 46.
if (bit47)
written_packet_mask[6] |= 0x20; // Set bit 47.
}
} else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
// The packet mask is 16 bits long.
uint16_t tmp_mask_part0 =
ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0.
ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0],
tmp_mask_part0);
bool bit15 = (packet_mask[1] & 0x01) != 0;
if (!bit15) {
written_packet_mask[0] |= 0x80; // Set K-bit 0.
} else {
memset(&written_packet_mask[2], 0U, 4); // Clear all trailing bits.
written_packet_mask[2] |= 0x80; // Set K-bit 1.
written_packet_mask[2] |= 0x40; // Set bit 15.
}
} else {
RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size
<< ".";
}
}
} // namespace webrtc

View File

@ -0,0 +1,86 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_FLEXFEC_HEADER_READER_WRITER_H_
#define WEBRTC_MODULES_RTP_RTCP_SOURCE_FLEXFEC_HEADER_READER_WRITER_H_
#include "webrtc/base/basictypes.h"
#include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h"
namespace webrtc {
// FlexFEC header, minimum 20 bytes.
// 0 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 0 |F|R|P|X| CC |M| PT recovery | length recovery |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 4 | TS recovery |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 8 | SSRCCount | reserved |
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
// 12 | SSRC_i |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 16 | SN base_i |k| Mask [0-14] |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 20 |k| Mask [15-45] (optional) |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 24 |k| |
// +-+ Mask [46-108] (optional) |
// 28 | |
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
// : ... next in SSRC_i ... :
//
//
// FlexFEC header in 'inflexible' mode (F = 1), 20 bytes.
// 0 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 0 |1|0|P|X| CC |M| PT recovery | length recovery |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 4 | TS recovery |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 8 | SSRCCount | reserved |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 12 | SSRC_i |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 16 | SN base_i | M (columns) | N (rows) |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
class FlexfecHeaderReader : public FecHeaderReader {
public:
FlexfecHeaderReader();
~FlexfecHeaderReader() override;
bool ReadFecHeader(
ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const override;
};
class FlexfecHeaderWriter : public FecHeaderWriter {
public:
FlexfecHeaderWriter();
~FlexfecHeaderWriter() override;
size_t MinPacketMaskSize(const uint8_t* packet_mask,
size_t packet_mask_size) const override;
size_t FecHeaderSize(size_t packet_mask_row_size) const override;
void FinalizeFecHeader(
uint32_t media_ssrc,
uint16_t seq_num_base,
const uint8_t* packet_mask,
size_t packet_mask_size,
ForwardErrorCorrection::Packet* fec_packet) const override;
};
} // namespace webrtc
#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_FLEXFEC_HEADER_READER_WRITER_H_

View File

@ -0,0 +1,559 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <string.h>
#include <memory>
#include <utility>
#include "webrtc/base/basictypes.h"
#include "webrtc/base/checks.h"
#include "webrtc/base/random.h"
#include "webrtc/base/scoped_ref_ptr.h"
#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
#include "webrtc/modules/rtp_rtcp/source/flexfec_header_reader_writer.h"
#include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h"
#include "webrtc/modules/rtp_rtcp/source/forward_error_correction_internal.h"
#include "webrtc/test/gmock.h"
#include "webrtc/test/gtest.h"
namespace webrtc {
namespace {
using Packet = ::webrtc::ForwardErrorCorrection::Packet;
using ReceivedFecPacket = ::webrtc::ForwardErrorCorrection::ReceivedFecPacket;
// General. Assume single-stream protection.
constexpr uint32_t kMediaSsrc = 1254983;
constexpr uint16_t kMediaStartSeqNum = 825;
constexpr size_t kMediaPacketLength = 1234;
constexpr uint32_t kFlexfecSsrc = 52142;
constexpr size_t kFlexfecHeaderSizes[] = {20, 24, 32};
constexpr size_t kFlexfecPacketMaskOffset = 18;
constexpr size_t kFlexfecPacketMaskSizes[] = {2, 6, 14};
constexpr size_t kFlexfecMaxPacketSize = kFlexfecPacketMaskSizes[2];
// Reader tests.
constexpr uint8_t kNoFBit = 0 << 7;
constexpr uint8_t kNoRBit = 0 << 6;
constexpr uint8_t kPtRecovery = 123;
constexpr uint8_t kLengthRecov[] = {0xab, 0xcd};
constexpr uint8_t kTsRecovery[] = {0x01, 0x23, 0x45, 0x67};
constexpr uint8_t kSsrcCount = 1;
constexpr uint8_t kReservedBits = 0x00;
constexpr uint8_t kProtSsrc[] = {0x11, 0x22, 0x33, 0x44};
constexpr uint8_t kSnBase[] = {0xaa, 0xbb};
constexpr uint8_t kPayloadBits = 0x00;
std::unique_ptr<uint8_t[]> GeneratePacketMask(size_t packet_mask_size,
uint64_t seed) {
Random random(seed);
std::unique_ptr<uint8_t[]> packet_mask(new uint8_t[kFlexfecMaxPacketSize]);
memset(packet_mask.get(), 0, kFlexfecMaxPacketSize);
for (size_t i = 0; i < packet_mask_size; ++i) {
packet_mask[i] = random.Rand<uint8_t>();
}
return packet_mask;
}
void ClearBit(size_t index, uint8_t* packet_mask) {
packet_mask[index / 8] &= ~(1 << (7 - index % 8));
}
void SetBit(size_t index, uint8_t* packet_mask) {
packet_mask[index / 8] |= (1 << (7 - index % 8));
}
rtc::scoped_refptr<Packet> WriteHeader(const uint8_t* packet_mask,
size_t packet_mask_size) {
FlexfecHeaderWriter writer;
rtc::scoped_refptr<Packet> written_packet(new Packet());
written_packet->length = kMediaPacketLength;
for (size_t i = 0; i < written_packet->length; ++i) {
written_packet->data[i] = i; // Actual content doesn't matter.
}
writer.FinalizeFecHeader(kMediaSsrc, kMediaStartSeqNum, packet_mask,
packet_mask_size, written_packet.get());
return written_packet;
}
std::unique_ptr<ReceivedFecPacket> ReadHeader(const Packet& written_packet) {
FlexfecHeaderReader reader;
std::unique_ptr<ReceivedFecPacket> read_packet(new ReceivedFecPacket());
read_packet->ssrc = kFlexfecSsrc;
read_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
memcpy(read_packet->pkt->data, written_packet.data, written_packet.length);
read_packet->pkt->length = written_packet.length;
EXPECT_TRUE(reader.ReadFecHeader(read_packet.get()));
return read_packet;
}
void VerifyReadHeaders(size_t expected_fec_header_size,
const uint8_t* expected_packet_mask,
size_t expected_packet_mask_size,
const ReceivedFecPacket& read_packet) {
EXPECT_EQ(expected_fec_header_size, read_packet.fec_header_size);
EXPECT_EQ(ByteReader<uint32_t>::ReadBigEndian(kProtSsrc),
read_packet.protected_ssrc);
EXPECT_EQ(ByteReader<uint16_t>::ReadBigEndian(kSnBase),
read_packet.seq_num_base);
const size_t packet_mask_offset = read_packet.packet_mask_offset;
EXPECT_EQ(kFlexfecPacketMaskOffset, packet_mask_offset);
EXPECT_EQ(expected_packet_mask_size, read_packet.packet_mask_size);
EXPECT_EQ(read_packet.pkt->length - expected_fec_header_size,
read_packet.protection_length);
// Ensure that the K-bits are removed and the packet mask has been packed.
EXPECT_THAT(::testing::make_tuple(read_packet.pkt->data + packet_mask_offset,
read_packet.packet_mask_size),
::testing::ElementsAreArray(expected_packet_mask,
expected_packet_mask_size));
}
void VerifyFinalizedHeaders(const uint8_t* expected_packet_mask,
size_t expected_packet_mask_size,
const Packet& written_packet) {
const uint8_t* packet = written_packet.data;
EXPECT_EQ(0x00, packet[0] & 0x80); // F bit clear.
EXPECT_EQ(0x00, packet[0] & 0x40); // R bit clear.
EXPECT_EQ(0x01, packet[8]); // SSRCCount = 1.
EXPECT_EQ(kMediaSsrc, ByteReader<uint32_t>::ReadBigEndian(packet + 12));
EXPECT_EQ(kMediaStartSeqNum,
ByteReader<uint16_t>::ReadBigEndian(packet + 16));
EXPECT_THAT(::testing::make_tuple(packet + kFlexfecPacketMaskOffset,
expected_packet_mask_size),
::testing::ElementsAreArray(expected_packet_mask,
expected_packet_mask_size));
}
void VerifyWrittenAndReadHeaders(size_t expected_fec_header_size,
const uint8_t* expected_packet_mask,
size_t expected_packet_mask_size,
const Packet& written_packet,
const ReceivedFecPacket& read_packet) {
EXPECT_EQ(kFlexfecSsrc, read_packet.ssrc);
EXPECT_EQ(expected_fec_header_size, read_packet.fec_header_size);
EXPECT_EQ(kMediaSsrc, read_packet.protected_ssrc);
EXPECT_EQ(kMediaStartSeqNum, read_packet.seq_num_base);
EXPECT_EQ(kFlexfecPacketMaskOffset, read_packet.packet_mask_offset);
ASSERT_EQ(expected_packet_mask_size, read_packet.packet_mask_size);
EXPECT_EQ(written_packet.length - expected_fec_header_size,
read_packet.protection_length);
// Verify that the call to ReadFecHeader did normalize the packet masks.
EXPECT_THAT(
::testing::make_tuple(read_packet.pkt->data + kFlexfecPacketMaskOffset,
read_packet.packet_mask_size),
::testing::ElementsAreArray(expected_packet_mask,
expected_packet_mask_size));
// Verify that the call to ReadFecHeader did not tamper with the payload.
EXPECT_THAT(::testing::make_tuple(
read_packet.pkt->data + read_packet.fec_header_size,
read_packet.pkt->length - read_packet.fec_header_size),
::testing::ElementsAreArray(
written_packet.data + expected_fec_header_size,
written_packet.length - expected_fec_header_size));
}
} // namespace
TEST(FlexfecHeaderReaderTest, ReadsHeaderWithKBit0Set) {
constexpr uint8_t kKBit0 = 1 << 7;
constexpr size_t kExpectedPacketMaskSize = 2;
constexpr size_t kExpectedFecHeaderSize = 20;
// clang-format off
constexpr uint8_t kFlexfecPktMask[] = {kKBit0 | 0x08, 0x81};
constexpr uint8_t kUlpfecPacketMask[] = {0x11, 0x02};
// clang-format on
constexpr uint8_t kPacketData[] = {
kNoFBit | kNoRBit, kPtRecovery, kLengthRecov[0], kLengthRecov[1],
kTsRecovery[0], kTsRecovery[1], kTsRecovery[2], kTsRecovery[3],
kSsrcCount, kReservedBits, kReservedBits, kReservedBits,
kProtSsrc[0], kProtSsrc[1], kProtSsrc[2], kProtSsrc[3],
kSnBase[0], kSnBase[1], kFlexfecPktMask[0], kFlexfecPktMask[1],
kPayloadBits, kPayloadBits, kPayloadBits, kPayloadBits};
const size_t packet_length = sizeof(kPacketData);
ReceivedFecPacket read_packet;
read_packet.pkt = rtc::scoped_refptr<Packet>(new Packet());
memcpy(read_packet.pkt->data, kPacketData, packet_length);
read_packet.pkt->length = packet_length;
FlexfecHeaderReader reader;
EXPECT_TRUE(reader.ReadFecHeader(&read_packet));
VerifyReadHeaders(kExpectedFecHeaderSize, kUlpfecPacketMask,
kExpectedPacketMaskSize, read_packet);
}
TEST(FlexfecHeaderReaderTest, ReadsHeaderWithKBit1Set) {
constexpr uint8_t kKBit0 = 0 << 7;
constexpr uint8_t kKBit1 = 1 << 7;
constexpr size_t kExpectedPacketMaskSize = 6;
constexpr size_t kExpectedFecHeaderSize = 24;
// clang-format off
constexpr uint8_t kFlxfecPktMsk[] = {kKBit0 | 0x48, 0x81,
kKBit1 | 0x02, 0x11, 0x00, 0x21};
constexpr uint8_t kUlpfecPacketMask[] = {0x91, 0x02,
0x08, 0x44, 0x00, 0x84};
// clang-format on
constexpr uint8_t kPacketData[] = {
kNoFBit | kNoRBit, kPtRecovery, kLengthRecov[0], kLengthRecov[1],
kTsRecovery[0], kTsRecovery[1], kTsRecovery[2], kTsRecovery[3],
kSsrcCount, kReservedBits, kReservedBits, kReservedBits,
kProtSsrc[0], kProtSsrc[1], kProtSsrc[2], kProtSsrc[3],
kSnBase[0], kSnBase[1], kFlxfecPktMsk[0], kFlxfecPktMsk[1],
kFlxfecPktMsk[2], kFlxfecPktMsk[3], kFlxfecPktMsk[4], kFlxfecPktMsk[5],
kPayloadBits, kPayloadBits, kPayloadBits, kPayloadBits};
const size_t packet_length = sizeof(kPacketData);
ReceivedFecPacket read_packet;
read_packet.pkt = rtc::scoped_refptr<Packet>(new Packet());
memcpy(read_packet.pkt->data, kPacketData, packet_length);
read_packet.pkt->length = packet_length;
FlexfecHeaderReader reader;
EXPECT_TRUE(reader.ReadFecHeader(&read_packet));
VerifyReadHeaders(kExpectedFecHeaderSize, kUlpfecPacketMask,
kExpectedPacketMaskSize, read_packet);
}
TEST(FlexfecHeaderReaderTest, ReadsHeaderWithKBit2Set) {
constexpr uint8_t kKBit0 = 0 << 7;
constexpr uint8_t kKBit1 = 0 << 7;
constexpr uint8_t kKBit2 = 1 << 7;
constexpr size_t kExpectedPacketMaskSize = 14;
constexpr size_t kExpectedFecHeaderSize = 32;
// clang-format off
constexpr uint8_t kFlxfcPktMsk[] = {kKBit0 | 0x48, 0x81,
kKBit1 | 0x02, 0x11, 0x00, 0x21,
kKBit2 | 0x01, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11};
constexpr uint8_t kUlpfecPacketMask[] = {0x91, 0x02,
0x08, 0x44, 0x00, 0x84,
0x08, 0x88, 0x88, 0x88,
0x88, 0x88, 0x88, 0x88};
// clang-format on
constexpr uint8_t kPacketData[] = {
kNoFBit | kNoRBit, kPtRecovery, kLengthRecov[0], kLengthRecov[1],
kTsRecovery[0], kTsRecovery[1], kTsRecovery[2], kTsRecovery[3],
kSsrcCount, kReservedBits, kReservedBits, kReservedBits,
kProtSsrc[0], kProtSsrc[1], kProtSsrc[2], kProtSsrc[3],
kSnBase[0], kSnBase[1], kFlxfcPktMsk[0], kFlxfcPktMsk[1],
kFlxfcPktMsk[2], kFlxfcPktMsk[3], kFlxfcPktMsk[4], kFlxfcPktMsk[5],
kFlxfcPktMsk[6], kFlxfcPktMsk[7], kFlxfcPktMsk[8], kFlxfcPktMsk[9],
kFlxfcPktMsk[10], kFlxfcPktMsk[11], kFlxfcPktMsk[12], kFlxfcPktMsk[13],
kPayloadBits, kPayloadBits, kPayloadBits, kPayloadBits};
const size_t packet_length = sizeof(kPacketData);
ReceivedFecPacket read_packet;
read_packet.pkt = rtc::scoped_refptr<Packet>(new Packet());
memcpy(read_packet.pkt->data, kPacketData, packet_length);
read_packet.pkt->length = packet_length;
FlexfecHeaderReader reader;
EXPECT_TRUE(reader.ReadFecHeader(&read_packet));
VerifyReadHeaders(kExpectedFecHeaderSize, kUlpfecPacketMask,
kExpectedPacketMaskSize, read_packet);
}
TEST(FlexfecHeaderReaderTest, ReadPacketWithoutStreamSpecificHeaderShouldFail) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitClear;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size);
// Simulate short received packet.
ReceivedFecPacket read_packet;
read_packet.ssrc = kFlexfecSsrc;
read_packet.pkt = std::move(written_packet);
read_packet.pkt->length = 12;
FlexfecHeaderReader reader;
EXPECT_FALSE(reader.ReadFecHeader(&read_packet));
}
TEST(FlexfecHeaderReaderTest, ReadShortPacketWithKBit0SetShouldFail) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitClear;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size);
// Simulate short received packet.
ReceivedFecPacket read_packet;
read_packet.ssrc = kFlexfecSsrc;
read_packet.pkt = std::move(written_packet);
read_packet.pkt->length = 18;
FlexfecHeaderReader reader;
EXPECT_FALSE(reader.ReadFecHeader(&read_packet));
}
TEST(FlexfecHeaderReaderTest, ReadShortPacketWithKBit1SetShouldFail) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitClear;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
SetBit(15, packet_mask.get()); // This expands the packet mask "once".
auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size);
// Simulate short received packet.
ReceivedFecPacket read_packet;
read_packet.ssrc = kFlexfecSsrc;
read_packet.pkt = std::move(written_packet);
read_packet.pkt->length = 20;
FlexfecHeaderReader reader;
EXPECT_FALSE(reader.ReadFecHeader(&read_packet));
}
TEST(FlexfecHeaderReaderTest, ReadShortPacketWithKBit2SetShouldFail) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
SetBit(47, packet_mask.get()); // This expands the packet mask "twice".
auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size);
// Simulate short received packet.
ReceivedFecPacket read_packet;
read_packet.ssrc = kFlexfecSsrc;
read_packet.pkt = std::move(written_packet);
read_packet.pkt->length = 24;
FlexfecHeaderReader reader;
EXPECT_FALSE(reader.ReadFecHeader(&read_packet));
}
TEST(FlexfecHeaderWriterTest, FinalizesHeaderWithKBit0Set) {
constexpr size_t kExpectedPacketMaskSize = 2;
constexpr uint8_t kFlexfecPacketMask[] = {0x88, 0x81};
constexpr uint8_t kUlpfecPacketMask[] = {0x11, 0x02};
Packet written_packet;
written_packet.length = kMediaPacketLength;
for (size_t i = 0; i < written_packet.length; ++i) {
written_packet.data[i] = i;
}
FlexfecHeaderWriter writer;
writer.FinalizeFecHeader(kMediaSsrc, kMediaStartSeqNum, kUlpfecPacketMask,
sizeof(kUlpfecPacketMask), &written_packet);
VerifyFinalizedHeaders(kFlexfecPacketMask, kExpectedPacketMaskSize,
written_packet);
}
TEST(FlexfecHeaderWriterTest, FinalizesHeaderWithKBit1Set) {
constexpr size_t kExpectedPacketMaskSize = 6;
constexpr uint8_t kFlexfecPacketMask[] = {0x48, 0x81, 0x82, 0x11, 0x00, 0x21};
constexpr uint8_t kUlpfecPacketMask[] = {0x91, 0x02, 0x08, 0x44, 0x00, 0x84};
Packet written_packet;
written_packet.length = kMediaPacketLength;
for (size_t i = 0; i < written_packet.length; ++i) {
written_packet.data[i] = i;
}
FlexfecHeaderWriter writer;
writer.FinalizeFecHeader(kMediaSsrc, kMediaStartSeqNum, kUlpfecPacketMask,
sizeof(kUlpfecPacketMask), &written_packet);
VerifyFinalizedHeaders(kFlexfecPacketMask, kExpectedPacketMaskSize,
written_packet);
}
TEST(FlexfecHeaderWriterTest, FinalizesHeaderWithKBit2Set) {
constexpr size_t kExpectedPacketMaskSize = 14;
constexpr uint8_t kFlexfecPacketMask[] = {
0x11, 0x11, // K-bit 0 clear.
0x11, 0x11, 0x11, 0x10, // K-bit 1 clear.
0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // K-bit 2 set.
};
constexpr uint8_t kUlpfecPacketMask[] = {0x22, 0x22, 0x44, 0x44, 0x44, 0x41};
Packet written_packet;
written_packet.length = kMediaPacketLength;
for (size_t i = 0; i < written_packet.length; ++i) {
written_packet.data[i] = i;
}
FlexfecHeaderWriter writer;
writer.FinalizeFecHeader(kMediaSsrc, kMediaStartSeqNum, kUlpfecPacketMask,
sizeof(kUlpfecPacketMask), &written_packet);
VerifyFinalizedHeaders(kFlexfecPacketMask, kExpectedPacketMaskSize,
written_packet);
}
TEST(FlexfecHeaderWriterTest, ContractsShortUlpfecPacketMaskWithBit15Clear) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitClear;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
ClearBit(15, packet_mask.get());
FlexfecHeaderWriter writer;
size_t min_packet_mask_size =
writer.MinPacketMaskSize(packet_mask.get(), packet_mask_size);
EXPECT_EQ(kFlexfecPacketMaskSizes[0], min_packet_mask_size);
EXPECT_EQ(kFlexfecHeaderSizes[0], writer.FecHeaderSize(min_packet_mask_size));
}
TEST(FlexfecHeaderWriterTest, ExpandsShortUlpfecPacketMaskWithBit15Set) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitClear;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
SetBit(15, packet_mask.get());
FlexfecHeaderWriter writer;
size_t min_packet_mask_size =
writer.MinPacketMaskSize(packet_mask.get(), packet_mask_size);
EXPECT_EQ(kFlexfecPacketMaskSizes[1], min_packet_mask_size);
EXPECT_EQ(kFlexfecHeaderSizes[1], writer.FecHeaderSize(min_packet_mask_size));
}
TEST(FlexfecHeaderWriterTest,
ContractsLongUlpfecPacketMaskWithBit46ClearBit47Clear) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
ClearBit(46, packet_mask.get());
ClearBit(47, packet_mask.get());
FlexfecHeaderWriter writer;
size_t min_packet_mask_size =
writer.MinPacketMaskSize(packet_mask.get(), packet_mask_size);
EXPECT_EQ(kFlexfecPacketMaskSizes[1], min_packet_mask_size);
EXPECT_EQ(kFlexfecHeaderSizes[1], writer.FecHeaderSize(min_packet_mask_size));
}
TEST(FlexfecHeaderWriterTest,
ExpandsLongUlpfecPacketMaskWithBit46SetBit47Clear) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
SetBit(46, packet_mask.get());
ClearBit(47, packet_mask.get());
FlexfecHeaderWriter writer;
size_t min_packet_mask_size =
writer.MinPacketMaskSize(packet_mask.get(), packet_mask_size);
EXPECT_EQ(kFlexfecPacketMaskSizes[2], min_packet_mask_size);
EXPECT_EQ(kFlexfecHeaderSizes[2], writer.FecHeaderSize(min_packet_mask_size));
}
TEST(FlexfecHeaderWriterTest,
ExpandsLongUlpfecPacketMaskWithBit46ClearBit47Set) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
ClearBit(46, packet_mask.get());
SetBit(47, packet_mask.get());
FlexfecHeaderWriter writer;
size_t min_packet_mask_size =
writer.MinPacketMaskSize(packet_mask.get(), packet_mask_size);
EXPECT_EQ(kFlexfecPacketMaskSizes[2], min_packet_mask_size);
EXPECT_EQ(kFlexfecHeaderSizes[2], writer.FecHeaderSize(min_packet_mask_size));
}
TEST(FlexfecHeaderWriterTest, ExpandsLongUlpfecPacketMaskWithBit46SetBit47Set) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
SetBit(46, packet_mask.get());
SetBit(47, packet_mask.get());
FlexfecHeaderWriter writer;
size_t min_packet_mask_size =
writer.MinPacketMaskSize(packet_mask.get(), packet_mask_size);
EXPECT_EQ(kFlexfecPacketMaskSizes[2], min_packet_mask_size);
EXPECT_EQ(kFlexfecHeaderSizes[2], writer.FecHeaderSize(min_packet_mask_size));
}
TEST(FlexfecHeaderReaderWriterTest,
WriteAndReadSmallUlpfecPacketHeaderWithMaskBit15Clear) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitClear;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
ClearBit(15, packet_mask.get());
auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size);
auto read_packet = ReadHeader(*written_packet);
VerifyWrittenAndReadHeaders(kFlexfecHeaderSizes[0], packet_mask.get(),
kFlexfecPacketMaskSizes[0], *written_packet,
*read_packet);
}
TEST(FlexfecHeaderReaderWriterTest,
WriteAndReadSmallUlpfecPacketHeaderWithMaskBit15Set) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitClear;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
SetBit(15, packet_mask.get());
auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size);
auto read_packet = ReadHeader(*written_packet);
VerifyWrittenAndReadHeaders(kFlexfecHeaderSizes[1], packet_mask.get(),
kFlexfecPacketMaskSizes[1], *written_packet,
*read_packet);
}
TEST(FlexfecHeaderReaderWriterTest,
WriteAndReadLargeUlpfecPacketHeaderWithMaskBits46And47Clear) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
ClearBit(46, packet_mask.get());
ClearBit(47, packet_mask.get());
auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size);
auto read_packet = ReadHeader(*written_packet);
VerifyWrittenAndReadHeaders(kFlexfecHeaderSizes[1], packet_mask.get(),
kFlexfecPacketMaskSizes[1], *written_packet,
*read_packet);
}
TEST(FlexfecHeaderReaderWriterTest,
WriteAndReadLargeUlpfecPacketHeaderWithMaskBit46SetBit47Clear) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
SetBit(46, packet_mask.get());
ClearBit(47, packet_mask.get());
auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size);
auto read_packet = ReadHeader(*written_packet);
VerifyWrittenAndReadHeaders(kFlexfecHeaderSizes[2], packet_mask.get(),
kFlexfecPacketMaskSizes[2], *written_packet,
*read_packet);
}
TEST(FlexfecHeaderReaderWriterTest,
WriteAndReadLargeUlpfecPacketHeaderMaskWithBit46ClearBit47Set) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
ClearBit(46, packet_mask.get());
SetBit(47, packet_mask.get());
auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size);
auto read_packet = ReadHeader(*written_packet);
VerifyWrittenAndReadHeaders(kFlexfecHeaderSizes[2], packet_mask.get(),
kFlexfecPacketMaskSizes[2], *written_packet,
*read_packet);
}
TEST(FlexfecHeaderReaderWriterTest,
WriteAndReadLargeUlpfecPacketHeaderWithMaskBits46And47Set) {
const size_t packet_mask_size = kUlpfecPacketMaskSizeLBitSet;
auto packet_mask = GeneratePacketMask(packet_mask_size, 0xabcd);
SetBit(46, packet_mask.get());
SetBit(47, packet_mask.get());
auto written_packet = WriteHeader(packet_mask.get(), packet_mask_size);
auto read_packet = ReadHeader(*written_packet);
VerifyWrittenAndReadHeaders(kFlexfecHeaderSizes[2], packet_mask.get(),
kFlexfecPacketMaskSizes[2], *written_packet,
*read_packet);
}
} // namespace webrtc

View File

@ -20,6 +20,8 @@
#include "webrtc/base/logging.h"
#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
#include "webrtc/modules/rtp_rtcp/source/flexfec_header_reader_writer.h"
#include "webrtc/modules/rtp_rtcp/source/forward_error_correction_internal.h"
#include "webrtc/modules/rtp_rtcp/source/ulpfec_header_reader_writer.h"
namespace webrtc {
@ -84,6 +86,14 @@ std::unique_ptr<ForwardErrorCorrection> ForwardErrorCorrection::CreateUlpfec() {
std::move(fec_header_reader), std::move(fec_header_writer)));
}
std::unique_ptr<ForwardErrorCorrection>
ForwardErrorCorrection::CreateFlexfec() {
std::unique_ptr<FecHeaderReader> fec_header_reader(new FlexfecHeaderReader());
std::unique_ptr<FecHeaderWriter> fec_header_writer(new FlexfecHeaderWriter());
return std::unique_ptr<ForwardErrorCorrection>(new ForwardErrorCorrection(
std::move(fec_header_reader), std::move(fec_header_writer)));
}
int ForwardErrorCorrection::EncodeFec(const PacketList& media_packets,
uint8_t protection_factor,
int num_important_packets,
@ -152,9 +162,10 @@ int ForwardErrorCorrection::EncodeFec(const PacketList& media_packets,
GenerateFecPayloads(media_packets, num_fec_packets);
// TODO(brandtr): Generalize this when multistream protection support is
// added.
const uint32_t media_ssrc = ParseSsrc(media_packets.front()->data);
const uint16_t seq_num_base =
ParseSequenceNumber(media_packets.front().get()->data);
FinalizeFecHeaders(num_fec_packets, seq_num_base);
ParseSequenceNumber(media_packets.front()->data);
FinalizeFecHeaders(num_fec_packets, media_ssrc, seq_num_base);
return 0;
}
@ -305,11 +316,12 @@ int ForwardErrorCorrection::InsertZerosInPacketMasks(
}
void ForwardErrorCorrection::FinalizeFecHeaders(size_t num_fec_packets,
uint32_t media_ssrc,
uint16_t seq_num_base) {
for (size_t i = 0; i < num_fec_packets; ++i) {
fec_header_writer_->FinalizeFecHeader(
seq_num_base, &packet_masks_[i * packet_mask_size_], packet_mask_size_,
&generated_fec_packets_[i]);
media_ssrc, seq_num_base, &packet_masks_[i * packet_mask_size_],
packet_mask_size_, &generated_fec_packets_[i]);
}
}
@ -381,7 +393,6 @@ void ForwardErrorCorrection::InsertFecPacket(
// Parse ULPFEC/FlexFEC header specific info.
bool ret = fec_header_reader_->ReadFecHeader(fec_packet.get());
if (!ret) {
LOG(LS_WARNING) << "Malformed FEC header: dropping packet.";
return;
}
// Parse packet mask from header and represent as protected packets.

View File

@ -151,6 +151,7 @@ class ForwardErrorCorrection {
// Creates a ForwardErrorCorrection tailored for a specific FEC scheme.
static std::unique_ptr<ForwardErrorCorrection> CreateUlpfec();
static std::unique_ptr<ForwardErrorCorrection> CreateFlexfec();
// Generates a list of FEC packets from supplied media packets.
//
@ -259,7 +260,9 @@ class ForwardErrorCorrection {
// Writes the FEC header fields that are not written by GenerateFecPayloads.
// This includes writing the packet masks.
void FinalizeFecHeaders(size_t num_fec_packets, uint16_t seq_num_base);
void FinalizeFecHeaders(size_t num_fec_packets,
uint32_t media_ssrc,
uint16_t seq_num_base);
// Inserts the |received_packets| into the internal received FEC packet list
// or into |recovered_packets|.
@ -391,6 +394,7 @@ class FecHeaderWriter {
// Writes FEC header.
virtual void FinalizeFecHeader(
uint32_t media_ssrc,
uint16_t seq_num_base,
const uint8_t* packet_mask,
size_t packet_mask_size,

View File

@ -16,6 +16,7 @@
#include "webrtc/base/random.h"
#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
#include "webrtc/modules/rtp_rtcp/source/fec_test_helper.h"
#include "webrtc/modules/rtp_rtcp/source/flexfec_header_reader_writer.h"
#include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h"
#include "webrtc/modules/rtp_rtcp/source/ulpfec_header_reader_writer.h"
#include "webrtc/test/gtest.h"
@ -28,6 +29,7 @@ namespace {
constexpr size_t kTransportOverhead = 28;
constexpr uint32_t kMediaSsrc = 83542;
constexpr uint32_t kFlexfecSsrc = 43245;
// Deep copies |src| to |dst|, but only keeps every Nth packet.
void DeepCopyEveryNthPacket(const ForwardErrorCorrection::PacketList& src,
@ -151,16 +153,14 @@ bool RtpFecTest<ForwardErrorCorrectionType>::IsRecoveryComplete() {
// All recovered packets must be identical to the corresponding
// original packets.
using PacketPtr = std::unique_ptr<ForwardErrorCorrection::Packet>;
using RecoveredPacketPtr =
std::unique_ptr<ForwardErrorCorrection::RecoveredPacket>;
auto cmp = [](const PacketPtr& media_packet,
const RecoveredPacketPtr& recovered_packet) {
auto cmp = [](
const std::unique_ptr<ForwardErrorCorrection::Packet>& media_packet,
const std::unique_ptr<ForwardErrorCorrection::RecoveredPacket>&
recovered_packet) {
if (media_packet->length != recovered_packet->pkt->length) {
return false;
}
if (memcmp(media_packet->data,
recovered_packet->pkt->data,
if (memcmp(media_packet->data, recovered_packet->pkt->data,
media_packet->length) != 0) {
return false;
}
@ -175,6 +175,14 @@ bool RtpFecTest<ForwardErrorCorrectionType>::IsRecoveryComplete() {
// member variables using |this|, thereby enforcing runtime
// resolution.
class FlexfecForwardErrorCorrection : public ForwardErrorCorrection {
public:
FlexfecForwardErrorCorrection()
: ForwardErrorCorrection(
std::unique_ptr<FecHeaderReader>(new FlexfecHeaderReader()),
std::unique_ptr<FecHeaderWriter>(new FlexfecHeaderWriter())) {}
};
class UlpfecForwardErrorCorrection : public ForwardErrorCorrection {
public:
UlpfecForwardErrorCorrection()
@ -183,7 +191,8 @@ class UlpfecForwardErrorCorrection : public ForwardErrorCorrection {
std::unique_ptr<FecHeaderWriter>(new UlpfecHeaderWriter())) {}
};
using FecTypes = Types<UlpfecForwardErrorCorrection>;
using FecTypes =
Types<FlexfecForwardErrorCorrection, UlpfecForwardErrorCorrection>;
TYPED_TEST_CASE(RtpFecTest, FecTypes);
TYPED_TEST(RtpFecTest, FecRecoveryNoLoss) {
@ -949,4 +958,42 @@ TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsWrap) {
EXPECT_FALSE(this->IsRecoveryComplete());
}
// 'using' directive needed for compiler to be happy.
using RtpFecTestWithFlexfec = RtpFecTest<FlexfecForwardErrorCorrection>;
TEST_F(RtpFecTestWithFlexfec,
FecRecoveryWithLossAndDifferentMediaAndFlexfecSsrcs) {
constexpr int kNumImportantPackets = 0;
constexpr bool kUseUnequalProtection = false;
constexpr int kNumMediaPackets = 4;
constexpr uint8_t kProtectionFactor = 60;
media_packets_ =
media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
EXPECT_EQ(0, fec_.EncodeFec(media_packets_, kProtectionFactor,
kNumImportantPackets, kUseUnequalProtection,
kFecMaskBursty, &generated_fec_packets_));
// Expect 1 FEC packet.
EXPECT_EQ(1u, generated_fec_packets_.size());
// 1 media packet lost
memset(media_loss_mask_, 0, sizeof(media_loss_mask_));
memset(fec_loss_mask_, 0, sizeof(fec_loss_mask_));
media_loss_mask_[3] = 1;
NetworkReceivedPackets(media_loss_mask_, fec_loss_mask_);
// Simulate FlexFEC packet received on different SSRC.
auto it = received_packets_.begin();
++it;
++it;
++it; // Now at the FEC packet.
(*it)->ssrc = kFlexfecSsrc;
EXPECT_EQ(0, fec_.DecodeFec(&received_packets_, &recovered_packets_));
// One packet lost, one FEC packet, expect complete recovery.
EXPECT_TRUE(IsRecoveryComplete());
}
} // namespace webrtc

View File

@ -99,6 +99,7 @@ size_t UlpfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const {
}
void UlpfecHeaderWriter::FinalizeFecHeader(
uint32_t /* media_ssrc */,
uint16_t seq_num_base,
const uint8_t* packet_mask,
size_t packet_mask_size,

View File

@ -55,6 +55,7 @@ class UlpfecHeaderWriter : public FecHeaderWriter {
size_t FecHeaderSize(size_t packet_mask_row_size) const override;
void FinalizeFecHeader(
uint32_t media_ssrc, // Unused by ULPFEC.
uint16_t seq_num_base,
const uint8_t* packet_mask,
size_t packet_mask_size,

View File

@ -56,8 +56,8 @@ std::unique_ptr<Packet> WriteHeader(const uint8_t* packet_mask,
for (size_t i = 0; i < written_packet->length; ++i) {
written_packet->data[i] = i; // Actual content doesn't matter.
}
writer.FinalizeFecHeader(kMediaStartSeqNum, packet_mask, packet_mask_size,
written_packet.get());
writer.FinalizeFecHeader(kMediaSsrc, kMediaStartSeqNum, packet_mask,
packet_mask_size, written_packet.get());
return written_packet;
}
@ -155,7 +155,7 @@ TEST(UlpfecHeaderWriterTest, FinalizesSmallHeader) {
}
UlpfecHeaderWriter writer;
writer.FinalizeFecHeader(kMediaStartSeqNum, packet_mask.get(),
writer.FinalizeFecHeader(kMediaSsrc, kMediaStartSeqNum, packet_mask.get(),
packet_mask_size, &written_packet);
const uint8_t* packet = written_packet.data;
@ -179,7 +179,7 @@ TEST(UlpfecHeaderWriterTest, FinalizesLargeHeader) {
}
UlpfecHeaderWriter writer;
writer.FinalizeFecHeader(kMediaStartSeqNum, packet_mask.get(),
writer.FinalizeFecHeader(kMediaSsrc, kMediaStartSeqNum, packet_mask.get(),
packet_mask_size, &written_packet);
const uint8_t* packet = written_packet.data;