|
1 | 1 |
|
2 | 2 | // STL includes |
| 3 | +#include <algorithm> |
3 | 4 | #include <cassert> |
4 | 5 | #include <iostream> |
5 | 6 |
|
|
14 | 15 | // Local includes |
15 | 16 | #include "AmlogicGrabber.h" |
16 | 17 |
|
17 | | -// Flags copied from 'linux/amlogic/amports/amvideocap.h' at https://github.com/codesnake/linux-amlogic/ |
| 18 | +// Flags copied from 'include/linux/amlogic/amports/amvideocap.h' at https://github.com/codesnake/linux-amlogic |
18 | 19 | #define AMVIDEOCAP_IOC_MAGIC 'V' |
19 | | -#define AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH _IOW(AMVIDEOCAP_IOC_MAGIC, 0x02, int) |
20 | | -#define AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT _IOW(AMVIDEOCAP_IOC_MAGIC, 0x03, int) |
| 20 | +#define AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH _IOW(AMVIDEOCAP_IOC_MAGIC, 0x02, int) |
| 21 | +#define AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT _IOW(AMVIDEOCAP_IOC_MAGIC, 0x03, int) |
| 22 | + |
| 23 | +// Flags copied from 'include/linux/amlogic/amports/amvstream.h' at https://github.com/codesnake/linux-amlogic |
| 24 | +#define AMSTREAM_IOC_MAGIC 'S' |
| 25 | +#define AMSTREAM_IOC_GET_VIDEO_DISABLE _IOR(AMSTREAM_IOC_MAGIC, 0x48, unsigned long) |
21 | 26 |
|
22 | 27 | AmlogicGrabber::AmlogicGrabber(const unsigned width, const unsigned height) : |
23 | | - _width(width), |
24 | | - _height(height), |
| 28 | + // Minimum required width or height is 160 |
| 29 | + _width(std::max(160u, width)), |
| 30 | + _height(std::max(160u, height)), |
25 | 31 | _amlogicCaptureDev(-1) |
26 | 32 | { |
| 33 | + std::cout << "[" << __PRETTY_FUNCTION__ << "] constructed(" << _width << "x" << _height << ")" << std::endl; |
27 | 34 | } |
28 | 35 |
|
29 | 36 | AmlogicGrabber::~AmlogicGrabber() |
@@ -54,40 +61,90 @@ void AmlogicGrabber::setVideoMode(const VideoMode videoMode) |
54 | 61 | } |
55 | 62 | } |
56 | 63 |
|
57 | | -void AmlogicGrabber::grabFrame(Image<ColorRgb> & image) |
| 64 | +bool AmlogicGrabber::isVideoPlaying() |
| 65 | +{ |
| 66 | + const std::string videoDevice = "/dev/amvideo"; |
| 67 | + |
| 68 | + // Open the video device |
| 69 | + int video_fd = open(videoDevice.c_str(), O_RDONLY); |
| 70 | + if (video_fd < 0) |
| 71 | + { |
| 72 | + std::cerr << "Failed to open video device(" << videoDevice << "): " << strerror(errno) << std::endl; |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + // Check the video disabled flag |
| 77 | + int videoDisabled; |
| 78 | + if (ioctl(video_fd, AMSTREAM_IOC_GET_VIDEO_DISABLE, &videoDisabled) == -1) |
| 79 | + { |
| 80 | + std::cerr << "Failed to retrieve video state from device: " << strerror(errno) << std::endl; |
| 81 | + close(video_fd); |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + // Make sure to close the device after use |
| 86 | + close(video_fd); |
| 87 | + |
| 88 | + return videoDisabled == 0; |
| 89 | +} |
| 90 | + |
| 91 | +int AmlogicGrabber::grabFrame(Image<ColorBgr> & image) |
58 | 92 | { |
59 | 93 | // resize the given image if needed |
60 | 94 | if (image.width() != _width || image.height() != _height) |
61 | 95 | { |
62 | 96 | image.resize(_width, _height); |
63 | 97 | } |
64 | | - |
65 | | - _amlogicCaptureDev = open("/dev/amvideocap0", O_RDONLY, 0); |
| 98 | + |
| 99 | + // Make sure video is playing, else there is nothing to grab |
| 100 | + if (!isVideoPlaying()) |
| 101 | + { |
| 102 | + return -1; |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + // If the device is not open, attempt to open it |
66 | 107 | if (_amlogicCaptureDev == -1) |
67 | 108 | { |
68 | | - std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to open the AMLOGIC device (" << errno << ")" << std::endl; |
69 | | - return; |
| 109 | + _amlogicCaptureDev = open("/dev/amvideocap0", O_RDONLY, 0); |
| 110 | + |
| 111 | + // If the device is still not open, there is something wrong |
| 112 | + if (_amlogicCaptureDev == -1) |
| 113 | + { |
| 114 | + std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to open the AMLOGIC device (" << errno << "): " << strerror(errno) << std::endl; |
| 115 | + return -1; |
| 116 | + } |
70 | 117 | } |
71 | | - |
72 | | - if (ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH, _width) == -1 || |
73 | | - ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT, _height) == -1) |
| 118 | + |
| 119 | + |
| 120 | + if (ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH, _width) == -1 || |
| 121 | + ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT, _height) == -1) |
74 | 122 | { |
75 | 123 | // Failed to configure frame width |
76 | | - std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to configure capture size (" << errno << ")" << std::endl; |
77 | | - return; |
| 124 | + std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to configure capture size (" << errno << "): " << strerror(errno) << std::endl; |
| 125 | + return -1; |
78 | 126 | } |
79 | | - |
80 | | - std::cout << "AMLOGIC grabber created (size " << _width << "x" << _height << ")" << std::endl; |
| 127 | + |
81 | 128 | // Read the snapshot into the memory |
82 | 129 | void * image_ptr = image.memptr(); |
83 | | - const size_t bytesToRead = _width * _height * sizeof(ColorRgb); |
84 | | - const size_t bytesRead = pread(_amlogicCaptureDev, image_ptr, bytesToRead, 0); |
85 | | - if (bytesToRead != bytesRead) |
| 130 | + const ssize_t bytesToRead = _width * _height * sizeof(ColorBgr); |
| 131 | + |
| 132 | + const ssize_t bytesRead = pread(_amlogicCaptureDev, image_ptr, bytesToRead, 0); |
| 133 | + if (bytesRead == -1) |
| 134 | + { |
| 135 | + std::cerr << "[" << __PRETTY_FUNCTION__ << "] Read of device failed (erno=" << errno << "): " << strerror(errno) << std::endl; |
| 136 | + return -1; |
| 137 | + } |
| 138 | + else if (bytesToRead != bytesRead) |
86 | 139 | { |
87 | 140 | // Read of snapshot failed |
88 | 141 | std::cerr << "[" << __PRETTY_FUNCTION__ << "] Capture failed to grab entire image [bytesToRead(" << bytesToRead << ") != bytesRead(" << bytesRead << ")]" << std::endl; |
| 142 | + return -1; |
89 | 143 | } |
90 | | - |
| 144 | + |
| 145 | + // For now we always close the device again |
91 | 146 | close(_amlogicCaptureDev); |
92 | 147 | _amlogicCaptureDev = -1; |
| 148 | + |
| 149 | + return 0; |
93 | 150 | } |
0 commit comments