Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
| 9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
| 10 #include "media/video/capture/fake_video_capture_device.h" | 10 #include "media/video/capture/fake_video_capture_device.h" |
| 11 #include "media/video/capture/fake_video_capture_device_factory.h" | 11 #include "media/video/capture/fake_video_capture_device_factory.h" |
| 12 #include "media/video/capture/video_capture_device.h" | 12 #include "media/video/capture/video_capture_device.h" |
| 13 #include "media/video/capture/video_capture_types.h" | 13 #include "media/video/capture/video_capture_types.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using ::testing::_; | 17 using ::testing::_; |
| 18 using ::testing::SaveArg; | 18 using ::testing::SaveArg; |
| 19 | 19 |
| 20 namespace media { | 20 namespace media { |
| 21 | 21 |
| 22 class MockClient : public media::VideoCaptureDevice::Client { | 22 namespace { |
|
mcasas
2015/02/09 23:41:05
I'm not 100% sure making an anonymous namespace
in
Lei Zhang
2015/02/10 00:04:15
Since nothing in this file is intended to be share
Lei Zhang
2015/02/10 20:23:06
I've done this in patch set 3. I'll land this CL t
| |
| 23 | |
| 24 class MockClient : public VideoCaptureDevice::Client { | |
| 23 public: | 25 public: |
| 24 MOCK_METHOD2(ReserveOutputBuffer, | 26 MOCK_METHOD2(ReserveOutputBuffer, |
| 25 scoped_refptr<Buffer>(media::VideoFrame::Format format, | 27 scoped_refptr<Buffer>(VideoFrame::Format format, |
| 26 const gfx::Size& dimensions)); | 28 const gfx::Size& dimensions)); |
| 27 MOCK_METHOD0(OnErr, void()); | 29 MOCK_METHOD0(OnErr, void()); |
| 28 | 30 |
| 29 explicit MockClient(base::Callback<void(const VideoCaptureFormat&)> frame_cb) | 31 explicit MockClient(base::Callback<void(const VideoCaptureFormat&)> frame_cb) |
| 30 : main_thread_(base::MessageLoopProxy::current()), frame_cb_(frame_cb) {} | 32 : main_thread_(base::MessageLoopProxy::current()), frame_cb_(frame_cb) {} |
| 31 | 33 |
| 32 virtual void OnError(const std::string& error_message) override { | 34 void OnError(const std::string& error_message) override { |
| 33 OnErr(); | 35 OnErr(); |
| 34 } | 36 } |
| 35 | 37 |
| 36 virtual void OnIncomingCapturedData(const uint8* data, | 38 void OnIncomingCapturedData(const uint8* data, |
| 37 int length, | 39 int length, |
| 38 const VideoCaptureFormat& format, | 40 const VideoCaptureFormat& format, |
| 39 int rotation, | 41 int rotation, |
| 40 base::TimeTicks timestamp) override { | 42 base::TimeTicks timestamp) override { |
| 41 main_thread_->PostTask(FROM_HERE, base::Bind(frame_cb_, format)); | 43 main_thread_->PostTask(FROM_HERE, base::Bind(frame_cb_, format)); |
| 42 } | 44 } |
| 43 | 45 |
| 44 virtual void OnIncomingCapturedVideoFrame( | 46 void OnIncomingCapturedVideoFrame(const scoped_refptr<Buffer>& buffer, |
| 45 const scoped_refptr<Buffer>& buffer, | 47 const VideoCaptureFormat& buffer_format, |
| 46 const media::VideoCaptureFormat& buffer_format, | 48 const scoped_refptr<VideoFrame>& frame, |
| 47 const scoped_refptr<media::VideoFrame>& frame, | 49 base::TimeTicks timestamp) override { |
| 48 base::TimeTicks timestamp) override { | |
| 49 NOTREACHED(); | 50 NOTREACHED(); |
| 50 } | 51 } |
| 51 | 52 |
| 52 private: | 53 private: |
| 53 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; | 54 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; |
| 54 base::Callback<void(const VideoCaptureFormat&)> frame_cb_; | 55 base::Callback<void(const VideoCaptureFormat&)> frame_cb_; |
| 55 }; | 56 }; |
| 56 | 57 |
| 57 class DeviceEnumerationListener : | 58 class DeviceEnumerationListener : |
| 58 public base::RefCounted<DeviceEnumerationListener> { | 59 public base::RefCounted<DeviceEnumerationListener> { |
| 59 public: | 60 public: |
| 60 MOCK_METHOD1(OnEnumeratedDevicesCallbackPtr, | 61 MOCK_METHOD1(OnEnumeratedDevicesCallbackPtr, |
| 61 void(media::VideoCaptureDevice::Names* names)); | 62 void(VideoCaptureDevice::Names* names)); |
| 62 // GMock doesn't support move-only arguments, so we use this forward method. | 63 // GMock doesn't support move-only arguments, so we use this forward method. |
| 63 void OnEnumeratedDevicesCallback( | 64 void OnEnumeratedDevicesCallback( |
| 64 scoped_ptr<media::VideoCaptureDevice::Names> names) { | 65 scoped_ptr<VideoCaptureDevice::Names> names) { |
| 65 OnEnumeratedDevicesCallbackPtr(names.release()); | 66 OnEnumeratedDevicesCallbackPtr(names.release()); |
| 66 } | 67 } |
| 67 | 68 |
| 68 private: | 69 private: |
| 69 friend class base::RefCounted<DeviceEnumerationListener>; | 70 friend class base::RefCounted<DeviceEnumerationListener>; |
| 70 virtual ~DeviceEnumerationListener() {} | 71 virtual ~DeviceEnumerationListener() {} |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 class FakeVideoCaptureDeviceTest : public testing::Test { | 74 class FakeVideoCaptureDeviceTest : public testing::Test { |
| 74 protected: | 75 protected: |
| 75 typedef media::VideoCaptureDevice::Client Client; | 76 typedef VideoCaptureDevice::Client Client; |
| 76 | 77 |
| 77 FakeVideoCaptureDeviceTest() | 78 FakeVideoCaptureDeviceTest() |
| 78 : loop_(new base::MessageLoop()), | 79 : loop_(new base::MessageLoop()), |
| 79 client_(new MockClient( | 80 client_(new MockClient( |
| 80 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured, | 81 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured, |
| 81 base::Unretained(this)))), | 82 base::Unretained(this)))), |
| 82 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) { | 83 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) { |
| 83 device_enumeration_listener_ = new DeviceEnumerationListener(); | 84 device_enumeration_listener_ = new DeviceEnumerationListener(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 void SetUp() override {} | |
| 87 | |
| 88 void OnFrameCaptured(const VideoCaptureFormat& format) { | 87 void OnFrameCaptured(const VideoCaptureFormat& format) { |
| 89 last_format_ = format; | 88 last_format_ = format; |
| 90 run_loop_->QuitClosure().Run(); | 89 run_loop_->QuitClosure().Run(); |
| 91 } | 90 } |
| 92 | 91 |
| 93 void WaitForCapturedFrame() { | 92 void WaitForCapturedFrame() { |
| 94 run_loop_.reset(new base::RunLoop()); | 93 run_loop_.reset(new base::RunLoop()); |
| 95 run_loop_->Run(); | 94 run_loop_->Run(); |
| 96 } | 95 } |
| 97 | 96 |
| 98 scoped_ptr<media::VideoCaptureDevice::Names> EnumerateDevices() { | 97 scoped_ptr<VideoCaptureDevice::Names> EnumerateDevices() { |
| 99 media::VideoCaptureDevice::Names* names; | 98 VideoCaptureDevice::Names* names; |
| 100 EXPECT_CALL(*device_enumeration_listener_.get(), | 99 EXPECT_CALL(*device_enumeration_listener_.get(), |
| 101 OnEnumeratedDevicesCallbackPtr(_)).WillOnce(SaveArg<0>(&names)); | 100 OnEnumeratedDevicesCallbackPtr(_)).WillOnce(SaveArg<0>(&names)); |
| 102 | 101 |
| 103 video_capture_device_factory_->EnumerateDeviceNames( | 102 video_capture_device_factory_->EnumerateDeviceNames( |
| 104 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback, | 103 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback, |
| 105 device_enumeration_listener_)); | 104 device_enumeration_listener_)); |
| 106 base::MessageLoop::current()->RunUntilIdle(); | 105 base::MessageLoop::current()->RunUntilIdle(); |
| 107 return scoped_ptr<media::VideoCaptureDevice::Names>(names); | 106 return scoped_ptr<VideoCaptureDevice::Names>(names); |
| 108 } | 107 } |
| 109 | 108 |
| 110 const VideoCaptureFormat& last_format() const { return last_format_; } | 109 const VideoCaptureFormat& last_format() const { return last_format_; } |
| 111 | 110 |
| 112 VideoCaptureDevice::Names names_; | 111 VideoCaptureDevice::Names names_; |
| 113 scoped_ptr<base::MessageLoop> loop_; | 112 scoped_ptr<base::MessageLoop> loop_; |
| 114 scoped_ptr<base::RunLoop> run_loop_; | 113 scoped_ptr<base::RunLoop> run_loop_; |
| 115 scoped_ptr<MockClient> client_; | 114 scoped_ptr<MockClient> client_; |
| 116 scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; | 115 scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; |
| 117 VideoCaptureFormat last_format_; | 116 VideoCaptureFormat last_format_; |
| 118 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_; | 117 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_; |
| 119 }; | 118 }; |
| 120 | 119 |
| 121 TEST_F(FakeVideoCaptureDeviceTest, Capture) { | 120 TEST_F(FakeVideoCaptureDeviceTest, Capture) { |
| 122 scoped_ptr<media::VideoCaptureDevice::Names> names(EnumerateDevices()); | 121 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); |
| 123 | 122 |
| 124 ASSERT_GT(static_cast<int>(names->size()), 0); | 123 ASSERT_FALSE(names->empty()); |
| 125 | 124 |
| 126 scoped_ptr<VideoCaptureDevice> device( | 125 scoped_ptr<VideoCaptureDevice> device( |
| 127 video_capture_device_factory_->Create(names->front())); | 126 video_capture_device_factory_->Create(names->front())); |
| 128 ASSERT_TRUE(device); | 127 ASSERT_TRUE(device); |
| 129 | 128 |
| 130 EXPECT_CALL(*client_, OnErr()).Times(0); | 129 EXPECT_CALL(*client_, OnErr()).Times(0); |
| 131 | 130 |
| 132 VideoCaptureParams capture_params; | 131 VideoCaptureParams capture_params; |
| 133 capture_params.requested_format.frame_size.SetSize(640, 480); | 132 capture_params.requested_format.frame_size.SetSize(640, 480); |
| 134 capture_params.requested_format.frame_rate = 30; | 133 capture_params.requested_format.frame_rate = 30; |
| 135 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 134 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 136 device->AllocateAndStart(capture_params, client_.Pass()); | 135 device->AllocateAndStart(capture_params, client_.Pass()); |
| 137 WaitForCapturedFrame(); | 136 WaitForCapturedFrame(); |
| 138 EXPECT_EQ(last_format().frame_size.width(), 640); | 137 EXPECT_EQ(last_format().frame_size.width(), 640); |
| 139 EXPECT_EQ(last_format().frame_size.height(), 480); | 138 EXPECT_EQ(last_format().frame_size.height(), 480); |
| 140 EXPECT_EQ(last_format().frame_rate, 30); | 139 EXPECT_EQ(last_format().frame_rate, 30); |
| 141 device->StopAndDeAllocate(); | 140 device->StopAndDeAllocate(); |
| 142 } | 141 } |
| 143 | 142 |
| 144 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { | 143 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { |
| 145 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); | 144 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); |
| 146 | 145 |
| 147 VideoCaptureFormats supported_formats; | 146 VideoCaptureFormats supported_formats; |
| 148 VideoCaptureDevice::Names::iterator names_iterator; | |
| 149 | 147 |
| 150 for (names_iterator = names->begin(); names_iterator != names->end(); | 148 for (const auto& names_iterator : *names) { |
|
mcasas
2015/02/09 23:41:05
Oh yeah!
| |
| 151 ++names_iterator) { | |
| 152 video_capture_device_factory_->GetDeviceSupportedFormats( | 149 video_capture_device_factory_->GetDeviceSupportedFormats( |
| 153 *names_iterator, &supported_formats); | 150 names_iterator, &supported_formats); |
| 154 EXPECT_EQ(supported_formats.size(), 4u); | 151 ASSERT_EQ(supported_formats.size(), 4u); |
|
mcasas
2015/02/09 23:41:05
Shouldn't this still be EXPECT_EQ? Why change it?
Lei Zhang
2015/02/10 00:04:15
No, it shouldn't. Below you are accessing |support
mcasas
2015/02/10 20:38:36
Gotcha, right.
| |
| 155 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); | 152 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); |
| 156 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); | 153 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); |
| 157 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420); | 154 EXPECT_EQ(supported_formats[0].pixel_format, PIXEL_FORMAT_I420); |
| 158 EXPECT_GE(supported_formats[0].frame_rate, 20); | 155 EXPECT_GE(supported_formats[0].frame_rate, 20); |
| 159 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); | 156 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); |
| 160 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); | 157 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); |
| 161 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420); | 158 EXPECT_EQ(supported_formats[1].pixel_format, PIXEL_FORMAT_I420); |
| 162 EXPECT_GE(supported_formats[1].frame_rate, 20); | 159 EXPECT_GE(supported_formats[1].frame_rate, 20); |
| 163 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); | 160 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); |
| 164 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); | 161 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); |
| 165 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420); | 162 EXPECT_EQ(supported_formats[2].pixel_format, PIXEL_FORMAT_I420); |
| 166 EXPECT_GE(supported_formats[2].frame_rate, 20); | 163 EXPECT_GE(supported_formats[2].frame_rate, 20); |
| 167 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); | 164 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); |
| 168 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); | 165 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); |
| 169 EXPECT_EQ(supported_formats[3].pixel_format, media::PIXEL_FORMAT_I420); | 166 EXPECT_EQ(supported_formats[3].pixel_format, PIXEL_FORMAT_I420); |
| 170 EXPECT_GE(supported_formats[3].frame_rate, 20); | 167 EXPECT_GE(supported_formats[3].frame_rate, 20); |
| 171 } | 168 } |
| 172 } | 169 } |
| 173 | 170 |
| 174 // Disabled, http://crbug.com/407061 . | 171 // Disabled, http://crbug.com/407061 . |
| 175 TEST_F(FakeVideoCaptureDeviceTest, DISABLED_CaptureVariableResolution) { | 172 TEST_F(FakeVideoCaptureDeviceTest, DISABLED_CaptureVariableResolution) { |
| 176 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); | 173 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); |
| 177 | 174 |
| 178 VideoCaptureParams capture_params; | 175 VideoCaptureParams capture_params; |
| 179 capture_params.requested_format.frame_size.SetSize(640, 480); | 176 capture_params.requested_format.frame_size.SetSize(640, 480); |
| 180 capture_params.requested_format.frame_rate = 30; | 177 capture_params.requested_format.frame_rate = 30; |
| 181 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 178 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
| 182 capture_params.resolution_change_policy = | 179 capture_params.resolution_change_policy = |
| 183 RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT; | 180 RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT; |
| 184 | 181 |
| 185 ASSERT_GT(static_cast<int>(names->size()), 0); | 182 ASSERT_FALSE(names->empty()); |
| 186 | 183 |
| 187 scoped_ptr<VideoCaptureDevice> device( | 184 scoped_ptr<VideoCaptureDevice> device( |
| 188 video_capture_device_factory_->Create(names->front())); | 185 video_capture_device_factory_->Create(names->front())); |
| 189 ASSERT_TRUE(device); | 186 ASSERT_TRUE(device); |
| 190 | 187 |
| 191 // Configure the FakeVideoCaptureDevice to use all its formats as roster. | 188 // Configure the FakeVideoCaptureDevice to use all its formats as roster. |
| 192 VideoCaptureFormats formats; | 189 VideoCaptureFormats formats; |
| 193 video_capture_device_factory_->GetDeviceSupportedFormats(names->front(), | 190 video_capture_device_factory_->GetDeviceSupportedFormats(names->front(), |
| 194 &formats); | 191 &formats); |
| 195 static_cast<FakeVideoCaptureDevice*>(device.get())-> | 192 static_cast<FakeVideoCaptureDevice*>(device.get())-> |
| 196 PopulateVariableFormatsRoster(formats); | 193 PopulateVariableFormatsRoster(formats); |
| 197 | 194 |
| 198 EXPECT_CALL(*client_, OnErr()) | 195 EXPECT_CALL(*client_, OnErr()).Times(0); |
| 199 .Times(0); | |
| 200 int action_count = 200; | 196 int action_count = 200; |
| 201 | 197 |
| 202 device->AllocateAndStart(capture_params, client_.Pass()); | 198 device->AllocateAndStart(capture_params, client_.Pass()); |
| 203 | 199 |
| 204 // We set TimeWait to 200 action timeouts and this should be enough for at | 200 // We set TimeWait to 200 action timeouts and this should be enough for at |
| 205 // least action_count/kFakeCaptureCapabilityChangePeriod calls. | 201 // least action_count/kFakeCaptureCapabilityChangePeriod calls. |
| 206 for (int i = 0; i < action_count; ++i) { | 202 for (int i = 0; i < action_count; ++i) { |
| 207 WaitForCapturedFrame(); | 203 WaitForCapturedFrame(); |
| 208 } | 204 } |
| 209 device->StopAndDeAllocate(); | 205 device->StopAndDeAllocate(); |
| 210 } | 206 } |
| 211 | 207 |
| 208 } // namespace | |
| 209 | |
| 212 }; // namespace media | 210 }; // namespace media |
| OLD | NEW |