Support find plugin to load

This commit is contained in:
John Zhao 2018-06-04 22:09:20 +08:00
parent b7436e41b9
commit dfb574c179
7 changed files with 184 additions and 0 deletions

View File

@ -52,6 +52,15 @@ if(WITH_API)
include(cmake/DetectOpenCV.cmake)
endif()
find_package(Boost COMPONENTS filesystem)
if(Boost_FOUND)
set(WITH_BOOST_FILESYSTEM true)
add_definitions(-DWITH_FILESYSTEM)
add_definitions(-DWITH_BOOST_FILESYSTEM)
message(STATUS "Found boost filesystem: ${Boost_VERSION}")
#message(STATUS " Boost_LIBRARIES: ${Boost_LIBRARIES}")
endif()
LIST(APPEND CMAKE_MODULE_PATH cmake)
include(CMakePackageConfigHelpers)
@ -63,6 +72,9 @@ set(MYNTEYE_NAME ${PROJECT_NAME})
set(MYNTEYE_NAMESPACE "mynteye")
message(STATUS "Namespace: ${MYNTEYE_NAMESPACE}")
file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" MYNTEYE_SDK_ROOT_DIR)
file(TO_NATIVE_PATH "${CMAKE_INSTALL_PREFIX}" MYNTEYE_SDK_INSTALL_DIR)
configure_file(
include/mynteye/mynteye.h.in
include/mynteye/mynteye.h @ONLY
@ -167,6 +179,9 @@ set(MYNTEYE_LINKLIBS
if(WITH_API)
list(APPEND MYNTEYE_LINKLIBS ${OpenCV_LIBS})
endif()
if(WITH_BOOST_FILESYSTEM)
list(APPEND MYNTEYE_LINKLIBS ${Boost_LIBRARIES})
endif()
#message(STATUS "MYNTEYE_LINKLIBS: ${MYNTEYE_LINKLIBS}")
add_library(${MYNTEYE_NAME} SHARED ${MYNTEYE_SRCS})

View File

@ -58,4 +58,7 @@ MYNTEYE_API_VERSION_CHECK( \
# define MYNTEYE_USE_NAMESPACE
#endif
constexpr char MYNTEYE_SDK_ROOT_DIR[] = "@MYNTEYE_SDK_ROOT_DIR@";
constexpr char MYNTEYE_SDK_INSTALL_DIR[] = "@MYNTEYE_SDK_INSTALL_DIR@";
#endif // MYNTEYE_MYNTEYE_H_

View File

@ -13,8 +13,14 @@
// limitations under the License.
#include "api/api.h"
#ifdef WITH_BOOST_FILESYSTEM
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#endif
#include <glog/logging.h>
#include <algorithm>
#include <thread>
#include "mynteye/glog_init.h"
@ -27,6 +33,143 @@
MYNTEYE_BEGIN_NAMESPACE
namespace {
#ifdef WITH_FILESYSTEM
#ifdef WITH_BOOST_FILESYSTEM
namespace fs = boost::filesystem;
bool file_exists(const fs::path &p) {
try {
fs::file_status s = fs::status(p);
return fs::exists(s) && fs::is_regular_file(s);
} catch (fs::filesystem_error &e) {
LOG(ERROR) << e.what();
return false;
}
}
bool dir_exists(const fs::path &p) {
try {
fs::file_status s = fs::status(p);
return fs::exists(s) && fs::is_directory(s);
} catch (fs::filesystem_error &e) {
LOG(ERROR) << e.what();
return false;
}
}
#endif
std::vector<std::string> get_plugin_paths() {
std::string info_path(MYNTEYE_SDK_INSTALL_DIR);
info_path.append(OS_SEP "share" OS_SEP "mynteye" OS_SEP "build.info");
cv::FileStorage fs(info_path, cv::FileStorage::READ);
if (!fs.isOpened()) {
// LOG(ERROR) << "build.info not found";
return {};
}
auto to_lower = [](std::string &s) { // NOLINT
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
};
std::string host_os = fs["HOST_OS"];
to_lower(host_os);
std::string host_name = fs["HOST_NAME"];
to_lower(host_name);
std::string host_arch = fs["HOST_ARCH"];
to_lower(host_arch);
// std::string gcc_version = fs["GCC_VERSION"];
int gcc_version_major = fs["GCC_VERSION_MAJOR"];
// int gcc_version_minor = fs["GCC_VERSION_MINOR"];
std::string cuda_version = fs["CUDA_VERSION"];
// int cuda_version_major = fs["CUDA_VERSION_MAJOR"];
// int cuda_version_minor = fs["CUDA_VERSION_MINOR"];
// std::string cuda_version_string = fs["CUDA_VERSION_STRING"];
std::string opencv_version = fs["OpenCV_VERSION"];
// int opencv_version_major = fs["OpenCV_VERSION_MAJOR"];
// int opencv_version_minor = fs["OpenCV_VERSION_MINOR"];
// int opencv_version_patch = fs["OpenCV_VERSION_PATCH"];
// int opencv_version_tweak = fs["OpenCV_VERSION_TWEAK"];
// std::string opencv_version_status = fs["OpenCV_VERSION_STATUS"];
std::string mynteye_version = fs["MYNTEYE_VERSION"];
// int mynteye_version_major = fs["MYNTEYE_VERSION_MAJOR"];
// int mynteye_version_minor = fs["MYNTEYE_VERSION_MINOR"];
// int mynteye_version_patch = fs["MYNTEYE_VERSION_PATCH"];
// int mynteye_version_tweak = fs["MYNTEYE_VERSION_TWEAK"];
fs.release();
std::string lib_prefix;
std::string lib_suffix;
if (host_os == "linux") {
if (gcc_version_major < 5)
return {};
lib_prefix = "lib";
lib_suffix = ".so";
} else if (host_os == "win") {
lib_prefix = "";
lib_suffix = ".dll";
} else if (host_os == "mac") {
lib_prefix = "lib";
lib_suffix = ".dylib";
} else {
return {};
}
std::vector<std::string> names;
{
std::vector<std::string> prefixes{
// lib_prefix + "plugin_b_ocl" + ocl_version,
lib_prefix + "plugin_g_cuda" + cuda_version,
};
for (auto &&prefix : prefixes) {
names.push_back(
prefix + "_opencv" + opencv_version + "_mynteye" + mynteye_version);
names.push_back(prefix + "_opencv" + opencv_version);
names.push_back(prefix);
}
for (auto &&name : names) {
name.append(lib_suffix);
}
}
std::vector<std::string> paths;
std::vector<std::string> plats;
if (host_name != host_os) {
plats.push_back(host_name + "-" + host_arch);
}
plats.push_back(host_os + "-" + host_arch);
std::vector<std::string> dirs{MYNTEYE_SDK_ROOT_DIR, MYNTEYE_SDK_INSTALL_DIR};
for (auto &&plat : plats) {
for (auto &&dir : dirs) {
auto &&plat_dir = dir + OS_SEP "plugins" + OS_SEP + plat;
// VLOG(2) << "plat_dir: " << plat_dir;
if (!dir_exists(plat_dir))
continue;
for (auto &&name : names) {
// VLOG(2) << " name: " << name;
auto &&path = plat_dir + OS_SEP + name;
if (!file_exists(path))
continue;
paths.push_back(path);
}
}
}
return paths;
}
#endif
} // namespace
API::API(std::shared_ptr<Device> device)
: device_(device), synthetic_(new Synthetic(this)) {
VLOG(2) << __func__;
@ -156,6 +299,14 @@ bool API::HasMotionCallback() const {
void API::Start(const Source &source) {
if (source == Source::VIDEO_STREAMING) {
#ifdef WITH_FILESYSTEM
if (!synthetic_->HasPlugin()) {
auto &&plugin_paths = get_plugin_paths();
if (plugin_paths.size() > 0) {
EnablePlugin(plugin_paths[0]);
}
}
#endif
synthetic_->StartVideoStreaming();
} else if (source == Source::MOTION_TRACKING) {
device_->StartMotionTracking();

View File

@ -235,6 +235,10 @@ void Synthetic::SetPlugin(std::shared_ptr<Plugin> plugin) {
plugin_ = plugin;
}
bool Synthetic::HasPlugin() const {
return plugin_ != nullptr;
}
void Synthetic::InitStreamSupports() {
auto &&device = api_->device();
if (device->Supports(Stream::LEFT) && device->Supports(Stream::RIGHT)) {

View File

@ -62,6 +62,7 @@ class Synthetic {
std::vector<api::StreamData> GetStreamDatas(const Stream &stream);
void SetPlugin(std::shared_ptr<Plugin> plugin);
bool HasPlugin() const;
private:
void InitStreamSupports();

View File

@ -65,6 +65,13 @@ bool starts_with(const std::string &text, const std::string &prefix) {
return text.compare(0, prefix.length(), prefix) == 0;
}
bool ends_with(const std::string &text, const std::string &suffix) {
if (suffix.length() > text.length())
return false;
return text.compare(
text.length() - suffix.length(), suffix.length(), suffix) == 0;
}
std::vector<std::string> split(
const std::string &text, const std::string &delimiters) {
std::vector<std::string> tokens;

View File

@ -41,6 +41,9 @@ int hex2int(const std::string &text);
MYNTEYE_API
bool starts_with(const std::string &text, const std::string &prefix);
MYNTEYE_API
bool ends_with(const std::string &text, const std::string &suffix);
MYNTEYE_API
std::vector<std::string> split(
const std::string &text, const std::string &delimiters);