OLD | NEW |
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/ulpfec_generator.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 |
(...skipping 23 matching lines...) Expand all Loading... |
45 | 45 |
46 // This threshold is used to adapt the |kMinMediaPackets| threshold, based | 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 | 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 | 48 // packets per frame (as given by this threshold), at least |
49 // |kMinMediaPackets| + 1 packets are sent to the FEC code. | 49 // |kMinMediaPackets| + 1 packets are sent to the FEC code. |
50 constexpr float kMinMediaPacketsAdaptationThreshold = 2.0f; | 50 constexpr float kMinMediaPacketsAdaptationThreshold = 2.0f; |
51 | 51 |
52 } // namespace | 52 } // namespace |
53 | 53 |
54 RedPacket::RedPacket(size_t length) | 54 RedPacket::RedPacket(size_t length) |
55 : data_(new uint8_t[length]), | 55 : data_(new uint8_t[length]), length_(length), header_length_(0) {} |
56 length_(length), | |
57 header_length_(0) { | |
58 } | |
59 | 56 |
60 void RedPacket::CreateHeader(const uint8_t* rtp_header, | 57 void RedPacket::CreateHeader(const uint8_t* rtp_header, |
61 size_t header_length, | 58 size_t header_length, |
62 int red_payload_type, | 59 int red_payload_type, |
63 int payload_type) { | 60 int payload_type) { |
64 RTC_DCHECK_LE(header_length + kRedForFecHeaderLength, length_); | 61 RTC_DCHECK_LE(header_length + kRedForFecHeaderLength, length_); |
65 memcpy(data_.get(), rtp_header, header_length); | 62 memcpy(data_.get(), rtp_header, header_length); |
66 // Replace payload type. | 63 // Replace payload type. |
67 data_[1] &= 0x80; | 64 data_[1] &= 0x80; |
68 data_[1] += red_payload_type; | 65 data_[1] += red_payload_type; |
(...skipping 20 matching lines...) Expand all Loading... |
89 } | 86 } |
90 | 87 |
91 uint8_t* RedPacket::data() const { | 88 uint8_t* RedPacket::data() const { |
92 return data_.get(); | 89 return data_.get(); |
93 } | 90 } |
94 | 91 |
95 size_t RedPacket::length() const { | 92 size_t RedPacket::length() const { |
96 return length_; | 93 return length_; |
97 } | 94 } |
98 | 95 |
99 ProducerFec::ProducerFec() | 96 UlpfecGenerator::UlpfecGenerator() |
100 : fec_(ForwardErrorCorrection::CreateUlpfec()), | 97 : fec_(ForwardErrorCorrection::CreateUlpfec()), |
101 num_protected_frames_(0), | 98 num_protected_frames_(0), |
102 min_num_media_packets_(1) { | 99 min_num_media_packets_(1) { |
103 memset(¶ms_, 0, sizeof(params_)); | 100 memset(¶ms_, 0, sizeof(params_)); |
104 memset(&new_params_, 0, sizeof(new_params_)); | 101 memset(&new_params_, 0, sizeof(new_params_)); |
105 } | 102 } |
106 | 103 |
107 ProducerFec::~ProducerFec() = default; | 104 UlpfecGenerator::~UlpfecGenerator() = default; |
108 | 105 |
109 std::unique_ptr<RedPacket> ProducerFec::BuildRedPacket( | 106 std::unique_ptr<RedPacket> UlpfecGenerator::BuildRedPacket( |
110 const uint8_t* data_buffer, | 107 const uint8_t* data_buffer, |
111 size_t payload_length, | 108 size_t payload_length, |
112 size_t rtp_header_length, | 109 size_t rtp_header_length, |
113 int red_payload_type) { | 110 int red_payload_type) { |
114 std::unique_ptr<RedPacket> red_packet(new RedPacket( | 111 std::unique_ptr<RedPacket> red_packet(new RedPacket( |
115 payload_length + kRedForFecHeaderLength + rtp_header_length)); | 112 payload_length + kRedForFecHeaderLength + rtp_header_length)); |
116 int payload_type = data_buffer[1] & 0x7f; | 113 int payload_type = data_buffer[1] & 0x7f; |
117 red_packet->CreateHeader(data_buffer, rtp_header_length, red_payload_type, | 114 red_packet->CreateHeader(data_buffer, rtp_header_length, red_payload_type, |
118 payload_type); | 115 payload_type); |
119 red_packet->AssignPayload(data_buffer + rtp_header_length, payload_length); | 116 red_packet->AssignPayload(data_buffer + rtp_header_length, payload_length); |
120 return red_packet; | 117 return red_packet; |
121 } | 118 } |
122 | 119 |
123 void ProducerFec::SetFecParameters(const FecProtectionParams* params) { | 120 void UlpfecGenerator::SetFecParameters(const FecProtectionParams* params) { |
124 RTC_DCHECK_GE(params->fec_rate, 0); | 121 RTC_DCHECK_GE(params->fec_rate, 0); |
125 RTC_DCHECK_LE(params->fec_rate, 255); | 122 RTC_DCHECK_LE(params->fec_rate, 255); |
126 // Store the new params and apply them for the next set of FEC packets being | 123 // Store the new params and apply them for the next set of FEC packets being |
127 // produced. | 124 // produced. |
128 new_params_ = *params; | 125 new_params_ = *params; |
129 if (params->fec_rate > kHighProtectionThreshold) { | 126 if (params->fec_rate > kHighProtectionThreshold) { |
130 min_num_media_packets_ = kMinMediaPackets; | 127 min_num_media_packets_ = kMinMediaPackets; |
131 } else { | 128 } else { |
132 min_num_media_packets_ = 1; | 129 min_num_media_packets_ = 1; |
133 } | 130 } |
134 } | 131 } |
135 | 132 |
136 int ProducerFec::AddRtpPacketAndGenerateFec(const uint8_t* data_buffer, | 133 int UlpfecGenerator::AddRtpPacketAndGenerateFec(const uint8_t* data_buffer, |
137 size_t payload_length, | 134 size_t payload_length, |
138 size_t rtp_header_length) { | 135 size_t rtp_header_length) { |
139 RTC_DCHECK(generated_fec_packets_.empty()); | 136 RTC_DCHECK(generated_fec_packets_.empty()); |
140 if (media_packets_.empty()) { | 137 if (media_packets_.empty()) { |
141 params_ = new_params_; | 138 params_ = new_params_; |
142 } | 139 } |
143 bool complete_frame = false; | 140 bool complete_frame = false; |
144 const bool marker_bit = (data_buffer[1] & kRtpMarkerBitMask) ? true : false; | 141 const bool marker_bit = (data_buffer[1] & kRtpMarkerBitMask) ? true : false; |
145 if (media_packets_.size() < kUlpfecMaxMediaPackets) { | 142 if (media_packets_.size() < kUlpfecMaxMediaPackets) { |
146 // Generic FEC can only protect up to |kUlpfecMaxMediaPackets| packets. | 143 // Generic FEC can only protect up to |kUlpfecMaxMediaPackets| packets. |
147 std::unique_ptr<ForwardErrorCorrection::Packet> packet( | 144 std::unique_ptr<ForwardErrorCorrection::Packet> packet( |
148 new ForwardErrorCorrection::Packet()); | 145 new ForwardErrorCorrection::Packet()); |
(...skipping 19 matching lines...) Expand all Loading... |
168 kNumImportantPackets, kUseUnequalProtection, | 165 kNumImportantPackets, kUseUnequalProtection, |
169 params_.fec_mask_type, &generated_fec_packets_); | 166 params_.fec_mask_type, &generated_fec_packets_); |
170 if (generated_fec_packets_.empty()) { | 167 if (generated_fec_packets_.empty()) { |
171 ResetState(); | 168 ResetState(); |
172 } | 169 } |
173 return ret; | 170 return ret; |
174 } | 171 } |
175 return 0; | 172 return 0; |
176 } | 173 } |
177 | 174 |
178 bool ProducerFec::ExcessOverheadBelowMax() const { | 175 bool UlpfecGenerator::ExcessOverheadBelowMax() const { |
179 return ((Overhead() - params_.fec_rate) < kMaxExcessOverhead); | 176 return ((Overhead() - params_.fec_rate) < kMaxExcessOverhead); |
180 } | 177 } |
181 | 178 |
182 bool ProducerFec::MinimumMediaPacketsReached() const { | 179 bool UlpfecGenerator::MinimumMediaPacketsReached() const { |
183 float average_num_packets_per_frame = | 180 float average_num_packets_per_frame = |
184 static_cast<float>(media_packets_.size()) / num_protected_frames_; | 181 static_cast<float>(media_packets_.size()) / num_protected_frames_; |
185 int num_media_packets = static_cast<int>(media_packets_.size()); | 182 int num_media_packets = static_cast<int>(media_packets_.size()); |
186 if (average_num_packets_per_frame < kMinMediaPacketsAdaptationThreshold) { | 183 if (average_num_packets_per_frame < kMinMediaPacketsAdaptationThreshold) { |
187 return num_media_packets >= min_num_media_packets_; | 184 return num_media_packets >= min_num_media_packets_; |
188 } else { | 185 } else { |
189 // For larger rates (more packets/frame), increase the threshold. | 186 // For larger rates (more packets/frame), increase the threshold. |
190 // TODO(brandtr): Investigate what impact this adaptation has. | 187 // TODO(brandtr): Investigate what impact this adaptation has. |
191 return num_media_packets >= min_num_media_packets_ + 1; | 188 return num_media_packets >= min_num_media_packets_ + 1; |
192 } | 189 } |
193 } | 190 } |
194 | 191 |
195 bool ProducerFec::FecAvailable() const { | 192 bool UlpfecGenerator::FecAvailable() const { |
196 return !generated_fec_packets_.empty(); | 193 return !generated_fec_packets_.empty(); |
197 } | 194 } |
198 | 195 |
199 size_t ProducerFec::NumAvailableFecPackets() const { | 196 size_t UlpfecGenerator::NumAvailableFecPackets() const { |
200 return generated_fec_packets_.size(); | 197 return generated_fec_packets_.size(); |
201 } | 198 } |
202 | 199 |
203 size_t ProducerFec::MaxPacketOverhead() const { | 200 size_t UlpfecGenerator::MaxPacketOverhead() const { |
204 return fec_->MaxPacketOverhead(); | 201 return fec_->MaxPacketOverhead(); |
205 } | 202 } |
206 | 203 |
207 std::vector<std::unique_ptr<RedPacket>> ProducerFec::GetUlpfecPacketsAsRed( | 204 std::vector<std::unique_ptr<RedPacket>> UlpfecGenerator::GetUlpfecPacketsAsRed( |
208 int red_payload_type, | 205 int red_payload_type, |
209 int ulpfec_payload_type, | 206 int ulpfec_payload_type, |
210 uint16_t first_seq_num, | 207 uint16_t first_seq_num, |
211 size_t rtp_header_length) { | 208 size_t rtp_header_length) { |
212 std::vector<std::unique_ptr<RedPacket>> red_packets; | 209 std::vector<std::unique_ptr<RedPacket>> red_packets; |
213 red_packets.reserve(generated_fec_packets_.size()); | 210 red_packets.reserve(generated_fec_packets_.size()); |
214 RTC_DCHECK(!media_packets_.empty()); | 211 RTC_DCHECK(!media_packets_.empty()); |
215 ForwardErrorCorrection::Packet* last_media_packet = | 212 ForwardErrorCorrection::Packet* last_media_packet = |
216 media_packets_.back().get(); | 213 media_packets_.back().get(); |
217 uint16_t seq_num = first_seq_num; | 214 uint16_t seq_num = first_seq_num; |
218 for (const auto& fec_packet : generated_fec_packets_) { | 215 for (const auto& fec_packet : generated_fec_packets_) { |
219 // Wrap FEC packet (including FEC headers) in a RED packet. Since the | 216 // Wrap FEC packet (including FEC headers) in a RED packet. Since the |
220 // FEC packets in |generated_fec_packets_| don't have RTP headers, we | 217 // FEC packets in |generated_fec_packets_| don't have RTP headers, we |
221 // reuse the header from the last media packet. | 218 // reuse the header from the last media packet. |
222 std::unique_ptr<RedPacket> red_packet(new RedPacket( | 219 std::unique_ptr<RedPacket> red_packet(new RedPacket( |
223 fec_packet->length + kRedForFecHeaderLength + rtp_header_length)); | 220 fec_packet->length + kRedForFecHeaderLength + rtp_header_length)); |
224 red_packet->CreateHeader(last_media_packet->data, rtp_header_length, | 221 red_packet->CreateHeader(last_media_packet->data, rtp_header_length, |
225 red_payload_type, ulpfec_payload_type); | 222 red_payload_type, ulpfec_payload_type); |
226 red_packet->SetSeqNum(seq_num++); | 223 red_packet->SetSeqNum(seq_num++); |
227 red_packet->ClearMarkerBit(); | 224 red_packet->ClearMarkerBit(); |
228 red_packet->AssignPayload(fec_packet->data, fec_packet->length); | 225 red_packet->AssignPayload(fec_packet->data, fec_packet->length); |
229 red_packets.push_back(std::move(red_packet)); | 226 red_packets.push_back(std::move(red_packet)); |
230 } | 227 } |
231 | 228 |
232 ResetState(); | 229 ResetState(); |
233 | 230 |
234 return red_packets; | 231 return red_packets; |
235 } | 232 } |
236 | 233 |
237 int ProducerFec::Overhead() const { | 234 int UlpfecGenerator::Overhead() const { |
238 RTC_DCHECK(!media_packets_.empty()); | 235 RTC_DCHECK(!media_packets_.empty()); |
239 int num_fec_packets = | 236 int num_fec_packets = |
240 fec_->NumFecPackets(media_packets_.size(), params_.fec_rate); | 237 fec_->NumFecPackets(media_packets_.size(), params_.fec_rate); |
241 // Return the overhead in Q8. | 238 // Return the overhead in Q8. |
242 return (num_fec_packets << 8) / media_packets_.size(); | 239 return (num_fec_packets << 8) / media_packets_.size(); |
243 } | 240 } |
244 | 241 |
245 void ProducerFec::ResetState() { | 242 void UlpfecGenerator::ResetState() { |
246 media_packets_.clear(); | 243 media_packets_.clear(); |
247 generated_fec_packets_.clear(); | 244 generated_fec_packets_.clear(); |
248 num_protected_frames_ = 0; | 245 num_protected_frames_ = 0; |
249 } | 246 } |
250 | 247 |
251 } // namespace webrtc | 248 } // namespace webrtc |
OLD | NEW |