mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
[feat] implementation for nack generator module
This commit is contained in:
313
src/rtcp/congestion_control_feedback.cpp
Normal file
313
src/rtcp/congestion_control_feedback.cpp
Normal file
@@ -0,0 +1,313 @@
|
||||
/*
|
||||
* Copyright (c) 2024 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 "congestion_control_feedback.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "api/array_view.h"
|
||||
#include "api/transport/ecn_marking.h"
|
||||
#include "api/units/time_delta.h"
|
||||
#include "api/units/timestamp.h"
|
||||
#include "byte_io.h"
|
||||
#include "common_header.h"
|
||||
#include "log.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace rtcp {
|
||||
|
||||
/*
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|V=2|P| FMT=11 | PT = 205 | length |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| SSRC of RTCP packet sender |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| SSRC of 1st RTP Stream |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| begin_seq | num_reports |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|R|ECN| Arrival time offset | ... .
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
. .
|
||||
. .
|
||||
. .
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| SSRC of nth RTP Stream |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| begin_seq | num_reports |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|R|ECN| Arrival time offset | ... |
|
||||
. .
|
||||
. .
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| Report Timestamp (32 bits) |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*/
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr size_t kSenderSsrcLength = 4;
|
||||
constexpr size_t kHeaderPerMediaSssrcLength = 8;
|
||||
constexpr size_t kTimestampLength = 4;
|
||||
|
||||
// RFC-3168, Section 5
|
||||
constexpr uint16_t kEcnEct1 = 0x01;
|
||||
constexpr uint16_t kEcnEct0 = 0x02;
|
||||
constexpr uint16_t kEcnCe = 0x03;
|
||||
|
||||
// Arrival time offset (ATO, 13 bits):
|
||||
// The arrival time of the RTP packet at the receiver, as an offset before the
|
||||
// time represented by the Report Timestamp (RTS) field of this RTCP congestion
|
||||
// control feedback report. The ATO field is in units of 1/1024 seconds (this
|
||||
// unit is chosen to give exact offsets from the RTS field) so, for example, an
|
||||
// ATO value of 512 indicates that the corresponding RTP packet arrived exactly
|
||||
// half a second before the time instant represented by the RTS field. If the
|
||||
// measured value is greater than 8189/1024 seconds (the value that would be
|
||||
// coded as 0x1FFD), the value 0x1FFE MUST be reported to indicate an over-range
|
||||
// measurement. If the measurement is unavailable or if the arrival time of the
|
||||
// RTP packet is after the time represented by the RTS field, then an ATO value
|
||||
// of 0x1FFF MUST be reported for the packet.
|
||||
uint16_t To13bitAto(TimeDelta arrival_time_offset) {
|
||||
if (arrival_time_offset < TimeDelta::Zero()) {
|
||||
return 0x1FFF;
|
||||
}
|
||||
return std::min(
|
||||
static_cast<int64_t>(1024 * arrival_time_offset.seconds<float>()),
|
||||
int64_t{0x1FFE});
|
||||
}
|
||||
|
||||
TimeDelta AtoToTimeDelta(uint16_t receive_info) {
|
||||
// receive_info
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// |R|ECN| Arrival time offset |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
const uint16_t ato = receive_info & 0x1FFF;
|
||||
if (ato == 0x1FFE) {
|
||||
return TimeDelta::PlusInfinity();
|
||||
}
|
||||
if (ato == 0x1FFF) {
|
||||
return TimeDelta::MinusInfinity();
|
||||
}
|
||||
return TimeDelta::Seconds(ato) / 1024;
|
||||
}
|
||||
|
||||
uint16_t To2BitEcn(EcnMarking ecn_marking) {
|
||||
switch (ecn_marking) {
|
||||
case EcnMarking::kNotEct:
|
||||
return 0;
|
||||
case EcnMarking::kEct1:
|
||||
return kEcnEct1 << 13;
|
||||
case EcnMarking::kEct0:
|
||||
return kEcnEct0 << 13;
|
||||
case EcnMarking::kCe:
|
||||
return kEcnCe << 13;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
EcnMarking ToEcnMarking(uint16_t receive_info) {
|
||||
const uint16_t ecn = (receive_info >> 13) & 0b11;
|
||||
if (ecn == kEcnEct1) {
|
||||
return EcnMarking::kEct1;
|
||||
}
|
||||
if (ecn == kEcnEct0) {
|
||||
return EcnMarking::kEct0;
|
||||
}
|
||||
if (ecn == kEcnCe) {
|
||||
return EcnMarking::kCe;
|
||||
}
|
||||
return EcnMarking::kNotEct;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CongestionControlFeedback ::CongestionControlFeedback(
|
||||
std::vector<PacketInfo> packets, uint32_t compact_ntp_timestamp)
|
||||
: packets_(std::move(packets)),
|
||||
report_timestamp_compact_ntp_(compact_ntp_timestamp) {}
|
||||
|
||||
bool CongestionControlFeedback::Create(uint8_t* buffer, size_t* position,
|
||||
size_t max_length,
|
||||
PacketReadyCallback callback) const {
|
||||
// Ensure there is enough room for this packet.
|
||||
while (*position + BlockLength() > max_length) {
|
||||
if (!OnBufferFull(buffer, position, callback)) return false;
|
||||
}
|
||||
const size_t position_end = *position + BlockLength();
|
||||
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// |V=2|P| FMT=11 | PT = 205 | length |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// | SSRC of RTCP packet sender |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), buffer,
|
||||
position);
|
||||
ByteWriter<uint32_t>::WriteBigEndian(&buffer[*position], sender_ssrc());
|
||||
*position += 4;
|
||||
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// | SSRC of nth RTP Stream |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// | begin_seq | num_reports |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// |R|ECN| Arrival time offset | ... .
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// . .
|
||||
auto write_report_for_ssrc = [&](rtc::ArrayView<const PacketInfo> packets) {
|
||||
// SSRC of nth RTP stream.
|
||||
ByteWriter<uint32_t>::WriteBigEndian(&buffer[*position], packets[0].ssrc);
|
||||
*position += 4;
|
||||
|
||||
// begin_seq
|
||||
ByteWriter<uint16_t>::WriteBigEndian(&buffer[*position],
|
||||
packets[0].sequence_number);
|
||||
*position += 2;
|
||||
// num_reports
|
||||
uint16_t num_reports = packets.size();
|
||||
|
||||
// Each report block MUST NOT include more than 16384 packet
|
||||
// metric blocks (i.e., it MUST NOT report on more than one
|
||||
// quarter of the sequence number space in a single report).
|
||||
if (num_reports > 16384) {
|
||||
LOG_ERROR("Unexpected number of reports:{}", num_reports);
|
||||
return;
|
||||
}
|
||||
ByteWriter<uint16_t>::WriteBigEndian(&buffer[*position], num_reports);
|
||||
*position += 2;
|
||||
|
||||
for (const PacketInfo& packet : packets) {
|
||||
bool received = packet.arrival_time_offset.IsFinite();
|
||||
uint16_t packet_info = 0;
|
||||
if (received) {
|
||||
packet_info = 0x8000 | To2BitEcn(packet.ecn) |
|
||||
To13bitAto(packet.arrival_time_offset);
|
||||
}
|
||||
ByteWriter<uint16_t>::WriteBigEndian(&buffer[*position], packet_info);
|
||||
*position += 2;
|
||||
}
|
||||
// 32bit align per SSRC block.
|
||||
if (num_reports % 2 != 0) {
|
||||
ByteWriter<uint16_t>::WriteBigEndian(&buffer[*position], 0);
|
||||
*position += 2;
|
||||
}
|
||||
};
|
||||
|
||||
rtc::ArrayView<const PacketInfo> remaining(packets_);
|
||||
while (!remaining.empty()) {
|
||||
int number_of_packets_for_ssrc = 0;
|
||||
uint32_t ssrc = remaining[0].ssrc;
|
||||
for (const PacketInfo& packet_info : remaining) {
|
||||
if (packet_info.ssrc != ssrc) {
|
||||
break;
|
||||
}
|
||||
++number_of_packets_for_ssrc;
|
||||
}
|
||||
write_report_for_ssrc(remaining.subview(0, number_of_packets_for_ssrc));
|
||||
remaining = remaining.subview(number_of_packets_for_ssrc);
|
||||
}
|
||||
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// | Report Timestamp (32 bits) |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
ByteWriter<uint32_t>::WriteBigEndian(&buffer[*position],
|
||||
report_timestamp_compact_ntp_);
|
||||
*position += 4;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t CongestionControlFeedback::BlockLength() const {
|
||||
// Total size of this packet
|
||||
size_t total_size = kSenderSsrcLength + kHeaderLength + kTimestampLength;
|
||||
if (packets_.empty()) {
|
||||
return total_size;
|
||||
}
|
||||
|
||||
auto increase_size_per_ssrc = [](int number_of_packets_for_ssrc) {
|
||||
// Each packet report needs two bytes.
|
||||
size_t packet_block_size = number_of_packets_for_ssrc * 2;
|
||||
// 32 bit aligned.
|
||||
return kHeaderPerMediaSssrcLength + packet_block_size +
|
||||
((number_of_packets_for_ssrc % 2) != 0 ? 2 : 0);
|
||||
};
|
||||
|
||||
uint32_t ssrc = packets_.front().ssrc;
|
||||
uint16_t first_sequence_number = packets_.front().sequence_number;
|
||||
for (size_t i = 0; i < packets_.size(); ++i) {
|
||||
if (packets_[i].ssrc != ssrc) {
|
||||
uint16_t number_of_packets =
|
||||
packets_[i - 1].sequence_number - first_sequence_number + 1;
|
||||
total_size += increase_size_per_ssrc(number_of_packets);
|
||||
ssrc = packets_[i].ssrc;
|
||||
first_sequence_number = packets_[i].sequence_number;
|
||||
}
|
||||
}
|
||||
uint16_t number_of_packets =
|
||||
packets_.back().sequence_number - first_sequence_number + 1;
|
||||
total_size += increase_size_per_ssrc(number_of_packets);
|
||||
|
||||
return total_size;
|
||||
}
|
||||
|
||||
bool CongestionControlFeedback::Parse(const rtcp::CommonHeader& packet) {
|
||||
const uint8_t* payload = packet.payload();
|
||||
const uint8_t* payload_end = packet.payload() + packet.payload_size_bytes();
|
||||
|
||||
if (packet.payload_size_bytes() % 4 != 0 ||
|
||||
packet.payload_size_bytes() < kSenderSsrcLength + kTimestampLength) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SetSenderSsrc(ByteReader<uint32_t>::ReadBigEndian(payload));
|
||||
payload += 4;
|
||||
|
||||
report_timestamp_compact_ntp_ =
|
||||
ByteReader<uint32_t>::ReadBigEndian(payload_end - 4);
|
||||
payload_end -= 4;
|
||||
|
||||
while (payload + kHeaderPerMediaSssrcLength < payload_end) {
|
||||
uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(payload);
|
||||
payload += 4;
|
||||
|
||||
uint16_t base_seqno = ByteReader<uint16_t>::ReadBigEndian(payload);
|
||||
payload += 2;
|
||||
uint16_t num_reports = ByteReader<uint16_t>::ReadBigEndian(payload);
|
||||
payload += 2;
|
||||
|
||||
constexpr size_t kPerPacketLength = 2;
|
||||
if (payload + kPerPacketLength * num_reports > payload_end) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < num_reports; ++i) {
|
||||
uint16_t packet_info = ByteReader<uint16_t>::ReadBigEndian(payload);
|
||||
payload += 2;
|
||||
|
||||
uint16_t seq_no = base_seqno + i;
|
||||
bool received = (packet_info & 0x8000);
|
||||
packets_.push_back(
|
||||
{ssrc, seq_no,
|
||||
received ? AtoToTimeDelta(packet_info) : TimeDelta::MinusInfinity(),
|
||||
ToEcnMarking(packet_info)});
|
||||
}
|
||||
if (num_reports % 2) {
|
||||
// 2 bytes padding
|
||||
payload += 2;
|
||||
}
|
||||
}
|
||||
return payload == payload_end;
|
||||
}
|
||||
} // namespace rtcp
|
||||
} // namespace webrtc
|
||||
69
src/rtcp/congestion_control_feedback.h
Normal file
69
src/rtcp/congestion_control_feedback.h
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2024 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.
|
||||
*/
|
||||
#ifndef MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_CONGESTION_CONTROL_FEEDBACK_H_
|
||||
#define MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_CONGESTION_CONTROL_FEEDBACK_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "api/array_view.h"
|
||||
#include "api/transport/ecn_marking.h"
|
||||
#include "api/units/time_delta.h"
|
||||
#include "common_header.h"
|
||||
#include "rtp_feedback.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace rtcp {
|
||||
|
||||
// Congestion control feedback message as specified in
|
||||
// https://www.rfc-editor.org/rfc/rfc8888.html
|
||||
class CongestionControlFeedback : public RtpFeedback {
|
||||
public:
|
||||
struct PacketInfo {
|
||||
uint32_t ssrc = 0;
|
||||
uint16_t sequence_number = 0;
|
||||
// Time offset from report timestamp. Minus infinity if the packet has not
|
||||
// been received.
|
||||
TimeDelta arrival_time_offset = TimeDelta::MinusInfinity();
|
||||
EcnMarking ecn = EcnMarking::kNotEct;
|
||||
};
|
||||
|
||||
static constexpr uint8_t kFeedbackMessageType = 11;
|
||||
|
||||
// `Packets` MUST be sorted in sequence_number order per SSRC. There MUST not
|
||||
// be missing sequence numbers between `Packets`. `Packets` MUST not include
|
||||
// duplicate sequence numbers.
|
||||
CongestionControlFeedback(std::vector<PacketInfo> packets,
|
||||
uint32_t report_timestamp_compact_ntp);
|
||||
CongestionControlFeedback() = default;
|
||||
|
||||
bool Parse(const CommonHeader& packet);
|
||||
|
||||
rtc::ArrayView<const PacketInfo> packets() const { return packets_; }
|
||||
|
||||
uint32_t report_timestamp_compact_ntp() const {
|
||||
return report_timestamp_compact_ntp_;
|
||||
}
|
||||
|
||||
// Serialize the packet.
|
||||
bool Create(uint8_t* packet, size_t* position, size_t max_length,
|
||||
PacketReadyCallback callback) const override;
|
||||
size_t BlockLength() const override;
|
||||
|
||||
private:
|
||||
std::vector<PacketInfo> packets_;
|
||||
uint32_t report_timestamp_compact_ntp_ = 0;
|
||||
};
|
||||
|
||||
} // namespace rtcp
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_CONGESTION_CONTROL_FEEDBACK_H_
|
||||
156
src/rtcp/nack.cpp
Normal file
156
src/rtcp/nack.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright (c) 2015 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 "nack.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
#include "byte_io.h"
|
||||
#include "common_header.h"
|
||||
#include "log.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace rtcp {
|
||||
// RFC 4585: Feedback format.
|
||||
//
|
||||
// Common packet format:
|
||||
//
|
||||
// 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
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// |V=2|P| FMT | PT | length |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// 0 | SSRC of packet sender |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// 4 | SSRC of media source |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// : Feedback Control Information (FCI) :
|
||||
// : :
|
||||
//
|
||||
// Generic NACK (RFC 4585).
|
||||
//
|
||||
// FCI:
|
||||
// 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
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// | PID | BLP |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
Nack::Nack() = default;
|
||||
Nack::Nack(const Nack& rhs) = default;
|
||||
Nack::~Nack() = default;
|
||||
|
||||
bool Nack::Parse(const CommonHeader& packet) {
|
||||
if (packet.payload_size_bytes() < kCommonFeedbackLength + kNackItemLength) {
|
||||
LOG_WARN("Payload length {} is too small for a Nack.",
|
||||
packet.payload_size_bytes());
|
||||
return false;
|
||||
}
|
||||
size_t nack_items =
|
||||
(packet.payload_size_bytes() - kCommonFeedbackLength) / kNackItemLength;
|
||||
|
||||
ParseCommonFeedback(packet.payload());
|
||||
const uint8_t* next_nack = packet.payload() + kCommonFeedbackLength;
|
||||
|
||||
packet_ids_.clear();
|
||||
packed_.resize(nack_items);
|
||||
for (size_t index = 0; index < nack_items; ++index) {
|
||||
packed_[index].first_pid = ByteReader<uint16_t>::ReadBigEndian(next_nack);
|
||||
packed_[index].bitmask = ByteReader<uint16_t>::ReadBigEndian(next_nack + 2);
|
||||
next_nack += kNackItemLength;
|
||||
}
|
||||
Unpack();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t Nack::BlockLength() const {
|
||||
return kHeaderLength + kCommonFeedbackLength +
|
||||
packed_.size() * kNackItemLength;
|
||||
}
|
||||
|
||||
bool Nack::Create(uint8_t* packet, size_t* index, size_t max_length,
|
||||
PacketReadyCallback callback) const {
|
||||
// If nack list can't fit in packet, try to fragment.
|
||||
constexpr size_t kNackHeaderLength = kHeaderLength + kCommonFeedbackLength;
|
||||
for (size_t nack_index = 0; nack_index < packed_.size();) {
|
||||
size_t bytes_left_in_buffer = max_length - *index;
|
||||
if (bytes_left_in_buffer < kNackHeaderLength + kNackItemLength) {
|
||||
if (!OnBufferFull(packet, index, callback)) return false;
|
||||
continue;
|
||||
}
|
||||
size_t num_nack_fields =
|
||||
std::min((bytes_left_in_buffer - kNackHeaderLength) / kNackItemLength,
|
||||
packed_.size() - nack_index);
|
||||
|
||||
size_t payload_size_bytes =
|
||||
kCommonFeedbackLength + (num_nack_fields * kNackItemLength);
|
||||
size_t payload_size_32bits = payload_size_bytes / 4;
|
||||
CreateHeader(kFeedbackMessageType, kPacketType, payload_size_32bits, packet,
|
||||
index);
|
||||
|
||||
CreateCommonFeedback(packet + *index);
|
||||
*index += kCommonFeedbackLength;
|
||||
|
||||
size_t nack_end_index = nack_index + num_nack_fields;
|
||||
for (; nack_index < nack_end_index; ++nack_index) {
|
||||
const PackedNack& item = packed_[nack_index];
|
||||
ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 0, item.first_pid);
|
||||
ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 2, item.bitmask);
|
||||
*index += kNackItemLength;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Nack::SetPacketIds(const uint16_t* nack_list, size_t length) {
|
||||
SetPacketIds(std::vector<uint16_t>(nack_list, nack_list + length));
|
||||
}
|
||||
|
||||
void Nack::SetPacketIds(std::vector<uint16_t> nack_list) {
|
||||
packet_ids_ = std::move(nack_list);
|
||||
Pack();
|
||||
}
|
||||
|
||||
void Nack::Pack() {
|
||||
auto it = packet_ids_.begin();
|
||||
const auto end = packet_ids_.end();
|
||||
while (it != end) {
|
||||
PackedNack item;
|
||||
item.first_pid = *it++;
|
||||
// Bitmask specifies losses in any of the 16 packets following the pid.
|
||||
item.bitmask = 0;
|
||||
while (it != end) {
|
||||
uint16_t shift = static_cast<uint16_t>(*it - item.first_pid - 1);
|
||||
if (shift <= 15) {
|
||||
item.bitmask |= (1 << shift);
|
||||
++it;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
packed_.push_back(item);
|
||||
}
|
||||
}
|
||||
|
||||
void Nack::Unpack() {
|
||||
for (const PackedNack& item : packed_) {
|
||||
packet_ids_.push_back(item.first_pid);
|
||||
uint16_t pid = item.first_pid + 1;
|
||||
for (uint16_t bitmask = item.bitmask; bitmask != 0; bitmask >>= 1, ++pid) {
|
||||
if (bitmask & 1) packet_ids_.push_back(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rtcp
|
||||
} // namespace webrtc
|
||||
56
src/rtcp/nack.h
Normal file
56
src/rtcp/nack.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2015 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.
|
||||
*/
|
||||
|
||||
#ifndef MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_NACK_H_
|
||||
#define MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_NACK_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "common_header.h"
|
||||
#include "rtp_feedback.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace rtcp {
|
||||
class Nack : public RtpFeedback {
|
||||
public:
|
||||
static constexpr uint8_t kFeedbackMessageType = 1;
|
||||
Nack();
|
||||
Nack(const Nack&);
|
||||
~Nack() override;
|
||||
|
||||
// Parse assumes header is already parsed and validated.
|
||||
bool Parse(const CommonHeader& packet);
|
||||
|
||||
void SetPacketIds(const uint16_t* nack_list, size_t length);
|
||||
void SetPacketIds(std::vector<uint16_t> nack_list);
|
||||
const std::vector<uint16_t>& packet_ids() const { return packet_ids_; }
|
||||
|
||||
size_t BlockLength() const override;
|
||||
|
||||
bool Create(uint8_t* packet, size_t* index, size_t max_length,
|
||||
PacketReadyCallback callback) const override;
|
||||
|
||||
private:
|
||||
static constexpr size_t kNackItemLength = 4;
|
||||
struct PackedNack {
|
||||
uint16_t first_pid;
|
||||
uint16_t bitmask;
|
||||
};
|
||||
|
||||
void Pack(); // Fills packed_ using packed_ids_. (used in SetPacketIds).
|
||||
void Unpack(); // Fills packet_ids_ using packed_. (used in Parse).
|
||||
|
||||
std::vector<PackedNack> packed_;
|
||||
std::vector<uint16_t> packet_ids_;
|
||||
};
|
||||
|
||||
} // namespace rtcp
|
||||
} // namespace webrtc
|
||||
#endif // MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_NACK_H_
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
class RtcpPacket {
|
||||
public:
|
||||
typedef enum { SR = 200, RR = 201, TCC = 205 } PAYLOAD_TYPE;
|
||||
typedef enum { SR = 200, RR = 201, TCC = 11, NACK = 1 } PAYLOAD_TYPE;
|
||||
// Callback used to signal that an RTCP packet is ready. Note that this may
|
||||
// not contain all data in this RtcpPacket; if a packet cannot fit in
|
||||
// max_length bytes, it will be fragmented and multiple calls to this
|
||||
@@ -61,7 +61,4 @@ class RtcpPacket {
|
||||
uint32_t sender_ssrc_ = 0;
|
||||
};
|
||||
|
||||
using RtcpSender =
|
||||
std::function<void(std::vector<std::unique_ptr<RtcpPacket>> packets)>;
|
||||
|
||||
#endif
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "congestion_control_feedback.h"
|
||||
#include "nack.h"
|
||||
|
||||
struct RtcpPacketInfo {
|
||||
uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
|
||||
|
||||
@@ -14,16 +14,16 @@
|
||||
|
||||
#include "log.h"
|
||||
|
||||
class RTCPSender {
|
||||
class RtcpSender {
|
||||
public:
|
||||
RTCPSender(std::function<int(const uint8_t*, size_t)> callback,
|
||||
RtcpSender(std::function<int(const uint8_t*, size_t)> callback,
|
||||
size_t max_packet_size)
|
||||
: callback_(callback), max_packet_size_(max_packet_size) {
|
||||
if (max_packet_size >= IP_PACKET_SIZE) {
|
||||
LOG_ERROR("max_packet_size must be less than IP_PACKET_SIZE");
|
||||
}
|
||||
}
|
||||
~RTCPSender() {
|
||||
~RtcpSender() {
|
||||
if (index_ != 0) {
|
||||
LOG_ERROR("Unsent rtcp packet");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user