Commit 31a1e592daf2b719a7fcc93d3e7cb6c2495b4994

Authored by Hu Chunming
1 parent 7d1675e3

完成接口匹配

FFNvDecoder/FFNvDecoderManager.cpp
@@ -8,9 +8,10 @@ FFNvDecoder* FFNvDecoderManager::createDecoder(MgrDecConfig& config){ @@ -8,9 +8,10 @@ FFNvDecoder* FFNvDecoderManager::createDecoder(MgrDecConfig& config){
8 8
9 closeAllFinishedDecoder(); 9 closeAllFinishedDecoder();
10 10
11 - int num = decoderMap.count(config.name);  
12 - if (num > 0)  
13 - { 11 + std::lock_guard<std::mutex> l(m_mutex);
  12 +
  13 + auto it = decoderMap.find(config.name);
  14 + if (it != decoderMap.end()){
14 cout << "已存在name所标记的解码器" << endl; 15 cout << "已存在name所标记的解码器" << endl;
15 return nullptr; 16 return nullptr;
16 } 17 }
@@ -46,6 +47,8 @@ bool FFNvDecoderManager::setUserPtr(const string name, const void * userPtr) @@ -46,6 +47,8 @@ bool FFNvDecoderManager::setUserPtr(const string name, const void * userPtr)
46 return false; 47 return false;
47 } 48 }
48 49
  50 + std::lock_guard<std::mutex> l(m_mutex);
  51 +
49 auto dec = decoderMap.find(name); 52 auto dec = decoderMap.find(name);
50 if (dec != decoderMap.end()) 53 if (dec != decoderMap.end())
51 { 54 {
@@ -65,6 +68,8 @@ FFNvDecoder* FFNvDecoderManager::getDecoderByName(const string name) @@ -65,6 +68,8 @@ FFNvDecoder* FFNvDecoderManager::getDecoderByName(const string name)
65 return nullptr; 68 return nullptr;
66 } 69 }
67 70
  71 + std::lock_guard<std::mutex> l(m_mutex);
  72 +
