static int initialize_capture(v4l2_std_id * cur_std)
{
	int ret;
	printf("initializing capture device\n");
	init_capture_device();
	printf("setting data format\n");
	ret = set_data_format(cur_std);
	if (ret) {
		printf("Error in setting capture format\n");
		return ret;
	}
	printf("initializing capture buffers\n");
	ret = init_capture_buffers();
	if (ret) {
		printf("Failed to initialize capture buffers\n");
		return ret;
	}
	printf("initializing display device\n");
	ret = start_streaming();
	if (ret) {
		printf("Failed to start capture streaming\n");
		return ret;
	}
	return 0;
}
Exemplo n.º 2
0
int main(int argc, char **argv) {
	/*Assumes args are in order !!!!
	1: V4L2 dev name, 2: height, 3: width, 4: channel
	5: standard, 6: jpeg_quality, 7: verbosity, 8: log source, 9: log_level
	*/
	struct video_device *d;
	struct capture_device *cdev;
	int sockfd, port;
	jpeg_quality = JPEG_QUALITY;
	int fmts[NB_SUPPORTED_FORMATS] = SUPPORTED_FORMATS;

	//catch ctrl-c
	signal (SIGINT, catch_int);
	signal (SIGPIPE, SIG_IGN );

	//Parse cmd line options
	if(argc != NB_ARGS) {
		print_usage();
		exit(1);
	}

	jpeg_quality = atoi(argv[6]);
	requested_fps = atoi(argv[7]);
	verbosity = atoi(argv[8]);
	port = atoi(argv[9]);

#ifdef DEBUG
	log_source = atoi(argv[10]);
	log_level = atoi(argv[11]);
#endif

	d = open_device(argv[1]);
	if(d==NULL){
		info(LOG_ERR, "Cant open device %s",argv[1]);
		exit(1);
	}

	//create capture device
	cdev = init_capture_device(d, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), 5);
	if(cdev==NULL) {
		info(LOG_ERR, "Failed to initialise video device\n");
		info(LOG_ERR, "Recompile libv4l with debugging enabled (see README)\n");
		info(LOG_ERR, "to see why/where the initialisation fails.\n");
		exit(1);
	}

	//Set capture param (image format, color, size, crop...)
	if ((*cdev->actions->set_cap_param)(d, fmts , NB_SUPPORTED_FORMATS)!=0) {
		info(LOG_ERR, "Unable to set capture parameters. It could be due to:\n");
		info(LOG_ERR, " - the chosen width and height,\n - the driver not supporting the image formats libv4l tried\n");
		info(LOG_ERR, "Recompile libv4l with debugging enabled (see README)\n");
		info(LOG_ERR, "to see why/where the setup fails.\n");
		free_capture_device(d);
		close_device(d);
		exit(1);
	}

	info(LOG_INFO, "Capturing at %dx%d\n", cdev->width, cdev->height);

	//Prepare capture:Allocates v4l2 buffers, mmap buffers, enqueue buffers
	if ((*cdev->actions->init_capture)(d)) {
		info(LOG_ERR, "Failed to setup capture\n");
		free_capture_device(d);
		close_device(d);
		exit(1);
	}

	info(LOG_INFO, "Using palette %s\n", libv4l_palettes[cdev->palette].name);

	//init tcp server
	sockfd = setup_tcp_server_sock(port);

	//main loop
	main_loop(sockfd, d);

	//close tcp socket
	close(sockfd);

	//Deallocates V4l2 buffers
	(*cdev->actions->free_capture)(d);

	//delete cdev
	free_capture_device(d);

	//close device
	close_device(d);

	return 0;
}
Exemplo n.º 3
0
int main(int argc, char** argv) {
	struct capture_device *c;
	struct control_list *l;
	struct v4l2_queryctrl *qc;
	struct video_device *v;
	int std=0, channel=0, i, j;

	if(argc!=2 && argc!=4) {
		printf("This program requires the path to the video device file to be tested.\n");
		printf("The optional second and third arguments are a video standard and channel.\n");
		printf("Usage: %s <video_device_file> [standard channel]\n", argv[0]);
		printf("Video standards: webcam:0 - PAL:1 - SECAM:2 - NTSC:3\n");
		return -1;
	}


	if (argc==4){
		std = atoi(argv[2]);
		channel = atoi(argv[3]);
		printf("Using standard %d, channel %d\n",std, channel);
	}

	v = open_device(argv[1]);
	if(v==NULL){
		printf("Error opening device\n");
		return -1;
	}
	c = init_capture_device(v,MAX_WIDTH,MAX_HEIGHT,channel,std,3);
	c->actions->list_cap(v->fd);
	free_capture_device(v);
	l = get_control_list(v);
	printf("Listing available controls (%d)\n", l->count);
	for(i=0;i<l->count; i++){
		qc = l->controls[i].v4l2_ctrl;
		printf("Control: id: 0x%x - name: %s - min: %d -max: %d - step: %d - type: %d(%s) - flags: %d (%s%s%s%s%s%s)\n",
				qc->id, (char *) &qc->name, qc->minimum, qc->maximum, qc->step, qc->type,
				qc->type == V4L2_CTRL_TYPE_INTEGER ? "Integer" :
				qc->type == V4L2_CTRL_TYPE_BOOLEAN ? "Boolean" :
				qc->type == V4L2_CTRL_TYPE_MENU ? "Menu" :
				qc->type == V4L2_CTRL_TYPE_BUTTON ? "Button" :
				qc->type == V4L2_CTRL_TYPE_INTEGER64 ? "Integer64" :
				qc->type == V4L2_CTRL_TYPE_CTRL_CLASS ? "Class" :
				qc->type == V4L2_CTRL_TYPE_STRING ? "String" :
				qc->type == V4L2_CTRL_TYPE_BITMASK ? "Bitmask" : "",
				qc->flags,
				qc->flags & V4L2_CTRL_FLAG_DISABLED ? "Disabled " : "",
				qc->flags & V4L2_CTRL_FLAG_GRABBED ? "Grabbed " : "",
				qc->flags & V4L2_CTRL_FLAG_READ_ONLY ? "ReadOnly " : "",
				qc->flags & V4L2_CTRL_FLAG_UPDATE ? "Update " : "",
				qc->flags & V4L2_CTRL_FLAG_INACTIVE ? "Inactive " : "",
				qc->flags & V4L2_CTRL_FLAG_SLIDER ? "slider " : "",
				qc->flags & V4L2_CTRL_FLAG_WRITE_ONLY ? "Write only" : "");

		if(l->controls[i].count_menu!=0){
			printf("Menu items (%d) %s\n", l->controls[i].count_menu, l->controls[i].v4l2_ctrl->step==1?"contiguous":"non-contiguous");
			for(j=0; j<l->controls[i].count_menu; j++)
				printf("\tMenu item: %s - %d\n", l->controls[i].v4l2_menu[j].name, l->controls[i].v4l2_menu[j].index);

		}
	}

	get_device_info(v);
	print_device_info(v);
	release_device_info(v);

	release_control_list(v);

	close_device(v);

	return 0;
}
Exemplo n.º 4
0
int main(int argc, char** argv) {
	struct capture_device *c;
	struct video_device *v;
	void *d;
	int size, std=0, channel=0, width=0, height=0, nb_frames = 0, fmt=-1;

	if(argc!=7 && argc!=8) {
		printf("Usage: %s <video_device_file> <number_of_frames> <standard> "
				"<input> <width> <height> [ format ]\n", argv[0]);
		printf("This program captures a number of frames from a video device "
				"and saves them in separate files");
		printf("The following arguments are required in this order:\n");
		printf("The video device file to be tested.\n");
		printf("The number of frames to be captured\n");
		printf("The video standard and input number.\n");
		printf("Video standards: webcam:0 - PAL:1 - SECAM:2 - NTSC:3\n");
		printf("The capture resolution (width and height)\n");
		printf("The last argument is optional and is an image format index. "
				"To see what formats are supported by a video device, run "
				"'./list-caps /dev/videoXX' and check the "
				"'Printing device info' section at the bottom.\n");
		printf("Arguments must be in the specified order !!!\n");
		return -1;
	}

	nb_frames = atoi(argv[2]);
	printf("This program will capture %d frame(s) from %s\n", nb_frames, argv[1]);

	std = atoi(argv[3]);
	channel = atoi(argv[4]);
	width = atoi(argv[5]);
	height = atoi(argv[6]);
	printf("Trying %dx%d standard %d, channel %d",width,height,std, channel);

	if(argc==8){
		fmt = atoi(argv[7]);
		printf(", image format %s (%d)",libvideo_palettes[fmt].name, fmt);
	}

	printf("\nMake sure your video source is connected, and press <Enter>, or Ctrl-C to abort now.");
	getchar();

	v = open_device(argv[1]);
	if(v==NULL){
		printf("Error opening device %s", argv[1]);
		return -1;
	}
	c = init_capture_device(v, width, height ,channel, std,2);

	if(c==NULL) {
		printf("Error initialising device.\n");
		close_device(v);
		return -1;
	}

	if(fmt!=-1){
		if((*c->actions->set_cap_param)(v, &fmt, 1)){
			free_capture_device(v);
			close_device(v);
			printf("Cant set capture parameters\n");
			return -1;
		}
	}else {
		if((*c->actions->set_cap_param)(v, NULL, 0)){
			free_capture_device(v);
			close_device(v);
			printf("Cant set capture parameters\n");
			return -1;
		}
	}


	printf("Capturing from %s at %dx%d.\n", argv[1], c->width,c->height);
	width = c->width;
	height = c->height;
	printf("Image format %s, size: %d\n", libvideo_palettes[c->palette].name, c->imagesize);

	if((*c->actions->init_capture)(v)<0){
		free_capture_device(v);
		close_device(v);
		printf("Cant initialise capture ");
		return -1;
	}

	if((*c->actions->start_capture)(v)<0){
		(*c->actions->free_capture)(v);
		free_capture_device(v);
		close_device(v);
		printf("Cant start capture");
		return -1;
	}

	while(nb_frames-->0){
		//get frame from v4l2
		if((d = (*c->actions->dequeue_buffer)(v, &size)) != NULL) {
			write_frame(d, size);
			//Put frame
			(*c->actions->enqueue_buffer)(v);
		} else {
			printf("Cant get buffer ");
			break;
		}
	}

	if((*c->actions->stop_capture)(v)<0)
		fprintf(stderr, "Error stopping capture\n");

	(*c->actions->free_capture)(v);
	free_capture_device(v);
	close_device(v);

	return 0;
}
Exemplo n.º 5
0
int main(int argc, char** argv) {
	struct capture_device *c;
	struct video_device *v;
	void *d;
	struct timeval start, now;
	int size, count=0, std=0, channel=0, width=0, height=0, cap_length = 0,
		fmt=-1;

	if(argc!=7 && argc!=8) {
		printf("Usage: %s <video_device_file> <single_frame> <standard> <input>"
				" <width> <height> [ format ]\n", argv[0]);
		printf("This program requires the following arguments in this order:\n");
		printf("The video device file to be tested.\n");
		printf("Single frame capture (1), or 10 second capture (0)\n");
		printf("The video standard and input number.\n");
		printf("Video standards: webcam:0 - PAL:1 - SECAM:2 - NTSC:3\n");
		printf("The capture resolution (width and height)\n");
		printf("The last argument is optional and is an image format index. "
				"To see what formats are supported by a video device, run "
				"'./list-caps /dev/videoXX' and check the "
				"'Printing device info' section at the bottom.\n");
		printf("Arguments must be in the specified order !!!\n");
		return -1;
	}

	if(atoi(argv[2])>=1){
			cap_length = 0;
			printf("This program will capture a single frame from %s\n",
					argv[1]);
	} else {
		cap_length = CAPTURE_LENGTH;
		printf("This program will capture frames from %s for %d seconds\n",
				argv[1], cap_length);
	}

	std = atoi(argv[3]);
	channel = atoi(argv[4]);
	width = atoi(argv[5]);
	height = atoi(argv[6]);
	printf("Trying %dx%d standard %d, channel %d\n",width,height,std, channel);

	if(argc==8){
		fmt = atoi(argv[7]);
		printf("Trying image format %s (%d)\n",libvideo_palettes[fmt].name, fmt);
	}

	printf("Make sure your video source is connected, and press <Enter>, or "
			"Ctrl-C to abort now.");

	getchar();

	v = open_device(argv[1]);
	if(v==NULL){
		printf("Error opening device %s", argv[1]);
		return -1;
	}
	c = init_capture_device(v, width, height ,channel, std,2);

	if(c==NULL) {
		printf("Error initialising device.\n");
		close_device(v);
		return -1;
	}

	if(fmt!=-1){
		if((*c->actions->set_cap_param)(v, &fmt, 1)){
			free_capture_device(v);
			close_device(v);
			printf("Cant set capture parameters\n");
			return -1;
		}
	}else {
		if((*c->actions->set_cap_param)(v, NULL, 0)){
			free_capture_device(v);
			close_device(v);
			printf("Cant set capture parameters\n");
			return -1;
		}
	}


	printf("Capturing from %s at %dx%d.\n", argv[1], c->width,c->height);
	width = c->width;
	height = c->height;
	printf("Image format %s, size: %d\n", libvideo_palettes[c->palette].name, c->imagesize);

	if((*c->actions->init_capture)(v)<0){
		free_capture_device(v);
		close_device(v);
		printf("Cant initialise capture ");
		return -1;
	}
//
//	if((*c->actions->set_frame_interval)(v, 1, 15)<0){
//		printf("Cant set the frame interval");
//		fflush(stdout);
//	}

	if((*c->actions->start_capture)(v)<0){
		(*c->actions->free_capture)(v);
		free_capture_device(v);
		close_device(v);
		printf("Cant start capture");
		return -1;
	}

	gettimeofday(&start, NULL);
	gettimeofday(&now, NULL);
	while(now.tv_sec<=start.tv_sec+cap_length) {

		//get frame from v4l2
		if((d = (*c->actions->dequeue_buffer)(v, &size, NULL, NULL)) != NULL) {
			//uncomment the following line to output raw captured frame
			//to a file
			//write_frame(d, size);
			count++;
			//Put frame
			(*c->actions->enqueue_buffer)(v);
		} else {
			printf("Cant get buffer ");
			break;
		}
		gettimeofday(&now, NULL);
		if(cap_length==0)
			break;
	}
	printf("fps: %.1f\n", (count/((now.tv_sec - start.tv_sec) + ((float) (now.tv_usec - start.tv_usec)/1000000))));

	if((*c->actions->stop_capture)(v)<0)
		fprintf(stderr, "Error stopping capture\n");

	(*c->actions->free_capture)(v);
	free_capture_device(v);
	close_device(v);

	return 0;
}