blob: 80e0302d018f78c4acf6d7c754a0e99f6a3a4ea9 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
kwiberg84be5112016-04-27 01:19:58 -070015#include <memory>
brandtr35c480c2016-08-09 01:23:23 -070016#include <utility>
kwiberg84be5112016-04-27 01:19:58 -070017
Mirko Bonadeid9708072019-01-25 20:26:48 +010018#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/rtp_rtcp/source/byte_io.h"
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020020#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/logging.h"
Niels Möllerb5997872019-01-23 08:45:57 +010022#include "rtc_base/time_utils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
niklase@google.com470e71d2011-07-07 08:21:25 +000024namespace webrtc {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000025
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020026std::unique_ptr<UlpfecReceiver> UlpfecReceiver::Create(
27 uint32_t ssrc,
28 RecoveredPacketReceiver* callback,
29 rtc::ArrayView<const RtpExtension> extensions) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020030 return std::make_unique<UlpfecReceiverImpl>(ssrc, callback, extensions);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000031}
32
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020033UlpfecReceiverImpl::UlpfecReceiverImpl(
34 uint32_t ssrc,
35 RecoveredPacketReceiver* callback,
36 rtc::ArrayView<const RtpExtension> extensions)
brandtrd726a3f2017-06-29 02:45:35 -070037 : ssrc_(ssrc),
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020038 extensions_(extensions),
brandtrd726a3f2017-06-29 02:45:35 -070039 recovered_packet_callback_(callback),
40 fec_(ForwardErrorCorrection::CreateUlpfec(ssrc_)) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000041
brandtrd55c3f62016-10-31 04:51:33 -070042UlpfecReceiverImpl::~UlpfecReceiverImpl() {
brandtr74811e52016-08-10 00:51:50 -070043 received_packets_.clear();
Rasmus Brandt78db1582016-09-21 09:19:34 +020044 fec_->ResetState(&recovered_packets_);
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
brandtrd55c3f62016-10-31 04:51:33 -070047FecPacketCounter UlpfecReceiverImpl::GetPacketCounter() const {
danilchap7c9426c2016-04-14 03:05:31 -070048 rtc::CritScope cs(&crit_sect_);
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000049 return packet_counter_;
50}
51
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +000052// 0 1 2 3
53// 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
54// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55// |F| block PT | timestamp offset | block length |
56// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57//
58//
59// RFC 2198 RTP Payload for Redundant Audio Data September 1997
60//
61// The bits in the header are specified as follows:
62//
63// F: 1 bit First bit in header indicates whether another header block
64// follows. If 1 further header blocks follow, if 0 this is the
65// last header block.
66// If 0 there is only 1 byte RED header
67//
68// block PT: 7 bits RTP payload type for this block.
69//
70// timestamp offset: 14 bits Unsigned offset of timestamp of this block
71// relative to timestamp given in RTP header. The use of an unsigned
72// offset implies that redundant data must be sent after the primary
73// data, and is hence a time to be subtracted from the current
74// timestamp to determine the timestamp of the data for which this
75// block is the redundancy.
76//
77// block length: 10 bits Length in bytes of the corresponding data
78// block excluding header.
niklase@google.com470e71d2011-07-07 08:21:25 +000079
brandtrd55c3f62016-10-31 04:51:33 -070080int32_t UlpfecReceiverImpl::AddReceivedRedPacket(
81 const RTPHeader& header,
82 const uint8_t* incoming_rtp_packet,
83 size_t packet_length,
84 uint8_t ulpfec_payload_type) {
brandtrd726a3f2017-06-29 02:45:35 -070085 if (header.ssrc != ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010086 RTC_LOG(LS_WARNING)
brandtrd726a3f2017-06-29 02:45:35 -070087 << "Received RED packet with different SSRC than expected; dropping.";
88 return -1;
89 }
Ying Wang7a84fcf2018-05-18 13:48:58 +020090 if (packet_length > IP_PACKET_SIZE) {
91 RTC_LOG(LS_WARNING) << "Received RED packet with length exceeds maximum IP "
92 "packet size; dropping.";
93 return -1;
94 }
danilchap7c9426c2016-04-14 03:05:31 -070095 rtc::CritScope cs(&crit_sect_);
brandtr74811e52016-08-10 00:51:50 -070096
97 uint8_t red_header_length = 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000098 size_t payload_data_length = packet_length - header.headerLength;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +000099
pbos70d5c472015-06-29 07:22:04 -0700100 if (payload_data_length == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100101 RTC_LOG(LS_WARNING) << "Corrupt/truncated FEC packet.";
pbos70d5c472015-06-29 07:22:04 -0700102 return -1;
103 }
104
brandtr74811e52016-08-10 00:51:50 -0700105 // Remove RED header of incoming packet and store as a virtual RTP packet.
kwiberg84be5112016-04-27 01:19:58 -0700106 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet(
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200107 new ForwardErrorCorrection::ReceivedPacket());
108 received_packet->pkt = new ForwardErrorCorrection::Packet();
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000109
brandtr74811e52016-08-10 00:51:50 -0700110 // Get payload type from RED header and sequence number from RTP header.
111 uint8_t payload_type = incoming_rtp_packet[header.headerLength] & 0x7f;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000112 received_packet->is_fec = payload_type == ulpfec_payload_type;
brandtrd726a3f2017-06-29 02:45:35 -0700113 received_packet->ssrc = header.ssrc;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000114 received_packet->seq_num = header.sequenceNumber;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000115
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000116 if (incoming_rtp_packet[header.headerLength] & 0x80) {
brandtr74811e52016-08-10 00:51:50 -0700117 // f bit set in RED header, i.e. there are more than one RED header blocks.
Ilya Nikolaevskiy36c8ef62019-06-27 10:08:50 +0200118 // WebRTC never generates multiple blocks in a RED packet for FEC.
119 RTC_LOG(LS_WARNING) << "More than 1 block in RED packet is not supported.";
120 return -1;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000121 }
Ilya Nikolaevskiy36c8ef62019-06-27 10:08:50 +0200122
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000123 ++packet_counter_.num_packets;
Niels Möllercaef51e2019-08-27 09:19:49 +0200124 packet_counter_.num_bytes += packet_length;
asapersson0c43f772016-11-30 01:42:26 -0800125 if (packet_counter_.first_packet_time_ms == -1) {
Niels Möllerb5997872019-01-23 08:45:57 +0100126 packet_counter_.first_packet_time_ms = rtc::TimeMillis();
asapersson0c43f772016-11-30 01:42:26 -0800127 }
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000128
Ilya Nikolaevskiy36c8ef62019-06-27 10:08:50 +0200129 if (received_packet->is_fec) {
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000130 ++packet_counter_.num_fec_packets;
Ying Wang7a84fcf2018-05-18 13:48:58 +0200131
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000132 // everything behind the RED header
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200133 received_packet->pkt->data.SetData(
134 incoming_rtp_packet + header.headerLength + red_header_length,
135 payload_data_length - red_header_length);
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000136 received_packet->ssrc =
sprang@webrtc.org779c3d12015-03-17 16:42:49 +0000137 ByteReader<uint32_t>::ReadBigEndian(&incoming_rtp_packet[8]);
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000138
139 } else {
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200140 received_packet->pkt->data.SetSize(header.headerLength +
141 payload_data_length - red_header_length);
brandtr74811e52016-08-10 00:51:50 -0700142 // Copy RTP header.
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200143 memcpy(received_packet->pkt->data.data(), incoming_rtp_packet,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000144 header.headerLength);
brandtr74811e52016-08-10 00:51:50 -0700145 // Set payload type.
146 received_packet->pkt->data[1] &= 0x80; // Reset RED payload type.
147 received_packet->pkt->data[1] += payload_type; // Set media payload type.
brandtr74811e52016-08-10 00:51:50 -0700148 // Copy payload data.
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200149 if (payload_data_length > red_header_length) {
150 memcpy(received_packet->pkt->data.data() + header.headerLength,
151 incoming_rtp_packet + header.headerLength + red_header_length,
152 payload_data_length - red_header_length);
153 }
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000154 }
155
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200156 if (received_packet->pkt->data.size() == 0) {
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000157 return 0;
158 }
159
brandtr74811e52016-08-10 00:51:50 -0700160 received_packets_.push_back(std::move(received_packet));
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000161 return 0;
162}
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
nissea5f043f2017-09-18 07:58:59 -0700164// TODO(nisse): Drop always-zero return value.
brandtrd55c3f62016-10-31 04:51:33 -0700165int32_t UlpfecReceiverImpl::ProcessReceivedFec() {
danilchap7c9426c2016-04-14 03:05:31 -0700166 crit_sect_.Enter();
philipeld8f6c162018-01-19 14:41:41 +0100167
168 // If we iterate over |received_packets_| and it contains a packet that cause
169 // us to recurse back to this function (for example a RED packet encapsulating
170 // a RED packet), then we will recurse forever. To avoid this we swap
171 // |received_packets_| with an empty vector so that the next recursive call
172 // wont iterate over the same packet again. This also solves the problem of
173 // not modifying the vector we are currently iterating over (packets are added
174 // in AddReceivedRedPacket).
175 std::vector<std::unique_ptr<ForwardErrorCorrection::ReceivedPacket>>
Yves Gerey665174f2018-06-19 15:03:05 +0200176 received_packets;
philipeld8f6c162018-01-19 14:41:41 +0100177 received_packets.swap(received_packets_);
178
179 for (const auto& received_packet : received_packets) {
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000180 // Send received media packet to VCM.
nissea5f043f2017-09-18 07:58:59 -0700181 if (!received_packet->is_fec) {
182 ForwardErrorCorrection::Packet* packet = received_packet->pkt;
danilchap7c9426c2016-04-14 03:05:31 -0700183 crit_sect_.Leave();
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200184 recovered_packet_callback_->OnRecoveredPacket(packet->data.data(),
185 packet->data.size());
danilchap7c9426c2016-04-14 03:05:31 -0700186 crit_sect_.Enter();
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200187 // Create a packet with the buffer to modify it.
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +0200188 RtpPacketReceived rtp_packet;
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200189 rtp_packet.Parse(packet->data);
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +0200190 rtp_packet.IdentifyExtensions(extensions_);
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200191 // Reset buffer reference, so zeroing would work on a buffer with a
192 // single reference.
193 packet->data = rtc::CopyOnWriteBuffer(0);
194 rtp_packet.ZeroMutableExtensions();
195 packet->data = rtp_packet.Buffer();
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000196 }
nissea5f043f2017-09-18 07:58:59 -0700197 fec_->DecodeFec(*received_packet, &recovered_packets_);
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000198 }
nissea5f043f2017-09-18 07:58:59 -0700199
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000200 // Send any recovered media packets to VCM.
brandtr74811e52016-08-10 00:51:50 -0700201 for (const auto& recovered_packet : recovered_packets_) {
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200202 if (recovered_packet->returned) {
203 // Already sent to the VCM and the jitter buffer.
stefan@webrtc.org7adab092012-02-09 12:34:52 +0000204 continue;
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200205 }
206 ForwardErrorCorrection::Packet* packet = recovered_packet->pkt;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000207 ++packet_counter_.num_recovered_packets;
nisse41476e02017-08-25 09:08:44 -0700208 // Set this flag first; in case the recovered packet carries a RED
209 // header, OnRecoveredPacket will recurse back here.
210 recovered_packet->returned = true;
danilchap7c9426c2016-04-14 03:05:31 -0700211 crit_sect_.Leave();
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200212 recovered_packet_callback_->OnRecoveredPacket(packet->data.data(),
213 packet->data.size());
danilchap7c9426c2016-04-14 03:05:31 -0700214 crit_sect_.Enter();
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000215 }
philipeld8f6c162018-01-19 14:41:41 +0100216
danilchap7c9426c2016-04-14 03:05:31 -0700217 crit_sect_.Leave();
marpan@webrtc.org57353a32011-12-16 17:21:09 +0000218 return 0;
219}
stefan@webrtc.org7adab092012-02-09 12:34:52 +0000220
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000221} // namespace webrtc