Merge pull request #19934 from alalek:videoio_plugin_query_api

pull/19962/head
Alexander Alekhin 4 years ago
commit e68657cdb2
  1. 27
      modules/videoio/include/opencv2/videoio/registry.hpp
  2. 12
      modules/videoio/src/backend.hpp
  3. 92
      modules/videoio/src/backend_plugin.cpp
  4. 4
      modules/videoio/src/backend_static.cpp
  5. 75
      modules/videoio/src/videoio_registry.cpp
  6. 105
      modules/videoio/test/test_plugins.cpp

@ -39,7 +39,32 @@ CV_EXPORTS_W std::vector<VideoCaptureAPIs> getStreamBackends();
CV_EXPORTS_W std::vector<VideoCaptureAPIs> getWriterBackends(); CV_EXPORTS_W std::vector<VideoCaptureAPIs> getWriterBackends();
/** @brief Returns true if backend is available */ /** @brief Returns true if backend is available */
CV_EXPORTS bool hasBackend(VideoCaptureAPIs api); CV_EXPORTS_W bool hasBackend(VideoCaptureAPIs api);
/** @brief Returns true if backend is built in (false if backend is used as plugin) */
CV_EXPORTS_W bool isBackendBuiltIn(VideoCaptureAPIs api);
/** @brief Returns description and ABI/API version of videoio plugin's camera interface */
CV_EXPORTS_W std::string getCameraBackendPluginVersion(
VideoCaptureAPIs api,
CV_OUT int& version_ABI,
CV_OUT int& version_API
);
/** @brief Returns description and ABI/API version of videoio plugin's stream capture interface */
CV_EXPORTS_W std::string getStreamBackendPluginVersion(
VideoCaptureAPIs api,
CV_OUT int& version_ABI,
CV_OUT int& version_API
);
/** @brief Returns description and ABI/API version of videoio plugin's writer interface */
CV_EXPORTS_W std::string getWriterBackendPluginVersion(
VideoCaptureAPIs api,
CV_OUT int& version_ABI,
CV_OUT int& version_API
);
//! @} //! @}
}} // namespace }} // namespace

@ -27,6 +27,7 @@ class IBackendFactory
public: public:
virtual ~IBackendFactory() {} virtual ~IBackendFactory() {}
virtual Ptr<IBackend> getBackend() const = 0; virtual Ptr<IBackend> getBackend() const = 0;
virtual bool isBuiltIn() const = 0;
}; };
//============================================================================= //=============================================================================
@ -48,6 +49,17 @@ Ptr<IBackendFactory> createPluginBackendFactory(VideoCaptureAPIs id, const char*
void applyParametersFallback(const Ptr<IVideoCapture>& cap, const VideoCaptureParameters& params); void applyParametersFallback(const Ptr<IVideoCapture>& cap, const VideoCaptureParameters& params);
std::string getCapturePluginVersion(
const Ptr<IBackendFactory>& backend_factory,
CV_OUT int& version_ABI,
CV_OUT int& version_API
);
std::string getWriterPluginVersion(
const Ptr<IBackendFactory>& backend_factory,
CV_OUT int& version_ABI,
CV_OUT int& version_API
);
} // namespace cv:: } // namespace cv::
#endif // BACKEND_HPP_DEFINED #endif // BACKEND_HPP_DEFINED

