Add multitracker and metrics
[trackerpp.git] / src / Engine.cpp
1 #include <mutex>
2 #include <thread>
3 #include "Engine.h"
4 #include "Logger.h"
5
6 using namespace suanzi;
7
8 const static std::string TAG = "Engine";
9
10 static std::mutex g_mutex;
11 static Engine* g_instance = nullptr;
12
13 Engine::Engine()
14 {
15     detector = new Detector();
16 }
17
18 Engine* Engine::create()
19 {
20     LOG_DEBUG(TAG, "create");
21     std::lock_guard<std::mutex> lock(g_mutex);
22     if (g_instance)
23         return g_instance;
24     
25     Engine* instance (new Engine());
26     g_instance = instance;
27     return g_instance;
28 }
29
30 void Engine::destroy()
31 {
32     delete g_instance;
33 }
34
35 Engine::~Engine()
36 {    
37     delete detector;
38 //    delete tracker;
39 }
40
41 void Engine::setVideoSrc(VideoSrcType type, const std::string& url)
42 {
43     videoSrc = url;
44     reader = VideoReaderFactory::createVideoReader(type, url);
45 }
46
47 void Engine::run()
48 {
49     LOG_DEBUG(TAG, "run");
50     cv::Mat frame;
51     while (reader->read(frame)){
52         LOG_DEBUG(TAG, "Size: " << frame.cols  <<  "x" << frame.rows);
53         detector->detect(frame);
54     }
55 }
56
57 void Engine::start()
58 {
59     LOG_DEBUG(TAG, "start");
60     if (!reader){
61         LOG_ERROR(TAG, "reader is null. exit");
62         return;
63     }
64     std::thread t(&Engine::run, this);
65     t.join();
66 }
67
68 void Engine::addObserver(EngineObserver *observer)
69 {
70     observer_list.insert(observer);
71 }