From dfb574c179e50487d90696bb90386d80787f5c43 Mon Sep 17 00:00:00 2001 From: John Zhao Date: Mon, 4 Jun 2018 22:09:20 +0800 Subject: [PATCH] Support find plugin to load --- CMakeLists.txt | 15 ++++ include/mynteye/mynteye.h.in | 3 + src/api/api.cc | 151 +++++++++++++++++++++++++++++++++++ src/api/synthetic.cc | 4 + src/api/synthetic.h | 1 + src/internal/strings.cc | 7 ++ src/internal/strings.h | 3 + 7 files changed, 184 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f540b5..d730c65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/include/mynteye/mynteye.h.in b/include/mynteye/mynteye.h.in index 6ebbcf8..dd44894 100644 --- a/include/mynteye/mynteye.h.in +++ b/include/mynteye/mynteye.h.in @@ -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_ diff --git a/src/api/api.cc b/src/api/api.cc index bafd3ba..f56582e 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -13,8 +13,14 @@ // limitations under the License. #include "api/api.h" +#ifdef WITH_BOOST_FILESYSTEM +#include +#include +#endif + #include +#include #include #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 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 names; + { + std::vector 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 paths; + + std::vector plats; + if (host_name != host_os) { + plats.push_back(host_name + "-" + host_arch); + } + plats.push_back(host_os + "-" + host_arch); + + std::vector 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), 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(); diff --git a/src/api/synthetic.cc b/src/api/synthetic.cc index 94bd351..fd86ede 100644 --- a/src/api/synthetic.cc +++ b/src/api/synthetic.cc @@ -235,6 +235,10 @@ void Synthetic::SetPlugin(std::shared_ptr 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)) { diff --git a/src/api/synthetic.h b/src/api/synthetic.h index 1a927bd..2153ed9 100644 --- a/src/api/synthetic.h +++ b/src/api/synthetic.h @@ -62,6 +62,7 @@ class Synthetic { std::vector GetStreamDatas(const Stream &stream); void SetPlugin(std::shared_ptr plugin); + bool HasPlugin() const; private: void InitStreamSupports(); diff --git a/src/internal/strings.cc b/src/internal/strings.cc index 15ec344..7ce294b 100644 --- a/src/internal/strings.cc +++ b/src/internal/strings.cc @@ -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 split( const std::string &text, const std::string &delimiters) { std::vector tokens; diff --git a/src/internal/strings.h b/src/internal/strings.h index 9311748..a6ccdcd 100644 --- a/src/internal/strings.h +++ b/src/internal/strings.h @@ -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 split( const std::string &text, const std::string &delimiters);