Instrument Neutral Distributed Interface INDI  2.0.2
v4l2_base.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2005 by Jasem Mutlaq <mutlaqja@ikarustech.com>
3  Copyright (C) 2013 Geehalel (geehalel@gmail.com)
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
35 #include "stream/streammanager.h"
36 
37 #include <stdio.h>
38 #include <cstdlib>
39 #include <map>
40 
41 #include <dirent.h>
42 #include <linux/videodev2.h>
43 
44 #define VIDEO_COMPRESSION_LEVEL 4
45 
46 class V4L2_Driver;
47 
48 enum
49 {
50  LX_ACTIVE = 0,
53 };
54 
55 namespace INDI
56 {
57 
58 class V4L2_Base
59 {
60  public:
61  V4L2_Base();
62  virtual ~V4L2_Base();
63 
65 
66  struct buffer
67  {
68  void *start;
69  size_t length;
70  };
71 
72  /* Connection */
73  virtual int connectCam(const char *devpath, char *errmsg, int pixelFormat = -1, int width = -1, int height = -1);
74  virtual void disconnectCam(bool stopcapture);
75  char *getDeviceName();
76  void setDeviceName(const char *name);
77  bool isLXmodCapable();
78 
79  /* Updates */
80  void callFrame(void *p);
81 
82  // Enumerate all available V42L devices and return a map with common device name
83  static std::map<std::string, std::string> enumerate();
84  static int video_dev_file_select(const dirent *entry);
85 
86  /* Image Format/Size */
87  int getFormat();
88  int getWidth();
89  int getHeight();
90  int getBpp();
91  void setNative(bool value) {m_Native = value;}
92  virtual int setSize(int x, int y);
93  virtual void getMaxMinSize(int &x_max, int &y_max, int &x_min, int &y_min);
94 
95  /* Frame rate */
96  int (V4L2_Base::*setframerate)(struct v4l2_fract frate, char *errmsg);
97  struct v4l2_fract (V4L2_Base::*getframerate)();
98 
99  unsigned char *getY();
100  unsigned char *getU();
101  unsigned char *getV();
102  unsigned char * getMJPEGBuffer(int &size);
103  unsigned char *getRGBBuffer();
104  float *getLinearY();
105 
106  void registerCallback(WPF *fp, void *ud);
107 
108  int start_capturing(char *errmsg);
109  int stop_capturing(char *errmsg);
110  static void newFrame(int fd, void *p);
111 
112  //void setDropFrameCount(unsigned int count) { dropFrameCount = count;}
113  void enumerate_ctrl();
114  void enumerate_menu();
115  bool enumerate_ext_ctrl();
116  int queryINTControls(INumberVectorProperty *nvp);
117  bool queryExtControls(INumberVectorProperty *nvp, unsigned int *nnumber, ISwitchVectorProperty **options,
118  unsigned int *noptions, const char *dev, const char *group);
119  void queryControls(INumberVectorProperty *nvp, unsigned int *nnumber, ISwitchVectorProperty **options,
120  unsigned int *noptions, const char *dev, const char *group);
121 
122  int getControl(unsigned int ctrl_id, double *value, char *errmsg);
123  int setINTControl(unsigned int ctrl_id, double new_value, char *errmsg);
124  int setOPTControl(unsigned int ctrl_id, unsigned int new_value, char *errmsg);
125 
126  int query_ctrl(unsigned int ctrl_id, double &ctrl_min, double &ctrl_max, double &ctrl_step, double &ctrl_value,
127  char *errmsg);
128  void getinputs(ISwitchVectorProperty *inputssp);
129  int setinput(unsigned int inputindex, char *errmsg);
130  void getcaptureformats(ISwitchVectorProperty *captureformatssp);
131  int setcaptureformat(unsigned int captureformatindex, char *errmsg);
132  void getcapturesizes(ISwitchVectorProperty *capturesizessp, INumberVectorProperty *capturesizenp);
133  int setcapturesize(unsigned int w, unsigned int h, char *errmsg);
134  void getframerates(ISwitchVectorProperty *frameratessp, INumberVectorProperty *frameratenp);
135  int setcroprect(int x, int y, int w, int h, char *errmsg);
136  struct v4l2_rect getcroprect();
137 
138  void setColorProcessing(bool quantization, bool colorconvert, bool linearization);
139 
140  void setlxstate(short s)
141  {
142  IDLog("setlexstate to %d\n", s);
143  lxstate = s;
144  }
145  short getlxstate()
146  {
147  return lxstate;
148  }
150  {
151  return streamactive;
152  }
153 
154  void doDecode(bool);
155 
156  protected:
157  int xioctl(int fd, int request, void *arg, char const *const request_str);
158  int ioctl_set_format(struct v4l2_format new_fmt, char *errmsg);
159 
160  int read_frame(char *errsg);
161  int uninit_device(char *errmsg);
162  int open_device(const char *devpath, char *errmsg);
163  int check_device(char *errmsg);
164  int init_device(char *errmsg);
165  int init_mmap(char *errmsg);
166  int errno_exit(const char *s, char *errmsg);
167 
168  void close_device();
169  void init_userp(unsigned int buffer_size);
170  void init_read(unsigned int buffer_size);
171 
172  void findMinMax();
173 
176 
177  /* Frame rate */
178  int stdsetframerate(struct v4l2_fract frate, char *errmsg);
179  int pwcsetframerate(struct v4l2_fract frate, char *errmsg);
180  struct v4l2_fract stdgetframerate();
181 
182  struct v4l2_capability cap;
183  struct v4l2_cropcap cropcap;
184  struct v4l2_crop crop;
185  struct v4l2_format fmt;
186  struct v4l2_input input;
187  struct v4l2_buffer buf;
188 
189  bool cancrop;
190  bool cropset;
194 
195  short lxstate;
196 
197  struct v4l2_queryctrl queryctrl;
198  struct v4l2_querymenu querymenu;
200 
201  bool is_compressed() const;
202 
204  void *uptr;
205  char dev_name[64];
206  const char *path;
207  bool m_Native {false};
209  int fd;
210  struct buffer *buffers;
211  unsigned int n_buffers;
213  //int dropFrame;
214  //bool dropFrameEnabled;
215  //unsigned int dropFrameCount;
216 
217  struct v4l2_fract frameRate;
218  int xmax, xmin, ymax, ymin;
220  //unsigned char * YBuf,*UBuf,*VBuf, *yuvBuffer, *colorBuffer, *rgb24_buffer, *cropbuf;
221 
224  bool dodecode;
225 
226  int bpp;
227 
228  friend class ::V4L2_Driver;
229 
231 };
232 }
void init_read(unsigned int buffer_size)
Definition: v4l2_base.cpp:840
bool has_ext_pix_format
Definition: v4l2_base.h:199
struct v4l2_querymenu querymenu
Definition: v4l2_base.h:198
int read_frame(char *errsg)
Definition: v4l2_base.cpp:469
char deviceName[MAXINDIDEVICE]
Definition: v4l2_base.h:230
int stop_capturing(char *errmsg)
Definition: v4l2_base.cpp:705
io_method io
Definition: v4l2_base.h:208
void callFrame(void *p)
bool reallocate_buffers
Definition: v4l2_base.h:212
void doDecode(bool)
Definition: v4l2_base.cpp:360
int check_device(char *errmsg)
Definition: v4l2_base.cpp:970
int xioctl(int fd, int request, void *arg, char const *const request_str)
Definition: v4l2_base.cpp:245
virtual void disconnectCam(bool stopcapture)
Definition: v4l2_base.cpp:389
struct v4l2_queryctrl queryctrl
Definition: v4l2_base.h:197
float * getLinearY()
Definition: v4l2_base.cpp:1968
const char * path
Definition: v4l2_base.h:206
int(V4L2_Base::* setframerate)(struct v4l2_fract frate, char *errmsg)
Definition: v4l2_base.h:96
struct v4l2_rect getcroprect()
Definition: v4l2_base.cpp:1835
bool isstreamactive()
Definition: v4l2_base.h:149
int pwcsetframerate(struct v4l2_fract frate, char *errmsg)
Definition: v4l2_base.cpp:1861
static int video_dev_file_select(const dirent *entry)
Definition: v4l2_base.cpp:3041
bool enumerate_ext_ctrl()
Definition: v4l2_base.cpp:2709
int stdsetframerate(struct v4l2_fract frate, char *errmsg)
Definition: v4l2_base.cpp:1840
int open_device(const char *devpath, char *errmsg)
Definition: v4l2_base.cpp:1346
int setINTControl(unsigned int ctrl_id, double new_value, char *errmsg)
Definition: v4l2_base.cpp:2637
struct v4l2_capability cap
Definition: v4l2_base.h:182
int query_ctrl(unsigned int ctrl_id, double &ctrl_min, double &ctrl_max, double &ctrl_step, double &ctrl_value, char *errmsg)
Definition: v4l2_base.cpp:2133
void setlxstate(short s)
Definition: v4l2_base.h:140
void queryControls(INumberVectorProperty *nvp, unsigned int *nnumber, ISwitchVectorProperty **options, unsigned int *noptions, const char *dev, const char *group)
Definition: v4l2_base.cpp:2179
int getControl(unsigned int ctrl_id, double *value, char *errmsg)
Definition: v4l2_base.cpp:2624
int errno_exit(const char *s, char *errmsg)
Definition: v4l2_base.cpp:349
void setColorProcessing(bool quantization, bool colorconvert, bool linearization)
Definition: v4l2_base.cpp:1935
void getframerates(ISwitchVectorProperty *frameratessp, INumberVectorProperty *frameratenp)
Definition: v4l2_base.cpp:1622
unsigned char * getV()
Definition: v4l2_base.cpp:1953
void enumerate_ctrl()
Definition: v4l2_base.cpp:2018
bool is_compressed() const
Helper indicating whether current pixel format is compressed or not.
Definition: v4l2_base.cpp:201
void registerCallback(WPF *fp, void *ud)
Definition: v4l2_base.cpp:1973
V4L2_Decode * v4l2_decode
Definition: v4l2_base.h:222
struct buffer * buffers
Definition: v4l2_base.h:210
struct v4l2_format fmt
Definition: v4l2_base.h:185
unsigned char * getU()
Definition: v4l2_base.cpp:1948
int uninit_device(char *errmsg)
Definition: v4l2_base.cpp:815
void getcaptureformats(ISwitchVectorProperty *captureformatssp)
Definition: v4l2_base.cpp:1458
static void newFrame(int fd, void *p)
Definition: v4l2_base.cpp:808
unsigned char * getY()
Definition: v4l2_base.cpp:1943
int start_capturing(char *errmsg)
Definition: v4l2_base.cpp:740
struct v4l2_fract stdgetframerate()
Definition: v4l2_base.cpp:1876
void getcapturesizes(ISwitchVectorProperty *capturesizessp, INumberVectorProperty *capturesizenp)
Definition: v4l2_base.cpp:1533
unsigned char * getMJPEGBuffer(int &size)
Definition: v4l2_base.cpp:1958
void init_userp(unsigned int buffer_size)
Definition: v4l2_base.cpp:925
struct v4l2_buffer buf
Definition: v4l2_base.h:187
struct v4l2_fract(V4L2_Base::* getframerate)()
Definition: v4l2_base.h:97
short getlxstate()
Definition: v4l2_base.h:145
void setDeviceName(const char *name)
Definition: v4l2_base.cpp:2978
int init_device(char *errmsg)
Definition: v4l2_base.cpp:1310
int setcapturesize(unsigned int w, unsigned int h, char *errmsg)
Definition: v4l2_base.cpp:1612
int setcaptureformat(unsigned int captureformatindex, char *errmsg)
Definition: v4l2_base.cpp:1522
struct v4l2_cropcap cropcap
Definition: v4l2_base.h:183
void enumerate_menu()
Definition: v4l2_base.cpp:2095
int ioctl_set_format(struct v4l2_format new_fmt, char *errmsg)
Definition: v4l2_base.cpp:289
char dev_name[64]
Definition: v4l2_base.h:205
static std::map< std::string, std::string > enumerate()
Definition: v4l2_base.cpp:2991
int enumeratedCaptureFormats
Definition: v4l2_base.h:175
virtual void getMaxMinSize(int &x_max, int &y_max, int &x_min, int &y_min)
Definition: v4l2_base.cpp:1900
unsigned char * getRGBBuffer()
Definition: v4l2_base.cpp:1963
char * getDeviceName()
Definition: v4l2_base.cpp:1895
int setcroprect(int x, int y, int w, int h, char *errmsg)
Definition: v4l2_base.cpp:1694
bool queryExtControls(INumberVectorProperty *nvp, unsigned int *nnumber, ISwitchVectorProperty **options, unsigned int *noptions, const char *dev, const char *group)
Definition: v4l2_base.cpp:2759
unsigned int n_buffers
Definition: v4l2_base.h:211
struct v4l2_fract frameRate
Definition: v4l2_base.h:217
V4L2_Decoder * decoder
Definition: v4l2_base.h:223
int selectCallBackID
Definition: v4l2_base.h:219
int enumeratedInputs
Definition: v4l2_base.h:174
int init_mmap(char *errmsg)
Definition: v4l2_base.cpp:860
struct v4l2_crop crop
Definition: v4l2_base.h:184
void getinputs(ISwitchVectorProperty *inputssp)
Definition: v4l2_base.cpp:1386
virtual int setSize(int x, int y)
Definition: v4l2_base.cpp:1914
struct v4l2_input input
Definition: v4l2_base.h:186
void setNative(bool value)
Definition: v4l2_base.h:91
int setinput(unsigned int inputindex, char *errmsg)
Definition: v4l2_base.cpp:1428
int setOPTControl(unsigned int ctrl_id, unsigned int new_value, char *errmsg)
Definition: v4l2_base.cpp:2673
int queryINTControls(INumberVectorProperty *nvp)
Definition: v4l2_base.cpp:2511
bool isLXmodCapable()
Definition: v4l2_base.cpp:407
virtual int connectCam(const char *devpath, char *errmsg, int pixelFormat=-1, int width=-1, int height=-1)
Definition: v4l2_base.cpp:365
virtual ~V4L2_Base()
Definition: v4l2_base.cpp:185
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:193
void IDLog(const char *fmt,...)
Definition: indicom.c:316
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:52
@ LX_ACTIVE
Definition: v4l2_base.h:50
@ LX_TRIGGERED
Definition: v4l2_base.h:51