save_snapshot_reprocessing.cpp
3.14 KB
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#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/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
m_devId = devId;
bFinish = false;
for (size_t i = 0; i < SAVE_THREAD_COUNT; i++)
{
m_save_img_thread[i] = std::thread(save_img_thread_process, this);
}
}
save_snapshot_reprocessing::~save_snapshot_reprocessing(){
// 结束线程
bFinish = true;
for (size_t i = 0; i < SAVE_THREAD_COUNT; i++)
{
m_save_img_thread[i].join();
}
}
// 释放资源
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()){
VPCUtil::vpc_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){
while(!bFinish){
waitforsave_img_queue_mutex.lock();
if(waitforsave_img_queue.size() > 320){
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() {
JpegUtil jpegUtil;
jpegUtil.jpeg_init(m_devId);
while (true) {
if (bFinish){
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(2));
waitforsave_img_queue_mutex.lock();
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();
waitforsave_img_queue_mutex.unlock();
bool bPost = false;
if(!cur_image.file_path.empty()){
bool bSaved = jpegUtil.jpeg_encode(cur_image.img_info.pic_desc, cur_image.file_path);
if(!bSaved){
LOG_ERROR("jpeg_encode failed");
} else {
bPost = true;
}
} else {
bPost = true;
}
VPCUtil::vpc_img_release(cur_image.img_info);
#ifdef POST_USE_RABBITMQ
if (bPost && callback_ != nullptr && cur_image.json_str.length() > 0) {
callback_(cur_image.json_str.c_str());
}
#endif
} else {
waitforsave_img_queue_mutex.unlock();
}
}
jpegUtil.jpeg_release();
}