blob: 4cf2d7d6226b3bfd25bdb35d70b6e2678ee7a3cc [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>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/rtp_rtcp/source/byte_io.h"
16#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
danilchapa8890a52015-12-22 03:43:04 -080019
20namespace webrtc {
21namespace rtcp {
danilchap95e75602016-08-01 06:51:13 -070022constexpr uint8_t Nack::kFeedbackMessageType;
23constexpr size_t Nack::kNackItemLength;
danilchapa8890a52015-12-22 03:43:04 -080024// RFC 4585: Feedback format.
25//
26// Common packet format:
27//
28// 0 1 2 3
29// 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
30// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31// |V=2|P| FMT | PT | length |
32// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33// 0 | SSRC of packet sender |
34// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35// 4 | SSRC of media source |
36// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37// : Feedback Control Information (FCI) :
38// : :
39//
40// Generic NACK (RFC 4585).
41//
42// FCI:
43// 0 1 2 3
44// 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
45// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46// | PID | BLP |
47// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
danilchap95e75602016-08-01 06:51:13 -070048Nack::Nack() {}
49Nack::~Nack() {}
danilchapa8890a52015-12-22 03:43:04 -080050
danilchap95e75602016-08-01 06:51:13 -070051bool Nack::Parse(const CommonHeader& packet) {
52 RTC_DCHECK_EQ(packet.type(), kPacketType);
53 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
54
55 if (packet.payload_size_bytes() < kCommonFeedbackLength + kNackItemLength) {
56 LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
danilchapa8890a52015-12-22 03:43:04 -080057 << " is too small for a Nack.";
58 return false;
59 }
60 size_t nack_items =
danilchap95e75602016-08-01 06:51:13 -070061 (packet.payload_size_bytes() - kCommonFeedbackLength) / kNackItemLength;
danilchapa8890a52015-12-22 03:43:04 -080062
danilchap95e75602016-08-01 06:51:13 -070063 ParseCommonFeedback(packet.payload());
64 const uint8_t* next_nack = packet.payload() + kCommonFeedbackLength;
danilchapa8890a52015-12-22 03:43:04 -080065
66 packet_ids_.clear();
67 packed_.resize(nack_items);
68 for (size_t index = 0; index < nack_items; ++index) {
69 packed_[index].first_pid = ByteReader<uint16_t>::ReadBigEndian(next_nack);
70 packed_[index].bitmask = ByteReader<uint16_t>::ReadBigEndian(next_nack + 2);
71 next_nack += kNackItemLength;
72 }
73 Unpack();
74
75 return true;
76}
77
eladalon8fa21c42017-06-16 07:07:47 -070078size_t Nack::BlockLength() const {
79 return kHeaderLength + kCommonFeedbackLength +
80 packed_.size() * kNackItemLength;
81}
82
danilchapa8890a52015-12-22 03:43:04 -080083bool Nack::Create(uint8_t* packet,
84 size_t* index,
85 size_t max_length,
86 RtcpPacket::PacketReadyCallback* callback) const {
87 RTC_DCHECK(!packed_.empty());
88 // If nack list can't fit in packet, try to fragment.
danilchap95e75602016-08-01 06:51:13 -070089 constexpr size_t kNackHeaderLength = kHeaderLength + kCommonFeedbackLength;
90 for (size_t nack_index = 0; nack_index < packed_.size();) {
danilchapa8890a52015-12-22 03:43:04 -080091 size_t bytes_left_in_buffer = max_length - *index;
danilchap95e75602016-08-01 06:51:13 -070092 if (bytes_left_in_buffer < kNackHeaderLength + kNackItemLength) {
danilchapa8890a52015-12-22 03:43:04 -080093 if (!OnBufferFull(packet, index, callback))
94 return false;
95 continue;
96 }
97 size_t num_nack_fields =
danilchap95e75602016-08-01 06:51:13 -070098 std::min((bytes_left_in_buffer - kNackHeaderLength) / kNackItemLength,
danilchapa8890a52015-12-22 03:43:04 -080099 packed_.size() - nack_index);
100
danilchap95e75602016-08-01 06:51:13 -0700101 size_t payload_size_bytes =
102 kCommonFeedbackLength + (num_nack_fields * kNackItemLength);
103 size_t payload_size_32bits =
104 rtc::CheckedDivExact<size_t>(payload_size_bytes, 4);
105 CreateHeader(kFeedbackMessageType, kPacketType, payload_size_32bits, packet,
danilchapa8890a52015-12-22 03:43:04 -0800106 index);
danilchap95e75602016-08-01 06:51:13 -0700107
danilchapa8890a52015-12-22 03:43:04 -0800108 CreateCommonFeedback(packet + *index);
109 *index += kCommonFeedbackLength;
danilchap95e75602016-08-01 06:51:13 -0700110
111 size_t nack_end_index = nack_index + num_nack_fields;
112 for (; nack_index < nack_end_index; ++nack_index) {
113 const PackedNack& item = packed_[nack_index];
danilchapa8890a52015-12-22 03:43:04 -0800114 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 0, item.first_pid);
115 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 2, item.bitmask);
116 *index += kNackItemLength;
117 }
118 RTC_DCHECK_LE(*index, max_length);
danilchap95e75602016-08-01 06:51:13 -0700119 }
danilchapa8890a52015-12-22 03:43:04 -0800120
121 return true;
122}
123
danilchap822a16f2016-09-27 09:27:47 -0700124void Nack::SetPacketIds(const uint16_t* nack_list, size_t length) {
danilchapa8890a52015-12-22 03:43:04 -0800125 RTC_DCHECK(nack_list);
126 RTC_DCHECK(packet_ids_.empty());
127 RTC_DCHECK(packed_.empty());
128 packet_ids_.assign(nack_list, nack_list + length);
129 Pack();
130}
131
132void Nack::Pack() {
133 RTC_DCHECK(!packet_ids_.empty());
134 RTC_DCHECK(packed_.empty());
135 auto it = packet_ids_.begin();
136 const auto end = packet_ids_.end();
137 while (it != end) {
138 PackedNack item;
139 item.first_pid = *it++;
140 // Bitmask specifies losses in any of the 16 packets following the pid.
141 item.bitmask = 0;
142 while (it != end) {
143 uint16_t shift = static_cast<uint16_t>(*it - item.first_pid - 1);
144 if (shift <= 15) {
145 item.bitmask |= (1 << shift);
146 ++it;
147 } else {
148 break;
149 }
150 }
151 packed_.push_back(item);
152 }
153}
154
155void Nack::Unpack() {
156 RTC_DCHECK(packet_ids_.empty());
157 RTC_DCHECK(!packed_.empty());
158 for (const PackedNack& item : packed_) {
159 packet_ids_.push_back(item.first_pid);
160 uint16_t pid = item.first_pid + 1;
danilchap95e75602016-08-01 06:51:13 -0700161 for (uint16_t bitmask = item.bitmask; bitmask != 0; bitmask >>= 1, ++pid) {
danilchapa8890a52015-12-22 03:43:04 -0800162 if (bitmask & 1)
163 packet_ids_.push_back(pid);
danilchap95e75602016-08-01 06:51:13 -0700164 }
danilchapa8890a52015-12-22 03:43:04 -0800165 }
166}
167
168} // namespace rtcp
169} // namespace webrtc