| 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 received_packet_list_.clear(); | 30 received_packets_.clear(); |
| 33 if (fec_ != NULL) { | 31 fec_.ResetState(&recovered_packets_); |
| 34 fec_->ResetState(&recovered_packet_list_); | |
| 35 delete fec_; | |
| 36 } | |
| 37 } | 32 } |
| 38 | 33 |
| 39 FecPacketCounter FecReceiverImpl::GetPacketCounter() const { | 34 FecPacketCounter FecReceiverImpl::GetPacketCounter() const { |
| 40 rtc::CritScope cs(&crit_sect_); | 35 rtc::CritScope cs(&crit_sect_); |
| 41 return packet_counter_; | 36 return packet_counter_; |
| 42 } | 37 } |
| 43 | 38 |
| 44 // 0 1 2 3 | 39 // 0 1 2 3 |
| 45 // 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 | 40 // 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 |
| 46 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 41 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| (...skipping 19 matching lines...) Expand all Loading... |
| 66 // timestamp to determine the timestamp of the data for which this | 61 // timestamp to determine the timestamp of the data for which this |
| 67 // block is the redundancy. | 62 // block is the redundancy. |
| 68 // | 63 // |
| 69 // block length: 10 bits Length in bytes of the corresponding data | 64 // block length: 10 bits Length in bytes of the corresponding data |
| 70 // block excluding header. | 65 // block excluding header. |
| 71 | 66 |
| 72 int32_t FecReceiverImpl::AddReceivedRedPacket( | 67 int32_t FecReceiverImpl::AddReceivedRedPacket( |
| 73 const RTPHeader& header, const uint8_t* incoming_rtp_packet, | 68 const RTPHeader& header, const uint8_t* incoming_rtp_packet, |
| 74 size_t packet_length, uint8_t ulpfec_payload_type) { | 69 size_t packet_length, uint8_t ulpfec_payload_type) { |
| 75 rtc::CritScope cs(&crit_sect_); | 70 rtc::CritScope cs(&crit_sect_); |
| 76 uint8_t REDHeaderLength = 1; | 71 |
| 72 uint8_t red_header_length = 1; |
| 77 size_t payload_data_length = packet_length - header.headerLength; | 73 size_t payload_data_length = packet_length - header.headerLength; |
| 78 | 74 |
| 79 if (payload_data_length == 0) { | 75 if (payload_data_length == 0) { |
| 80 LOG(LS_WARNING) << "Corrupt/truncated FEC packet."; | 76 LOG(LS_WARNING) << "Corrupt/truncated FEC packet."; |
| 81 return -1; | 77 return -1; |
| 82 } | 78 } |
| 83 | 79 |
| 84 // Add to list without RED header, aka a virtual RTP packet | 80 // Remove RED header of incoming packet and store as a virtual RTP packet. |
| 85 // we remove the RED header | |
| 86 | |
| 87 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet( | 81 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet( |
| 88 new ForwardErrorCorrection::ReceivedPacket()); | 82 new ForwardErrorCorrection::ReceivedPacket()); |
| 89 received_packet->pkt = new ForwardErrorCorrection::Packet(); | 83 received_packet->pkt = new ForwardErrorCorrection::Packet(); |
| 90 | 84 |
| 91 // get payload type from RED header | 85 // Get payload type from RED header and sequence number from RTP header. |
| 92 uint8_t payload_type = | 86 uint8_t payload_type = incoming_rtp_packet[header.headerLength] & 0x7f; |
| 93 incoming_rtp_packet[header.headerLength] & 0x7f; | |
| 94 | |
| 95 received_packet->is_fec = payload_type == ulpfec_payload_type; | 87 received_packet->is_fec = payload_type == ulpfec_payload_type; |
| 96 received_packet->seq_num = header.sequenceNumber; | 88 received_packet->seq_num = header.sequenceNumber; |
| 97 | 89 |
| 98 uint16_t blockLength = 0; | 90 uint16_t block_length = 0; |
| 99 if (incoming_rtp_packet[header.headerLength] & 0x80) { | 91 if (incoming_rtp_packet[header.headerLength] & 0x80) { |
| 100 // f bit set in RED header | 92 // f bit set in RED header, i.e. there are more than one RED header blocks. |
| 101 REDHeaderLength = 4; | 93 red_header_length = 4; |
| 102 if (payload_data_length < REDHeaderLength + 1u) { | 94 if (payload_data_length < red_header_length + 1u) { |
| 103 LOG(LS_WARNING) << "Corrupt/truncated FEC packet."; | 95 LOG(LS_WARNING) << "Corrupt/truncated FEC packet."; |
| 104 return -1; | 96 return -1; |
| 105 } | 97 } |
| 106 | 98 |
| 107 uint16_t timestamp_offset = | 99 uint16_t timestamp_offset = |
| 108 (incoming_rtp_packet[header.headerLength + 1]) << 8; | 100 incoming_rtp_packet[header.headerLength + 1] << 8; |
| 109 timestamp_offset += | 101 timestamp_offset += |
| 110 incoming_rtp_packet[header.headerLength + 2]; | 102 incoming_rtp_packet[header.headerLength + 2]; |
| 111 timestamp_offset = timestamp_offset >> 2; | 103 timestamp_offset = timestamp_offset >> 2; |
| 112 if (timestamp_offset != 0) { | 104 if (timestamp_offset != 0) { |
| 113 LOG(LS_WARNING) << "Corrupt payload found."; | 105 LOG(LS_WARNING) << "Corrupt payload found."; |
| 114 return -1; | 106 return -1; |
| 115 } | 107 } |
| 116 | 108 |
| 117 blockLength = | 109 block_length = (0x3 & incoming_rtp_packet[header.headerLength + 2]) << 8; |
| 118 (0x03 & incoming_rtp_packet[header.headerLength + 2]) << 8; | 110 block_length += incoming_rtp_packet[header.headerLength + 3]; |
| 119 blockLength += (incoming_rtp_packet[header.headerLength + 3]); | |
| 120 | 111 |
| 121 // check next RED header | 112 // Check next RED header block. |
| 122 if (incoming_rtp_packet[header.headerLength + 4] & 0x80) { | 113 if (incoming_rtp_packet[header.headerLength + 4] & 0x80) { |
| 123 LOG(LS_WARNING) << "More than 2 blocks in packet not supported."; | 114 LOG(LS_WARNING) << "More than 2 blocks in packet not supported."; |
| 124 return -1; | 115 return -1; |
| 125 } | 116 } |
| 126 // Check that the packet is long enough to contain data in the following | 117 // Check that the packet is long enough to contain data in the following |
| 127 // block. | 118 // block. |
| 128 if (blockLength > payload_data_length - (REDHeaderLength + 1)) { | 119 if (block_length > payload_data_length - (red_header_length + 1)) { |
| 129 LOG(LS_WARNING) << "Block length longer than packet."; | 120 LOG(LS_WARNING) << "Block length longer than packet."; |
| 130 return -1; | 121 return -1; |
| 131 } | 122 } |
| 132 } | 123 } |
| 133 ++packet_counter_.num_packets; | 124 ++packet_counter_.num_packets; |
| 134 | 125 |
| 135 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> | 126 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> |
| 136 second_received_packet; | 127 second_received_packet; |
| 137 if (blockLength > 0) { | 128 if (block_length > 0) { |
| 138 // handle block length, split into 2 packets | 129 // Handle block length, split into two packets. |
| 139 REDHeaderLength = 5; | 130 red_header_length = 5; |
| 140 | 131 |
| 141 // copy the RTP header | 132 // Copy RTP header. |
| 142 memcpy(received_packet->pkt->data, incoming_rtp_packet, | 133 memcpy(received_packet->pkt->data, incoming_rtp_packet, |
| 143 header.headerLength); | 134 header.headerLength); |
| 144 | 135 |
| 145 // replace the RED payload type | 136 // Set payload type. |
| 146 received_packet->pkt->data[1] &= 0x80; // reset the payload | 137 received_packet->pkt->data[1] &= 0x80; // reset RED payload type |
| 147 received_packet->pkt->data[1] += | 138 received_packet->pkt->data[1] += payload_type; // set media payload type |
| 148 payload_type; // set the media payload type | |
| 149 | 139 |
| 150 // copy the payload data | 140 // Copy payload data. |
| 151 memcpy( | 141 memcpy( |
| 152 received_packet->pkt->data + header.headerLength, | 142 received_packet->pkt->data + header.headerLength, |
| 153 incoming_rtp_packet + header.headerLength + REDHeaderLength, | 143 incoming_rtp_packet + header.headerLength + red_header_length, |
| 154 blockLength); | 144 block_length); |
| 145 received_packet->pkt->length = block_length; |
| 155 | 146 |
| 156 received_packet->pkt->length = blockLength; | 147 second_received_packet.reset(new ForwardErrorCorrection::ReceivedPacket); |
| 157 | 148 second_received_packet->pkt = new ForwardErrorCorrection::Packet; |
| 158 second_received_packet.reset(new ForwardErrorCorrection::ReceivedPacket()); | |
| 159 second_received_packet->pkt = new ForwardErrorCorrection::Packet(); | |
| 160 | 149 |
| 161 second_received_packet->is_fec = true; | 150 second_received_packet->is_fec = true; |
| 162 second_received_packet->seq_num = header.sequenceNumber; | 151 second_received_packet->seq_num = header.sequenceNumber; |
| 163 ++packet_counter_.num_fec_packets; | 152 ++packet_counter_.num_fec_packets; |
| 164 | 153 |
| 165 // copy the FEC payload data | 154 // Copy FEC payload data. |
| 166 memcpy(second_received_packet->pkt->data, | 155 memcpy(second_received_packet->pkt->data, |
| 167 incoming_rtp_packet + header.headerLength + | 156 incoming_rtp_packet + header.headerLength + |
| 168 REDHeaderLength + blockLength, | 157 red_header_length + block_length, |
| 169 payload_data_length - REDHeaderLength - blockLength); | 158 payload_data_length - red_header_length - block_length); |
| 170 | 159 |
| 171 second_received_packet->pkt->length = | 160 second_received_packet->pkt->length = |
| 172 payload_data_length - REDHeaderLength - blockLength; | 161 payload_data_length - red_header_length - block_length; |
| 173 | 162 |
| 174 } else if (received_packet->is_fec) { | 163 } else if (received_packet->is_fec) { |
| 175 ++packet_counter_.num_fec_packets; | 164 ++packet_counter_.num_fec_packets; |
| 176 // everything behind the RED header | 165 // everything behind the RED header |
| 177 memcpy( | 166 memcpy( |
| 178 received_packet->pkt->data, | 167 received_packet->pkt->data, |
| 179 incoming_rtp_packet + header.headerLength + REDHeaderLength, | 168 incoming_rtp_packet + header.headerLength + red_header_length, |
| 180 payload_data_length - REDHeaderLength); | 169 payload_data_length - red_header_length); |
| 181 received_packet->pkt->length = payload_data_length - REDHeaderLength; | 170 received_packet->pkt->length = payload_data_length - red_header_length; |
| 182 received_packet->ssrc = | 171 received_packet->ssrc = |
| 183 ByteReader<uint32_t>::ReadBigEndian(&incoming_rtp_packet[8]); | 172 ByteReader<uint32_t>::ReadBigEndian(&incoming_rtp_packet[8]); |
| 184 | 173 |
| 185 } else { | 174 } else { |
| 186 // copy the RTP header | 175 // Copy RTP header. |
| 187 memcpy(received_packet->pkt->data, incoming_rtp_packet, | 176 memcpy(received_packet->pkt->data, |
| 177 incoming_rtp_packet, |
| 188 header.headerLength); | 178 header.headerLength); |
| 189 | 179 |
| 190 // replace the RED payload type | 180 // Set payload type. |
| 191 received_packet->pkt->data[1] &= 0x80; // reset the payload | 181 received_packet->pkt->data[1] &= 0x80; // reset RED payload type |
| 192 received_packet->pkt->data[1] += | 182 received_packet->pkt->data[1] += payload_type; // set media payload type |
| 193 payload_type; // set the media payload type | |
| 194 | 183 |
| 195 // copy the media payload data | 184 // Copy payload data. |
| 196 memcpy( | 185 memcpy( |
| 197 received_packet->pkt->data + header.headerLength, | 186 received_packet->pkt->data + header.headerLength, |
| 198 incoming_rtp_packet + header.headerLength + REDHeaderLength, | 187 incoming_rtp_packet + header.headerLength + red_header_length, |
| 199 payload_data_length - REDHeaderLength); | 188 payload_data_length - red_header_length); |
| 200 | |
| 201 received_packet->pkt->length = | 189 received_packet->pkt->length = |
| 202 header.headerLength + payload_data_length - REDHeaderLength; | 190 header.headerLength + payload_data_length - red_header_length; |
| 203 } | 191 } |
| 204 | 192 |
| 205 if (received_packet->pkt->length == 0) { | 193 if (received_packet->pkt->length == 0) { |
| 206 return 0; | 194 return 0; |
| 207 } | 195 } |
| 208 | 196 |
| 209 received_packet_list_.push_back(std::move(received_packet)); | 197 received_packets_.push_back(std::move(received_packet)); |
| 210 if (second_received_packet) { | 198 if (second_received_packet) { |
| 211 received_packet_list_.push_back(std::move(second_received_packet)); | 199 received_packets_.push_back(std::move(received_packet)); |
| 212 } | 200 } |
| 213 return 0; | 201 return 0; |
| 214 } | 202 } |
| 215 | 203 |
| 216 int32_t FecReceiverImpl::ProcessReceivedFec() { | 204 int32_t FecReceiverImpl::ProcessReceivedFec() { |
| 217 crit_sect_.Enter(); | 205 crit_sect_.Enter(); |
| 218 if (!received_packet_list_.empty()) { | 206 if (!received_packets_.empty()) { |
| 219 // Send received media packet to VCM. | 207 // Send received media packet to VCM. |
| 220 if (!received_packet_list_.front()->is_fec) { | 208 if (!received_packets_.front()->is_fec) { |
| 221 ForwardErrorCorrection::Packet* packet = | 209 ForwardErrorCorrection::Packet* packet = |
| 222 received_packet_list_.front()->pkt; | 210 received_packets_.front()->pkt; |
| 223 crit_sect_.Leave(); | 211 crit_sect_.Leave(); |
| 224 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, | 212 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, |
| 225 packet->length)) { | 213 packet->length)) { |
| 226 return -1; | 214 return -1; |
| 227 } | 215 } |
| 228 crit_sect_.Enter(); | 216 crit_sect_.Enter(); |
| 229 } | 217 } |
| 230 if (fec_->DecodeFec(&received_packet_list_, &recovered_packet_list_) != 0) { | 218 if (fec_.DecodeFec(&received_packets_, &recovered_packets_) != 0) { |
| 231 crit_sect_.Leave(); | 219 crit_sect_.Leave(); |
| 232 return -1; | 220 return -1; |
| 233 } | 221 } |
| 234 RTC_DCHECK(received_packet_list_.empty()); | 222 RTC_DCHECK(received_packets_.empty()); |
| 235 } | 223 } |
| 236 // Send any recovered media packets to VCM. | 224 // Send any recovered media packets to VCM. |
| 237 for(auto& recovered_packet : recovered_packet_list_) { | 225 for (auto& recovered_packet : recovered_packets_) { |
| 238 if (recovered_packet->returned) { | 226 if (recovered_packet->returned) { |
| 239 // Already sent to the VCM and the jitter buffer. | 227 // Already sent to the VCM and the jitter buffer. |
| 240 continue; | 228 continue; |
| 241 } | 229 } |
| 242 ForwardErrorCorrection::Packet* packet = recovered_packet->pkt; | 230 ForwardErrorCorrection::Packet* packet = recovered_packet->pkt; |
| 243 ++packet_counter_.num_recovered_packets; | 231 ++packet_counter_.num_recovered_packets; |
| 244 crit_sect_.Leave(); | 232 crit_sect_.Leave(); |
| 245 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, | 233 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, |
| 246 packet->length)) { | 234 packet->length)) { |
| 247 return -1; | 235 return -1; |
| 248 } | 236 } |
| 249 crit_sect_.Enter(); | 237 crit_sect_.Enter(); |
| 250 recovered_packet->returned = true; | 238 recovered_packet->returned = true; |
| 251 } | 239 } |
| 252 crit_sect_.Leave(); | 240 crit_sect_.Leave(); |
| 253 return 0; | 241 return 0; |
| 254 } | 242 } |
| 255 | 243 |
| 256 } // namespace webrtc | 244 } // namespace webrtc |
| OLD | NEW |