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/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 kAuxHeaderSize = 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 = | |
45 kBaseHeaderSize + kAuxHeaderSize + kFlexfecPacketMaskSize0KBitsSet; | |
46 constexpr size_t kHeaderSize1KBitsSet = | |
47 kBaseHeaderSize + kAuxHeaderSize + kFlexfecPacketMaskSize1KBitsSet; | |
48 constexpr size_t kHeaderSize2KBitsSet = | |
49 kBaseHeaderSize + kAuxHeaderSize + kFlexfecPacketMaskSize2KBitsSet; | |
50 | |
51 // TODO(brandtr): Update this when we support multistream protection. | |
52 constexpr size_t kPacketMaskOffset = kBaseHeaderSize + kAuxHeaderSize; | |
53 | |
54 // Here we count the K-bits as belonging to the packet mask. | |
55 // This can be used in conjunction with FlexfecHeaderWriter::MinPacketMaskSize, | |
56 // which calculates a bound on the needed packet mask size including K-bits, | |
57 // given a packet mask without K-bits. | |
58 size_t FlexfecHeaderSize(size_t packet_mask_size) { | |
59 RTC_DCHECK_LE(packet_mask_size, kFlexfecPacketMaskSize2KBitsSet); | |
60 if (packet_mask_size <= kFlexfecPacketMaskSize0KBitsSet) { | |
61 return kHeaderSize0KBitsSet; | |
62 } else if (packet_mask_size <= kFlexfecPacketMaskSize1KBitsSet) { | |
63 return kHeaderSize1KBitsSet; | |
64 } | |
65 return kHeaderSize2KBitsSet; | |
66 } | |
67 | |
68 } // namespace | |
69 | |
70 FlexfecHeaderReader::FlexfecHeaderReader() | |
71 : FecHeaderReader(kMaxMediaPackets, kMaxFecPackets) {} | |
72 | |
73 FlexfecHeaderReader::~FlexfecHeaderReader() = default; | |
74 | |
75 // TODO(brandtr): Update this function when we support flexible masks, | |
76 // retransmissions, and/or several protected SSRCs. | |
77 bool FlexfecHeaderReader::ReadFecHeader( | |
78 ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const { | |
79 bool f_bit = (fec_packet->pkt->data[0] & 0x80) != 0; | |
80 if (f_bit) { | |
81 return false; | |
82 } | |
83 bool r_bit = (fec_packet->pkt->data[0] & 0x40) != 0; | |
84 if (r_bit) { | |
85 return false; | |
86 } | |
87 uint8_t ssrc_count = | |
88 ByteReader<uint8_t>::ReadBigEndian(&fec_packet->pkt->data[8]); | |
89 if (ssrc_count != 1U) { | |
danilchap
2016/09/07 12:41:17
I think integer promotion work very well for const
brandtr
2016/09/12 11:31:21
Done.
| |
90 return false; | |
91 } | |
92 uint32_t protected_ssrc = | |
93 ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[12]); | |
94 uint16_t seq_num_base = | |
95 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[16]); | |
96 | |
97 // Read packet mask and pack it tight by removing the interleaved K bits. | |
98 // This destroys the FlexFEC standards compliance of the packet masks, | |
99 // but makes it compatible with the ULPFEC masks. | |
100 // | |
101 // We treat the mask parts as unsigned integers with host order endianness | |
102 // in order to simplify the bit shifting between bytes. | |
103 uint16_t mask_part0 = | |
104 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[18]); | |
105 bool k_bit0 = (mask_part0 & (1 << 15)) != 0; // Read K-bit 0. | |
danilchap
2016/09/07 12:41:17
0x80 for f_bit and (1 << 15) here.
Choose the one
brandtr
2016/09/12 11:31:21
Updated to use variable & 0xYZ for consistency.
I
| |
106 mask_part0 <<= 1; // Shift away K-bit 0. | |
107 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->pkt->data[18], mask_part0); | |
108 size_t packet_mask_size; | |
109 if (k_bit0) { | |
110 packet_mask_size = kFlexfecPacketMaskSize0KBitsSet; | |
111 } else { | |
112 uint32_t mask_part1 = | |
113 ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[20]); | |
114 bool k_bit1 = (mask_part1 & (1 << 31)) != 0; // Read K-bit 1. | |
danilchap
2016/09/07 12:41:17
may be k_bit1 = (fec_packet->pkt->data[20] & 0x80)
brandtr
2016/09/12 11:31:21
With the reordering, I now always read from the pa
| |
115 bool bit15 = (mask_part1 & (1 << 30)) != 0; // Read bit 15. | |
116 if (bit15) { | |
117 fec_packet->pkt->data[19] |= 0x01; // Set bit 15. | |
118 } | |
119 mask_part1 <<= 2; // Shift away K-bit 1 and bit 15. | |
120 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->pkt->data[20], | |
121 mask_part1); | |
122 if (k_bit1) { | |
123 packet_mask_size = kFlexfecPacketMaskSize1KBitsSet; | |
124 } else { | |
125 uint64_t mask_part2 = | |
126 ByteReader<uint64_t>::ReadBigEndian(&fec_packet->pkt->data[24]); | |
127 bool k_bit2 = (mask_part2 & (1ULL << 63)) != 0; // Read K-bit 2. | |
128 if (k_bit2) { | |
129 packet_mask_size = kFlexfecPacketMaskSize2KBitsSet; | |
130 } else { | |
131 // Malformed header. | |
132 return false; | |
133 } | |
134 bool bit46 = (mask_part2 & (1ULL << 62)) != 0; // Read bit 46. | |
135 bool bit47 = (mask_part2 & (1ULL << 61)) != 0; // Read bit 47. | |
136 if (bit46) { | |
137 fec_packet->pkt->data[23] |= 0x01; // Set bit 46. | |
138 } | |
139 if (bit47) { | |
140 fec_packet->pkt->data[23] |= 0x02; // Set bit 47. | |
141 } | |
142 mask_part2 <<= 3; // Shift away K-bit 2, bit 46, and bit 47. | |
143 ByteWriter<uint64_t>::WriteBigEndian(&fec_packet->pkt->data[24], | |
144 mask_part2); | |
145 } | |
146 } | |
147 | |
148 // Store "ULPFECized" packet mask info. | |
149 fec_packet->fec_header_size = FlexfecHeaderSize(packet_mask_size); | |
150 fec_packet->protected_ssrc = protected_ssrc; | |
151 fec_packet->seq_num_base = seq_num_base; | |
152 fec_packet->packet_mask_offset = kPacketMaskOffset; | |
153 fec_packet->packet_mask_size = packet_mask_size; | |
154 | |
155 // In FlexFEC, all media packets are protected in their entirety. | |
156 fec_packet->protection_length = | |
157 fec_packet->pkt->length - fec_packet->fec_header_size; | |
158 | |
159 return true; | |
160 } | |
161 | |
162 FlexfecHeaderWriter::FlexfecHeaderWriter() | |
163 : FecHeaderWriter(kMaxMediaPackets, | |
164 kMaxFecPackets, | |
165 kHeaderSize2KBitsSet) {} | |
166 | |
167 FlexfecHeaderWriter::~FlexfecHeaderWriter() = default; | |
168 | |
169 size_t FlexfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask, | |
170 size_t packet_mask_size) const { | |
171 if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear && | |
172 ((packet_mask[1] & 0x01) == 0)) { | |
173 // Packet mask is 16 bits long, with bit 15 clear. | |
174 // It can be used as is. | |
175 return kFlexfecPacketMaskSize0KBitsSet; | |
176 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) { | |
177 // Packet mask is 16 bits long, with bit 15 set. | |
178 // We must expand the packet mask with zeros in the FlexFEC header. | |
179 return kFlexfecPacketMaskSize1KBitsSet; | |
180 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet && | |
181 ((packet_mask[5] & 0x03) == 0)) { | |
182 // Packet mask is 48 bits long, with bits 46 and 47 clear. | |
183 // It can be used as is. | |
184 return kFlexfecPacketMaskSize1KBitsSet; | |
185 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) { | |
186 // Packet mask is 48 bits long, with at least one of bits 46 and 47 set. | |
187 // We must expand it with zeros. | |
188 return kFlexfecPacketMaskSize2KBitsSet; | |
189 } else { | |
190 RTC_NOTREACHED() << "Packet mask size is " << packet_mask_size | |
191 << ", which is incorrect"; | |
192 } | |
193 } | |
194 | |
195 size_t FlexfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const { | |
196 return FlexfecHeaderSize(packet_mask_size); | |
197 } | |
198 | |
199 // This function adapts the precomputed ULPFEC packet masks to the | |
200 // FlexFEC header standard. Note that the header size is computed by | |
201 // FecHeaderSize(), so in this function we can be sure that we are | |
202 // writing in space that is intended for the header. | |
203 // | |
204 // TODO(brandtr): Update this function when we support offset-based masks, | |
205 // retransmissions, and protecting multiple SSRCs. | |
206 void FlexfecHeaderWriter::FinalizeFecHeader( | |
207 uint32_t ssrc, | |
208 uint16_t seq_num_base, | |
209 const uint8_t* packet_mask, | |
210 size_t packet_mask_size, | |
211 ForwardErrorCorrection::Packet* fec_packet) const { | |
212 fec_packet->data[0] &= 0x7f; // Clear F bit. | |
213 fec_packet->data[0] &= 0xbf; // Clear R bit. | |
214 // Write SSRC count, SSRC base, sequence number base. | |
215 ByteWriter<uint8_t>::WriteBigEndian(&fec_packet->data[8], 1U); | |
216 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[12], ssrc); | |
217 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[16], seq_num_base); | |
218 // Adapt ULPFEC packet mask to FlexFEC header. | |
219 // | |
220 // We treat the mask parts as unsigned integers with host order endianness | |
221 // in order to simplify the bit shifting between bytes. | |
222 if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) { | |
223 // The packet mask is 48 bits long. | |
224 uint16_t tmp_mask_part0 = | |
225 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]); | |
226 uint32_t tmp_mask_part1 = | |
227 ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]); | |
228 | |
229 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0. | |
230 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[18], tmp_mask_part0); | |
231 tmp_mask_part1 >>= 2; // Shift, thus clearing K-bit 1 and bit 15. | |
232 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[20], tmp_mask_part1); | |
233 bool bit15 = (packet_mask[1] & 0x01) != 0; | |
234 if (bit15) { | |
235 fec_packet->data[20] |= 0x40; // Set bit 15. | |
236 } | |
237 bool bit46 = (packet_mask[5] & 0x01) != 0; | |
238 bool bit47 = (packet_mask[5] & 0x02) != 0; | |
239 if (!bit46 && !bit47) { | |
240 fec_packet->data[20] |= 0x80; // Set K-bit 1. | |
241 } else { | |
242 memset(&fec_packet->data[24], 0U, 8); // Clear all trailing bits. | |
danilchap
2016/09/07 12:41:17
memset take 2nd parameter as (signed) int actually
brandtr
2016/09/12 11:31:21
Fixed.
| |
243 fec_packet->data[24] |= 0x80; // Set K-bit 2. | |
244 if (bit46) { | |
245 fec_packet->data[24] |= 0x40; // Set bit 46. | |
246 } | |
247 if (bit47) { | |
248 fec_packet->data[24] |= 0x20; // Set bit 47. | |
249 } | |
250 } | |
251 } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) { | |
252 // The packet mask is 16 bits long. | |
253 uint16_t tmp_mask_part0 = | |
254 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]); | |
255 | |
256 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0. | |
257 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[18], tmp_mask_part0); | |
258 bool bit15 = (packet_mask[1] & 0x01) != 0; | |
259 if (!bit15) { | |
260 fec_packet->data[18] |= 0x80; // Set K-bit 0. | |
261 } else { | |
262 memset(&fec_packet->data[20], 0U, 4); // Clear all trailing bits. | |
263 fec_packet->data[20] |= 0x80; // Set K-bit 1. | |
264 fec_packet->data[20] |= 0x40; // Set bit 15. | |
265 } | |
266 } else { | |
267 RTC_NOTREACHED() << "Packet mask size is " << packet_mask_size | |
268 << ", which is incorrect"; | |
269 } | |
270 } | |
271 | |
272 } // namespace webrtc | |
OLD | NEW |