Blame view

src/decoder/dvpp/FFRecoderTaskManager.cpp 18.5 KB
746db74c   Hu Chunming   实现recode
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
  #include "FFRecoderTaskManager.h"
  #include <chrono>
  
  struct RecodeThreadParam {
      FFRecoderTaskManager* _this;
      RecodeParam param;
  };
  
  static long get_cur_time() {
  
  	chrono::time_point<chrono::system_clock, chrono::milliseconds> tpMicro
  		= chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
  
  	return tpMicro.time_since_epoch().count();
  }
  
  static string get_cur_datetime()
  {
  	using namespace std;
  	auto t = chrono::system_clock::to_time_t(chrono::system_clock::now());
  
  	char buffer[40];
  	strftime(buffer, 80, "%Y_%m_%d_%H_%M_%S", std::localtime(&t));
  	buffer[20] = '\0';
  	return buffer;
  }
  
  static bool is_key_frame(AVPacket *pkt) {  
      return (pkt->flags & AV_PKT_FLAG_KEY) != 0;  
  }
  
  FFRecoderTaskManager::FFRecoderTaskManager(){
5a84488e   Hu Chunming   添加农村事件开关
33
      m_recoder_thread = nullptr;
746db74c   Hu Chunming   实现recode
34
35
36
37
38
39
  }
  
  FFRecoderTaskManager::~FFRecoderTaskManager(){
      
  }
  
bf661eb0   Hu Chunming   录像文件保存优化
40
  bool FFRecoderTaskManager::init(AVStream* stream, AVCodecContext* avctx){
1b57a1c5   Hu Chunming   代码优化,避免可能的崩溃
41
42
43
44
      if(stream == nullptr || avctx == nullptr) {
          return false;
      }
  
bf661eb0   Hu Chunming   录像文件保存优化
45
      m_time_base = stream->time_base;
746db74c   Hu Chunming   实现recode
46
      m_avctx = avctx;
bf661eb0   Hu Chunming   录像文件保存优化
47
      m_inStream = stream;
746db74c   Hu Chunming   实现recode
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  
      m_recoder_thread = new std::thread(
          [](void* arg) {
              FFRecoderTaskManager* _this=(FFRecoderTaskManager*)arg;
              if(_this != nullptr) {
                  _this->recode_thread2();
              }else{
                  LOG_ERROR("recode 线程启动失败 !");
              }
              return (void*)0;
          }, this);
  
      return true;
  }
  
bf661eb0   Hu Chunming   录像文件保存优化
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  bool FFRecoderTaskManager::init3(AVRational time_base, AVCodecContext* avctx){
      m_time_base = time_base;
      m_avctx = avctx;
  
      m_recoder_thread = new std::thread(
          [](void* arg) {
              FFRecoderTaskManager* _this=(FFRecoderTaskManager*)arg;
              if(_this != nullptr) {
                  _this->recode_thread3();
              }else{
                  LOG_ERROR("recode 线程启动失败 !");
              }
              return (void*)0;
          }, this);
  
      return true;
  }
  
746db74c   Hu Chunming   实现recode
81
  void FFRecoderTaskManager::cache_pkt(AVPacket* pkt, long long frame_nb){
d9fc3e82   Hu Chunming   recode添加colse功能和mq功能
82
83
84
85
86
      if(m_bExit) {
          // 任务退出了就不再缓存数据了
          return;
      }
  
746db74c   Hu Chunming   实现recode
87
88
89
      std::lock_guard<std::mutex> l_pkt(m_pkt_list_mtx);
  
      // 考虑到一个AVPacket中的数据并不很大,为减少与解码模块的耦合度,方便管理,这里做一个clone
fbdee5c4   Hu Chunming   修正recode保存异常问题
90
91
92
93
94
95
96
97
      AVPacket *new_pkt = av_packet_alloc();
  	av_init_packet( new_pkt );
      new_pkt->data = (uint8_t *)av_malloc(pkt->size) ;
      memcpy(new_pkt->data, pkt->data, pkt->size);
      new_pkt->size = pkt->size;
      new_pkt->pts = pkt->pts;
      new_pkt->dts = pkt->dts;
      av_copy_packet_side_data(new_pkt, pkt);
746db74c   Hu Chunming   实现recode
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
  
      DataPacket* newDataPkt = new DataPacket();
      newDataPkt->pkt = new_pkt;
      newDataPkt->frame_nb = frame_nb;
      m_pkt_list.emplace_back(newDataPkt);
  
      if(is_key_frame(pkt)){
          // 越来越大的值
          newDataPkt->isKeyFrame = true;
          LOG_INFO("key frame_nb: {}", frame_nb);
      } else {
          newDataPkt->isKeyFrame = false;
      }
  
      std::lock_guard<std::mutex> l_info(m_recoderinfo_list_mtx);
      if(m_recoderinfo_list.size() <= 0){
          // 没有任务的时候,维持500的长度
          while(m_pkt_list.size() > 1000) {
              DataPacket* dataPkt = m_pkt_list.front();
              delete dataPkt;
              dataPkt = nullptr;
              m_pkt_list.pop_front();
          }
      }
  }
  
