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.

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