OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/nack.h" | 11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/nack.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 | 14 |
15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
16 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
17 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 17 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
18 | 18 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h" |
19 using webrtc::RTCPUtility::RtcpCommonHeader; | |
20 | 19 |
21 namespace webrtc { | 20 namespace webrtc { |
22 namespace rtcp { | 21 namespace rtcp { |
23 | 22 constexpr uint8_t Nack::kFeedbackMessageType; |
| 23 constexpr size_t Nack::kNackItemLength; |
24 // RFC 4585: Feedback format. | 24 // RFC 4585: Feedback format. |
25 // | 25 // |
26 // Common packet format: | 26 // Common packet format: |
27 // | 27 // |
28 // 0 1 2 3 | 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 | 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 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 30 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
31 // |V=2|P| FMT | PT | length | | 31 // |V=2|P| FMT | PT | length | |
32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
33 // 0 | SSRC of packet sender | | 33 // 0 | SSRC of packet sender | |
34 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 34 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
35 // 4 | SSRC of media source | | 35 // 4 | SSRC of media source | |
36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
37 // : Feedback Control Information (FCI) : | 37 // : Feedback Control Information (FCI) : |
38 // : : | 38 // : : |
39 // | 39 // |
40 // Generic NACK (RFC 4585). | 40 // Generic NACK (RFC 4585). |
41 // | 41 // |
42 // FCI: | 42 // FCI: |
43 // 0 1 2 3 | 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 | 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 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 45 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
46 // | PID | BLP | | 46 // | PID | BLP | |
47 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 47 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
48 bool Nack::Parse(const RtcpCommonHeader& header, const uint8_t* payload) { | 48 Nack::Nack() {} |
49 RTC_DCHECK(header.packet_type == kPacketType); | 49 Nack::~Nack() {} |
50 RTC_DCHECK(header.count_or_format == kFeedbackMessageType); | |
51 | 50 |
52 if (header.payload_size_bytes < kCommonFeedbackLength + kNackItemLength) { | 51 bool Nack::Parse(const CommonHeader& packet) { |
53 LOG(LS_WARNING) << "Payload length " << header.payload_size_bytes | 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() |
54 << " is too small for a Nack."; | 57 << " is too small for a Nack."; |
55 return false; | 58 return false; |
56 } | 59 } |
57 size_t nack_items = | 60 size_t nack_items = |
58 (header.payload_size_bytes - kCommonFeedbackLength) / kNackItemLength; | 61 (packet.payload_size_bytes() - kCommonFeedbackLength) / kNackItemLength; |
59 | 62 |
60 ParseCommonFeedback(payload); | 63 ParseCommonFeedback(packet.payload()); |
61 const uint8_t* next_nack = payload + kCommonFeedbackLength; | 64 const uint8_t* next_nack = packet.payload() + kCommonFeedbackLength; |
62 | 65 |
63 packet_ids_.clear(); | 66 packet_ids_.clear(); |
64 packed_.resize(nack_items); | 67 packed_.resize(nack_items); |
65 for (size_t index = 0; index < nack_items; ++index) { | 68 for (size_t index = 0; index < nack_items; ++index) { |
66 packed_[index].first_pid = ByteReader<uint16_t>::ReadBigEndian(next_nack); | 69 packed_[index].first_pid = ByteReader<uint16_t>::ReadBigEndian(next_nack); |
67 packed_[index].bitmask = ByteReader<uint16_t>::ReadBigEndian(next_nack + 2); | 70 packed_[index].bitmask = ByteReader<uint16_t>::ReadBigEndian(next_nack + 2); |
68 next_nack += kNackItemLength; | 71 next_nack += kNackItemLength; |
69 } | 72 } |
70 Unpack(); | 73 Unpack(); |
71 | 74 |
72 return true; | 75 return true; |
73 } | 76 } |
74 | 77 |
75 bool Nack::Create(uint8_t* packet, | 78 bool Nack::Create(uint8_t* packet, |
76 size_t* index, | 79 size_t* index, |
77 size_t max_length, | 80 size_t max_length, |
78 RtcpPacket::PacketReadyCallback* callback) const { | 81 RtcpPacket::PacketReadyCallback* callback) const { |
79 RTC_DCHECK(!packed_.empty()); | 82 RTC_DCHECK(!packed_.empty()); |
80 // If nack list can't fit in packet, try to fragment. | 83 // If nack list can't fit in packet, try to fragment. |
81 size_t nack_index = 0; | 84 constexpr size_t kNackHeaderLength = kHeaderLength + kCommonFeedbackLength; |
82 const size_t kCommonFbFmtLength = kHeaderLength + kCommonFeedbackLength; | 85 for (size_t nack_index = 0; nack_index < packed_.size();) { |
83 do { | |
84 size_t bytes_left_in_buffer = max_length - *index; | 86 size_t bytes_left_in_buffer = max_length - *index; |
85 if (bytes_left_in_buffer < kCommonFbFmtLength + kNackItemLength) { | 87 if (bytes_left_in_buffer < kNackHeaderLength + kNackItemLength) { |
86 if (!OnBufferFull(packet, index, callback)) | 88 if (!OnBufferFull(packet, index, callback)) |
87 return false; | 89 return false; |
88 continue; | 90 continue; |
89 } | 91 } |
90 size_t num_nack_fields = | 92 size_t num_nack_fields = |
91 std::min((bytes_left_in_buffer - kCommonFbFmtLength) / kNackItemLength, | 93 std::min((bytes_left_in_buffer - kNackHeaderLength) / kNackItemLength, |
92 packed_.size() - nack_index); | 94 packed_.size() - nack_index); |
93 | 95 |
94 size_t size_bytes = | 96 size_t payload_size_bytes = |
95 (num_nack_fields * kNackItemLength) + kCommonFbFmtLength; | 97 kCommonFeedbackLength + (num_nack_fields * kNackItemLength); |
96 size_t header_length = ((size_bytes + 3) / 4) - 1; // As 32bit words - 1 | 98 size_t payload_size_32bits = |
97 CreateHeader(kFeedbackMessageType, kPacketType, header_length, packet, | 99 rtc::CheckedDivExact<size_t>(payload_size_bytes, 4); |
| 100 CreateHeader(kFeedbackMessageType, kPacketType, payload_size_32bits, packet, |
98 index); | 101 index); |
| 102 |
99 CreateCommonFeedback(packet + *index); | 103 CreateCommonFeedback(packet + *index); |
100 *index += kCommonFeedbackLength; | 104 *index += kCommonFeedbackLength; |
101 size_t end_index = nack_index + num_nack_fields; | 105 |
102 for (; nack_index < end_index; ++nack_index) { | 106 size_t nack_end_index = nack_index + num_nack_fields; |
103 const auto& item = packed_[nack_index]; | 107 for (; nack_index < nack_end_index; ++nack_index) { |
| 108 const PackedNack& item = packed_[nack_index]; |
104 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 0, item.first_pid); | 109 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 0, item.first_pid); |
105 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 2, item.bitmask); | 110 ByteWriter<uint16_t>::WriteBigEndian(packet + *index + 2, item.bitmask); |
106 *index += kNackItemLength; | 111 *index += kNackItemLength; |
107 } | 112 } |
108 RTC_DCHECK_LE(*index, max_length); | 113 RTC_DCHECK_LE(*index, max_length); |
109 } while (nack_index < packed_.size()); | 114 } |
110 | 115 |
111 return true; | 116 return true; |
112 } | 117 } |
113 | 118 |
114 size_t Nack::BlockLength() const { | 119 size_t Nack::BlockLength() const { |
115 return (packed_.size() * kNackItemLength) + kCommonFeedbackLength + | 120 return kHeaderLength + kCommonFeedbackLength + |
116 kHeaderLength; | 121 packed_.size() * kNackItemLength; |
117 } | 122 } |
118 | 123 |
119 void Nack::WithList(const uint16_t* nack_list, size_t length) { | 124 void Nack::WithList(const uint16_t* nack_list, size_t length) { |
120 RTC_DCHECK(nack_list); | 125 RTC_DCHECK(nack_list); |
121 RTC_DCHECK(packet_ids_.empty()); | 126 RTC_DCHECK(packet_ids_.empty()); |
122 RTC_DCHECK(packed_.empty()); | 127 RTC_DCHECK(packed_.empty()); |
123 packet_ids_.assign(nack_list, nack_list + length); | 128 packet_ids_.assign(nack_list, nack_list + length); |
124 Pack(); | 129 Pack(); |
125 } | 130 } |
126 | 131 |
(...skipping 19 matching lines...) Expand all Loading... |
146 packed_.push_back(item); | 151 packed_.push_back(item); |
147 } | 152 } |
148 } | 153 } |
149 | 154 |
150 void Nack::Unpack() { | 155 void Nack::Unpack() { |
151 RTC_DCHECK(packet_ids_.empty()); | 156 RTC_DCHECK(packet_ids_.empty()); |
152 RTC_DCHECK(!packed_.empty()); | 157 RTC_DCHECK(!packed_.empty()); |
153 for (const PackedNack& item : packed_) { | 158 for (const PackedNack& item : packed_) { |
154 packet_ids_.push_back(item.first_pid); | 159 packet_ids_.push_back(item.first_pid); |
155 uint16_t pid = item.first_pid + 1; | 160 uint16_t pid = item.first_pid + 1; |
156 for (uint16_t bitmask = item.bitmask; bitmask != 0; bitmask >>= 1, ++pid) | 161 for (uint16_t bitmask = item.bitmask; bitmask != 0; bitmask >>= 1, ++pid) { |
157 if (bitmask & 1) | 162 if (bitmask & 1) |
158 packet_ids_.push_back(pid); | 163 packet_ids_.push_back(pid); |
| 164 } |
159 } | 165 } |
160 } | 166 } |
161 | 167 |
162 } // namespace rtcp | 168 } // namespace rtcp |
163 } // namespace webrtc | 169 } // namespace webrtc |
OLD | NEW |