JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
CameraDevice.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #pragma once
19 
21 #include <jevois/Image/RawImage.H>
22 
23 #include <linux/videodev2.h>
24 #include <mutex>
25 #include <future>
26 #include <atomic>
27 
28 namespace jevois
29 {
30  //! Accessory class to hold all the data associated with a V4L2 camera device
31  /*! This class is used internally by jevois::Camera and is of little use to end users. \ingroup core */
33  {
34  public:
35  //! Constructor opens the device, checks its capabilities, starts run thread
36  /*! When dummy is true, we will just dequeue and requeue the buffers with no processing. */
37  CameraDevice(std::string const & devname, unsigned int const nbufs, bool dummy);
38 
39  //! Destructor frees all buffers and closes the device
40  ~CameraDevice();
41 
42  //! Get our fd; used by Camera to access controls
43  int getFd() const;
44 
45  //! Start streaming
46  void streamOn();
47 
48  //! Abort streaming
49  void abortStream();
50 
51  //! Stop streaming
52  void streamOff();
53 
54  //! Get the next captured buffer
55  void get(RawImage & img);
56 
57  //! Indicate that user processing is done with an image previously obtained via get()
58  void done(RawImage & img);
59 
60  //! Set the video format and frame rate
61  void setFormat(unsigned int const fmt, unsigned int const capw, unsigned int const caph, float const fps,
62  unsigned int const cropw, unsigned int const croph, int preset = -1);
63 
64  private:
65  std::string const itsDevName; //!< Our device or movie file name
66  unsigned int const itsNbufs; //!< Our number of buffers
67 
68  int itsFd = -1; // file descriptor we use to grab frames
69  bool itsMplane = false; // true if device uses multiplane V4L2 API
70  VideoBuffers * itsBuffers; // video buffers
71  struct v4l2_format itsFormat { }; // capture format
72 
73  std::atomic<bool> itsStreaming;
74  std::future<void> itsRunFuture;
75  bool itsFormatOk;
76  std::atomic<bool> itsRunning;
77 
78  mutable std::condition_variable_any itsOutputCondVar;
79  mutable std::timed_mutex itsOutputMtx;
80  RawImage itsOutputImage;
81  RawImage itsConvertedOutputImage;
82  std::vector<size_t> itsDoneIdx;
83  float itsFps = 0.0F;
84 
85  mutable std::timed_mutex itsMtx;
86 
87  void run();
88  };
89 
90 } // namespace jevois
jevois::CameraDevice::CameraDevice
CameraDevice(std::string const &devname, unsigned int const nbufs, bool dummy)
Constructor opens the device, checks its capabilities, starts run thread.
Definition: CameraDevice.C:76
jevois::CameraDevice::done
void done(RawImage &img)
Indicate that user processing is done with an image previously obtained via get()
Definition: CameraDevice.C:481
RawImage.H
jevois::CameraDevice::setFormat
void setFormat(unsigned int const fmt, unsigned int const capw, unsigned int const caph, float const fps, unsigned int const cropw, unsigned int const croph, int preset=-1)
Set the video format and frame rate.
Definition: CameraDevice.C:496
jevois::CameraDevice::abortStream
void abortStream()
Abort streaming.
Definition: CameraDevice.C:371
jevois::RawImage
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
jevois::CameraDevice::get
void get(RawImage &img)
Get the next captured buffer.
Definition: CameraDevice.C:428
jevois::CameraDevice::getFd
int getFd() const
Get our fd; used by Camera to access controls.
Definition: CameraDevice.C:195
jevois
Definition: Concepts.dox:1
jevois::CameraDevice::streamOff
void streamOff()
Stop streaming.
Definition: CameraDevice.C:384
jevois::CameraDevice::~CameraDevice
~CameraDevice()
Destructor frees all buffers and closes the device.
Definition: CameraDevice.C:172
jevois::CameraDevice
Accessory class to hold all the data associated with a V4L2 camera device.
Definition: CameraDevice.H:32
VideoBuffers.H
jevois::CameraDevice::streamOn
void streamOn()
Start streaming.
Definition: CameraDevice.C:320
jevois::VideoBuffers
Collection of buffers for V4L2 video frames (Camera or Gadget) with hooks to the MMAP'd areas.
Definition: VideoBuffers.H:42