<commit_before>/*
 *
 *
 *
 */

#include "TelmateFrameGrabberOpenCVImpl.hpp"
#include <KurentoException.hpp>

#define GST_CAT_DEFAULT telmate_frame_grabber_opencv_impl
GST_DEBUG_CATEGORY_STATIC(GST_CAT_DEFAULT);
#define GST_DEFAULT_NAME "TelmateFrameGrabberOpenCVImpl"


namespace kurento
{
namespace module
{
namespace telmateframegrabber
{

TelmateFrameGrabberOpenCVImpl::TelmateFrameGrabberOpenCVImpl ()
{

  GST_DEBUG_CATEGORY_INIT(GST_CAT_DEFAULT, GST_DEFAULT_NAME, 0,
                          GST_DEFAULT_NAME);

  this->uuid = "UUID_UNINITIALIZED";
  this->thrLoop = true;
  this->monThreadLoop = true;
  this->snapInterval = 1000;
  this->epName = "EP_NAME_UNINITIALIZED";
  this->storagePath = "/tmp/";
  this->framesCounter = 0;
  this->outputFormat = FGFMT_JPEG;
  this->lastQueueTimeStamp = 0;
  this->queueLength = 0;
  this->frameQueue = new avis_blocking_queue<VideoFrame*>;
  this->monitorTimeoutMs = 60000; // 60 sec.


  this->thr = new boost::thread(boost::bind(
          &TelmateFrameGrabberOpenCVImpl::queueHandler, this));
  this->thr->detach();

  this->wdThr = new boost::thread(boost::bind(
          &TelmateFrameGrabberOpenCVImpl::watchDogThread, this));
  this->wdThr->detach();

  GST_INFO("Constructor was called for %s", this->epName.c_str());

}

TelmateFrameGrabberOpenCVImpl::~TelmateFrameGrabberOpenCVImpl() {

  VideoFrame *ptrVf;
  this->thrLoop = false;
  boost::this_thread::sleep_for(boost::chrono::milliseconds(250));


  while(this->queueLength > 0) {
    this->frameQueue->pop(ptrVf); // blocks
    --this->queueLength;
    if(ptrVf != NULL) {
        delete ptrVf;
        ptrVf = NULL;
    }
  }

  delete this->frameQueue;
  this->frameQueue = NULL;

  this->monThreadLoop = false;

  GST_INFO("Destructor was called for %s", this->epName.c_str());

}

int TelmateFrameGrabberOpenCVImpl::cleanup() {
    /* for now do nothing, we may need to trigger our destructor from this */
  return 1;
}

/*
 * This function will be called for each new frame. The &mat variable
 * contains the current frame. Any image processing code should
 * be performed here and will be sent back to Media Pipline.
 */
void TelmateFrameGrabberOpenCVImpl::process(cv::Mat &mat) {
  if ((this->getCurrentTimestampLong() - this->lastQueueTimeStamp) >= this->snapInterval) {

    if(this->thrLoop) { // do not push into the queue if the destructor was called.<commit_msg>reduce destructor timeouts<commit_after>/*
 *
 *
 *
 */

#include "TelmateFrameGrabberOpenCVImpl.hpp"
#include <KurentoException.hpp>

#define GST_CAT_DEFAULT telmate_frame_grabber_opencv_impl
GST_DEBUG_CATEGORY_STATIC(GST_CAT_DEFAULT);
#define GST_DEFAULT_NAME "TelmateFrameGrabberOpenCVImpl"


namespace kurento
{
namespace module
{
namespace telmateframegrabber
{

TelmateFrameGrabberOpenCVImpl::TelmateFrameGrabberOpenCVImpl ()
{

  GST_DEBUG_CATEGORY_INIT(GST_CAT_DEFAULT, GST_DEFAULT_NAME, 0,
                          GST_DEFAULT_NAME);

  this->uuid = "UUID_UNINITIALIZED";
  this->thrLoop = true;
  this->monThreadLoop = false;
  this->snapInterval = 1000;
  this->epName = "EP_NAME_UNINITIALIZED";
  this->storagePath = "/tmp/";
  this->framesCounter = 0;
  this->outputFormat = FGFMT_JPEG;
  this->lastQueueTimeStamp = 0;
  this->queueLength = 0;
  this->frameQueue = new avis_blocking_queue<VideoFrame*>;
  this->monitorTimeoutMs = 60000; // 60 sec.


  this->thr = new boost::thread(boost::bind(
          &TelmateFrameGrabberOpenCVImpl::queueHandler, this));
  this->thr->detach();

  this->wdThr = new boost::thread(boost::bind(
          &TelmateFrameGrabberOpenCVImpl::watchDogThread, this));
  this->wdThr->detach();

  GST_INFO("Constructor was called for %s", this->epName.c_str());

}

TelmateFrameGrabberOpenCVImpl::~TelmateFrameGrabberOpenCVImpl() {

  VideoFrame *ptrVf;
  this->thrLoop = false;
  this->monThreadLoop = false;

  boost::this_thread::sleep_for(boost::chrono::milliseconds(50));


  while(this->queueLength > 0) {
    this->frameQueue->pop(ptrVf); // blocks
    --this->queueLength;
    if(ptrVf != NULL) {
        delete ptrVf;
        ptrVf = NULL;
    }
  }

  delete this->frameQueue;
  this->frameQueue = NULL;


  GST_INFO("Destructor was called for %s", this->epName.c_str());

}

int TelmateFrameGrabberOpenCVImpl::cleanup() {
    /* for now do nothing, we may need to trigger our destructor from this */
  return 1;
}

/*
 * This function will be called for each new frame. The &mat variable
 * contains the current frame. Any image processing code should
 * be performed here and will be sent back to Media Pipline.
 */
void TelmateFrameGrabberOpenCVImpl::process(cv::Mat &mat) {
  if ((this->getCurrentTimestampLong() - this->lastQueueTimeStamp) >= this->snapInterval) {

    if(this->thrLoop) { // do not push into the queue if the destructor was called.
{'fraction_non_alphanumeric': 0.12260216847372811, 'fraction_numerical': 0.008131776480400334, 'mean_word_length': 4.30641592920354, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '141415', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1627, 'n_words': 331}