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.

700 lines
29 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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 LOG( X ) argusKernelModule::m_pLog->Log( "MOD_EVIDEO", this, X )
  26. #ifdef DEBUG
  27. #define LOG_ERROR( X ) {\
  28. char _tBfrErrMSG2w34[256];\
  29. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  30. argusKernelModule::m_pLog->Log( "MOD_EVIDEO", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  31. }
  32. #else
  33. #define LOG_ERROR( X ) argusKernelModule::m_pLog->Log( "MOD_EVIDEO", this, _LOG_ERROR, X );
  34. #endif
  35. #define TLOG( X ) basicLogCollection::getInstance()->Log( "MOD_EVIDEO_T", CTX, X )
  36. #define TLOG_ERROR( X ) basicLogCollection::getInstance()->Log( "MOD_EVIDEO_T", CTX, _LOG_ERROR, X )
  37. enum {
  38. WEBRTC_REQ_SIGNIN = 0,
  39. WEBRTC_REQ_WAIT = 1,
  40. WEBRTC_REQ_SIGNOUT = 2,
  41. WEBRTC_REQ_MESSAGE = 3
  42. };
  43. static const char* kRequestPaths[] = { "/stream", "/sign_in", "/wait", "/sign_out", "/message" };
  44. //buffer_data bd[1]={0};
  45. argusModuleEmbedVideo::argusModuleEmbedVideo( uint32_t moduleID ): argusKernelModule( moduleID, ARGUS_MODULE_EMBED_VIDEO ), basicMediaDestination(), basicThread(){
  46. m_pEventHeap = basicEventHeap::getInstance();
  47. DBC_MODULE_EMBED_VIDEO_DATA *tDATA = m_pDBC->getEmbedVideoData( m_nDBAUXID );
  48. if( tDATA ) {
  49. memcpy( &m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA) );
  50. free( tDATA );
  51. } else {
  52. memset( &m_sEmbedVideoData, 0, sizeof(DBC_MODULE_EMBED_VIDEO_DATA) );
  53. m_sEmbedVideoData.port = 8972;
  54. };
  55. char tB[64];
  56. sprintf( tB, "Web Streams Port: %d", m_sEmbedVideoData.port );
  57. LOG( tB );
  58. sprintf( tB, "Frame: [%dx%d]", m_sEmbedVideoData.width, m_sEmbedVideoData.height );
  59. LOG( tB );
  60. n_pNoSignalData = NULL;
  61. m_nNoSignalSize = 0;
  62. if( m_sEmbedVideoData.show_ts ) LOG("Show TimeStamp");
  63. if( m_sEmbedVideoData.show_label ) LOG("Show Label");
  64. if( m_sEmbedVideoData.show_logo ){
  65. LOG("Show Logo");
  66. LOG( m_sEmbedVideoData.logo_md5 );
  67. };
  68. };
  69. argusModuleEmbedVideo::~argusModuleEmbedVideo(){
  70. if( n_pNoSignalData ) free( n_pNoSignalData );
  71. m_bEnabled = false;
  72. stopService();
  73. };
  74. void *argusModuleEmbedVideo::webClientProc( void *ptr ){
  75. static char *head = "HTTP/1.1 200 OK\r\n"
  76. "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
  77. "Pragma: no-cache\r\n"
  78. "MIME-version: 1.0\r\n"
  79. "Connection: close\r\n"
  80. //"Keep-Alive: timeout=5, max=1\r\n"
  81. //"Connection: Keep-Alive\r\n"
  82. //"Content-Type: video/mp4\r\n"
  83. "Content-Transfer-Encoding: BINARY\r\n"
  84. //"Transfer-Encoding: chunked\r\n"
  85. "Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n"
  86. "\r\n";
  87. static char *abort = "HTTP/1.1 404 OK\r\n"
  88. "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
  89. "Pragma: no-cache\r\n"
  90. "Connection: close\r\n\r\n";
  91. static char *no_signal ="HTTP/1.1 200 OK\r\n"
  92. "MIME-version: 1.0\r\n"
  93. "Connection: close\r\n"
  94. "Content-Transfer-Encoding: BINARY\r\n"
  95. //"Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n\r\n"
  96. "Content-Type: image/jpeg\r\nContent-length: %d\r\n\r\n";
  97. WEB_CLIENT_CTX * CTX = (WEB_CLIENT_CTX *) ptr;
  98. argusModuleEmbedVideo * MODULE = CTX->ModulePtr;
  99. if( CTX ) {
  100. char mesg[1024];
  101. size_t RecvSize;
  102. struct pollfd pfd[1];
  103. char cGetURI[1024];
  104. bool m_bDeflatePerMessage = false;
  105. bool m_pInit;
  106. #ifdef DEBUG_EXTRA
  107. pid_t pid = gettid();
  108. char pidBuf[16];
  109. sprintf( pidBuf, "pid:%d", pid);
  110. TLOG( pidBuf );
  111. #endif
  112. pfd->fd = CTX->m_iSocketFID;
  113. pfd->events = POLLIN | POLLHUP;
  114. while( CTX->bValid ) {
  115. if( !CTX->initDone ){
  116. int RetVal;
  117. char * reqline[256];
  118. RetVal = poll( pfd, 1, 1000 );
  119. if( RetVal <= 0 ){
  120. if ( EINTR == errno ) continue;
  121. TLOG_ERROR("Unrecoverable error");
  122. CTX->bValid = false;
  123. continue;
  124. };
  125. memset( mesg, 0, sizeof(mesg) );
  126. RetVal = recv( CTX->m_iSocketFID, mesg, sizeof(mesg), 0 );
  127. if ( RetVal <= 0 ) {
  128. sprintf( mesg, "web recv() error: %s", strerror(errno));
  129. TLOG_ERROR( mesg );
  130. CTX->bValid = false;
  131. continue;
  132. };
  133. //TLOG( mesg );
  134. uint32_t index = 0;
  135. reqline[index] = strtok( mesg, "\n" );
  136. if( strncmp( reqline[index], "GET", 3 ) == 0 ) {
  137. char * pPartURI;
  138. char * pPartPROTO;
  139. pPartURI = strstr( reqline[index], " " );
  140. if( pPartURI != NULL ) {
  141. pPartURI++;
  142. pPartPROTO = strstr( pPartURI, " " );
  143. pPartPROTO++;
  144. int URILen = pPartPROTO - pPartURI;
  145. strncpy( cGetURI, pPartURI, URILen );
  146. cGetURI[URILen] = (char)0;
  147. if(strncmp( pPartPROTO, "HTTP/1.0", 8) != 0 && strncmp( pPartPROTO, "HTTP/1.1", 8) != 0) {
  148. TLOG_ERROR("No HTTP version supplied in request!");
  149. if( write( CTX->m_iSocketFID, "HTTP/1.1 400 Bad Request\r\n", 26) < 0 ){
  150. sprintf( mesg, "Failed returning status 400 to client: %s", strerror(errno));
  151. TLOG_ERROR(mesg);
  152. };
  153. CTX->bValid = false;
  154. continue;
  155. };
  156. };
  157. TLOG( cGetURI );
  158. };
  159. // parse rest of request
  160. while( ( reqline[++index] = strtok( NULL, "\n" ) )!= NULL ) {
  161. if( strncmp( reqline[index], "Connection:", 11 ) == 0 ) {
  162. char * pPart;
  163. pPart = strcasestr( reqline[index], "Upgrade" );
  164. if( pPart != NULL ) {
  165. TLOG("Upgrade Mode ON");
  166. //bUpgradeMode = true;
  167. };
  168. };
  169. if( strncmp( reqline[index], "Upgrade:", 8 ) == 0 ) {
  170. /*
  171. if( bUpgradeMode ) {
  172. char * pPart;
  173. pPart = strcasestr( reqline[index], "websocket" );
  174. if( pPart != NULL ) {
  175. TLOG("WebSocket Mode ON");
  176. //bWebSocketMode = true;
  177. };
  178. };
  179. */
  180. };
  181. if( strncmp( reqline[index], "Sec-WebSocket-Version:", 22 ) == 0 ) {
  182. char * pPart;
  183. pPart = strstr( reqline[index], " " );
  184. if( pPart != NULL ) {
  185. char tBuf[CTEXT];
  186. //nWebSocketVersion = atoi(pPart+1);
  187. sprintf( tBuf,"Web Socket Version: %d", 0 );//nWebSocketVersion );
  188. TLOG( tBuf );
  189. };
  190. };
  191. if( strncmp( reqline[index], "Sec-WebSocket-Key:", 18 ) == 0 ) {
  192. /*char * pPart;
  193. pPart = strstr( reqline[index], " " );
  194. if( pPart != NULL ) {
  195. char tGenStr[256];
  196. unsigned char * tBase64;
  197. size_t nBase64;
  198. int kLen;
  199. unsigned char hash[SHA_DIGEST_LENGTH]; // == 20
  200. pPart++;
  201. kLen = strstr(pPart,"\r") - pPart;
  202. strncpy(cWSKey,pPart,kLen);
  203. cWSKey[kLen]=(char)0;
  204. TLOG( cWSKey );
  205. sprintf( tGenStr, "%s%s", cWSKey, ws_guid );
  206. SHA1( (const unsigned char*)tGenStr, strlen(tGenStr), hash );
  207. tBase64 = base64_encode( hash, SHA_DIGEST_LENGTH, &nBase64 );
  208. strncpy( cWSRep, (const char*)tBase64, nBase64 );
  209. free( tBase64 );
  210. cWSRep[nBase64] = (char)0;
  211. TLOG( cWSRep );
  212. };
  213. */
  214. };
  215. if( strncmp( reqline[index], "Sec-WebSocket-Protocol:", 23 ) == 0 ) {
  216. char * pPart;
  217. pPart = strstr( reqline[index], " " );
  218. if( pPart != NULL ) {
  219. // Check for protos
  220. };
  221. };
  222. if( strncmp( reqline[index], "Sec-WebSocket-Extensions:", 25 ) == 0 ) {
  223. char * pPart;
  224. pPart = strstr( reqline[index], " " );
  225. if( pPart != NULL ) {
  226. pPart++;
  227. if(strncmp( pPart, "permessage-deflate", 18 ) == 0 ) {
  228. m_bDeflatePerMessage = true;
  229. TLOG("Using Deflate");
  230. };
  231. };
  232. };
  233. if( strncmp( reqline[index],"Accept-Encoding:", 16 ) == 0 ){
  234. char * pPart;
  235. pPart = strstr( reqline[index], " " );
  236. if( pPart != NULL ) {
  237. pPart++;
  238. if(strncmp( pPart, "gzip, deflate", 13 ) == 0 ) {
  239. m_bDeflatePerMessage = true;
  240. TLOG("Using Deflate");
  241. };
  242. };
  243. };
  244. };
  245. if( strncmp( cGetURI, "/stream", 7 ) == 0 ) {
  246. uint32_t tStreamID = 0;
  247. CSTR tP;
  248. if( tP = strstr( cGetURI, "?" ) ) {
  249. CSTR tB;
  250. tP++;
  251. if( tB = strstr( tP, "id=" ) ) {
  252. tStreamID = atoi( tB+3 );
  253. };
  254. };
  255. if( tStreamID ) {
  256. char tB[64];
  257. sprintf(tB,"StremID: %d", tStreamID );
  258. TLOG( tB );
  259. if( CTX->ModulePtr->registerForMediaStream( tStreamID, CTX ) ){
  260. // Send Header
  261. if( send( CTX->m_iSocketFID, head, strlen(head), 0 ) < 0 ){
  262. sprintf( mesg, "Failed sending file to client: %s", strerror(errno));
  263. TLOG_ERROR(mesg);
  264. CTX->bValid = false;
  265. };
  266. CTX->initDone = true;
  267. continue;
  268. } else {
  269. TLOG("Stream id Not Online");
  270. };
  271. } else TLOG("No Stream ID");
  272. };
  273. if( CTX->ModulePtr->n_pNoSignalData ) {
  274. sprintf( mesg, no_signal, CTX->ModulePtr->m_nNoSignalSize );
  275. send( CTX->m_iSocketFID, mesg, strlen(mesg), 0 );
  276. send( CTX->m_iSocketFID, CTX->ModulePtr->n_pNoSignalData, CTX->ModulePtr->m_nNoSignalSize, 0 );
  277. send( CTX->m_iSocketFID, "\r\n\r\n", 4, 0 );
  278. TLOG("Sent No Signal Image!");
  279. } else {
  280. // Send 404 Header
  281. send( CTX->m_iSocketFID, abort, strlen(abort), 0 );
  282. TLOG("Aborted!");
  283. };
  284. CTX->bValid = false;
  285. continue;
  286. } else {
  287. uint32_t msgSize;
  288. int retVal;
  289. retVal = read( CTX->m_iEncoderPipe[0], &msgSize, sizeof(uint32_t));
  290. if( retVal == -1 ){
  291. if( errno == EAGAIN || errno == EWOULDBLOCK ){
  292. continue;
  293. };
  294. TLOG_ERROR("Pipe Communication Error[1]!");
  295. CTX->bValid = false;
  296. continue;
  297. };
  298. uint8_t *tBuf = (uint8_t *) malloc(msgSize);
  299. if( tBuf ){
  300. retVal = read( CTX->m_iEncoderPipe[0], tBuf, msgSize );
  301. if( retVal == -1 ){
  302. free( tBuf );
  303. if( errno == EAGAIN || errno == EWOULDBLOCK ){
  304. continue;
  305. };
  306. TLOG_ERROR("Pipe Communication Error[2]!");
  307. CTX->bValid = false;
  308. continue;
  309. };
  310. sprintf( mesg, "--MPFrame\r\nContent-type: image/jpeg\r\nContent-length: %d\r\n\r\n", msgSize );
  311. if( send( CTX->m_iSocketFID, mesg, strlen( mesg ), 0 )< 0 ){
  312. sprintf( mesg, "[1]Failed sending file to client: %s", strerror(errno));
  313. TLOG_ERROR(mesg);
  314. CTX->bValid = false;
  315. continue;
  316. };
  317. if( send( CTX->m_iSocketFID, tBuf, msgSize, 0 ) < 0 ){
  318. sprintf( mesg, "[2]Failed sending file to client: %s", strerror(errno));
  319. TLOG_ERROR(mesg);
  320. CTX->bValid = false;
  321. continue;
  322. };
  323. if( send( CTX->m_iSocketFID, "\r\n\r\n", 4, 0 )< 0 ){
  324. sprintf( mesg, "[3]Failed sending file to client: %s", strerror(errno));
  325. TLOG_ERROR(mesg);
  326. CTX->bValid = false;
  327. continue;
  328. };
  329. free( tBuf );
  330. } else {
  331. TLOG_ERROR("Mem Alloc Error!");
  332. CTX->bValid = false;
  333. };
  334. };
  335. };
  336. };
  337. CTX->bValid = false;
  338. TLOG("WEB Client Handler Ended!");
  339. close( CTX->m_iEncoderPipe[0] );
  340. shutdown( CTX->m_iSocketFID, SHUT_RDWR);
  341. close( CTX->m_iSocketFID );
  342. //pthread_exit( NULL );
  343. return NULL;
  344. };
  345. void argusModuleEmbedVideo::m_fThreadProc( void ){
  346. if( !m_bEnabled ) {
  347. #ifdef DEBUG
  348. LOG("Module is Disabled!");
  349. #endif
  350. return;
  351. };
  352. #ifdef DEBUG
  353. LOG("Module Started!");
  354. #endif
  355. struct pollfd pfd[1];
  356. pfd->fd = m_nSocketDescriptor;
  357. pfd->events = POLLIN | POLLHUP;
  358. m_bThreadStatus = TH_WORKING;
  359. while( m_bThreadStatus < TH_ENDED ) {
  360. if( !m_bEnabled ) {
  361. m_bThreadStatus = TH_ENDED;
  362. continue;
  363. };
  364. int RetVal = poll( pfd, 1, 1000 );
  365. if( RetVal < 0 ){
  366. if( EINTR == errno ) continue;
  367. LOG_ERROR("Unrecoverable error");
  368. m_bThreadStatus = TH_ENDED;
  369. continue;
  370. };
  371. // Handle Connections
  372. if( RetVal != 0 ){
  373. if ( pfd[0].revents & POLLIN ) {
  374. LOG("Handle New Client");
  375. int tSID = accept( pfd[0].fd, NULL, NULL);
  376. if( tSID < 0 ){
  377. char tBuf[CTEXT];
  378. sprintf(tBuf, "Accept() error: %s", strerror(errno));
  379. LOG_ERROR(tBuf);
  380. } else {
  381. LOG("Accept OK");
  382. WEB_CLIENT_CTX *tCTX = (WEB_CLIENT_CTX *) malloc(sizeof(WEB_CLIENT_CTX));
  383. if( tCTX ) {
  384. LOG("CTX OK");
  385. WEB_HANDLER *tHANDLER = (WEB_HANDLER *) malloc(sizeof(WEB_HANDLER));
  386. if( tHANDLER ) {
  387. LOG("HANDLER OK");
  388. memset( tCTX, 0, sizeof(WEB_CLIENT_CTX) );
  389. memset( tHANDLER, 0, sizeof(WEB_HANDLER) );
  390. tCTX->m_iSocketFID = tSID;
  391. tCTX->m_iEncoderPipe[0] = -1;
  392. tCTX->m_iEncoderPipe[1] = -1;
  393. tCTX->mediaStreamId = 0;
  394. tCTX->bValid = true;
  395. tCTX->ModulePtr = this;
  396. LOG("Before Thread Create");
  397. int RetVal = pthread_create( &tHANDLER->m_hThreadHandle, NULL, webClientProc, ( void * ) tCTX );
  398. if( RetVal == -1 ) {
  399. free(tCTX);
  400. free(tHANDLER);
  401. LOG_ERROR("Thread Creation Faled.");
  402. shutdown( tSID, SHUT_RDWR );
  403. close( tSID );
  404. } else {
  405. tHANDLER->CTX = tCTX;
  406. m_mHandlersProtector.lock();
  407. m_vHandlersPool.push_back( tHANDLER );
  408. m_mHandlersProtector.unlock();
  409. LOG("Handler Pushed to a Pool");
  410. };
  411. } else { //https://stackoverflow.com/questions/18545077/image-fullscreen-on-click
  412. free(tCTX);
  413. LOG_ERROR("CL_HANDLER Mem Allocation Failure!");
  414. shutdown( tSID, SHUT_RDWR );
  415. close( tSID );
  416. };
  417. } else {
  418. LOG_ERROR("CL_CTX Mem Allocation Failure!");
  419. shutdown( tSID, SHUT_RDWR );
  420. close( tSID );
  421. };
  422. };
  423. };
  424. };
  425. // Handle Dead Clients
  426. if( m_vHandlersPool.size() ) {
  427. for( vectorHANDLERS::iterator idx = m_vHandlersPool.begin(); idx < m_vHandlersPool.end(); idx++ ) {
  428. if( !( (*idx)->CTX->bValid ) ) {
  429. pthread_join( (*idx)->m_hThreadHandle, NULL );
  430. //close( (*idx)->CTX->m_iEncoderPipe[0] );
  431. close( (*idx)->CTX->m_iEncoderPipe[1] );
  432. free( (*idx)->CTX );
  433. free( (*idx) );
  434. m_mHandlersProtector.lock();
  435. m_vHandlersPool.erase(idx);
  436. m_mHandlersProtector.unlock();
  437. #ifdef DEBUG
  438. LOG("Web Handler Ripped!");
  439. #endif
  440. };
  441. };
  442. };
  443. SLEEP( 50 );
  444. };
  445. };
  446. #define ENABLE_SOCKOPT(sd, level, opt) \
  447. do { \
  448. int val = 1; \
  449. if (setsockopt(sd, level, opt, &val, sizeof(val)) < 0) \
  450. warn("Failed enabling %s for web service", #opt); \
  451. } while (0);
  452. bool argusModuleEmbedVideo::startService(){
  453. LOG("Create");
  454. //Create NoSignal Image
  455. cv::Mat tMat( m_sEmbedVideoData.height, m_sEmbedVideoData.width, CV_8UC3 );
  456. cv::rectangle( tMat, cv::Point( 0, 0 ), cv::Point( m_sEmbedVideoData.width, m_sEmbedVideoData.height ), cv::Scalar( 255, 0, 0 ), cv::FILLED );
  457. cv::putText( tMat, "NO SIGNAL", cv::Point( 10, 30 ), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar( 0, 255, 255 ), 3 );
  458. if( m_sEmbedVideoData.show_logo ) {
  459. int a_x, a_y;
  460. if( m_sEmbedVideoData.logo_x < 0 ) {
  461. a_x = m_sEmbedVideoData.width + m_sEmbedVideoData.logo_x - m_sEmbedVideoData.logo_w;
  462. if( a_x < 0 ) a_x = 0;
  463. } else a_x = m_sEmbedVideoData.logo_x;
  464. if( ( a_x + m_sEmbedVideoData.logo_w ) > m_sEmbedVideoData.width ) a_x = m_sEmbedVideoData.width - m_sEmbedVideoData.logo_w;
  465. if( m_sEmbedVideoData.logo_y < 0 ) {
  466. a_y = m_sEmbedVideoData.height + m_sEmbedVideoData.logo_y - m_sEmbedVideoData.logo_h;
  467. if( a_y < 0 ) a_y = 0;
  468. } else a_y = m_sEmbedVideoData.logo_y;
  469. if( ( a_y + m_sEmbedVideoData.logo_h ) > m_sEmbedVideoData.height ) a_y = m_sEmbedVideoData.height - m_sEmbedVideoData.logo_h;
  470. if( ( a_x > -1 ) && ( a_y > -1 ) ) {
  471. char tName[256];
  472. m_sEmbedVideoData.logo_x = a_x;
  473. m_sEmbedVideoData.logo_y = a_y;
  474. sprintf( tName, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5 );
  475. cv::Mat T = cv::imread( tName, cv::IMREAD_UNCHANGED );
  476. if( T.cols > 0 ) {
  477. cv::Mat mask;
  478. vector<cv::Mat> layers;
  479. cv::split( T, layers); // seperate channels
  480. cv::Mat rgb[3] = { layers[0], layers[1], layers[2] };
  481. mask = layers[3]; // png's alpha channel used as mask
  482. cv::merge( rgb, 3, T ); // put together the RGB channels, now transp insn't transparent
  483. cv::resize( T, m_mLogoPic, cv::Size( m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h ), cv::INTER_CUBIC );
  484. cv::resize( mask, m_mLogoMask, cv::Size( m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h ), cv::INTER_CUBIC );
  485. cv::Rect roi(cv::Point( m_sEmbedVideoData.logo_x, m_sEmbedVideoData.logo_y ), cv::Size( m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h ));
  486. cv::Mat destinationROI = tMat( roi );
  487. m_mLogoPic.copyTo( destinationROI, m_mLogoMask );
  488. //m_sEmbedVideoData.show_logo = false;
  489. } else {
  490. LOG("Logo Disabled - No Picture is Loaded");
  491. m_sEmbedVideoData.show_logo = false;
  492. };
  493. } else {
  494. LOG("Logo Disabled - Wrong Coords");
  495. m_sEmbedVideoData.show_logo = false;
  496. };
  497. };
  498. vector<int> compression_params;
  499. //compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
  500. //compression_params.push_back(9);
  501. compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
  502. compression_params.push_back(75);
  503. vector<uchar> buf;
  504. cv::imencode( ".jpg", tMat, buf, compression_params );
  505. m_nNoSignalSize = buf.size();
  506. n_pNoSignalData = (uint8_t *) malloc( m_nNoSignalSize );
  507. if( n_pNoSignalData ) {
  508. memcpy( n_pNoSignalData, buf.data(), m_nNoSignalSize );
  509. } else m_nNoSignalSize =0;
  510. buf.clear();
  511. struct sockaddr sa;
  512. struct sockaddr_in *sin;
  513. memset(&sa, 0, sizeof(struct sockaddr));
  514. sa.sa_family = AF_INET;
  515. sin = (struct sockaddr_in *)&sa;
  516. sin->sin_addr.s_addr = htonl( INADDR_ANY );
  517. sin->sin_port = htons( m_sEmbedVideoData.port );
  518. m_nSocketDescriptor = socket( sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
  519. if( m_nSocketDescriptor == -1 ){
  520. LOG_ERROR("Failed creating ACC socket");
  521. return false;
  522. };
  523. ENABLE_SOCKOPT( m_nSocketDescriptor, SOL_SOCKET, SO_REUSEADDR);
  524. #ifdef SO_REUSEPORT
  525. ENABLE_SOCKOPT( m_nSocketDescriptor, SOL_SOCKET, SO_REUSEPORT);
  526. #endif
  527. if( bind( m_nSocketDescriptor, &sa, sizeof(sa)) < 0 ){
  528. LOG_ERROR("Failed binding Socket");
  529. close( m_nSocketDescriptor );
  530. return false;
  531. } else {
  532. LOG("Bind Socessfull");
  533. };
  534. if( listen( m_nSocketDescriptor, 10) != 0 ){
  535. LOG_ERROR("Failed setting web listen backlog");
  536. close( m_nSocketDescriptor );
  537. return false;
  538. } else {
  539. LOG("Listen OK");
  540. };
  541. return init();
  542. };
  543. bool argusModuleEmbedVideo::stopService(){
  544. waitThreadEnd( true );
  545. /*
  546. if( m_vThreads.size() ) { // Kill-Wait Started Probes
  547. #ifdef DEBUG
  548. LOG("Have to Wait Probes!");
  549. #endif
  550. for( vectorOBSERVER_CTX::iterator index = m_vThreads.begin(); index < m_vThreads.end(); index++ ) {
  551. pthread_join( (*index)->m_hThreadHandle, NULL );
  552. };
  553. m_vThreads.clear();
  554. };
  555. #ifdef DEBUG
  556. LOG("No More Probes!");
  557. #endif
  558. if (m_vResultPool.size()){ // Clear structures
  559. for( vectorOBSERVER_RESULT::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
  560. free(*index);
  561. };
  562. m_vResultPool.clear();
  563. };
  564. #ifdef DEBUG
  565. LOG("Data is Cleared!");
  566. #endif
  567. */
  568. return true;
  569. };
  570. bool argusModuleEmbedVideo::reloadSettings(){
  571. return true;
  572. };
  573. void argusModuleEmbedVideo::dispatchEvent(EVENT_CONTAINER *Evt){
  574. };
  575. bool argusModuleEmbedVideo::validate(){
  576. return true;
  577. };
  578. void argusModuleEmbedVideo::pushFrame( AVFrame *Frame, void* src){
  579. MEDIA_SOURCE_DESC * tMD = (MEDIA_SOURCE_DESC *) src;
  580. if( tMD ) {
  581. };
  582. };
  583. void argusModuleEmbedVideo::pushFrame( cv::Mat &Frame, void* src ){
  584. MEDIA_SOURCE_DESC * tMD = (MEDIA_SOURCE_DESC *) src;
  585. if( tMD ) {
  586. if( m_vHandlersPool.size() ) {
  587. cv::Mat RD;
  588. cv::resize( Frame, RD, cv::Size( m_sEmbedVideoData.width, m_sEmbedVideoData.height ), cv::INTER_LINEAR );
  589. if( m_sEmbedVideoData.show_ts ){
  590. char DateStr[200];
  591. time_t t = time( NULL );
  592. struct tm * tmp = gmtime( &t );
  593. if( tmp ){
  594. if( strftime( DateStr, sizeof(DateStr), "%F %T GMT", tmp )!=0 ){
  595. cv::rectangle( RD, cv::Point( 10, 10 ), cv::Point( 490, 60 ), cv::Scalar( 0, 0, 0 ), cv::FILLED );
  596. cv::putText( RD, DateStr, cv::Point( 30, 45 ),
  597. (cv::HersheyFonts) m_sEmbedVideoData.ts_font_face,
  598. m_sEmbedVideoData.ts_font_scale, cv::Scalar( 0, 255, 255 ),
  599. m_sEmbedVideoData.ts_font_thickness,
  600. m_sEmbedVideoData.ts_line_type
  601. );
  602. };
  603. };
  604. };
  605. if( m_sEmbedVideoData.show_logo ){
  606. cv::Rect roi( cv::Point( m_sEmbedVideoData.logo_x, m_sEmbedVideoData.logo_y ), cv::Size( m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h ));
  607. cv::Mat destinationROI = RD( roi );
  608. m_mLogoPic.copyTo( destinationROI, m_mLogoMask );
  609. };
  610. vector<int> compression_params;
  611. //compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
  612. //compression_params.push_back(9);
  613. compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
  614. compression_params.push_back(75);
  615. vector<uchar> buf;
  616. uint32_t DataSize;
  617. cv::imencode( ".jpg", RD, buf, compression_params );
  618. DataSize = buf.size();
  619. uint8_t *pData = buf.data();
  620. for( vectorHANDLERS::iterator idx = m_vHandlersPool.begin(); idx < m_vHandlersPool.end(); idx++ ) {
  621. if( (*idx)->CTX->mediaStreamId == tMD->mediaStreamId ) {
  622. write( (*idx)->CTX->m_iEncoderPipe[1], &DataSize, sizeof(uint32_t));
  623. write( (*idx)->CTX->m_iEncoderPipe[1], pData, DataSize );
  624. };
  625. };
  626. };
  627. };
  628. };
  629. bool argusModuleEmbedVideo::registerForMediaStream( uint32_t id, WEB_CLIENT_CTX *&CTX ){
  630. if( CTX ){
  631. if( id ){
  632. //Check if its Valid!
  633. DBC_MEDIA_STREAM_DATA *tSD = m_pDBC->getMediaStreamData( id );
  634. if( tSD ) {
  635. if( ! tSD->valid ) {
  636. free( tSD );
  637. return false;
  638. };
  639. free( tSD );
  640. if( pipe( CTX->m_iEncoderPipe ) == -1 ){
  641. CTX->bValid = false;
  642. return false;
  643. };
  644. CTX->mediaStreamId = id;
  645. return true;
  646. };
  647. };
  648. } else LOG_ERROR("Client Context error!");
  649. return false;
  650. };
  651. #undef LOG
  652. #undef LOG_ERROR
  653. #undef TLOG
  654. #undef TLOG_ERROR