include/cinder/Capture.h
Go to the documentation of this file.
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 CaptureImplQtKit;
00032         @class QTCaptureDevice;
00033     #else
00034         class CaptureImplQtKit;
00035         class QTCaptureDevice;
00036     #endif
00037 #elif defined( CINDER_COCOA_TOUCH_SIMULATOR )
00038     #if defined( __OBJC__ )
00039         @class CaptureImplCocoaDummy;
00040     #else
00041         class CaptureImplCocoaDummy;
00042     #endif
00043 #elif defined( CINDER_COCOA_TOUCH_DEVICE )
00044     #if defined( __OBJC__ )
00045         @class CaptureImplAvFoundation;
00046     #else
00047         class CaptureImplAvFoundation;
00048     #endif
00049 #elif defined( CINDER_MSW )
00050     namespace cinder {
00051         class CaptureImplDirectShow;
00052     }
00053 #endif
00054 
00055 #include <map>
00056 
00057 namespace cinder {
00058 
00059 typedef std::shared_ptr<class Capture>  CaptureRef;
00060 
00061 class Capture {
00062   public:
00063     class Device;
00064     typedef std::shared_ptr<Device> DeviceRef;
00065 
00066     static CaptureRef   create( int32_t width, int32_t height, const DeviceRef device = DeviceRef() ) { return CaptureRef( new Capture( width, height, device ) ); }
00067 
00068     Capture() {}
00070     Capture( int32_t width, int32_t height, const DeviceRef device = DeviceRef() );
00071     ~Capture() {}
00072 
00074     void        start();
00076     void        stop();
00078     bool        isCapturing();
00079 
00081     bool        checkNewFrame() const;
00082 
00084     int32_t     getWidth() const;
00086     int32_t     getHeight() const;
00088     Vec2i       getSize() const { return Vec2i( getWidth(), getHeight() ); }
00090     float       getAspectRatio() const { return getWidth() / (float)getHeight(); }
00092     Area        getBounds() const { return Area( 0, 0, getWidth(), getHeight() ); }
00093     
00095     Surface8u   getSurface() const;
00097     const Capture::DeviceRef getDevice() const;
00098 
00100     static const std::vector<DeviceRef>&    getDevices( bool forceRefresh = false );
00102     static DeviceRef findDeviceByName( const std::string &name );
00104     static DeviceRef findDeviceByNameContains( const std::string &nameFragment );
00105 
00106 #if defined( CINDER_COCOA )
00107     typedef std::string DeviceIdentifier;
00108 #else
00109     typedef int DeviceIdentifier;
00110 #endif
00111 
00112     // This is an abstract base class for implementing platform specific devices
00113     class Device {
00114      public:
00115         virtual ~Device() {}
00117         const std::string&                  getName() const { return mName; }
00119         virtual bool                        checkAvailable() const = 0;
00121         virtual bool                        isConnected() const = 0;
00123         virtual Capture::DeviceIdentifier   getUniqueId() const = 0;
00125 #if defined( CINDER_COCOA )
00126         virtual void*       getNative() const = 0;
00127 #endif
00128 #if defined( CINDER_COCOA_TOUCH )
00129 
00130         virtual bool        isFrontFacing() const = 0;
00131 #endif
00132      protected:
00133         Device() {}
00134         std::string     mName;
00135     };
00136         
00137  protected: 
00138     struct Obj {
00139         Obj( int32_t width, int32_t height, const Capture::DeviceRef device );
00140         virtual ~Obj();
00141 
00142 #if defined( CINDER_MAC ) 
00143         CaptureImplQtKit                *mImpl;
00144 #elif defined( CINDER_COCOA_TOUCH_SIMULATOR )
00145         CaptureImplCocoaDummy           *mImpl;
00146 #elif defined( CINDER_COCOA_TOUCH_DEVICE )
00147         CaptureImplAvFoundation         *mImpl;
00148 #elif defined( CINDER_MSW )
00149         CaptureImplDirectShow           *mImpl;
00150 #endif
00151     };
00152     
00153     std::shared_ptr<Obj>                mObj;
00154     
00155   public:
00157 
00158     typedef std::shared_ptr<Obj> Capture::*unspecified_bool_type;
00159     operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Capture::mObj; }
00160     void reset() { mObj.reset(); }
00162 };
00163 
00164 class CaptureExc : public Exception {
00165 };
00166 
00167 class CaptureExcInitFail : public CaptureExc {
00168 };
00169 
00170 class CaptureExcInvalidChannelOrder : public CaptureExc {
00171 };
00172 
00173 } //namespace cinder