@ -210,6 +210,24 @@ public:
Ptr<IVideoCapture> createCapture(const std::string &filename, const VideoCaptureParameters& params) const CV_OVERRIDE; Ptr<IVideoCapture> createCapture(const std::string &filename, const VideoCaptureParameters& params) const CV_OVERRIDE;
Ptr<IVideoWriter> createWriter(const std::string& filename, int fourcc, double fps, Ptr<IVideoWriter> createWriter(const std::string& filename, int fourcc, double fps,
const cv::Size& sz, const VideoWriterParameters& params) const CV_OVERRIDE; const cv::Size& sz, const VideoWriterParameters& params) const CV_OVERRIDE;
std::string getCapturePluginVersion(CV_OUT int& version_ABI, CV_OUT int& version_API)
{
CV_Assert(capture_api_ || plugin_api_);
const OpenCV_API_Header& api_header = capture_api_ ? capture_api_->api_header : plugin_api_->api_header;
version_ABI = api_header.min_api_version;
version_API = api_header.api_version;
return api_header.api_description;
}
std::string getWriterPluginVersion(CV_OUT int& version_ABI, CV_OUT int& version_API)
{
CV_Assert(writer_api_ || plugin_api_);
const OpenCV_API_Header& api_header = writer_api_ ? writer_api_->api_header : plugin_api_->api_header;
version_ABI = api_header.min_api_version;
version_API = api_header.api_version;
return api_header.api_description;
}
}; };
class PluginBackendFactory : public IBackendFactory class PluginBackendFactory : public IBackendFactory
@ -229,14 +247,41 @@ public:
Ptr<IBackend> getBackend() const CV_OVERRIDE Ptr<IBackend> getBackend() const CV_OVERRIDE
{ {
if (!initialized) initBackend();
return backend.staticCast<IBackend>();
}
bool isBuiltIn() const CV_OVERRIDE { return false; }
std::string getCapturePluginVersion(
CV_OUT int& version_ABI,
CV_OUT int& version_API) const
{ {
const_cast<PluginBackendFactory*>(this)->initBackend(); initBackend();
if (!backend)
CV_Error_(Error::StsNotImplemented, ("Backend '%s' is not available", baseName_));
return backend->getCapturePluginVersion(version_ABI, version_API);
} }
return backend.staticCast<IBackend>();
std::string getWriterPluginVersion(
CV_OUT int& version_ABI,
CV_OUT int& version_API) const
{
initBackend();
if (!backend)
CV_Error_(Error::StsNotImplemented, ("Backend '%s' is not available", baseName_));
return backend->getWriterPluginVersion(version_ABI, version_API);
} }
protected: protected:
void initBackend() inline void initBackend() const
{
if (!initialized)
{
const_cast<PluginBackendFactory*>(this)->initBackend_();
}
}
void initBackend_()
{ {
AutoLock lock(getInitializationMutex()); AutoLock lock(getInitializationMutex());
try { try {
@ -690,4 +735,43 @@ Ptr<IBackendFactory> createPluginBackendFactory(VideoCaptureAPIs id, const char*
#endif #endif
} }
std::string getCapturePluginVersion(
const Ptr<IBackendFactory>& backend_factory,
CV_OUT int& version_ABI,
CV_OUT int& version_API
)
{
#if OPENCV_HAVE_FILESYSTEM_SUPPORT && defined(ENABLE_PLUGINS)
using namespace impl;
CV_Assert(backend_factory);
PluginBackendFactory* plugin_backend_factory = dynamic_cast<PluginBackendFactory*>(backend_factory.get());
CV_Assert(plugin_backend_factory);
return plugin_backend_factory->getCapturePluginVersion(version_ABI, version_API);
#else
CV_UNUSED(version_ABI);
CV_UNUSED(version_API);
CV_Error(Error::StsBadFunc, "Plugins are not available in this build");
#endif
}
std::string getWriterPluginVersion(
const Ptr<IBackendFactory>& backend_factory,
CV_OUT int& version_ABI,
CV_OUT int& version_API
)
{
#if OPENCV_HAVE_FILESYSTEM_SUPPORT && defined(ENABLE_PLUGINS)
using namespace impl;
CV_Assert(backend_factory);
PluginBackendFactory* plugin_backend_factory = dynamic_cast<PluginBackendFactory*>(backend_factory.get());
CV_Assert(plugin_backend_factory);
return plugin_backend_factory->getWriterPluginVersion(version_ABI, version_API);
#else
CV_UNUSED(version_ABI);
CV_UNUSED(version_API);
CV_Error(Error::StsBadFunc, "Plugins are not available in this build");
#endif
}
} // namespace } // namespace

@ -99,6 +99,8 @@ public:
{ {
return backend.staticCast<IBackend>(); return backend.staticCast<IBackend>();
} }
bool isBuiltIn() const CV_OVERRIDE { return true; }
}; };
@ -165,6 +167,8 @@ public:
{ {
return backend.staticCast<IBackend>(); return backend.staticCast<IBackend>();
} }
bool isBuiltIn() const CV_OVERRIDE { return true; }
}; };

