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.
 
 
 
 
 
 

236 lines
8.4 KiB

#ifndef __KERNEL_MODULE_EMBED_VIDEO_HEADER__
#define __KERNEL_MODULE_EMBED_VIDEO_HEADER__
#include "../../components/base/basicDualThread.h"
#include "../../components/base/basicEventHeap.h"
#include "../../components/base/basicMutex.h"
#include "kernelModule.h"
//#include "streamHandler.h"
extern const char * m_pVideoScratchPath;
#define EVE_WORK_WITH_MEDIAMTX
#define EVE_OUTPUT_PICTURES 1
#define EVE_OUTPUT_VIDEO 2
#define EVE_OUTPUT_PIC_AND_VID 3
#ifndef LRF_PROXY_ONLY
// #include "../../data/media/video/videoAVLIBDestination.h"
#include "../../components/base/basicMediaDestination.h"
#include "../../components/base/basicMediaSource.h"
#include "../../data/media/mtx/core/mediaPath.h"
#include <deque>
#include <map>
#include <memory>
#include <mutex>
#include <string>
namespace mtx { class WebrtcServer; }
class streamHandler;
typedef map<uint32_t, streamHandler *> mapSTREAMHANDLERS;
#ifndef EVE_WORK_WITH_MEDIAMTX
class kernelModuleEmbedVideo;
typedef struct S_WEB_CLIENT_CTX
{
int m_iSocketFID;
int m_iEncoderPipe[2];
bool bValid;
bool initDone;
bool headerIsSent;
uint32_t mediaStreamId;
#ifdef HAVE_SMAL_PREVIEW_STREAMS
bool bPreviewMode;
#endif
streamHandler * m_pHandler;
kernelModuleEmbedVideo * ModulePtr;
} WEB_CLIENT_CTX;
typedef struct S_WEB_HANDLER
{
DWORD m_dwThreadID;
#ifdef __linux__
pthread_t m_hThreadHandle;
#else
HANDLE m_hThreadHandle;
#endif
WEB_CLIENT_CTX * CTX;
} WEB_HANDLER;
typedef vector<WEB_HANDLER *> vectorHANDLERS;
#endif // !EVE_WORK_WITH_MEDIAMTX
// typedef vector<STREAM_CTX*> vectorSTREAMS;
typedef struct S_STREAM_CAP
{
uint32_t stream_id;
uint32_t buffer_size;
uint8_t picture_buffer;
} STREAM_CAP;
typedef struct S_VIDEO_EVT
{
uint32_t dbid;
uint32_t frame;
} VIDEO_EVT;
#ifndef EVE_WORK_WITH_MEDIAMTX
typedef vector<uchar> compressedFRAME;
typedef map<uint32_t, compressedFRAME> mapSTREAMCAPS;
#endif // !EVE_WORK_WITH_MEDIAMTX
typedef map<uint32_t, cv::Mat> mapSTREAMMAPS;
/*
Live view and continuous recording (replaces the MediaMTX service)
Every stream handler feeds a path "stream_<camera node id>" (the names MediaMTX had: the dashboard
plays http://<host>:8889/stream_<node>/whep). live_mode selects what the path carries:
- EVE_LIVE_PASSTHROUGH: the camera's H264 as the source receives it (no CPU, original quality)
- EVE_LIVE_ENCODE: the processed frames of this module (time stamp, label, logo, event marks)
encoded to H264 (streamLiveEncoder)
The paths outlive the stream handlers (a stalled stream is rebuilt; the viewers reconnect to the same
path). The WebRTC (WHEP) server and the recorder are the MediaMtx library (lib/data/media/mtx).
*/
class kernelModuleEmbedVideo : public kernelModule, public mtx::PathProvider
#ifndef EVE_WORK_WITH_MEDIAMTX
, public basicDualThread
#endif
{
private:
basicEventHeap * m_pEventHeap;
#ifndef EVE_WORK_WITH_MEDIAMTX
static void * webClientProc(void * ptr);
static void * viewClientProc(void * ptr);
vectorHANDLERS m_vHandlersPool;
mapSTREAMCAPS m_mapCAPTURES;
vectorHANDLERS m_vViewHandlersPool;
basicMutex m_mViewHandlersProtector;
int m_nViewSocketDescriptor;
basicMutex m_mWebHandlersProtector;
bool registerForMediaStream(uint32_t id, WEB_CLIENT_CTX *& CTX);
#endif // !EVE_WORK_WITH_MEDIAMTX
int m_nSocketDescriptor;
basicMutex m_mStreamHandlersProtector;
basicMutex m_mPictureProtector;
basicMutex m_mEncodedProtector;
DBC_MODULE_EMBED_VIDEO_DATA m_sEmbedVideoData;
mapSTREAMMAPS m_mapMATS;
mapSTREAMHANDLERS m_mapHANDLERS;
uint8_t * n_pNoSignalData;
uint32_t m_nNoSignalSize;
#ifdef HAVE_SMAL_PREVIEW_STREAMS
mapSTREAMCAPS m_mapPREVIEWS;
uint8_t * n_pNoSignalSmallData;
uint32_t m_nNoSignalSmallSize;
#endif
cv::Mat m_mLogoPic;
cv::Mat m_mLogoMask;
// Logo state computed in startService(); m_sEmbedVideoData is kept equal to the DB row
// (validate() compares it with the DB and reloads the module on any difference)
bool m_bLogoActive;
int m_nLogoX;
int m_nLogoY;
char m_pBaseSavePath[PATH_MAX];
bool saveStreamPicture(uint32_t id);
bool saveStreamPicture(DBC_SNAPSHOT_DATA * ptr);
bool generateSavePicPath(CSTR MD5, CSTR Output);
// Live view (WebRTC) and recording
mtx::ServerConf m_sLiveConf;
std::unique_ptr<mtx::WebrtcServer> m_pWebrtc;
time_t m_tLiveRetry; // next start attempt of the WebRTC server
mutable std::mutex m_mLivePaths;
std::map<std::string, std::shared_ptr<mtx::MediaPath>> m_mapLivePaths;
void applyLiveSettings();
bool startLive();
void stopLive();
// Alarm clips from the recorded segments (live_record), replacing the old encoder's clips:
// recent segments are kept for a while, a segment around an alarm is handed to the kernel
// (EVT_CAPTURE_DONE -> Reports/History) like the old encoder's clips.
struct EveClipSegment
{
std::string file;
int64_t startUs;
int64_t endUs;
int width;
int height;
bool handed;
};
struct EveClipAlarm
{
uint32_t eventID;
int64_t timeUs;
bool bound; // the segment with the alarm in it is handed over
};
struct EveClipStream
{
uint32_t streamID = 0;
uint64_t sourceID = 0;
uint8_t fps = 0;
std::deque<EveClipSegment> segments; // oldest first
std::vector<EveClipAlarm> alarms;
};
std::mutex m_mClips;
std::map<std::string, EveClipStream> m_mapClips; // path name -> stream
time_t m_tClipsScan; // last scan of the scratch folders for left-over segments
bool liveClipsActive(void) const;
void onLiveSegment(const mtx::SegmentInfo & info);
void matchClipsLocked(EveClipStream & stream, int64_t nowUs);
bool handOffClipLocked(EveClipStream & stream, EveClipSegment & seg);
void sweepClips(void);
#ifndef EVE_WORK_WITH_MEDIAMTX
protected:
/* From Basic Dual Thread */
virtual void m_fThreadAProc(void);
virtual void m_fThreadBProc(void);
#endif // !EVE_WORK_WITH_MEDIAMTX
public:
explicit kernelModuleEmbedVideo(uint32_t moduleID);
virtual ~kernelModuleEmbedVideo();
/* From Kernel Module */
virtual bool reloadSettings();
virtual bool startService();
virtual bool stopService(bool force = false);
virtual bool dispatchEvent(EVENT_CONTAINER * Evt);
virtual bool validate();
/*
//from Media Destination
virtual bool pushFrame( AVFrame *Frame, void* src = NULL);
//virtual void pushFrame( cv::Mat* Frame, void* src = NULL);
virtual bool pushFrame( cv::Mat &Frame, void* src = NULL);
// Module Specific Functionality
*/
int getWidh(void);
int getHeight(void);
bool registerStream( uint32_t streamID, uint8_t flip_mode = 0 );
bool unregisterStream( uint32_t streamID );
uint8_t getMediaMode(void);
bool isProcessingVideoStream();
cv::Mat * getCurrentFrame( uint32_t streamID );
// Live view: the mode, and the path of a camera node (created on first use; nullptr: live view off)
uint8_t getLiveMode(void) const { return m_sEmbedVideoData.live_mode; }
uint8_t getLiveFps(void) const { return m_sEmbedVideoData.live_fps; }
uint16_t getLiveBitrate(void) const { return m_sEmbedVideoData.live_bitrate; }
std::shared_ptr<mtx::MediaPath> livePath(uint32_t nodeID);
// Alarm clips (live_record): the stream of a path, and an alarm on a stream
void registerClipStream(const std::string & path, uint32_t streamID, uint64_t sourceID, uint8_t fps);
void clipAlarm(uint32_t streamID, uint32_t eventID);
// mtx::PathProvider (the WebRTC server looks up the path of a WHEP request)
std::shared_ptr<mtx::MediaPath> findPath(const std::string & name) const override;
friend class streamHandler;
};
#else //LRF_PROXY_ONLY
class kernelModuleEmbedVideo : public kernelModule{
public:
explicit kernelModuleEmbedVideo(uint32_t moduleID);
virtual ~kernelModuleEmbedVideo();
};
#endif //LRF_PROXY_ONLY
#endif