OLD | NEW |
---|---|
(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 kFlexfecPacketMaskSizeKBit0Set = 2; | |
danilchap
2016/09/14 13:12:09
Yes, this name(s) explain constant(s) better.
Do y
brandtr
2016/09/20 11:13:32
Done.
| |
34 constexpr size_t kFlexfecPacketMaskSizeKBit1Set = 6; | |
35 constexpr size_t kFlexfecPacketMaskSizeKBit2Set = 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 kHeaderSizeKBit0Set = kBaseHeaderSize + | |
46 kStreamSpecificHeaderSize + | |
47 kFlexfecPacketMaskSizeKBit0Set; | |
48 constexpr size_t kHeaderSizeKBit1Set = kBaseHeaderSize + | |
49 kStreamSpecificHeaderSize + | |
50 kFlexfecPacketMaskSizeKBit1Set; | |
51 constexpr size_t kHeaderSizeKBit2Set = kBaseHeaderSize + | |
52 kStreamSpecificHeaderSize + | |
53 kFlexfecPacketMaskSizeKBit2Set; | |
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, kFlexfecPacketMaskSizeKBit2Set); | |
65 if (packet_mask_size <= kFlexfecPacketMaskSizeKBit0Set) { | |
66 return kHeaderSizeKBit0Set; | |
67 } else if (packet_mask_size <= kFlexfecPacketMaskSizeKBit1Set) { | |
68 return kHeaderSizeKBit1Set; | |
69 } | |
70 return kHeaderSizeKBit2Set; | |
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 < kHeaderSizeKBit0Set) { | |
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. | |
danilchap
2016/09/14 13:12:09
comment probably not needed because duplicates var
brandtr
2016/09/20 11:13:32
Done.
| |
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 = kFlexfecPacketMaskSizeKBit0Set; | |
126 } else { | |
127 if (fec_packet->pkt->length < kHeaderSizeKBit1Set) { | |
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 = kFlexfecPacketMaskSizeKBit1Set; | |
140 } else { | |
141 if (fec_packet->pkt->length < kHeaderSizeKBit2Set) { | |
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 = kFlexfecPacketMaskSizeKBit2Set; | |
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, kHeaderSizeKBit2Set) {} | |
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 kFlexfecPacketMaskSizeKBit0Set; | |
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 kFlexfecPacketMaskSizeKBit1Set; | |
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 kFlexfecPacketMaskSizeKBit1Set; | |
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 kFlexfecPacketMaskSizeKBit2Set; | |
202 } | |
203 RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size << "."; | |
204 return kFlexfecPacketMaskSizeKBit2Set; | |
205 } | |
206 | |
207 size_t FlexfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const { | |
208 return FlexfecHeaderSize(packet_mask_size); | |
209 } | |
210 | |
211 // This function adapts the precomputed ULPFEC packet masks to the | |
212 // FlexFEC header standard. Note that the header size is computed by | |
213 // FecHeaderSize(), so in this function we can be sure that we are | |
214 // writing in space that is intended for the header. | |
215 // | |
216 // TODO(brandtr): Update this function when we support offset-based masks, | |
217 // retransmissions, and protecting multiple SSRCs. | |
218 void FlexfecHeaderWriter::FinalizeFecHeader( | |
219 uint32_t ssrc, | |
220 uint16_t seq_num_base, | |
221 const uint8_t* packet_mask, | |
222 size_t packet_mask_size, | |
223 ForwardErrorCorrection::Packet* fec_packet) const { | |
224 fec_packet->data[0] &= 0x7f; // Clear F bit. | |
225 fec_packet->data[0] &= 0xbf; // Clear R bit. | |
226 // Write SSRC count, SSRC base, sequence number base. | |
227 ByteWriter<uint8_t>::WriteBigEndian(&fec_packet->data[8], 1U); | |
228 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[12], ssrc); | |
229 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[16], seq_num_base); | |
230 // Adapt ULPFEC packet mask to FlexFEC header. | |
231 // | |
232 // We treat the mask parts as unsigned integers with host order endianness | |
233 // in order to simplify the bit shifting between bytes. | |
234 uint8_t* const written_packet_mask = fec_packet->data + kPacketMaskOffset; | |
235 if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) { | |
236 // The packet mask is 48 bits long. | |
237 uint16_t tmp_mask_part0 = | |
238 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]); | |
239 uint32_t tmp_mask_part1 = | |
240 ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]); | |
241 | |
242 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0. | |
243 ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0], | |
244 tmp_mask_part0); | |
245 tmp_mask_part1 >>= 2; // Shift, thus clearing K-bit 1 and bit 15. | |
246 ByteWriter<uint32_t>::WriteBigEndian(&written_packet_mask[2], | |
247 tmp_mask_part1); | |
248 bool bit15 = (packet_mask[1] & 0x01) != 0; | |
249 if (bit15) { | |
250 written_packet_mask[2] |= 0x40; // Set bit 15. | |
251 } | |
252 bool bit46 = (packet_mask[5] & 0x02) != 0; | |
253 bool bit47 = (packet_mask[5] & 0x01) != 0; | |
254 if (!bit46 && !bit47) { | |
255 written_packet_mask[2] |= 0x80; // Set K-bit 1. | |
256 } else { | |
257 memset(&written_packet_mask[6], 0, 8); // Clear all trailing bits. | |
258 written_packet_mask[6] |= 0x80; // Set K-bit 2. | |
259 if (bit46) { | |
260 written_packet_mask[6] |= 0x40; // Set bit 46. | |
261 } | |
262 if (bit47) { | |
263 written_packet_mask[6] |= 0x20; // Set bit 47. | |
264 } | |
265 } | |
266 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) { | |
267 // The packet mask is 16 bits long. | |
268 uint16_t tmp_mask_part0 = | |
269 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]); | |
270 | |
271 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0. | |
272 ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0], | |
273 tmp_mask_part0); | |
274 bool bit15 = (packet_mask[1] & 0x01) != 0; | |
275 if (!bit15) { | |
276 written_packet_mask[0] |= 0x80; // Set K-bit 0. | |
277 } else { | |
278 memset(&written_packet_mask[2], 0U, 4); // Clear all trailing bits. | |
279 written_packet_mask[2] |= 0x80; // Set K-bit 1. | |
280 written_packet_mask[2] |= 0x40; // Set bit 15. | |
281 } | |
282 } else { | |
283 RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size | |
284 << "."; | |
285 } | |
286 } | |
287 | |
288 } // namespace webrtc | |
OLD | NEW |