Valkka  1.6.1
OpenSource Video Management
usbthread.h
Go to the documentation of this file.
1 #ifndef usbthread_HEADER_GUARD
2 #define usbthread_HEADER_GUARD
3 /*
4  * usbthread.h : USB Camera control and streaming
5  *
6  * (c) Copyright 2017-2024 Sampsa Riikonen
7  *
8  * Authors: Sampsa Riikonen <sampsa.riikonen@iki.fi>
9  *
10  * This file is part of the Valkka library.
11  *
12  * Valkka is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as
14  * published by the Free Software Foundation, either version 3 of the
15  * License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>
24  *
25  */
26 
37 #include "common.h"
38 #include "framefilter.h"
39 #include "thread.h"
40 
41 using namespace std::chrono_literals;
42 using std::this_thread::sleep_for;
43 
44 #define CLEAR(x) memset(&(x), 0, sizeof(x))
45 
46 /* TODO
47 
48 usbthread has a list of devices ..
49 each device has :
50  - a file handle that can be used with select
51  - out_frame that can be written to an output framefilter .. should be valid after select has been triggered
52  - can be opened and closed
53 
54  - at thread backend, when incoming request is executed internally, for error messages, could use the python callback
55 
56  - could have a std::map<int, Device> (maps fd to device)
57 
58 
59 base class: Device
60 derived class: v4LDevice
61 
62 */
63 
64 
68 struct USBCameraConnectionContext { // <pyapi>
69  USBCameraConnectionContext(const char *device, SlotNumber slot, FrameFilter *f) : // <pyapi>
70  device(device), slot(slot), framefilter(f), width(N720.width), height(N720.height), time_correction(TimeCorrectionType::smart) {}; // <pyapi>
71  USBCameraConnectionContext() : time_correction(TimeCorrectionType::smart) {}; // <pyapi>
72  std::string device; // <pyapi>
74  SlotNumber slot; // <pyapi>
76  FrameFilter* framefilter; // <pyapi>
77  int width; // <pyapi>
78  int height; // <pyapi>
81  // TODO: format, fps, etc. // <pyapi>
82 }; // <pyapi>
83 
84 
87 enum class USBDeviceSignal {
88  none,
89  exit,
90  register_camera_stream,
91  deregister_camera_stream,
92  play_camera_stream,
93  stop_camera_stream
94 };
95 
96 
101  USBCameraConnectionContext camera_connection_ctx;
102 };
103 
104 
105 class USBDevice {
106 
107 public:
108  USBDevice(FrameFilter *framefilter);
109  virtual ~USBDevice();
110 
111 protected:
112  int fd;
116  bool playing;
117 
118 public: // getters
119  int getFd() {return fd;}
120  virtual bool isPlaying() {return this->playing;}
121 
122 
123 public:
124  void setupFrame(SetupFrame frame);
125  virtual void open_();
126  virtual void close_();
127  virtual int pull();
128  virtual void play();
129  virtual void stop();
130 };
131 
132 
133 
134 // at the python level, check this directory: /sys/class/video4linux
135 // .. that directory has video0/ video1/ .. with file "name", etc.
136 // .. /dev/video0 has the actualy device for reading
137 
138 int xioctl(int fh, int request, void *arg);
139 
143  none =0,
146  not_read =3,
147  ok_open =4,
148  not_v4l2 =5,
153  not_ptr =10,
154  not_map =11,
155  not_on =12,
156  ok =13
157 };
158 
159 
160 class V4LDevice : public USBDevice {
161 
162 public:
163 
174  ~V4LDevice();
175 
176 protected:
177  USBCameraConnectionContext camera_ctx;
179  struct v4l2_buffer buf;
180  std::vector<BasicFrame*> ring_buffer;
181  SetupFrame setup_frame;
182  static const int n_ring_buffer = 5;
183 
184  FrameFilter* timestampfilter;
185  FrameFilter* inputfilter;
186 
187 
188 public: // getters
189  const v4l_status getStatus() {return this->status;}
190  const std::string getName() {return this->camera_ctx.device;}
191 
192 public: // virtual redefined
193  virtual bool isPlaying();
194  virtual void open_();
195  virtual void close_();
196  virtual int pull();
197  virtual void play();
198  virtual void stop();
199 
200 };
201 
202 
203 
204 class USBDeviceThread : public Thread { // <pyapi>
205 
206 public: // <pyapi>
207  USBDeviceThread(const char *name); // <pyapi>
208  ~USBDeviceThread(); // <pyapi>
209 
210 // protected: // no frame input into this thread
211  // FrameFifo infifo; ///< Incoming frames are read from here
212  // FifoFrameFilter infilter; ///< Write incoming frames here
213  // BlockingFifoFrameFilter infilter_block; ///< Incoming frames can also be written here. If stack runs out of frames, writing will block
214 
215 protected: // frame output from this thread
216  std::map<SlotNumber, USBDevice*> slots_;
217 
218 protected: // Thread member redefinitions
219  std::deque<USBDeviceSignalContext> signal_fifo;
220 
221 public: // redefined virtual functions
222  void run();
223  void preRun();
224  void postRun();
225  void sendSignal(USBDeviceSignalContext signal_ctx);
226 
227 protected:
228  void handleSignal(USBDeviceSignalContext &signal_ctx);
229  void handleSignals();
230 
231 protected:
232  //void registerCameraStream (USBCameraConnectionContext &ctx);
233  //void deRegisterCameraStream (USBCameraConnectionContext &ctx);
234  void playCameraStream (USBCameraConnectionContext &ctx);
235  void stopCameraStream (USBCameraConnectionContext &ctx);
236 
237 
238 // public API section
239 public: // <pyapi>
240  //void registerCameraStreamCall (USBCameraConnectionContext ctx); // <pyapi>
241  //void deRegisterCameraStreamCall (USBCameraConnectionContext ctx); // <pyapi>
242  void playCameraStreamCall (USBCameraConnectionContext ctx); // <pyapi>
243  void stopCameraStreamCall (USBCameraConnectionContext ctx); // <pyapi>
244  void requestStopCall(); // <pyapi>
245 }; // <pyapi>
246 
247 
248 
249 
250 
251 
252 
253 #endif
Custom payload Frame.
Definition: frame.h:166
The mother class of all frame filters! FrameFilters are used to create "filter chains".
Definition: framefilter.h:44
Setup frame.
Definition: frame.h:277
A class for multithreading with a signaling system.
Definition: thread.h:87
Definition: usbthread.h:204
std::deque< USBDeviceSignalContext > signal_fifo
Redefinition of signal fifo.
Definition: usbthread.h:219
std::map< SlotNumber, USBDevice * > slots_
Devices are organized in slots.
Definition: usbthread.h:216
Definition: usbthread.h:105
SetupFrame setupframe
This frame is used to send subsession information.
Definition: usbthread.h:114
bool playing
Is currently streaming or not.
Definition: usbthread.h:116
BasicFrame basicframe
Data is being copied into this frame.
Definition: usbthread.h:115
FrameFilter * framefilter
Output FrameFilter.
Definition: usbthread.h:113
Definition: usbthread.h:160
v4l_status status
State of the device.
Definition: usbthread.h:178
List of common header files.
TimeCorrectionType
Methods to correct frame timestamps.
Definition: frame.h:87
Definition of FrameFilter and derived classes for various purposes.
@ signal
signal to AVThread or OpenGLThread. Also custom signals to custom Threads
Parameters for connecting to a usb camera.
Definition: usbthread.h:68
FrameFilter * framefilter
Frames are feeded into this FrameFilter.
Definition: usbthread.h:76
SlotNumber slot
A unique stream slot that identifies this stream.
Definition: usbthread.h:74
TimeCorrectionType time_correction
How to perform frame timestamp correction.
Definition: usbthread.h:80
Redefinition of characteristic signal contexts (info that goes with the signal)
Definition: usbthread.h:99
Base class for multithreading.
USBDeviceSignal
Signals used by USBDeviceThread.
Definition: usbthread.h:87
v4l_status
Different stages of v4l2 device initialization.
Definition: usbthread.h:142
@ not_on
could not turn stream on
Definition: usbthread.h:155
@ not_found
file not found
Definition: usbthread.h:144
@ not_v4l2
not v4l2 device
Definition: usbthread.h:148
@ not_ptr
does not support user pointers
Definition: usbthread.h:153
@ not_read
could not read device
Definition: usbthread.h:146
@ not_device
not device file
Definition: usbthread.h:145
@ ok_format
format is ok
Definition: usbthread.h:152
@ not_stream
does not support streaming
Definition: usbthread.h:150
@ not_format
device could not satisfy the demanded format
Definition: usbthread.h:151
@ not_map
could not communicate pointers with the drivers
Definition: usbthread.h:154
@ ok_open
device opened
Definition: usbthread.h:147
@ none
undefined (initial value)
Definition: usbthread.h:143
@ not_video_cap
not video capture devices
Definition: usbthread.h:149
@ ok
stream is playing allright
Definition: usbthread.h:156