blob: ec9088c027f0c123bac506966c44553e07fef966 [file] [log] [blame]
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001/*
2 * Copyright (c) 2012 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/ulpfec_generator.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <cstdint>
brandtr35c480c2016-08-09 01:23:23 -070016#include <memory>
17#include <utility>
18
Rasmus Brandt393e2662018-01-22 12:52:36 +010019#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/rtp_rtcp/source/byte_io.h"
21#include "modules/rtp_rtcp/source/forward_error_correction.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "modules/rtp_rtcp/source/forward_error_correction_internal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/rtp_rtcp/source/rtp_utility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/checks.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000025
26namespace webrtc {
27
Rasmus Brandtc07ebb32016-10-04 10:57:36 +020028namespace {
29
brandtr74811e52016-08-10 00:51:50 -070030constexpr size_t kRedForFecHeaderLength = 1;
31
marpan@webrtc.org747cd872012-05-22 16:50:00 +000032// This controls the maximum amount of excess overhead (actual - target)
brandtrece4aba2016-09-20 23:16:28 -070033// allowed in order to trigger EncodeFec(), before |params_.max_fec_frames|
marpan@webrtc.org747cd872012-05-22 16:50:00 +000034// is reached. Overhead here is defined as relative to number of media packets.
brandtr74811e52016-08-10 00:51:50 -070035constexpr int kMaxExcessOverhead = 50; // Q8.
36
marpan@webrtc.org747cd872012-05-22 16:50:00 +000037// This is the minimum number of media packets required (above some protection
brandtrece4aba2016-09-20 23:16:28 -070038// level) in order to trigger EncodeFec(), before |params_.max_fec_frames| is
marpan@webrtc.org747cd872012-05-22 16:50:00 +000039// reached.
brandtr74811e52016-08-10 00:51:50 -070040constexpr size_t kMinMediaPackets = 4;
41
marpan@webrtc.org747cd872012-05-22 16:50:00 +000042// Threshold on the received FEC protection level, above which we enforce at
brandtr74811e52016-08-10 00:51:50 -070043// least |kMinMediaPackets| packets for the FEC code. Below this
44// threshold |kMinMediaPackets| is set to default value of 1.
45//
46// The range is between 0 and 255, where 255 corresponds to 100% overhead
47// (relative to the number of protected media packets).
48constexpr uint8_t kHighProtectionThreshold = 80;
49
50// This threshold is used to adapt the |kMinMediaPackets| threshold, based
51// on the average number of packets per frame seen so far. When there are few
52// packets per frame (as given by this threshold), at least
53// |kMinMediaPackets| + 1 packets are sent to the FEC code.
54constexpr float kMinMediaPacketsAdaptationThreshold = 2.0f;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000055
brandtrd726a3f2017-06-29 02:45:35 -070056// At construction time, we don't know the SSRC that is used for the generated
57// FEC packets, but we still need to give it to the ForwardErrorCorrection ctor
58// to be used in the decoding.
59// TODO(brandtr): Get rid of this awkwardness by splitting
60// ForwardErrorCorrection in two objects -- one encoder and one decoder.
61constexpr uint32_t kUnknownSsrc = 0;
62
Rasmus Brandtc07ebb32016-10-04 10:57:36 +020063} // namespace
64
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000065RedPacket::RedPacket(size_t length)
brandtr869e7cd2016-10-31 05:27:07 -070066 : data_(new uint8_t[length]), length_(length), header_length_(0) {}
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000067
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010068RedPacket::~RedPacket() = default;
69
brandtr74811e52016-08-10 00:51:50 -070070void RedPacket::CreateHeader(const uint8_t* rtp_header,
71 size_t header_length,
72 int red_payload_type,
73 int payload_type) {
brandtr624c3352016-09-01 05:01:56 -070074 RTC_DCHECK_LE(header_length + kRedForFecHeaderLength, length_);
brandtr74811e52016-08-10 00:51:50 -070075 memcpy(data_.get(), rtp_header, header_length);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000076 // Replace payload type.
77 data_[1] &= 0x80;
brandtr74811e52016-08-10 00:51:50 -070078 data_[1] += red_payload_type;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000079 // Add RED header
80 // f-bit always 0
brandtr74811e52016-08-10 00:51:50 -070081 data_[header_length] = static_cast<uint8_t>(payload_type);
82 header_length_ = header_length + kRedForFecHeaderLength;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000083}
84
85void RedPacket::SetSeqNum(int seq_num) {
brandtr74811e52016-08-10 00:51:50 -070086 RTC_DCHECK_GE(seq_num, 0);
87 RTC_DCHECK_LT(seq_num, 1 << 16);
sprang@webrtc.org779c3d12015-03-17 16:42:49 +000088
89 ByteWriter<uint16_t>::WriteBigEndian(&data_[2], seq_num);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000090}
91
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000092void RedPacket::AssignPayload(const uint8_t* payload, size_t length) {
brandtr74811e52016-08-10 00:51:50 -070093 RTC_DCHECK_LE(header_length_ + length, length_);
94 memcpy(data_.get() + header_length_, payload, length);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000095}
96
97void RedPacket::ClearMarkerBit() {
98 data_[1] &= 0x7F;
99}
100
101uint8_t* RedPacket::data() const {
brandtr74811e52016-08-10 00:51:50 -0700102 return data_.get();
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000103}
104
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000105size_t RedPacket::length() const {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000106 return length_;
107}
108
brandtr869e7cd2016-10-31 05:27:07 -0700109UlpfecGenerator::UlpfecGenerator()
brandtrd726a3f2017-06-29 02:45:35 -0700110 : UlpfecGenerator(ForwardErrorCorrection::CreateUlpfec(kUnknownSsrc)) {}
brandtrc295e002016-11-03 09:22:33 -0700111
112UlpfecGenerator::UlpfecGenerator(std::unique_ptr<ForwardErrorCorrection> fec)
113 : fec_(std::move(fec)),
Rasmus Brandt393e2662018-01-22 12:52:36 +0100114 last_media_packet_rtp_header_length_(0),
Rasmus Brandt78db1582016-09-21 09:19:34 +0200115 num_protected_frames_(0),
Rasmus Brandt78db1582016-09-21 09:19:34 +0200116 min_num_media_packets_(1) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000117 memset(&params_, 0, sizeof(params_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000118 memset(&new_params_, 0, sizeof(new_params_));
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000119}
120
brandtr869e7cd2016-10-31 05:27:07 -0700121UlpfecGenerator::~UlpfecGenerator() = default;
brandtr74811e52016-08-10 00:51:50 -0700122
brandtr1743a192016-11-07 03:36:05 -0800123void UlpfecGenerator::SetFecParameters(const FecProtectionParams& params) {
124 RTC_DCHECK_GE(params.fec_rate, 0);
125 RTC_DCHECK_LE(params.fec_rate, 255);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000126 // Store the new params and apply them for the next set of FEC packets being
127 // produced.
brandtr1743a192016-11-07 03:36:05 -0800128 new_params_ = params;
129 if (params.fec_rate > kHighProtectionThreshold) {
brandtr74811e52016-08-10 00:51:50 -0700130 min_num_media_packets_ = kMinMediaPackets;
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000131 } else {
brandtr74811e52016-08-10 00:51:50 -0700132 min_num_media_packets_ = 1;
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000133 }
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000134}
135
Ilya Nikolaevskiy082696e2019-09-03 07:52:52 +0000136int UlpfecGenerator::AddRtpPacketAndGenerateFec(const uint8_t* data_buffer,
137 size_t payload_length,
138 size_t rtp_header_length) {
brandtr74811e52016-08-10 00:51:50 -0700139 RTC_DCHECK(generated_fec_packets_.empty());
140 if (media_packets_.empty()) {
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000141 params_ = new_params_;
142 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200143 bool complete_frame = false;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000144 const bool marker_bit = (data_buffer[1] & kRtpMarkerBitMask) ? true : false;
Rasmus Brandt78db1582016-09-21 09:19:34 +0200145 if (media_packets_.size() < kUlpfecMaxMediaPackets) {
brandtrc295e002016-11-03 09:22:33 -0700146 // Our packet masks can only protect up to |kUlpfecMaxMediaPackets| packets.
brandtr35c480c2016-08-09 01:23:23 -0700147 std::unique_ptr<ForwardErrorCorrection::Packet> packet(
148 new ForwardErrorCorrection::Packet());
Ilya Nikolaevskiy082696e2019-09-03 07:52:52 +0000149 packet->length = payload_length + rtp_header_length;
150 memcpy(packet->data, data_buffer, packet->length);
brandtr74811e52016-08-10 00:51:50 -0700151 media_packets_.push_back(std::move(packet));
Rasmus Brandt393e2662018-01-22 12:52:36 +0100152 // Keep track of the RTP header length, so we can copy the RTP header
153 // from |packet| to newly generated ULPFEC+RED packets.
154 RTC_DCHECK_GE(rtp_header_length, kRtpHeaderSize);
155 last_media_packet_rtp_header_length_ = rtp_header_length;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000156 }
157 if (marker_bit) {
brandtr74811e52016-08-10 00:51:50 -0700158 ++num_protected_frames_;
mflodmanfcf54bd2015-04-14 21:28:08 +0200159 complete_frame = true;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000160 }
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000161 // Produce FEC over at most |params_.max_fec_frames| frames, or as soon as:
162 // (1) the excess overhead (actual overhead - requested/target overhead) is
163 // less than |kMaxExcessOverhead|, and
brandtr74811e52016-08-10 00:51:50 -0700164 // (2) at least |min_num_media_packets_| media packets is reached.
mflodmanfcf54bd2015-04-14 21:28:08 +0200165 if (complete_frame &&
brandtr74811e52016-08-10 00:51:50 -0700166 (num_protected_frames_ == params_.max_fec_frames ||
167 (ExcessOverheadBelowMax() && MinimumMediaPacketsReached()))) {
Rasmus Brandt38213992016-10-04 14:27:56 +0200168 // We are not using Unequal Protection feature of the parity erasure code.
169 constexpr int kNumImportantPackets = 0;
brandtr74811e52016-08-10 00:51:50 -0700170 constexpr bool kUseUnequalProtection = false;
Rasmus Brandt78db1582016-09-21 09:19:34 +0200171 int ret = fec_->EncodeFec(media_packets_, params_.fec_rate,
Rasmus Brandt38213992016-10-04 14:27:56 +0200172 kNumImportantPackets, kUseUnequalProtection,
Rasmus Brandt78db1582016-09-21 09:19:34 +0200173 params_.fec_mask_type, &generated_fec_packets_);
brandtr74811e52016-08-10 00:51:50 -0700174 if (generated_fec_packets_.empty()) {
Rasmus Brandtc07ebb32016-10-04 10:57:36 +0200175 ResetState();
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000176 }
177 return ret;
178 }
179 return 0;
180}
181
brandtr869e7cd2016-10-31 05:27:07 -0700182bool UlpfecGenerator::ExcessOverheadBelowMax() const {
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000183 return ((Overhead() - params_.fec_rate) < kMaxExcessOverhead);
184}
185
brandtr869e7cd2016-10-31 05:27:07 -0700186bool UlpfecGenerator::MinimumMediaPacketsReached() const {
brandtr74811e52016-08-10 00:51:50 -0700187 float average_num_packets_per_frame =
188 static_cast<float>(media_packets_.size()) / num_protected_frames_;
189 int num_media_packets = static_cast<int>(media_packets_.size());
190 if (average_num_packets_per_frame < kMinMediaPacketsAdaptationThreshold) {
191 return num_media_packets >= min_num_media_packets_;
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000192 } else {
193 // For larger rates (more packets/frame), increase the threshold.
brandtr74811e52016-08-10 00:51:50 -0700194 // TODO(brandtr): Investigate what impact this adaptation has.
195 return num_media_packets >= min_num_media_packets_ + 1;
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000196 }
197}
198
brandtr869e7cd2016-10-31 05:27:07 -0700199bool UlpfecGenerator::FecAvailable() const {
brandtr74811e52016-08-10 00:51:50 -0700200 return !generated_fec_packets_.empty();
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000201}
202
brandtr869e7cd2016-10-31 05:27:07 -0700203size_t UlpfecGenerator::NumAvailableFecPackets() const {
brandtr74811e52016-08-10 00:51:50 -0700204 return generated_fec_packets_.size();
mflodmanfcf54bd2015-04-14 21:28:08 +0200205}
206
brandtr869e7cd2016-10-31 05:27:07 -0700207size_t UlpfecGenerator::MaxPacketOverhead() const {
Rasmus Brandt78db1582016-09-21 09:19:34 +0200208 return fec_->MaxPacketOverhead();
brandtr6631e8a2016-09-13 03:23:29 -0700209}
210
brandtr869e7cd2016-10-31 05:27:07 -0700211std::vector<std::unique_ptr<RedPacket>> UlpfecGenerator::GetUlpfecPacketsAsRed(
brandtr74811e52016-08-10 00:51:50 -0700212 int red_payload_type,
213 int ulpfec_payload_type,
Rasmus Brandt393e2662018-01-22 12:52:36 +0100214 uint16_t first_seq_num) {
brandtr74811e52016-08-10 00:51:50 -0700215 std::vector<std::unique_ptr<RedPacket>> red_packets;
216 red_packets.reserve(generated_fec_packets_.size());
217 RTC_DCHECK(!media_packets_.empty());
218 ForwardErrorCorrection::Packet* last_media_packet =
219 media_packets_.back().get();
220 uint16_t seq_num = first_seq_num;
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100221 for (const auto* fec_packet : generated_fec_packets_) {
brandtr74811e52016-08-10 00:51:50 -0700222 // Wrap FEC packet (including FEC headers) in a RED packet. Since the
223 // FEC packets in |generated_fec_packets_| don't have RTP headers, we
224 // reuse the header from the last media packet.
Rasmus Brandt393e2662018-01-22 12:52:36 +0100225 RTC_DCHECK_GT(last_media_packet_rtp_header_length_, 0);
226 std::unique_ptr<RedPacket> red_packet(
227 new RedPacket(last_media_packet_rtp_header_length_ +
Ilya Nikolaevskiy082696e2019-09-03 07:52:52 +0000228 kRedForFecHeaderLength + fec_packet->length));
229 red_packet->CreateHeader(last_media_packet->data,
Rasmus Brandt393e2662018-01-22 12:52:36 +0100230 last_media_packet_rtp_header_length_,
brandtr74811e52016-08-10 00:51:50 -0700231 red_payload_type, ulpfec_payload_type);
232 red_packet->SetSeqNum(seq_num++);
mflodmanfcf54bd2015-04-14 21:28:08 +0200233 red_packet->ClearMarkerBit();
Ilya Nikolaevskiy082696e2019-09-03 07:52:52 +0000234 red_packet->AssignPayload(fec_packet->data, fec_packet->length);
brandtr74811e52016-08-10 00:51:50 -0700235 red_packets.push_back(std::move(red_packet));
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000236 }
Rasmus Brandtc07ebb32016-10-04 10:57:36 +0200237
238 ResetState();
239
brandtr74811e52016-08-10 00:51:50 -0700240 return red_packets;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000241}
242
brandtr869e7cd2016-10-31 05:27:07 -0700243int UlpfecGenerator::Overhead() const {
brandtr74811e52016-08-10 00:51:50 -0700244 RTC_DCHECK(!media_packets_.empty());
245 int num_fec_packets =
Rasmus Brandt78db1582016-09-21 09:19:34 +0200246 fec_->NumFecPackets(media_packets_.size(), params_.fec_rate);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000247 // Return the overhead in Q8.
brandtr74811e52016-08-10 00:51:50 -0700248 return (num_fec_packets << 8) / media_packets_.size();
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000249}
250
brandtr869e7cd2016-10-31 05:27:07 -0700251void UlpfecGenerator::ResetState() {
brandtr74811e52016-08-10 00:51:50 -0700252 media_packets_.clear();
Rasmus Brandt393e2662018-01-22 12:52:36 +0100253 last_media_packet_rtp_header_length_ = 0;
Rasmus Brandtc07ebb32016-10-04 10:57:36 +0200254 generated_fec_packets_.clear();
255 num_protected_frames_ = 0;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000256}
257
258} // namespace webrtc