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

​
​
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 days ago
​
5 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __KERNEL_MODULE_EMBED_VIDEO_HEADER__
  2. #define __KERNEL_MODULE_EMBED_VIDEO_HEADER__
  3. #include "../../components/base/basicDualThread.h"
  4. #include "../../components/base/basicEventHeap.h"
  5. #include "../../components/base/basicMutex.h"
  6. #include "kernelModule.h"
  7. //#include "streamHandler.h"
  8. extern const char * m_pVideoScratchPath;
  9. #define EVE_WORK_WITH_MEDIAMTX
  10. #define EVE_OUTPUT_PICTURES 1
  11. #define EVE_OUTPUT_VIDEO 2
  12. #define EVE_OUTPUT_PIC_AND_VID 3
  13. #ifndef LRF_PROXY_ONLY
  14. // #include "../../data/media/video/videoAVLIBDestination.h"
  15. #include "../../components/base/basicMediaDestination.h"
  16. #include "../../components/base/basicMediaSource.h"
  17. #include "../../data/media/mtx/core/mediaPath.h"
  18. #include <deque>
  19. #include <map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <string>
  23. namespace mtx { class WebrtcServer; }
  24. class streamHandler;
  25. typedef map<uint32_t, streamHandler *> mapSTREAMHANDLERS;
  26. #ifndef EVE_WORK_WITH_MEDIAMTX
  27. class kernelModuleEmbedVideo;
  28. typedef struct S_WEB_CLIENT_CTX
  29. {
  30. int m_iSocketFID;
  31. int m_iEncoderPipe[2];
  32. bool bValid;
  33. bool initDone;
  34. bool headerIsSent;
  35. uint32_t mediaStreamId;
  36. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  37. bool bPreviewMode;
  38. #endif
  39. streamHandler * m_pHandler;
  40. kernelModuleEmbedVideo * ModulePtr;
  41. } WEB_CLIENT_CTX;
  42. typedef struct S_WEB_HANDLER
  43. {
  44. DWORD m_dwThreadID;
  45. #ifdef __linux__
  46. pthread_t m_hThreadHandle;
  47. #else
  48. HANDLE m_hThreadHandle;
  49. #endif
  50. WEB_CLIENT_CTX * CTX;
  51. } WEB_HANDLER;
  52. typedef vector<WEB_HANDLER *> vectorHANDLERS;
  53. #endif // !EVE_WORK_WITH_MEDIAMTX
  54. // typedef vector<STREAM_CTX*> vectorSTREAMS;
  55. typedef struct S_STREAM_CAP
  56. {
  57. uint32_t stream_id;
  58. uint32_t buffer_size;
  59. uint8_t picture_buffer;
  60. } STREAM_CAP;
  61. typedef struct S_VIDEO_EVT
  62. {
  63. uint32_t dbid;
  64. uint32_t frame;
  65. } VIDEO_EVT;
  66. #ifndef EVE_WORK_WITH_MEDIAMTX
  67. typedef vector<uchar> compressedFRAME;
  68. typedef map<uint32_t, compressedFRAME> mapSTREAMCAPS;
  69. #endif // !EVE_WORK_WITH_MEDIAMTX
  70. typedef map<uint32_t, cv::Mat> mapSTREAMMAPS;
  71. /*
  72. Live view and continuous recording (replaces the MediaMTX service)
  73. Every stream handler feeds a path "stream_<camera node id>" (the names MediaMTX had: the dashboard
  74. plays http://<host>:8889/stream_<node>/whep). live_mode selects what the path carries:
  75. - EVE_LIVE_PASSTHROUGH: the camera's H264 as the source receives it (no CPU, original quality)
  76. - EVE_LIVE_ENCODE: the processed frames of this module (time stamp, label, logo, event marks)
  77. encoded to H264 (streamLiveEncoder)
  78. The paths outlive the stream handlers (a stalled stream is rebuilt; the viewers reconnect to the same
  79. path). The WebRTC (WHEP) server and the recorder are the MediaMtx library (lib/data/media/mtx).
  80. */
  81. class kernelModuleEmbedVideo : public kernelModule, public mtx::PathProvider
  82. #ifndef EVE_WORK_WITH_MEDIAMTX
  83. , public basicDualThread
  84. #endif
  85. {
  86. private:
  87. basicEventHeap * m_pEventHeap;
  88. #ifndef EVE_WORK_WITH_MEDIAMTX
  89. static void * webClientProc(void * ptr);
  90. static void * viewClientProc(void * ptr);
  91. vectorHANDLERS m_vHandlersPool;
  92. mapSTREAMCAPS m_mapCAPTURES;
  93. vectorHANDLERS m_vViewHandlersPool;
  94. basicMutex m_mViewHandlersProtector;
  95. int m_nViewSocketDescriptor;
  96. basicMutex m_mWebHandlersProtector;
  97. bool registerForMediaStream(uint32_t id, WEB_CLIENT_CTX *& CTX);
  98. #endif // !EVE_WORK_WITH_MEDIAMTX
  99. int m_nSocketDescriptor;
  100. basicMutex m_mStreamHandlersProtector;
  101. basicMutex m_mPictureProtector;
  102. basicMutex m_mEncodedProtector;
  103. DBC_MODULE_EMBED_VIDEO_DATA m_sEmbedVideoData;
  104. mapSTREAMMAPS m_mapMATS;
  105. mapSTREAMHANDLERS m_mapHANDLERS;
  106. uint8_t * n_pNoSignalData;
  107. uint32_t m_nNoSignalSize;
  108. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  109. mapSTREAMCAPS m_mapPREVIEWS;
  110. uint8_t * n_pNoSignalSmallData;
  111. uint32_t m_nNoSignalSmallSize;
  112. #endif
  113. cv::Mat m_mLogoPic;
  114. cv::Mat m_mLogoMask;
  115. // Logo state computed in startService(); m_sEmbedVideoData is kept equal to the DB row
  116. // (validate() compares it with the DB and reloads the module on any difference)
  117. bool m_bLogoActive;
  118. int m_nLogoX;
  119. int m_nLogoY;
  120. char m_pBaseSavePath[PATH_MAX];
  121. bool saveStreamPicture(uint32_t id);
  122. bool saveStreamPicture(DBC_SNAPSHOT_DATA * ptr);
  123. bool generateSavePicPath(CSTR MD5, CSTR Output);
  124. // Live view (WebRTC) and recording
  125. mtx::ServerConf m_sLiveConf;
  126. std::unique_ptr<mtx::WebrtcServer> m_pWebrtc;
  127. time_t m_tLiveRetry; // next start attempt of the WebRTC server
  128. mutable std::mutex m_mLivePaths;
  129. std::map<std::string, std::shared_ptr<mtx::MediaPath>> m_mapLivePaths;
  130. void applyLiveSettings();
  131. bool startLive();
  132. void stopLive();
  133. // Alarm clips from the recorded segments (live_record), replacing the old encoder's clips:
  134. // recent segments are kept for a while, a segment around an alarm is handed to the kernel
  135. // (EVT_CAPTURE_DONE -> Reports/History) like the old encoder's clips.
  136. struct EveClipSegment
  137. {
  138. std::string file;
  139. int64_t startUs;
  140. int64_t endUs;
  141. int width;
  142. int height;
  143. bool handed;
  144. };
  145. struct EveClipAlarm
  146. {
  147. uint32_t eventID;
  148. int64_t timeUs;
  149. bool bound; // the segment with the alarm in it is handed over
  150. };
  151. struct EveClipStream
  152. {
  153. uint32_t streamID = 0;
  154. uint64_t sourceID = 0;
  155. uint8_t fps = 0;
  156. std::deque<EveClipSegment> segments; // oldest first
  157. std::vector<EveClipAlarm> alarms;
  158. };
  159. std::mutex m_mClips;
  160. std::map<std::string, EveClipStream> m_mapClips; // path name -> stream
  161. time_t m_tClipsScan; // last scan of the scratch folders for left-over segments
  162. bool liveClipsActive(void) const;
  163. void onLiveSegment(const mtx::SegmentInfo & info);
  164. void matchClipsLocked(EveClipStream & stream, int64_t nowUs);
  165. bool handOffClipLocked(EveClipStream & stream, EveClipSegment & seg);
  166. void sweepClips(void);
  167. #ifndef EVE_WORK_WITH_MEDIAMTX
  168. protected:
  169. /* From Basic Dual Thread */
  170. virtual void m_fThreadAProc(void);
  171. virtual void m_fThreadBProc(void);
  172. #endif // !EVE_WORK_WITH_MEDIAMTX
  173. public:
  174. explicit kernelModuleEmbedVideo(uint32_t moduleID);
  175. virtual ~kernelModuleEmbedVideo();
  176. /* From Kernel Module */
  177. virtual bool reloadSettings();
  178. virtual bool startService();
  179. virtual bool stopService(bool force = false);
  180. virtual bool dispatchEvent(EVENT_CONTAINER * Evt);
  181. virtual bool validate();
  182. /*
  183. //from Media Destination
  184. virtual bool pushFrame( AVFrame *Frame, void* src = NULL);
  185. //virtual void pushFrame( cv::Mat* Frame, void* src = NULL);
  186. virtual bool pushFrame( cv::Mat &Frame, void* src = NULL);
  187. // Module Specific Functionality
  188. */
  189. int getWidh(void);
  190. int getHeight(void);
  191. bool registerStream( uint32_t streamID, uint8_t flip_mode = 0 );
  192. bool unregisterStream( uint32_t streamID );
  193. uint8_t getMediaMode(void);
  194. bool isProcessingVideoStream();
  195. cv::Mat * getCurrentFrame( uint32_t streamID );
  196. // Live view: the mode, and the path of a camera node (created on first use; nullptr: live view off)
  197. uint8_t getLiveMode(void) const { return m_sEmbedVideoData.live_mode; }
  198. uint8_t getLiveFps(void) const { return m_sEmbedVideoData.live_fps; }
  199. uint16_t getLiveBitrate(void) const { return m_sEmbedVideoData.live_bitrate; }
  200. std::shared_ptr<mtx::MediaPath> livePath(uint32_t nodeID);
  201. // Alarm clips (live_record): the stream of a path, and an alarm on a stream
  202. void registerClipStream(const std::string & path, uint32_t streamID, uint64_t sourceID, uint8_t fps);
  203. void clipAlarm(uint32_t streamID, uint32_t eventID);
  204. // mtx::PathProvider (the WebRTC server looks up the path of a WHEP request)
  205. std::shared_ptr<mtx::MediaPath> findPath(const std::string & name) const override;
  206. friend class streamHandler;
  207. };
  208. #else //LRF_PROXY_ONLY
  209. class kernelModuleEmbedVideo : public kernelModule{
  210. public:
  211. explicit kernelModuleEmbedVideo(uint32_t moduleID);
  212. virtual ~kernelModuleEmbedVideo();
  213. };
  214. #endif //LRF_PROXY_ONLY
  215. #endif