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.

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