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.

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