blob: fa7505ebbf3d1a7dbf49657de8324e912147ecd4 [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
brandtr869e7cd2016-10-31 05:27:07 -070011#include "webrtc/modules/rtp_rtcp/source/ulpfec_generator.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000012
brandtr35c480c2016-08-09 01:23:23 -070013#include <memory>
14#include <utility>
15
sprang@webrtc.org779c3d12015-03-17 16:42:49 +000016#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000017#include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h"
18#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020019#include "webrtc/rtc_base/basictypes.h"
20#include "webrtc/rtc_base/checks.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000021
22namespace webrtc {
23
Rasmus Brandtc07ebb32016-10-04 10:57:36 +020024namespace {
25
brandtr74811e52016-08-10 00:51:50 -070026constexpr size_t kRedForFecHeaderLength = 1;
27
marpan@webrtc.org747cd872012-05-22 16:50:00 +000028// This controls the maximum amount of excess overhead (actual - target)
brandtrece4aba2016-09-20 23:16:28 -070029// allowed in order to trigger EncodeFec(), before |params_.max_fec_frames|
marpan@webrtc.org747cd872012-05-22 16:50:00 +000030// is reached. Overhead here is defined as relative to number of media packets.
brandtr74811e52016-08-10 00:51:50 -070031constexpr int kMaxExcessOverhead = 50; // Q8.
32
marpan@webrtc.org747cd872012-05-22 16:50:00 +000033// This is the minimum number of media packets required (above some protection
brandtrece4aba2016-09-20 23:16:28 -070034// level) in order to trigger EncodeFec(), before |params_.max_fec_frames| is
marpan@webrtc.org747cd872012-05-22 16:50:00 +000035// reached.
brandtr74811e52016-08-10 00:51:50 -070036constexpr size_t kMinMediaPackets = 4;
37
marpan@webrtc.org747cd872012-05-22 16:50:00 +000038// Threshold on the received FEC protection level, above which we enforce at
brandtr74811e52016-08-10 00:51:50 -070039// least |kMinMediaPackets| packets for the FEC code. Below this
40// threshold |kMinMediaPackets| is set to default value of 1.
41//
42// The range is between 0 and 255, where 255 corresponds to 100% overhead
43// (relative to the number of protected media packets).
44constexpr uint8_t kHighProtectionThreshold = 80;
45
46// This threshold is used to adapt the |kMinMediaPackets| threshold, based
47// on the average number of packets per frame seen so far. When there are few
48// packets per frame (as given by this threshold), at least
49// |kMinMediaPackets| + 1 packets are sent to the FEC code.
50constexpr float kMinMediaPacketsAdaptationThreshold = 2.0f;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000051
brandtrd726a3f2017-06-29 02:45:35 -070052// At construction time, we don't know the SSRC that is used for the generated
53// FEC packets, but we still need to give it to the ForwardErrorCorrection ctor
54// to be used in the decoding.
55// TODO(brandtr): Get rid of this awkwardness by splitting
56// ForwardErrorCorrection in two objects -- one encoder and one decoder.
57constexpr uint32_t kUnknownSsrc = 0;
58
Rasmus Brandtc07ebb32016-10-04 10:57:36 +020059} // namespace
60
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000061RedPacket::RedPacket(size_t length)
brandtr869e7cd2016-10-31 05:27:07 -070062 : data_(new uint8_t[length]), length_(length), header_length_(0) {}
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000063
brandtr74811e52016-08-10 00:51:50 -070064void RedPacket::CreateHeader(const uint8_t* rtp_header,
65 size_t header_length,
66 int red_payload_type,
67 int payload_type) {
brandtr624c3352016-09-01 05:01:56 -070068 RTC_DCHECK_LE(header_length + kRedForFecHeaderLength, length_);
brandtr74811e52016-08-10 00:51:50 -070069 memcpy(data_.get(), rtp_header, header_length);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000070 // Replace payload type.
71 data_[1] &= 0x80;
brandtr74811e52016-08-10 00:51:50 -070072 data_[1] += red_payload_type;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000073 // Add RED header
74 // f-bit always 0
brandtr74811e52016-08-10 00:51:50 -070075 data_[header_length] = static_cast<uint8_t>(payload_type);
76 header_length_ = header_length + kRedForFecHeaderLength;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000077}
78
79void RedPacket::SetSeqNum(int seq_num) {
brandtr74811e52016-08-10 00:51:50 -070080 RTC_DCHECK_GE(seq_num, 0);
81 RTC_DCHECK_LT(seq_num, 1 << 16);
sprang@webrtc.org779c3d12015-03-17 16:42:49 +000082
83 ByteWriter<uint16_t>::WriteBigEndian(&data_[2], seq_num);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000084}
85
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000086void RedPacket::AssignPayload(const uint8_t* payload, size_t length) {
brandtr74811e52016-08-10 00:51:50 -070087 RTC_DCHECK_LE(header_length_ + length, length_);
88 memcpy(data_.get() + header_length_, payload, length);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000089}
90
91void RedPacket::ClearMarkerBit() {
92 data_[1] &= 0x7F;
93}
94
95uint8_t* RedPacket::data() const {
brandtr74811e52016-08-10 00:51:50 -070096 return data_.get();
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000097}
98
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000099size_t RedPacket::length() const {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000100 return length_;
101}
102
brandtr869e7cd2016-10-31 05:27:07 -0700103UlpfecGenerator::UlpfecGenerator()
brandtrd726a3f2017-06-29 02:45:35 -0700104 : UlpfecGenerator(ForwardErrorCorrection::CreateUlpfec(kUnknownSsrc)) {}
brandtrc295e002016-11-03 09:22:33 -0700105
106UlpfecGenerator::UlpfecGenerator(std::unique_ptr<ForwardErrorCorrection> fec)
107 : fec_(std::move(fec)),
Rasmus Brandt78db1582016-09-21 09:19:34 +0200108 num_protected_frames_(0),
Rasmus Brandt78db1582016-09-21 09:19:34 +0200109 min_num_media_packets_(1) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000110 memset(&params_, 0, sizeof(params_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000111 memset(&new_params_, 0, sizeof(new_params_));
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000112}
113
brandtr869e7cd2016-10-31 05:27:07 -0700114UlpfecGenerator::~UlpfecGenerator() = default;
brandtr74811e52016-08-10 00:51:50 -0700115
brandtr869e7cd2016-10-31 05:27:07 -0700116std::unique_ptr<RedPacket> UlpfecGenerator::BuildRedPacket(
brandtr74811e52016-08-10 00:51:50 -0700117 const uint8_t* data_buffer,
118 size_t payload_length,
119 size_t rtp_header_length,
120 int red_payload_type) {
121 std::unique_ptr<RedPacket> red_packet(new RedPacket(
122 payload_length + kRedForFecHeaderLength + rtp_header_length));
123 int payload_type = data_buffer[1] & 0x7f;
124 red_packet->CreateHeader(data_buffer, rtp_header_length, red_payload_type,
125 payload_type);
126 red_packet->AssignPayload(data_buffer + rtp_header_length, payload_length);
127 return red_packet;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000128}
129
brandtr1743a192016-11-07 03:36:05 -0800130void UlpfecGenerator::SetFecParameters(const FecProtectionParams& params) {
131 RTC_DCHECK_GE(params.fec_rate, 0);
132 RTC_DCHECK_LE(params.fec_rate, 255);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000133 // Store the new params and apply them for the next set of FEC packets being
134 // produced.
brandtr1743a192016-11-07 03:36:05 -0800135 new_params_ = params;
136 if (params.fec_rate > kHighProtectionThreshold) {
brandtr74811e52016-08-10 00:51:50 -0700137 min_num_media_packets_ = kMinMediaPackets;
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000138 } else {
brandtr74811e52016-08-10 00:51:50 -0700139 min_num_media_packets_ = 1;
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000140 }
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000141}
142
brandtr869e7cd2016-10-31 05:27:07 -0700143int UlpfecGenerator::AddRtpPacketAndGenerateFec(const uint8_t* data_buffer,
144 size_t payload_length,
145 size_t rtp_header_length) {
brandtr74811e52016-08-10 00:51:50 -0700146 RTC_DCHECK(generated_fec_packets_.empty());
147 if (media_packets_.empty()) {
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000148 params_ = new_params_;
149 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200150 bool complete_frame = false;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000151 const bool marker_bit = (data_buffer[1] & kRtpMarkerBitMask) ? true : false;
Rasmus Brandt78db1582016-09-21 09:19:34 +0200152 if (media_packets_.size() < kUlpfecMaxMediaPackets) {
brandtrc295e002016-11-03 09:22:33 -0700153 // Our packet masks can only protect up to |kUlpfecMaxMediaPackets| packets.
brandtr35c480c2016-08-09 01:23:23 -0700154 std::unique_ptr<ForwardErrorCorrection::Packet> packet(
155 new ForwardErrorCorrection::Packet());
stefan@webrtc.orgaf5ffd52012-03-23 16:01:15 +0000156 packet->length = payload_length + rtp_header_length;
157 memcpy(packet->data, data_buffer, packet->length);
brandtr74811e52016-08-10 00:51:50 -0700158 media_packets_.push_back(std::move(packet));
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000159 }
160 if (marker_bit) {
brandtr74811e52016-08-10 00:51:50 -0700161 ++num_protected_frames_;
mflodmanfcf54bd2015-04-14 21:28:08 +0200162 complete_frame = true;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000163 }
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000164 // Produce FEC over at most |params_.max_fec_frames| frames, or as soon as:
165 // (1) the excess overhead (actual overhead - requested/target overhead) is
166 // less than |kMaxExcessOverhead|, and
brandtr74811e52016-08-10 00:51:50 -0700167 // (2) at least |min_num_media_packets_| media packets is reached.
mflodmanfcf54bd2015-04-14 21:28:08 +0200168 if (complete_frame &&
brandtr74811e52016-08-10 00:51:50 -0700169 (num_protected_frames_ == params_.max_fec_frames ||
170 (ExcessOverheadBelowMax() && MinimumMediaPacketsReached()))) {
Rasmus Brandt38213992016-10-04 14:27:56 +0200171 // We are not using Unequal Protection feature of the parity erasure code.
172 constexpr int kNumImportantPackets = 0;
brandtr74811e52016-08-10 00:51:50 -0700173 constexpr bool kUseUnequalProtection = false;
Rasmus Brandt78db1582016-09-21 09:19:34 +0200174 int ret = fec_->EncodeFec(media_packets_, params_.fec_rate,
Rasmus Brandt38213992016-10-04 14:27:56 +0200175 kNumImportantPackets, kUseUnequalProtection,
Rasmus Brandt78db1582016-09-21 09:19:34 +0200176 params_.fec_mask_type, &generated_fec_packets_);
brandtr74811e52016-08-10 00:51:50 -0700177 if (generated_fec_packets_.empty()) {
Rasmus Brandtc07ebb32016-10-04 10:57:36 +0200178 ResetState();
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000179 }
180 return ret;
181 }
182 return 0;
183}
184
brandtr869e7cd2016-10-31 05:27:07 -0700185bool UlpfecGenerator::ExcessOverheadBelowMax() const {
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000186 return ((Overhead() - params_.fec_rate) < kMaxExcessOverhead);
187}
188
brandtr869e7cd2016-10-31 05:27:07 -0700189bool UlpfecGenerator::MinimumMediaPacketsReached() const {
brandtr74811e52016-08-10 00:51:50 -0700190 float average_num_packets_per_frame =
191 static_cast<float>(media_packets_.size()) / num_protected_frames_;
192 int num_media_packets = static_cast<int>(media_packets_.size());
193 if (average_num_packets_per_frame < kMinMediaPacketsAdaptationThreshold) {
194 return num_media_packets >= min_num_media_packets_;
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000195 } else {
196 // For larger rates (more packets/frame), increase the threshold.
brandtr74811e52016-08-10 00:51:50 -0700197 // TODO(brandtr): Investigate what impact this adaptation has.
198 return num_media_packets >= min_num_media_packets_ + 1;
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000199 }
200}
201
brandtr869e7cd2016-10-31 05:27:07 -0700202bool UlpfecGenerator::FecAvailable() const {
brandtr74811e52016-08-10 00:51:50 -0700203 return !generated_fec_packets_.empty();
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000204}
205
brandtr869e7cd2016-10-31 05:27:07 -0700206size_t UlpfecGenerator::NumAvailableFecPackets() const {
brandtr74811e52016-08-10 00:51:50 -0700207 return generated_fec_packets_.size();
mflodmanfcf54bd2015-04-14 21:28:08 +0200208}
209
brandtr869e7cd2016-10-31 05:27:07 -0700210size_t UlpfecGenerator::MaxPacketOverhead() const {
Rasmus Brandt78db1582016-09-21 09:19:34 +0200211 return fec_->MaxPacketOverhead();
brandtr6631e8a2016-09-13 03:23:29 -0700212}
213
brandtr869e7cd2016-10-31 05:27:07 -0700214std::vector<std::unique_ptr<RedPacket>> UlpfecGenerator::GetUlpfecPacketsAsRed(
brandtr74811e52016-08-10 00:51:50 -0700215 int red_payload_type,
216 int ulpfec_payload_type,
217 uint16_t first_seq_num,
218 size_t rtp_header_length) {
219 std::vector<std::unique_ptr<RedPacket>> red_packets;
220 red_packets.reserve(generated_fec_packets_.size());
221 RTC_DCHECK(!media_packets_.empty());
222 ForwardErrorCorrection::Packet* last_media_packet =
223 media_packets_.back().get();
224 uint16_t seq_num = first_seq_num;
225 for (const auto& fec_packet : generated_fec_packets_) {
226 // Wrap FEC packet (including FEC headers) in a RED packet. Since the
227 // FEC packets in |generated_fec_packets_| don't have RTP headers, we
228 // reuse the header from the last media packet.
229 std::unique_ptr<RedPacket> red_packet(new RedPacket(
230 fec_packet->length + kRedForFecHeaderLength + rtp_header_length));
mflodmanfcf54bd2015-04-14 21:28:08 +0200231 red_packet->CreateHeader(last_media_packet->data, rtp_header_length,
brandtr74811e52016-08-10 00:51:50 -0700232 red_payload_type, ulpfec_payload_type);
233 red_packet->SetSeqNum(seq_num++);
mflodmanfcf54bd2015-04-14 21:28:08 +0200234 red_packet->ClearMarkerBit();
brandtr74811e52016-08-10 00:51:50 -0700235 red_packet->AssignPayload(fec_packet->data, fec_packet->length);
brandtr74811e52016-08-10 00:51:50 -0700236 red_packets.push_back(std::move(red_packet));
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000237 }
Rasmus Brandtc07ebb32016-10-04 10:57:36 +0200238
239 ResetState();
240
brandtr74811e52016-08-10 00:51:50 -0700241 return red_packets;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000242}
243
brandtr869e7cd2016-10-31 05:27:07 -0700244int UlpfecGenerator::Overhead() const {
brandtr74811e52016-08-10 00:51:50 -0700245 RTC_DCHECK(!media_packets_.empty());
246 int num_fec_packets =
Rasmus Brandt78db1582016-09-21 09:19:34 +0200247 fec_->NumFecPackets(media_packets_.size(), params_.fec_rate);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000248 // Return the overhead in Q8.
brandtr74811e52016-08-10 00:51:50 -0700249 return (num_fec_packets << 8) / media_packets_.size();
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000250}
251
brandtr869e7cd2016-10-31 05:27:07 -0700252void UlpfecGenerator::ResetState() {
brandtr74811e52016-08-10 00:51:50 -0700253 media_packets_.clear();
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