X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=src%2FEngine.cpp;h=1b7cca2d3baeae39b20ca7b6167e611fdfbad80c;hb=70532232dd98f31467eff7baaaff6e68f803bb45;hp=74659a09ae266b72d6299fec5bd77c36b7f00227;hpb=209cfd9fe0ea398b794d1d1995629a826125f035;p=trackerpp.git diff --git a/src/Engine.cpp b/src/Engine.cpp index 74659a0..1b7cca2 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -1,16 +1,18 @@ #include #include +#include #include "Engine.h" #include "Logger.h" #include "PredictorWrapper.h" using namespace suanzi; +using namespace std; static const std::string TAG = "Engine"; - static std::mutex g_mutex; static EngineWPtr g_instance; +typedef std::shared_ptr> PersonsInfoPtr; EnginePtr Engine::create() { @@ -18,7 +20,7 @@ EnginePtr Engine::create() std::lock_guard lock(g_mutex); if (g_instance.lock()){ LOG_ERROR(TAG, "already exists"); - return EnginePtr(); // nullptr + return EnginePtr(); } EnginePtr instance (new Engine()); g_instance = instance; @@ -28,7 +30,6 @@ EnginePtr Engine::create() Engine::Engine() { detector = std::make_shared(); - multiTracker = std::make_shared(); } Engine::~Engine() @@ -45,7 +46,6 @@ void Engine::destroy() observer_list.clear(); } - void Engine::setVideoSrc(VideoSrcType type, const std::string& url) { reader = VideoReaderFactory::createVideoReader(type, url); @@ -66,6 +66,7 @@ void Engine::run() void Engine::start() { LOG_DEBUG(TAG, "start"); + multiTracker = std::make_shared(g_instance); if (!reader){ LOG_ERROR(TAG, "reader is null. exit"); return; @@ -76,5 +77,54 @@ void Engine::start() void Engine::addObserver(EngineObserver *observer) { + LOG_DEBUG(TAG, "addObserver"); observer_list.insert(observer); } + + +// WorkItem class for event thread +class PersonInEventWorkItem : public WorkItem +{ +public: + PersonInEventWorkItem(std::set obs, PersonsInfoPtr info) : obs(obs), info(info){ + } + ~PersonInEventWorkItem(){} + void run (){ + for (auto o : obs){ + o->onPersonsIn(*(info.get())); + } + } +private: + std::set obs; + PersonsInfoPtr info; +}; + +class PersonOutEventWorkItem : public WorkItem +{ +public: + PersonOutEventWorkItem(std::set obs, PersonsInfoPtr info) : obs(obs), info(info){ + } + ~PersonOutEventWorkItem(){} + void run (){ + for (auto o : obs){ + o->onPersonsIn(*(info.get())); + } + } +private: + std::set obs; + PersonsInfoPtr info; +}; + +void Engine::onPersonsOut(const std::vector& p) +{ + LOG_DEBUG(TAG, "onPersonOut"); + PersonsInfoPtr pp = std::make_shared>(p); + eventThread.enqueue(new PersonOutEventWorkItem(this->observer_list, pp)); +} + +void Engine::onPersonsIn(const std::vector& p) +{ + LOG_DEBUG(TAG, "onPersonIn"); + PersonsInfoPtr pp = std::make_shared>(p); + eventThread.enqueue(new PersonInEventWorkItem(this->observer_list, pp)); +}