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.

148 lines
4.0 KiB

7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
7 years ago
3 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. bool videoNode::pushFrame(cv::Mat *Frame, void *src ){
  29. if(m_bThreadStatus != TH_WORKING) return false;
  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. return true;
  50. };
  51. return false;
  52. };
  53. void videoNode::m_fThreadProc(){
  54. m_bThreadStatus = TH_WORKING;
  55. while( m_bThreadStatus == TH_WORKING ){
  56. if (m_bNewDataArival){
  57. if (m_vDestinations.size()){
  58. m_pMatProtector->lock();
  59. for (int index = 0; index < m_vDestinations.size(); index++){
  60. if (m_bThreadStatus == TH_WORKING) if (m_vDestinations.at(index) != NULL) m_vDestinations.at(index)->pushFrame( m_pCurrentMat, this );
  61. };
  62. m_pMatProtector->unlock();
  63. if (m_nRecorderCounter == 1) stopRecord();
  64. };
  65. m_bNewDataArival = false;
  66. } else SLEEP(10);
  67. };
  68. };
  69. void videoNode::connect(basicMediaDestination *V){
  70. if (V != NULL){
  71. m_pMatProtector->lock();
  72. m_vDestinations.push_back(V);
  73. m_pMatProtector->unlock();
  74. };
  75. };
  76. void videoNode::disconnect(basicMediaDestination *V){
  77. if (V != NULL){
  78. m_pMatProtector->lock();
  79. mediaDestinationsCollection::iterator index;
  80. for (index = m_vDestinations.begin(); index < m_vDestinations.end(); index++){
  81. if ((*index) == V) break;
  82. };
  83. m_vDestinations.erase(index);
  84. m_pMatProtector->unlock();
  85. };
  86. };
  87. bool videoNode::attachVideoAnalyzer(basicVideoAnalyzer *BVA){
  88. return true;
  89. };
  90. int videoNode::startRecord(uint32_t framesToRecord){
  91. m_pMatProtector->lock();
  92. if (!m_bRecording) {
  93. char tNAME[CTEXT];
  94. CSTR tVAL = storageProvider::getInstance()->getNewFileName(tNAME);
  95. if (tVAL == NULL) {
  96. return -1;
  97. };
  98. sprintf(m_sCurrentRecordName, "%s.%s", tNAME, VIDEO_OUTPUT_CONTAINER);
  99. sprintf(m_sCurrentRecordPath, "%s.%s", tVAL, VIDEO_OUTPUT_CONTAINER);
  100. free(tVAL);
  101. m_nRecordedFramesCount = 0;
  102. mediaDestinationsCollection::iterator index;
  103. uint8_t cnt = 0;
  104. for (index = m_vDestinations.begin(); index < m_vDestinations.end(); index++) {
  105. if ((*index)->isRecorder()) {
  106. (*index)->startRecord(m_sCurrentRecordPath);
  107. cnt++;
  108. };
  109. };
  110. if (cnt) {
  111. m_bRecording = true;
  112. } else {
  113. m_nRecorderCounter = 0;
  114. m_pMatProtector->unlock();
  115. return -1;
  116. };
  117. };
  118. m_nRecorderCounter += framesToRecord;
  119. m_pMatProtector->unlock();
  120. return m_nRecordedFramesCount;
  121. };
  122. int videoNode::stopRecord(void){
  123. if (!m_bRecording) return -1;
  124. m_pMatProtector->lock();
  125. mediaDestinationsCollection::iterator index;
  126. for (index = m_vDestinations.begin(); index < m_vDestinations.end(); index++){
  127. if ((*index)->isRecorder()) (*index)->stopRecord();
  128. };
  129. m_bRecording = false;
  130. m_pMatProtector->unlock();
  131. return m_nRecordedFramesCount;
  132. };
  133. CSTR videoNode::takeSnapshot(void){
  134. return NULL;
  135. };