Exemple #1
0
void camera_close()
{
	free(current_pic.buffer);
	free_buf_mmap();
	v4lconvert_destroy(lib);
	close(fd_cam);
}
Exemple #2
0
int camera_init(struct picture_t *out_info)
{
	fd_cam = open(CAM_NAME, O_RDWR);
	if(fd_cam < 0){
		perror("open camera " CAM_NAME);
		return 0;
	}
	if(!get_format())
		goto label_close;

	lib = v4lconvert_create(fd_cam);
	if(!lib) {
		perror("v4lconvert_create");
		goto label_close;
	}

	if(!buf_alloc_mmap())
		goto label_free;

	YUV420_size = current_pic.width*current_pic.height*3/2;
	if(!(current_pic.buffer = malloc(YUV420_size))){
		perror("malloc");
		goto label_free;
	}

	*out_info = current_pic;
	return 1;

label_free:
	free_buf_mmap();
	v4lconvert_destroy(lib);
label_close:
	close(fd_cam);
	return 0;
}
static void tc_v4l2_teardown_image_format(V4L2Source *vs)
{
    if (vs->v4l_convert) {
        v4lconvert_destroy(vs->v4l_convert);
        vs->v4l_convert = NULL;
    }
}
Exemple #4
0
/*}}}*/
static inline void video_shutdownio (opi_video_device_t *dev) /*{{{*/
{
	if (dev->convert != NULL)
		v4lconvert_destroy (dev->convert);

	if (dev->fd < 0) {
		/* nothing */
	} else if (dev->use_mmap < 0) {
		/* nothing */
	} else if (dev->use_mmap) {
		int i;
		if (dev->api == 2 && !dev->oneshot) {
			int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
			int r;
			while (((r = ioctl (dev->fd, VIDIOC_STREAMOFF, &type)) == -1) && (errno == EINTR));	/* retry */
		}
		for (i = 0; i < dev->n_buffers; ++i) {
			munmap (dev->buffers[i].addr, dev->buffers[i].size);
			dev->buffers[i].idx = -1;
			dev->buffers[i].addr = NULL;
			dev->buffers[i].size = 0;
		}
		dev->use_mmap = -1;
		dev->n_buffers = 0;
	} else {
		/* FIXME! */
	}
}
Exemple #5
0
int v4l2_close(int fd)
{
	int index, result;

	index = v4l2_get_index(fd);
	if (index == -1)
		return SYS_CLOSE(fd);

	/* Abuse stream_lock to stop 2 closes from racing and trying to free the
	   resources twice */
	pthread_mutex_lock(&devices[index].stream_lock);
	devices[index].open_count--;
	result = devices[index].open_count != 0;
	pthread_mutex_unlock(&devices[index].stream_lock);

	if (result)
		return 0;

	/* Free resources */
	v4l2_unmap_buffers(index);
	if (devices[index].convert_mmap_buf != MAP_FAILED) {
		if (v4l2_buffers_mapped(index))
			V4L2_LOG_WARN("v4l2 mmap buffers still mapped on close()\n");
		else
			SYS_MUNMAP(devices[index].convert_mmap_buf,
					devices[index].no_frames * V4L2_FRAME_BUF_SIZE);
		devices[index].convert_mmap_buf = MAP_FAILED;
	}
	v4lconvert_destroy(devices[index].convert);
	free(devices[index].readbuf);
	devices[index].readbuf = NULL;
	devices[index].readbuf_size = 0;

	/* Remove the fd from our list of managed fds before closing it, because as
	   soon as we've done the actual close the fd maybe returned by an open in
	   another thread and we don't want to intercept calls to this new fd. */
	devices[index].fd = -1;

	/* Since we've marked the fd as no longer used, and freed the resources,
	   redo the close in case it was interrupted */
	do {
		result = SYS_CLOSE(fd);
	} while (result == -1 && errno == EINTR);

	V4L2_LOG("close: %d\n", fd);

	return result;
}
Exemple #6
0
static void vidcap_v4l2_done(void *state)
{
        struct vidcap_v4l2_state *s = (struct vidcap_v4l2_state *) state;

        int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        if(ioctl(s->fd, VIDIOC_STREAMOFF, &type) != 0) {
                fprintf(stderr, "Stream stopping error.\n");
        };

        close(s->fd);

        if(s->conversion_needed) {
                v4lconvert_destroy(s->convert);
        }

        free(s);
}
Exemple #7
0
//=============================================================================
//
//	gv4l2_uninit_device(THIS)
//
//	Uninitialise the device and free all the associated memory
//
void gv4l2_uninit_device(CWEBCAM * _object)
{
	unsigned int i;

	GB.Free( POINTER(&THIS->frame) );

	v4lconvert_destroy(THIS->convert);
	
	if( !THIS->use_mmap) { 
		GB.Free ( POINTER(&THIS->buffers[0].start));
		GB.Free ( POINTER(&THIS->buffers));
		return;
	}
	for (i = 0; i < THIS->buffer_count; ++i )
		if(munmap(THIS->buffers[i].start,THIS->buffers[i].length)==-1) 
			gv4l2_debug("MUNMAP Error");

	GB.Free ( POINTER(&THIS->buffers));
}
Exemple #8
0
void capture()
{
	unsigned char *yuv420_buf;
	int yuv420_size = fmt.fmt.pix.width*fmt.fmt.pix.height*3/2;
	int src_size, i, j, k, nframe;

	struct v4lconvert_data *lib;

	buffersize = calc_size(fmt.fmt.pix.sizeimage);

	buf_alloc_mmap();

	lib = v4lconvert_create(fd_cam);
	if(!lib) {
		perror("v4lconvert_create");
		exit(1);
	}

	yuv420_buf = malloc(yuv420_size);
	if(!yuv420_buf){
		perror("malloc");
		exit(1);
	}

	if(ioctl(fd_cam, VIDIOC_STREAMON, &reqbuf.type) < 0) {
		perror("VIDIOC_STREAMON");
		exit(1);
	}

	dst_fmt = fmt;
	dst_fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;

	if(!v4lconvert_supported_dst_format(dst_fmt.fmt.pix.pixelformat)){
		puts("v4lconvert_supported_dst_format");
		exit(1);
	}

	for(errno = 0, nframe = 0; nframe < NUM_FRAME; nframe++) {
		struct v4l2_buffer cam_buf = {0};

		cam_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		cam_buf.memory = reqbuf.memory;

		if(ioctl(fd_cam, VIDIOC_DQBUF, &cam_buf) < 0) {
			perror("VIDIOC_DQBUF");
			exit(1);
		}

		printf("DQBUF: index=%d, seq=%d, time=%d.%06d\n", cam_buf.index, cam_buf.sequence, cam_buf.timestamp.tv_sec, cam_buf.timestamp.tv_usec);

		src_size = cam_buf.length;

		if(v4lconvert_convert(lib, &fmt, &dst_fmt, (void*)buf_pointer[cam_buf.index], src_size, yuv420_buf, yuv420_size) <= 0){
			perror("v4lconvert_convert");
			exit(1);
		}

		cam_buf.length = buffersize;
		if(ioctl(fd_cam, VIDIOC_QBUF, &cam_buf) < 0) {
			perror("VIDIOC_QBUF");
			exit(1);
		}

		write(fd_out, yuv420_buf, yuv420_size);
	}

	if(ioctl(fd_cam, VIDIOC_STREAMOFF, &reqbuf.type) < 0) {
		perror("VIDIOC_STREAMOFF");
		exit(1);
	}

	free(yuv420_buf);

	v4lconvert_destroy(lib);

	free_buf_mmap();
}
Exemple #9
0
static void init_device(int w, int h)
{
	struct v4lconvert_data *v4lconvert_data;
	struct v4l2_format src_fmt;	 /* raw source format */
	struct v4l2_capability cap;

	if (v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
		if (EINVAL == errno) {
			fprintf(stderr, "%s is no V4L2 device\n",
				dev_name);
			exit(EXIT_FAILURE);
		} else {
			errno_exit("VIDIOC_QUERYCAP");
		}
	}

	if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
		fprintf(stderr, "%s is no video capture device\n",
			dev_name);
		exit(EXIT_FAILURE);
	}

	/* libv4l emulates read() on those v4l2 devices that do not support
	it, so this print is just instructional, it should work regardless */
	printf("device capabilities\n\tread:\t%c\n\tstream:\t%c\n",
		(cap.capabilities & V4L2_CAP_READWRITE) ? 'Y' : 'N',
		(cap.capabilities & V4L2_CAP_STREAMING) ? 'Y' : 'N');

	/* set our requested format to V4L2_PIX_FMT_RGB24 */
	CLEAR(fmt);
	fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	fmt.fmt.pix.width = w;
	fmt.fmt.pix.height = h;
	fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
	fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;

	/* libv4l also converts mutiple supported formats to V4l2_PIX_FMT_BGR24 or 
	V4l2_PIX_FMT_YUV420, which means the following call should *always* 
	succeed 

	However, we use the libv4lconvert library to print debugging information
	to tell us if libv4l will be doing the conversion internally*/
	v4lconvert_data = v4lconvert_create(fd);
	if (v4lconvert_data == NULL)
		errno_exit("v4lconvert_create");
	if (v4lconvert_try_format(v4lconvert_data, &fmt, &src_fmt) != 0)
		errno_exit("v4lconvert_try_format");

	printf("\tpixfmt:\t%c%c%c%c (%dx%d)\n",
		src_fmt.fmt.pix.pixelformat & 0xff,
		(src_fmt.fmt.pix.pixelformat >> 8) & 0xff,
		(src_fmt.fmt.pix.pixelformat >> 16) & 0xff,
		(src_fmt.fmt.pix.pixelformat >> 24) & 0xff,
		src_fmt.fmt.pix.width, src_fmt.fmt.pix.height);

	printf("application\n\tconv:\t%c\n", 
		v4lconvert_needs_conversion(v4lconvert_data,
			&src_fmt,
			&fmt) ? 'Y' : 'N');

	v4lconvert_destroy(v4lconvert_data);

	/* Actually set the pixfmt so that libv4l uses its conversion magic */
	if (v4l2_ioctl(fd, VIDIOC_S_FMT, &fmt) < 0)
		errno_exit("VIDIOC_S_FMT");

	printf("\tpixfmt:\t%c%c%c%c (%dx%d)\n",
		fmt.fmt.pix.pixelformat & 0xff,
		(fmt.fmt.pix.pixelformat >> 8) & 0xff,
		(fmt.fmt.pix.pixelformat >> 16) & 0xff,
		(fmt.fmt.pix.pixelformat >> 24) & 0xff,
		fmt.fmt.pix.width, fmt.fmt.pix.height);

	switch (io) {
	case IO_METHOD_READ:
		printf("\tio:\tio\n");
		init_read(fmt.fmt.pix.sizeimage);
		break;
	case V4L2_MEMORY_MMAP:
		printf("\tio:\tmmap\n");
		init_mmap();
		break;
	case V4L2_MEMORY_USERPTR:
		printf("\tio:\tusrptr\n");
		init_userp(fmt.fmt.pix.sizeimage);
		break;
	}
}