xref: /haiku/src/add-ons/media/media-add-ons/usb_webcam/CamDevice.h (revision a2db5acb28c0635849a76467faac1e94f6c55659)
1 #ifndef _CAM_DEVICE_H
2 #define _CAM_DEVICE_H
3 
4 
5 #include <OS.h>
6 #include <image.h>
7 #ifdef __HAIKU__
8 #	include <USB3.h>
9 #	include <USBKit.h>
10 #else
11 #	include <USB.h>
12 #	include <usb/USBKit.h>
13 #endif
14 #include <Locker.h>
15 #include <MediaAddOn.h>
16 #include <String.h>
17 #include <Rect.h>
18 
19 class BBitmap;
20 class BBuffer;
21 class BDataIO;
22 class CamRoster;
23 class CamDeviceAddon;
24 class CamSensor;
25 class CamDeframer;
26 class WebCamMediaAddOn;
27 
28 
29 typedef struct {
30 	usb_support_descriptor desc;
31 	const char *vendor;
32 	const char *product;
33 } usb_named_support_descriptor;
34 
35 // This class represents each webcam
36 class CamDevice {
37 	public:
38 						CamDevice(CamDeviceAddon &_addon, BUSBDevice* _device);
39 	virtual				~CamDevice();
40 
41 	virtual status_t	InitCheck();
42 		bool			Matches(BUSBDevice* _device);
43 		BUSBDevice*		GetDevice();
44 	virtual void		Unplugged(); // called before the BUSBDevice deletion
45 	virtual bool		IsPlugged(); // asserts on-line hardware
46 
47 	virtual const char*	BrandName();
48 	virtual const char*	ModelName();
49 	const flavor_info*	FlavorInfo() const { return &fFlavorInfo; };
50 	virtual bool		SupportsBulk();
51 	virtual bool		SupportsIsochronous();
52 	virtual status_t	StartTransfer();
53 	virtual status_t	StopTransfer();
54 	virtual bool		TransferEnabled() const { return fTransferEnabled; };
55 
56 	virtual status_t	SetVideoFrame(BRect rect);
57 	virtual BRect		VideoFrame() const { return fVideoFrame; };
58 	virtual status_t	SetScale(float scale);
59 	virtual status_t	SetVideoParams(float brightness, float contrast, float hue, float red, float green, float blue);
60 
61 	// for use by deframer
62 	virtual size_t		MinRawFrameSize();
63 	virtual size_t		MaxRawFrameSize();
64 	virtual bool		ValidateStartOfFrameTag(const uint8 *tag, size_t taglen);
65 	virtual bool		ValidateEndOfFrameTag(const uint8 *tag, size_t taglen, size_t datalen);
66 
67 	// several ways to get raw frames
68 	virtual status_t	WaitFrame(bigtime_t timeout);
69 	virtual status_t	GetFrameBitmap(BBitmap **bm);
70 	virtual status_t	FillFrameBuffer(BBuffer *buffer);
71 
72 	// locking
73 	bool				Lock();
74 	void				Unlock();
75 	BLocker*			Locker() { return &fLocker; };
76 
77 	// sensor chip handling
78 	CamSensor*			Sensor() const { return fSensor; };
79 
80 	// generic register-like access
81 	virtual ssize_t		WriteReg(uint16 address, uint8 *data, size_t count=1);
82 	virtual ssize_t		WriteReg8(uint16 address, uint8 data);
83 	virtual ssize_t		WriteReg16(uint16 address, uint16 data);
84 	virtual ssize_t		ReadReg(uint16 address, uint8 *data, size_t count=1, bool cached=false);
85 
86 	// I2C-like access
87 	//virtual status_t	GetStatusIIC();
88 	//virtual status_t	WaitReadyIIC();
89 	virtual ssize_t		WriteIIC(uint8 address, uint8 *data, size_t count);
90 	virtual ssize_t		WriteIIC8(uint8 address, uint8 data);
91 	virtual ssize_t		ReadIIC(uint8 address, uint8 *data);
92 
93 
94 	void				SetDataInput(BDataIO *input);
95 	virtual status_t	DataPumpThread();
96 	static int32		_DataPumpThread(void *_this);
97 
98 	virtual void		DumpRegs();
99 
100 	protected:
101 	CamSensor			*CreateSensor(const char *name);
102 		status_t		fInitStatus;
103 		flavor_info		fFlavorInfo;
104 		media_format	fMediaFormat;
105 		BString			fFlavorInfoNameStr;
106 		BString			fFlavorInfoInfoStr;
107 		CamSensor*		fSensor;
108 		CamDeframer*	fDeframer;
109 		BDataIO*		fDataInput; // where data from usb goes, likely fDeframer
110 		const BUSBEndpoint*	fBulkIn;
111 
112 	private:
113 		friend class CamDeviceAddon;
114 		CamDeviceAddon&	fCamDeviceAddon;
115 		BUSBDevice*		fDevice;
116 		int				fSupportedDeviceIndex;
117 		bool			fTransferEnabled;
118 		thread_id		fPumpThread;
119 		BLocker			fLocker;
120 		uint8			*fBuffer;
121 		size_t			fBufferLen;
122 		BRect			fVideoFrame;
123 		int fDumpFD;
124 };
125 
126 // the addon itself, that instanciate
127 
128 class CamDeviceAddon {
129 	public:
130 						CamDeviceAddon(WebCamMediaAddOn* webcam);
131 	virtual 			~CamDeviceAddon();
132 
133 	virtual const char*	BrandName();
134 	virtual status_t	Sniff(BUSBDevice *device);
135 	virtual CamDevice*	Instantiate(CamRoster &roster, BUSBDevice *from);
136 
137 	void				SetSupportedDevices(const usb_named_support_descriptor *devs);
138 	const usb_named_support_descriptor*	SupportedDevices() const
139 		{ return fSupportedDevices; };
140 	WebCamMediaAddOn*	WebCamAddOn() const { return fWebCamAddOn; };
141 
142 	private:
143 	WebCamMediaAddOn*	fWebCamAddOn;
144 	const usb_named_support_descriptor*	fSupportedDevices; // last is {{0,0,0,0,0}, NULL, NULL}
145 };
146 
147 // internal modules
148 #define B_WEBCAM_MKINTFUNC(modname) \
149 get_webcam_addon_##modname
150 
151 // external addons -- UNIMPLEMENTED
152 extern "C" status_t get_webcam_addon(WebCamMediaAddOn* webcam,
153 	CamDeviceAddon **addon);
154 #define B_WEBCAM_ADDON_INSTANTIATION_FUNC_NAME "get_webcam_addon"
155 
156 #endif // _CAM_DEVICE_H
157