• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * v4l2_device.h - v4l2 device
3  *
4  *  Copyright (c) 2014-2015 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Wind Yuan <feng.yuan@intel.com>
19  */
20 
21 #ifndef XCAM_V4L2_DEVICE_H
22 #define XCAM_V4L2_DEVICE_H
23 
24 #include <xcam_std.h>
25 #include <linux/videodev2.h>
26 #include <list>
27 #include <vector>
28 
29 extern "C" {
30     struct v4l2_event;
31     struct v4l2_format;
32     struct v4l2_fmtdesc;
33     struct v4l2_frmsizeenum;
34 }
35 
36 namespace XCam {
37 
38 class V4l2Buffer;
39 
40 class V4l2Device {
41     friend class V4l2BufferProxy;
42     typedef std::vector<SmartPtr<V4l2Buffer>> BufferPool;
43 
44 public:
45     V4l2Device (const char *name = NULL);
46     virtual ~V4l2Device ();
47 
48     // before device open
49     bool set_device_name (const char *name);
50     bool set_sensor_id (int id);
51     bool set_capture_mode (uint32_t capture_mode);
52 
get_fd()53     int get_fd () const {
54         return _fd;
55     }
get_device_name()56     const char *get_device_name () const {
57         return _name;
58     }
is_opened()59     bool is_opened () const    {
60         return (_fd != -1);
61     }
is_activated()62     bool is_activated () const {
63         return _active;
64     }
65 
66     // set_mem_type must before set_format
67     bool set_mem_type (enum v4l2_memory type);
get_mem_type()68     enum v4l2_memory get_mem_type () const {
69         return _memory_type;
70     }
get_capture_buf_type()71     enum v4l2_buf_type get_capture_buf_type () const {
72         return _capture_buf_type;
73     }
get_size(uint32_t & width,uint32_t & height)74     void get_size (uint32_t &width, uint32_t &height) const {
75         width = _format.fmt.pix.width;
76         height = _format.fmt.pix.height;
77     }
get_pixel_format()78     uint32_t get_pixel_format () const {
79         return _format.fmt.pix.pixelformat;
80     }
81 
82     bool set_buffer_count (uint32_t buf_count);
83 
84     // set_framerate must before set_format
85     bool set_framerate (uint32_t n, uint32_t d);
86     void get_framerate (uint32_t &n, uint32_t &d);
87 
88     XCamReturn open ();
89     XCamReturn close ();
90     // set_format
91     XCamReturn get_format (struct v4l2_format &format);
92     XCamReturn set_format (struct v4l2_format &format);
93     XCamReturn set_format (
94         uint32_t width, uint32_t height, uint32_t pixelformat,
95         enum v4l2_field field = V4L2_FIELD_NONE, uint32_t bytes_perline = 0);
96 
97     std::list<struct v4l2_fmtdesc> enum_formats ();
98 
99     virtual XCamReturn start ();
100     virtual XCamReturn stop ();
101 
102     int poll_event (int timeout_msec);
103     XCamReturn dequeue_buffer (SmartPtr<V4l2Buffer> &buf);
104     XCamReturn queue_buffer (SmartPtr<V4l2Buffer> &buf);
105 
106     // use as less as possible
107     virtual int io_control (int cmd, void *arg);
108 
109 protected:
110 
111     //virtual functions, handle private actions on set_format
112     virtual XCamReturn pre_set_format (struct v4l2_format &format);
113     virtual XCamReturn post_set_format (struct v4l2_format &format);
114     virtual XCamReturn allocate_buffer (
115         SmartPtr<V4l2Buffer> &buf,
116         const struct v4l2_format &format,
117         const uint32_t index);
118 
119 private:
120     XCamReturn request_buffer ();
121     XCamReturn init_buffer_pool ();
122     XCamReturn fini_buffer_pool ();
123 
124     XCAM_DEAD_COPY (V4l2Device);
125 
126 protected:
127     char               *_name;
128     int                 _fd;
129     int32_t             _sensor_id;
130     uint32_t            _capture_mode;
131     enum v4l2_buf_type  _capture_buf_type;
132     enum v4l2_memory    _memory_type;
133 
134     struct v4l2_format  _format;
135     uint32_t            _fps_n;
136     uint32_t            _fps_d;
137 
138     bool                _active;
139 
140     // buffer pool
141     BufferPool          _buf_pool;
142     uint32_t            _buf_count;
143 
144     XCamReturn buffer_new();
145     XCamReturn buffer_del();
146 };
147 
148 class V4l2SubDevice
149     : public V4l2Device
150 {
151 public:
152     explicit V4l2SubDevice (const char *name = NULL);
153 
154     XCamReturn subscribe_event (int event);
155     XCamReturn unsubscribe_event (int event);
156     XCamReturn dequeue_event (struct v4l2_event &event);
157 
158     virtual XCamReturn start ();
159     virtual XCamReturn stop ();
160 
161 private:
162     XCAM_DEAD_COPY (V4l2SubDevice);
163 };
164 
165 };
166 #endif // XCAM_V4L2_DEVICE_H
167 
168