| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 mojo::ScopedSharedBufferHandle handle, | 71 mojo::ScopedSharedBufferHandle handle, |
| 72 int length, | 72 int length, |
| 73 int buffer_id) override { | 73 int buffer_id) override { |
| 74 DoBufferCreated(id); | 74 DoBufferCreated(id); |
| 75 } | 75 } |
| 76 void OnBufferDestroyed(VideoCaptureControllerID id, int buffer_id) override { | 76 void OnBufferDestroyed(VideoCaptureControllerID id, int buffer_id) override { |
| 77 DoBufferDestroyed(id); | 77 DoBufferDestroyed(id); |
| 78 } | 78 } |
| 79 void OnBufferReady(VideoCaptureControllerID id, | 79 void OnBufferReady(VideoCaptureControllerID id, |
| 80 int buffer_id, | 80 int buffer_id, |
| 81 const scoped_refptr<media::VideoFrame>& frame) override { | 81 media::mojom::VideoFrameInfoPtr frame_info) override { |
| 82 EXPECT_EQ(expected_pixel_format_, frame->format()); | 82 EXPECT_EQ(expected_pixel_format_, frame_info->pixel_format); |
| 83 media::VideoFrameMetadata metadata; |
| 84 metadata.MergeInternalValuesFrom(*frame_info->metadata); |
| 83 base::TimeTicks reference_time; | 85 base::TimeTicks reference_time; |
| 84 EXPECT_TRUE(frame->metadata()->GetTimeTicks( | 86 EXPECT_TRUE(metadata.GetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, |
| 85 media::VideoFrameMetadata::REFERENCE_TIME, &reference_time)); | 87 &reference_time)); |
| 86 DoBufferReady(id, frame->coded_size()); | 88 DoBufferReady(id, frame_info->coded_size); |
| 87 base::ThreadTaskRunnerHandle::Get()->PostTask( | 89 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 88 FROM_HERE, | 90 FROM_HERE, |
| 89 base::Bind(&VideoCaptureController::ReturnBuffer, | 91 base::Bind(&VideoCaptureController::ReturnBuffer, |
| 90 base::Unretained(controller_), id, this, buffer_id, | 92 base::Unretained(controller_), id, this, buffer_id, |
| 91 gpu::SyncToken(), resource_utilization_)); | 93 gpu::SyncToken(), resource_utilization_)); |
| 92 } | 94 } |
| 93 void OnEnded(VideoCaptureControllerID id) override { | 95 void OnEnded(VideoCaptureControllerID id) override { |
| 94 DoEnded(id); | 96 DoEnded(id); |
| 95 // OnEnded() must respond by (eventually) unregistering the client. | 97 // OnEnded() must respond by (eventually) unregistering the client. |
| 96 base::ThreadTaskRunnerHandle::Get()->PostTask( | 98 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 97 FROM_HERE, | 99 FROM_HERE, |
| 98 base::Bind(base::IgnoreResult(&VideoCaptureController::RemoveClient), | 100 base::Bind(base::IgnoreResult(&VideoCaptureController::RemoveClient), |
| 99 base::Unretained(controller_), id, this)); | 101 base::Unretained(controller_), id, this)); |
| 100 } | 102 } |
| 101 | 103 |
| 102 VideoCaptureController* controller_; | 104 VideoCaptureController* controller_; |
| 103 media::VideoPixelFormat expected_pixel_format_ = media::PIXEL_FORMAT_I420; | 105 media::VideoPixelFormat expected_pixel_format_ = media::PIXEL_FORMAT_I420; |
| 104 double resource_utilization_; | 106 double resource_utilization_; |
| 105 }; | 107 }; |
| 106 | 108 |
| 107 class MockConsumerFeedbackObserver | 109 class MockConsumerFeedbackObserver |
| 108 : public media::VideoFrameConsumerFeedbackObserver { | 110 : public media::VideoFrameConsumerFeedbackObserver { |
| 109 public: | 111 public: |
| 110 MOCK_METHOD2(OnUtilizationReport, | 112 MOCK_METHOD2(OnUtilizationReport, |
| 111 void(int frame_feedback_id, double utilization)); | 113 void(int frame_feedback_id, double utilization)); |
| 112 }; | 114 }; |
| 113 | 115 |
| 114 class MockFrameBufferPool : public media::FrameBufferPool { | |
| 115 public: | |
| 116 MOCK_METHOD1(SetBufferHold, void(int buffer_id)); | |
| 117 MOCK_METHOD1(ReleaseBufferHold, void(int buffer_id)); | |
| 118 }; | |
| 119 | |
| 120 // Test class. | 116 // Test class. |
| 121 class VideoCaptureControllerTest | 117 class VideoCaptureControllerTest |
| 122 : public testing::Test, | 118 : public testing::Test, |
| 123 public testing::WithParamInterface<media::VideoPixelFormat> { | 119 public testing::WithParamInterface<media::VideoPixelFormat> { |
| 124 public: | 120 public: |
| 125 VideoCaptureControllerTest() {} | 121 VideoCaptureControllerTest() {} |
| 126 ~VideoCaptureControllerTest() override {} | 122 ~VideoCaptureControllerTest() override {} |
| 127 | 123 |
| 128 protected: | 124 protected: |
| 129 static const int kPoolSize = 3; | 125 static const int kPoolSize = 3; |
| 130 | 126 |
| 131 void SetUp() override { | 127 void SetUp() override { |
| 132 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool( | 128 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool( |
| 133 new media::VideoCaptureBufferPoolImpl( | 129 new media::VideoCaptureBufferPoolImpl( |
| 134 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), | 130 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), |
| 135 kPoolSize)); | 131 kPoolSize)); |
| 136 controller_.reset(new VideoCaptureController()); | 132 controller_.reset(new VideoCaptureController()); |
| 137 device_client_.reset(new media::VideoCaptureDeviceClient( | 133 device_client_.reset(new media::VideoCaptureDeviceClient( |
| 138 base::MakeUnique<VideoFrameReceiverOnIOThread>( | 134 base::MakeUnique<VideoFrameReceiverOnIOThread>( |
| 139 controller_->GetWeakPtrForIOThread()), | 135 controller_->GetWeakPtrForIOThread()), |
| 140 buffer_pool, | 136 buffer_pool, |
| 141 base::Bind( | 137 base::Bind(&CreateGpuJpegDecoder, |
| 142 &CreateGpuJpegDecoder, | 138 base::Bind(&media::VideoFrameReceiver::OnFrameReadyInBuffer, |
| 143 base::Bind(&media::VideoFrameReceiver::OnIncomingCapturedVideoFrame, | 139 controller_->GetWeakPtrForIOThread())))); |
| 144 controller_->GetWeakPtrForIOThread())))); | |
| 145 auto frame_receiver_observer = base::MakeUnique<MockFrameBufferPool>(); | |
| 146 mock_frame_receiver_observer_ = frame_receiver_observer.get(); | |
| 147 controller_->SetFrameBufferPool(std::move(frame_receiver_observer)); | |
| 148 auto consumer_feedback_observer = | 140 auto consumer_feedback_observer = |
| 149 base::MakeUnique<MockConsumerFeedbackObserver>(); | 141 base::MakeUnique<MockConsumerFeedbackObserver>(); |
| 150 mock_consumer_feedback_observer_ = consumer_feedback_observer.get(); | 142 mock_consumer_feedback_observer_ = consumer_feedback_observer.get(); |
| 151 controller_->SetConsumerFeedbackObserver( | 143 controller_->SetConsumerFeedbackObserver( |
| 152 std::move(consumer_feedback_observer)); | 144 std::move(consumer_feedback_observer)); |
| 153 client_a_.reset(new MockVideoCaptureControllerEventHandler( | 145 client_a_.reset(new MockVideoCaptureControllerEventHandler( |
| 154 controller_.get())); | 146 controller_.get())); |
| 155 client_b_.reset(new MockVideoCaptureControllerEventHandler( | 147 client_b_.reset(new MockVideoCaptureControllerEventHandler( |
| 156 controller_.get())); | 148 controller_.get())); |
| 157 } | 149 } |
| 158 | 150 |
| 159 void TearDown() override { base::RunLoop().RunUntilIdle(); } | 151 void TearDown() override { base::RunLoop().RunUntilIdle(); } |
| 160 | 152 |
| 161 TestBrowserThreadBundle bundle_; | 153 TestBrowserThreadBundle bundle_; |
| 162 std::unique_ptr<MockVideoCaptureControllerEventHandler> client_a_; | 154 std::unique_ptr<MockVideoCaptureControllerEventHandler> client_a_; |
| 163 std::unique_ptr<MockVideoCaptureControllerEventHandler> client_b_; | 155 std::unique_ptr<MockVideoCaptureControllerEventHandler> client_b_; |
| 164 std::unique_ptr<VideoCaptureController> controller_; | 156 std::unique_ptr<VideoCaptureController> controller_; |
| 165 std::unique_ptr<media::VideoCaptureDevice::Client> device_client_; | 157 std::unique_ptr<media::VideoCaptureDevice::Client> device_client_; |
| 166 MockFrameBufferPool* mock_frame_receiver_observer_; | |
| 167 MockConsumerFeedbackObserver* mock_consumer_feedback_observer_; | 158 MockConsumerFeedbackObserver* mock_consumer_feedback_observer_; |
| 168 const float arbitrary_frame_rate_ = 10.0f; | 159 const float arbitrary_frame_rate_ = 10.0f; |
| 169 const base::TimeTicks arbitrary_reference_time_ = base::TimeTicks(); | 160 const base::TimeTicks arbitrary_reference_time_ = base::TimeTicks(); |
| 170 const base::TimeDelta arbitrary_timestamp_ = base::TimeDelta(); | 161 const base::TimeDelta arbitrary_timestamp_ = base::TimeDelta(); |
| 171 | 162 |
| 172 private: | 163 private: |
| 173 DISALLOW_COPY_AND_ASSIGN(VideoCaptureControllerTest); | 164 DISALLOW_COPY_AND_ASSIGN(VideoCaptureControllerTest); |
| 174 }; | 165 }; |
| 175 | 166 |
| 176 // A simple test of VideoCaptureController's ability to add, remove, and keep | 167 // A simple test of VideoCaptureController's ability to add, remove, and keep |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 } | 328 } |
| 338 { | 329 { |
| 339 InSequence s; | 330 InSequence s; |
| 340 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1); | 331 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1); |
| 341 EXPECT_CALL(*client_a_, | 332 EXPECT_CALL(*client_a_, |
| 342 DoBufferReady(client_a_route_2, device_format.frame_size)) | 333 DoBufferReady(client_a_route_2, device_format.frame_size)) |
| 343 .Times(1); | 334 .Times(1); |
| 344 } | 335 } |
| 345 client_a_->resource_utilization_ = 0.5; | 336 client_a_->resource_utilization_ = 0.5; |
| 346 client_b_->resource_utilization_ = -1.0; | 337 client_b_->resource_utilization_ = -1.0; |
| 347 { | 338 // Expect VideoCaptureController to call the load observer with a |
| 348 InSequence s; | 339 // resource utilization of 0.5 (the largest of all reported values). |
| 349 EXPECT_CALL(*mock_frame_receiver_observer_, SetBufferHold(buffer.id())) | 340 EXPECT_CALL(*mock_consumer_feedback_observer_, |
| 350 .Times(1); | 341 OnUtilizationReport(arbitrary_frame_feedback_id, 0.5)) |
| 351 // Expect VideoCaptureController to call the load observer with a | 342 .Times(1); |
| 352 // resource utilization of 0.5 (the largest of all reported values). | |
| 353 EXPECT_CALL(*mock_consumer_feedback_observer_, | |
| 354 OnUtilizationReport(arbitrary_frame_feedback_id, 0.5)) | |
| 355 .Times(1); | |
| 356 EXPECT_CALL(*mock_frame_receiver_observer_, ReleaseBufferHold(buffer.id())) | |
| 357 .Times(1); | |
| 358 } | |
| 359 | 343 |
| 360 device_client_->OnIncomingCapturedBuffer(std::move(buffer), | 344 device_client_->OnIncomingCapturedBuffer(std::move(buffer), |
| 361 device_format, | 345 device_format, |
| 362 arbitrary_reference_time_, | 346 arbitrary_reference_time_, |
| 363 arbitrary_timestamp_); | 347 arbitrary_timestamp_); |
| 364 | 348 |
| 365 base::RunLoop().RunUntilIdle(); | 349 base::RunLoop().RunUntilIdle(); |
| 366 Mock::VerifyAndClearExpectations(client_a_.get()); | 350 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 367 Mock::VerifyAndClearExpectations(client_b_.get()); | 351 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 368 Mock::VerifyAndClearExpectations(mock_consumer_feedback_observer_); | 352 Mock::VerifyAndClearExpectations(mock_consumer_feedback_observer_); |
| 369 Mock::VerifyAndClearExpectations(mock_frame_receiver_observer_); | |
| 370 | 353 |
| 371 // Second buffer which ought to use the same shared memory buffer. In this | 354 // Second buffer which ought to use the same shared memory buffer. In this |
| 372 // case pretend that the Buffer pointer is held by the device for a long | 355 // case pretend that the Buffer pointer is held by the device for a long |
| 373 // delay. This shouldn't affect anything. | 356 // delay. This shouldn't affect anything. |
| 374 const int arbitrary_frame_feedback_id_2 = 102; | 357 const int arbitrary_frame_feedback_id_2 = 102; |
| 375 media::VideoCaptureDevice::Client::Buffer buffer2 = | 358 media::VideoCaptureDevice::Client::Buffer buffer2 = |
| 376 device_client_->ReserveOutputBuffer( | 359 device_client_->ReserveOutputBuffer( |
| 377 device_format.frame_size, device_format.pixel_format, | 360 device_format.frame_size, device_format.pixel_format, |
| 378 device_format.pixel_storage, arbitrary_frame_feedback_id_2); | 361 device_format.pixel_storage, arbitrary_frame_feedback_id_2); |
| 379 ASSERT_TRUE(buffer2.is_valid()); | 362 ASSERT_TRUE(buffer2.is_valid()); |
| 380 auto buffer2_access = buffer2.handle_provider->GetHandleForInProcessAccess(); | 363 auto buffer2_access = buffer2.handle_provider->GetHandleForInProcessAccess(); |
| 381 memset(buffer2_access->data(), buffer_no++, buffer2_access->mapped_size()); | 364 memset(buffer2_access->data(), buffer_no++, buffer2_access->mapped_size()); |
| 382 client_a_->resource_utilization_ = 0.5; | 365 client_a_->resource_utilization_ = 0.5; |
| 383 client_b_->resource_utilization_ = 3.14; | 366 client_b_->resource_utilization_ = 3.14; |
| 384 // Expect VideoCaptureController to call the load observer with a | 367 // Expect VideoCaptureController to call the load observer with a |
| 385 // resource utilization of 3.14 (the largest of all reported values). | 368 // resource utilization of 3.14 (the largest of all reported values). |
| 386 { | 369 EXPECT_CALL(*mock_consumer_feedback_observer_, |
| 387 InSequence s; | 370 OnUtilizationReport(arbitrary_frame_feedback_id_2, 3.14)) |
| 388 EXPECT_CALL(*mock_frame_receiver_observer_, SetBufferHold(buffer2.id())) | 371 .Times(1); |
| 389 .Times(1); | |
| 390 // Expect VideoCaptureController to call the load observer with a | |
| 391 // resource utilization of 3.14 (the largest of all reported values). | |
| 392 EXPECT_CALL(*mock_consumer_feedback_observer_, | |
| 393 OnUtilizationReport(arbitrary_frame_feedback_id_2, 3.14)) | |
| 394 .Times(1); | |
| 395 EXPECT_CALL(*mock_frame_receiver_observer_, ReleaseBufferHold(buffer2.id())) | |
| 396 .Times(1); | |
| 397 } | |
| 398 | 372 |
| 399 device_client_->OnIncomingCapturedBuffer(std::move(buffer2), | 373 device_client_->OnIncomingCapturedBuffer(std::move(buffer2), |
| 400 device_format, | 374 device_format, |
| 401 arbitrary_reference_time_, | 375 arbitrary_reference_time_, |
| 402 arbitrary_timestamp_); | 376 arbitrary_timestamp_); |
| 403 | 377 |
| 404 // The buffer should be delivered to the clients in any order. | 378 // The buffer should be delivered to the clients in any order. |
| 405 { | 379 { |
| 406 InSequence s; | 380 InSequence s; |
| 407 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)).Times(1); | 381 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)).Times(1); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 420 InSequence s; | 394 InSequence s; |
| 421 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1); | 395 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1); |
| 422 EXPECT_CALL(*client_a_, | 396 EXPECT_CALL(*client_a_, |
| 423 DoBufferReady(client_a_route_2, device_format.frame_size)) | 397 DoBufferReady(client_a_route_2, device_format.frame_size)) |
| 424 .Times(1); | 398 .Times(1); |
| 425 } | 399 } |
| 426 base::RunLoop().RunUntilIdle(); | 400 base::RunLoop().RunUntilIdle(); |
| 427 Mock::VerifyAndClearExpectations(client_a_.get()); | 401 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 428 Mock::VerifyAndClearExpectations(client_b_.get()); | 402 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 429 Mock::VerifyAndClearExpectations(mock_consumer_feedback_observer_); | 403 Mock::VerifyAndClearExpectations(mock_consumer_feedback_observer_); |
| 430 Mock::VerifyAndClearExpectations(mock_frame_receiver_observer_); | |
| 431 | 404 |
| 432 // Add a fourth client now that some buffers have come through. | 405 // Add a fourth client now that some buffers have come through. |
| 433 controller_->AddClient(client_b_route_2, | 406 controller_->AddClient(client_b_route_2, |
| 434 client_b_.get(), | 407 client_b_.get(), |
| 435 1, | 408 1, |
| 436 session_1); | 409 session_1); |
| 437 Mock::VerifyAndClearExpectations(client_b_.get()); | 410 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 438 | 411 |
| 439 // Third, fourth, and fifth buffers. Pretend they all arrive at the same time. | 412 // Third, fourth, and fifth buffers. Pretend they all arrive at the same time. |
| 440 for (int i = 0; i < kPoolSize; i++) { | 413 for (int i = 0; i < kPoolSize; i++) { |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 Mock::VerifyAndClearExpectations(client_a_.get()); | 590 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 618 | 591 |
| 619 // Second client connects after the error state. It also should get told of | 592 // Second client connects after the error state. It also should get told of |
| 620 // the error. | 593 // the error. |
| 621 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); | 594 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); |
| 622 controller_->AddClient(route_id, client_b_.get(), 200, session_200); | 595 controller_->AddClient(route_id, client_b_.get(), 200, session_200); |
| 623 Mock::VerifyAndClearExpectations(client_b_.get()); | 596 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 624 } | 597 } |
| 625 | 598 |
| 626 } // namespace content | 599 } // namespace content |
| OLD | NEW |