Blame view

src/reprocessing_module/save_snapshot_reprocessing.cpp 2.93 KB
0b4cd5d5   Hu Chunming   完成轨迹定时抓拍
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  #include "save_snapshot_reprocessing.h"
  #include <thread>
  
  #include "opencv2/opencv.hpp"
  #include <opencv2/imgproc/types_c.h>
  #include <algorithm>
  #include "../common/logger.hpp"
  #include "../util/JpegUtil.h"
  #include "../util/vpc_util.h"
  
  const bool DRAW_ON_IMG = false;
  
  int save_img_thread_process(void* param) {
    save_snapshot_reprocessing *pThreadParam = (save_snapshot_reprocessing *)param;
    if (pThreadParam != nullptr){
      pThreadParam->save_img_process();
    }
    return 0;
  }
  
  // 初始化快照保存模块 开启图片保存线程
  save_snapshot_reprocessing::save_snapshot_reprocessing(int devId) {
  #ifdef POST_USE_RABBITMQ
    callback_ = nullptr;
  #endif
  
    bFinish = false;
    m_save_img_thread = std::thread(save_img_thread_process, this);
  
    m_devId = devId;
    DVPP_UTIL::dvpp_jpeg_init(m_devId);
  }
  
  save_snapshot_reprocessing::~save_snapshot_reprocessing(){
    // 结束线程
    bFinish = true;
    m_save_img_thread.join();
    DVPP_UTIL::dvpp_jpeg_release();
  }
  
  // 释放资源
  void save_snapshot_reprocessing::save_snapshot_reprocessing_release() {
  
    std::unique_lock<std::mutex> l(waitforsave_img_queue_mutex);
  
    while (!waitforsave_img_queue.empty()) {
      ImgSaveInfo cur_image = waitforsave_img_queue.front();
      waitforsave_img_queue.pop();
  
      if(!cur_image.file_path.empty()){
        dvpp_img_release(cur_image.img_info);
      }
    }
  
    l.unlock();
  }
  
  #ifdef POST_USE_RABBITMQ
  
  // 设置MQ返回回调函数 方便内部调用MQ推送结果
  void save_snapshot_reprocessing::set_callback(callback_t cb) {
    callback_ = cb;
  }
  
  #endif // #ifdef POST_USE_RABBITMQ
  
  
  void save_snapshot_reprocessing::reprocessing_process_wo_locus_async(ImgSaveInfo saveInfo){
  
0b4cd5d5   Hu Chunming   完成轨迹定时抓拍
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
     while(!bFinish){
      waitforsave_img_queue_mutex.lock();
      if(waitforsave_img_queue.size() > 100){
        waitforsave_img_queue_mutex.unlock();
        std::this_thread::sleep_for(std::chrono::milliseconds(5));
        continue;
      }
      waitforsave_img_queue.push(saveInfo);
      waitforsave_img_queue_mutex.unlock();
      break;
    }
    
  }
  
  void save_snapshot_reprocessing::save_img_process() {
    while (true) {
      if (bFinish){
        break;
      }
  
      std::this_thread::sleep_for(std::chrono::milliseconds(2));
      std::unique_lock<std::mutex> l(waitforsave_img_queue_mutex);
      if (!waitforsave_img_queue.empty()) {
        LOG_DEBUG("waitforsave_image_queue size: {}", waitforsave_img_queue.size());
        ImgSaveInfo cur_image = waitforsave_img_queue.front();
        waitforsave_img_queue.pop();
        l.unlock();
  
        if(!cur_image.file_path.empty()){
          DVPP_UTIL::dvpp_jpeg_encode(cur_image.img_info.pic_desc, cur_image.file_path);
        }
        dvpp_img_release(cur_image.img_info);
  
  #ifdef POST_USE_RABBITMQ
        if (callback_ != nullptr && cur_image.json_str.length() > 0) {
          // LOG_DEBUG("mq publish process 00000000000000000");
          callback_(cur_image.json_str.c_str());
          // LOG_DEBUG("mq publish process 11111111111111111");
        }
  #endif
  
      } else {
        l.unlock();
      }
  
    }
  }