Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(290)

Side by Side Diff: webrtc/modules/rtp_rtcp/source/producer_fec.cc

Issue 2305793003: Simplify public interface of ProducerFec. (Closed)
Patch Set: Fix fuzzer. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/rtp_rtcp/source/producer_fec.h" 11 #include "webrtc/modules/rtp_rtcp/source/producer_fec.h"
12 12
13 #include <memory> 13 #include <memory>
14 #include <utility> 14 #include <utility>
15 15
16 #include "webrtc/base/basictypes.h" 16 #include "webrtc/base/basictypes.h"
17 #include "webrtc/base/checks.h" 17 #include "webrtc/base/checks.h"
18 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" 18 #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
19 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h" 19 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h"
20 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" 20 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
21 21
22 namespace webrtc { 22 namespace webrtc {
23 23
24 namespace {
25
24 constexpr size_t kRedForFecHeaderLength = 1; 26 constexpr size_t kRedForFecHeaderLength = 1;
25 27
26 // This controls the maximum amount of excess overhead (actual - target) 28 // This controls the maximum amount of excess overhead (actual - target)
27 // allowed in order to trigger EncodeFec(), before |params_.max_fec_frames| 29 // allowed in order to trigger EncodeFec(), before |params_.max_fec_frames|
28 // is reached. Overhead here is defined as relative to number of media packets. 30 // is reached. Overhead here is defined as relative to number of media packets.
29 constexpr int kMaxExcessOverhead = 50; // Q8. 31 constexpr int kMaxExcessOverhead = 50; // Q8.
30 32
31 // This is the minimum number of media packets required (above some protection 33 // This is the minimum number of media packets required (above some protection
32 // level) in order to trigger EncodeFec(), before |params_.max_fec_frames| is 34 // level) in order to trigger EncodeFec(), before |params_.max_fec_frames| is
33 // reached. 35 // reached.
34 constexpr size_t kMinMediaPackets = 4; 36 constexpr size_t kMinMediaPackets = 4;
35 37
36 // Threshold on the received FEC protection level, above which we enforce at 38 // Threshold on the received FEC protection level, above which we enforce at
37 // least |kMinMediaPackets| packets for the FEC code. Below this 39 // least |kMinMediaPackets| packets for the FEC code. Below this
38 // threshold |kMinMediaPackets| is set to default value of 1. 40 // threshold |kMinMediaPackets| is set to default value of 1.
39 // 41 //
40 // The range is between 0 and 255, where 255 corresponds to 100% overhead 42 // The range is between 0 and 255, where 255 corresponds to 100% overhead
41 // (relative to the number of protected media packets). 43 // (relative to the number of protected media packets).
42 constexpr uint8_t kHighProtectionThreshold = 80; 44 constexpr uint8_t kHighProtectionThreshold = 80;
43 45
44 // This threshold is used to adapt the |kMinMediaPackets| threshold, based 46 // This threshold is used to adapt the |kMinMediaPackets| threshold, based
45 // on the average number of packets per frame seen so far. When there are few 47 // on the average number of packets per frame seen so far. When there are few
46 // packets per frame (as given by this threshold), at least 48 // packets per frame (as given by this threshold), at least
47 // |kMinMediaPackets| + 1 packets are sent to the FEC code. 49 // |kMinMediaPackets| + 1 packets are sent to the FEC code.
48 constexpr float kMinMediaPacketsAdaptationThreshold = 2.0f; 50 constexpr float kMinMediaPacketsAdaptationThreshold = 2.0f;
49 51
52 } // namespace
53
50 RedPacket::RedPacket(size_t length) 54 RedPacket::RedPacket(size_t length)
51 : data_(new uint8_t[length]), 55 : data_(new uint8_t[length]),
52 length_(length), 56 length_(length),
53 header_length_(0) { 57 header_length_(0) {
54 } 58 }
55 59
56 void RedPacket::CreateHeader(const uint8_t* rtp_header, 60 void RedPacket::CreateHeader(const uint8_t* rtp_header,
57 size_t header_length, 61 size_t header_length,
58 int red_payload_type, 62 int red_payload_type,
59 int payload_type) { 63 int payload_type) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 98
95 ProducerFec::ProducerFec() 99 ProducerFec::ProducerFec()
96 : fec_(ForwardErrorCorrection::CreateUlpfec()), 100 : fec_(ForwardErrorCorrection::CreateUlpfec()),
97 num_protected_frames_(0), 101 num_protected_frames_(0),
98 num_important_packets_(0), 102 num_important_packets_(0),
99 min_num_media_packets_(1) { 103 min_num_media_packets_(1) {
100 memset(&params_, 0, sizeof(params_)); 104 memset(&params_, 0, sizeof(params_));
101 memset(&new_params_, 0, sizeof(new_params_)); 105 memset(&new_params_, 0, sizeof(new_params_));
102 } 106 }
103 107
104 ProducerFec::~ProducerFec() { 108 ProducerFec::~ProducerFec() = default;
105 DeleteMediaPackets();
106 }
107 109
108 std::unique_ptr<RedPacket> ProducerFec::BuildRedPacket( 110 std::unique_ptr<RedPacket> ProducerFec::BuildRedPacket(
109 const uint8_t* data_buffer, 111 const uint8_t* data_buffer,
110 size_t payload_length, 112 size_t payload_length,
111 size_t rtp_header_length, 113 size_t rtp_header_length,
112 int red_payload_type) { 114 int red_payload_type) {
113 std::unique_ptr<RedPacket> red_packet(new RedPacket( 115 std::unique_ptr<RedPacket> red_packet(new RedPacket(
114 payload_length + kRedForFecHeaderLength + rtp_header_length)); 116 payload_length + kRedForFecHeaderLength + rtp_header_length));
115 int payload_type = data_buffer[1] & 0x7f; 117 int payload_type = data_buffer[1] & 0x7f;
116 red_packet->CreateHeader(data_buffer, rtp_header_length, red_payload_type, 118 red_packet->CreateHeader(data_buffer, rtp_header_length, red_payload_type,
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 // TODO(pbos): Consider whether unequal protection should be enabled or not, 174 // TODO(pbos): Consider whether unequal protection should be enabled or not,
173 // it is currently always disabled. 175 // it is currently always disabled.
174 // 176 //
175 // Since unequal protection is disabled, the value of 177 // Since unequal protection is disabled, the value of
176 // |num_important_packets_| has no importance when calling GenerateFec(). 178 // |num_important_packets_| has no importance when calling GenerateFec().
177 constexpr bool kUseUnequalProtection = false; 179 constexpr bool kUseUnequalProtection = false;
178 int ret = fec_->EncodeFec(media_packets_, params_.fec_rate, 180 int ret = fec_->EncodeFec(media_packets_, params_.fec_rate,
179 num_important_packets_, kUseUnequalProtection, 181 num_important_packets_, kUseUnequalProtection,
180 params_.fec_mask_type, &generated_fec_packets_); 182 params_.fec_mask_type, &generated_fec_packets_);
181 if (generated_fec_packets_.empty()) { 183 if (generated_fec_packets_.empty()) {
182 num_protected_frames_ = 0; 184 ResetState();
183 DeleteMediaPackets();
184 } 185 }
185 return ret; 186 return ret;
186 } 187 }
187 return 0; 188 return 0;
188 } 189 }
189 190
190 bool ProducerFec::ExcessOverheadBelowMax() const { 191 bool ProducerFec::ExcessOverheadBelowMax() const {
191 return ((Overhead() - params_.fec_rate) < kMaxExcessOverhead); 192 return ((Overhead() - params_.fec_rate) < kMaxExcessOverhead);
192 } 193 }
193 194
(...skipping 15 matching lines...) Expand all
209 } 210 }
210 211
211 size_t ProducerFec::NumAvailableFecPackets() const { 212 size_t ProducerFec::NumAvailableFecPackets() const {
212 return generated_fec_packets_.size(); 213 return generated_fec_packets_.size();
213 } 214 }
214 215
215 size_t ProducerFec::MaxPacketOverhead() const { 216 size_t ProducerFec::MaxPacketOverhead() const {
216 return fec_->MaxPacketOverhead(); 217 return fec_->MaxPacketOverhead();
217 } 218 }
218 219
219 std::vector<std::unique_ptr<RedPacket>> ProducerFec::GetFecPacketsAsRed( 220 std::vector<std::unique_ptr<RedPacket>> ProducerFec::GetUlpfecPacketsAsRed(
220 int red_payload_type, 221 int red_payload_type,
221 int ulpfec_payload_type, 222 int ulpfec_payload_type,
222 uint16_t first_seq_num, 223 uint16_t first_seq_num,
223 size_t rtp_header_length) { 224 size_t rtp_header_length) {
224 std::vector<std::unique_ptr<RedPacket>> red_packets; 225 std::vector<std::unique_ptr<RedPacket>> red_packets;
225 red_packets.reserve(generated_fec_packets_.size()); 226 red_packets.reserve(generated_fec_packets_.size());
226 RTC_DCHECK(!media_packets_.empty()); 227 RTC_DCHECK(!media_packets_.empty());
227 ForwardErrorCorrection::Packet* last_media_packet = 228 ForwardErrorCorrection::Packet* last_media_packet =
228 media_packets_.back().get(); 229 media_packets_.back().get();
229 uint16_t seq_num = first_seq_num; 230 uint16_t seq_num = first_seq_num;
230 for (const auto& fec_packet : generated_fec_packets_) { 231 for (const auto& fec_packet : generated_fec_packets_) {
231 // Wrap FEC packet (including FEC headers) in a RED packet. Since the 232 // Wrap FEC packet (including FEC headers) in a RED packet. Since the
232 // FEC packets in |generated_fec_packets_| don't have RTP headers, we 233 // FEC packets in |generated_fec_packets_| don't have RTP headers, we
233 // reuse the header from the last media packet. 234 // reuse the header from the last media packet.
234 std::unique_ptr<RedPacket> red_packet(new RedPacket( 235 std::unique_ptr<RedPacket> red_packet(new RedPacket(
235 fec_packet->length + kRedForFecHeaderLength + rtp_header_length)); 236 fec_packet->length + kRedForFecHeaderLength + rtp_header_length));
236 red_packet->CreateHeader(last_media_packet->data, rtp_header_length, 237 red_packet->CreateHeader(last_media_packet->data, rtp_header_length,
237 red_payload_type, ulpfec_payload_type); 238 red_payload_type, ulpfec_payload_type);
238 red_packet->SetSeqNum(seq_num++); 239 red_packet->SetSeqNum(seq_num++);
239 red_packet->ClearMarkerBit(); 240 red_packet->ClearMarkerBit();
240 red_packet->AssignPayload(fec_packet->data, fec_packet->length); 241 red_packet->AssignPayload(fec_packet->data, fec_packet->length);
241 red_packets.push_back(std::move(red_packet)); 242 red_packets.push_back(std::move(red_packet));
242 } 243 }
243 // Reset state. 244
244 DeleteMediaPackets(); 245 ResetState();
245 generated_fec_packets_.clear(); 246
246 num_protected_frames_ = 0;
247 return red_packets; 247 return red_packets;
248 } 248 }
249 249
250 int ProducerFec::Overhead() const { 250 int ProducerFec::Overhead() const {
251 // Overhead is defined as relative to the number of media packets, and not
252 // relative to total number of packets. This definition is inherited from the
253 // protection factor produced by video_coding module and how the FEC
254 // generation is implemented.
255 RTC_DCHECK(!media_packets_.empty()); 251 RTC_DCHECK(!media_packets_.empty());
256 int num_fec_packets = 252 int num_fec_packets =
257 fec_->NumFecPackets(media_packets_.size(), params_.fec_rate); 253 fec_->NumFecPackets(media_packets_.size(), params_.fec_rate);
258 // Return the overhead in Q8. 254 // Return the overhead in Q8.
259 return (num_fec_packets << 8) / media_packets_.size(); 255 return (num_fec_packets << 8) / media_packets_.size();
260 } 256 }
261 257
262 void ProducerFec::DeleteMediaPackets() { 258 void ProducerFec::ResetState() {
263 media_packets_.clear(); 259 media_packets_.clear();
260 generated_fec_packets_.clear();
261 num_protected_frames_ = 0;
264 } 262 }
265 263
266 } // namespace webrtc 264 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/rtp_rtcp/source/producer_fec.h ('k') | webrtc/modules/rtp_rtcp/source/producer_fec_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698