Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
static int eventThread(void *data)
{
    struct pt_data *gdata = (struct pt_data *) data;
    struct v4l2_control control;
    SDL_Surface *pscreen = *gdata->ptscreen;
    struct vdIn *videoIn = gdata->ptvideoIn;
    SDL_Event *sdlevent = gdata->ptsdlevent;
    SDL_Rect *drect = gdata->drect;
    SDL_mutex *affmutex = gdata->affmutex;
    unsigned char frmrate;
    int x, y;
    int mouseon = 0;
    int value = 0;
    int len = 0;
    short incpantilt = INCPANTILT;
    int boucle = 0;
    action_gui curr_action = A_VIDEO;
    while (videoIn->signalquit)
    {
        SDL_LockMutex(affmutex);
        frmrate = gdata->frmrate;
        while (SDL_PollEvent(sdlevent))
        {	//scan the event queue
            switch (sdlevent->type) {
            case SDL_KEYUP:
            case SDL_MOUSEBUTTONUP:
                mouseon = 0;
                incpantilt = INCPANTILT;
                boucle = 0;
                break;
            case SDL_MOUSEBUTTONDOWN:
                mouseon = 1;
            case SDL_MOUSEMOTION:
                SDL_GetMouseState(&x, &y);
                curr_action = GUI_whichbutton(x, y, pscreen, videoIn);
                break;
            case SDL_VIDEORESIZE:
                pscreen =
                        SDL_SetVideoMode(sdlevent->resize.w,
                                         sdlevent->resize.h, 0,
                                         SDL_VIDEO_Flags);
                drect->w = sdlevent->resize.w;
                drect->h = sdlevent->resize.h;
                break;
            case SDL_KEYDOWN:
                curr_action = GUI_keytoaction(sdlevent->key.keysym.sym);
                if (curr_action != A_VIDEO)
                    mouseon = 1;
                break;
            case SDL_QUIT:
                printf("\nStop asked\n");
                videoIn->signalquit = 0;
                break;
            }
        }			//end if poll
        SDL_UnlockMutex(affmutex);
        /* traiter les actions */
        value = 0;
        if (mouseon){
            boucle++;
            switch (curr_action) {
            case A_BRIGHTNESS_UP:
                if ((value =
                     v4l2UpControl(videoIn, V4L2_CID_BRIGHTNESS)) < 0)
                    printf("Set Brightness up error\n");
                break;
            case A_CONTRAST_UP:
                if ((value =
                     v4l2UpControl(videoIn, V4L2_CID_CONTRAST)) < 0)
                    printf("Set Contrast up error \n");
                break;
            case A_SATURATION_UP:
                if ((value =
                     v4l2UpControl(videoIn, V4L2_CID_SATURATION)) < 0)
                    printf("Set Saturation up error\n");
                break;
            case A_GAIN_UP:
                if ((value = v4l2UpControl(videoIn, V4L2_CID_GAIN)) < 0)
                    printf("Set Gain up error\n");
                break;
            case A_SHARPNESS_UP:
                if ((value =
                     v4l2UpControl(videoIn, V4L2_CID_SHARPNESS)) < 0)
                    printf("Set Sharpness up error\n");
                break;
            case A_GAMMA_UP:
                if ((value = v4l2UpControl(videoIn, V4L2_CID_GAMMA)) < 0)
                    printf("Set Gamma up error\n");
                break;
            case A_PAN_UP:
                if ((value =v4L2UpDownPan(videoIn, -incpantilt)) < 0)
                    printf("Set Pan up error\n");
                break;
            case A_TILT_UP:
                if ((value =v4L2UpDownTilt(videoIn, -incpantilt)) < 0)
                    printf("Set Tilt up error\n");
                break;
            case  A_PAN_RESET:
                if (v4l2ResetPanTilt(videoIn,1) < 0)
                    printf("reset pantilt error\n");
                break;
            case A_SCREENSHOT:
                SDL_Delay(200);
                videoIn->getPict = 1;
                value = 1;
                break;
            case A_RESET:

                if (v4l2ResetControl(videoIn, V4L2_CID_BRIGHTNESS) < 0)
                    printf("reset Brightness error\n");
                if (v4l2ResetControl(videoIn, V4L2_CID_SATURATION) < 0)
                    printf("reset Saturation error\n");
                if (v4l2ResetControl(videoIn, V4L2_CID_CONTRAST) < 0)
                    printf("reset Contrast error\n");
                if (v4l2ResetControl(videoIn, V4L2_CID_HUE) < 0)
                    printf("reset Hue error\n");
                if (v4l2ResetControl(videoIn, V4L2_CID_SHARPNESS) < 0)
                    printf("reset Sharpness error\n");
                if (v4l2ResetControl(videoIn, V4L2_CID_GAMMA) < 0)
                    printf("reset Gamma error\n");
                if (v4l2ResetControl(videoIn, V4L2_CID_GAIN) < 0)
                    printf("reset Gain error\n");
                if (v4l2ResetPanTilt(videoIn,3) < 0)
                    printf("reset pantilt error\n");

                break;
            case A_BRIGHTNESS_DOWN:
                if ((value = v4l2DownControl(videoIn, V4L2_CID_BRIGHTNESS)) < 0)
                    printf("Set Brightness down error\n");
                break;
            case A_CONTRAST_DOWN:
                if ((value = v4l2DownControl(videoIn, V4L2_CID_CONTRAST)) < 0)
                    printf("Set Contrast down error\n");
                break;
            case A_SATURATION_DOWN:
                if ((value = v4l2DownControl(videoIn, V4L2_CID_SATURATION)) < 0)
                    printf("Set Saturation down error\n");
                break;
            case A_GAIN_DOWN:
                if ((value = v4l2DownControl(videoIn, V4L2_CID_GAIN)) < 0)
                    printf("Set Gain down error\n");
                break;
            case A_SHARPNESS_DOWN:
                if ((value = v4l2DownControl(videoIn, V4L2_CID_SHARPNESS)) < 0)
                    printf("Set Sharpness down error\n");
                break;
            case A_GAMMA_DOWN:
                if ((value = v4l2DownControl(videoIn, V4L2_CID_GAMMA)) < 0)
                    printf("Set Gamma down error\n");
                break;
            case A_PAN_DOWN:
                if ((value =v4L2UpDownPan(videoIn, incpantilt)) < 0)
                    printf("Set Pan down error\n");
                break;
            case A_TILT_DOWN:
                if ((value =v4L2UpDownTilt(videoIn,incpantilt)) < 0)
                    printf("Set Tilt down error\n");
                break;
            case A_TILT_RESET:
                if (v4l2ResetPanTilt(videoIn,2) < 0)
                    printf("reset pantilt error\n");
                break;
            case A_RECORD_TOGGLE:
                SDL_Delay(200);
                videoIn->toggleAvi = !videoIn->toggleAvi;
                value = videoIn->toggleAvi;
                if ( value == 1 ) {
                    printf("avi recording started\n");
                    videoIn->recordstart=SDL_GetTicks();
                }
                else {
                    int dur=SDL_GetTicks()-videoIn->recordstart;
                    printf("\navi recording stopped (%ds)\n",dur/1000);
                    videoIn->recordtime+=dur;
                }
                break;
            case A_SWITCH_LIGHTFREQFILT:
                if ((value =v4l2GetControl(videoIn,V4L2_CID_POWER_LINE_FREQUENCY)) < 0)
                    printf("Get value of light frequency filter error\n");

                if(value < 2) // round switch 50->60->NoFliker->.
                    value++;   //		 \_______________;
                else
                    value=0;

                if(value == 0)
                    printf("Current light frequency filter: 50Hz\n");
                else if(value == 1)
                    printf("Current light frequency filter: 60Hz\n");
                else if(value == 2)
                    printf("Current light frequency filter: NoFliker\n");

                if ((value =v4l2SetLightFrequencyFilter(videoIn,value)) < 0)
                    printf("Switch light frequency filter error\n");


                break;
            case A_QUIT:
                videoIn->signalquit = 0;
                break;
            case A_VIDEO:
                break;
            case A_CAPTURE_FRAME:
                value = 1;
                videoIn->rawFrameCapture = 1;
                break;
            case A_CAPTURE_FRAMESTREAM:
                value = 1;
                if (!videoIn->rawFrameCapture) {
                    videoIn->rawFrameCapture = 2;
                    videoIn->rfsBytesWritten = 0;
                    videoIn->rfsFramesWritten = 0;
                    printf("Starting raw frame stream capturing ...\n");
                } else if(videoIn->framesWritten >= 5) {
                    videoIn->rawFrameCapture = 0;
                    printf("Stopped raw frame stream capturing. %u bytes written for %u frames.\n",
                           videoIn->rfsBytesWritten, videoIn->rfsFramesWritten);
                }
                break;
            case A_CAPTURE_STREAM:
                value = 1;
                if (videoIn->captureFile == NULL) {
                    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");
                    }
                    videoIn->bytesWritten = 0;
                    videoIn->framesWritten = 0;
                } else if(videoIn->framesWritten >= 5) {
                    fclose(videoIn->captureFile);
                    printf("Stopped raw stream capturing to stream.raw. %u bytes written for %u frames.\n",
                           videoIn->bytesWritten, videoIn->framesWritten);
                    videoIn->captureFile = NULL;
                }
                break;
            case A_EXPOSURE_UP:
                if ((value = v4l2UpControl(videoIn, V4L2_CID_EXPOSURE_ABSOLUTE)) < 0)
                    printf("Set Absolute Exposure up error\n");
                break;
            case A_EXPOSURE_DOWN:
                if ((value = v4l2DownControl(videoIn, V4L2_CID_EXPOSURE_ABSOLUTE)) < 0)
                    printf("Set Absolute Exposure down error\n");
                break;
            case A_EXPOSURE_ON:
                control.id    =V4L2_CID_EXPOSURE_AUTO;
                control.value =1;
                if ((value = ioctl(videoIn->fd, VIDIOC_S_CTRL, &control)) < 0)
                    printf("Set Auto Exposure on error\n");
                else
                    printf("Auto Exposure set to %d \n", control.value);
                break;
            case A_EXPOSURE_OFF:
                control.id    =V4L2_CID_EXPOSURE_AUTO;
                control.value =8;
                if ((value = ioctl(videoIn->fd, VIDIOC_S_CTRL, &control)) < 0)
                    printf("Set Auto Exposure off error\n");
                else
                    printf("Auto Exposure set to %d \n", control.value);
                break;
            case A_BALANCE_UP:
                if ((value = v4l2UpControl(videoIn, V4L2_CID_WHITE_BALANCE_TEMPERATURE)) < 0)
                    printf("Set Balance Temperature up error\n");
                break;
            case A_BALANCE_DOWN:
                if ((value = v4l2DownControl(videoIn, V4L2_CID_WHITE_BALANCE_TEMPERATURE)) < 0)
                    printf("Set Balance Temperature down error\n");
                break;
            case A_BALANCE_ON:
                control.id    =V4L2_CID_WHITE_BALANCE_TEMPERATURE_AUTO;
                control.value =1;
                if ((value = ioctl(videoIn->fd, VIDIOC_S_CTRL, &control)) < 0)
                    printf("Set Auto Balance on error\n");
                else
                    printf("Auto Balance set to %d \n", control.value);
                break;
            case A_BALANCE_OFF:
                control.id    =V4L2_CID_WHITE_BALANCE_TEMPERATURE_AUTO;
                control.value =0;
                if ((value = ioctl(videoIn->fd, VIDIOC_S_CTRL, &control)) < 0)
                    printf("Set Auto Balance off error\n");
                else
                    printf("Auto Balance set to %d \n", control.value);
                break;
            case A_SAVE:
                printf("Save controls \n");
                save_controls(videoIn->fd);
                break;
            case A_LOAD:
                printf("load controls \n");
                load_controls(videoIn->fd);
                break;
            default:
                break;
            }
            if(!(boucle%10)) // smooth pan tilt method
                if(incpantilt < (10*INCPANTILT))
                    incpantilt += (INCPANTILT/4);
            if(value){
                len = strlen(title_act[curr_action].title)+8;
                snprintf(videoIn->status, len,"%s %06d",title_act[curr_action].title,value);
            }
        } else { // mouseon

            len = strlen(title_act[curr_action].title)+9;
            snprintf(videoIn->status, len,"%s, %02d Fps",title_act[curr_action].title, frmrate);

        }
        SDL_Delay(50);
        //printf("fp/s %d \n",frmrate);
    }				//end main loop

    /* Close the stream capture file */
    if (videoIn->captureFile) {
        fclose(videoIn->captureFile);
        printf("Stopped raw stream capturing to stream.raw. %u bytes written for %u frames.\n",
               videoIn->bytesWritten, videoIn->framesWritten);
    }
    /* Display stats for raw frame stream capturing */
    if (videoIn->rawFrameCapture == 2) {
        printf("Stopped raw frame stream capturing. %u bytes written for %u frames.\n",
               videoIn->rfsBytesWritten, videoIn->rfsFramesWritten);
    }
}
Ejemplo n.º 3
0
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;
}
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
0
/******************************************************************************
Description.: process commands, allows to set certain runtime configurations
              and settings like pan/tilt, colors, saturation etc.
Input Value.: * cmd specifies the command, a complete list is maintained in
                the file "input.h"
              * value is used for commands that make use of a parameter.
Return Value: depends in the command, for most cases 0 means no errors and
              -1 signals an error. This is just rule of thumb, not more!
******************************************************************************/
int input_cmd(in_cmd_type cmd, int value) {
  int res=0;
  static int pan=0, tilt=0, pan_tilt_valid=-1;
  static int focus=-1;
  const int one_degree = ONE_DEGREE;

  /* certain commands do not need the mutex */
  if ( cmd != IN_CMD_RESET_PAN_TILT_NO_MUTEX )
    pthread_mutex_lock( &controls_mutex );

  switch (cmd) {
    case IN_CMD_HELLO:
      fprintf(stderr, "Hello from input plugin\n");
      break;

    case IN_CMD_RESET:
      DBG("about to reset all image controls to defaults\n");
      res = v4l2ResetControl(videoIn, V4L2_CID_BRIGHTNESS);
      res |= v4l2ResetControl(videoIn, V4L2_CID_CONTRAST);
      res |= v4l2ResetControl(videoIn, V4L2_CID_SATURATION);
      res |= v4l2ResetControl(videoIn, V4L2_CID_GAIN);
      if ( res != 0 ) res = -1;
      break;

    case IN_CMD_RESET_PAN_TILT:
    case IN_CMD_RESET_PAN_TILT_NO_MUTEX:
      DBG("about to set pan/tilt to default position\n");
      if ( uvcPanTilt(videoIn->fd, 0, 0, 3) != 0 ) {
        res = -1;
        break;
      }
      pan_tilt_valid = 1;
      pan = tilt = 0;
      sleep(4);
      break;

    case IN_CMD_PAN_SET:
      DBG("set pan to %d degrees\n", value);

      /* in order to calculate absolute positions we must check for initialized values */
      if ( pan_tilt_valid != 1 ) {
        if ( input_cmd(IN_CMD_RESET_PAN_TILT_NO_MUTEX, 0) == -1 ) {
          res = -1;
          break;
        }
      }

      /* limit pan-value to min and max, multiply it with constant "one_degree" */
      value = MIN(MAX(value*one_degree, MIN_PAN), MAX_PAN);

      /* calculate the relative degrees to move to the desired absolute pan-value */
      if( (res = value - pan) == 0 ) {
        /* do not move if this would mean to move by 0 degrees */
        res = pan/one_degree;
        break;
      }

      /* move it */
      pan = value;
      uvcPanTilt(videoIn->fd, res, 0, 0);
      res = pan/one_degree;

      DBG("pan: %d\n", pan);
      break;

    case IN_CMD_PAN_PLUS:
      DBG("pan +\n");

      if ( pan_tilt_valid != 1 ) {
        if ( input_cmd(IN_CMD_RESET_PAN_TILT_NO_MUTEX, 0) == -1 ) {
          res = -1;
          break;
        }
      }

      if ( (MAX_PAN) >= (pan+MIN_RES) ) {
        pan += MIN_RES;
        uvcPanTilt(videoIn->fd, MIN_RES, 0, 0);
      }
      res = pan/one_degree;

      DBG("pan: %d\n", pan);
      break;

    case IN_CMD_PAN_MINUS:
      DBG("pan -\n");

      if ( pan_tilt_valid != 1 ) {
        if ( input_cmd(IN_CMD_RESET_PAN_TILT_NO_MUTEX, 0) == -1 ) {
          res = -1;
          break;
        }
      }

      if ( (MIN_PAN) <= (pan-MIN_RES) ) {
        pan -= MIN_RES;
        uvcPanTilt(videoIn->fd, -MIN_RES, 0, 0);
      }
      res = pan/one_degree;

      DBG("pan: %d\n", pan);
      break;

    case IN_CMD_TILT_SET:
      DBG("set tilt to %d degrees\n", value);

      if ( pan_tilt_valid != 1 ) {
        if ( input_cmd(IN_CMD_RESET_PAN_TILT_NO_MUTEX, 0) == -1 ) {
          res = -1;
          break;
        }
      }

      /* limit pan-value to min and max, multiply it with constant "one_degree" */
      value = MIN(MAX(value*one_degree, MIN_TILT), MAX_TILT);

      /* calculate the relative degrees to move to the desired absolute pan-value */
      if( (res = value - tilt) == 0 ) {
        /* do not move if this would mean to move by 0 degrees */
        res = tilt/one_degree;
        break;
      }

      /* move it */
      tilt = value;
      uvcPanTilt(videoIn->fd, 0, res, 0);
      res = tilt/one_degree;

      DBG("tilt: %d\n", tilt);
      break;

    case IN_CMD_TILT_PLUS:
      DBG("tilt +\n");

      if ( pan_tilt_valid != 1 ) {
        if ( input_cmd(IN_CMD_RESET_PAN_TILT_NO_MUTEX, 0) == -1 ) {
          res = -1;
          break;
        }
      }

      if ( (MAX_TILT) >= (tilt+MIN_RES) ) {
        tilt += MIN_RES;
        uvcPanTilt(videoIn->fd, 0, MIN_RES, 0);
      }
      res = tilt/one_degree;

      DBG("tilt: %d\n", tilt);
      break;

    case IN_CMD_TILT_MINUS:
      DBG("tilt -\n");

      if ( pan_tilt_valid != 1 ) {
        if ( input_cmd(IN_CMD_RESET_PAN_TILT_NO_MUTEX, 0) == -1 ) {
          res = -1;
          break;
        }
      }

      if ( (MIN_TILT) <= (tilt-MIN_RES) ) {
        tilt -= MIN_RES;
        uvcPanTilt(videoIn->fd, 0, -MIN_RES, 0);
      }
      res = tilt/one_degree;

      DBG("tilt: %d\n", tilt);
      break;

    case IN_CMD_SATURATION_PLUS:
      DBG("saturation + (%d)\n", v4l2GetControl (videoIn, V4L2_CID_SATURATION));
      res = v4l2UpControl(videoIn, V4L2_CID_SATURATION);
      break;

    case IN_CMD_SATURATION_MINUS:
      DBG("saturation - (%d)\n", v4l2GetControl (videoIn, V4L2_CID_SATURATION));
      res = v4l2DownControl(videoIn, V4L2_CID_SATURATION);
      break;

    case IN_CMD_CONTRAST_PLUS:
      DBG("contrast + (%d)\n", v4l2GetControl (videoIn, V4L2_CID_CONTRAST));
      res = v4l2UpControl(videoIn, V4L2_CID_CONTRAST);
      break;

    case IN_CMD_CONTRAST_MINUS:
      DBG("contrast - (%d)\n", v4l2GetControl (videoIn, V4L2_CID_CONTRAST));
      res = v4l2DownControl(videoIn, V4L2_CID_CONTRAST);
      break;

    case IN_CMD_BRIGHTNESS_PLUS:
      DBG("brightness + (%d)\n", v4l2GetControl (videoIn, V4L2_CID_BRIGHTNESS));
      res = v4l2UpControl(videoIn, V4L2_CID_BRIGHTNESS);
      break;

    case IN_CMD_BRIGHTNESS_MINUS:
      DBG("brightness - (%d)\n", v4l2GetControl (videoIn, V4L2_CID_BRIGHTNESS));
      res = v4l2DownControl(videoIn, V4L2_CID_BRIGHTNESS);
      break;

    case IN_CMD_GAIN_PLUS:
      DBG("gain + (%d)\n", v4l2GetControl (videoIn, V4L2_CID_GAIN));
      res = v4l2UpControl(videoIn, V4L2_CID_GAIN);
      break;

    case IN_CMD_GAIN_MINUS:
      DBG("gain - (%d)\n", v4l2GetControl (videoIn, V4L2_CID_GAIN));
      res = v4l2DownControl(videoIn, V4L2_CID_GAIN);
      break;

    case IN_CMD_FOCUS_PLUS:
      DBG("focus + (%d)\n", focus);

      value=MIN(MAX(focus+10,0),255);

      if ( (res = v4l2SetControl(videoIn, V4L2_CID_FOCUS_LOGITECH, value)) == 0) {
        focus = value;
      }
      res = focus;
      break;

    case IN_CMD_FOCUS_MINUS:
      DBG("focus - (%d)\n", focus);

      value=MIN(MAX(focus-10,0),255);

      if ( (res = v4l2SetControl(videoIn, V4L2_CID_FOCUS_LOGITECH, value)) == 0) {
        focus = value;
      }
      res = focus;
      break;

    case IN_CMD_FOCUS_SET:
      value=MIN(MAX(value,0),255);
      DBG("set focus to %d\n", value);

      if ( (res = v4l2SetControl(videoIn, V4L2_CID_FOCUS_LOGITECH, value)) == 0) {
        focus = value;
      }
      res = focus;
      break;

    /* switch the webcam LED permanently on */
    case IN_CMD_LED_ON:
      res = v4l2SetControl(videoIn, V4L2_CID_LED1_MODE_LOGITECH, 1);
    break;

    /* switch the webcam LED permanently off */
    case IN_CMD_LED_OFF:
      res = v4l2SetControl(videoIn, V4L2_CID_LED1_MODE_LOGITECH, 0);
    break;

    /* switch the webcam LED on if streaming, off if not streaming */
    case IN_CMD_LED_AUTO:
      res = v4l2SetControl(videoIn, V4L2_CID_LED1_MODE_LOGITECH, 3);
    break;

    /* let the webcam LED blink at a given hardcoded intervall */
    case IN_CMD_LED_BLINK:
      res = v4l2SetControl(videoIn, V4L2_CID_LED1_MODE_LOGITECH, 2);
      res = v4l2SetControl(videoIn, V4L2_CID_LED1_FREQUENCY_LOGITECH, 255);
    break;

    default:
      DBG("nothing matched\n");
      res = -1;
  }

  if ( cmd != IN_CMD_RESET_PAN_TILT_NO_MUTEX )
    pthread_mutex_unlock( &controls_mutex );

  return res;
}