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.

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