68 auto dec = decoderMap.find(name); 73 auto dec = decoderMap.find(name);
69 if (dec != decoderMap.end()) 74 if (dec != decoderMap.end())
70 { 75 {
@@ -89,6 +94,8 @@ bool FFNvDecoderManager::startDecodeByName(const string name){ @@ -89,6 +94,8 @@ bool FFNvDecoderManager::startDecodeByName(const string name){
89 return false; 94 return false;
90 } 95 }
91 96
  97 + std::lock_guard<std::mutex> l(m_mutex);
  98 +
92 auto dec = decoderMap.find(name); 99 auto dec = decoderMap.find(name);
93 if (dec != decoderMap.end()) 100 if (dec != decoderMap.end())
94 { 101 {
@@ -101,6 +108,9 @@ bool FFNvDecoderManager::startDecodeByName(const string name){ @@ -101,6 +108,9 @@ bool FFNvDecoderManager::startDecodeByName(const string name){
101 } 108 }
102 109
103 void FFNvDecoderManager::startAllDecode(){ 110 void FFNvDecoderManager::startAllDecode(){
  111 +
  112 + std::lock_guard<std::mutex> l(m_mutex);
  113 +
104 for(auto iter = decoderMap.begin(); iter != decoderMap.end(); iter++){ 114 for(auto iter = decoderMap.begin(); iter != decoderMap.end(); iter++){
105 if (!iter->second->isRunning()) 115 if (!iter->second->isRunning())
106 { 116 {
@@ -116,7 +126,8 @@ bool FFNvDecoderManager::closeDecoderByName(const string name){ @@ -116,7 +126,8 @@ bool FFNvDecoderManager::closeDecoderByName(const string name){
116 return false; 126 return false;
117 } 127 }
118 128
119 - m_mutex_erase.lock(); 129 + std::lock_guard<std::mutex> l(m_mutex);
  130 +
120 auto dec = decoderMap.find(name); 131 auto dec = decoderMap.find(name);
121 if (dec != decoderMap.end()) 132 if (dec != decoderMap.end())
122 { 133 {
@@ -125,30 +136,29 @@ bool FFNvDecoderManager::closeDecoderByName(const string name){ @@ -125,30 +136,29 @@ bool FFNvDecoderManager::closeDecoderByName(const string name){
125 dec->second = nullptr; 136 dec->second = nullptr;
126 decoderMap.erase(dec); 137 decoderMap.erase(dec);
127 138
128 - m_mutex_erase.unlock();  
129 return true; 139 return true;
130 } 140 }
131 141
132 - m_mutex_erase.unlock();  
133 cout << "没有找到name为" << name << "的解码器!" << endl; 142 cout << "没有找到name为" << name << "的解码器!" << endl;
134 return false; 143 return false;
135 } 144 }
136 145
137 void FFNvDecoderManager::closeAllDecoder() 146 void FFNvDecoderManager::closeAllDecoder()
138 { 147 {
139 - m_mutex_erase.lock(); 148 + std::lock_guard<std::mutex> l(m_mutex);
  149 +
140 for(auto iter = decoderMap.begin(); iter != decoderMap.end(); iter++){ 150 for(auto iter = decoderMap.begin(); iter != decoderMap.end(); iter++){
141 iter->second->close(); 151 iter->second->close();
142 delete iter->second; 152 delete iter->second;
143 iter->second = nullptr; 153 iter->second = nullptr;
144 } 154 }
145 decoderMap.clear(); 155 decoderMap.clear();
146 - m_mutex_erase.unlock();  
147 } 156 }
148 157
149 void FFNvDecoderManager::closeAllFinishedDecoder() 158 void FFNvDecoderManager::closeAllFinishedDecoder()
150 { 159 {
151 - m_mutex_erase.lock(); 160 + std::lock_guard<std::mutex> l(m_mutex);
  161 +
152 for(auto iter = decoderMap.begin(); iter != decoderMap.end(); ){ 162 for(auto iter = decoderMap.begin(); iter != decoderMap.end(); ){
153 if (iter->second->isFinished()) 163 if (iter->second->isFinished())
154 { 164 {
@@ -161,7 +171,6 @@ void FFNvDecoderManager::closeAllFinishedDecoder() @@ -161,7 +171,6 @@ void FFNvDecoderManager::closeAllFinishedDecoder()
161 iter++ ; 171 iter++ ;
162 } 172 }
163 } 173 }
164 - m_mutex_erase.unlock();  
165 } 174 }
166 175
167 int FFNvDecoderManager::count() 176 int FFNvDecoderManager::count()
@@ -179,6 +188,8 @@ bool FFNvDecoderManager::pauseDecoder(const string name) @@ -179,6 +188,8 @@ bool FFNvDecoderManager::pauseDecoder(const string name)
179 return false; 188 return false;
180 } 189 }
181 190
  191 + std::lock_guard<std::mutex> l(m_mutex);
  192 +
182 auto dec = decoderMap.find(name); 193 auto dec = decoderMap.find(name);
183 if (dec != decoderMap.end()) 194 if (dec != decoderMap.end())
184 { 195 {
@@ -198,6 +209,8 @@ bool FFNvDecoderManager::resumeDecoder(const string name) @@ -198,6 +209,8 @@ bool FFNvDecoderManager::resumeDecoder(const string name)
198 return false; 209 return false;
199 } 210 }
200 211
  212 + std::lock_guard<std::mutex> l(m_mutex);
  213 +
201 auto dec = decoderMap.find(name); 214 auto dec = decoderMap.find(name);
202 if (dec != decoderMap.end()) 215 if (dec != decoderMap.end())
203 { 216 {
@@ -222,6 +235,8 @@ bool FFNvDecoderManager::isRunning(const string name){ @@ -222,6 +235,8 @@ bool FFNvDecoderManager::isRunning(const string name){
222 return false; 235 return false;
223 } 236 }
224 237
  238 + std::lock_guard<std::mutex> l(m_mutex);
  239 +
225 auto dec = decoderMap.find(name); 240 auto dec = decoderMap.find(name);
226 if (dec != decoderMap.end()) 241 if (dec != decoderMap.end())
227 { 242 {
@@ -239,6 +254,8 @@ bool FFNvDecoderManager::isFinished(const string name){ @@ -239,6 +254,8 @@ bool FFNvDecoderManager::isFinished(const string name){
239 return false; 254 return false;
240 } 255 }
241 256
  257 + std::lock_guard<std::mutex> l(m_mutex);
  258 +
242 auto dec = decoderMap.find(name); 259 auto dec = decoderMap.find(name);
243 if (dec != decoderMap.end()) 260 if (dec != decoderMap.end())
244 { 261 {
@@ -256,6 +273,8 @@ bool FFNvDecoderManager::isPausing(const string name){ @@ -256,6 +273,8 @@ bool FFNvDecoderManager::isPausing(const string name){
256 return false; 273 return false;
257 } 274 }
258 275
  276 + std::lock_guard<std::mutex> l(m_mutex);
  277 +
259 auto dec = decoderMap.find(name); 278 auto dec = decoderMap.find(name);
260 if (dec != decoderMap.end()) 279 if (dec != decoderMap.end())
261 { 280 {
@@ -274,6 +293,8 @@ bool FFNvDecoderManager::setDecKeyframe(const string name, bool bKeyframe) @@ -274,6 +293,8 @@ bool FFNvDecoderManager::setDecKeyframe(const string name, bool bKeyframe)
274 return false; 293 return false;
275 } 294 }
276 295
  296 + std::lock_guard<std::mutex> l(m_mutex);
  297 +
277 auto dec = decoderMap.find(name); 298 auto dec = decoderMap.find(name);
278 if (dec != decoderMap.end()) 299 if (dec != decoderMap.end())
279 { 300 {
@@ -293,6 +314,8 @@ bool FFNvDecoderManager::getResolution(const string name, int &amp;width, int &amp;heigh @@ -293,6 +314,8 @@ bool FFNvDecoderManager::getResolution(const string name, int &amp;width, int &amp;heigh
293 return false; 314 return false;
294 } 315 }
295 316
  317 + std::lock_guard<std::mutex> l(m_mutex);
  318 +
296 auto dec = decoderMap.find(name); 319 auto dec = decoderMap.find(name);
297 if (dec != decoderMap.end()) 320 if (dec != decoderMap.end())
298 { 321 {
@@ -302,4 +325,17 @@ bool FFNvDecoderManager::getResolution(const string name, int &amp;width, int &amp;heigh @@ -302,4 +325,17 @@ bool FFNvDecoderManager::getResolution(const string name, int &amp;width, int &amp;heigh
302 325
303 cout << "没有找到name为" << name << "的解码器!" << endl; 326 cout << "没有找到name为" << name << "的解码器!" << endl;
304 return false; 327 return false;
  328 +}
  329 +
  330 +vector<string> FFNvDecoderManager::getAllDecodeName(){
  331 +
  332 + closeAllFinishedDecoder();
  333 +
  334 + std::lock_guard<std::mutex> l(m_mutex);
  335 +
  336 + vector<string> decode_names;
  337 + for(auto it = decoderMap.begin(); it != decoderMap.end(); ++it){
  338 + decode_names.push_back(it->first);
  339 + }
  340 + return decode_names;
305 } 341 }
306 \ No newline at end of file 342 \ No newline at end of file
FFNvDecoder/FFNvDecoderManager.h
@@ -111,6 +111,15 @@ public: @@ -111,6 +111,15 @@ public:
111 **************************************************/ 111 **************************************************/
112 void closeAllDecoder(); 112 void closeAllDecoder();
113 113
  114 + /**************************************************
  115 + * 接口:closeAllDecoderByGpuid
  116 + * 功能:关闭某张显卡撒花姑娘的全部解码器
  117 + * 参数:const string gpuid gpu的id
  118 + * 返回:void
  119 + * 备注:
  120 + **************************************************/
  121 + void closeAllDecoderByGpuid(const string gpuid);
  122 +
114 /************************************************** 123 /**************************************************
115 * 接口:pauseDecoder 124 * 接口:pauseDecoder
116 * 功能:暂停指定名称的解码器 125 * 功能:暂停指定名称的解码器
@@ -179,13 +188,31 @@ public: @@ -179,13 +188,31 @@ public:
179 * 功能:设置是否只解码关键帧。默认全解 188 * 功能:设置是否只解码关键帧。默认全解
180 * 参数:const string name 解码器名称 189 * 参数:const string name 解码器名称
181 * bool bKeyframe 是否只解码关键帧。true,只解码关键帧;false,普通的全解码 190 * bool bKeyframe 是否只解码关键帧。true,只解码关键帧;false,普通的全解码
182 - * 返回:void 191 + * 返回:bool 成功返回true,失败返回false
183 * 备注: 192 * 备注:
184 **************************************************/ 193 **************************************************/
185 bool setDecKeyframe(const string name, bool bKeyframe); 194 bool setDecKeyframe(const string name, bool bKeyframe);
186 195
  196 + /**************************************************
  197 + * 接口:getResolution
  198 + * 功能:获取视频分辨率
  199 + * 参数:const string name 解码器名称
  200 + * int &width 从 width 返回视频宽度
  201 + * int &height 从 height 返回视频高度
  202 + * 返回:bool 成功获取返回true,失败返回false
  203 + * 备注:
  204 + **************************************************/
187 bool getResolution(const string name, int &width, int &height); 205 bool getResolution(const string name, int &width, int &height);
188 206
  207 + /**************************************************
  208 + * 接口:getAllDecodeName
  209 + * 功能:获取全部解码器名称
  210 + * 参数:void
  211 + * 返回:vector<string> 返回全部解码器名称
  212 + * 备注:
  213 + **************************************************/
  214 + vector<string> getAllDecodeName();
  215 +
189 private: 216 private:
190 FFNvDecoderManager(){} 217 FFNvDecoderManager(){}
191 218
@@ -194,5 +221,5 @@ private: @@ -194,5 +221,5 @@ private:
194 private: 221 private:
195 map<string, FFNvDecoder*> decoderMap; 222 map<string, FFNvDecoder*> decoderMap;
196 223
197 - mutex m_mutex_erase; 224 + mutex m_mutex;
198 }; 225 };
199 \ No newline at end of file 226 \ No newline at end of file
tsl_aiplatform/ai_platform/MultiSourceProcess.cpp
@@ -46,12 +46,54 @@ void decode_finished_cbk(const void * userPtr){ @@ -46,12 +46,54 @@ void decode_finished_cbk(const void * userPtr){
46 } 46 }
47 } 47 }
48 48
  49 +CMultiSourceProcess::CMultiSourceProcess(){
  50 + m_bfinish = false;
  51 +}
  52 +
  53 +CMultiSourceProcess::~CMultiSourceProcess(){
  54 +
  55 +}
  56 +
  57 +/* 授权校验线程 */
  58 +#ifdef AUTHORIZATION
  59 +void CMultiSourceProcess::check_thread() {
  60 + int res = -1;
  61 +#ifndef _MSC_VER
  62 + char wtime[15];
  63 + memset(wtime, 0, 15);
  64 + char *time = wtime;
  65 +#endif
  66 +
  67 + while (1) {
  68 + if(m_bfinish){
  69 + break;
  70 + }
  71 +#ifdef _MSC_VER
  72 + res = sy_licence(productSN);
  73 +#else
  74 + res = sy_licence(productSN, &time);
  75 +#endif
  76 + if (res < 0) {
  77 + licence_status = licence_status - 1;
  78 + } else {
  79 + if (licence_status < 0) {
  80 + licence_status = 0;
  81 + }
  82 + }
  83 + std::this_thread::sleep_for(std::chrono::seconds(300));
  84 + }
  85 +}
  86 +#endif
  87 +
49 /* @InitAlgorthim 88 /* @InitAlgorthim
50 * @Description: 初始化全局参数 + 根据配置初始化算法模型 89 * @Description: 初始化全局参数 + 根据配置初始化算法模型
51 */ 90 */
52 int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) { 91 int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) {
53 licence_status_ = -1; 92 licence_status_ = -1;
54 - thread_status_ = -1; 93 +
  94 + m_bfinish = false;
  95 +
  96 + gpu_id_ = to_string(vptParam.gpuid);
55 97
56 int ret = SUCCESS; 98 int ret = SUCCESS;
57 99
@@ -68,14 +110,8 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) { @@ -68,14 +110,8 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) {
68 ret = sy_time_check(2023, 2); // license_check(param.auth_license, productSN);// 110 ret = sy_time_check(2023, 2); // license_check(param.auth_license, productSN);//
69 if (ret == SUCCESS) 111 if (ret == SUCCESS)
70 #endif // ifdef AUTHORIZATION 112 #endif // ifdef AUTHORIZATION
71 -  
72 { 113 {
73 /*初始化全局参数*/ 114 /*初始化全局参数*/
74 - viewTaskID = -1;  
75 - TaskInPlay = 0;  
76 - TotalTask = 0;  
77 - ProcessFlag = false;  
78 - SourceFlag = false;  
79 115
80 mModeSnapshotVideo = "cpu"; 116 mModeSnapshotVideo = "cpu";
81 mModeSnapshotLittle = "cpu"; 117 mModeSnapshotLittle = "cpu";
@@ -102,7 +138,7 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) { @@ -102,7 +138,7 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) {
102 CUresult rlt = CUDA_SUCCESS; 138 CUresult rlt = CUDA_SUCCESS;
103 rlt = cuDeviceTotalMem(&memSize, dev); 139 rlt = cuDeviceTotalMem(&memSize, dev);
104 140
105 - gpu_total_memory = (float)memSize / (1024 * 1024); 141 + double gpu_total_memory = (float)memSize / (1024 * 1024);
106 142
107 if (gpu_total_memory < 9000) // small gpu memory 143 if (gpu_total_memory < 9000) // small gpu memory
108 section_batch_size_ = 10; 144 section_batch_size_ = 10;
@@ -128,7 +164,6 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) { @@ -128,7 +164,6 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) {
128 param.max_batch = section_batch_size_; 164 param.max_batch = section_batch_size_;
129 param.serialize_file = "./serialize_file/FPN_VPT"; 165 param.serialize_file = "./serialize_file/FPN_VPT";
130 param.auth_license = /*vptParam.auth_license*/ "sy_tsl_aiplatform_sdk_2021"; 166 param.auth_license = /*vptParam.auth_license*/ "sy_tsl_aiplatform_sdk_2021";
131 - gpu_id_ = vptParam.gpuid;  
132 } 167 }
133 168
134 VPT_Handle_ = nullptr; 169 VPT_Handle_ = nullptr;
@@ -205,9 +240,15 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) { @@ -205,9 +240,15 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) {
205 { 240 {
206 licence_status_ = 0; 241 licence_status_ = 0;
207 #ifdef AUTHORIZATION 242 #ifdef AUTHORIZATION
208 - thread_ = boost::thread(check_thread, this); 243 + pthread_create(&m_authority_check_thread,0,
  244 + [](void* arg)
  245 + {
  246 + CMultiSourceProcess* a=(CMultiSourceProcess*)arg;
  247 + a->check_thread();
  248 + return (void*)0;
  249 + }
  250 + ,this);
209 #endif 251 #endif
210 - thread_status_ = 0;  
211 } 252 }
212 } else { 253 } else {
213 return AUTHOR_ERROR; 254 return AUTHOR_ERROR;
@@ -225,7 +266,7 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) { @@ -225,7 +266,7 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam) {
225 return ret; 266 return ret;
226 } 267 }
227 268
228 -bool CMultiSourceProcess::add_task_operation(task_param _cur_task_param){ 269 +bool CMultiSourceProcess::AddTask(task_param _cur_task_param){
229 270
230 FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance(); 271 FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
231 272
@@ -235,7 +276,7 @@ bool CMultiSourceProcess::add_task_operation(task_param _cur_task_param){ @@ -235,7 +276,7 @@ bool CMultiSourceProcess::add_task_operation(task_param _cur_task_param){
235 config.cfg.post_decoded_cbk = decoded_cbk; 276 config.cfg.post_decoded_cbk = decoded_cbk;
236 config.cfg.decode_finished_cbk = decode_finished_cbk; 277 config.cfg.decode_finished_cbk = decode_finished_cbk;
237 config.cfg.force_tcp = true; // rtsp用tcp 278 config.cfg.force_tcp = true; // rtsp用tcp
238 - config.cfg.gpuid = _cur_task_param.gpu_id_; 279 + config.cfg.gpuid = gpu_id_;
239 FFNvDecoder* dec = pDecManager->createDecoder(config); 280 FFNvDecoder* dec = pDecManager->createDecoder(config);
240 if (!dec) 281 if (!dec)
241 { 282 {
@@ -286,7 +327,7 @@ bool CMultiSourceProcess::add_task_operation(task_param _cur_task_param){ @@ -286,7 +327,7 @@ bool CMultiSourceProcess::add_task_operation(task_param _cur_task_param){
286 #endif 327 #endif
287 328
288 // 启动算法处理线程 329 // 启动算法处理线程
289 - startProcessByGpuid(_cur_task_param.gpu_id_); 330 + startProcessByGpuid(gpu_id_);
290 } 331 }
291 332
292 // 启动算法处理线程 333 // 启动算法处理线程
@@ -318,10 +359,11 @@ void CMultiSourceProcess::startProcessByGpuid(const string gpuid){ @@ -318,10 +359,11 @@ void CMultiSourceProcess::startProcessByGpuid(const string gpuid){
318 } 359 }
319 360
320 void CMultiSourceProcess::post_decode_thread(task_param _cur_task_param, AVFrame * gpuFrame){ 361 void CMultiSourceProcess::post_decode_thread(task_param _cur_task_param, AVFrame * gpuFrame){
  362 +
321 if (gpuFrame->format == AV_PIX_FMT_CUDA){ 363 if (gpuFrame->format == AV_PIX_FMT_CUDA){
322 - GpuRgbMemory* gpuMem = new GpuRgbMemory(3, gpuFrame->width, gpuFrame->height, _cur_task_param.task_id, _cur_task_param.gpu_id_ , true); 364 + GpuRgbMemory* gpuMem = new GpuRgbMemory(3, gpuFrame->width, gpuFrame->height, _cur_task_param.task_id, gpu_id_ , true);
323 365
324 - cudaSetDevice(atoi(_cur_task_param.gpu_id_)); 366 + cudaSetDevice(atoi(gpu_id_.c_str()));
325 cuda_common::setColorSpace( ITU_709, 0 ); 367 cuda_common::setColorSpace( ITU_709, 0 );
326 cudaError_t cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], gpuMem->getMem(), gpuFrame->width, gpuFrame->height); 368 cudaError_t cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], gpuMem->getMem(), gpuFrame->width, gpuFrame->height);
327 cudaDeviceSynchronize(); 369 cudaDeviceSynchronize();
@@ -331,6 +373,9 @@ void CMultiSourceProcess::post_decode_thread(task_param _cur_task_param, AVFrame @@ -331,6 +373,9 @@ void CMultiSourceProcess::post_decode_thread(task_param _cur_task_param, AVFrame
331 } 373 }
332 374
333 do{ 375 do{
  376 + if(m_bfinish){
  377 + break;
  378 + }
334 // TODO 本循环需要一个可以手动终止的开关 379 // TODO 本循环需要一个可以手动终止的开关
335 m_QueueMtx.lock(); 380 m_QueueMtx.lock();
336 if(m_RgbDataList.size() >= (20 * gpuProcessthreadMap.size() + 20)){ 381 if(m_RgbDataList.size() >= (20 * gpuProcessthreadMap.size() + 20)){
@@ -349,6 +394,8 @@ void CMultiSourceProcess::decode_finished_thread(task_param t_param){ @@ -349,6 +394,8 @@ void CMultiSourceProcess::decode_finished_thread(task_param t_param){
349 // 任务结束,关闭跟踪 394 // 任务结束,关闭跟踪
350 if (!FinishTaskTracker(VPT_Handle_, t_param.task_id)) 395 if (!FinishTaskTracker(VPT_Handle_, t_param.task_id))
351 LOG_ERROR("Finish VPT Tracker failed, task_id: {}", t_param.task_id); 396 LOG_ERROR("Finish VPT Tracker failed, task_id: {}", t_param.task_id);
  397 +
  398 + finish_task(t_param.task_id,false);
352 } 399 }
353 400
354 bool CMultiSourceProcess::task_has_vpt_algor(const std::string &task_id){ 401 bool CMultiSourceProcess::task_has_vpt_algor(const std::string &task_id){
@@ -399,6 +446,10 @@ void CMultiSourceProcess::algorthim_process_thread(const string gpuid){ @@ -399,6 +446,10 @@ void CMultiSourceProcess::algorthim_process_thread(const string gpuid){
399 break; 446 break;
400 } 447 }
401 448
  449 + if(m_bfinish){
  450 + break;
  451 + }
  452 +
402 /* step5. 凑齐的解码数据 拼batch */ 453 /* step5. 凑齐的解码数据 拼batch */
403 m_QueueMtx.lock(); 454 m_QueueMtx.lock();
404 455
@@ -1066,7 +1117,6 @@ void CMultiSourceProcess::algorthim_face_detect(vector&lt;string&gt;&amp; task_list, sy_im @@ -1066,7 +1117,6 @@ void CMultiSourceProcess::algorthim_face_detect(vector&lt;string&gt;&amp; task_list, sy_im
1066 face_det_interest_task_id, face_det_interest_imgs.data(), facedet_result, face_deleteObjectID); 1117 face_det_interest_task_id, face_det_interest_imgs.data(), facedet_result, face_deleteObjectID);
1067 1118
1068 // 保存已结束轨迹的目标 1119 // 保存已结束轨迹的目标
1069 - // auto task_iter_face = pThreadParam->TaskInPlayID.begin();  
1070 auto task_iter_face = face_det_interest_task_id.begin(); //debug by zsh 1120 auto task_iter_face = face_det_interest_task_id.begin(); //debug by zsh
1071 for (int i = 0; i < face_deleteObjectID.size(); i++) { 1121 for (int i = 0; i < face_deleteObjectID.size(); i++) {
1072 for (int j = 0; j < face_deleteObjectID[i].size(); ++j) { 1122 for (int j = 0; j < face_deleteObjectID[i].size(); ++j) {
@@ -1126,4 +1176,83 @@ int CMultiSourceProcess::GetTaskStatus(const string taskID) { @@ -1126,4 +1176,83 @@ int CMultiSourceProcess::GetTaskStatus(const string taskID) {
1126 } 1176 }
1127 1177
1128 return SUCCESS; 1178 return SUCCESS;
  1179 +}
  1180 +
  1181 +bool CMultiSourceProcess::PauseTask(const string taskID) {
  1182 + FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
  1183 + return pDecManager->pauseDecoder(taskID);
  1184 +}
  1185 +
  1186 +bool CMultiSourceProcess::RestartTask(const string taskID){
  1187 + FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
  1188 + return pDecManager->resumeDecoder(taskID);
  1189 +}
  1190 +
  1191 +bool CMultiSourceProcess::finish_task(const string taskID, const bool delete_snapshot){
  1192 + FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
  1193 + bool ret = pDecManager->closeDecoderByName(taskID);
  1194 +
  1195 +#ifdef POST_USE_RABBITMQ
  1196 + auto json_str = helpers::gen_json::gen_office_task_heart_beat_json({taskID});
  1197 + mq_manager_->publish(mq_type_t::HEART_BEAT_MQ, json_str.c_str(), true);
  1198 +#endif
  1199 +
  1200 + #ifdef WITH_FACE_DET_SS
  1201 + // 人脸任务结束
  1202 + auto task_param_ptr = m_task_param_manager->get_task_algor_param(taskID);
  1203 + if (task_param_ptr->human_face_algors.find(algorithm_type_t::FACE_SNAPSHOT) !=
  1204 + task_param_ptr->human_face_algors.end()) {
  1205 + m_face_det_ai_engine.finish_task(taskID);
  1206 + }
  1207 +#endif
  1208 +
  1209 + m_task_param_manager->delete_task_param(taskID);
  1210 +
  1211 + if (delete_snapshot) {
  1212 + m_snapshot_reprocessing->delete_finishtask_snapshot(taskID);
  1213 + ((save_snapshot_reprocessing *)m_save_snapshot_reprocessing)->delete_finishtask(taskID);
  1214 + }
  1215 +
  1216 + return ret;
  1217 +}
  1218 +
  1219 +bool CMultiSourceProcess::FinishTask(const string taskID){
  1220 + return finish_task(taskID,true);
  1221 +}
  1222 +
  1223 +void CMultiSourceProcess::CloseAllTask(){
  1224 + m_bfinish = true;
  1225 +
  1226 + FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
  1227 + pDecManager->closeAllDecoder();
  1228 +
  1229 + for (auto it = gpuProcessthreadMap.begin(); it != gpuProcessthreadMap.end(); it++){
  1230 + if(it->second != 0){
  1231 + pthread_join(*(it->second),0);
  1232 + delete it->second;
  1233 + }
  1234 + }
  1235 + gpuProcessthreadMap.clear();
  1236 +
  1237 + m_QueueMtx.lock();
  1238 + for (auto iter = m_RgbDataList.begin(); iter!=m_RgbDataList.end(); ){
  1239 + GpuRgbMemory* gpuMem = *iter;
  1240 + delete gpuMem;
  1241 + gpuMem = nullptr;
  1242 +
  1243 + iter = m_RgbDataList.erase(iter);
  1244 + }
  1245 + m_RgbDataList.clear();
  1246 + m_QueueMtx.unlock();
  1247 +
  1248 + VPT_Release(VPT_Handle_);
  1249 +
  1250 + ((save_snapshot_reprocessing *)m_save_snapshot_reprocessing)->save_snapshot_reprocessing_release();
  1251 + if (m_save_snapshot_reprocessing) {
  1252 + delete m_save_snapshot_reprocessing;
  1253 + m_save_snapshot_reprocessing = nullptr;
  1254 + }
  1255 +
  1256 + m_task_param_manager->task_param_manager_release();
  1257 +
1129 } 1258 }
1130 \ No newline at end of file 1259 \ No newline at end of file
tsl_aiplatform/ai_platform/MultiSourceProcess.h
@@ -140,9 +140,6 @@ public: @@ -140,9 +140,6 @@ public:
140 ~CMultiSourceProcess(); 140 ~CMultiSourceProcess();
141 141
142 int InitAlgorthim(tsl_aiplatform_param vptParam); 142 int InitAlgorthim(tsl_aiplatform_param vptParam);
143 - void *GetVPT_Handle() const {  
144 - return VPT_Handle_;  
145 - };  
146 143
147 #ifdef POST_USE_RABBITMQ 144 #ifdef POST_USE_RABBITMQ
148 int AddMqConn(mq_type_t mq_type, rabbitmq_conn_params_t mq_conn_param); 145 int AddMqConn(mq_type_t mq_type, rabbitmq_conn_params_t mq_conn_param);
@@ -150,38 +147,21 @@ public: @@ -150,38 +147,21 @@ public:
150 #endif 147 #endif
151 148
152 /* task api */ 149 /* task api */
153 - bool add_task_operation(task_param _cur_task_param);  
154 -  
155 - int AddOperator(task_param tparam);  
156 - void AddOperator(string taskID, int taskOper);  
157 -  
158 - void OperatorTask();  
159 - bool HasNewTask() const {  
160 - return !TaskOperatorQ.empty();  
161 - }  
162 - void PauseTask(const string taskID);  
163 - void RestartTask(const string taskID);  
164 - void FinishTask(const string taskID, const bool delete_snapshot);  
165 - bool DeleteTaskQ(const string taskID);  
166 -  
167 - /* decode api */  
168 - void FinishDecode(const string taskID);  
169 - bool FinishDecode(std::pair<const std::string, task_resource> &iter);  
170 -  
171 - /* sync api */  
172 - int WaitAndPauseTask(const string taskID, const int max_timeout_ms);  
173 - int WaitAndFinishTask(const string taskID, const int max_timeout_ms);  
174 - int WaitAndRestartTask(const string taskID, const int max_timeout_ms);  
175 -  
176 - /* frame process api */  
177 - int FinishProcessThread();  
178 -  
179 - int everyframe_process(vector<string> &task_in_play_id, sy_img *images, vector<onelevel_det_result> &ol_det_result); 150 + bool AddTask(task_param _cur_task_param);
  151 + bool PauseTask(const string taskID);
  152 + bool RestartTask(const string taskID);
  153 + bool FinishTask(const string taskID);
  154 + void CloseAllTask();
180 155
181 public: 156 public:
182 - void algorthim_process_thread(const string gpuid);  
183 - void post_decode_thread(task_param _cur_task_param, AVFrame * gpuFrame);  
184 - void decode_finished_thread(task_param _cur_task_param); 157 + // 生成线程的内部接口,外部勿调用
  158 + void algorthim_process_thread(const string gpuid); // 算法处理线程
  159 + void post_decode_thread(task_param _cur_task_param, AVFrame * gpuFrame); // 解码数据处理函数,在解码线程中执行
  160 + void decode_finished_thread(task_param _cur_task_param); // 解码线程结束后处理函数,在解码线程中执行
  161 +
  162 +#ifdef AUTHORIZATION
  163 + void check_thread(); // 授权校验线程
  164 +#endif
185 165
186 private: 166 private:
187 void startProcessByGpuid(const string gpuid); 167 void startProcessByGpuid(const string gpuid);
@@ -189,6 +169,8 @@ private: @@ -189,6 +169,8 @@ private:
189 bool task_has_face_algor(const std::string &task_id); 169 bool task_has_face_algor(const std::string &task_id);
190 void cuda_free_wrap(sy_img &img); 170 void cuda_free_wrap(sy_img &img);
191 171
  172 + bool finish_task(const string taskID, const bool delete_snapshot);
  173 +
192 // VPT 174 // VPT
193 void algorthim_vpt(vector<string>& task_list, sy_img *batch_img); 175 void algorthim_vpt(vector<string>& task_list, sy_img *batch_img);
194 // 行人安全分析算法 176 // 行人安全分析算法
@@ -217,34 +199,22 @@ private: @@ -217,34 +199,22 @@ private:
217 bool enable_async = true, const bool ori_img_is_in_gpu = true, 199 bool enable_async = true, const bool ori_img_is_in_gpu = true,
218 const bool roi_img_is_in_gpu = true); 200 const bool roi_img_is_in_gpu = true);
219 201
220 -private:  
221 - map<string, pthread_t*> gpuProcessthreadMap;  
222 -  
223 - boost::thread ProcessThread;  
224 - std::mutex _tx_add_task;  
225 -  
226 - deque<Operator> TaskOperatorQ;  
227 - int capacity; 202 + int everyframe_process(vector<string> &task_in_play_id, sy_img *images, vector<onelevel_det_result> &ol_det_result);
228 203
229 - double gpu_total_memory;  
230 - boost::thread thread_;  
231 - void *authority_handle{nullptr}; 204 +private:
  205 + map<string, pthread_t*> gpuProcessthreadMap; // 本想做个多GPU的,但是原版的多GPU设置由上层来做,而且似乎可以更好
  206 + list<GpuRgbMemory*> m_RgbDataList;
  207 + std::mutex m_QueueMtx;
  208 + bool m_bfinish;
232 209
233 -public: /*按道理不应该是public的 但是在线程函数中会用到以下的数据 每个都写一个get函数太过复杂*/  
234 - map<string, task_resource> system_all_tasks_; 210 + pthread_t m_authority_check_thread;
235 211
236 void *VPT_Handle_{nullptr}; 212 void *VPT_Handle_{nullptr};
237 213
238 int section_batch_size_; 214 int section_batch_size_;
239 int licence_status_; 215 int licence_status_;
240 - int thread_status_;  
241 - int gpu_id_; 216 + string gpu_id_;
242 217
243 - int AddTaskSucFlag; // 0:初始化状态 1:添加任务成功 -1:添加任务失败  
244 - int TaskInPlay;  
245 - int TotalTask;  
246 - set<string> TaskInPlayID;  
247 - map<string, uint> task_id_to_processed_frame_;  
248 218
249 // vector<onelevel_det_result> VPTResult; 219 // vector<onelevel_det_result> VPTResult;
250 std::atomic<bool> ProcessFlag; 220 std::atomic<bool> ProcessFlag;
@@ -290,15 +260,8 @@ public: /*按道理不应该是public的 但是在线程函数中会用到以下 @@ -290,15 +260,8 @@ public: /*按道理不应该是public的 但是在线程函数中会用到以下
290 private: 260 private:
291 base_reprocessing_unit *m_save_snapshot_reprocessing{nullptr}; 261 base_reprocessing_unit *m_save_snapshot_reprocessing{nullptr};
292 262
293 -  
294 -  
295 -private:  
296 - list<GpuRgbMemory*> m_RgbDataList;  
297 - std::mutex m_QueueMtx;  
298 }; 263 };
299 264
300 -static CMultiSourceProcess mainProcess;  
301 -  
302 #if 0 265 #if 0
303 266
304 struct CUVID_USERDATA { 267 struct CUVID_USERDATA {
tsl_aiplatform/ai_platform/header.h
@@ -429,7 +429,6 @@ typedef struct task_param { @@ -429,7 +429,6 @@ typedef struct task_param {
429 const char *task_id; //外部传入任务id 429 const char *task_id; //外部传入任务id
430 algor_config_param *algor_config_params; //该路rtsp流配置的所有算法参数 430 algor_config_param *algor_config_params; //该路rtsp流配置的所有算法参数
431 int algor_counts; //该路rtsp流共配置几种算法 431 int algor_counts; //该路rtsp流共配置几种算法
432 - const char* gpu_id_;  
433 } task_param; 432 } task_param;
434 #endif 433 #endif
435 434
tsl_aiplatform/ai_platform/stl_aiplatform.cpp
@@ -6,13 +6,13 @@ @@ -6,13 +6,13 @@
6 * @Description: 6 * @Description:
7 */ 7 */
8 #include "stl_aiplatform.h" 8 #include "stl_aiplatform.h"
9 -#include "MultiSourceVideoProcess.h" 9 +#include "MultiSourceProcess.h"
10 10
11 11
12 int tsl_aiplatform_init(void **handle, tsl_aiplatform_param param) 12 int tsl_aiplatform_init(void **handle, tsl_aiplatform_param param)
13 { 13 {
14 - *handle = new CMultiSourceVideoProcess();  
15 - CMultiSourceVideoProcess* tools = (CMultiSourceVideoProcess*)*handle; 14 + *handle = new CMultiSourceProcess();
  15 + CMultiSourceProcess* tools = (CMultiSourceProcess*)*handle;
16 return tools->InitAlgorthim(param); 16 return tools->InitAlgorthim(param);
17 } 17 }
18 18
@@ -20,7 +20,7 @@ int tsl_aiplatform_init(void **handle, tsl_aiplatform_param param) @@ -20,7 +20,7 @@ int tsl_aiplatform_init(void **handle, tsl_aiplatform_param param)
20 #ifdef POST_USE_RABBITMQ 20 #ifdef POST_USE_RABBITMQ
21 int add_mq_conn(void *handle, mq_type_t tstatus, rabbitmq_conn_params_t mq_conn_param) 21 int add_mq_conn(void *handle, mq_type_t tstatus, rabbitmq_conn_params_t mq_conn_param)
22 { 22 {
23 - CMultiSourceVideoProcess* tools = (CMultiSourceVideoProcess*)handle; 23 + CMultiSourceProcess* tools = (CMultiSourceProcess*)handle;
24 int res = tools->AddMqConn(tstatus, mq_conn_param); 24 int res = tools->AddMqConn(tstatus, mq_conn_param);
25 return res; 25 return res;
26 } 26 }
@@ -29,7 +29,7 @@ int add_mq_conn(void *handle, mq_type_t tstatus, rabbitmq_conn_params_t mq_conn_ @@ -29,7 +29,7 @@ int add_mq_conn(void *handle, mq_type_t tstatus, rabbitmq_conn_params_t mq_conn_
29 29
30 int get_task_status(void *handle, char *task_id) 30 int get_task_status(void *handle, char *task_id)
31 { 31 {
32 - CMultiSourceVideoProcess* tools = (CMultiSourceVideoProcess*)handle; 32 + CMultiSourceProcess* tools = (CMultiSourceProcess*)handle;
33 int res = tools->GetTaskStatus(task_id); 33 int res = tools->GetTaskStatus(task_id);
34 return res; 34 return res;
35 } 35 }
@@ -37,8 +37,8 @@ int get_task_status(void *handle, char *task_id) @@ -37,8 +37,8 @@ int get_task_status(void *handle, char *task_id)
37 37
38 int add_task(void *handle, task_param param) 38 int add_task(void *handle, task_param param)
39 { 39 {
40 - CMultiSourceVideoProcess* tools = (CMultiSourceVideoProcess*)handle;  
41 - int res = tools->AddOperator(param); 40 + CMultiSourceProcess* tools = (CMultiSourceProcess*)handle;
  41 + int res = tools->AddTask(param);
42 return res; 42 return res;
43 } 43 }
44 44
@@ -46,8 +46,8 @@ int add_task(void *handle, task_param param) @@ -46,8 +46,8 @@ int add_task(void *handle, task_param param)
46 int pause_task(void *handle, char * task_id, const int max_timeout_ms) 46 int pause_task(void *handle, char * task_id, const int max_timeout_ms)
47 { 47 {
48 int error_code = FAILED; 48 int error_code = FAILED;
49 - CMultiSourceVideoProcess* tools = (CMultiSourceVideoProcess*)handle;  
50 - error_code = tools->WaitAndPauseTask(task_id, max_timeout_ms); 49 + CMultiSourceProcess* tools = (CMultiSourceProcess*)handle;
  50 + error_code = tools->PauseTask(task_id);
51 return error_code; 51 return error_code;
52 } 52 }
53 53
@@ -55,16 +55,16 @@ int pause_task(void *handle, char * task_id, const int max_timeout_ms) @@ -55,16 +55,16 @@ int pause_task(void *handle, char * task_id, const int max_timeout_ms)
55 int restart_task(void *handle, char * task_id, const int max_timeout_ms) 55 int restart_task(void *handle, char * task_id, const int max_timeout_ms)
56 { 56 {
57 int error_code = FAILED; 57 int error_code = FAILED;
58 - CMultiSourceVideoProcess* tools = (CMultiSourceVideoProcess*)handle;  
59 - error_code = tools->WaitAndRestartTask(task_id, max_timeout_ms); 58 + CMultiSourceProcess* tools = (CMultiSourceProcess*)handle;
  59 + error_code = tools->RestartTask(task_id);
60 return error_code; 60 return error_code;
61 } 61 }
62 62
63 int finish_task(void *handle, char * task_id, const int max_timeout_ms) 63 int finish_task(void *handle, char * task_id, const int max_timeout_ms)
64 { 64 {
65 int error_code = FAILED; 65 int error_code = FAILED;
66 - CMultiSourceVideoProcess* tools = (CMultiSourceVideoProcess*)handle;  
67 - error_code = tools->WaitAndFinishTask(task_id, max_timeout_ms); 66 + CMultiSourceProcess* tools = (CMultiSourceProcess*)handle;
  67 + error_code = tools->FinishTask(task_id);
68 return error_code; 68 return error_code;
69 } 69 }
70 70
@@ -73,8 +73,8 @@ int tsl_aiplatform_release(void **handle) @@ -73,8 +73,8 @@ int tsl_aiplatform_release(void **handle)
73 { 73 {
74 if (*handle) 74 if (*handle)
75 { 75 {
76 - CMultiSourceVideoProcess* tools = (CMultiSourceVideoProcess*)*handle;  
77 - tools->FinishProcessThread(); 76 + CMultiSourceProcess* tools = (CMultiSourceProcess*)*handle;
  77 + tools->CloseAllTask();
78 78
79 delete tools; 79 delete tools;
80 tools = NULL; 80 tools = NULL;