From c75704bde332f8d0027da12b13c95975bc0a879e Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Mon, 14 Dec 2015 12:18:43 -0200 Subject: [PATCH] AP_HAL_Linux: VideoIn: follow coding style and minor fixes - No need to if/else if just returning - Sort includes - Fix missing space in log message - When closing the fd, set it to -1. It's better to later fail the operation than to operate on another random file descriptor - Add some spaces to improve readability - Use pragma once - Do not initialize members to zero, it's already the behavior for our custom allocator --- libraries/AP_HAL_Linux/VideoIn.cpp | 35 ++++++++++++++---------------- libraries/AP_HAL_Linux/VideoIn.h | 28 +++++++++++------------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/libraries/AP_HAL_Linux/VideoIn.cpp b/libraries/AP_HAL_Linux/VideoIn.cpp index 0ea6d3b651..fc3658c9e6 100644 --- a/libraries/AP_HAL_Linux/VideoIn.cpp +++ b/libraries/AP_HAL_Linux/VideoIn.cpp @@ -21,22 +21,21 @@ #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP #include "VideoIn.h" -#include -#include -#include -#include -#include #include -#include -#include +#include +#include #include +#include +#include +#include +#include #include #include #include #include #include - -#include +#include +#include extern const AP_HAL::HAL& hal; @@ -52,11 +51,7 @@ bool VideoIn::get_frame(Frame &frame) _streaming = true; } - if (_dequeue_frame(frame)) { - return true; - } else { - return false; - } + return _dequeue_frame(frame); } void VideoIn::put_frame(Frame &frame) @@ -88,9 +83,10 @@ bool VideoIn::open_device(const char *device_path, uint32_t memtype) } if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { - hal.console->printf("Error opening device %s: is not a video capture" - "device\n", device_path); + hal.console->printf("Error opening device %s: is not a video capture device\n", + device_path); close(_fd); + _fd = -1; return false; } @@ -112,8 +108,7 @@ bool VideoIn::allocate_buffers(uint32_t nbufs) ret = ioctl(_fd, VIDIOC_REQBUFS, &rb); if (ret < 0) { - printf("Unable to request buffers: %s (%d).\n", strerror(errno), - errno); + printf("Unable to request buffers: %s (%d).\n", strerror(errno), errno); return ret; } @@ -138,7 +133,7 @@ bool VideoIn::allocate_buffers(uint32_t nbufs) switch (_memtype) { case V4L2_MEMORY_MMAP: buffers[i].mem = mmap(0, buf.length, PROT_READ | PROT_WRITE, - MAP_SHARED, _fd, buf.m.offset); + MAP_SHARED, _fd, buf.m.offset); if (buffers[i].mem == MAP_FAILED) { hal.console->printf("Unable to map buffer %u: %s (%d)\n", i, strerror(errno), errno); @@ -249,6 +244,7 @@ void VideoIn::_queue_buffer(int index) { int ret; struct v4l2_buffer buf; + memset(&buf, 0, sizeof buf); buf.index = index; buf.type = V4L2_CAP_VIDEO_CAPTURE; @@ -257,6 +253,7 @@ void VideoIn::_queue_buffer(int index) if (_memtype == V4L2_MEMORY_USERPTR) { buf.m.userptr = (unsigned long) _buffers[index].mem; } + ret = ioctl(_fd, VIDIOC_QBUF, &buf); if (ret < 0) { hal.console->printf("Unable to queue buffer : %s (%d).\n", diff --git a/libraries/AP_HAL_Linux/VideoIn.h b/libraries/AP_HAL_Linux/VideoIn.h index d1014c6e37..1e451e2e92 100644 --- a/libraries/AP_HAL_Linux/VideoIn.h +++ b/libraries/AP_HAL_Linux/VideoIn.h @@ -12,8 +12,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#ifndef __VIDEOIN_H__ -#define __VIDEOIN_H__ +#pragma once #include "AP_HAL_Linux.h" #include @@ -25,15 +24,15 @@ struct buffer { class Linux::VideoIn { public: - /* This structure implements the fields of the v4l2_pix_format struct + /* This structure implements the fields of the v4l2_pix_format struct * that are considered useful for an optical flow application along * with the v4l2_buffer fields timestamp and sequence*/ class Frame { friend class VideoIn; public: - uint32_t timestamp = 0; - uint32_t sequence = 0; - void *data = NULL; + uint32_t timestamp; + uint32_t sequence; + void *data; private: uint32_t buf_index; }; @@ -56,14 +55,13 @@ private: bool _dequeue_frame(Frame &frame); uint32_t _timeval_to_us(struct timeval& tv); int _fd = -1; - struct buffer *_buffers = NULL; - unsigned int _nbufs = 0; - bool _streaming = false; - uint32_t _width = 0; - uint32_t _height = 0; - uint32_t _format = 0; - uint32_t _bytesperline = 0; - uint32_t _sizeimage = 0; + struct buffer *_buffers; + unsigned int _nbufs; + bool _streaming; + uint32_t _width; + uint32_t _height; + uint32_t _format; + uint32_t _bytesperline; + uint32_t _sizeimage; uint32_t _memtype = V4L2_MEMORY_MMAP; }; -#endif