| 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" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "content/browser/renderer_host/media/media_stream_provider.h" | 16 #include "content/browser/renderer_host/media/media_stream_provider.h" |
| 17 #include "content/browser/renderer_host/media/video_capture_controller.h" | 17 #include "content/browser/renderer_host/media/video_capture_controller.h" |
| 18 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 18 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
| 19 #include "content/browser/renderer_host/media/video_capture_manager.h" | 19 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 20 #include "content/common/gpu/client/gl_helper.h" | 20 #include "content/common/gpu/client/gl_helper.h" |
| 21 #include "content/common/media/media_stream_options.h" | 21 #include "content/common/media/media_stream_options.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
| 23 #include "gpu/command_buffer/common/mailbox_holder.h" | 23 #include "gpu/command_buffer/common/mailbox_holder.h" |
| 24 #include "media/base/video_capture_types.h" | 24 #include "media/base/video_capture_types.h" |
| 25 #include "media/base/video_frame_metadata.h" |
| 25 #include "media/base/video_util.h" | 26 #include "media/base/video_util.h" |
| 26 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 29 |
| 29 #if !defined(OS_ANDROID) | 30 #if !defined(OS_ANDROID) |
| 30 #include "content/browser/compositor/test/no_transport_image_transport_factory.h
" | 31 #include "content/browser/compositor/test/no_transport_image_transport_factory.h
" |
| 31 #endif | 32 #endif |
| 32 | 33 |
| 33 using ::testing::_; | 34 using ::testing::_; |
| 34 using ::testing::InSequence; | 35 using ::testing::InSequence; |
| 35 using ::testing::Mock; | 36 using ::testing::Mock; |
| 36 using ::testing::SaveArg; | 37 using ::testing::SaveArg; |
| 37 | 38 |
| 38 namespace content { | 39 namespace content { |
| 39 | 40 |
| 40 class MockVideoCaptureControllerEventHandler | 41 class MockVideoCaptureControllerEventHandler |
| 41 : public VideoCaptureControllerEventHandler { | 42 : public VideoCaptureControllerEventHandler { |
| 42 public: | 43 public: |
| 43 explicit MockVideoCaptureControllerEventHandler( | 44 explicit MockVideoCaptureControllerEventHandler( |
| 44 VideoCaptureController* controller) | 45 VideoCaptureController* controller) |
| 45 : controller_(controller) {} | 46 : controller_(controller), |
| 47 resource_utilization_(-1.0) {} |
| 46 ~MockVideoCaptureControllerEventHandler() override {} | 48 ~MockVideoCaptureControllerEventHandler() override {} |
| 47 | 49 |
| 48 // These mock methods are delegated to by our fake implementation of | 50 // These mock methods are delegated to by our fake implementation of |
| 49 // VideoCaptureControllerEventHandler, to be used in EXPECT_CALL(). | 51 // VideoCaptureControllerEventHandler, to be used in EXPECT_CALL(). |
| 50 MOCK_METHOD1(DoBufferCreated, void(VideoCaptureControllerID)); | 52 MOCK_METHOD1(DoBufferCreated, void(VideoCaptureControllerID)); |
| 51 MOCK_METHOD1(DoBufferDestroyed, void(VideoCaptureControllerID)); | 53 MOCK_METHOD1(DoBufferDestroyed, void(VideoCaptureControllerID)); |
| 52 MOCK_METHOD2(DoBufferReady, void(VideoCaptureControllerID, const gfx::Size&)); | 54 MOCK_METHOD2(DoBufferReady, void(VideoCaptureControllerID, const gfx::Size&)); |
| 53 MOCK_METHOD1(DoMailboxBufferReady, void(VideoCaptureControllerID)); | 55 MOCK_METHOD1(DoMailboxBufferReady, void(VideoCaptureControllerID)); |
| 54 MOCK_METHOD1(DoEnded, void(VideoCaptureControllerID)); | 56 MOCK_METHOD1(DoEnded, void(VideoCaptureControllerID)); |
| 55 MOCK_METHOD1(DoError, void(VideoCaptureControllerID)); | 57 MOCK_METHOD1(DoError, void(VideoCaptureControllerID)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 73 const base::TimeTicks& timestamp, | 75 const base::TimeTicks& timestamp, |
| 74 scoped_ptr<base::DictionaryValue> metadata) override { | 76 scoped_ptr<base::DictionaryValue> metadata) override { |
| 75 DoBufferReady(id, coded_size); | 77 DoBufferReady(id, coded_size); |
| 76 base::MessageLoop::current()->PostTask( | 78 base::MessageLoop::current()->PostTask( |
| 77 FROM_HERE, | 79 FROM_HERE, |
| 78 base::Bind(&VideoCaptureController::ReturnBuffer, | 80 base::Bind(&VideoCaptureController::ReturnBuffer, |
| 79 base::Unretained(controller_), | 81 base::Unretained(controller_), |
| 80 id, | 82 id, |
| 81 this, | 83 this, |
| 82 buffer_id, | 84 buffer_id, |
| 83 0)); | 85 0, |
| 86 resource_utilization_)); |
| 84 } | 87 } |
| 85 void OnMailboxBufferReady( | 88 void OnMailboxBufferReady( |
| 86 VideoCaptureControllerID id, | 89 VideoCaptureControllerID id, |
| 87 int buffer_id, | 90 int buffer_id, |
| 88 const gpu::MailboxHolder& mailbox_holder, | 91 const gpu::MailboxHolder& mailbox_holder, |
| 89 const gfx::Size& packed_frame_size, | 92 const gfx::Size& packed_frame_size, |
| 90 const base::TimeTicks& timestamp, | 93 const base::TimeTicks& timestamp, |
| 91 scoped_ptr<base::DictionaryValue> metadata) override { | 94 scoped_ptr<base::DictionaryValue> metadata) override { |
| 92 DoMailboxBufferReady(id); | 95 DoMailboxBufferReady(id); |
| 93 base::MessageLoop::current()->PostTask( | 96 base::MessageLoop::current()->PostTask( |
| 94 FROM_HERE, | 97 FROM_HERE, |
| 95 base::Bind(&VideoCaptureController::ReturnBuffer, | 98 base::Bind(&VideoCaptureController::ReturnBuffer, |
| 96 base::Unretained(controller_), | 99 base::Unretained(controller_), |
| 97 id, | 100 id, |
| 98 this, | 101 this, |
| 99 buffer_id, | 102 buffer_id, |
| 100 mailbox_holder.sync_point)); | 103 mailbox_holder.sync_point, |
| 104 resource_utilization_)); |
| 101 } | 105 } |
| 102 void OnEnded(VideoCaptureControllerID id) override { | 106 void OnEnded(VideoCaptureControllerID id) override { |
| 103 DoEnded(id); | 107 DoEnded(id); |
| 104 // OnEnded() must respond by (eventually) unregistering the client. | 108 // OnEnded() must respond by (eventually) unregistering the client. |
| 105 base::MessageLoop::current()->PostTask(FROM_HERE, | 109 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 106 base::Bind(base::IgnoreResult(&VideoCaptureController::RemoveClient), | 110 base::Bind(base::IgnoreResult(&VideoCaptureController::RemoveClient), |
| 107 base::Unretained(controller_), id, this)); | 111 base::Unretained(controller_), id, this)); |
| 108 } | 112 } |
| 109 | 113 |
| 110 VideoCaptureController* controller_; | 114 VideoCaptureController* controller_; |
| 115 double resource_utilization_; |
| 111 }; | 116 }; |
| 112 | 117 |
| 113 // Test class. | 118 // Test class. |
| 114 class VideoCaptureControllerTest : public testing::Test { | 119 class VideoCaptureControllerTest : public testing::Test { |
| 115 public: | 120 public: |
| 116 VideoCaptureControllerTest() {} | 121 VideoCaptureControllerTest() {} |
| 117 ~VideoCaptureControllerTest() override {} | 122 ~VideoCaptureControllerTest() override {} |
| 118 | 123 |
| 119 protected: | 124 protected: |
| 120 static const int kPoolSize = 3; | 125 static const int kPoolSize = 3; |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_1)).Times(1); | 340 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_1)).Times(1); |
| 336 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1,_)).Times(1); | 341 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1,_)).Times(1); |
| 337 } | 342 } |
| 338 { | 343 { |
| 339 InSequence s; | 344 InSequence s; |
| 340 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1); | 345 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1); |
| 341 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2,_)).Times(1); | 346 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2,_)).Times(1); |
| 342 } | 347 } |
| 343 scoped_refptr<media::VideoFrame> video_frame = | 348 scoped_refptr<media::VideoFrame> video_frame = |
| 344 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer->data())); | 349 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer->data())); |
| 350 ASSERT_FALSE(video_frame->metadata()->HasKey( |
| 351 media::VideoFrameMetadata::RESOURCE_UTILIZATION)); |
| 352 client_a_->resource_utilization_ = 0.5; |
| 353 client_b_->resource_utilization_ = -1.0; |
| 345 device_->OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, | 354 device_->OnIncomingCapturedVideoFrame(buffer.Pass(), video_frame, |
| 346 base::TimeTicks()); | 355 base::TimeTicks()); |
| 347 | 356 |
| 348 base::RunLoop().RunUntilIdle(); | 357 base::RunLoop().RunUntilIdle(); |
| 349 Mock::VerifyAndClearExpectations(client_a_.get()); | 358 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 350 Mock::VerifyAndClearExpectations(client_b_.get()); | 359 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 360 // Expect VideoCaptureController set the metadata in |video_frame| to hold a |
| 361 // resource utilization of 0.5 (the largest of all reported values). |
| 362 double resource_utilization_in_metadata = -1.0; |
| 363 ASSERT_TRUE(video_frame->metadata()->GetDouble( |
| 364 media::VideoFrameMetadata::RESOURCE_UTILIZATION, |
| 365 &resource_utilization_in_metadata)); |
| 366 ASSERT_EQ(0.5, resource_utilization_in_metadata); |
| 351 | 367 |
| 352 // Second buffer which ought to use the same shared memory buffer. In this | 368 // Second buffer which ought to use the same shared memory buffer. In this |
| 353 // case pretend that the Buffer pointer is held by the device for a long | 369 // case pretend that the Buffer pointer is held by the device for a long |
| 354 // delay. This shouldn't affect anything. | 370 // delay. This shouldn't affect anything. |
| 355 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer2 = | 371 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer2 = |
| 356 device_->ReserveOutputBuffer(media::PIXEL_FORMAT_I420, | 372 device_->ReserveOutputBuffer(media::PIXEL_FORMAT_I420, |
| 357 capture_resolution); | 373 capture_resolution); |
| 358 ASSERT_TRUE(buffer2.get()); | 374 ASSERT_TRUE(buffer2.get()); |
| 359 memset(buffer2->data(), buffer_no++, buffer2->size()); | 375 memset(buffer2->data(), buffer_no++, buffer2->size()); |
| 360 video_frame = | 376 video_frame = |
| 361 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer2->data())); | 377 WrapI420Buffer(capture_resolution, static_cast<uint8*>(buffer2->data())); |
| 378 ASSERT_FALSE(video_frame->metadata()->HasKey( |
| 379 media::VideoFrameMetadata::RESOURCE_UTILIZATION)); |
| 380 client_a_->resource_utilization_ = 0.5; |
| 381 client_b_->resource_utilization_ = 3.14; |
| 362 device_->OnIncomingCapturedVideoFrame(buffer2.Pass(), video_frame, | 382 device_->OnIncomingCapturedVideoFrame(buffer2.Pass(), video_frame, |
| 363 base::TimeTicks()); | 383 base::TimeTicks()); |
| 364 | 384 |
| 365 // The buffer should be delivered to the clients in any order. | 385 // The buffer should be delivered to the clients in any order. |
| 366 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1,_)).Times(1); | 386 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1,_)).Times(1); |
| 367 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1,_)).Times(1); | 387 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1,_)).Times(1); |
| 368 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2,_)).Times(1); | 388 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2,_)).Times(1); |
| 369 base::RunLoop().RunUntilIdle(); | 389 base::RunLoop().RunUntilIdle(); |
| 370 Mock::VerifyAndClearExpectations(client_a_.get()); | 390 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 371 Mock::VerifyAndClearExpectations(client_b_.get()); | 391 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 392 // Expect VideoCaptureController set the metadata in |video_frame| to hold a |
| 393 // resource utilization of 3.14 (the largest of all reported values). |
| 394 resource_utilization_in_metadata = -1.0; |
| 395 ASSERT_TRUE(video_frame->metadata()->GetDouble( |
| 396 media::VideoFrameMetadata::RESOURCE_UTILIZATION, |
| 397 &resource_utilization_in_metadata)); |
| 398 ASSERT_EQ(3.14, resource_utilization_in_metadata); |
| 372 | 399 |
| 373 // Add a fourth client now that some buffers have come through. | 400 // Add a fourth client now that some buffers have come through. |
| 374 controller_->AddClient(client_b_route_2, | 401 controller_->AddClient(client_b_route_2, |
| 375 client_b_.get(), | 402 client_b_.get(), |
| 376 base::kNullProcessHandle, | 403 base::kNullProcessHandle, |
| 377 1, | 404 1, |
| 378 session_1); | 405 session_1); |
| 379 Mock::VerifyAndClearExpectations(client_b_.get()); | 406 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 380 | 407 |
| 381 // Third, fourth, and fifth buffers. Pretend they all arrive at the same time. | 408 // Third, fourth, and fifth buffers. Pretend they all arrive at the same time. |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 EXPECT_EQ(coded_size.width(), size_and_rotation.output_resolution.width()); | 725 EXPECT_EQ(coded_size.width(), size_and_rotation.output_resolution.width()); |
| 699 EXPECT_EQ(coded_size.height(), | 726 EXPECT_EQ(coded_size.height(), |
| 700 size_and_rotation.output_resolution.height()); | 727 size_and_rotation.output_resolution.height()); |
| 701 | 728 |
| 702 EXPECT_EQ(kSessionId, controller_->RemoveClient(route_id, client_a_.get())); | 729 EXPECT_EQ(kSessionId, controller_->RemoveClient(route_id, client_a_.get())); |
| 703 Mock::VerifyAndClearExpectations(client_a_.get()); | 730 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 704 } | 731 } |
| 705 } | 732 } |
| 706 | 733 |
| 707 } // namespace content | 734 } // namespace content |
| OLD | NEW |