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.

165 lines
5.1 KiB

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