IplImage *image_input::capture_uvc_camera() { #ifdef ENABLE_CAMERA struct vdIn *videoIn; char *videodevice = (char*)"/dev/video0"; int format = V4L2_PIX_FMT_YUYV; int grabmethod = 1; IplImage* frame; videoIn = (struct vdIn *) calloc (1, sizeof (struct vdIn)); if (init_videoIn(videoIn, videodevice, camera.width, camera.height, format, grabmethod) < 0) { puts("init failed"); exit (1); } //Reset all camera controls v4l2ResetControl (videoIn, V4L2_CID_BRIGHTNESS); v4l2ResetControl (videoIn, V4L2_CID_CONTRAST); v4l2ResetControl (videoIn, V4L2_CID_SATURATION); v4l2ResetControl (videoIn, V4L2_CID_GAIN); //Setup Camera Parameters v4l2SetControl (videoIn, V4L2_CID_BRIGHTNESS, camera.brightness); v4l2SetControl (videoIn, V4L2_CID_CONTRAST, camera.contrast); v4l2SetControl (videoIn, V4L2_CID_SATURATION, camera.saturation); v4l2SetControl (videoIn, V4L2_CID_GAIN, camera.gain); if (uvcGrab (videoIn) < 0) { fprintf(stderr, "Error grabbing\n"); close_v4l2(videoIn); free(videoIn); exit(1); } // IplImage frame = cvCreateImage(cvSize(videoIn->width, videoIn->height), IPL_DEPTH_8U, 3); // convert image format convert_yuyv_to_rgb(videoIn->framebuffer, (unsigned char*)frame->imageData, videoIn->width, videoIn->height); close_v4l2 (videoIn); free (videoIn); return frame; #else // ENABLE_CAMERA return NULL; #endif // ENABLE_CAMERA }
void clean_struct (struct ALL_DATA *all_data) { struct GWIDGET *gwidget = all_data->gwidget; struct VidState *s = all_data->s; struct paRecordData *pdata = all_data->pdata; struct GLOBAL *global = all_data->global; struct focusData *AFdata = all_data->AFdata; struct vdIn *videoIn = all_data->videoIn; struct VideoFormatData *videoF = all_data->videoF; gboolean control_only = (global->control_only || global->add_ctrls); if((!control_only) && (pdata != NULL)) { /*destroy mutex for sound buffers*/ if (global->debug) g_printf("free audio mutex\n"); g_mutex_free( pdata->mutex ); g_free(pdata); pdata=NULL; all_data->pdata=NULL; g_free(videoF); videoF=NULL; } if(videoIn) close_v4l2(videoIn, control_only); videoIn=NULL; if (global->debug) g_printf("closed v4l2 strutures\n"); if (s->control_list) { free_control_list (s->control_list); s->control_list = NULL; g_printf("free controls\n"); } g_free(s); s = NULL; all_data->s = NULL; g_free(gwidget); gwidget = NULL; all_data->gwidget = NULL; if (global->debug) g_printf("free controls - vidState\n"); g_free(AFdata); AFdata = NULL; all_data->AFdata = NULL; if(global) closeGlobals(global); global=NULL; all_data->global=NULL; g_printf("cleaned allocations - 100%%\n"); }
/****************************************************************************** Description.: Input Value.: Return Value: ******************************************************************************/ void cam_cleanup(context_t *pcontext) { if (!pcontext->cleaned) { pcontext->cleaned = 1; close_v4l2(pcontext->videoIn); if (pcontext->videoIn->tmpbuffer != NULL) free(pcontext->videoIn->tmpbuffer); if (pcontext->videoIn != NULL) free(pcontext->videoIn); } }
/****************************************************************************** Description.: Input Value.: Return Value: ******************************************************************************/ void cam_cleanup(void *arg) { input * in = (input*)arg; context *pctx = in->context; LOG("cleaning up resources allocated by input thread\n"); if (pctx->videoIn != NULL) { close_v4l2(pctx->videoIn); free(pctx->videoIn); pctx->videoIn = NULL; } free(in->buf); in->buf = NULL; in->size = 0; }
int main(int argc,char **argv) { rfbScreenInfoPtr s; double t1, t2; double timeout; int nread; if (argc == 2) v4l2str = argv[1]; printf("Using dev %s\n", v4l2str); if (argc == 3) v4l2fps = atoi(argv[2]); printf("Using fps %d\n", v4l2fps); if (open_v4l2() != 0) exit(EXIT_FAILURE); if (init_v4l2() != 0) exit(EXIT_FAILURE); if (alloc_mem() != 0) exit(EXIT_FAILURE); s = rfbGetScreen(&argc, argv, v4l2width, v4l2height, 8, 3, v4l2Bpp); s->desktopName = "v4l2tovnc"; s->frameBuffer = (char *) v4l2buf; s->serverFormat.blueShift = 0; s->serverFormat.greenShift = 8; s->serverFormat.redShift = 16; timeout = 1.0 / v4l2fps; rfbInitServer(s); t1 = timestamp(); while (rfbIsActive(s)) { t2 = timestamp(); if ((t2 - t1) >= timeout) { nread = read_frame(); if (nread < 0) break; rfbMarkRectAsModified(s, 0, 0, v4l2width, v4l2height); t1 = timestamp(); } rfbProcessEvents(s, -1); } close_v4l2(); return 0; }
/****************************************************************************** Description.: Input Value.: Return Value: ******************************************************************************/ void cam_cleanup(void *arg) { static unsigned char first_run = 1; context *pcontext = (context*) arg; pglobal = pcontext->pglobal; if (!first_run) { DBG("already cleaned up ressources\n"); return; } first_run = 0; IPRINT("cleaning up ressources allocated by input thread\n"); close_v4l2(pcontext->videoIn); if (pcontext->videoIn->tmpbuffer != NULL) free(pcontext->videoIn->tmpbuffer); if (pcontext->videoIn != NULL) free(pcontext->videoIn); if (pglobal->in[pcontext->id].buf != NULL) free(pglobal->in[pcontext->id].buf); }
void CameraManager2::start_grabbing() { char outputfile[40]; for (int i = 0; i<11; i++) { sprintf(outputfile, "snap%i.jpg", i); std::cout << i << std::endl; if (uvcGrab() < 0) { fprintf (stderr, "Error grabbing\n"); } else { FILE *file = fopen(outputfile, "wb"); fwrite(vd->tmpbuffer, vd->buf.bytesused + DHT_SIZE, 1, file); } } close_v4l2(); }
/****************************************************************************** Description.: Input Value.: Return Value: ******************************************************************************/ void cam_cleanup(void *arg) { static unsigned char first_run=1; if ( !first_run ) { DBG("already cleaned up ressources\n"); return; } first_run = 0; IPRINT("cleaning up ressources allocated by input thread\n"); /* restore behaviour of the LED to auto */ input_cmd(IN_CMD_LED_AUTO, 0); close_v4l2(videoIn); if (videoIn->tmpbuffer != NULL) free(videoIn->tmpbuffer); if (videoIn != NULL) free(videoIn); if (pglobal->buf != NULL) free(pglobal->buf); }
void signal_handler(int sigm) { /* signal "stop" to threads */ stop = 1; /* cleanup most important structures */ fprintf(stderr, "shutdown...\n"); usleep(1000 * 1000); close_v4l2(cd.videoIn); free(cd.videoIn); if (close(sd) < 0) perror("close sd"); ; pthread_cond_destroy(&db_update); pthread_mutex_destroy(&db); vision_control_task_dangerous(); motor(0, 0); exit(0); return; }
/****************************************************************************** Description.: Input Value.: Return Value: ******************************************************************************/ void cam_cleanup(void *arg) { int i; static unsigned char first_run = 1; context *activecam, *pcontext = arg; pglobal = pcontext->pglobal; if(!first_run) { DBG("already cleaned up ressources\n"); return; } first_run = 0; IPRINT("cleaning up ressources allocated by input thread\n"); for(i = 0; i < mcnt; i++) { activecam = &cams[pcontext->id + i]; close_v4l2(activecam->videoIn); if(activecam->videoIn->tmpbuffer != NULL) free(activecam->videoIn->tmpbuffer); if(activecam->videoIn != NULL) free(activecam->videoIn); } if(pglobal->in[pcontext->id].buf != NULL) free(pglobal->in[pcontext->id].buf); }
int main(int argc, char *argv[]) { #ifndef EMBEDED_X210 //PC platform const SDL_VideoInfo *info; char driver[128]; SDL_Surface *pscreen; SDL_Overlay *overlay; SDL_Rect drect; SDL_Event sdlevent; SDL_Thread *mythread; SDL_mutex *affmutex; Uint32 currtime; Uint32 lasttime; #endif int status; unsigned char *p = NULL; int hwaccel = 0; const char *videodevice = NULL; const char *mode = NULL; int format = V4L2_PIX_FMT_MJPEG; int i; int grabmethod = 1; int width = 320; int height = 240; int fps = 15; unsigned char frmrate = 0; char *avifilename = NULL; int queryformats = 0; int querycontrols = 0; int readconfigfile = 0; char *separateur; char *sizestring = NULL; char *fpsstring = NULL; int enableRawStreamCapture = 0; int enableRawFrameCapture = 0; char * pRGBData=NULL; printf("luvcview version %s \n", version); 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"); exit(1); } videodevice = strdup(argv[i + 1]); } if (strcmp(argv[i], "-g") == 0) { /* Ask for read instead default mmap */ grabmethod = 0; } if (strcmp(argv[i], "-w") == 0) { /* disable hw acceleration */ hwaccel = 1; } if (strcmp(argv[i], "-f") == 0) { if (i + 1 >= argc) { printf("No parameter specified with -f, aborting.\n"); exit(1); } mode = strdup(argv[i + 1]); if (strncmp(mode, "yuv", 3) == 0) { format = V4L2_PIX_FMT_YUYV; } else if (strncmp(mode, "jpg", 3) == 0) { format = V4L2_PIX_FMT_MJPEG; } else { format = V4L2_PIX_FMT_MJPEG; } } if (strcmp(argv[i], "-s") == 0) { if (i + 1 >= argc) { printf("No parameter specified with -s, aborting.\n"); exit(1); } sizestring = strdup(argv[i + 1]); width = strtoul(sizestring, &separateur, 10); if (*separateur != 'x') { printf("Error in size use -s widthxheight \n"); exit(1); } else { ++separateur; height = strtoul(separateur, &separateur, 10); if (*separateur != 0) printf("hmm.. dont like that!! trying this height \n"); printf(" size width: %d height: %d \n", width, height); } } if (strcmp(argv[i], "-i") == 0){ if (i + 1 >= argc) { printf("No parameter specified with -i, aborting. \n"); exit(1); } fpsstring = strdup(argv[i + 1]); fps = strtoul(fpsstring, &separateur, 10); printf(" interval: %d fps \n", fps); } if (strcmp(argv[i], "-S") == 0) { /* Enable raw stream capture from the start */ enableRawStreamCapture = 1; } if (strcmp(argv[i], "-c") == 0) { /* Enable raw frame capture for the first frame */ enableRawFrameCapture = 1; } if (strcmp(argv[i], "-C") == 0) { /* Enable raw frame stream capture from the start*/ enableRawFrameCapture = 2; } if (strcmp(argv[i], "-o") == 0) { /* set the avi filename */ if (i + 1 >= argc) { printf("No parameter specified with -o, aborting.\n"); exit(1); } avifilename = strdup(argv[i + 1]); } if (strcmp(argv[i], "-L") == 0) { /* query list of valid video formats */ queryformats = 1; } if (strcmp(argv[i], "-l") == 0) { /* query list of valid video formats */ querycontrols = 1; } if (strcmp(argv[i], "-r") == 0) { /* query list of valid video formats */ readconfigfile = 1; } if (strcmp(argv[i], "-h") == 0) { printf("usage: uvcview [-h -d -g -f -s -i -c -o -C -S -L -l -r] \n"); printf("-h print this message \n"); printf("-d /dev/videoX use videoX device\n"); printf("-g use read method for grab instead mmap \n"); printf("-w disable SDL hardware accel. \n"); printf("-f video format default jpg others options are yuv jpg \n"); printf("-i fps use specified frame interval \n"); printf("-s widthxheight use specified input size \n"); printf("-c enable raw frame capturing for the first frame\n"); printf("-C enable raw frame stream capturing from the start\n"); printf("-S enable raw stream capturing from the start\n"); printf("-o avifile create avifile, default video.avi\n"); printf("-L query valid video formats\n"); printf("-l query valid controls and settings\n"); printf("-r read and set control settings from luvcview.cfg\n"); exit(0); } } #ifndef EMBEDED_X210 //PC platform /************* Test SDL capabilities ************/ if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } /* For this version, we'll be save and disable hardware acceleration */ if(hwaccel) { if ( ! getenv("SDL_VIDEO_YUV_HWACCEL") ) { putenv("SDL_VIDEO_YUV_HWACCEL=0"); } } if (SDL_VideoDriverName(driver, sizeof(driver))) { printf("Video driver: %s\n", driver); } info = SDL_GetVideoInfo(); if (info->wm_available) { printf("A window manager is available\n"); } if (info->hw_available) { printf("Hardware surfaces are available (%dK video memory)\n", info->video_mem); SDL_VIDEO_Flags |= SDL_HWSURFACE; } if (info->blit_hw) { printf("Copy blits between hardware surfaces are accelerated\n"); SDL_VIDEO_Flags |= SDL_ASYNCBLIT; } if (info->blit_hw_CC) { printf ("Colorkey blits between hardware surfaces are accelerated\n"); } if (info->blit_hw_A) { printf("Alpha blits between hardware surfaces are accelerated\n"); } if (info->blit_sw) { printf ("Copy blits from software surfaces to hardware surfaces are accelerated\n"); } if (info->blit_sw_CC) { printf ("Colorkey blits from software surfaces to hardware surfaces are accelerated\n"); } if (info->blit_sw_A) { printf ("Alpha blits from software surfaces to hardware surfaces are accelerated\n"); } if (info->blit_fill) { printf("Color fills on hardware surfaces are accelerated\n"); } if (!(SDL_VIDEO_Flags & SDL_HWSURFACE)) SDL_VIDEO_Flags |= SDL_SWSURFACE; #endif if (videodevice == NULL || *videodevice == 0) { videodevice = "/dev/video0"; } if (avifilename == NULL || *avifilename == 0) { avifilename = "video.avi"; } videoIn = (struct vdIn *) calloc(1, sizeof(struct vdIn)); if ( queryformats ) { /* if we're supposed to list the video formats, do that now and go out */ check_videoIn(videoIn,(char *) videodevice); free(videoIn); #ifndef EMBEDED_X210 SDL_Quit(); #endif exit(1); } if (init_videoIn(videoIn, (char *) videodevice, width, height, fps, format, grabmethod, avifilename) < 0) exit(1); /* if we're supposed to list the controls, do that now */ if ( querycontrols ) enum_controls(videoIn->fd); /* if we're supposed to read the control settings from a configfile, do that now */ if ( readconfigfile ) load_controls(videoIn->fd); #ifdef EMBEDED_X210 #ifdef SOFT_COLOR_CONVERT init_framebuffer(); #else x6410_init_Draw(videoIn->width,videoIn->height); #endif #else pscreen = SDL_SetVideoMode(videoIn->width, videoIn->height+30 , 0,SDL_VIDEO_Flags); overlay =SDL_CreateYUVOverlay(videoIn->width, videoIn->height+30 , SDL_YUY2_OVERLAY, pscreen); p = (unsigned char *) overlay->pixels[0]; drect.x = 0; drect.y = 0; drect.w =pscreen->w; drect.h = pscreen->h; #endif if (enableRawStreamCapture) { videoIn->captureFile = fopen("stream.raw", "wb"); if(videoIn->captureFile == NULL) { perror("Unable to open file for raw stream capturing"); } else { printf("Starting raw stream capturing to stream.raw ...\n"); } } if (enableRawFrameCapture) videoIn->rawFrameCapture = enableRawFrameCapture; initLut(); #ifndef EMBEDED_X210 SDL_WM_SetCaption(title_act[A_VIDEO].title, NULL); lasttime = SDL_GetTicks(); creatButt(videoIn->width, 32); SDL_LockYUVOverlay(overlay); memcpy(p + (videoIn->width * (videoIn->height) * 2), YUYVbutt, videoIn->width * 64); SDL_UnlockYUVOverlay(overlay); /* initialize thread data */ ptdata.ptscreen = &pscreen; ptdata.ptvideoIn = videoIn; ptdata.ptsdlevent = &sdlevent; ptdata.drect = &drect; affmutex = SDL_CreateMutex(); ptdata.affmutex = affmutex; mythread = SDL_CreateThread(eventThread, (void *) &ptdata); #endif pRGBData = (unsigned char *)malloc(videoIn->width*videoIn->width*4*sizeof(char)); if(pRGBData==NULL) { return ; } /* main big loop */ while (videoIn->signalquit) { #ifndef EMBEDED_X210 currtime = SDL_GetTicks(); if (currtime - lasttime > 0) { frmrate = 1000/(currtime - lasttime); } lasttime = currtime; #endif if (uvcGrab(videoIn) < 0) { printf("Error grabbing \n"); break; } /* if we're grabbing video, show the frame rate */ if (videoIn->toggleAvi) printf("\rframe rate: %d ",frmrate); #ifndef EMBEDED_X210 SDL_LockYUVOverlay(overlay); memcpy(p, videoIn->framebuffer, videoIn->width * (videoIn->height) * 2); SDL_UnlockYUVOverlay(overlay); SDL_DisplayYUVOverlay(overlay, &drect); #endif #ifdef EMBEDED_X210 #ifdef SOFT_COLOR_CONVERT // yuv to rgb565 ,and to frambuffer process_image(videoIn->framebuffer,fbp,videoIn->width,videoIn->height,vinfo,finfo); // convertYUYVtoRGB565(videoIn->framebuffer,pRGBData,videoIn->width,videoIn->height); // Pyuv422torgb24(videoIn->framebuffer, pRGBData, videoIn->width, videoIn->height); // memcpy(fbp,pRGBData,videoIn->width*videoIn->height*2); #else //X6410 post processor convert yuv to rgb,X210 not suport now. /* memcpy(pInbuffer, videoIn->framebuffer, videoIn->width * (videoIn->height) * 2); ioctl(dev_fb0, GET_FB_INFO, &fb_info); pp_param.SrcFrmSt = ioctl(dev_pp, S3C_PP_GET_RESERVED_MEM_ADDR_PHY); //must be physical adress pp_param.DstFrmSt = fb_info.map_dma_f1; //must be physical adress ioctl(dev_pp, S3C_PP_SET_PARAMS, &pp_param); ioctl(dev_pp, S3C_PP_SET_DST_BUF_ADDR_PHY, &pp_param); ioctl(dev_pp, S3C_PP_SET_SRC_BUF_ADDR_PHY, &pp_param); ioctl(dev_pp, S3C_PP_START); */ #endif #endif if (videoIn->getPict) { switch(videoIn->formatIn){ case V4L2_PIX_FMT_MJPEG: get_picture(videoIn->tmpbuffer,videoIn->buf.bytesused); break; case V4L2_PIX_FMT_YUYV: get_pictureYV2(videoIn->framebuffer,videoIn->width,videoIn->height); break; default: break; } videoIn->getPict = 0; printf("get picture !\n"); } #ifndef EMBEDED_X210 SDL_LockMutex(affmutex); ptdata.frmrate = frmrate; SDL_WM_SetCaption(videoIn->status, NULL); SDL_UnlockMutex(affmutex); #endif #ifdef EMBEDED_X210 usleep(10); #else SDL_Delay(10); #endif } #ifndef EMBEDED_X210 SDL_WaitThread(mythread, &status); SDL_DestroyMutex(affmutex); #endif /* if avifile is defined, we made a video: compute the exact fps and set it in the video */ if (videoIn->avifile != NULL) { float fps=(videoIn->framecount/(videoIn->recordtime/1000)); fprintf(stderr,"setting fps to %f\n",fps); AVI_set_video(videoIn->avifile, videoIn->width, videoIn->height, fps, "MJPG"); AVI_close(videoIn->avifile); } close_v4l2(videoIn); #ifdef EMBEDED_X210 #ifdef SOFT_COLOR_CONVERT close_frambuffer(); #else x6410_DeInit_Draw(); #endif #endif free(pRGBData); free(videoIn); destroyButt(); freeLut(); printf(" Clean Up done Quit \n"); #ifndef EMBEDED_X210 SDL_Quit(); #endif }
gint shutd (gint restart) { int exec_status=0; int *tstatus; int i; printf("Shuting Down Thread\n"); videoIn->signalquit=0; printf("waiting for thread to finish\n"); SDL_WaitThread(mythread, tstatus);//wait for thread to finish printf("Thread terminated with satus=%i\n",*tstatus); /*It seems there is a race condition causing the call to SDL_Quit to sometimes segfault */ /*Thread signals exit before it actualy closes, ?some sort off buffering? */ /*will put SDL_quit some lines down and wait some ms just in case */ SDL_Delay(SDL_WAIT_TIME); gtk_window_get_size(mainwin,&winwidth,&winheight);//mainwin or widget close_v4l2(videoIn); close(videoIn->fd); printf("closed strutures\n"); free(videoIn); free(capt); SDL_Quit(); printf("SDL Quit\n"); printf("cleaned allocations - 50%\n"); gtk_main_quit(); printf("GTK quit\n"); writeConf(confPath); input_free_controls (s->control, s->num_controls); printf("free controls - vidState\n"); char locpath[30]; char fullpath[50]; char *pwd; if (restart==1) { if (sscanf(ARG_V[0],"./%s",locpath)==1) { //guvcview started from local path pwd=getenv("PWD"); sprintf(fullpath,"%s/%s",pwd,locpath); if((ARG_V[0]=realloc(ARG_V[0],(strlen(fullpath)+1)*sizeof(char)))!=NULL){ strcpy(ARG_V[0],fullpath); } else { printf("Couldn't realloc mem (terminating..)\n"); for(i=0;i<ARG_C;i++){ free(ARG_V[i]); } free(ARG_V); printf("cleaned allocations - 100%\n"); return(2); } } printf("restarting guvcview\n"); exec_status = execvp(ARG_V[0], ARG_V); } for(i=0;i<ARG_C;i++){ free(ARG_V[i]); } free(ARG_V); printf("cleanig allocations - 100%\n"); return exec_status; }
int main (int argc, char *argv[]) { char *videodevice = "/dev/video0"; char *outputfile = "snap.jpg"; char *post_capture_command[3]; int format = V4L2_PIX_FMT_MJPEG; int grabmethod = 1; int width = 320; int height = 240; int brightness = 0, contrast = 0, saturation = 0, gain = 0; int verbose = 0; int delay = 0; int quality = 95; int post_capture_command_wait = 0; time_t ref_time; struct vdIn *videoIn; FILE *file; (void) signal (SIGINT, sigcatch); (void) signal (SIGQUIT, sigcatch); (void) signal (SIGKILL, sigcatch); (void) signal (SIGTERM, sigcatch); (void) signal (SIGABRT, sigcatch); (void) signal (SIGTRAP, sigcatch); // set post_capture_command to default values post_capture_command[0] = NULL; post_capture_command[1] = NULL; post_capture_command[2] = NULL; //Options Parsing (FIXME) while ((argc > 1) && (argv[1][0] == '-')) { switch (argv[1][1]) { case 'v': verbose++; break; case 'o': outputfile = &argv[1][2]; break; case 'd': videodevice = &argv[1][2]; break; case 'x': width = atoi (&argv[1][2]); break; case 'y': height = atoi (&argv[1][2]); break; case 'r': grabmethod = 0; break; case 'm': format = V4L2_PIX_FMT_YUYV; break; case 't': delay = atoi (&argv[1][2]); break; case 'c': post_capture_command[0] = &argv[1][2]; break; case 'w': post_capture_command_wait = 1; break; case 'B': brightness = atoi (&argv[1][2]); break; case 'C': contrast = atoi (&argv[1][2]); break; case 'S': saturation = atoi (&argv[1][2]); break; case 'G': gain = atoi (&argv[1][2]); break; case 'q': quality = atoi (&argv[1][2]); break; case 'h': usage (); break; default: fprintf (stderr, "Unknown option %s \n", argv[1]); usage (); } ++argv; --argc; } if ((width > 960) || (height > 720) || (quality != 95)) format = V4L2_PIX_FMT_YUYV; if (post_capture_command[0]) post_capture_command[1] = outputfile; if (verbose >= 1) { fprintf (stderr, "Using videodevice: %s\n", videodevice); fprintf (stderr, "Saving images to: %s\n", outputfile); fprintf (stderr, "Image size: %dx%d\n", width, height); fprintf (stderr, "Taking snapshot every %d seconds\n", delay); if (grabmethod == 1) fprintf (stderr, "Taking images using mmap\n"); else fprintf (stderr, "Taking images using read\n"); if (post_capture_command[0]) fprintf (stderr, "Executing '%s' after each image capture\n", post_capture_command[0]); } videoIn = (struct vdIn *) calloc (1, sizeof (struct vdIn)); if (init_videoIn (videoIn, (char *) videodevice, width, height, format, grabmethod) < 0) exit (1); //Reset all camera controls if (verbose >= 1) fprintf (stderr, "Resetting camera settings\n"); v4l2ResetControl (videoIn, V4L2_CID_BRIGHTNESS); v4l2ResetControl (videoIn, V4L2_CID_CONTRAST); v4l2ResetControl (videoIn, V4L2_CID_SATURATION); v4l2ResetControl (videoIn, V4L2_CID_GAIN); //Setup Camera Parameters if (brightness != 0) { if (verbose >= 1) fprintf (stderr, "Setting camera brightness to %d\n", brightness); v4l2SetControl (videoIn, V4L2_CID_BRIGHTNESS, brightness); } else if (verbose >= 1) { fprintf (stderr, "Camera brightness level is %d\n", v4l2GetControl (videoIn, V4L2_CID_BRIGHTNESS)); } if (contrast != 0) { if (verbose >= 1) fprintf (stderr, "Setting camera contrast to %d\n", contrast); v4l2SetControl (videoIn, V4L2_CID_CONTRAST, contrast); } else if (verbose >= 1) { fprintf (stderr, "Camera contrast level is %d\n", v4l2GetControl (videoIn, V4L2_CID_CONTRAST)); } if (saturation != 0) { if (verbose >= 1) fprintf (stderr, "Setting camera saturation to %d\n", saturation); v4l2SetControl (videoIn, V4L2_CID_SATURATION, saturation); } else if (verbose >= 1) { fprintf (stderr, "Camera saturation level is %d\n", v4l2GetControl (videoIn, V4L2_CID_SATURATION)); } if (gain != 0) { if (verbose >= 1) fprintf (stderr, "Setting camera gain to %d\n", gain); v4l2SetControl (videoIn, V4L2_CID_GAIN, gain); } else if (verbose >= 1) { fprintf (stderr, "Camera gain level is %d\n", v4l2GetControl (videoIn, V4L2_CID_GAIN)); } ref_time = time (NULL); while (run) { if (verbose >= 2) fprintf (stderr, "Grabbing frame\n"); if (uvcGrab (videoIn) < 0) { fprintf (stderr, "Error grabbing\n"); close_v4l2 (videoIn); free (videoIn); exit (1); } if ((difftime (time (NULL), ref_time) > delay) || delay == 0) { if (verbose >= 1) fprintf (stderr, "Saving image to: %s\n", outputfile); file = fopen (outputfile, "wb"); if (file != NULL) { switch (videoIn->formatIn) { case V4L2_PIX_FMT_YUYV: compress_yuyv_to_jpeg (videoIn, file, quality); break; default: fwrite (videoIn->tmpbuffer, videoIn->buf.bytesused + DHT_SIZE, 1, file); break; } fclose (file); videoIn->getPict = 0; } if (post_capture_command[0]) { if (verbose >= 1) fprintf (stderr, "Executing '%s %s'\n", post_capture_command[0], post_capture_command[1]); if (spawn (post_capture_command, post_capture_command_wait, verbose)) { fprintf (stderr, "Command exited with error\n"); close_v4l2 (videoIn); free (videoIn); exit (1); } } ref_time = time (NULL); } if (delay == 0) break; } close_v4l2 (videoIn); free (videoIn); return 0; }
void startVideoSrvr() { pthread_t videoSocketThread; /* alloc mameory for the videoIn struct & initialize */ videoIn = (struct vdIn *) calloc (1, sizeof (struct vdIn)); if (init_videoIn (videoIn, (char *) videodevice, width, height, format, grabmethod) < 0) exit (1); /* alloc memory for the control struct & video out array & initialize */ ctrlStruct ctrl, *pc; if ((ctrl.imgArray = malloc(3 * width * height)) < 0) // enough space for rgb exit(-1); ctrl.doCapture = 0; //Reset all camera controls if (verbose >= 1) fprintf (stderr, "Resetting camera settings\n"); v4l2ResetControl (videoIn, V4L2_CID_BRIGHTNESS); v4l2ResetControl (videoIn, V4L2_CID_CONTRAST); v4l2ResetControl (videoIn, V4L2_CID_SATURATION); v4l2ResetControl (videoIn, V4L2_CID_GAIN); //Setup Camera Parameters if (brightness != 0) { if (verbose >= 1) fprintf (stderr, "Setting camera brightness to %d\n", brightness); v4l2SetControl (videoIn, V4L2_CID_BRIGHTNESS, brightness); } else if (verbose >= 1) { fprintf (stderr, "Camera brightness level is %d\n", v4l2GetControl (videoIn, V4L2_CID_BRIGHTNESS)); } if (contrast != 0) { if (verbose >= 1) fprintf (stderr, "Setting camera contrast to %d\n", contrast); v4l2SetControl (videoIn, V4L2_CID_CONTRAST, contrast); } else if (verbose >= 1) { fprintf (stderr, "Camera contrast level is %d\n", v4l2GetControl (videoIn, V4L2_CID_CONTRAST)); } if (saturation != 0) { if (verbose >= 1) fprintf (stderr, "Setting camera saturation to %d\n", saturation); v4l2SetControl (videoIn, V4L2_CID_SATURATION, saturation); } else if (verbose >= 1) { fprintf (stderr, "Camera saturation level is %d\n", v4l2GetControl (videoIn, V4L2_CID_SATURATION)); } if (gain != 0) { if (verbose >= 1) fprintf (stderr, "Setting camera gain to %d\n", gain); v4l2SetControl (videoIn, V4L2_CID_GAIN, gain); } else if (verbose >= 1) { fprintf (stderr, "Camera gain level is %d\n", v4l2GetControl (videoIn, V4L2_CID_GAIN)); } // wait for a video client to connect before proceeding fprintf (stderr, "waiting for video client connection on port %d\n", port); if ((ctrl.videoSocket = wait4client(port)) <= 0) { fprintf (stderr, "error connecting to client: %d\n", ctrl.videoSocket); exit(-1); } // start the thread that handles the video client requests pthread_create(&videoSocketThread, NULL, (void *)cmdHandler, (void *)&ctrl); while (run) { if (verbose >= 2) fprintf (stderr, "."); if (uvcGrab (videoIn) < 0) { fprintf (stderr, "Error grabbing\n"); close_v4l2 (videoIn); free (videoIn); exit (1); } if (ctrl.doCapture == 1) { if (verbose >= 1) { fprintf (stderr, "captured %d byte image at 0x%x %dx%d\n", videoIn->framesizeIn, videoIn->framebuffer, videoIn->width, videoIn->height); } else { fprintf (stderr, "."); } if (outputType == 0) yuyv2Y(videoIn, &ctrl); else yuyv2rgb(videoIn, &ctrl); if (verbose >=1) fprintf (stderr, "converted image to luminance in buffer at 0x%x\n", ctrl.imgArray); videoIn->getPict = 0; ctrl.doCapture = 0; } } close_v4l2 (videoIn); free (videoIn); return; }
int main(int argc, char *argv[]) { char driver[128]; int status; //Uint32 currtime; //Uint32 lasttime; unsigned char *p = NULL; int hwaccel = 0; const char *videodevice = NULL; const char *mode = NULL; int format = V4L2_PIX_FMT_MJPEG; int i; int grabmethod = 1; int width = 320; int height = 240; int fps = 15; unsigned char frmrate = 0; char *avifilename = NULL; int queryformats = 0; int querycontrols = 0; int readconfigfile = 0; char *separateur; char *sizestring = NULL; char *fpsstring = NULL; int enableRawStreamCapture = 0; int enableRawFrameCapture = 0; printf("luvcview version %s \n", version); 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"); exit(1); } videodevice = strdup(argv[i + 1]); } if (strcmp(argv[i], "-g") == 0) { /* Ask for read instead default mmap */ grabmethod = 0; } if (strcmp(argv[i], "-w") == 0) { /* disable hw acceleration */ hwaccel = 1; } if (strcmp(argv[i], "-f") == 0) { if (i + 1 >= argc) { printf("No parameter specified with -f, aborting.\n"); exit(1); } mode = strdup(argv[i + 1]); if (strncmp(mode, "yuv", 3) == 0) { format = V4L2_PIX_FMT_YUYV; } else if (strncmp(mode, "jpg", 3) == 0) { format = V4L2_PIX_FMT_MJPEG; } else { format = V4L2_PIX_FMT_JPEG; } } if (strcmp(argv[i], "-s") == 0) { if (i + 1 >= argc) { printf("No parameter specified with -s, aborting.\n"); exit(1); } sizestring = strdup(argv[i + 1]); width = strtoul(sizestring, &separateur, 10); if (*separateur != 'x') { printf("Error in size use -s widthxheight \n"); exit(1); } else { ++separateur; height = strtoul(separateur, &separateur, 10); if (*separateur != 0) printf("hmm.. dont like that!! trying this height \n"); printf(" size width: %d height: %d \n", width, height); } } if (strcmp(argv[i], "-i") == 0) { if (i + 1 >= argc) { printf("No parameter specified with -i, aborting. \n"); exit(1); } fpsstring = strdup(argv[i + 1]); fps = strtoul(fpsstring, &separateur, 10); printf(" interval: %d fps \n", fps); } if (strcmp(argv[i], "-S") == 0) { /* Enable raw stream capture from the start */ enableRawStreamCapture = 1; } if (strcmp(argv[i], "-c") == 0) { /* Enable raw frame capture for the first frame */ enableRawFrameCapture = 1; } if (strcmp(argv[i], "-C") == 0) { /* Enable raw frame stream capture from the start*/ enableRawFrameCapture = 2; } if (strcmp(argv[i], "-o") == 0) { /* set the avi filename */ if (i + 1 >= argc) { printf("No parameter specified with -o, aborting.\n"); exit(1); } avifilename = strdup(argv[i + 1]); } if (strcmp(argv[i], "-L") == 0) { /* query list of valid video formats */ queryformats = 1; } if (strcmp(argv[i], "-l") == 0) { /* query list of valid video formats */ querycontrols = 1; } if (strcmp(argv[i], "-r") == 0) { /* query list of valid video formats */ readconfigfile = 1; } if (strcmp(argv[i], "-O") == 0) { /* get picture */ getpictureflag = 1; } if (strcmp(argv[i], "-h") == 0) { printf( "usage: uvcview [-h -d -g -f -s -i -c -o -C -S -L -l -r] \n"); printf("-h print this message \n"); printf("-d /dev/videoX use videoX device\n"); printf("-g use read method for grab instead mmap \n"); printf("-w disable SDL hardware accel. \n"); printf( "-f video format default jpg others options are yuv jpg \n"); printf("-i fps use specified frame interval \n"); printf("-s widthxheight use specified input size \n"); printf("-c enable raw frame capturing for the first frame\n"); printf("-C enable raw frame stream capturing from the start\n"); printf("-S enable raw stream capturing from the start\n"); printf("-o avifile create avifile, default video.avi\n"); printf("-L query valid video formats\n"); printf("-l query valid controls and settings\n"); printf("-r read and set control settings from luvcview.cfg\n"); printf("-O get picture.\n"); exit(0); } } if (videodevice == NULL || *videodevice == 0) { videodevice = "/dev/video0"; } if (avifilename == NULL || *avifilename == 0) { avifilename = "video.avi"; } videoIn = (struct vdIn *) calloc(1, sizeof(struct vdIn)); if (queryformats) { /* if we're supposed to list the video formats, do that now and go out */ check_videoIn(videoIn, (char *) videodevice); free(videoIn); exit(1); } if (init_videoIn(videoIn, (char *) videodevice, width, height, fps, format, grabmethod, avifilename) < 0) exit(1); /* if we're supposed to list the controls, do that now */ if (querycontrols) enum_controls(videoIn->fd); /* if we're supposed to read the control settings from a configfile, do that now */ if (readconfigfile) load_controls(videoIn->fd); printf("Enable Raw Stream Capture\n"); if (enableRawStreamCapture) { videoIn->captureFile = fopen("stream.raw", "wb"); if (videoIn->captureFile == NULL) { perror("Unable to open file for raw stream capturing"); } else { printf("Starting raw stream capturing to stream.raw ...\n"); } } if (enableRawFrameCapture) videoIn->rawFrameCapture = enableRawFrameCapture; initLut(); printf("Begin main big loop\n"); int loopNum = 0; /* main big loop */ while (videoIn->signalquit) { // if (uvcGrab(videoIn) < 0) { printf("Error grabbing \n"); break; } // /* if we're grabbing video, show the frame rate */ if (videoIn->toggleAvi) printf("\rframe rate: %d ", frmrate); // if (getpictureflag) { //if (videoIn->getPict) { switch (videoIn->formatIn) { case V4L2_PIX_FMT_MJPEG: get_picture(videoIn->tmpbuffer, videoIn->buf.bytesused); break; case V4L2_PIX_FMT_YUYV: printf("get picture yuv...\n"); get_pictureYV2(videoIn->framebuffer, videoIn->width, videoIn->height); break; default: break; } videoIn->getPict = 0; printf("get picture !\n"); } printf("loop number %d\n",loopNum); loopNum ++; } /* if avifile is defined, we made a video: compute the exact fps and set it in the video */ if (videoIn->avifile != NULL) { float fps = (videoIn->framecount / (videoIn->recordtime / 1000)); fprintf(stderr, "setting fps to %f\n", fps); AVI_set_video(videoIn->avifile, videoIn->width, videoIn->height, fps, "MJPG"); AVI_close(videoIn->avifile); } close_v4l2(videoIn); free(videoIn); freeLut(); printf(" Clean Up done Quit \n"); }
int luvcview() { const SDL_VideoInfo *info; char driver[128]; SDL_Surface *pscreen; SDL_Surface *pparent; SDL_Overlay *overlay; SDL_Rect drect; int status; Uint32 currtime; Uint32 lasttime; unsigned char *p = NULL; int hwaccel = 0; const char *videodevice = NULL; const char *mode = NULL; int format = V4L2_PIX_FMT_MJPEG; int i; int grabmethod = 1; int width = 320; int height = 240; int fps = 15; unsigned char frmrate = 0; char *avifilename = NULL; int queryformats = 0; int querycontrols = 0; int readconfigfile = 0; char *separateur; char *sizestring = NULL; char *fpsstring = NULL; int enableRawStreamCapture = 0; int enableRawFrameCapture = 0; format = V4L2_PIX_FMT_YUYV; videodevice = "/dev/video0"; printf("luvcview version %s \n", version); if (SDL_VideoDriverName(driver, sizeof(driver))) { printf("Video driver: %s\n", driver); } info = SDL_GetVideoInfo(); if (info->wm_available) { printf("A window manager is available\n"); } if (info->hw_available) { printf("Hardware surfaces are available (%dK video memory)\n", info->video_mem); SDL_VIDEO_Flags |= SDL_HWSURFACE; } if (info->blit_hw) { printf("Copy blits between hardware surfaces are accelerated\n"); SDL_VIDEO_Flags |= SDL_ASYNCBLIT; } if (info->blit_hw_CC) { printf ("Colorkey blits between hardware surfaces are accelerated\n"); } if (info->blit_hw_A) { printf("Alpha blits between hardware surfaces are accelerated\n"); } if (info->blit_sw) { printf ("Copy blits from software surfaces to hardware surfaces are accelerated\n"); } if (info->blit_sw_CC) { printf ("Colorkey blits from software surfaces to hardware surfaces are accelerated\n"); } if (info->blit_sw_A) { printf ("Alpha blits from software surfaces to hardware surfaces are accelerated\n"); } if (info->blit_fill) { printf("Color fills on hardware surfaces are accelerated\n"); } if (!(SDL_VIDEO_Flags & SDL_HWSURFACE)) SDL_VIDEO_Flags |= SDL_SWSURFACE; if (avifilename == NULL || *avifilename == 0) { avifilename = "video.avi"; } videoIn = (struct vdIn *) calloc(1, sizeof(struct vdIn)); if (init_videoIn (videoIn, (char *) videodevice, width, height, fps, format, grabmethod, avifilename) < 0) exit(1); pscreen = SDL_GetVideoSurface(); overlay = SDL_CreateYUVOverlay(videoIn->width, videoIn->height , SDL_YUY2_OVERLAY, pscreen); p = (unsigned char *) overlay->pixels[0]; drect.x = 0; drect.y = 0; drect.w = pscreen->w; drect.h = pscreen->h; initLut(); lasttime = SDL_GetTicks(); int loop = 0; /* main big loop */ while (videoIn->signalquit) { currtime = SDL_GetTicks(); if (currtime - lasttime > 0) { frmrate = 1000/(currtime - lasttime); } lasttime = currtime; if (uvcGrab(videoIn) < 0) { printf("Error grabbing \n"); break; } SDL_LockYUVOverlay(overlay); memcpy(p, videoIn->framebuffer, videoIn->width * (videoIn->height) * 2); SDL_UnlockYUVOverlay(overlay); SDL_DisplayYUVOverlay(overlay, &drect); if (loop > 35) { printf( "loop: %d\n", loop); break; } ++loop; SDL_Delay(10); } close_v4l2(videoIn); free(videoIn); freeLut(); printf(" Clean Up done Quit \n"); }