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