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 
00038 #elif defined( CINDER_COCOA_TOUCH )
00039 #   if defined( __OBJC__ )
00040         @class CaptureImplAvFoundation;
00041         //@class QTCaptureDevice;
00042 #   else
00043         class CaptureImplAvFoundation;
00044         //class QTCaptureDevice;
00045 #   endif
00046 #endif
00047 
00048 #include <map>
00049 
00050 namespace cinder {
00051 
00052 #if defined( CINDER_MSW )
00053     class CaptureImplDirectShow;
00054 #endif
00055 
00056 class Capture {
00057  public:
00058     class Device;
00059     typedef std::shared_ptr<Device> DeviceRef;
00060     
00061     Capture() {}
00062     Capture( int32_t width, int32_t height, const DeviceRef device = DeviceRef() );
00063     ~Capture() {}
00064 
00066     void        start();
00068     void        stop();
00070     bool        isCapturing();
00071 
00073     bool        checkNewFrame() const;
00074 
00076     int32_t     getWidth() const;
00078     int32_t     getHeight() const;
00080     Vec2i       getSize() const { return Vec2i( getWidth(), getHeight() ); }
00082     float       getAspectRatio() const { return getWidth() / (float)getHeight(); }
00084     Area        getBounds() const { return Area( 0, 0, getWidth(), getHeight() ); }
00085     
00087     Surface8u   getSurface() const;
00089     const Capture::DeviceRef getDevice() const;
00090 
00092     static const std::vector<DeviceRef>&    getDevices( bool forceRefresh = false );
00094     static DeviceRef findDeviceByName( const std::string &name );
00096     static DeviceRef findDeviceByNameContains( const std::string &nameFragment );
00097 
00098 #if defined( CINDER_COCOA )
00099     typedef std::string DeviceIdentifier;
00100 #else
00101     typedef int DeviceIdentifier;
00102 #endif
00103 
00104     // This is an abstract base class for implementing platform specific devices
00105     class Device {
00106      public:
00107         virtual ~Device() {}
00109         const std::string&                  getName() const { return mName; }
00111         virtual bool                        checkAvailable() const = 0;
00113         virtual bool                        isConnected() const = 0;
00115         virtual Capture::DeviceIdentifier   getUniqueId() const = 0;
00116      protected:
00117         Device() {}
00118         std::string     mName;
00119     };
00120         
00121  protected: 
00122     struct Obj {
00123         Obj( int32_t width, int32_t height, const Capture::DeviceRef device );
00124         virtual ~Obj();
00125 
00126 #if defined( CINDER_MAC ) 
00127         CaptureImplQtKit                *mImpl;
00128 #elif defined( CINDER_COCOA_TOUCH )
00129         CaptureImplAvFoundation         *mImpl;
00130 #elif defined( CINDER_MSW )
00131         CaptureImplDirectShow           *mImpl;
00132 #endif
00133     };
00134     
00135     std::shared_ptr<Obj>                mObj;
00136     
00137   public:
00139 
00140     typedef std::shared_ptr<Obj> Capture::*unspecified_bool_type;
00141     operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Capture::mObj; }
00142     void reset() { mObj.reset(); }
00144 };
00145 
00146 class CaptureExc : public Exception {
00147 };
00148 
00149 class CaptureExcInitFail : public CaptureExc {
00150 };
00151 
00152 class CaptureExcInvalidChannelOrder : public CaptureExc {
00153 };
00154 
00155 } //namespace cinder