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.

423 lines
10 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <sys/socket.h>
  5. #include <time.h>
  6. #include <unistd.h>
  7. #include <pthread.h>
  8. #include <math.h>
  9. #include <vector>
  10. #include <stdio.h>
  11. #include <unistd.h> // UNIX standard function definitions
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <sys/ioctl.h>
  15. #include <netinet/in.h>
  16. #include <netdb.h>
  17. #include <opencv2/opencv.hpp>
  18. using namespace cv;
  19. using namespace std;
  20. #define IMG_SYM
  21. #define NPH
  22. #ifdef NPH
  23. #define CHUNKED
  24. #ifdef CHUNKED
  25. #define CHUNK_SIZE 4118
  26. #endif
  27. #endif
  28. bool Exit_State = false;
  29. int pipefd[2];
  30. VideoCapture cap;
  31. pthread_t thread;
  32. // SIGPIPE
  33. void SIGPIPE_handler( int sig ){
  34. if( sig == SIGPIPE ){
  35. Exit_State = true;
  36. pthread_join( thread, NULL );
  37. };
  38. };
  39. #ifdef IMG_SYM
  40. int cStep = 0;
  41. void createAlphaMat(Mat &mat){
  42. CV_Assert(mat.channels() == 4);
  43. for (int i = 0; i < mat.rows; ++i) {
  44. for (int j = 0; j < mat.cols; ++j) {
  45. Vec4b& bgra = mat.at<Vec4b>(i, j);
  46. bgra[0] = UCHAR_MAX; // Blue
  47. bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
  48. bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
  49. bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
  50. };
  51. };
  52. Point Center( mat.cols / 2 + 20.0 * cos( cStep * ( 3.14 / 20.0 ) ), mat.rows / 2 + 20.0 * sin( cStep * ( 3.14 / 20.0 ) ) );
  53. cStep++;
  54. if( cStep == 40 ) cStep = 0;
  55. circle( mat, Center, 5, Scalar(255,255,0) );
  56. char DateStr[200];
  57. time_t t;
  58. struct tm *tmp;
  59. t = time(NULL);
  60. tmp = gmtime(&t);
  61. if( tmp == NULL) {
  62. perror("localtime");
  63. return;
  64. };
  65. if( strftime( DateStr, sizeof(DateStr), "%F %T GMT", tmp )==0 ){
  66. fprintf( stderr, "strftime returned 0" );
  67. return;
  68. };
  69. putText( mat, DateStr, Point( 5, 20 ), FONT_HERSHEY_SIMPLEX, .2, Scalar( 0, 255, 255 ) );
  70. };
  71. void putTimeStamp(Mat &mat){
  72. char DateStr[200];
  73. time_t t;
  74. struct tm *tmp;
  75. t = time(NULL);
  76. tmp = gmtime(&t);
  77. if( tmp == NULL) {
  78. perror("localtime");
  79. return;
  80. };
  81. if( strftime( DateStr, sizeof(DateStr), "%F %T GMT", tmp )==0 ){
  82. fprintf( stderr, "strftime returned 0" );
  83. return;
  84. };
  85. rectangle( mat, Point( 720, 840 ), Point( 1200, 890 ), Scalar( 0, 0, 0 ), CV_FILLED );
  86. putText( mat, DateStr, Point( 740, 875 ), FONT_HERSHEY_SIMPLEX, 1, Scalar( 0, 255, 255 ), 3 );
  87. };
  88. void putTimeS(Mat &mat){
  89. char DateStr[200];
  90. time_t t;
  91. struct tm *tmp;
  92. t = time(NULL);
  93. tmp = gmtime(&t);
  94. if( tmp == NULL) {
  95. perror("localtime");
  96. return;
  97. };
  98. if( strftime( DateStr, sizeof(DateStr), "%F %T GMT", tmp )==0 ){
  99. fprintf( stderr, "strftime returned 0" );
  100. return;
  101. };
  102. rectangle( mat, Point( 10, 10 ), Point( 490, 60 ), Scalar( 0, 0, 0 ), CV_FILLED );
  103. putText( mat, DateStr, Point( 30, 45 ), FONT_HERSHEY_SIMPLEX, 1, Scalar( 0, 255, 255 ), 3 );
  104. };
  105. #endif
  106. void sendStaticBugaBuga( void ){
  107. FILE * fid;
  108. char Buffer[1024];
  109. uint8_t BigData[2048000];
  110. int DataSize;
  111. fid = fopen("nosignal.jpg","rb");
  112. if( fid != NULL ) {
  113. DataSize = fread( BigData,1,2048000,fid);
  114. fclose(fid);
  115. if( DataSize > 0 ){
  116. //Exit_State = true;
  117. while( ! Exit_State ) {
  118. sprintf( Buffer, "Content-type: image/jpeg\r\nContent-length: %d\r\n\r\n", DataSize );
  119. write( pipefd[1], Buffer, strlen( Buffer ) );
  120. write( pipefd[1], BigData, DataSize );
  121. sprintf( Buffer, "\r\n\r\n--MPFrame\r\n" );
  122. write( pipefd[1], Buffer, strlen( Buffer ) );
  123. usleep(500000);
  124. };
  125. };
  126. };
  127. };
  128. void *video_capture_function( void *patr ){
  129. int DataSize = 0;
  130. int count;
  131. int m_nSocketFD;
  132. int iResult;
  133. struct addrinfo * result;
  134. struct addrinfo * ptr;
  135. struct addrinfo hints;
  136. struct sockaddr_in serv_addr;
  137. char cBuffer[256];
  138. struct hostent * server;
  139. m_nSocketFD = socket(AF_INET, SOCK_STREAM, 0);
  140. if( m_nSocketFD < 0 ){
  141. sendStaticBugaBuga();
  142. printf("ERROR creating socket");
  143. return NULL;
  144. };
  145. server = gethostbyname( "localhost" );
  146. if( server == NULL ){
  147. sendStaticBugaBuga();
  148. printf( "ERROR, no such host [%s]", "localhost" );
  149. return NULL;
  150. };
  151. bzero((char *) &serv_addr, sizeof(serv_addr));
  152. serv_addr.sin_family = AF_INET;
  153. bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length );
  154. serv_addr.sin_port = htons( 2222 );
  155. struct timeval tv;
  156. tv.tv_sec = 2;
  157. tv.tv_usec = 500000; // 0.5 sec
  158. setsockopt( m_nSocketFD, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof( tv ));
  159. if ( connect( m_nSocketFD,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0){
  160. sendStaticBugaBuga();
  161. printf("ERROR connect to [%s:%d]", "localhost", 2222 );
  162. return NULL;
  163. };
  164. char Buffer[1024];
  165. uint8_t BigData[2048000];
  166. uint8_t Data[16];
  167. Data[0] = 0x55;
  168. Data[1] = 0x00;
  169. Data[2] = 0x01;
  170. Data[3] = 0xA5;
  171. Data[4] = 0x00;
  172. Data[5] = 0xFB;
  173. sleep(1);
  174. while( Exit_State == false ){
  175. iResult = write( m_nSocketFD, Data, 6 );
  176. if( iResult != 6 ) {
  177. perror("Error in Connection!\n");
  178. sendStaticBugaBuga();
  179. Exit_State = true;
  180. continue;
  181. } else {
  182. DataSize = recv( m_nSocketFD, BigData, 2048000, 0 );
  183. if( DataSize < 0 ) {
  184. perror("Error in Connection!\n");
  185. sendStaticBugaBuga();
  186. Exit_State = true;
  187. continue;
  188. };
  189. if( DataSize > 0 ) {
  190. sprintf( Buffer, "Content-type: image/jpeg\r\nContent-length: %d\r\n\r\n", DataSize );
  191. count = write( pipefd[1], Buffer, strlen( Buffer ) );
  192. if( count != strlen( Buffer ) ) Exit_State = true;
  193. count = write( pipefd[1], BigData, DataSize );
  194. if( count != DataSize ) Exit_State = true;
  195. sprintf( Buffer, "\r\n\r\n--MPFrame\r\n" );
  196. count = write( pipefd[1], Buffer, strlen( Buffer ) );
  197. };
  198. }
  199. usleep( 200000 );
  200. };
  201. return NULL;
  202. };
  203. int main( int, char** ){
  204. int RetVal = 0;
  205. int DataSize = 0;
  206. if( signal( SIGPIPE, SIGPIPE_handler ) == SIG_ERR ){
  207. printf( "Content-type: text/html\n\n" );
  208. printf( "Can't catch SIGPIPE\n" );
  209. return -1;
  210. };
  211. printf( "%s 200 OK\r\n", getenv("SERVER_PROTOCOL") );
  212. printf( "Server: %s\r\n", getenv("SERVER_SOFTWARE") );
  213. char DateStr[200];
  214. time_t t;
  215. struct tm *tmp;
  216. t = time(NULL);
  217. tmp = gmtime(&t);
  218. if( tmp == NULL) {
  219. perror("localtime");
  220. return -1;
  221. };
  222. if( strftime( DateStr, sizeof(DateStr), "%a, %d %b %Y %T GMT", tmp )==0 ){
  223. fprintf( stderr, "strftime returned 0" );
  224. return -1;
  225. };
  226. printf( "Date: %s\r\n", DateStr );
  227. printf( "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n" );
  228. printf( "Pragma: no-cache\r\n" );
  229. printf( "MIME-version: 1.0\r\n");
  230. printf( "Keep-Alive: timeout=5, max=1\r\n" );
  231. printf( "Connection: Keep-Alive\r\n" );
  232. printf( "Transfer-Encoding: chunked\r\n" );
  233. #ifndef IMG_SYM
  234. // open the default camera
  235. cap.open(0);
  236. if(!cap.isOpened()){
  237. printf( "Content-type: text/html\n\n");
  238. printf( "Error Opening Video Source\n" );
  239. return -1;
  240. };
  241. #endif
  242. if( pipe( pipefd ) == -1 ){
  243. printf( "Content-type: text/html\n\n");
  244. printf( "Error Creating Pipe\n" );
  245. return -1;
  246. };
  247. //Start capture tread
  248. RetVal = pthread_create( &thread, NULL, video_capture_function, NULL );
  249. if( RetVal ){
  250. printf( "Content-type: text/html\n\n");
  251. printf( "Error - pthread_create() return code: %d\n", RetVal );
  252. return -1;
  253. };
  254. printf( "Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n");
  255. unsigned char Byte[1];
  256. int index = 0;
  257. while( !Exit_State ){
  258. if( index == 0 ){
  259. printf( "\r\n%x\r\n", CHUNK_SIZE );
  260. };
  261. if( read( pipefd[0], Byte, 1) != 1 ) {
  262. Exit_State = true;
  263. break;
  264. } else {
  265. if( fwrite( Byte, 1, 1, stdout ) != 1 ){
  266. Exit_State = true;
  267. break;
  268. };
  269. index++;
  270. if( index == CHUNK_SIZE ) index = 0;
  271. };
  272. };
  273. };
  274. /*
  275. client
  276. struct sockaddr_un address;
  277. int socket_fd, nbytes;
  278. char buffer[256];
  279. socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  280. if(socket_fd < 0)
  281. {
  282. printf("socket() failed\n");
  283. return 1;
  284. }
  285. // start with a clean address structure
  286. memset(&address, 0, sizeof(struct sockaddr_un));
  287. address.sun_family = AF_UNIX;
  288. snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
  289. if(connect(socket_fd,
  290. (struct sockaddr *) &address,
  291. sizeof(struct sockaddr_un)) != 0)
  292. {
  293. printf("connect() failed\n");
  294. return 1;
  295. }
  296. server
  297. http://www.thomasstover.com/uds.html
  298. #include <stdio.h>
  299. #include <sys/socket.h>
  300. #include <sys/un.h>
  301. #include <sys/types.h>
  302. #include <unistd.h>
  303. #include <string.h>
  304. int connection_handler(int connection_fd)
  305. {
  306. int nbytes;
  307. char buffer[256];
  308. nbytes = read(connection_fd, buffer, 256);
  309. buffer[nbytes] = 0;
  310. printf("MESSAGE FROM CLIENT: %s\n", buffer);
  311. nbytes = snprintf(buffer, 256, "hello from the server");
  312. write(connection_fd, buffer, nbytes);
  313. close(connection_fd);
  314. return 0;
  315. }
  316. int main(void)
  317. {
  318. struct sockaddr_un address;
  319. int socket_fd, connection_fd;
  320. socklen_t address_length;
  321. pid_t child;
  322. socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  323. if(socket_fd < 0)
  324. {
  325. printf("socket() failed\n");
  326. return 1;
  327. }
  328. unlink("./demo_socket");
  329. // start with a clean address structure
  330. memset(&address, 0, sizeof(struct sockaddr_un));
  331. address.sun_family = AF_UNIX;
  332. snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
  333. if(bind(socket_fd,
  334. (struct sockaddr *) &address,
  335. sizeof(struct sockaddr_un)) != 0)
  336. {
  337. printf("bind() failed\n");
  338. return 1;
  339. }
  340. if(listen(socket_fd, 5) != 0)
  341. {
  342. printf("listen() failed\n");
  343. return 1;
  344. }
  345. while((connection_fd = accept(socket_fd,
  346. (struct sockaddr *) &address,
  347. &address_length)) > -1)
  348. {
  349. child = fork();
  350. if(child == 0)
  351. {
  352. // now inside newly created connection handling process
  353. return connection_handler(connection_fd);
  354. }
  355. // still inside server process
  356. close(connection_fd);
  357. }
  358. close(socket_fd);
  359. unlink("./demo_socket");
  360. return 0;
  361. }
  362. */