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.
424 lines
10 KiB
424 lines
10 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <signal.h>
|
|
#include <sys/socket.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <pthread.h>
|
|
#include <math.h>
|
|
#include <vector>
|
|
#include <stdio.h>
|
|
#include <unistd.h> // UNIX standard function definitions
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/ioctl.h>
|
|
#include <netinet/in.h>
|
|
#include <netdb.h>
|
|
#include <opencv2/opencv.hpp>
|
|
|
|
using namespace cv;
|
|
using namespace std;
|
|
|
|
#define IMG_SYM
|
|
|
|
#define NPH
|
|
|
|
#ifdef NPH
|
|
#define CHUNKED
|
|
|
|
#ifdef CHUNKED
|
|
|
|
#define CHUNK_SIZE 4118
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
bool Exit_State = false;
|
|
|
|
int pipefd[2];
|
|
|
|
VideoCapture cap;
|
|
|
|
pthread_t thread;
|
|
|
|
// SIGPIPE
|
|
|
|
void SIGPIPE_handler( int sig ){
|
|
if( sig == SIGPIPE ){
|
|
Exit_State = true;
|
|
pthread_join( thread, NULL );
|
|
};
|
|
};
|
|
|
|
#ifdef IMG_SYM
|
|
int cStep = 0;
|
|
void createAlphaMat(Mat &mat){
|
|
CV_Assert(mat.channels() == 4);
|
|
for (int i = 0; i < mat.rows; ++i) {
|
|
for (int j = 0; j < mat.cols; ++j) {
|
|
Vec4b& bgra = mat.at<Vec4b>(i, j);
|
|
bgra[0] = UCHAR_MAX; // Blue
|
|
bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
|
|
bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
|
|
bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
|
|
};
|
|
};
|
|
Point Center( mat.cols / 2 + 20.0 * cos( cStep * ( 3.14 / 20.0 ) ), mat.rows / 2 + 20.0 * sin( cStep * ( 3.14 / 20.0 ) ) );
|
|
cStep++;
|
|
if( cStep == 40 ) cStep = 0;
|
|
circle( mat, Center, 5, Scalar(255,255,0) );
|
|
|
|
char DateStr[200];
|
|
time_t t;
|
|
struct tm *tmp;
|
|
|
|
t = time(NULL);
|
|
tmp = gmtime(&t);
|
|
if( tmp == NULL) {
|
|
perror("localtime");
|
|
return;
|
|
};
|
|
if( strftime( DateStr, sizeof(DateStr), "%F %T GMT", tmp )==0 ){
|
|
fprintf( stderr, "strftime returned 0" );
|
|
return;
|
|
};
|
|
putText( mat, DateStr, Point( 5, 20 ), FONT_HERSHEY_SIMPLEX, .2, Scalar( 0, 255, 255 ) );
|
|
};
|
|
void putTimeStamp(Mat &mat){
|
|
char DateStr[200];
|
|
time_t t;
|
|
struct tm *tmp;
|
|
|
|
t = time(NULL);
|
|
tmp = gmtime(&t);
|
|
if( tmp == NULL) {
|
|
perror("localtime");
|
|
return;
|
|
};
|
|
if( strftime( DateStr, sizeof(DateStr), "%F %T GMT", tmp )==0 ){
|
|
fprintf( stderr, "strftime returned 0" );
|
|
return;
|
|
};
|
|
|
|
rectangle( mat, Point( 720, 840 ), Point( 1200, 890 ), Scalar( 0, 0, 0 ), CV_FILLED );
|
|
putText( mat, DateStr, Point( 740, 875 ), FONT_HERSHEY_SIMPLEX, 1, Scalar( 0, 255, 255 ), 3 );
|
|
|
|
};
|
|
|
|
void putTimeS(Mat &mat){
|
|
char DateStr[200];
|
|
time_t t;
|
|
struct tm *tmp;
|
|
|
|
t = time(NULL);
|
|
tmp = gmtime(&t);
|
|
if( tmp == NULL) {
|
|
perror("localtime");
|
|
return;
|
|
};
|
|
if( strftime( DateStr, sizeof(DateStr), "%F %T GMT", tmp )==0 ){
|
|
fprintf( stderr, "strftime returned 0" );
|
|
return;
|
|
};
|
|
rectangle( mat, Point( 10, 10 ), Point( 490, 60 ), Scalar( 0, 0, 0 ), CV_FILLED );
|
|
putText( mat, DateStr, Point( 30, 45 ), FONT_HERSHEY_SIMPLEX, 1, Scalar( 0, 255, 255 ), 3 );
|
|
|
|
};
|
|
#endif
|
|
|
|
|
|
void sendStaticBugaBuga( void ){
|
|
FILE * fid;
|
|
char Buffer[1024];
|
|
uint8_t BigData[2048000];
|
|
int DataSize;
|
|
fid = fopen("nosignal.jpg","rb");
|
|
if( fid != NULL ) {
|
|
DataSize = fread( BigData,1,2048000,fid);
|
|
fclose(fid);
|
|
if( DataSize > 0 ){
|
|
//Exit_State = true;
|
|
while( ! Exit_State ) {
|
|
sprintf( Buffer, "Content-type: image/jpeg\r\nContent-length: %d\r\n\r\n", DataSize );
|
|
write( pipefd[1], Buffer, strlen( Buffer ) );
|
|
write( pipefd[1], BigData, DataSize );
|
|
sprintf( Buffer, "\r\n\r\n--MPFrame\r\n" );
|
|
write( pipefd[1], Buffer, strlen( Buffer ) );
|
|
usleep(500000);
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
void *video_capture_function( void *patr ){
|
|
int DataSize = 0;
|
|
int count;
|
|
int m_nSocketFD;
|
|
int iResult;
|
|
struct addrinfo * result;
|
|
struct addrinfo * ptr;
|
|
struct addrinfo hints;
|
|
struct sockaddr_in serv_addr;
|
|
char cBuffer[256];
|
|
|
|
struct hostent * server;
|
|
m_nSocketFD = socket(AF_INET, SOCK_STREAM, 0);
|
|
if( m_nSocketFD < 0 ){
|
|
sendStaticBugaBuga();
|
|
printf("ERROR creating socket");
|
|
return NULL;
|
|
};
|
|
server = gethostbyname( "localhost" );
|
|
if( server == NULL ){
|
|
sendStaticBugaBuga();
|
|
printf( "ERROR, no such host [%s]", "localhost" );
|
|
return NULL;
|
|
};
|
|
bzero((char *) &serv_addr, sizeof(serv_addr));
|
|
serv_addr.sin_family = AF_INET;
|
|
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length );
|
|
serv_addr.sin_port = htons( 2222 );
|
|
struct timeval tv;
|
|
tv.tv_sec = 2;
|
|
tv.tv_usec = 500000; // 0.5 sec
|
|
setsockopt( m_nSocketFD, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof( tv ));
|
|
if ( connect( m_nSocketFD,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0){
|
|
sendStaticBugaBuga();
|
|
printf("ERROR connect to [%s:%d]", "localhost", 2222 );
|
|
return NULL;
|
|
};
|
|
|
|
char Buffer[1024];
|
|
uint8_t BigData[2048000];
|
|
uint8_t Data[16];
|
|
Data[0] = 0x55;
|
|
Data[1] = 0x00;
|
|
Data[2] = 0x01;
|
|
Data[3] = 0xA5;
|
|
Data[4] = 0x00;
|
|
Data[5] = 0xFB;
|
|
sleep(1);
|
|
while( Exit_State == false ){
|
|
iResult = write( m_nSocketFD, Data, 6 );
|
|
if( iResult != 6 ) {
|
|
perror("Error in Connection!\n");
|
|
sendStaticBugaBuga();
|
|
Exit_State = true;
|
|
continue;
|
|
} else {
|
|
DataSize = recv( m_nSocketFD, BigData, 2048000, 0 );
|
|
if( DataSize < 0 ) {
|
|
perror("Error in Connection!\n");
|
|
sendStaticBugaBuga();
|
|
Exit_State = true;
|
|
continue;
|
|
};
|
|
if( DataSize > 0 ) {
|
|
sprintf( Buffer, "Content-type: image/jpeg\r\nContent-length: %d\r\n\r\n", DataSize );
|
|
count = write( pipefd[1], Buffer, strlen( Buffer ) );
|
|
if( count != strlen( Buffer ) ) Exit_State = true;
|
|
count = write( pipefd[1], BigData, DataSize );
|
|
if( count != DataSize ) Exit_State = true;
|
|
sprintf( Buffer, "\r\n\r\n--MPFrame\r\n" );
|
|
count = write( pipefd[1], Buffer, strlen( Buffer ) );
|
|
};
|
|
}
|
|
usleep( 200000 );
|
|
};
|
|
return NULL;
|
|
};
|
|
|
|
int main( int, char** ){
|
|
int RetVal = 0;
|
|
int DataSize = 0;
|
|
|
|
if( signal( SIGPIPE, SIGPIPE_handler ) == SIG_ERR ){
|
|
printf( "Content-type: text/html\n\n" );
|
|
printf( "Can't catch SIGPIPE\n" );
|
|
return -1;
|
|
};
|
|
printf( "%s 200 OK\r\n", getenv("SERVER_PROTOCOL") );
|
|
printf( "Server: %s\r\n", getenv("SERVER_SOFTWARE") );
|
|
|
|
char DateStr[200];
|
|
time_t t;
|
|
struct tm *tmp;
|
|
|
|
t = time(NULL);
|
|
tmp = gmtime(&t);
|
|
if( tmp == NULL) {
|
|
perror("localtime");
|
|
return -1;
|
|
};
|
|
if( strftime( DateStr, sizeof(DateStr), "%a, %d %b %Y %T GMT", tmp )==0 ){
|
|
fprintf( stderr, "strftime returned 0" );
|
|
return -1;
|
|
};
|
|
printf( "Date: %s\r\n", DateStr );
|
|
printf( "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n" );
|
|
printf( "Pragma: no-cache\r\n" );
|
|
printf( "MIME-version: 1.0\r\n");
|
|
printf( "Keep-Alive: timeout=5, max=1\r\n" );
|
|
printf( "Connection: Keep-Alive\r\n" );
|
|
printf( "Transfer-Encoding: chunked\r\n" );
|
|
|
|
#ifndef IMG_SYM
|
|
// open the default camera
|
|
cap.open(0);
|
|
|
|
if(!cap.isOpened()){
|
|
printf( "Content-type: text/html\n\n");
|
|
printf( "Error Opening Video Source\n" );
|
|
return -1;
|
|
};
|
|
|
|
#endif
|
|
|
|
if( pipe( pipefd ) == -1 ){
|
|
printf( "Content-type: text/html\n\n");
|
|
printf( "Error Creating Pipe\n" );
|
|
return -1;
|
|
};
|
|
|
|
//Start capture tread
|
|
RetVal = pthread_create( &thread, NULL, video_capture_function, NULL );
|
|
if( RetVal ){
|
|
printf( "Content-type: text/html\n\n");
|
|
printf( "Error - pthread_create() return code: %d\n", RetVal );
|
|
return -1;
|
|
};
|
|
|
|
printf( "Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n");
|
|
|
|
unsigned char Byte[1];
|
|
int index = 0;
|
|
while( !Exit_State ){
|
|
if( index == 0 ){
|
|
printf( "\r\n%x\r\n", CHUNK_SIZE );
|
|
};
|
|
if( read( pipefd[0], Byte, 1) != 1 ) {
|
|
Exit_State = true;
|
|
break;
|
|
} else {
|
|
if( fwrite( Byte, 1, 1, stdout ) != 1 ){
|
|
Exit_State = true;
|
|
break;
|
|
};
|
|
index++;
|
|
if( index == CHUNK_SIZE ) index = 0;
|
|
};
|
|
};
|
|
};
|
|
/*
|
|
client
|
|
|
|
struct sockaddr_un address;
|
|
int socket_fd, nbytes;
|
|
char buffer[256];
|
|
|
|
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
if(socket_fd < 0)
|
|
{
|
|
printf("socket() failed\n");
|
|
return 1;
|
|
}
|
|
|
|
// start with a clean address structure
|
|
memset(&address, 0, sizeof(struct sockaddr_un));
|
|
|
|
address.sun_family = AF_UNIX;
|
|
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
|
|
|
|
if(connect(socket_fd,
|
|
(struct sockaddr *) &address,
|
|
sizeof(struct sockaddr_un)) != 0)
|
|
{
|
|
printf("connect() failed\n");
|
|
return 1;
|
|
}
|
|
|
|
server
|
|
http://www.thomasstover.com/uds.html
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/un.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
int connection_handler(int connection_fd)
|
|
{
|
|
int nbytes;
|
|
char buffer[256];
|
|
|
|
nbytes = read(connection_fd, buffer, 256);
|
|
buffer[nbytes] = 0;
|
|
|
|
printf("MESSAGE FROM CLIENT: %s\n", buffer);
|
|
nbytes = snprintf(buffer, 256, "hello from the server");
|
|
write(connection_fd, buffer, nbytes);
|
|
|
|
close(connection_fd);
|
|
return 0;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
struct sockaddr_un address;
|
|
int socket_fd, connection_fd;
|
|
socklen_t address_length;
|
|
pid_t child;
|
|
|
|
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
if(socket_fd < 0)
|
|
{
|
|
printf("socket() failed\n");
|
|
return 1;
|
|
}
|
|
|
|
unlink("./demo_socket");
|
|
|
|
// start with a clean address structure
|
|
memset(&address, 0, sizeof(struct sockaddr_un));
|
|
|
|
address.sun_family = AF_UNIX;
|
|
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
|
|
|
|
if(bind(socket_fd,
|
|
(struct sockaddr *) &address,
|
|
sizeof(struct sockaddr_un)) != 0)
|
|
{
|
|
printf("bind() failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if(listen(socket_fd, 5) != 0)
|
|
{
|
|
printf("listen() failed\n");
|
|
return 1;
|
|
}
|
|
|
|
while((connection_fd = accept(socket_fd,
|
|
(struct sockaddr *) &address,
|
|
&address_length)) > -1)
|
|
{
|
|
child = fork();
|
|
if(child == 0)
|
|
{
|
|
// now inside newly created connection handling process
|
|
return connection_handler(connection_fd);
|
|
}
|
|
|
|
// still inside server process
|
|
close(connection_fd);
|
|
}
|
|
|
|
close(socket_fd);
|
|
unlink("./demo_socket");
|
|
return 0;
|
|
}
|
|
|
|
*/
|