From aec056c7c74062aa2ab415aa21db85654189f2a0 Mon Sep 17 00:00:00 2001 From: poocka Date: Tue, 24 Sep 2019 19:55:12 +0300 Subject: [PATCH] Event Report Object Work stage 1 --- lib/App/3dGUI/main_window.cpp | 4 +- lib/components/base/basicMediaDestination.h | 6 +- lib/components/object/objectEventReport.cpp | 201 +++++++++++++----- lib/components/object/objectEventReport.h | 36 +++- lib/data/media/mediaVideoSourceManager.h | 3 +- .../media/video/videoAVLIBDestination.cpp | 9 +- lib/data/media/video/videoAVLIBDestination.h | 6 +- lib/data/media/videoNode.cpp | 44 +++- lib/data/media/videoNode.h | 27 ++- 9 files changed, 245 insertions(+), 91 deletions(-) diff --git a/lib/App/3dGUI/main_window.cpp b/lib/App/3dGUI/main_window.cpp index 97fc12e1..aff68fc1 100644 --- a/lib/App/3dGUI/main_window.cpp +++ b/lib/App/3dGUI/main_window.cpp @@ -431,14 +431,14 @@ void mainWinow::handleVideoRecorder(){ if(!m_bUserRecordsVideo){ // Generate Video Event -> Starts Record tData->m_cCode = CMD_CAMERA_START_RECORD; - pEvt = createBasicEvent( 0, EVT_CLASS_IMAGE, EVT_COMMAND, "User Starts Record Video", tData ); + pEvt = createBasicEvent( 0, EVT_CLASS_VIDEO, EVT_COMMAND, "User Starts Record Video", tData ); basicEventHeap::getInstance()->broadcast(pEvt); m_bUserRecordsVideo = true; m_pButtonCmdVideoRecord->setIcon(QIcon(":pictures/appbar.control.stop.png")); m_pButtonCmdVideoRecord->setText(QStringLiteral("Stop Record")); } else { tData->m_cCode = CMD_CAMERA_STOP_RECORD; - pEvt = createBasicEvent( 0, EVT_CLASS_IMAGE, EVT_COMMAND, "User Stops Record Video", tData ); + pEvt = createBasicEvent( 0, EVT_CLASS_VIDEO, EVT_COMMAND, "User Stops Record Video", tData ); basicEventHeap::getInstance()->broadcast(pEvt); m_bUserRecordsVideo = false; m_pButtonCmdVideoRecord->setIcon(QIcon(":pictures/appbar.cabinet.in.png")); diff --git a/lib/components/base/basicMediaDestination.h b/lib/components/base/basicMediaDestination.h index 664e3930..0ae3afbd 100644 --- a/lib/components/base/basicMediaDestination.h +++ b/lib/components/base/basicMediaDestination.h @@ -18,7 +18,6 @@ class basicMediaDestination { basicLogCollection *m_pLog; // Constructor basicMediaDestination(); - // Destructor virtual ~basicMediaDestination(); @@ -35,8 +34,9 @@ class basicMediaDestination { #endif virtual void setError(bool A, CSTR BOBO = NULL ){}; //virtual bool init(void){ return true; }; - virtual void startRecord( CSTR fileName ){}; - virtual void stopRecord(void){}; + virtual int startRecord(uint32_t FramesToRecord) { return -1; }; + virtual void startRecord(CSTR FileName) {}; + virtual int stopRecord(void) { return -1; }; virtual bool isRecorder(void){ return false; }; }; diff --git a/lib/components/object/objectEventReport.cpp b/lib/components/object/objectEventReport.cpp index 10eeddeb..e2cb4738 100644 --- a/lib/components/object/objectEventReport.cpp +++ b/lib/components/object/objectEventReport.cpp @@ -1,6 +1,7 @@ #include "objectEventReport.h" #include "../base/basicEventHeap.h" #include "../../hardware/soc/general/3DLRF.h" +#include "../../data/media/mediaVideoSourceManager.h" objectEventReport *objectEventReport::_instance = NULL; /** @@ -19,6 +20,7 @@ objectEventReport* objectEventReport::getInstance() { // Constructor implementation objectEventReport::objectEventReport() : basicEventHandler(){ m_pSettingsEngine = NULL; + m_pMediaManager = mediaVideoSourceManager::getInstance(); m_pStorageProvider = storageProvider::getInstance(); m_pStorageProvider->startNewSeries(); sprintf( m_pReportFileName, "%s", m_pStorageProvider->getNewFileName()); @@ -29,61 +31,164 @@ objectEventReport::~objectEventReport(){ }; -void objectEventReport::dispatchEvent( basicEvent *Evt ){ - if( Evt == NULL ) return; - //if (Evt->m_eClass == EVT_CLASS_SENSOR || Evt->m_eClass == EVT_CLASS_SYSTEM){ - //Store all events - addGenericEvent( Evt ); - //}; -}; -bool objectEventReport::addGenericEvent( basicEvent *Evt ){ - FILE *FD; - FD = fopen(m_pReportFileName, "a+"); - if( FD != NULL ){ - if( Evt->m_pEvtData == NULL ){ - fprintf( FD, "%d,\"%s\"\n", Evt->m_nSourceUID, Evt->m_pHumanReadableMsg ); - } else { - CLIENT_EVENT *tData; - float fRange, fAzimuth, fElevation; - uint16_t cX, cY; - bool lMaster; - tData = (CLIENT_EVENT*)Evt->m_pEvtData; - uint8_t kid; - kid = tData->k_id & 0x07; - lMaster = ((tData->k_id & 0x08) == 0x08 ); - fRange = 300.0 / 4095.0 * tData->range; - //makeCds( tData->mStartID, sX, sY, m_bIsMaster ); - //makeCds( tData->mEndID, eX, eY, m_bIsMaster ); - //cX = ((sX+eX)>>1); - //cY = ((sY+eY)>>1); - makeXY( tData->th_id, cX, cY, lMaster ); - fAzimuth = 360.0 - (360.0 / TH_LINE_NODES )*cX; - fElevation = 90.0 - (Q_DEGREE_FOV/Q_SCAN_COUNT )*cY + Q_SHIFT_DOWN_FOV; - - fprintf( FD, "%d,%d:%d,%c,%d,%c,%0.3f,%0.3f,%0.3f,%d\n", - Evt->m_nSourceUID, - tData->uts, - tData->ums, - (lMaster?'M':'S'), - kid, - ((tData->type == EVT_TARGET_WHITE )?'W':'B'), - fRange, - fAzimuth, - fElevation, - tData->w_cnt*tData->range - ); +bool objectEventReport::getMedia(REPORT_RECORD* Record){ + if (Record == NULL) return false; + if (m_pMediaManager == NULL) return false; + if (m_pMediaManager->getCameraCollectionCount()) { + Record->media_names = (char**)malloc(sizeof(char*) * m_pMediaManager->getCameraCollectionCount()); + if (Record->media_names != NULL) { + vectorCamerasInterator index = m_pMediaManager->getCameraCollectionBegin(); + Record->media_count = 0; + for (; index < m_pMediaManager->getCameraCollectionEnd(); index++) { + char *tName = (*index)->m_pVideoNode->getCurrentRecordName(); + Record->media_names[Record->media_count] = (char*)malloc(strlen(tName) + 1); + if (Record->media_names[Record->media_count]!= NULL) { + memcpy(Record->media_names[Record->media_count], tName, strlen(tName)); + Record->media_names[Record->media_count][strlen(tName)] = (char)0; + Record->media_count++; + }; + }; }; - fclose( FD ); }; return true; }; -bool objectEventReport::addVideoRecord( CSTR fileName ){ - return true; +void objectEventReport::dispatchEvent( basicEvent *Evt ){ + REPORT_RECORD * tVal = NULL; + if( Evt == NULL ) return; + /* + switch (Evt->m_eType) { + case EVT_NOINIT: { break; }; + case EVT_READY: { break; }; + case EVT_ALARM: { break; }; + case EVT_COMMAND: { break; }; + case EVT_KEY: { break; }; + case EVT_JOYSTICK: { break; }; + case EVT_STUDY: { break; }; + case EVT_BUSY: { break; }; + case EVT_FLIP: { break; }; + case EVT_SCAN: { break; }; + case EVT_TARGET: { break; }; + case EVT_TARGET_WHITE: { break; }; + case EVT_TARGET_BLACK: { break; }; + case EVT_CAPTURE_START: { break; }; + case EVT_CAPTURE_END: { break; }; + case EVT_CAPTURE_DONE: { break; }; + case EVT_ERROR: { break; }; + default: {}; + };*/ + + tVal = (REPORT_RECORD*)malloc(sizeof(REPORT_RECORD)); + if (tVal != NULL) { + prefill(tVal); + memset(tVal, 0, sizeof(REPORT_RECORD)); + switch (Evt->m_eClass) { + case EVT_CLASS_SYSTEM: { + switch (Evt->m_eType) { + case EVT_NOINIT: { break; }; + case EVT_COMMAND: { break; }; + case EVT_KEY: { break; }; + case EVT_JOYSTICK: { break; }; + case EVT_ERROR: { break; }; + default: {}; + }; + break; + }; + case EVT_CLASS_SENSOR: { + switch (Evt->m_eType) { + case EVT_NOINIT: { break; }; + case EVT_READY: { break; }; + case EVT_ALARM: { break; }; + case EVT_STUDY: { break; }; + case EVT_BUSY: { break; }; + case EVT_FLIP: { break; }; + case EVT_SCAN: { break; }; + case EVT_TARGET: + case EVT_TARGET_WHITE: + case EVT_TARGET_BLACK: { + + break; + }; + case EVT_ERROR: { break; }; + default: {}; + }; + break; + }; + case EVT_CLASS_VIDEO: { + switch (Evt->m_eType) { + case EVT_NOINIT: { break; }; + case EVT_READY: { break; }; + case EVT_ALARM: { break; }; + case EVT_COMMAND: { + basicCameraCommand * tCMD; + tCMD = (basicCameraCommand*)Evt->m_pEvtData; + if (tCMD->m_cCode == CMD_CAMERA_START_RECORD) { + + }; + break; + }; + case EVT_CAPTURE_START: { break; }; + case EVT_CAPTURE_END: { break; }; + case EVT_CAPTURE_DONE: { break; }; + case EVT_ERROR: { break; }; + default: {}; + }; + break; + }; + case EVT_CLASS_IMAGE: { + switch (Evt->m_eType) { + case EVT_NOINIT: { break; }; + case EVT_READY: { break; }; + case EVT_COMMAND: { break; }; + case EVT_CAPTURE_START: { break; }; + case EVT_CAPTURE_END: { break; }; + case EVT_CAPTURE_DONE: { break; }; + case EVT_ERROR: { break; }; + default: {}; + }; + break; + }; + default: { + }; + }; + free(tVal); + }; }; -bool objectEventReport::addSnapshot(CSTR fileName) { - return true; +bool objectEventReport::saveReportRecord(REPORT_RECORD* Record){ + FILE *FD; + if (Record == NULL) return false; + FD = fopen(m_pReportFileName, "ab+"); + if( FD != NULL ){ + size_t rVal; + uint16_t nSize; + rVal = fwrite(Record, 18, 1, FD); + if (rVal != 18) { + fclose(FD); + return false; + }; + if (Record->media_count) { + + }; + rVal = fwrite(&Record->data_size, 2, 1, FD); + if (rVal != 2) { + fclose(FD); + return false; + }; + rVal = fwrite(Record->data, Record->data_size, 1, FD); + if (rVal != Record->data_size) { + fclose(FD); + return false; + }; + rVal = fwrite( "\r\n", 2, 1, FD); + if (rVal != 2) { + fclose(FD); + return false; + }; + fclose(FD); + return true; + }; + return false; }; bool objectEventReport::loadReportList(struct tm* LTM){ diff --git a/lib/components/object/objectEventReport.h b/lib/components/object/objectEventReport.h index 69ebbd5c..0c4ec079 100644 --- a/lib/components/object/objectEventReport.h +++ b/lib/components/object/objectEventReport.h @@ -14,23 +14,39 @@ * * @author Gagua */ - +typedef struct S_REPORT_RECORD { + uint32_t ms; //4 + uint16_t ums; //2 + uint8_t cls_id; //1 + uint8_t type; //1 + uint32_t src; //4 + uint8_t media_type; //1 + uint32_t media_marker; //4 + uint8_t media_count; //1 + //----------------------------------- 18 bytes = Fixed Size Header + char ** media_names; + uint16_t data_size; + void * data; +}REPORT_RECORD; typedef std::vector Rere; +class mediaVideoSourceManager; + class objectEventReport : public basicEventHandler { private: - storageProvider * m_pStorageProvider; - basicSettings * m_pSettingsEngine; - char m_pReportFileName[MAX_PATH]; - bool addVideoRecord( CSTR fileName ); - bool addSnapshot(CSTR fileName); - bool addGenericEvent( basicEvent *Evt ); - Rere m_vLoaddedReports; + storageProvider * m_pStorageProvider; + basicSettings * m_pSettingsEngine; + mediaVideoSourceManager * m_pMediaManager; + char m_pReportFileName[MAX_PATH]; + bool saveReportRecord(REPORT_RECORD *Record); + bool getMedia(REPORT_RECORD* Record); + bool prefill(REPORT_RECORD* Record); + REPORT_RECORD * loadReportRecord(FILE* FD); + Rere m_vLoaddedReports; public: static objectEventReport *getInstance(void); - bool bindCamera( basicCamera * tPtr ); virtual ~objectEventReport(void); - void dispatchEvent(basicEvent *Evt); + void dispatchEvent( basicEvent *Evt); bool loadReportList(struct tm *LTM); Rere * getLoaddedReports(void); private: diff --git a/lib/data/media/mediaVideoSourceManager.h b/lib/data/media/mediaVideoSourceManager.h index 68d5466f..29bb6564 100644 --- a/lib/data/media/mediaVideoSourceManager.h +++ b/lib/data/media/mediaVideoSourceManager.h @@ -47,7 +47,8 @@ class mediaVideoSourceManager { static const char *getStandardSize( bool wide, int id ); static const int getStandardWidth( bool wide, int id); static const int getStandardHeight( bool wide, int id); - basicMediaSource *getVideoSource( int sourceId ); + basicMediaSource *getVideoSource( int sourceId ); + size_t getCameraCollectionCount(void){ return m_vCameras.size(); }; vectorCamerasInterator getCameraCollectionBegin(void){ return m_vCameras.begin(); }; vectorCamerasInterator getCameraCollectionEnd(void){ return m_vCameras.end(); }; void shutdownAllVideoSources(void); diff --git a/lib/data/media/video/videoAVLIBDestination.cpp b/lib/data/media/video/videoAVLIBDestination.cpp index 50565d78..c5c9fbc2 100644 --- a/lib/data/media/video/videoAVLIBDestination.cpp +++ b/lib/data/media/video/videoAVLIBDestination.cpp @@ -114,9 +114,12 @@ void videoAVLIBDestination::startRecord( CSTR fileName ){ }; }; -void videoAVLIBDestination::stopRecord(void){ - m_bRecordIsStarted = false; - close(); +int videoAVLIBDestination::stopRecord(void){ + if (m_bRecordIsStarted) { + close(); + m_bRecordIsStarted = false; + }; + return 0; }; diff --git a/lib/data/media/video/videoAVLIBDestination.h b/lib/data/media/video/videoAVLIBDestination.h index 57034e38..c9831cca 100644 --- a/lib/data/media/video/videoAVLIBDestination.h +++ b/lib/data/media/video/videoAVLIBDestination.h @@ -141,9 +141,9 @@ class videoAVLIBDestination : public basicMediaDestination { virtual void pushFrame(cv::Mat *Frame, void *src = NULL); bool create( CSTR URI, int fps=-1, int w=-1, int h=-1); //void setError(bool A, CSTR BOBO = ""); - void startRecord( CSTR fileName ); - void stopRecord(void); - bool isRecorder(void){ return true; }; + virtual void startRecord( CSTR fileName ); + virtual int stopRecord(void); + virtual bool isRecorder(void){ return true; }; }; #endif diff --git a/lib/data/media/videoNode.cpp b/lib/data/media/videoNode.cpp index 33f3ffd5..907fe823 100644 --- a/lib/data/media/videoNode.cpp +++ b/lib/data/media/videoNode.cpp @@ -30,7 +30,10 @@ void videoNode::stopConveyour(void){ }; void videoNode::pushFrame(cv::Mat *Frame, void *src ){ if(m_bThreadStatus != TH_WORKING) return; - if( m_pMatProtector->try_lock() ){ + if( m_pMatProtector->try_lock() ){ + if (m_nRecorderCounter) { + m_nRecorderCounter--; + }; if (m_pCurrentMat == NULL){ m_pCurrentMat = new cv::Mat(Frame->rows, Frame->cols, Frame->type()); } else { @@ -59,6 +62,7 @@ void videoNode::m_fThreadProc(){ if (m_bThreadStatus == TH_WORKING) if (m_vDestinations.at(index) != NULL) m_vDestinations.at(index)->pushFrame(m_pCurrentMat, this ); }; m_pMatProtector->unlock(); + if (m_nRecorderCounter == 1) stopRecord(); }; m_bNewDataArival = false; } else SLEEP(10); @@ -90,26 +94,44 @@ bool videoNode::attachVideoAnalyzer(basicVideoAnalyzer *BVA){ return true; }; -void videoNode::startRecord(void){ +int videoNode::startRecord(uint32_t framesToRecord){ m_pMatProtector->lock(); - mediaDestinationsCollection::iterator index; - for (index = m_vDestinations.begin(); index < m_vDestinations.end(); index++){ - char tBuffer[MAX_PATH]; - sprintf(tBuffer, "%s.%s", storageProvider::getInstance()->getNewFileName(), VIDEO_OUTPUT_CONTAINER ); - if ((*index)->isRecorder()) (*index)->startRecord(tBuffer); + if (!m_bRecording) { + sprintf(m_sCurrentRecordName, "%s.%s", storageProvider::getInstance()->getNewFileName(), VIDEO_OUTPUT_CONTAINER ); + m_nRecordedFramesCount = 0; + mediaDestinationsCollection::iterator index; + uint8_t cnt = 0; + for (index = m_vDestinations.begin(); index < m_vDestinations.end(); index++) { + if ((*index)->isRecorder()) { + (*index)->startRecord(m_sCurrentRecordName); + cnt++; + }; + }; + if (cnt) { + m_bRecording = true; + } else { + m_nRecorderCounter = 0; + m_pMatProtector->unlock(); + return -1; + }; }; + m_nRecorderCounter += framesToRecord; m_pMatProtector->unlock(); + return m_nRecordedFramesCount; }; -void videoNode::stopRecord(void){ - m_pMatProtector->lock(); +int videoNode::stopRecord(void){ + if (!m_bRecording) return -1; + m_pMatProtector->lock(); mediaDestinationsCollection::iterator index; for (index = m_vDestinations.begin(); index < m_vDestinations.end(); index++){ if ((*index)->isRecorder()) (*index)->stopRecord(); }; + m_bRecording = false; m_pMatProtector->unlock(); + return m_nRecordedFramesCount; }; -void videoNode::takeSnapshot(CSTR filename){ - +CSTR videoNode::takeSnapshot(void){ + return NULL; }; diff --git a/lib/data/media/videoNode.h b/lib/data/media/videoNode.h index f5ce28b3..ef6ac46f 100644 --- a/lib/data/media/videoNode.h +++ b/lib/data/media/videoNode.h @@ -9,6 +9,8 @@ #include "../../components/base/basicSettingsProvider.h" #include "../../components/base/basicVideoAnalyzer.h" +#define DEFAULT_FRAMES_COUNT 100 // 4 sec for 25 fps + class videoNode : public basicMediaDestination, public basicThread { private: config_setting_t *m_pSettings; @@ -16,19 +18,24 @@ class videoNode : public basicMediaDestination, public basicThread { bool m_bNewDataArival; mediaDestinationsCollection m_vDestinations; basicMutex *m_pMatProtector; + uint32_t m_nRecorderCounter; + uint32_t m_nRecordedFramesCount; + char m_sCurrentRecordName[MAX_PATH_LEN]; + bool m_bRecording; protected: void m_fThreadProc(); public: - explicit videoNode( config_setting_t *Settings = NULL ); - ~videoNode(); - void stopConveyour( void ); - void startRecord( void); - void stopRecord( void); - void takeSnapshot( CSTR filename ); - virtual void pushFrame( cv::Mat *Frame, void *src = NULL ); - bool attachVideoAnalyzer( basicVideoAnalyzer *BVA ); - void connect( basicMediaDestination *V); - void disconnect( basicMediaDestination *V); + explicit videoNode( config_setting_t *Settings = NULL ); + ~videoNode(); + void stopConveyour( void); + virtual int startRecord( uint32_t RecordFrames = DEFAULT_FRAMES_COUNT ); + virtual int stopRecord( void); // Forcingly Stop Record + CSTR takeSnapshot( void); + CSTR getCurrentRecordName(void) { return m_sCurrentRecordName; }; + virtual void pushFrame( cv::Mat *Frame, void *src = NULL ); + bool attachVideoAnalyzer( basicVideoAnalyzer *BVA ); + void connect( basicMediaDestination *V); + void disconnect( basicMediaDestination *V); }; typedef vector videoNodeCollection;