blob: 0e5f3afb27b4c61fe4e3e6aa73e6be8bf391373c [file] [log] [blame]
Eddie James21b177e2018-12-11 13:14:46 -06001#pragma once
2
3#include "ikvm_input.hpp"
4
Eddie James90d49582018-12-11 13:22:00 -06005#include <mutex>
Eddie James21b177e2018-12-11 13:14:46 -06006#include <string>
Eddie James90d49582018-12-11 13:22:00 -06007#include <vector>
Eddie James21b177e2018-12-11 13:14:46 -06008
9namespace ikvm
10{
11
12/*
13 * @class Video
14 * @brief Sets up the V4L2 video device and performs read operations
15 */
16class Video
17{
18 public:
19 /*
20 * @brief Constructs Video object
21 *
22 * @param[in] p - Path to the V4L2 video device
23 * @param[in] input - Reference to the Input object
24 * @param[in] fr - desired frame rate of the video
25 */
26 Video(const std::string& p, Input& input, int fr = 30);
27 ~Video();
28 Video(const Video&) = default;
29 Video& operator=(const Video&) = default;
30 Video(Video&&) = default;
31 Video& operator=(Video&&) = default;
32
33 /*
Eddie James90d49582018-12-11 13:22:00 -060034 * @brief Gets the video frame data
35 *
36 * @return Pointer to the video frame data
37 */
38 char* getData();
39 /* @brief Performs read to grab latest video frame */
40 void getFrame();
41 /*
42 * @brief Gets whether or not the video frame needs to be resized
43 *
44 * @return Boolean indicating if the frame needs to be resized
45 */
46 bool needsResize();
47 /* @brief Performs the resize and re-allocates framebuffer */
48 void resize();
49 /* @brief Starts streaming from the video device */
50 void start();
51 /* @brief Stops streaming from the video device */
52 void stop();
53
54 /*
55 * @brief Gets the desired video frame rate in frames per second
56 *
57 * @return Value of the desired frame rate
58 */
59 inline int getFrameRate() const
60 {
61 return frameRate;
62 }
63 /*
64 * @brief Gets the size of the video frame data
65 *
66 * @return Value of the size of the video frame data in bytes
67 */
68 inline size_t getFrameSize() const
69 {
70 return buffers[lastFrameIndex].payload;
71 }
72 /*
Eddie James21b177e2018-12-11 13:14:46 -060073 * @brief Gets the height of the video frame
74 *
75 * @return Value of the height of video frame in pixels
76 */
77 inline size_t getHeight() const
78 {
79 return height;
80 }
81 /*
82 * @brief Gets the width of the video frame
83 *
84 * @return Value of the width of video frame in pixels
85 */
86 inline size_t getWidth() const
87 {
88 return width;
89 }
90
Eddie James90d49582018-12-11 13:22:00 -060091 /* @brief Number of bits per component of a pixel */
92 static const int bitsPerSample;
93 /* @brief Number of bytes of storage for a pixel */
94 static const int bytesPerPixel;
95 /* @brief Number of components in a pixel (i.e. 3 for RGB pixel) */
96 static const int samplesPerPixel;
97
Eddie James21b177e2018-12-11 13:14:46 -060098 private:
Eddie James90d49582018-12-11 13:22:00 -060099 /*
100 * @struct Buffer
101 * @brief Store the address and size of frame data from streaming
102 * operations
103 */
104 struct Buffer
105 {
106 Buffer() : data(nullptr), queued(false), payload(0), size(0)
107 {
108 }
109 ~Buffer() = default;
110 Buffer(const Buffer&) = default;
111 Buffer& operator=(const Buffer&) = default;
112 Buffer(Buffer&&) = default;
113 Buffer& operator=(Buffer&&) = default;
114
115 void* data;
116 bool queued;
117 size_t payload;
118 size_t size;
119 };
120
121 /*
122 * @brief Boolean to indicate whether the resize was triggered during
123 * the open operation
124 */
125 bool resizeAfterOpen;
126 /* @brief File descriptor for the V4L2 video device */
127 int fd;
128 /* @brief Desired frame rate of video stream in frames per second */
129 int frameRate;
130 /* @brief Buffer index for the last video frame */
131 int lastFrameIndex;
Eddie James21b177e2018-12-11 13:14:46 -0600132 /* @brief Height in pixels of the video frame */
133 size_t height;
134 /* @brief Width in pixels of the video frame */
135 size_t width;
136 /* @brief Reference to the Input object */
137 Input& input;
138 /* @brief Path to the V4L2 video device */
139 const std::string path;
Eddie James90d49582018-12-11 13:22:00 -0600140 /* @brief Streaming buffer storage */
141 std::vector<Buffer> buffers;
Eddie James21b177e2018-12-11 13:14:46 -0600142};
143
144} // namespace ikvm