Skip to content

Commit

Permalink
WIP: unified sync_frame_count()
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixstars-momoko committed Aug 30, 2024
1 parent cfedbfe commit ffe048c
Showing 1 changed file with 44 additions and 45 deletions.
89 changes: 44 additions & 45 deletions src/bb/image-io/rt_u3v.h
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,50 @@ class U3V {
}
}

void sync_frame_count(std::vector<ArvBuffer *> &bufs, int timeout_us){
uint32_t max_cnt = 0;
while (true) {
// Update max_cnt
for (int i = 0; i < num_sensor_; ++i) {
if (max_cnt < devices_[i].frame_count_) {
max_cnt = devices_[i].frame_count_;
}
}

// Check all count is same as max_cnt;
bool synchronized = true;
for (int i = 0; i < num_sensor_; ++i) {
synchronized &= devices_[i].frame_count_ == max_cnt;
}

// If it is synchronized, break the loop
if (synchronized) {
break;
}

// Acquire buffer until cnt is at least max_cnt
for (int i = 0; i < devices_.size(); ++i) {
while (devices_[i].frame_count_ < max_cnt) {
arv_stream_push_buffer(devices_[i].stream_, bufs[i]);
bufs[i] = arv_stream_timeout_pop_buffer(devices_[i].stream_, timeout_us);

if (bufs[i] == nullptr) {
log::error("pop_buffer failed when sync frame due to timeout ({}s)", timeout_us * 1e-6f);
throw ::std::runtime_error("buffer is null");
}
devices_[i].frame_count_ = frame_count_method_ == FrameCountMethod::TYPESPECIFIC3
? static_cast<uint32_t>(get_frame_count_from_genDC_descriptor(bufs[i], devices_[i]))
: frame_count_method_ == FrameCountMethod::TIMESTAMP
? static_cast<uint32_t>(arv_buffer_get_timestamp(bufs[i]) & 0x00000000FFFFFFFF)
: -1;
i == 0 ?
log::trace("All-Popped Frames (USB0, USB1)=({:20}, {:20})", devices_[i].frame_count_, "") :
log::trace("All-Popped Frames (USB0, USB1)=({:20}, {:20})", "", devices_[i].frame_count_);
}
}
}
}

g_object_unref_t g_object_unref;

arv_get_major_version_t arv_get_major_version;
Expand Down Expand Up @@ -1093,51 +1137,6 @@ class U3VRealCam: public U3V{
}
};


void sync_frame_count(std::vector<ArvBuffer *> &bufs, int timeout_us){
uint32_t max_cnt = 0;
while (true) {
// Update max_cnt
for (int i = 0; i < num_sensor_; ++i) {
if (max_cnt < devices_[i].frame_count_) {
max_cnt = devices_[i].frame_count_;
}
}

// Check all count is same as max_cnt;
bool synchronized = true;
for (int i = 0; i < num_sensor_; ++i) {
synchronized &= devices_[i].frame_count_ == max_cnt;
}

// If it is synchronized, break the loop
if (synchronized) {
break;
}

// Acquire buffer until cnt is at least max_cnt
for (int i = 0; i < devices_.size(); ++i) {
while (devices_[i].frame_count_ < max_cnt) {
arv_stream_push_buffer(devices_[i].stream_, bufs[i]);
bufs[i] = arv_stream_timeout_pop_buffer(devices_[i].stream_, timeout_us);

if (bufs[i] == nullptr) {
log::error("pop_buffer failed when sync frame due to timeout ({}s)", timeout_us * 1e-6f);
throw ::std::runtime_error("buffer is null");
}
devices_[i].frame_count_ = frame_count_method_ == FrameCountMethod::TYPESPECIFIC3
? static_cast<uint32_t>(get_frame_count_from_genDC_descriptor(bufs[i], devices_[i]))
: frame_count_method_ == FrameCountMethod::TIMESTAMP
? static_cast<uint32_t>(arv_buffer_get_timestamp(bufs[i]) & 0x00000000FFFFFFFF)
: -1;
i == 0 ?
log::trace("All-Popped Frames (USB0, USB1)=({:20}, {:20})", devices_[i].frame_count_, "") :
log::trace("All-Popped Frames (USB0, USB1)=({:20}, {:20})", "", devices_[i].frame_count_);
}
}
}
}

void consume_old_buffer(std::vector<ArvBuffer *> &bufs, int timeout_us = 3 * 1000 * 1000){
std::vector<int32_t> N_output_buffers(num_sensor_);
for (auto i = 0; i < num_sensor_; ++i) {
Expand Down

0 comments on commit ffe048c

Please sign in to comment.