summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/indi/webcam/v4l2_base.h
blob: 668c77c9ec16e99678d6beb5a4a0779ec8e0c1e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
    Copyright (C) 2005 by Jasem Mutlaq

    Based on V4L 2 Example
    http://v4l2spec.bytesex.org/spec-single/v4l2.html#CAPTURE-EXAMPLE

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#ifndef V4L2_BASE_H
#define V4L2_BASE_H

#include <stdio.h>
#include <stdlib.h>
#include "videodev2.h"
#include "../eventloop.h"
#include "../indidevapi.h"

#define VIDEO_COMPRESSION_LEVEL		4

class V4L2_Base
{
  public:
   V4L2_Base();
   virtual ~V4L2_Base();

   typedef enum { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR } io_method;

   struct buffer
   {
        void *                  start;
        size_t                  length;
   };

  /* Connection */
  virtual int connectCam(const char * devpath, char *errmsg, int pixelFormat = V4L2_PIX_FMT_YUYV , int width = 160, int height = 120);
  virtual void disconnectCam();
  char * getDeviceName();

  /* Image settings */
  int  getBrightness();
  int  getContrast();
  int  getColor();
  int  getHue();
  int  getWhiteness();
  void setContrast(int val);
  void setBrightness(int val);
  void setColor(int val);
  void setHue(int val);
  void setWhiteness(int val);

  /* Updates */
  void callFrame(void *p);
  void setPictureSettings();
  void getPictureSettings();

  /* Image Size */
  int getWidth();
  int getHeight();
  virtual int setSize(int x, int y);
  virtual void getMaxMinSize(int & x_max, int & y_max, int & x_min, int & y_min);

  /* Frame rate */
  void setFPS(int fps);
  int  getFPS();

  void allocBuffers();
  unsigned char * getY();
  unsigned char * getU();
  unsigned char * getV();
  unsigned char * getColorBuffer();

  void registerCallback(WPF *fp, void *ud);

  int start_capturing(char *errmsg);
  int stop_capturing(char *errmsg);
  static void newFrame(int fd, void *p);
  
  void enumerate_ctrl (void);
  void enumerate_menu (void);
  int  queryINTControls(INumberVectorProperty *nvp);
  int  setINTControl(unsigned int ctrl_id, double new_value, char *errmsg);

  int  query_ctrl(unsigned int ctrl_id, double & ctrl_min, double & ctrl_max, double & ctrl_step, double & ctrl_value, char *errmsg);

  protected:

  int xioctl(int fd, int request, void *arg);
  int read_frame(char *errsg);
  int uninit_device(char *errmsg);
  int open_device(const char *devpath, char *errmsg);
  int init_device(char *errmsg, int pixelFormat , int width, int height); 
  int init_mmap(char *errmsg);
  int errno_exit(const char *s, char *errmsg);
  
  void close_device(void);
  void init_userp(unsigned int buffer_size);
  void init_read(unsigned int buffer_size);

  void findMinMax();
  
  struct v4l2_capability cap;
  struct v4l2_cropcap cropcap;
  struct v4l2_crop crop;
  struct v4l2_format fmt;

  struct v4l2_queryctrl queryctrl;
  struct v4l2_querymenu querymenu;

  WPF *callback;
  void *uptr;
  char          dev_name[64];
  io_method	io;
  int           fd;
  struct buffer *buffers;
  unsigned int  n_buffers;

  bool		dropFrame;

  
  int frameRate;
  int  xmax, xmin, ymax, ymin;
  int  selectCallBackID;
  unsigned char * YBuf,*UBuf,*VBuf, *colorBuffer, *rgb24_buffer;

};
   
#endif