danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/source/rtcp_packet/nack.h" |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 14 | #include <cstdint> |
Danil Chapovalov | 327c43c | 2017-11-27 17:23:04 +0100 | [diff] [blame] | 15 | #include <utility> |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #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" |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | namespace rtcp { |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 24 | constexpr uint8_t Nack::kFeedbackMessageType; |
| 25 | constexpr size_t Nack::kNackItemLength; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 26 | // 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 Bonadei | 55d5ef0 | 2018-09-03 09:47:38 +0200 | [diff] [blame] | 50 | Nack::Nack() = default; |
| 51 | Nack::Nack(const Nack& rhs) = default; |
| 52 | Nack::~Nack() = default; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 53 | |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 54 | bool 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 59 | RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes() |
| 60 | << " is too small for a Nack."; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | size_t nack_items = |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 64 | (packet.payload_size_bytes() - kCommonFeedbackLength) / kNackItemLength; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 65 | |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 66 | ParseCommonFeedback(packet.payload()); |
| 67 | const uint8_t* next_nack = packet.payload() + kCommonFeedbackLength; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 68 | |
| 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 | |
eladalon | 8fa21c4 | 2017-06-16 07:07:47 -0700 | [diff] [blame] | 81 | size_t Nack::BlockLength() const { |
| 82 | return kHeaderLength + kCommonFeedbackLength + |
| 83 | packed_.size() * kNackItemLength; |
| 84 | } |
| 85 | |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 86 | bool Nack::Create(uint8_t* packet, |
| 87 | size_t* index, |
| 88 | size_t max_length, |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 89 | PacketReadyCallback callback) const { |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 90 | RTC_DCHECK(!packed_.empty()); |
| 91 | // If nack list can't fit in packet, try to fragment. |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 92 | constexpr size_t kNackHeaderLength = kHeaderLength + kCommonFeedbackLength; |
| 93 | for (size_t nack_index = 0; nack_index < packed_.size();) { |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 94 | size_t bytes_left_in_buffer = max_length - *index; |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 95 | if (bytes_left_in_buffer < kNackHeaderLength + kNackItemLength) { |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 96 | if (!OnBufferFull(packet, index, callback)) |
| 97 | return false; |
| 98 | continue; |
| 99 | } |
| 100 | size_t num_nack_fields = |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 101 | std::min((bytes_left_in_buffer - kNackHeaderLength) / kNackItemLength, |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 102 | packed_.size() - nack_index); |
| 103 | |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 104 | 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, |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 109 | index); |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 110 | |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 111 | CreateCommonFeedback(packet + *index); |
| 112 | *index += kCommonFeedbackLength; |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 113 | |
| 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]; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 117 | 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); |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 122 | } |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 127 | void Nack::SetPacketIds(const uint16_t* nack_list, size_t length) { |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 128 | RTC_DCHECK(nack_list); |
Danil Chapovalov | 327c43c | 2017-11-27 17:23:04 +0100 | [diff] [blame] | 129 | SetPacketIds(std::vector<uint16_t>(nack_list, nack_list + length)); |
| 130 | } |
| 131 | |
| 132 | void Nack::SetPacketIds(std::vector<uint16_t> nack_list) { |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 133 | RTC_DCHECK(packet_ids_.empty()); |
| 134 | RTC_DCHECK(packed_.empty()); |
Danil Chapovalov | 327c43c | 2017-11-27 17:23:04 +0100 | [diff] [blame] | 135 | packet_ids_ = std::move(nack_list); |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 136 | Pack(); |
| 137 | } |
| 138 | |
| 139 | void 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 | |
| 162 | void 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; |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 168 | for (uint16_t bitmask = item.bitmask; bitmask != 0; bitmask >>= 1, ++pid) { |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 169 | if (bitmask & 1) |
| 170 | packet_ids_.push_back(pid); |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 171 | } |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | } // namespace rtcp |
| 176 | } // namespace webrtc |