1 /* 2 * Copyright 2004-2008, François Revol, <revol@free.fr>. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _CAM_DEVICE_H 6 #define _CAM_DEVICE_H 7 8 9 #include <OS.h> 10 #include <image.h> 11 #ifdef __HAIKU__ 12 //#if 1 13 # include <USB3.h> 14 # include <USBKit.h> 15 # define SUPPORT_ISO 16 #else 17 # include <USB.h> 18 # include <usb/USBKit.h> 19 #endif 20 #include <Locker.h> 21 #include <MediaAddOn.h> 22 #include <String.h> 23 #include <Rect.h> 24 25 class BBitmap; 26 class BBuffer; 27 class BDataIO; 28 class BParameterGroup; 29 class CamRoster; 30 class CamDeviceAddon; 31 class CamSensor; 32 class CamDeframer; 33 class WebCamMediaAddOn; 34 35 36 typedef struct { 37 usb_support_descriptor desc; 38 const char *vendor; 39 const char *product; 40 const char *sensors; // possible sensors this cam uses (comma separated) 41 } usb_webcam_support_descriptor; 42 43 // This class represents each webcam 44 class CamDevice { 45 public: 46 CamDevice(CamDeviceAddon &_addon, BUSBDevice* _device); 47 virtual ~CamDevice(); 48 49 virtual status_t InitCheck(); 50 bool Matches(BUSBDevice* _device); 51 BUSBDevice* GetDevice(); 52 virtual void Unplugged(); // called before the BUSBDevice deletion 53 virtual bool IsPlugged(); // asserts on-line hardware 54 55 virtual const char* BrandName(); 56 virtual const char* ModelName(); 57 const flavor_info* FlavorInfo() const { return &fFlavorInfo; }; 58 virtual bool SupportsBulk(); 59 virtual bool SupportsIsochronous(); 60 virtual status_t StartTransfer(); 61 virtual status_t StopTransfer(); 62 virtual bool TransferEnabled() const { return fTransferEnabled; }; 63 64 virtual status_t SuggestVideoFrame(uint32 &width, uint32 &height); 65 virtual status_t AcceptVideoFrame(uint32 &width, uint32 &height); 66 virtual status_t SetVideoFrame(BRect rect); 67 virtual BRect VideoFrame() const { return fVideoFrame; }; 68 virtual status_t SetScale(float scale); 69 virtual status_t SetVideoParams(float brightness, float contrast, float hue, float red, float green, float blue); 70 71 virtual void AddParameters(BParameterGroup *group, int32 &index); 72 virtual status_t GetParameterValue(int32 id, bigtime_t *last_change, void *value, size_t *size); 73 virtual status_t SetParameterValue(int32 id, bigtime_t when, const void *value, size_t size); 74 75 76 // for use by deframer 77 virtual size_t MinRawFrameSize(); 78 virtual size_t MaxRawFrameSize(); 79 virtual bool ValidateStartOfFrameTag(const uint8 *tag, size_t taglen); 80 virtual bool ValidateEndOfFrameTag(const uint8 *tag, size_t taglen, size_t datalen); 81 82 // several ways to get raw frames 83 virtual status_t WaitFrame(bigtime_t timeout); 84 virtual status_t GetFrameBitmap(BBitmap **bm, bigtime_t *stamp=NULL); 85 virtual status_t FillFrameBuffer(BBuffer *buffer, bigtime_t *stamp=NULL); 86 87 // locking 88 bool Lock(); 89 void Unlock(); 90 BLocker* Locker() { return &fLocker; }; 91 92 // sensor chip handling 93 CamSensor* Sensor() const { return fSensor; }; 94 95 virtual status_t PowerOnSensor(bool on); 96 97 // generic register-like access 98 virtual ssize_t WriteReg(uint16 address, uint8 *data, size_t count=1); 99 virtual ssize_t WriteReg8(uint16 address, uint8 data); 100 virtual ssize_t WriteReg16(uint16 address, uint16 data); 101 virtual ssize_t ReadReg(uint16 address, uint8 *data, size_t count=1, bool cached=false); 102 103 ssize_t OrReg8(uint16 address, uint8 data, uint8 mask=0xff); 104 ssize_t AndReg8(uint16 address, uint8 data); 105 106 // I2C-like access 107 //virtual status_t GetStatusIIC(); 108 //virtual status_t WaitReadyIIC(); 109 virtual ssize_t WriteIIC(uint8 address, uint8 *data, size_t count); 110 virtual ssize_t WriteIIC8(uint8 address, uint8 data); 111 virtual ssize_t WriteIIC16(uint8 address, uint16 data); 112 virtual ssize_t ReadIIC(uint8 address, uint8 *data); 113 virtual ssize_t ReadIIC8(uint8 address, uint8 *data); 114 virtual ssize_t ReadIIC16(uint8 address, uint16 *data); 115 virtual status_t SetIICBitsMode(size_t bits=8); 116 117 118 void SetDataInput(BDataIO *input); 119 virtual status_t DataPumpThread(); 120 static int32 _DataPumpThread(void *_this); 121 122 virtual void DumpRegs(); 123 124 protected: 125 virtual status_t SendCommand(uint8 dir, uint8 request, uint16 value, 126 uint16 index, uint16 length, void* data); 127 virtual status_t ProbeSensor(); 128 CamSensor *CreateSensor(const char *name); 129 status_t fInitStatus; 130 flavor_info fFlavorInfo; 131 media_format fMediaFormat; 132 BString fFlavorInfoNameStr; 133 BString fFlavorInfoInfoStr; 134 CamSensor* fSensor; 135 CamDeframer* fDeframer; 136 BDataIO* fDataInput; // where data from usb goes, likely fDeframer 137 const BUSBEndpoint* fBulkIn; 138 const BUSBEndpoint* fIsoIn; 139 int32 fFirstParameterID; 140 bigtime_t fLastParameterChanges; 141 142 protected: 143 friend class CamDeviceAddon; 144 CamDeviceAddon& fCamDeviceAddon; 145 BUSBDevice* fDevice; 146 int fSupportedDeviceIndex; 147 bool fChipIsBigEndian; 148 bool fTransferEnabled; 149 thread_id fPumpThread; 150 BLocker fLocker; 151 uint8 *fBuffer; 152 size_t fBufferLen; 153 BRect fVideoFrame; 154 int fDumpFD; 155 }; 156 157 // the addon itself, that instanciate 158 159 class CamDeviceAddon { 160 public: 161 CamDeviceAddon(WebCamMediaAddOn* webcam); 162 virtual ~CamDeviceAddon(); 163 164 virtual const char* BrandName(); 165 virtual status_t Sniff(BUSBDevice *device); 166 virtual CamDevice* Instantiate(CamRoster &roster, BUSBDevice *from); 167 168 void SetSupportedDevices(const usb_webcam_support_descriptor *devs); 169 const usb_webcam_support_descriptor* SupportedDevices() const 170 { return fSupportedDevices; }; 171 WebCamMediaAddOn* WebCamAddOn() const { return fWebCamAddOn; }; 172 173 private: 174 WebCamMediaAddOn* fWebCamAddOn; 175 const usb_webcam_support_descriptor* fSupportedDevices; // last is {{0,0,0,0,0}, NULL, NULL, NULL } 176 }; 177 178 // internal modules 179 #define B_WEBCAM_MKINTFUNC(modname) \ 180 get_webcam_addon_##modname 181 182 // external addons -- UNIMPLEMENTED 183 extern "C" status_t get_webcam_addon(WebCamMediaAddOn* webcam, 184 CamDeviceAddon **addon); 185 #define B_WEBCAM_ADDON_INSTANTIATION_FUNC_NAME "get_webcam_addon" 186 187 #endif // _CAM_DEVICE_H 188