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.

166 lines
5.1 KiB

7 years ago
​
6 years ago
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
  1. #include "lapsedVideoRecorder.h"
  2. #define LOG( X ) m_pLog->Log( "LVR", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "LVR", this, _LOG_ERROR, X )
  4. lapsedVideoRecorder::lapsedVideoRecorder() :basicMediaDestination(), basicEventHandler(){
  5. m_nTimeLapse = 0;
  6. m_nFrameLapse = 0;
  7. m_bLapseByTime = true;
  8. m_nCurrentFrame = 0;
  9. m_bOutputIsStarted = false;
  10. m_pCurrentOutput = NULL;
  11. m_pRealOutput = NULL;
  12. m_pLog = basicLogCollection::getInstance();
  13. };
  14. lapsedVideoRecorder::~lapsedVideoRecorder(){
  15. if( m_pRealOutput != NULL ) {
  16. //m_pRealOutput->stopRecord();
  17. delete( m_pRealOutput );
  18. };
  19. if( m_pCurrentOutput!=NULL ) {
  20. free( m_pCurrentOutput );
  21. };
  22. };
  23. void lapsedVideoRecorder::pushFrame(cv::Mat *Frame){
  24. if( Frame != NULL ){
  25. FRAME_QUEUE_ITEM *tItem;
  26. cv::Mat *tMat;
  27. tMat = new cv::Mat(Frame->rows, Frame->cols, Frame->type());
  28. if( tMat != NULL ) {
  29. memcpy( tMat->data, Frame->data, Frame->total() * Frame->elemSize() );
  30. if( m_nFrameLapse > 0 || m_nTimeLapse > 0 ){
  31. // Must have queue
  32. tItem = (FRAME_QUEUE_ITEM *)malloc(sizeof(FRAME_QUEUE_ITEM));
  33. if( tItem != NULL ){
  34. long long tCTime;
  35. tCTime = getMilliseconds();
  36. tItem->TS = tCTime;
  37. tItem->FRAME = tMat;
  38. m_vTimeLapse.push(tItem);
  39. if( m_bLapseByTime ) {
  40. // Time
  41. if( ( m_vTimeLapse.front()->TS + m_nTimeLapse ) < tCTime ){
  42. if (m_vTimeLapse.front()->FRAME != NULL){
  43. if( m_bOutputIsStarted ) {
  44. // Encode Frame
  45. m_pRealOutput->pushFrame( m_vTimeLapse.front()->FRAME );
  46. m_nCurrentFrame++;
  47. };
  48. // Free mem
  49. delete(m_vTimeLapse.front()->FRAME);
  50. }
  51. // free mem
  52. free(m_vTimeLapse.front());
  53. m_vTimeLapse.pop();
  54. };
  55. } else {
  56. // Frame
  57. if( m_vTimeLapse.size() > m_nFrameLapse ){
  58. if( m_vTimeLapse.front()->FRAME != NULL ){
  59. if( m_bOutputIsStarted ) {
  60. m_pRealOutput->pushFrame( m_vTimeLapse.front()->FRAME );
  61. // Check to finish
  62. m_mProtecor.lock();
  63. if( m_nFrameToSave ){
  64. m_nFrameToSave--;
  65. } else {
  66. // Finish up record
  67. m_pRealOutput->stopRecord();
  68. m_bOutputIsStarted = false;
  69. };
  70. m_nCurrentFrame++;
  71. m_mProtecor.unlock();
  72. };
  73. // Free mem
  74. delete(m_vTimeLapse.front()->FRAME);
  75. };
  76. // Free mem
  77. free(m_vTimeLapse.front());
  78. m_vTimeLapse.pop();
  79. };
  80. };
  81. } else {
  82. // Handle allocation error - cleanup
  83. LOG_ERROR("Cannot Alocate Queue Element!");
  84. delete( tMat );
  85. };
  86. } else {
  87. // Immidiate encode output (No TimeShift)
  88. LOG("NO Shift Defined!");
  89. };
  90. };
  91. };
  92. };
  93. void lapsedVideoRecorder::setTimeLapse( uint32_t milliseconds ){
  94. m_nTimeLapse = milliseconds;
  95. m_bLapseByTime = true;
  96. };
  97. void lapsedVideoRecorder::setFrameLapse( uint32_t frames ){
  98. m_nFrameLapse = frames;
  99. m_bLapseByTime = false;
  100. };
  101. bool lapsedVideoRecorder::continueOutput( void ){
  102. if( m_pCurrentOutput == NULL ) return false;
  103. if( m_pRealOutput == NULL ) return false;
  104. if( !m_bOutputIsStarted ) return false;
  105. m_mProtecor.lock();
  106. if( m_bLapseByTime ) {
  107. m_nTimeToSave = m_nTimeLapse << 1;
  108. m_nFrameToSave = 0;
  109. } else {
  110. m_nTimeToSave = 0;
  111. m_nFrameToSave = m_nFrameLapse << 1;
  112. };
  113. m_mProtecor.unlock();
  114. return true;
  115. };
  116. bool lapsedVideoRecorder::beginOutput( CSTR FName ){
  117. if( m_pRealOutput == NULL ){
  118. LOG("Create AVLib Dest!");
  119. m_pRealOutput = new videoAVLIBDestination();
  120. if( m_pRealOutput == NULL ) return false;
  121. LOG("OK!");
  122. };
  123. if( m_pCurrentOutput != NULL ){
  124. free( m_pCurrentOutput );
  125. };
  126. m_pCurrentOutput = (CSTR) malloc( strlen(FName)+1 );
  127. if( m_pCurrentOutput == NULL ) return false;
  128. memcpy( m_pCurrentOutput, FName, strlen(FName)+1 );
  129. LOG("StartRec AVLib Dest!");
  130. m_pRealOutput->startRecord( m_pCurrentOutput );
  131. LOG("Record is Started");
  132. if( m_bLapseByTime ) {
  133. m_nTimeToSave = m_nTimeLapse << 1;
  134. m_nFrameToSave = 0;
  135. } else {
  136. m_nTimeToSave = 0;
  137. m_nFrameToSave = m_nFrameLapse << 1;
  138. };
  139. m_bOutputIsStarted = true;
  140. return m_bOutputIsStarted;
  141. };
  142. bool lapsedVideoRecorder::finishOutput( void ){
  143. if( m_pCurrentOutput == NULL ) return true;
  144. if( m_pRealOutput != NULL ) m_pRealOutput->stopRecord();
  145. return true;
  146. };
  147. void lapsedVideoRecorder::dispatchEvent(EVENT_CONTAINER *Evt){
  148. if (Evt == NULL)return;
  149. };
  150. #undef LOG
  151. #undef LOG_ERROR