blob: a80039a599c37b0ab7cedac66a9670427aef7580 [file] [log] [blame]
perkj@webrtc.org8627adc2011-12-05 09:58:55 +00001/*
leozwang@webrtc.org1745e932012-03-01 16:30:40 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
perkj@webrtc.org8627adc2011-12-05 09:58:55 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <stdio.h>
12
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +000013#include <map>
kwiberge065fcf2016-03-02 01:01:11 -080014#include <memory>
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +000015#include <sstream>
16
nisseaf916892017-01-10 07:44:26 -080017#include "webrtc/api/video/i420_buffer.h"
18#include "webrtc/api/video/video_frame.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000019#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander5dda80a2015-11-12 12:46:09 +010021#include "webrtc/modules/video_capture/video_capture.h"
22#include "webrtc/modules/video_capture/video_capture_factory.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020023#include "webrtc/rtc_base/criticalsection.h"
24#include "webrtc/rtc_base/scoped_ref_ptr.h"
25#include "webrtc/rtc_base/timeutils.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010026#include "webrtc/system_wrappers/include/sleep.h"
nissec9c142f2016-05-17 04:05:47 -070027#include "webrtc/test/frame_utils.h"
kwibergac9f8762016-09-30 22:29:43 -070028#include "webrtc/test/gtest.h"
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +000029
hta@webrtc.orgf494fd02012-06-25 11:33:34 +000030using webrtc::SleepMs;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000031using webrtc::VideoCaptureCapability;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000032using webrtc::VideoCaptureFactory;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000033using webrtc::VideoCaptureModule;
34
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +000035
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000036#define WAIT_(ex, timeout, res) \
37 do { \
38 res = (ex); \
Niels Möllerd28db7f2016-05-10 16:31:47 +020039 int64_t start = rtc::TimeMillis(); \
40 while (!res && rtc::TimeMillis() < start + timeout) { \
hta@webrtc.orgf494fd02012-06-25 11:33:34 +000041 SleepMs(5); \
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000042 res = (ex); \
43 } \
torbjornga0892572015-12-16 18:38:31 -080044 } while (0)
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000045
46#define EXPECT_TRUE_WAIT(ex, timeout) \
47 do { \
48 bool res; \
49 WAIT_(ex, timeout, res); \
50 if (!res) EXPECT_TRUE(ex); \
torbjornga0892572015-12-16 18:38:31 -080051 } while (0)
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000052
53
54static const int kTimeOut = 5000;
55static const int kTestHeight = 288;
56static const int kTestWidth = 352;
57static const int kTestFramerate = 30;
58
nisseb29b9c82016-12-12 00:22:56 -080059class TestVideoCaptureCallback
60 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000061 public:
kthelgasonff046c72017-03-31 02:03:55 -070062 TestVideoCaptureCallback() :
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000063 last_render_time_ms_(0),
64 incoming_frames_(0),
65 timing_warnings_(0),
66 rotate_frame_(webrtc::kVideoRotation_0) {}
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000067
68 ~TestVideoCaptureCallback() {
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +000069 if (timing_warnings_ > 0)
70 printf("No of timing warnings %d\n", timing_warnings_);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000071 }
72
nisseb29b9c82016-12-12 00:22:56 -080073 void OnFrame(const webrtc::VideoFrame& videoFrame) override {
kthelgasonff046c72017-03-31 02:03:55 -070074 rtc::CritScope cs(&capture_cs_);
magjed@webrtc.org2056ee32015-03-16 13:46:52 +000075 int height = videoFrame.height();
76 int width = videoFrame.width();
kwiberg77eab702016-09-28 17:42:01 -070077#if defined(ANDROID) && ANDROID
glaznev@webrtc.orgf7030d42014-10-17 16:25:06 +000078 // Android camera frames may be rotated depending on test device
79 // orientation.
80 EXPECT_TRUE(height == capability_.height || height == capability_.width);
81 EXPECT_TRUE(width == capability_.width || width == capability_.height);
82#else
deadbeeff5629ad2016-03-18 11:38:26 -070083 EXPECT_EQ(height, capability_.height);
84 EXPECT_EQ(width, capability_.width);
85 EXPECT_EQ(rotate_frame_, videoFrame.rotation());
glaznev@webrtc.orgf7030d42014-10-17 16:25:06 +000086#endif
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000087 // RenderTimstamp should be the time now.
88 EXPECT_TRUE(
Niels Möllerd28db7f2016-05-10 16:31:47 +020089 videoFrame.render_time_ms() >= rtc::TimeMillis()-30 &&
90 videoFrame.render_time_ms() <= rtc::TimeMillis());
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000091
magjed@webrtc.org2056ee32015-03-16 13:46:52 +000092 if ((videoFrame.render_time_ms() >
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +000093 last_render_time_ms_ + (1000 * 1.1) / capability_.maxFPS &&
94 last_render_time_ms_ > 0) ||
magjed@webrtc.org2056ee32015-03-16 13:46:52 +000095 (videoFrame.render_time_ms() <
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +000096 last_render_time_ms_ + (1000 * 0.9) / capability_.maxFPS &&
97 last_render_time_ms_ > 0)) {
98 timing_warnings_++;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +000099 }
100
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000101 incoming_frames_++;
magjed@webrtc.org2056ee32015-03-16 13:46:52 +0000102 last_render_time_ms_ = videoFrame.render_time_ms();
nissec9c142f2016-05-17 04:05:47 -0700103 last_frame_ = videoFrame.video_frame_buffer();
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000104 }
105
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000106 void SetExpectedCapability(VideoCaptureCapability capability) {
kthelgasonff046c72017-03-31 02:03:55 -0700107 rtc::CritScope cs(&capture_cs_);
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000108 capability_= capability;
109 incoming_frames_ = 0;
110 last_render_time_ms_ = 0;
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000111 }
112 int incoming_frames() {
kthelgasonff046c72017-03-31 02:03:55 -0700113 rtc::CritScope cs(&capture_cs_);
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000114 return incoming_frames_;
115 }
116
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000117 int timing_warnings() {
kthelgasonff046c72017-03-31 02:03:55 -0700118 rtc::CritScope cs(&capture_cs_);
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000119 return timing_warnings_;
120 }
121 VideoCaptureCapability capability() {
kthelgasonff046c72017-03-31 02:03:55 -0700122 rtc::CritScope cs(&capture_cs_);
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000123 return capability_;
124 }
125
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700126 bool CompareLastFrame(const webrtc::VideoFrame& frame) {
kthelgasonff046c72017-03-31 02:03:55 -0700127 rtc::CritScope cs(&capture_cs_);
nissec9c142f2016-05-17 04:05:47 -0700128 return webrtc::test::FrameBufsEqual(last_frame_,
129 frame.video_frame_buffer());
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000130 }
131
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000132 void SetExpectedCaptureRotation(webrtc::VideoRotation rotation) {
kthelgasonff046c72017-03-31 02:03:55 -0700133 rtc::CritScope cs(&capture_cs_);
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000134 rotate_frame_ = rotation;
135 }
136
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000137 private:
kthelgasonff046c72017-03-31 02:03:55 -0700138 rtc::CriticalSection capture_cs_;
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000139 VideoCaptureCapability capability_;
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000140 int64_t last_render_time_ms_;
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000141 int incoming_frames_;
142 int timing_warnings_;
nissec9c142f2016-05-17 04:05:47 -0700143 rtc::scoped_refptr<webrtc::VideoFrameBuffer> last_frame_;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000144 webrtc::VideoRotation rotate_frame_;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000145};
146
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000147class VideoCaptureTest : public testing::Test {
148 public:
149 VideoCaptureTest() : number_of_devices_(0) {}
150
151 void SetUp() {
nisseb29b9c82016-12-12 00:22:56 -0800152 device_info_.reset(VideoCaptureFactory::CreateDeviceInfo());
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +0000153 assert(device_info_.get());
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000154 number_of_devices_ = device_info_->NumberOfDevices();
155 ASSERT_GT(number_of_devices_, 0u);
156 }
157
Peter Boström26b08602015-06-04 15:18:17 +0200158 rtc::scoped_refptr<VideoCaptureModule> OpenVideoCaptureDevice(
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000159 unsigned int device,
nisseb29b9c82016-12-12 00:22:56 -0800160 rtc::VideoSinkInterface<webrtc::VideoFrame>* callback) {
leozwang@webrtc.org1745e932012-03-01 16:30:40 +0000161 char device_name[256];
162 char unique_name[256];
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000163
164 EXPECT_EQ(0, device_info_->GetDeviceName(
165 device, device_name, 256, unique_name, 256));
166
Peter Boström26b08602015-06-04 15:18:17 +0200167 rtc::scoped_refptr<VideoCaptureModule> module(
nisseb29b9c82016-12-12 00:22:56 -0800168 VideoCaptureFactory::Create(unique_name));
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000169 if (module.get() == NULL)
170 return NULL;
171
172 EXPECT_FALSE(module->CaptureStarted());
173
nisseb29b9c82016-12-12 00:22:56 -0800174 module->RegisterCaptureDataCallback(callback);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000175 return module;
176 }
177
178 void StartCapture(VideoCaptureModule* capture_module,
179 VideoCaptureCapability capability) {
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +0000180 ASSERT_EQ(0, capture_module->StartCapture(capability));
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000181 EXPECT_TRUE(capture_module->CaptureStarted());
182
183 VideoCaptureCapability resulting_capability;
184 EXPECT_EQ(0, capture_module->CaptureSettings(resulting_capability));
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000185 EXPECT_EQ(capability.width, resulting_capability.width);
186 EXPECT_EQ(capability.height, resulting_capability.height);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000187 }
188
kwiberge065fcf2016-03-02 01:01:11 -0800189 std::unique_ptr<VideoCaptureModule::DeviceInfo> device_info_;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000190 unsigned int number_of_devices_;
191};
192
kjellander335ecf52016-01-06 05:23:09 -0800193#ifdef WEBRTC_MAC
194// Currently fails on Mac 64-bit, see
195// https://bugs.chromium.org/p/webrtc/issues/detail?id=5406
196#define MAYBE_CreateDelete DISABLED_CreateDelete
197#else
198#define MAYBE_CreateDelete CreateDelete
199#endif
200TEST_F(VideoCaptureTest, MAYBE_CreateDelete) {
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000201 for (int i = 0; i < 5; ++i) {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200202 int64_t start_time = rtc::TimeMillis();
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000203 TestVideoCaptureCallback capture_observer;
Peter Boström26b08602015-06-04 15:18:17 +0200204 rtc::scoped_refptr<VideoCaptureModule> module(
205 OpenVideoCaptureDevice(0, &capture_observer));
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000206 ASSERT_TRUE(module.get() != NULL);
207
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000208 VideoCaptureCapability capability;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000209#ifndef WEBRTC_MAC
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000210 device_info_->GetCapability(module->CurrentDeviceName(), 0, capability);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000211#else
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000212 capability.width = kTestWidth;
213 capability.height = kTestHeight;
214 capability.maxFPS = kTestFramerate;
nisseeb44b392017-04-28 07:18:05 -0700215 capability.videoType = webrtc::VideoType::kUnknown;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000216#endif
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000217 capture_observer.SetExpectedCapability(capability);
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +0000218 ASSERT_NO_FATAL_FAILURE(StartCapture(module.get(), capability));
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000219
220 // Less than 4s to start the camera.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200221 EXPECT_LE(rtc::TimeMillis() - start_time, 4000);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000222
223 // Make sure 5 frames are captured.
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000224 EXPECT_TRUE_WAIT(capture_observer.incoming_frames() >= 5, kTimeOut);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000225
Niels Möllerd28db7f2016-05-10 16:31:47 +0200226 int64_t stop_time = rtc::TimeMillis();
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000227 EXPECT_EQ(0, module->StopCapture());
228 EXPECT_FALSE(module->CaptureStarted());
229
230 // Less than 3s to stop the camera.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200231 EXPECT_LE(rtc::TimeMillis() - stop_time, 3000);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000232 }
233}
234
kjellander335ecf52016-01-06 05:23:09 -0800235#ifdef WEBRTC_MAC
236// Currently fails on Mac 64-bit, see
237// https://bugs.chromium.org/p/webrtc/issues/detail?id=5406
238#define MAYBE_Capabilities DISABLED_Capabilities
239#else
240#define MAYBE_Capabilities Capabilities
241#endif
242TEST_F(VideoCaptureTest, MAYBE_Capabilities) {
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000243#ifdef WEBRTC_MAC
244 printf("Video capture capabilities are not supported on Mac.\n");
245 return;
246#endif
247
248 TestVideoCaptureCallback capture_observer;
249
Peter Boström26b08602015-06-04 15:18:17 +0200250 rtc::scoped_refptr<VideoCaptureModule> module(
251 OpenVideoCaptureDevice(0, &capture_observer));
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000252 ASSERT_TRUE(module.get() != NULL);
253
254 int number_of_capabilities = device_info_->NumberOfCapabilities(
255 module->CurrentDeviceName());
256 EXPECT_GT(number_of_capabilities, 0);
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +0000257 // Key is <width>x<height>, value is vector of maxFPS values at that
258 // resolution.
259 typedef std::map<std::string, std::vector<int> > FrameRatesByResolution;
260 FrameRatesByResolution frame_rates_by_resolution;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000261 for (int i = 0; i < number_of_capabilities; ++i) {
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000262 VideoCaptureCapability capability;
263 EXPECT_EQ(0, device_info_->GetCapability(module->CurrentDeviceName(), i,
264 capability));
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +0000265 std::ostringstream resolutionStream;
266 resolutionStream << capability.width << "x" << capability.height;
267 resolutionStream.flush();
268 std::string resolution = resolutionStream.str();
269 frame_rates_by_resolution[resolution].push_back(capability.maxFPS);
270
271 // Since Android presents so many resolution/FPS combinations and the test
272 // runner imposes a timeout, we only actually start the capture and test
273 // that a frame was captured for 2 frame-rates at each resolution.
274 if (frame_rates_by_resolution[resolution].size() > 2)
275 continue;
276
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000277 capture_observer.SetExpectedCapability(capability);
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +0000278 ASSERT_NO_FATAL_FAILURE(StartCapture(module.get(), capability));
279 // Make sure at least one frame is captured.
280 EXPECT_TRUE_WAIT(capture_observer.incoming_frames() >= 1, kTimeOut);
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000281
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000282 EXPECT_EQ(0, module->StopCapture());
283 }
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +0000284
kwiberg77eab702016-09-28 17:42:01 -0700285#if defined(ANDROID) && ANDROID
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +0000286 // There's no reason for this to _necessarily_ be true, but in practice all
287 // Android devices this test runs on in fact do support multiple capture
288 // resolutions and multiple frame-rates per captured resolution, so we assert
289 // this fact here as a regression-test against the time that we only noticed a
290 // single frame-rate per resolution (bug 2974). If this test starts being run
291 // on devices for which this is untrue (e.g. Nexus4) then the following should
292 // probably be wrapped in a base::android::BuildInfo::model()/device() check.
293 EXPECT_GT(frame_rates_by_resolution.size(), 1U);
294 for (FrameRatesByResolution::const_iterator it =
295 frame_rates_by_resolution.begin();
296 it != frame_rates_by_resolution.end();
297 ++it) {
298 EXPECT_GT(it->second.size(), 1U) << it->first;
299 }
300#endif // ANDROID
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000301}
302
phoglund@webrtc.org6b84b612012-08-27 15:16:20 +0000303// NOTE: flaky, crashes sometimes.
304// http://code.google.com/p/webrtc/issues/detail?id=777
305TEST_F(VideoCaptureTest, DISABLED_TestTwoCameras) {
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000306 if (number_of_devices_ < 2) {
307 printf("There are not two cameras available. Aborting test. \n");
308 return;
309 }
310
311 TestVideoCaptureCallback capture_observer1;
Peter Boström26b08602015-06-04 15:18:17 +0200312 rtc::scoped_refptr<VideoCaptureModule> module1(
313 OpenVideoCaptureDevice(0, &capture_observer1));
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000314 ASSERT_TRUE(module1.get() != NULL);
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000315 VideoCaptureCapability capability1;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000316#ifndef WEBRTC_MAC
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000317 device_info_->GetCapability(module1->CurrentDeviceName(), 0, capability1);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000318#else
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000319 capability1.width = kTestWidth;
320 capability1.height = kTestHeight;
321 capability1.maxFPS = kTestFramerate;
nisseeb44b392017-04-28 07:18:05 -0700322 capability1.videoType = webrtc::VideoType::kUnknown;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000323#endif
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000324 capture_observer1.SetExpectedCapability(capability1);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000325
326 TestVideoCaptureCallback capture_observer2;
Peter Boström26b08602015-06-04 15:18:17 +0200327 rtc::scoped_refptr<VideoCaptureModule> module2(
328 OpenVideoCaptureDevice(1, &capture_observer2));
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000329 ASSERT_TRUE(module1.get() != NULL);
330
331
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000332 VideoCaptureCapability capability2;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000333#ifndef WEBRTC_MAC
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000334 device_info_->GetCapability(module2->CurrentDeviceName(), 0, capability2);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000335#else
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000336 capability2.width = kTestWidth;
337 capability2.height = kTestHeight;
338 capability2.maxFPS = kTestFramerate;
nisseeb44b392017-04-28 07:18:05 -0700339 capability2.videoType = webrtc::VideoType::kUnknown;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000340#endif
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000341 capture_observer2.SetExpectedCapability(capability2);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000342
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +0000343 ASSERT_NO_FATAL_FAILURE(StartCapture(module1.get(), capability1));
344 ASSERT_NO_FATAL_FAILURE(StartCapture(module2.get(), capability2));
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000345 EXPECT_TRUE_WAIT(capture_observer1.incoming_frames() >= 5, kTimeOut);
346 EXPECT_TRUE_WAIT(capture_observer2.incoming_frames() >= 5, kTimeOut);
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +0000347 EXPECT_EQ(0, module2->StopCapture());
348 EXPECT_EQ(0, module1->StopCapture());
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000349}
350
351// Test class for testing external capture and capture feedback information
352// such as frame rate and picture alarm.
353class VideoCaptureExternalTest : public testing::Test {
354 public:
355 void SetUp() {
nisseb29b9c82016-12-12 00:22:56 -0800356 capture_module_ = VideoCaptureFactory::Create(capture_input_interface_);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000357
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000358 VideoCaptureCapability capability;
359 capability.width = kTestWidth;
360 capability.height = kTestHeight;
nisseeb44b392017-04-28 07:18:05 -0700361 capability.videoType = webrtc::VideoType::kYV12;
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +0000362 capability.maxFPS = kTestFramerate;
363 capture_callback_.SetExpectedCapability(capability);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000364
Magnus Jedvert7a721e82017-06-14 11:28:08 +0200365 rtc::scoped_refptr<webrtc::I420Buffer> buffer =
366 webrtc::I420Buffer::Create(kTestWidth, kTestHeight);
nisse64ec8f82016-09-27 00:17:25 -0700367
Magnus Jedvert7a721e82017-06-14 11:28:08 +0200368 memset(buffer->MutableDataY(), 127, buffer->height() * buffer->StrideY());
nisse64ec8f82016-09-27 00:17:25 -0700369 memset(buffer->MutableDataU(), 127,
Magnus Jedvert7a721e82017-06-14 11:28:08 +0200370 buffer->ChromaHeight() * buffer->StrideU());
nisse64ec8f82016-09-27 00:17:25 -0700371 memset(buffer->MutableDataV(), 127,
Magnus Jedvert7a721e82017-06-14 11:28:08 +0200372 buffer->ChromaHeight() * buffer->StrideV());
nisse64ec8f82016-09-27 00:17:25 -0700373 test_frame_.reset(
374 new webrtc::VideoFrame(buffer, 0, 0, webrtc::kVideoRotation_0));
375
376 SleepMs(1); // Wait 1ms so that two tests can't have the same timestamp.
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000377
nisseb29b9c82016-12-12 00:22:56 -0800378 capture_module_->RegisterCaptureDataCallback(&capture_callback_);
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000379 }
380
381 void TearDown() {
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000382 }
383
384 webrtc::VideoCaptureExternal* capture_input_interface_;
Peter Boström26b08602015-06-04 15:18:17 +0200385 rtc::scoped_refptr<VideoCaptureModule> capture_module_;
nisse64ec8f82016-09-27 00:17:25 -0700386 std::unique_ptr<webrtc::VideoFrame> test_frame_;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000387 TestVideoCaptureCallback capture_callback_;
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000388};
389
390// Test input of external video frames.
andresp@webrtc.org3bd56032014-09-17 11:56:25 +0000391TEST_F(VideoCaptureExternalTest, TestExternalCapture) {
nisseeb44b392017-04-28 07:18:05 -0700392 size_t length = webrtc::CalcBufferSize(
393 webrtc::VideoType::kI420, test_frame_->width(), test_frame_->height());
kwiberge065fcf2016-03-02 01:01:11 -0800394 std::unique_ptr<uint8_t[]> test_buffer(new uint8_t[length]);
nisse64ec8f82016-09-27 00:17:25 -0700395 webrtc::ExtractBuffer(*test_frame_, length, test_buffer.get());
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000396 EXPECT_EQ(0, capture_input_interface_->IncomingFrame(test_buffer.get(),
397 length, capture_callback_.capability(), 0));
nisse64ec8f82016-09-27 00:17:25 -0700398 EXPECT_TRUE(capture_callback_.CompareLastFrame(*test_frame_));
perkj@webrtc.org8627adc2011-12-05 09:58:55 +0000399}
400
andresp@webrtc.org3bd56032014-09-17 11:56:25 +0000401TEST_F(VideoCaptureExternalTest, Rotation) {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000402 EXPECT_EQ(0, capture_module_->SetCaptureRotation(webrtc::kVideoRotation_0));
nisseeb44b392017-04-28 07:18:05 -0700403 size_t length = webrtc::CalcBufferSize(
404 webrtc::VideoType::kI420, test_frame_->width(), test_frame_->height());
kwiberge065fcf2016-03-02 01:01:11 -0800405 std::unique_ptr<uint8_t[]> test_buffer(new uint8_t[length]);
nisse64ec8f82016-09-27 00:17:25 -0700406 webrtc::ExtractBuffer(*test_frame_, length, test_buffer.get());
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000407 EXPECT_EQ(0, capture_input_interface_->IncomingFrame(test_buffer.get(),
408 length, capture_callback_.capability(), 0));
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000409 EXPECT_EQ(0, capture_module_->SetCaptureRotation(webrtc::kVideoRotation_90));
410 capture_callback_.SetExpectedCaptureRotation(webrtc::kVideoRotation_90);
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000411 EXPECT_EQ(0, capture_input_interface_->IncomingFrame(test_buffer.get(),
412 length, capture_callback_.capability(), 0));
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000413 EXPECT_EQ(0, capture_module_->SetCaptureRotation(webrtc::kVideoRotation_180));
414 capture_callback_.SetExpectedCaptureRotation(webrtc::kVideoRotation_180);
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000415 EXPECT_EQ(0, capture_input_interface_->IncomingFrame(test_buffer.get(),
416 length, capture_callback_.capability(), 0));
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000417 EXPECT_EQ(0, capture_module_->SetCaptureRotation(webrtc::kVideoRotation_270));
418 capture_callback_.SetExpectedCaptureRotation(webrtc::kVideoRotation_270);
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000419 EXPECT_EQ(0, capture_input_interface_->IncomingFrame(test_buffer.get(),
420 length, capture_callback_.capability(), 0));
421}