Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: media/video/capture/fake_video_capture_device_unittest.cc

Issue 294893006: VideoCaptureDeviceFactory: Change device enumeration to callback + QTKit enumerates in UI thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: perkj@s comments Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 19
19 namespace media { 20 namespace media {
20 21
21 class MockClient : public media::VideoCaptureDevice::Client { 22 class MockClient : public media::VideoCaptureDevice::Client {
22 public: 23 public:
23 MOCK_METHOD2(ReserveOutputBuffer, 24 MOCK_METHOD2(ReserveOutputBuffer,
24 scoped_refptr<Buffer>(media::VideoFrame::Format format, 25 scoped_refptr<Buffer>(media::VideoFrame::Format format,
25 const gfx::Size& dimensions)); 26 const gfx::Size& dimensions));
26 MOCK_METHOD0(OnErr, void()); 27 MOCK_METHOD0(OnErr, void());
27 28
(...skipping 18 matching lines...) Expand all
46 const scoped_refptr<media::VideoFrame>& frame, 47 const scoped_refptr<media::VideoFrame>& frame,
47 base::TimeTicks timestamp) OVERRIDE { 48 base::TimeTicks timestamp) OVERRIDE {
48 NOTREACHED(); 49 NOTREACHED();
49 } 50 }
50 51
51 private: 52 private:
52 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; 53 scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
53 base::Callback<void(const VideoCaptureFormat&)> frame_cb_; 54 base::Callback<void(const VideoCaptureFormat&)> frame_cb_;
54 }; 55 };
55 56
57 class DeviceEnumerationListener :
58 public base::RefCounted<DeviceEnumerationListener>{
tommi (sloooow) - chröme 2014/05/30 12:37:51 space before {
mcasas 2014/05/30 14:08:50 Done.
59 public:
60 MOCK_METHOD1(OnEnumeratedDevicesCallback,
61 void(media::VideoCaptureDevice::Names& names));
62
63 void EnumeratedDevicesCallback(media::VideoCaptureDevice::Names& names) {
tommi (sloooow) - chröme 2014/05/30 12:37:51 const
tommi (sloooow) - chröme 2014/05/30 12:37:51 why is this method needed? (add a comment?) should
mcasas 2014/05/30 14:08:50 It isn't, so removed.
mcasas 2014/05/30 14:08:50 const& in the previous MOCK_METHOD.
64 OnEnumeratedDevicesCallback(names);
65 }
66 private:
67 friend class base::RefCounted<DeviceEnumerationListener>;
68 ~DeviceEnumerationListener() {}
tommi (sloooow) - chröme 2014/05/30 12:37:51 virtual?
mcasas 2014/05/30 14:08:50 Done.
69 };
70
56 class FakeVideoCaptureDeviceTest : public testing::Test { 71 class FakeVideoCaptureDeviceTest : public testing::Test {
57 protected: 72 protected:
58 typedef media::VideoCaptureDevice::Client Client; 73 typedef media::VideoCaptureDevice::Client Client;
59 74
60 FakeVideoCaptureDeviceTest() 75 FakeVideoCaptureDeviceTest()
61 : loop_(new base::MessageLoop()), 76 : loop_(new base::MessageLoop()),
62 client_(new MockClient( 77 client_(new MockClient(
63 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured, 78 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured,
64 base::Unretained(this)))), 79 base::Unretained(this)))),
65 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {} 80 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {
81 device_enumeration_listener_ = new DeviceEnumerationListener();
82 }
66 83
67 virtual void SetUp() { 84 virtual void SetUp() {
68 } 85 }
69 86
70 void OnFrameCaptured(const VideoCaptureFormat& format) { 87 void OnFrameCaptured(const VideoCaptureFormat& format) {
71 last_format_ = format; 88 last_format_ = format;
72 run_loop_->QuitClosure().Run(); 89 run_loop_->QuitClosure().Run();
73 } 90 }
74 91
75 void WaitForCapturedFrame() { 92 void WaitForCapturedFrame() {
76 run_loop_.reset(new base::RunLoop()); 93 run_loop_.reset(new base::RunLoop());
77 run_loop_->Run(); 94 run_loop_->Run();
78 } 95 }
79 96
97 void EnumerateDevices(VideoCaptureDevice::Names* device_names) {
98 EXPECT_CALL(*device_enumeration_listener_, OnEnumeratedDevicesCallback(_))
99 .WillOnce(SaveArg<0>(device_names));
tommi (sloooow) - chröme 2014/05/30 12:37:51 indent
mcasas 2014/05/30 14:08:50 Done.
100
101 video_capture_device_factory_->EnumerateDeviceNames(
102 base::Bind(&DeviceEnumerationListener::EnumeratedDevicesCallback,
103 device_enumeration_listener_));
104 base::MessageLoop::current()->RunUntilIdle();
105 }
106
80 const VideoCaptureFormat& last_format() const { return last_format_; } 107 const VideoCaptureFormat& last_format() const { return last_format_; }
81 108
82 VideoCaptureDevice::Names names_; 109 VideoCaptureDevice::Names names_;
83 scoped_ptr<base::MessageLoop> loop_; 110 scoped_ptr<base::MessageLoop> loop_;
84 scoped_ptr<base::RunLoop> run_loop_; 111 scoped_ptr<base::RunLoop> run_loop_;
85 scoped_ptr<MockClient> client_; 112 scoped_ptr<MockClient> client_;
113 scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_;
86 VideoCaptureFormat last_format_; 114 VideoCaptureFormat last_format_;
87 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_; 115 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_;
88 }; 116 };
89 117
90 TEST_F(FakeVideoCaptureDeviceTest, Capture) { 118 TEST_F(FakeVideoCaptureDeviceTest, Capture) {
91 VideoCaptureDevice::Names names; 119 VideoCaptureDevice::Names names;
92 120
93 video_capture_device_factory_->GetDeviceNames(&names); 121 EnumerateDevices(&names);
94 122
95 ASSERT_GT(static_cast<int>(names.size()), 0); 123 ASSERT_GT(static_cast<int>(names.size()), 0);
96 124
97 scoped_ptr<VideoCaptureDevice> device( 125 scoped_ptr<VideoCaptureDevice> device(
98 video_capture_device_factory_->Create( 126 video_capture_device_factory_->Create(names.front()));
99 base::MessageLoopProxy::current(), names.front()));
100 ASSERT_TRUE(device); 127 ASSERT_TRUE(device);
101 128
102 EXPECT_CALL(*client_, OnErr()).Times(0); 129 EXPECT_CALL(*client_, OnErr()).Times(0);
103 130
104 VideoCaptureParams capture_params; 131 VideoCaptureParams capture_params;
105 capture_params.requested_format.frame_size.SetSize(640, 480); 132 capture_params.requested_format.frame_size.SetSize(640, 480);
106 capture_params.requested_format.frame_rate = 30; 133 capture_params.requested_format.frame_rate = 30;
107 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; 134 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420;
108 capture_params.allow_resolution_change = false; 135 capture_params.allow_resolution_change = false;
109 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); 136 device->AllocateAndStart(capture_params, client_.PassAs<Client>());
110 WaitForCapturedFrame(); 137 WaitForCapturedFrame();
111 EXPECT_EQ(last_format().frame_size.width(), 640); 138 EXPECT_EQ(last_format().frame_size.width(), 640);
112 EXPECT_EQ(last_format().frame_size.height(), 480); 139 EXPECT_EQ(last_format().frame_size.height(), 480);
113 EXPECT_EQ(last_format().frame_rate, 30); 140 EXPECT_EQ(last_format().frame_rate, 30);
114 device->StopAndDeAllocate(); 141 device->StopAndDeAllocate();
115 } 142 }
116 143
117 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { 144 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) {
118 VideoCaptureDevice::Names names; 145 VideoCaptureDevice::Names names;
119 video_capture_device_factory_->GetDeviceNames(&names); 146 EnumerateDevices(&names);
120 147
121 VideoCaptureFormats supported_formats; 148 VideoCaptureFormats supported_formats;
122 VideoCaptureDevice::Names::iterator names_iterator; 149 VideoCaptureDevice::Names::iterator names_iterator;
123 150
124 for (names_iterator = names.begin(); names_iterator != names.end(); 151 for (names_iterator = names.begin(); names_iterator != names.end();
125 ++names_iterator) { 152 ++names_iterator) {
126 video_capture_device_factory_->GetDeviceSupportedFormats( 153 video_capture_device_factory_->GetDeviceSupportedFormats(
127 *names_iterator, &supported_formats); 154 *names_iterator, &supported_formats);
128 EXPECT_EQ(supported_formats.size(), 3u); 155 EXPECT_EQ(supported_formats.size(), 3u);
129 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); 156 EXPECT_EQ(supported_formats[0].frame_size.width(), 320);
130 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); 157 EXPECT_EQ(supported_formats[0].frame_size.height(), 240);
131 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420); 158 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420);
132 EXPECT_GE(supported_formats[0].frame_rate, 20); 159 EXPECT_GE(supported_formats[0].frame_rate, 20);
133 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); 160 EXPECT_EQ(supported_formats[1].frame_size.width(), 640);
134 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); 161 EXPECT_EQ(supported_formats[1].frame_size.height(), 480);
135 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420); 162 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420);
136 EXPECT_GE(supported_formats[1].frame_rate, 20); 163 EXPECT_GE(supported_formats[1].frame_rate, 20);
137 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); 164 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280);
138 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); 165 EXPECT_EQ(supported_formats[2].frame_size.height(), 720);
139 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420); 166 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420);
140 EXPECT_GE(supported_formats[2].frame_rate, 20); 167 EXPECT_GE(supported_formats[2].frame_rate, 20);
141 } 168 }
142 } 169 }
143 170
144 TEST_F(FakeVideoCaptureDeviceTest, CaptureVariableResolution) { 171 TEST_F(FakeVideoCaptureDeviceTest, CaptureVariableResolution) {
145 VideoCaptureDevice::Names names; 172 VideoCaptureDevice::Names names;
146 173
147 video_capture_device_factory_->GetDeviceNames(&names); 174 EnumerateDevices(&names);
148 VideoCaptureParams capture_params; 175 VideoCaptureParams capture_params;
149 capture_params.requested_format.frame_size.SetSize(640, 480); 176 capture_params.requested_format.frame_size.SetSize(640, 480);
150 capture_params.requested_format.frame_rate = 30; 177 capture_params.requested_format.frame_rate = 30;
151 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; 178 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420;
152 capture_params.allow_resolution_change = true; 179 capture_params.allow_resolution_change = true;
153 180
154 ASSERT_GT(static_cast<int>(names.size()), 0); 181 ASSERT_GT(static_cast<int>(names.size()), 0);
155 182
156 scoped_ptr<VideoCaptureDevice> device( 183 scoped_ptr<VideoCaptureDevice> device(
157 video_capture_device_factory_->Create( 184 video_capture_device_factory_->Create(names.front()));
158 base::MessageLoopProxy::current(), names.front()));
159 ASSERT_TRUE(device); 185 ASSERT_TRUE(device);
160 186
161 // Configure the FakeVideoCaptureDevice to use all its formats as roster. 187 // Configure the FakeVideoCaptureDevice to use all its formats as roster.
162 VideoCaptureFormats formats; 188 VideoCaptureFormats formats;
163 video_capture_device_factory_->GetDeviceSupportedFormats(names.front(), 189 video_capture_device_factory_->GetDeviceSupportedFormats(names.front(),
164 &formats); 190 &formats);
165 static_cast<FakeVideoCaptureDevice*>(device.get())-> 191 static_cast<FakeVideoCaptureDevice*>(device.get())->
166 PopulateVariableFormatsRoster(formats); 192 PopulateVariableFormatsRoster(formats);
167 193
168 EXPECT_CALL(*client_, OnErr()) 194 EXPECT_CALL(*client_, OnErr())
169 .Times(0); 195 .Times(0);
170 int action_count = 200; 196 int action_count = 200;
171 197
172 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); 198 device->AllocateAndStart(capture_params, client_.PassAs<Client>());
173 199
174 // 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
175 // least action_count/kFakeCaptureCapabilityChangePeriod calls. 201 // least action_count/kFakeCaptureCapabilityChangePeriod calls.
176 for (int i = 0; i < action_count; ++i) { 202 for (int i = 0; i < action_count; ++i) {
177 WaitForCapturedFrame(); 203 WaitForCapturedFrame();
178 } 204 }
179 device->StopAndDeAllocate(); 205 device->StopAndDeAllocate();
180 } 206 }
181 207
182 }; // namespace media 208 }; // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698