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> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #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" |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace rtcp { |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 22 | constexpr uint8_t Nack::kFeedbackMessageType; |
| 23 | constexpr size_t Nack::kNackItemLength; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 24 | // 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 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 48 | Nack::Nack() {} |
| 49 | Nack::~Nack() {} |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 50 | |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 51 | bool 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) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 56 | RTC_LOG(LS_WARNING) << "Payload length " << packet.payload_size_bytes() |
| 57 | << " is too small for a Nack."; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | size_t nack_items = |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 61 | (packet.payload_size_bytes() - kCommonFeedbackLength) / kNackItemLength; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 62 | |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 63 | ParseCommonFeedback(packet.payload()); |
| 64 | const uint8_t* next_nack = packet.payload() + kCommonFeedbackLength; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 65 | |
| 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 | |
eladalon | 8fa21c4 | 2017-06-16 07:07:47 -0700 | [diff] [blame] | 78 | size_t Nack::BlockLength() const { |
| 79 | return kHeaderLength + kCommonFeedbackLength + |
| 80 | packed_.size() * kNackItemLength; |
| 81 | } |
| 82 | |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 83 | bool 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. |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 89 | constexpr size_t kNackHeaderLength = kHeaderLength + kCommonFeedbackLength; |
| 90 | for (size_t nack_index = 0; nack_index < packed_.size();) { |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 91 | size_t bytes_left_in_buffer = max_length - *index; |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 92 | if (bytes_left_in_buffer < kNackHeaderLength + kNackItemLength) { |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 93 | if (!OnBufferFull(packet, index, callback)) |
| 94 | return false; |
| 95 | continue; |
| 96 | } |
| 97 | size_t num_nack_fields = |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 98 | std::min((bytes_left_in_buffer - kNackHeaderLength) / kNackItemLength, |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 99 | packed_.size() - nack_index); |
| 100 | |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 101 | 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, |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 106 | index); |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 107 | |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 108 | CreateCommonFeedback(packet + *index); |
| 109 | *index += kCommonFeedbackLength; |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 110 | |
| 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]; |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 114 | 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); |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 119 | } |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 124 | void Nack::SetPacketIds(const uint16_t* nack_list, size_t length) { |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 125 | 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 | |
| 132 | void 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 | |
| 155 | void 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; |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 161 | for (uint16_t bitmask = item.bitmask; bitmask != 0; bitmask >>= 1, ++pid) { |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 162 | if (bitmask & 1) |
| 163 | packet_ids_.push_back(pid); |
danilchap | 95e7560 | 2016-08-01 06:51:13 -0700 | [diff] [blame] | 164 | } |
danilchap | a8890a5 | 2015-12-22 03:43:04 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | } // namespace rtcp |
| 169 | } // namespace webrtc |