📄 webcam_server.c
字号:
/*-------------------------------------------------------------------------
Waits for a client request, gets and sends latest
image from imagedata, loop .
-------------------------------------------------------------------------*/
int handle_connection(struct connection *con)
{
char buf[16];
char *ImageTrans = "aa55102000000000";
char *Login = "aa551010abcd1234";
char *ImageStop = "aa55102100000000";
char *LogoBuf = "aa552010";
char *Exit = "aa55101100000000";
int ret;
pthread_t sendpt;
//time_t start;
//time_t end;
//pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL); //允许退出线程
//pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); //设置立即取消
while(1)
{
/*处理请求并回答客户 */
if(recv(con->socketfd, buf, sizeof(buf), 0)>0)
{
if (strncasecmp(buf, ImageTrans, 8) == 0) //图像传输
{
if((ret = pthread_create(&sendpt, NULL, (void*)send_image, (void*)con))!=0)
{
printf ("Create send pthread error.......\n");
//continue;
}
else
printf ("Create send pthread Succeed!\n");
pthread_detach(sendpt);
printf("the result is %d\n", ret);
}
else if (strncasecmp(buf, Login, 16) == 0) //登录
{
if((ret=send(con->socketfd,LogoBuf,strlen(LogoBuf),0))!= strlen(LogoBuf))
printf("send the login failure \n");
else
printf("send the login succeeded \n");
}
else if (strncasecmp(buf, ImageStop, 8) == 0) //图像暂停
{
printf("receive the stop information\n");
pthread_cancel(sendpt); //取消线砳
}
else if (strncasecmp(buf, Exit, 8) == 0) //退出
{
printf("receive the exit information\n");
pthread_cancel(sendpt); //取消线程
break;
}
/*else
{
if((ret=send(con->socketfd,WrongBuf,strlen(WrongBuf),0))!= strlen(WrongBuf))
printf("send the wrong failure\n");
else
printf("send the wrong information\n");
}*/
}
/*else
time(&end);
if(start-end)
break;*/
}
close(con->socketfd);
free(con);
return 0;
}
/*-------------------------------------------------------------------------
Gets and sends latest
image from imagedata, loop .
-------------------------------------------------------------------------*/
int send_image(struct connection *con)
{
struct caminfo *cam = con->cam;
struct imagedata *ptr;
int result;
int a, b;
char* buf;
char* buffer;
char* ImageRsp = "aa552020";
while(1)
{
sleep(1);
//int res = 1;
/* get the newest image */
ptr = peek(cam);
if(!ptr)
{
printf("could not get right image\n");
free(ptr);
continue;
}
/* send jpeg */
buf=ecvt((double)ptr->len, 4, &a, &b);
int len1=strlen(ImageRsp);
int len2=strlen(buf);
printf("the image respond length is %d--the length of data is %d\n", len1, len2);
buffer=(char* )malloc(len1+len2+ptr->len);
memset(buffer, 0, len1+len2+ptr->len);
printf("start the data copy\n");
memcpy(buffer, ImageRsp, len1);
memcpy(buffer+len1, buf, len2);
memcpy(buffer+len1+len2, ptr->jpeg_data, ptr->len);
printf("get the information of length and jpegdata is %d---%d\n",len1+len2+ptr->len, ptr->len);
if ((result = send(con->socketfd, buffer, len1+len2+ptr->len, 0)) != len1+len2+ptr->len)
{
printf("send the jpeg image failure\n");
continue;
}
/*if ((result = send(con->socketfd, ptr->jpeg_data, ptr->len, 0)) != ptr->len)
{
printf("send the jpeg image failure\n");
res = 0;
}
if (!res)
break;*/
printf("send the jpeg image succeeded\n");
//printf("send the length of data--%d\n", result);
free(buffer);
free(ptr);
}
return 0;
}
/*------------------------------------------------------------------------------------
The Main Function
------------------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
struct sockaddr_in client_addr;
socklen_t caddr_len;
pthread_t pt;
char *sizestring = NULL;
char *separateur = NULL;
struct caminfo *cam;
struct connection *con;
int res,i;
int listenfd,connfd;
cam = (struct caminfo *)malloc(sizeof(struct caminfo));
if(!cam)
{
fprintf(stderr, "malloc error\n");
exit(1);
}
cam->mmap = NULL;
/* ignore broken pipes n stuff */
//signal(SIGPIPE, SIG_IGN);
//signal(SIGALRM, SIG_IGN);
/* init mutexes */
//pthread_mutex_init(&cam->lock_queue, NULL);
//pthread_mutex_init(&cam->lock_info, NULL);
//pthread_mutex_init(&cam->lock_id, NULL);
//pthread_mutex_init(&cam->lock_log, NULL);
//sem_init(&cam->sem_con, 0, 0);
/* defaults */
//cam->o.caption = DEF_CAPTION;
cam->o.devfile = DEF_CAMDEVFILE;
cam->o.port = DEF_PORT;
cam->o.jpeg_quality = DEF_QUALITY;
//cam->o.retry_init = DEF_RETRYINIT;
//cam->o.daemon = DEF_DAEMON;
cam->o.swap_rgb = DEF_SWAPRGB;
//cam->o.test_fps = DEF_TESTFPS;
cam->o.width = DEF_WIDTH;
cam->o.height = DEF_HEIGHT;
//cam->o.logfile = DEF_LOGFILE;
cam->o.flip_horiz = DEF_FLIP_HORIZ;
cam->o.flip_vert = DEF_FLIP_VERT;
cam->o.gamma = DEF_GAMMA;
struct RGB fg = DEF_TEXT_FG;
struct RGB bg = DEF_TEXT_BG;
struct RGB trans = DEF_TRANS;
cam->o.text_fg = fg;
cam->o.text_bg = bg;
cam->o.trans = trans;
cam->o.text_xpos = DEF_TEXT_XPOS;
cam->o.text_ypos = DEF_TEXT_YPOS;
cam->o.rotate = DEF_ROTATE;
//cam->o.use_read = DEF_FORCE_READ;
//cam->o.allow_http = DEF_ALLOW_HTTP;
//cam->o.allow_stream = DEF_ALLOW_STREAM;
//cam->o.max_stream_bytes = DEF_MAX_STREAM_BYTES;
//cam->o.max_stream_seconds = DEF_MAX_STREAM_SECONDS;
//cam->o.max_stream_frames = DEF_MAX_STREAM_FRAMES;
/* currently not implemented
cam->o.admin_pw = DEF_ADMIN_PW;
cam->o.allow_admin = DEF_ALLOW_ADMIN;
*/
cam->o.palette = DEF_PALETTE;
/* overwrite defaults with command line args */
//if(parse_args(cam, argc, argv) < 0)
//exit(1);
/* overwrite defaults with command line args */
for (i = 1; i < argc; i++)
{
/* skip bad arguments */
if (argv[i] == NULL || *argv[i] == 0 || *argv[i] != '-')
continue;
if (strcmp (argv[i], "-d") == 0)
{
if (i + 1 >= argc)
{
printf ("No parameter specified with -d, aborting.\n");
return 0;
}
cam->o.devfile = strdup (argv[i + 1]);
printf (" device file : %s \n", cam->o.devfile);
}
if (strcmp (argv[i], "-g") == 0)
{
if (i + 1 >= argc)
{
printf ("No parameter specified with -g, aborting.\n");
return 0;
}
sizestring = strdup (argv[i + 1]);
cam->o.width = strtoul (sizestring, &separateur, 10);
if (*separateur != 'x')
{
printf ("Error in size use -s widthxheight \n");
return 0;
}
else
{
++separateur;
cam->o.height = strtoul (separateur, &separateur, 10);
if (*separateur != 0)
{
printf ("hmm.. dont like that!! trying this height \n");
printf (" size width: %d height: %d \n", cam->o.width, cam->o.height);
}
}
}
if (strcmp (argv[i], "-p") == 0)
{
if (i + 1 >= argc)
{
printf ("No parameter specified with -p, aborting.\n");
return 0;
}
cam->o.port = (unsigned short) atoi (argv[i + 1]);
printf (" port: %d \n", cam->o.port);
if (cam->o.port < 1024 )
{
printf ("Port should be between 1024 to 65536 set default 7070 !.\n");
cam->o.port = 7070;
}
}
if (strcmp (argv[i], "-q") == 0)
{
if (i + 1 >= argc)
{
printf ("No parameter specified with -q, aborting.\n");
return 0;
}
cam->o.jpeg_quality = (unsigned short) atoi (argv[i + 1]);
printf ("jpeg_quality : %d \n", cam->o.jpeg_quality);
}
if (strcmp (argv[i], "-h") == 0)
{
printf("server 1.1, copyright 2007 Jason totti M\n\n");
printf("usage: webcam_server [options]\n\n\
option description [default value]\n\n\
-d video_device - v4l compliant device [/dev/video]\n\
-g geometry - change default geometry (WIDTHxHEIGHT) [176x144]\n\
-h - this help screen\n\
-p tcp_port - tcp listen port [8888]\n\
-q jpeg_quality - jpeg compression quality (0-100) [60]\n\
\n");
return 0;
}
}
/* don't do this stuff if we're in test mode */
//if(!cam->o.test_fps)
//{
/* 创建服务套接字并建立侦听队列 */
listenfd = create_and_listen(cam->o.port);
if(listenfd < 1)
exit(1);
/* go into daemon mode if told to */
/*if(cam->o.daemon)
if(daemon(0,0) < 0)
{
fprintf(stderr, "daemon failed.\n");
exit(1);
}*/
//}
/* open camera device...retry infinitely if told to */
res = open_cam(cam, cam->o.devfile);
if(res < 0)
{
usleep(5000000);
printf("camera init failed, retrying...\n");
res = open_cam(cam, cam->o.devfile);
}
if(res < 0)
{
printf("unable to initialize camera\n");
exit(1);
}
/* get camera defaults */
get_cam_info(cam);
cam->vid_win.width = cam->o.width;
cam->vid_win.height = cam->o.height;
if(set_cam_info(cam) < 0)
{
printf("error setting video device parameters, using defaults\n");
get_cam_info(cam);
if(set_cam_info(cam) < 0)
{
printf("not a valid video device? quitting.\n");
exit(1);
}
}
printf("set the camera device succeeded\n");
cam->o.width = cam->vid_win.width;
cam->o.height = cam->vid_win.height;
if(cam->o.palette != -1)
{
/* force use of certain palette */
cam->vid_pic.palette = cam->o.palette;
ioctl(cam->dev, VIDIOCSPICT, &cam->vid_pic);
ioctl(cam->dev, VIDIOCGPICT, &cam->vid_pic);
if(cam->vid_pic.palette == cam->o.palette)
printf("using palette %d\n", cam->o.palette);
else
{
printf("could not set palette to %d\n", cam->o.palette);
exit(1);
}
}
/*else
{
//check for a valid colour palette
int palfound = 0;
int brightness = -1, hue = -1, colour = -1, contrast = -1, whiteness = -1;
for(cam->pal = palettes; cam->pal->val >= 0; cam->pal++)
{
if(cam->o.palette != -1)
{
if(cam->pal->val != cam->o.palette)
continue;
else
cam->vid_pic.palette = cam->pal->val;
}
else
{
cam->vid_pic.palette = cam->pal->val;
}
cam->vid_pic.depth = cam->pal->depth;
if(brightness >= 0) cam->vid_pic.brightness = brightness;
if(hue >= 0) cam->vid_pic.hue = hue;
if(colour >= 0) cam->vid_pic.colour = colour;
if(contrast >= 0) cam->vid_pic.contrast = contrast;
if(whiteness >= 0) cam->vid_pic.whiteness = whiteness;
ioctl(cam->dev, VIDIOCSPICT, &cam->vid_pic);
ioctl(cam->dev, VIDIOCGPICT, &cam->vid_pic);
if(cam->vid_pic.palette == cam->pal->val)
{
printf("using palette %d\n", cam->pal->val);
palfound = 1;
break;
}
}
if(!palfound)
{
fprintf(stderr, "No supported colour palette found.\n");
return 1;
}
}*/
//cam->grab_thread_alive = 1;
pthread_create(&cam->pt_grab, NULL, (void*)grab_thread, (void *)cam);
/*if(cam->o.test_fps)
{
pthread_join(cam->pt_grab, NULL);
exit(0);
}*/
pthread_detach(cam->pt_grab);
printf("Webcam_Server======Started\n");
caddr_len = sizeof(client_addr);
/* 等待客户 */
while(1)
{
if((connfd=accept(listenfd,(struct sockaddr *)&client_addr,&caddr_len))>0)
{
/* con is free'd at end of handle_connection */
con = (struct connection*)malloc(sizeof(struct connection));
con->cam = cam;
/* get connection info */
con->socketfd = connfd;
//memcpy(&con->client_addr, &client_addr, caddr_len);
printf("Received socket from %s\n", inet_ntoa(client_addr.sin_addr));
printf("socket is --%d\n", connfd);
printf("addr len is --%d\n", caddr_len);
printf("port --%d\n", client_addr.sin_port);
if((res = pthread_create(&pt, NULL, (void*)handle_connection, (void*)con))!=0)
{
printf ("Create pthread error! relay the other line...!\n");
continue;
}
else
printf ("Create pthread Succeed!\n");
//pthread_join(pt, NULL); //update gjj
pthread_detach(pt);
}
}
close(listenfd);
exit(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -