diff --git a/src/api/api.cc b/src/api/api.cc index fed52bb..c432590 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -152,6 +152,10 @@ void API::EnableStreamData(const Stream &stream) { synthetic_->EnableStreamData(stream); } +void API::DisableStreamData(const Stream &stream) { + synthetic_->DisableStreamData(stream); +} + api::StreamData API::GetStreamData(const Stream &stream) { return synthetic_->GetStreamData(stream); } diff --git a/src/api/api.h b/src/api/api.h index 4954831..def1055 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -79,6 +79,8 @@ class MYNTEYE_API API { void WaitForStreams(); void EnableStreamData(const Stream &stream); + void DisableStreamData(const Stream &stream); + api::StreamData GetStreamData(const Stream &stream); std::vector GetStreamDatas(const Stream &stream); diff --git a/src/api/processor/processor.cc b/src/api/processor/processor.cc index 2383a39..c274575 100644 --- a/src/api/processor/processor.cc +++ b/src/api/processor/processor.cc @@ -59,10 +59,10 @@ void Processor::SetProcessCallback(ProcessCallback callback) { callback_ = std::move(callback); } -void Processor::Activate(bool tree) { +void Processor::Activate(bool parents) { if (activated_) return; - if (tree) { + if (parents) { // Activate all parents Processor *parent = parent_; while (parent != nullptr) { @@ -75,10 +75,10 @@ void Processor::Activate(bool tree) { // thread_.detach(); } -void Processor::Deactivate(bool tree) { +void Processor::Deactivate(bool childs) { if (!activated_) return; - if (tree) { + if (childs) { // Deactivate all childs iterate_processors(GetChilds(), [](std::shared_ptr proc) { proc->Deactivate(); diff --git a/src/api/processor/processor.h b/src/api/processor/processor.h index 9159d9f..ca4283e 100644 --- a/src/api/processor/processor.h +++ b/src/api/processor/processor.h @@ -39,8 +39,8 @@ class Processor /*: public std::enable_shared_from_this*/ { void SetPostProcessCallback(PostProcessCallback callback); void SetProcessCallback(ProcessCallback callback); - void Activate(bool tree = false); - void Deactivate(bool tree = false); + void Activate(bool parents = false); + void Deactivate(bool childs = false); bool IsActivated(); bool IsIdle(); diff --git a/src/api/synthetic.cc b/src/api/synthetic.cc index f12d2da..3dc730c 100644 --- a/src/api/synthetic.cc +++ b/src/api/synthetic.cc @@ -2,11 +2,14 @@ #include +#include +#include #include #include "api/processor/depth_processor.h" #include "api/processor/disparity_normalized_processor.h" #include "api/processor/disparity_processor.h" +#include "api/processor/object.h" #include "api/processor/points_processor.h" #include "api/processor/processor.h" #include "api/processor/rectify_processor.h" @@ -18,10 +21,15 @@ Synthetic::Synthetic(API *api) : api_(api) { VLOG(2) << __func__; CHECK_NOTNULL(api_); InitStreamSupports(); + InitProcessors(); } Synthetic::~Synthetic() { VLOG(2) << __func__; + if (processor_) { + processor_->Deactivate(true); + processor_ = nullptr; + } } Synthetic::mode_t Synthetic::GetMode(const Stream &stream) const { @@ -37,67 +45,28 @@ bool Synthetic::Supports(const Stream &stream) const { } void Synthetic::EnableStreamData(const Stream &stream) { - if (Supports(stream)) - return; - switch (stream) { - case Stream::LEFT_RECTIFIED: { - if (Supports(Stream::LEFT)) { - stream_supports_mode_[stream] = MODE_SYNTHETIC; - return; - } - } break; - case Stream::RIGHT_RECTIFIED: { - if (Supports(Stream::RIGHT)) { - stream_supports_mode_[stream] = MODE_SYNTHETIC; - return; - } - } break; - case Stream::DISPARITY: { - if (Supports(Stream::LEFT) && Supports(Stream::RIGHT)) { - stream_supports_mode_[stream] = MODE_SYNTHETIC; - return; - } - } break; - case Stream::DISPARITY_NORMALIZED: { - if ((Supports(Stream::LEFT) && Supports(Stream::RIGHT)) || - Supports(Stream::DISPARITY)) { - stream_supports_mode_[stream] = MODE_SYNTHETIC; - return; - } - } break; - case Stream::DEPTH: { - if ((Supports(Stream::LEFT) && Supports(Stream::RIGHT)) || - Supports(Stream::DISPARITY) || Supports(Stream::POINTS)) { - stream_supports_mode_[stream] = MODE_SYNTHETIC; - return; - } - } break; - case Stream::POINTS: { - if ((Supports(Stream::LEFT) && Supports(Stream::RIGHT)) || - Supports(Stream::DISPARITY)) { - stream_supports_mode_[stream] = MODE_SYNTHETIC; - return; - } - } break; - default: - break; - } - LOG(WARNING) << "Enable stream data of " << stream << " failed"; + EnableStreamData(stream, 0); } void Synthetic::DisableStreamData(const Stream &stream) { - UNUSED(stream) + DisableStreamData(stream, 0); +} + +bool Synthetic::IsStreamDataEnabled(const Stream &stream) const { + return stream_supports_mode_.find(stream) != stream_supports_mode_.end(); } void Synthetic::SetStreamCallback( const Stream &stream, stream_callback_t callback) { - UNUSED(stream) - UNUSED(callback) + if (callback == nullptr) { + stream_callbacks_.erase(stream); + } else { + stream_callbacks_[stream] = callback; + } } bool Synthetic::HasStreamCallback(const Stream &stream) const { - UNUSED(stream) - return false; + return stream_callbacks_.find(stream) != stream_callbacks_.end(); } void Synthetic::StartVideoStreaming() {} @@ -126,4 +95,181 @@ void Synthetic::InitStreamSupports() { } } +void Synthetic::EnableStreamData(const Stream &stream, std::uint32_t depth) { + if (Supports(stream)) + return; + // Activate processors of synthetic stream + switch (stream) { + case Stream::LEFT_RECTIFIED: { + if (!Supports(Stream::LEFT)) + break; + stream_supports_mode_[stream] = MODE_SYNTHETIC; + CHECK(ActivateProcessor()); + } + return; + case Stream::RIGHT_RECTIFIED: { + if (!Supports(Stream::RIGHT)) + break; + stream_supports_mode_[stream] = MODE_SYNTHETIC; + CHECK(ActivateProcessor()); + } + return; + case Stream::DISPARITY: { + stream_supports_mode_[stream] = MODE_SYNTHETIC; + EnableStreamData(Stream::LEFT_RECTIFIED, depth + 1); + EnableStreamData(Stream::RIGHT_RECTIFIED, depth + 1); + CHECK(ActivateProcessor()); + } + return; + case Stream::DISPARITY_NORMALIZED: { + stream_supports_mode_[stream] = MODE_SYNTHETIC; + EnableStreamData(Stream::DISPARITY, depth + 1); + CHECK(ActivateProcessor()); + } + return; + case Stream::POINTS: { + stream_supports_mode_[stream] = MODE_SYNTHETIC; + EnableStreamData(Stream::DISPARITY, depth + 1); + CHECK(ActivateProcessor()); + } + return; + case Stream::DEPTH: { + stream_supports_mode_[stream] = MODE_SYNTHETIC; + EnableStreamData(Stream::POINTS, depth + 1); + CHECK(ActivateProcessor()); + } + return; + default: + break; + } + if (depth == 0) { + LOG(WARNING) << "Enable stream data of " << stream << " failed"; + } +} + +void Synthetic::DisableStreamData(const Stream &stream, std::uint32_t depth) { + if (!IsStreamDataEnabled(stream)) + return; + // Deactivate processors of synthetic stream + if (stream_supports_mode_[stream] != MODE_NATIVE) { + stream_supports_mode_.erase(stream); + switch (stream) { + case Stream::LEFT_RECTIFIED: { + if (GetMode(Stream::RIGHT_RECTIFIED) == MODE_SYNTHETIC) { + DisableStreamData(Stream::RIGHT_RECTIFIED, depth + 1); + } + if (GetMode(Stream::DISPARITY) == MODE_SYNTHETIC) { + DisableStreamData(Stream::DISPARITY, depth + 1); + } + DeactivateProcessor(); + } break; + case Stream::RIGHT_RECTIFIED: { + if (GetMode(Stream::LEFT_RECTIFIED) == MODE_SYNTHETIC) { + DisableStreamData(Stream::LEFT_RECTIFIED, depth + 1); + } + if (GetMode(Stream::DISPARITY) == MODE_SYNTHETIC) { + DisableStreamData(Stream::DISPARITY, depth + 1); + } + DeactivateProcessor(); + } break; + case Stream::DISPARITY: { + if (GetMode(Stream::DISPARITY_NORMALIZED) == MODE_SYNTHETIC) { + DisableStreamData(Stream::DISPARITY_NORMALIZED, depth + 1); + } + if (GetMode(Stream::POINTS) == MODE_SYNTHETIC) { + DisableStreamData(Stream::POINTS, depth + 1); + } + DeactivateProcessor(); + } break; + case Stream::DISPARITY_NORMALIZED: { + DeactivateProcessor(); + } break; + case Stream::POINTS: { + if (GetMode(Stream::DEPTH) == MODE_SYNTHETIC) { + DisableStreamData(Stream::DEPTH, depth + 1); + } + DeactivateProcessor(); + } break; + case Stream::DEPTH: { + DeactivateProcessor(); + } break; + default: + return; + } + if (depth > 0) { + LOG(WARNING) << "Disable synthetic stream data of " << stream << " too"; + } + } else if (depth == 0) { + LOG(WARNING) << "Disable native stream data of " << stream << " failed"; + } +} + +void Synthetic::InitProcessors() { + auto &&rectify_processor = std::make_shared(); + auto &&disparity_processor = std::make_shared(); + auto &&disparitynormalized_processor = + std::make_shared(); + auto &&points_processor = std::make_shared(); + auto &&depth_processor = std::make_shared(); + + using namespace std::placeholders; // NOLINT + rectify_processor->SetProcessCallback( + std::bind(&Synthetic::OnRectifyProcess, this, _1, _2, _3)); + disparity_processor->SetProcessCallback( + std::bind(&Synthetic::OnDisparityProcess, this, _1, _2, _3)); + disparitynormalized_processor->SetProcessCallback( + std::bind(&Synthetic::OnDisparityNormalizedProcess, this, _1, _2, _3)); + points_processor->SetProcessCallback( + std::bind(&Synthetic::OnPointsProcess, this, _1, _2, _3)); + depth_processor->SetProcessCallback( + std::bind(&Synthetic::OnDepthProcess, this, _1, _2, _3)); + + rectify_processor->AddChild(disparity_processor); + disparity_processor->AddChild(disparitynormalized_processor); + disparity_processor->AddChild(points_processor); + points_processor->AddChild(depth_processor); + + processor_ = rectify_processor; +} + +bool Synthetic::OnRectifyProcess( + Object *const in, Object *const out, Processor *const parent) { + UNUSED(in) + UNUSED(out) + UNUSED(parent) + return false; +} + +bool Synthetic::OnDisparityProcess( + Object *const in, Object *const out, Processor *const parent) { + UNUSED(in) + UNUSED(out) + UNUSED(parent) + return false; +} + +bool Synthetic::OnDisparityNormalizedProcess( + Object *const in, Object *const out, Processor *const parent) { + UNUSED(in) + UNUSED(out) + UNUSED(parent) + return false; +} + +bool Synthetic::OnPointsProcess( + Object *const in, Object *const out, Processor *const parent) { + UNUSED(in) + UNUSED(out) + UNUSED(parent) + return false; +} + +bool Synthetic::OnDepthProcess( + Object *const in, Object *const out, Processor *const parent) { + UNUSED(in) + UNUSED(out) + UNUSED(parent) + return false; +} + MYNTEYE_END_NAMESPACE diff --git a/src/api/synthetic.h b/src/api/synthetic.h index d63a359..81bcb60 100644 --- a/src/api/synthetic.h +++ b/src/api/synthetic.h @@ -4,6 +4,7 @@ #include #include +#include #include #include "api/api.h" @@ -11,6 +12,7 @@ MYNTEYE_BEGIN_NAMESPACE class API; +class Object; class Processor; class Synthetic { @@ -31,6 +33,7 @@ class Synthetic { void EnableStreamData(const Stream &stream); void DisableStreamData(const Stream &stream); + bool IsStreamDataEnabled(const Stream &stream) const; void SetStreamCallback(const Stream &stream, stream_callback_t callback); bool HasStreamCallback(const Stream &stream) const; @@ -46,13 +49,86 @@ class Synthetic { private: void InitStreamSupports(); + void EnableStreamData(const Stream &stream, std::uint32_t depth); + void DisableStreamData(const Stream &stream, std::uint32_t depth); + + void InitProcessors(); + + template + bool ActivateProcessor(bool tree = false); + template + bool DeactivateProcessor(bool tree = false); + + bool OnRectifyProcess( + Object *const in, Object *const out, Processor *const parent); + bool OnDisparityProcess( + Object *const in, Object *const out, Processor *const parent); + bool OnDisparityNormalizedProcess( + Object *const in, Object *const out, Processor *const parent); + bool OnPointsProcess( + Object *const in, Object *const out, Processor *const parent); + bool OnDepthProcess( + Object *const in, Object *const out, Processor *const parent); + API *api_; std::map stream_supports_mode_; - std::vector> processors_; + std::map stream_callbacks_; + + std::shared_ptr processor_; }; +template +std::shared_ptr find_processor(const P &processor) { + if (processor->Name() == T::NAME) { + return std::dynamic_pointer_cast(processor); + } + auto &&childs = processor->GetChilds(); + return find_processor(std::begin(childs), std::end(childs), T::NAME); +} + +template +std::shared_ptr find_processor( + InputIt first, InputIt last, const std::string &name) { + if (first == last) + return nullptr; + for (auto it = first; it != last; ++it) { + if ((*it)->Name() == name) { + return std::dynamic_pointer_cast(*it); + } + } + for (auto it = first; it != last; ++it) { + auto &&childs = (*it)->GetChilds(); + if (childs.empty()) + continue; + auto &&result = + find_processor(std::begin(childs), std::end(childs), name); + if (result == nullptr) + continue; + return result; + } + return nullptr; +} + +template +bool Synthetic::ActivateProcessor(bool parents) { + auto &&processor = find_processor(processor_); + if (processor == nullptr) + return false; + processor->Activate(parents); + return true; +} + +template +bool Synthetic::DeactivateProcessor(bool childs) { + auto &&processor = find_processor(processor_); + if (processor == nullptr) + return false; + processor->Deactivate(childs); + return true; +} + MYNTEYE_END_NAMESPACE #endif // MYNTEYE_SYNTHETIC_H_ NOLINT