bf661eb0   Hu Chunming   录像文件保存优化
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  void FFRecoderTaskManager::cache_frame(AVFrame* frame, long long frame_nb){
      if(m_bExit) {
          // 任务退出了就不再缓存数据了
          return;
      }
  
      std::lock_guard<std::mutex> l_pkt(m_frame_list_mtx);
  
      // 考虑到一个AVPacket中的数据并不很大,为减少与解码模块的耦合度,方便管理,这里做一个clone
      AVFrame *new_pkt = av_frame_clone(frame);
  
      DataFrame* newFrame = new DataFrame();
      newFrame->frame = new_pkt;
      newFrame->frame_nb = frame_nb;
      m_frame_list.emplace_back(newFrame);
  
      // if(is_key_frame(pkt)){
      //     // 越来越大的值
      //     newFrame->isKeyFrame = true;
      //     LOG_INFO("key frame_nb: {}", frame_nb);
      // } else {
      //     newFrame->isKeyFrame = false;
      // }
  
      std::lock_guard<std::mutex> l_info(m_recoderinfo_list_mtx);
      if(m_recoderinfo_list.size() <= 0){
          // 没有任务的时候,维持500的长度
          while(m_frame_list.size() > 1000) {
              DataFrame* dataPkt = m_frame_list.front();
              delete dataPkt;
              dataPkt = nullptr;
              m_frame_list.pop_front();
          }
      }
  }
  
746db74c   Hu Chunming   实现recode
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  void FFRecoderTaskManager::save_intask_frame_nb(unsigned long long frame_nb) {
      if(m_intask_frame_nb_list.size() <= 0) {
          m_intask_frame_nb_list.push_back(frame_nb);
           return;
      }
  
      for(auto it = m_intask_frame_nb_list.begin(); it != m_intask_frame_nb_list.end(); it++) {
          if(*it > frame_nb) {
              m_intask_frame_nb_list.insert(it, frame_nb);
              return;
          }
      }
      
      //  frame_nb 比所有的都大
      m_intask_frame_nb_list.push_back(frame_nb);
  }
  
  void FFRecoderTaskManager::save_intask_recoderinfo(RecoderInfo info) {
      std::lock_guard<std::mutex> l(m_recoderinfo_list_mtx);
      if(m_recoderinfo_list.size() <= 0) {
          m_recoderinfo_list.push_back(info);
           return;
      }
  
      for(auto it = m_recoderinfo_list.begin(); it != m_recoderinfo_list.end(); it++) {
          if(it->frame_nb > info.frame_nb) {
              m_recoderinfo_list.insert(it, info);
              return;
          }
      }
      
      //  frame_nb 比所有的都大
      m_recoderinfo_list.push_back(info);
  }
  
  list<DataPacket*>::iterator FFRecoderTaskManager::getStartIterator(unsigned long long frame_nb){
      std::lock_guard<std::mutex> l(m_pkt_list_mtx);
  
      auto it_first = m_pkt_list.begin();
  
      long long start_frame_nb = (long long)(frame_nb - 375);
      if(start_frame_nb <= 0) {
          return it_first;
      }
  
746db74c   Hu Chunming   实现recode
205
206
207
208
209
210
211
212
213
214
215
216
217
218
      auto it_second = m_pkt_list.begin();
      for(;it_second != m_pkt_list.end(); it_second++) {
          DataPacket* dataPkt = *it_second;
          if(dataPkt->isKeyFrame) {
              it_first = it_second;
          }
          if(start_frame_nb >= (*it_first)->frame_nb && start_frame_nb <= (*it_second)->frame_nb) {
              return it_first;
          }
      }
  
      return m_pkt_list.begin();
  }
  
