blob: 6a4a0bdc0e43ba7c348eff75c09fa39f9821d56a [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>
Danil Chapovalov327c43c2017-11-27 17:23:04 +010014#include <utility>
danilchapa8890a52015-12-22 03:43:04 -080015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/rtp_rtcp/source/byte_io.h"
17#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
danilchapa8890a52015-12-22 03:43:04 -080020
21namespace webrtc {
22namespace rtcp {
danilchap95e75602016-08-01 06:51:13 -070023constexpr uint8_t Nack::kFeedbackMessageType;
24constexpr size_t Nack::kNackItemLength;
danilchapa8890a52015-12-22 03:43:04 -080025// RFC 4585: Feedback format.
26//
27// Common packet format:
28//
29// 0 1 2 3
30// 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
31// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32// |V=2|P| FMT | PT | length |
33// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34// 0 | SSRC of packet sender |
35// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36// 4 | SSRC of media source |
37// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38// : Feedback Control Information (FCI) :
39// : :
40//
41// Generic NACK (RFC 4585).
42//
43// FCI:
44// 0 1 2 3
45// 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
46// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47// | PID | BLP |
48// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Mirko Bonadei55d5ef02018-09-03 09:47:38 +020049Nack::Nack() = default;
50Nack::Nack(const Nack& rhs) = default;
51Nack::~Nack() = default;
danilchapa8890a52015-12-22 03:43:04 -080052
danilchap95e75602016-08-01 06:51:13 -070053bool Nack::Parse(const CommonHeader& packet) {
54 RTC_DCHECK_EQ(packet.type(), kPacketType);
55 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
56
57 if (packet.payload_size_bytes() < kCommonFeedbackLength + kNackItemLength) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010058 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
59 << " is too small for a Nack.";
danilchapa8890a52015-12-22 03:43:04 -080060 return false;
61 }
62 size_t nack_items =
danilchap95e75602016-08-01 06:51:13 -070063 (packet.payload_size_bytes() - kCommonFeedbackLength) / kNackItemLength;
danilchapa8890a52015-12-22 03:43:04 -080064
danilchap95e75602016-08-01 06:51:13 -070065 ParseCommonFeedback(packet.payload());
66 const uint8_t* next_nack = packet.payload() + kCommonFeedbackLength;
danilchapa8890a52015-12-22 03:43:04 -080067
68 packet_ids_.clear();
69 packed_.resize(nack_items);
70 for (size_t index = 0; index < nack_items; ++index) {
71 packed_[index].first_pid = ByteReader<uint16_t>::ReadBigEndian(next_nack);
72 packed_[index].bitmask = ByteReader<uint16_t>::ReadBigEndian(next_nack + 2);
73 next_nack += kNackItemLength;
74 }
75 Unpack();
76
77 return true;
78}
79
eladalon8fa21c42017-06-16 07:07:47 -070080size_t Nack::BlockLength() const {
81 return kHeaderLength + kCommonFeedbackLength +
82 packed_.size() * kNackItemLength;
83}
84
danilchapa8890a52015-12-22 03:43:04 -080085bool Nack::Create(uint8_t* packet,
86 size_t* index,
87 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010088 PacketReadyCallback callback) const {
danilchapa8890a52015-12-22 03:43:04 -080089 RTC_DCHECK(!packed_.empty());
90 // If nack list can't fit in packet, try to fragment.
danilchap95e75602016-08-01 06:51:13 -070091 constexpr size_t kNackHeaderLength = kHeaderLength + kCommonFeedbackLength;
92 for (size_t nack_index = 0; nack_index < packed_.size();) {
danilchapa8890a52015-12-22 03:43:04 -080093 size_t bytes_left_in_buffer = max_length - *index;
danilchap95e75602016-08-01 06:51:13 -070094 if (bytes_left_in_buffer < kNackHeaderLength + kNackItemLength) {
danilchapa8890a52015-12-22 03:43:04 -080095 if (!OnBufferFull(packet, index, callback))
96 return false;
97 continue;
98 }
99 size_t num_nack_fields =
danilchap95e75602016-08-01 06:51:13 -0700100 std::min((bytes_left_in_buffer - kNackHeaderLength) / kNackItemLength,
danilchapa8890a52015-12-22 03:43:04 -0800101 packed_.size() - nack_index);
102
danilchap95e75602016-08-01 06:51:13 -0700103 size_t payload_size_bytes =
104 kCommonFeedbackLength + (num_nack_fields * kNackItemLength);
105 size_t payload_size_32bits =
106 rtc::CheckedDivExact<size_t>(payload_size_bytes, 4);
107 CreateHeader(kFeedbackMessageType, kPacketType, payload_size_32bits, packet,
danilchapa8890a52015-12-22 03:43:04 -0800108 index);
danilchap95e75602016-08-01 06:51:13 -0700109
danilchapa8890a52015-12-22 03:43:04 -0800110 CreateCommonFeedback(packet + *index);
111 *index += kCommonFeedbackLength;
danilchap95e75602016-08-01 06:51:13 -0700112
113 size_t nack_end_index = nack_index + num_nack_fields;
114 for (; nack_index < nack_end_index; ++nack_index) {
115 const PackedNack& item = packed_[nack_index];
danilchapa8890a52015-12-22 03:43:04 -0800116 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 0, item.first_pid);
117 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 2, item.bitmask);
118 *index += kNackItemLength;
119 }
120 RTC_DCHECK_LE(*index, max_length);
danilchap95e75602016-08-01 06:51:13 -0700121 }
danilchapa8890a52015-12-22 03:43:04 -0800122
123 return true;
124}
125
danilchap822a16f2016-09-27 09:27:47 -0700126void Nack::SetPacketIds(const uint16_t* nack_list, size_t length) {
danilchapa8890a52015-12-22 03:43:04 -0800127 RTC_DCHECK(nack_list);
Danil Chapovalov327c43c2017-11-27 17:23:04 +0100128 SetPacketIds(std::vector<uint16_t>(nack_list, nack_list + length));
129}
130
131void Nack::SetPacketIds(std::vector<uint16_t> nack_list) {
danilchapa8890a52015-12-22 03:43:04 -0800132 RTC_DCHECK(packet_ids_.empty());
133 RTC_DCHECK(packed_.empty());
Danil Chapovalov327c43c2017-11-27 17:23:04 +0100134 packet_ids_ = std::move(nack_list);
danilchapa8890a52015-12-22 03:43:04 -0800135 Pack();
136}
137
138void Nack::Pack() {
139 RTC_DCHECK(!packet_ids_.empty());
140 RTC_DCHECK(packed_.empty());
141 auto it = packet_ids_.begin();
142 const auto end = packet_ids_.end();
143 while (it != end) {
144 PackedNack item;
145 item.first_pid = *it++;
146 // Bitmask specifies losses in any of the 16 packets following the pid.
147 item.bitmask = 0;
148 while (it != end) {
149 uint16_t shift = static_cast<uint16_t>(*it - item.first_pid - 1);
150 if (shift <= 15) {
151 item.bitmask |= (1 << shift);
152 ++it;
153 } else {
154 break;
155 }
156 }
157 packed_.push_back(item);
158 }
159}
160
161void Nack::Unpack() {
162 RTC_DCHECK(packet_ids_.empty());
163 RTC_DCHECK(!packed_.empty());
164 for (const PackedNack& item : packed_) {
165 packet_ids_.push_back(item.first_pid);
166 uint16_t pid = item.first_pid + 1;
danilchap95e75602016-08-01 06:51:13 -0700167 for (uint16_t bitmask = item.bitmask; bitmask != 0; bitmask >>= 1, ++pid) {
danilchapa8890a52015-12-22 03:43:04 -0800168 if (bitmask & 1)
169 packet_ids_.push_back(pid);
danilchap95e75602016-08-01 06:51:13 -0700170 }
danilchapa8890a52015-12-22 03:43:04 -0800171 }
172}
173
174} // namespace rtcp
175} // namespace webrtc