2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
12 #include "webrtc/modules/rtp_rtcp/source/rtp_format_vp8_test_helper.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 RtpFormatVp8TestHelper::RtpFormatVp8TestHelper(const RTPVideoHeaderVP8* hdr)
21 : payload_data_(NULL),
29 sloppy_partitioning_(false),
32 RtpFormatVp8TestHelper::~RtpFormatVp8TestHelper() {
33 delete fragmentation_;
34 delete [] payload_data_;
38 bool RtpFormatVp8TestHelper::Init(const int* partition_sizes,
40 if (inited_) return false;
41 fragmentation_ = new RTPFragmentationHeader;
42 fragmentation_->VerifyAndAllocateFragmentationHeader(num_partitions);
44 // Calculate sum payload size.
45 for (int p = 0; p < num_partitions; ++p) {
46 payload_size_ += partition_sizes[p];
48 buffer_size_ = payload_size_ + 6; // Add space for payload descriptor.
49 payload_data_ = new uint8_t[payload_size_];
50 buffer_ = new uint8_t[buffer_size_];
52 // Loop through the partitions again.
53 for (int p = 0; p < num_partitions; ++p) {
54 fragmentation_->fragmentationLength[p] = partition_sizes[p];
55 fragmentation_->fragmentationOffset[p] = j;
56 for (int i = 0; i < partition_sizes[p]; ++i) {
57 assert(j < payload_size_);
58 payload_data_[j++] = p; // Set the payload value to the partition index.
61 data_ptr_ = payload_data_;
66 void RtpFormatVp8TestHelper::GetAllPacketsAndCheck(
67 RtpPacketizerVp8* packetizer,
68 const int* expected_sizes,
69 const int* expected_part,
70 const bool* expected_frag_start,
71 int expected_num_packets) {
73 size_t send_bytes = 0;
75 for (int i = 0; i < expected_num_packets; ++i) {
76 std::ostringstream ss;
77 ss << "Checking packet " << i;
78 SCOPED_TRACE(ss.str());
79 EXPECT_TRUE(packetizer->NextPacket(buffer_, &send_bytes, &last));
80 CheckPacket(send_bytes, expected_sizes[i], last,
81 expected_frag_start[i]);
89 // |X|R|N|S|PartID | (REQUIRED)
91 // X: |I|L|T|K| RSV | (OPTIONAL)
93 // I: | PictureID | (OPTIONAL)
95 // L: | TL0PICIDX | (OPTIONAL)
97 // T/K: | TID | KEYIDX | (OPTIONAL)
100 // First octet tests.
101 #define EXPECT_BIT_EQ(x, n, a) EXPECT_EQ((((x) >> (n)) & 0x1), a)
103 #define EXPECT_RSV_ZERO(x) EXPECT_EQ(((x) & 0xE0), 0)
105 #define EXPECT_BIT_X_EQ(x, a) EXPECT_BIT_EQ(x, 7, a)
107 #define EXPECT_BIT_N_EQ(x, a) EXPECT_BIT_EQ(x, 5, a)
109 #define EXPECT_BIT_S_EQ(x, a) EXPECT_BIT_EQ(x, 4, a)
111 #define EXPECT_PART_ID_EQ(x, a) EXPECT_EQ(((x) & 0x0F), a)
113 // Extension fields tests
114 #define EXPECT_BIT_I_EQ(x, a) EXPECT_BIT_EQ(x, 7, a)
116 #define EXPECT_BIT_L_EQ(x, a) EXPECT_BIT_EQ(x, 6, a)
118 #define EXPECT_BIT_T_EQ(x, a) EXPECT_BIT_EQ(x, 5, a)
120 #define EXPECT_BIT_K_EQ(x, a) EXPECT_BIT_EQ(x, 4, a)
122 #define EXPECT_TID_EQ(x, a) EXPECT_EQ((((x) & 0xC0) >> 6), a)
124 #define EXPECT_BIT_Y_EQ(x, a) EXPECT_BIT_EQ(x, 5, a)
126 #define EXPECT_KEYIDX_EQ(x, a) EXPECT_EQ(((x) & 0x1F), a)
128 void RtpFormatVp8TestHelper::CheckHeader(bool frag_start) {
130 EXPECT_BIT_EQ(buffer_[0], 6, 0); // Check reserved bit.
132 if (hdr_info_->pictureId != kNoPictureId ||
133 hdr_info_->temporalIdx != kNoTemporalIdx ||
134 hdr_info_->tl0PicIdx != kNoTl0PicIdx ||
135 hdr_info_->keyIdx != kNoKeyIdx) {
136 EXPECT_BIT_X_EQ(buffer_[0], 1);
142 EXPECT_BIT_X_EQ(buffer_[0], 0);
145 EXPECT_BIT_N_EQ(buffer_[0], hdr_info_->nonReference ? 1 : 0);
146 EXPECT_BIT_S_EQ(buffer_[0], frag_start ? 1 : 0);
148 // Check partition index.
149 if (!sloppy_partitioning_) {
150 // The test payload data is constructed such that the payload value is the
151 // same as the partition index.
152 EXPECT_EQ(buffer_[0] & 0x0F, buffer_[payload_start_]);
154 // Partition should be set to 0.
155 EXPECT_EQ(buffer_[0] & 0x0F, 0);
159 // Verify that the I bit and the PictureID field are both set in accordance
160 // with the information in hdr_info_->pictureId.
161 void RtpFormatVp8TestHelper::CheckPictureID() {
162 if (hdr_info_->pictureId != kNoPictureId) {
163 EXPECT_BIT_I_EQ(buffer_[1], 1);
164 if (hdr_info_->pictureId > 0x7F) {
165 EXPECT_BIT_EQ(buffer_[payload_start_], 7, 1);
166 EXPECT_EQ(buffer_[payload_start_] & 0x7F,
167 (hdr_info_->pictureId >> 8) & 0x7F);
168 EXPECT_EQ(buffer_[payload_start_ + 1],
169 hdr_info_->pictureId & 0xFF);
172 EXPECT_BIT_EQ(buffer_[payload_start_], 7, 0);
173 EXPECT_EQ(buffer_[payload_start_] & 0x7F,
174 (hdr_info_->pictureId) & 0x7F);
178 EXPECT_BIT_I_EQ(buffer_[1], 0);
182 // Verify that the L bit and the TL0PICIDX field are both set in accordance
183 // with the information in hdr_info_->tl0PicIdx.
184 void RtpFormatVp8TestHelper::CheckTl0PicIdx() {
185 if (hdr_info_->tl0PicIdx != kNoTl0PicIdx) {
186 EXPECT_BIT_L_EQ(buffer_[1], 1);
187 EXPECT_EQ(buffer_[payload_start_], hdr_info_->tl0PicIdx);
190 EXPECT_BIT_L_EQ(buffer_[1], 0);
194 // Verify that the T bit and the TL0PICIDX field, and the K bit and KEYIDX
195 // field are all set in accordance with the information in
196 // hdr_info_->temporalIdx and hdr_info_->keyIdx, respectively.
197 void RtpFormatVp8TestHelper::CheckTIDAndKeyIdx() {
198 if (hdr_info_->temporalIdx == kNoTemporalIdx &&
199 hdr_info_->keyIdx == kNoKeyIdx) {
200 EXPECT_BIT_T_EQ(buffer_[1], 0);
201 EXPECT_BIT_K_EQ(buffer_[1], 0);
204 if (hdr_info_->temporalIdx != kNoTemporalIdx) {
205 EXPECT_BIT_T_EQ(buffer_[1], 1);
206 EXPECT_TID_EQ(buffer_[payload_start_], hdr_info_->temporalIdx);
207 EXPECT_BIT_Y_EQ(buffer_[payload_start_], hdr_info_->layerSync ? 1 : 0);
209 EXPECT_BIT_T_EQ(buffer_[1], 0);
210 EXPECT_TID_EQ(buffer_[payload_start_], 0);
211 EXPECT_BIT_Y_EQ(buffer_[payload_start_], 0);
213 if (hdr_info_->keyIdx != kNoKeyIdx) {
214 EXPECT_BIT_K_EQ(buffer_[1], 1);
215 EXPECT_KEYIDX_EQ(buffer_[payload_start_], hdr_info_->keyIdx);
217 EXPECT_BIT_K_EQ(buffer_[1], 0);
218 EXPECT_KEYIDX_EQ(buffer_[payload_start_], 0);
223 // Verify that the payload (i.e., after the headers) of the packet stored in
224 // buffer_ is identical to the expected (as found in data_ptr_).
225 void RtpFormatVp8TestHelper::CheckPayload(int payload_end) {
226 for (int i = payload_start_; i < payload_end; ++i, ++data_ptr_)
227 EXPECT_EQ(buffer_[i], *data_ptr_);
230 // Verify that the input variable "last" agrees with the position of data_ptr_.
231 // If data_ptr_ has advanced payload_size_ bytes from the start (payload_data_)
232 // we are at the end and last should be true. Otherwise, it should be false.
233 void RtpFormatVp8TestHelper::CheckLast(bool last) const {
234 EXPECT_EQ(last, data_ptr_ == payload_data_ + payload_size_);
237 // Verify the contents of a packet. Check the length versus expected_bytes,
238 // the header, payload, and "last" flag.
239 void RtpFormatVp8TestHelper::CheckPacket(int send_bytes,
243 EXPECT_EQ(expect_bytes, send_bytes);
244 CheckHeader(frag_start);
245 CheckPayload(send_bytes);
251 } // namespace webrtc