
#ifndef	__Webcam__
#define	__Webcam__

#include	"script_squirrel/cobject/squirrel_bindings_utils.h"
#include	"script_squirrel/legacy/squirrel_binding.h"
#include	"script_squirrel/squirrel_vm.h"
#include "math/vector.h"
#include "core/render_data.h"
#include "picture/pict.h"

#include <windows.h>
#include <vector>
#include "thread/thread.h"
#include "thread/mutex.h"
#include "platform.h"
#include <string>

namespace cv{
	class VideoCapture;
	class VideoWriter;
	class Mat;
}

using namespace GS::Threading;

struct	ProcessWebcamThread : public Thread
{
	cv::VideoCapture *InputVideo;
	cv::VideoWriter *outputVideo;

	bool record_frame;
	bool not_finish;
	bool pause;

	Mutex mutex_record_frame;
	Mutex mutex_not_finish;
	Mutex mutex_pause;

	float current_frame_clock;

	int videoIndex;

	cv::Mat *im;
	std::string outputVideoName;

	void		Execute();
	ProcessWebcamThread(cv::VideoCapture *_InputVideo, cv::VideoWriter *_outputVideo, int width, int height, std::string _outputVideoName, int _videoIndex);
};

/**
*/
class Webcam
{
public:
	Webcam();
	~Webcam();

	bool Open(int width_, int height_);
	void Reset(bool _reset_video);
	void ResetVideo();
	uchar* SetFrame(float clock, GS::String record_path, int record_index = 0);
	void setPause(bool pause);
	void RecordFrame(float _clock);
	bool IsThreadRunning();
	void Close();

private:
	cv::VideoCapture *InputVideo;
	cv::VideoWriter *outputVideo;

	int index_save_video;
	int index_replay_video;
	std::vector<float> video_times;
	cv::VideoCapture *ReplayInputVideo;
	cv::Mat* ReplayIm;
	cv::Mat* ReplayImframe;

	int width, height;

	ProcessWebcamThread *thread_process;
};

#endif	// __Webcam__
