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.

1092 lines
45 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
​
​
​
5 years ago
​
5 years ago
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
5 years ago
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
​
​
​
5 years ago
5 years ago
5 years ago
​
​
​
5 years ago
5 years ago
5 years ago
​
5 years ago
​
​
​
5 years ago
​
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
5 years ago
​
5 years ago
​
5 years ago
​
​
​
​
5 years ago
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseModuleEmbedVideo.h"
  2. #include "rgooseHardwareResourceNetworkProvider.h"
  3. #include "../alg/generic/base64.h"
  4. #include <ctype.h>
  5. #include <err.h>
  6. #include <errno.h>
  7. #include <getopt.h>
  8. #include <ifaddrs.h>
  9. #include <netdb.h>
  10. #include <paths.h>
  11. #include <poll.h>
  12. #include <stdio.h>
  13. #include <signal.h>
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/ip.h>
  21. #include <netinet/udp.h>
  22. #include <sys/param.h> /* MIN() */
  23. #include <sys/socket.h>
  24. #include <openssl/sha.h>
  25. #define STORAGE_DIR_MODE 0x1FF
  26. #define STORAGE_FILE_MODE 0x1B6
  27. #define LOG( X ) argusKernelModule::m_pLog->Log( "MOD_EVIDEO", this, X )
  28. #ifdef DEBUG
  29. #define LOG_ERROR( X ) {\
  30. char _tBfrErrMSG2w34[256];\
  31. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  32. argusKernelModule::m_pLog->Log( "MOD_EVIDEO", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  33. }
  34. #else
  35. #define LOG_ERROR( X ) argusKernelModule::m_pLog->Log( "MOD_EVIDEO", this, _LOG_ERROR, X );
  36. #endif
  37. #define TLOG( X ) basicLogCollection::getInstance()->Log( "MOD_EVIDEO_T", CTX, X )
  38. #ifdef DEBUG
  39. #define TLOG_ERROR( X ) {\
  40. char _tBfrErrMSG2w34[256];\
  41. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  42. basicLogCollection::getInstance()->Log( "MOD_EVIDEO_T", CTX, _LOG_ERROR, _tBfrErrMSG2w34 );\
  43. }
  44. #else
  45. #define TLOG_ERROR( X ) basicLogCollection::getInstance()->Log( "MOD_EVIDEO_T", CTX, _LOG_ERROR, X );
  46. #endif
  47. enum {
  48. WEBRTC_REQ_SIGNIN = 0,
  49. WEBRTC_REQ_WAIT = 1,
  50. WEBRTC_REQ_SIGNOUT = 2,
  51. WEBRTC_REQ_MESSAGE = 3
  52. };
  53. static const char* kRequestPaths[] = { "/stream", "/sign_in", "/wait", "/sign_out", "/message" };
  54. //buffer_data bd[1]={0};
  55. argusModuleEmbedVideo::argusModuleEmbedVideo( uint32_t moduleID ): argusKernelModule( moduleID, ARGUS_MODULE_EMBED_VIDEO ), basicMediaDestination(), basicThread(){
  56. m_pEventHeap = basicEventHeap::getInstance();
  57. if( m_pDBC ){
  58. DBC_MODULE_EMBED_VIDEO_DATA *tDATA = m_pDBC->getEmbedVideoData( m_nDBAUXID );
  59. if( tDATA ) {
  60. memcpy( &m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA) );
  61. free( tDATA );
  62. } else {
  63. memset( &m_sEmbedVideoData, 0, sizeof(DBC_MODULE_EMBED_VIDEO_DATA) );
  64. m_sEmbedVideoData.port = 8972;
  65. };
  66. } else {
  67. LOG_ERROR( "DB Error!" );
  68. };
  69. n_pNoSignalData = NULL;
  70. m_nNoSignalSize = 0;
  71. char tBuff[1024];
  72. sprintf( m_pBaseSavePath, "%s/data/reports", getcwd(tBuff, 1024) );
  73. #ifdef DEBUG
  74. char tB[64];
  75. sprintf( tB, "Web Streams Port: %d", m_sEmbedVideoData.port );
  76. LOG( tB );
  77. sprintf( tB, "Frame: [%dx%d]", m_sEmbedVideoData.width, m_sEmbedVideoData.height );
  78. LOG( tB );
  79. if( m_sEmbedVideoData.show_ts ) LOG("Show TimeStamp");
  80. if( m_sEmbedVideoData.show_label ) LOG("Show Label");
  81. if( m_sEmbedVideoData.show_logo ) LOG("Show Logo");
  82. #endif
  83. if( m_sEmbedVideoData.show_logo ){
  84. LOG( m_sEmbedVideoData.logo_md5 );
  85. char tPath[MAX_PATH];
  86. sprintf( tPath, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5 );
  87. if( !fileExist( tPath ) ){
  88. LOG_ERROR("Logo File Missed!");
  89. m_sEmbedVideoData.show_logo = false;
  90. }
  91. #ifdef DEBUG
  92. else LOG("Logo File is OK");
  93. #endif
  94. ;
  95. };
  96. };
  97. argusModuleEmbedVideo::~argusModuleEmbedVideo(){
  98. m_bEnabled = false;
  99. waitThreadEnd( true );
  100. if( m_vHandlersPool.size() ) { // Kill-Wait Started Probes
  101. #ifdef DEBUG
  102. char tB[200];
  103. sprintf(tB, "Have to Wait Client Handlers[%d]!", m_vHandlersPool.size() );
  104. LOG( tB );
  105. #endif
  106. for( vectorHANDLERS::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++ ) pthread_cancel( (*index)->m_hThreadHandle );
  107. m_vHandlersPool.clear();
  108. };
  109. #ifdef DEBUG
  110. LOG("No More Client Handlers!");
  111. #endif
  112. if( m_mapCAPTURES.size() ) m_mapCAPTURES.clear();
  113. if( m_mapMATS.size() ) m_mapMATS.clear();
  114. if(n_pNoSignalData) free(n_pNoSignalData);
  115. };
  116. static const char * ht_head = "HTTP/1.1 200 OK\r\n"
  117. "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
  118. "Pragma: no-cache\r\n"
  119. "MIME-version: 1.0\r\n"
  120. "Connection: close\r\n"
  121. //"Keep-Alive: timeout=5, max=1\r\n"
  122. //"Connection: Keep-Alive\r\n"
  123. //"Content-Type: video/mp4\r\n"
  124. "Content-Transfer-Encoding: BINARY\r\n"
  125. //"Transfer-Encoding: chunked\r\n"
  126. "Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n"
  127. "\r\n";
  128. static const char * ht_abort = "HTTP/1.1 404 OK\r\n"
  129. "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
  130. "Pragma: no-cache\r\n"
  131. "Connection: close\r\n\r\n";
  132. static const char * ht_no_signal = "HTTP/1.1 200 OK\r\n"
  133. "MIME-version: 1.0\r\n"
  134. "Connection: close\r\n"
  135. "Content-Transfer-Encoding: BINARY\r\n"
  136. //"Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n\r\n"
  137. "Content-Type: image/jpeg\r\nContent-length: %d\r\n\r\n";
  138. void *argusModuleEmbedVideo::webClientProc( void *ptr ){
  139. WEB_CLIENT_CTX * CTX = (WEB_CLIENT_CTX *) ptr;
  140. argusModuleEmbedVideo * MODULE = CTX->ModulePtr;
  141. uint32_t msgSize;
  142. uint8_t tBuf[FILESTREAMBUFFERSZ];
  143. int retVal;
  144. if( CTX ) {
  145. char mesg[1024];
  146. size_t RecvSize;
  147. struct pollfd pfd[1];
  148. char cGetURI[1024];
  149. bool m_bDeflatePerMessage = false;
  150. bool m_pInit;
  151. #ifdef DEBUG_EXTRA
  152. pid_t pid = gettid();
  153. char pidBuf[16];
  154. sprintf( pidBuf, "pid:%d", pid);
  155. TLOG( pidBuf );
  156. #endif
  157. pfd->fd = CTX->m_iSocketFID;
  158. pfd->events = POLLIN | POLLHUP;
  159. while( CTX->bValid ) {
  160. if( !CTX->initDone ){
  161. int RetVal;
  162. char * reqline[256];
  163. RetVal = poll( pfd, 1, 1000 );
  164. if( RetVal <= 0 ){
  165. if ( EINTR == errno ) continue;
  166. TLOG_ERROR("Unrecoverable error");
  167. CTX->bValid = false;
  168. continue;
  169. };
  170. memset( mesg, 0, sizeof(mesg) );
  171. RetVal = recv( CTX->m_iSocketFID, mesg, sizeof(mesg), 0 );
  172. if ( RetVal <= 0 ) {
  173. sprintf( mesg, "web recv() error: %s", strerror(errno));
  174. TLOG_ERROR( mesg );
  175. CTX->bValid = false;
  176. continue;
  177. };
  178. #ifdef DEBUG_EXTRA
  179. TLOG( mesg );
  180. #endif
  181. uint32_t index = 0;
  182. reqline[index] = strtok( mesg, "\n" );
  183. if( strncmp( reqline[index], "GET", 3 ) == 0 ) {
  184. char * pPartURI;
  185. char * pPartPROTO;
  186. pPartURI = strstr( reqline[index], " " );
  187. if( pPartURI != NULL ) {
  188. pPartURI++;
  189. pPartPROTO = strstr( pPartURI, " " );
  190. pPartPROTO++;
  191. int URILen = pPartPROTO - pPartURI;
  192. strncpy( cGetURI, pPartURI, URILen );
  193. cGetURI[URILen] = (char)0;
  194. if(strncmp( pPartPROTO, "HTTP/1.0", 8) != 0 && strncmp( pPartPROTO, "HTTP/1.1", 8) != 0) {
  195. TLOG_ERROR("No HTTP version supplied in request!");
  196. if( write( CTX->m_iSocketFID, "HTTP/1.1 400 Bad Request\r\n", 26) < 0 ){
  197. sprintf( mesg, "Failed returning status 400 to client: %s", strerror(errno));
  198. TLOG_ERROR(mesg);
  199. };
  200. CTX->bValid = false;
  201. continue;
  202. };
  203. };
  204. #ifdef DEBUG_EXTRA
  205. TLOG( cGetURI );
  206. #endif
  207. };
  208. if( strncmp( cGetURI, "/stream", 7 ) == 0 ) {
  209. uint32_t tStreamID = 0;
  210. CSTR tP;
  211. if( tP = strstr( cGetURI, "?" ) ) {
  212. CSTR tB;
  213. tP++;
  214. if( tB = strstr( tP, "id=" ) ) {
  215. tStreamID = atoi( tB+3 );
  216. };
  217. };
  218. if( tStreamID ) {
  219. #ifdef DEBUG_EXTRA
  220. char tB[64];
  221. sprintf(tB,"StremID: %d", tStreamID );
  222. TLOG( tB );
  223. #endif
  224. if( CTX->ModulePtr->registerForMediaStream( tStreamID, CTX ) ){
  225. // Send Header
  226. if( send( CTX->m_iSocketFID, ht_head, strlen(ht_head), 0 ) < 0 ){
  227. #ifdef DEBUG_EXTRA
  228. sprintf( mesg, "Failed sending file to client: %s", strerror(errno));
  229. TLOG_ERROR(mesg);
  230. #endif
  231. CTX->bValid = false;
  232. };
  233. CTX->initDone = true;
  234. continue;
  235. }
  236. #ifdef DEBUG_EXTRA
  237. else {
  238. TLOG("Stream id Not Online");
  239. }
  240. #endif
  241. ;
  242. }
  243. #ifdef DEBUG_EXTRA
  244. else TLOG("No Stream ID")
  245. #endif
  246. ;
  247. };
  248. if( CTX->ModulePtr->n_pNoSignalData ) {
  249. //Send Dummy Picture
  250. sprintf( mesg, ht_no_signal, CTX->ModulePtr->m_nNoSignalSize );
  251. send( CTX->m_iSocketFID, mesg, strlen(mesg), 0 );
  252. send( CTX->m_iSocketFID, CTX->ModulePtr->n_pNoSignalData, CTX->ModulePtr->m_nNoSignalSize, 0 );
  253. send( CTX->m_iSocketFID, "\r\n\r\n", 4, 0 );
  254. #ifdef DEBUG_EXTRA
  255. TLOG("Sent No Signal Image!");
  256. #endif
  257. } else {
  258. // Send 404 Header
  259. send( CTX->m_iSocketFID, ht_abort, strlen(ht_abort), 0 );
  260. #ifdef DEBUG_EXTRA
  261. TLOG("Aborted!");
  262. #endif
  263. };
  264. CTX->bValid = false;
  265. continue;
  266. } else {
  267. /*
  268. retVal = read( CTX->m_iEncoderPipe[0], &msgSize, sizeof(uint32_t));
  269. if( retVal == -1 ){
  270. if( errno == EAGAIN || errno == EWOULDBLOCK ){
  271. continue;
  272. };
  273. TLOG_ERROR("Pipe Communication Error[1]!");
  274. CTX->bValid = false;
  275. continue;
  276. };
  277. if( msgSize < FILESTREAMBUFFERSZ ) {
  278. retVal = read( CTX->m_iEncoderPipe[0], tBuf, msgSize );
  279. } else {
  280. TLOG_ERROR("Data too Big in Pipe Transfer!");
  281. CTX->bValid = false;
  282. continue;
  283. };
  284. if( retVal == -1 ){
  285. //free( tBuf );
  286. if( errno == EAGAIN || errno == EWOULDBLOCK ){
  287. continue;
  288. };
  289. TLOG_ERROR("Pipe Communication Error[2]!");
  290. CTX->bValid = false;
  291. continue;
  292. };
  293. */
  294. /**************/
  295. CTX->ModulePtr->m_mEncodedProtector.lock();
  296. if( CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].size() ) {
  297. msgSize = CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].size();
  298. if( msgSize < FILESTREAMBUFFERSZ ) {
  299. memcpy( tBuf, CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].data(), msgSize );
  300. CTX->ModulePtr->m_mEncodedProtector.unlock();
  301. } else {
  302. CTX->ModulePtr->m_mEncodedProtector.unlock();
  303. TLOG_ERROR("Data too Big in Pipe Transfer!");
  304. CTX->bValid = false;
  305. continue;
  306. };
  307. } else {
  308. CTX->ModulePtr->m_mEncodedProtector.unlock();
  309. if( CTX->ModulePtr->n_pNoSignalData ){
  310. msgSize = CTX->ModulePtr->m_nNoSignalSize;
  311. memcpy( tBuf, CTX->ModulePtr->n_pNoSignalData, msgSize );
  312. } else {
  313. TLOG_ERROR("Data too Big in Pipe Transfer!");
  314. CTX->bValid = false;
  315. continue;
  316. };
  317. };
  318. /*********/
  319. sprintf( mesg, "--MPFrame\r\nContent-type: image/jpeg\r\nContent-length: %d\r\n\r\n", msgSize );
  320. if( send( CTX->m_iSocketFID, mesg, strlen( mesg ), 0 )< 0 ){
  321. #ifdef DEBUG_EXTRA
  322. sprintf( mesg, "[1]Failed sending file to client: %s", strerror(errno));
  323. TLOG_ERROR(mesg);
  324. #endif
  325. CTX->bValid = false;
  326. continue;
  327. };
  328. if( send( CTX->m_iSocketFID, tBuf, msgSize, 0 ) < 0 ){
  329. #ifdef DEBUG_EXTRA
  330. sprintf( mesg, "[2]Failed sending file to client: %s", strerror(errno));
  331. TLOG_ERROR(mesg);
  332. #endif
  333. CTX->bValid = false;
  334. continue;
  335. };
  336. if( send( CTX->m_iSocketFID, "\r\n\r\n", 4, 0 )< 0 ){
  337. #ifdef DEBUG_EXTRA
  338. sprintf( mesg, "[3]Failed sending file to client: %s", strerror(errno));
  339. TLOG_ERROR(mesg);
  340. #endif
  341. CTX->bValid = false;
  342. continue;
  343. };
  344. SLEEP( 80 );
  345. };
  346. };
  347. };
  348. CTX->bValid = false;
  349. #ifdef DEBUG_EXTRA
  350. TLOG("WEB Client Handler Ended!");
  351. #endif
  352. close( CTX->m_iEncoderPipe[0] );
  353. shutdown( CTX->m_iSocketFID, SHUT_RDWR);
  354. close( CTX->m_iSocketFID );
  355. //pthread_exit( NULL );
  356. return NULL;
  357. };
  358. void argusModuleEmbedVideo::m_fThreadProc( void ){
  359. if( !m_bEnabled ) {
  360. #ifdef DEBUG
  361. LOG("Module is Disabled!");
  362. #endif
  363. return;
  364. };
  365. #ifdef DEBUG
  366. LOG("Module Started!");
  367. #endif
  368. struct pollfd pfd[1];
  369. pfd->fd = m_nSocketDescriptor;
  370. pfd->events = POLLIN | POLLHUP;
  371. m_bThreadStatus = TH_WORKING;
  372. while( m_bThreadStatus < TH_ENDED ) {
  373. if( !m_bEnabled ) {
  374. m_bThreadStatus = TH_ENDED;
  375. continue;
  376. };
  377. int RetVal = poll( pfd, 1, 1000 );
  378. if( RetVal < 0 ){
  379. if( EINTR == errno ) continue;
  380. LOG_ERROR("Unrecoverable error");
  381. m_bThreadStatus = TH_ENDED;
  382. continue;
  383. };
  384. // Handle Connections
  385. if( RetVal != 0 ){
  386. if ( pfd[0].revents & POLLIN ) {
  387. #ifdef DEBUG_EXTRA
  388. LOG("Handle New Client");
  389. #endif
  390. int tSID = accept( pfd[0].fd, NULL, NULL);
  391. if( tSID < 0 ){
  392. char tBuf[CTEXT];
  393. sprintf(tBuf, "Accept() error: %s", strerror(errno));
  394. LOG_ERROR(tBuf);
  395. } else {
  396. #ifdef DEBUG_EXTRA
  397. LOG("Accept OK");
  398. #endif
  399. WEB_CLIENT_CTX *tCTX = (WEB_CLIENT_CTX *) malloc(sizeof(WEB_CLIENT_CTX));
  400. if( tCTX ) {
  401. #ifdef DEBUG_EXTRA
  402. LOG("CTX OK");
  403. #endif
  404. WEB_HANDLER *tHANDLER = (WEB_HANDLER *) malloc(sizeof(WEB_HANDLER));
  405. if( tHANDLER ) {
  406. #ifdef DEBUG_EXTRA
  407. LOG("HANDLER OK");
  408. #endif
  409. memset( tCTX, 0, sizeof(WEB_CLIENT_CTX) );
  410. memset( tHANDLER, 0, sizeof(WEB_HANDLER) );
  411. tCTX->m_iSocketFID = tSID;
  412. tCTX->m_iEncoderPipe[0] = -1;
  413. tCTX->m_iEncoderPipe[1] = -1;
  414. tCTX->mediaStreamId = 0;
  415. tCTX->bValid = true;
  416. tCTX->ModulePtr = this;
  417. #ifdef DEBUG_EXTRA
  418. LOG("Before Thread Create");
  419. #endif
  420. int RetVal = pthread_create( &tHANDLER->m_hThreadHandle, NULL, webClientProc, ( void * ) tCTX );
  421. if( RetVal == -1 ) {
  422. free(tCTX);
  423. free(tHANDLER);
  424. LOG_ERROR("Thread Creation Faled.");
  425. shutdown( tSID, SHUT_RDWR );
  426. close( tSID );
  427. } else {
  428. tHANDLER->CTX = tCTX;
  429. m_mHandlersProtector.lock();
  430. m_vHandlersPool.push_back( tHANDLER );
  431. m_mHandlersProtector.unlock();
  432. #ifdef DEBUG_EXTRA
  433. LOG("Handler Pushed to a Pool");
  434. #endif
  435. };
  436. } else { //https://stackoverflow.com/questions/18545077/image-fullscreen-on-click
  437. free(tCTX);
  438. LOG_ERROR("CL_HANDLER Mem Allocation Failure!");
  439. shutdown( tSID, SHUT_RDWR );
  440. close( tSID );
  441. };
  442. } else {
  443. LOG_ERROR("CL_CTX Mem Allocation Failure!");
  444. shutdown( tSID, SHUT_RDWR );
  445. close( tSID );
  446. };
  447. };
  448. };
  449. };
  450. // Handle Dead Clients
  451. if( m_vHandlersPool.size() ) {
  452. for( vectorHANDLERS::iterator idx = m_vHandlersPool.begin(); idx < m_vHandlersPool.end(); idx++ ) {
  453. if( !( (*idx)->CTX->bValid ) ) {
  454. pthread_join( (*idx)->m_hThreadHandle, NULL );
  455. //close( (*idx)->CTX->m_iEncoderPipe[0] );
  456. close( (*idx)->CTX->m_iEncoderPipe[1] );
  457. free( (*idx)->CTX );
  458. free( (*idx) );
  459. m_mHandlersProtector.lock();
  460. m_vHandlersPool.erase(idx);
  461. m_mHandlersProtector.unlock();
  462. #ifdef DEBUG_EXTRA
  463. LOG("Web Handler Ripped!");
  464. #endif
  465. };
  466. };
  467. };
  468. SLEEP( 50 );
  469. };
  470. };
  471. #define ENABLE_SOCKOPT(sd, level, opt) \
  472. do { \
  473. int val = 1; \
  474. if (setsockopt(sd, level, opt, &val, sizeof(val)) < 0) \
  475. warn("Failed enabling %s for web service", #opt); \
  476. } while (0);
  477. bool argusModuleEmbedVideo::startService(){
  478. m_bEnabled = true;
  479. //Create NoSignal Image
  480. cv::Mat tMat( m_sEmbedVideoData.height, m_sEmbedVideoData.width, CV_8UC3 );
  481. cv::rectangle( tMat, cv::Point( 0, 0 ), cv::Point( m_sEmbedVideoData.width, m_sEmbedVideoData.height ), cv::Scalar( 255, 0, 0 ), cv::FILLED );
  482. cv::putText( tMat, "NO SIGNAL", cv::Point( 10, 30 ), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar( 0, 255, 255 ), 3 );
  483. if( m_sEmbedVideoData.show_logo ) {
  484. int a_x, a_y;
  485. if( m_sEmbedVideoData.logo_x < 0 ) {
  486. a_x = m_sEmbedVideoData.width + m_sEmbedVideoData.logo_x - m_sEmbedVideoData.logo_w;
  487. if( a_x < 0 ) a_x = 0;
  488. } else a_x = m_sEmbedVideoData.logo_x;
  489. if( ( a_x + m_sEmbedVideoData.logo_w ) > m_sEmbedVideoData.width ) a_x = m_sEmbedVideoData.width - m_sEmbedVideoData.logo_w;
  490. if( m_sEmbedVideoData.logo_y < 0 ) {
  491. a_y = m_sEmbedVideoData.height + m_sEmbedVideoData.logo_y - m_sEmbedVideoData.logo_h;
  492. if( a_y < 0 ) a_y = 0;
  493. } else a_y = m_sEmbedVideoData.logo_y;
  494. if( ( a_y + m_sEmbedVideoData.logo_h ) > m_sEmbedVideoData.height ) a_y = m_sEmbedVideoData.height - m_sEmbedVideoData.logo_h;
  495. if( ( a_x > -1 ) && ( a_y > -1 ) ) {
  496. char tName[256];
  497. m_sEmbedVideoData.logo_x = a_x;
  498. m_sEmbedVideoData.logo_y = a_y;
  499. sprintf( tName, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5 );
  500. cv::Mat T = cv::imread( tName, cv::IMREAD_UNCHANGED );
  501. if( T.cols > 0 ) {
  502. cv::Mat mask;
  503. vector<cv::Mat> layers;
  504. #ifdef DEBUG
  505. LOG("Logo Loaded OK");
  506. #endif
  507. cv::split( T, layers); // seperate channels
  508. cv::Mat rgb[3] = { layers[0], layers[1], layers[2] };
  509. mask = layers[3]; // png's alpha channel used as mask
  510. cv::merge( rgb, 3, T ); // put together the RGB channels, now transp insn't transparent
  511. cv::resize( T, m_mLogoPic, cv::Size( m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h ), cv::INTER_CUBIC );
  512. cv::resize( mask, m_mLogoMask, cv::Size( m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h ), cv::INTER_CUBIC );
  513. cv::Rect roi(cv::Point( m_sEmbedVideoData.logo_x, m_sEmbedVideoData.logo_y ), cv::Size( m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h ));
  514. cv::Mat destinationROI = tMat( roi );
  515. m_mLogoPic.copyTo( destinationROI, m_mLogoMask );
  516. //m_sEmbedVideoData.show_logo = false;
  517. } else {
  518. LOG("Logo Disabled - No Picture is Loaded");
  519. m_sEmbedVideoData.show_logo = false;
  520. };
  521. } else {
  522. LOG("Logo Disabled - Wrong Coords");
  523. m_sEmbedVideoData.show_logo = false;
  524. };
  525. LOG("Logo Draw OK");
  526. };
  527. try{
  528. vector<int> compression_params;
  529. //compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
  530. //compression_params.push_back(9);
  531. compression_params.push_back( cv::IMWRITE_JPEG_QUALITY );
  532. compression_params.push_back( m_sEmbedVideoData.compression_quality );
  533. vector<uchar> buf;
  534. cv::imencode( ".jpg", tMat, buf, compression_params );
  535. m_nNoSignalSize = buf.size();
  536. n_pNoSignalData = (uint8_t *) malloc( m_nNoSignalSize );
  537. if( n_pNoSignalData ) {
  538. memcpy( n_pNoSignalData, buf.data(), m_nNoSignalSize );
  539. } else m_nNoSignalSize =0;
  540. buf.clear();
  541. } catch (cv::Exception & e) {
  542. LOG_ERROR(e.msg.c_str());
  543. };
  544. // Socket
  545. struct sockaddr sa;
  546. struct sockaddr_in *sin;
  547. memset(&sa, 0, sizeof(struct sockaddr));
  548. sa.sa_family = AF_INET;
  549. sin = (struct sockaddr_in *)&sa;
  550. sin->sin_addr.s_addr = htonl( INADDR_ANY );
  551. sin->sin_port = htons( m_sEmbedVideoData.port );
  552. m_nSocketDescriptor = socket( sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
  553. if( m_nSocketDescriptor == -1 ){
  554. LOG_ERROR("Failed creating ACC socket");
  555. return false;
  556. };
  557. ENABLE_SOCKOPT( m_nSocketDescriptor, SOL_SOCKET, SO_REUSEADDR);
  558. #ifdef SO_REUSEPORT
  559. ENABLE_SOCKOPT( m_nSocketDescriptor, SOL_SOCKET, SO_REUSEPORT);
  560. #endif
  561. if( bind( m_nSocketDescriptor, &sa, sizeof(sa)) < 0 ){
  562. LOG_ERROR("Failed binding Socket");
  563. close( m_nSocketDescriptor );
  564. return false;
  565. } else {
  566. LOG("Bind Socessfull");
  567. };
  568. if( listen( m_nSocketDescriptor, 10) != 0 ){
  569. LOG_ERROR("Failed setting web listen backlog");
  570. close( m_nSocketDescriptor );
  571. return false;
  572. } else {
  573. LOG("Listen OK");
  574. };
  575. LOG("Start Thread");
  576. m_bEnabled = init();
  577. return m_bEnabled;
  578. };
  579. bool argusModuleEmbedVideo::stopService( bool force ){
  580. m_bEnabled = false;
  581. m_bDetached = uninit();
  582. #ifdef DEBUG
  583. LOG("Service Thread ended!");
  584. #endif
  585. if( m_vHandlersPool.size() ) { // Kill-Wait Started Probes
  586. #ifdef DEBUG
  587. char tB[200];
  588. sprintf(tB, "Have to Wait Client Handlers[%d]!", m_vHandlersPool.size() );
  589. LOG( tB );
  590. #endif
  591. for( vectorHANDLERS::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++ ) {
  592. if( force ) {
  593. #ifdef DEBUG
  594. LOG("Force Client Handler to Stop!");
  595. #endif
  596. pthread_cancel( (*index)->m_hThreadHandle );
  597. } else {
  598. #ifdef DEBUG
  599. LOG("Waiting Client Handler to End!");
  600. #endif
  601. pthread_join( (*index)->m_hThreadHandle, NULL );
  602. }
  603. };
  604. m_vHandlersPool.clear();
  605. };
  606. #ifdef DEBUG
  607. LOG("No More Client Handlers!");
  608. #endif
  609. if( m_mapCAPTURES.size() ){
  610. m_mapCAPTURES.clear();
  611. //for( mapSTREAMCAPS::iterator idx = m_mapCAPTURES.begin(); idx < m_mapCAPTURES.end(); idx++ ) {
  612. //};
  613. };
  614. if( m_mapMATS.size() ){
  615. m_mapMATS.clear();
  616. //for( mapSTREAMMAPS::iterator idx = m_mapMATS.begin(); idx < m_mapMATS.end(); idx++ ) {
  617. //};
  618. };
  619. if ( n_pNoSignalData ) {
  620. free( n_pNoSignalData );
  621. n_pNoSignalData = NULL;
  622. m_nNoSignalSize = 0;
  623. };
  624. return m_bDetached;
  625. };
  626. bool argusModuleEmbedVideo::reloadSettings(){
  627. LOG("Reload Service");
  628. if( m_pDBC ){
  629. DBC_MODULE_EMBED_VIDEO_DATA *tDATA = m_pDBC->getEmbedVideoData( m_nDBAUXID );
  630. if( tDATA ) {
  631. if( stopService() ){
  632. LOG("Stoped!");
  633. memcpy( &m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  634. free( tDATA );
  635. LOG("Starting...");
  636. return startService();
  637. } else {
  638. free( tDATA );
  639. };
  640. };
  641. };
  642. return false;
  643. };
  644. bool argusModuleEmbedVideo::validate(){
  645. if( m_pDBC ){
  646. DBC_MODULE_EMBED_VIDEO_DATA *tDATA = m_pDBC->getEmbedVideoData( m_nDBAUXID );
  647. if( tDATA ) {
  648. if( memcmp( &m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA)) != 0 ){
  649. free( tDATA );
  650. LOG("Module Settings Changed!");
  651. return reloadSettings();
  652. };
  653. free( tDATA );
  654. return true;
  655. };
  656. } else {
  657. LOG_ERROR( "DB Error!" );
  658. };
  659. return false;
  660. };
  661. bool argusModuleEmbedVideo::pushFrame( AVFrame *Frame, void* src){
  662. if( !m_bEnabled ) return false;
  663. MEDIA_SOURCE_DESC *tMD = (MEDIA_SOURCE_DESC *)src;
  664. if( tMD ) {
  665. };
  666. return true;
  667. };
  668. bool argusModuleEmbedVideo::pushFrame( cv::Mat &Frame, void* src ){
  669. if( !m_bEnabled ) return false;
  670. MEDIA_SOURCE_DESC * tMD = (MEDIA_SOURCE_DESC *) src;
  671. char tBuffer[128];
  672. if( tMD ) {
  673. float mK, oK;
  674. cv::Rect ROI;
  675. //if( !m_mPictureProtector.try_lock() ) return;
  676. m_mPictureProtector.lock();
  677. if( m_mapMATS[tMD->mediaStreamId].cols == 0 ) {
  678. LOG("First Frame Enqueued");
  679. sprintf( tBuffer, "INP: [%dx%d] SrcID:%d StreamID:%d", Frame.cols, Frame.rows, tMD->mediaSourceId, tMD->mediaStreamId );
  680. LOG( tBuffer );
  681. m_mapMATS[tMD->mediaStreamId].create( m_sEmbedVideoData.height, m_sEmbedVideoData.width, CV_8UC3 );
  682. cv::rectangle( m_mapMATS[tMD->mediaStreamId], cv::Point( 0, 0 ), cv::Point( m_sEmbedVideoData.height, m_sEmbedVideoData.width ), cv::Scalar( 0, 0, 0 ), cv::FILLED );
  683. };
  684. #ifdef USE_SCALING_IN
  685. oK = ((float)m_sEmbedVideoData.height )/ ((float)m_sEmbedVideoData.width);
  686. mK = ((float)Frame.rows )/ ((float)Frame.cols);
  687. //sprintf( tBuffer, "INP: [%dx%d] IK:%0.3f OK:%0.3f", Frame.cols, Frame.rows, mK, oK );
  688. if( ( fabsf( mK - oK ) < 0.1f ) || ( fabsf( oK - mK ) < 0.1f ) ) {
  689. ROI.width = (int) m_sEmbedVideoData.width;
  690. ROI.height = (int) m_sEmbedVideoData.height;
  691. ROI.x = (int) 0;
  692. ROI.y = (int) 0;
  693. } else {
  694. if( mK < oK ) {
  695. ROI.width = (int) m_sEmbedVideoData.width;
  696. ROI.height = (int) (((float)m_sEmbedVideoData.width) * mK );
  697. ROI.x = (int) 0;
  698. ROI.y = (int)( m_sEmbedVideoData.height - ROI.height ) >> 1;
  699. } else {
  700. ROI.height = (int) m_sEmbedVideoData.height;
  701. ROI.width = (int) (((float)m_sEmbedVideoData.height) / mK );
  702. ROI.y = (int) 0;
  703. ROI.x = (int) ( m_sEmbedVideoData.width - ROI.width ) >> 1;
  704. };
  705. };
  706. cv::Mat mROI = m_mapMATS[tMD->mediaStreamId]( ROI );
  707. cv::resize( Frame, mROI , mROI.size(), 0, 0, cv::INTER_LINEAR );
  708. #else
  709. if(( m_sEmbedVideoData.height==Frame.rows )&&( m_sEmbedVideoData.width==Frame.cols )){
  710. Frame.copyTo( m_mapMATS[tMD->mediaStreamId] );
  711. } else {
  712. //Mat dst_roi = dst(Rect(left, top, src.cols, src.rows));
  713. //src.copyTo(dst_roi);
  714. if (m_sEmbedVideoData.width == Frame.cols ) {
  715. ROI.width = Frame.cols;
  716. ROI.height = Frame.rows;
  717. ROI.x = 0;
  718. ROI.y = ( m_sEmbedVideoData.height - Frame.rows ) >> 1;
  719. cv::Mat mROI = m_mapMATS[tMD->mediaStreamId]( ROI );
  720. Frame.copyTo(mROI);
  721. } else {
  722. ROI.width = Frame.cols;
  723. ROI.height = Frame.rows;
  724. ROI.x = ( m_sEmbedVideoData.width - Frame.cols ) >> 1;
  725. ROI.y = 0;
  726. cv::Mat mROI = m_mapMATS[tMD->mediaStreamId]( ROI );
  727. Frame.copyTo(mROI);
  728. };
  729. };
  730. #endif
  731. if( m_sEmbedVideoData.show_label ){
  732. time_t t = time( NULL );
  733. struct tm * tmp = gmtime( &t );
  734. if( tmp ){
  735. char tsb[64];
  736. if( strftime( tsb, sizeof(tsb), "%F %T GMT", tmp )!=0 ){
  737. sprintf( tBuffer, ">>%d>%d>%s>%s>", tMD->mediaSourceId, tMD->mediaStreamId, tMD->mediaName, tsb );
  738. int baseline=0;
  739. cv::Size textSize = cv::getTextSize( tBuffer,
  740. m_sEmbedVideoData.label_font_face,
  741. m_sEmbedVideoData.label_font_scale,
  742. m_sEmbedVideoData.label_font_thickness,
  743. &baseline
  744. );
  745. int x = m_sEmbedVideoData.label_x;
  746. int y = m_sEmbedVideoData.label_y;
  747. if( x < 0 ) {
  748. x = m_sEmbedVideoData.width+x-textSize.width;
  749. };
  750. if( y < 0 ) {
  751. y = m_sEmbedVideoData.height+y-baseline;//
  752. };
  753. if( !m_sEmbedVideoData.label_transperant ) {
  754. cv::rectangle( m_mapMATS[tMD->mediaStreamId], cv::Rect( x, y-textSize.height-4, textSize.width, textSize.height+baseline+4 ), cv::Scalar( 0, 0, 0 ), cv::FILLED );
  755. };
  756. cv::putText( m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point( x, y ),
  757. (cv::HersheyFonts) m_sEmbedVideoData.label_font_face,
  758. m_sEmbedVideoData.label_font_scale, cv::Scalar( 0, 255, 0 ),
  759. m_sEmbedVideoData.label_font_thickness,
  760. m_sEmbedVideoData.label_line_type
  761. );
  762. };
  763. };
  764. } else {
  765. if( m_sEmbedVideoData.show_ts ){
  766. time_t t = time( NULL );
  767. struct tm * tmp = gmtime( &t );
  768. if( tmp ){
  769. if( strftime( tBuffer, sizeof(tBuffer), "%F %T GMT", tmp )!=0 ){
  770. uint8_t shift_x = 0;
  771. uint8_t shift_y = 15;
  772. if( !m_sEmbedVideoData.ts_transperant ) {
  773. if( m_sEmbedVideoData.ts_font_face ) {
  774. shift_y = 15;
  775. cv::rectangle( m_mapMATS[tMD->mediaStreamId], cv::Point( m_sEmbedVideoData.ts_x, m_sEmbedVideoData.ts_y ), cv::Point( 490, 60 ), cv::Scalar( 0, 0, 0 ), cv::FILLED );
  776. } else {
  777. shift_y = 23;
  778. cv::rectangle( m_mapMATS[tMD->mediaStreamId], cv::Point( m_sEmbedVideoData.ts_x, m_sEmbedVideoData.ts_y ), cv::Point( 490, 60 ), cv::Scalar( 0, 0, 0 ), cv::FILLED );
  779. };
  780. cv::putText( m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point( m_sEmbedVideoData.ts_x + shift_x, m_sEmbedVideoData.ts_y + shift_y ),
  781. (cv::HersheyFonts) m_sEmbedVideoData.ts_font_face,
  782. m_sEmbedVideoData.ts_font_scale, cv::Scalar( 0, 255, 255 ),
  783. m_sEmbedVideoData.ts_font_thickness,
  784. m_sEmbedVideoData.ts_line_type
  785. );
  786. } else {
  787. if( m_sEmbedVideoData.ts_font_face ) {
  788. shift_y = 15;
  789. } else {
  790. shift_y = 23;
  791. };
  792. cv::putText( m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point( m_sEmbedVideoData.ts_x + shift_x - 1, m_sEmbedVideoData.ts_y + shift_y - 1 ),
  793. (cv::HersheyFonts) m_sEmbedVideoData.ts_font_face,
  794. m_sEmbedVideoData.ts_font_scale, cv::Scalar( 0, 0, 0 ),
  795. m_sEmbedVideoData.ts_font_thickness,
  796. m_sEmbedVideoData.ts_line_type
  797. );
  798. cv::putText( m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point( m_sEmbedVideoData.ts_x + shift_x + 1, m_sEmbedVideoData.ts_y + shift_y - 1 ),
  799. (cv::HersheyFonts) m_sEmbedVideoData.ts_font_face,
  800. m_sEmbedVideoData.ts_font_scale, cv::Scalar( 0, 0, 0 ),
  801. m_sEmbedVideoData.ts_font_thickness,
  802. m_sEmbedVideoData.ts_line_type
  803. );
  804. cv::putText( m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point( m_sEmbedVideoData.ts_x + shift_x - 1, m_sEmbedVideoData.ts_y + shift_y + 1 ),
  805. (cv::HersheyFonts) m_sEmbedVideoData.ts_font_face,
  806. m_sEmbedVideoData.ts_font_scale, cv::Scalar( 0, 0, 0 ),
  807. m_sEmbedVideoData.ts_font_thickness,
  808. m_sEmbedVideoData.ts_line_type
  809. );
  810. cv::putText( m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point( m_sEmbedVideoData.ts_x + shift_x - 1, m_sEmbedVideoData.ts_y + shift_y + 2 ),
  811. (cv::HersheyFonts) m_sEmbedVideoData.ts_font_face,
  812. m_sEmbedVideoData.ts_font_scale, cv::Scalar( 0, 0, 0 ),
  813. m_sEmbedVideoData.ts_font_thickness,
  814. m_sEmbedVideoData.ts_line_type
  815. );
  816. cv::putText( m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point( m_sEmbedVideoData.ts_x + shift_x, m_sEmbedVideoData.ts_y + shift_y ),
  817. (cv::HersheyFonts) m_sEmbedVideoData.ts_font_face,
  818. m_sEmbedVideoData.ts_font_scale, cv::Scalar( 0, 255, 255 ),
  819. m_sEmbedVideoData.ts_font_thickness,
  820. m_sEmbedVideoData.ts_line_type
  821. );
  822. }
  823. };
  824. };
  825. };
  826. };
  827. if( m_sEmbedVideoData.show_logo ){
  828. cv::Rect roi( cv::Point( m_sEmbedVideoData.logo_x, m_sEmbedVideoData.logo_y ), cv::Size( m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h ));
  829. cv::Mat destinationROI = m_mapMATS[tMD->mediaStreamId]( roi );
  830. m_mLogoPic.copyTo( destinationROI, m_mLogoMask );
  831. };
  832. vector<int> compression_params;
  833. //compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
  834. //compression_params.push_back(9);
  835. compression_params.push_back( cv::IMWRITE_JPEG_QUALITY );
  836. compression_params.push_back( m_sEmbedVideoData.compression_quality );
  837. m_mEncodedProtector.lock();
  838. m_mapCAPTURES[tMD->mediaStreamId].clear();
  839. cv::imencode( ".jpg", m_mapMATS[tMD->mediaStreamId], m_mapCAPTURES[tMD->mediaStreamId], compression_params );
  840. m_mEncodedProtector.unlock();
  841. /*ToDo: Move Broadcast Pic From Push Frame Procedure
  842. uint32_t DataSize = m_mapCAPTURES[tMD->mediaStreamId].size(); //buf.size();
  843. if( DataSize ) {
  844. uint8_t *pData = m_mapCAPTURES[tMD->mediaStreamId].data(); //buf.data();
  845. if( m_vHandlersPool.size() ) {
  846. for( vectorHANDLERS::iterator idx = m_vHandlersPool.begin(); idx < m_vHandlersPool.end(); idx++ ) {
  847. if( (*idx)->CTX->mediaStreamId == tMD->mediaStreamId ) {
  848. write( (*idx)->CTX->m_iEncoderPipe[1], &DataSize, sizeof(uint32_t));
  849. write( (*idx)->CTX->m_iEncoderPipe[1], pData, DataSize );
  850. };
  851. };
  852. };
  853. };
  854. */
  855. m_mPictureProtector.unlock();
  856. };
  857. return true;
  858. };
  859. bool argusModuleEmbedVideo::registerForMediaStream( uint32_t id, WEB_CLIENT_CTX *&CTX ){
  860. if( CTX ){
  861. if( id ){
  862. //Check if its Valid!
  863. DBC_MEDIA_STREAM_DATA *tSD = m_pDBC->getMediaStreamData( id );
  864. if( tSD ) {
  865. //if( !tSD->valid ) {
  866. // free( tSD );
  867. // return false;
  868. //};
  869. free( tSD );
  870. //if( pipe( CTX->m_iEncoderPipe ) == -1 ){
  871. // CTX->bValid = false;
  872. // return false;
  873. //};
  874. CTX->mediaStreamId = id;
  875. return true;
  876. };
  877. };
  878. } else LOG_ERROR("Client Context error!");
  879. return false;
  880. };
  881. void argusModuleEmbedVideo::dispatchEvent( EVENT_CONTAINER *Evt ){
  882. if( Evt ) {
  883. switch( Evt->m_eClass ) {
  884. case EVT_CLASS_IMAGE:
  885. case EVT_CLASS_VIDEO:{
  886. switch( Evt->m_eType ) {
  887. case EVT_CAPTURE_START:{
  888. #ifdef DEBUG
  889. LOG("Capture Event");
  890. #endif
  891. if( Evt->m_ref_id && Evt->m_db_id ){
  892. uint32_t tPicID = saveStreamPicture( Evt->m_ref_id );
  893. if( tPicID ){
  894. //
  895. //m_pDBC->addPictureEvent( Evt->m_db_id, tPicID );
  896. } else {
  897. #ifdef DEBUG
  898. LOG_ERROR("No Picture is Saved!");
  899. #endif
  900. };
  901. } else {
  902. #ifdef DEBUG
  903. LOG_ERROR("No Stream ID or Event ID in Event Data!");
  904. #endif
  905. };
  906. break;
  907. };
  908. };
  909. break;
  910. };
  911. case EVT_CLASS_CAMERA_CTRL:{
  912. switch ( Evt->m_eType ) {
  913. case EVT_CAMERA_ON_POSITION:{
  914. #ifdef DEBUG
  915. LOG("Cam On Position After Event -> Capture Picture");
  916. #endif
  917. DBC_SNAPSHOT_DATA *tData = (DBC_SNAPSHOT_DATA *) Evt->m_pEvtData;
  918. if( tData ){
  919. if( !saveStreamPicture( tData ) ) LOG_ERROR("Save Error!");
  920. tData->assoc_event_ids.clear();
  921. delete( tData );
  922. Evt->m_pEvtData = NULL;
  923. } else LOG_ERROR("No Evt Data!");
  924. break;
  925. };
  926. };
  927. break;
  928. };
  929. };
  930. };
  931. };
  932. //https://www.ps2-home.com/forum/viewtopic.php?t=98
  933. //https://www.mikekohn.net/software/playstation2.php
  934. bool argusModuleEmbedVideo::saveStreamPicture( DBC_SNAPSHOT_DATA *ptr ){
  935. if( ptr == NULL ) return false;
  936. if( ptr->assoc_event_ids.size() ){
  937. if( m_mapCAPTURES.find( ptr->stream_id ) != m_mapCAPTURES.end() ){
  938. m_mPictureProtector.lock();
  939. ptr->data = (uint8_t *) malloc( m_mapCAPTURES[ptr->stream_id].size() );
  940. if( ptr->data ){
  941. ptr->data_size = m_mapCAPTURES[ptr->stream_id].size();
  942. memcpy( ptr->data, m_mapCAPTURES[ptr->stream_id].data(), ptr->data_size );
  943. m_mPictureProtector.unlock();
  944. CSTR MD5 = MD5Binary( ptr->data, ptr->data_size );
  945. if( MD5 ){
  946. strncpy( ptr->md5, MD5, 32 );
  947. free( MD5 );
  948. if( !m_pDBC->addPicture( ptr, "jpg" ) ) LOG_ERROR("Database Error to consume capture!");
  949. } else LOG_ERROR("no MD5 Hash!");
  950. free( ptr->data );
  951. } else {
  952. m_mPictureProtector.unlock();
  953. LOG_ERROR("No memory to allocate!");
  954. };
  955. }
  956. #ifdef DEBUG
  957. else {
  958. LOG("No apropriate Stream Found!");
  959. }
  960. #endif
  961. ;
  962. };
  963. return true;
  964. };
  965. bool argusModuleEmbedVideo::saveStreamPicture( uint32_t id ){
  966. uint32_t retVal = 0;
  967. if( m_mapCAPTURES.find( id ) != m_mapCAPTURES.end() ){
  968. FILE *tFD;
  969. m_mPictureProtector.lock();
  970. size_t tBufferSize = m_mapCAPTURES[id].size();
  971. uint8_t * tBuffer = (uint8_t *) malloc( tBufferSize );
  972. if( tBuffer ){
  973. memcpy( tBuffer, m_mapCAPTURES[id].data(), tBufferSize );
  974. m_mPictureProtector.unlock();
  975. CSTR MD5 = MD5Binary( tBuffer, tBufferSize );
  976. if( MD5 ){
  977. char tFileName[MAX_PATH];
  978. if( generateSavePicPath( MD5, tFileName ) ){
  979. FILE *fd = fopen( tFileName, "wb+" );
  980. if( fd ){
  981. fwrite(tBuffer, 1, tBufferSize, fd );
  982. fclose(fd);
  983. chown( tFileName, "nobody", "nogroup" );
  984. //retVal = m_pDBC->addPicture( id, MD5, "jpg" );
  985. } else LOG_ERROR("Can not open picture file!");
  986. } else LOG_ERROR("Can not generate file name!");
  987. free(MD5);
  988. } else LOG_ERROR("no MD5 Hash!");
  989. free(tBuffer);
  990. } else {
  991. m_mPictureProtector.unlock();
  992. LOG_ERROR("no memory to allocate!");
  993. };
  994. };
  995. return retVal;
  996. };
  997. int argusModuleEmbedVideo::getWidh( void ){
  998. if( m_sEmbedVideoData.width ) return m_sEmbedVideoData.width;
  999. return -1;
  1000. };
  1001. int argusModuleEmbedVideo::getHeight( void ){
  1002. if( m_sEmbedVideoData.height ) return m_sEmbedVideoData.height;
  1003. return -1;
  1004. };
  1005. bool argusModuleEmbedVideo::generateSavePicPath( CSTR MD5, CSTR Output ){
  1006. if( MD5 == NULL ) return false;
  1007. if( Output == NULL ) return false;
  1008. if( !fileExist( m_pBaseSavePath ) ) {
  1009. LOG_ERROR("No Base Picture Save Path Defined!");
  1010. return false;
  1011. };
  1012. if( strlen(MD5) < 4 ) return false;
  1013. char test[MAX_PATH];
  1014. sprintf( test, "%s/%c%c", m_pBaseSavePath, MD5[0], MD5[1] );
  1015. if( !fileExist( test ) ) {
  1016. #ifdef __linux__
  1017. mkdir( test, STORAGE_DIR_MODE );
  1018. #else
  1019. _mkdir( test );
  1020. #endif
  1021. chown( test, "nobody", "nogroup" );
  1022. };
  1023. sprintf( test, "%s/%c%c/%c%c", m_pBaseSavePath, MD5[0], MD5[1], MD5[2], MD5[3] );
  1024. if( !fileExist( test ) ) {
  1025. #ifdef __linux__
  1026. mkdir( test, STORAGE_DIR_MODE );
  1027. #else
  1028. _mkdir( test );
  1029. #endif
  1030. chown( test, "nobody", "nogroup" );
  1031. };
  1032. sprintf( Output, "%s/%c%c/%c%c/%s.jpg", m_pBaseSavePath, MD5[0], MD5[1], MD5[2], MD5[3], MD5 );
  1033. return true;
  1034. };
  1035. #undef LOG
  1036. #undef LOG_ERROR
  1037. #undef TLOG
  1038. #undef TLOG_ERROR