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.

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