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

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: merge 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_METHOD2(OnFrameReady,
129 void(const scoped_refptr<media::VideoFrame>&,
130 const media::VideoCaptureFormat&));
131 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState));
132 MOCK_METHOD1(OnDeviceFormatsInUse,
133 void(const media::VideoCaptureFormats&));
134 MOCK_METHOD1(OnDeviceSupportedFormats,
135 void(const media::VideoCaptureFormats&));
136
137 void Init() {
138 video_capture_impl_->Init();
139 }
140
141 void StartCapture(int client_id,
142 const media::VideoCaptureParams& params) {
143 video_capture_impl_->StartCapture(
144 client_id, params,
145 base::Bind(&VideoCaptureImplTest::OnStateUpdate,
146 base::Unretained(this)),
147 base::Bind(&VideoCaptureImplTest::OnFrameReady,
148 base::Unretained(this)));
149 }
150
151 void StopCapture(int client_id) {
152 video_capture_impl_->StopCapture(client_id);
153 }
154
155 void DeInit() {
156 video_capture_impl_->DeInit();
157 }
158
159 void GetDeviceSupportedFormats() {
160 const base::Callback<void(const media::VideoCaptureFormats&)>
161 callback = base::Bind(
162 &VideoCaptureImplTest::OnDeviceSupportedFormats,
163 base::Unretained(this));
164 video_capture_impl_->GetDeviceSupportedFormats(callback);
165 }
166
167 void GetDeviceFormatsInUse() {
168 const base::Callback<void(const media::VideoCaptureFormats&)>
169 callback = base::Bind(
170 &VideoCaptureImplTest::OnDeviceFormatsInUse,
171 base::Unretained(this));
172 video_capture_impl_->GetDeviceFormatsInUse(callback);
173 }
174
124 base::MessageLoop message_loop_; 175 base::MessageLoop message_loop_;
125 base::RunLoop run_loop_;
126 scoped_ptr<ChildProcess> child_process_; 176 scoped_ptr<ChildProcess> child_process_;
127 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; 177 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_;
128 media::VideoCaptureSessionId session_id_; 178 media::VideoCaptureSessionId session_id_;
129 MockVideoCaptureImpl* video_capture_impl_; 179 scoped_ptr<MockVideoCaptureImpl> video_capture_impl_;
130 media::VideoCaptureParams params_small_; 180 media::VideoCaptureParams params_small_;
131 media::VideoCaptureParams params_large_; 181 media::VideoCaptureParams params_large_;
132 182
133 private: 183 private:
134 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); 184 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest);
135 }; 185 };
136 186
137 TEST_F(VideoCaptureImplTest, Simple) { 187 TEST_F(VideoCaptureImplTest, Simple) {
138 // Execute SetCapture() and StopCapture() for one client. 188 // Execute SetCapture() and StopCapture() for one client.
139 scoped_ptr<MockVideoCaptureEventHandler> client( 189 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
140 new MockVideoCaptureEventHandler); 190 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
141 191
142 EXPECT_CALL(*client, OnStarted(_)); 192 Init();
143 EXPECT_CALL(*client, OnStopped(_)); 193 StartCapture(0, params_small_);
144 EXPECT_CALL(*client, OnRemoved(_)); 194 StopCapture(0);
145 195 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 } 196 }
152 197
153 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { 198 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) {
154 // Execute SetCapture() and StopCapture() for 2 clients in sequence. 199 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
155 scoped_ptr<MockVideoCaptureEventHandler> client1( 200 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
156 new MockVideoCaptureEventHandler);
157 scoped_ptr<MockVideoCaptureEventHandler> client2(
158 new MockVideoCaptureEventHandler);
159 201
160 EXPECT_CALL(*client1, OnStarted(_)); 202 Init();
161 EXPECT_CALL(*client1, OnStopped(_)); 203 StartCapture(0, params_small_);
162 EXPECT_CALL(*client1, OnRemoved(_)); 204 StopCapture(0);
163 EXPECT_CALL(*client2, OnStarted(_)); 205 StartCapture(1, params_small_);
164 EXPECT_CALL(*client2, OnStopped(_)); 206 StopCapture(1);
165 EXPECT_CALL(*client2, OnRemoved(_)); 207 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 } 208 }
175 209
176 TEST_F(VideoCaptureImplTest, LargeAndSmall) { 210 TEST_F(VideoCaptureImplTest, LargeAndSmall) {
177 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. 211 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
178 // The large client starts first and stops first. 212 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 213
184 EXPECT_CALL(*client_large, OnStarted(_)); 214 Init();
185 EXPECT_CALL(*client_small, OnStarted(_)); 215 StartCapture(0, params_large_);
186 EXPECT_CALL(*client_large, OnStopped(_)); 216 StopCapture(0);
187 EXPECT_CALL(*client_large, OnRemoved(_)); 217 StartCapture(1, params_small_);
188 EXPECT_CALL(*client_small, OnStopped(_)); 218 StopCapture(1);
189 EXPECT_CALL(*client_small, OnRemoved(_)); 219 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 } 220 }
199 221
200 TEST_F(VideoCaptureImplTest, SmallAndLarge) { 222 TEST_F(VideoCaptureImplTest, SmallAndLarge) {
201 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. 223 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
202 // The small client starts first and stops first. 224 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 225
208 EXPECT_CALL(*client_small, OnStarted(_)); 226 Init();
209 EXPECT_CALL(*client_large, OnStarted(_)); 227 StartCapture(0, params_small_);
210 EXPECT_CALL(*client_small, OnStopped(_)); 228 StopCapture(0);
211 EXPECT_CALL(*client_small, OnRemoved(_)); 229 StartCapture(1, params_large_);
212 EXPECT_CALL(*client_large, OnStopped(_)); 230 StopCapture(1);
213 EXPECT_CALL(*client_large, OnRemoved(_)); 231 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 } 232 }
223 233
224 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the 234 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the
225 // provided callback. 235 // provided callback.
226 TEST_F(VideoCaptureImplTest, GetDeviceFormats) { 236 TEST_F(VideoCaptureImplTest, GetDeviceFormats) {
227 scoped_ptr<MockVideoCaptureEventHandler> client( 237 EXPECT_CALL(*this, OnDeviceSupportedFormats(_));
228 new MockVideoCaptureEventHandler);
229 238
230 EXPECT_CALL(*client, OnDeviceSupportedFormatsEnumerated(_)); 239 Init();
231 240 GetDeviceSupportedFormats();
232 const base::Callback<void(const media::VideoCaptureFormats&)> 241 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 } 242 }
241 243
242 // Check that two requests to GetDeviceSupportedFormats() end up eventually 244 // Check that two requests to GetDeviceSupportedFormats() end up eventually
243 // calling the provided callbacks. 245 // calling the provided callbacks.
244 TEST_F(VideoCaptureImplTest, TwoClientsGetDeviceFormats) { 246 TEST_F(VideoCaptureImplTest, TwoClientsGetDeviceFormats) {
245 scoped_ptr<MockVideoCaptureEventHandler> client1( 247 EXPECT_CALL(*this, OnDeviceSupportedFormats(_)).Times(2);
246 new MockVideoCaptureEventHandler);
247 scoped_ptr<MockVideoCaptureEventHandler> client2(
248 new MockVideoCaptureEventHandler);
249 248
250 EXPECT_CALL(*client1, OnDeviceSupportedFormatsEnumerated(_)); 249 Init();
251 EXPECT_CALL(*client2, OnDeviceSupportedFormatsEnumerated(_)); 250 GetDeviceSupportedFormats();
252 251 GetDeviceSupportedFormats();
253 const base::Callback<void(const media::VideoCaptureFormats&)> 252 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 } 253 }
268 254
269 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the 255 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the
270 // provided callback. 256 // provided callback.
271 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { 257 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) {
272 scoped_ptr<MockVideoCaptureEventHandler> client( 258 EXPECT_CALL(*this, OnDeviceFormatsInUse(_));
273 new MockVideoCaptureEventHandler);
274 259
275 media::VideoCaptureFormats formats_in_use; 260 Init();
276 EXPECT_CALL(*client, OnDeviceFormatsInUseReceived(_)) 261 GetDeviceFormatsInUse();
277 .WillOnce(SaveArg<0>(&formats_in_use)); 262 DeInit();
263 }
278 264
279 const base::Callback<void(const media::VideoCaptureFormats&)> callback = 265 TEST_F(VideoCaptureImplTest, AlreadyStarted) {
280 base::Bind(&MockVideoCaptureEventHandler::OnDeviceFormatsInUseReceived, 266 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
281 base::Unretained(client.get())); 267 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 268
287 EXPECT_TRUE(formats_in_use.empty()); 269 Init();
270 StartCapture(0, params_small_);
271 StartCapture(1, params_large_);
272 StopCapture(0);
273 StopCapture(1);
274 DeInit();
275 DCHECK(video_capture_impl_->capture_params().requested_format
276 .frame_size ==
277 params_small_.requested_format.frame_size);
288 } 278 }
289 279
290 } // namespace content 280 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/video_capture_impl_manager_unittest.cc ('k') | content/renderer/media/video_capture_message_filter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698