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

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: Corresponding fixes. 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/modules/rtp_rtcp/source/byte_io.h"
19 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction_internal.h"
20
21 namespace webrtc {
22
23 namespace {
24
25 // Maximum number of media packets that can be protected in one batch.
26 constexpr size_t kMaxMediaPackets = 48; // Since we are reusing ULPFEC masks.
27
28 // Maximum number of FEC packets stored inside ForwardErrorCorrection.
29 constexpr size_t kMaxFecPackets = kMaxMediaPackets;
30
31 // Size (in bytes) of packet masks, given number of K bits set.
32 constexpr size_t kFlexfecPacketMaskSize0KBitsSet = 2;
33 constexpr size_t kFlexfecPacketMaskSize1KBitsSet = 6;
34 constexpr size_t kFlexfecPacketMaskSize2KBitsSet = 14;
35
36 // Size (in bytes) of part of header which is not packet mask specific.
37 constexpr size_t kBaseHeaderSize = 12;
38
39 // Size (in bytes) of part of header which is stream specific.
40 constexpr size_t kStreamSpecificHeaderSize = 6;
41
42 // Size (in bytes) of header, given the single stream packet mask size, i.e.
43 // the number of K-bits set.
44 constexpr size_t kHeaderSize0KBitsSet = kBaseHeaderSize +
45 kStreamSpecificHeaderSize +
46 kFlexfecPacketMaskSize0KBitsSet;
47 constexpr size_t kHeaderSize1KBitsSet = kBaseHeaderSize +
48 kStreamSpecificHeaderSize +
49 kFlexfecPacketMaskSize1KBitsSet;
50 constexpr size_t kHeaderSize2KBitsSet = kBaseHeaderSize +
51 kStreamSpecificHeaderSize +
52 kFlexfecPacketMaskSize2KBitsSet;
53
54 // TODO(brandtr): Update this when we support multistream protection.
55 constexpr size_t kPacketMaskOffset =
56 kBaseHeaderSize + kStreamSpecificHeaderSize;
57
58 // Here we count the K-bits as belonging to the packet mask.
59 // This can be used in conjunction with FlexfecHeaderWriter::MinPacketMaskSize,
60 // which calculates a bound on the needed packet mask size including K-bits,
61 // given a packet mask without K-bits.
62 size_t FlexfecHeaderSize(size_t packet_mask_size) {
63 RTC_DCHECK_LE(packet_mask_size, kFlexfecPacketMaskSize2KBitsSet);
64 if (packet_mask_size <= kFlexfecPacketMaskSize0KBitsSet) {
65 return kHeaderSize0KBitsSet;
66 } else if (packet_mask_size <= kFlexfecPacketMaskSize1KBitsSet) {
67 return kHeaderSize1KBitsSet;
68 }
69 return kHeaderSize2KBitsSet;
70 }
71
72 } // namespace
73
74 FlexfecHeaderReader::FlexfecHeaderReader()
75 : FecHeaderReader(kMaxMediaPackets, kMaxFecPackets) {}
76
77 FlexfecHeaderReader::~FlexfecHeaderReader() = default;
78
79 // TODO(brandtr): Update this function when we support flexible masks,
80 // retransmissions, and/or several protected SSRCs.
81 bool FlexfecHeaderReader::ReadFecHeader(
82 ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const {
83 if (fec_packet->pkt->length <= kBaseHeaderSize + kStreamSpecificHeaderSize) {
84 // Not enough space for the packet mask.
danilchap 2016/09/12 13:30:29 may be turn this comment into log.
brandtr 2016/09/14 11:45:53 Done.
85 return false;
86 }
87 bool f_bit = (fec_packet->pkt->data[0] & 0x80) != 0;
88 if (f_bit) {
89 return false;
90 }
91 bool r_bit = (fec_packet->pkt->data[0] & 0x40) != 0;
92 if (r_bit) {
93 return false;
94 }
95 uint8_t ssrc_count =
96 ByteReader<uint8_t>::ReadBigEndian(&fec_packet->pkt->data[8]);
97 if (ssrc_count != 1) {
98 return false;
99 }
100 uint32_t protected_ssrc =
101 ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[12]);
102 uint16_t seq_num_base =
103 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[16]);
104
105 // Read packet mask and adapt it by removing the interleaved K bits.
106 // This destroys the FlexFEC standards compliance of the packet masks,
107 // but makes it compatible with the ULPFEC masks.
108 // TODO(brandtr): Store the adapted pack mask out-of-band, when the
109 // FEC packet classes have been refactored.
110 //
111 // We treat the mask parts as unsigned integers with host order endianness
112 // in order to simplify the bit shifting between bytes.
113 if (fec_packet->pkt->length < kHeaderSize0KBitsSet) {
114 return false;
115 }
116 bool k_bit0 = (fec_packet->pkt->data[18] & 0x80) != 0; // Read K-bit 0.
danilchap 2016/09/12 13:30:29 may be uint8_t* const packet_mask = &fec_packet->p
brandtr 2016/09/14 11:45:54 Done. Cannot make it a '_const_ uint8_t* const' h
117 uint16_t mask_part0 =
118 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[18]);
119 mask_part0 <<= 1; // Shift away K-bit 0 from adapted mask.
120 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->pkt->data[18], mask_part0);
121 size_t packet_mask_size;
122 if (k_bit0) {
123 packet_mask_size = kFlexfecPacketMaskSize0KBitsSet;
124 } else {
125 if (fec_packet->pkt->length < kHeaderSize1KBitsSet) {
126 return false;
127 }
128 bool k_bit1 = (fec_packet->pkt->data[20] & 0x80) != 0; // Read K-bit 1.
129 bool bit15 = (fec_packet->pkt->data[20] & 0x40) != 0; // Read bit 15.
130 if (bit15) {
131 fec_packet->pkt->data[19] |= 0x01; // Set bit 15 in adapted mask.
132 }
133 uint32_t mask_part1 =
134 ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[20]);
135 mask_part1 <<= 2; // Shift away K-bit 1 and bit 15 from adapted mask.
136 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->pkt->data[20],
137 mask_part1);
138 if (k_bit1) {
139 packet_mask_size = kFlexfecPacketMaskSize1KBitsSet;
140 } else {
141 if (fec_packet->pkt->length < kHeaderSize2KBitsSet) {
142 return false;
143 }
144 bool k_bit2 = (fec_packet->pkt->data[24] & 0x80) != 0; // Read K-bit 2.
145 if (k_bit2) {
146 packet_mask_size = kFlexfecPacketMaskSize2KBitsSet;
147 } else {
148 // Malformed header.
149 return false;
150 }
151 bool bit46 = (fec_packet->pkt->data[24] & 0x40) != 0; // Read bit 46.
152 bool bit47 = (fec_packet->pkt->data[24] & 0x20) != 0; // Read bit 47.
153 if (bit46) {
154 fec_packet->pkt->data[23] |= 0x01; // Set bit 46 in adapted mask.
danilchap 2016/09/12 13:30:29 shouldn't bit46 be more significant than bit47? i.
brandtr 2016/09/14 11:45:53 Good idea, done! Yes, this was a bug in both the
155 }
156 if (bit47) {
157 fec_packet->pkt->data[23] |= 0x02; // Set bit 47 in adapted mask.
158 }
159 uint64_t mask_part2 =
160 ByteReader<uint64_t>::ReadBigEndian(&fec_packet->pkt->data[24]);
161 // Shift away K-bit 2, bit 46, and bit 47 from adapted mask.
162 mask_part2 <<= 3;
163 ByteWriter<uint64_t>::WriteBigEndian(&fec_packet->pkt->data[24],
164 mask_part2);
165 }
166 }
167
168 // Store "ULPFECized" packet mask info.
169 fec_packet->fec_header_size = FlexfecHeaderSize(packet_mask_size);
170 fec_packet->protected_ssrc = protected_ssrc;
171 fec_packet->seq_num_base = seq_num_base;
172 fec_packet->packet_mask_offset = kPacketMaskOffset;
173 fec_packet->packet_mask_size = packet_mask_size;
174
175 // In FlexFEC, all media packets are protected in their entirety.
176 fec_packet->protection_length =
177 fec_packet->pkt->length - fec_packet->fec_header_size;
178
179 return true;
180 }
181
182 FlexfecHeaderWriter::FlexfecHeaderWriter()
183 : FecHeaderWriter(kMaxMediaPackets, kMaxFecPackets, kHeaderSize2KBitsSet) {}
184
185 FlexfecHeaderWriter::~FlexfecHeaderWriter() = default;
186
187 size_t FlexfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask,
188 size_t packet_mask_size) const {
189 if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear &&
190 ((packet_mask[1] & 0x01) == 0)) {
191 // Packet mask is 16 bits long, with bit 15 clear.
192 // It can be used as is.
193 return kFlexfecPacketMaskSize0KBitsSet;
194 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
195 // Packet mask is 16 bits long, with bit 15 set.
196 // We must expand the packet mask with zeros in the FlexFEC header.
197 return kFlexfecPacketMaskSize1KBitsSet;
198 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet &&
199 ((packet_mask[5] & 0x03) == 0)) {
200 // Packet mask is 48 bits long, with bits 46 and 47 clear.
201 // It can be used as is.
202 return kFlexfecPacketMaskSize1KBitsSet;
203 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) {
204 // Packet mask is 48 bits long, with at least one of bits 46 and 47 set.
205 // We must expand it with zeros.
206 return kFlexfecPacketMaskSize2KBitsSet;
207 } else {
208 RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size
209 << ".";
210 }
211 return kFlexfecPacketMaskSize2KBitsSet; // Should never reach here.
danilchap 2016/09/12 13:30:29 move inside else just below RTC_NOTREACHED, commen
brandtr 2016/09/14 11:45:54 Ok. I placed the return outside the if clause due
212 }
213
214 size_t FlexfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const {
215 return FlexfecHeaderSize(packet_mask_size);
216 }
217
218 // This function adapts the precomputed ULPFEC packet masks to the
219 // FlexFEC header standard. Note that the header size is computed by
220 // FecHeaderSize(), so in this function we can be sure that we are
221 // writing in space that is intended for the header.
222 //
223 // TODO(brandtr): Update this function when we support offset-based masks,
224 // retransmissions, and protecting multiple SSRCs.
225 void FlexfecHeaderWriter::FinalizeFecHeader(
226 uint32_t ssrc,
227 uint16_t seq_num_base,
228 const uint8_t* packet_mask,
229 size_t packet_mask_size,
230 ForwardErrorCorrection::Packet* fec_packet) const {
231 fec_packet->data[0] &= 0x7f; // Clear F bit.
232 fec_packet->data[0] &= 0xbf; // Clear R bit.
233 // Write SSRC count, SSRC base, sequence number base.
234 ByteWriter<uint8_t>::WriteBigEndian(&fec_packet->data[8], 1U);
235 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[12], ssrc);
236 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[16], seq_num_base);
237 // Adapt ULPFEC packet mask to FlexFEC header.
238 //
239 // We treat the mask parts as unsigned integers with host order endianness
240 // in order to simplify the bit shifting between bytes.
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(&fec_packet->data[18], tmp_mask_part0);
250 tmp_mask_part1 >>= 2; // Shift, thus clearing K-bit 1 and bit 15.
251 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[20], tmp_mask_part1);
252 bool bit15 = (packet_mask[1] & 0x01) != 0;
253 if (bit15) {
254 fec_packet->data[20] |= 0x40; // Set bit 15.
255 }
256 bool bit46 = (packet_mask[5] & 0x01) != 0;
257 bool bit47 = (packet_mask[5] & 0x02) != 0;
258 if (!bit46 && !bit47) {
259 fec_packet->data[20] |= 0x80; // Set K-bit 1.
260 } else {
261 memset(&fec_packet->data[24], 0, 8); // Clear all trailing bits.
262 fec_packet->data[24] |= 0x80; // Set K-bit 2.
263 if (bit46) {
264 fec_packet->data[24] |= 0x40; // Set bit 46.
265 }
266 if (bit47) {
267 fec_packet->data[24] |= 0x20; // Set bit 47.
268 }
269 }
270 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
271 // The packet mask is 16 bits long.
272 uint16_t tmp_mask_part0 =
273 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
274
275 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0.
276 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[18], tmp_mask_part0);
277 bool bit15 = (packet_mask[1] & 0x01) != 0;
278 if (!bit15) {
279 fec_packet->data[18] |= 0x80; // Set K-bit 0.
280 } else {
281 memset(&fec_packet->data[20], 0U, 4); // Clear all trailing bits.
282 fec_packet->data[20] |= 0x80; // Set K-bit 1.
283 fec_packet->data[20] |= 0x40; // Set bit 15.
284 }
285 } else {
286 RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size
287 << ".";
288 }
289 }
290
291 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698