1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 // Note: all tests here are DISABLED by default due specific requirements.
6 // Don't use #if 0 - these tests should be tested for compilation at least.
8 // Usage: opencv_test_videoio --gtest_also_run_disabled_tests --gtest_filter=*videoio_camera*<tested case>*
10 #include "test_precomp.hpp"
11 #include <opencv2/core/utils/configuration.private.hpp>
13 namespace opencv_test { namespace {
15 static void test_readFrames(/*const*/ VideoCapture& capture, const int N = 100, Mat* lastFrame = NULL, bool testTimestamps = true)
18 int64 time0 = cv::getTickCount();
19 int64 sysTimePrev = time0;
20 const double cvTickFreq = cv::getTickFrequency();
22 double camTimePrev = 0.0;
23 const double fps = capture.get(cv::CAP_PROP_FPS);
24 const double framePeriod = fps == 0.0 ? 1. : 1.0 / fps;
26 const bool validTickAndFps = cvTickFreq != 0 && fps != 0.;
27 testTimestamps &= validTickAndFps;
29 for (int i = 0; i < N; i++)
31 SCOPED_TRACE(cv::format("frame=%d", i));
34 const int64 sysTimeCurr = cv::getTickCount();
35 const double camTimeCurr = capture.get(cv::CAP_PROP_POS_MSEC);
36 ASSERT_FALSE(frame.empty());
38 // Do we have a previous frame?
39 if (i > 0 && testTimestamps)
41 const double sysTimeElapsedSecs = (sysTimeCurr - sysTimePrev) / cvTickFreq;
42 const double camTimeElapsedSecs = (camTimeCurr - camTimePrev) / 1000.;
44 // Check that the time between two camera frames and two system time calls
45 // are within 1.5 frame periods of one another.
47 // 1.5x is chosen to accomodate for a dropped frame, and an additional 50%
48 // to account for drift in the scale of the camera and system time domains.
49 EXPECT_NEAR(sysTimeElapsedSecs, camTimeElapsedSecs, framePeriod * 1.5);
52 EXPECT_GT(cvtest::norm(frame, NORM_INF), 0) << "Complete black image has been received";
54 sysTimePrev = sysTimeCurr;
55 camTimePrev = camTimeCurr;
58 int64 time1 = cv::getTickCount();
59 printf("Processed %d frames on %.2f FPS\n", N, (N * cvTickFreq) / (time1 - time0 + 1));
60 if (lastFrame) *lastFrame = frame.clone();
63 TEST(DISABLED_videoio_camera, basic)
65 VideoCapture capture(0);
66 ASSERT_TRUE(capture.isOpened());
67 std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
68 std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
69 std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
70 std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
71 test_readFrames(capture);
75 TEST(DISABLED_videoio_camera, v4l_read_mjpg)
77 VideoCapture capture(CAP_V4L2);
78 ASSERT_TRUE(capture.isOpened());
79 ASSERT_TRUE(capture.set(CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G')));
80 std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
81 std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
82 std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
83 std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
84 int fourcc = (int)capture.get(CAP_PROP_FOURCC);
85 std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
86 test_readFrames(capture);
90 //Following test if for capture device using PhysConn_Video_SerialDigital as crossbar input pin
91 TEST(DISABLED_videoio_camera, channel6)
93 VideoCapture capture(0);
94 ASSERT_TRUE(capture.isOpened());
95 capture.set(CAP_PROP_CHANNEL, 6);
96 std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
97 std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
98 std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
99 std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
100 test_readFrames(capture);
104 TEST(DISABLED_videoio_camera, v4l_read_framesize)
106 VideoCapture capture(CAP_V4L2);
107 ASSERT_TRUE(capture.isOpened());
108 std::cout << "Camera 0 via " << capture.getBackendName() << " backend" << std::endl;
109 std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
110 std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
111 std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
112 int fourcc = (int)capture.get(CAP_PROP_FOURCC);
113 std::cout << "FOURCC code: " << cv::format("0x%8x", fourcc) << std::endl;
114 test_readFrames(capture, 30);
116 EXPECT_TRUE(capture.set(CAP_PROP_FRAME_WIDTH, 640));
117 EXPECT_TRUE(capture.set(CAP_PROP_FRAME_HEIGHT, 480));
118 std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
119 std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
120 std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
122 test_readFrames(capture, 30, &frame640x480);
123 EXPECT_EQ(640, frame640x480.cols);
124 EXPECT_EQ(480, frame640x480.rows);
126 EXPECT_TRUE(capture.set(CAP_PROP_FRAME_WIDTH, 1280));
127 EXPECT_TRUE(capture.set(CAP_PROP_FRAME_HEIGHT, 720));
128 std::cout << "Frame width: " << capture.get(CAP_PROP_FRAME_WIDTH) << std::endl;
129 std::cout << " height: " << capture.get(CAP_PROP_FRAME_HEIGHT) << std::endl;
130 std::cout << "Capturing FPS: " << capture.get(CAP_PROP_FPS) << std::endl;
132 test_readFrames(capture, 30, &frame1280x720);
133 EXPECT_EQ(1280, frame1280x720.cols);
134 EXPECT_EQ(720, frame1280x720.rows);
141 utils::Paths getTestCameras()
143 static utils::Paths cameras = utils::getConfigurationParameterPaths("OPENCV_TEST_CAMERA_LIST");
147 TEST(DISABLED_videoio_camera, waitAny_V4L)
149 auto cameraNames = getTestCameras();
150 if (cameraNames.empty())
151 throw SkipTestException("No list of tested cameras. Use OPENCV_TEST_CAMERA_LIST parameter");
153 const int totalFrames = 50; // number of expected frames (summary for all cameras)
154 const int64 timeoutNS = 100 * 1000000;
156 const Size frameSize(640, 480);
157 const int fpsDefaultEven = 30;
158 const int fpsDefaultOdd = 15;
160 std::vector<VideoCapture> cameras;
161 for (size_t i = 0; i < cameraNames.size(); ++i)
163 const auto& name = cameraNames[i];
164 int fps = (int)utils::getConfigurationParameterSizeT(cv::format("OPENCV_TEST_CAMERA%d_FPS", (int)i).c_str(), (i & 1) ? fpsDefaultOdd : fpsDefaultEven);
165 std::cout << "Camera[" << i << "] = '" << name << "', fps=" << fps << std::endl;
166 VideoCapture cap(name, CAP_V4L);
167 ASSERT_TRUE(cap.isOpened()) << name;
168 EXPECT_TRUE(cap.set(CAP_PROP_FRAME_WIDTH, frameSize.width)) << name;
169 EXPECT_TRUE(cap.set(CAP_PROP_FRAME_HEIGHT, frameSize.height)) << name;
170 EXPECT_TRUE(cap.set(CAP_PROP_FPS, fps)) << name;
173 EXPECT_TRUE(cap.read(firstFrame));
174 EXPECT_EQ(frameSize.width, firstFrame.cols);
175 EXPECT_EQ(frameSize.height, firstFrame.rows);
176 cameras.push_back(cap);
179 std::vector<size_t> frameFromCamera(cameraNames.size(), 0);
182 std::vector<int> cameraReady;
185 EXPECT_TRUE(VideoCapture::waitAny(cameras, cameraReady, timeoutNS));
186 EXPECT_FALSE(cameraReady.empty());
187 for (int idx : cameraReady)
189 //std::cout << "Reading frame from camera: " << idx << std::endl;
190 ASSERT_TRUE(idx >= 0 && (size_t)idx < cameras.size()) << idx;
191 VideoCapture& c = cameras[idx];
194 ASSERT_TRUE(c.retrieve(frame)) << idx;
196 ASSERT_TRUE(c.read(frame)) << idx;
198 EXPECT_EQ(frameSize.width, frame.cols) << idx;
199 EXPECT_EQ(frameSize.height, frame.rows) << idx;
201 ++frameFromCamera[idx];
205 while(counter < totalFrames);
208 for (size_t i = 0; i < cameraNames.size(); ++i)
210 EXPECT_GT(frameFromCamera[i], (size_t)0) << i;