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

Side by Side Diff: content/renderer/media/video_capture_impl_unittest.cc

Issue 242013002: Refactor video capturing code in the render process (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merged again :( Created 6 years, 8 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 (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 #include "base/message_loop/message_loop.h" 5 #include "base/message_loop/message_loop.h"
6 #include "base/run_loop.h"
7 #include "content/child/child_process.h" 6 #include "content/child/child_process.h"
8 #include "content/common/media/video_capture_messages.h" 7 #include "content/common/media/video_capture_messages.h"
9 #include "content/renderer/media/video_capture_impl.h" 8 #include "content/renderer/media/video_capture_impl.h"
10 #include "media/base/bind_to_current_loop.h" 9 #include "media/base/bind_to_current_loop.h"
11 #include "media/video/capture/mock_video_capture_event_handler.h"
12 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
14 12
15 using ::testing::_; 13 using ::testing::_;
16 using ::testing::AtLeast; 14 using ::testing::AtLeast;
17 using ::testing::InvokeWithoutArgs; 15 using ::testing::InvokeWithoutArgs;
18 using ::testing::Return; 16 using ::testing::Return;
19 using ::testing::SaveArg; 17 using ::testing::SaveArg;
20 using media::MockVideoCaptureEventHandler;
21 18
22 namespace content { 19 namespace content {
23 20
24 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter { 21 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter {
25 public: 22 public:
26 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {} 23 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {}
27 24
28 // Filter implementation. 25 // Filter implementation.
29 MOCK_METHOD1(Send, bool(IPC::Message* message)); 26 MOCK_METHOD1(Send, bool(IPC::Message* message));
30 27
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 IPC_MESSAGE_UNHANDLED(handled = false) 62 IPC_MESSAGE_UNHANDLED(handled = false)
66 IPC_END_MESSAGE_MAP() 63 IPC_END_MESSAGE_MAP()
67 EXPECT_TRUE(handled); 64 EXPECT_TRUE(handled);
68 delete message; 65 delete message;
69 } 66 }
70 67
71 void DeviceStartCapture(int device_id, 68 void DeviceStartCapture(int device_id,
72 media::VideoCaptureSessionId session_id, 69 media::VideoCaptureSessionId session_id,
73 const media::VideoCaptureParams& params) { 70 const media::VideoCaptureParams& params) {
74 OnStateChanged(VIDEO_CAPTURE_STATE_STARTED); 71 OnStateChanged(VIDEO_CAPTURE_STATE_STARTED);
72 capture_params_ = params;
75 } 73 }
76 74
77 void DevicePauseCapture(int device_id) {} 75 void DevicePauseCapture(int device_id) {}
78 76
79 void DeviceStopCapture(int device_id) { 77 void DeviceStopCapture(int device_id) {
80 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); 78 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED);
81 } 79 }
82 80
83 void DeviceReceiveEmptyBuffer(int device_id, 81 void DeviceReceiveEmptyBuffer(int device_id,
84 int buffer_id, 82 int buffer_id,
85 uint32 sync_point) {} 83 uint32 sync_point) {}
86 84
87 void DeviceGetSupportedFormats(int device_id, 85 void DeviceGetSupportedFormats(int device_id,
88 media::VideoCaptureSessionId session_id) { 86 media::VideoCaptureSessionId session_id) {
89 // When the mock message filter receives a request for the device 87 // When the mock message filter receives a request for the device
90 // supported formats, replies immediately with an empty format list. 88 // supported formats, replies immediately with an empty format list.
91 OnDeviceSupportedFormatsEnumerated( 89 OnDeviceSupportedFormatsEnumerated(
92 media::VideoCaptureFormats()); 90 media::VideoCaptureFormats());
93 } 91 }
94 92
95 void DeviceGetFormatsInUse(int device_id, 93 void DeviceGetFormatsInUse(int device_id,
96 media::VideoCaptureSessionId session_id) { 94 media::VideoCaptureSessionId session_id) {
97 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats()); 95 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats());
98 } 96 }
97
98 const media::VideoCaptureParams& capture_params() const {
99 return capture_params_;
100 }
101
102 private:
103 media::VideoCaptureParams capture_params_;
99 }; 104 };
100 105
101 VideoCaptureImplTest() { 106 VideoCaptureImplTest() {
102 params_small_.requested_format = media::VideoCaptureFormat( 107 params_small_.requested_format = media::VideoCaptureFormat(
103 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); 108 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420);
104 109
105 params_large_.requested_format = media::VideoCaptureFormat( 110 params_large_.requested_format = media::VideoCaptureFormat(
106 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); 111 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420);
107 112
108 child_process_.reset(new ChildProcess()); 113 child_process_.reset(new ChildProcess());
109 114
110 message_filter_ = new MockVideoCaptureMessageFilter; 115 message_filter_ = new MockVideoCaptureMessageFilter;
111 session_id_ = 1; 116 session_id_ = 1;
112 117
113 video_capture_impl_ = new MockVideoCaptureImpl( 118 video_capture_impl_.reset(new MockVideoCaptureImpl(
114 session_id_, message_filter_.get()); 119 session_id_, message_filter_.get()));
115 120
116 video_capture_impl_->device_id_ = 2; 121 video_capture_impl_->device_id_ = 2;
117 } 122 }
118 123
119 virtual ~VideoCaptureImplTest() { 124 virtual ~VideoCaptureImplTest() {
120 delete video_capture_impl_;
121 } 125 }
122 126
123 protected: 127 protected:
128 MOCK_METHOD3(OnFrameReady,
129 void(const scoped_refptr<media::VideoFrame>&,
130 const media::VideoCaptureFormat&,
131 const base::TimeTicks&));
132 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState));
133 MOCK_METHOD1(OnDeviceFormatsInUse,
134 void(const media::VideoCaptureFormats&));
135 MOCK_METHOD1(OnDeviceSupportedFormats,
136 void(const media::VideoCaptureFormats&));
137
138 void Init() {
139 video_capture_impl_->Init();
140 }
141
142 void StartCapture(int client_id,
143 const media::VideoCaptureParams& params) {
144 video_capture_impl_->StartCapture(
145 client_id, params,
146 base::Bind(&VideoCaptureImplTest::OnStateUpdate,
147 base::Unretained(this)),
148 base::Bind(&VideoCaptureImplTest::OnFrameReady,
149 base::Unretained(this)));
150 }
151
152 void StopCapture(int client_id) {
153 video_capture_impl_->StopCapture(client_id);
154 }
155
156 void DeInit() {
157 video_capture_impl_->DeInit();
158 }
159
160 void GetDeviceSupportedFormats() {
161 const base::Callback<void(const media::VideoCaptureFormats&)>
162 callback = base::Bind(
163 &VideoCaptureImplTest::OnDeviceSupportedFormats,
164 base::Unretained(this));
165 video_capture_impl_->GetDeviceSupportedFormats(callback);
166 }
167
168 void GetDeviceFormatsInUse() {
169 const base::Callback<void(const media::VideoCaptureFormats&)>
170 callback = base::Bind(
171 &VideoCaptureImplTest::OnDeviceFormatsInUse,
172 base::Unretained(this));
173 video_capture_impl_->GetDeviceFormatsInUse(callback);
174 }
175
124 base::MessageLoop message_loop_; 176 base::MessageLoop message_loop_;
125 base::RunLoop run_loop_;
126 scoped_ptr<ChildProcess> child_process_; 177 scoped_ptr<ChildProcess> child_process_;
127 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; 178 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_;
128 media::VideoCaptureSessionId session_id_; 179 media::VideoCaptureSessionId session_id_;
129 MockVideoCaptureImpl* video_capture_impl_; 180 scoped_ptr<MockVideoCaptureImpl> video_capture_impl_;
130 media::VideoCaptureParams params_small_; 181 media::VideoCaptureParams params_small_;
131 media::VideoCaptureParams params_large_; 182 media::VideoCaptureParams params_large_;
132 183
133 private: 184 private:
134 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); 185 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest);
135 }; 186 };
136 187
137 TEST_F(VideoCaptureImplTest, Simple) { 188 TEST_F(VideoCaptureImplTest, Simple) {
138 // Execute SetCapture() and StopCapture() for one client. 189 // Execute SetCapture() and StopCapture() for one client.
139 scoped_ptr<MockVideoCaptureEventHandler> client( 190 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
140 new MockVideoCaptureEventHandler); 191 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
141 192
142 EXPECT_CALL(*client, OnStarted(_)); 193 Init();
143 EXPECT_CALL(*client, OnStopped(_)); 194 StartCapture(0, params_small_);
144 EXPECT_CALL(*client, OnRemoved(_)); 195 StopCapture(0);
145 196 DeInit();
146 video_capture_impl_->StartCapture(client.get(), params_small_);
147 video_capture_impl_->StopCapture(client.get());
148 video_capture_impl_->DeInit(
149 media::BindToCurrentLoop(run_loop_.QuitClosure()));
150 run_loop_.Run();
151 } 197 }
152 198
153 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { 199 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) {
154 // Execute SetCapture() and StopCapture() for 2 clients in sequence. 200 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
155 scoped_ptr<MockVideoCaptureEventHandler> client1( 201 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
156 new MockVideoCaptureEventHandler);
157 scoped_ptr<MockVideoCaptureEventHandler> client2(
158 new MockVideoCaptureEventHandler);
159 202
160 EXPECT_CALL(*client1, OnStarted(_)); 203 Init();
161 EXPECT_CALL(*client1, OnStopped(_)); 204 StartCapture(0, params_small_);
162 EXPECT_CALL(*client1, OnRemoved(_)); 205 StopCapture(0);
163 EXPECT_CALL(*client2, OnStarted(_)); 206 StartCapture(1, params_small_);
164 EXPECT_CALL(*client2, OnStopped(_)); 207 StopCapture(1);
165 EXPECT_CALL(*client2, OnRemoved(_)); 208 DeInit();
166
167 video_capture_impl_->StartCapture(client1.get(), params_small_);
168 video_capture_impl_->StopCapture(client1.get());
169 video_capture_impl_->StartCapture(client2.get(), params_small_);
170 video_capture_impl_->StopCapture(client2.get());
171 video_capture_impl_->DeInit(
172 media::BindToCurrentLoop(run_loop_.QuitClosure()));
173 run_loop_.Run();
174 } 209 }
175 210
176 TEST_F(VideoCaptureImplTest, LargeAndSmall) { 211 TEST_F(VideoCaptureImplTest, LargeAndSmall) {
177 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. 212 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
178 // The large client starts first and stops first. 213 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
179 scoped_ptr<MockVideoCaptureEventHandler> client_small(
180 new MockVideoCaptureEventHandler);
181 scoped_ptr<MockVideoCaptureEventHandler> client_large(
182 new MockVideoCaptureEventHandler);
183 214
184 EXPECT_CALL(*client_large, OnStarted(_)); 215 Init();
185 EXPECT_CALL(*client_small, OnStarted(_)); 216 StartCapture(0, params_large_);
186 EXPECT_CALL(*client_large, OnStopped(_)); 217 StopCapture(0);
187 EXPECT_CALL(*client_large, OnRemoved(_)); 218 StartCapture(1, params_small_);
188 EXPECT_CALL(*client_small, OnStopped(_)); 219 StopCapture(1);
189 EXPECT_CALL(*client_small, OnRemoved(_)); 220 DeInit();
190
191 video_capture_impl_->StartCapture(client_large.get(), params_large_);
192 video_capture_impl_->StartCapture(client_small.get(), params_small_);
193 video_capture_impl_->StopCapture(client_large.get());
194 video_capture_impl_->StopCapture(client_small.get());
195 video_capture_impl_->DeInit(
196 media::BindToCurrentLoop(run_loop_.QuitClosure()));
197 run_loop_.Run();
198 } 221 }
199 222
200 TEST_F(VideoCaptureImplTest, SmallAndLarge) { 223 TEST_F(VideoCaptureImplTest, SmallAndLarge) {
201 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. 224 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
202 // The small client starts first and stops first. 225 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
203 scoped_ptr<MockVideoCaptureEventHandler> client_small(
204 new MockVideoCaptureEventHandler);
205 scoped_ptr<MockVideoCaptureEventHandler> client_large(
206 new MockVideoCaptureEventHandler);
207 226
208 EXPECT_CALL(*client_small, OnStarted(_)); 227 Init();
209 EXPECT_CALL(*client_large, OnStarted(_)); 228 StartCapture(0, params_small_);
210 EXPECT_CALL(*client_small, OnStopped(_)); 229 StopCapture(0);
211 EXPECT_CALL(*client_small, OnRemoved(_)); 230 StartCapture(1, params_large_);
212 EXPECT_CALL(*client_large, OnStopped(_)); 231 StopCapture(1);
213 EXPECT_CALL(*client_large, OnRemoved(_)); 232 DeInit();
214
215 video_capture_impl_->StartCapture(client_small.get(), params_small_);
216 video_capture_impl_->StartCapture(client_large.get(), params_large_);
217 video_capture_impl_->StopCapture(client_small.get());
218 video_capture_impl_->StopCapture(client_large.get());
219 video_capture_impl_->DeInit(
220 media::BindToCurrentLoop(run_loop_.QuitClosure()));
221 run_loop_.Run();
222 } 233 }
223 234
224 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the 235 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the
225 // provided callback. 236 // provided callback.
226 TEST_F(VideoCaptureImplTest, GetDeviceFormats) { 237 TEST_F(VideoCaptureImplTest, GetDeviceFormats) {
227 scoped_ptr<MockVideoCaptureEventHandler> client( 238 EXPECT_CALL(*this, OnDeviceSupportedFormats(_));
228 new MockVideoCaptureEventHandler);
229 239
230 EXPECT_CALL(*client, OnDeviceSupportedFormatsEnumerated(_)); 240 Init();
231 241 GetDeviceSupportedFormats();
232 const base::Callback<void(const media::VideoCaptureFormats&)> 242 DeInit();
233 callback = base::Bind(
234 &MockVideoCaptureEventHandler::OnDeviceSupportedFormatsEnumerated,
235 base::Unretained(client.get()));
236 video_capture_impl_->GetDeviceSupportedFormats(callback);
237 video_capture_impl_->DeInit(
238 media::BindToCurrentLoop(run_loop_.QuitClosure()));
239 run_loop_.Run();
240 } 243 }
241 244
242 // Check that two requests to GetDeviceSupportedFormats() end up eventually 245 // Check that two requests to GetDeviceSupportedFormats() end up eventually
243 // calling the provided callbacks. 246 // calling the provided callbacks.
244 TEST_F(VideoCaptureImplTest, TwoClientsGetDeviceFormats) { 247 TEST_F(VideoCaptureImplTest, TwoClientsGetDeviceFormats) {
245 scoped_ptr<MockVideoCaptureEventHandler> client1( 248 EXPECT_CALL(*this, OnDeviceSupportedFormats(_)).Times(2);
246 new MockVideoCaptureEventHandler);
247 scoped_ptr<MockVideoCaptureEventHandler> client2(
248 new MockVideoCaptureEventHandler);
249 249
250 EXPECT_CALL(*client1, OnDeviceSupportedFormatsEnumerated(_)); 250 Init();
251 EXPECT_CALL(*client2, OnDeviceSupportedFormatsEnumerated(_)); 251 GetDeviceSupportedFormats();
252 252 GetDeviceSupportedFormats();
253 const base::Callback<void(const media::VideoCaptureFormats&)> 253 DeInit();
254 callback1 = base::Bind(
255 &MockVideoCaptureEventHandler::OnDeviceSupportedFormatsEnumerated,
256 base::Unretained(client1.get()));
257 const base::Callback<void(const media::VideoCaptureFormats&)>
258 callback2 = base::Bind(
259 &MockVideoCaptureEventHandler::OnDeviceSupportedFormatsEnumerated,
260 base::Unretained(client2.get()));
261
262 video_capture_impl_->GetDeviceSupportedFormats(callback1);
263 video_capture_impl_->GetDeviceSupportedFormats(callback2);
264 video_capture_impl_->DeInit(
265 media::BindToCurrentLoop(run_loop_.QuitClosure()));
266 run_loop_.Run();
267 } 254 }
268 255
269 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the 256 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the
270 // provided callback. 257 // provided callback.
271 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { 258 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) {
272 scoped_ptr<MockVideoCaptureEventHandler> client( 259 EXPECT_CALL(*this, OnDeviceFormatsInUse(_));
273 new MockVideoCaptureEventHandler);
274 260
275 media::VideoCaptureFormats formats_in_use; 261 Init();
276 EXPECT_CALL(*client, OnDeviceFormatsInUseReceived(_)) 262 GetDeviceFormatsInUse();
277 .WillOnce(SaveArg<0>(&formats_in_use)); 263 DeInit();
264 }
278 265
279 const base::Callback<void(const media::VideoCaptureFormats&)> callback = 266 TEST_F(VideoCaptureImplTest, AlreadyStarted) {
280 base::Bind(&MockVideoCaptureEventHandler::OnDeviceFormatsInUseReceived, 267 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
281 base::Unretained(client.get())); 268 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
282 video_capture_impl_->GetDeviceFormatsInUse(callback);
283 video_capture_impl_->DeInit(
284 media::BindToCurrentLoop(run_loop_.QuitClosure()));
285 run_loop_.Run();
286 269
287 EXPECT_TRUE(formats_in_use.empty()); 270 Init();
271 StartCapture(0, params_small_);
272 StartCapture(1, params_large_);
273 StopCapture(0);
274 StopCapture(1);
275 DeInit();
276 DCHECK(video_capture_impl_->capture_params().requested_format
277 .frame_size ==
278 params_small_.requested_format.frame_size);
288 } 279 }
289 280
290 } // namespace content 281 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698