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.

386 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __STREAM_HANDLER_EMBED_VIDEO_HEADER__
  2. #define __STREAM_HANDLER_EMBED_VIDEO_HEADER__
  3. #include "kernelModuleEmbedVideo.h"
  4. #ifndef LRF_PROXY_ONLY
  5. #include "../../components/base/basicDualThread.h"
  6. #include "../../components/base/basicEventHeap.h"
  7. #include "../../components/base/basicMutex.h"
  8. #include "kernelModule.h"
  9. #define EVE_SEQUENTAL_OUTPUT
  10. #ifndef EVE_SEQUENTAL_OUTPUT
  11. //#define EVE_ENABLE_EASE_OUTPUT_FPS
  12. #endif
  13. // #include "../../data/media/video/videoAVLIBDestination.h"
  14. #include "../../components/base/basicMediaDestination.h"
  15. #include "../../components/base/basicMediaSource.h"
  16. #ifndef DB_SHMEM_POINT
  17. #define DB_SHMEM_POINT "/dev/shm/argus"
  18. #endif
  19. #define VIDEO_SCRATCHPAD_PATH (DB_SHMEM_POINT "/video_scratch")
  20. //Predefine Classess
  21. class kernelModuleEmbedVideo;
  22. // A stream is restarted only after this many validations in a row (module factory: every ~5 s) without a
  23. // new frame. One gap of ~5 s (a camera on a mobile link) restarted the stream and cut the recording.
  24. #ifndef STREAM_STALL_CHECKS
  25. #define STREAM_STALL_CHECKS 3
  26. #endif
  27. class streamHandler;
  28. #define FILESTREAMBUFFERSZ 1048576
  29. #ifndef MEKOROT_PROJECT__
  30. #define EVE_ENABLE_VIDEO
  31. #endif
  32. #define USE_SCALING_IN
  33. // #define USE_CV_DENOISE
  34. // #define USE_CV_TONEMAP
  35. #ifdef EVE_ENABLE_VIDEO
  36. #define VIDEO_OUTPUT_FPS 15
  37. #ifndef EVE_SEQUENTAL_OUTPUT
  38. // In case of sequental video files - no need two encoders
  39. #define SEPARATED_VIDEO_ENCODERS
  40. #endif
  41. // #define EVE_DONT_DELETE_SEGMENTS
  42. /* Video CODECS */
  43. #define VC_VPX 0
  44. #define VC_X264 1
  45. #define VC_MP4 2
  46. #define VC_MJPEG 3
  47. #define VC_FLV 4
  48. /* Video FORMATS */
  49. #define VF_WEBM 0
  50. #define VF_MP4 1
  51. #define VF_MP3 2
  52. #define VF_AAC 3
  53. #define VF_OGG 4
  54. #define VF_MJPEG 5
  55. #define VF_FLV 6
  56. /* Encoder PROFILES */
  57. #define VP_ULTRAFAST 0
  58. #define VP_VERYFAST 1
  59. #define VP_FAST 2
  60. #define VP_MEDIUM 3
  61. #define VP_SLOW 4
  62. #define VP_VERYSLOW 5
  63. #define VP_ULTRASLOW 6
  64. #define VO_MJPEG 1
  65. #define VO_MPEG4 2
  66. #define VO_H264 3
  67. #define VO_H265 4
  68. #define VO_FLV 5
  69. #define VO_DIRECT_MJPG 6
  70. #ifndef EVE_WORK_WITH_MEDIAMTX
  71. #define DIRECT_MJPEG_COMPOSING
  72. #endif
  73. #ifdef DIRECT_MJPEG_COMPOSING
  74. #define VIDEO_OUTPUT VO_DIRECT_MJPG
  75. #else
  76. #define OPENCV_VIDEO_OUTPUT
  77. //#define VIDEO_OUTPUT VO_DIRECT_MJPG
  78. #ifndef OPENCV_VIDEO_OUTPUT
  79. // #define VIDEO_OUTPUT VO_MJPEG
  80. #define VIDEO_OUTPUT VO_MPEG4
  81. // #define VIDEO_OUTPUT VO_H264
  82. // #define VIDEO_OUTPUT VO_H265
  83. // #define VIDEO_OUTPUT VO_FLV
  84. // #define VIDEO_OUTPUT_WIDTH 640 //1920
  85. // #define VIDEO_OUTPUT_HEIGHT 480 //1080
  86. // #define VIDEO_OUTPUT_FPS 10
  87. // #define VIDEO_OUTPUT_GOP 15
  88. #else // OPENCV_VIDEO_OUTPUT
  89. //#define VIDEO_OUTPUT VO_MJPEG // VO_CV_MP4
  90. #define VIDEO_OUTPUT VO_CV_MP4
  91. #endif
  92. #endif // DIRECT_MJPEG_COMPOSING
  93. #if (LIBAVCODEC_VERSION_MAJOR < 54)
  94. #define VIDEO_OUTPUT_CODEC CODEC_ID_MJPEG
  95. #define VIDEO_OUTPUT_PIX_FMT PIX_FMT_YUVJ422P
  96. #else
  97. #if VIDEO_OUTPUT == VO_MJPEG
  98. #define VIDEO_OUTPUT_CODEC AV_CODEC_ID_MJPEG
  99. #define VIDEO_OUTPUT_PIX_FMT AV_PIX_FMT_YUV420P // AV_PIX_FMT_YUVJ422P
  100. #define VIDEO_OUTPUT_CONTAINER_LONG "mjpeg"
  101. #define VIDEO_OUTPUT_BITRATE 4000000
  102. #define VIDEO_OUTPUT_CONTAINER "mjpeg"
  103. #elif VIDEO_OUTPUT == VO_MPEG4
  104. #define VIDEO_OUTPUT_CODEC AV_CODEC_ID_MPEG4
  105. #define VIDEO_OUTPUT_PIX_FMT AV_PIX_FMT_YUV420P
  106. #define VIDEO_OUTPUT_CONTAINER_LONG "mp4"
  107. #define VIDEO_OUTPUT_BITRATE 4000000
  108. #define VIDEO_OUTPUT_CONTAINER "mp4"
  109. #elif VIDEO_OUTPUT == VO_H264
  110. #define VIDEO_OUTPUT_CODEC AV_CODEC_ID_H264
  111. #define VIDEO_OUTPUT_PIX_FMT AV_PIX_FMT_YUV420P
  112. #define VIDEO_OUTPUT_BITRATE 4000000
  113. #define VIDEO_OUTPUT_CONTAINER_LONG "mp4"
  114. #define VIDEO_OUTPUT_CONTAINER "mp4"
  115. #elif VIDEO_OUTPUT == VO_H265
  116. #define VIDEO_OUTPUT_CODEC AV_CODEC_ID_H265
  117. #define VIDEO_OUTPUT_PIX_FMT AV_PIX_FMT_YUV420P
  118. #define VIDEO_OUTPUT_BITRATE 4000000
  119. #define VIDEO_OUTPUT_CONTAINER_LONG "mp4"
  120. #define VIDEO_OUTPUT_CONTAINER "mp4"
  121. #elif VIDEO_OUTPUT == VO_FLV
  122. #define VIDEO_OUTPUT_CODEC AV_CODEC_ID_FLV1
  123. #define VIDEO_OUTPUT_PIX_FMT AV_PIX_FMT_YUV420P
  124. #define VIDEO_OUTPUT_CONTAINER_LONG "flv"
  125. #define VIDEO_OUTPUT_BITRATE 4000000
  126. #define VIDEO_OUTPUT_CONTAINER "flv"
  127. #elif VIDEO_OUTPUT == VO_CV_MP4
  128. #define VIDEO_OUTPUT_PIX_FMT AV_PIX_FMT_YUV420P
  129. #define VIDEO_OUTPUT_BITRATE 4000000
  130. #define VIDEO_OUTPUT_CONTAINER_LONG "mp4"
  131. #define VIDEO_OUTPUT_CONTAINER "mp4"
  132. #elif VIDEO_OUTPUT == VO_DIRECT_MJPG
  133. #define VIDEO_OUTPUT_CONTAINER_LONG "mjpeg"
  134. #define VIDEO_OUTPUT_CONTAINER "mjpeg"
  135. #endif
  136. #endif
  137. #endif // EVE_ENABLE_VIDEO
  138. typedef struct S_buffer_data
  139. {
  140. uint8_t * buf;
  141. int size;
  142. uint8_t * ptr;
  143. size_t room; ///< size left in the buffer
  144. } buffer_data;
  145. #define NR_COLORS 4
  146. #define AVIO_BUFFER_SIZE 4096
  147. #define BD_BUFFER_SIZE 1024
  148. #define PRE_ALARM_TIME_MILSECONDS 5000 // 5 sec
  149. #define POST_ALARM_TIME_MILSECONDS 5000 // 5 sec
  150. #define ENCODING_SEGMENT_MILSECONDS 50000 // 50 sec
  151. #define ENCODING_SEGMENT_LIMIT_MILSECONDS 60000 // 1 minute
  152. // #define ENCODING_SEGMENT_LIMIT_MILSECONDS 900000 // 15 minutes
  153. // #define ENCODER_DISABLED 0
  154. // #define ENCODER_ENABLED 1
  155. #define EVE_ENABLE_DROP_OUTPUT_FPS
  156. #ifdef EVE_ENABLE_VIDEO
  157. #ifdef DIRECT_MJPEG_COMPOSING
  158. typedef struct S_ENCODING_BUFFER
  159. {
  160. uint8_t * data;
  161. size_t size;
  162. } ENCODING_BUFFER;
  163. #else //! DIRECT_MJPEG_COMPOSING
  164. #ifdef OPENCV_VIDEO_OUTPUT
  165. typedef struct S_ENCODING_BUFFER
  166. {
  167. int cols;
  168. int rows;
  169. uint8_t * data;
  170. size_t size;
  171. } ENCODING_BUFFER;
  172. #else //! OPENCV_VIDEO_OUTPUT
  173. typedef struct S_ENCODING_BUFFER
  174. {
  175. int linesize[2];
  176. int rows;
  177. uint8_t * data[2];
  178. size_t size;
  179. } ENCODING_BUFFER;
  180. #endif //! OPENCV_VIDEO_OUTPUT
  181. #endif //! DIRECT_MJPEG_COMPOSING
  182. typedef std::queue<ENCODING_BUFFER *> Q_ENCODING_BUFFER;
  183. #endif // EVE_ENABLE_VIDEO
  184. class streamHandler :
  185. public basicMediaDestination
  186. #ifdef EVE_ENABLE_VIDEO
  187. , public basicThread
  188. #endif
  189. {
  190. private:
  191. basicLogCollection * m_pLog;
  192. basicMediaSource * m_pSource;
  193. MEDIA_SOURCE_DESC m_sSourceDesc;
  194. char m_cSourceType;
  195. DBC_MEDIA_STREAM_DATA m_sStreamData;
  196. basicMutex m_mProtector;
  197. kernelModuleEmbedVideo * m_pMaster;
  198. uint8_t m_bEvtMarkerCounter;
  199. uint8_t m_bCamMoveCounter;
  200. dataBinder * m_pDBC;
  201. MS_WORK_MODE m_eWM;
  202. basicMutex m_mEvtIDS_Protector;
  203. #ifdef EVE_ENABLE_VIDEO
  204. #ifdef EVE_SEQUENTAL_OUTPUT
  205. basicMutex m_mEventBindingBlocker;
  206. #endif
  207. uint32_t m_nPreAlarmFrames;
  208. uint32_t m_nPostAlarmFrames;
  209. uint32_t m_nSegmentLimitFrames;
  210. uint32_t m_nHardLimitFrames;
  211. basicMutex m_mFrameCounterProtector;
  212. uint64_t m_nGlobalFrameCounter;
  213. uint64_t m_nValidatorFrameCounter;
  214. uint8_t m_nStallChecks; // validate() calls in a row without a new frame
  215. time_t m_tLastFrameProgress; // last validate() that saw new frames
  216. #ifndef EVE_SEQUENTAL_OUTPUT
  217. uint32_t m_nSegAFrameCounter;
  218. uint32_t m_nSegBFrameCounter;
  219. uint32_t m_nSegAShiftFrames;
  220. uint32_t m_nSegBShiftFrames;
  221. uint32_t m_nSegASkippedFrames;
  222. uint32_t m_nSegBSkippedFrames;
  223. uint8_t m_bActiveEncoderStream;
  224. bool m_bSegAActive;
  225. bool m_bSegADelete;
  226. bool m_bSegBActive;
  227. bool m_bSegBDelete;
  228. VIDEO_SEGMENT_INFO m_sStreamSegmentInfo[2];
  229. #else
  230. uint32_t m_nSegFrameCounter;
  231. uint32_t m_nSegShiftFrames;
  232. uint32_t m_nSegSkippedFrames;
  233. bool m_bSegActive;
  234. bool m_bSegDelete;
  235. bool m_bNSegDelete;
  236. VIDEO_SEGMENT_INFO m_sStreamSegmentInfo;
  237. #endif
  238. uint8_t m_bFlipMode;
  239. basicMutex m_mCurrentOperationProtector;
  240. uint8_t m_bCOP;
  241. #ifdef DIRECT_MJPEG_COMPOSING
  242. #ifndef EVE_SEQUENTAL_OUTPUT
  243. FILE * m_pOutputFD[2];
  244. #else
  245. FILE * m_pOutputFD;
  246. #endif
  247. #else // !DIRECT_MJPEG_COMPOSING
  248. #ifdef OPENCV_VIDEO_OUTPUT
  249. #ifndef EVE_SEQUENTAL_OUTPUT
  250. cv::VideoWriter * m_pEncoderA;
  251. cv::VideoWriter * m_pEncoderB;
  252. #else
  253. cv::VideoWriter * m_pEncoder;
  254. #endif
  255. #else //! OPENCV_VIDEO_OUTPUT
  256. // char m_pVideoScratchPath[PATH_MAX];
  257. #ifdef SEPARATED_VIDEO_ENCODERS
  258. // LibAV encoding video stuff
  259. #ifndef EVE_SEQUENTAL_OUTPUT
  260. // Stream A
  261. AVCodec * m_pVideoACodec;
  262. AVCodecContext * m_pCodecACtx;
  263. AVFrame * m_pAVAFrame;
  264. uint8_t * m_pOutABuffer;
  265. // Stream B
  266. AVCodec * m_pVideoBCodec;
  267. AVCodecContext * m_pCodecBCtx;
  268. AVFrame * m_pAVBFrame;
  269. uint8_t * m_pOutBBuffer;
  270. #else
  271. AVCodec * m_pVideoCodec;
  272. AVCodecContext * m_pCodecCtx;
  273. AVFrame * m_pAVFrame;
  274. uint8_t * m_pOutBuffer;
  275. #endif
  276. #else //! SEPARATED_VIDEO_ENCODERS
  277. // LibAV encoding video stuff
  278. AVCodec * m_pVideoCodec;
  279. AVCodecContext * m_pCodecCtx;
  280. AVFrame * m_pAVFrame;
  281. uint8_t * m_pOutBuffer;
  282. #endif //! SEPARATED_VIDEO_ENCODERS
  283. // Common Pix Converter ( for both streams )
  284. SwsContext * m_pConvertCtx;
  285. #endif //! OPENCV_VIDEO_OUTPUT
  286. // char m_cSegNameA[128];
  287. // char m_cSegMD5A[33];
  288. // char m_cSegNameB[128];
  289. // char m_cSegMD5B[33];
  290. AVOutputFormat * m_pFormat;
  291. #ifdef SEPARATED_VIDEO_ENCODERS
  292. // File Context 2 units
  293. AVFormatContext * m_pFormatCtxA;
  294. AVFormatContext * m_pFormatCtxB;
  295. AVStream * m_pVideoStrA;
  296. AVStream * m_pVideoStrB;
  297. #else
  298. // File Context 1 units
  299. AVFormatContext * m_pFormatCtx;
  300. AVStream * m_pVideoStr;
  301. #endif
  302. // common output options
  303. AVDictionary * m_pFormatOpts;
  304. #endif //! DIRECT_MJPEG_COMPOSING
  305. basicMutex m_mEncodingQueueProtector;
  306. Q_ENCODING_BUFFER m_qEncodingQueue;
  307. #ifndef EVE_SEQUENTAL_OUTPUT
  308. bool openSegmentA(void);
  309. void closeSegmentA(void);
  310. bool openSegmentB(void);
  311. void closeSegmentB(void);
  312. void swapActiveStreams(void);
  313. #endif
  314. void closeAll();
  315. bool initVideoOutput();
  316. protected:
  317. void m_fThreadProc(void);
  318. #endif //! EVE_ENABLE_VIDEO
  319. public:
  320. explicit streamHandler(kernelModuleEmbedVideo * ptr, MS_WORK_MODE M = MS_MODE_DECODE_FRAMES, uint8_t flip_mode = 0);
  321. virtual ~streamHandler();
  322. bool create(dataBinder * ptr, uint32_t stremID);
  323. /* from Media Destination */
  324. virtual bool pushFrame(AVFrame * Frame, void * src = NULL);
  325. // virtual void pushFrame( cv::Mat* Frame, void* src = NULL);
  326. virtual bool pushFrame(cv::Mat & Frame, void * src = NULL);
  327. bool triggerEvent(uint32_t eid, uint32_t & dbid, uint32_t & frame);
  328. bool triggerCamMove(void);
  329. bool validate(void);
  330. bool preservingCurrentRecording(){
  331. #ifdef EVE_SEQUENTAL_OUTPUT
  332. return !m_bSegDelete;
  333. #else
  334. return !( m_bSegADelete || m_bSegBDelete );
  335. #endif
  336. };
  337. };
  338. #endif // !LRF_PROXY_ONLY
  339. #endif