@ -403,6 +403,81 @@ bool hasBackend(VideoCaptureAPIs api)
return false; return false;
} }
bool isBackendBuiltIn(VideoCaptureAPIs api)
{
std::vector<VideoBackendInfo> backends = VideoBackendRegistry::getInstance().getEnabledBackends();
for (size_t i = 0; i < backends.size(); i++)
{
const VideoBackendInfo& info = backends[i];
if (api == info.id)
{
CV_Assert(!info.backendFactory.empty());
return info.backendFactory->isBuiltIn();
}
}
return false;
}
std::string getCameraBackendPluginVersion(VideoCaptureAPIs api,
CV_OUT int& version_ABI,
CV_OUT int& version_API
)
{
const std::vector<VideoBackendInfo> backends = VideoBackendRegistry::getInstance().getAvailableBackends_CaptureByIndex();
for (size_t i = 0; i < backends.size(); i++)
{
const VideoBackendInfo& info = backends[i];
if (api == info.id)
{
CV_Assert(!info.backendFactory.empty());
CV_Assert(!info.backendFactory->isBuiltIn());
return getCapturePluginVersion(info.backendFactory, version_ABI, version_API);
}
}
CV_Error(Error::StsError, "Unknown or wrong backend ID");
}
std::string getStreamBackendPluginVersion(VideoCaptureAPIs api,
CV_OUT int& version_ABI,
CV_OUT int& version_API
)
{
const std::vector<VideoBackendInfo> backends = VideoBackendRegistry::getInstance().getAvailableBackends_CaptureByFilename();
for (size_t i = 0; i < backends.size(); i++)
{
const VideoBackendInfo& info = backends[i];
if (api == info.id)
{
CV_Assert(!info.backendFactory.empty());
CV_Assert(!info.backendFactory->isBuiltIn());
return getCapturePluginVersion(info.backendFactory, version_ABI, version_API);
}
}
CV_Error(Error::StsError, "Unknown or wrong backend ID");
}
/** @brief Returns description and ABI/API version of videoio plugin's writer interface */
std::string getWriterBackendPluginVersion(VideoCaptureAPIs api,
CV_OUT int& version_ABI,
CV_OUT int& version_API
)
{
const std::vector<VideoBackendInfo> backends = VideoBackendRegistry::getInstance().getAvailableBackends_Writer();
for (size_t i = 0; i < backends.size(); i++)
{
const VideoBackendInfo& info = backends[i];
if (api == info.id)
{
CV_Assert(!info.backendFactory.empty());
CV_Assert(!info.backendFactory->isBuiltIn());
return getWriterPluginVersion(info.backendFactory, version_ABI, version_API);
}
}
CV_Error(Error::StsError, "Unknown or wrong backend ID");
}
} // namespace registry } // namespace registry
} // namespace } // namespace

@ -0,0 +1,105 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
namespace opencv_test { namespace {
enum VideoBackendMode
{
MODE_CAMERA,
MODE_STREAM,
MODE_WRITER,
};
static
void dumpBackendInfo(VideoCaptureAPIs backend, enum VideoBackendMode mode)
{
std::string name;
try
{
name = videoio_registry::getBackendName(backend);
}
catch (const std::exception& e)
{
ADD_FAILURE() << "Can't query name of backend=" << backend << ": " << e.what();
}
catch (...)
{
ADD_FAILURE() << "Can't query name of backend=" << backend << ": unknown C++ exception";
}
bool isBuiltIn = true;
try
{
isBuiltIn = videoio_registry::isBackendBuiltIn(backend);
}
catch (const std::exception& e)
{
ADD_FAILURE() << "Failed isBackendBuiltIn(backend=" << backend << "): " << e.what();
cout << name << " - UNKNOWN TYPE" << endl;
return;
}
if (isBuiltIn)
{
cout << name << " - BUILTIN" << endl;
return;
}
std::string description = "NO_DESCRIPTION";
int version_ABI = 0;
int version_API = 0;
try
{
if (mode == MODE_CAMERA)
description = videoio_registry::getCameraBackendPluginVersion(backend, version_ABI, version_API);
else if (mode == MODE_STREAM)
description = videoio_registry::getStreamBackendPluginVersion(backend, version_ABI, version_API);
else if (mode == MODE_WRITER)
description = videoio_registry::getWriterBackendPluginVersion(backend, version_ABI, version_API);
else
CV_Error(Error::StsInternal, "");
cout << name << " - PLUGIN (" << description << ") ABI=" << version_ABI << " API=" << version_API << endl;
return;
}
catch (const cv::Exception& e)
{
if (e.code == Error::StsNotImplemented)
{
cout << name << " - PLUGIN - NOT LOADED" << endl;
return;
}
ADD_FAILURE() << "Failed getBackendPluginDescription(backend=" << backend << "): " << e.what();
}
catch (const std::exception& e)
{
ADD_FAILURE() << "Failed getBackendPluginDescription(backend=" << backend << "): " << e.what();
}
cout << name << " - PLUGIN (ERROR on quering information)" << endl;
}
TEST(VideoIO_Plugins, query)
{
const std::vector<cv::VideoCaptureAPIs> camera_backends = cv::videoio_registry::getCameraBackends();
cout << "== Camera APIs (" << camera_backends.size() << "):" << endl;
for (auto backend : camera_backends)
{
dumpBackendInfo(backend, MODE_CAMERA);
}
const std::vector<cv::VideoCaptureAPIs> stream_backends = cv::videoio_registry::getStreamBackends();
cout << "== Stream capture APIs (" << stream_backends.size() << "):" << endl;
for (auto backend : stream_backends)
{
dumpBackendInfo(backend, MODE_STREAM);
}
const std::vector<cv::VideoCaptureAPIs> writer_backends = cv::videoio_registry::getWriterBackends();
cout << "== Writer APIs (" << writer_backends.size() << "):" << endl;
for (auto backend : writer_backends)
{
dumpBackendInfo(backend, MODE_WRITER);
}
}
}}
Loading…
Cancel
Save