You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
7 years ago
​
​
​
7 years ago
7 years ago
​
​
7 years ago
  1. #include "videoNode.h"
  2. #include "../storage/storageProvider.h"
  3. videoNode::videoNode( config_setting_t * Settings ) : basicMediaDestination(), basicThread(){
  4. //m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "VIDEO_NODE");
  5. m_bNewDataArival = false;
  6. m_pCurrentMat = NULL;
  7. m_pMatProtector = new basicMutex();
  8. init();
  9. };
  10. videoNode::~videoNode(){
  11. stopConveyour();
  12. };
  13. void videoNode::stopConveyour(void){
  14. waitThreadEnd(true);
  15. if (m_vDestinations.size())m_vDestinations.empty();
  16. if (m_pMatProtector){
  17. //m_pMatProtector->unlock();
  18. delete(m_pMatProtector);
  19. m_pMatProtector = NULL;
  20. };
  21. if (m_pCurrentMat != NULL){
  22. m_pCurrentMat->release();
  23. delete m_pCurrentMat;
  24. m_pCurrentMat = NULL;
  25. };
  26. };
  27. void videoNode::pushFrame(cv::Mat *Frame, void *src ){
  28. if(m_bThreadStatus != TH_WORKING) return;
  29. if( m_pMatProtector->try_lock() ){
  30. if (m_nRecorderCounter) {
  31. m_nRecorderCounter--;
  32. };
  33. if (m_pCurrentMat == NULL){
  34. m_pCurrentMat = new cv::Mat(Frame->rows, Frame->cols, Frame->type());
  35. } else {
  36. if( (m_pCurrentMat->rows != Frame->rows) ||
  37. (m_pCurrentMat->cols != Frame->cols) ||
  38. (m_pCurrentMat->type() != Frame->type())){
  39. delete(m_pCurrentMat);
  40. m_pCurrentMat = new cv::Mat(Frame->rows, Frame->cols, Frame->type());
  41. };
  42. };
  43. if(m_pCurrentMat != NULL){
  44. memcpy(m_pCurrentMat->data, Frame->data, Frame->total() * Frame->elemSize());
  45. m_bNewDataArival = true;
  46. };
  47. m_pMatProtector->unlock();
  48. };
  49. };
  50. void videoNode::m_fThreadProc(){
  51. m_bThreadStatus = TH_WORKING;
  52. while( m_bThreadStatus == TH_WORKING ){
  53. if (m_bNewDataArival){
  54. if (m_vDestinations.size()){
  55. m_pMatProtector->lock();
  56. for (int index = 0; index < m_vDestinations.size(); index++){
  57. if (m_bThreadStatus == TH_WORKING) if (m_vDestinations.at(index) != NULL) m_vDestinations.at(index)->pushFrame(m_pCurrentMat, this );
  58. };
  59. m_pMatProtector->unlock();
  60. if (m_nRecorderCounter == 1) stopRecord();
  61. };
  62. m_bNewDataArival = false;
  63. } else SLEEP(10);
  64. };
  65. };
  66. void videoNode::connect(basicMediaDestination *V){
  67. if (V != NULL){
  68. m_pMatProtector->lock();
  69. m_vDestinations.push_back(V);
  70. m_pMatProtector->unlock();
  71. };
  72. };
  73. void videoNode::disconnect(basicMediaDestination *V){
  74. if (V != NULL){
  75. m_pMatProtector->lock();
  76. mediaDestinationsCollection::iterator index;
  77. for (index = m_vDestinations.begin(); index < m_vDestinations.end(); index++){
  78. if ((*index) == V) break;
  79. };
  80. m_vDestinations.erase(index);
  81. m_pMatProtector->unlock();
  82. };
  83. };
  84. bool videoNode::attachVideoAnalyzer(basicVideoAnalyzer *BVA){
  85. return true;
  86. };
  87. int videoNode::startRecord(uint32_t framesToRecord){
  88. m_pMatProtector->lock();
  89. if (!m_bRecording) {
  90. sprintf(m_sCurrentRecordName, "%s.%s", storageProvider::getInstance()->getNewFileName(), VIDEO_OUTPUT_CONTAINER );
  91. m_nRecordedFramesCount = 0;
  92. mediaDestinationsCollection::iterator index;
  93. uint8_t cnt = 0;
  94. for (index = m_vDestinations.begin(); index < m_vDestinations.end(); index++) {
  95. if ((*index)->isRecorder()) {
  96. (*index)->startRecord(m_sCurrentRecordName);
  97. cnt++;
  98. };
  99. };
  100. if (cnt) {
  101. m_bRecording = true;
  102. } else {
  103. m_nRecorderCounter = 0;
  104. m_pMatProtector->unlock();
  105. return -1;
  106. };
  107. };
  108. m_nRecorderCounter += framesToRecord;
  109. m_pMatProtector->unlock();
  110. return m_nRecordedFramesCount;
  111. };
  112. int videoNode::stopRecord(void){
  113. if (!m_bRecording) return -1;
  114. m_pMatProtector->lock();
  115. mediaDestinationsCollection::iterator index;
  116. for (index = m_vDestinations.begin(); index < m_vDestinations.end(); index++){
  117. if ((*index)->isRecorder()) (*index)->stopRecord();
  118. };
  119. m_bRecording = false;
  120. m_pMatProtector->unlock();
  121. return m_nRecordedFramesCount;
  122. };
  123. CSTR videoNode::takeSnapshot(void){
  124. return NULL;
  125. };