#ifndef __FRAMEGRABBER_H
#define __FRAMEGRABBER_H

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/videodev.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <opencv/cv.h>
#include "video.h"

struct frame_grabber {
	char *device;
	int handle;
	int shutdown;

	char *frame;
	char *frame_p;
	int frame_width;
	int frame_height;
	int readsize;
	int buffer_index;

	struct video_capability	capability;
	struct video_channel	channel;
	struct video_window		window;
	struct video_picture	picture;
	struct video_mbuf		memory_buffer;
	struct video_mmap		*mmaps;

	char *memory_map;
	char *image;

	// Pointer to an existing video_t structure
	// for encoding MPEG files directly from the
	// camera.  If equal to NULL then skip video.
	video_t *video_ptr;
};

typedef struct frame_grabber frame_grabber_t;

pthread_mutex_t image_mutex;
pthread_mutex_t shutdown_mutex;

frame_grabber_t * fg_init(char *device, unsigned int, unsigned int, video_t *);
int  fg_open(frame_grabber_t *);
void fg_close(frame_grabber_t *);
void fg_list_channels(frame_grabber_t *);
int  fg_select_channel(frame_grabber_t *, int channel);
int  fg_probe(frame_grabber_t *);
void fg_begin_capture_loop(frame_grabber_t *);
void fg_end_capture_loop(frame_grabber_t *);
void fg_capture_frame(frame_grabber_t *);
void fg_get_frame(frame_grabber_t *, IplImage *image, char *);
int  fg_get_frame_width(frame_grabber_t *);
int  fg_get_frame_height(frame_grabber_t *);
int  fg_work_around(frame_grabber_t *, int channel);

#endif

