OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/rtp_rtcp/source/fec_receiver_impl.h" | 11 #include "webrtc/modules/rtp_rtcp/source/fec_receiver_impl.h" |
12 | 12 |
13 #include <memory> | 13 #include <memory> |
14 | 14 |
15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
16 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
17 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 17 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
18 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_video.h" | 18 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_video.h" |
19 | 19 |
20 // RFC 5109 | |
21 namespace webrtc { | 20 namespace webrtc { |
22 | 21 |
23 FecReceiver* FecReceiver::Create(RtpData* callback) { | 22 FecReceiver* FecReceiver::Create(RtpData* callback) { |
24 return new FecReceiverImpl(callback); | 23 return new FecReceiverImpl(callback); |
25 } | 24 } |
26 | 25 |
27 FecReceiverImpl::FecReceiverImpl(RtpData* callback) | 26 FecReceiverImpl::FecReceiverImpl(RtpData* callback) |
28 : recovered_packet_callback_(callback), | 27 : recovered_packet_callback_(callback) {} |
29 fec_(new ForwardErrorCorrection()) {} | |
30 | 28 |
31 FecReceiverImpl::~FecReceiverImpl() { | 29 FecReceiverImpl::~FecReceiverImpl() { |
32 while (!received_packet_list_.empty()) { | 30 while (!received_packets_.empty()) { |
33 delete received_packet_list_.front(); | 31 delete received_packets_.front(); |
34 received_packet_list_.pop_front(); | 32 received_packets_.pop_front(); |
35 } | 33 } |
36 if (fec_ != NULL) { | 34 fec_.ResetState(&recovered_packets_); |
37 fec_->ResetState(&recovered_packet_list_); | |
38 delete fec_; | |
39 } | |
40 } | 35 } |
41 | 36 |
42 FecPacketCounter FecReceiverImpl::GetPacketCounter() const { | 37 FecPacketCounter FecReceiverImpl::GetPacketCounter() const { |
43 rtc::CritScope cs(&crit_sect_); | 38 rtc::CritScope cs(&crit_sect_); |
44 return packet_counter_; | 39 return packet_counter_; |
45 } | 40 } |
46 | 41 |
47 // 0 1 2 3 | 42 // 0 1 2 3 |
48 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | 43 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
49 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 44 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
(...skipping 19 matching lines...) Expand all Loading... | |
69 // timestamp to determine the timestamp of the data for which this | 64 // timestamp to determine the timestamp of the data for which this |
70 // block is the redundancy. | 65 // block is the redundancy. |
71 // | 66 // |
72 // block length: 10 bits Length in bytes of the corresponding data | 67 // block length: 10 bits Length in bytes of the corresponding data |
73 // block excluding header. | 68 // block excluding header. |
74 | 69 |
75 int32_t FecReceiverImpl::AddReceivedRedPacket( | 70 int32_t FecReceiverImpl::AddReceivedRedPacket( |
76 const RTPHeader& header, const uint8_t* incoming_rtp_packet, | 71 const RTPHeader& header, const uint8_t* incoming_rtp_packet, |
77 size_t packet_length, uint8_t ulpfec_payload_type) { | 72 size_t packet_length, uint8_t ulpfec_payload_type) { |
78 rtc::CritScope cs(&crit_sect_); | 73 rtc::CritScope cs(&crit_sect_); |
79 uint8_t REDHeaderLength = 1; | 74 |
75 uint8_t red_header_length = 1; | |
80 size_t payload_data_length = packet_length - header.headerLength; | 76 size_t payload_data_length = packet_length - header.headerLength; |
81 | 77 |
82 if (payload_data_length == 0) { | 78 if (payload_data_length == 0) { |
83 LOG(LS_WARNING) << "Corrupt/truncated FEC packet."; | 79 LOG(LS_WARNING) << "Corrupt/truncated FEC packet."; |
84 return -1; | 80 return -1; |
85 } | 81 } |
86 | 82 |
87 // Add to list without RED header, aka a virtual RTP packet | 83 // Remove RED header of incoming packet and store as a virtual RTP packet. |
88 // we remove the RED header | |
89 | |
90 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet( | 84 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet( |
91 new ForwardErrorCorrection::ReceivedPacket()); | 85 new ForwardErrorCorrection::ReceivedPacket()); |
92 received_packet->pkt = new ForwardErrorCorrection::Packet(); | 86 received_packet->pkt = new ForwardErrorCorrection::Packet(); |
93 | 87 |
94 // get payload type from RED header | 88 // Get payload type from RED header and sequence number from RTP header. |
95 uint8_t payload_type = | 89 uint8_t payload_type = incoming_rtp_packet[header.headerLength] & 0x7f; |
96 incoming_rtp_packet[header.headerLength] & 0x7f; | |
97 | |
98 received_packet->is_fec = payload_type == ulpfec_payload_type; | 90 received_packet->is_fec = payload_type == ulpfec_payload_type; |
99 received_packet->seq_num = header.sequenceNumber; | 91 received_packet->seq_num = header.sequenceNumber; |
100 | 92 |
101 uint16_t blockLength = 0; | 93 uint16_t block_length = 0; |
102 if (incoming_rtp_packet[header.headerLength] & 0x80) { | 94 if (incoming_rtp_packet[header.headerLength] & 0x80) { |
103 // f bit set in RED header | 95 // f bit set in RED header, i.e. there are more than one RED header blocks. |
104 REDHeaderLength = 4; | 96 red_header_length = 4; |
105 if (payload_data_length < REDHeaderLength + 1u) { | 97 if (payload_data_length < red_header_length + 1u) { |
106 LOG(LS_WARNING) << "Corrupt/truncated FEC packet."; | 98 LOG(LS_WARNING) << "Corrupt/truncated FEC packet."; |
107 return -1; | 99 return -1; |
108 } | 100 } |
109 | 101 |
110 uint16_t timestamp_offset = | 102 uint16_t timestamp_offset = |
111 (incoming_rtp_packet[header.headerLength + 1]) << 8; | 103 incoming_rtp_packet[header.headerLength + 1] << 8; |
112 timestamp_offset += | 104 timestamp_offset += |
113 incoming_rtp_packet[header.headerLength + 2]; | 105 incoming_rtp_packet[header.headerLength + 2]; |
114 timestamp_offset = timestamp_offset >> 2; | 106 timestamp_offset = timestamp_offset >> 2; |
115 if (timestamp_offset != 0) { | 107 if (timestamp_offset != 0) { |
116 LOG(LS_WARNING) << "Corrupt payload found."; | 108 LOG(LS_WARNING) << "Corrupt payload found."; |
117 return -1; | 109 return -1; |
118 } | 110 } |
119 | 111 |
120 blockLength = | 112 block_length = (0x3 & incoming_rtp_packet[header.headerLength + 2]) << 8; |
121 (0x03 & incoming_rtp_packet[header.headerLength + 2]) << 8; | 113 block_length += incoming_rtp_packet[header.headerLength + 3]; |
stefan-webrtc
2016/07/20 09:29:19
remove extra space after +=
brandtr
2016/07/21 09:03:58
Done.
| |
122 blockLength += (incoming_rtp_packet[header.headerLength + 3]); | |
123 | 114 |
124 // check next RED header | 115 // Check next RED header block. |
125 if (incoming_rtp_packet[header.headerLength + 4] & 0x80) { | 116 if (incoming_rtp_packet[header.headerLength + 4] & 0x80) { |
126 LOG(LS_WARNING) << "More than 2 blocks in packet not supported."; | 117 LOG(LS_WARNING) << "More than 2 blocks in packet not supported."; |
127 return -1; | 118 return -1; |
128 } | 119 } |
129 // Check that the packet is long enough to contain data in the following | 120 // Check that the packet is long enough to contain data in the following |
130 // block. | 121 // block. |
131 if (blockLength > payload_data_length - (REDHeaderLength + 1)) { | 122 if (block_length > payload_data_length - (red_header_length + 1)) { |
132 LOG(LS_WARNING) << "Block length longer than packet."; | 123 LOG(LS_WARNING) << "Block length longer than packet."; |
133 return -1; | 124 return -1; |
134 } | 125 } |
135 } | 126 } |
136 ++packet_counter_.num_packets; | 127 ++packet_counter_.num_packets; |
137 | 128 |
138 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> | 129 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> |
139 second_received_packet; | 130 second_received_packet; |
140 if (blockLength > 0) { | 131 if (block_length > 0) { |
141 // handle block length, split into 2 packets | 132 // Handle block length, split into two packets. |
142 REDHeaderLength = 5; | 133 red_header_length = 5; |
143 | 134 |
144 // copy the RTP header | 135 // Copy RTP header. |
145 memcpy(received_packet->pkt->data, incoming_rtp_packet, | 136 memcpy(received_packet->pkt->data, incoming_rtp_packet, |
146 header.headerLength); | 137 header.headerLength); |
147 | 138 |
148 // replace the RED payload type | 139 // Set payload type. |
149 received_packet->pkt->data[1] &= 0x80; // reset the payload | 140 received_packet->pkt->data[1] &= 0x80; // reset RED payload type |
danilchap
2016/07/19 14:55:32
fix comment here and next line: Uppercase 1st lett
brandtr
2016/07/21 09:03:58
Done.
| |
150 received_packet->pkt->data[1] += | 141 received_packet->pkt->data[1] += payload_type; // set media payload type |
151 payload_type; // set the media payload type | |
152 | 142 |
153 // copy the payload data | 143 // Copy payload data. |
154 memcpy( | 144 memcpy( |
155 received_packet->pkt->data + header.headerLength, | 145 received_packet->pkt->data + header.headerLength, |
156 incoming_rtp_packet + header.headerLength + REDHeaderLength, | 146 incoming_rtp_packet + header.headerLength + red_header_length, |
157 blockLength); | 147 block_length); |
148 received_packet->pkt->length = block_length; | |
158 | 149 |
159 received_packet->pkt->length = blockLength; | 150 second_received_packet.reset(new ForwardErrorCorrection::ReceivedPacket); |
160 | 151 second_received_packet->pkt = new ForwardErrorCorrection::Packet; |
161 second_received_packet.reset(new ForwardErrorCorrection::ReceivedPacket()); | |
162 second_received_packet->pkt = new ForwardErrorCorrection::Packet(); | |
163 | 152 |
164 second_received_packet->is_fec = true; | 153 second_received_packet->is_fec = true; |
165 second_received_packet->seq_num = header.sequenceNumber; | 154 second_received_packet->seq_num = header.sequenceNumber; |
166 ++packet_counter_.num_fec_packets; | 155 ++packet_counter_.num_fec_packets; |
167 | 156 |
168 // copy the FEC payload data | 157 // Copy FEC payload data. |
169 memcpy(second_received_packet->pkt->data, | 158 memcpy(second_received_packet->pkt->data, |
170 incoming_rtp_packet + header.headerLength + | 159 incoming_rtp_packet + header.headerLength + |
171 REDHeaderLength + blockLength, | 160 red_header_length + block_length, |
172 payload_data_length - REDHeaderLength - blockLength); | 161 payload_data_length - red_header_length - block_length); |
173 | 162 |
174 second_received_packet->pkt->length = | 163 second_received_packet->pkt->length = |
175 payload_data_length - REDHeaderLength - blockLength; | 164 payload_data_length - red_header_length - block_length; |
176 | 165 |
177 } else if (received_packet->is_fec) { | 166 } else if (received_packet->is_fec) { |
178 ++packet_counter_.num_fec_packets; | 167 ++packet_counter_.num_fec_packets; |
179 // everything behind the RED header | 168 // everything behind the RED header |
180 memcpy( | 169 memcpy( |
181 received_packet->pkt->data, | 170 received_packet->pkt->data, |
182 incoming_rtp_packet + header.headerLength + REDHeaderLength, | 171 incoming_rtp_packet + header.headerLength + red_header_length, |
183 payload_data_length - REDHeaderLength); | 172 payload_data_length - red_header_length); |
184 received_packet->pkt->length = payload_data_length - REDHeaderLength; | 173 received_packet->pkt->length = payload_data_length - red_header_length; |
185 received_packet->ssrc = | 174 received_packet->ssrc = |
186 ByteReader<uint32_t>::ReadBigEndian(&incoming_rtp_packet[8]); | 175 ByteReader<uint32_t>::ReadBigEndian(&incoming_rtp_packet[8]); |
187 | 176 |
188 } else { | 177 } else { |
189 // copy the RTP header | 178 // Copy RTP header. |
190 memcpy(received_packet->pkt->data, incoming_rtp_packet, | 179 memcpy(received_packet->pkt->data, |
180 incoming_rtp_packet, | |
191 header.headerLength); | 181 header.headerLength); |
192 | 182 |
193 // replace the RED payload type | 183 // Set payload type. |
194 received_packet->pkt->data[1] &= 0x80; // reset the payload | 184 received_packet->pkt->data[1] &= 0x80; // reset RED payload type |
danilchap
2016/07/19 14:55:32
fix this 2 comments too.
brandtr
2016/07/21 09:03:58
Done.
| |
195 received_packet->pkt->data[1] += | 185 received_packet->pkt->data[1] += payload_type; // set media payload type |
196 payload_type; // set the media payload type | |
197 | 186 |
198 // copy the media payload data | 187 // Copy payload data. |
199 memcpy( | 188 memcpy( |
200 received_packet->pkt->data + header.headerLength, | 189 received_packet->pkt->data + header.headerLength, |
201 incoming_rtp_packet + header.headerLength + REDHeaderLength, | 190 incoming_rtp_packet + header.headerLength + red_header_length, |
202 payload_data_length - REDHeaderLength); | 191 payload_data_length - red_header_length); |
203 | |
204 received_packet->pkt->length = | 192 received_packet->pkt->length = |
205 header.headerLength + payload_data_length - REDHeaderLength; | 193 header.headerLength + payload_data_length - red_header_length; |
206 } | 194 } |
207 | 195 |
208 if (received_packet->pkt->length == 0) { | 196 if (received_packet->pkt->length == 0) { |
209 return 0; | 197 return 0; |
210 } | 198 } |
211 | 199 |
212 received_packet_list_.push_back(received_packet.release()); | 200 received_packets_.push_back(received_packet.release()); |
213 if (second_received_packet) { | 201 if (second_received_packet) { |
214 received_packet_list_.push_back(second_received_packet.release()); | 202 received_packets_.push_back(second_received_packet.release()); |
215 } | 203 } |
216 return 0; | 204 return 0; |
217 } | 205 } |
218 | 206 |
219 int32_t FecReceiverImpl::ProcessReceivedFec() { | 207 int32_t FecReceiverImpl::ProcessReceivedFec() { |
220 crit_sect_.Enter(); | 208 crit_sect_.Enter(); |
221 if (!received_packet_list_.empty()) { | 209 if (!received_packets_.empty()) { |
222 // Send received media packet to VCM. | 210 // Send received media packet to VCM. |
223 if (!received_packet_list_.front()->is_fec) { | 211 if (!received_packets_.front()->is_fec) { |
224 ForwardErrorCorrection::Packet* packet = | 212 ForwardErrorCorrection::Packet* packet = |
225 received_packet_list_.front()->pkt; | 213 received_packets_.front()->pkt; |
226 crit_sect_.Leave(); | 214 crit_sect_.Leave(); |
227 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, | 215 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, |
228 packet->length)) { | 216 packet->length)) { |
229 return -1; | 217 return -1; |
230 } | 218 } |
231 crit_sect_.Enter(); | 219 crit_sect_.Enter(); |
232 } | 220 } |
233 if (fec_->DecodeFec(&received_packet_list_, &recovered_packet_list_) != 0) { | 221 if (fec_.DecodeFec(&received_packets_, &recovered_packets_) != 0) { |
234 crit_sect_.Leave(); | 222 crit_sect_.Leave(); |
235 return -1; | 223 return -1; |
236 } | 224 } |
237 RTC_DCHECK(received_packet_list_.empty()); | 225 RTC_DCHECK(received_packets_.empty()); |
238 } | 226 } |
239 // Send any recovered media packets to VCM. | 227 // Send any recovered media packets to VCM. |
240 for(auto* recovered_packet : recovered_packet_list_) { | 228 for (auto* recovered_packet : recovered_packets_) { |
241 if (recovered_packet->returned) { | 229 if (recovered_packet->returned) { |
242 // Already sent to the VCM and the jitter buffer. | 230 // Already sent to the VCM and the jitter buffer. |
243 continue; | 231 continue; |
244 } | 232 } |
245 ForwardErrorCorrection::Packet* packet = recovered_packet->pkt; | 233 ForwardErrorCorrection::Packet* packet = recovered_packet->pkt; |
246 ++packet_counter_.num_recovered_packets; | 234 ++packet_counter_.num_recovered_packets; |
247 crit_sect_.Leave(); | 235 crit_sect_.Leave(); |
248 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, | 236 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, |
249 packet->length)) { | 237 packet->length)) { |
250 return -1; | 238 return -1; |
251 } | 239 } |
252 crit_sect_.Enter(); | 240 crit_sect_.Enter(); |
253 recovered_packet->returned = true; | 241 recovered_packet->returned = true; |
254 } | 242 } |
255 crit_sect_.Leave(); | 243 crit_sect_.Leave(); |
256 return 0; | 244 return 0; |
257 } | 245 } |
258 | 246 |
259 } // namespace webrtc | 247 } // namespace webrtc |
OLD | NEW |