📄 webcam_server.c
字号:
cam->o.text_fg.b = b; } else res = -1; break; case 'b': if(i+1 < argc) { int r,g,b; sscanf(argv[++i], "%d,%d,%d", &r, &g, &b); cam->o.text_bg.r = r; cam->o.text_bg.g = g; cam->o.text_bg.b = b; } else res = -1; break; case 't': if(i+1 < argc) { int r,g,b; sscanf(argv[++i], "%d,%d,%d", &r, &g, &b); cam->o.trans.r = r; cam->o.trans.g = g; cam->o.trans.b = b; } else res = -1; break; default: res = -1; } break; case 'D': switch(argv[i][2]) { case 'h': cam->o.allow_http = 0; break; case 's': cam->o.allow_stream = 0; break; default: res = -1; } break;/* ** currently not implemented case 'E': switch(argv[i][2]) { case 'a': cam->o.allow_admin = 1; break; default: res = -1; } break; case 'e': if(i+1 < argc) cam->o.admin_pw = argv[++i]; else res = -1; break;*/ case 'm': if(i+1 < argc) switch(argv[i][2]) { case 'b': cam->o.max_stream_bytes = atoi(argv[++i]); break; case 's': cam->o.max_stream_seconds = atoi(argv[++i]); break; case 'f': cam->o.max_stream_frames = atoi(argv[++i]); break; default: res = -1; } else res = -1; break; case 'a': cam->o.test_fps = 1; break; case 'n': cam->o.use_read = 1; break; case 'l': if(i+1 < argc) cam->o.logfile = argv[++i]; else res = -1; break; case 'R': if(i+1 < argc) cam->o.rotate = atoi(argv[++i]); else res = -1; break; case 'g': if(i+1 < argc) { int w=0,h=0; if(strchr(argv[i+1], 'x') == NULL) { res = -1; break; } sscanf(argv[++i], "%dx%d", &w, &h); if(w < 1 || h < 1 || w > 2048 || h > 2048) { fprintf(stderr, "invalid width, height: %dx%d. using defaults: %dx%d\n", w,h,DEF_WIDTH,DEF_HEIGHT); cam->o.width = DEF_WIDTH; cam->o.height = DEF_HEIGHT; } cam->o.width = w; cam->o.height = h; } else res = -1; break; case 'G': if(i+1 < argc) { cam->o.gamma = atoi(argv[++i]); } else res = -1; break; default: res = -1; break; }; } else { res = -1; } if(res == -1) { fprintf(stderr, "invalid option or missing parameter: %s\n", argv[i]); return res; } } return res;}int main(int argc, char *argv[]){ int sockfd=0, clientfd; struct sockaddr_in remote_addr; socklen_t addr_len; pthread_t pt; struct caminfo *cam; struct connection *con; int res; 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); /* don't do this stuff if we're in test mode */ if(!cam->o.test_fps) { /* create listening socket */ sockfd = create_and_listen(cam->o.port); if(sockfd < 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); while(res < 0 && cam->o.retry_init) { usleep(5000000); fprintf(stderr, "camera init failed, retrying...\n"); res = open_cam(cam, cam->o.devfile); } if(res < 0) { fprintf(stderr, "unable to initialise 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) { fprintf(stderr, "error setting video device parameters, using defaults\n"); get_cam_info(cam); if(set_cam_info(cam) < 0) { fprintf(stderr, "not a valid video device? quitting.\n"); exit(1); } } 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); } } 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); /* start the watchdog thread */ /* pthread_create(&pt, NULL, (void*)watchdog_thread, (void *)cam); pthread_detach(pt);*/ if(cam->o.test_fps) { pthread_join(cam->pt_grab, NULL); exit(0); } else { pthread_detach(cam->pt_grab); } log(cam, "webcam_server started\n"); addr_len = sizeof(remote_addr); while((clientfd = accept(sockfd, (struct sockaddr *)&remote_addr, &addr_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 = clientfd; memcpy(&con->remote_addr, &remote_addr, addr_len); /* handle the connection */ pthread_create(&pt, NULL, (void*)handle_connection, (void*)con); pthread_detach(pt); } close(sockfd); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -