mirror of
https://github.com/opencv/opencv.git
synced 2026-11-23 17:57:56 -06:00
G-API: Introduce a Queue Source #24178 - Added a new IStreamSource class: in fact, a wrapper over a concurrent queue; - Added minimal example on how it can be used; - Extended IStreamSource with optional "halt" interface to break the blocking calls in the emitter threads when required to stop. - Introduced a QueueInput class which allows to pass the whole graph's input vector at once. In fact it is a thin wrapper atop of individual Queue Sources. There is a hidden trap found with our type system as described in https://github.com/orgs/g-api-org/discussions/2 While it works even in this form, it should be addressed somewhere in the 5.0 timeframe. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
99 lines
2.3 KiB
C++
99 lines
2.3 KiB
C++
// 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.
|
|
//
|
|
// Copyright (C) 2023 Intel Corporation
|
|
|
|
#include <chrono>
|
|
#include <atomic>
|
|
|
|
#include <ade/util/zip_range.hpp>
|
|
|
|
#include <opencv2/gapi/streaming/queue_source.hpp>
|
|
#include <opencv2/gapi/streaming/meta.hpp>
|
|
|
|
#include "executor/conc_queue.hpp"
|
|
|
|
namespace cv {
|
|
namespace gapi {
|
|
namespace wip {
|
|
|
|
class QueueSourceBase::Priv {
|
|
public:
|
|
explicit Priv(const cv::GMetaArg &meta) {
|
|
m = meta;
|
|
halted = false;
|
|
}
|
|
|
|
cv::GMetaArg m;
|
|
cv::gapi::own::concurrent_bounded_queue<cv::GRunArg> q;
|
|
int64_t c = 0;
|
|
std::atomic<bool> halted;
|
|
};
|
|
|
|
QueueSourceBase::QueueSourceBase(const cv::GMetaArg &m)
|
|
: m_priv(new Priv(m)) {
|
|
}
|
|
|
|
void QueueSourceBase::push(Data &&data) {
|
|
|
|
// Tag data with seq_id/ts
|
|
const auto now = std::chrono::system_clock::now();
|
|
const auto dur = std::chrono::duration_cast<std::chrono::microseconds>
|
|
(now.time_since_epoch());
|
|
data.meta[cv::gapi::streaming::meta_tag::timestamp] = int64_t{dur.count()};
|
|
data.meta[cv::gapi::streaming::meta_tag::seq_id] = int64_t{m_priv->c++};
|
|
|
|
m_priv->q.push(data);
|
|
}
|
|
|
|
bool QueueSourceBase::pull(Data &data) {
|
|
m_priv->q.pop(data);
|
|
|
|
if (m_priv->halted) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void QueueSourceBase::halt() {
|
|
m_priv->halted.store(true);
|
|
m_priv->q.push(cv::GRunArg{});
|
|
}
|
|
|
|
cv::GMetaArg QueueSourceBase::descr_of() const {
|
|
return m_priv->m;
|
|
}
|
|
|
|
QueueInput::QueueInput(const cv::GMetaArgs &args) {
|
|
for (auto &&m : args) {
|
|
m_sources.emplace_back(new cv::gapi::wip::QueueSourceBase(m));
|
|
}
|
|
}
|
|
|
|
void QueueInput::push(cv::GRunArgs &&args) {
|
|
GAPI_Assert(m_sources.size() == args.size());
|
|
for (auto && it : ade::util::zip(ade::util::toRange(m_sources),
|
|
ade::util::toRange(args)))
|
|
{
|
|
auto &src = std::get<0>(it);
|
|
auto &obj = std::get<1>(it);
|
|
|
|
Data d;
|
|
d = obj;
|
|
src->push(std::move(d));
|
|
}
|
|
}
|
|
|
|
QueueInput::operator cv::GRunArgs () {
|
|
cv::GRunArgs args;
|
|
for (auto &&s : m_sources) {
|
|
args.push_back(s->ptr());
|
|
}
|
|
return args;
|
|
}
|
|
|
|
} // wip
|
|
} // gapi
|
|
} // cv
|