source: eviacam/trunk/fuentes/creavision/crvcamera_v4l2.h @ 29

Last change on this file since 29 was 29, checked in by mabarracus, 4 years ago

added eviacam content

  • Property svn:executable set to *
File size: 5.3 KB
Line 
1/////////////////////////////////////////////////////////////////////////////
2// Name:        crvcamera_v4l2.h
3// Purpose:     Provide a camera capture class around v4l2 and libwebcam
4// Author:      Cesar Mauri Loba (cesar at crea-si dot com)
5// Modified by:
6// Created:     17/05/2010
7// Copyright:   (C) 2008-10 Cesar Mauri Loba - CREA Software Systems
8//              Portions of guvcview are (c) of Paulo Assis and others
9//
10//  This program is free software: you can redistribute it and/or modify
11//  it under the terms of the GNU General Public License as published by
12//  the Free Software Foundation, either version 3 of the License, or
13//  (at your option) any later version.
14//
15//  This program is distributed in the hope that it will be useful,
16//  but WITHOUT ANY WARRANTY; without even the implied warranty of
17//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18//  GNU General Public License for more details.
19//
20//  You should have received a copy of the GNU General Public License
21//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22/////////////////////////////////////////////////////////////////////////////
23#ifndef CRVCAMERA_V4L2_H_
24#define CRVCAMERA_V4L2_H_
25
26#include <inttypes.h>
27#include <linux/videodev2.h>
28#include "crvcamera_enum.h"
29#include "crvcamera.h"
30#include "crvimage.h"
31#include "webcam.h"
32#include <vector>
33#include <string>
34
35// Forward declarations
36class CCameraControlV4L2;
37
38class CCameraV4L2 : public CCamera
39{
40public:
41        CCameraV4L2(int cameraId= -1, unsigned int width= 320, 
42                unsigned int height= 240, float fr= 30.0f) throw(camera_exception);
43        virtual ~CCameraV4L2 (void);
44
45       
46        virtual bool HasCameraControls() { return true; }
47        virtual unsigned int GetCameraControlsCount();
48        virtual CCameraControl* GetCameraControl(unsigned int numControl);
49       
50        static int GetNumDevices();
51        static const char* GetDeviceName (unsigned int id);
52        void Dump();
53
54protected:
55        virtual bool DoOpen();
56        virtual void DoClose(); 
57        virtual IplImage *DoQueryFrame();
58        virtual bool DoQueryFrame(CIplImage& image);
59
60private:
61        // Private types
62        enum ECaptureMethod { CAP_NONE= 0, CAP_READ, CAP_STREAMING_MMAP, CAP_STREAMING_USR };
63        enum { STREAMING_CAPTURE_NBUFFERS= 2 };
64        struct TImageFormat { 
65                unsigned int frame_rate;
66                unsigned int width;
67                unsigned int height;
68                uint32_t pixelformat;   // Four character code
69        };
70       
71        int m_Id;
72        TImageFormat m_desiredFormat;
73        TImageFormat m_currentFormat;
74        CHandle m_libWebcamHandle;     
75        ECaptureMethod m_captureMethod;
76        bool m_isStreaming;
77        bool m_buffersReady;
78        struct v4l2_buffer m_captureBuffersInfo[STREAMING_CAPTURE_NBUFFERS];
79        void* m_captureBuffersPtr[STREAMING_CAPTURE_NBUFFERS];
80        CIplImage m_resultImage;       
81        std::vector<uint32_t> m_supportedPixelFormats;
82        std::vector<CCameraControlV4L2> m_cameraControls;
83
84        //
85        // Static members
86        //
87        enum { MAX_CAM_DEVICES= 10, CAM_DEVICE_NAME_LENGHT= 50 };
88        static int g_numDevices;
89        static char g_deviceNames[MAX_CAM_DEVICES][CAM_DEVICE_NAME_LENGHT];
90        enum { CAM_DEVICE_SHORT_NAME_LENGHT= 32, CAM_DEVICE_DRIVER_NAME_LENGHT= 20 };
91        static char g_deviceShortNames[MAX_CAM_DEVICES][CAM_DEVICE_SHORT_NAME_LENGHT];
92        static char g_deviceDriverNames[MAX_CAM_DEVICES][CAM_DEVICE_DRIVER_NAME_LENGHT];
93        // Instance count for automatic libwebcam cleanup
94        static int g_numInstances;
95        // Instances inc/dec
96        void InstanceCreated() throw(camera_exception);
97        void InstanceDestroyed();
98
99        // Private methods
100        bool InternalOpen();
101        ECaptureMethod DetectCaptureMethod();
102        bool DetectBestImageFormat();
103        bool SetImageFormat();
104        bool EnableVideo(bool enable);
105        bool AllocateBuffers();
106        bool DeallocateBuffers();
107        bool RequestBuffers(enum v4l2_memory mem);
108        bool UnRequestBuffers(enum v4l2_memory mem);
109        void UnmapBuffers();
110        bool DecodeToRGB (void* src, void* dst, int width, int height, uint32_t pixelformat);
111        void AddSupportedPixelFormats ();
112        bool PopulateCameraControls ();
113};
114
115// Class that models each camera control
116class CCameraControlV4L2 : public CCameraControl {
117public:
118        CCameraControlV4L2 (CHandle handle, const CControl& control);
119       
120        virtual ECameraControlId GetId() const { return LibwebcamId2ECameraControlId(m_id); }
121        // Get the name of the control provided by the driver
122        virtual const char* GetName() const { return m_name.c_str(); }
123        virtual ECameraControlType GetType() const { return CControlType2ECameraControlType (m_type); }
124
125        // Get/set the current value. For boolean controls 0 and 1 are the only
126        // possible values. For choice controls 0 represents the first option.
127        // Set method returns true on success, false otherwise
128        virtual int GetValue() const;
129        virtual bool SetValue(int value);
130
131        virtual int GetDefaultValue() const { return m_default; }
132        virtual int GetMinimumValue() const { return m_min; }
133        virtual int GetMaximumValue() const { return m_max; }
134
135        // For choices only
136        virtual const char* GetChoiceName(unsigned int numOption) const;
137       
138        // Check whether a certain control id is supported
139        static bool CheckSupportedLibwebcamId (CControlId id);
140
141        void Dump();
142
143private:
144        static ECameraControlId LibwebcamId2ECameraControlId (CControlId id);
145        static ECameraControlType CControlType2ECameraControlType (CControlType type);
146       
147        CHandle m_handle;
148        CControlId m_id;        // The identifier of the control
149        std::string m_name;     // The name of the control
150        CControlType m_type;    // The type of the control
151        int m_default, m_min, m_max;
152        std::vector<std::string> m_choices;
153};
154
155#endif
Note: See TracBrowser for help on using the repository browser.