00001 /* 00002 Copyright (c) 2010, The Barbarian Group 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00006 the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and 00009 the following disclaimer. 00010 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 00011 the following disclaimer in the documentation and/or other materials provided with the distribution. 00012 00013 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00014 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00015 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00016 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00017 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00018 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00019 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00020 POSSIBILITY OF SUCH DAMAGE. 00021 */ 00022 00023 #pragma once 00024 00025 #include "cinder/Cinder.h" 00026 #include "cinder/Surface.h" 00027 #include "cinder/Exception.h" 00028 00029 #if defined( CINDER_MAC ) 00030 # if defined( __OBJC__ ) 00031 @class CaptureCocoa; 00032 @class QTCaptureDevice; 00033 # else 00034 class CaptureCocoa; 00035 class QTCaptureDevice; 00036 # endif 00037 #elif defined( CINDER_MSW ) 00038 # include "msw/videoInput/videoInput.h" 00039 #endif 00040 00041 #include <map> 00042 00043 namespace cinder { 00044 00045 class Capture { 00046 public: 00047 class Device; 00048 00049 Capture() {} 00050 Capture( int32_t width, int32_t height, const Device &device = Device() ); 00051 ~Capture() {} 00052 00054 void start(); 00056 void stop(); 00058 bool isCapturing(); 00059 00061 bool checkNewFrame() const; 00062 00064 int32_t getWidth() const { return mObj->mWidth; } 00066 int32_t getHeight() const { return mObj->mHeight; } 00068 Vec2i getSize() const { return Vec2i( getWidth(), getHeight() ); } 00070 float getAspectRatio() const { return getWidth() / (float)getHeight(); } 00072 Area getBounds() const { return Area( 0, 0, getWidth(), getHeight() ); } 00073 00075 Surface8u getSurface() const; 00077 Device getDevice() const { return mObj->mDevice; } 00078 00080 static const std::vector<Device>& getDevices( bool forceRefresh = false ); 00082 static Device findDeviceByName( const std::string &name ); 00083 00085 class Device { 00086 public: 00088 const std::string& getName() const { return mName; } 00090 bool checkAvailable() const; 00092 bool isConnected() const; 00093 00095 #if defined( CINDER_MAC ) 00096 const std::string& getUniqueId() const { return mUniqueId; } 00097 #else 00098 int getUniqueId() const { return mUniqueId; } 00099 #endif 00100 00101 #if defined( CINDER_MAC ) 00102 Device() {} // this represents a null device 00103 #else 00104 Device() : mUniqueId( 0 ) {} 00105 #endif 00106 protected: 00107 #if defined( CINDER_MAC ) 00108 Device( QTCaptureDevice* device ); 00109 #else 00110 Device( const std::string &name, int uniqueId ) : mName( name ), mUniqueId( uniqueId ) {} 00111 #endif 00112 00113 std::string mName; 00114 #if defined( CINDER_MAC ) 00115 std::string mUniqueId; 00116 #else 00117 int mUniqueId; 00118 #endif 00119 00120 friend class Capture; 00121 }; 00122 00123 protected: 00124 struct Obj { 00125 Obj( int32_t width, int32_t height, const Capture::Device &device ); 00126 virtual ~Obj(); 00127 00128 #if defined( CINDER_MAC ) 00129 CaptureCocoa *mImpl; 00130 #elif defined( CINDER_MSW ) 00131 int mDeviceID; 00132 // this maintains a reference to the mgr so that we don't destroy it before 00133 // the last Capture is destroyed 00134 shared_ptr<class CaptureMgr> mMgrPtr; 00135 bool mIsCapturing; 00136 shared_ptr<class SurfaceCache> mSurfaceCache; 00137 #endif 00138 int32_t mWidth, mHeight; 00139 mutable Surface8u mCurrentFrame; 00140 Capture::Device mDevice; 00141 }; 00142 00143 shared_ptr<Obj> mObj; 00144 static bool sDevicesEnumerated; 00145 static std::vector<Capture::Device> sDevices; 00146 00147 public: 00149 00150 typedef shared_ptr<Obj> Capture::*unspecified_bool_type; 00151 operator unspecified_bool_type() { return ( mObj.get() == 0 ) ? 0 : &Capture::mObj; } 00152 void reset() { mObj.reset(); } 00154 }; 00155 00156 class CaptureExc : public Exception { 00157 }; 00158 00159 class CaptureExcInitFail : public CaptureExc { 00160 }; 00161 00162 class CaptureExcInvalidChannelOrder : public CaptureExc { 00163 }; 00164 00165 } //namespace cinder