blob: f83e5c0b38ac61803ab02c70867597c48ec084d0 [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// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
danilchap95e75602016-08-01 06:51:13 -070049Nack::Nack() {}
50Nack::~Nack() {}
danilchapa8890a52015-12-22 03:43:04 -080051
danilchap95e75602016-08-01 06:51:13 -070052bool Nack::Parse(const CommonHeader& packet) {
53 RTC_DCHECK_EQ(packet.type(), kPacketType);
54 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType);
55
56 if (packet.payload_size_bytes() < kCommonFeedbackLength + kNackItemLength) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010057 RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes()
58 << " is too small for a Nack.";
danilchapa8890a52015-12-22 03:43:04 -080059 return false;
60 }
61 size_t nack_items =
danilchap95e75602016-08-01 06:51:13 -070062 (packet.payload_size_bytes() - kCommonFeedbackLength) / kNackItemLength;
danilchapa8890a52015-12-22 03:43:04 -080063
danilchap95e75602016-08-01 06:51:13 -070064 ParseCommonFeedback(packet.payload());
65 const uint8_t* next_nack = packet.payload() + kCommonFeedbackLength;
danilchapa8890a52015-12-22 03:43:04 -080066
67 packet_ids_.clear();
68 packed_.resize(nack_items);
69 for (size_t index = 0; index < nack_items; ++index) {
70 packed_[index].first_pid = ByteReader<uint16_t>::ReadBigEndian(next_nack);
71 packed_[index].bitmask = ByteReader<uint16_t>::ReadBigEndian(next_nack + 2);
72 next_nack += kNackItemLength;
73 }
74 Unpack();
75
76 return true;
77}
78
eladalon8fa21c42017-06-16 07:07:47 -070079size_t Nack::BlockLength() const {
80 return kHeaderLength + kCommonFeedbackLength +
81 packed_.size() * kNackItemLength;
82}
83
danilchapa8890a52015-12-22 03:43:04 -080084bool Nack::Create(uint8_t* packet,
85 size_t* index,
86 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010087 PacketReadyCallback callback) const {
danilchapa8890a52015-12-22 03:43:04 -080088 RTC_DCHECK(!packed_.empty());
89 // If nack list can't fit in packet, try to fragment.
danilchap95e75602016-08-01 06:51:13 -070090 constexpr size_t kNackHeaderLength = kHeaderLength + kCommonFeedbackLength;
91 for (size_t nack_index = 0; nack_index < packed_.size();) {
danilchapa8890a52015-12-22 03:43:04 -080092 size_t bytes_left_in_buffer = max_length - *index;
danilchap95e75602016-08-01 06:51:13 -070093 if (bytes_left_in_buffer < kNackHeaderLength + kNackItemLength) {
danilchapa8890a52015-12-22 03:43:04 -080094 if (!OnBufferFull(packet, index, callback))
95 return false;
96 continue;
97 }
98 size_t num_nack_fields =
danilchap95e75602016-08-01 06:51:13 -070099 std::min((bytes_left_in_buffer - kNackHeaderLength) / kNackItemLength,
danilchapa8890a52015-12-22 03:43:04 -0800100 packed_.size() - nack_index);
101
danilchap95e75602016-08-01 06:51:13 -0700102 size_t payload_size_bytes =
103 kCommonFeedbackLength + (num_nack_fields * kNackItemLength);
104 size_t payload_size_32bits =
105 rtc::CheckedDivExact<size_t>(payload_size_bytes, 4);
106 CreateHeader(kFeedbackMessageType, kPacketType, payload_size_32bits, packet,
danilchapa8890a52015-12-22 03:43:04 -0800107 index);
danilchap95e75602016-08-01 06:51:13 -0700108
danilchapa8890a52015-12-22 03:43:04 -0800109 CreateCommonFeedback(packet + *index);
110 *index += kCommonFeedbackLength;
danilchap95e75602016-08-01 06:51:13 -0700111
112 size_t nack_end_index = nack_index + num_nack_fields;
113 for (; nack_index < nack_end_index; ++nack_index) {
114 const PackedNack& item = packed_[nack_index];
danilchapa8890a52015-12-22 03:43:04 -0800115 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 0, item.first_pid);
116 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 2, item.bitmask);
117 *index += kNackItemLength;
118 }
119 RTC_DCHECK_LE(*index, max_length);
danilchap95e75602016-08-01 06:51:13 -0700120 }
danilchapa8890a52015-12-22 03:43:04 -0800121
122 return true;
123}
124
danilchap822a16f2016-09-27 09:27:47 -0700125void Nack::SetPacketIds(const uint16_t* nack_list, size_t length) {
danilchapa8890a52015-12-22 03:43:04 -0800126 RTC_DCHECK(nack_list);
Danil Chapovalov327c43c2017-11-27 17:23:04 +0100127 SetPacketIds(std::vector<uint16_t>(nack_list, nack_list + length));
128}
129
130void Nack::SetPacketIds(std::vector<uint16_t> nack_list) {
danilchapa8890a52015-12-22 03:43:04 -0800131 RTC_DCHECK(packet_ids_.empty());
132 RTC_DCHECK(packed_.empty());
Danil Chapovalov327c43c2017-11-27 17:23:04 +0100133 packet_ids_ = std::move(nack_list);
danilchapa8890a52015-12-22 03:43:04 -0800134 Pack();
135}
136
137void Nack::Pack() {
138 RTC_DCHECK(!packet_ids_.empty());
139 RTC_DCHECK(packed_.empty());
140 auto it = packet_ids_.begin();
141 const auto end = packet_ids_.end();
142 while (it != end) {
143 PackedNack item;
144 item.first_pid = *it++;
145 // Bitmask specifies losses in any of the 16 packets following the pid.
146 item.bitmask = 0;
147 while (it != end) {
148 uint16_t shift = static_cast<uint16_t>(*it - item.first_pid - 1);
149 if (shift <= 15) {
150 item.bitmask |= (1 << shift);
151 ++it;
152 } else {
153 break;
154 }
155 }
156 packed_.push_back(item);
157 }
158}
159
160void Nack::Unpack() {
161 RTC_DCHECK(packet_ids_.empty());
162 RTC_DCHECK(!packed_.empty());
163 for (const PackedNack& item : packed_) {
164 packet_ids_.push_back(item.first_pid);
165 uint16_t pid = item.first_pid + 1;
danilchap95e75602016-08-01 06:51:13 -0700166 for (uint16_t bitmask = item.bitmask; bitmask != 0; bitmask >>= 1, ++pid) {
danilchapa8890a52015-12-22 03:43:04 -0800167 if (bitmask & 1)
168 packet_ids_.push_back(pid);
danilchap95e75602016-08-01 06:51:13 -0700169 }
danilchapa8890a52015-12-22 03:43:04 -0800170 }
171}
172
173} // namespace rtcp
174} // namespace webrtc