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

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: Review response 3. 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 kFlexfecPacketMaskSize0KBitsSet = 2;
34 constexpr size_t kFlexfecPacketMaskSize1KBitsSet = 6;
brandtr 2016/09/14 11:45:54 These names were not strictly correct, so I change
35 constexpr size_t kFlexfecPacketMaskSize2KBitsSet = 14;
36
37 // Size (in bytes) of part of header which is not packet mask specific.
38 constexpr size_t kBaseHeaderSize = 12;
39
40 // Size (in bytes) of part of header which is stream specific.
41 constexpr size_t kStreamSpecificHeaderSize = 6;
42
43 // Size (in bytes) of header, given the single stream packet mask size, i.e.
44 // the number of K-bits set.
45 constexpr size_t kHeaderSize0KBitsSet = kBaseHeaderSize +
46 kStreamSpecificHeaderSize +
47 kFlexfecPacketMaskSize0KBitsSet;
48 constexpr size_t kHeaderSize1KBitsSet = kBaseHeaderSize +
49 kStreamSpecificHeaderSize +
50 kFlexfecPacketMaskSize1KBitsSet;
51 constexpr size_t kHeaderSize2KBitsSet = kBaseHeaderSize +
52 kStreamSpecificHeaderSize +
53 kFlexfecPacketMaskSize2KBitsSet;
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, kFlexfecPacketMaskSize2KBitsSet);
65 if (packet_mask_size <= kFlexfecPacketMaskSize0KBitsSet) {
66 return kHeaderSize0KBitsSet;
67 } else if (packet_mask_size <= kFlexfecPacketMaskSize1KBitsSet) {
68 return kHeaderSize1KBitsSet;
69 }
70 return kHeaderSize2KBitsSet;
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 return false;
91 }
92 bool r_bit = (fec_packet->pkt->data[0] & 0x40) != 0;
93 if (r_bit) {
94 return false;
95 }
96 uint8_t ssrc_count =
97 ByteReader<uint8_t>::ReadBigEndian(&fec_packet->pkt->data[8]);
98 if (ssrc_count != 1) {
99 return false;
100 }
101 uint32_t protected_ssrc =
102 ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[12]);
103 uint16_t seq_num_base =
104 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[16]);
105
106 // Read packet mask and adapt it by removing the interleaved K bits.
107 // This destroys the FlexFEC standards compliance of the packet masks,
108 // but makes it compatible with the ULPFEC masks.
109 // TODO(brandtr): Store the adapted pack mask out-of-band, when the
110 // FEC packet classes have been refactored.
111 //
112 // We treat the mask parts as unsigned integers with host order endianness
113 // in order to simplify the bit shifting between bytes.
114 if (fec_packet->pkt->length < kHeaderSize0KBitsSet) {
115 LOG(LS_INFO) << "Discarding truncated FlexFEC packet.";
116 return false;
117 }
118 uint8_t* const packet_mask = fec_packet->pkt->data + kPacketMaskOffset;
119 bool k_bit0 = (packet_mask[0] & 0x80) != 0; // Read K-bit 0.
120 uint16_t mask_part0 = ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
121 mask_part0 <<= 1; // Shift away K-bit 0 from adapted mask.
122 ByteWriter<uint16_t>::WriteBigEndian(&packet_mask[0], mask_part0);
123 size_t packet_mask_size;
124 if (k_bit0) {
125 packet_mask_size = kFlexfecPacketMaskSize0KBitsSet;
126 } else {
127 if (fec_packet->pkt->length < kHeaderSize1KBitsSet) {
128 return false;
129 }
130 bool k_bit1 = (packet_mask[2] & 0x80) != 0; // Read K-bit 1.
131 bool bit15 = (packet_mask[2] & 0x40) != 0; // Read bit 15.
132 if (bit15) {
133 packet_mask[1] |= 0x01; // Set bit 15 in adapted mask.
134 }
135 uint32_t mask_part1 = ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]);
136 mask_part1 <<= 2; // Shift away K-bit 1 and bit 15 from adapted mask.
137 ByteWriter<uint32_t>::WriteBigEndian(&packet_mask[2], mask_part1);
138 if (k_bit1) {
139 packet_mask_size = kFlexfecPacketMaskSize1KBitsSet;
140 } else {
141 if (fec_packet->pkt->length < kHeaderSize2KBitsSet) {
142 LOG(LS_INFO) << "Discarding truncated FlexFEC packet.";
143 return false;
144 }
145 bool k_bit2 = (packet_mask[6] & 0x80) != 0; // Read K-bit 2.
146 if (k_bit2) {
147 packet_mask_size = kFlexfecPacketMaskSize2KBitsSet;
148 } else {
149 LOG(LS_INFO) << "Discarding FlexFEC packet with malformed header.";
150 return false;
151 }
152 // Copy bits 46 and 47.
153 uint8_t tail_bits = (packet_mask[6] >> 5) & 0x03;
154 packet_mask[5] |= tail_bits;
155 uint64_t mask_part2 =
156 ByteReader<uint64_t>::ReadBigEndian(&packet_mask[6]);
157 // Shift away K-bit 2, bit 46, and bit 47 from adapted mask.
158 mask_part2 <<= 3;
159 ByteWriter<uint64_t>::WriteBigEndian(&packet_mask[6], mask_part2);
160 }
161 }
162
163 // Store "ULPFECized" packet mask info.
164 fec_packet->fec_header_size = FlexfecHeaderSize(packet_mask_size);
165 fec_packet->protected_ssrc = protected_ssrc;
166 fec_packet->seq_num_base = seq_num_base;
167 fec_packet->packet_mask_offset = kPacketMaskOffset;
168 fec_packet->packet_mask_size = packet_mask_size;
169
170 // In FlexFEC, all media packets are protected in their entirety.
171 fec_packet->protection_length =
172 fec_packet->pkt->length - fec_packet->fec_header_size;
173
174 return true;
175 }
176
177 FlexfecHeaderWriter::FlexfecHeaderWriter()
178 : FecHeaderWriter(kMaxMediaPackets, kMaxFecPackets, kHeaderSize2KBitsSet) {}
179
180 FlexfecHeaderWriter::~FlexfecHeaderWriter() = default;
181
182 size_t FlexfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask,
183 size_t packet_mask_size) const {
184 if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear &&
185 ((packet_mask[1] & 0x01) == 0)) {
186 // Packet mask is 16 bits long, with bit 15 clear.
187 // It can be used as is.
188 return kFlexfecPacketMaskSize0KBitsSet;
189 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
190 // Packet mask is 16 bits long, with bit 15 set.
191 // We must expand the packet mask with zeros in the FlexFEC header.
192 return kFlexfecPacketMaskSize1KBitsSet;
193 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet &&
194 ((packet_mask[5] & 0x03) == 0)) {
195 // Packet mask is 48 bits long, with bits 46 and 47 clear.
196 // It can be used as is.
197 return kFlexfecPacketMaskSize1KBitsSet;
198 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) {
199 // Packet mask is 48 bits long, with at least one of bits 46 and 47 set.
200 // We must expand it with zeros.
201 return kFlexfecPacketMaskSize2KBitsSet;
202 }
203 RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size
204 << ".";
205 return kFlexfecPacketMaskSize2KBitsSet;
206 }
207
208 size_t FlexfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const {
209 return FlexfecHeaderSize(packet_mask_size);
210 }
211
212 // This function adapts the precomputed ULPFEC packet masks to the
213 // FlexFEC header standard. Note that the header size is computed by
214 // FecHeaderSize(), so in this function we can be sure that we are
215 // writing in space that is intended for the header.
216 //
217 // TODO(brandtr): Update this function when we support offset-based masks,
218 // retransmissions, and protecting multiple SSRCs.
219 void FlexfecHeaderWriter::FinalizeFecHeader(
220 uint32_t ssrc,
221 uint16_t seq_num_base,
222 const uint8_t* packet_mask,
223 size_t packet_mask_size,
224 ForwardErrorCorrection::Packet* fec_packet) const {
225 fec_packet->data[0] &= 0x7f; // Clear F bit.
226 fec_packet->data[0] &= 0xbf; // Clear R bit.
227 // Write SSRC count, SSRC base, sequence number base.
228 ByteWriter<uint8_t>::WriteBigEndian(&fec_packet->data[8], 1U);
229 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[12], ssrc);
230 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[16], seq_num_base);
231 // Adapt ULPFEC packet mask to FlexFEC header.
232 //
233 // We treat the mask parts as unsigned integers with host order endianness
234 // in order to simplify the bit shifting between bytes.
235 uint8_t* const written_packet_mask = fec_packet->data + kPacketMaskOffset;
236 if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) {
237 // The packet mask is 48 bits long.
238 uint16_t tmp_mask_part0 =
239 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
240 uint32_t tmp_mask_part1 =
241 ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]);
242
243 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0.
244 ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0],
245 tmp_mask_part0);
246 tmp_mask_part1 >>= 2; // Shift, thus clearing K-bit 1 and bit 15.
247 ByteWriter<uint32_t>::WriteBigEndian(&written_packet_mask[2],
248 tmp_mask_part1);
249 bool bit15 = (packet_mask[1] & 0x01) != 0;
250 if (bit15) {
251 written_packet_mask[2] |= 0x40; // Set bit 15.
252 }
253 bool bit46 = (packet_mask[5] & 0x02) != 0;
254 bool bit47 = (packet_mask[5] & 0x01) != 0;
255 if (!bit46 && !bit47) {
256 written_packet_mask[2] |= 0x80; // Set K-bit 1.
257 } else {
258 memset(&written_packet_mask[6], 0, 8); // Clear all trailing bits.
259 written_packet_mask[6] |= 0x80; // Set K-bit 2.
260 if (bit46) {
261 written_packet_mask[6] |= 0x40; // Set bit 46.
262 }
263 if (bit47) {
264 written_packet_mask[6] |= 0x20; // Set bit 47.
265 }
266 }
267 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
268 // The packet mask is 16 bits long.
269 uint16_t tmp_mask_part0 =
270 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
271
272 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0.
273 ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0],
274 tmp_mask_part0);
275 bool bit15 = (packet_mask[1] & 0x01) != 0;
276 if (!bit15) {
277 written_packet_mask[0] |= 0x80; // Set K-bit 0.
278 } else {
279 memset(&written_packet_mask[2], 0U, 4); // Clear all trailing bits.
280 written_packet_mask[2] |= 0x80; // Set K-bit 1.
281 written_packet_mask[2] |= 0x40; // Set bit 15.
282 }
283 } else {
284 RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size
285 << ".";
286 }
287 }
288
289 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698