home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Utilities / macam.0.8.4.dmg / macam sources / driver_core / MyCameraDriver.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-11-16  |  13.5 KB  |  300 lines

  1. /*
  2.     macam - webcam app and QuickTime driver component
  3.     Copyright (C) 2002 Matthias Krauss (macam@matthias-krauss.de)
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  $Id: MyCameraDriver.h,v 1.10 2005/10/18 17:50:24 hxr Exp $
  19. */
  20.  
  21. #import <Cocoa/Cocoa.h>
  22. #include <QuickTime/QuickTime.h>
  23. #include <IOKit/IOKitLib.h>
  24. #include <IOKit/IOCFPlugIn.h>
  25. #include <IOKit/usb/IOUSBLib.h>
  26. #include "GlobalDefs.h"
  27. #import "MyCameraInfo.h"
  28.  
  29. //CameraEvents are Events caused by the camera and propagated to the driver/client in some way. Extend if needed but please coordinate...
  30. typedef enum CameraEvent {
  31.     CameraEventSnapshotButtonDown,
  32.     CameraEventSnapshotButtonUp,
  33. } CameraEvent;
  34.  
  35. //CameraFeatures are custom feeatures of the camera
  36.  
  37. typedef enum CameraFeature {
  38.     CameraFeatureInspectorClassName    //A NSString containing the name of a MyCameraInspector subclass. Read-only.
  39. } CameraFeature;
  40.  
  41.  
  42. @interface MyCameraDriver : NSObject {
  43. //General stuff
  44.     id delegate;            //The delegate to notify [imageReady], [grabFinished] and [cameraHasShutDown].
  45.     id central;                //The central singleton to indicate [cameraHasShutDown].
  46.     
  47. //usb connection camera interfaces
  48.     IOUSBDeviceInterface** dev;        //An interface to the device
  49.     IOUSBInterfaceInterface** intf;     //An interface to the interface
  50.     
  51. //Camera settings. 
  52.     float brightness;
  53.     float contrast;
  54.     float saturation;
  55.     float gamma;
  56.     float sharpness;
  57.     float shutter;
  58.     float gain;
  59.     BOOL autoGain;
  60.     BOOL hFlip;
  61.     CameraResolution resolution;
  62.     WhiteBalanceMode whiteBalanceMode;
  63.     BOOL blackWhiteMode;    // is color or Black and White (greyscale)
  64.     BOOL LEDon;            // is the LED on or off (Philips cameras)
  65.     short fps;
  66.     short compression;            //0 = uncompressed, higher means more compressed
  67.  
  68.     //Driver states. Sorry, this has changed - the old version was too sensible to racing conditions. Everything except atomic read access has to be mutexed with stateLock (there is an exception: drivers may unset shouldBeGrabbing from within their internal grabbing and decoding since it's for sure that isGrabbing is set in that situation)
  69.         
  70.     BOOL isStarted;        //If the driver has been started up
  71.     BOOL isGrabbing;        //If the driver is in grabbing state
  72.     BOOL shouldBeGrabbing;    //If the grabbing thread should stop running
  73.     BOOL isShuttingDown;    //If the driver is shutting down and shouldn't accept new grabbing requests
  74.     BOOL isShutDown;        //If the driver has already been down
  75.     BOOL isUSBOK;        //If USB calls to intf and dev are ok. Unset without lock to be as fast as possible
  76.     NSLock* stateLock;        //The Lock to mutex all of this stuff
  77.     
  78. /* Stuff for merging notifications. Init these if you want to use the merging notification forwarders and the client wants notifications on the main thread. This is a good candidate for refacturing... */
  79.     
  80.     BOOL doNotificationsOnMainThread;    //If the client wants main thread calls or accepts other thread notifications
  81.     NSRunLoop* mainThreadRunLoop;
  82.     NSConnection* mainThreadConnection;
  83.     NSConnection* decodingThreadConnection;
  84.  
  85. /*
  86.  
  87. Image buffers. There are two sets: lastIamgeBuffer and nextImageBuffer. The client writes the next buffer to fill into nextImageBuffer via [setImageBuffer]. This also sets nextImageBufferSet to true, indicating the driver that image data may be written into it. The driver then writes the next available image into this buffer, copies the properties into lastImageBuffer and unsets nextImageBuffer. The read functions for the client may then read out lastImageBuffer. There is a lock to manage the access to these variables, imageBufferLock. The get functions don't use the lock since they don't change anything. They are only guaranteed to be valid during the [imageReady] notification. Setting nextImageBuffer is locked during the whole procedure of decoding the image. 
  88.  
  89. */
  90.         
  91.     unsigned char*     lastImageBuffer;
  92.     short         lastImageBufferBPP;
  93.     long         lastImageBufferRowBytes;
  94.     unsigned char*     nextImageBuffer;
  95.     short         nextImageBufferBPP;
  96.     long         nextImageBufferRowBytes;
  97.     BOOL         nextImageBufferSet;
  98.     NSLock*         imageBufferLock;
  99.     MyCameraInfo*   cameraInfo;
  100. }
  101.  
  102. //Get info about the camera specifics - simple mechanism
  103. + (unsigned short) cameraUsbProductID;
  104. + (unsigned short) cameraUsbVendorID;
  105. + (NSString*) cameraName;
  106.  
  107. //Get info - new mechanism. Overload this one if you have more than one idVendor/idProduct pair
  108.  
  109. + (NSArray*) cameraUsbDescriptions;
  110. //Should return an array of dictionaries with keys "idVendor" (NSNumber), "idProduct" (NSNumber) and "name" (NSString). The default implementation creates an array with one entry with values of the above methods.
  111.  
  112. // get/set camera info
  113. - (MyCameraInfo*) getCameraInfo;
  114. - (void) setCameraInfo:(MyCameraInfo *)info;
  115.  
  116. //Start/stop
  117. - (id) initWithCentral:(id)c;
  118. - (CameraError) startupWithUsbLocationId:(UInt32)usbLocationId;
  119. - (void) shutdown; // shuts the driver down or initiates this procedure. You will receive a [cameraHasShutDown] message.
  120. - (void) stopUsingUSB; //Makes sure no further USB calls to intf and dev are sent.
  121. - (void) dealloc;
  122.  
  123. //delegate management
  124. - (id) delegate;
  125. - (void) setDelegate:(id)d;
  126. - (void) enableNotifyOnMainThread;    //Has to be enabled before [startupWithUsbLocationId]! Cannot be unset - anymore!
  127. - (void) setCentral:(id)c;        //Don't use unless you know what you're doing!
  128. - (id) central;                //Don't use unless you know what you're doing!
  129.  
  130. //Camera introspection
  131. - (BOOL) realCamera;    //Returns if the camera is a real image grabber or a dummy
  132. - (BOOL) hasSpecificName; // Returns is the camera has a more specific name (derived from USB connection perhaps)
  133. - (NSString *) getSpecificName;
  134.  
  135. //Image / camera property get/set: All continuous data in the range [0 .. 1]. Their use should be quite obvious.
  136.  
  137. //Brightness
  138. - (BOOL) canSetBrightness;
  139. - (float) brightness;
  140. - (void) setBrightness:(float)v;
  141.  
  142. //Contrast
  143. - (BOOL) canSetContrast;
  144. - (float) contrast;
  145. - (void) setContrast:(float)v;
  146.  
  147. //Saturation - colorfulness
  148. - (BOOL) canSetSaturation;
  149. - (float) saturation;
  150. - (void) setSaturation:(float)v;
  151.  
  152. //Gamma value - grey value
  153. - (BOOL) canSetGamma;
  154. - (float) gamma;
  155. - (void) setGamma:(float)v;
  156.  
  157. //Sharpness value - contour enhancement
  158. - (BOOL) canSetSharpness;
  159. - (float) sharpness;
  160. - (void) setSharpness:(float)v;
  161.  
  162. //gain - electronic amplification
  163. - (BOOL) canSetGain;
  164. - (float) gain;
  165. - (void) setGain:(float)v;
  166.  
  167. //Shutter speed
  168. - (BOOL) canSetShutter;
  169. - (float) shutter;
  170. - (void) setShutter:(float)v;
  171.  
  172. //Automatic exposure - will affect shutter and gain
  173. - (BOOL) canSetAutoGain;
  174. - (BOOL) isAutoGain;
  175. - (void) setAutoGain:(BOOL)v;
  176.  
  177. //LED ON / OFF
  178. - (BOOL) canSetLed;
  179. - (BOOL) isLedOn;
  180. - (void) setLed:(BOOL)v;
  181.  
  182. //Horizontal flipping
  183. - (BOOL) canSetHFlip;        //Horizontal flipping
  184. - (BOOL) hFlip;
  185. - (void) setHFlip:(BOOL)v;
  186.  
  187. //Compression
  188. - (short) maxCompression;    //0 = no compression available
  189. - (short) compression;
  190. - (void) setCompression:(short)v;
  191.  
  192. //White Balance
  193. - (BOOL) canSetWhiteBalanceMode;
  194. - (BOOL) canSetWhiteBalanceModeTo:(WhiteBalanceMode)newMode;
  195. - (WhiteBalanceMode) defaultWhiteBalanceMode;
  196. - (WhiteBalanceMode) whiteBalanceMode;
  197. - (void) setWhiteBalanceMode:(WhiteBalanceMode)newMode;
  198.  
  199. //Black & White Mode
  200. - (BOOL) canBlackWhiteMode;
  201. - (BOOL) blackWhiteMode;
  202. - (void) setBlackWhiteMode:(BOOL)newMode;
  203.  
  204.  
  205. //Resolution and frame rate
  206. - (short) width;                        //Current image width
  207. - (short) height;                        //Current image height
  208. - (CameraResolution) resolution;                //Current image predefined format constant
  209. - (short) fps;                            //Current frames per second
  210. - (BOOL) supportsResolution:(CameraResolution)r fps:(short)fr;    //Does this combination work?
  211. - (void) setResolution:(CameraResolution)r fps:(short)fr;    //Set a resolution and frame rate
  212.  
  213. //Resolution and fps negotiation - the default implementation will use [supportsResolution:fps:] to find something.
  214. //Override these if you want to.
  215. - (CameraResolution) findResolutionForWidth:(short)width height:(short) height;    //returns a (hopefully) good native resolution
  216. - (short) findFrameRateForResolution:(CameraResolution)res;    //returns fps or <=0 if resolution not supported
  217. - (CameraResolution) defaultResolutionAndRate:(short*)fps;    //Override this to set the startup resolution
  218.  
  219. //Grabbing
  220. - (BOOL) startGrabbing;                    //start async grabbing. Returns if the camera is grabbing
  221. - (BOOL) stopGrabbing;                    //Stop async grabbing. Returns if the camera is grabbing
  222. - (void) setImageBuffer:(unsigned char*)buffer bpp:(short)bpp rowBytes:(long)rb;    //Set next image buffer to fill
  223. - (BOOL) isGrabbing;                    // Returns if the camera is grabbing
  224.  
  225. //Grabbing internal
  226. - (void) decodingThreadWrapper:(id)data;        //Don't subclass this...
  227. - (CameraError) decodingThread;                //Subclass this!
  228.  
  229. //Grabbing get info
  230. - (unsigned char*) imageBuffer;                //last filled image buffer
  231. - (short) imageBufferBPP;                //last BYTES per pixel
  232. - (long) imageBufferRowBytes;                //last bytes per image row
  233.  
  234. //DSC (Digital Still Camera) management - for cameras that can store media / also operate USB-unplugged
  235. - (BOOL) canStoreMedia;                    //If the device supports DSC or similar functions
  236. - (long) numberOfStoredMediaObjects;            //How many images are currently on the camera?
  237. - (NSDictionary*) getStoredMediaObject:(long)idx;    //downloads a media object
  238. //required keys: "type" and "data". Currently handled combinations:
  239. //type        data
  240. //"bitmap"    NSBitmapImageRep object
  241. //"jpeg"    NSData with JPEG (JFIF) file contents
  242.  
  243. - (BOOL) canGetStoredMediaObjectInfo;    //does the camera support [getStoredMediaObjectInfo:]?
  244. - (NSDictionary*) getStoredMediaObjectInfo:(long)idx;    //gets a media object info
  245. //required fields: type (currently "bitmap","jpeg")
  246. //required fields for type="bitmap" or "jpeg": "width", "height", recommended: "size"
  247.  
  248. - (BOOL) canDeleteAll;            //Does the camera support [deleteAll]?
  249. - (CameraError) deleteAll;        //Clears the camera media memory
  250.  
  251. - (BOOL) canDeleteOne;            //Does the camera support [deleteOne:]?
  252. - (CameraError) deleteOne:(long)idx;    //Clears one camera media object
  253.  
  254. - (BOOL) canDeleteLast;            //Does the camera support [deleteLast]?
  255. - (CameraError) deleteLast;        //Clears the last camera media object
  256.  
  257. - (BOOL) canCaptureOne;            //Does the camera support [CaptureOne]?
  258. - (CameraError) captureOne;        //Captures one image (or whatever - camera's current setting)
  259.  
  260.  
  261. //Camera Custom features
  262. - (BOOL) supportsCameraFeature:(CameraFeature)feature;
  263. - (id) valueOfCameraFeature:(CameraFeature)feature;
  264. - (void) setValue:(id)val ofCameraFeature:(CameraFeature)feature;
  265.  
  266. /*Note that "notifications" here are not exactly Cocoa notifications. They are delegate methods (the delegate concept matches a bit better to the Cocoa counterpart). But they also notify... */
  267.  
  268. //Merged Notification forwarders - should be used for notifications from decodingThread
  269. - (void) mergeImageReady;
  270. - (void) mergeGrabFinishedWithError:(CameraError)err;
  271. - (void) mergeCameraHasShutDown;
  272. //There's no mergeCameraEventHappened because most likely you won't call it from decodingThread. Merge yourself.
  273.  
  274. //Notification forwarders - should not be called from outside but may be overridden by subclasses
  275. - (void) imageReady:(id)sender;                //sends "notification" to "delegate"
  276. - (void) grabFinished:(id)sender withError:(CameraError)err;    //sends "notification" to "delegate"
  277. - (void) cameraHasShutDown:(id)sender;            //sends "notification" to "delegate"
  278. - (void) cameraEventHappened:(id)sender event:(CameraEvent)evt;    //sends "notification" to "delegate"    
  279.  
  280. //USB tool functions - should be used internally only
  281. - (BOOL) usbCmdWithBRequestType:(UInt8)bReqType bRequest:(UInt8)bReq wValue:(UInt16)wVal wIndex:(UInt16)wIdx buf:(void*)buf len:(short)len;//Sends a generic command
  282.  
  283. - (BOOL) usbReadCmdWithBRequest:(short)bReq wValue:(short)wVal wIndex:(short)wIdx buf:(void*)buf len:(short)len;//Sends a IN|VENDOR|DEVICE command
  284. - (BOOL) usbReadVICmdWithBRequest:(short)bReq wValue:(short)wVal wIndex:(short)wIdx buf:(void*)buf len:(short)len;//Sends a IN|VENDOR|INTERFACE command
  285. - (BOOL) usbWriteCmdWithBRequest:(short)bReq wValue:(short)wVal wIndex:(short)wIdx buf:(void*)buf len:(short)len;//Sends a OUT|VENDOR|DEVICE command
  286. - (BOOL) usbWriteVICmdWithBRequest:(short)bReq wValue:(short)wVal wIndex:(short)wIdx buf:(void*)buf len:(short)len;//Sends a OUT|VENDOR|INTERFACE command 
  287.  
  288. - (BOOL) usbSetAltInterfaceTo:(short)alt testPipe:(short)pipe;    //Sets the alt interface and optionally tests if a pipe exists
  289. - (CameraError) usbConnectToCam:(UInt32)usbLocationId configIdx:(short)configIdx;
  290.     //Standard open dev, reset device, set config (if>=0), open intf 
  291. - (void) usbCloseConnection;                //Close and release intf and dev
  292. - (BOOL) usbGetSoon:(UInt64*)to;            //Get a bus frame number in the near future
  293.  
  294. //Other tool functions - may also be used from outside
  295. - (BOOL) makeErrorImage:(CameraError) err;        //Draws and sends an error image. Returns if the image was actually sent
  296. - (BOOL) makeMessageImage:(char*)msg;            //Draws and sends a message image. Returns if the image was actually sent
  297. - (BOOL) makeOKImage;                    //Draws and sends a test pattern image. Returns if the image was actually sent
  298.  
  299. @end
  300.