Instrument Neutral Distributed Interface INDI 2.2.5
v4l2_base.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2005 by Jasem Mutlaq <[email protected]>
3 Copyright (C) 2013 Geehalel ([email protected])
4
5 Based on V4L 2 Example
6 http://v4l2spec.bytesex.org/spec-single/v4l2.html#CAPTURE-EXAMPLE
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
22*/
23
24#pragma once
25
26//#include "videodev2.h"
27#include "eventloop.h"
28#include "indidevapi.h"
29// this adds add dependency to indidriver for v4l_legacy, meade_lpi
30
31// Can't use logger as legacy drivers don't use defaultdevice
32//#include "indilogger.h"
34// for direct recording
36
37#include <stdio.h>
38#include <cstdlib>
39#include <map>
40#include <string>
41
42#include <dirent.h>
43#ifdef __OpenBSD__
44#include <sys/videoio.h>
45#else
46#include <linux/videodev2.h>
47#endif
48
49#define VIDEO_COMPRESSION_LEVEL 4
50
51class V4L2_Driver;
52
53enum
54{
58};
59
60namespace INDI
61{
62
64{
65 public:
66 V4L2_Base();
67 virtual ~V4L2_Base();
68
70
71 struct buffer
72 {
73 void *start;
74 size_t length;
75 };
76
77 /* Connection */
78 virtual int connectCam(const char *devpath, char *errmsg, int pixelFormat = -1, int width = -1, int height = -1);
79 virtual void disconnectCam(bool stopcapture);
80 char *getDeviceName();
81 void setDeviceName(const char *name);
82 bool isLXmodCapable();
83
84 /* Updates */
85 void callFrame(void *p);
86
87 // Enumerate all available V42L devices and return a map with common device name
88 static std::map<std::string, std::string> enumerate();
89 static int video_dev_file_select(const dirent *entry);
90
91 /* Image Format/Size */
92 int getFormat();
93 int getWidth();
94 int getHeight();
95 int getBpp();
96 void setNative(bool value) {m_Native = value;}
97 virtual int setSize(int x, int y);
98 virtual void getMaxMinSize(int &x_max, int &y_max, int &x_min, int &y_min);
99
100 /* Frame rate */
101 int (V4L2_Base::*setframerate)(struct v4l2_fract frate, char *errmsg);
102 struct v4l2_fract (V4L2_Base::*getframerate)();
103
104 unsigned char *getY();
105 unsigned char *getU();
106 unsigned char *getV();
107 unsigned char * getMJPEGBuffer(int &size);
108 unsigned char *getRGBBuffer();
109 float *getLinearY();
110
111 void registerCallback(WPF *fp, void *ud);
112
113 int start_capturing(char *errmsg);
114 int stop_capturing(char *errmsg);
115 static void newFrame(int fd, void *p);
116
117 //void setDropFrameCount(unsigned int count) { dropFrameCount = count;}
118 void enumerate_ctrl();
119 void enumerate_menu();
120 bool enumerate_ext_ctrl();
121 int queryINTControls(INumberVectorProperty *nvp);
122 bool queryExtControls(INumberVectorProperty *nvp, unsigned int *nnumber, ISwitchVectorProperty **options,
123 unsigned int *noptions, const char *dev, const char *group);
124 void queryControls(INumberVectorProperty *nvp, unsigned int *nnumber, ISwitchVectorProperty **options,
125 unsigned int *noptions, const char *dev, const char *group);
126
127 int getControl(unsigned int ctrl_id, double *value, char *errmsg);
128 int setINTControl(unsigned int ctrl_id, double new_value, char *errmsg);
129 int setOPTControl(unsigned int ctrl_id, unsigned int new_value, char *errmsg);
130
131 int query_ctrl(unsigned int ctrl_id, double &ctrl_min, double &ctrl_max, double &ctrl_step, double &ctrl_value,
132 char *errmsg);
133 void getinputs(ISwitchVectorProperty *inputssp);
134 int setinput(unsigned int inputindex, char *errmsg);
135 void getcaptureformats(ISwitchVectorProperty *captureformatssp);
136 int setcaptureformat(unsigned int captureformatindex, char *errmsg);
137 void getcapturesizes(ISwitchVectorProperty *capturesizessp, INumberVectorProperty *capturesizenp);
138 int setcapturesize(unsigned int w, unsigned int h, char *errmsg);
139 void getframerates(ISwitchVectorProperty *frameratessp, INumberVectorProperty *frameratenp);
140 int setcroprect(int x, int y, int w, int h, char *errmsg);
141 struct v4l2_rect getcroprect();
142
143 void setColorProcessing(bool quantization, bool colorconvert, bool linearization);
144
145 void setlxstate(short s)
146 {
147 IDLog("setlexstate to %d\n", s);
148 lxstate = s;
149 }
151 {
152 return lxstate;
153 }
155 {
156 return streamactive;
157 }
158
159 void doDecode(bool);
160
161 protected:
162 int xioctl(int fd, int request, void *arg, char const *const request_str);
163 int ioctl_set_format(struct v4l2_format new_fmt, char *errmsg);
164
165 int read_frame(char *errsg);
166 int uninit_device(char *errmsg);
167 int open_device(const char *devpath, char *errmsg);
168 int check_device(char *errmsg);
169 int init_device(char *errmsg);
170 int init_mmap(char *errmsg);
171 int errno_exit(const char *s, char *errmsg);
172
173 void close_device();
174 void init_userp(unsigned int buffer_size);
175 void init_read(unsigned int buffer_size);
176
177 void findMinMax();
178
181
182 /* Frame rate */
183 int stdsetframerate(struct v4l2_fract frate, char *errmsg);
184 int pwcsetframerate(struct v4l2_fract frate, char *errmsg);
185 struct v4l2_fract stdgetframerate();
186
187 struct v4l2_capability cap;
188 struct v4l2_cropcap cropcap;
189 struct v4l2_crop crop;
190 struct v4l2_format fmt;
191 struct v4l2_input input;
192 struct v4l2_buffer buf;
193
199
200 short lxstate;
201
202 struct v4l2_queryctrl queryctrl;
203 struct v4l2_querymenu querymenu;
205
206 bool is_compressed() const;
207
209 void *uptr;
210 char dev_name[64];
211 const char *path;
212 bool m_Native {false};
214 int fd;
216 unsigned int n_buffers;
218 //int dropFrame;
219 //bool dropFrameEnabled;
220 //unsigned int dropFrameCount;
221
222 struct v4l2_fract frameRate;
225 //unsigned char * YBuf,*UBuf,*VBuf, *yuvBuffer, *colorBuffer, *rgb24_buffer, *cropbuf;
226
230
231 int bpp;
232
233 friend class ::V4L2_Driver;
234
236};
237}
void init_read(unsigned int buffer_size)
bool has_ext_pix_format
Definition v4l2_base.h:204
struct v4l2_querymenu querymenu
Definition v4l2_base.h:203
int read_frame(char *errsg)
char deviceName[MAXINDIDEVICE]
Definition v4l2_base.h:235
int stop_capturing(char *errmsg)
io_method io
Definition v4l2_base.h:213
void callFrame(void *p)
bool reallocate_buffers
Definition v4l2_base.h:217
void doDecode(bool)
int check_device(char *errmsg)
int xioctl(int fd, int request, void *arg, char const *const request_str)
virtual void disconnectCam(bool stopcapture)
struct v4l2_queryctrl queryctrl
Definition v4l2_base.h:202
float * getLinearY()
const char * path
Definition v4l2_base.h:211
int(V4L2_Base::* setframerate)(struct v4l2_fract frate, char *errmsg)
Definition v4l2_base.h:101
struct v4l2_rect getcroprect()
bool isstreamactive()
Definition v4l2_base.h:154
int pwcsetframerate(struct v4l2_fract frate, char *errmsg)
static int video_dev_file_select(const dirent *entry)
bool enumerate_ext_ctrl()
int stdsetframerate(struct v4l2_fract frate, char *errmsg)
int open_device(const char *devpath, char *errmsg)
int setINTControl(unsigned int ctrl_id, double new_value, char *errmsg)
struct v4l2_capability cap
Definition v4l2_base.h:187
int query_ctrl(unsigned int ctrl_id, double &ctrl_min, double &ctrl_max, double &ctrl_step, double &ctrl_value, char *errmsg)
void setlxstate(short s)
Definition v4l2_base.h:145
void queryControls(INumberVectorProperty *nvp, unsigned int *nnumber, ISwitchVectorProperty **options, unsigned int *noptions, const char *dev, const char *group)
int getControl(unsigned int ctrl_id, double *value, char *errmsg)
int errno_exit(const char *s, char *errmsg)
void setColorProcessing(bool quantization, bool colorconvert, bool linearization)
void getframerates(ISwitchVectorProperty *frameratessp, INumberVectorProperty *frameratenp)
unsigned char * getV()
bool is_compressed() const
Helper indicating whether current pixel format is compressed or not.
void registerCallback(WPF *fp, void *ud)
V4L2_Decode * v4l2_decode
Definition v4l2_base.h:227
struct buffer * buffers
Definition v4l2_base.h:215
struct v4l2_format fmt
Definition v4l2_base.h:190
unsigned char * getU()
int uninit_device(char *errmsg)
void getcaptureformats(ISwitchVectorProperty *captureformatssp)
static void newFrame(int fd, void *p)
unsigned char * getY()
int start_capturing(char *errmsg)
struct v4l2_fract stdgetframerate()
void getcapturesizes(ISwitchVectorProperty *capturesizessp, INumberVectorProperty *capturesizenp)
unsigned char * getMJPEGBuffer(int &size)
void init_userp(unsigned int buffer_size)
struct v4l2_buffer buf
Definition v4l2_base.h:192
struct v4l2_fract(V4L2_Base::* getframerate)()
Definition v4l2_base.h:102
short getlxstate()
Definition v4l2_base.h:150
void setDeviceName(const char *name)
int init_device(char *errmsg)
int setcapturesize(unsigned int w, unsigned int h, char *errmsg)
int setcaptureformat(unsigned int captureformatindex, char *errmsg)
struct v4l2_cropcap cropcap
Definition v4l2_base.h:188
int ioctl_set_format(struct v4l2_format new_fmt, char *errmsg)
char dev_name[64]
Definition v4l2_base.h:210
static std::map< std::string, std::string > enumerate()
int enumeratedCaptureFormats
Definition v4l2_base.h:180
virtual void getMaxMinSize(int &x_max, int &y_max, int &x_min, int &y_min)
unsigned char * getRGBBuffer()
char * getDeviceName()
int setcroprect(int x, int y, int w, int h, char *errmsg)
bool queryExtControls(INumberVectorProperty *nvp, unsigned int *nnumber, ISwitchVectorProperty **options, unsigned int *noptions, const char *dev, const char *group)
unsigned int n_buffers
Definition v4l2_base.h:216
struct v4l2_fract frameRate
Definition v4l2_base.h:222
V4L2_Decoder * decoder
Definition v4l2_base.h:228
int init_mmap(char *errmsg)
struct v4l2_crop crop
Definition v4l2_base.h:189
void getinputs(ISwitchVectorProperty *inputssp)
virtual int setSize(int x, int y)
struct v4l2_input input
Definition v4l2_base.h:191
void setNative(bool value)
Definition v4l2_base.h:96
int setinput(unsigned int inputindex, char *errmsg)
int setOPTControl(unsigned int ctrl_id, unsigned int new_value, char *errmsg)
int queryINTControls(INumberVectorProperty *nvp)
bool isLXmodCapable()
virtual int connectCam(const char *devpath, char *errmsg, int pixelFormat=-1, int width=-1, int height=-1)
virtual ~V4L2_Base()
Public interface to INDI's eventloop mechanism.
void() WPF(void *)
Signature of a work procedure function.
Definition eventloop.h:43
#define MAXINDIDEVICE
Definition indiapi.h:191
void IDLog(const char *fmt,...)
Definition indicom.c:305
Interface to the reference INDI C API device implementation on the Device Driver side.
Namespace to encapsulate INDI client, drivers, and mediator classes.
@ LX_ACCUMULATING
Definition v4l2_base.h:57
@ LX_ACTIVE
Definition v4l2_base.h:55
@ LX_TRIGGERED
Definition v4l2_base.h:56