bf661eb0   Hu Chunming   录像文件保存优化
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  list<DataPacket*>::iterator FFRecoderTaskManager::getEndIterator(unsigned long long frame_nb){
      std::lock_guard<std::mutex> l(m_pkt_list_mtx);
  
      auto it_first = m_pkt_list.end();
  
      auto it_second = m_pkt_list.begin();
      for(;it_second != m_pkt_list.end(); it_second++) {
          DataPacket* dataPkt = *it_second;
          if (dataPkt->isKeyFrame && dataPkt->frame_nb >= frame_nb){
              return it_second;
          }
      }
  
      return m_pkt_list.end();
  }
  
  list<DataFrame*>::iterator FFRecoderTaskManager::getStartIterator3(unsigned long long frame_nb) {
      std::lock_guard<std::mutex> l(m_frame_list_mtx);
  
      auto it_first = m_frame_list.begin();
  
      long long start_frame_nb = (long long)(frame_nb - 375);
      if(start_frame_nb <= 0) {
          return it_first;
      }
  
      auto it_second = m_frame_list.begin();
      for(;it_second != m_frame_list.end(); it_second++) {
          DataFrame* dataPkt = *it_second;
          if (dataPkt->frame_nb >= start_frame_nb){
              return it_second;
          }
      }
  
      return m_frame_list.begin();
  }
  
