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 AcceptVideoFrame(uint32 &width, uint32 &height); 65 virtual status_t SetVideoFrame(BRect rect); 66 virtual BRect VideoFrame() const { return fVideoFrame; }; 67 virtual status_t SetScale(float scale); 68 virtual status_t SetVideoParams(float brightness, float contrast, float hue, float red, float green, float blue); 69 70 virtual void AddParameters(BParameterGroup *group, int32 &index); 71 virtual status_t GetParameterValue(int32 id, bigtime_t *last_change, void *value, size_t *size); 72 virtual status_t SetParameterValue(int32 id, bigtime_t when, const void *value, size_t size); 73 74 75 // for use by deframer 76 virtual size_t MinRawFrameSize(); 77 virtual size_t MaxRawFrameSize(); 78 virtual bool ValidateStartOfFrameTag(const uint8 *tag, size_t taglen); 79 virtual bool ValidateEndOfFrameTag(const uint8 *tag, size_t taglen, size_t datalen); 80 81 // several ways to get raw frames 82 virtual status_t WaitFrame(bigtime_t timeout); 83 virtual status_t GetFrameBitmap(BBitmap **bm, bigtime_t *stamp=NULL); 84 virtual status_t FillFrameBuffer(BBuffer *buffer, bigtime_t *stamp=NULL); 85 86 // locking 87 bool Lock(); 88 void Unlock(); 89 BLocker* Locker() { return &fLocker; }; 90 91 // sensor chip handling 92 CamSensor* Sensor() const { return fSensor; }; 93 94 virtual status_t PowerOnSensor(bool on); 95 96 // generic register-like access 97 virtual ssize_t WriteReg(uint16 address, uint8 *data, size_t count=1); 98 virtual ssize_t WriteReg8(uint16 address, uint8 data); 99 virtual ssize_t WriteReg16(uint16 address, uint16 data); 100 virtual ssize_t ReadReg(uint16 address, uint8 *data, size_t count=1, bool cached=false); 101 102 ssize_t OrReg8(uint16 address, uint8 data, uint8 mask=0xff); 103 ssize_t AndReg8(uint16 address, uint8 data); 104 105 // I2C-like access 106 //virtual status_t GetStatusIIC(); 107 //virtual status_t WaitReadyIIC(); 108 virtual ssize_t WriteIIC(uint8 address, uint8 *data, size_t count); 109 virtual ssize_t WriteIIC8(uint8 address, uint8 data); 110 virtual ssize_t WriteIIC16(uint8 address, uint16 data); 111 virtual ssize_t ReadIIC(uint8 address, uint8 *data); 112 virtual ssize_t ReadIIC8(uint8 address, uint8 *data); 113 virtual ssize_t ReadIIC16(uint8 address, uint16 *data); 114 virtual status_t SetIICBitsMode(size_t bits=8); 115 116 117 void SetDataInput(BDataIO *input); 118 virtual status_t DataPumpThread(); 119 static int32 _DataPumpThread(void *_this); 120 121 virtual void DumpRegs(); 122 123 protected: 124 virtual status_t SendCommand(uint8 dir, uint8 request, uint16 value, 125 uint16 index, uint16 length, void* data); 126 virtual status_t ProbeSensor(); 127 CamSensor *CreateSensor(const char *name); 128 status_t fInitStatus; 129 flavor_info fFlavorInfo; 130 media_format fMediaFormat; 131 BString fFlavorInfoNameStr; 132 BString fFlavorInfoInfoStr; 133 CamSensor* fSensor; 134 CamDeframer* fDeframer; 135 BDataIO* fDataInput; // where data from usb goes, likely fDeframer 136 const BUSBEndpoint* fBulkIn; 137 const BUSBEndpoint* fIsoIn; 138 int32 fFirstParameterID; 139 bigtime_t fLastParameterChanges; 140 141 protected: 142 friend class CamDeviceAddon; 143 CamDeviceAddon& fCamDeviceAddon; 144 BUSBDevice* fDevice; 145 int fSupportedDeviceIndex; 146 bool fChipIsBigEndian; 147 bool fTransferEnabled; 148 thread_id fPumpThread; 149 BLocker fLocker; 150 uint8 *fBuffer; 151 size_t fBufferLen; 152 BRect fVideoFrame; 153 int fDumpFD; 154 }; 155 156 // the addon itself, that instanciate 157 158 class CamDeviceAddon { 159 public: 160 CamDeviceAddon(WebCamMediaAddOn* webcam); 161 virtual ~CamDeviceAddon(); 162 163 virtual const char* BrandName(); 164 virtual status_t Sniff(BUSBDevice *device); 165 virtual CamDevice* Instantiate(CamRoster &roster, BUSBDevice *from); 166 167 void SetSupportedDevices(const usb_webcam_support_descriptor *devs); 168 const usb_webcam_support_descriptor* SupportedDevices() const 169 { return fSupportedDevices; }; 170 WebCamMediaAddOn* WebCamAddOn() const { return fWebCamAddOn; }; 171 172 private: 173 WebCamMediaAddOn* fWebCamAddOn; 174 const usb_webcam_support_descriptor* fSupportedDevices; // last is {{0,0,0,0,0}, NULL, NULL, NULL } 175 }; 176 177 // internal modules 178 #define B_WEBCAM_MKINTFUNC(modname) \ 179 get_webcam_addon_##modname 180 181 // external addons -- UNIMPLEMENTED 182 extern "C" status_t get_webcam_addon(WebCamMediaAddOn* webcam, 183 CamDeviceAddon **addon); 184 #define B_WEBCAM_ADDON_INSTANTIATION_FUNC_NAME "get_webcam_addon" 185 186 #endif // _CAM_DEVICE_H 187