OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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/rpsi.h" | 11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rpsi.h" |
12 | 12 |
13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 16 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h" |
16 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" | 17 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" |
17 | 18 |
18 using webrtc::RTCPUtility::RtcpCommonHeader; | |
19 using webrtc::RtpUtility::Word32Align; | 19 using webrtc::RtpUtility::Word32Align; |
20 | 20 |
21 namespace webrtc { | 21 namespace webrtc { |
22 namespace rtcp { | 22 namespace rtcp { |
| 23 constexpr uint8_t Rpsi::kFeedbackMessageType; |
23 // RFC 4585: Feedback format. | 24 // RFC 4585: Feedback format. |
24 // Reference picture selection indication (RPSI) (RFC 4585). | 25 // Reference picture selection indication (RPSI) (RFC 4585). |
25 // | 26 // |
26 // 0 1 2 3 | 27 // 0 1 2 3 |
27 // 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 | 28 // 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 |
28 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 29 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
29 // |V=2|P| RPSI=3 | PT=PSFB=206 | length | | 30 // |V=2|P| RPSI=3 | PT=PSFB=206 | length | |
30 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 31 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
31 // 0 | SSRC of packet sender | | 32 // 0 | SSRC of packet sender | |
32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 33 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
33 // 4 | SSRC of media source | | 34 // 4 | SSRC of media source | |
34 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 35 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
35 // 8 | Padding Bits | | 36 // 8 | Padding Bits | |
36 // 9 |0| Payload Type| | 37 // 9 |0| Payload Type| |
37 // 10 | Native RPSI bit string : | 38 // 10 | Native RPSI bit string : |
38 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 39 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
39 // : defined per codec ... | Padding (0) | | 40 // : defined per codec ... | Padding (0) | |
40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 41 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
41 namespace { | 42 namespace { |
42 const size_t kPaddingSizeOffset = 8; | 43 constexpr size_t kPaddingSizeOffset = 8; |
43 const size_t kPayloadTypeOffset = 9; | 44 constexpr size_t kPayloadTypeOffset = 9; |
44 const size_t kBitStringOffset = 10; | 45 constexpr size_t kBitStringOffset = 10; |
45 | 46 |
46 const size_t kPidBits = 7; | 47 constexpr size_t kPidBits = 7; |
47 // Calculates number of bytes required to store given picture id. | 48 // Calculates number of bytes required to store given picture id. |
48 uint8_t RequiredBytes(uint64_t picture_id) { | 49 uint8_t RequiredBytes(uint64_t picture_id) { |
49 uint8_t required_bytes = 0; | 50 uint8_t required_bytes = 0; |
50 uint64_t shifted_pid = picture_id; | 51 uint64_t shifted_pid = picture_id; |
51 do { | 52 do { |
52 ++required_bytes; | 53 ++required_bytes; |
53 shifted_pid >>= kPidBits; | 54 shifted_pid >>= kPidBits; |
54 } while (shifted_pid > 0); | 55 } while (shifted_pid > 0); |
55 | 56 |
56 return required_bytes; | 57 return required_bytes; |
57 } | 58 } |
58 } // namespace | 59 } // namespace |
59 | 60 |
60 Rpsi::Rpsi() | 61 Rpsi::Rpsi() |
61 : payload_type_(0), | 62 : payload_type_(0), |
62 picture_id_(0), | 63 picture_id_(0), |
63 block_length_(CalculateBlockLength(1)) {} | 64 block_length_(CalculateBlockLength(1)) {} |
64 | 65 |
65 bool Rpsi::Parse(const RTCPUtility::RtcpCommonHeader& header, | 66 bool Rpsi::Parse(const CommonHeader& packet) { |
66 const uint8_t* payload) { | 67 RTC_DCHECK_EQ(packet.type(), kPacketType); |
67 RTC_CHECK(header.packet_type == kPacketType); | 68 RTC_DCHECK_EQ(packet.fmt(), kFeedbackMessageType); |
68 RTC_CHECK(header.count_or_format == kFeedbackMessageType); | |
69 | 69 |
70 if (header.payload_size_bytes < kCommonFeedbackLength + 4) { | 70 if (packet.payload_size_bytes() < kCommonFeedbackLength + 4) { |
71 LOG(LS_WARNING) << "Packet is too small to be a valid RPSI packet."; | 71 LOG(LS_WARNING) << "Packet is too small to be a valid RPSI packet."; |
72 return false; | 72 return false; |
73 } | 73 } |
74 | 74 |
75 ParseCommonFeedback(payload); | 75 ParseCommonFeedback(packet.payload()); |
76 | 76 |
77 uint8_t padding_bits = payload[kPaddingSizeOffset]; | 77 uint8_t padding_bits = packet.payload()[kPaddingSizeOffset]; |
78 if (padding_bits % 8 != 0) { | 78 if (padding_bits % 8 != 0) { |
79 LOG(LS_WARNING) << "Unknown rpsi packet with fractional number of bytes."; | 79 LOG(LS_WARNING) << "Unknown rpsi packet with fractional number of bytes."; |
80 return false; | 80 return false; |
81 } | 81 } |
82 size_t padding_bytes = padding_bits / 8; | 82 size_t padding_bytes = padding_bits / 8; |
83 if (padding_bytes + kBitStringOffset >= header.payload_size_bytes) { | 83 if (padding_bytes + kBitStringOffset >= packet.payload_size_bytes()) { |
84 LOG(LS_WARNING) << "Too many padding bytes in a RPSI packet."; | 84 LOG(LS_WARNING) << "Too many padding bytes in a RPSI packet."; |
85 return false; | 85 return false; |
86 } | 86 } |
87 size_t padding_offset = header.payload_size_bytes - padding_bytes; | 87 size_t padding_offset = packet.payload_size_bytes() - padding_bytes; |
88 payload_type_ = payload[kPayloadTypeOffset] & 0x7f; | 88 payload_type_ = packet.payload()[kPayloadTypeOffset] & 0x7f; |
89 picture_id_ = 0; | 89 picture_id_ = 0; |
90 for (size_t pos = kBitStringOffset; pos < padding_offset; ++pos) { | 90 for (size_t pos = kBitStringOffset; pos < padding_offset; ++pos) { |
91 picture_id_ <<= kPidBits; | 91 picture_id_ <<= kPidBits; |
92 picture_id_ |= (payload[pos] & 0x7f); | 92 picture_id_ |= (packet.payload()[pos] & 0x7f); |
93 } | 93 } |
94 // Required bytes might become less than came in the packet. | 94 // Required bytes might become less than came in the packet. |
95 block_length_ = CalculateBlockLength(RequiredBytes(picture_id_)); | 95 block_length_ = CalculateBlockLength(RequiredBytes(picture_id_)); |
96 return true; | 96 return true; |
97 } | 97 } |
98 | 98 |
99 bool Rpsi::Create(uint8_t* packet, | 99 bool Rpsi::Create(uint8_t* packet, |
100 size_t* index, | 100 size_t* index, |
101 size_t max_length, | 101 size_t max_length, |
102 RtcpPacket::PacketReadyCallback* callback) const { | 102 RtcpPacket::PacketReadyCallback* callback) const { |
(...skipping 13 matching lines...) Expand all Loading... |
116 Word32Align(2 + bitstring_size_bytes) - (2 + bitstring_size_bytes); | 116 Word32Align(2 + bitstring_size_bytes) - (2 + bitstring_size_bytes); |
117 packet[(*index)++] = padding_bytes * 8; | 117 packet[(*index)++] = padding_bytes * 8; |
118 packet[(*index)++] = payload_type_; | 118 packet[(*index)++] = payload_type_; |
119 | 119 |
120 // Convert picture id to native bit string (defined by the video codec). | 120 // Convert picture id to native bit string (defined by the video codec). |
121 for (size_t i = bitstring_size_bytes - 1; i > 0; --i) { | 121 for (size_t i = bitstring_size_bytes - 1; i > 0; --i) { |
122 packet[(*index)++] = | 122 packet[(*index)++] = |
123 0x80 | static_cast<uint8_t>(picture_id_ >> (i * kPidBits)); | 123 0x80 | static_cast<uint8_t>(picture_id_ >> (i * kPidBits)); |
124 } | 124 } |
125 packet[(*index)++] = static_cast<uint8_t>(picture_id_ & 0x7f); | 125 packet[(*index)++] = static_cast<uint8_t>(picture_id_ & 0x7f); |
126 const uint8_t kPadding = 0; | 126 constexpr uint8_t kPadding = 0; |
127 for (size_t i = 0; i < padding_bytes; ++i) | 127 for (size_t i = 0; i < padding_bytes; ++i) |
128 packet[(*index)++] = kPadding; | 128 packet[(*index)++] = kPadding; |
129 RTC_CHECK_EQ(*index, index_end); | 129 RTC_CHECK_EQ(*index, index_end); |
130 return true; | 130 return true; |
131 } | 131 } |
132 | 132 |
133 void Rpsi::WithPayloadType(uint8_t payload) { | 133 void Rpsi::WithPayloadType(uint8_t payload) { |
134 RTC_DCHECK_LE(payload, 0x7f); | 134 RTC_DCHECK_LE(payload, 0x7f); |
135 payload_type_ = payload; | 135 payload_type_ = payload; |
136 } | 136 } |
137 | 137 |
138 void Rpsi::WithPictureId(uint64_t picture_id) { | 138 void Rpsi::WithPictureId(uint64_t picture_id) { |
139 picture_id_ = picture_id; | 139 picture_id_ = picture_id; |
140 block_length_ = CalculateBlockLength(RequiredBytes(picture_id_)); | 140 block_length_ = CalculateBlockLength(RequiredBytes(picture_id_)); |
141 } | 141 } |
142 | 142 |
143 size_t Rpsi::CalculateBlockLength(uint8_t bitstring_size_bytes) { | 143 size_t Rpsi::CalculateBlockLength(uint8_t bitstring_size_bytes) { |
144 return RtcpPacket::kHeaderLength + Psfb::kCommonFeedbackLength + | 144 return RtcpPacket::kHeaderLength + Psfb::kCommonFeedbackLength + |
145 Word32Align(2 + bitstring_size_bytes); | 145 Word32Align(2 + bitstring_size_bytes); |
146 } | 146 } |
147 } // namespace rtcp | 147 } // namespace rtcp |
148 } // namespace webrtc | 148 } // namespace webrtc |
OLD | NEW |