2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-01-30 13:05:29 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/rtp_rtcp/source/ulpfec_receiver_impl.h"
|
2012-02-01 02:40:37 +00:00
|
|
|
|
2018-10-23 12:03:01 +02:00
|
|
|
#include <string.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2016-04-27 01:19:58 -07:00
|
|
|
#include <memory>
|
2016-08-09 01:23:23 -07:00
|
|
|
#include <utility>
|
2016-04-27 01:19:58 -07:00
|
|
|
|
2019-01-25 20:26:48 +01:00
|
|
|
#include "api/scoped_refptr.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/rtp_rtcp/source/byte_io.h"
|
2019-06-26 14:39:36 +02:00
|
|
|
#include "modules/rtp_rtcp/source/rtp_packet_received.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2019-01-23 08:45:57 +01:00
|
|
|
#include "rtc_base/time_utils.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2013-09-06 13:40:11 +00:00
|
|
|
|
2019-06-26 14:39:36 +02:00
|
|
|
std::unique_ptr<UlpfecReceiver> UlpfecReceiver::Create(
|
|
|
|
|
uint32_t ssrc,
|
|
|
|
|
RecoveredPacketReceiver* callback,
|
|
|
|
|
rtc::ArrayView<const RtpExtension> extensions) {
|
2019-09-17 17:06:18 +02:00
|
|
|
return std::make_unique<UlpfecReceiverImpl>(ssrc, callback, extensions);
|
2013-09-06 13:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-26 14:39:36 +02:00
|
|
|
UlpfecReceiverImpl::UlpfecReceiverImpl(
|
|
|
|
|
uint32_t ssrc,
|
|
|
|
|
RecoveredPacketReceiver* callback,
|
|
|
|
|
rtc::ArrayView<const RtpExtension> extensions)
|
2017-06-29 02:45:35 -07:00
|
|
|
: ssrc_(ssrc),
|
2019-06-26 14:39:36 +02:00
|
|
|
extensions_(extensions),
|
2017-06-29 02:45:35 -07:00
|
|
|
recovered_packet_callback_(callback),
|
|
|
|
|
fec_(ForwardErrorCorrection::CreateUlpfec(ssrc_)) {}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-10-31 04:51:33 -07:00
|
|
|
UlpfecReceiverImpl::~UlpfecReceiverImpl() {
|
2016-08-10 00:51:50 -07:00
|
|
|
received_packets_.clear();
|
2016-09-21 09:19:34 +02:00
|
|
|
fec_->ResetState(&recovered_packets_);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-31 04:51:33 -07:00
|
|
|
FecPacketCounter UlpfecReceiverImpl::GetPacketCounter() const {
|
2016-04-14 03:05:31 -07:00
|
|
|
rtc::CritScope cs(&crit_sect_);
|
2015-01-15 07:40:20 +00:00
|
|
|
return packet_counter_;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-16 15:06:28 +00:00
|
|
|
// 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.
|
|
|
|
|
|
2016-10-31 04:51:33 -07:00
|
|
|
int32_t UlpfecReceiverImpl::AddReceivedRedPacket(
|
|
|
|
|
const RTPHeader& header,
|
|
|
|
|
const uint8_t* incoming_rtp_packet,
|
|
|
|
|
size_t packet_length,
|
|
|
|
|
uint8_t ulpfec_payload_type) {
|
2017-06-29 02:45:35 -07:00
|
|
|
if (header.ssrc != ssrc_) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING)
|
2017-06-29 02:45:35 -07:00
|
|
|
<< "Received RED packet with different SSRC than expected; dropping.";
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2018-05-18 13:48:58 +02:00
|
|
|
if (packet_length > IP_PACKET_SIZE) {
|
|
|
|
|
RTC_LOG(LS_WARNING) << "Received RED packet with length exceeds maximum IP "
|
|
|
|
|
"packet size; dropping.";
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2016-04-14 03:05:31 -07:00
|
|
|
rtc::CritScope cs(&crit_sect_);
|
2016-08-10 00:51:50 -07:00
|
|
|
|
|
|
|
|
uint8_t red_header_length = 1;
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
size_t payload_data_length = packet_length - header.headerLength;
|
2012-01-20 06:59:06 +00:00
|
|
|
|
2015-06-29 07:22:04 -07:00
|
|
|
if (payload_data_length == 0) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING) << "Corrupt/truncated FEC packet.";
|
2015-06-29 07:22:04 -07:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-10 00:51:50 -07:00
|
|
|
// Remove RED header of incoming packet and store as a virtual RTP packet.
|
2016-04-27 01:19:58 -07:00
|
|
|
std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet(
|
2016-07-07 09:40:51 +02:00
|
|
|
new ForwardErrorCorrection::ReceivedPacket());
|
|
|
|
|
received_packet->pkt = new ForwardErrorCorrection::Packet();
|
2012-01-20 06:59:06 +00:00
|
|
|
|
2016-08-10 00:51:50 -07:00
|
|
|
// Get payload type from RED header and sequence number from RTP header.
|
|
|
|
|
uint8_t payload_type = incoming_rtp_packet[header.headerLength] & 0x7f;
|
2013-09-06 13:40:11 +00:00
|
|
|
received_packet->is_fec = payload_type == ulpfec_payload_type;
|
2017-06-29 02:45:35 -07:00
|
|
|
received_packet->ssrc = header.ssrc;
|
2013-09-06 13:40:11 +00:00
|
|
|
received_packet->seq_num = header.sequenceNumber;
|
2012-01-20 06:59:06 +00:00
|
|
|
|
2013-09-06 13:40:11 +00:00
|
|
|
if (incoming_rtp_packet[header.headerLength] & 0x80) {
|
2016-08-10 00:51:50 -07:00
|
|
|
// f bit set in RED header, i.e. there are more than one RED header blocks.
|
2019-06-27 10:08:50 +02:00
|
|
|
// 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 -1;
|
2012-01-20 06:59:06 +00:00
|
|
|
}
|
2019-06-27 10:08:50 +02:00
|
|
|
|
2015-01-15 07:40:20 +00:00
|
|
|
++packet_counter_.num_packets;
|
2019-08-27 09:19:49 +02:00
|
|
|
packet_counter_.num_bytes += packet_length;
|
2016-11-30 01:42:26 -08:00
|
|
|
if (packet_counter_.first_packet_time_ms == -1) {
|
2019-01-23 08:45:57 +01:00
|
|
|
packet_counter_.first_packet_time_ms = rtc::TimeMillis();
|
2016-11-30 01:42:26 -08:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2019-06-27 10:08:50 +02:00
|
|
|
if (received_packet->is_fec) {
|
2015-01-15 07:40:20 +00:00
|
|
|
++packet_counter_.num_fec_packets;
|
2018-05-18 13:48:58 +02:00
|
|
|
|
2012-01-20 06:59:06 +00:00
|
|
|
// everything behind the RED header
|
2019-09-03 11:07:37 +02:00
|
|
|
received_packet->pkt->data.SetData(
|
|
|
|
|
incoming_rtp_packet + header.headerLength + red_header_length,
|
|
|
|
|
payload_data_length - red_header_length);
|
2013-05-16 15:06:28 +00:00
|
|
|
received_packet->ssrc =
|
2015-03-17 16:42:49 +00:00
|
|
|
ByteReader<uint32_t>::ReadBigEndian(&incoming_rtp_packet[8]);
|
2012-01-20 06:59:06 +00:00
|
|
|
|
|
|
|
|
} else {
|
2019-09-03 11:07:37 +02:00
|
|
|
received_packet->pkt->data.SetSize(header.headerLength +
|
|
|
|
|
payload_data_length - red_header_length);
|
2016-08-10 00:51:50 -07:00
|
|
|
// Copy RTP header.
|
2019-09-03 11:07:37 +02:00
|
|
|
memcpy(received_packet->pkt->data.data(), incoming_rtp_packet,
|
2013-09-06 13:40:11 +00:00
|
|
|
header.headerLength);
|
2016-08-10 00:51:50 -07:00
|
|
|
// 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.
|
2019-09-03 11:07:37 +02:00
|
|
|
if (payload_data_length > red_header_length) {
|
|
|
|
|
memcpy(received_packet->pkt->data.data() + header.headerLength,
|
|
|
|
|
incoming_rtp_packet + header.headerLength + red_header_length,
|
|
|
|
|
payload_data_length - red_header_length);
|
|
|
|
|
}
|
2012-01-20 06:59:06 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2019-09-03 11:07:37 +02:00
|
|
|
if (received_packet->pkt->data.size() == 0) {
|
2012-01-20 06:59:06 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2011-12-16 17:21:09 +00:00
|
|
|
|
2016-08-10 00:51:50 -07:00
|
|
|
received_packets_.push_back(std::move(received_packet));
|
2012-01-20 06:59:06 +00:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-18 07:58:59 -07:00
|
|
|
// TODO(nisse): Drop always-zero return value.
|
2016-10-31 04:51:33 -07:00
|
|
|
int32_t UlpfecReceiverImpl::ProcessReceivedFec() {
|
2016-04-14 03:05:31 -07:00
|
|
|
crit_sect_.Enter();
|
2018-01-19 14:41:41 +01:00
|
|
|
|
|
|
|
|
// 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) {
|
2012-03-30 16:16:21 +00:00
|
|
|
// Send received media packet to VCM.
|
2017-09-18 07:58:59 -07:00
|
|
|
if (!received_packet->is_fec) {
|
|
|
|
|
ForwardErrorCorrection::Packet* packet = received_packet->pkt;
|
2016-04-14 03:05:31 -07:00
|
|
|
crit_sect_.Leave();
|
2019-09-03 11:07:37 +02:00
|
|
|
recovered_packet_callback_->OnRecoveredPacket(packet->data.data(),
|
|
|
|
|
packet->data.size());
|
2016-04-14 03:05:31 -07:00
|
|
|
crit_sect_.Enter();
|
2019-09-03 11:07:37 +02:00
|
|
|
// Create a packet with the buffer to modify it.
|
2019-06-26 14:39:36 +02:00
|
|
|
RtpPacketReceived rtp_packet;
|
2019-09-03 11:07:37 +02:00
|
|
|
rtp_packet.Parse(packet->data);
|
2019-06-26 14:39:36 +02:00
|
|
|
rtp_packet.IdentifyExtensions(extensions_);
|
2019-09-03 11:07:37 +02:00
|
|
|
// 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();
|
2012-03-30 16:16:21 +00:00
|
|
|
}
|
2017-09-18 07:58:59 -07:00
|
|
|
fec_->DecodeFec(*received_packet, &recovered_packets_);
|
2012-01-20 06:59:06 +00:00
|
|
|
}
|
2017-09-18 07:58:59 -07:00
|
|
|
|
2012-03-30 16:16:21 +00:00
|
|
|
// Send any recovered media packets to VCM.
|
2016-08-10 00:51:50 -07:00
|
|
|
for (const auto& recovered_packet : recovered_packets_) {
|
2016-07-07 09:40:51 +02:00
|
|
|
if (recovered_packet->returned) {
|
|
|
|
|
// Already sent to the VCM and the jitter buffer.
|
2012-02-09 12:34:52 +00:00
|
|
|
continue;
|
2016-07-07 09:40:51 +02:00
|
|
|
}
|
|
|
|
|
ForwardErrorCorrection::Packet* packet = recovered_packet->pkt;
|
2015-01-15 07:40:20 +00:00
|
|
|
++packet_counter_.num_recovered_packets;
|
2017-08-25 09:08:44 -07:00
|
|
|
// Set this flag first; in case the recovered packet carries a RED
|
|
|
|
|
// header, OnRecoveredPacket will recurse back here.
|
|
|
|
|
recovered_packet->returned = true;
|
2016-04-14 03:05:31 -07:00
|
|
|
crit_sect_.Leave();
|
2019-09-03 11:07:37 +02:00
|
|
|
recovered_packet_callback_->OnRecoveredPacket(packet->data.data(),
|
|
|
|
|
packet->data.size());
|
2016-04-14 03:05:31 -07:00
|
|
|
crit_sect_.Enter();
|
2012-01-20 06:59:06 +00:00
|
|
|
}
|
2018-01-19 14:41:41 +01:00
|
|
|
|
2016-04-14 03:05:31 -07:00
|
|
|
crit_sect_.Leave();
|
2011-12-16 17:21:09 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2012-02-09 12:34:52 +00:00
|
|
|
|
2013-05-16 15:06:28 +00:00
|
|
|
} // namespace webrtc
|