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.

2841 lines
69 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
2 years ago
2 years ago
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
2 years ago
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
2 years ago
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
2 years ago
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
2 years ago
​
​
2 years ago
2 years ago
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
2 years ago
2 years ago
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
2 years ago
2 years ago
2 years ago
​
2 years ago
​
2 years ago
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
5 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "kernelModuleEmbedVideo.h"
  2. #include "streamHandler.h"
  3. //#define DEBUG_EXTRA
  4. #define LOG(X) kernelModule::m_pLog->Log("MOD_EVIDEO", this, X)
  5. #ifdef DEBUG
  6. #define LOG_ERROR(X) \
  7. { \
  8. char _tBfrErrMSG2w34[2048]; \
  9. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  10. kernelModule::m_pLog->Log("MOD_EVIDEO", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  11. }
  12. #else
  13. #define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_EVIDEO", this, _LOG_ERROR, X);
  14. #endif
  15. #ifndef LRF_PROXY_ONLY
  16. // #include "rgooseHardwareResourceNetworkProvider.h"
  17. // #include "mojoKernel.h"
  18. #include "../alg/generic/base64.h"
  19. #include "../data/media/video/videoAVLIBSource.h"
  20. #include "../data/media/video/videoLive555AVLibSource.h"
  21. #include "../../data/media/mtx/servers/webrtc/webrtcServer.h"
  22. #include <arpa/inet.h>
  23. #include <ctype.h>
  24. #include <dirent.h>
  25. #include <sys/stat.h>
  26. #include <err.h>
  27. #include <errno.h>
  28. #include <getopt.h>
  29. #include <ifaddrs.h>
  30. #include <netdb.h>
  31. #include <netinet/ip.h>
  32. #include <netinet/udp.h>
  33. #include <openssl/sha.h>
  34. #include <paths.h>
  35. #include <poll.h>
  36. #include <signal.h>
  37. #include <stdint.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <sys/param.h> /* MIN() */
  42. #include <sys/socket.h>
  43. #include <time.h>
  44. #include <unistd.h>
  45. // #define DEBUG
  46. // #undef DEBUG_EXTRA
  47. // #define DEBUG_EXTRA
  48. const char * m_pVideoScratchPath = VIDEO_SCRATCHPAD_PATH;
  49. #ifndef STORAGE_DIR_MODE
  50. #define STORAGE_DIR_MODE 0x1FF
  51. #endif
  52. #ifndef STORAGE_FILE_MODE
  53. #define STORAGE_FILE_MODE 0x1B6
  54. #endif
  55. #define TLOG(X) basicLogCollection::getInstance()->Log("MOD_EVIDEO_T", CTX, X)
  56. #ifdef DEBUG
  57. #define TLOG_ERROR(X) \
  58. { \
  59. char _tBfrErrMSG2w34[2048]; \
  60. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  61. basicLogCollection::getInstance()->Log("MOD_EVIDEO_T", CTX, _LOG_ERROR, _tBfrErrMSG2w34); \
  62. }
  63. #else
  64. #define TLOG_ERROR(X) basicLogCollection::getInstance()->Log("MOD_EVIDEO_T", CTX, _LOG_ERROR, X);
  65. #endif
  66. enum
  67. {
  68. WEBRTC_REQ_SIGNIN = 0,
  69. WEBRTC_REQ_WAIT = 1,
  70. WEBRTC_REQ_SIGNOUT = 2,
  71. WEBRTC_REQ_MESSAGE = 3
  72. };
  73. //static const char * kRequestPaths[] = {"/stream", "/sign_in", "/wait", "/sign_out", "/message"};
  74. // buffer_data bd[1]={0};
  75. kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) :
  76. kernelModule(moduleID, KERNEL_MODULE_EMBED_VIDEO)
  77. #ifndef EVE_WORK_WITH_MEDIAMTX
  78. ,basicDualThread()
  79. #endif // !EVE_WORK_WITH_MEDIAMTX
  80. {
  81. m_pEventHeap = basicEventHeap::getInstance();
  82. if (m_pDBC)
  83. {
  84. DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
  85. if (tDATA)
  86. {
  87. memcpy(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  88. free(tDATA);
  89. }
  90. else
  91. {
  92. LOG_ERROR("No DB Data Error!");
  93. memset(&m_sEmbedVideoData, 0, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  94. m_sEmbedVideoData.port = 8972;
  95. dbcEmbedVideoLiveDefaults(&m_sEmbedVideoData);
  96. };
  97. }
  98. else
  99. {
  100. LOG_ERROR("DB Error!");
  101. };
  102. m_bDetached = true;
  103. m_tLiveRetry = 0;
  104. m_tClipsScan = 0;
  105. // The MediaMtx library logs into the la_mojo log (tag MTX)
  106. mtx::Logger::instance().setDestinations(false, std::string());
  107. mtx::Logger::instance().setSink([](mtx::LogLevel level, const char * line) {
  108. if (level == mtx::LogLevel::Debug)
  109. {
  110. return;
  111. }
  112. if (level == mtx::LogLevel::Error)
  113. {
  114. basicLogCollection::getInstance()->Log("MTX", NULL, _LOG_ERROR, line);
  115. }
  116. else
  117. {
  118. basicLogCollection::getInstance()->Log("MTX", NULL, line);
  119. }
  120. });
  121. applyLiveSettings();
  122. n_pNoSignalData = NULL;
  123. m_nNoSignalSize = 0;
  124. m_bLogoActive = false;
  125. m_nLogoX = 0;
  126. m_nLogoY = 0;
  127. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  128. n_pNoSignalSmallData = NULL;
  129. m_nNoSignalSmallSize = 0;
  130. #endif
  131. char tBuff[1024];
  132. sprintf(m_pBaseSavePath, "%s/data/reports", getcwd(tBuff, 1024));
  133. #ifdef DEBUG
  134. char tB[64];
  135. sprintf(tB, "Web Streams Port: %d", m_sEmbedVideoData.port);
  136. LOG(tB);
  137. sprintf(tB, "Frame: [%dx%d]", m_sEmbedVideoData.width, m_sEmbedVideoData.height);
  138. LOG(tB);
  139. if (m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES)
  140. {
  141. LOG("Output Pictures");
  142. }
  143. if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO)
  144. {
  145. LOG("Output Video");
  146. }
  147. if (m_sEmbedVideoData.show_ts)
  148. {
  149. LOG("Show TimeStamp");
  150. }
  151. if (m_sEmbedVideoData.show_label)
  152. {
  153. LOG("Show Label");
  154. }
  155. if (m_sEmbedVideoData.show_logo)
  156. {
  157. LOG("Show Logo");
  158. }
  159. #endif
  160. if (m_sEmbedVideoData.show_logo)
  161. {
  162. LOG(m_sEmbedVideoData.logo_md5);
  163. char tPath[MAX_PATH];
  164. sprintf(tPath, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5);
  165. if (! fileExist(tPath))
  166. {
  167. LOG_ERROR("Logo File Missed!"); // startService() leaves the logo off
  168. }
  169. #ifdef DEBUG
  170. else
  171. {
  172. LOG("Logo File is OK");
  173. }
  174. #endif
  175. ;
  176. };
  177. if (! fileExist(DB_SHMEM_POINT))
  178. {
  179. mkdir(DB_SHMEM_POINT, 0775); // S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  180. chmod(DB_SHMEM_POINT, 0775);
  181. mkdir(m_pVideoScratchPath, 0775);
  182. chmod(m_pVideoScratchPath, 0775);
  183. }
  184. else
  185. {
  186. if (! fileExist((CSTR) m_pVideoScratchPath))
  187. {
  188. mkdir(m_pVideoScratchPath, 0775);
  189. chmod(m_pVideoScratchPath, 0775);
  190. }
  191. else
  192. {
  193. // Clean Video Scratch
  194. char tCMD[MAX_PATH];
  195. sprintf(tCMD, "rm -r -f -d %s/* ", m_pVideoScratchPath);
  196. system(tCMD);
  197. };
  198. };
  199. }
  200. kernelModuleEmbedVideo::~kernelModuleEmbedVideo()
  201. {
  202. detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h)
  203. #ifdef DEBUG
  204. LOG("Destructor");
  205. #endif
  206. if (! m_bDetached)
  207. {
  208. stopService(true);
  209. };
  210. stopLive(); // the WebRTC server uses this object (PathProvider): stop it before the members go
  211. // Sanity check
  212. LOG("Sanity check in Destructor!");
  213. #ifndef EVE_WORK_WITH_MEDIAMTX
  214. waitThreadAEnd(true);
  215. if (m_vHandlersPool.size())
  216. { // Kill-Wait Started Probes
  217. #ifdef DEBUG
  218. char tB[200];
  219. sprintf(tB, "Have to Wait Client Handlers[%lu]!", m_vHandlersPool.size());
  220. LOG(tB);
  221. #endif
  222. for (vectorHANDLERS::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++)
  223. {
  224. pthread_cancel((*index)->m_hThreadHandle);
  225. }
  226. m_vHandlersPool.clear();
  227. };
  228. #ifdef DEBUG
  229. LOG("No More Clients Left");
  230. #endif
  231. waitThreadBEnd(true);
  232. if (m_vViewHandlersPool.size())
  233. { // Kill-Wait Started Viewers
  234. #ifdef DEBUG
  235. char tB[200];
  236. sprintf(tB, "Have to Wait View Handlers[%lu]!", m_vViewHandlersPool.size());
  237. LOG(tB);
  238. #endif
  239. for (vectorHANDLERS::iterator index = m_vViewHandlersPool.begin(); index < m_vViewHandlersPool.end(); index++)
  240. {
  241. pthread_cancel((*index)->m_hThreadHandle);
  242. }
  243. m_vViewHandlersPool.clear();
  244. };
  245. #ifdef DEBUG_EXTRA
  246. LOG("No More Viewers Left");
  247. #endif
  248. #ifdef DEBUG_EXTRA
  249. LOG("Deleting CAPS");
  250. #endif
  251. if (m_mapCAPTURES.size())
  252. {
  253. m_mapCAPTURES.clear();
  254. }
  255. #endif // !EVE_WORK_WITH_MEDIAMTX
  256. #ifdef DEBUG_EXTRA
  257. LOG("Deleting MATS");
  258. #endif
  259. if (m_mapMATS.size())
  260. {
  261. m_mapMATS.clear();
  262. }
  263. #ifdef DEBUG_EXTRA
  264. LOG("Deleting No Signal");
  265. #endif
  266. if (n_pNoSignalData)
  267. {
  268. free(n_pNoSignalData);
  269. }
  270. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  271. if (n_pNoSignalSmallData)
  272. {
  273. free(n_pNoSignalSmallData);
  274. }
  275. #endif
  276. if (fileExist((CSTR) m_pVideoScratchPath))
  277. {
  278. // Clean Video Scratch
  279. char tCMD[MAX_PATH];
  280. sprintf(tCMD, "rm -r -f -d %s/* ", m_pVideoScratchPath);
  281. system(tCMD);
  282. };
  283. #ifdef DEBUG_EXTRA
  284. LOG("All Done!");
  285. #endif
  286. }
  287. static const char * ht_head =
  288. "HTTP/1.1 200 OK\r\n"
  289. "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
  290. "Pragma: no-cache\r\n"
  291. "Expires: 0\r\n"
  292. "MIME-version: 1.0\r\n"
  293. "Connection: close\r\n"
  294. //"Keep-Alive: timeout=5, max=1\r\n"
  295. //"Connection: Keep-Alive\r\n"
  296. //"Content-Type: video/mp4\r\n"
  297. "Content-Transfer-Encoding: BINARY\r\n"
  298. //"Transfer-Encoding: chunked\r\n"
  299. "Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n"
  300. "\r\n";
  301. static const char * ht_abort =
  302. "HTTP/1.1 404 OK\r\n"
  303. "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
  304. "Pragma: no-cache\r\n"
  305. "Expires: 0\r\n"
  306. "Connection: close\r\n\r\n";
  307. static const char * ht_no_signal =
  308. "HTTP/1.1 200 OK\r\n"
  309. "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
  310. "Pragma: no-cache\r\n"
  311. "Expires: 0\r\n"
  312. "MIME-version: 1.0\r\n"
  313. "Connection: close\r\n"
  314. "Content-Transfer-Encoding: BINARY\r\n"
  315. //"Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n\r\n"
  316. "Content-Type: image/jpeg\r\nContent-length: %d\r\n\r\n";
  317. void signal_pipe_handler(int SIG)
  318. {
  319. // Web Client Disconnected!
  320. if ( SIG ) SIG = SIG;
  321. #ifdef DEBUG_EXTRA
  322. basicLogCollection::getInstance()->Log("SIG_PIPE", NULL, _LOG_ERROR, "Sig Pipe Received Web Client Disconnected!");
  323. #endif
  324. }
  325. bool kernelModuleEmbedVideo::isProcessingVideoStream(){
  326. bool RV = false;
  327. m_mStreamHandlersProtector.lock();
  328. if (m_mapHANDLERS.size() ){
  329. for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++){
  330. if( (*idx).second->preservingCurrentRecording() ){
  331. RV = true;
  332. break;
  333. };
  334. };
  335. };
  336. m_mStreamHandlersProtector.unlock();
  337. if (! RV)
  338. {
  339. // recorder clips: an alarm whose clip is not handed over yet
  340. std::lock_guard<std::mutex> lock(m_mClips);
  341. for (auto & c : m_mapClips)
  342. {
  343. if (! c.second.alarms.empty())
  344. {
  345. RV = true;
  346. break;
  347. };
  348. };
  349. };
  350. return RV;
  351. };
  352. uint8_t kernelModuleEmbedVideo::getMediaMode(void)
  353. {
  354. return m_sEmbedVideoData.output_mode;
  355. }
  356. #ifndef EVE_WORK_WITH_MEDIAMTX
  357. void * kernelModuleEmbedVideo::webClientProc(void * ptr)
  358. {
  359. WEB_CLIENT_CTX * CTX = (WEB_CLIENT_CTX *) ptr;
  360. //kernelModuleEmbedVideo * MODULE = CTX->ModulePtr;
  361. uint32_t msgSize;
  362. uint8_t tBuf[FILESTREAMBUFFERSZ];
  363. //int retVal;
  364. bool w = true;
  365. #ifdef DEBUG_EXTRA
  366. TLOG("Start WebClient");
  367. #endif
  368. pthread_setname_np(pthread_self(), "LiveVideoCL");
  369. if (CTX)
  370. {
  371. char mesg[1024];
  372. //size_t RecvSize;
  373. struct pollfd pfd[1];
  374. char cGetURI[1024];
  375. //bool m_bDeflatePerMessage = false;
  376. //bool m_pInit;
  377. #ifdef DEBUG_EXTRA
  378. pid_t pid = gettid();
  379. char pidBuf[16];
  380. sprintf(pidBuf, "pid:%d", pid);
  381. TLOG(pidBuf);
  382. #endif
  383. signal(SIGPIPE, signal_pipe_handler);
  384. pfd->fd = CTX->m_iSocketFID;
  385. pfd->events = POLLIN | POLLHUP;
  386. while (CTX->bValid && w)
  387. {
  388. if (! CTX->initDone)
  389. {
  390. int RetVal;
  391. char * reqline[256];
  392. RetVal = poll(pfd, 1, 1000);
  393. if (RetVal <= 0)
  394. {
  395. if (EINTR == errno)
  396. {
  397. continue;
  398. }
  399. TLOG_ERROR("Unrecoverable error");
  400. w = false;
  401. continue;
  402. };
  403. memset(mesg, 0, sizeof(mesg));
  404. RetVal = recv(CTX->m_iSocketFID, mesg, sizeof(mesg), 0);
  405. if (RetVal <= 0)
  406. {
  407. sprintf(mesg, "web recv() error: %s", strerror(errno));
  408. TLOG_ERROR(mesg);
  409. w = false;
  410. continue;
  411. };
  412. #ifdef DEBUG_EXTRA
  413. TLOG(mesg);
  414. #endif
  415. uint32_t index = 0;
  416. reqline[index] = strtok(mesg, "\n");
  417. if (strncmp(reqline[index], "GET", 3) == 0)
  418. {
  419. char * pPartURI;
  420. char * pPartPROTO;
  421. pPartURI = strstr(reqline[index], " ");
  422. if (pPartURI != NULL)
  423. {
  424. pPartURI++;
  425. pPartPROTO = strstr(pPartURI, " ");
  426. pPartPROTO++;
  427. int URILen = pPartPROTO - pPartURI;
  428. strncpy(cGetURI, pPartURI, URILen);
  429. cGetURI[URILen] = (char) 0;
  430. if (strncmp(pPartPROTO, "HTTP/1.0", 8) != 0 && strncmp(pPartPROTO, "HTTP/1.1", 8) != 0)
  431. {
  432. TLOG_ERROR("No HTTP version supplied in request!");
  433. if (write(CTX->m_iSocketFID, "HTTP/1.1 400 Bad Request\r\n", 26) < 0)
  434. {
  435. sprintf(mesg, "Failed returning status 400 to client: %s", strerror(errno));
  436. TLOG_ERROR(mesg);
  437. };
  438. w = false;
  439. continue;
  440. };
  441. };
  442. #ifdef DEBUG_EXTRA
  443. TLOG("Req URI");
  444. TLOG(cGetURI);
  445. #endif
  446. };
  447. // if( ( strncmp( cGetURI, "/stream", 7 ) == 0 ) || ( strncmp( cGetURI, "/video_feed", 11 ) == 0 ) ) {
  448. if (strncmp(cGetURI, "/video_feed", 11) == 0)
  449. {
  450. uint32_t tStreamID = 0;
  451. CSTR tP;
  452. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  453. if ((tP = strstr(cGetURI, "/preview")))
  454. {
  455. CTX->bPreviewMode = true;
  456. }
  457. #endif
  458. if ((tP = strstr(cGetURI, "?")))
  459. {
  460. CSTR tB;
  461. tP++;
  462. if ((tB = strstr(tP, "id=")))
  463. {
  464. tStreamID = atoi(tB + 3);
  465. };
  466. };
  467. if (tStreamID)
  468. {
  469. #ifdef DEBUG_EXTRA
  470. char tB[64];
  471. sprintf(tB, "StremID: %d", tStreamID);
  472. TLOG(tB);
  473. #endif
  474. if (CTX->ModulePtr->registerForMediaStream(tStreamID, CTX))
  475. {
  476. // Send Header
  477. if (send(CTX->m_iSocketFID, ht_head, strlen(ht_head), 0) < 0)
  478. {
  479. #ifdef DEBUG_EXTRA
  480. sprintf(mesg, "Failed sending file to client: %s", strerror(errno));
  481. TLOG_ERROR(mesg);
  482. #endif
  483. w = false;
  484. };
  485. CTX->initDone = true;
  486. continue;
  487. #ifdef DEBUG_EXTRA
  488. }
  489. else
  490. {
  491. TLOG("Stream id Not Online");
  492. #endif
  493. };
  494. #ifdef DEBUG_EXTRA
  495. }
  496. else
  497. {
  498. TLOG("No Stream ID");
  499. #endif
  500. };
  501. };
  502. if (CTX->ModulePtr->n_pNoSignalData)
  503. {
  504. // Send Dummy Picture
  505. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  506. if (CTX->bPreviewMode)
  507. {
  508. sprintf(mesg, ht_no_signal, CTX->ModulePtr->m_nNoSignalSmallSize);
  509. send(CTX->m_iSocketFID, mesg, strlen(mesg), 0);
  510. send(CTX->m_iSocketFID, CTX->ModulePtr->n_pNoSignalSmallData, CTX->ModulePtr->m_nNoSignalSmallSize, 0);
  511. send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0);
  512. }
  513. else
  514. {
  515. #endif
  516. sprintf(mesg, ht_no_signal, CTX->ModulePtr->m_nNoSignalSize);
  517. send(CTX->m_iSocketFID, mesg, strlen(mesg), 0);
  518. send(CTX->m_iSocketFID, CTX->ModulePtr->n_pNoSignalData, CTX->ModulePtr->m_nNoSignalSize, 0);
  519. send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0);
  520. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  521. };
  522. #endif
  523. #ifdef DEBUG_EXTRA_X
  524. TLOG("VFEED Sent No Signal Image!");
  525. #endif
  526. }
  527. else
  528. {
  529. // Send 404 Header
  530. send(CTX->m_iSocketFID, ht_abort, strlen(ht_abort), 0);
  531. #ifdef DEBUG_EXTRA
  532. TLOG("Aborted!");
  533. #endif
  534. };
  535. w = false;
  536. continue;
  537. }
  538. else
  539. {
  540. /*
  541. retVal = read( CTX->m_iEncoderPipe[0], &msgSize, sizeof(uint32_t));
  542. if( retVal == -1 ){
  543. if( errno == EAGAIN || errno == EWOULDBLOCK ){
  544. continue;
  545. };
  546. TLOG_ERROR("Pipe Communication Error[1]!");
  547. CTX->bValid = false;
  548. continue;
  549. };
  550. if( msgSize < FILESTREAMBUFFERSZ ) {
  551. retVal = read( CTX->m_iEncoderPipe[0], tBuf, msgSize );
  552. } else {
  553. TLOG_ERROR("Data too Big in Pipe Transfer!");
  554. CTX->bValid = false;
  555. continue;
  556. };
  557. if( retVal == -1 ){
  558. //free( tBuf );
  559. if( errno == EAGAIN || errno == EWOULDBLOCK ){
  560. continue;
  561. };
  562. TLOG_ERROR("Pipe Communication Error[2]!");
  563. CTX->bValid = false;
  564. continue;
  565. };
  566. */
  567. /**************/
  568. CTX->ModulePtr->m_mEncodedProtector.lock();
  569. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  570. if (CTX->bPreviewMode)
  571. {
  572. if (CTX->ModulePtr->m_mapPREVIEWS[CTX->mediaStreamId].size())
  573. {
  574. msgSize = CTX->ModulePtr->m_mapPREVIEWS[CTX->mediaStreamId].size();
  575. if (msgSize < FILESTREAMBUFFERSZ)
  576. {
  577. memcpy(tBuf, CTX->ModulePtr->m_mapPREVIEWS[CTX->mediaStreamId].data(), msgSize);
  578. CTX->ModulePtr->m_mEncodedProtector.unlock();
  579. }
  580. else
  581. {
  582. CTX->ModulePtr->m_mEncodedProtector.unlock();
  583. TLOG_ERROR("Data too Big in Pipe Transfer!");
  584. w = false;
  585. continue;
  586. };
  587. }
  588. else
  589. {
  590. CTX->ModulePtr->m_mEncodedProtector.unlock();
  591. if (CTX->ModulePtr->n_pNoSignalSmallData)
  592. {
  593. msgSize = CTX->ModulePtr->m_nNoSignalSmallSize;
  594. memcpy(tBuf, CTX->ModulePtr->n_pNoSignalSmallData, msgSize);
  595. }
  596. else
  597. {
  598. TLOG_ERROR("Data too Big in Pipe Transfer!");
  599. w = false;
  600. continue;
  601. };
  602. };
  603. }
  604. else
  605. {
  606. #endif
  607. if (CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].size())
  608. {
  609. msgSize = CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].size();
  610. if (msgSize < FILESTREAMBUFFERSZ)
  611. {
  612. memcpy(tBuf, CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].data(), msgSize);
  613. CTX->ModulePtr->m_mEncodedProtector.unlock();
  614. }
  615. else
  616. {
  617. CTX->ModulePtr->m_mEncodedProtector.unlock();
  618. TLOG_ERROR("Data too Big in Pipe Transfer!");
  619. w = false;
  620. continue;
  621. };
  622. }
  623. else
  624. {
  625. #ifdef DEBUG_EXTRA
  626. TLOG("VFEED Sent No Signal Image!");
  627. #endif
  628. CTX->ModulePtr->m_mEncodedProtector.unlock();
  629. if (CTX->ModulePtr->n_pNoSignalData)
  630. {
  631. msgSize = CTX->ModulePtr->m_nNoSignalSize;
  632. memcpy(tBuf, CTX->ModulePtr->n_pNoSignalData, msgSize);
  633. }
  634. else
  635. {
  636. TLOG_ERROR("Data too Big in Pipe Transfer!");
  637. w = false;
  638. continue;
  639. };
  640. };
  641. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  642. };
  643. #endif
  644. /*********/
  645. sprintf(mesg, "--MPFrame\r\nContent-type: image/jpeg\r\nContent-length: %d\r\n\r\n", msgSize);
  646. if (send(CTX->m_iSocketFID, mesg, strlen(mesg), 0) < 0)
  647. {
  648. #ifdef DEBUG_EXTRA
  649. sprintf(mesg, "[1]Failed sending file to client: %s", strerror(errno));
  650. TLOG_ERROR(mesg);
  651. #endif
  652. w = false;
  653. continue;
  654. };
  655. if (send(CTX->m_iSocketFID, tBuf, msgSize, 0) < 0)
  656. {
  657. #ifdef DEBUG_EXTRA
  658. sprintf(mesg, "[2]Failed sending file to client: %s", strerror(errno));
  659. TLOG_ERROR(mesg);
  660. #endif
  661. w = false;
  662. continue;
  663. };
  664. if (send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0) < 0)
  665. {
  666. #ifdef DEBUG_EXTRA
  667. sprintf(mesg, "[3]Failed sending file to client: %s", strerror(errno));
  668. TLOG_ERROR(mesg);
  669. #endif
  670. w = false;
  671. continue;
  672. };
  673. usleep(66666);
  674. };
  675. };
  676. };
  677. close(CTX->m_iEncoderPipe[0]);
  678. shutdown(CTX->m_iSocketFID, SHUT_RDWR);
  679. SLEEP(3);
  680. close(CTX->m_iSocketFID);
  681. CTX->bValid = false;
  682. #ifdef DEBUG_EXTRA
  683. TLOG("WEB Client Handler Ended!");
  684. #endif
  685. pthread_exit(NULL);
  686. return NULL;
  687. }
  688. void * kernelModuleEmbedVideo::viewClientProc(void * ptr)
  689. {
  690. WEB_CLIENT_CTX * CTX = (WEB_CLIENT_CTX *) ptr;
  691. //kernelModuleEmbedVideo * MODULE = CTX->ModulePtr;
  692. //uint32_t tVideoID = 0;
  693. long int tDTS = 0;
  694. uint32_t tETS = 0;
  695. uint32_t msgSize;
  696. uint8_t tBuf[FILESTREAMBUFFERSZ];
  697. uint8_t rBuf[8192];
  698. int rSIZE;
  699. int cSIZE;
  700. //int retVal;
  701. bool w = true;
  702. char tBASE[11] = {0};
  703. char tID[33] = {0};
  704. char tFILE[PATH_MAX];
  705. uint32_t cFRAME = 0;
  706. void * sPtr = NULL;
  707. void * ePtr = NULL;
  708. uint8_t baba[2] = {0xff, 0xd8};
  709. uint8_t yaba[2] = {0xff, 0xd9};
  710. FILE * tFID = NULL;
  711. TLOG("Start ViewClient");
  712. pthread_setname_np(pthread_self(), "ReportVideoCL");
  713. if (CTX)
  714. {
  715. char mesg[1024];
  716. //size_t RecvSize;
  717. struct pollfd pfd[1];
  718. char cGetURI[1024];
  719. //bool m_bDeflatePerMessage = false;
  720. //bool m_pInit;
  721. #ifdef DEBUG_EXTRA
  722. pid_t pid = gettid();
  723. char pidBuf[16];
  724. sprintf(pidBuf, "pid:%d", pid);
  725. TLOG(pidBuf);
  726. #endif
  727. signal(SIGPIPE, signal_pipe_handler);
  728. pfd->fd = CTX->m_iSocketFID;
  729. pfd->events = POLLIN | POLLHUP;
  730. while (CTX->bValid && w)
  731. {
  732. if (! CTX->initDone)
  733. {
  734. int RetVal;
  735. char * reqline[256];
  736. RetVal = poll(pfd, 1, 1000);
  737. if (RetVal <= 0)
  738. {
  739. if (EINTR == errno)
  740. {
  741. continue;
  742. }
  743. TLOG_ERROR("Unrecoverable error");
  744. w = false;
  745. continue;
  746. };
  747. memset(mesg, 0, sizeof(mesg));
  748. RetVal = recv(CTX->m_iSocketFID, mesg, sizeof(mesg), 0);
  749. if (RetVal <= 0)
  750. {
  751. sprintf(mesg, "web recv() error: %s", strerror(errno));
  752. TLOG_ERROR(mesg);
  753. w = false;
  754. continue;
  755. };
  756. #ifdef DEBUG_EXTRA
  757. TLOG(mesg);
  758. #endif
  759. uint32_t index = 0;
  760. reqline[index] = strtok(mesg, "\n");
  761. if (strncmp(reqline[index], "GET", 3) == 0)
  762. {
  763. char * pPartURI;
  764. char * pPartPROTO;
  765. pPartURI = strstr(reqline[index], " ");
  766. if (pPartURI != NULL)
  767. {
  768. pPartURI++;
  769. pPartPROTO = strstr(pPartURI, " ");
  770. pPartPROTO++;
  771. int URILen = pPartPROTO - pPartURI;
  772. strncpy(cGetURI, pPartURI, URILen);
  773. cGetURI[URILen] = (char) 0;
  774. if (strncmp(pPartPROTO, "HTTP/1.0", 8) != 0 && strncmp(pPartPROTO, "HTTP/1.1", 8) != 0)
  775. {
  776. TLOG_ERROR("No HTTP version supplied in request!");
  777. if (write(CTX->m_iSocketFID, "HTTP/1.1 400 Bad Request\r\n", 26) < 0)
  778. {
  779. sprintf(mesg, "Failed returning status 400 to client: %s", strerror(errno));
  780. TLOG_ERROR(mesg);
  781. };
  782. w = false;
  783. continue;
  784. };
  785. };
  786. #ifdef DEBUG_EXTRA
  787. TLOG("Req URI");
  788. TLOG(cGetURI);
  789. #endif
  790. };
  791. if ((strncmp(cGetURI, "/stream", 7) == 0) || (strncmp(cGetURI, "/video_view", 11) == 0))
  792. {
  793. CSTR tP;
  794. if ((tP = strstr(cGetURI, "?")))
  795. {
  796. CSTR tB;
  797. tP++;
  798. if ((tB = strcasestr(tP, "id=")))
  799. {
  800. strncpy(tID, tB + 3, 32);
  801. tID[32] = (char) 0;
  802. };
  803. if ((tB = strcasestr(tP, "ts=")))
  804. {
  805. tDTS = atol(tB + 3);
  806. // struct tm * ptm = gmtime ( &tDTS );
  807. struct tm * ptm = localtime(&tDTS);
  808. if (ptm)
  809. {
  810. sprintf(tBASE, "%04d/%02d/%02d", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
  811. };
  812. };
  813. if ((tB = strcasestr(tP, "sf=")))
  814. {
  815. tETS = atoi(tB + 3);
  816. };
  817. sprintf(tFILE, "./data/reports/%s/%s.mjpeg", tBASE, tID);
  818. };
  819. #ifdef DEBUG_EXTRA
  820. char tB[64];
  821. sprintf(tB, "Requested Video: %s", tFILE);
  822. TLOG(tB);
  823. #endif
  824. if (fileExist(tFILE))
  825. {
  826. tFID = fopen(tFILE, "r");
  827. if (tFID)
  828. {
  829. // Send Header
  830. if (send(CTX->m_iSocketFID, ht_head, strlen(ht_head), 0) < 0)
  831. {
  832. #ifdef DEBUG
  833. sprintf(mesg, "Failed sending file to client: %s", strerror(errno));
  834. TLOG_ERROR(mesg);
  835. #endif
  836. w = false;
  837. };
  838. CTX->initDone = true;
  839. continue;
  840. #ifdef DEBUG
  841. }
  842. else
  843. {
  844. TLOG("Stream id Not Online");
  845. #endif
  846. };
  847. }
  848. else
  849. {
  850. TLOG_ERROR("VVIEW File Not Exists!");
  851. };
  852. };
  853. #ifdef DEBUG_EXTRA
  854. TLOG("VVIEW Catched in DeadEnd!");
  855. #endif
  856. if (1)
  857. {
  858. // Send Dummy Picture
  859. sprintf(mesg, ht_no_signal, CTX->ModulePtr->m_nNoSignalSize);
  860. send(CTX->m_iSocketFID, mesg, strlen(mesg), 0);
  861. send(CTX->m_iSocketFID, CTX->ModulePtr->n_pNoSignalData, CTX->ModulePtr->m_nNoSignalSize, 0);
  862. send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0);
  863. #ifdef DEBUG_EXTRA
  864. TLOG("VVIEW Sent No Signal Image!");
  865. #endif
  866. }
  867. else
  868. {
  869. // Send 404 Header
  870. send(CTX->m_iSocketFID, ht_abort, strlen(ht_abort), 0);
  871. #ifdef DEBUG_EXTRA
  872. TLOG("Aborted!");
  873. #endif
  874. };
  875. w = false;
  876. continue;
  877. }
  878. else
  879. {
  880. cSIZE = 0;
  881. while (! feof(tFID) && CTX->bValid && w)
  882. {
  883. rSIZE = fread(rBuf, 1, 8192, tFID);
  884. if (rSIZE > 0)
  885. {
  886. memcpy(tBuf + cSIZE, rBuf, rSIZE);
  887. cSIZE += rSIZE;
  888. if (sPtr == NULL)
  889. {
  890. sPtr = memmem(tBuf, cSIZE, baba, 2);
  891. }
  892. if (sPtr)
  893. {
  894. if (sPtr != tBuf)
  895. {
  896. cSIZE -= ((uint64_t) sPtr - (uint64_t) tBuf);
  897. memcpy(tBuf, sPtr, cSIZE);
  898. sPtr = tBuf;
  899. };
  900. if (ePtr == NULL)
  901. {
  902. ePtr = memmem(sPtr, cSIZE, yaba, 2);
  903. }
  904. };
  905. };
  906. if (sPtr != NULL && ePtr != NULL)
  907. {
  908. if (feof(tFID))
  909. {
  910. TLOG("LAST FRAME");
  911. }
  912. cFRAME++;
  913. ePtr =(void *)(((uint64_t)ePtr) + 2);
  914. msgSize = ((uint64_t) ePtr - (uint64_t) sPtr);
  915. if (cFRAME > tETS || feof(tFID))
  916. { // or last chanse to send data
  917. if (feof(tFID))
  918. {
  919. TLOG("SEND HEADER");
  920. }
  921. sprintf(mesg, "--MPFrame\r\nContent-type: image/jpeg\r\nContent-length: %d\r\n\r\n", msgSize);
  922. if (send(CTX->m_iSocketFID, mesg, strlen(mesg), 0) < 0)
  923. {
  924. #ifdef DEBUG
  925. sprintf(mesg, "[1]Failed sending file to client: %s", strerror(errno));
  926. TLOG_ERROR(mesg);
  927. #endif
  928. w = false;
  929. continue;
  930. };
  931. if (feof(tFID))
  932. {
  933. TLOG("SEND BODY");
  934. }
  935. if (send(CTX->m_iSocketFID, tBuf, msgSize, 0) < 0)
  936. {
  937. #ifdef DEBUG
  938. sprintf(mesg, "[2]Failed sending file to client: %s", strerror(errno));
  939. TLOG_ERROR(mesg);
  940. #endif
  941. w = false;
  942. continue;
  943. };
  944. if (feof(tFID))
  945. {
  946. TLOG("SEND TAIL");
  947. }
  948. if (send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0) < 0)
  949. {
  950. #ifdef DEBUG
  951. sprintf(mesg, "[3]Failed sending file to client: %s", strerror(errno));
  952. TLOG_ERROR(mesg);
  953. #endif
  954. w = false;
  955. continue;
  956. };
  957. SLEEP(66);
  958. };
  959. if (! feof(tFID))
  960. {
  961. cSIZE -= msgSize;
  962. memcpy(tBuf, ePtr, cSIZE);
  963. sPtr = ePtr = NULL;
  964. }
  965. #ifdef DEBUG
  966. else
  967. {
  968. TLOG("File is Finished!");
  969. }
  970. #endif
  971. };
  972. #ifdef DEBUG
  973. if (feof(tFID))
  974. {
  975. TLOG("LAST FRAME SENT");
  976. }
  977. #endif
  978. };
  979. #ifdef DEBUG_EXTRA
  980. TLOG("Close File");
  981. #endif
  982. fclose(tFID);
  983. w = false;
  984. };
  985. };
  986. };
  987. #ifdef DEBUG_EXTRA
  988. TLOG("Shut Down Socket");
  989. #endif
  990. shutdown(CTX->m_iSocketFID, SHUT_RDWR);
  991. #ifdef DEBUG_EXTRA
  992. TLOG("Close Socket");
  993. #endif
  994. close(CTX->m_iSocketFID);
  995. CTX->bValid = false;
  996. #ifdef DEBUG_EXTRA
  997. TLOG("View Client Handler Ended!");
  998. #endif
  999. pthread_exit(NULL);
  1000. TLOG("After Pthread Exit");
  1001. return NULL;
  1002. }
  1003. // Start Thread To view Live Streams
  1004. void kernelModuleEmbedVideo::m_fThreadAProc(void)
  1005. {
  1006. if (! m_bEnabled)
  1007. {
  1008. #ifdef DEBUG_EXTRA
  1009. LOG("Module is Disabled!");
  1010. #endif
  1011. return;
  1012. };
  1013. #ifdef DEBUG_EXTRA
  1014. LOG("[LIVE] Module Started!");
  1015. #endif
  1016. pthread_setname_np(pthread_self(), "Live Video Stream");
  1017. struct pollfd pfd[1];
  1018. pfd->fd = m_nSocketDescriptor;
  1019. pfd->events = POLLIN | POLLHUP;
  1020. m_bThreadAStatus = TH_WORKING;
  1021. while (m_bThreadAStatus < TH_ENDED)
  1022. {
  1023. if (! m_bEnabled)
  1024. {
  1025. m_bThreadAStatus = TH_ENDED;
  1026. continue;
  1027. };
  1028. int RetVal = poll(pfd, 1, 1000);
  1029. if (RetVal < 0)
  1030. {
  1031. if (EINTR == errno)
  1032. {
  1033. continue;
  1034. }
  1035. LOG_ERROR("Unrecoverable error");
  1036. m_bThreadAStatus = TH_ENDED;
  1037. continue;
  1038. };
  1039. // Handle Connections
  1040. if (RetVal != 0)
  1041. {
  1042. if (pfd[0].revents & POLLIN)
  1043. {
  1044. #ifdef DEBUG_EXTRA
  1045. LOG("Handle New Client");
  1046. #endif
  1047. int tSID = accept(pfd[0].fd, NULL, NULL);
  1048. if (tSID < 0)
  1049. {
  1050. char tBuf[CTEXT];
  1051. sprintf(tBuf, "Accept() error: %s", strerror(errno));
  1052. LOG_ERROR(tBuf);
  1053. }
  1054. else
  1055. {
  1056. #ifdef DEBUG_EXTRA
  1057. LOG("Accept OK");
  1058. #endif
  1059. WEB_CLIENT_CTX * tCTX = (WEB_CLIENT_CTX *) malloc(sizeof(WEB_CLIENT_CTX));
  1060. if (tCTX)
  1061. {
  1062. #ifdef DEBUG_EXTRA
  1063. LOG("CTX OK");
  1064. #endif
  1065. WEB_HANDLER * tHANDLER = (WEB_HANDLER *) malloc(sizeof(WEB_HANDLER));
  1066. if (tHANDLER)
  1067. {
  1068. #ifdef DEBUG_EXTRA
  1069. LOG("HANDLER OK");
  1070. #endif
  1071. memset(tCTX, 0, sizeof(WEB_CLIENT_CTX));
  1072. memset(tHANDLER, 0, sizeof(WEB_HANDLER));
  1073. tCTX->m_iSocketFID = tSID;
  1074. tCTX->m_iEncoderPipe[0] = -1;
  1075. tCTX->m_iEncoderPipe[1] = -1;
  1076. tCTX->mediaStreamId = 0;
  1077. tCTX->bValid = true;
  1078. tCTX->ModulePtr = this;
  1079. #ifdef DEBUG_EXTRA
  1080. LOG("Before Thread Create");
  1081. #endif
  1082. int RetVal = pthread_create(&tHANDLER->m_hThreadHandle, NULL, webClientProc, (void *) tCTX);
  1083. if (RetVal == -1)
  1084. {
  1085. free(tCTX);
  1086. free(tHANDLER);
  1087. LOG_ERROR("Thread Creation Faled.");
  1088. shutdown(tSID, SHUT_RDWR);
  1089. close(tSID);
  1090. }
  1091. else
  1092. {
  1093. tHANDLER->CTX = tCTX;
  1094. m_mWebHandlersProtector.lock();
  1095. m_vHandlersPool.push_back(tHANDLER);
  1096. m_mWebHandlersProtector.unlock();
  1097. #ifdef DEBUG_EXTRA
  1098. LOG("Handler Pushed to a Pool");
  1099. #endif
  1100. };
  1101. }
  1102. else
  1103. { // https://stackoverflow.com/questions/18545077/image-fullscreen-on-click
  1104. free(tCTX);
  1105. LOG_ERROR("CL_HANDLER Mem Allocation Failure!");
  1106. shutdown(tSID, SHUT_RDWR);
  1107. close(tSID);
  1108. };
  1109. }
  1110. else
  1111. {
  1112. LOG_ERROR("CL_CTX Mem Allocation Failure!");
  1113. shutdown(tSID, SHUT_RDWR);
  1114. close(tSID);
  1115. };
  1116. };
  1117. };
  1118. };
  1119. // Handle Dead Clients
  1120. m_mWebHandlersProtector.lock();
  1121. if (m_vHandlersPool.size())
  1122. {
  1123. for (vectorHANDLERS::iterator idx = m_vHandlersPool.begin(); idx != m_vHandlersPool.end();)
  1124. {
  1125. if (! ((*idx)->CTX->bValid))
  1126. {
  1127. #ifdef DEBUG_EXTRA
  1128. LOG("Have Dead Client!");
  1129. #endif
  1130. pthread_join((*idx)->m_hThreadHandle, NULL);
  1131. // close( (*idx)->CTX->m_iEncoderPipe[0] );
  1132. close((*idx)->CTX->m_iEncoderPipe[1]);
  1133. free((*idx)->CTX);
  1134. free(*idx);
  1135. m_vHandlersPool.erase(idx);
  1136. #ifdef DEBUG_EXTRA
  1137. LOG("Web Handler Ripped!");
  1138. #endif
  1139. }
  1140. else
  1141. {
  1142. idx++;
  1143. }
  1144. };
  1145. };
  1146. m_mWebHandlersProtector.unlock();
  1147. // validate active streams ?
  1148. SLEEP(50);
  1149. };
  1150. }
  1151. void kernelModuleEmbedVideo::m_fThreadBProc(void)
  1152. {
  1153. if (! m_bEnabled)
  1154. {
  1155. #ifdef DEBUG_EXTRA
  1156. LOG("Module is Disabled!");
  1157. #endif
  1158. return;
  1159. };
  1160. #ifdef DEBUG_EXTRA
  1161. LOG("[VIEW] Module Started!");
  1162. #endif
  1163. struct pollfd pfd[1];
  1164. pfd->fd = m_nViewSocketDescriptor;
  1165. pfd->events = POLLIN | POLLHUP;
  1166. m_bThreadBStatus = TH_WORKING;
  1167. pthread_setname_np(pthread_self(), "Report Video View");
  1168. while (m_bThreadBStatus < TH_ENDED)
  1169. {
  1170. if (! m_bEnabled)
  1171. {
  1172. m_bThreadBStatus = TH_ENDED;
  1173. continue;
  1174. };
  1175. int RetVal = poll(pfd, 1, 1000);
  1176. if (RetVal < 0)
  1177. {
  1178. if (EINTR == errno)
  1179. {
  1180. continue;
  1181. }
  1182. LOG_ERROR("Unrecoverable error");
  1183. m_bThreadBStatus = TH_ENDED;
  1184. continue;
  1185. };
  1186. // Handle Connections
  1187. if (RetVal != 0)
  1188. {
  1189. if (pfd[0].revents & POLLIN)
  1190. {
  1191. #ifdef DEBUG_EXTRA
  1192. LOG("Handle New View");
  1193. #endif
  1194. int tSID = accept(pfd[0].fd, NULL, NULL);
  1195. if (tSID < 0)
  1196. {
  1197. char tBuf[CTEXT];
  1198. sprintf(tBuf, "Accept() error: %s", strerror(errno));
  1199. LOG_ERROR(tBuf);
  1200. }
  1201. else
  1202. {
  1203. #ifdef DEBUG_EXTRA
  1204. LOG("Accept OK");
  1205. #endif
  1206. WEB_CLIENT_CTX * tCTX = (WEB_CLIENT_CTX *) malloc(sizeof(WEB_CLIENT_CTX));
  1207. if (tCTX)
  1208. {
  1209. #ifdef DEBUG_EXTRA
  1210. LOG("CTX OK");
  1211. #endif
  1212. WEB_HANDLER * tHANDLER = (WEB_HANDLER *) malloc(sizeof(WEB_HANDLER));
  1213. if (tHANDLER)
  1214. {
  1215. #ifdef DEBUG_EXTRA
  1216. LOG("HANDLER OK");
  1217. #endif
  1218. memset(tCTX, 0, sizeof(WEB_CLIENT_CTX));
  1219. memset(tHANDLER, 0, sizeof(WEB_HANDLER));
  1220. tCTX->m_iSocketFID = tSID;
  1221. tCTX->m_iEncoderPipe[0] = -1;
  1222. tCTX->m_iEncoderPipe[1] = -1;
  1223. tCTX->mediaStreamId = 0;
  1224. tCTX->bValid = true;
  1225. tCTX->ModulePtr = this;
  1226. #ifdef DEBUG_EXTRA
  1227. LOG("Before Thread Create");
  1228. #endif
  1229. int RetVal = pthread_create(&tHANDLER->m_hThreadHandle, NULL, viewClientProc, (void *) tCTX);
  1230. if (RetVal == -1)
  1231. {
  1232. free(tCTX);
  1233. free(tHANDLER);
  1234. LOG_ERROR("Thread Creation Faled.");
  1235. shutdown(tSID, SHUT_RDWR);
  1236. close(tSID);
  1237. }
  1238. else
  1239. {
  1240. tHANDLER->CTX = tCTX;
  1241. m_mViewHandlersProtector.lock();
  1242. m_vViewHandlersPool.push_back(tHANDLER);
  1243. m_mViewHandlersProtector.unlock();
  1244. #ifdef DEBUG_EXTRA
  1245. LOG("Handler Pushed to a Pool");
  1246. #endif
  1247. };
  1248. }
  1249. else
  1250. { // https://stackoverflow.com/questions/18545077/image-fullscreen-on-click
  1251. free(tCTX);
  1252. LOG_ERROR("CL_HANDLER Mem Allocation Failure!");
  1253. shutdown(tSID, SHUT_RDWR);
  1254. close(tSID);
  1255. };
  1256. }
  1257. else
  1258. {
  1259. LOG_ERROR("CL_CTX Mem Allocation Failure!");
  1260. shutdown(tSID, SHUT_RDWR);
  1261. close(tSID);
  1262. };
  1263. };
  1264. };
  1265. };
  1266. // Handle Dead Clients
  1267. m_mViewHandlersProtector.lock();
  1268. if (m_vViewHandlersPool.size())
  1269. {
  1270. for (vectorHANDLERS::iterator idx = m_vViewHandlersPool.begin(); idx != m_vViewHandlersPool.end();)
  1271. {
  1272. if (! ((*idx)->CTX->bValid))
  1273. {
  1274. #ifdef DEBUG_EXTRA
  1275. LOG("Have Dead Viewer!");
  1276. #endif
  1277. pthread_join((*idx)->m_hThreadHandle, NULL);
  1278. free((*idx)->CTX);
  1279. free(*idx);
  1280. m_vViewHandlersPool.erase(idx);
  1281. #ifdef DEBUG_EXTRA
  1282. LOG("View Handler Ripped!");
  1283. #endif
  1284. }
  1285. else
  1286. {
  1287. idx++;
  1288. }
  1289. };
  1290. };
  1291. m_mViewHandlersProtector.unlock();
  1292. // validate active streams ?
  1293. SLEEP(50);
  1294. };
  1295. }
  1296. #endif // !EVE_WORK_WITH_MEDIAMTX
  1297. #define ENABLE_SOCKOPT(sd, level, opt) \
  1298. do \
  1299. { \
  1300. int val = 1; \
  1301. if (setsockopt(sd, level, opt, &val, sizeof(val)) < 0) \
  1302. warn("Failed enabling %s for web service", #opt); \
  1303. } \
  1304. while (0);
  1305. bool kernelModuleEmbedVideo::startService()
  1306. {
  1307. m_bDetached = false;
  1308. // Create NoSignal Image
  1309. cv::Mat tMat(m_sEmbedVideoData.height, m_sEmbedVideoData.width, CV_8UC3);
  1310. cv::rectangle(tMat, cv::Point(0, 0), cv::Point(m_sEmbedVideoData.width, m_sEmbedVideoData.height), cv::Scalar(255, 0, 0), cv::FILLED);
  1311. cv::putText(tMat, "NO SIGNAL", cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255), 3);
  1312. m_bLogoActive = false;
  1313. m_mLogoPic.release();
  1314. m_mLogoMask.release();
  1315. if (m_sEmbedVideoData.show_logo && m_sEmbedVideoData.logo_w > 0 && m_sEmbedVideoData.logo_h > 0 && m_sEmbedVideoData.logo_w <= m_sEmbedVideoData.width && m_sEmbedVideoData.logo_h <= m_sEmbedVideoData.height)
  1316. {
  1317. // Negative coordinates are measured from the right / bottom edge
  1318. int a_x = m_sEmbedVideoData.logo_x;
  1319. int a_y = m_sEmbedVideoData.logo_y;
  1320. if (a_x < 0)
  1321. {
  1322. a_x = m_sEmbedVideoData.width + a_x - m_sEmbedVideoData.logo_w;
  1323. }
  1324. if (a_y < 0)
  1325. {
  1326. a_y = m_sEmbedVideoData.height + a_y - m_sEmbedVideoData.logo_h;
  1327. }
  1328. a_x = std::max(0, std::min(a_x, (int) m_sEmbedVideoData.width - (int) m_sEmbedVideoData.logo_w));
  1329. a_y = std::max(0, std::min(a_y, (int) m_sEmbedVideoData.height - (int) m_sEmbedVideoData.logo_h));
  1330. char tName[256];
  1331. snprintf(tName, sizeof(tName), "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5);
  1332. cv::Mat T = cv::imread(tName, cv::IMREAD_UNCHANGED);
  1333. if (T.cols > 0 && T.rows > 0)
  1334. {
  1335. cv::Mat rgb, mask;
  1336. if (T.channels() == 4)
  1337. {
  1338. vector<cv::Mat> layers;
  1339. cv::split(T, layers);
  1340. mask = layers[3]; // png's alpha channel used as mask
  1341. layers.pop_back();
  1342. cv::merge(layers, rgb);
  1343. }
  1344. else
  1345. {
  1346. // No alpha channel: the whole picture is drawn
  1347. if (T.channels() == 1)
  1348. {
  1349. cv::cvtColor(T, rgb, cv::COLOR_GRAY2BGR);
  1350. }
  1351. else
  1352. {
  1353. rgb = T;
  1354. }
  1355. mask = cv::Mat(T.rows, T.cols, CV_8UC1, cv::Scalar(255));
  1356. }
  1357. if (rgb.depth() != CV_8U)
  1358. {
  1359. rgb.convertTo(rgb, CV_8U, 1.0 / 257.0);
  1360. mask.convertTo(mask, CV_8U, 1.0 / 257.0);
  1361. }
  1362. cv::resize(rgb, m_mLogoPic, cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h), 0, 0, cv::INTER_CUBIC);
  1363. cv::resize(mask, m_mLogoMask, cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h), 0, 0, cv::INTER_CUBIC);
  1364. m_nLogoX = a_x;
  1365. m_nLogoY = a_y;
  1366. m_bLogoActive = true;
  1367. cv::Mat destinationROI = tMat(cv::Rect(m_nLogoX, m_nLogoY, m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h));
  1368. m_mLogoPic.copyTo(destinationROI, m_mLogoMask);
  1369. }
  1370. else
  1371. {
  1372. LOG_ERROR("Logo Disabled - No Picture is Loaded");
  1373. };
  1374. };
  1375. try
  1376. {
  1377. vector<int> compression_params;
  1378. // compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
  1379. // compression_params.push_back(9);
  1380. compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
  1381. compression_params.push_back(m_sEmbedVideoData.compression_quality);
  1382. vector<uchar> buf;
  1383. cv::imencode(".jpg", tMat, buf, compression_params);
  1384. m_nNoSignalSize = buf.size();
  1385. n_pNoSignalData = (uint8_t *) malloc(m_nNoSignalSize);
  1386. if (n_pNoSignalData)
  1387. {
  1388. memcpy(n_pNoSignalData, buf.data(), m_nNoSignalSize);
  1389. }
  1390. else
  1391. {
  1392. m_nNoSignalSize = 0;
  1393. }
  1394. buf.clear();
  1395. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  1396. // Make Small Preview Thumb
  1397. cv::Mat dst;
  1398. cv::resize(tMat, dst, cv::Size(PREVIEW_SIZE_WIDTH, PREVIEW_SIZE_HEIGHT));
  1399. cv::imencode(".jpg", dst, buf, compression_params);
  1400. m_nNoSignalSmallSize = buf.size();
  1401. n_pNoSignalSmallData = (uint8_t *) malloc(m_nNoSignalSmallSize);
  1402. if (n_pNoSignalSmallData)
  1403. {
  1404. memcpy(n_pNoSignalSmallData, buf.data(), m_nNoSignalSmallSize);
  1405. }
  1406. else
  1407. {
  1408. m_nNoSignalSmallSize = 0;
  1409. }
  1410. buf.clear();
  1411. #endif
  1412. }
  1413. catch (cv::Exception & e)
  1414. {
  1415. LOG_ERROR(e.msg.c_str());
  1416. };
  1417. #ifndef EVE_WORK_WITH_MEDIAMTX
  1418. // Socket Live
  1419. struct sockaddr sa;
  1420. struct sockaddr_in * sin;
  1421. memset(&sa, 0, sizeof(struct sockaddr));
  1422. sa.sa_family = AF_INET;
  1423. sin = (struct sockaddr_in *) &sa;
  1424. sin->sin_addr.s_addr = htonl(INADDR_ANY);
  1425. sin->sin_port = htons(m_sEmbedVideoData.port);
  1426. m_nSocketDescriptor = socket(sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
  1427. if (m_nSocketDescriptor == -1)
  1428. {
  1429. LOG_ERROR("Failed creating ACC socket");
  1430. return false;
  1431. };
  1432. ENABLE_SOCKOPT(m_nSocketDescriptor, SOL_SOCKET, SO_REUSEADDR);
  1433. #ifdef SO_REUSEPORT
  1434. ENABLE_SOCKOPT(m_nSocketDescriptor, SOL_SOCKET, SO_REUSEPORT);
  1435. #endif
  1436. if (bind(m_nSocketDescriptor, &sa, sizeof(sa)) < 0)
  1437. {
  1438. LOG_ERROR("Failed binding Socket");
  1439. close(m_nSocketDescriptor);
  1440. return false;
  1441. #ifdef DEBUG_EXTRA
  1442. }
  1443. else
  1444. {
  1445. LOG("Bind Socessfull");
  1446. #endif
  1447. };
  1448. if (listen(m_nSocketDescriptor, 10) != 0)
  1449. {
  1450. LOG_ERROR("Failed setting web listen backlog");
  1451. close(m_nSocketDescriptor);
  1452. return false;
  1453. #ifdef DEBUG_EXTRA
  1454. }
  1455. else
  1456. {
  1457. LOG("Listen OK");
  1458. #endif
  1459. };
  1460. // Socket Viewer
  1461. // struct sockaddr sa;
  1462. // struct sockaddr_in *sin;
  1463. memset(&sa, 0, sizeof(struct sockaddr));
  1464. sa.sa_family = AF_INET;
  1465. sin = (struct sockaddr_in *) &sa;
  1466. sin->sin_addr.s_addr = htonl(INADDR_ANY);
  1467. sin->sin_port = htons(8974);
  1468. m_nViewSocketDescriptor = socket(sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
  1469. if (m_nViewSocketDescriptor == -1)
  1470. {
  1471. LOG_ERROR("Failed creating ACC socket");
  1472. return false;
  1473. };
  1474. ENABLE_SOCKOPT(m_nViewSocketDescriptor, SOL_SOCKET, SO_REUSEADDR);
  1475. #ifdef SO_REUSEPORT
  1476. ENABLE_SOCKOPT(m_nViewSocketDescriptor, SOL_SOCKET, SO_REUSEPORT);
  1477. #endif
  1478. if (bind(m_nViewSocketDescriptor, &sa, sizeof(sa)) < 0)
  1479. {
  1480. LOG_ERROR("Failed binding Socket");
  1481. close(m_nViewSocketDescriptor);
  1482. return false;
  1483. #ifdef DEBUG_EXTRA
  1484. }
  1485. else
  1486. {
  1487. LOG("Bind Socessfull");
  1488. #endif
  1489. };
  1490. if (listen(m_nViewSocketDescriptor, 10) != 0)
  1491. {
  1492. LOG_ERROR("Failed setting web listen backlog");
  1493. close(m_nViewSocketDescriptor);
  1494. return false;
  1495. #ifdef DEBUG_EXTRA
  1496. }
  1497. else
  1498. {
  1499. LOG("Listen OK");
  1500. #endif
  1501. };
  1502. #ifdef DEBUG_EXTRA
  1503. LOG("Start Thread");
  1504. #endif
  1505. m_bEnabled = init();
  1506. #else // !EVE_WORK_WITH_MEDIAMTX
  1507. m_bEnabled = true;
  1508. #endif // !EVE_WORK_WITH_MEDIAMTX
  1509. applyLiveSettings();
  1510. startLive(); // a failure (port in use) is retried by validate()
  1511. return m_bEnabled;
  1512. }
  1513. bool kernelModuleEmbedVideo::stopService(bool force)
  1514. {
  1515. m_bEnabled = false;
  1516. #ifdef DEBUG_EXTRA
  1517. LOG("StopService!");
  1518. #endif
  1519. #ifndef EVE_WORK_WITH_MEDIAMTX
  1520. // Stop Conveyor
  1521. m_bDetached = uninit();
  1522. #else
  1523. m_bDetached = true; // no picture server to detach: stopping always succeeds (reloadSettings() depends on it)
  1524. #endif
  1525. m_mStreamHandlersProtector.lock();
  1526. if (m_mapHANDLERS.size())
  1527. {
  1528. #ifdef DEBUG
  1529. LOG("Deleting Stream Handlers!");
  1530. #endif
  1531. for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++)
  1532. {
  1533. #ifdef DEBUG
  1534. LOG("Deleting Stream Handler");
  1535. #endif
  1536. delete((*idx).second);
  1537. }
  1538. m_mapHANDLERS.clear();
  1539. };
  1540. m_mStreamHandlersProtector.unlock();
  1541. stopLive(); // after the handlers: they released their feeds
  1542. #ifndef EVE_WORK_WITH_MEDIAMTX
  1543. if (m_vHandlersPool.size())
  1544. { // Kill-Wait Started Probes
  1545. #ifdef DEBUG_EXTRA
  1546. char tB[200];
  1547. sprintf(tB, "Have to Wait Client Handlers[%d]!", m_vHandlersPool.size());
  1548. LOG(tB);
  1549. #endif
  1550. for (vectorHANDLERS::iterator index = m_vHandlersPool.begin(); index != m_vHandlersPool.end(); index++)
  1551. {
  1552. if (force)
  1553. {
  1554. #ifdef DEBUG_EXTRA
  1555. LOG("Force Client Handler to Stop!");
  1556. #endif
  1557. pthread_cancel((*index)->m_hThreadHandle);
  1558. }
  1559. else
  1560. {
  1561. #ifdef DEBUG_EXTRA
  1562. LOG("Waiting Client Handler to End!");
  1563. #endif
  1564. pthread_join((*index)->m_hThreadHandle, NULL);
  1565. }
  1566. };
  1567. m_vHandlersPool.clear();
  1568. };
  1569. #ifdef DEBUG_EXTRA
  1570. LOG("No More Client Handlers!");
  1571. #endif
  1572. if (m_mapCAPTURES.size())
  1573. {
  1574. #ifdef DEBUG_EXTRA
  1575. LOG("Some Caps!");
  1576. #endif
  1577. for (mapSTREAMCAPS::iterator idx = m_mapCAPTURES.begin(); idx != m_mapCAPTURES.end(); idx++)
  1578. {
  1579. idx->second.clear();
  1580. }
  1581. m_mapCAPTURES.clear();
  1582. #ifdef DEBUG_EXTRA
  1583. LOG("No More Caps!");
  1584. #endif
  1585. };
  1586. #endif // !EVE_WORK_WITH_MEDIAMTX
  1587. if (m_mapMATS.size())
  1588. {
  1589. #ifdef DEBUG_EXTRA
  1590. LOG("Some Mats!");
  1591. #endif
  1592. for (mapSTREAMMAPS::iterator idx = m_mapMATS.begin(); idx != m_mapMATS.end(); idx++)
  1593. {
  1594. #ifdef DEBUG_EXTRA
  1595. LOG("Release Mat!");
  1596. #endif
  1597. idx->second.release();
  1598. };
  1599. #ifdef DEBUG_EXTRA
  1600. LOG("Clear Map Mats!");
  1601. #endif
  1602. m_mapMATS.clear();
  1603. #ifdef DEBUG_EXTRA
  1604. LOG("No More Mats!");
  1605. #endif
  1606. };
  1607. if (n_pNoSignalData)
  1608. {
  1609. #ifdef DEBUG_EXTRA
  1610. LOG("No Sig Data!");
  1611. #endif
  1612. free(n_pNoSignalData);
  1613. n_pNoSignalData = NULL;
  1614. m_nNoSignalSize = 0;
  1615. #ifdef DEBUG_EXTRA
  1616. LOG("No No Sig Data!");
  1617. #endif
  1618. };
  1619. return m_bDetached;
  1620. }
  1621. bool kernelModuleEmbedVideo::reloadSettings()
  1622. {
  1623. #ifdef DEBUG_EXTRA
  1624. LOG("Reload Service");
  1625. #endif
  1626. if (m_pDBC)
  1627. {
  1628. DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
  1629. if (tDATA)
  1630. {
  1631. if (stopService())
  1632. {
  1633. #ifdef DEBUG_EXTRA
  1634. LOG("Stoped!");
  1635. #endif
  1636. memcpy(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  1637. free(tDATA);
  1638. #ifdef DEBUG_EXTRA
  1639. LOG("Starting...");
  1640. #endif
  1641. return startService();
  1642. }
  1643. else
  1644. {
  1645. free(tDATA);
  1646. };
  1647. };
  1648. };
  1649. return false;
  1650. }
  1651. #include "deviceNodeCamera.h"
  1652. #include "deviceNodeFactory.h"
  1653. bool kernelModuleEmbedVideo::validate()
  1654. {
  1655. bool RetVal = true;
  1656. // Settings changed in the DB (Mojo_Web module settings): restart the service with them.
  1657. // m_sEmbedVideoData must stay equal to the DB row, runtime state lives in other members.
  1658. if (m_pDBC)
  1659. {
  1660. DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
  1661. if (tDATA)
  1662. {
  1663. bool tChanged = (memcmp(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA)) != 0);
  1664. free(tDATA);
  1665. if (tChanged)
  1666. {
  1667. LOG("Module Settings Changed, reloading");
  1668. return reloadSettings();
  1669. };
  1670. };
  1671. };
  1672. sweepClips(); // old recorded segments, alarms whose clip is overdue
  1673. // The WebRTC server could not start (e.g. the port was still used by the MediaMTX service): retry
  1674. if (m_bEnabled && m_sEmbedVideoData.live_mode != EVE_LIVE_OFF && ! m_pWebrtc && time(NULL) >= m_tLiveRetry)
  1675. {
  1676. startLive();
  1677. };
  1678. deviceNode ** tA = NULL;
  1679. uint32_t tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_STATIC_CAMERA, tA);
  1680. if( tCNT ){
  1681. for (uint32_t i = 0; i < tCNT; i++){
  1682. deviceNodeCamera * tCAM = (deviceNodeCamera *) tA[i];
  1683. if (! registerStream(tCAM->getCamStream(), tCAM->getFlipMode())){
  1684. RetVal = false;
  1685. // tCAM->set
  1686. continue;
  1687. };
  1688. };
  1689. free(tA);
  1690. tA = NULL;
  1691. #ifdef DEBUG_EXTRA
  1692. } else {
  1693. LOG("No Static Camera Defined/Enabled In the system!");
  1694. #endif
  1695. };
  1696. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_PTZ_CAMERA, tA);
  1697. if( tCNT ){
  1698. for (uint32_t i = 0; i < tCNT; i++){
  1699. deviceNodeCamera * tCAM = (deviceNodeCamera *) tA[i];
  1700. if (! registerStream(tCAM->getCamStream(), tCAM->getFlipMode())){
  1701. RetVal = false;
  1702. // tCAM->set
  1703. continue;
  1704. };
  1705. };
  1706. free(tA);
  1707. tA = NULL;
  1708. #ifdef DEBUG_EXTRA
  1709. } else {
  1710. LOG("No Analog Static Camera Defined/Enabled In the system!");
  1711. #endif
  1712. };
  1713. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_STATIC_CAMERA, tA);
  1714. if( tCNT ){
  1715. for (uint32_t i = 0; i < tCNT; i++){
  1716. deviceNodeCamera * tCAM = (deviceNodeCamera *) tA[i];
  1717. if (! registerStream(tCAM->getCamStream(), tCAM->getFlipMode())){
  1718. RetVal = false;
  1719. // tCAM->set
  1720. continue;
  1721. };
  1722. };
  1723. free(tA);
  1724. tA = NULL;
  1725. #ifdef DEBUG_EXTRA
  1726. } else {
  1727. LOG("No IP Static Camera Defined/Enabled In the system!");
  1728. #endif
  1729. };
  1730. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_PTZ_CAMERA, tA);
  1731. if( tCNT ){
  1732. for (uint32_t i = 0; i < tCNT; i++){
  1733. deviceNodeCamera * tCAM = (deviceNodeCamera *) tA[i];
  1734. #ifdef DEBUG_EXTRA
  1735. LOG("Registering Stream!");
  1736. #endif
  1737. if (! registerStream(tCAM->getCamStream(), tCAM->getFlipMode())){
  1738. RetVal = false;
  1739. // tCAM->set
  1740. continue;
  1741. };
  1742. };
  1743. free(tA);
  1744. tA = NULL;
  1745. #ifdef DEBUG_EXTRA
  1746. } else {
  1747. LOG("No IP PTZ Camera Defined/Enabled In the system!");
  1748. #endif
  1749. };
  1750. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_PRESET_CAMERA, tA);
  1751. if( tCNT ){
  1752. for (uint32_t i = 0; i < tCNT; i++){
  1753. deviceNodeCamera * tCAM = (deviceNodeCamera *) tA[i];
  1754. if (! registerStream(tCAM->getCamStream(), tCAM->getFlipMode())){
  1755. RetVal = false;
  1756. // tCAM->set
  1757. continue;
  1758. };
  1759. };
  1760. free(tA);
  1761. tA = NULL;
  1762. #ifdef DEBUG_EXTRA
  1763. } else {
  1764. LOG("No IP Preset Camera Defined/Enabled In the system!");
  1765. #endif
  1766. };
  1767. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_PRESET_CAMERA, tA);
  1768. if( tCNT ){
  1769. for (uint32_t i = 0; i < tCNT; i++){
  1770. deviceNodeCamera * tCAM = (deviceNodeCamera *) tA[i];
  1771. if (! registerStream(tCAM->getCamStream(), tCAM->getFlipMode())){
  1772. RetVal = false;
  1773. // tCAM->set
  1774. continue;
  1775. };
  1776. };
  1777. free(tA);
  1778. tA = NULL;
  1779. #ifdef DEBUG_EXTRA
  1780. } else {
  1781. LOG("No Analog Preset Camera Defined/Enabled In the system!");
  1782. #endif
  1783. };
  1784. if (RetVal){
  1785. DBC_INT_LIST tInvalidList;
  1786. m_mStreamHandlersProtector.lock();
  1787. for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++) {
  1788. if ((*idx).second != NULL){
  1789. if (! (*idx).second->validate()){
  1790. tInvalidList.push_back((*idx).first);
  1791. };
  1792. };
  1793. };
  1794. m_mStreamHandlersProtector.unlock();
  1795. if (tInvalidList.size()){
  1796. for (DBC_INT_LIST::iterator idx = tInvalidList.begin(); idx != tInvalidList.end(); idx++){
  1797. if (unregisterStream(*idx)){
  1798. registerStream(*idx);
  1799. };
  1800. };
  1801. tInvalidList.clear();
  1802. };
  1803. };
  1804. return RetVal;
  1805. if (m_pDBC)
  1806. {
  1807. DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
  1808. if (tDATA)
  1809. {
  1810. if (memcmp(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA)) != 0)
  1811. {
  1812. free(tDATA);
  1813. #ifdef DEBUG_EXTRA
  1814. LOG("Module Settings Changed!");
  1815. #endif
  1816. return reloadSettings();
  1817. };
  1818. free(tDATA);
  1819. }
  1820. else
  1821. {
  1822. LOG_ERROR("No Fetched EVE Data");
  1823. RetVal = false;
  1824. };
  1825. if (RetVal)
  1826. {
  1827. DBC_INT_LIST * tList = m_pDBC->listMediaStreams(0, LIST_VALID_RECORDS);
  1828. if (tList != NULL)
  1829. {
  1830. for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
  1831. {
  1832. #ifdef DEBUG_EXTRA
  1833. LOG("REG STREAM");
  1834. #endif
  1835. if (! registerStream(*idx))
  1836. {
  1837. RetVal = false;
  1838. continue;
  1839. };
  1840. };
  1841. delete(tList);
  1842. #ifdef DEBUG_EXTRA
  1843. }
  1844. else
  1845. {
  1846. LOG("- No Valid Streams -");
  1847. #endif
  1848. };
  1849. };
  1850. if (RetVal)
  1851. {
  1852. DBC_INT_LIST tInvalidList;
  1853. m_mStreamHandlersProtector.lock();
  1854. for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++)
  1855. {
  1856. if ((*idx).second != NULL)
  1857. {
  1858. if (! (*idx).second->validate())
  1859. {
  1860. tInvalidList.push_back((*idx).first);
  1861. }
  1862. }
  1863. };
  1864. m_mStreamHandlersProtector.unlock();
  1865. if (tInvalidList.size())
  1866. {
  1867. for (DBC_INT_LIST::iterator idx = tInvalidList.begin(); idx != tInvalidList.end(); idx++)
  1868. {
  1869. if (unregisterStream(*idx))
  1870. {
  1871. registerStream(*idx);
  1872. }
  1873. };
  1874. tInvalidList.clear();
  1875. };
  1876. };
  1877. }
  1878. else
  1879. {
  1880. LOG_ERROR("DB Error!");
  1881. RetVal = false;
  1882. };
  1883. return RetVal;
  1884. }
  1885. #ifndef EVE_WORK_WITH_MEDIAMTX
  1886. bool kernelModuleEmbedVideo::registerForMediaStream(uint32_t id, WEB_CLIENT_CTX *& CTX)
  1887. {
  1888. if (CTX)
  1889. {
  1890. if (id)
  1891. {
  1892. // Check if its Valid!
  1893. DBC_MEDIA_STREAM_DATA * tSD = m_pDBC->getMediaStreamData(id);
  1894. if (tSD)
  1895. {
  1896. // if( !tSD->valid ) {
  1897. // free( tSD );
  1898. // return false;
  1899. // };
  1900. free(tSD);
  1901. // if( pipe( CTX->m_iEncoderPipe ) == -1 ){
  1902. // CTX->bValid = false;
  1903. // return false;
  1904. // };
  1905. CTX->mediaStreamId = id;
  1906. return true;
  1907. };
  1908. };
  1909. }
  1910. else
  1911. {
  1912. LOG_ERROR("Client Context error!");
  1913. }
  1914. return false;
  1915. }
  1916. #endif // !EVE_WORK_WITH_MEDIAMTX
  1917. bool kernelModuleEmbedVideo::dispatchEvent(EVENT_CONTAINER * Evt)
  1918. {
  1919. if (Evt)
  1920. {
  1921. #ifdef DEBUG_EXTRA
  1922. LOG("Dispatch EVT");
  1923. #endif
  1924. switch (Evt->m_eClass)
  1925. {
  1926. case EVT_CLASS_IMAGE:
  1927. case EVT_CLASS_VIDEO:
  1928. {
  1929. switch (Evt->m_eType)
  1930. {
  1931. case EVT_CAPTURE_START:
  1932. {
  1933. #ifdef DEBUG
  1934. LOG("Capture Event");
  1935. #endif
  1936. if (Evt->m_ref_id && Evt->m_db_id)
  1937. {
  1938. uint32_t tPicID = saveStreamPicture(Evt->m_ref_id);
  1939. if (tPicID)
  1940. {
  1941. //
  1942. // m_pDBC->addPictureEvent( Evt->m_db_id, tPicID );
  1943. }
  1944. else
  1945. {
  1946. #ifdef DEBUG
  1947. LOG_ERROR("No Picture is Saved!");
  1948. #endif
  1949. };
  1950. }
  1951. else
  1952. {
  1953. #ifdef DEBUG
  1954. LOG_ERROR("No Stream ID or Event ID in Event Data!");
  1955. #endif
  1956. };
  1957. break;
  1958. };
  1959. default: break;
  1960. };
  1961. break;
  1962. };
  1963. case EVT_CLASS_CAMERA_CTRL:
  1964. {
  1965. switch (Evt->m_eType)
  1966. {
  1967. case EVT_CAMERA_MOVE_TO:
  1968. {
  1969. #ifdef DEBUG_EXTRA
  1970. LOG("Cam OnMove After Event -> Register SourceID");
  1971. #endif
  1972. uint32_t sid = m_pDBC->getNodeAuxParam(Evt->m_nSourceNodeID);
  1973. if (sid)
  1974. {
  1975. #ifdef DEBUG_EXTRA
  1976. LOG("Source ID found!");
  1977. #endif
  1978. m_mStreamHandlersProtector.lock();
  1979. #ifdef DEBUG_EXTRA
  1980. LOG("Mutex Locked!");
  1981. #endif
  1982. mapSTREAMHANDLERS::iterator hnd = m_mapHANDLERS.find(sid);
  1983. if (hnd != m_mapHANDLERS.end())
  1984. #ifdef DEBUG_EXTRA
  1985. {
  1986. LOG("Handler found!");
  1987. #endif
  1988. (*hnd).second->triggerCamMove();
  1989. #ifdef DEBUG_EXTRA
  1990. LOG("Triggered!");
  1991. }
  1992. else
  1993. {
  1994. LOG("No Stream Found!");
  1995. }
  1996. #endif
  1997. m_mStreamHandlersProtector.unlock();
  1998. #ifdef DEBUG_EXTRA
  1999. LOG("Mutex Unlocked!");
  2000. #endif
  2001. }
  2002. else
  2003. {
  2004. LOG("No Stream ID Found!");
  2005. }
  2006. break;
  2007. };
  2008. case EVT_CAMERA_ON_POSITION:
  2009. {
  2010. #ifdef DEBUG_EXTRA
  2011. LOG("Cam On Position After Event -> Capture Picture");
  2012. #endif
  2013. DBC_SNAPSHOT_DATA * tData = (DBC_SNAPSHOT_DATA *) Evt->m_pEvtData;
  2014. if (tData)
  2015. {
  2016. if (! saveStreamPicture(tData))
  2017. {
  2018. LOG_ERROR("Save Error!");
  2019. }
  2020. tData->assoc_event_ids.clear();
  2021. //free(tData);
  2022. delete( tData );
  2023. Evt->m_pEvtData = NULL;
  2024. #ifdef DEBUG_EXTRA
  2025. LOG("Event Data Saved!");
  2026. #endif
  2027. }
  2028. else
  2029. {
  2030. LOG_ERROR("No Evt Data!");
  2031. }
  2032. break;
  2033. };
  2034. default: break;
  2035. };
  2036. break;
  2037. };
  2038. default: break;
  2039. };
  2040. return true;
  2041. };
  2042. return false;
  2043. }
  2044. // https://www.ps2-home.com/forum/viewtopic.php?t=98
  2045. // https://www.mikekohn.net/software/playstation2.php
  2046. bool kernelModuleEmbedVideo::saveStreamPicture(DBC_SNAPSHOT_DATA * ptr)
  2047. {
  2048. if (ptr == NULL)
  2049. {
  2050. return false;
  2051. }
  2052. if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES) == EVE_OUTPUT_PICTURES)
  2053. {
  2054. #ifdef DEBUG_EXTRA
  2055. LOG("Saving Picture!");
  2056. #endif
  2057. // saveStreamPicture( ptr->stream_id );
  2058. //*
  2059. if (ptr->assoc_event_ids.size())
  2060. {
  2061. #ifndef EVE_WORK_WITH_MEDIAMTX
  2062. if (m_mapCAPTURES.find(ptr->stream_id) != m_mapCAPTURES.end())
  2063. {
  2064. m_mPictureProtector.lock();
  2065. ptr->data = (uint8_t *) malloc(m_mapCAPTURES[ptr->stream_id].size());
  2066. if (ptr->data)
  2067. {
  2068. ptr->data_size = m_mapCAPTURES[ptr->stream_id].size();
  2069. memcpy(ptr->data, m_mapCAPTURES[ptr->stream_id].data(), ptr->data_size);
  2070. m_mPictureProtector.unlock();
  2071. CSTR MD5 = MD5Binary(ptr->data, ptr->data_size);
  2072. if (MD5)
  2073. {
  2074. strncpy(ptr->md5, MD5, 32);
  2075. free(MD5);
  2076. if (! m_pDBC->addPicture(ptr, "jpg"))
  2077. {
  2078. LOG_ERROR("Database Error to consume capture!");
  2079. }
  2080. }
  2081. else
  2082. {
  2083. LOG_ERROR("no MD5 Hash!");
  2084. }
  2085. free(ptr->data);
  2086. }
  2087. else
  2088. {
  2089. m_mPictureProtector.unlock();
  2090. LOG_ERROR("No memory to allocate!");
  2091. };
  2092. #ifdef DEBUG_EXTRA
  2093. }
  2094. else
  2095. {
  2096. LOG("No apropriate Stream Found!");
  2097. #endif
  2098. };
  2099. #endif // !EVE_WORK_WITH_MEDIAMTX
  2100. };
  2101. //*/
  2102. #ifdef DEBUG_EXTRA
  2103. }
  2104. else
  2105. {
  2106. LOG("No Pic Mode!");
  2107. #endif
  2108. }
  2109. if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO)
  2110. {
  2111. #ifdef DEBUG
  2112. LOG("Saving Video!");
  2113. #endif
  2114. if (ptr->assoc_event_ids.size() && liveClipsActive())
  2115. {
  2116. for (DBC_INT_LIST::iterator idx = ptr->assoc_event_ids.begin(); idx < ptr->assoc_event_ids.end(); idx++)
  2117. {
  2118. clipAlarm(ptr->stream_id, *idx);
  2119. };
  2120. }
  2121. else if (ptr->assoc_event_ids.size())
  2122. {
  2123. m_mStreamHandlersProtector.lock();
  2124. mapSTREAMHANDLERS::iterator hnd = m_mapHANDLERS.find(ptr->stream_id);
  2125. if (hnd != m_mapHANDLERS.end())
  2126. {
  2127. uint32_t db_id;
  2128. uint32_t cfrm;
  2129. for (DBC_INT_LIST::iterator idx = ptr->assoc_event_ids.begin(); idx < ptr->assoc_event_ids.end(); idx++)
  2130. {
  2131. (*hnd).second->triggerEvent(*idx, db_id, cfrm);
  2132. }
  2133. #ifdef DEBUG
  2134. }
  2135. else
  2136. {
  2137. LOG("No Stream Found!");
  2138. #endif
  2139. };
  2140. m_mStreamHandlersProtector.unlock();
  2141. #ifdef DEBUG_EXTRA
  2142. }
  2143. else
  2144. {
  2145. LOG("No Assoc EVTIDS!");
  2146. #endif
  2147. };
  2148. #ifdef DEBUG
  2149. }
  2150. else
  2151. {
  2152. LOG("No Video Mode!");
  2153. #endif
  2154. };
  2155. return true;
  2156. }
  2157. bool kernelModuleEmbedVideo::saveStreamPicture(uint32_t id)
  2158. {
  2159. if (! id)
  2160. {
  2161. return false;
  2162. }
  2163. if (m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES)
  2164. {
  2165. #ifndef EVE_WORK_WITH_MEDIAMTX
  2166. if (m_mapCAPTURES.find(id) != m_mapCAPTURES.end())
  2167. {
  2168. //FILE * tFD;
  2169. m_mPictureProtector.lock();
  2170. size_t tBufferSize = m_mapCAPTURES[id].size();
  2171. uint8_t * tBuffer = (uint8_t *) malloc(tBufferSize);
  2172. if (tBuffer)
  2173. {
  2174. memcpy(tBuffer, m_mapCAPTURES[id].data(), tBufferSize);
  2175. m_mPictureProtector.unlock();
  2176. CSTR MD5 = MD5Binary(tBuffer, tBufferSize);
  2177. if (MD5)
  2178. {
  2179. char tFileName[MAX_PATH];
  2180. if (generateSavePicPath(MD5, tFileName))
  2181. {
  2182. FILE * fd = fopen(tFileName, "wb+");
  2183. if (fd)
  2184. {
  2185. fwrite(tBuffer, 1, tBufferSize, fd);
  2186. fclose(fd);
  2187. ch_chown(tFileName, "nobody", "nogroup");
  2188. // retVal = m_pDBC->addPicture( id, MD5, "jpg" );
  2189. }
  2190. else
  2191. {
  2192. LOG_ERROR("Can not open picture file!");
  2193. }
  2194. }
  2195. else
  2196. {
  2197. LOG_ERROR("Can not generate file name!");
  2198. }
  2199. free(MD5);
  2200. }
  2201. else
  2202. {
  2203. LOG_ERROR("no MD5 Hash!");
  2204. }
  2205. free(tBuffer);
  2206. }
  2207. else
  2208. {
  2209. m_mPictureProtector.unlock();
  2210. LOG_ERROR("no memory to allocate!");
  2211. };
  2212. };
  2213. #endif // !EVE_WORK_WITH_MEDIAMTX
  2214. }
  2215. return true;
  2216. }
  2217. int kernelModuleEmbedVideo::getWidh(void)
  2218. {
  2219. if (m_sEmbedVideoData.width)
  2220. {
  2221. return m_sEmbedVideoData.width;
  2222. }
  2223. return -1;
  2224. }
  2225. int kernelModuleEmbedVideo::getHeight(void)
  2226. {
  2227. if (m_sEmbedVideoData.height)
  2228. {
  2229. return m_sEmbedVideoData.height;
  2230. }
  2231. return -1;
  2232. }
  2233. bool kernelModuleEmbedVideo::generateSavePicPath(CSTR MD5, CSTR Output)
  2234. {
  2235. if (MD5 == NULL)
  2236. {
  2237. return false;
  2238. }
  2239. if (Output == NULL)
  2240. {
  2241. return false;
  2242. }
  2243. if (! fileExist(m_pBaseSavePath))
  2244. {
  2245. LOG_ERROR("No Base Picture Save Path Defined!");
  2246. return false;
  2247. };
  2248. if (strlen(MD5) < 4)
  2249. {
  2250. return false;
  2251. }
  2252. char test[5000];
  2253. sprintf(test, "%s/%c%c", m_pBaseSavePath, MD5[0], MD5[1]);
  2254. if (! fileExist(test))
  2255. {
  2256. #ifdef __linux__
  2257. mkdir(test, STORAGE_DIR_MODE);
  2258. #else
  2259. _mkdir(test);
  2260. #endif
  2261. ch_chown(test, "nobody", "nogroup");
  2262. };
  2263. sprintf(test, "%s/%c%c/%c%c", m_pBaseSavePath, MD5[0], MD5[1], MD5[2], MD5[3]);
  2264. if (! fileExist(test))
  2265. {
  2266. #ifdef __linux__
  2267. mkdir(test, STORAGE_DIR_MODE);
  2268. #else
  2269. _mkdir(test);
  2270. #endif
  2271. ch_chown(test, "nobody", "nogroup");
  2272. };
  2273. sprintf(Output, "%s/%c%c/%c%c/%s.jpg", m_pBaseSavePath, MD5[0], MD5[1], MD5[2], MD5[3], MD5);
  2274. return true;
  2275. }
  2276. bool kernelModuleEmbedVideo::registerStream(uint32_t streamID, uint8_t flip_mode)
  2277. {
  2278. // return true;
  2279. try
  2280. {
  2281. m_mStreamHandlersProtector.lock();
  2282. streamHandler * tData = m_mapHANDLERS.at(streamID);
  2283. // If SH allready exists
  2284. if (tData != NULL)
  2285. {
  2286. #ifdef DEBUG_EXTRA
  2287. LOG("Stream Exists!");
  2288. #endif
  2289. m_mStreamHandlersProtector.unlock();
  2290. return true;
  2291. };
  2292. #ifdef DEBUG_EXTRA
  2293. LOG( "Create New Stream Handler!" );
  2294. #endif
  2295. // Alarm clips: from the recorder segments when it records (liveClipsActive), else the old encoder
  2296. if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) != EVE_OUTPUT_VIDEO || liveClipsActive())
  2297. {
  2298. tData = new streamHandler(this, MS_MODE_DECODE_FRAMES, flip_mode);
  2299. }
  2300. else
  2301. {
  2302. tData = new streamHandler(this, MS_MODE_ENCODE_FRAMES, flip_mode);
  2303. };
  2304. if (tData != NULL)
  2305. {
  2306. if (tData->create(m_pDBC, streamID))
  2307. {
  2308. m_mapHANDLERS[streamID] = tData;
  2309. m_mStreamHandlersProtector.unlock();
  2310. return true;
  2311. }
  2312. else
  2313. {
  2314. LOG_ERROR("Error Creating new Stream Handler[t]!");
  2315. delete(tData);
  2316. m_mapHANDLERS[streamID] = NULL;
  2317. };
  2318. }
  2319. else
  2320. {
  2321. LOG_ERROR("Error Constructing new Stream Handler[t]!");
  2322. };
  2323. m_mStreamHandlersProtector.unlock();
  2324. }
  2325. catch (const std::out_of_range & oor)
  2326. {
  2327. #ifdef DEBUG_EXTRA
  2328. LOG("Exception Catched - Stream Handler not found in collection!");
  2329. #endif
  2330. streamHandler * tSH = NULL;
  2331. // Create New One
  2332. if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) != EVE_OUTPUT_VIDEO || liveClipsActive())
  2333. {
  2334. #ifdef DEBUG_EXTRA
  2335. LOG("New 1");
  2336. #endif
  2337. tSH = new streamHandler(this, MS_MODE_DECODE_FRAMES, flip_mode);
  2338. }
  2339. else
  2340. {
  2341. #ifdef DEBUG_EXTRA
  2342. LOG("New 2");
  2343. #endif
  2344. tSH = new streamHandler(this, MS_MODE_ENCODE_FRAMES, flip_mode);
  2345. #ifdef DEBUG_EXTRA
  2346. LOG("A C");
  2347. #endif
  2348. };
  2349. if (tSH != NULL)
  2350. {
  2351. #ifdef DEBUG_EXTRA
  2352. LOG("Constructed Ok");
  2353. #endif
  2354. if (tSH->create(m_pDBC, streamID))
  2355. {
  2356. #ifdef DEBUG_EXTRA
  2357. LOG("Put to map");
  2358. #endif
  2359. m_mapHANDLERS[streamID] = tSH;
  2360. m_mStreamHandlersProtector.unlock();
  2361. return true;
  2362. }
  2363. else
  2364. {
  2365. LOG_ERROR("Error Creating new Stream Handler[c]!");
  2366. delete(tSH);
  2367. m_mapHANDLERS[streamID] = NULL;
  2368. };
  2369. }
  2370. else
  2371. {
  2372. LOG_ERROR("Error Constructing new Stream Handler[c]!");
  2373. };
  2374. m_mStreamHandlersProtector.unlock();
  2375. };
  2376. return false;
  2377. }
  2378. bool kernelModuleEmbedVideo::unregisterStream(uint32_t streamID)
  2379. {
  2380. try
  2381. {
  2382. m_mStreamHandlersProtector.lock();
  2383. streamHandler * tData = m_mapHANDLERS.at(streamID);
  2384. if (tData != NULL)
  2385. {
  2386. delete(tData);
  2387. // ToDo: Remove Stream Mats and Clients Connected to a Stream ( or Push them No Signal Image )
  2388. m_mapHANDLERS[streamID] = NULL;
  2389. m_mStreamHandlersProtector.unlock();
  2390. return true;
  2391. };
  2392. m_mStreamHandlersProtector.unlock();
  2393. LOG_ERROR("Registered Stream is Empty!");
  2394. }
  2395. catch (const std::out_of_range & oor)
  2396. {
  2397. m_mStreamHandlersProtector.unlock();
  2398. LOG_ERROR("No Such Stream Registered!");
  2399. };
  2400. return false;
  2401. }
  2402. cv::Mat *kernelModuleEmbedVideo::getCurrentFrame( uint32_t streamID ){
  2403. cv::Mat *retVal = NULL;
  2404. return retVal;
  2405. }
  2406. /* ---------------------------------------------------------------- live view (WebRTC) and recording */
  2407. void kernelModuleEmbedVideo::applyLiveSettings()
  2408. {
  2409. m_sLiveConf = mtx::ServerConf();
  2410. m_sLiveConf.webrtcAddress = ":" + std::to_string(m_sEmbedVideoData.live_port ? m_sEmbedVideoData.live_port : 8889);
  2411. // next to the module's own clips; kernelHelperMediaMTX indexes <scratch>/stream_<node>/*.mp4
  2412. m_sLiveConf.recordPath = std::string(m_pVideoScratchPath) + "/%path/%Y-%m-%d_%H-%M-%S-%f";
  2413. m_sLiveConf.recordSegmentDurationMs = (int64_t) (m_sEmbedVideoData.live_record_segment ? m_sEmbedVideoData.live_record_segment : 60) * 1000;
  2414. m_sLiveConf.recordPartDurationMs = 1000;
  2415. m_sLiveConf.onSegmentDone = [this](const mtx::SegmentInfo & info) { onLiveSegment(info); };
  2416. m_sLiveConf.webrtcICEServers.clear();
  2417. std::string tIce(m_sEmbedVideoData.live_ice_servers);
  2418. size_t tPos = 0;
  2419. while (tPos < tIce.size())
  2420. {
  2421. size_t tEnd = tIce.find_first_of(", ", tPos);
  2422. std::string tOne = tIce.substr(tPos, tEnd == std::string::npos ? std::string::npos : tEnd - tPos);
  2423. if (! tOne.empty())
  2424. {
  2425. m_sLiveConf.webrtcICEServers.push_back(tOne);
  2426. };
  2427. if (tEnd == std::string::npos)
  2428. {
  2429. break;
  2430. };
  2431. tPos = tEnd + 1;
  2432. };
  2433. }
  2434. bool kernelModuleEmbedVideo::startLive()
  2435. {
  2436. if (m_sEmbedVideoData.live_mode == EVE_LIVE_OFF)
  2437. {
  2438. return true;
  2439. };
  2440. if (m_pWebrtc)
  2441. {
  2442. return true;
  2443. };
  2444. std::unique_ptr<mtx::WebrtcServer> tServer(new mtx::WebrtcServer(m_sLiveConf, this));
  2445. std::string tErr;
  2446. if (! tServer->start(tErr))
  2447. {
  2448. char tB[512];
  2449. snprintf(tB, sizeof(tB), "Live view: the WebRTC server cannot listen on %s (%s) - retry in 10 s", m_sLiveConf.webrtcAddress.c_str(), tErr.c_str());
  2450. LOG_ERROR(tB);
  2451. m_tLiveRetry = time(NULL) + 10;
  2452. return false;
  2453. };
  2454. m_pWebrtc = std::move(tServer);
  2455. char tB[256];
  2456. snprintf(tB, sizeof(tB), "Live view: WebRTC (WHEP) on %s, %s, recording %s", m_sLiveConf.webrtcAddress.c_str(),
  2457. m_sEmbedVideoData.live_mode == EVE_LIVE_ENCODE ? "processed frames (encoded)" : "camera H264 (passthrough)", m_sEmbedVideoData.live_record ? "on" : "off");
  2458. LOG(tB);
  2459. return true;
  2460. }
  2461. void kernelModuleEmbedVideo::stopLive()
  2462. {
  2463. if (m_pWebrtc)
  2464. {
  2465. m_pWebrtc->stop(); // closes the sessions (they hold their paths)
  2466. m_pWebrtc.reset();
  2467. };
  2468. std::map<std::string, std::shared_ptr<mtx::MediaPath>> tPaths;
  2469. {
  2470. std::lock_guard<std::mutex> lock(m_mLivePaths);
  2471. tPaths.swap(m_mapLivePaths);
  2472. }
  2473. for (auto & p : tPaths)
  2474. {
  2475. p.second->sourceSetNotReady(); // closes the recording
  2476. };
  2477. }
  2478. std::shared_ptr<mtx::MediaPath> kernelModuleEmbedVideo::livePath(uint32_t nodeID)
  2479. {
  2480. if (m_sEmbedVideoData.live_mode == EVE_LIVE_OFF || nodeID == 0)
  2481. {
  2482. return nullptr;
  2483. };
  2484. std::string tName = "stream_" + std::to_string(nodeID);
  2485. std::lock_guard<std::mutex> lock(m_mLivePaths);
  2486. auto it = m_mapLivePaths.find(tName);
  2487. if (it != m_mapLivePaths.end())
  2488. {
  2489. return it->second;
  2490. };
  2491. auto tPath = std::make_shared<mtx::MediaPath>(tName, m_sEmbedVideoData.live_record, m_sLiveConf);
  2492. m_mapLivePaths[tName] = tPath;
  2493. return tPath;
  2494. }
  2495. std::shared_ptr<mtx::MediaPath> kernelModuleEmbedVideo::findPath(const std::string & name) const
  2496. {
  2497. std::lock_guard<std::mutex> lock(m_mLivePaths);
  2498. auto it = m_mapLivePaths.find(name);
  2499. return (it == m_mapLivePaths.end()) ? nullptr : it->second;
  2500. }
  2501. /* ---------------------------------------------------------------- alarm clips from the recorder
  2502. When the recorder writes segments (live_record), the alarm clips come from them and the old encoder
  2503. (MS_MODE_ENCODE_FRAMES) is not started, so the video is no longer encoded and written twice.
  2504. - every closed segment is kept for EVE_CLIP_KEEP_MS (<scratch>/stream_<node>/...mp4), then deleted;
  2505. - an alarm (saveStreamPicture -> clipAlarm) wants the segments within EVE_CLIP_PRE_ALARM_MS before
  2506. and EVE_CLIP_POST_ALARM_MS after it. Those segments are handed to the kernel like the old encoder's
  2507. clips: a hard link <scratch>/<md5>.mp4 in a VIDEO_SEGMENT_INFO with EVT_CAPTURE_DONE. The alarm is
  2508. bound (eventIDS / eventFrames) to the segment it happened in;
  2509. - mojoKernel::handleVideo copies the clip into the report and deletes the link (or the remote server
  2510. keeps it); the recorder's own file is deleted by sweepClips() as usual.
  2511. */
  2512. #ifndef EVE_CLIP_PRE_ALARM_MS
  2513. #define EVE_CLIP_PRE_ALARM_MS 5000
  2514. #endif
  2515. #ifndef EVE_CLIP_POST_ALARM_MS
  2516. #define EVE_CLIP_POST_ALARM_MS 5000
  2517. #endif
  2518. #ifndef EVE_CLIP_KEEP_MS
  2519. #define EVE_CLIP_KEEP_MS 180000 // recorded segments kept for clips of later alarms (pre-alarm part)
  2520. #endif
  2521. bool kernelModuleEmbedVideo::liveClipsActive(void) const
  2522. {
  2523. return m_sEmbedVideoData.live_mode != EVE_LIVE_OFF && m_sEmbedVideoData.live_record;
  2524. }
  2525. void kernelModuleEmbedVideo::registerClipStream(const std::string & path, uint32_t streamID, uint64_t sourceID, uint8_t fps)
  2526. {
  2527. std::lock_guard<std::mutex> lock(m_mClips);
  2528. EveClipStream & tS = m_mapClips[path];
  2529. tS.streamID = streamID;
  2530. tS.sourceID = sourceID;
  2531. tS.fps = fps ? fps : 25;
  2532. }
  2533. void kernelModuleEmbedVideo::clipAlarm(uint32_t streamID, uint32_t eventID)
  2534. {
  2535. int64_t tNow = mtx::nowUs();
  2536. std::lock_guard<std::mutex> lock(m_mClips);
  2537. for (auto & c : m_mapClips)
  2538. {
  2539. if (c.second.streamID == streamID)
  2540. {
  2541. c.second.alarms.push_back({eventID, tNow, false});
  2542. matchClipsLocked(c.second, tNow); // the pre-alarm part may be recorded already
  2543. return;
  2544. };
  2545. };
  2546. char tB[128];
  2547. snprintf(tB, sizeof(tB), "Alarm clip: stream %u is not recorded - no clip for event %u", streamID, eventID);
  2548. LOG_ERROR(tB);
  2549. }
  2550. // Recorder thread: a segment is complete
  2551. void kernelModuleEmbedVideo::onLiveSegment(const mtx::SegmentInfo & info)
  2552. {
  2553. std::lock_guard<std::mutex> lock(m_mClips);
  2554. EveClipStream & tS = m_mapClips[info.path];
  2555. tS.segments.push_back({info.file, info.startUs, info.startUs + info.durationUs, info.width, info.height, false});
  2556. matchClipsLocked(tS, mtx::nowUs());
  2557. }
  2558. void kernelModuleEmbedVideo::matchClipsLocked(EveClipStream & stream, int64_t nowUs)
  2559. {
  2560. if (stream.alarms.empty())
  2561. {
  2562. return;
  2563. };
  2564. for (EveClipSegment & tSeg : stream.segments)
  2565. {
  2566. if (tSeg.handed)
  2567. {
  2568. continue;
  2569. };
  2570. for (const EveClipAlarm & a : stream.alarms)
  2571. {
  2572. if (a.timeUs - EVE_CLIP_PRE_ALARM_MS * 1000LL < tSeg.endUs && a.timeUs + EVE_CLIP_POST_ALARM_MS * 1000LL > tSeg.startUs)
  2573. {
  2574. handOffClipLocked(stream, tSeg);
  2575. break;
  2576. };
  2577. };
  2578. };
  2579. // An alarm is done when its segment is handed over and the post-alarm time is recorded
  2580. int64_t tRecordedUntil = stream.segments.empty() ? 0 : stream.segments.back().endUs;
  2581. for (auto it = stream.alarms.begin(); it != stream.alarms.end();)
  2582. {
  2583. bool tDone = it->bound && tRecordedUntil >= it->timeUs + EVE_CLIP_POST_ALARM_MS * 1000LL;
  2584. // nothing recorded for it (stream down): give up after a while
  2585. bool tLost = ! it->bound && nowUs - it->timeUs > EVE_CLIP_KEEP_MS * 1000LL;
  2586. if (tLost)
  2587. {
  2588. char tB[128];
  2589. snprintf(tB, sizeof(tB), "Alarm clip: nothing recorded for event %u - no clip", it->eventID);
  2590. LOG_ERROR(tB);
  2591. };
  2592. if (tDone || tLost || (it->bound && nowUs - it->timeUs > EVE_CLIP_KEEP_MS * 1000LL))
  2593. {
  2594. it = stream.alarms.erase(it);
  2595. }
  2596. else
  2597. {
  2598. ++it;
  2599. };
  2600. };
  2601. }
  2602. bool kernelModuleEmbedVideo::handOffClipLocked(EveClipStream & stream, EveClipSegment & seg)
  2603. {
  2604. seg.handed = true; // also on failure: do not retry every time
  2605. if (stream.streamID == 0)
  2606. {
  2607. return false; // a path no stream handler registered (cannot happen: initLive registers it)
  2608. };
  2609. CSTR tMD5 = getUniqID_MD5();
  2610. if (tMD5 == NULL)
  2611. {
  2612. LOG_ERROR("Alarm clip: no MD5");
  2613. return false;
  2614. };
  2615. VIDEO_SEGMENT_INFO * tSI = new VIDEO_SEGMENT_INFO(); // owned by the receiver (mojoKernel deletes it)
  2616. snprintf(tSI->scratchPath, sizeof(tSI->scratchPath), "%s/%s.mp4", m_pVideoScratchPath, tMD5);
  2617. strncpy(tSI->recordMD5, tMD5, 32);
  2618. tSI->recordMD5[32] = (char) 0;
  2619. free(tMD5);
  2620. // a hard link: the recorder's file stays until sweepClips(), the kernel deletes its link
  2621. if (link(seg.file.c_str(), tSI->scratchPath) != 0 && fileCopy(seg.file.c_str(), tSI->scratchPath) != 0)
  2622. {
  2623. char tB[PATH_MAX + 64];
  2624. snprintf(tB, sizeof(tB), "Alarm clip: cannot link %s (%s)", seg.file.c_str(), strerror(errno));
  2625. LOG_ERROR(tB);
  2626. delete tSI;
  2627. return false;
  2628. };
  2629. strncpy(tSI->recordEXT, "mp4", sizeof(tSI->recordEXT));
  2630. tSI->stream_id = stream.streamID;
  2631. tSI->streamTSS = (uint32_t) (seg.startUs / 1000000);
  2632. tSI->streamTMS = (uint32_t) ((seg.startUs / 1000) % 1000);
  2633. tSI->streamFPS = stream.fps;
  2634. tSI->streamWidth = (uint16_t) seg.width;
  2635. tSI->streamHeight = (uint16_t) seg.height;
  2636. struct stat tSt;
  2637. tSI->dataSize = (stat(tSI->scratchPath, &tSt) == 0) ? (uint32_t) tSt.st_size : 0;
  2638. for (EveClipAlarm & a : stream.alarms)
  2639. {
  2640. // the segment the alarm happened in (or the first one after it: nothing recorded at its time)
  2641. if (! a.bound && a.timeUs < seg.endUs)
  2642. {
  2643. a.bound = true;
  2644. int64_t tOffset = a.timeUs > seg.startUs ? a.timeUs - seg.startUs : 0;
  2645. tSI->eventIDS.push_back(a.eventID);
  2646. tSI->eventTSS.push_back((uint32_t) (a.timeUs / 1000000));
  2647. tSI->eventTMS.push_back((uint32_t) ((a.timeUs / 1000) % 1000));
  2648. tSI->eventFrames.push_back((uint32_t) (tOffset * stream.fps / 1000000));
  2649. };
  2650. };
  2651. EVENT_CONTAINER * bEVT = createBasicEvent(stream.sourceID, EVT_CLASS_VIDEO, EVT_CAPTURE_DONE, NULL, NULL, tSI);
  2652. if (bEVT == NULL)
  2653. {
  2654. LOG_ERROR("Alarm clip: event creation failure!");
  2655. unlink(tSI->scratchPath);
  2656. delete tSI;
  2657. return false;
  2658. };
  2659. char tB[PATH_MAX + 96];
  2660. snprintf(tB, sizeof(tB), "Alarm clip: %s (%u event(s))", seg.file.c_str(), (unsigned) tSI->eventIDS.size());
  2661. LOG(tB);
  2662. basicEventHeap::getInstance()->broadcast(bEVT);
  2663. return true;
  2664. }
  2665. // validate(): deletes old segments, checks the alarms; every minute also left-overs (a previous run)
  2666. void kernelModuleEmbedVideo::sweepClips(void)
  2667. {
  2668. int64_t tNow = mtx::nowUs();
  2669. {
  2670. std::lock_guard<std::mutex> lock(m_mClips);
  2671. for (auto & c : m_mapClips)
  2672. {
  2673. std::deque<EveClipSegment> & tSegs = c.second.segments;
  2674. while (! tSegs.empty() && tNow - tSegs.front().endUs > EVE_CLIP_KEEP_MS * 1000LL)
  2675. {
  2676. unlink(tSegs.front().file.c_str());
  2677. tSegs.pop_front();
  2678. };
  2679. matchClipsLocked(c.second, tNow);
  2680. };
  2681. }
  2682. time_t tSec = time(NULL);
  2683. if (tSec - m_tClipsScan < 60)
  2684. {
  2685. return;
  2686. };
  2687. m_tClipsScan = tSec;
  2688. // <scratch>/stream_*/*.mp4 older than the keep time (+1 min): not tracked any more (restart, path gone)
  2689. DIR * tDir = opendir(m_pVideoScratchPath);
  2690. if (tDir == NULL)
  2691. {
  2692. return;
  2693. };
  2694. struct dirent * tE;
  2695. while ((tE = readdir(tDir)) != NULL)
  2696. {
  2697. if (strncmp(tE->d_name, "stream_", 7) != 0)
  2698. {
  2699. continue;
  2700. };
  2701. std::string tSub = std::string(m_pVideoScratchPath) + "/" + tE->d_name;
  2702. DIR * tD = opendir(tSub.c_str());
  2703. if (tD == NULL)
  2704. {
  2705. continue;
  2706. };
  2707. struct dirent * tF;
  2708. while ((tF = readdir(tD)) != NULL)
  2709. {
  2710. size_t tLen = strlen(tF->d_name);
  2711. if (tLen < 4 || strcmp(tF->d_name + tLen - 4, ".mp4") != 0)
  2712. {
  2713. continue;
  2714. };
  2715. std::string tFile = tSub + "/" + tF->d_name;
  2716. struct stat tSt;
  2717. if (stat(tFile.c_str(), &tSt) == 0 && S_ISREG(tSt.st_mode) && (tSec - tSt.st_mtime) * 1000LL > EVE_CLIP_KEEP_MS + 60000)
  2718. {
  2719. unlink(tFile.c_str());
  2720. };
  2721. };
  2722. closedir(tD);
  2723. };
  2724. closedir(tDir);
  2725. }
  2726. #undef TLOG
  2727. #undef TLOG_ERROR
  2728. #else //LRF_PROXY_ONLY
  2729. kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) :
  2730. kernelModule(moduleID, KERNEL_MODULE_EMBED_VIDEO)
  2731. {
  2732. }
  2733. kernelModuleEmbedVideo::~kernelModuleEmbedVideo()
  2734. {
  2735. detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h)
  2736. }
  2737. #endif //LRF_PROXY_ONLY
  2738. #undef LOG
  2739. #undef LOG_ERROR
  2740. //#undef DEBUG_EXTRA