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

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

Issue 2269903002: Add FlexFEC header formatters. (pt. 5) (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@header_reader_writer-pt4-generalize_header_formatting
Patch Set: Changes based on comments from earlier patch set. Created 4 years, 3 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
(Empty)
1 /*
2 * Copyright (c) 2016 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
11 #include "webrtc/modules/rtp_rtcp/source/flexfec_header_reader_writer.h"
12
13 #include <string.h>
14
15 #include <utility>
16
17 #include "webrtc/base/checks.h"
18 #include "webrtc/base/logging.h"
19 #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
20 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction_internal.h"
21
22 namespace webrtc {
23
24 namespace {
25
26 // Maximum number of media packets that can be protected in one batch.
27 constexpr size_t kMaxMediaPackets = 48; // Since we are reusing ULPFEC masks.
28
29 // Maximum number of FEC packets stored inside ForwardErrorCorrection.
30 constexpr size_t kMaxFecPackets = kMaxMediaPackets;
31
32 // Size (in bytes) of packet masks, given number of K bits set.
33 constexpr size_t kFlexfecPacketMaskSizes[] = {2, 6, 14};
34
35 // Size (in bytes) of part of header which is not packet mask specific.
36 constexpr size_t kBaseHeaderSize = 12;
37
38 // Size (in bytes) of part of header which is stream specific.
39 constexpr size_t kStreamSpecificHeaderSize = 6;
40
41 // Size (in bytes) of header, given the single stream packet mask size, i.e.
42 // the number of K-bits set.
43 constexpr size_t kHeaderSizes[] = {
44 kBaseHeaderSize + kStreamSpecificHeaderSize + kFlexfecPacketMaskSizes[0],
45 kBaseHeaderSize + kStreamSpecificHeaderSize + kFlexfecPacketMaskSizes[1],
46 kBaseHeaderSize + kStreamSpecificHeaderSize + kFlexfecPacketMaskSizes[2]};
47
48 // We currently only support single-stream protection.
49 // TODO(brandtr): Update this when we support multistream protection.
50 constexpr uint8_t kSsrcCount = 1;
51
52 // There are three reserved bytes that MUST be set to zero in the header.
53 constexpr uint32_t kReservedBits = 0;
54
55 // TODO(brandtr): Update this when we support multistream protection.
56 constexpr size_t kPacketMaskOffset =
57 kBaseHeaderSize + kStreamSpecificHeaderSize;
58
59 // Here we count the K-bits as belonging to the packet mask.
60 // This can be used in conjunction with FlexfecHeaderWriter::MinPacketMaskSize,
61 // which calculates a bound on the needed packet mask size including K-bits,
62 // given a packet mask without K-bits.
63 size_t FlexfecHeaderSize(size_t packet_mask_size) {
64 RTC_DCHECK_LE(packet_mask_size, kFlexfecPacketMaskSizes[2]);
65 if (packet_mask_size <= kFlexfecPacketMaskSizes[0]) {
66 return kHeaderSizes[0];
67 } else if (packet_mask_size <= kFlexfecPacketMaskSizes[1]) {
68 return kHeaderSizes[1];
69 }
70 return kHeaderSizes[2];
71 }
72
73 } // namespace
74
75 FlexfecHeaderReader::FlexfecHeaderReader()
76 : FecHeaderReader(kMaxMediaPackets, kMaxFecPackets) {}
77
78 FlexfecHeaderReader::~FlexfecHeaderReader() = default;
79
80 // TODO(brandtr): Update this function when we support flexible masks,
81 // retransmissions, and/or several protected SSRCs.
82 bool FlexfecHeaderReader::ReadFecHeader(
83 ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const {
84 if (fec_packet->pkt->length <= kBaseHeaderSize + kStreamSpecificHeaderSize) {
85 LOG(LS_INFO) << "Discarding truncated FlexFEC packet.";
86 return false;
87 }
88 bool f_bit = (fec_packet->pkt->data[0] & 0x80) != 0;
89 if (f_bit) {
90 LOG(LS_INFO) << "FlexFEC packet with inflexible generator matrix. We do "
91 "not yet support this, thus discarding packet.";
92 return false;
93 }
94 bool r_bit = (fec_packet->pkt->data[0] & 0x40) != 0;
95 if (r_bit) {
96 LOG(LS_INFO) << "FlexFEC packet with retransmission bit set. We do not yet "
97 "support this, thus discarding the packet.";
98 return false;
99 }
100 uint8_t ssrc_count =
101 ByteReader<uint8_t>::ReadBigEndian(&fec_packet->pkt->data[8]);
102 if (ssrc_count != 1) {
103 LOG(LS_INFO) << "FlexFEC packet protecting multiple media SSRCs. We do not "
104 "yet support this, thus discarding packet.";
105 return false;
106 }
107 uint32_t protected_ssrc =
108 ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[12]);
109 uint16_t seq_num_base =
110 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[16]);
111
112 // Read packet mask and adapt it by removing the interleaved K bits.
113 // This destroys the FlexFEC standards compliance of the packet masks,
114 // but makes it compatible with the ULPFEC masks.
stefan-webrtc 2016/09/28 07:53:29 It's not entirely clear to me how severe this comp
brandtr 2016/09/28 09:11:31 It's not a problem, since this parsed packet will
115 // TODO(brandtr): Store the adapted pack mask out-of-band, when the
116 // FEC packet classes have been refactored.
117 //
118 // We treat the mask parts as unsigned integers with host order endianness
119 // in order to simplify the bit shifting between bytes.
120 if (fec_packet->pkt->length < kHeaderSizes[0]) {
121 LOG(LS_INFO) << "Discarding truncated FlexFEC packet.";
122 return false;
123 }
124 uint8_t* const packet_mask = fec_packet->pkt->data + kPacketMaskOffset;
125 bool k_bit0 = (packet_mask[0] & 0x80) != 0;
126 uint16_t mask_part0 = ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
127 mask_part0 <<= 1; // Shift away K-bit 0 from adapted mask.
128 ByteWriter<uint16_t>::WriteBigEndian(&packet_mask[0], mask_part0);
129 size_t packet_mask_size;
130 if (k_bit0) {
131 packet_mask_size = kFlexfecPacketMaskSizes[0];
132 } else {
133 if (fec_packet->pkt->length < kHeaderSizes[1]) {
134 return false;
135 }
136 bool k_bit1 = (packet_mask[2] & 0x80) != 0;
137 bool bit15 = (packet_mask[2] & 0x40) != 0;
138 if (bit15) {
139 packet_mask[1] |= 0x01; // Set bit 15 in adapted mask.
140 }
141 uint32_t mask_part1 = ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]);
142 mask_part1 <<= 2; // Shift away K-bit 1 and bit 15 from adapted mask.
143 ByteWriter<uint32_t>::WriteBigEndian(&packet_mask[2], mask_part1);
144 if (k_bit1) {
stefan-webrtc 2016/09/28 07:53:29 It's a bit hard to follow what the k_bit1 being se
brandtr 2016/09/28 09:11:32 Done.
145 packet_mask_size = kFlexfecPacketMaskSizes[1];
146 } else {
147 if (fec_packet->pkt->length < kHeaderSizes[2]) {
148 LOG(LS_INFO) << "Discarding truncated FlexFEC packet.";
stefan-webrtc 2016/09/28 07:53:29 Is this perhaps worth a warning?
brandtr 2016/09/28 09:11:32 Done.
149 return false;
150 }
151 bool k_bit2 = (packet_mask[6] & 0x80) != 0;
152 if (k_bit2) {
stefan-webrtc 2016/09/28 07:53:29 I'd like a comment here too
brandtr 2016/09/28 09:11:31 Done.
153 packet_mask_size = kFlexfecPacketMaskSizes[2];
154 } else {
155 LOG(LS_INFO) << "Discarding FlexFEC packet with malformed header.";
stefan-webrtc 2016/09/28 07:53:29 Same here, perhaps warn?
brandtr 2016/09/28 09:11:32 Done.
156 return false;
157 }
158 // Copy bits 46 and 47.
stefan-webrtc 2016/09/28 07:53:29 Are you copying them from byte 6 to byte 5 of the
brandtr 2016/09/28 09:11:32 Done.
159 uint8_t tail_bits = (packet_mask[6] >> 5) & 0x03;
160 packet_mask[5] |= tail_bits;
161 uint64_t mask_part2 =
162 ByteReader<uint64_t>::ReadBigEndian(&packet_mask[6]);
163 // Shift away K-bit 2, bit 46, and bit 47 from adapted mask.
stefan-webrtc 2016/09/28 07:53:29 Same here I guess. An option would be to have a ge
brandtr 2016/09/28 09:11:31 Done.
164 mask_part2 <<= 3;
165 ByteWriter<uint64_t>::WriteBigEndian(&packet_mask[6], mask_part2);
166 }
167 }
168
169 // Store "ULPFECized" packet mask info.
170 fec_packet->fec_header_size = FlexfecHeaderSize(packet_mask_size);
171 fec_packet->protected_ssrc = protected_ssrc;
172 fec_packet->seq_num_base = seq_num_base;
173 fec_packet->packet_mask_offset = kPacketMaskOffset;
174 fec_packet->packet_mask_size = packet_mask_size;
175
176 // In FlexFEC, all media packets are protected in their entirety.
177 fec_packet->protection_length =
178 fec_packet->pkt->length - fec_packet->fec_header_size;
179
180 return true;
181 }
182
183 FlexfecHeaderWriter::FlexfecHeaderWriter()
184 : FecHeaderWriter(kMaxMediaPackets, kMaxFecPackets, kHeaderSizes[2]) {}
185
186 FlexfecHeaderWriter::~FlexfecHeaderWriter() = default;
187
188 size_t FlexfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask,
189 size_t packet_mask_size) const {
190 if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear &&
stefan-webrtc 2016/09/28 07:53:29 I don't follow why we have ulpfec constants here.
brandtr 2016/09/28 09:11:31 For the time being, I call our pregenerated packet
191 ((packet_mask[1] & 0x01) == 0)) {
stefan-webrtc 2016/09/28 07:53:29 You can remove () around the equality check, here
brandtr 2016/09/28 09:11:32 Done.
192 // Packet mask is 16 bits long, with bit 15 clear.
193 // It can be used as is.
194 return kFlexfecPacketMaskSizes[0];
195 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
196 // Packet mask is 16 bits long, with bit 15 set.
197 // We must expand the packet mask with zeros in the FlexFEC header.
198 return kFlexfecPacketMaskSizes[1];
199 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet &&
200 ((packet_mask[5] & 0x03) == 0)) {
201 // Packet mask is 48 bits long, with bits 46 and 47 clear.
202 // It can be used as is.
203 return kFlexfecPacketMaskSizes[1];
204 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) {
205 // Packet mask is 48 bits long, with at least one of bits 46 and 47 set.
206 // We must expand it with zeros.
207 return kFlexfecPacketMaskSizes[2];
208 }
209 RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size << ".";
210 return kFlexfecPacketMaskSizes[2];
211 }
212
213 size_t FlexfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const {
214 return FlexfecHeaderSize(packet_mask_size);
215 }
216
217 // This function adapts the precomputed ULPFEC packet masks to the
218 // FlexFEC header standard. Note that the header size is computed by
219 // FecHeaderSize(), so in this function we can be sure that we are
220 // writing in space that is intended for the header.
221 //
222 // TODO(brandtr): Update this function when we support offset-based masks,
223 // retransmissions, and protecting multiple SSRCs.
224 void FlexfecHeaderWriter::FinalizeFecHeader(
225 uint32_t media_ssrc,
226 uint16_t seq_num_base,
227 const uint8_t* packet_mask,
228 size_t packet_mask_size,
229 ForwardErrorCorrection::Packet* fec_packet) const {
230 fec_packet->data[0] &= 0x7f; // Clear F bit.
231 fec_packet->data[0] &= 0xbf; // Clear R bit.
232 ByteWriter<uint8_t>::WriteBigEndian(&fec_packet->data[8], kSsrcCount);
233 ByteWriter<uint32_t, 3>::WriteBigEndian(&fec_packet->data[9], kReservedBits);
234 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[12], media_ssrc);
235 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[16], seq_num_base);
236 // Adapt ULPFEC packet mask to FlexFEC header.
237 //
238 // We treat the mask parts as unsigned integers with host order endianness
239 // in order to simplify the bit shifting between bytes.
240 uint8_t* const written_packet_mask = fec_packet->data + kPacketMaskOffset;
241 if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) {
242 // The packet mask is 48 bits long.
243 uint16_t tmp_mask_part0 =
244 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
245 uint32_t tmp_mask_part1 =
246 ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]);
247
248 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0.
249 ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0],
250 tmp_mask_part0);
251 tmp_mask_part1 >>= 2; // Shift, thus clearing K-bit 1 and bit 15.
252 ByteWriter<uint32_t>::WriteBigEndian(&written_packet_mask[2],
253 tmp_mask_part1);
254 bool bit15 = (packet_mask[1] & 0x01) != 0;
255 if (bit15) {
stefan-webrtc 2016/09/28 07:53:29 Remove {}
brandtr 2016/09/28 09:11:32 Done.
256 written_packet_mask[2] |= 0x40; // Set bit 15.
257 }
258 bool bit46 = (packet_mask[5] & 0x02) != 0;
259 bool bit47 = (packet_mask[5] & 0x01) != 0;
260 if (!bit46 && !bit47) {
261 written_packet_mask[2] |= 0x80; // Set K-bit 1.
262 } else {
263 memset(&written_packet_mask[6], 0, 8); // Clear all trailing bits.
264 written_packet_mask[6] |= 0x80; // Set K-bit 2.
265 if (bit46) {
stefan-webrtc 2016/09/28 07:53:29 Remove {}, and below.
brandtr 2016/09/28 09:11:31 Done.
266 written_packet_mask[6] |= 0x40; // Set bit 46.
267 }
268 if (bit47) {
269 written_packet_mask[6] |= 0x20; // Set bit 47.
270 }
271 }
272 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
273 // The packet mask is 16 bits long.
274 uint16_t tmp_mask_part0 =
275 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
276
277 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0.
278 ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0],
279 tmp_mask_part0);
280 bool bit15 = (packet_mask[1] & 0x01) != 0;
281 if (!bit15) {
282 written_packet_mask[0] |= 0x80; // Set K-bit 0.
283 } else {
284 memset(&written_packet_mask[2], 0U, 4); // Clear all trailing bits.
285 written_packet_mask[2] |= 0x80; // Set K-bit 1.
286 written_packet_mask[2] |= 0x40; // Set bit 15.
287 }
288 } else {
289 RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size
290 << ".";
291 }
292 }
293
294 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698