Cinder

  • Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

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 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 
00105     // This is an abstract base class for implementing platform specific devices
00106     class Device {
00107      public:
00108         virtual ~Device() {}
00110         const std::string&                  getName() const { return mName; }
00112         virtual bool                        checkAvailable() const = 0;
00114         virtual bool                        isConnected() const = 0;
00116         virtual Capture::DeviceIdentifier   getUniqueId() const = 0;
00117      protected:
00118         Device() {}
00119         std::string     mName;
00120     };
00121         
00122  protected: 
00123     struct Obj {
00124         Obj( int32_t width, int32_t height, const Capture::DeviceRef device );
00125         virtual ~Obj();
00126 
00127 #if defined( CINDER_MAC ) 
00128         CaptureImplQtKit                *mImpl;
00129 #elif defined( CINDER_COCOA_TOUCH )
00130         CaptureImplAvFoundation         *mImpl;
00131 #elif defined( CINDER_MSW )
00132         CaptureImplDirectShow           *mImpl;
00133 #endif
00134     };
00135     
00136     shared_ptr<Obj>                     mObj;
00137     
00138   public:
00140 
00141     typedef shared_ptr<Obj> Capture::*unspecified_bool_type;
00142     operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Capture::mObj; }
00143     void reset() { mObj.reset(); }
00145 };
00146 
00147 class CaptureExc : public Exception {
00148 };
00149 
00150 class CaptureExcInitFail : public CaptureExc {
00151 };
00152 
00153 class CaptureExcInvalidChannelOrder : public CaptureExc {
00154 };
00155 
00156 } //namespace cinder