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