improvement
[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     tracker = new Tracker();
17 }
18
19 Engine* Engine::create()
20 {
21     LOG_DEBUG(TAG, "create");
22     std::lock_guard<std::mutex> lock(g_mutex);
23     if (g_instance)
24         return g_instance;
25     
26     Engine* instance (new Engine());
27     g_instance = instance;
28     return g_instance;
29 }
30
31 void Engine::destroy()
32 {
33     delete g_instance;
34 }
35
36 Engine::~Engine()
37 {    
38     delete detector;
39     delete tracker;
40 }
41
42 void Engine::setVideoSrc(VideoSrcType type, const std::string& url)
43 {
44     videoSrc = url;
45     reader = VideoReaderFactory::createVideoReader(type, url);
46 }
47
48 void Engine::run()
49 {
50     LOG_DEBUG(TAG, "run");
51     cv::Mat frame;
52     while (reader->read(frame)){
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 }