8
8
9
9
namespace cv {
10
10
11
+
12
+ void applyParametersFallback (const Ptr<IVideoCapture>& cap, const VideoCaptureParameters& params)
13
+ {
14
+ std::vector<int > props = params.getUnused ();
15
+ CV_LOG_INFO (NULL , " VIDEOIO: Backend '" << videoio_registry::getBackendName ((VideoCaptureAPIs)cap->getCaptureDomain ()) <<
16
+ " ' implementation doesn't support parameters in .open(). Applying " <<
17
+ props.size () << " properties through .setProperty()" );
18
+ for (int prop : props)
19
+ {
20
+ double value = params.get <double >(prop, -1 );
21
+ CV_LOG_INFO (NULL , " VIDEOIO: apply parameter: [" << prop << " ]=" <<
22
+ cv::format (" %g / %lld / 0x%16llx" , value, (long long )value, (long long )value));
23
+ if (!cap->setProperty (prop, value))
24
+ {
25
+ CV_Error_ (cv::Error::StsNotImplemented, (" VIDEOIO: Failed to apply invalid or unsupported parameter: [%d]=%g / %lld / 0x%08llx" , prop, value, (long long )value, (long long )value));
26
+ }
27
+ }
28
+ // NB: there is no dedicated "commit" parameters event, implementations should commit after each property automatically
29
+ }
30
+
31
+
32
+ // Legacy API. Modern API with parameters is below
11
33
class StaticBackend : public IBackend
12
34
{
13
35
public:
@@ -20,18 +42,33 @@ class StaticBackend: public IBackend
20
42
{
21
43
// nothing
22
44
}
45
+
23
46
~StaticBackend () CV_OVERRIDE {}
24
47
25
- Ptr<IVideoCapture> createCapture (int camera) const CV_OVERRIDE
48
+ Ptr<IVideoCapture> createCapture (int camera, const VideoCaptureParameters& params ) const CV_OVERRIDE
26
49
{
27
50
if (fn_createCaptureCamera_)
28
- return fn_createCaptureCamera_ (camera);
51
+ {
52
+ Ptr<IVideoCapture> cap = fn_createCaptureCamera_ (camera);
53
+ if (cap && !params.empty ())
54
+ {
55
+ applyParametersFallback (cap, params);
56
+ }
57
+ return cap;
58
+ }
29
59
return Ptr<IVideoCapture>();
30
60
}
31
- Ptr<IVideoCapture> createCapture (const std::string &filename) const CV_OVERRIDE
61
+ Ptr<IVideoCapture> createCapture (const std::string &filename, const VideoCaptureParameters& params ) const CV_OVERRIDE
32
62
{
33
63
if (fn_createCaptureFile_)
34
- return fn_createCaptureFile_ (filename);
64
+ {
65
+ Ptr<IVideoCapture> cap = fn_createCaptureFile_ (filename);
66
+ if (cap && !params.empty ())
67
+ {
68
+ applyParametersFallback (cap, params);
69
+ }
70
+ return cap;
71
+ }
35
72
return Ptr<IVideoCapture>();
36
73
}
37
74
Ptr<IVideoWriter> createWriter (const std::string& filename, int fourcc, double fps,
@@ -63,11 +100,79 @@ class StaticBackendFactory : public IBackendFactory
63
100
}
64
101
};
65
102
103
+
66
104
Ptr<IBackendFactory> createBackendFactory (FN_createCaptureFile createCaptureFile,
67
105
FN_createCaptureCamera createCaptureCamera,
68
106
FN_createWriter createWriter)
69
107
{
70
108
return makePtr<StaticBackendFactory>(createCaptureFile, createCaptureCamera, createWriter).staticCast <IBackendFactory>();
71
109
}
72
110
111
+
112
+
113
+ class StaticBackendWithParams : public IBackend
114
+ {
115
+ public:
116
+ FN_createCaptureFileWithParams fn_createCaptureFile_;
117
+ FN_createCaptureCameraWithParams fn_createCaptureCamera_;
118
+ FN_createWriter fn_createWriter_;
119
+
120
+ StaticBackendWithParams (FN_createCaptureFileWithParams fn_createCaptureFile, FN_createCaptureCameraWithParams fn_createCaptureCamera, FN_createWriter fn_createWriter)
121
+ : fn_createCaptureFile_(fn_createCaptureFile), fn_createCaptureCamera_(fn_createCaptureCamera), fn_createWriter_(fn_createWriter)
122
+ {
123
+ // nothing
124
+ }
125
+
126
+ ~StaticBackendWithParams () CV_OVERRIDE {}
127
+
128
+ Ptr<IVideoCapture> createCapture (int camera, const VideoCaptureParameters& params) const CV_OVERRIDE
129
+ {
130
+ if (fn_createCaptureCamera_)
131
+ return fn_createCaptureCamera_ (camera, params);
132
+ return Ptr<IVideoCapture>();
133
+ }
134
+ Ptr<IVideoCapture> createCapture (const std::string &filename, const VideoCaptureParameters& params) const CV_OVERRIDE
135
+ {
136
+ if (fn_createCaptureFile_)
137
+ return fn_createCaptureFile_ (filename, params);
138
+ return Ptr<IVideoCapture>();
139
+ }
140
+ Ptr<IVideoWriter> createWriter (const std::string& filename, int fourcc, double fps,
141
+ const cv::Size& sz, const VideoWriterParameters& params) const CV_OVERRIDE
142
+ {
143
+ if (fn_createWriter_)
144
+ return fn_createWriter_ (filename, fourcc, fps, sz, params);
145
+ return Ptr<IVideoWriter>();
146
+ }
147
+ }; // StaticBackendWithParams
148
+
149
+ class StaticBackendWithParamsFactory : public IBackendFactory
150
+ {
151
+ protected:
152
+ Ptr<StaticBackendWithParams> backend;
153
+
154
+ public:
155
+ StaticBackendWithParamsFactory (FN_createCaptureFileWithParams createCaptureFile, FN_createCaptureCameraWithParams createCaptureCamera, FN_createWriter createWriter)
156
+ : backend(makePtr<StaticBackendWithParams>(createCaptureFile, createCaptureCamera, createWriter))
157
+ {
158
+ // nothing
159
+ }
160
+
161
+ ~StaticBackendWithParamsFactory () CV_OVERRIDE {}
162
+
163
+ Ptr<IBackend> getBackend () const CV_OVERRIDE
164
+ {
165
+ return backend.staticCast <IBackend>();
166
+ }
167
+ };
168
+
169
+
170
+ Ptr<IBackendFactory> createBackendFactory (FN_createCaptureFileWithParams createCaptureFile,
171
+ FN_createCaptureCameraWithParams createCaptureCamera,
172
+ FN_createWriter createWriter)
173
+ {
174
+ return makePtr<StaticBackendWithParamsFactory>(createCaptureFile, createCaptureCamera, createWriter).staticCast <IBackendFactory>();
175
+ }
176
+
177
+
73
178
} // namespace
0 commit comments