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.

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