Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / rtp_rtcp / source / rtp_format_vp8_unittest.cc
1 /*
2  *  Copyright (c) 2012 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
11 /*
12  * This file includes unit tests for the VP8 packetizer.
13  */
14
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "webrtc/modules/rtp_rtcp/source/rtp_format_vp8.h"
17 #include "webrtc/modules/rtp_rtcp/source/rtp_format_vp8_test_helper.h"
18 #include "webrtc/system_wrappers/interface/compile_assert.h"
19 #include "webrtc/typedefs.h"
20
21 #define CHECK_ARRAY_SIZE(expected_size, array)                       \
22   COMPILE_ASSERT(expected_size == sizeof(array) / sizeof(array[0]),  \
23                  check_array_size);
24
25 namespace webrtc {
26
27 class RtpPacketizerVp8Test : public ::testing::Test {
28  protected:
29   RtpPacketizerVp8Test() : helper_(NULL) {}
30   virtual void TearDown() { delete helper_; }
31   bool Init(const int* partition_sizes, int num_partitions) {
32     hdr_info_.pictureId = kNoPictureId;
33     hdr_info_.nonReference = false;
34     hdr_info_.temporalIdx = kNoTemporalIdx;
35     hdr_info_.layerSync = false;
36     hdr_info_.tl0PicIdx = kNoTl0PicIdx;
37     hdr_info_.keyIdx = kNoKeyIdx;
38     if (helper_ != NULL) return false;
39     helper_ = new test::RtpFormatVp8TestHelper(&hdr_info_);
40     return helper_->Init(partition_sizes, num_partitions);
41   }
42
43   RTPVideoHeaderVP8 hdr_info_;
44   test::RtpFormatVp8TestHelper* helper_;
45 };
46
47 TEST_F(RtpPacketizerVp8Test, TestStrictMode) {
48   const int kSizeVector[] = {10, 8, 27};
49   const int kNumPartitions = sizeof(kSizeVector) / sizeof(kSizeVector[0]);
50   ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
51
52   hdr_info_.pictureId = 200;  // > 0x7F should produce 2-byte PictureID.
53   const int kMaxSize = 13;
54   RtpPacketizerVp8 packetizer(hdr_info_, kMaxSize, kStrict);
55   packetizer.SetPayloadData(helper_->payload_data(),
56                             helper_->payload_size(),
57                             helper_->fragmentation());
58
59   // The expected sizes are obtained by running a verified good implementation.
60   const int kExpectedSizes[] = {9, 9, 12, 11, 11, 11, 10};
61   const int kExpectedPart[] = {0, 0, 1, 2, 2, 2, 2};
62   const bool kExpectedFragStart[] =
63       {true, false, true, true, false, false, false};
64   const int kExpectedNum = sizeof(kExpectedSizes) / sizeof(kExpectedSizes[0]);
65   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
66   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
67
68   helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
69                                  kExpectedFragStart, kExpectedNum);
70 }
71
72 TEST_F(RtpPacketizerVp8Test, TestAggregateMode) {
73   const int kSizeVector[] = {60, 10, 10};
74   const int kNumPartitions = sizeof(kSizeVector) / sizeof(kSizeVector[0]);
75   ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
76
77   hdr_info_.pictureId = 20;  // <= 0x7F should produce 1-byte PictureID.
78   const int kMaxSize = 25;
79   RtpPacketizerVp8 packetizer(hdr_info_, kMaxSize, kAggregate);
80   packetizer.SetPayloadData(helper_->payload_data(),
81                             helper_->payload_size(),
82                             helper_->fragmentation());
83
84   // The expected sizes are obtained by running a verified good implementation.
85   const int kExpectedSizes[] = {23, 23, 23, 23};
86   const int kExpectedPart[] = {0, 0, 0, 1};
87   const bool kExpectedFragStart[] = {true, false, false, true};
88   const int kExpectedNum = sizeof(kExpectedSizes) / sizeof(kExpectedSizes[0]);
89   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
90   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
91
92   helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
93                                  kExpectedFragStart, kExpectedNum);
94 }
95
96 TEST_F(RtpPacketizerVp8Test, TestAggregateModeManyPartitions1) {
97   const int kSizeVector[] = {1600, 200, 200, 200, 200, 200, 200, 200, 200};
98   const int kNumPartitions = sizeof(kSizeVector) / sizeof(kSizeVector[0]);
99   ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
100
101   hdr_info_.pictureId = 20;  // <= 0x7F should produce 1-byte PictureID.
102   const int kMaxSize = 1500;
103   RtpPacketizerVp8 packetizer(hdr_info_, kMaxSize, kAggregate);
104   packetizer.SetPayloadData(helper_->payload_data(),
105                             helper_->payload_size(),
106                             helper_->fragmentation());
107
108   // The expected sizes are obtained by running a verified good implementation.
109   const int kExpectedSizes[] = {803, 803, 803, 803};
110   const int kExpectedPart[] = {0, 0, 1, 5};
111   const bool kExpectedFragStart[] = {true, false, true, true};
112   const int kExpectedNum = sizeof(kExpectedSizes) / sizeof(kExpectedSizes[0]);
113   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
114   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
115
116   helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
117                                  kExpectedFragStart, kExpectedNum);
118 }
119
120 TEST_F(RtpPacketizerVp8Test, TestAggregateModeManyPartitions2) {
121   const int kSizeVector[] = {1599, 200, 200, 200, 1600, 200, 200, 200, 200};
122   const int kNumPartitions = sizeof(kSizeVector) / sizeof(kSizeVector[0]);
123   ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
124
125   hdr_info_.pictureId = 20;  // <= 0x7F should produce 1-byte PictureID.
126   const int kMaxSize = 1500;
127   RtpPacketizerVp8 packetizer(hdr_info_, kMaxSize, kAggregate);
128   packetizer.SetPayloadData(helper_->payload_data(),
129                             helper_->payload_size(),
130                             helper_->fragmentation());
131
132   // The expected sizes are obtained by running a verified good implementation.
133   const int kExpectedSizes[] = {803, 802, 603, 803, 803, 803};
134   const int kExpectedPart[] = {0, 0, 1, 4, 4, 5};
135   const bool kExpectedFragStart[] = {true, false, true, true, false, true};
136   const int kExpectedNum = sizeof(kExpectedSizes) / sizeof(kExpectedSizes[0]);
137   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
138   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
139
140   helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
141                                  kExpectedFragStart, kExpectedNum);
142 }
143
144 TEST_F(RtpPacketizerVp8Test, TestAggregateModeTwoLargePartitions) {
145   const int kSizeVector[] = {1654, 2268};
146   const int kNumPartitions = sizeof(kSizeVector) / sizeof(kSizeVector[0]);
147   ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
148
149   hdr_info_.pictureId = 20;  // <= 0x7F should produce 1-byte PictureID.
150   const int kMaxSize = 1460;
151   RtpPacketizerVp8 packetizer(hdr_info_, kMaxSize, kAggregate);
152   packetizer.SetPayloadData(helper_->payload_data(),
153                             helper_->payload_size(),
154                             helper_->fragmentation());
155
156   // The expected sizes are obtained by running a verified good implementation.
157   const int kExpectedSizes[] = {830, 830, 1137, 1137};
158   const int kExpectedPart[] = {0, 0, 1, 1};
159   const bool kExpectedFragStart[] = {true, false, true, false};
160   const int kExpectedNum = sizeof(kExpectedSizes) / sizeof(kExpectedSizes[0]);
161   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
162   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
163
164   helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
165                                  kExpectedFragStart, kExpectedNum);
166 }
167
168 // Verify that EqualSize mode is forced if fragmentation info is missing.
169 TEST_F(RtpPacketizerVp8Test, TestEqualSizeModeFallback) {
170   const int kSizeVector[] = {10, 10, 10};
171   const int kNumPartitions = sizeof(kSizeVector) / sizeof(kSizeVector[0]);
172   ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
173
174   hdr_info_.pictureId = 200;  // > 0x7F should produce 2-byte PictureID
175   const int kMaxSize = 12;  // Small enough to produce 4 packets.
176   RtpPacketizerVp8 packetizer(hdr_info_, kMaxSize);
177   packetizer.SetPayloadData(
178       helper_->payload_data(), helper_->payload_size(), NULL);
179
180   // Expecting three full packets, and one with the remainder.
181   const int kExpectedSizes[] = {12, 11, 12, 11};
182   const int kExpectedPart[] = {0, 0, 0, 0};  // Always 0 for equal size mode.
183   // Frag start only true for first packet in equal size mode.
184   const bool kExpectedFragStart[] = {true, false, false, false};
185   const int kExpectedNum = sizeof(kExpectedSizes) / sizeof(kExpectedSizes[0]);
186   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
187   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
188
189   helper_->set_sloppy_partitioning(true);
190   helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
191                                  kExpectedFragStart, kExpectedNum);
192 }
193
194 // Verify that non-reference bit is set. EqualSize mode fallback is expected.
195 TEST_F(RtpPacketizerVp8Test, TestNonReferenceBit) {
196   const int kSizeVector[] = {10, 10, 10};
197   const int kNumPartitions = sizeof(kSizeVector) / sizeof(kSizeVector[0]);
198   ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
199
200   hdr_info_.nonReference = true;
201   const int kMaxSize = 25;  // Small enough to produce two packets.
202   RtpPacketizerVp8 packetizer(hdr_info_, kMaxSize);
203   packetizer.SetPayloadData(
204       helper_->payload_data(), helper_->payload_size(), NULL);
205
206   // EqualSize mode => First packet full; other not.
207   const int kExpectedSizes[] = {16, 16};
208   const int kExpectedPart[] = {0, 0};  // Always 0 for equal size mode.
209   // Frag start only true for first packet in equal size mode.
210   const bool kExpectedFragStart[] = {true, false};
211   const int kExpectedNum = sizeof(kExpectedSizes) / sizeof(kExpectedSizes[0]);
212   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
213   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
214
215   helper_->set_sloppy_partitioning(true);
216   helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
217                                  kExpectedFragStart, kExpectedNum);
218 }
219
220 // Verify Tl0PicIdx and TID fields, and layerSync bit.
221 TEST_F(RtpPacketizerVp8Test, TestTl0PicIdxAndTID) {
222   const int kSizeVector[] = {10, 10, 10};
223   const int kNumPartitions = sizeof(kSizeVector) / sizeof(kSizeVector[0]);
224   ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
225
226   hdr_info_.tl0PicIdx = 117;
227   hdr_info_.temporalIdx = 2;
228   hdr_info_.layerSync = true;
229   // kMaxSize is only limited by allocated buffer size.
230   const int kMaxSize = helper_->buffer_size();
231   RtpPacketizerVp8 packetizer(hdr_info_, kMaxSize, kAggregate);
232   packetizer.SetPayloadData(helper_->payload_data(),
233                             helper_->payload_size(),
234                             helper_->fragmentation());
235
236   // Expect one single packet of payload_size() + 4 bytes header.
237   const int kExpectedSizes[1] = {helper_->payload_size() + 4};
238   const int kExpectedPart[1] = {0};  // Packet starts with partition 0.
239   const bool kExpectedFragStart[1] = {true};
240   const int kExpectedNum = sizeof(kExpectedSizes) / sizeof(kExpectedSizes[0]);
241   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
242   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
243
244   helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
245                                  kExpectedFragStart, kExpectedNum);
246 }
247
248 // Verify KeyIdx field.
249 TEST_F(RtpPacketizerVp8Test, TestKeyIdx) {
250   const int kSizeVector[] = {10, 10, 10};
251   const int kNumPartitions = sizeof(kSizeVector) / sizeof(kSizeVector[0]);
252   ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
253
254   hdr_info_.keyIdx = 17;
255   // kMaxSize is only limited by allocated buffer size.
256   const int kMaxSize = helper_->buffer_size();
257   RtpPacketizerVp8 packetizer(hdr_info_, kMaxSize, kAggregate);
258   packetizer.SetPayloadData(helper_->payload_data(),
259                             helper_->payload_size(),
260                             helper_->fragmentation());
261
262   // Expect one single packet of payload_size() + 3 bytes header.
263   const int kExpectedSizes[1] = {helper_->payload_size() + 3};
264   const int kExpectedPart[1] = {0};  // Packet starts with partition 0.
265   const bool kExpectedFragStart[1] = {true};
266   const int kExpectedNum = sizeof(kExpectedSizes) / sizeof(kExpectedSizes[0]);
267   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
268   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
269
270   helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
271                                  kExpectedFragStart, kExpectedNum);
272 }
273
274 // Verify TID field and KeyIdx field in combination.
275 TEST_F(RtpPacketizerVp8Test, TestTIDAndKeyIdx) {
276   const int kSizeVector[] = {10, 10, 10};
277   const int kNumPartitions = sizeof(kSizeVector) / sizeof(kSizeVector[0]);
278   ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
279
280   hdr_info_.temporalIdx = 1;
281   hdr_info_.keyIdx = 5;
282   // kMaxSize is only limited by allocated buffer size.
283   const int kMaxSize = helper_->buffer_size();
284   RtpPacketizerVp8 packetizer(hdr_info_, kMaxSize, kAggregate);
285   packetizer.SetPayloadData(helper_->payload_data(),
286                             helper_->payload_size(),
287                             helper_->fragmentation());
288
289   // Expect one single packet of payload_size() + 3 bytes header.
290   const int kExpectedSizes[1] = {helper_->payload_size() + 3};
291   const int kExpectedPart[1] = {0};  // Packet starts with partition 0.
292   const bool kExpectedFragStart[1] = {true};
293   const int kExpectedNum = sizeof(kExpectedSizes) / sizeof(kExpectedSizes[0]);
294   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
295   CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
296
297   helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
298                                  kExpectedFragStart, kExpectedNum);
299 }
300
301 }  // namespace