#ifndef OPENCV_GAPI_PIPELINE_MODELING_TOOL_DUMMY_SOURCE_HPP #define OPENCV_GAPI_PIPELINE_MODELING_TOOL_DUMMY_SOURCE_HPP #include #include #include #include #include // cv::gapi::wip::IStreamSource #include "utils.hpp" class DummySource final: public cv::gapi::wip::IStreamSource { public: using Ptr = std::shared_ptr; DummySource(const double latency, const OutputDescr& output); bool pull(cv::gapi::wip::Data& data) override; cv::GMetaArg descr_of() const override; private: double m_latency; cv::Mat m_mat; using TimePoint = std::chrono::time_point; cv::optional m_prev_pull_tp; }; DummySource::DummySource(const double latency, const OutputDescr& output) : m_latency(latency), m_mat(output.dims, output.precision) { if (output.dims.size() == 1) { //FIXME: Well-known 1D mat WA m_mat.dims = 1; } utils::generateRandom(m_mat); } bool DummySource::pull(cv::gapi::wip::Data& data) { using namespace std::chrono; using namespace cv::gapi::streaming; // NB: In case it's the first pull. if (!m_prev_pull_tp) { m_prev_pull_tp = cv::util::make_optional(high_resolution_clock::now()); } // NB: Just increase reference counter not to release mat memory // after assigning it to the data. cv::Mat mat = m_mat; auto end = high_resolution_clock::now(); auto elapsed = duration_cast>(end - *m_prev_pull_tp).count(); auto delta = m_latency - elapsed; if (delta > 0) { utils::sleep(delta); } data.meta[meta_tag::timestamp] = int64_t{utils::timestamp()}; data = mat; m_prev_pull_tp = cv::util::make_optional(high_resolution_clock::now()); return true; } cv::GMetaArg DummySource::descr_of() const { return cv::GMetaArg{cv::descr_of(m_mat)}; } #endif // OPENCV_GAPI_PIPELINE_MODELING_TOOL_DUMMY_SOURCE_HPP