OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 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 <memory> | |
12 #include <vector> | 11 #include <vector> |
13 | 12 |
14 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "webrtc/base/scoped_ptr.h" |
16 #include "webrtc/modules/include/module_common_types.h" | 16 #include "webrtc/modules/include/module_common_types.h" |
17 #include "webrtc/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h" | 17 #include "webrtc/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h" |
18 #include "webrtc/modules/rtp_rtcp/source/rtp_format.h" | 18 #include "webrtc/modules/rtp_rtcp/source/rtp_format.h" |
19 | 19 |
20 namespace webrtc { | 20 namespace webrtc { |
21 namespace { | 21 namespace { |
22 const size_t kMaxPayloadSize = 1200; | 22 const size_t kMaxPayloadSize = 1200; |
23 const size_t kLengthFieldLength = 2; | 23 const size_t kLengthFieldLength = 2; |
24 | 24 |
25 enum Nalu { | 25 enum Nalu { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 &expected_payload[offset + expected_sizes[fua_index]]); | 65 &expected_payload[offset + expected_sizes[fua_index]]); |
66 EXPECT_THAT( | 66 EXPECT_THAT( |
67 expected_packet_payload, | 67 expected_packet_payload, |
68 ::testing::ElementsAreArray(&packet[2], expected_sizes[fua_index])) | 68 ::testing::ElementsAreArray(&packet[2], expected_sizes[fua_index])) |
69 << "FUA index: " << fua_index; | 69 << "FUA index: " << fua_index; |
70 } | 70 } |
71 | 71 |
72 void TestFua(size_t frame_size, | 72 void TestFua(size_t frame_size, |
73 size_t max_payload_size, | 73 size_t max_payload_size, |
74 const std::vector<size_t>& expected_sizes) { | 74 const std::vector<size_t>& expected_sizes) { |
75 std::unique_ptr<uint8_t[]> frame; | 75 rtc::scoped_ptr<uint8_t[]> frame; |
76 frame.reset(new uint8_t[frame_size]); | 76 frame.reset(new uint8_t[frame_size]); |
77 frame[0] = 0x05; // F=0, NRI=0, Type=5. | 77 frame[0] = 0x05; // F=0, NRI=0, Type=5. |
78 for (size_t i = 0; i < frame_size - kNalHeaderSize; ++i) { | 78 for (size_t i = 0; i < frame_size - kNalHeaderSize; ++i) { |
79 frame[i + kNalHeaderSize] = i; | 79 frame[i + kNalHeaderSize] = i; |
80 } | 80 } |
81 RTPFragmentationHeader fragmentation; | 81 RTPFragmentationHeader fragmentation; |
82 fragmentation.VerifyAndAllocateFragmentationHeader(1); | 82 fragmentation.VerifyAndAllocateFragmentationHeader(1); |
83 fragmentation.fragmentationOffset[0] = 0; | 83 fragmentation.fragmentationOffset[0] = 0; |
84 fragmentation.fragmentationLength[0] = frame_size; | 84 fragmentation.fragmentationLength[0] = frame_size; |
85 std::unique_ptr<RtpPacketizer> packetizer(RtpPacketizer::Create( | 85 rtc::scoped_ptr<RtpPacketizer> packetizer(RtpPacketizer::Create( |
86 kRtpVideoH264, max_payload_size, NULL, kEmptyFrame)); | 86 kRtpVideoH264, max_payload_size, NULL, kEmptyFrame)); |
87 packetizer->SetPayloadData(frame.get(), frame_size, &fragmentation); | 87 packetizer->SetPayloadData(frame.get(), frame_size, &fragmentation); |
88 | 88 |
89 std::unique_ptr<uint8_t[]> packet(new uint8_t[max_payload_size]); | 89 rtc::scoped_ptr<uint8_t[]> packet(new uint8_t[max_payload_size]); |
90 size_t length = 0; | 90 size_t length = 0; |
91 bool last = false; | 91 bool last = false; |
92 size_t offset = kNalHeaderSize; | 92 size_t offset = kNalHeaderSize; |
93 for (size_t i = 0; i < expected_sizes.size(); ++i) { | 93 for (size_t i = 0; i < expected_sizes.size(); ++i) { |
94 ASSERT_TRUE(packetizer->NextPacket(packet.get(), &length, &last)); | 94 ASSERT_TRUE(packetizer->NextPacket(packet.get(), &length, &last)); |
95 VerifyFua(i, frame.get(), offset, packet.get(), length, expected_sizes); | 95 VerifyFua(i, frame.get(), offset, packet.get(), length, expected_sizes); |
96 EXPECT_EQ(i == expected_sizes.size() - 1, last) << "FUA index: " << i; | 96 EXPECT_EQ(i == expected_sizes.size() - 1, last) << "FUA index: " << i; |
97 offset += expected_sizes[i]; | 97 offset += expected_sizes[i]; |
98 } | 98 } |
99 | 99 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 ::testing::ElementsAreArray(packet, packet_length)); | 149 ::testing::ElementsAreArray(packet, packet_length)); |
150 } | 150 } |
151 } // namespace | 151 } // namespace |
152 | 152 |
153 TEST(RtpPacketizerH264Test, TestSingleNalu) { | 153 TEST(RtpPacketizerH264Test, TestSingleNalu) { |
154 const uint8_t frame[2] = {0x05, 0xFF}; // F=0, NRI=0, Type=5. | 154 const uint8_t frame[2] = {0x05, 0xFF}; // F=0, NRI=0, Type=5. |
155 RTPFragmentationHeader fragmentation; | 155 RTPFragmentationHeader fragmentation; |
156 fragmentation.VerifyAndAllocateFragmentationHeader(1); | 156 fragmentation.VerifyAndAllocateFragmentationHeader(1); |
157 fragmentation.fragmentationOffset[0] = 0; | 157 fragmentation.fragmentationOffset[0] = 0; |
158 fragmentation.fragmentationLength[0] = sizeof(frame); | 158 fragmentation.fragmentationLength[0] = sizeof(frame); |
159 std::unique_ptr<RtpPacketizer> packetizer( | 159 rtc::scoped_ptr<RtpPacketizer> packetizer( |
160 RtpPacketizer::Create(kRtpVideoH264, kMaxPayloadSize, NULL, kEmptyFrame)); | 160 RtpPacketizer::Create(kRtpVideoH264, kMaxPayloadSize, NULL, kEmptyFrame)); |
161 packetizer->SetPayloadData(frame, sizeof(frame), &fragmentation); | 161 packetizer->SetPayloadData(frame, sizeof(frame), &fragmentation); |
162 uint8_t packet[kMaxPayloadSize] = {0}; | 162 uint8_t packet[kMaxPayloadSize] = {0}; |
163 size_t length = 0; | 163 size_t length = 0; |
164 bool last = false; | 164 bool last = false; |
165 ASSERT_TRUE(packetizer->NextPacket(packet, &length, &last)); | 165 ASSERT_TRUE(packetizer->NextPacket(packet, &length, &last)); |
166 EXPECT_EQ(2u, length); | 166 EXPECT_EQ(2u, length); |
167 EXPECT_TRUE(last); | 167 EXPECT_TRUE(last); |
168 VerifySingleNaluPayload( | 168 VerifySingleNaluPayload( |
169 fragmentation, 0, frame, sizeof(frame), packet, length); | 169 fragmentation, 0, frame, sizeof(frame), packet, length); |
170 EXPECT_FALSE(packetizer->NextPacket(packet, &length, &last)); | 170 EXPECT_FALSE(packetizer->NextPacket(packet, &length, &last)); |
171 } | 171 } |
172 | 172 |
173 TEST(RtpPacketizerH264Test, TestSingleNaluTwoPackets) { | 173 TEST(RtpPacketizerH264Test, TestSingleNaluTwoPackets) { |
174 const size_t kFrameSize = kMaxPayloadSize + 100; | 174 const size_t kFrameSize = kMaxPayloadSize + 100; |
175 uint8_t frame[kFrameSize] = {0}; | 175 uint8_t frame[kFrameSize] = {0}; |
176 for (size_t i = 0; i < kFrameSize; ++i) | 176 for (size_t i = 0; i < kFrameSize; ++i) |
177 frame[i] = i; | 177 frame[i] = i; |
178 RTPFragmentationHeader fragmentation; | 178 RTPFragmentationHeader fragmentation; |
179 fragmentation.VerifyAndAllocateFragmentationHeader(2); | 179 fragmentation.VerifyAndAllocateFragmentationHeader(2); |
180 fragmentation.fragmentationOffset[0] = 0; | 180 fragmentation.fragmentationOffset[0] = 0; |
181 fragmentation.fragmentationLength[0] = kMaxPayloadSize; | 181 fragmentation.fragmentationLength[0] = kMaxPayloadSize; |
182 fragmentation.fragmentationOffset[1] = kMaxPayloadSize; | 182 fragmentation.fragmentationOffset[1] = kMaxPayloadSize; |
183 fragmentation.fragmentationLength[1] = 100; | 183 fragmentation.fragmentationLength[1] = 100; |
184 // Set NAL headers. | 184 // Set NAL headers. |
185 frame[fragmentation.fragmentationOffset[0]] = 0x01; | 185 frame[fragmentation.fragmentationOffset[0]] = 0x01; |
186 frame[fragmentation.fragmentationOffset[1]] = 0x01; | 186 frame[fragmentation.fragmentationOffset[1]] = 0x01; |
187 | 187 |
188 std::unique_ptr<RtpPacketizer> packetizer( | 188 rtc::scoped_ptr<RtpPacketizer> packetizer( |
189 RtpPacketizer::Create(kRtpVideoH264, kMaxPayloadSize, NULL, kEmptyFrame)); | 189 RtpPacketizer::Create(kRtpVideoH264, kMaxPayloadSize, NULL, kEmptyFrame)); |
190 packetizer->SetPayloadData(frame, kFrameSize, &fragmentation); | 190 packetizer->SetPayloadData(frame, kFrameSize, &fragmentation); |
191 | 191 |
192 uint8_t packet[kMaxPayloadSize] = {0}; | 192 uint8_t packet[kMaxPayloadSize] = {0}; |
193 size_t length = 0; | 193 size_t length = 0; |
194 bool last = false; | 194 bool last = false; |
195 ASSERT_TRUE(packetizer->NextPacket(packet, &length, &last)); | 195 ASSERT_TRUE(packetizer->NextPacket(packet, &length, &last)); |
196 ASSERT_EQ(fragmentation.fragmentationOffset[1], length); | 196 ASSERT_EQ(fragmentation.fragmentationOffset[1], length); |
197 VerifySingleNaluPayload(fragmentation, 0, frame, kFrameSize, packet, length); | 197 VerifySingleNaluPayload(fragmentation, 0, frame, kFrameSize, packet, length); |
198 | 198 |
(...skipping 16 matching lines...) Expand all Loading... |
215 frame[i + kPayloadOffset] = i; | 215 frame[i + kPayloadOffset] = i; |
216 RTPFragmentationHeader fragmentation; | 216 RTPFragmentationHeader fragmentation; |
217 fragmentation.VerifyAndAllocateFragmentationHeader(3); | 217 fragmentation.VerifyAndAllocateFragmentationHeader(3); |
218 fragmentation.fragmentationOffset[0] = 0; | 218 fragmentation.fragmentationOffset[0] = 0; |
219 fragmentation.fragmentationLength[0] = 2; | 219 fragmentation.fragmentationLength[0] = 2; |
220 fragmentation.fragmentationOffset[1] = 2; | 220 fragmentation.fragmentationOffset[1] = 2; |
221 fragmentation.fragmentationLength[1] = 2; | 221 fragmentation.fragmentationLength[1] = 2; |
222 fragmentation.fragmentationOffset[2] = 4; | 222 fragmentation.fragmentationOffset[2] = 4; |
223 fragmentation.fragmentationLength[2] = | 223 fragmentation.fragmentationLength[2] = |
224 kNalHeaderSize + kFrameSize - kPayloadOffset; | 224 kNalHeaderSize + kFrameSize - kPayloadOffset; |
225 std::unique_ptr<RtpPacketizer> packetizer( | 225 rtc::scoped_ptr<RtpPacketizer> packetizer( |
226 RtpPacketizer::Create(kRtpVideoH264, kMaxPayloadSize, NULL, kEmptyFrame)); | 226 RtpPacketizer::Create(kRtpVideoH264, kMaxPayloadSize, NULL, kEmptyFrame)); |
227 packetizer->SetPayloadData(frame, kFrameSize, &fragmentation); | 227 packetizer->SetPayloadData(frame, kFrameSize, &fragmentation); |
228 | 228 |
229 uint8_t packet[kMaxPayloadSize] = {0}; | 229 uint8_t packet[kMaxPayloadSize] = {0}; |
230 size_t length = 0; | 230 size_t length = 0; |
231 bool last = false; | 231 bool last = false; |
232 ASSERT_TRUE(packetizer->NextPacket(packet, &length, &last)); | 232 ASSERT_TRUE(packetizer->NextPacket(packet, &length, &last)); |
233 size_t expected_packet_size = | 233 size_t expected_packet_size = |
234 kNalHeaderSize + 3 * kLengthFieldLength + kFrameSize; | 234 kNalHeaderSize + 3 * kLengthFieldLength + kFrameSize; |
235 ASSERT_EQ(expected_packet_size, length); | 235 ASSERT_EQ(expected_packet_size, length); |
(...skipping 14 matching lines...) Expand all Loading... |
250 frame[i + kPayloadOffset] = i; | 250 frame[i + kPayloadOffset] = i; |
251 RTPFragmentationHeader fragmentation; | 251 RTPFragmentationHeader fragmentation; |
252 fragmentation.VerifyAndAllocateFragmentationHeader(3); | 252 fragmentation.VerifyAndAllocateFragmentationHeader(3); |
253 fragmentation.fragmentationOffset[0] = 0; | 253 fragmentation.fragmentationOffset[0] = 0; |
254 fragmentation.fragmentationLength[0] = 2; | 254 fragmentation.fragmentationLength[0] = 2; |
255 fragmentation.fragmentationOffset[1] = 2; | 255 fragmentation.fragmentationOffset[1] = 2; |
256 fragmentation.fragmentationLength[1] = 2; | 256 fragmentation.fragmentationLength[1] = 2; |
257 fragmentation.fragmentationOffset[2] = 4; | 257 fragmentation.fragmentationOffset[2] = 4; |
258 fragmentation.fragmentationLength[2] = | 258 fragmentation.fragmentationLength[2] = |
259 kNalHeaderSize + kFrameSize - kPayloadOffset; | 259 kNalHeaderSize + kFrameSize - kPayloadOffset; |
260 std::unique_ptr<RtpPacketizer> packetizer( | 260 rtc::scoped_ptr<RtpPacketizer> packetizer( |
261 RtpPacketizer::Create(kRtpVideoH264, kMaxPayloadSize, NULL, kEmptyFrame)); | 261 RtpPacketizer::Create(kRtpVideoH264, kMaxPayloadSize, NULL, kEmptyFrame)); |
262 packetizer->SetPayloadData(frame, kFrameSize, &fragmentation); | 262 packetizer->SetPayloadData(frame, kFrameSize, &fragmentation); |
263 | 263 |
264 uint8_t packet[kMaxPayloadSize] = {0}; | 264 uint8_t packet[kMaxPayloadSize] = {0}; |
265 size_t length = 0; | 265 size_t length = 0; |
266 bool last = false; | 266 bool last = false; |
267 ASSERT_TRUE(packetizer->NextPacket(packet, &length, &last)); | 267 ASSERT_TRUE(packetizer->NextPacket(packet, &length, &last)); |
268 size_t expected_packet_size = kNalHeaderSize; | 268 size_t expected_packet_size = kNalHeaderSize; |
269 for (size_t i = 0; i < 2; ++i) { | 269 for (size_t i = 0; i < 2; ++i) { |
270 expected_packet_size += | 270 expected_packet_size += |
(...skipping 27 matching lines...) Expand all Loading... |
298 const size_t kFrameSize = kFuaNaluSize + 2 * kStapANaluSize; | 298 const size_t kFrameSize = kFuaNaluSize + 2 * kStapANaluSize; |
299 uint8_t frame[kFrameSize]; | 299 uint8_t frame[kFrameSize]; |
300 size_t nalu_offset = 0; | 300 size_t nalu_offset = 0; |
301 for (size_t i = 0; i < fragmentation.fragmentationVectorSize; ++i) { | 301 for (size_t i = 0; i < fragmentation.fragmentationVectorSize; ++i) { |
302 nalu_offset = fragmentation.fragmentationOffset[i]; | 302 nalu_offset = fragmentation.fragmentationOffset[i]; |
303 frame[nalu_offset] = 0x05; // F=0, NRI=0, Type=5. | 303 frame[nalu_offset] = 0x05; // F=0, NRI=0, Type=5. |
304 for (size_t j = 1; j < fragmentation.fragmentationLength[i]; ++j) { | 304 for (size_t j = 1; j < fragmentation.fragmentationLength[i]; ++j) { |
305 frame[nalu_offset + j] = i + j; | 305 frame[nalu_offset + j] = i + j; |
306 } | 306 } |
307 } | 307 } |
308 std::unique_ptr<RtpPacketizer> packetizer( | 308 rtc::scoped_ptr<RtpPacketizer> packetizer( |
309 RtpPacketizer::Create(kRtpVideoH264, kMaxPayloadSize, NULL, kEmptyFrame)); | 309 RtpPacketizer::Create(kRtpVideoH264, kMaxPayloadSize, NULL, kEmptyFrame)); |
310 packetizer->SetPayloadData(frame, kFrameSize, &fragmentation); | 310 packetizer->SetPayloadData(frame, kFrameSize, &fragmentation); |
311 | 311 |
312 // First expecting two FU-A packets. | 312 // First expecting two FU-A packets. |
313 std::vector<size_t> fua_sizes; | 313 std::vector<size_t> fua_sizes; |
314 fua_sizes.push_back(1100); | 314 fua_sizes.push_back(1100); |
315 fua_sizes.push_back(1099); | 315 fua_sizes.push_back(1099); |
316 uint8_t packet[kMaxPayloadSize] = {0}; | 316 uint8_t packet[kMaxPayloadSize] = {0}; |
317 size_t length = 0; | 317 size_t length = 0; |
318 bool last = false; | 318 bool last = false; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 void ExpectPacket(RtpDepacketizer::ParsedPayload* parsed_payload, | 387 void ExpectPacket(RtpDepacketizer::ParsedPayload* parsed_payload, |
388 const uint8_t* data, | 388 const uint8_t* data, |
389 size_t length) { | 389 size_t length) { |
390 ASSERT_TRUE(parsed_payload != NULL); | 390 ASSERT_TRUE(parsed_payload != NULL); |
391 EXPECT_THAT(std::vector<uint8_t>( | 391 EXPECT_THAT(std::vector<uint8_t>( |
392 parsed_payload->payload, | 392 parsed_payload->payload, |
393 parsed_payload->payload + parsed_payload->payload_length), | 393 parsed_payload->payload + parsed_payload->payload_length), |
394 ::testing::ElementsAreArray(data, length)); | 394 ::testing::ElementsAreArray(data, length)); |
395 } | 395 } |
396 | 396 |
397 std::unique_ptr<RtpDepacketizer> depacketizer_; | 397 rtc::scoped_ptr<RtpDepacketizer> depacketizer_; |
398 }; | 398 }; |
399 | 399 |
400 TEST_F(RtpDepacketizerH264Test, TestSingleNalu) { | 400 TEST_F(RtpDepacketizerH264Test, TestSingleNalu) { |
401 uint8_t packet[2] = {0x05, 0xFF}; // F=0, NRI=0, Type=5 (IDR). | 401 uint8_t packet[2] = {0x05, 0xFF}; // F=0, NRI=0, Type=5 (IDR). |
402 RtpDepacketizer::ParsedPayload payload; | 402 RtpDepacketizer::ParsedPayload payload; |
403 | 403 |
404 ASSERT_TRUE(depacketizer_->Parse(&payload, packet, sizeof(packet))); | 404 ASSERT_TRUE(depacketizer_->Parse(&payload, packet, sizeof(packet))); |
405 ExpectPacket(&payload, packet, sizeof(packet)); | 405 ExpectPacket(&payload, packet, sizeof(packet)); |
406 EXPECT_EQ(kVideoFrameKey, payload.frame_type); | 406 EXPECT_EQ(kVideoFrameKey, payload.frame_type); |
407 EXPECT_EQ(kRtpVideoH264, payload.type.Video.codec); | 407 EXPECT_EQ(kRtpVideoH264, payload.type.Video.codec); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 EXPECT_FALSE(depacketizer_->Parse(&payload, kPayload, sizeof(kPayload))); | 563 EXPECT_FALSE(depacketizer_->Parse(&payload, kPayload, sizeof(kPayload))); |
564 } | 564 } |
565 | 565 |
566 TEST_F(RtpDepacketizerH264Test, TestShortSpsPacket) { | 566 TEST_F(RtpDepacketizerH264Test, TestShortSpsPacket) { |
567 const uint8_t kPayload[] = {0x27, 0x80, 0x00}; | 567 const uint8_t kPayload[] = {0x27, 0x80, 0x00}; |
568 RtpDepacketizer::ParsedPayload payload; | 568 RtpDepacketizer::ParsedPayload payload; |
569 EXPECT_TRUE(depacketizer_->Parse(&payload, kPayload, sizeof(kPayload))); | 569 EXPECT_TRUE(depacketizer_->Parse(&payload, kPayload, sizeof(kPayload))); |
570 } | 570 } |
571 | 571 |
572 } // namespace webrtc | 572 } // namespace webrtc |
OLD | NEW |