blob: 6fe7eade62b6478096b2dbbade23cb32835d91fa [file] [log] [blame]
danilchapa8890a52015-12-22 03:43:04 -08001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
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/rtcp_packet/nack.h"
danilchapa8890a52015-12-22 03:43:04 -080012
13#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <cstdint>
Danil Chapovalov327c43c2017-11-27 17:23:04 +010015#include <utility>
danilchapa8890a52015-12-22 03:43:04 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/rtp_rtcp/source/byte_io.h"
18#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
danilchapa8890a52015-12-22 03:43:04 -080021
22namespace webrtc {
23namespace rtcp {
danilchap95e75602016-08-01 06:51:13 -070024constexpr uint8_t Nack::kFeedbackMessageType;
25constexpr size_t Nack::kNackItemLength;
danilchapa8890a52015-12-22 03:43:04 -080026// RFC 4585: Feedback format.
27//
28// Common packet format:
29//
30// 0 1 2 3
31// 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
32// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33// |V=2|P| FMT | PT | length |
34// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35// 0 | SSRC of packet sender |
36// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37// 4 | SSRC of media source |
38// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39// : Feedback Control Information (FCI) :
40// : :
41//
42// Generic NACK (RFC 4585).
43//
44// FCI:
45// 0 1 2 3
46// 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
47// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48// | PID | BLP |
49// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Mirko Bonadei55d5ef02018-09-03 09:47:38 +020050Nack::Nack() = default;
51Nack::Nack(const Nack& rhs) = default;
52Nack::~Nack() = default;
danilchapa8890a52015-12-22 03:43:04 -080053
danilchap95e75602016-08-01 06:51:13 -070054bool Nack::Parse(const CommonHeader& packet) {
55 RTC_DCHECK_EQ(packet.type(), kPacketType);
56 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
57
58 if (packet.payload_size_bytes() < kCommonFeedbackLength + kNackItemLength) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010059 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
60 << " is too small for a Nack.";
danilchapa8890a52015-12-22 03:43:04 -080061 return false;
62 }
63 size_t nack_items =
danilchap95e75602016-08-01 06:51:13 -070064 (packet.payload_size_bytes() - kCommonFeedbackLength) / kNackItemLength;
danilchapa8890a52015-12-22 03:43:04 -080065
danilchap95e75602016-08-01 06:51:13 -070066 ParseCommonFeedback(packet.payload());
67 const uint8_t* next_nack = packet.payload() + kCommonFeedbackLength;
danilchapa8890a52015-12-22 03:43:04 -080068
69 packet_ids_.clear();
70 packed_.resize(nack_items);
71 for (size_t index = 0; index < nack_items; ++index) {
72 packed_[index].first_pid = ByteReader<uint16_t>::ReadBigEndian(next_nack);
73 packed_[index].bitmask = ByteReader<uint16_t>::ReadBigEndian(next_nack + 2);
74 next_nack += kNackItemLength;
75 }
76 Unpack();
77
78 return true;
79}
80
eladalon8fa21c42017-06-16 07:07:47 -070081size_t Nack::BlockLength() const {
82 return kHeaderLength + kCommonFeedbackLength +
83 packed_.size() * kNackItemLength;
84}
85
danilchapa8890a52015-12-22 03:43:04 -080086bool Nack::Create(uint8_t* packet,
87 size_t* index,
88 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010089 PacketReadyCallback callback) const {
danilchapa8890a52015-12-22 03:43:04 -080090 RTC_DCHECK(!packed_.empty());
91 // If nack list can't fit in packet, try to fragment.
danilchap95e75602016-08-01 06:51:13 -070092 constexpr size_t kNackHeaderLength = kHeaderLength + kCommonFeedbackLength;
93 for (size_t nack_index = 0; nack_index < packed_.size();) {
danilchapa8890a52015-12-22 03:43:04 -080094 size_t bytes_left_in_buffer = max_length - *index;
danilchap95e75602016-08-01 06:51:13 -070095 if (bytes_left_in_buffer < kNackHeaderLength + kNackItemLength) {
danilchapa8890a52015-12-22 03:43:04 -080096 if (!OnBufferFull(packet, index, callback))
97 return false;
98 continue;
99 }
100 size_t num_nack_fields =
danilchap95e75602016-08-01 06:51:13 -0700101 std::min((bytes_left_in_buffer - kNackHeaderLength) / kNackItemLength,
danilchapa8890a52015-12-22 03:43:04 -0800102 packed_.size() - nack_index);
103
danilchap95e75602016-08-01 06:51:13 -0700104 size_t payload_size_bytes =
105 kCommonFeedbackLength + (num_nack_fields * kNackItemLength);
106 size_t payload_size_32bits =
107 rtc::CheckedDivExact<size_t>(payload_size_bytes, 4);
108 CreateHeader(kFeedbackMessageType, kPacketType, payload_size_32bits, packet,
danilchapa8890a52015-12-22 03:43:04 -0800109 index);
danilchap95e75602016-08-01 06:51:13 -0700110
danilchapa8890a52015-12-22 03:43:04 -0800111 CreateCommonFeedback(packet + *index);
112 *index += kCommonFeedbackLength;
danilchap95e75602016-08-01 06:51:13 -0700113
114 size_t nack_end_index = nack_index + num_nack_fields;
115 for (; nack_index < nack_end_index; ++nack_index) {
116 const PackedNack& item = packed_[nack_index];
danilchapa8890a52015-12-22 03:43:04 -0800117 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 0, item.first_pid);
118 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 2, item.bitmask);
119 *index += kNackItemLength;
120 }
121 RTC_DCHECK_LE(*index, max_length);
danilchap95e75602016-08-01 06:51:13 -0700122 }
danilchapa8890a52015-12-22 03:43:04 -0800123
124 return true;
125}
126
danilchap822a16f2016-09-27 09:27:47 -0700127void Nack::SetPacketIds(const uint16_t* nack_list, size_t length) {
danilchapa8890a52015-12-22 03:43:04 -0800128 RTC_DCHECK(nack_list);
Danil Chapovalov327c43c2017-11-27 17:23:04 +0100129 SetPacketIds(std::vector<uint16_t>(nack_list, nack_list + length));
130}
131
132void Nack::SetPacketIds(std::vector<uint16_t> nack_list) {
danilchapa8890a52015-12-22 03:43:04 -0800133 RTC_DCHECK(packet_ids_.empty());
134 RTC_DCHECK(packed_.empty());
Danil Chapovalov327c43c2017-11-27 17:23:04 +0100135 packet_ids_ = std::move(nack_list);
danilchapa8890a52015-12-22 03:43:04 -0800136 Pack();
137}
138
139void Nack::Pack() {
140 RTC_DCHECK(!packet_ids_.empty());
141 RTC_DCHECK(packed_.empty());
142 auto it = packet_ids_.begin();
143 const auto end = packet_ids_.end();
144 while (it != end) {
145 PackedNack item;
146 item.first_pid = *it++;
147 // Bitmask specifies losses in any of the 16 packets following the pid.
148 item.bitmask = 0;
149 while (it != end) {
150 uint16_t shift = static_cast<uint16_t>(*it - item.first_pid - 1);
151 if (shift <= 15) {
152 item.bitmask |= (1 << shift);
153 ++it;
154 } else {
155 break;
156 }
157 }
158 packed_.push_back(item);
159 }
160}
161
162void Nack::Unpack() {
163 RTC_DCHECK(packet_ids_.empty());
164 RTC_DCHECK(!packed_.empty());
165 for (const PackedNack& item : packed_) {
166 packet_ids_.push_back(item.first_pid);
167 uint16_t pid = item.first_pid + 1;
danilchap95e75602016-08-01 06:51:13 -0700168 for (uint16_t bitmask = item.bitmask; bitmask != 0; bitmask >>= 1, ++pid) {
danilchapa8890a52015-12-22 03:43:04 -0800169 if (bitmask & 1)
170 packet_ids_.push_back(pid);
danilchap95e75602016-08-01 06:51:13 -0700171 }
danilchapa8890a52015-12-22 03:43:04 -0800172 }
173}
174
175} // namespace rtcp
176} // namespace webrtc