Chromium Code Reviews| 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; | |
|
philipel
2016/07/04 15:35:03
red_header_length
brandtr
2016/07/05 11:46:05
Done.
| |
| 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 = (0x03 & incoming_rtp_packet[header.headerLength + 2]) << 8; |
|
philipel
2016/07/04 15:35:03
0x3
brandtr
2016/07/05 11:46:05
Done.
| |
| 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> received_packet2; |
|
philipel
2016/07/04 15:35:04
Not 100% sure what this variable is suppose to hol
brandtr
2016/07/05 11:46:05
IIUC, this variable holds a second virtual RTP pac
| |
| 136 second_received_packet; | 127 if (block_length > 0) { |
| 137 if (blockLength > 0) { | 128 // Handle block length, split into two packets. |
| 138 // handle block length, split into 2 packets | 129 RED_header_length = 5; |
| 139 REDHeaderLength = 5; | |
| 140 | 130 |
| 141 // copy the RTP header | 131 // Copy RTP header. |
| 142 memcpy(received_packet->pkt->data, incoming_rtp_packet, | 132 memcpy(received_packet->pkt->data, incoming_rtp_packet, |
| 143 header.headerLength); | 133 header.headerLength); |
| 144 | 134 |
| 145 // replace the RED payload type | 135 // Set payload type. |
| 146 received_packet->pkt->data[1] &= 0x80; // reset the payload | 136 received_packet->pkt->data[1] &= 0x80; // reset RED payload type |
| 147 received_packet->pkt->data[1] += | 137 received_packet->pkt->data[1] += payload_type; // set media payload type |
| 148 payload_type; // set the media payload type | |
| 149 | 138 |
| 150 // copy the payload data | 139 // Copy payload data. |
| 151 memcpy( | 140 memcpy( |
| 152 received_packet->pkt->data + header.headerLength, | 141 received_packet->pkt->data + header.headerLength, |
| 153 incoming_rtp_packet + header.headerLength + REDHeaderLength, | 142 incoming_rtp_packet + header.headerLength + RED_header_length, |
| 154 blockLength); | 143 block_length); |
| 144 received_packet->pkt->length = block_length; | |
| 155 | 145 |
| 156 received_packet->pkt->length = blockLength; | 146 received_packet2.reset(new ForwardErrorCorrection::ReceivedPacket); |
| 147 received_packet2->pkt = new ForwardErrorCorrection::Packet; | |
| 157 | 148 |
| 158 second_received_packet.reset(new ForwardErrorCorrection::ReceivedPacket()); | 149 received_packet2->is_fec = true; |
| 159 second_received_packet->pkt = new ForwardErrorCorrection::Packet(); | 150 received_packet2->seq_num = header.sequenceNumber; |
| 160 | |
| 161 second_received_packet->is_fec = true; | |
| 162 second_received_packet->seq_num = header.sequenceNumber; | |
| 163 ++packet_counter_.num_fec_packets; | 151 ++packet_counter_.num_fec_packets; |
| 164 | 152 |
| 165 // copy the FEC payload data | 153 // Copy FEC payload data. |
| 166 memcpy(second_received_packet->pkt->data, | 154 memcpy(received_packet2->pkt->data, |
| 167 incoming_rtp_packet + header.headerLength + | 155 incoming_rtp_packet + header.headerLength + |
| 168 REDHeaderLength + blockLength, | 156 RED_header_length + block_length, |
| 169 payload_data_length - REDHeaderLength - blockLength); | 157 payload_data_length - RED_header_length - block_length); |
| 170 | 158 |
| 171 second_received_packet->pkt->length = | 159 received_packet2->pkt->length = |
| 172 payload_data_length - REDHeaderLength - blockLength; | 160 payload_data_length - RED_header_length - block_length; |
| 173 | 161 |
| 174 } else if (received_packet->is_fec) { | 162 } else if (received_packet->is_fec) { |
| 175 ++packet_counter_.num_fec_packets; | 163 ++packet_counter_.num_fec_packets; |
| 176 // everything behind the RED header | 164 // everything behind the RED header |
| 177 memcpy( | 165 memcpy( |
| 178 received_packet->pkt->data, | 166 received_packet->pkt->data, |
| 179 incoming_rtp_packet + header.headerLength + REDHeaderLength, | 167 incoming_rtp_packet + header.headerLength + RED_header_length, |
| 180 payload_data_length - REDHeaderLength); | 168 payload_data_length - RED_header_length); |
| 181 received_packet->pkt->length = payload_data_length - REDHeaderLength; | 169 received_packet->pkt->length = payload_data_length - RED_header_length; |
| 182 received_packet->ssrc = | 170 received_packet->ssrc = |
| 183 ByteReader<uint32_t>::ReadBigEndian(&incoming_rtp_packet[8]); | 171 ByteReader<uint32_t>::ReadBigEndian(&incoming_rtp_packet[8]); |
| 184 | 172 |
| 185 } else { | 173 } else { |
| 186 // copy the RTP header | 174 // Copy RTP header. |
| 187 memcpy(received_packet->pkt->data, incoming_rtp_packet, | 175 memcpy(received_packet->pkt->data, |
| 176 incoming_rtp_packet, | |
| 188 header.headerLength); | 177 header.headerLength); |
| 189 | 178 |
| 190 // replace the RED payload type | 179 // Set payload type. |
| 191 received_packet->pkt->data[1] &= 0x80; // reset the payload | 180 received_packet->pkt->data[1] &= 0x80; // reset RED payload type |
| 192 received_packet->pkt->data[1] += | 181 received_packet->pkt->data[1] += payload_type; // set media payload type |
| 193 payload_type; // set the media payload type | |
| 194 | 182 |
| 195 // copy the media payload data | 183 // Copy payload data. |
| 196 memcpy( | 184 memcpy( |
| 197 received_packet->pkt->data + header.headerLength, | 185 received_packet->pkt->data + header.headerLength, |
| 198 incoming_rtp_packet + header.headerLength + REDHeaderLength, | 186 incoming_rtp_packet + header.headerLength + RED_header_length, |
| 199 payload_data_length - REDHeaderLength); | 187 payload_data_length - RED_header_length); |
| 200 | |
| 201 received_packet->pkt->length = | 188 received_packet->pkt->length = |
| 202 header.headerLength + payload_data_length - REDHeaderLength; | 189 header.headerLength + payload_data_length - RED_header_length; |
| 203 } | 190 } |
| 204 | 191 |
| 205 if (received_packet->pkt->length == 0) { | 192 if (received_packet->pkt->length == 0) { |
| 206 return 0; | 193 return 0; |
| 207 } | 194 } |
| 208 | 195 |
| 209 received_packet_list_.push_back(std::move(received_packet)); | 196 received_packets_.push_back(std::move(received_packet)); |
| 210 if (second_received_packet) { | 197 if (received_packet2) { |
| 211 received_packet_list_.push_back(std::move(second_received_packet)); | 198 received_packets_.push_back(std::move(received_packet)); |
| 212 } | 199 } |
| 213 return 0; | 200 return 0; |
| 214 } | 201 } |
| 215 | 202 |
| 216 int32_t FecReceiverImpl::ProcessReceivedFec() { | 203 int32_t FecReceiverImpl::ProcessReceivedFec() { |
| 217 crit_sect_.Enter(); | 204 crit_sect_.Enter(); |
| 218 if (!received_packet_list_.empty()) { | 205 if (!received_packets_.empty()) { |
| 219 // Send received media packet to VCM. | 206 // Send received media packet to VCM. |
| 220 if (!received_packet_list_.front()->is_fec) { | 207 if (!received_packets_.front()->is_fec) { |
| 221 ForwardErrorCorrection::Packet* packet = | 208 ForwardErrorCorrection::Packet* packet = |
| 222 received_packet_list_.front()->pkt; | 209 received_packets_.front()->pkt; |
| 223 crit_sect_.Leave(); | 210 crit_sect_.Leave(); |
| 224 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, | 211 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, |
| 225 packet->length)) { | 212 packet->length)) { |
| 226 return -1; | 213 return -1; |
| 227 } | 214 } |
| 228 crit_sect_.Enter(); | 215 crit_sect_.Enter(); |
| 229 } | 216 } |
| 230 if (fec_->DecodeFec(&received_packet_list_, &recovered_packet_list_) != 0) { | 217 if (fec_.DecodeFec(&received_packets_, &recovered_packets_) != 0) { |
| 231 crit_sect_.Leave(); | 218 crit_sect_.Leave(); |
| 232 return -1; | 219 return -1; |
| 233 } | 220 } |
| 234 RTC_DCHECK(received_packet_list_.empty()); | 221 RTC_DCHECK(received_packets_.empty()); |
| 235 } | 222 } |
| 236 // Send any recovered media packets to VCM. | 223 // Send any recovered media packets to VCM. |
| 237 for(auto& recovered_packet : recovered_packet_list_) { | 224 for (auto& recovered_packet : recovered_packets_) { |
| 238 if (recovered_packet->returned) { | 225 if (recovered_packet->returned) { |
| 239 // Already sent to the VCM and the jitter buffer. | 226 // Already sent to the VCM and the jitter buffer. |
| 240 continue; | 227 continue; |
| 241 } | 228 } |
| 242 ForwardErrorCorrection::Packet* packet = recovered_packet->pkt; | 229 ForwardErrorCorrection::Packet* packet = recovered_packet->pkt; |
| 243 ++packet_counter_.num_recovered_packets; | 230 ++packet_counter_.num_recovered_packets; |
| 244 crit_sect_.Leave(); | 231 crit_sect_.Leave(); |
| 245 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, | 232 if (!recovered_packet_callback_->OnRecoveredPacket(packet->data, |
| 246 packet->length)) { | 233 packet->length)) { |
| 247 return -1; | 234 return -1; |
| 248 } | 235 } |
| 249 crit_sect_.Enter(); | 236 crit_sect_.Enter(); |
| 250 recovered_packet->returned = true; | 237 recovered_packet->returned = true; |
| 251 } | 238 } |
| 252 crit_sect_.Leave(); | 239 crit_sect_.Leave(); |
| 253 return 0; | 240 return 0; |
| 254 } | 241 } |
| 255 | 242 |
| 256 } // namespace webrtc | 243 } // namespace webrtc |
| OLD | NEW |