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" | |
14 #include "webrtc/base/logging.h" | |
13 #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/rtp_utility.h" | |
14 | 17 |
15 using webrtc::RTCPUtility::PT_PSFB; | 18 using webrtc::RTCPUtility::RtcpCommonHeader; |
16 using webrtc::RTCPUtility::RTCPPacketPSFBRPSI; | 19 using webrtc::RtpUtility::Word32Align; |
17 | 20 |
18 namespace webrtc { | 21 namespace webrtc { |
19 namespace rtcp { | 22 namespace rtcp { |
20 namespace { | 23 // RFC 4585: Feedback format. |
21 void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) { | |
22 buffer[(*offset)++] = value; | |
23 } | |
24 | |
25 void AssignUWord32(uint8_t* buffer, size_t* offset, uint32_t value) { | |
26 ByteWriter<uint32_t>::WriteBigEndian(buffer + *offset, value); | |
27 *offset += 4; | |
28 } | |
29 | |
30 // Reference picture selection indication (RPSI) (RFC 4585). | 24 // Reference picture selection indication (RPSI) (RFC 4585). |
31 // | 25 // |
32 // FCI: | 26 // 0 1 2 3 |
33 // | 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 |
34 // 0 1 2 3 | 28 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
35 // 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 // |V=2|P| RPSI=3 | PT=PSFB=206 | length | |
36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 30 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
37 // | PB |0| Payload Type| Native RPSI bit string | | 31 // 0 | SSRC of packet sender | |
38 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
39 // | defined per codec ... | Padding (0) | | 33 // 4 | SSRC of media source | |
40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 34 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
41 void CreateRpsi(const RTCPPacketPSFBRPSI& rpsi, | 35 // 8 | Padding Bits | |
42 uint8_t padding_bytes, | 36 // 9 |0| Payload Type| |
43 uint8_t* buffer, | 37 // 10 | Native RPSI bit string : |
44 size_t* pos) { | 38 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
45 // Native bit string should be a multiple of 8 bits. | 39 // : defined per codec ... | Padding (0) | |
46 assert(rpsi.NumberOfValidBits % 8 == 0); | 40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
47 AssignUWord32(buffer, pos, rpsi.SenderSSRC); | 41 namespace { |
48 AssignUWord32(buffer, pos, rpsi.MediaSSRC); | 42 const size_t kPaddingSizeOffset = 8; |
49 AssignUWord8(buffer, pos, padding_bytes * 8); | 43 const size_t kPayloadTypeOffset = 9; |
50 AssignUWord8(buffer, pos, rpsi.PayloadType); | 44 const size_t kBitStringOffset = 10; |
51 memcpy(buffer + *pos, rpsi.NativeBitString, rpsi.NumberOfValidBits / 8); | 45 |
52 *pos += rpsi.NumberOfValidBits / 8; | 46 const size_t kPidBits = 7; |
53 memset(buffer + *pos, 0, padding_bytes); | 47 // Calculates number of bytes required to store given picture id. |
54 *pos += padding_bytes; | 48 uint8_t RequiredBytes(uint64_t picture_id) { |
49 uint8_t required_bytes = 0; | |
50 uint64_t shifted_pid = picture_id; | |
51 do { | |
52 ++required_bytes; | |
53 shifted_pid >>= kPidBits; | |
54 } while (shifted_pid > 0); | |
55 | |
56 return required_bytes; | |
55 } | 57 } |
56 } // namespace | 58 } // namespace |
57 | 59 |
60 Rpsi::Rpsi() | |
61 : payload_type_(0), | |
62 picture_id_(0), | |
63 block_length_(CalculateBlockLength(1)) {} | |
64 | |
65 bool Rpsi::Parse(const RTCPUtility::RtcpCommonHeader& header, | |
66 const uint8_t* payload) { | |
67 RTC_CHECK(header.packet_type == kPacketType); | |
68 RTC_CHECK(header.count_or_format == kFeedbackMessageType); | |
69 | |
70 if (header.payload_size_bytes < kCommonFeedbackLength + 4) { | |
71 LOG(LS_WARNING) << "Packet is too small to be a valid RPSI packet."; | |
72 return false; | |
73 } | |
74 | |
75 ParseCommonFeedback(payload); | |
76 | |
77 uint8_t padding_bits = payload[kPaddingSizeOffset]; | |
78 if (padding_bits % 8 != 0) { | |
79 LOG(LS_WARNING) << "Unknown rpsi packet with fractinal number of bytes."; | |
åsapersson
2016/01/29 15:46:01
s/fractinal/fractional
danilchap
2016/01/29 16:53:24
Done.
| |
80 return false; | |
81 } | |
82 size_t padding_bytes = padding_bits / 8; | |
83 if (padding_bytes + kBitStringOffset >= header.payload_size_bytes) { | |
84 LOG(LS_WARNING) << "Too many padding bytes in a RPSI packet."; | |
85 return false; | |
86 } | |
87 size_t padding_offset = header.payload_size_bytes - padding_bytes; | |
88 payload_type_ = payload[kPayloadTypeOffset] & 0x7f; | |
89 picture_id_ = 0; | |
90 for (size_t pos = kBitStringOffset; pos < padding_offset; ++pos) { | |
91 picture_id_ <<= kPidBits; | |
92 picture_id_ |= (payload[pos] & 0x7f); | |
93 } | |
94 // Required bytes might become less than came in the packet. | |
95 block_length_ = CalculateBlockLength(RequiredBytes(picture_id_)); | |
96 return true; | |
97 } | |
98 | |
58 bool Rpsi::Create(uint8_t* packet, | 99 bool Rpsi::Create(uint8_t* packet, |
59 size_t* index, | 100 size_t* index, |
60 size_t max_length, | 101 size_t max_length, |
61 RtcpPacket::PacketReadyCallback* callback) const { | 102 RtcpPacket::PacketReadyCallback* callback) const { |
62 assert(rpsi_.NumberOfValidBits > 0); | |
63 while (*index + BlockLength() > max_length) { | 103 while (*index + BlockLength() > max_length) { |
64 if (!OnBufferFull(packet, index, callback)) | 104 if (!OnBufferFull(packet, index, callback)) |
65 return false; | 105 return false; |
66 } | 106 } |
67 const uint8_t kFmt = 3; | 107 size_t index_end = *index + BlockLength(); |
68 CreateHeader(kFmt, PT_PSFB, HeaderLength(), packet, index); | 108 |
69 CreateRpsi(rpsi_, padding_bytes_, packet, index); | 109 CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet, |
110 index); | |
111 CreateCommonFeedback(packet + *index); | |
112 *index += kCommonFeedbackLength; | |
113 | |
114 size_t bitstring_size_bytes = RequiredBytes(picture_id_); | |
115 size_t padding_bytes = | |
116 Word32Align(2 + bitstring_size_bytes) - (2 + bitstring_size_bytes); | |
117 packet[(*index)++] = padding_bytes * 8; | |
118 packet[(*index)++] = payload_type_; | |
119 | |
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) { | |
122 packet[(*index)++] = | |
123 0x80 | static_cast<uint8_t>(picture_id_ >> (i * kPidBits)); | |
124 } | |
125 packet[(*index)++] = static_cast<uint8_t>(picture_id_ & 0x7f); | |
126 const uint8_t kPadding = 0; | |
127 for (size_t i = 0; i < padding_bytes; ++i) | |
128 packet[(*index)++] = kPadding; | |
129 RTC_CHECK_EQ(*index, index_end); | |
70 return true; | 130 return true; |
71 } | 131 } |
72 | 132 |
73 void Rpsi::WithPictureId(uint64_t picture_id) { | 133 void Rpsi::WithPayloadType(uint8_t payload) { |
74 const uint32_t kPidBits = 7; | 134 RTC_CHECK_LE(payload, 0x7f); |
åsapersson
2016/01/29 15:46:01
maybe DCHECK?
danilchap
2016/01/29 16:53:25
Done.
| |
75 const uint64_t k7MsbZeroMask = 0x1ffffffffffffffULL; | 135 payload_type_ = payload; |
76 uint8_t required_bytes = 0; | |
77 uint64_t shifted_pid = picture_id; | |
78 do { | |
79 ++required_bytes; | |
80 shifted_pid = (shifted_pid >> kPidBits) & k7MsbZeroMask; | |
81 } while (shifted_pid > 0); | |
82 | |
83 // Convert picture id to native bit string (natively defined by the video | |
84 // codec). | |
85 int pos = 0; | |
86 for (int i = required_bytes - 1; i > 0; i--) { | |
87 rpsi_.NativeBitString[pos++] = | |
88 0x80 | static_cast<uint8_t>(picture_id >> (i * kPidBits)); | |
89 } | |
90 rpsi_.NativeBitString[pos++] = static_cast<uint8_t>(picture_id & 0x7f); | |
91 rpsi_.NumberOfValidBits = pos * 8; | |
92 | |
93 // Calculate padding bytes (to reach next 32-bit boundary, 1, 2 or 3 bytes). | |
94 padding_bytes_ = 4 - ((2 + required_bytes) % 4); | |
95 if (padding_bytes_ == 4) { | |
96 padding_bytes_ = 0; | |
97 } | |
98 } | 136 } |
99 | 137 |
138 void Rpsi::WithPictureId(uint64_t picture_id) { | |
139 picture_id_ = picture_id; | |
140 block_length_ = CalculateBlockLength(RequiredBytes(picture_id_)); | |
141 } | |
100 | 142 |
143 size_t Rpsi::CalculateBlockLength(uint8_t bitstring_size_bytes) { | |
144 return RtcpPacket::kHeaderLength + Psfb::kCommonFeedbackLength + | |
145 Word32Align(2 + bitstring_size_bytes); | |
146 } | |
101 } // namespace rtcp | 147 } // namespace rtcp |
102 } // namespace webrtc | 148 } // namespace webrtc |
OLD | NEW |