Skip to content

Commit

Permalink
merged the updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixstars-momoko committed Aug 30, 2024
2 parents 0126697 + fa55e64 commit 70d1666
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 110 deletions.
80 changes: 42 additions & 38 deletions src/bb/image-io/rt_u3v.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,13 @@ class U3V {

protected:
U3V(int32_t num_sensor, bool frame_sync, bool realtime_display_mode, bool sim_mode, int32_t width, int32_t height , float_t fps, const std::string & pixel_format, char* dev_id = nullptr)
: gobject_(GOBJECT_FILE, true), aravis_(ARAVIS_FILE, true),
: gobject_(GOBJECT_FILE, true), aravis_(ARAVIS_FILE, true, true),
num_sensor_(num_sensor), frame_count_method_(FrameCountMethod::UNAVAILABLE),
frame_sync_(frame_sync), realtime_display_mode_(realtime_display_mode), is_gendc_(false), is_param_integer_(false),
devices_(num_sensor), buffers_(num_sensor), operation_mode_(OperationMode::Came1USB1), frame_cnt_(0), device_idx_(-1), disposed_(false), sim_mode_(sim_mode)
devices_(num_sensor), buffers_(num_sensor), operation_mode_(OperationMode::Came1USB1), frame_cnt_(0), device_idx_(-1), disposed_(false), sim_mode_(sim_mode), order_filp_(false)
{
init_symbols();
log::debug("U3V:: 23-11-18 : updating obtain and write");
log::debug("U3V:: 24-08-29 : revert the order of AcquisitionStart and create stream as a default");
log::info("Using aravis-{}.{}.{}", arv_get_major_version(), arv_get_minor_version(), arv_get_micro_version());
}

Expand All @@ -359,9 +359,6 @@ class U3V {
}

void init_symbols_aravis() {
if (!aravis_.is_available()) {
throw ::std::runtime_error("libaravis-0.8 is unavailable on your system.");
}

#define GET_SYMBOL(LOCAL_VAR, TARGET_SYMBOL) \
LOCAL_VAR = aravis_.get_symbol<LOCAL_VAR##_t>(TARGET_SYMBOL); \
Expand Down Expand Up @@ -487,6 +484,23 @@ class U3V {
return err_;
}

GError* CommandAcquisitionModeContdStart(){
for (auto i=0; i<devices_.size(); ++i) {
arv_device_set_string_feature_value(devices_[i].device_, "AcquisitionMode", arv_acquisition_mode_to_string(ARV_ACQUISITION_MODE_CONTINUOUS), &err_);
if (err_) {
throw std::runtime_error(err_->message);
}
log::info("\tDevice/USB {}::{} : {}", i, "Command", "AcquisitionMode");

arv_device_execute_command(devices_[i].device_, "AcquisitionStart", &err_);
if (err_) {
throw std::runtime_error(err_->message);
}
log::info("\tDevice/USB {}::{} : {}", i, "Command", "AcquisitionStart");
}
return err_;
}

g_object_unref_t g_object_unref;

arv_get_major_version_t arv_get_major_version;
Expand Down Expand Up @@ -578,7 +592,7 @@ class U3V {
bool disposed_;
bool sim_mode_;


bool order_filp_;

}; // class U3V

Expand Down Expand Up @@ -1069,27 +1083,18 @@ class U3VRealCam: public U3V{
}else{
log::info("\tDevice/USB {}::{} : {} ... skipped", index_on_opened_device, "device protocol", device_protocol);
}
}

index_on_detected_device += 1;
}
/*
* ion-kit starts the acquisition before stream creation This is a tentative fix only in ion-kit due to hardware issue
* In aravis, the acquisition should be done afterward. Since this maps better with GenAPI, where buffers
* must be pushed to DataStream objectsbefore DataStream acquisition is started.
*/
for (auto i=0; i<devices_.size(); ++i) {
arv_device_set_string_feature_value(devices_[i].device_, "AcquisitionMode", arv_acquisition_mode_to_string(ARV_ACQUISITION_MODE_CONTINUOUS), &err_);
if (err_) {
throw std::runtime_error(err_->message);
}
log::info("\tDevice/USB {}::{} : {}", i, "Command", "AcquisitionMode");

arv_device_execute_command(devices_[i].device_, "AcquisitionStart", &err_);
if (err_) {
throw std::runtime_error(err_->message);
}
log::info("\tDevice/USB {}::{} : {}", i, "Command", "AcquisitionStart");
* ion-kit starts the acquisition before stream creation This is a tentative fix only in ion-kit due to hardware issue
* In aravis, the acquisition should be done afterward. Since this maps better with GenAPI, where buffers
* must be pushed to DataStream objectsbefore DataStream acquisition is started.
* refer to https://github.com/AravisProject/aravis/blob/2ebaa8661761ea4bbc4df878aa67b4a9e1a9a3b9/docs/reference/aravis/porting-0.10.md
*/
if (order_filp_){
err_ = CommandAcquisitionModeContdStart();
}

//start streaming after AcquisitionStart
for (auto i=0; i<devices_.size(); ++i) {
devices_[i].stream_ = arv_device_create_stream(devices_[i].device_, nullptr, nullptr, &err_);
Expand All @@ -1101,6 +1106,10 @@ class U3VRealCam: public U3V{
}
}

if (! order_filp_){
err_ = CommandAcquisitionModeContdStart();
}

for (auto i=0; i<devices_.size(); ++i) {
const size_t buffer_size = 1 * 1024 * 1024 * 1024; // 1GiB for each
auto n = (buffer_size + devices_[i].u3v_payload_size_ - 1) / devices_[i].u3v_payload_size_;
Expand Down Expand Up @@ -1574,6 +1583,8 @@ class U3VGenDC: public U3V{
}else{
log::info("\tDevice/USB {}::{} : {} ... skipped", index_on_opened_device, "device protocol", device_protocol);
}

index_on_detected_device += 1;
}

/*
Expand All @@ -1582,20 +1593,9 @@ class U3VGenDC: public U3V{
* must be pushed to DataStream objectsbefore DataStream acquisition is started.
* refer to https://github.com/AravisProject/aravis/blob/2ebaa8661761ea4bbc4df878aa67b4a9e1a9a3b9/docs/reference/aravis/porting-0.10.md
*/
for (auto i=0; i<devices_.size(); ++i) {
arv_device_set_string_feature_value(devices_[i].device_, "AcquisitionMode", arv_acquisition_mode_to_string(ARV_ACQUISITION_MODE_CONTINUOUS), &err_);
if (err_) {
throw std::runtime_error(err_->message);
}
log::info("\tDevice/USB {}::{} : {}", i, "Command", "AcquisitionMode");

arv_device_execute_command(devices_[i].device_, "AcquisitionStart", &err_);
if (err_) {
throw std::runtime_error(err_->message);
}
log::info("\tDevice/USB {}::{} : {}", i, "Command", "AcquisitionStart");
if (order_filp_){
err_ = CommandAcquisitionModeContdStart();
}

//start streaming after AcquisitionStart
for (auto i=0; i<devices_.size(); ++i) {
devices_[i].stream_ = arv_device_create_stream(devices_[i].device_, nullptr, nullptr, &err_);
Expand All @@ -1607,6 +1607,10 @@ class U3VGenDC: public U3V{
}
}

if (! order_filp_){
err_ = CommandAcquisitionModeContdStart();
}

for (auto i=0; i<devices_.size(); ++i) {
const size_t buffer_size = 1 * 1024 * 1024 * 1024; // 1GiB for each
auto n = (buffer_size + devices_[i].u3v_payload_size_ - 1) / devices_[i].u3v_payload_size_;
Expand Down
200 changes: 128 additions & 72 deletions src/dynamic_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#if _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <psapi.h>
#define ION_DYNAMIC_MODULE_PREFIX ""
#define ION_DYNAMIC_MODULE_EXT ".dll"
#elif __APPLE__
Expand All @@ -29,100 +30,155 @@ bool has_prefix_and_ext(const std::string& n) {
namespace ion {

class DynamicModule {
public:
public:
#ifdef _WIN32
using Handle = HMODULE;
#else
using Handle = void *;
#endif

DynamicModule(const std::string& module_name_or_path, bool essential = true, bool lazy_load = false) {
if (module_name_or_path == "") {
handle_ = nullptr;
return;
}

// set target
std::string target;
if (std::filesystem::exists(module_name_or_path) || has_prefix_and_ext(module_name_or_path)) {
// This is absolute path or file name
target = module_name_or_path;
} else {
target = std::string(ION_DYNAMIC_MODULE_PREFIX) + module_name_or_path + std::string(ION_DYNAMIC_MODULE_EXT);
}
target_ = target;
essential_ = essential;

if (lazy_load) {
#ifdef _WIN32
using Handle = HMODULE;
handle_ = nullptr;
#else
using Handle = void*;
handle_ = RTLD_DEFAULT;
#endif
} else {

DynamicModule(const std::string& module_name_or_path, bool essential = true) {
if (module_name_or_path == "") {
handle_ = nullptr;
return;
}

std::string target;
if (std::filesystem::exists(module_name_or_path) || has_prefix_and_ext(module_name_or_path)) {
// This is absolute path or file name
target = module_name_or_path;
} else {
target = std::string(ION_DYNAMIC_MODULE_PREFIX) + module_name_or_path + std::string(ION_DYNAMIC_MODULE_EXT);
}

// TODO: WIP: test moduel_name_or_path using std::filesystem
// TODO: WIP: test moduel_name_or_path using std::filesystem
#ifdef _WIN32
handle_ = LoadLibraryA(target.c_str());
handle_ = LoadLibraryA(target.c_str());
#else
handle_ = dlopen(target.c_str(), RTLD_NOW);
handle_ = dlopen(target.c_str(), RTLD_NOW);
#endif
if (handle_ == nullptr) {
if (essential) {
throw std::runtime_error(getErrorString());
} else {
log::warn("Not found inessential library {} : {}", target, getErrorString());
}
}
}

~DynamicModule() {
if (handle_ != nullptr) {
// NOTE: DSO which is loaded by with_bb_module should not be unloaded even if Builder is destructed.
// Loading more than twice does not have any side effects.
}
}
if (handle_ == nullptr) {
if (essential) {
throw std::runtime_error(getErrorString());
} else {
log::warn("Not found inessential library {} : {}", target, getErrorString());
}
}
}
}

~DynamicModule() {
if (handle_ != nullptr) {
// NOTE: DSO which is loaded by with_bb_module should not be unloaded even if Builder is destructed.
// Loading more than twice does not have any side effects.
}
}

bool is_available(void) const {
return handle_ != NULL;
return handle_ != nullptr;
}

template<typename T>
T get_symbol(const std::string &symbol_name) const {
#if defined(_WIN32)
return reinterpret_cast<T>(GetProcAddress(handle_, symbol_name.c_str()));
T get_symbol(const std::string &symbol_name) {
#ifdef _WIN32
if (handle_ != nullptr) {
return reinterpret_cast<T>(GetProcAddress(handle_, symbol_name.c_str()));
} else {
// search symbol globally first
Handle hmods[1024]; // the array size should be large enough
DWORD cb_needed;
if (EnumProcessModules(GetCurrentProcess(), hmods, sizeof(hmods), &cb_needed)) {
for (unsigned int i = 0; i < (cb_needed / sizeof(HMODULE)); i++) {
char path[MAX_PATH];
// Get the module name
if (GetModuleFileNameA(hmods[i], path, sizeof(path) / sizeof(char))) {
// Try to get the address of the symbol if module path includes target_
std::string module_path(path);
if (module_path.find(target_) != std::string::npos) {
handle_ = hmods[i];
return reinterpret_cast<T>(GetProcAddress(hmods[i], symbol_name.c_str()));
}
}
}
}
// failed to load symbol gloablly, load it explicitly
handle_ = LoadLibraryA(target_.c_str());
if (handle_ != nullptr) {
log::info("Lazy loading library {}", target_);
} else {
if (essential_) {
throw std::runtime_error(getErrorString());
}
}
return reinterpret_cast<T>(GetProcAddress(handle_, symbol_name.c_str()));
}
#else
return reinterpret_cast<T>(dlsym(handle_, symbol_name.c_str()));
if (handle_ == RTLD_DEFAULT) {
void *func_ptr = dlsym(handle_, symbol_name.c_str());
if (func_ptr != nullptr) {
return reinterpret_cast<T>(func_ptr);
} else {
handle_ = dlopen(target_.c_str(), RTLD_NOW);
if (handle_ != nullptr) {
log::info("Lazy loading library {}", target_);
} else {
if (essential_) {
throw std::runtime_error(getErrorString());
}
}
return reinterpret_cast<T>(dlsym(handle_, symbol_name.c_str()));
}
} else {
return reinterpret_cast<T>(dlsym(handle_, symbol_name.c_str()));
}
#endif
}


private:

std::string getErrorString(void) const {
std::string error_msg;
private:
std::string getErrorString(void) const {
std::string error_msg;

#ifdef _WIN32
DWORD error = GetLastError();
if (error)
{
LPVOID lpMsgBuf;
DWORD bufLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
error,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(LPTSTR) &lpMsgBuf, 0, nullptr );
if (bufLen)
{
LPCSTR lpMsgStr = (LPCSTR)lpMsgBuf;
error_msg = std::string(lpMsgStr, lpMsgStr+bufLen);
LocalFree(lpMsgBuf);
}
}
DWORD error = GetLastError();
if (error) {
LPVOID lpMsgBuf;
DWORD bufLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
error,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(LPTSTR)&lpMsgBuf, 0, nullptr);
if (bufLen) {
LPCSTR lpMsgStr = (LPCSTR)lpMsgBuf;
error_msg = std::string(lpMsgStr, lpMsgStr + bufLen);
LocalFree(lpMsgBuf);
}
}
#else
const char* buf(dlerror());
error_msg.assign(buf ? buf : "none");
const char *buf(dlerror());
error_msg.assign(buf ? buf : "none");
#endif
return error_msg;
}

return error_msg;
}

Handle handle_;
Handle handle_;
std::string target_;
bool essential_;
};

} // namespace ion
} // namespace ion

#endif // ION_DYNAMIC_MODULE_H
#endif // ION_DYNAMIC_MODULE_H

0 comments on commit 70d1666

Please sign in to comment.