summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/vcap/vcap.hpp
blob: 9b222d76799f2f6b459305bf3a42c4d36e4b04f8 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2016 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#pragma once

#include <string>
#include <iostream>

#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <jpeglib.h>
#include <linux/videodev2.h>

#define VCAP_DEFAULT_VIDEODEV "/dev/video0"
#define VCAP_DEFAULT_OUTPUTFILE "vcap.jpg"
#define VCAP_DEFAULT_WIDTH 640
#define VCAP_DEFAULT_HEIGHT 480
#define VCAP_DEFAULT_JPEG_QUALITY 99

namespace upm {
    /**
     * @brief Take a snapshot from a video camera and save as a JPEG
     * @defgroup vcap libupm-vcap
     * @ingroup video
     */

    /**
     * @library vcap
     * @sensor vcap
     * @comname Video Capture
     * @type video
     *
     * @brief API for the Video Capture driver
     *
     * This UPM module captures a still frame from a Linux V4L device,
     * such as a USB webcam, and and then allows you to save it as a
     * JPEG image into a file.
     *
     * The camera and driver in use must support streaming, mmap-able
     * buffers and must provide data in YUYV format.  This should
     * encompass most video cameras out there.  It has been tested
     * with a few off the shelf cameras without any problems.
     *
     * @snippet vcap.cxx Interesting
     */

  class VCAP {
  public:

    /**
     * VCAP object constructor
     *
     * @param videoDev The path to the video device, default is /dev/video0.
     */
    VCAP(std::string videoDev=VCAP_DEFAULT_VIDEODEV);

    /**
     * VCAP object destructor
     */
    ~VCAP();

    /**
     * Set the desired resolution of the output image.  Note, this is
     * a hint to the underlying video driver.  The video driver is
     * free to lower the specified resolution if the hardware cannot
     * support it.  You can use getHeight() and getWidth() after
     * calling this method to see what the video driver chose.
     *
     * @param width The desired width of the image.
     * @param width The desired height of the image.
     * @return true if the operation succeeded, false otherwise.
     */
    bool setResolution(int width, int height);

    /**
     * Capture an image from the camera.
     *
     * @return true if the operation succeeded, false otherwise.
     */
    bool captureImage();

    /**
     * Save the captured image (created with captureImage()) to a file
     * in JPEG format.  The file will be overwritten if it already
     * exists.
     *
     * @param filename The name of the file in which to store the image.
     * @return true if the operation succeeded, false otherwise.
     */
    bool saveImage(std::string filename=VCAP_DEFAULT_OUTPUTFILE);

    /**
     * Return the current width of the image.  You can use this method
     * to determine if the video driver downgraded it after a call to
     * setResolution().
     *
     * @return true Current width of capture.
     */
    int getWidth() const
    {
      return m_width;
    };

    /**
     * Return the current height of the image.  You can use this method
     * to determine if the video driver downgraded it after a call to
     * setResolution().
     *
     * @return true Current height of capture.
     */
    int getHeight() const
    {
      return m_height;
    };

    /**
     * Set the JPEG quality.
     *
     * @param quality A number between 0-100, with higher numbers
     * meaning higher quality. Numbers less than 0 will be clamped to
     * 0, numbers higher than 100 will be clamped to 100.
     */
    void setJPGQuality(unsigned int quality);

    /**
     * Get the current JPEG quality setting.
     *
     * @return the current JPEG quality setting.
     */
    int getJPGQuality() const
    {
      return m_jpgQuality;
    };

    /**
     * Enable or disable debugging output.
     *
     * @param enable true to enable debugging, false otherwise
     */
    void setDebug(bool enable)
    {
      m_debugging = enable;
    };
    
  protected:
    // open the device and check that it meats minimum requirements
    bool initVideoDevice();

    // make sure device is streamable, supports mmap and capture
    bool checkCapabilities();

    // read the mmapped buffer in YUYV format and create a jpeg image
    bool YUYV2JPEG(FILE *file);

    // buffer management
    bool allocBuffer();
    void releaseBuffer();

    // does the actual capture
    bool doCaptureImage();
    
  private:
    // internal ioctl
    int xioctl(int fd, int request, void* argp);
               
    std::string m_videoDevice;
    
    // our file descriptor to the video device
    int m_fd;

    // v4l info
    struct v4l2_capability m_caps;
    struct v4l2_format m_format;

    // our mmaped buffer
    unsigned char *m_buffer;
    size_t m_bufferLen;

    // the resolution and quality
    int m_width;
    int m_height;
    int m_jpgQuality;

    // at least one image captured with current settings?
    bool m_imageCaptured;

    // are we debugging?
    bool m_debugging;
  };
}