//---------------------------------------------------------------------------- // Dr.Frucht Systems Ltd. // Copyright © 2000-2017. All Rights Reserved. // Version 2.0 6/11/2019 // Last modification: 6/11/2019 // PROJECT: // SUBSYSTEM: // FILE: rgooseIPCamera.h // AUTHOR: gagua // OVERVIEW // ~~~~~~~~ // Header file of rgooseIPCamera.h //---------------------------------------------------------------------------- // #ifndef __DEVICE_NODE_CAMERA_HEADER__ #define __DEVICE_NODE_CAMERA_HEADER__ #include "../../components/base/basicEventHeap.h" #include "../../components/base/basicMutex.h" #include "../../components/base/basicThread.h" #include "../../data/protocol/protocolPTZ.h" #include #include // #include "rgooseDBConnector.h" #include "deviceNode.h" // https://www.ebay.com/itm/Professional-Rolling-Makeup-Case-Bulb-Mirror-Cosmetic-Artist-Dressing-Train-Case/233264525474 // ffmpeg -i input.mp4 -af loudnorm=I=-23:LRA=7:tp=-2:print_format=json -f null - // ffmpeg -i input.mp4 -af loudnorm=I=-23:LRA=7:tp=-2:measured_I=-30:measured_LRA=1.1:measured_tp=-11 04:measured_thresh=-40.21:offset=-0.47 -ar 48k -y output.mp4 #define CAM_POST_INIT //#define NO_CAMERA_FUSION_ENGINE #define CAM_CTRL_PROTO_VISCA 1 #define CAM_CTRL_PROTO_PELCO 2 #define CAM_CTRL_PROTO_ONVIF 3 #define CAM_CTRL_PROTO_PSIA 4 #define CAM_CTRL_PROTO_T28181 5 #define CAM_CTRL_PROTO_BRCP 6 // Wait before EVT_CAMERA_ON_POSITION when the protocol cannot report the end of a move #define CAM_MOVE_FIXED_WAIT_MS 3000 #define UART_TYPE_HARDWARE 1 #define UART_TYPE_SOL 2 #define ARGUS_CAM_PCMD_MOVE_MASK 0x7F #define ARGUS_CAM_PCMD_CMD_MASK 0x80 #define ARGUS_CAM_PCMD_STOP 1 #define ARGUS_PRESET_TYPE_POSITION 1 #define ARGUS_PRESET_TYPE_HOME 2 #define ARGUS_PRESET_TYPE_IR_AUTO 3 #define ARGUS_PRESET_TYPE_IR_ON 4 #define ARGUS_PRESET_TYPE_IR_OFF 5 #define ARGUS_PRESET_TYPE_IR_WISHER 6 #define DEFAULT_SHINE_ON_TARGET_TO 60 // seconds typedef struct S_CAM_PTZ_CMD { uint8_t CMD; float PAN; float TILT; float ZOOM; } CAM_PTZ_CMD; typedef enum E_CAM_PTZ_CMD { PTZ_STOP = 0, PTZ_MOVE = 1, PTZ_ABSOLUTE = 2, PTZ_RELOAD_CAMERA = 3, PTZ_CAPTURE_ONLY = 4, PTZ_SET_PRESET = 5, PTZ_CALL_PRESET = 6 } PTZ_CMD; typedef std::queue CAM_PTZ_CMD_QUEUE; typedef enum E_CAM_WORKING_STATE { CAM_WS_INIT = 0, CAM_WS_POWER_INIT = 1, CAM_WS_WHITE_BALLANCE = 2, CAM_WS_BACKLIGHT = 3, CAM_WS_AUTO_FOCUS = 4, CAM_WS_INIT_GO_HOME = 5, CAM_WS_AUTOMATED_MOVE = 10, CAM_WS_STOP_MOVE = 11, CAM_WS_GO_ABS_POSITION = 12, CAM_WS_GO_HOMING_PRESET = 13, CAM_WS_GO_PRESET = 14, CAM_WS_GO_ABS_ROI = 15, CAM_WS_SET_PRESET = 16, CAM_WS_READY = 64, CAM_WS_BUSY = 128, CAM_WS_WAIT_ONPOS = 129, CAM_WS_END = 255 } CAM_WORK_STATE; /* PTZ control health of a camera (deviceNodeCamera::getPtzHealth), e.g. for the health status sent to the remote server. getPtzStatus() > 0 <=> state is PTZ_HEALTH_OK or PTZ_HEALTH_NONE. */ typedef enum E_PTZ_HEALTH_STATE { PTZ_HEALTH_NONE = 0, // camera has no PTZ control protocol (static camera): nothing that can fail PTZ_HEALTH_INIT = 1, // protocol not created yet (first initialisation) PTZ_HEALTH_OK = 2, // protocol created and working PTZ_HEALTH_FAILED = 3 // protocol lost or cannot be created; re-initialisation is retried (next_retry) } PTZ_HEALTH_STATE; typedef struct S_PTZ_HEALTH_INFO { PTZ_HEALTH_STATE state; uint32_t failures; // PTZ failures detected since start (lost protocol / failed init) uint32_t recoveries; // successful re-initialisations after a failure uint32_t failed_inits; // consecutive failed initialisations (reset on success) time_t since; // time of the last state change time_t last_failure; // time of the last failure (0 = never) time_t next_retry; // next re-initialisation attempt while FAILED (0 = none) char last_error[128]; } PTZ_HEALTH_INFO; #define CAM_PTZ_PROBE_INTERVAL_S 30 // idle liveness check of the PTZ protocol (protocolPTZ::probe) #define CAM_PTZ_RETRY_MIN_S 5 // first re-initialisation delay after a failure #define CAM_PTZ_RETRY_MAX_S 60 // delay doubles per failed attempt up to this typedef struct S_CAM_TASK { bool isDone; uint32_t source_evt_id; bool movemet_needed; CAM_PTZ_CMD * CMD; } CAM_TASK; typedef std::queue CAM_TASK_QUEUE; #define LOSE_INTERES_TIME_MS 2000 inline CAM_ROI * createCAM_ROI(void) { //CAM_ROI * tROI = (CAM_ROI *) malloc(sizeof(CAM_ROI)); CAM_ROI * tROI = new CAM_ROI(); if (tROI) { //memset(tROI, 0, sizeof(CAM_ROI)); tROI->targets.clear(); }; return tROI; } inline void utilizeROI(CAM_ROI *& ptr) { if (ptr) { if (ptr->targets.size()) { for (vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx != ptr->targets.end(); idx++) { free(*idx); } ptr->targets.clear(); }; //free(ptr); delete( ptr ); ptr = NULL; }; } class deviceNodeCamera : public deviceNode, public basicThread { private: basicPosition m_oPosition; time_t m_tTimeTSeconds; CAM_ROI * m_pCurrentROI; float m_fMaxRange; float m_fMinRange; float m_fMaxFL; float m_fMinFL; float m_fR2FL; // Range to Focal Length float m_fZoomCorrection; uint16_t m_nRealFrameWidth; uint16_t m_nRealFrameHeight; float m_fMinHFov; float m_fMaxHFov; float m_fMinVFov; float m_fMaxVFov; protocolPTZ * m_pProtocol; // PTZ health (written by the camera thread, read by others through getPtzHealth / getPtzStatus) PTZ_HEALTH_INFO m_sPtzHealth; basicMutex m_mPtzHealth; time_t m_tPtzLastProbe; void ptzHealthSet(PTZ_HEALTH_STATE state, const char * reason = NULL); bool ptzReinitDue(void); void ptzCheck(uint8_t tState); void ptzLost(const char * reason); void ptzCommandFailed(const char * command); // basicTransport * m_pTransport; CAM_TASK_QUEUE m_qCAM_TASK; basicMutex m_mQPROTECTOR; uint32_t m_nRelativeMoveCounter; uint32_t m_nHomingCounter; uint8_t m_bRelativeMoveMatrix; uint8_t m_bCurrentPresetCol; uint8_t m_bCurrentPresetRow; float m_fSpeedRise; float m_fRM_Pan; float m_fRM_Tilt; float m_fRM_Zoom; float m_fCP_Pan; float m_fCP_Tilt; float m_fCP_Zoom; bool m_bSkeepPositioningWait; bool m_bNoMoveDoneReport; // protocol cannot report the end of a move -> fixed wait before EVT_CAMERA_ON_POSITION bool m_bNoAbsPositionning; bool m_bCameraIsStatic; // IR functionality basicMutex m_mIRProtector; bool m_bShineOnTarget; bool m_bIRIsOn; uint8_t m_bIRTO; // Default 60 seconds (1 video slice) // bool m_bCameraOnHomePosition; /* Methotds Sutable to Control Camera */ bool relativeMove(float PanSPD, float TiltSPD, float ZoomSPD); bool relativeStop(); bool absoluteMove(uint32_t RefID, float PanPOS, float TiltPOS, float ZoomPOS); bool absoluteMove(uint32_t RefID, C3D_ORIENT_ONVIF O); void updateOpticalParameters(uint16_t frame_width, uint16_t frame_heigth); void translateROI2PRESET(float P, float T, float Z, uint8_t & col, uint8_t & row); protected: DBC_CAM_DATA m_sCamData; MEDIA_SOURCE_DESC m_sMediaSource; basicEventHeap * m_pBEH; // bool m_bSourceConnected; // uint32_t m_nActiveStreamID; // bool inTheFOV( float lat, float lon, float alt ); #ifndef NO_CAMERA_FUSION_ENGINE /* tracking Fusion methods */ vCAM_ROIS m_vROIS; basicMutex m_mROIsProtector; bool fusion_consumeData(uint32_t db_id, C3D_SPH_DEGREES tOR); bool fusion_validateROI(uint64_t cts, CAM_ROI * ptr); bool fusion_compare(CAM_ROI * ptr, C3D_SPH_DEGREES tp); void fusion_validate(void); CAM_ROI * fusion_fetchROI(void); DBC_SNAPSHOT_DATA * fusion_translate_ROI(CAM_ROI *(&ptr)); #else #endif /* presets Fusion methods */ CAM_PRESETS_SET m_sCamPresets; virtual bool load_presets(void); CSTR getPresetToken(uint8_t CCOL, uint8_t CROW); CSTR getPresetToken(uint8_t specialRole); bool testFOV(C3D_WGS84_LOC); // Global Coords bool testFOV(C3D_DECART); // System Coords bool testFOV(C3D_SPH_RADIANS); // Local Coords bool testFOV(C3D_SPH_DEGREES); // Local Coords // bool lookAtWGS84( uint32_t TargetRefID, float lat, float lon, float alt ); bool lookAt(uint32_t TargetRefID, C3D_WGS84_LOC L); // GLobal bool lookAt(uint32_t TargetRefID, C3D_DECART L); // System bool lookAt(uint32_t TargetRefID, C3D_SPH_RADIANS L); // Local bool lookAt(uint32_t TargetRefID, C3D_SPH_DEGREES L); // Local bool makeSnap(uint32_t TargetRefID); // void setBusy( bool Status ); bool connectToStream(uint32_t id); bool disconnectFromStream(void); CAM_TASK * fetchCamTask(void); bool pushCamTask(CAM_TASK *); virtual void m_fThreadProc(void); public: explicit deviceNodeCamera(uint32_t NodeID); explicit deviceNodeCamera(DBC_NODE_DATA * Data); virtual ~deviceNodeCamera(); bool create(); virtual bool validate(); virtual bool validateStream(void); bool setNoPositionWait(bool FL = true); virtual uint8_t getCamStatus() { return (uint8_t) m_sCamData.online; } uint32_t getCamStream() { return m_sCamData.stream_id; } virtual uint8_t getFlipMode() { return m_sCamData.flip_mode; } virtual uint8_t getPtzStatus(); void getPtzHealth(PTZ_HEALTH_INFO * info); virtual uint8_t getStreamStatus(); virtual float getSystemLocationX(){ return m_sCamData.x; }; virtual float getSystemLocationY(){ return m_sCamData.y; }; virtual float getSystemLocationZ(){ return m_sCamData.z; }; virtual float getMaxRange(){ return m_fMaxRange; }; }; typedef std::vector vectorNodeCameras; #endif