Visual Servoing Platform  version 3.6.1 under development (2024-03-29)
tutorial-grabber-v4l2-threaded.cpp
1 #include <iostream>
4 #include <visp3/core/vpImageConvert.h>
5 #include <visp3/core/vpTime.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/sensor/vpV4l2Grabber.h>
8 
9 #if defined(VISP_HAVE_V4L2) && defined(VISP_HAVE_THREADS)
10 
11 #include <thread>
12 #include <mutex>
13 
14 // Shared vars
15 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
17 
19 void captureFunction(vpV4l2Grabber &cap, std::mutex &mutex_capture, vpImage<unsigned char> &frame, t_CaptureState &capture_state)
20 {
22  bool stop_capture_ = false;
23 
24  cap.open(frame_);
25 
26  double start_time = vpTime::measureTimeSecond();
27  while ((vpTime::measureTimeSecond() - start_time) < 30 && !stop_capture_) {
28  // Capture in progress
29  cap.acquire(frame_); // get a new frame from camera
30 
31  // Update shared data
32  {
33  std::lock_guard<std::mutex> lock(mutex_capture);
34  if (capture_state == capture_stopped)
35  stop_capture_ = true;
36  else
37  capture_state = capture_started;
38  frame = frame_;
39  }
40  }
41 
42  {
43  std::lock_guard<std::mutex> lock(mutex_capture);
44  capture_state = capture_stopped;
45  }
46  std::cout << "End of capture thread" << std::endl;
47 }
49 
51 void displayFunction(std::mutex &mutex_capture, vpImage<unsigned char> &frame, t_CaptureState &capture_state)
52 {
54 
55  t_CaptureState capture_state_;
56  bool display_initialized_ = false;
57 #if defined(VISP_HAVE_X11)
58  vpDisplayX *d_ = nullptr;
59 #endif
60 
61  do {
62  mutex_capture.lock();
63  capture_state_ = capture_state;
64  mutex_capture.unlock();
65 
66  // Check if a frame is available
67  if (capture_state_ == capture_started) {
68  // Create a copy of the captured frame
69  {
70  std::lock_guard<std::mutex> lock(mutex_capture);
71  I_ = frame;
72  }
73 
74  // Check if we need to initialize the display with the first frame
75  if (!display_initialized_) {
76 // Initialize the display
77 #if defined(VISP_HAVE_X11)
78  d_ = new vpDisplayX(I_);
79  display_initialized_ = true;
80 #endif
81  }
82 
83  // Display the image
85 
86  // Trigger end of acquisition with a mouse click
87  vpDisplay::displayText(I_, 10, 10, "Click to exit...", vpColor::red);
88  if (vpDisplay::getClick(I_, false)) {
89  std::lock_guard<std::mutex> lock(mutex_capture);
90  capture_state = capture_stopped;
91  }
92 
93  // Update the display
94  vpDisplay::flush(I_);
95  }
96  else {
97  vpTime::wait(2); // Sleep 2ms
98  }
99  } while (capture_state_ != capture_stopped);
100 
101 #if defined(VISP_HAVE_X11)
102  delete d_;
103 #endif
104 
105  std::cout << "End of display thread" << std::endl;
106 }
108 
110 int main(int argc, const char *argv[])
111 {
112  unsigned int opt_device = 0; // Default is opening /dev/video0
113  unsigned int opt_scale = 2; // Default value is 2 in the constructor. Turn
114  // it to 1 to avoid subsampling
115 
116  // Command line options
117  for (int i = 0; i < argc; i++) {
118  if (std::string(argv[i]) == "--camera_device")
119  opt_device = (unsigned int)atoi(argv[i + 1]);
120  else if (std::string(argv[i]) == "--scale")
121  opt_scale = (unsigned int)atoi(argv[i + 1]);
122  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "--h") {
123  std::cout << "Usage: " << argv[0]
124  << " [--camera_device <camera device (default: 0)>] [--scale <subsampling factor>]"
125  << " [--help] [-h]" << std::endl;
126  return EXIT_SUCCESS;
127  }
128  }
129 
130  // Instantiate the grabber
131  vpV4l2Grabber g;
132  std::ostringstream device;
133  device << "/dev/video" << opt_device;
134  g.setDevice(device.str());
135  g.setScale(opt_scale);
136 
138  std::mutex mutex_capture;
139  t_CaptureState capture_state = capture_waiting;
140 
141  // Start the threads
142  std::thread thread_capture(&captureFunction, std::ref(g), std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
143  std::thread thread_display(&displayFunction, std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
144 
145  // Wait until thread ends up
146  thread_capture.join();
147  thread_display.join();
148 
149  return EXIT_SUCCESS;
150 }
152 
153 #else
154 int main()
155 {
156 #ifndef VISP_HAVE_V4L2
157  std::cout << "You should enable V4L2 to make this example working..." << std::endl;
158 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
159  std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
160 #else
161  std::cout << "Multi-threading seems not supported on this platform" << std::endl;
162 #endif
163 }
164 
165 #endif
static const vpColor red
Definition: vpColor.h:211
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeSecond()