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

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: QTKit/AVFoundation Factory create of inexistent device and associated unit tests 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> {
59 public:
60 MOCK_METHOD1(OnEnumeratedDevicesCallbackPtr,
61 void(media::VideoCaptureDevice::Names* names));
62 // GMock doesn't support move-only arguments, so we use this forward method.
63 void OnEnumeratedDevicesCallback(
64 scoped_ptr<media::VideoCaptureDevice::Names> names) {
65 OnEnumeratedDevicesCallbackPtr(names.release());
66 }
67
68 private:
69 friend class base::RefCounted<DeviceEnumerationListener>;
70 virtual ~DeviceEnumerationListener() {}
71 };
72
56 class FakeVideoCaptureDeviceTest : public testing::Test { 73 class FakeVideoCaptureDeviceTest : public testing::Test {
57 protected: 74 protected:
58 typedef media::VideoCaptureDevice::Client Client; 75 typedef media::VideoCaptureDevice::Client Client;
59 76
60 FakeVideoCaptureDeviceTest() 77 FakeVideoCaptureDeviceTest()
61 : loop_(new base::MessageLoop()), 78 : loop_(new base::MessageLoop()),
62 client_(new MockClient( 79 client_(new MockClient(
63 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured, 80 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured,
64 base::Unretained(this)))), 81 base::Unretained(this)))),
65 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {} 82 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {
83 device_enumeration_listener_ = new DeviceEnumerationListener();
84 }
66 85
67 virtual void SetUp() { 86 virtual void SetUp() {
68 } 87 }
69 88
70 void OnFrameCaptured(const VideoCaptureFormat& format) { 89 void OnFrameCaptured(const VideoCaptureFormat& format) {
71 last_format_ = format; 90 last_format_ = format;
72 run_loop_->QuitClosure().Run(); 91 run_loop_->QuitClosure().Run();
73 } 92 }
74 93
75 void WaitForCapturedFrame() { 94 void WaitForCapturedFrame() {
76 run_loop_.reset(new base::RunLoop()); 95 run_loop_.reset(new base::RunLoop());
77 run_loop_->Run(); 96 run_loop_->Run();
78 } 97 }
79 98
99 scoped_ptr<media::VideoCaptureDevice::Names> EnumerateDevices() {
100 media::VideoCaptureDevice::Names* names;
101 EXPECT_CALL(*device_enumeration_listener_,
102 OnEnumeratedDevicesCallbackPtr(_)).WillOnce(SaveArg<0>(&names));
103
104 video_capture_device_factory_->EnumerateDeviceNames(
105 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback,
106 device_enumeration_listener_));
107 base::MessageLoop::current()->RunUntilIdle();
108 return scoped_ptr<media::VideoCaptureDevice::Names>(names);
109 }
110
80 const VideoCaptureFormat& last_format() const { return last_format_; } 111 const VideoCaptureFormat& last_format() const { return last_format_; }
81 112
82 VideoCaptureDevice::Names names_; 113 VideoCaptureDevice::Names names_;
83 scoped_ptr<base::MessageLoop> loop_; 114 scoped_ptr<base::MessageLoop> loop_;
84 scoped_ptr<base::RunLoop> run_loop_; 115 scoped_ptr<base::RunLoop> run_loop_;
85 scoped_ptr<MockClient> client_; 116 scoped_ptr<MockClient> client_;
117 scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_;
86 VideoCaptureFormat last_format_; 118 VideoCaptureFormat last_format_;
87 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_; 119 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_;
88 }; 120 };
89 121
90 TEST_F(FakeVideoCaptureDeviceTest, Capture) { 122 TEST_F(FakeVideoCaptureDeviceTest, Capture) {
91 VideoCaptureDevice::Names names; 123 scoped_ptr<media::VideoCaptureDevice::Names> names(EnumerateDevices());
92 124
93 video_capture_device_factory_->GetDeviceNames(&names); 125 ASSERT_GT(static_cast<int>(names->size()), 0);
94
95 ASSERT_GT(static_cast<int>(names.size()), 0);
96 126
97 scoped_ptr<VideoCaptureDevice> device( 127 scoped_ptr<VideoCaptureDevice> device(
98 video_capture_device_factory_->Create( 128 video_capture_device_factory_->Create(names->front()));
99 base::MessageLoopProxy::current(), names.front()));
100 ASSERT_TRUE(device); 129 ASSERT_TRUE(device);
101 130
102 EXPECT_CALL(*client_, OnErr()).Times(0); 131 EXPECT_CALL(*client_, OnErr()).Times(0);
103 132
104 VideoCaptureParams capture_params; 133 VideoCaptureParams capture_params;
105 capture_params.requested_format.frame_size.SetSize(640, 480); 134 capture_params.requested_format.frame_size.SetSize(640, 480);
106 capture_params.requested_format.frame_rate = 30; 135 capture_params.requested_format.frame_rate = 30;
107 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; 136 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420;
108 capture_params.allow_resolution_change = false; 137 capture_params.allow_resolution_change = false;
109 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); 138 device->AllocateAndStart(capture_params, client_.PassAs<Client>());
110 WaitForCapturedFrame(); 139 WaitForCapturedFrame();
111 EXPECT_EQ(last_format().frame_size.width(), 640); 140 EXPECT_EQ(last_format().frame_size.width(), 640);
112 EXPECT_EQ(last_format().frame_size.height(), 480); 141 EXPECT_EQ(last_format().frame_size.height(), 480);
113 EXPECT_EQ(last_format().frame_rate, 30); 142 EXPECT_EQ(last_format().frame_rate, 30);
114 device->StopAndDeAllocate(); 143 device->StopAndDeAllocate();
115 } 144 }
116 145
117 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { 146 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) {
118 VideoCaptureDevice::Names names; 147 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices());
119 video_capture_device_factory_->GetDeviceNames(&names);
120 148
121 VideoCaptureFormats supported_formats; 149 VideoCaptureFormats supported_formats;
122 VideoCaptureDevice::Names::iterator names_iterator; 150 VideoCaptureDevice::Names::iterator names_iterator;
123 151
124 for (names_iterator = names.begin(); names_iterator != names.end(); 152 for (names_iterator = names->begin(); names_iterator != names->end();
125 ++names_iterator) { 153 ++names_iterator) {
126 video_capture_device_factory_->GetDeviceSupportedFormats( 154 video_capture_device_factory_->GetDeviceSupportedFormats(
127 *names_iterator, &supported_formats); 155 *names_iterator, &supported_formats);
128 EXPECT_EQ(supported_formats.size(), 3u); 156 EXPECT_EQ(supported_formats.size(), 3u);
129 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); 157 EXPECT_EQ(supported_formats[0].frame_size.width(), 320);
130 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); 158 EXPECT_EQ(supported_formats[0].frame_size.height(), 240);
131 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420); 159 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420);
132 EXPECT_GE(supported_formats[0].frame_rate, 20); 160 EXPECT_GE(supported_formats[0].frame_rate, 20);
133 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); 161 EXPECT_EQ(supported_formats[1].frame_size.width(), 640);
134 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); 162 EXPECT_EQ(supported_formats[1].frame_size.height(), 480);
135 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420); 163 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420);
136 EXPECT_GE(supported_formats[1].frame_rate, 20); 164 EXPECT_GE(supported_formats[1].frame_rate, 20);
137 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); 165 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280);
138 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); 166 EXPECT_EQ(supported_formats[2].frame_size.height(), 720);
139 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420); 167 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420);
140 EXPECT_GE(supported_formats[2].frame_rate, 20); 168 EXPECT_GE(supported_formats[2].frame_rate, 20);
141 } 169 }
142 } 170 }
143 171
144 TEST_F(FakeVideoCaptureDeviceTest, CaptureVariableResolution) { 172 TEST_F(FakeVideoCaptureDeviceTest, CaptureVariableResolution) {
145 VideoCaptureDevice::Names names; 173 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices());
146 174
147 video_capture_device_factory_->GetDeviceNames(&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