blob: 0bb4ad8a086c4ddcae329875720b20194834185b [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.org5dad00b2012-01-30 13:05:29 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/ulpfec_receiver_impl.h"
andrew@webrtc.org7fe219f2012-02-01 02:40:37 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
kwiberg84be5112016-04-27 01:19:58 -070014#include <memory>
brandtr35c480c2016-08-09 01:23:23 -070015#include <utility>
kwiberg84be5112016-04-27 01:19:58 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/rtp_rtcp/source/byte_io.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/logging.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "rtc_base/scoped_ref_ptr.h"
Niels Möllerb5997872019-01-23 08:45:57 +010020#include "rtc_base/time_utils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
niklase@google.com470e71d2011-07-07 08:21:25 +000022namespace webrtc {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000023
brandtrd726a3f2017-06-29 02:45:35 -070024UlpfecReceiver* UlpfecReceiver::Create(uint32_t ssrc,
25 RecoveredPacketReceiver* callback) {
26 return new UlpfecReceiverImpl(ssrc, callback);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000027}
28
brandtrd726a3f2017-06-29 02:45:35 -070029UlpfecReceiverImpl::UlpfecReceiverImpl(uint32_t ssrc,
30 RecoveredPacketReceiver* callback)
31 : ssrc_(ssrc),
32 recovered_packet_callback_(callback),
33 fec_(ForwardErrorCorrection::CreateUlpfec(ssrc_)) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000034
brandtrd55c3f62016-10-31 04:51:33 -070035UlpfecReceiverImpl::~UlpfecReceiverImpl() {
brandtr74811e52016-08-10 00:51:50 -070036 received_packets_.clear();
Rasmus Brandt78db1582016-09-21 09:19:34 +020037 fec_->ResetState(&recovered_packets_);
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
brandtrd55c3f62016-10-31 04:51:33 -070040FecPacketCounter UlpfecReceiverImpl::GetPacketCounter() const {
danilchap7c9426c2016-04-14 03:05:31 -070041 rtc::CritScope cs(&crit_sect_);
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000042 return packet_counter_;
43}
44
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +000045// 0 1 2 3
46// 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
47// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48// |F| block PT | timestamp offset | block length |
49// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50//
51//
52// RFC 2198 RTP Payload for Redundant Audio Data September 1997
53//
54// The bits in the header are specified as follows:
55//
56// F: 1 bit First bit in header indicates whether another header block
57// follows. If 1 further header blocks follow, if 0 this is the
58// last header block.
59// If 0 there is only 1 byte RED header
60//
61// block PT: 7 bits RTP payload type for this block.
62//
63// timestamp offset: 14 bits Unsigned offset of timestamp of this block
64// relative to timestamp given in RTP header. The use of an unsigned
65// offset implies that redundant data must be sent after the primary
66// data, and is hence a time to be subtracted from the current
67// timestamp to determine the timestamp of the data for which this
68// block is the redundancy.
69//
70// block length: 10 bits Length in bytes of the corresponding data
71// block excluding header.
niklase@google.com470e71d2011-07-07 08:21:25 +000072
brandtrd55c3f62016-10-31 04:51:33 -070073int32_t UlpfecReceiverImpl::AddReceivedRedPacket(
74 const RTPHeader& header,
75 const uint8_t* incoming_rtp_packet,
76 size_t packet_length,
77 uint8_t ulpfec_payload_type) {
brandtrd726a3f2017-06-29 02:45:35 -070078 if (header.ssrc != ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010079 RTC_LOG(LS_WARNING)
brandtrd726a3f2017-06-29 02:45:35 -070080 << "Received RED packet with different SSRC than expected; dropping.";
81 return -1;
82 }
Ying Wang7a84fcf2018-05-18 13:48:58 +020083 if (packet_length > IP_PACKET_SIZE) {
84 RTC_LOG(LS_WARNING) << "Received RED packet with length exceeds maximum IP "
85 "packet size; dropping.";
86 return -1;
87 }
danilchap7c9426c2016-04-14 03:05:31 -070088 rtc::CritScope cs(&crit_sect_);
brandtr74811e52016-08-10 00:51:50 -070089
90 uint8_t red_header_length = 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000091 size_t payload_data_length = packet_length - header.headerLength;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +000092
pbos70d5c472015-06-29 07:22:04 -070093 if (payload_data_length == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010094 RTC_LOG(LS_WARNING) << "Corrupt/truncated FEC packet.";
pbos70d5c472015-06-29 07:22:04 -070095 return -1;
96 }
97
brandtr74811e52016-08-10 00:51:50 -070098 // Remove RED header of incoming packet and store as a virtual RTP packet.
kwiberg84be5112016-04-27 01:19:58 -070099 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet(
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200100 new ForwardErrorCorrection::ReceivedPacket());
101 received_packet->pkt = new ForwardErrorCorrection::Packet();
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000102
brandtr74811e52016-08-10 00:51:50 -0700103 // Get payload type from RED header and sequence number from RTP header.
104 uint8_t payload_type = incoming_rtp_packet[header.headerLength] & 0x7f;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000105 received_packet->is_fec = payload_type == ulpfec_payload_type;
brandtrd726a3f2017-06-29 02:45:35 -0700106 received_packet->ssrc = header.ssrc;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000107 received_packet->seq_num = header.sequenceNumber;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000108
brandtr74811e52016-08-10 00:51:50 -0700109 uint16_t block_length = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000110 if (incoming_rtp_packet[header.headerLength] & 0x80) {
brandtr74811e52016-08-10 00:51:50 -0700111 // f bit set in RED header, i.e. there are more than one RED header blocks.
112 red_header_length = 4;
113 if (payload_data_length < red_header_length + 1u) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100114 RTC_LOG(LS_WARNING) << "Corrupt/truncated FEC packet.";
pbos70d5c472015-06-29 07:22:04 -0700115 return -1;
116 }
117
brandtr74811e52016-08-10 00:51:50 -0700118 uint16_t timestamp_offset = incoming_rtp_packet[header.headerLength + 1]
119 << 8;
brandtrd55c3f62016-10-31 04:51:33 -0700120 timestamp_offset += incoming_rtp_packet[header.headerLength + 2];
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000121 timestamp_offset = timestamp_offset >> 2;
122 if (timestamp_offset != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100123 RTC_LOG(LS_WARNING) << "Corrupt payload found.";
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000124 return -1;
125 }
punyabrata@webrtc.org6da8eeb2012-01-24 00:48:36 +0000126
brandtr74811e52016-08-10 00:51:50 -0700127 block_length = (0x3 & incoming_rtp_packet[header.headerLength + 2]) << 8;
128 block_length += incoming_rtp_packet[header.headerLength + 3];
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000129
brandtr74811e52016-08-10 00:51:50 -0700130 // Check next RED header block.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000131 if (incoming_rtp_packet[header.headerLength + 4] & 0x80) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100132 RTC_LOG(LS_WARNING) << "More than 2 blocks in packet not supported.";
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000133 return -1;
134 }
pbos2e43b262015-06-30 01:32:40 -0700135 // Check that the packet is long enough to contain data in the following
136 // block.
brandtr74811e52016-08-10 00:51:50 -0700137 if (block_length > payload_data_length - (red_header_length + 1)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100138 RTC_LOG(LS_WARNING) << "Block length longer than packet.";
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000139 return -1;
140 }
141 }
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000142 ++packet_counter_.num_packets;
asapersson0c43f772016-11-30 01:42:26 -0800143 if (packet_counter_.first_packet_time_ms == -1) {
Niels Möllerb5997872019-01-23 08:45:57 +0100144 packet_counter_.first_packet_time_ms = rtc::TimeMillis();
asapersson0c43f772016-11-30 01:42:26 -0800145 }
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000146
kwiberg84be5112016-04-27 01:19:58 -0700147 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket>
pbos70d5c472015-06-29 07:22:04 -0700148 second_received_packet;
brandtr74811e52016-08-10 00:51:50 -0700149 if (block_length > 0) {
150 // Handle block length, split into two packets.
151 red_header_length = 5;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000152
brandtr74811e52016-08-10 00:51:50 -0700153 // Copy RTP header.
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000154 memcpy(received_packet->pkt->data, incoming_rtp_packet,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000155 header.headerLength);
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000156
brandtr74811e52016-08-10 00:51:50 -0700157 // Set payload type.
158 received_packet->pkt->data[1] &= 0x80; // Reset RED payload type.
159 received_packet->pkt->data[1] += payload_type; // Set media payload type.
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000160
brandtr74811e52016-08-10 00:51:50 -0700161 // Copy payload data.
162 memcpy(received_packet->pkt->data + header.headerLength,
163 incoming_rtp_packet + header.headerLength + red_header_length,
164 block_length);
165 received_packet->pkt->length = block_length;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000166
brandtr74811e52016-08-10 00:51:50 -0700167 second_received_packet.reset(new ForwardErrorCorrection::ReceivedPacket);
168 second_received_packet->pkt = new ForwardErrorCorrection::Packet;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000169
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000170 second_received_packet->is_fec = true;
brandtrd726a3f2017-06-29 02:45:35 -0700171 second_received_packet->ssrc = header.ssrc;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000172 second_received_packet->seq_num = header.sequenceNumber;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000173 ++packet_counter_.num_fec_packets;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000174
brandtr74811e52016-08-10 00:51:50 -0700175 // Copy FEC payload data.
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000176 memcpy(second_received_packet->pkt->data,
brandtr74811e52016-08-10 00:51:50 -0700177 incoming_rtp_packet + header.headerLength + red_header_length +
178 block_length,
179 payload_data_length - red_header_length - block_length);
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000180
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000181 second_received_packet->pkt->length =
brandtr74811e52016-08-10 00:51:50 -0700182 payload_data_length - red_header_length - block_length;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000183
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000184 } else if (received_packet->is_fec) {
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000185 ++packet_counter_.num_fec_packets;
Ying Wang7a84fcf2018-05-18 13:48:58 +0200186
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000187 // everything behind the RED header
brandtr74811e52016-08-10 00:51:50 -0700188 memcpy(received_packet->pkt->data,
189 incoming_rtp_packet + header.headerLength + red_header_length,
190 payload_data_length - red_header_length);
191 received_packet->pkt->length = payload_data_length - red_header_length;
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000192 received_packet->ssrc =
sprang@webrtc.org779c3d12015-03-17 16:42:49 +0000193 ByteReader<uint32_t>::ReadBigEndian(&incoming_rtp_packet[8]);
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000194
195 } else {
brandtr74811e52016-08-10 00:51:50 -0700196 // Copy RTP header.
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000197 memcpy(received_packet->pkt->data, incoming_rtp_packet,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000198 header.headerLength);
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000199
brandtr74811e52016-08-10 00:51:50 -0700200 // Set payload type.
201 received_packet->pkt->data[1] &= 0x80; // Reset RED payload type.
202 received_packet->pkt->data[1] += payload_type; // Set media payload type.
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000203
brandtr74811e52016-08-10 00:51:50 -0700204 // Copy payload data.
205 memcpy(received_packet->pkt->data + header.headerLength,
206 incoming_rtp_packet + header.headerLength + red_header_length,
207 payload_data_length - red_header_length);
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000208 received_packet->pkt->length =
brandtr74811e52016-08-10 00:51:50 -0700209 header.headerLength + payload_data_length - red_header_length;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000210 }
211
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000212 if (received_packet->pkt->length == 0) {
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000213 return 0;
214 }
215
brandtr74811e52016-08-10 00:51:50 -0700216 received_packets_.push_back(std::move(received_packet));
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000217 if (second_received_packet) {
brandtr74811e52016-08-10 00:51:50 -0700218 received_packets_.push_back(std::move(second_received_packet));
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000219 }
220 return 0;
221}
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
nissea5f043f2017-09-18 07:58:59 -0700223// TODO(nisse): Drop always-zero return value.
brandtrd55c3f62016-10-31 04:51:33 -0700224int32_t UlpfecReceiverImpl::ProcessReceivedFec() {
danilchap7c9426c2016-04-14 03:05:31 -0700225 crit_sect_.Enter();
philipeld8f6c162018-01-19 14:41:41 +0100226
227 // If we iterate over |received_packets_| and it contains a packet that cause
228 // us to recurse back to this function (for example a RED packet encapsulating
229 // a RED packet), then we will recurse forever. To avoid this we swap
230 // |received_packets_| with an empty vector so that the next recursive call
231 // wont iterate over the same packet again. This also solves the problem of
232 // not modifying the vector we are currently iterating over (packets are added
233 // in AddReceivedRedPacket).
234 std::vector<std::unique_ptr<ForwardErrorCorrection::ReceivedPacket>>
Yves Gerey665174f2018-06-19 15:03:05 +0200235 received_packets;
philipeld8f6c162018-01-19 14:41:41 +0100236 received_packets.swap(received_packets_);
237
238 for (const auto& received_packet : received_packets) {
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000239 // Send received media packet to VCM.
nissea5f043f2017-09-18 07:58:59 -0700240 if (!received_packet->is_fec) {
241 ForwardErrorCorrection::Packet* packet = received_packet->pkt;
danilchap7c9426c2016-04-14 03:05:31 -0700242 crit_sect_.Leave();
nisse30e89312017-05-29 08:16:37 -0700243 recovered_packet_callback_->OnRecoveredPacket(packet->data,
244 packet->length);
danilchap7c9426c2016-04-14 03:05:31 -0700245 crit_sect_.Enter();
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000246 }
nissea5f043f2017-09-18 07:58:59 -0700247 fec_->DecodeFec(*received_packet, &recovered_packets_);
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000248 }
nissea5f043f2017-09-18 07:58:59 -0700249
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000250 // Send any recovered media packets to VCM.
brandtr74811e52016-08-10 00:51:50 -0700251 for (const auto& recovered_packet : recovered_packets_) {
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200252 if (recovered_packet->returned) {
253 // Already sent to the VCM and the jitter buffer.
stefan@webrtc.org7adab092012-02-09 12:34:52 +0000254 continue;
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200255 }
256 ForwardErrorCorrection::Packet* packet = recovered_packet->pkt;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000257 ++packet_counter_.num_recovered_packets;
nisse41476e02017-08-25 09:08:44 -0700258 // Set this flag first; in case the recovered packet carries a RED
259 // header, OnRecoveredPacket will recurse back here.
260 recovered_packet->returned = true;
danilchap7c9426c2016-04-14 03:05:31 -0700261 crit_sect_.Leave();
Yves Gerey665174f2018-06-19 15:03:05 +0200262 recovered_packet_callback_->OnRecoveredPacket(packet->data, packet->length);
danilchap7c9426c2016-04-14 03:05:31 -0700263 crit_sect_.Enter();
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000264 }
philipeld8f6c162018-01-19 14:41:41 +0100265
danilchap7c9426c2016-04-14 03:05:31 -0700266 crit_sect_.Leave();
marpan@webrtc.org57353a32011-12-16 17:21:09 +0000267 return 0;
268}
stefan@webrtc.org7adab092012-02-09 12:34:52 +0000269
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000270} // namespace webrtc