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
|
/*
* Copyright (c) 2012 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 "modules/rtp_rtcp/source/ulpfec_generator.h"
#include <string.h>
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>
#include "api/environment/environment.h"
#include "api/units/data_rate.h"
#include "api/units/time_delta.h"
#include "modules/include/module_fec_types.h"
#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
#include "modules/rtp_rtcp/source/forward_error_correction.h"
#include "modules/rtp_rtcp/source/forward_error_correction_internal.h"
#include "rtc_base/checks.h"
#include "rtc_base/race_checker.h"
#include "rtc_base/synchronization/mutex.h"
namespace webrtc {
namespace {
constexpr size_t kRedForFecHeaderLength = 1;
// This controls the maximum amount of excess overhead (actual - target)
// allowed in order to trigger EncodeFec(), before `params_.max_fec_frames`
// is reached. Overhead here is defined as relative to number of media packets.
constexpr int kMaxExcessOverhead = 50; // Q8.
// This is the minimum number of media packets required (above some protection
// level) in order to trigger EncodeFec(), before `params_.max_fec_frames` is
// reached.
constexpr size_t kMinMediaPackets = 4;
// Threshold on the received FEC protection level, above which we enforce at
// least `kMinMediaPackets` packets for the FEC code. Below this
// threshold `kMinMediaPackets` is set to default value of 1.
//
// The range is between 0 and 255, where 255 corresponds to 100% overhead
// (relative to the number of protected media packets).
constexpr uint8_t kHighProtectionThreshold = 80;
// This threshold is used to adapt the `kMinMediaPackets` threshold, based
// on the average number of packets per frame seen so far. When there are few
// packets per frame (as given by this threshold), at least
// `kMinMediaPackets` + 1 packets are sent to the FEC code.
constexpr float kMinMediaPacketsAdaptationThreshold = 2.0f;
// At construction time, we don't know the SSRC that is used for the generated
// FEC packets, but we still need to give it to the ForwardErrorCorrection ctor
// to be used in the decoding.
// TODO(brandtr): Get rid of this awkwardness by splitting
// ForwardErrorCorrection in two objects -- one encoder and one decoder.
constexpr uint32_t kUnknownSsrc = 0;
} // namespace
UlpfecGenerator::Params::Params() = default;
UlpfecGenerator::Params::Params(FecProtectionParams delta_params,
FecProtectionParams keyframe_params)
: delta_params(delta_params), keyframe_params(keyframe_params) {}
UlpfecGenerator::UlpfecGenerator(const Environment& env,
int red_payload_type,
int ulpfec_payload_type)
: env_(env),
red_payload_type_(red_payload_type),
ulpfec_payload_type_(ulpfec_payload_type),
fec_(ForwardErrorCorrection::CreateUlpfec(kUnknownSsrc)),
num_protected_frames_(0),
min_num_media_packets_(1),
media_contains_keyframe_(false),
fec_bitrate_(/*max_window_size=*/TimeDelta::Seconds(1)) {}
// Used by FlexFecSender, payload types are unused.
UlpfecGenerator::UlpfecGenerator(const Environment& env,
std::unique_ptr<ForwardErrorCorrection> fec)
: env_(env),
red_payload_type_(0),
ulpfec_payload_type_(0),
fec_(std::move(fec)),
num_protected_frames_(0),
min_num_media_packets_(1),
media_contains_keyframe_(false),
fec_bitrate_(/*max_window_size=*/TimeDelta::Seconds(1)) {}
UlpfecGenerator::~UlpfecGenerator() = default;
void UlpfecGenerator::SetProtectionParameters(
const FecProtectionParams& delta_params,
const FecProtectionParams& key_params) {
RTC_DCHECK_GE(delta_params.fec_rate, 0);
RTC_DCHECK_LE(delta_params.fec_rate, 255);
RTC_DCHECK_GE(key_params.fec_rate, 0);
RTC_DCHECK_LE(key_params.fec_rate, 255);
// Store the new params and apply them for the next set of FEC packets being
// produced.
MutexLock lock(&mutex_);
pending_params_.emplace(delta_params, key_params);
}
void UlpfecGenerator::AddPacketAndGenerateFec(const RtpPacketToSend& packet) {
RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
RTC_DCHECK(generated_fec_packets_.empty());
{
MutexLock lock(&mutex_);
if (pending_params_) {
current_params_ = *pending_params_;
pending_params_.reset();
if (CurrentParams().fec_rate > kHighProtectionThreshold) {
min_num_media_packets_ = kMinMediaPackets;
} else {
min_num_media_packets_ = 1;
}
}
}
if (packet.is_key_frame()) {
media_contains_keyframe_ = true;
}
const bool complete_frame = packet.Marker();
if (media_packets_.size() < kUlpfecMaxMediaPackets) {
// Our packet masks can only protect up to `kUlpfecMaxMediaPackets` packets.
auto fec_packet = std::make_unique<ForwardErrorCorrection::Packet>();
fec_packet->data = packet.Buffer();
media_packets_.push_back(std::move(fec_packet));
// Keep a copy of the last RTP packet, so we can copy the RTP header
// from it when creating newly generated ULPFEC+RED packets.
RTC_DCHECK_GE(packet.headers_size(), kRtpHeaderSize);
last_media_packet_ = packet;
}
if (complete_frame) {
++num_protected_frames_;
}
auto params = CurrentParams();
// Produce FEC over at most `params_.max_fec_frames` frames, or as soon as:
// (1) the excess overhead (actual overhead - requested/target overhead) is
// less than `kMaxExcessOverhead`, and
// (2) at least `min_num_media_packets_` media packets is reached.
if (complete_frame &&
(num_protected_frames_ >= params.max_fec_frames ||
(ExcessOverheadBelowMax() && MinimumMediaPacketsReached()))) {
// We are not using Unequal Protection feature of the parity erasure code.
constexpr int kNumImportantPackets = 0;
constexpr bool kUseUnequalProtection = false;
fec_->EncodeFec(media_packets_, params.fec_rate, kNumImportantPackets,
kUseUnequalProtection, params.fec_mask_type,
&generated_fec_packets_);
if (generated_fec_packets_.empty()) {
ResetState();
}
}
}
bool UlpfecGenerator::ExcessOverheadBelowMax() const {
RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
return ((Overhead() - CurrentParams().fec_rate) < kMaxExcessOverhead);
}
bool UlpfecGenerator::MinimumMediaPacketsReached() const {
RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
float average_num_packets_per_frame =
static_cast<float>(media_packets_.size()) / num_protected_frames_;
int num_media_packets = static_cast<int>(media_packets_.size());
if (average_num_packets_per_frame < kMinMediaPacketsAdaptationThreshold) {
return num_media_packets >= min_num_media_packets_;
} else {
// For larger rates (more packets/frame), increase the threshold.
// TODO(brandtr): Investigate what impact this adaptation has.
return num_media_packets >= min_num_media_packets_ + 1;
}
}
const FecProtectionParams& UlpfecGenerator::CurrentParams() const {
RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
return media_contains_keyframe_ ? current_params_.keyframe_params
: current_params_.delta_params;
}
size_t UlpfecGenerator::MaxPacketOverhead() const {
RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
return fec_->MaxPacketOverhead();
}
std::vector<std::unique_ptr<RtpPacketToSend>> UlpfecGenerator::GetFecPackets() {
RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
if (generated_fec_packets_.empty()) {
return std::vector<std::unique_ptr<RtpPacketToSend>>();
}
// Wrap FEC packet (including FEC headers) in a RED packet. Since the
// FEC packets in `generated_fec_packets_` don't have RTP headers, we
// reuse the header from the last media packet.
RTC_CHECK(last_media_packet_.has_value());
last_media_packet_->SetPayloadSize(0);
std::vector<std::unique_ptr<RtpPacketToSend>> fec_packets;
fec_packets.reserve(generated_fec_packets_.size());
size_t total_fec_size_bytes = 0;
for (const auto* fec_packet : generated_fec_packets_) {
std::unique_ptr<RtpPacketToSend> red_packet =
std::make_unique<RtpPacketToSend>(*last_media_packet_);
red_packet->SetPayloadType(red_payload_type_);
red_packet->SetMarker(false);
uint8_t* payload_buffer = red_packet->SetPayloadSize(
kRedForFecHeaderLength + fec_packet->data.size());
// Primary RED header with F bit unset.
// See https://tools.ietf.org/html/rfc2198#section-3
payload_buffer[0] = ulpfec_payload_type_; // RED header.
memcpy(&payload_buffer[1], fec_packet->data.data(),
fec_packet->data.size());
total_fec_size_bytes += red_packet->size();
red_packet->set_packet_type(RtpPacketMediaType::kForwardErrorCorrection);
red_packet->set_allow_retransmission(false);
red_packet->set_is_red(true);
red_packet->set_fec_protect_packet(false);
fec_packets.push_back(std::move(red_packet));
}
ResetState();
MutexLock lock(&mutex_);
fec_bitrate_.Update(total_fec_size_bytes, env_.clock().CurrentTime());
return fec_packets;
}
DataRate UlpfecGenerator::CurrentFecRate() const {
MutexLock lock(&mutex_);
return fec_bitrate_.Rate(env_.clock().CurrentTime())
.value_or(DataRate::Zero());
}
int UlpfecGenerator::Overhead() const {
RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
RTC_DCHECK(!media_packets_.empty());
int num_fec_packets =
fec_->NumFecPackets(media_packets_.size(), CurrentParams().fec_rate);
// Return the overhead in Q8.
return (num_fec_packets << 8) / media_packets_.size();
}
void UlpfecGenerator::ResetState() {
RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
media_packets_.clear();
last_media_packet_.reset();
generated_fec_packets_.clear();
num_protected_frames_ = 0;
media_contains_keyframe_ = false;
}
} // namespace webrtc
|