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.

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