Merge remote-tracking branch 'upstream/3.4' into merge-3.4
[platform/upstream/opencv.git] / modules / videoio / test / test_camera.cpp
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.
4
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.
7 //
8 // Usage: opencv_test_videoio --gtest_also_run_disabled_tests --gtest_filter=*videoio_camera*<tested case>*
9
10 #include "test_precomp.hpp"
11 #include <opencv2/core/utils/configuration.private.hpp>
12
13 namespace opencv_test { namespace {
14
15 static void test_readFrames(/*const*/ VideoCapture& capture, const int N = 100, Mat* lastFrame = NULL, bool testTimestamps = true)
16 {
17     Mat frame;
18     int64 time0 = cv::getTickCount();
19     int64 sysTimePrev = time0;
20     const double cvTickFreq = cv::getTickFrequency();
21
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;
25
26     const bool validTickAndFps = cvTickFreq != 0 && fps != 0.;
27     testTimestamps &= validTickAndFps;
28
29     for (int i = 0; i < N; i++)
30     {
31         SCOPED_TRACE(cv::format("frame=%d", i));
32
33         capture >> frame;
34         const int64 sysTimeCurr = cv::getTickCount();
35         const double camTimeCurr = capture.get(cv::CAP_PROP_POS_MSEC);
36         ASSERT_FALSE(frame.empty());
37
38         // Do we have a previous frame?
39         if (i > 0 && testTimestamps)
40         {
41             const double sysTimeElapsedSecs = (sysTimeCurr - sysTimePrev) / cvTickFreq;
42             const double camTimeElapsedSecs = (camTimeCurr - camTimePrev) / 1000.;
43
44             // Check that the time between two camera frames and two system time calls
45             // are within 1.5 frame periods of one another.
46             //
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);
50         }
51
52         EXPECT_GT(cvtest::norm(frame, NORM_INF), 0) << "Complete black image has been received";
53
54         sysTimePrev = sysTimeCurr;
55         camTimePrev = camTimeCurr;
56     }
57
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();
61 }
62
63 TEST(DISABLED_videoio_camera, basic)
64 {
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);
72     capture.release();
73 }
74
75 TEST(DISABLED_videoio_camera, v4l_read_mjpg)
76 {
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);
87     capture.release();
88 }
89
90 //Following test if for capture device using PhysConn_Video_SerialDigital as crossbar input pin
91 TEST(DISABLED_videoio_camera, channel6)
92 {
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);
101     capture.release();
102 }
103
104 TEST(DISABLED_videoio_camera, v4l_read_framesize)
105 {
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);
115
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;
121     Mat frame640x480;
122     test_readFrames(capture, 30, &frame640x480);
123     EXPECT_EQ(640, frame640x480.cols);
124     EXPECT_EQ(480, frame640x480.rows);
125
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;
131     Mat frame1280x720;
132     test_readFrames(capture, 30, &frame1280x720);
133     EXPECT_EQ(1280, frame1280x720.cols);
134     EXPECT_EQ(720, frame1280x720.rows);
135
136     capture.release();
137 }
138
139
140 static
141 utils::Paths getTestCameras()
142 {
143     static utils::Paths cameras = utils::getConfigurationParameterPaths("OPENCV_TEST_CAMERA_LIST");
144     return cameras;
145 }
146
147 TEST(DISABLED_videoio_camera, waitAny_V4L)
148 {
149     auto cameraNames = getTestCameras();
150     if (cameraNames.empty())
151        throw SkipTestException("No list of tested cameras. Use OPENCV_TEST_CAMERA_LIST parameter");
152
153     const int totalFrames = 50; // number of expected frames (summary for all cameras)
154     const int64 timeoutNS = 100 * 1000000;
155
156     const Size frameSize(640, 480);
157     const int fpsDefaultEven = 30;
158     const int fpsDefaultOdd = 15;
159
160     std::vector<VideoCapture> cameras;
161     for (size_t i = 0; i < cameraNames.size(); ++i)
162     {
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;
171         //launch cameras
172         Mat firstFrame;
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);
177     }
178
179     std::vector<size_t> frameFromCamera(cameraNames.size(), 0);
180     {
181         int counter = 0;
182         std::vector<int> cameraReady;
183         do
184         {
185             EXPECT_TRUE(VideoCapture::waitAny(cameras, cameraReady, timeoutNS));
186             EXPECT_FALSE(cameraReady.empty());
187             for (int idx : cameraReady)
188             {
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];
192                 Mat frame;
193 #if 1
194                 ASSERT_TRUE(c.retrieve(frame)) << idx;
195 #else
196                 ASSERT_TRUE(c.read(frame)) << idx;
197 #endif
198                 EXPECT_EQ(frameSize.width, frame.cols) << idx;
199                 EXPECT_EQ(frameSize.height, frame.rows) << idx;
200
201                 ++frameFromCamera[idx];
202                 ++counter;
203             }
204         }
205         while(counter < totalFrames);
206     }
207
208     for (size_t i = 0; i < cameraNames.size(); ++i)
209     {
210         EXPECT_GT(frameFromCamera[i], (size_t)0) << i;
211     }
212 }
213
214 }} // namespace