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