| 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 "content/browser/renderer_host/media/video_capture_controller.h" | 7 #include "content/browser/renderer_host/media/video_capture_controller.h" |
| 8 | 8 |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 media::PIXEL_FORMAT_I420, dimensions), | 132 media::PIXEL_FORMAT_I420, dimensions), |
| 133 base::SharedMemory::NULLHandle(), 0u, base::TimeDelta()); | 133 base::SharedMemory::NULLHandle(), 0u, base::TimeDelta()); |
| 134 EXPECT_TRUE(video_frame); | 134 EXPECT_TRUE(video_frame); |
| 135 return video_frame; | 135 return video_frame; |
| 136 } | 136 } |
| 137 | 137 |
| 138 TestBrowserThreadBundle bundle_; | 138 TestBrowserThreadBundle bundle_; |
| 139 std::unique_ptr<MockVideoCaptureControllerEventHandler> client_a_; | 139 std::unique_ptr<MockVideoCaptureControllerEventHandler> client_a_; |
| 140 std::unique_ptr<MockVideoCaptureControllerEventHandler> client_b_; | 140 std::unique_ptr<MockVideoCaptureControllerEventHandler> client_b_; |
| 141 std::unique_ptr<VideoCaptureController> controller_; | 141 std::unique_ptr<VideoCaptureController> controller_; |
| 142 std::unique_ptr<media::VideoCaptureDevice::Client> device_; | 142 std::unique_ptr<device::VideoCaptureDevice::Client> device_; |
| 143 | 143 |
| 144 private: | 144 private: |
| 145 DISALLOW_COPY_AND_ASSIGN(VideoCaptureControllerTest); | 145 DISALLOW_COPY_AND_ASSIGN(VideoCaptureControllerTest); |
| 146 }; | 146 }; |
| 147 | 147 |
| 148 // A simple test of VideoCaptureController's ability to add, remove, and keep | 148 // A simple test of VideoCaptureController's ability to add, remove, and keep |
| 149 // track of clients. | 149 // track of clients. |
| 150 TEST_F(VideoCaptureControllerTest, AddAndRemoveClients) { | 150 TEST_F(VideoCaptureControllerTest, AddAndRemoveClients) { |
| 151 media::VideoCaptureParams session_100; | 151 media::VideoCaptureParams session_100; |
| 152 session_100.requested_format = media::VideoCaptureFormat( | 152 session_100.requested_format = media::VideoCaptureFormat( |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 client_a_.get(), | 286 client_a_.get(), |
| 287 base::kNullProcessHandle, | 287 base::kNullProcessHandle, |
| 288 200, | 288 200, |
| 289 session_200); | 289 session_200); |
| 290 ASSERT_EQ(3, controller_->GetClientCount()); | 290 ASSERT_EQ(3, controller_->GetClientCount()); |
| 291 | 291 |
| 292 // Now, simulate an incoming captured buffer from the capture device. As a | 292 // Now, simulate an incoming captured buffer from the capture device. As a |
| 293 // side effect this will cause the first buffer to be shared with clients. | 293 // side effect this will cause the first buffer to be shared with clients. |
| 294 uint8_t buffer_no = 1; | 294 uint8_t buffer_no = 1; |
| 295 ASSERT_EQ(0.0, device_->GetBufferPoolUtilization()); | 295 ASSERT_EQ(0.0, device_->GetBufferPoolUtilization()); |
| 296 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer( | 296 std::unique_ptr<device::VideoCaptureDevice::Client::Buffer> buffer( |
| 297 device_->ReserveOutputBuffer(capture_resolution, media::PIXEL_FORMAT_I420, | 297 device_->ReserveOutputBuffer(capture_resolution, media::PIXEL_FORMAT_I420, |
| 298 media::PIXEL_STORAGE_CPU)); | 298 media::PIXEL_STORAGE_CPU)); |
| 299 ASSERT_TRUE(buffer.get()); | 299 ASSERT_TRUE(buffer.get()); |
| 300 ASSERT_EQ(1.0 / kPoolSize, device_->GetBufferPoolUtilization()); | 300 ASSERT_EQ(1.0 / kPoolSize, device_->GetBufferPoolUtilization()); |
| 301 memset(buffer->data(), buffer_no++, buffer->mapped_size()); | 301 memset(buffer->data(), buffer_no++, buffer->mapped_size()); |
| 302 { | 302 { |
| 303 InSequence s; | 303 InSequence s; |
| 304 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)).Times(1); | 304 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)).Times(1); |
| 305 EXPECT_CALL(*client_a_, | 305 EXPECT_CALL(*client_a_, |
| 306 DoI420BufferReady(client_a_route_1, capture_resolution)) | 306 DoI420BufferReady(client_a_route_1, capture_resolution)) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 // resource utilization of 0.5 (the largest of all reported values). | 339 // resource utilization of 0.5 (the largest of all reported values). |
| 340 double resource_utilization_in_metadata = -1.0; | 340 double resource_utilization_in_metadata = -1.0; |
| 341 ASSERT_TRUE(video_frame->metadata()->GetDouble( | 341 ASSERT_TRUE(video_frame->metadata()->GetDouble( |
| 342 media::VideoFrameMetadata::RESOURCE_UTILIZATION, | 342 media::VideoFrameMetadata::RESOURCE_UTILIZATION, |
| 343 &resource_utilization_in_metadata)); | 343 &resource_utilization_in_metadata)); |
| 344 ASSERT_EQ(0.5, resource_utilization_in_metadata); | 344 ASSERT_EQ(0.5, resource_utilization_in_metadata); |
| 345 | 345 |
| 346 // Second buffer which ought to use the same shared memory buffer. In this | 346 // Second buffer which ought to use the same shared memory buffer. In this |
| 347 // case pretend that the Buffer pointer is held by the device for a long | 347 // case pretend that the Buffer pointer is held by the device for a long |
| 348 // delay. This shouldn't affect anything. | 348 // delay. This shouldn't affect anything. |
| 349 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer2 = | 349 std::unique_ptr<device::VideoCaptureDevice::Client::Buffer> buffer2 = |
| 350 device_->ReserveOutputBuffer(capture_resolution, media::PIXEL_FORMAT_I420, | 350 device_->ReserveOutputBuffer(capture_resolution, media::PIXEL_FORMAT_I420, |
| 351 media::PIXEL_STORAGE_CPU); | 351 media::PIXEL_STORAGE_CPU); |
| 352 ASSERT_TRUE(buffer2.get()); | 352 ASSERT_TRUE(buffer2.get()); |
| 353 memset(buffer2->data(), buffer_no++, buffer2->mapped_size()); | 353 memset(buffer2->data(), buffer_no++, buffer2->mapped_size()); |
| 354 video_frame = WrapI420Buffer(capture_resolution, | 354 video_frame = WrapI420Buffer(capture_resolution, |
| 355 static_cast<uint8_t*>(buffer2->data())); | 355 static_cast<uint8_t*>(buffer2->data())); |
| 356 ASSERT_TRUE(video_frame); | 356 ASSERT_TRUE(video_frame); |
| 357 ASSERT_FALSE(video_frame->metadata()->HasKey( | 357 ASSERT_FALSE(video_frame->metadata()->HasKey( |
| 358 media::VideoFrameMetadata::RESOURCE_UTILIZATION)); | 358 media::VideoFrameMetadata::RESOURCE_UTILIZATION)); |
| 359 client_a_->resource_utilization_ = 0.5; | 359 client_a_->resource_utilization_ = 0.5; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 // Add a fourth client now that some buffers have come through. | 398 // Add a fourth client now that some buffers have come through. |
| 399 controller_->AddClient(client_b_route_2, | 399 controller_->AddClient(client_b_route_2, |
| 400 client_b_.get(), | 400 client_b_.get(), |
| 401 base::kNullProcessHandle, | 401 base::kNullProcessHandle, |
| 402 1, | 402 1, |
| 403 session_1); | 403 session_1); |
| 404 Mock::VerifyAndClearExpectations(client_b_.get()); | 404 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 405 | 405 |
| 406 // Third, fourth, and fifth buffers. Pretend they all arrive at the same time. | 406 // Third, fourth, and fifth buffers. Pretend they all arrive at the same time. |
| 407 for (int i = 0; i < kPoolSize; i++) { | 407 for (int i = 0; i < kPoolSize; i++) { |
| 408 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer = | 408 std::unique_ptr<device::VideoCaptureDevice::Client::Buffer> buffer = |
| 409 device_->ReserveOutputBuffer(capture_resolution, | 409 device_->ReserveOutputBuffer(capture_resolution, |
| 410 media::PIXEL_FORMAT_I420, | 410 media::PIXEL_FORMAT_I420, |
| 411 media::PIXEL_STORAGE_CPU); | 411 media::PIXEL_STORAGE_CPU); |
| 412 ASSERT_TRUE(buffer.get()); | 412 ASSERT_TRUE(buffer.get()); |
| 413 memset(buffer->data(), buffer_no++, buffer->mapped_size()); | 413 memset(buffer->data(), buffer_no++, buffer->mapped_size()); |
| 414 video_frame = WrapI420Buffer(capture_resolution, | 414 video_frame = WrapI420Buffer(capture_resolution, |
| 415 static_cast<uint8_t*>(buffer->data())); | 415 static_cast<uint8_t*>(buffer->data())); |
| 416 ASSERT_TRUE(video_frame); | 416 ASSERT_TRUE(video_frame); |
| 417 video_frame->metadata()->SetTimeTicks( | 417 video_frame->metadata()->SetTimeTicks( |
| 418 media::VideoFrameMetadata::REFERENCE_TIME, base::TimeTicks()); | 418 media::VideoFrameMetadata::REFERENCE_TIME, base::TimeTicks()); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 449 Mock::VerifyAndClearExpectations(client_a_.get()); | 449 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 450 Mock::VerifyAndClearExpectations(client_b_.get()); | 450 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 451 | 451 |
| 452 // Now test the interaction of client shutdown and buffer delivery. | 452 // Now test the interaction of client shutdown and buffer delivery. |
| 453 // Kill A1 via renderer disconnect (synchronous). | 453 // Kill A1 via renderer disconnect (synchronous). |
| 454 controller_->RemoveClient(client_a_route_1, client_a_.get()); | 454 controller_->RemoveClient(client_a_route_1, client_a_.get()); |
| 455 // Kill B1 via session close (posts a task to disconnect). | 455 // Kill B1 via session close (posts a task to disconnect). |
| 456 EXPECT_CALL(*client_b_, DoEnded(client_b_route_1)).Times(1); | 456 EXPECT_CALL(*client_b_, DoEnded(client_b_route_1)).Times(1); |
| 457 controller_->StopSession(300); | 457 controller_->StopSession(300); |
| 458 // Queue up another buffer. | 458 // Queue up another buffer. |
| 459 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer3 = | 459 std::unique_ptr<device::VideoCaptureDevice::Client::Buffer> buffer3 = |
| 460 device_->ReserveOutputBuffer(capture_resolution, media::PIXEL_FORMAT_I420, | 460 device_->ReserveOutputBuffer(capture_resolution, media::PIXEL_FORMAT_I420, |
| 461 media::PIXEL_STORAGE_CPU); | 461 media::PIXEL_STORAGE_CPU); |
| 462 ASSERT_TRUE(buffer3.get()); | 462 ASSERT_TRUE(buffer3.get()); |
| 463 memset(buffer3->data(), buffer_no++, buffer3->mapped_size()); | 463 memset(buffer3->data(), buffer_no++, buffer3->mapped_size()); |
| 464 video_frame = WrapI420Buffer(capture_resolution, | 464 video_frame = WrapI420Buffer(capture_resolution, |
| 465 static_cast<uint8_t*>(buffer3->data())); | 465 static_cast<uint8_t*>(buffer3->data())); |
| 466 ASSERT_TRUE(video_frame); | 466 ASSERT_TRUE(video_frame); |
| 467 video_frame->metadata()->SetTimeTicks( | 467 video_frame->metadata()->SetTimeTicks( |
| 468 media::VideoFrameMetadata::REFERENCE_TIME, base::TimeTicks()); | 468 media::VideoFrameMetadata::REFERENCE_TIME, base::TimeTicks()); |
| 469 device_->OnIncomingCapturedVideoFrame(std::move(buffer3), video_frame); | 469 device_->OnIncomingCapturedVideoFrame(std::move(buffer3), video_frame); |
| 470 | 470 |
| 471 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer4 = | 471 std::unique_ptr<device::VideoCaptureDevice::Client::Buffer> buffer4 = |
| 472 device_->ReserveOutputBuffer(capture_resolution, media::PIXEL_FORMAT_I420, | 472 device_->ReserveOutputBuffer(capture_resolution, media::PIXEL_FORMAT_I420, |
| 473 media::PIXEL_STORAGE_CPU); | 473 media::PIXEL_STORAGE_CPU); |
| 474 { | 474 { |
| 475 // Kill A2 via session close (posts a task to disconnect, but A2 must not | 475 // Kill A2 via session close (posts a task to disconnect, but A2 must not |
| 476 // be sent either of these two buffers). | 476 // be sent either of these two buffers). |
| 477 EXPECT_CALL(*client_a_, DoEnded(client_a_route_2)).Times(1); | 477 EXPECT_CALL(*client_a_, DoEnded(client_a_route_2)).Times(1); |
| 478 controller_->StopSession(200); | 478 controller_->StopSession(200); |
| 479 } | 479 } |
| 480 ASSERT_TRUE(buffer4.get()); | 480 ASSERT_TRUE(buffer4.get()); |
| 481 memset(buffer4->data(), buffer_no++, buffer4->mapped_size()); | 481 memset(buffer4->data(), buffer_no++, buffer4->mapped_size()); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 Mock::VerifyAndClearExpectations(client_a_.get()); | 517 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 518 | 518 |
| 519 // Second client connects after the error state. It also should get told of | 519 // Second client connects after the error state. It also should get told of |
| 520 // the error. | 520 // the error. |
| 521 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); | 521 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); |
| 522 controller_->AddClient( | 522 controller_->AddClient( |
| 523 route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200); | 523 route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200); |
| 524 base::RunLoop().RunUntilIdle(); | 524 base::RunLoop().RunUntilIdle(); |
| 525 Mock::VerifyAndClearExpectations(client_b_.get()); | 525 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 526 | 526 |
| 527 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer( | 527 std::unique_ptr<device::VideoCaptureDevice::Client::Buffer> buffer( |
| 528 device_->ReserveOutputBuffer(capture_resolution, media::PIXEL_FORMAT_I420, | 528 device_->ReserveOutputBuffer(capture_resolution, media::PIXEL_FORMAT_I420, |
| 529 media::PIXEL_STORAGE_CPU)); | 529 media::PIXEL_STORAGE_CPU)); |
| 530 ASSERT_TRUE(buffer.get()); | 530 ASSERT_TRUE(buffer.get()); |
| 531 scoped_refptr<media::VideoFrame> video_frame = | 531 scoped_refptr<media::VideoFrame> video_frame = |
| 532 WrapI420Buffer(capture_resolution, static_cast<uint8_t*>(buffer->data())); | 532 WrapI420Buffer(capture_resolution, static_cast<uint8_t*>(buffer->data())); |
| 533 ASSERT_TRUE(video_frame); | 533 ASSERT_TRUE(video_frame); |
| 534 video_frame->metadata()->SetTimeTicks( | 534 video_frame->metadata()->SetTimeTicks( |
| 535 media::VideoFrameMetadata::REFERENCE_TIME, base::TimeTicks()); | 535 media::VideoFrameMetadata::REFERENCE_TIME, base::TimeTicks()); |
| 536 device_->OnIncomingCapturedVideoFrame(std::move(buffer), video_frame); | 536 device_->OnIncomingCapturedVideoFrame(std::move(buffer), video_frame); |
| 537 | 537 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 555 media::VideoCaptureFormat device_format( | 555 media::VideoCaptureFormat device_format( |
| 556 gfx::Size(10, 10), 25, media::PIXEL_FORMAT_ARGB); | 556 gfx::Size(10, 10), 25, media::PIXEL_FORMAT_ARGB); |
| 557 | 557 |
| 558 // Start the device. Then, before the first buffer, signal an error and | 558 // Start the device. Then, before the first buffer, signal an error and |
| 559 // deliver the buffer. The error should be propagated to clients; the buffer | 559 // deliver the buffer. The error should be propagated to clients; the buffer |
| 560 // should not be. | 560 // should not be. |
| 561 base::RunLoop().RunUntilIdle(); | 561 base::RunLoop().RunUntilIdle(); |
| 562 Mock::VerifyAndClearExpectations(client_a_.get()); | 562 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 563 | 563 |
| 564 const gfx::Size dims(320, 240); | 564 const gfx::Size dims(320, 240); |
| 565 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer( | 565 std::unique_ptr<device::VideoCaptureDevice::Client::Buffer> buffer( |
| 566 device_->ReserveOutputBuffer(dims, media::PIXEL_FORMAT_I420, | 566 device_->ReserveOutputBuffer(dims, media::PIXEL_FORMAT_I420, |
| 567 media::PIXEL_STORAGE_CPU)); | 567 media::PIXEL_STORAGE_CPU)); |
| 568 ASSERT_TRUE(buffer.get()); | 568 ASSERT_TRUE(buffer.get()); |
| 569 | 569 |
| 570 scoped_refptr<media::VideoFrame> video_frame = | 570 scoped_refptr<media::VideoFrame> video_frame = |
| 571 WrapI420Buffer(dims, static_cast<uint8_t*>(buffer->data())); | 571 WrapI420Buffer(dims, static_cast<uint8_t*>(buffer->data())); |
| 572 ASSERT_TRUE(video_frame); | 572 ASSERT_TRUE(video_frame); |
| 573 device_->OnError(FROM_HERE, "Test Error"); | 573 device_->OnError(FROM_HERE, "Test Error"); |
| 574 video_frame->metadata()->SetTimeTicks( | 574 video_frame->metadata()->SetTimeTicks( |
| 575 media::VideoFrameMetadata::REFERENCE_TIME, base::TimeTicks()); | 575 media::VideoFrameMetadata::REFERENCE_TIME, base::TimeTicks()); |
| 576 device_->OnIncomingCapturedVideoFrame(std::move(buffer), video_frame); | 576 device_->OnIncomingCapturedVideoFrame(std::move(buffer), video_frame); |
| 577 | 577 |
| 578 EXPECT_CALL(*client_a_, DoError(route_id)).Times(1); | 578 EXPECT_CALL(*client_a_, DoError(route_id)).Times(1); |
| 579 base::RunLoop().RunUntilIdle(); | 579 base::RunLoop().RunUntilIdle(); |
| 580 Mock::VerifyAndClearExpectations(client_a_.get()); | 580 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 581 | 581 |
| 582 // Second client connects after the error state. It also should get told of | 582 // Second client connects after the error state. It also should get told of |
| 583 // the error. | 583 // the error. |
| 584 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); | 584 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); |
| 585 controller_->AddClient( | 585 controller_->AddClient( |
| 586 route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200); | 586 route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200); |
| 587 Mock::VerifyAndClearExpectations(client_b_.get()); | 587 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 588 } | 588 } |
| 589 | 589 |
| 590 } // namespace content | 590 } // namespace content |
| OLD | NEW |