1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cast/streaming/rtcp_common.h"
#include <chrono>
#include <limits>
#include "absl/types/span.h"
#include "gtest/gtest.h"
#include "platform/api/time.h"
#include "util/chrono_helpers.h"
namespace openscreen {
namespace cast {
namespace {
template <typename T>
void SerializeAndExpectPointerAdvanced(const T& source,
int num_bytes,
uint8_t* buffer) {
absl::Span<uint8_t> buffer_span(buffer, num_bytes);
source.AppendFields(&buffer_span);
EXPECT_EQ(buffer + num_bytes, buffer_span.data());
}
// Tests that the RTCP Common Header for a packet type that includes an Item
// Count is successfully serialized and re-parsed.
TEST(RtcpCommonTest, SerializesAndParsesHeaderForSenderReports) {
RtcpCommonHeader original;
original.packet_type = RtcpPacketType::kSenderReport;
original.with.report_count = 31;
original.payload_size = 16;
uint8_t buffer[kRtcpCommonHeaderSize];
SerializeAndExpectPointerAdvanced(original, kRtcpCommonHeaderSize, buffer);
const auto parsed = RtcpCommonHeader::Parse(buffer);
ASSERT_TRUE(parsed.has_value());
EXPECT_EQ(original.packet_type, parsed->packet_type);
EXPECT_EQ(original.with.report_count, parsed->with.report_count);
EXPECT_EQ(original.payload_size, parsed->payload_size);
}
// Tests that the RTCP Common Header for a packet type that includes a RTCP
// Subtype is successfully serialized and re-parsed.
TEST(RtcpCommonTest, SerializesAndParsesHeaderForCastFeedback) {
RtcpCommonHeader original;
original.packet_type = RtcpPacketType::kPayloadSpecific;
original.with.subtype = RtcpSubtype::kFeedback;
original.payload_size = 99 * sizeof(uint32_t);
uint8_t buffer[kRtcpCommonHeaderSize];
SerializeAndExpectPointerAdvanced(original, kRtcpCommonHeaderSize, buffer);
const auto parsed = RtcpCommonHeader::Parse(buffer);
ASSERT_TRUE(parsed.has_value());
EXPECT_EQ(original.packet_type, parsed->packet_type);
EXPECT_EQ(original.with.subtype, parsed->with.subtype);
EXPECT_EQ(original.payload_size, parsed->payload_size);
}
// Tests that a RTCP Common Header will not be parsed from an empty buffer.
TEST(RtcpCommonTest, WillNotParseHeaderFromEmptyBuffer) {
const uint8_t kEmptyPacket[] = {};
EXPECT_FALSE(
RtcpCommonHeader::Parse(absl::Span<const uint8_t>(kEmptyPacket, 0))
.has_value());
}
// Tests that a RTCP Common Header will not be parsed from a buffer containing
// garbage data.
TEST(RtcpCommonTest, WillNotParseHeaderFromGarbage) {
// clang-format off
const uint8_t kGarbage[] = {
0x4f, 0x27, 0xeb, 0x22, 0x27, 0xeb, 0x22, 0x4f,
0xeb, 0x22, 0x4f, 0x27, 0x22, 0x4f, 0x27, 0xeb,
};
// clang-format on
EXPECT_FALSE(RtcpCommonHeader::Parse(kGarbage).has_value());
}
// Tests whether RTCP Common Header validation logic is correct.
TEST(RtcpCommonTest, WillNotParseHeaderWithInvalidData) {
// clang-format off
const uint8_t kCastFeedbackPacket[] = {
0b10000001, // Version=2, Padding=no, ItemCount=1 byte.
206, // RTCP Packet type byte.
0x00, 0x04, // Length of remainder of packet, in 32-bit words.
9, 8, 7, 6, // SSRC of receiver.
1, 2, 3, 4, // SSRC of sender.
'C', 'A', 'S', 'T',
0x0a, // Checkpoint Frame ID (lower 8 bits).
0x00, // Number of "Loss Fields"
0x00, 0x28, // Current Playout Delay in milliseconds.
};
// clang-format on
// Start with a valid packet, and expect the parse to succeed.
uint8_t buffer[sizeof(kCastFeedbackPacket)];
memcpy(buffer, kCastFeedbackPacket, sizeof(buffer));
EXPECT_TRUE(RtcpCommonHeader::Parse(buffer).has_value());
// Wrong version in first byte: Expect parse failure.
buffer[0] = 0b01000001;
EXPECT_FALSE(RtcpCommonHeader::Parse(buffer).has_value());
buffer[0] = kCastFeedbackPacket[0];
// Wrong packet type (not in RTCP range): Expect parse failure.
buffer[1] = 42;
EXPECT_FALSE(RtcpCommonHeader::Parse(buffer).has_value());
buffer[1] = kCastFeedbackPacket[1];
}
// Test that the Report Block optionally included in Sender Reports or Receiver
// Reports can be serialized and re-parsed correctly.
TEST(RtcpCommonTest, SerializesAndParsesRtcpReportBlocks) {
constexpr Ssrc kSsrc{0x04050607};
RtcpReportBlock original;
original.ssrc = kSsrc;
original.packet_fraction_lost_numerator = 0x67;
original.cumulative_packets_lost = 74536;
original.extended_high_sequence_number = 0x0201fedc;
original.jitter = RtpTimeDelta::FromTicks(123);
original.last_status_report_id = 0x0908;
original.delay_since_last_report = RtcpReportBlock::Delay(99999);
uint8_t buffer[kRtcpReportBlockSize];
SerializeAndExpectPointerAdvanced(original, kRtcpReportBlockSize, buffer);
// If the number of report blocks is zero, or some other SSRC is specified,
// ParseOne() should not return a result.
EXPECT_FALSE(RtcpReportBlock::ParseOne(buffer, 0, 0).has_value());
EXPECT_FALSE(RtcpReportBlock::ParseOne(buffer, 0, kSsrc).has_value());
EXPECT_FALSE(RtcpReportBlock::ParseOne(buffer, 1, 0).has_value());
// Expect that the report block is parsed correctly.
const auto parsed = RtcpReportBlock::ParseOne(buffer, 1, kSsrc);
ASSERT_TRUE(parsed.has_value());
EXPECT_EQ(original.ssrc, parsed->ssrc);
EXPECT_EQ(original.packet_fraction_lost_numerator,
parsed->packet_fraction_lost_numerator);
EXPECT_EQ(original.cumulative_packets_lost, parsed->cumulative_packets_lost);
EXPECT_EQ(original.extended_high_sequence_number,
parsed->extended_high_sequence_number);
EXPECT_EQ(original.jitter, parsed->jitter);
EXPECT_EQ(original.last_status_report_id, parsed->last_status_report_id);
EXPECT_EQ(original.delay_since_last_report, parsed->delay_since_last_report);
}
// Tests that the Report Block parser can, among multiple Report Blocks, find
// the one with a matching recipient SSRC.
TEST(RtcpCommonTest, ParsesOneReportBlockFromMultipleBlocks) {
constexpr Ssrc kSsrc{0x04050607};
constexpr int kNumBlocks = 5;
RtcpReportBlock expected;
expected.ssrc = kSsrc;
expected.packet_fraction_lost_numerator = 0x67;
expected.cumulative_packets_lost = 74536;
expected.extended_high_sequence_number = 0x0201fedc;
expected.jitter = RtpTimeDelta::FromTicks(123);
expected.last_status_report_id = 0x0908;
expected.delay_since_last_report = RtcpReportBlock::Delay(99999);
// Generate multiple report blocks with different recipient SSRCs.
uint8_t buffer[kRtcpReportBlockSize * kNumBlocks];
absl::Span<uint8_t> buffer_span(buffer, kRtcpReportBlockSize * kNumBlocks);
for (int i = 0; i < kNumBlocks; ++i) {
RtcpReportBlock another;
another.ssrc = expected.ssrc + i - 2;
another.packet_fraction_lost_numerator =
expected.packet_fraction_lost_numerator + i - 2;
another.cumulative_packets_lost = expected.cumulative_packets_lost + i - 2;
another.extended_high_sequence_number =
expected.extended_high_sequence_number + i - 2;
another.jitter = expected.jitter + RtpTimeDelta::FromTicks(i - 2);
another.last_status_report_id = expected.last_status_report_id + i - 2;
another.delay_since_last_report =
expected.delay_since_last_report + RtcpReportBlock::Delay(i - 2);
another.AppendFields(&buffer_span);
}
// Expect that the desired report block is found and parsed correctly.
const auto parsed = RtcpReportBlock::ParseOne(buffer, kNumBlocks, kSsrc);
ASSERT_TRUE(parsed.has_value());
EXPECT_EQ(expected.ssrc, parsed->ssrc);
EXPECT_EQ(expected.packet_fraction_lost_numerator,
parsed->packet_fraction_lost_numerator);
EXPECT_EQ(expected.cumulative_packets_lost, parsed->cumulative_packets_lost);
EXPECT_EQ(expected.extended_high_sequence_number,
parsed->extended_high_sequence_number);
EXPECT_EQ(expected.jitter, parsed->jitter);
EXPECT_EQ(expected.last_status_report_id, parsed->last_status_report_id);
EXPECT_EQ(expected.delay_since_last_report, parsed->delay_since_last_report);
}
// Tests the helper for computing the packet fraction loss numerator, a value
// that should always be between 0 and 255, in terms of absolute packet counts.
TEST(RtcpCommonTest, ComputesPacketLossFractionForReportBlocks) {
const auto ComputeFractionLost = [](int64_t num_apparently_sent,
int64_t num_received) {
RtcpReportBlock report;
report.SetPacketFractionLostNumerator(num_apparently_sent, num_received);
return report.packet_fraction_lost_numerator;
};
// If no non-duplicate packets were sent to the Receiver, the packet loss
// fraction should be zero.
EXPECT_EQ(0, ComputeFractionLost(0, 0));
EXPECT_EQ(0, ComputeFractionLost(0, 1));
EXPECT_EQ(0, ComputeFractionLost(0, 999));
// If the same number or more packets were received than those apparently
// sent, the packet loss fraction should be zero.
EXPECT_EQ(0, ComputeFractionLost(1, 1));
EXPECT_EQ(0, ComputeFractionLost(1, 2));
EXPECT_EQ(0, ComputeFractionLost(1, 4));
EXPECT_EQ(0, ComputeFractionLost(4, 5));
EXPECT_EQ(0, ComputeFractionLost(42, 42));
EXPECT_EQ(0, ComputeFractionLost(60, 999));
// Test various partial loss scenarios.
EXPECT_EQ(85, ComputeFractionLost(3, 2));
EXPECT_EQ(128, ComputeFractionLost(10, 5));
EXPECT_EQ(174, ComputeFractionLost(22, 7));
// Test various total-loss/near-total-loss scenarios.
EXPECT_EQ(255, ComputeFractionLost(17, 0));
EXPECT_EQ(255, ComputeFractionLost(100, 0));
EXPECT_EQ(255, ComputeFractionLost(9876, 1));
}
// Tests the helper for computing the cumulative packet loss total, a value that
// should always be between 0 and 2^24 - 1, in terms of absolute packet counts.
TEST(RtcpCommonTest, ComputesCumulativePacketLossForReportBlocks) {
const auto ComputeLoss = [](int64_t num_apparently_sent,
int64_t num_received) {
RtcpReportBlock report;
report.SetCumulativePacketsLost(num_apparently_sent, num_received);
return report.cumulative_packets_lost;
};
// Test various no-loss scenarios (including duplicate packets).
EXPECT_EQ(0, ComputeLoss(0, 0));
EXPECT_EQ(0, ComputeLoss(0, 1));
EXPECT_EQ(0, ComputeLoss(3, 3));
EXPECT_EQ(0, ComputeLoss(56, 56));
EXPECT_EQ(0, ComputeLoss(std::numeric_limits<int64_t>::max() - 12,
std::numeric_limits<int64_t>::max()));
EXPECT_EQ(0, ComputeLoss(std::numeric_limits<int64_t>::max(),
std::numeric_limits<int64_t>::max()));
// Test various partial loss scenarios.
EXPECT_EQ(1, ComputeLoss(2, 1));
EXPECT_EQ(2, ComputeLoss(42, 40));
EXPECT_EQ(1025, ComputeLoss(999999, 999999 - 1025));
EXPECT_EQ(1, ComputeLoss(std::numeric_limits<int64_t>::max(),
std::numeric_limits<int64_t>::max() - 1));
// Test that a huge cumulative loss saturates to the maximum valid value for
// the field.
EXPECT_EQ((1 << 24) - 1, ComputeLoss(999999999, 1));
}
// Tests the helper that converts Clock::durations to the report blocks timebase
// (1/65536 sconds), and also that it saturates to to the valid range of values
// (0 to 2^32 - 1 ticks).
TEST(RtcpCommonTest, ComputesDelayForReportBlocks) {
RtcpReportBlock report;
using Delay = RtcpReportBlock::Delay;
const auto ComputeDelay = [](Clock::duration delay_in_wrong_timebase) {
RtcpReportBlock report;
report.SetDelaySinceLastReport(delay_in_wrong_timebase);
return report.delay_since_last_report;
};
// A duration less than or equal to zero should clamp to zero.
EXPECT_EQ(Delay::zero(), ComputeDelay(Clock::duration::min()));
EXPECT_EQ(Delay::zero(), ComputeDelay(milliseconds{-1234}));
EXPECT_EQ(Delay::zero(), ComputeDelay(Clock::duration::zero()));
// Test conversion of various durations that should not clamp.
EXPECT_EQ(Delay(32768 /* 1/2 second worth of ticks */),
ComputeDelay(milliseconds(500)));
EXPECT_EQ(Delay(65536 /* 1 second worth of ticks */),
ComputeDelay(seconds(1)));
EXPECT_EQ(Delay(655360 /* 10 seconds worth of ticks */),
ComputeDelay(seconds(10)));
EXPECT_EQ(Delay(4294967294), ComputeDelay(microseconds(65535999983)));
EXPECT_EQ(Delay(4294967294), ComputeDelay(microseconds(65535999984)));
// A too-large duration should clamp to the maximum-possible Delay value.
EXPECT_EQ(Delay(4294967295), ComputeDelay(microseconds(65535999985)));
EXPECT_EQ(Delay(4294967295), ComputeDelay(microseconds(65535999986)));
EXPECT_EQ(Delay(4294967295), ComputeDelay(microseconds(999999000000)));
EXPECT_EQ(Delay(4294967295), ComputeDelay(Clock::duration::max()));
}
} // namespace
} // namespace cast
} // namespace openscreen
|