OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Unit test for VideoCaptureController. | 5 // Unit test for VideoCaptureController. |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 client_a_.reset(new MockVideoCaptureControllerEventHandler( | 110 client_a_.reset(new MockVideoCaptureControllerEventHandler( |
111 controller_.get())); | 111 controller_.get())); |
112 client_b_.reset(new MockVideoCaptureControllerEventHandler( | 112 client_b_.reset(new MockVideoCaptureControllerEventHandler( |
113 controller_.get())); | 113 controller_.get())); |
114 } | 114 } |
115 | 115 |
116 void TearDown() override { base::RunLoop().RunUntilIdle(); } | 116 void TearDown() override { base::RunLoop().RunUntilIdle(); } |
117 | 117 |
118 scoped_refptr<media::VideoFrame> WrapI420Buffer(gfx::Size dimensions, | 118 scoped_refptr<media::VideoFrame> WrapI420Buffer(gfx::Size dimensions, |
119 uint8* data) { | 119 uint8* data) { |
120 return media::VideoFrame::WrapExternalSharedMemory( | 120 scoped_refptr<media::VideoFrame> video_frame = |
121 media::PIXEL_FORMAT_I420, dimensions, gfx::Rect(dimensions), dimensions, | 121 media::VideoFrame::WrapExternalSharedMemory( |
122 data, | 122 media::PIXEL_FORMAT_I420, dimensions, gfx::Rect(dimensions), |
123 media::VideoFrame::AllocationSize(media::PIXEL_FORMAT_I420, dimensions), | 123 dimensions, data, media::VideoFrame::AllocationSize( |
124 base::SharedMemory::NULLHandle(), 0u, base::TimeDelta()); | 124 media::PIXEL_FORMAT_I420, dimensions), |
| 125 base::SharedMemory::NULLHandle(), 0u, base::TimeDelta()); |
| 126 CHECK(video_frame); |
| 127 return video_frame; |
125 } | 128 } |
126 | 129 |
127 TestBrowserThreadBundle bundle_; | 130 TestBrowserThreadBundle bundle_; |
128 scoped_ptr<MockVideoCaptureControllerEventHandler> client_a_; | 131 scoped_ptr<MockVideoCaptureControllerEventHandler> client_a_; |
129 scoped_ptr<MockVideoCaptureControllerEventHandler> client_b_; | 132 scoped_ptr<MockVideoCaptureControllerEventHandler> client_b_; |
130 scoped_ptr<VideoCaptureController> controller_; | 133 scoped_ptr<VideoCaptureController> controller_; |
131 scoped_ptr<media::VideoCaptureDevice::Client> device_; | 134 scoped_ptr<media::VideoCaptureDevice::Client> device_; |
132 | 135 |
133 private: | 136 private: |
134 DISALLOW_COPY_AND_ASSIGN(VideoCaptureControllerTest); | 137 DISALLOW_COPY_AND_ASSIGN(VideoCaptureControllerTest); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 } | 308 } |
306 { | 309 { |
307 InSequence s; | 310 InSequence s; |
308 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1); | 311 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1); |
309 EXPECT_CALL(*client_a_, | 312 EXPECT_CALL(*client_a_, |
310 DoI420BufferReady(client_a_route_2, capture_resolution)) | 313 DoI420BufferReady(client_a_route_2, capture_resolution)) |
311 .Times(1); | 314 .Times(1); |
312 } | 315 } |
313 scoped_refptr<media::VideoFrame> video_frame = | 316 scoped_refptr<media::VideoFrame> video_frame = |
314 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer->data())); | 317 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer->data())); |
| 318 CHECK(video_frame); |
315 ASSERT_FALSE(video_frame->metadata()->HasKey( | 319 ASSERT_FALSE(video_frame->metadata()->HasKey( |
316 media::VideoFrameMetadata::RESOURCE_UTILIZATION)); | 320 media::VideoFrameMetadata::RESOURCE_UTILIZATION)); |
317 client_a_->resource_utilization_ = 0.5; | 321 client_a_->resource_utilization_ = 0.5; |
318 client_b_->resource_utilization_ = -1.0; | 322 client_b_->resource_utilization_ = -1.0; |
319 device_->OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, | 323 device_->OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, |
320 base::TimeTicks()); | 324 base::TimeTicks()); |
321 | 325 |
322 base::RunLoop().RunUntilIdle(); | 326 base::RunLoop().RunUntilIdle(); |
323 Mock::VerifyAndClearExpectations(client_a_.get()); | 327 Mock::VerifyAndClearExpectations(client_a_.get()); |
324 Mock::VerifyAndClearExpectations(client_b_.get()); | 328 Mock::VerifyAndClearExpectations(client_b_.get()); |
325 // Expect VideoCaptureController set the metadata in |video_frame| to hold a | 329 // Expect VideoCaptureController set the metadata in |video_frame| to hold a |
326 // resource utilization of 0.5 (the largest of all reported values). | 330 // resource utilization of 0.5 (the largest of all reported values). |
327 double resource_utilization_in_metadata = -1.0; | 331 double resource_utilization_in_metadata = -1.0; |
328 ASSERT_TRUE(video_frame->metadata()->GetDouble( | 332 ASSERT_TRUE(video_frame->metadata()->GetDouble( |
329 media::VideoFrameMetadata::RESOURCE_UTILIZATION, | 333 media::VideoFrameMetadata::RESOURCE_UTILIZATION, |
330 &resource_utilization_in_metadata)); | 334 &resource_utilization_in_metadata)); |
331 ASSERT_EQ(0.5, resource_utilization_in_metadata); | 335 ASSERT_EQ(0.5, resource_utilization_in_metadata); |
332 | 336 |
333 // Second buffer which ought to use the same shared memory buffer. In this | 337 // Second buffer which ought to use the same shared memory buffer. In this |
334 // case pretend that the Buffer pointer is held by the device for a long | 338 // case pretend that the Buffer pointer is held by the device for a long |
335 // delay. This shouldn't affect anything. | 339 // delay. This shouldn't affect anything. |
336 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer2 = | 340 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer2 = |
337 device_->ReserveOutputBuffer(capture_resolution, | 341 device_->ReserveOutputBuffer(capture_resolution, |
338 media::PIXEL_FORMAT_I420, | 342 media::PIXEL_FORMAT_I420, |
339 media::PIXEL_STORAGE_CPU); | 343 media::PIXEL_STORAGE_CPU); |
340 ASSERT_TRUE(buffer2.get()); | 344 ASSERT_TRUE(buffer2.get()); |
341 memset(buffer2->data(), buffer_no++, buffer2->mapped_size()); | 345 memset(buffer2->data(), buffer_no++, buffer2->mapped_size()); |
342 video_frame = | 346 video_frame = |
343 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer2->data())); | 347 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer2->data())); |
| 348 CHECK(video_frame); |
344 ASSERT_FALSE(video_frame->metadata()->HasKey( | 349 ASSERT_FALSE(video_frame->metadata()->HasKey( |
345 media::VideoFrameMetadata::RESOURCE_UTILIZATION)); | 350 media::VideoFrameMetadata::RESOURCE_UTILIZATION)); |
346 client_a_->resource_utilization_ = 0.5; | 351 client_a_->resource_utilization_ = 0.5; |
347 client_b_->resource_utilization_ = 3.14; | 352 client_b_->resource_utilization_ = 3.14; |
348 device_->OnIncomingCapturedVideoFrame(buffer2.Pass(), video_frame, | 353 device_->OnIncomingCapturedVideoFrame(buffer2.Pass(), video_frame, |
349 base::TimeTicks()); | 354 base::TimeTicks()); |
350 | 355 |
351 // The buffer should be delivered to the clients in any order. | 356 // The buffer should be delivered to the clients in any order. |
352 EXPECT_CALL(*client_a_, | 357 EXPECT_CALL(*client_a_, |
353 DoI420BufferReady(client_a_route_1, capture_resolution)) | 358 DoI420BufferReady(client_a_route_1, capture_resolution)) |
(...skipping 26 matching lines...) Expand all Loading... |
380 // Third, fourth, and fifth buffers. Pretend they all arrive at the same time. | 385 // Third, fourth, and fifth buffers. Pretend they all arrive at the same time. |
381 for (int i = 0; i < kPoolSize; i++) { | 386 for (int i = 0; i < kPoolSize; i++) { |
382 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer = | 387 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer = |
383 device_->ReserveOutputBuffer(capture_resolution, | 388 device_->ReserveOutputBuffer(capture_resolution, |
384 media::PIXEL_FORMAT_I420, | 389 media::PIXEL_FORMAT_I420, |
385 media::PIXEL_STORAGE_CPU); | 390 media::PIXEL_STORAGE_CPU); |
386 ASSERT_TRUE(buffer.get()); | 391 ASSERT_TRUE(buffer.get()); |
387 memset(buffer->data(), buffer_no++, buffer->mapped_size()); | 392 memset(buffer->data(), buffer_no++, buffer->mapped_size()); |
388 video_frame = | 393 video_frame = |
389 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer->data())); | 394 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer->data())); |
| 395 CHECK(video_frame); |
390 device_->OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, | 396 device_->OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, |
391 base::TimeTicks()); | 397 base::TimeTicks()); |
392 } | 398 } |
393 // ReserveOutputBuffer ought to fail now, because the pool is depleted. | 399 // ReserveOutputBuffer ought to fail now, because the pool is depleted. |
394 ASSERT_FALSE( | 400 ASSERT_FALSE( |
395 device_->ReserveOutputBuffer(capture_resolution, | 401 device_->ReserveOutputBuffer(capture_resolution, |
396 media::PIXEL_FORMAT_I420, | 402 media::PIXEL_FORMAT_I420, |
397 media::PIXEL_STORAGE_CPU).get()); | 403 media::PIXEL_STORAGE_CPU).get()); |
398 | 404 |
399 // The new client needs to be told of 3 buffers; the old clients only 2. | 405 // The new client needs to be told of 3 buffers; the old clients only 2. |
(...skipping 28 matching lines...) Expand all Loading... |
428 controller_->StopSession(300); | 434 controller_->StopSession(300); |
429 // Queue up another buffer. | 435 // Queue up another buffer. |
430 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer3 = | 436 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer3 = |
431 device_->ReserveOutputBuffer(capture_resolution, | 437 device_->ReserveOutputBuffer(capture_resolution, |
432 media::PIXEL_FORMAT_I420, | 438 media::PIXEL_FORMAT_I420, |
433 media::PIXEL_STORAGE_CPU); | 439 media::PIXEL_STORAGE_CPU); |
434 ASSERT_TRUE(buffer3.get()); | 440 ASSERT_TRUE(buffer3.get()); |
435 memset(buffer3->data(), buffer_no++, buffer3->mapped_size()); | 441 memset(buffer3->data(), buffer_no++, buffer3->mapped_size()); |
436 video_frame = | 442 video_frame = |
437 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer3->data())); | 443 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer3->data())); |
| 444 CHECK(video_frame); |
438 device_->OnIncomingCapturedVideoFrame(buffer3.Pass(), video_frame, | 445 device_->OnIncomingCapturedVideoFrame(buffer3.Pass(), video_frame, |
439 base::TimeTicks()); | 446 base::TimeTicks()); |
440 | 447 |
441 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer4 = | 448 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer4 = |
442 device_->ReserveOutputBuffer(capture_resolution, | 449 device_->ReserveOutputBuffer(capture_resolution, |
443 media::PIXEL_FORMAT_I420, | 450 media::PIXEL_FORMAT_I420, |
444 media::PIXEL_STORAGE_CPU); | 451 media::PIXEL_STORAGE_CPU); |
445 { | 452 { |
446 // Kill A2 via session close (posts a task to disconnect, but A2 must not | 453 // Kill A2 via session close (posts a task to disconnect, but A2 must not |
447 // be sent either of these two buffers). | 454 // be sent either of these two buffers). |
448 EXPECT_CALL(*client_a_, DoEnded(client_a_route_2)).Times(1); | 455 EXPECT_CALL(*client_a_, DoEnded(client_a_route_2)).Times(1); |
449 controller_->StopSession(200); | 456 controller_->StopSession(200); |
450 } | 457 } |
451 ASSERT_TRUE(buffer4.get()); | 458 ASSERT_TRUE(buffer4.get()); |
452 memset(buffer4->data(), buffer_no++, buffer4->mapped_size()); | 459 memset(buffer4->data(), buffer_no++, buffer4->mapped_size()); |
453 video_frame = | 460 video_frame = |
454 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer4->data())); | 461 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer4->data())); |
| 462 CHECK(video_frame); |
455 device_->OnIncomingCapturedVideoFrame(buffer4.Pass(), video_frame, | 463 device_->OnIncomingCapturedVideoFrame(buffer4.Pass(), video_frame, |
456 base::TimeTicks()); | 464 base::TimeTicks()); |
457 // B2 is the only client left, and is the only one that should | 465 // B2 is the only client left, and is the only one that should |
458 // get the buffer. | 466 // get the buffer. |
459 EXPECT_CALL(*client_b_, | 467 EXPECT_CALL(*client_b_, |
460 DoI420BufferReady(client_b_route_2, capture_resolution)) | 468 DoI420BufferReady(client_b_route_2, capture_resolution)) |
461 .Times(2); | 469 .Times(2); |
462 base::RunLoop().RunUntilIdle(); | 470 base::RunLoop().RunUntilIdle(); |
463 Mock::VerifyAndClearExpectations(client_a_.get()); | 471 Mock::VerifyAndClearExpectations(client_a_.get()); |
464 Mock::VerifyAndClearExpectations(client_b_.get()); | 472 Mock::VerifyAndClearExpectations(client_b_.get()); |
(...skipping 28 matching lines...) Expand all Loading... |
493 base::RunLoop().RunUntilIdle(); | 501 base::RunLoop().RunUntilIdle(); |
494 Mock::VerifyAndClearExpectations(client_b_.get()); | 502 Mock::VerifyAndClearExpectations(client_b_.get()); |
495 | 503 |
496 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer( | 504 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer( |
497 device_->ReserveOutputBuffer(capture_resolution, | 505 device_->ReserveOutputBuffer(capture_resolution, |
498 media::PIXEL_FORMAT_I420, | 506 media::PIXEL_FORMAT_I420, |
499 media::PIXEL_STORAGE_CPU)); | 507 media::PIXEL_STORAGE_CPU)); |
500 ASSERT_TRUE(buffer.get()); | 508 ASSERT_TRUE(buffer.get()); |
501 scoped_refptr<media::VideoFrame> video_frame = | 509 scoped_refptr<media::VideoFrame> video_frame = |
502 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer->data())); | 510 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer->data())); |
| 511 CHECK(video_frame); |
503 device_->OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, | 512 device_->OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, |
504 base::TimeTicks()); | 513 base::TimeTicks()); |
505 | 514 |
506 base::RunLoop().RunUntilIdle(); | 515 base::RunLoop().RunUntilIdle(); |
507 } | 516 } |
508 | 517 |
509 // Exercises the OnError() codepath of VideoCaptureController, and tests the | 518 // Exercises the OnError() codepath of VideoCaptureController, and tests the |
510 // behavior of various operations after the error state has been signalled. | 519 // behavior of various operations after the error state has been signalled. |
511 TEST_F(VideoCaptureControllerTest, ErrorAfterDeviceCreation) { | 520 TEST_F(VideoCaptureControllerTest, ErrorAfterDeviceCreation) { |
512 media::VideoCaptureParams session_100; | 521 media::VideoCaptureParams session_100; |
(...skipping 17 matching lines...) Expand all Loading... |
530 Mock::VerifyAndClearExpectations(client_a_.get()); | 539 Mock::VerifyAndClearExpectations(client_a_.get()); |
531 | 540 |
532 const gfx::Size dims(320, 240); | 541 const gfx::Size dims(320, 240); |
533 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer( | 542 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer( |
534 device_->ReserveOutputBuffer(dims, media::PIXEL_FORMAT_I420, | 543 device_->ReserveOutputBuffer(dims, media::PIXEL_FORMAT_I420, |
535 media::PIXEL_STORAGE_CPU)); | 544 media::PIXEL_STORAGE_CPU)); |
536 ASSERT_TRUE(buffer.get()); | 545 ASSERT_TRUE(buffer.get()); |
537 | 546 |
538 scoped_refptr<media::VideoFrame> video_frame = | 547 scoped_refptr<media::VideoFrame> video_frame = |
539 WrapI420Buffer(dims, static_cast<uint8*>(buffer->data())); | 548 WrapI420Buffer(dims, static_cast<uint8*>(buffer->data())); |
| 549 CHECK(video_frame); |
540 device_->OnError(FROM_HERE, "Test Error"); | 550 device_->OnError(FROM_HERE, "Test Error"); |
541 device_->OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, | 551 device_->OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, |
542 base::TimeTicks()); | 552 base::TimeTicks()); |
543 | 553 |
544 EXPECT_CALL(*client_a_, DoError(route_id)).Times(1); | 554 EXPECT_CALL(*client_a_, DoError(route_id)).Times(1); |
545 base::RunLoop().RunUntilIdle(); | 555 base::RunLoop().RunUntilIdle(); |
546 Mock::VerifyAndClearExpectations(client_a_.get()); | 556 Mock::VerifyAndClearExpectations(client_a_.get()); |
547 | 557 |
548 // Second client connects after the error state. It also should get told of | 558 // Second client connects after the error state. It also should get told of |
549 // the error. | 559 // the error. |
550 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); | 560 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); |
551 controller_->AddClient( | 561 controller_->AddClient( |
552 route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200); | 562 route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200); |
553 Mock::VerifyAndClearExpectations(client_b_.get()); | 563 Mock::VerifyAndClearExpectations(client_b_.get()); |
554 } | 564 } |
555 | 565 |
556 } // namespace content | 566 } // namespace content |
OLD | NEW |