blob: ea85422ffe42f67fcc88a661353837e25f4f60d8 [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
kwiberg84be5112016-04-27 01:19:58 -070013#include <memory>
brandtr35c480c2016-08-09 01:23:23 -070014#include <utility>
kwiberg84be5112016-04-27 01:19:58 -070015
Mirko Bonadeid9708072019-01-25 20:26:48 +010016#include "api/scoped_refptr.h"
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020017#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/logging.h"
Niels Möllerb5997872019-01-23 08:45:57 +010019#include "rtc_base/time_utils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
niklase@google.com470e71d2011-07-07 08:21:25 +000021namespace webrtc {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000022
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020023std::unique_ptr<UlpfecReceiver> UlpfecReceiver::Create(
24 uint32_t ssrc,
25 RecoveredPacketReceiver* callback,
26 rtc::ArrayView<const RtpExtension> extensions) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020027 return std::make_unique<UlpfecReceiverImpl>(ssrc, callback, extensions);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000028}
29
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020030UlpfecReceiverImpl::UlpfecReceiverImpl(
31 uint32_t ssrc,
32 RecoveredPacketReceiver* callback,
33 rtc::ArrayView<const RtpExtension> extensions)
brandtrd726a3f2017-06-29 02:45:35 -070034 : ssrc_(ssrc),
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020035 extensions_(extensions),
brandtrd726a3f2017-06-29 02:45:35 -070036 recovered_packet_callback_(callback),
37 fec_(ForwardErrorCorrection::CreateUlpfec(ssrc_)) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000038
brandtrd55c3f62016-10-31 04:51:33 -070039UlpfecReceiverImpl::~UlpfecReceiverImpl() {
brandtr74811e52016-08-10 00:51:50 -070040 received_packets_.clear();
Rasmus Brandt78db1582016-09-21 09:19:34 +020041 fec_->ResetState(&recovered_packets_);
niklase@google.com470e71d2011-07-07 08:21:25 +000042}
43
brandtrd55c3f62016-10-31 04:51:33 -070044FecPacketCounter UlpfecReceiverImpl::GetPacketCounter() const {
danilchap7c9426c2016-04-14 03:05:31 -070045 rtc::CritScope cs(&crit_sect_);
asapersson@webrtc.org0800db72015-01-15 07:40:20 +000046 return packet_counter_;
47}
48
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +000049// 0 1 2 3
50// 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
51// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52// |F| block PT | timestamp offset | block length |
53// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54//
55//
56// RFC 2198 RTP Payload for Redundant Audio Data September 1997
57//
58// The bits in the header are specified as follows:
59//
60// F: 1 bit First bit in header indicates whether another header block
61// follows. If 1 further header blocks follow, if 0 this is the
62// last header block.
63// If 0 there is only 1 byte RED header
64//
65// block PT: 7 bits RTP payload type for this block.
66//
67// timestamp offset: 14 bits Unsigned offset of timestamp of this block
68// relative to timestamp given in RTP header. The use of an unsigned
69// offset implies that redundant data must be sent after the primary
70// data, and is hence a time to be subtracted from the current
71// timestamp to determine the timestamp of the data for which this
72// block is the redundancy.
73//
74// block length: 10 bits Length in bytes of the corresponding data
75// block excluding header.
niklase@google.com470e71d2011-07-07 08:21:25 +000076
Danil Chapovalov04fd2152019-09-20 11:40:12 +020077bool UlpfecReceiverImpl::AddReceivedRedPacket(const RtpPacket& rtp_packet,
78 uint8_t ulpfec_payload_type) {
79 if (rtp_packet.Ssrc() != ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010080 RTC_LOG(LS_WARNING)
brandtrd726a3f2017-06-29 02:45:35 -070081 << "Received RED packet with different SSRC than expected; dropping.";
Danil Chapovalov04fd2152019-09-20 11:40:12 +020082 return false;
brandtrd726a3f2017-06-29 02:45:35 -070083 }
Danil Chapovalov04fd2152019-09-20 11:40:12 +020084 if (rtp_packet.size() > IP_PACKET_SIZE) {
Ying Wang7a84fcf2018-05-18 13:48:58 +020085 RTC_LOG(LS_WARNING) << "Received RED packet with length exceeds maximum IP "
86 "packet size; dropping.";
Danil Chapovalov04fd2152019-09-20 11:40:12 +020087 return false;
Ying Wang7a84fcf2018-05-18 13:48:58 +020088 }
danilchap7c9426c2016-04-14 03:05:31 -070089 rtc::CritScope cs(&crit_sect_);
brandtr74811e52016-08-10 00:51:50 -070090
Danil Chapovalov04fd2152019-09-20 11:40:12 +020091 static constexpr uint8_t kRedHeaderLength = 1;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +000092
Danil Chapovalov04fd2152019-09-20 11:40:12 +020093 if (rtp_packet.payload_size() == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010094 RTC_LOG(LS_WARNING) << "Corrupt/truncated FEC packet.";
Danil Chapovalov04fd2152019-09-20 11:40:12 +020095 return false;
pbos70d5c472015-06-29 07:22:04 -070096 }
97
brandtr74811e52016-08-10 00:51:50 -070098 // Remove RED header of incoming packet and store as a virtual RTP packet.
Danil Chapovalov04fd2152019-09-20 11:40:12 +020099 auto received_packet =
100 std::make_unique<ForwardErrorCorrection::ReceivedPacket>();
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200101 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.
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200104 uint8_t payload_type = rtp_packet.payload()[0] & 0x7f;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000105 received_packet->is_fec = payload_type == ulpfec_payload_type;
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200106 received_packet->ssrc = rtp_packet.Ssrc();
107 received_packet->seq_num = rtp_packet.SequenceNumber();
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000108
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200109 if (rtp_packet.payload()[0] & 0x80) {
brandtr74811e52016-08-10 00:51:50 -0700110 // f bit set in RED header, i.e. there are more than one RED header blocks.
Ilya Nikolaevskiy36c8ef62019-06-27 10:08:50 +0200111 // WebRTC never generates multiple blocks in a RED packet for FEC.
112 RTC_LOG(LS_WARNING) << "More than 1 block in RED packet is not supported.";
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200113 return false;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000114 }
Ilya Nikolaevskiy36c8ef62019-06-27 10:08:50 +0200115
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000116 ++packet_counter_.num_packets;
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200117 packet_counter_.num_bytes += rtp_packet.size();
asapersson0c43f772016-11-30 01:42:26 -0800118 if (packet_counter_.first_packet_time_ms == -1) {
Niels Möllerb5997872019-01-23 08:45:57 +0100119 packet_counter_.first_packet_time_ms = rtc::TimeMillis();
asapersson0c43f772016-11-30 01:42:26 -0800120 }
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000121
Ilya Nikolaevskiy36c8ef62019-06-27 10:08:50 +0200122 if (received_packet->is_fec) {
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000123 ++packet_counter_.num_fec_packets;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000124 // everything behind the RED header
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200125 received_packet->pkt->data =
126 rtp_packet.Buffer().Slice(rtp_packet.headers_size() + kRedHeaderLength,
127 rtp_packet.payload_size() - kRedHeaderLength);
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000128 } else {
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200129 auto red_payload = rtp_packet.payload().subview(kRedHeaderLength);
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200130 received_packet->pkt->data.EnsureCapacity(rtp_packet.headers_size() +
131 red_payload.size());
brandtr74811e52016-08-10 00:51:50 -0700132 // Copy RTP header.
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200133 received_packet->pkt->data.SetData(rtp_packet.data(),
134 rtp_packet.headers_size());
brandtr74811e52016-08-10 00:51:50 -0700135 // Set payload type.
136 received_packet->pkt->data[1] &= 0x80; // Reset RED payload type.
137 received_packet->pkt->data[1] += payload_type; // Set media payload type.
brandtr74811e52016-08-10 00:51:50 -0700138 // Copy payload data.
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200139 received_packet->pkt->data.AppendData(red_payload.data(),
140 red_payload.size());
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000141 }
142
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200143 if (received_packet->pkt->data.size() > 0) {
144 received_packets_.push_back(std::move(received_packet));
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000145 }
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200146 return true;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000147}
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
nissea5f043f2017-09-18 07:58:59 -0700149// TODO(nisse): Drop always-zero return value.
brandtrd55c3f62016-10-31 04:51:33 -0700150int32_t UlpfecReceiverImpl::ProcessReceivedFec() {
danilchap7c9426c2016-04-14 03:05:31 -0700151 crit_sect_.Enter();
philipeld8f6c162018-01-19 14:41:41 +0100152
153 // If we iterate over |received_packets_| and it contains a packet that cause
154 // us to recurse back to this function (for example a RED packet encapsulating
155 // a RED packet), then we will recurse forever. To avoid this we swap
156 // |received_packets_| with an empty vector so that the next recursive call
157 // wont iterate over the same packet again. This also solves the problem of
158 // not modifying the vector we are currently iterating over (packets are added
159 // in AddReceivedRedPacket).
160 std::vector<std::unique_ptr<ForwardErrorCorrection::ReceivedPacket>>
Yves Gerey665174f2018-06-19 15:03:05 +0200161 received_packets;
philipeld8f6c162018-01-19 14:41:41 +0100162 received_packets.swap(received_packets_);
163
164 for (const auto& received_packet : received_packets) {
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000165 // Send received media packet to VCM.
nissea5f043f2017-09-18 07:58:59 -0700166 if (!received_packet->is_fec) {
167 ForwardErrorCorrection::Packet* packet = received_packet->pkt;
danilchap7c9426c2016-04-14 03:05:31 -0700168 crit_sect_.Leave();
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200169 recovered_packet_callback_->OnRecoveredPacket(packet->data.data(),
170 packet->data.size());
danilchap7c9426c2016-04-14 03:05:31 -0700171 crit_sect_.Enter();
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200172 // Create a packet with the buffer to modify it.
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +0200173 RtpPacketReceived rtp_packet;
Ilya Nikolaevskiy741bab02019-09-25 14:37:10 +0200174 const uint8_t* const original_data = packet->data.cdata();
Ilya Nikolaevskiye7314cd2019-09-30 11:36:42 +0200175 if (!rtp_packet.Parse(packet->data)) {
176 RTC_LOG(LS_WARNING) << "Corrupted media packet";
177 } else {
178 rtp_packet.IdentifyExtensions(extensions_);
179 // Reset buffer reference, so zeroing would work on a buffer with a
180 // single reference.
181 packet->data = rtc::CopyOnWriteBuffer(0);
182 rtp_packet.ZeroMutableExtensions();
183 packet->data = rtp_packet.Buffer();
184 // Ensure that zeroing of extensions was done in place.
185 RTC_DCHECK_EQ(packet->data.cdata(), original_data);
186 }
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000187 }
nissea5f043f2017-09-18 07:58:59 -0700188 fec_->DecodeFec(*received_packet, &recovered_packets_);
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000189 }
nissea5f043f2017-09-18 07:58:59 -0700190
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000191 // Send any recovered media packets to VCM.
brandtr74811e52016-08-10 00:51:50 -0700192 for (const auto& recovered_packet : recovered_packets_) {
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200193 if (recovered_packet->returned) {
194 // Already sent to the VCM and the jitter buffer.
stefan@webrtc.org7adab092012-02-09 12:34:52 +0000195 continue;
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200196 }
197 ForwardErrorCorrection::Packet* packet = recovered_packet->pkt;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000198 ++packet_counter_.num_recovered_packets;
nisse41476e02017-08-25 09:08:44 -0700199 // Set this flag first; in case the recovered packet carries a RED
200 // header, OnRecoveredPacket will recurse back here.
201 recovered_packet->returned = true;
danilchap7c9426c2016-04-14 03:05:31 -0700202 crit_sect_.Leave();
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200203 recovered_packet_callback_->OnRecoveredPacket(packet->data.data(),
204 packet->data.size());
danilchap7c9426c2016-04-14 03:05:31 -0700205 crit_sect_.Enter();
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000206 }
philipeld8f6c162018-01-19 14:41:41 +0100207
danilchap7c9426c2016-04-14 03:05:31 -0700208 crit_sect_.Leave();
marpan@webrtc.org57353a32011-12-16 17:21:09 +0000209 return 0;
210}
stefan@webrtc.org7adab092012-02-09 12:34:52 +0000211
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000212} // namespace webrtc