webrtc_m130/modules/rtp_rtcp/source/ulpfec_receiver_impl.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

206 lines
7.8 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/rtp_rtcp/source/ulpfec_receiver_impl.h"
#include <memory>
#include <utility>
#include "api/scoped_refptr.h"
#include "modules/rtp_rtcp/source/rtp_packet_received.h"
#include "rtc_base/logging.h"
#include "rtc_base/time_utils.h"
namespace webrtc {
std::unique_ptr<UlpfecReceiver> UlpfecReceiver::Create(
uint32_t ssrc,
RecoveredPacketReceiver* callback,
rtc::ArrayView<const RtpExtension> extensions) {
Use std::make_unique instead of absl::make_unique. WebRTC is now using C++14 so there is no need to use the Abseil version of std::make_unique. This CL has been created with the following steps: git grep -l absl::make_unique | sort | uniq > /tmp/make_unique.txt git grep -l absl::WrapUnique | sort | uniq > /tmp/wrap_unique.txt git grep -l "#include <memory>" | sort | uniq > /tmp/memory.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/make_unique.txt /tmp/wrap_unique.txt | sort | \ uniq > /tmp/only_make_unique.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/only_make_unique.txt /tmp/memory.txt | \ xargs grep -l "absl/memory" > /tmp/add-memory.txt git grep -l "\babsl::make_unique\b" | \ xargs sed -i "s/\babsl::make_unique\b/std::make_unique/g" git checkout PRESUBMIT.py abseil-in-webrtc.md cat /tmp/add-memory.txt | \ xargs sed -i \ 's/#include "absl\/memory\/memory.h"/#include <memory>/g' git cl format # Manual fix order of the new inserted #include <memory> cat /tmp/only_make_unique | xargs grep -l "#include <memory>" | \ xargs sed -i '/#include "absl\/memory\/memory.h"/d' git ls-files | grep BUILD.gn | \ xargs sed -i '/\/\/third_party\/abseil-cpp\/absl\/memory/d' python tools_webrtc/gn_check_autofix.py \ -m tryserver.webrtc -b linux_rel # Repead the gn_check_autofix step for other platforms git ls-files | grep BUILD.gn | \ xargs sed -i 's/absl\/memory:memory/absl\/memory/g' git cl format Bug: webrtc:10945 Change-Id: I3fe28ea80f4dd3ba3cf28effd151d5e1f19aff89 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/153221 Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29209}
2019-09-17 17:06:18 +02:00
return std::make_unique<UlpfecReceiverImpl>(ssrc, callback, extensions);
}
UlpfecReceiverImpl::UlpfecReceiverImpl(
uint32_t ssrc,
RecoveredPacketReceiver* callback,
rtc::ArrayView<const RtpExtension> extensions)
Reland of Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. (patchset #1 id:1 of https://codereview.webrtc.org/2919313005/ ) Reason for revert: Fix RtpStreamReceiver to not recover RTX packets with incorrect SSRC. Original issue's description: > Revert of Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. (patchset #5 id:120001 of https://codereview.webrtc.org/2893293003/ ) > > Reason for revert: > Breaks fuzzer. > > Original issue's description: > > Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. > > > > Prior to this CL, the ForwardErrorCorrection state would be reset whenever > > the difference in sequence numbers of the last recovered media packet > > and the new packet (media or FEC) was too large. This comparison did not > > take into account that FlexFEC uses a different SSRC for the FEC packets, > > meaning that the the state would be reset very frequently when FlexFEC > > is used. This should not have led to any major problems, except for a > > decreased decoding efficiency. > > > > This CL verifies that whenever we compare sequence numbers in > > ForwardErrorCorrection, they do indeed belong to the same SSRC. > > > > BUG=webrtc:5654 > > > > Review-Url: https://codereview.webrtc.org/2893293003 > > Cr-Commit-Position: refs/heads/master@{#18399} > > Committed: https://chromium.googlesource.com/external/webrtc/+/1476a9d789db03457595cf7dbea7e362972f2a4d > > TBR=stefan@webrtc.org,holmer@google.com > # Not skipping CQ checks because original CL landed more than 1 days ago. > BUG=webrtc:5654 > > Review-Url: https://codereview.webrtc.org/2919313005 > Cr-Commit-Position: refs/heads/master@{#18446} > Committed: https://chromium.googlesource.com/external/webrtc/+/92732ecc5ccc81e17c1dea75ecc5e34c7ff6274f R=stefan@webrtc.org BUG=webrtc:5654 Review-Url: https://codereview.webrtc.org/2918333002 Cr-Commit-Position: refs/heads/master@{#18827}
2017-06-29 02:45:35 -07:00
: ssrc_(ssrc),
extensions_(extensions),
Reland of Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. (patchset #1 id:1 of https://codereview.webrtc.org/2919313005/ ) Reason for revert: Fix RtpStreamReceiver to not recover RTX packets with incorrect SSRC. Original issue's description: > Revert of Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. (patchset #5 id:120001 of https://codereview.webrtc.org/2893293003/ ) > > Reason for revert: > Breaks fuzzer. > > Original issue's description: > > Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. > > > > Prior to this CL, the ForwardErrorCorrection state would be reset whenever > > the difference in sequence numbers of the last recovered media packet > > and the new packet (media or FEC) was too large. This comparison did not > > take into account that FlexFEC uses a different SSRC for the FEC packets, > > meaning that the the state would be reset very frequently when FlexFEC > > is used. This should not have led to any major problems, except for a > > decreased decoding efficiency. > > > > This CL verifies that whenever we compare sequence numbers in > > ForwardErrorCorrection, they do indeed belong to the same SSRC. > > > > BUG=webrtc:5654 > > > > Review-Url: https://codereview.webrtc.org/2893293003 > > Cr-Commit-Position: refs/heads/master@{#18399} > > Committed: https://chromium.googlesource.com/external/webrtc/+/1476a9d789db03457595cf7dbea7e362972f2a4d > > TBR=stefan@webrtc.org,holmer@google.com > # Not skipping CQ checks because original CL landed more than 1 days ago. > BUG=webrtc:5654 > > Review-Url: https://codereview.webrtc.org/2919313005 > Cr-Commit-Position: refs/heads/master@{#18446} > Committed: https://chromium.googlesource.com/external/webrtc/+/92732ecc5ccc81e17c1dea75ecc5e34c7ff6274f R=stefan@webrtc.org BUG=webrtc:5654 Review-Url: https://codereview.webrtc.org/2918333002 Cr-Commit-Position: refs/heads/master@{#18827}
2017-06-29 02:45:35 -07:00
recovered_packet_callback_(callback),
fec_(ForwardErrorCorrection::CreateUlpfec(ssrc_)) {}
UlpfecReceiverImpl::~UlpfecReceiverImpl() {
received_packets_.clear();
fec_->ResetState(&recovered_packets_);
}
FecPacketCounter UlpfecReceiverImpl::GetPacketCounter() const {
rtc::CritScope cs(&crit_sect_);
return packet_counter_;
}
// 0 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |F| block PT | timestamp offset | block length |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
//
// RFC 2198 RTP Payload for Redundant Audio Data September 1997
//
// The bits in the header are specified as follows:
//
// F: 1 bit First bit in header indicates whether another header block
// follows. If 1 further header blocks follow, if 0 this is the
// last header block.
// If 0 there is only 1 byte RED header
//
// block PT: 7 bits RTP payload type for this block.
//
// timestamp offset: 14 bits Unsigned offset of timestamp of this block
// relative to timestamp given in RTP header. The use of an unsigned
// offset implies that redundant data must be sent after the primary
// data, and is hence a time to be subtracted from the current
// timestamp to determine the timestamp of the data for which this
// block is the redundancy.
//
// block length: 10 bits Length in bytes of the corresponding data
// block excluding header.
bool UlpfecReceiverImpl::AddReceivedRedPacket(const RtpPacket& rtp_packet,
uint8_t ulpfec_payload_type) {
if (rtp_packet.Ssrc() != ssrc_) {
RTC_LOG(LS_WARNING)
Reland of Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. (patchset #1 id:1 of https://codereview.webrtc.org/2919313005/ ) Reason for revert: Fix RtpStreamReceiver to not recover RTX packets with incorrect SSRC. Original issue's description: > Revert of Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. (patchset #5 id:120001 of https://codereview.webrtc.org/2893293003/ ) > > Reason for revert: > Breaks fuzzer. > > Original issue's description: > > Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. > > > > Prior to this CL, the ForwardErrorCorrection state would be reset whenever > > the difference in sequence numbers of the last recovered media packet > > and the new packet (media or FEC) was too large. This comparison did not > > take into account that FlexFEC uses a different SSRC for the FEC packets, > > meaning that the the state would be reset very frequently when FlexFEC > > is used. This should not have led to any major problems, except for a > > decreased decoding efficiency. > > > > This CL verifies that whenever we compare sequence numbers in > > ForwardErrorCorrection, they do indeed belong to the same SSRC. > > > > BUG=webrtc:5654 > > > > Review-Url: https://codereview.webrtc.org/2893293003 > > Cr-Commit-Position: refs/heads/master@{#18399} > > Committed: https://chromium.googlesource.com/external/webrtc/+/1476a9d789db03457595cf7dbea7e362972f2a4d > > TBR=stefan@webrtc.org,holmer@google.com > # Not skipping CQ checks because original CL landed more than 1 days ago. > BUG=webrtc:5654 > > Review-Url: https://codereview.webrtc.org/2919313005 > Cr-Commit-Position: refs/heads/master@{#18446} > Committed: https://chromium.googlesource.com/external/webrtc/+/92732ecc5ccc81e17c1dea75ecc5e34c7ff6274f R=stefan@webrtc.org BUG=webrtc:5654 Review-Url: https://codereview.webrtc.org/2918333002 Cr-Commit-Position: refs/heads/master@{#18827}
2017-06-29 02:45:35 -07:00
<< "Received RED packet with different SSRC than expected; dropping.";
return false;
Reland of Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. (patchset #1 id:1 of https://codereview.webrtc.org/2919313005/ ) Reason for revert: Fix RtpStreamReceiver to not recover RTX packets with incorrect SSRC. Original issue's description: > Revert of Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. (patchset #5 id:120001 of https://codereview.webrtc.org/2893293003/ ) > > Reason for revert: > Breaks fuzzer. > > Original issue's description: > > Only compare sequence numbers from the same SSRC in ForwardErrorCorrection. > > > > Prior to this CL, the ForwardErrorCorrection state would be reset whenever > > the difference in sequence numbers of the last recovered media packet > > and the new packet (media or FEC) was too large. This comparison did not > > take into account that FlexFEC uses a different SSRC for the FEC packets, > > meaning that the the state would be reset very frequently when FlexFEC > > is used. This should not have led to any major problems, except for a > > decreased decoding efficiency. > > > > This CL verifies that whenever we compare sequence numbers in > > ForwardErrorCorrection, they do indeed belong to the same SSRC. > > > > BUG=webrtc:5654 > > > > Review-Url: https://codereview.webrtc.org/2893293003 > > Cr-Commit-Position: refs/heads/master@{#18399} > > Committed: https://chromium.googlesource.com/external/webrtc/+/1476a9d789db03457595cf7dbea7e362972f2a4d > > TBR=stefan@webrtc.org,holmer@google.com > # Not skipping CQ checks because original CL landed more than 1 days ago. > BUG=webrtc:5654 > > Review-Url: https://codereview.webrtc.org/2919313005 > Cr-Commit-Position: refs/heads/master@{#18446} > Committed: https://chromium.googlesource.com/external/webrtc/+/92732ecc5ccc81e17c1dea75ecc5e34c7ff6274f R=stefan@webrtc.org BUG=webrtc:5654 Review-Url: https://codereview.webrtc.org/2918333002 Cr-Commit-Position: refs/heads/master@{#18827}
2017-06-29 02:45:35 -07:00
}
if (rtp_packet.size() > IP_PACKET_SIZE) {
RTC_LOG(LS_WARNING) << "Received RED packet with length exceeds maximum IP "
"packet size; dropping.";
return false;
}
rtc::CritScope cs(&crit_sect_);
static constexpr uint8_t kRedHeaderLength = 1;
if (rtp_packet.payload_size() == 0) {
RTC_LOG(LS_WARNING) << "Corrupt/truncated FEC packet.";
return false;
}
// Remove RED header of incoming packet and store as a virtual RTP packet.
auto received_packet =
std::make_unique<ForwardErrorCorrection::ReceivedPacket>();
received_packet->pkt = new ForwardErrorCorrection::Packet();
// Get payload type from RED header and sequence number from RTP header.
uint8_t payload_type = rtp_packet.payload()[0] & 0x7f;
received_packet->is_fec = payload_type == ulpfec_payload_type;
received_packet->ssrc = rtp_packet.Ssrc();
received_packet->seq_num = rtp_packet.SequenceNumber();
if (rtp_packet.payload()[0] & 0x80) {
// f bit set in RED header, i.e. there are more than one RED header blocks.
// WebRTC never generates multiple blocks in a RED packet for FEC.
RTC_LOG(LS_WARNING) << "More than 1 block in RED packet is not supported.";
return false;
}
++packet_counter_.num_packets;
packet_counter_.num_bytes += rtp_packet.size();
if (packet_counter_.first_packet_time_ms == -1) {
packet_counter_.first_packet_time_ms = rtc::TimeMillis();
}
auto red_payload = rtp_packet.payload().subview(kRedHeaderLength);
if (received_packet->is_fec) {
++packet_counter_.num_fec_packets;
// everything behind the RED header
received_packet->pkt->data.SetData(red_payload.data(), red_payload.size());
} else {
received_packet->pkt->data.EnsureCapacity(rtp_packet.headers_size() +
red_payload.size());
// Copy RTP header.
received_packet->pkt->data.SetData(rtp_packet.data(),
rtp_packet.headers_size());
// Set payload type.
received_packet->pkt->data[1] &= 0x80; // Reset RED payload type.
received_packet->pkt->data[1] += payload_type; // Set media payload type.
// Copy payload data.
received_packet->pkt->data.AppendData(red_payload.data(),
red_payload.size());
}
if (received_packet->pkt->data.size() > 0) {
received_packets_.push_back(std::move(received_packet));
}
return true;
}
// TODO(nisse): Drop always-zero return value.
int32_t UlpfecReceiverImpl::ProcessReceivedFec() {
crit_sect_.Enter();
// If we iterate over |received_packets_| and it contains a packet that cause
// us to recurse back to this function (for example a RED packet encapsulating
// a RED packet), then we will recurse forever. To avoid this we swap
// |received_packets_| with an empty vector so that the next recursive call
// wont iterate over the same packet again. This also solves the problem of
// not modifying the vector we are currently iterating over (packets are added
// in AddReceivedRedPacket).
std::vector<std::unique_ptr<ForwardErrorCorrection::ReceivedPacket>>
received_packets;
received_packets.swap(received_packets_);
for (const auto& received_packet : received_packets) {
// Send received media packet to VCM.
if (!received_packet->is_fec) {
ForwardErrorCorrection::Packet* packet = received_packet->pkt;
crit_sect_.Leave();
recovered_packet_callback_->OnRecoveredPacket(packet->data.data(),
packet->data.size());
crit_sect_.Enter();
// Create a packet with the buffer to modify it.
RtpPacketReceived rtp_packet;
rtp_packet.Parse(packet->data);
rtp_packet.IdentifyExtensions(extensions_);
// Reset buffer reference, so zeroing would work on a buffer with a
// single reference.
packet->data = rtc::CopyOnWriteBuffer(0);
rtp_packet.ZeroMutableExtensions();
packet->data = rtp_packet.Buffer();
}
fec_->DecodeFec(*received_packet, &recovered_packets_);
}
// Send any recovered media packets to VCM.
for (const auto& recovered_packet : recovered_packets_) {
if (recovered_packet->returned) {
// Already sent to the VCM and the jitter buffer.
continue;
}
ForwardErrorCorrection::Packet* packet = recovered_packet->pkt;
++packet_counter_.num_recovered_packets;
// Set this flag first; in case the recovered packet carries a RED
// header, OnRecoveredPacket will recurse back here.
recovered_packet->returned = true;
crit_sect_.Leave();
recovered_packet_callback_->OnRecoveredPacket(packet->data.data(),
packet->data.size());
crit_sect_.Enter();
}
crit_sect_.Leave();
return 0;
}
} // namespace webrtc