746db74c   Hu Chunming   实现recode
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  // 多线程版
  void FFRecoderTaskManager::create_recode_task(AVRational time_base, AVCodecContext* avctx, RecoderInfo& recoderInfo){
  
      std::lock_guard<std::mutex> l(m_task_creat_mtx);
  
      RecodeParam recodeParam;
      recodeParam.time_base = time_base;
      recodeParam.avctx = avctx;
      recodeParam.recoderInfo = recoderInfo;
  
      RecodeThreadParam* threadParam = new RecodeThreadParam();
      threadParam->_this = this;
      threadParam->param = recodeParam;
      std::thread* recode_thread = new std::thread(
          [](void* arg) {
              RecodeThreadParam* threadParam = (RecodeThreadParam*)arg;
              if(threadParam != nullptr){
                  FFRecoderTaskManager* _this=(FFRecoderTaskManager*)threadParam->_this;
                  if(_this != nullptr) {
                      _this->recode_thread(threadParam->param);
                  }else{
                      LOG_ERROR("recode 线程启动失败 !");
                  }
              }
              return (void*)0;
          }, threadParam);
  
      std::string id = recoderInfo.task_id + "_" + recoderInfo.object_id + "_" + std::to_string(recoderInfo.frame_nb);
      m_id_recoderTask[id] = recode_thread;
  
      save_intask_frame_nb(recoderInfo.frame_nb);
  }
  
  void FFRecoderTaskManager::create_recode_task2(RecoderInfo& recoderInfo) {
d9fc3e82   Hu Chunming   recode添加colse功能和mq功能
290
291
292
293
      if(m_bExit) {
          // 任务退出了就不再接收录制任务
          return;
      }
746db74c   Hu Chunming   实现recode
294
295
296
297
298
299
300
301
302
303
304
305
306
      save_intask_recoderinfo(recoderInfo);
  }
  
  // 多线程版
  void FFRecoderTaskManager::recode_thread(RecodeParam recodeParam){
      {
          // 此处确保create_recode_task执行完成,m_id_recoderTask 已经保存当前线程信息
          std::lock_guard<std::mutex> l(m_task_creat_mtx);
      }
  
      RecoderInfo recoderInfo;
      recoderInfo = recodeParam.recoderInfo;
      std::string id = recoderInfo.task_id + "_" + recoderInfo.object_id + "_" + std::to_string(recoderInfo.frame_nb);
af3f87b8   Hu Chunming   recoderDir改为recod...
307
      string file_name = recoderInfo.recoderPath;
746db74c   Hu Chunming   实现recode
308
      FFRecoder ffrecoder;
bf661eb0   Hu Chunming   录像文件保存优化
309
      bool bInit = ffrecoder.init(m_inStream, recodeParam.avctx, file_name.c_str());
746db74c   Hu Chunming   实现recode
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
      if (!bInit) {
          LOG_ERROR("ffrecoder init error : {} {} {}", recoderInfo.task_id, recoderInfo.object_id, recoderInfo.frame_nb);
          m_id_recoderTask.erase(id);
          return;
      }
      LOG_INFO("record start, pkt_list size: {} id: {}", m_pkt_list.size(), id);
  
      int count = 0;
      for (auto it = m_pkt_list.begin(); it != m_pkt_list.end() && count < 500; ++it) {
          DataPacket* dataPkt = *it;
          AVPacket* pkt = dataPkt->pkt;
          ffrecoder.write_pkt(pkt);
          count++;
      }
  
      ffrecoder.flush();
      ffrecoder.uninit();
  
      m_id_recoderTask.erase(id);
  
      LOG_INFO("record end : {}", file_name);
  }
  
  void FFRecoderTaskManager::recode_thread2() {
d9fc3e82   Hu Chunming   recode添加colse功能和mq功能
334
      LOG_INFO("recode_thread2 start...");
746db74c   Hu Chunming   实现recode
335
      while(true) {
d9fc3e82   Hu Chunming   recode添加colse功能和mq功能
336
337
338
339
          if(m_bExit) {
              break;
          }
  
746db74c   Hu Chunming   实现recode
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
          m_recoderinfo_list_mtx.lock();
          if(m_recoderinfo_list.size() <= 0){
              m_recoderinfo_list_mtx.unlock();
              std::this_thread::sleep_for(std::chrono::milliseconds(3));
              continue;
          }
  
          auto it_param = m_recoderinfo_list.begin();
          RecoderInfo recoderinfo = *it_param;
          m_recoderinfo_list.pop_front();
          m_recoderinfo_list_mtx.unlock();
  
          auto it_data = getStartIterator(recoderinfo.frame_nb);
          if(it_data == m_pkt_list.end()) {
              std::this_thread::sleep_for(std::chrono::milliseconds(3));
              continue;
          }
  
bf661eb0   Hu Chunming   录像文件保存优化
358
359
360
361
362
363
          LOG_INFO("start frame_nb: {}", (*it_data)->frame_nb);
  
          m_pkt_list_mtx.lock();
          auto it = m_pkt_list.begin();
          while (it != it_data) {
              DataPacket* dataPkt = m_pkt_list.front();
fbdee5c4   Hu Chunming   修正recode保存异常问题
364
365
366
367
              // if(dataPkt->pkt != nullptr) {
              //     av_packet_free(&dataPkt->pkt);
              //     dataPkt->pkt = nullptr;
              // }
bf661eb0   Hu Chunming   录像文件保存优化
368
369
370
371
372
373
374
              delete dataPkt;
              dataPkt = nullptr;
              m_pkt_list.pop_front();
              it = m_pkt_list.begin();
          }
          m_pkt_list_mtx.unlock();
  
af3f87b8   Hu Chunming   recoderDir改为recod...
375
          string file_name = recoderinfo.recoderPath;
bf661eb0   Hu Chunming   录像文件保存优化
376
377
378
379
380
381
382
          FFRecoder ffrecoder;
          bool bInit = ffrecoder.init(m_inStream, m_avctx, file_name.c_str());
          if (!bInit) {
              LOG_ERROR("ffrecoder init error : {} {} {}", recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);
              ffrecoder.uninit();
              continue;
          }
af3f87b8   Hu Chunming   recoderDir改为recod...
383
          LOG_DEBUG("record start, pkt_list size: {} task_id: {}  object_id:{}  frame_nb: {}", m_pkt_list.size(), recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);
bf661eb0   Hu Chunming   录像文件保存优化
384
385
386
387
388
389
390
391
392
393
394
  
          int count = 0;
          auto it_save = it_data;
          unsigned long long start_frame_nb = (*it_data)->frame_nb;
          unsigned long long end_frame_nb = (*it_data)->frame_nb;
          for (; it_save != m_pkt_list.end() && count < 500; ++it_save) {
              DataPacket* dataPkt = *it_save;
              if(dataPkt->frame_nb > recoderinfo.frame_nb) {
                  break;
              }
              AVPacket* pkt = dataPkt->pkt;
1b57a1c5   Hu Chunming   代码优化,避免可能的崩溃
395
396
397
398
399
400
401
402
              if(pkt == nullptr) {
                  LOG_ERROR("{} pkt is nullptr", recoderinfo.task_id);
                  continue;
              } else if (pkt->data == nullptr || pkt->size <= 0){
                  LOG_ERROR("{} pkt data is nullptr", recoderinfo.task_id);
                  continue;
              }
              
bf661eb0   Hu Chunming   录像文件保存优化
403
404
405
406
407
              ffrecoder.write_pkt(pkt);
              count++;
              end_frame_nb = (*it_save)->frame_nb;
          }
  
e01a0397   Hu Chunming   代码优化;
408
          ffrecoder.flush_pkt();
bf661eb0   Hu Chunming   录像文件保存优化
409
410
411
          ffrecoder.uninit();
  
          // 发送mq消息
fbdee5c4   Hu Chunming   修正recode保存异常问题
412
          if(mq_publish_func && recoderinfo.mq_info.length() > 0) {
bf661eb0   Hu Chunming   录像文件保存优化
413
              mq_publish_func(recoderinfo.mq_info.c_str());
a63dd3d2   Hu Chunming   修复recode初始化崩溃问题
414
              // LOG_INFO("record save: {}", recoderinfo.mq_info.c_str());
bf661eb0   Hu Chunming   录像文件保存优化
415
416
417
418
419
420
421
422
423
          }
  
          LOG_INFO("record end, total save: {} start_frame_nb: {} end_frame_nb: {}  file_path: {}", count, start_frame_nb, end_frame_nb, file_name);
      }
  
      LOG_INFO("recode_thread2 end.");
  }
  
  void FFRecoderTaskManager::recode_thread3() {
e6fa5f85   Hu Chunming   日志优化
424
      LOG_INFO("recode_thread3 start...");
bf661eb0   Hu Chunming   录像文件保存优化
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
      while(true) {
          if(m_bExit) {
              break;
          }
  
          m_recoderinfo_list_mtx.lock();
          if(m_recoderinfo_list.size() <= 0){
              m_recoderinfo_list_mtx.unlock();
              std::this_thread::sleep_for(std::chrono::milliseconds(3));
              continue;
          }
  
          auto it_param = m_recoderinfo_list.begin();
          RecoderInfo recoderinfo = *it_param;
          m_recoderinfo_list.pop_front();
          m_recoderinfo_list_mtx.unlock();
  
          auto it_data = getStartIterator3(recoderinfo.frame_nb);
          if(it_data == m_frame_list.end()) {
              std::this_thread::sleep_for(std::chrono::milliseconds(3));
              continue;
          }
  
          LOG_INFO("start frame_nb: {}", (*it_data)->frame_nb);
  
          m_frame_list_mtx.lock();
          auto it = m_frame_list.begin();
          while (it != it_data) {
              DataFrame* dataPkt = m_frame_list.front();
              delete dataPkt;
              dataPkt = nullptr;
              m_frame_list.pop_front();
              it = m_frame_list.begin();
          }
          m_frame_list_mtx.unlock();
  
          std::string id = recoderinfo.task_id + "_" + recoderinfo.object_id + "_" + std::to_string(recoderinfo.frame_nb);
af3f87b8   Hu Chunming   recoderDir改为recod...
462
          string file_name = recoderinfo.recoderPath;
bf661eb0   Hu Chunming   录像文件保存优化
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
          FFRecoder ffrecoder;
          bool bInit = ffrecoder.init(m_avctx->width, m_avctx->height, m_time_base, m_avctx, file_name.c_str());
          if (!bInit) {
              LOG_ERROR("ffrecoder init error : {} {} {}", recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);
              ffrecoder.uninit();
              continue;
          }
          LOG_INFO("record start, pkt_list size: {} id: {}", m_frame_list.size(), id);
  
          int count = 0;
          auto it_save = it_data;
          unsigned long long start_frame_nb = (*it_data)->frame_nb;
          unsigned long long end_frame_nb = (*it_data)->frame_nb;
          for (; it_save != m_frame_list.end() && count < 500; ++it_save) {
              DataFrame* dataPkt = *it_save;
              AVFrame* pkt = dataPkt->frame;
              ffrecoder.write_frame(pkt);
              count++;
              end_frame_nb = (*it_save)->frame_nb;
          }
  
          // ffrecoder.flush();
          ffrecoder.uninit();
  
          // 发送mq消息
          if(mq_publish_func) {
              mq_publish_func(recoderinfo.mq_info.c_str());
          }
  
          LOG_INFO("record end, total save: {} start_frame_nb: {} end_frame_nb: {}  file_path: {}", count, start_frame_nb, end_frame_nb, file_name);
      }
  
e6fa5f85   Hu Chunming   日志优化
495
      LOG_INFO("recode_thread3 end.");
bf661eb0   Hu Chunming   录像文件保存优化
496
497
498
  }
  
  void FFRecoderTaskManager::recode_thread4() {
e6fa5f85   Hu Chunming   日志优化
499
      LOG_INFO("recode_thread4 start...");
bf661eb0   Hu Chunming   录像文件保存优化
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
      while(true) {
          if(m_bExit) {
              break;
          }
  
          m_recoderinfo_list_mtx.lock();
          if(m_recoderinfo_list.size() <= 0){
              m_recoderinfo_list_mtx.unlock();
              std::this_thread::sleep_for(std::chrono::milliseconds(3));
              continue;
          }
  
          auto it_param = m_recoderinfo_list.begin();
          RecoderInfo recoderinfo = *it_param;
          m_recoderinfo_list.pop_front();
          m_recoderinfo_list_mtx.unlock();
  
          auto it_data = getStartIterator(recoderinfo.frame_nb);
          if(it_data == m_pkt_list.end()) {
              std::this_thread::sleep_for(std::chrono::milliseconds(3));
              continue;
          }
  
746db74c   Hu Chunming   实现recode
523
524
525
526
527
528
529
530
531
532
533
534
535
536
          LOG_INFO("start frame_nb: {}", (*it_data)->frame_nb);
  
          m_pkt_list_mtx.lock();
          auto it = m_pkt_list.begin();
          while (it != it_data) {
              DataPacket* dataPkt = m_pkt_list.front();
              delete dataPkt;
              dataPkt = nullptr;
              m_pkt_list.pop_front();
              it = m_pkt_list.begin();
          }
          m_pkt_list_mtx.unlock();
  
          std::string id = recoderinfo.task_id + "_" + recoderinfo.object_id + "_" + std::to_string(recoderinfo.frame_nb);
af3f87b8   Hu Chunming   recoderDir改为recod...
537
          string file_name = recoderinfo.recoderPath;
746db74c   Hu Chunming   实现recode
538
          FFRecoder ffrecoder;
bf661eb0   Hu Chunming   录像文件保存优化
539
          bool bInit = ffrecoder.init(m_inStream, m_avctx, file_name.c_str());
746db74c   Hu Chunming   实现recode
540
541
          if (!bInit) {
              LOG_ERROR("ffrecoder init error : {} {} {}", recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);
d9fc3e82   Hu Chunming   recode添加colse功能和mq功能
542
              ffrecoder.uninit();
746db74c   Hu Chunming   实现recode
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
              continue;
          }
          LOG_INFO("record start, pkt_list size: {} id: {}", m_pkt_list.size(), id);
  
          int count = 0;
          auto it_save = it_data;
          unsigned long long start_frame_nb = (*it_data)->frame_nb;
          unsigned long long end_frame_nb = (*it_data)->frame_nb;
          for (; it_save != m_pkt_list.end() && count < 500; ++it_save) {
              DataPacket* dataPkt = *it_save;
              AVPacket* pkt = dataPkt->pkt;
              ffrecoder.write_pkt(pkt);
              count++;
              end_frame_nb = (*it_save)->frame_nb;
          }
  
          // ffrecoder.flush();
          ffrecoder.uninit();
  
d9fc3e82   Hu Chunming   recode添加colse功能和mq功能
562
563
564
565
566
          // 发送mq消息
          if(mq_publish_func) {
              mq_publish_func(recoderinfo.mq_info.c_str());
          }
  
746db74c   Hu Chunming   实现recode
567
568
569
          LOG_INFO("record end, total save: {} start_frame_nb: {} end_frame_nb: {}  file_path: {}", count, start_frame_nb, end_frame_nb, file_name);
      }
  
e6fa5f85   Hu Chunming   日志优化
570
      LOG_INFO("recode_thread4 end.");
d9fc3e82   Hu Chunming   recode添加colse功能和mq功能
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
  }
  
  void FFRecoderTaskManager::close() {
      m_bExit = true;
  
      if (m_recoder_thread) {
          m_recoder_thread->join();
          m_recoder_thread = nullptr;
      }
  
      // 清空数据
      while(!m_pkt_list.empty()) {
          DataPacket* dataPkt = m_pkt_list.front();
          delete dataPkt;
          dataPkt = nullptr;
          m_pkt_list.pop_front();
      }
  }
  
  void FFRecoderTaskManager::set_mq_callback(mq_callback_t cb) {
      mq_publish_func = cb;
746db74c   Hu Chunming   实现recode
592
  }