Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Capture.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/Surface.h"
27 #include "cinder/Exception.h"
28 
29 #if defined( CINDER_MAC )
30  #if defined( __OBJC__ )
31  @class CaptureImplQtKit;
32  @class QTCaptureDevice;
33  #else
34  class CaptureImplQtKit;
35  class QTCaptureDevice;
36  #endif
37 #elif defined( CINDER_COCOA_TOUCH_SIMULATOR )
38  #if defined( __OBJC__ )
39  @class CaptureImplCocoaDummy;
40  #else
41  class CaptureImplCocoaDummy;
42  #endif
43 #elif defined( CINDER_COCOA_TOUCH_DEVICE )
44  #if defined( __OBJC__ )
45  @class CaptureImplAvFoundation;
46  #else
47  class CaptureImplAvFoundation;
48  #endif
49 #elif defined( CINDER_MSW )
50  namespace cinder {
51  class CaptureImplDirectShow;
52  }
53 #endif
54 
55 #include <map>
56 
57 namespace cinder {
58 
59 typedef std::shared_ptr<class Capture> CaptureRef;
60 
61 class Capture {
62  public:
63  class Device;
64  typedef std::shared_ptr<Device> DeviceRef;
65 
66  static CaptureRef create( int32_t width, int32_t height, const DeviceRef device = DeviceRef() ) { return CaptureRef( new Capture( width, height, device ) ); }
67 
68  Capture() {}
70  Capture( int32_t width, int32_t height, const DeviceRef device = DeviceRef() );
71  ~Capture() {}
72 
74  void start();
76  void stop();
78  bool isCapturing();
79 
81  bool checkNewFrame() const;
82 
84  int32_t getWidth() const;
86  int32_t getHeight() const;
88  Vec2i getSize() const { return Vec2i( getWidth(), getHeight() ); }
90  float getAspectRatio() const { return getWidth() / (float)getHeight(); }
92  Area getBounds() const { return Area( 0, 0, getWidth(), getHeight() ); }
93 
95  Surface8u getSurface() const;
97  const Capture::DeviceRef getDevice() const;
98 
100  static const std::vector<DeviceRef>& getDevices( bool forceRefresh = false );
102  static DeviceRef findDeviceByName( const std::string &name );
104  static DeviceRef findDeviceByNameContains( const std::string &nameFragment );
105 
106 #if defined( CINDER_COCOA )
108 #else
109  typedef int DeviceIdentifier;
110 #endif
111 
112  // This is an abstract base class for implementing platform specific devices
113  class Device {
114  public:
115  virtual ~Device() {}
117  const std::string& getName() const { return mName; }
119  virtual bool checkAvailable() const = 0;
121  virtual bool isConnected() const = 0;
123  virtual Capture::DeviceIdentifier getUniqueId() const = 0;
125 #if defined( CINDER_COCOA )
126  virtual void* getNative() const = 0;
127 #endif
128 #if defined( CINDER_COCOA_TOUCH )
129  virtual bool isFrontFacing() const = 0;
131 #endif
132  protected:
133  Device() {}
135  };
136 
137  protected:
138  struct Obj {
139  Obj( int32_t width, int32_t height, const Capture::DeviceRef device );
140  virtual ~Obj();
141 
142 #if defined( CINDER_MAC )
143  CaptureImplQtKit *mImpl;
144 #elif defined( CINDER_COCOA_TOUCH_SIMULATOR )
145  CaptureImplCocoaDummy *mImpl;
146 #elif defined( CINDER_COCOA_TOUCH_DEVICE )
147  CaptureImplAvFoundation *mImpl;
148 #elif defined( CINDER_MSW )
149  CaptureImplDirectShow *mImpl;
150 #endif
151  };
152 
153  std::shared_ptr<Obj> mObj;
154 
155  public:
157  typedef std::shared_ptr<Obj> Capture::*unspecified_bool_type;
159  operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Capture::mObj; }
160  void reset() { mObj.reset(); }
162 };
163 
164 class CaptureExc : public Exception {
165 };
166 
168 };
169 
171 };
172 
173 } //namespace cinder
virtual Capture::DeviceIdentifier getUniqueId() const =0
Returns the OS-specific unique identifier.
virtual ~Device()
Definition: Capture.h:115
Definition: CaptureImplDirectShow.h:32
virtual bool checkAvailable() const =0
Returns whether the device is available for use.
void * getNative()
Definition: AppCocoaTouch.mm:1051
Definition: Capture.h:61
void stop()
Stop capturing video.
Definition: Capture.cpp:110
bool checkNewFrame() const
Returns whether there is a new video frame available since the last call to checkNewFrame() ...
Definition: Capture.cpp:128
Definition: Area.h:37
virtual ~Obj()
Definition: Capture.cpp:87
int DeviceIdentifier
Definition: Capture.h:109
GLsizei const GLchar ** string
Definition: GLee.h:2427
const Capture::DeviceRef getDevice() const
Returns the associated Device for this instace of Capture.
Definition: Capture.cpp:162
static CaptureRef create(int32_t width, int32_t height, const DeviceRef device=DeviceRef())
Definition: Capture.h:66
std::shared_ptr< Obj > Capture::* unspecified_bool_type
Emulates shared_ptr-like behavior.
Definition: Capture.h:158
Area getBounds() const
Returns the bounding rectangle of the capture imagee, which is Area( 0, 0, width, height ) ...
Definition: Capture.h:92
GLenum GLsizei width
Definition: GLee.h:969
Capture()
Definition: Capture.h:68
Definition: Capture.h:138
void reset()
Emulates shared_ptr-like behavior.
Definition: Capture.h:160
static const std::vector< DeviceRef > & getDevices(bool forceRefresh=false)
Returns a vector of all Devices connected to the system. If forceRefresh then the system will be poll...
Definition: Capture.cpp:45
Surface8u getSurface() const
Returns a Surface representing the current captured frame.
Definition: Capture.cpp:137
std::string mName
Definition: Capture.h:134
GLenum GLsizei GLsizei height
Definition: GLee.h:1029
Definition: Capture.h:167
Obj(int32_t width, int32_t height, const Capture::DeviceRef device)
Definition: Capture.cpp:78
static DeviceRef findDeviceByName(const std::string &name)
Finds a particular device based on its name.
Definition: Capture.cpp:54
static DeviceRef findDeviceByNameContains(const std::string &nameFragment)
Finds the first device whose name contains the string nameFragment.
Definition: Capture.cpp:65
~Capture()
Definition: Capture.h:71
float getAspectRatio() const
Returns the aspect ratio of the capture imagee, which is its width / height.
Definition: Capture.h:90
Device()
Returns an OS-specific pointer. QTCaptureDevice* on Mac OS X, AVCaptureDevice* on iOS...
Definition: Capture.h:133
GLuint const GLchar * name
Definition: GLee.h:2259
void start()
Begin capturing video.
Definition: Capture.cpp:101
const std::string & getName() const
Returns the human-readable name of the device.
Definition: Capture.h:117
virtual bool isConnected() const =0
Returns whether the device is currently connected.
Definition: Capture.h:113
Definition: Exception.h:32
int32_t getWidth() const
Returns the width of the captured image in pixels.
Definition: Capture.cpp:146
bool isCapturing()
Is the device capturing video.
Definition: Capture.cpp:119
std::shared_ptr< Device > DeviceRef
Definition: Capture.h:63
Vec2i getSize() const
Returns the size of the captured image in pixels.
Definition: Capture.h:88
std::shared_ptr< Obj > mObj
Definition: Capture.h:153
Definition: Capture.h:164
int32_t getHeight() const
Returns the height of the captured image in pixels.
Definition: Capture.cpp:154
std::shared_ptr< class Capture > CaptureRef
Definition: Capture.h:59
Vec2< int > Vec2i
Definition: Vector.h:1313
Definition: Capture.h:170