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

Side by Side Diff: talk/media/base/videocapturer_unittest.cc

Issue 1587193006: Move talk/media to webrtc/media (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebased to b647aca12a884a13c1728118586245399b55fa3d (#11493) Created 4 years, 10 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
« no previous file with comments | « talk/media/base/videocapturer.cc ('k') | talk/media/base/videocapturerfactory.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * libjingle
3 * Copyright 2008 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <stdio.h>
29 #include <vector>
30
31 #include "talk/media/base/fakevideocapturer.h"
32 #include "talk/media/base/fakevideorenderer.h"
33 #include "talk/media/base/testutils.h"
34 #include "talk/media/base/videocapturer.h"
35 #include "webrtc/base/gunit.h"
36 #include "webrtc/base/logging.h"
37 #include "webrtc/base/thread.h"
38
39 using cricket::FakeVideoCapturer;
40
41 namespace {
42
43 const int kMsCallbackWait = 500;
44 // For HD only the height matters.
45 const int kMinHdHeight = 720;
46 const uint32_t kTimeout = 5000U;
47
48 } // namespace
49
50 class VideoCapturerTest
51 : public sigslot::has_slots<>,
52 public testing::Test {
53 public:
54 VideoCapturerTest()
55 : capture_state_(cricket::CS_STOPPED),
56 num_state_changes_(0),
57 video_frames_received_(0),
58 expects_rotation_applied_(true) {
59 capturer_.SignalVideoFrame.connect(this, &VideoCapturerTest::OnVideoFrame);
60 capturer_.SignalStateChange.connect(this,
61 &VideoCapturerTest::OnStateChange);
62 }
63
64 void set_expected_compensation(bool compensation) {
65 expects_rotation_applied_ = compensation;
66 }
67
68 protected:
69 void OnVideoFrame(cricket::VideoCapturer*, const cricket::VideoFrame* frame) {
70 ++video_frames_received_;
71 if (expects_rotation_applied_) {
72 EXPECT_EQ(webrtc::kVideoRotation_0, frame->GetRotation());
73 } else {
74 EXPECT_EQ(capturer_.GetRotation(), frame->GetRotation());
75 }
76 renderer_.RenderFrame(frame);
77 }
78 void OnStateChange(cricket::VideoCapturer*,
79 cricket::CaptureState capture_state) {
80 capture_state_ = capture_state;
81 ++num_state_changes_;
82 }
83 cricket::CaptureState capture_state() { return capture_state_; }
84 int num_state_changes() { return num_state_changes_; }
85 int video_frames_received() const {
86 return video_frames_received_;
87 }
88
89 cricket::FakeVideoCapturer capturer_;
90 cricket::CaptureState capture_state_;
91 int num_state_changes_;
92 int video_frames_received_;
93 cricket::FakeVideoRenderer renderer_;
94 bool expects_rotation_applied_;
95 };
96
97 TEST_F(VideoCapturerTest, CaptureState) {
98 EXPECT_TRUE(capturer_.enable_video_adapter());
99 EXPECT_EQ(cricket::CS_RUNNING, capturer_.Start(cricket::VideoFormat(
100 640,
101 480,
102 cricket::VideoFormat::FpsToInterval(30),
103 cricket::FOURCC_I420)));
104 EXPECT_TRUE(capturer_.IsRunning());
105 EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
106 EXPECT_EQ(1, num_state_changes());
107 capturer_.Stop();
108 EXPECT_EQ_WAIT(cricket::CS_STOPPED, capture_state(), kMsCallbackWait);
109 EXPECT_EQ(2, num_state_changes());
110 capturer_.Stop();
111 rtc::Thread::Current()->ProcessMessages(100);
112 EXPECT_EQ(2, num_state_changes());
113 }
114
115 TEST_F(VideoCapturerTest, TestRestart) {
116 EXPECT_EQ(cricket::CS_RUNNING, capturer_.Start(cricket::VideoFormat(
117 640,
118 480,
119 cricket::VideoFormat::FpsToInterval(30),
120 cricket::FOURCC_I420)));
121 EXPECT_TRUE(capturer_.IsRunning());
122 EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
123 EXPECT_EQ(1, num_state_changes());
124 EXPECT_TRUE(capturer_.Restart(cricket::VideoFormat(
125 320,
126 240,
127 cricket::VideoFormat::FpsToInterval(30),
128 cricket::FOURCC_I420)));
129 EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
130 EXPECT_TRUE(capturer_.IsRunning());
131 EXPECT_GE(1, num_state_changes());
132 capturer_.Stop();
133 rtc::Thread::Current()->ProcessMessages(100);
134 EXPECT_FALSE(capturer_.IsRunning());
135 }
136
137 TEST_F(VideoCapturerTest, TestStartingWithRestart) {
138 EXPECT_FALSE(capturer_.IsRunning());
139 EXPECT_TRUE(capturer_.Restart(cricket::VideoFormat(
140 640,
141 480,
142 cricket::VideoFormat::FpsToInterval(30),
143 cricket::FOURCC_I420)));
144 EXPECT_TRUE(capturer_.IsRunning());
145 EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
146 }
147
148 TEST_F(VideoCapturerTest, TestRestartWithSameFormat) {
149 cricket::VideoFormat format(640, 480,
150 cricket::VideoFormat::FpsToInterval(30),
151 cricket::FOURCC_I420);
152 EXPECT_EQ(cricket::CS_RUNNING, capturer_.Start(format));
153 EXPECT_TRUE(capturer_.IsRunning());
154 EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
155 EXPECT_EQ(1, num_state_changes());
156 EXPECT_TRUE(capturer_.Restart(format));
157 EXPECT_EQ(cricket::CS_RUNNING, capture_state());
158 EXPECT_TRUE(capturer_.IsRunning());
159 EXPECT_EQ(1, num_state_changes());
160 }
161
162 TEST_F(VideoCapturerTest, CameraOffOnMute) {
163 EXPECT_EQ(cricket::CS_RUNNING, capturer_.Start(cricket::VideoFormat(
164 640,
165 480,
166 cricket::VideoFormat::FpsToInterval(30),
167 cricket::FOURCC_I420)));
168 EXPECT_TRUE(capturer_.IsRunning());
169 EXPECT_EQ(0, video_frames_received());
170 EXPECT_TRUE(capturer_.CaptureFrame());
171 EXPECT_EQ(1, video_frames_received());
172 EXPECT_FALSE(capturer_.IsMuted());
173
174 // Mute the camera and expect black output frame.
175 capturer_.MuteToBlackThenPause(true);
176 EXPECT_TRUE(capturer_.IsMuted());
177 for (int i = 0; i < 31; ++i) {
178 EXPECT_TRUE(capturer_.CaptureFrame());
179 EXPECT_TRUE(renderer_.black_frame());
180 }
181 EXPECT_EQ(32, video_frames_received());
182 EXPECT_EQ_WAIT(cricket::CS_PAUSED,
183 capturer_.capture_state(), kTimeout);
184
185 // Verify that the camera is off.
186 EXPECT_FALSE(capturer_.CaptureFrame());
187 EXPECT_EQ(32, video_frames_received());
188
189 // Unmute the camera and expect non-black output frame.
190 capturer_.MuteToBlackThenPause(false);
191 EXPECT_FALSE(capturer_.IsMuted());
192 EXPECT_EQ_WAIT(cricket::CS_RUNNING,
193 capturer_.capture_state(), kTimeout);
194 EXPECT_TRUE(capturer_.CaptureFrame());
195 EXPECT_FALSE(renderer_.black_frame());
196 EXPECT_EQ(33, video_frames_received());
197 }
198
199 TEST_F(VideoCapturerTest, ScreencastScaledOddWidth) {
200 capturer_.SetScreencast(true);
201
202 int kWidth = 1281;
203 int kHeight = 720;
204
205 std::vector<cricket::VideoFormat> formats;
206 formats.push_back(cricket::VideoFormat(kWidth, kHeight,
207 cricket::VideoFormat::FpsToInterval(5), cricket::FOURCC_ARGB));
208 capturer_.ResetSupportedFormats(formats);
209
210 EXPECT_EQ(cricket::CS_RUNNING, capturer_.Start(cricket::VideoFormat(
211 kWidth,
212 kHeight,
213 cricket::VideoFormat::FpsToInterval(30),
214 cricket::FOURCC_ARGB)));
215 EXPECT_TRUE(capturer_.IsRunning());
216 EXPECT_EQ(0, renderer_.num_rendered_frames());
217 EXPECT_TRUE(capturer_.CaptureFrame());
218 EXPECT_EQ(1, renderer_.num_rendered_frames());
219 EXPECT_EQ(kWidth, renderer_.width());
220 EXPECT_EQ(kHeight, renderer_.height());
221 }
222
223 TEST_F(VideoCapturerTest, TestRotationPending) {
224 int kWidth = 800;
225 int kHeight = 400;
226 int frame_count = 0;
227
228 std::vector<cricket::VideoFormat> formats;
229 formats.push_back(cricket::VideoFormat(kWidth, kHeight,
230 cricket::VideoFormat::FpsToInterval(5),
231 cricket::FOURCC_I420));
232
233 capturer_.ResetSupportedFormats(formats);
234 // capturer_ should compensate rotation as default.
235 capturer_.UpdateAspectRatio(400, 200);
236
237 EXPECT_EQ(cricket::CS_RUNNING,
238 capturer_.Start(cricket::VideoFormat(
239 kWidth, kHeight, cricket::VideoFormat::FpsToInterval(30),
240 cricket::FOURCC_I420)));
241 EXPECT_TRUE(capturer_.IsRunning());
242 EXPECT_EQ(0, renderer_.num_rendered_frames());
243
244 // If the frame's rotation is compensated anywhere in the pipeline based on
245 // the rotation information, the renderer should be given the right dimension
246 // such that the frame could be rendered.
247
248 capturer_.SetRotation(webrtc::kVideoRotation_90);
249 EXPECT_TRUE(capturer_.CaptureFrame());
250 EXPECT_EQ(++frame_count, renderer_.num_rendered_frames());
251 // Swapped width and height
252 EXPECT_EQ(kWidth, renderer_.height());
253 EXPECT_EQ(kHeight, renderer_.width());
254
255 capturer_.SetRotation(webrtc::kVideoRotation_270);
256 EXPECT_TRUE(capturer_.CaptureFrame());
257 EXPECT_EQ(++frame_count, renderer_.num_rendered_frames());
258 // Swapped width and height
259 EXPECT_EQ(kWidth, renderer_.height());
260 EXPECT_EQ(kHeight, renderer_.width());
261
262 capturer_.SetRotation(webrtc::kVideoRotation_180);
263 EXPECT_TRUE(capturer_.CaptureFrame());
264 EXPECT_EQ(++frame_count, renderer_.num_rendered_frames());
265 // Back to normal width and height
266 EXPECT_EQ(kWidth, renderer_.width());
267 EXPECT_EQ(kHeight, renderer_.height());
268 }
269
270 TEST_F(VideoCapturerTest, TestRotationApplied) {
271 int kWidth = 800;
272 int kHeight = 400;
273
274 std::vector<cricket::VideoFormat> formats;
275 formats.push_back(cricket::VideoFormat(kWidth, kHeight,
276 cricket::VideoFormat::FpsToInterval(5),
277 cricket::FOURCC_I420));
278
279 capturer_.ResetSupportedFormats(formats);
280 // capturer_ should not compensate rotation.
281 capturer_.SetApplyRotation(false);
282 capturer_.UpdateAspectRatio(400, 200);
283 set_expected_compensation(false);
284
285 EXPECT_EQ(cricket::CS_RUNNING,
286 capturer_.Start(cricket::VideoFormat(
287 kWidth, kHeight, cricket::VideoFormat::FpsToInterval(30),
288 cricket::FOURCC_I420)));
289 EXPECT_TRUE(capturer_.IsRunning());
290 EXPECT_EQ(0, renderer_.num_rendered_frames());
291
292 // If the frame's rotation is compensated anywhere in the pipeline, the frame
293 // won't have its original dimension out from capturer. Since the renderer
294 // here has the same dimension as the capturer, it will skip that frame as the
295 // resolution won't match anymore.
296
297 int frame_count = 0;
298 capturer_.SetRotation(webrtc::kVideoRotation_0);
299 EXPECT_TRUE(capturer_.CaptureFrame());
300 EXPECT_EQ(++frame_count, renderer_.num_rendered_frames());
301
302 capturer_.SetRotation(webrtc::kVideoRotation_90);
303 EXPECT_TRUE(capturer_.CaptureFrame());
304 EXPECT_EQ(++frame_count, renderer_.num_rendered_frames());
305
306 capturer_.SetRotation(webrtc::kVideoRotation_180);
307 EXPECT_TRUE(capturer_.CaptureFrame());
308 EXPECT_EQ(++frame_count, renderer_.num_rendered_frames());
309
310 capturer_.SetRotation(webrtc::kVideoRotation_270);
311 EXPECT_TRUE(capturer_.CaptureFrame());
312 EXPECT_EQ(++frame_count, renderer_.num_rendered_frames());
313 }
314
315 TEST_F(VideoCapturerTest, ScreencastScaledSuperLarge) {
316 capturer_.SetScreencast(true);
317
318 const int kMaxWidth = 4096;
319 const int kMaxHeight = 3072;
320 int kWidth = kMaxWidth + 4;
321 int kHeight = kMaxHeight + 4;
322
323 std::vector<cricket::VideoFormat> formats;
324 formats.push_back(cricket::VideoFormat(kWidth, kHeight,
325 cricket::VideoFormat::FpsToInterval(5), cricket::FOURCC_ARGB));
326 capturer_.ResetSupportedFormats(formats);
327
328 EXPECT_EQ(cricket::CS_RUNNING, capturer_.Start(cricket::VideoFormat(
329 kWidth,
330 kHeight,
331 cricket::VideoFormat::FpsToInterval(30),
332 cricket::FOURCC_ARGB)));
333 EXPECT_TRUE(capturer_.IsRunning());
334 EXPECT_EQ(0, renderer_.num_rendered_frames());
335 EXPECT_TRUE(capturer_.CaptureFrame());
336 EXPECT_EQ(1, renderer_.num_rendered_frames());
337 EXPECT_EQ(kWidth / 2, renderer_.width());
338 EXPECT_EQ(kHeight / 2, renderer_.height());
339 }
340
341 TEST_F(VideoCapturerTest, TestFourccMatch) {
342 cricket::VideoFormat desired(640, 480,
343 cricket::VideoFormat::FpsToInterval(30),
344 cricket::FOURCC_ANY);
345 cricket::VideoFormat best;
346 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
347 EXPECT_EQ(640, best.width);
348 EXPECT_EQ(480, best.height);
349 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
350
351 desired.fourcc = cricket::FOURCC_MJPG;
352 EXPECT_FALSE(capturer_.GetBestCaptureFormat(desired, &best));
353
354 desired.fourcc = cricket::FOURCC_I420;
355 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
356 }
357
358 TEST_F(VideoCapturerTest, TestResolutionMatch) {
359 cricket::VideoFormat desired(1920, 1080,
360 cricket::VideoFormat::FpsToInterval(30),
361 cricket::FOURCC_ANY);
362 cricket::VideoFormat best;
363 // Ask for 1920x1080. Get HD 1280x720 which is the highest.
364 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
365 EXPECT_EQ(1280, best.width);
366 EXPECT_EQ(720, best.height);
367 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
368
369 desired.width = 360;
370 desired.height = 250;
371 // Ask for a little higher than QVGA. Get QVGA.
372 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
373 EXPECT_EQ(320, best.width);
374 EXPECT_EQ(240, best.height);
375 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
376
377 desired.width = 480;
378 desired.height = 270;
379 // Ask for HVGA. Get VGA.
380 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
381 EXPECT_EQ(640, best.width);
382 EXPECT_EQ(480, best.height);
383 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
384
385 desired.width = 320;
386 desired.height = 240;
387 // Ask for QVGA. Get QVGA.
388 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
389 EXPECT_EQ(320, best.width);
390 EXPECT_EQ(240, best.height);
391 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
392
393 desired.width = 80;
394 desired.height = 60;
395 // Ask for lower than QQVGA. Get QQVGA, which is the lowest.
396 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
397 EXPECT_EQ(160, best.width);
398 EXPECT_EQ(120, best.height);
399 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
400 }
401
402 TEST_F(VideoCapturerTest, TestHDResolutionMatch) {
403 // Add some HD formats typical of a mediocre HD webcam.
404 std::vector<cricket::VideoFormat> formats;
405 formats.push_back(cricket::VideoFormat(320, 240,
406 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
407 formats.push_back(cricket::VideoFormat(640, 480,
408 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
409 formats.push_back(cricket::VideoFormat(960, 544,
410 cricket::VideoFormat::FpsToInterval(24), cricket::FOURCC_I420));
411 formats.push_back(cricket::VideoFormat(1280, 720,
412 cricket::VideoFormat::FpsToInterval(15), cricket::FOURCC_I420));
413 formats.push_back(cricket::VideoFormat(2592, 1944,
414 cricket::VideoFormat::FpsToInterval(7), cricket::FOURCC_I420));
415 capturer_.ResetSupportedFormats(formats);
416
417 cricket::VideoFormat desired(960, 720,
418 cricket::VideoFormat::FpsToInterval(30),
419 cricket::FOURCC_ANY);
420 cricket::VideoFormat best;
421 // Ask for 960x720 30 fps. Get qHD 24 fps
422 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
423 EXPECT_EQ(960, best.width);
424 EXPECT_EQ(544, best.height);
425 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(24), best.interval);
426
427 desired.width = 960;
428 desired.height = 544;
429 desired.interval = cricket::VideoFormat::FpsToInterval(30);
430 // Ask for qHD 30 fps. Get qHD 24 fps
431 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
432 EXPECT_EQ(960, best.width);
433 EXPECT_EQ(544, best.height);
434 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(24), best.interval);
435
436 desired.width = 360;
437 desired.height = 250;
438 desired.interval = cricket::VideoFormat::FpsToInterval(30);
439 // Ask for a little higher than QVGA. Get QVGA.
440 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
441 EXPECT_EQ(320, best.width);
442 EXPECT_EQ(240, best.height);
443 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
444
445 desired.width = 480;
446 desired.height = 270;
447 // Ask for HVGA. Get VGA.
448 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
449 EXPECT_EQ(640, best.width);
450 EXPECT_EQ(480, best.height);
451 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
452
453 desired.width = 320;
454 desired.height = 240;
455 // Ask for QVGA. Get QVGA.
456 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
457 EXPECT_EQ(320, best.width);
458 EXPECT_EQ(240, best.height);
459 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
460
461 desired.width = 160;
462 desired.height = 120;
463 // Ask for lower than QVGA. Get QVGA, which is the lowest.
464 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
465 EXPECT_EQ(320, best.width);
466 EXPECT_EQ(240, best.height);
467 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
468
469 desired.width = 1280;
470 desired.height = 720;
471 // Ask for HD. 720p fps is too low. Get VGA which has 30 fps.
472 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
473 EXPECT_EQ(640, best.width);
474 EXPECT_EQ(480, best.height);
475 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
476
477 desired.width = 1280;
478 desired.height = 720;
479 desired.interval = cricket::VideoFormat::FpsToInterval(15);
480 // Ask for HD 15 fps. Fps matches. Get HD
481 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
482 EXPECT_EQ(1280, best.width);
483 EXPECT_EQ(720, best.height);
484 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(15), best.interval);
485
486 desired.width = 1920;
487 desired.height = 1080;
488 desired.interval = cricket::VideoFormat::FpsToInterval(30);
489 // Ask for 1080p. Fps of HD formats is too low. Get VGA which can do 30 fps.
490 EXPECT_TRUE(capturer_.GetBestCaptureFormat(desired, &best));
491 EXPECT_EQ(640, best.width);
492 EXPECT_EQ(480, best.height);
493 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
494 }
495
496 // Some cameras support 320x240 and 320x640. Verify we choose 320x240.
497 TEST_F(VideoCapturerTest, TestStrangeFormats) {
498 std::vector<cricket::VideoFormat> supported_formats;
499 supported_formats.push_back(cricket::VideoFormat(320, 240,
500 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
501 supported_formats.push_back(cricket::VideoFormat(320, 640,
502 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
503 capturer_.ResetSupportedFormats(supported_formats);
504
505 std::vector<cricket::VideoFormat> required_formats;
506 required_formats.push_back(cricket::VideoFormat(320, 240,
507 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
508 required_formats.push_back(cricket::VideoFormat(320, 200,
509 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
510 required_formats.push_back(cricket::VideoFormat(320, 180,
511 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
512 cricket::VideoFormat best;
513 for (size_t i = 0; i < required_formats.size(); ++i) {
514 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[i], &best));
515 EXPECT_EQ(320, best.width);
516 EXPECT_EQ(240, best.height);
517 }
518
519 supported_formats.clear();
520 supported_formats.push_back(cricket::VideoFormat(320, 640,
521 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
522 supported_formats.push_back(cricket::VideoFormat(320, 240,
523 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
524 capturer_.ResetSupportedFormats(supported_formats);
525
526 for (size_t i = 0; i < required_formats.size(); ++i) {
527 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[i], &best));
528 EXPECT_EQ(320, best.width);
529 EXPECT_EQ(240, best.height);
530 }
531 }
532
533 // Some cameras only have very low fps. Verify we choose something sensible.
534 TEST_F(VideoCapturerTest, TestPoorFpsFormats) {
535 // all formats are low framerate
536 std::vector<cricket::VideoFormat> supported_formats;
537 supported_formats.push_back(cricket::VideoFormat(320, 240,
538 cricket::VideoFormat::FpsToInterval(10), cricket::FOURCC_I420));
539 supported_formats.push_back(cricket::VideoFormat(640, 480,
540 cricket::VideoFormat::FpsToInterval(7), cricket::FOURCC_I420));
541 supported_formats.push_back(cricket::VideoFormat(1280, 720,
542 cricket::VideoFormat::FpsToInterval(2), cricket::FOURCC_I420));
543 capturer_.ResetSupportedFormats(supported_formats);
544
545 std::vector<cricket::VideoFormat> required_formats;
546 required_formats.push_back(cricket::VideoFormat(320, 240,
547 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
548 required_formats.push_back(cricket::VideoFormat(640, 480,
549 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
550 cricket::VideoFormat best;
551 for (size_t i = 0; i < required_formats.size(); ++i) {
552 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[i], &best));
553 EXPECT_EQ(required_formats[i].width, best.width);
554 EXPECT_EQ(required_formats[i].height, best.height);
555 }
556
557 // Increase framerate of 320x240. Expect low fps VGA avoided.
558 supported_formats.clear();
559 supported_formats.push_back(cricket::VideoFormat(320, 240,
560 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
561 supported_formats.push_back(cricket::VideoFormat(640, 480,
562 cricket::VideoFormat::FpsToInterval(7), cricket::FOURCC_I420));
563 supported_formats.push_back(cricket::VideoFormat(1280, 720,
564 cricket::VideoFormat::FpsToInterval(2), cricket::FOURCC_I420));
565 capturer_.ResetSupportedFormats(supported_formats);
566
567 for (size_t i = 0; i < required_formats.size(); ++i) {
568 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[i], &best));
569 EXPECT_EQ(320, best.width);
570 EXPECT_EQ(240, best.height);
571 }
572 }
573
574 // Some cameras support same size with different frame rates. Verify we choose
575 // the frame rate properly.
576 TEST_F(VideoCapturerTest, TestSameSizeDifferentFpsFormats) {
577 std::vector<cricket::VideoFormat> supported_formats;
578 supported_formats.push_back(cricket::VideoFormat(320, 240,
579 cricket::VideoFormat::FpsToInterval(10), cricket::FOURCC_I420));
580 supported_formats.push_back(cricket::VideoFormat(320, 240,
581 cricket::VideoFormat::FpsToInterval(20), cricket::FOURCC_I420));
582 supported_formats.push_back(cricket::VideoFormat(320, 240,
583 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
584 capturer_.ResetSupportedFormats(supported_formats);
585
586 std::vector<cricket::VideoFormat> required_formats = supported_formats;
587 cricket::VideoFormat best;
588 for (size_t i = 0; i < required_formats.size(); ++i) {
589 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[i], &best));
590 EXPECT_EQ(320, best.width);
591 EXPECT_EQ(240, best.height);
592 EXPECT_EQ(required_formats[i].interval, best.interval);
593 }
594 }
595
596 // Some cameras support the correct resolution but at a lower fps than
597 // we'd like. This tests we get the expected resolution and fps.
598 TEST_F(VideoCapturerTest, TestFpsFormats) {
599 // We have VGA but low fps. Choose VGA, not HD
600 std::vector<cricket::VideoFormat> supported_formats;
601 supported_formats.push_back(cricket::VideoFormat(1280, 720,
602 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
603 supported_formats.push_back(cricket::VideoFormat(640, 480,
604 cricket::VideoFormat::FpsToInterval(15), cricket::FOURCC_I420));
605 supported_formats.push_back(cricket::VideoFormat(640, 400,
606 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
607 supported_formats.push_back(cricket::VideoFormat(640, 360,
608 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
609 capturer_.ResetSupportedFormats(supported_formats);
610
611 std::vector<cricket::VideoFormat> required_formats;
612 required_formats.push_back(cricket::VideoFormat(640, 480,
613 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_ANY));
614 required_formats.push_back(cricket::VideoFormat(640, 480,
615 cricket::VideoFormat::FpsToInterval(20), cricket::FOURCC_ANY));
616 required_formats.push_back(cricket::VideoFormat(640, 480,
617 cricket::VideoFormat::FpsToInterval(10), cricket::FOURCC_ANY));
618 cricket::VideoFormat best;
619
620 // Expect 30 fps to choose 30 fps format.
621 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[0], &best));
622 EXPECT_EQ(640, best.width);
623 EXPECT_EQ(400, best.height);
624 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
625
626 // Expect 20 fps to choose 30 fps format.
627 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[1], &best));
628 EXPECT_EQ(640, best.width);
629 EXPECT_EQ(400, best.height);
630 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), best.interval);
631
632 // Expect 10 fps to choose 15 fps format and set fps to 15.
633 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[2], &best));
634 EXPECT_EQ(640, best.width);
635 EXPECT_EQ(480, best.height);
636 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(15), best.interval);
637
638 // We have VGA 60 fps and 15 fps. Choose best fps.
639 supported_formats.clear();
640 supported_formats.push_back(cricket::VideoFormat(1280, 720,
641 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
642 supported_formats.push_back(cricket::VideoFormat(640, 480,
643 cricket::VideoFormat::FpsToInterval(60), cricket::FOURCC_MJPG));
644 supported_formats.push_back(cricket::VideoFormat(640, 480,
645 cricket::VideoFormat::FpsToInterval(15), cricket::FOURCC_I420));
646 supported_formats.push_back(cricket::VideoFormat(640, 400,
647 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
648 supported_formats.push_back(cricket::VideoFormat(640, 360,
649 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
650 capturer_.ResetSupportedFormats(supported_formats);
651
652 // Expect 30 fps to choose 60 fps format and will set best fps to 60.
653 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[0], &best));
654 EXPECT_EQ(640, best.width);
655 EXPECT_EQ(480, best.height);
656 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(60), best.interval);
657
658 // Expect 20 fps to choose 60 fps format, and will set best fps to 60.
659 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[1], &best));
660 EXPECT_EQ(640, best.width);
661 EXPECT_EQ(480, best.height);
662 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(60), best.interval);
663
664 // Expect 10 fps to choose 15 fps.
665 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[2], &best));
666 EXPECT_EQ(640, best.width);
667 EXPECT_EQ(480, best.height);
668 EXPECT_EQ(cricket::VideoFormat::FpsToInterval(15), best.interval);
669 }
670
671 TEST_F(VideoCapturerTest, TestRequest16x10_9) {
672 std::vector<cricket::VideoFormat> supported_formats;
673 // We do not support HD, expect 4x3 for 4x3, 16x10, and 16x9 requests.
674 supported_formats.push_back(cricket::VideoFormat(640, 480,
675 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
676 supported_formats.push_back(cricket::VideoFormat(640, 400,
677 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
678 supported_formats.push_back(cricket::VideoFormat(640, 360,
679 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
680 capturer_.ResetSupportedFormats(supported_formats);
681
682 std::vector<cricket::VideoFormat> required_formats = supported_formats;
683 cricket::VideoFormat best;
684 // Expect 4x3, 16x10, and 16x9 requests are respected.
685 for (size_t i = 0; i < required_formats.size(); ++i) {
686 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[i], &best));
687 EXPECT_EQ(required_formats[i].width, best.width);
688 EXPECT_EQ(required_formats[i].height, best.height);
689 }
690
691 // We do not support 16x9 HD, expect 4x3 for 4x3, 16x10, and 16x9 requests.
692 supported_formats.clear();
693 supported_formats.push_back(cricket::VideoFormat(960, 720,
694 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
695 supported_formats.push_back(cricket::VideoFormat(640, 480,
696 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
697 supported_formats.push_back(cricket::VideoFormat(640, 400,
698 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
699 supported_formats.push_back(cricket::VideoFormat(640, 360,
700 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
701 capturer_.ResetSupportedFormats(supported_formats);
702
703 // Expect 4x3, 16x10, and 16x9 requests are respected.
704 for (size_t i = 0; i < required_formats.size(); ++i) {
705 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[i], &best));
706 EXPECT_EQ(required_formats[i].width, best.width);
707 EXPECT_EQ(required_formats[i].height, best.height);
708 }
709
710 // We support 16x9HD, Expect 4x3, 16x10, and 16x9 requests are respected.
711 supported_formats.clear();
712 supported_formats.push_back(cricket::VideoFormat(1280, 720,
713 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
714 supported_formats.push_back(cricket::VideoFormat(640, 480,
715 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
716 supported_formats.push_back(cricket::VideoFormat(640, 400,
717 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
718 supported_formats.push_back(cricket::VideoFormat(640, 360,
719 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
720 capturer_.ResetSupportedFormats(supported_formats);
721
722 // Expect 4x3 for 4x3 and 16x10 requests.
723 for (size_t i = 0; i < required_formats.size() - 1; ++i) {
724 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[i], &best));
725 EXPECT_EQ(required_formats[i].width, best.width);
726 EXPECT_EQ(required_formats[i].height, best.height);
727 }
728
729 // Expect 16x9 for 16x9 request.
730 EXPECT_TRUE(capturer_.GetBestCaptureFormat(required_formats[2], &best));
731 EXPECT_EQ(640, best.width);
732 EXPECT_EQ(360, best.height);
733 }
734
735 // If HAVE_WEBRTC_VIDEO is not defined the video capturer will not be able to
736 // provide OnVideoFrame-callbacks since they require cricket::CapturedFrame to
737 // be decoded as a cricket::VideoFrame (i.e. an I420 frame). This functionality
738 // only exist if HAVE_WEBRTC_VIDEO is defined below. I420 frames are also a
739 // requirement for the VideoProcessors so they will not be called either.
740 #if defined(HAVE_WEBRTC_VIDEO)
741 TEST_F(VideoCapturerTest, VideoFrame) {
742 EXPECT_EQ(cricket::CS_RUNNING, capturer_.Start(cricket::VideoFormat(
743 640,
744 480,
745 cricket::VideoFormat::FpsToInterval(30),
746 cricket::FOURCC_I420)));
747 EXPECT_TRUE(capturer_.IsRunning());
748 EXPECT_EQ(0, video_frames_received());
749 EXPECT_TRUE(capturer_.CaptureFrame());
750 EXPECT_EQ(1, video_frames_received());
751 }
752 #endif // HAVE_WEBRTC_VIDEO
753
754 bool HdFormatInList(const std::vector<cricket::VideoFormat>& formats) {
755 for (std::vector<cricket::VideoFormat>::const_iterator found =
756 formats.begin(); found != formats.end(); ++found) {
757 if (found->height >= kMinHdHeight) {
758 return true;
759 }
760 }
761 return false;
762 }
763
764 TEST_F(VideoCapturerTest, Whitelist) {
765 // The definition of HD only applies to the height. Set the HD width to the
766 // smallest legal number to document this fact in this test.
767 const int kMinHdWidth = 1;
768 cricket::VideoFormat hd_format(kMinHdWidth,
769 kMinHdHeight,
770 cricket::VideoFormat::FpsToInterval(30),
771 cricket::FOURCC_I420);
772 cricket::VideoFormat vga_format(640, 480,
773 cricket::VideoFormat::FpsToInterval(30),
774 cricket::FOURCC_I420);
775 std::vector<cricket::VideoFormat> formats = *capturer_.GetSupportedFormats();
776 formats.push_back(hd_format);
777
778 // Enable whitelist. Expect HD not in list.
779 capturer_.set_enable_camera_list(true);
780 capturer_.ResetSupportedFormats(formats);
781 EXPECT_TRUE(HdFormatInList(*capturer_.GetSupportedFormats()));
782 capturer_.ConstrainSupportedFormats(vga_format);
783 EXPECT_FALSE(HdFormatInList(*capturer_.GetSupportedFormats()));
784
785 // Disable whitelist. Expect HD in list.
786 capturer_.set_enable_camera_list(false);
787 capturer_.ResetSupportedFormats(formats);
788 EXPECT_TRUE(HdFormatInList(*capturer_.GetSupportedFormats()));
789 capturer_.ConstrainSupportedFormats(vga_format);
790 EXPECT_TRUE(HdFormatInList(*capturer_.GetSupportedFormats()));
791 }
792
793 TEST_F(VideoCapturerTest, BlacklistAllFormats) {
794 cricket::VideoFormat vga_format(640, 480,
795 cricket::VideoFormat::FpsToInterval(30),
796 cricket::FOURCC_I420);
797 std::vector<cricket::VideoFormat> supported_formats;
798 // Mock a device that only supports HD formats.
799 supported_formats.push_back(cricket::VideoFormat(1280, 720,
800 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
801 supported_formats.push_back(cricket::VideoFormat(1920, 1080,
802 cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
803 capturer_.ResetSupportedFormats(supported_formats);
804 EXPECT_EQ(2u, capturer_.GetSupportedFormats()->size());
805 // Now, enable the list, which would exclude both formats. However, since
806 // only HD formats are available, we refuse to filter at all, so we don't
807 // break this camera.
808 capturer_.set_enable_camera_list(true);
809 capturer_.ConstrainSupportedFormats(vga_format);
810 EXPECT_EQ(2u, capturer_.GetSupportedFormats()->size());
811 // To make sure it's not just the camera list being broken, add in VGA and
812 // try again. This time, only the VGA format should be there.
813 supported_formats.push_back(vga_format);
814 capturer_.ResetSupportedFormats(supported_formats);
815 ASSERT_EQ(1u, capturer_.GetSupportedFormats()->size());
816 EXPECT_EQ(vga_format.height, capturer_.GetSupportedFormats()->at(0).height);
817 }
OLDNEW
« no previous file with comments | « talk/media/base/videocapturer.cc ('k') | talk/media/base/videocapturerfactory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698