blob: 43ce2b024544cd2b99a383bca535ab60107fb93e [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
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200122 auto red_payload = rtp_packet.payload().subview(kRedHeaderLength);
Ilya Nikolaevskiy36c8ef62019-06-27 10:08:50 +0200123 if (received_packet->is_fec) {
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000124 ++packet_counter_.num_fec_packets;
Ying Wang7a84fcf2018-05-18 13:48:58 +0200125
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000126 // everything behind the RED header
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200127 received_packet->pkt->data.SetData(red_payload.data(), red_payload.size());
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000128 } else {
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200129 received_packet->pkt->data.EnsureCapacity(rtp_packet.headers_size() +
130 red_payload.size());
brandtr74811e52016-08-10 00:51:50 -0700131 // Copy RTP header.
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200132 received_packet->pkt->data.SetData(rtp_packet.data(),
133 rtp_packet.headers_size());
brandtr74811e52016-08-10 00:51:50 -0700134 // Set payload type.
135 received_packet->pkt->data[1] &= 0x80; // Reset RED payload type.
136 received_packet->pkt->data[1] += payload_type; // Set media payload type.
brandtr74811e52016-08-10 00:51:50 -0700137 // Copy payload data.
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200138 received_packet->pkt->data.AppendData(red_payload.data(),
139 red_payload.size());
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000140 }
141
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200142 if (received_packet->pkt->data.size() > 0) {
143 received_packets_.push_back(std::move(received_packet));
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000144 }
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200145 return true;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000146}
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
nissea5f043f2017-09-18 07:58:59 -0700148// TODO(nisse): Drop always-zero return value.
brandtrd55c3f62016-10-31 04:51:33 -0700149int32_t UlpfecReceiverImpl::ProcessReceivedFec() {
danilchap7c9426c2016-04-14 03:05:31 -0700150 crit_sect_.Enter();
philipeld8f6c162018-01-19 14:41:41 +0100151
152 // If we iterate over |received_packets_| and it contains a packet that cause
153 // us to recurse back to this function (for example a RED packet encapsulating
154 // a RED packet), then we will recurse forever. To avoid this we swap
155 // |received_packets_| with an empty vector so that the next recursive call
156 // wont iterate over the same packet again. This also solves the problem of
157 // not modifying the vector we are currently iterating over (packets are added
158 // in AddReceivedRedPacket).
159 std::vector<std::unique_ptr<ForwardErrorCorrection::ReceivedPacket>>
Yves Gerey665174f2018-06-19 15:03:05 +0200160 received_packets;
philipeld8f6c162018-01-19 14:41:41 +0100161 received_packets.swap(received_packets_);
162
163 for (const auto& received_packet : received_packets) {
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000164 // Send received media packet to VCM.
nissea5f043f2017-09-18 07:58:59 -0700165 if (!received_packet->is_fec) {
166 ForwardErrorCorrection::Packet* packet = received_packet->pkt;
danilchap7c9426c2016-04-14 03:05:31 -0700167 crit_sect_.Leave();
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200168 recovered_packet_callback_->OnRecoveredPacket(packet->data.data(),
169 packet->data.size());
danilchap7c9426c2016-04-14 03:05:31 -0700170 crit_sect_.Enter();
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200171 // Create a packet with the buffer to modify it.
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +0200172 RtpPacketReceived rtp_packet;
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200173 rtp_packet.Parse(packet->data);
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +0200174 rtp_packet.IdentifyExtensions(extensions_);
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200175 // Reset buffer reference, so zeroing would work on a buffer with a
176 // single reference.
177 packet->data = rtc::CopyOnWriteBuffer(0);
178 rtp_packet.ZeroMutableExtensions();
179 packet->data = rtp_packet.Buffer();
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000180 }
nissea5f043f2017-09-18 07:58:59 -0700181 fec_->DecodeFec(*received_packet, &recovered_packets_);
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000182 }
nissea5f043f2017-09-18 07:58:59 -0700183
marpan@webrtc.org3a6080d2012-03-30 16:16:21 +0000184 // Send any recovered media packets to VCM.
brandtr74811e52016-08-10 00:51:50 -0700185 for (const auto& recovered_packet : recovered_packets_) {
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200186 if (recovered_packet->returned) {
187 // Already sent to the VCM and the jitter buffer.
stefan@webrtc.org7adab092012-02-09 12:34:52 +0000188 continue;
Rasmus Brandtae4f7672016-07-07 09:40:51 +0200189 }
190 ForwardErrorCorrection::Packet* packet = recovered_packet->pkt;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000191 ++packet_counter_.num_recovered_packets;
nisse41476e02017-08-25 09:08:44 -0700192 // Set this flag first; in case the recovered packet carries a RED
193 // header, OnRecoveredPacket will recurse back here.
194 recovered_packet->returned = true;
danilchap7c9426c2016-04-14 03:05:31 -0700195 crit_sect_.Leave();
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200196 recovered_packet_callback_->OnRecoveredPacket(packet->data.data(),
197 packet->data.size());
danilchap7c9426c2016-04-14 03:05:31 -0700198 crit_sect_.Enter();
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +0000199 }
philipeld8f6c162018-01-19 14:41:41 +0100200
danilchap7c9426c2016-04-14 03:05:31 -0700201 crit_sect_.Leave();
marpan@webrtc.org57353a32011-12-16 17:21:09 +0000202 return 0;
203}
stefan@webrtc.org7adab092012-02-09 12:34:52 +0000204
phoglund@webrtc.org9919ad52013-05-16 15:06:28 +0000205} // namespace webrtc