save_snapshot_reprocessing.cpp 3.66 KB
#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

  bFinish = false;
  m_save_img_thread = std::thread(save_img_thread_process, this);

  m_devId = devId;
  jpegUtil.jpeg_init(m_devId);
}

save_snapshot_reprocessing::~save_snapshot_reprocessing(){
  // 结束线程
  bFinish = true;
  m_save_img_thread.join();
  jpegUtil.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()){
      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(ImgSaveInfo saveInfo) {
  if (!bFinish) {
      bool bSaved = false;
      if(!saveInfo.file_path.empty()){
        bSaved = jpegUtil.jpeg_encode(saveInfo.img_info.pic_desc, saveInfo.file_path);
      }
      VPCUtil::vpc_img_release(saveInfo.img_info);

#ifdef POST_USE_RABBITMQ
      // LOG_INFO("mq publish process in: {}", saveInfo.json_str);
      if (bSaved && callback_ != nullptr && saveInfo.json_str.length() > 0) {
        // LOG_INFO("mq publish process begin");
        callback_(saveInfo.json_str.c_str());
        LOG_INFO("mq publish process end: {} ", saveInfo.json_str);
      }
#endif
  }

}

void save_snapshot_reprocessing::reprocessing_process_wo_locus_async(ImgSaveInfo saveInfo){

   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::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();

      bool bSaved = false;
      if(!cur_image.file_path.empty()){
        bSaved = jpegUtil.jpeg_encode(cur_image.img_info.pic_desc, cur_image.file_path);
      }
      VPCUtil::vpc_img_release(cur_image.img_info);

#ifdef POST_USE_RABBITMQ
      // LOG_INFO("mq publish process in: {}", cur_image.json_str);
      if (bSaved && callback_ != nullptr && cur_image.json_str.length() > 0) {
        // LOG_INFO("mq publish process begin");
        callback_(cur_image.json_str.c_str());
        LOG_INFO("mq publish process end: {} ", cur_image.json_str);
      }
#endif

    } else {
      l.unlock();
      std::this_thread::sleep_for(std::chrono::milliseconds(2));
    }

  }
}