Ocean
VideoDevice.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_SYSTEM_USB_VIDEO_VIDEO_DEVICE_H
9 #define META_OCEAN_SYSTEM_USB_VIDEO_VIDEO_DEVICE_H
10 
14 
16 
17 #include "ocean/base/Memory.h"
18 
19 namespace Ocean
20 {
21 
22 namespace System
23 {
24 
25 namespace USB
26 {
27 
28 namespace Video
29 {
30 
31 // Forward declaration.
32 class VideoDevice;
33 
34 /**
35  * Definition of a shared pointer holding a VideoDevice.
36  * @see VideoDevice
37  * @ingroup systemusbvideo
38  */
39 using SharedVideoDevice = std::shared_ptr<VideoDevice>;
40 
41 /**
42  * This class implements a USB video device.
43  * The device allows to select individual stream configurations, to start a stream, and to receive samples from the stream.
44  * @ingroup systemusbvideo
45  */
46 class OCEAN_SYSTEM_USB_VIDEO_EXPORT VideoDevice : public Device
47 {
48  protected:
49 
50  // Forward declaration.
51  class PayloadHeader;
52 
53  public:
54 
55  /**
56  * Definition of individual USB UVC device stream types.
57  */
58  enum DeviceStreamType : uint32_t
59  {
60  /// An invalid stream type.
61  DST_INVALID = 0u,
62  /// A stream composed of uncompressed video frames.
64  /// A stream based on MJpeg.
66  /// A stream based on advanced compression like H.264 or H.265
67  DST_FRAME_BASED
68  };
69 
70  /**
71  * This class holds the data of a media sample.
72  */
73  class OCEAN_SYSTEM_USB_VIDEO_EXPORT Sample
74  {
75  friend class VideoDevice;
76 
77  public:
78 
79  /**
80  * Default constructor creating an invalid sample object.
81  */
82  Sample() = default;
83 
84  /**
85  * Creates a new sample associated with a specific stream configuration.
86  * @param capacity The maximal capacity of the sample, in bytes, with range [0, infinity)
87  * @param descriptorFormatIndex The video format index specifying the format descriptor of the stream delivering this sample, must be valid
88  * @param descriptorFrameIndex The video frame index specifying the frame descriptor of the stream delivering this sample, must be valid
89  * @param dwClockFrequency The clock frequency of the stream, in Hz, with range [1, infinity)
90  */
91  explicit Sample(const size_t capacity, const uint8_t descriptorFormatIndex, const uint8_t descriptorFrameIndex, const uint32_t dwClockFrequency);
92 
93  /**
94  * Returns the video format index specifying the format descriptor of the stream delivering this sample.
95  * @return The sample's format index
96  */
97  inline uint8_t descriptorFormatIndex() const;
98 
99  /**
100  * Returns the video frame index specifying the frame descriptor of the stream delivering this sample, must be valid.
101  * @return The sample's frame index
102  */
103  inline uint8_t descriptorFrameIndex() const;
104 
105  /**
106  * Determines the timestamp the sample was captured.
107  * @return The sample's capture timestamp
108  */
110 
111  /**
112  * Returns the buffer of this sample.
113  * @return The sample's buffer
114  */
115  inline const void* data() const;
116 
117  /**
118  * Returns the size of the sample.
119  * @return The sample's size, in bytes
120  */
121  inline size_t size() const;
122 
123  /**
124  * Returns whether this sample may contain errors (e.g., because there was a transmission error).
125  * @return True, if so
126  */
127  inline bool mayContainError() const;
128 
129  /**
130  * Returns a string with the relevant information of this sample.
131  * @return The string with the relevant information
132  */
133  std::string toString() const;
134 
135  protected:
136 
137  /**
138  * Appends a payload packet to the sample.
139  * @param payloadHeader The payload header of the packet
140  * @param data The actual payload data to append, must be valid
141  * @param size The size of the payload data to append, in bytes, with range [1, infinity)
142  * @return True, if succeeded
143  */
144  bool append(const PayloadHeader& payloadHeader, const void* data, const size_t size);
145 
146  /**
147  * Resets the sample so that it can be reused.
148  */
149  void reset();
150 
151  protected:
152 
153  /**
154  * Unwraps two timestamps while both are wrapped around 2^32.
155  * @param timestampA The first timestamp, with input range [0, 2^32-1] and output range [0, 2^33-1]
156  * @param timestampB The second timestamp, with input range [0, 2^32-1] and output range [0, 2^33-1]
157  * @return Which timestamp was unwrapped, 0 for timestampA, 1 for timestampB and -1 if no timestamp needed to be unwrapped
158  */
159  static int unwrapTimestamps(uint64_t& timestampA, uint64_t& timestampB);
160 
161  /**
162  * Returns the earlier timestamp of the two given timestamps while both timestamps are wrapped around 2^32.
163  * @param timestampA The first timestamp, with range [0, 2^31-1]
164  * @param timestampB The second timestamp, with range [0, 2^31-1]
165  * @return The earlier of both timestamps
166  */
167  static uint64_t earlierTimestamp(const uint64_t timestampA, const uint64_t timestampB);
168 
169  public:
170 
171  /// The payload buffer which can be appended with payload packets.
172  std::vector<uint8_t> buffer_;
173 
174  /// The current position inside the payload buffer, with range [0, data_.size() - 1].
175  size_t position_ = 0;
176 
177  /// True, if the sample may contain any error.
178  bool mayContainError_ = false;
179 
180  /// The device's timestamp when capturing began (taken from dwPresentationTime).
181  uint64_t captureDeviceTime_ = uint64_t(-1);
182 
183  /// Up to two device timestamps when the first and last payload packets were delivered/received (taken from scrSourceClock).
184  uint64_t payloadDeviceTimes_[2] = {uint64_t(-1), uint64_t(-1)};
185 
186  /// Up to two host/local unix timestamps when the first and last payload packets were received.
187  Timestamp payloadHostTimestamps_[2] = {Timestamp(false), Timestamp(false)};
188 
189  //// The index of the next device timestamps, with range [0, 1]
190  size_t nextDeviceTimeIndex_ = 0;
191 
192  /// True, to allow the buffer to be resized if incoming data does not fit into the buffer.
193  bool allowToResize_ = false;
194 
195  /// The video format index specifying the format descriptor of the stream delivering this sample.
196  uint8_t descriptorFormatIndex_ = 0u;
197 
198  /// The video frame index specifying the frame descriptor of the stream delivering this sample.
199  uint8_t descriptorFrameIndex_ = 0u;
200 
201  /// The dwClockFrequency field of the VideoProbe Control response in Hz (the frequency of captureDeviceTime_ and payloadDeviceTimes_), with range [1, infinity)
202  uint32_t dwClockFrequency_ = 0u;
203  };
204 
205  /**
206  * Definition of a shared pointer holding a Sample.
207  */
208  using SharedSample = std::shared_ptr<Sample>;
209 
210  /**
211  * Definition of a vector holding samples.
212  */
213  using Samples = std::vector<SharedSample>;
214 
215  /**
216  * Definition of a queue holding samples.
217  */
218  using SampleQueue = std::queue<SharedSample>;
219 
220  /// The maximal size of the sample queue before queued samples will be dropped.
221  static constexpr size_t maximalSampleQueueSize_ = 10;
222 
223  /**
224  * This class combines the video control interface functionality.
225  */
226  class OCEAN_SYSTEM_USB_VIDEO_EXPORT VideoControlInterface
227  {
228  friend class VideoDevice;
229 
230  public:
231 
232  /**
233  * Returns whether this interface object is valid
234  * @return True, if so
235  */
236  inline bool isValid() const;
237 
238  protected:
239 
240  /**
241  * Default constructor creating an invalid interface.
242  */
244 
245  /**
246  * Creates a new video control interface based on a given interface descriptor.
247  * @param interfaceDescriptor The descriptor of the interface based on which this object will be created
248  * @param usbDeviceHandle The handle to the USB device associated with given interface descriptor
249  */
250  VideoControlInterface(const libusb_interface_descriptor& interfaceDescriptor, libusb_device_handle* usbDeviceHandle);
251 
252  protected:
253 
254  /// The descriptor of the header interface.
256 
257  /// The input terminal descriptors.
258  std::vector<VCInputTerminalDescriptor> vcInputTerminalDescriptors_;
259 
260  /// The selector unit descriptors.
261  std::vector<VCSelectorUnitDescriptor> vcSelectorUnitDescriptors_;
262 
263  /// The processing unit descriptors.
264  std::vector<VCProcessingUnitDescriptor> vcProcessingUnitDescriptors_;
265 
266  /// The extension unit descriptors.
267  std::vector<VCExtensionUnitDescriptor> vcExtensionUnitDescriptors_;
268 
269  /// The index of the control interface.
270  uint8_t bInterfaceIndex_ = 0u;
271 
272  /// The endpoint of the control interface.
273  uint8_t bEndpointAddress_ = 0u;
274 
275  /// True, if this control interface object is valid.
276  bool isValid_ = false;
277  };
278 
279  /**
280  * This class combines the video streaming interface functionality.
281  */
282  class OCEAN_SYSTEM_USB_VIDEO_EXPORT VideoStreamingInterface
283  {
284  friend class VideoDevice;
285 
286  public:
287 
288  /**
289  * Definition of a priority triple combining format index, frame index, and frame interval of a stream.
290  */
292 
293  /**
294  * Definition of an ordered map mapping priorities to triples combining formats, frames, and intervals.
295  */
296  using PriorityMap = std::map<float, PriorityTriple>;
297 
298  public:
299 
300  /**
301  * This class implements the base class for all video streams.
302  */
304  {
305  public:
306 
307  /// The color matching descriptor of the stream.
309  };
310 
311  /**
312  * This class implements an uncompressed video stream.
313  */
315  {
316  public:
317 
318  /// The format descriptor of the uncompressed stream.
320 
321  /// The frame descriptors of the uncompressed stream.
322  std::vector<VSUncompressedVideoFrameDescriptor> vsUncompressedVideoFrameDescriptors_;
323  };
324 
325  /**
326  * This class implements a frame-based video stream.
327  */
329  {
330  public:
331 
332  /// The format descriptor of the frame-based stream.
334 
335  /// The frame descriptors of the frame-based stream.
336  std::vector<VSFrameBasedFrameDescriptor> vsFrameBasedFrameDescriptors_;
337  };
338 
339  /**
340  * This class implements a Mjpeg video stream.
341  */
343  {
344  public:
345 
346  /// The format descriptor of the Mjpeg stream.
348 
349  /// The frame descriptors of the Mjpeg stream.
350  std::vector<VSMJPEGVideoFrameDescriptor> vsMJPEGVideoFrameDescriptors_;
351  };
352 
353  /**
354  * Definition of a vector holding UncompressedVideoStream objects.
355  */
356  using UncompressedVideoStreams = std::vector<UncompressedVideoStream>;
357 
358  /**
359  * Definition of a vector holding FrameBasedVideoStream objects.
360  */
361  using FrameBasedVideoStreams = std::vector<FrameBasedVideoStream>;
362 
363  /**
364  * Definition of a vector holding MJPEGVideoStream objects.
365  */
366  using MJPEGVideoStreams = std::vector<MJPEGVideoStream>;
367 
368  public:
369 
370  /**
371  * Returns the available uncompressed video streams.
372  * @return The interface's uncompressed video streams
373  */
374  inline const UncompressedVideoStreams& uncompressedVideoStreams() const;
375 
376  /**
377  * Returns the available frame-based video streams.
378  * @return The interface's frame-based video streams
379  */
380  inline const FrameBasedVideoStreams& frameBasedVideoStreams() const;
381 
382  /**
383  * Returns the available Mjpeg video streams.
384  * @return The interface's Mjpeg video streams
385  */
386  inline const MJPEGVideoStreams& mjpegVideoStreams() const;
387 
388  /**
389  * Returns a priority map with best matching stream configurations.
390  * @param preferredWidth The preferred width, in pixel, 0 to use a default width, with range [0, infinity)
391  * @param preferredHeight The preferred height, in pixel, 0 to use a default height, with range [0, infinity)
392  * @param preferredFrameRate The preferred frame rate, in Hz, 0 to use a default frame rate, with range [0, infinity)
393  * @param deviceStreamType The stream type to be used, ST_INVALID to allow any stream type
394  * @param pixelFormat The pixel format which needs to be used in case of an uncompressed stream, FORMAT_UNDEFINED to use any pixel format
395  * @param encodingFormat The encoding format which needs to be used in case of an frame-based stream, EF_INVALID to allow any encoding format
396  * @return The map with best matching stream configurations, the higher the key the better the match
397  */
398  PriorityMap findBestMatchingStream(const unsigned int preferredWidth, const unsigned int preferredHeight, const double preferredFrameRate, const DeviceStreamType deviceStreamType = DST_INVALID, const FrameType::PixelFormat pixelFormat = FrameType::FORMAT_UNDEFINED, VSFrameBasedVideoFormatDescriptor::EncodingFormat encodingFormat = VSFrameBasedVideoFormatDescriptor::EF_INVALID);
399 
400  /**
401  * Returns whether this interface object is valid.
402  * @return True, if so
403  */
404  inline bool isValid() const;
405 
406  protected:
407 
408  /**
409  * Creates an invalid interface object.
410  */
412 
413  /**
414  * Parses an additional interface and adds it to this streaming interface object.
415  * @param interfaceDescriptor The descriptor of the interface to be added
416  * @param usbDeviceHandle The handle to the USB device associated with given interface descriptor
417  */
418  bool parseAdditionalInterface(const libusb_interface_descriptor& interfaceDescriptor, libusb_device_handle* usbDeviceHandle);
419 
420  /**
421  * Returns a factor describing how well the resolution of a descriptor matches a preferred image resolution.
422  * @param frameDescriptor The frame descriptor for which the factor will be determined
423  * @param preferredWidth The preferred width, in pixel, with range [0, infinity)
424  * @param preferredHeight The preferred height, in pixel, with range [0, infinity)
425  * @param defaultPixels The default number of pixels which is used as a reference resolution in case preferred width and height are zero, with range [1, infinity)
426  * @return The matching factor, the higher the better the match, with range [0, 1]
427  * @tparam T The data type of the descriptor
428  */
429  template <typename T>
430  static float determineResolutionFactor(const T& frameDescriptor, const unsigned int preferredWidth, const unsigned int preferredHeight, const unsigned int defaultPixels = 1280u * 720u);
431 
432  /**
433  * Returns a factor describing how well one of the frame rates of a descriptor matches a preferred frame rate.
434  * @param frameDescriptor The frame descriptor for which the factor will be determined
435  * @param preferredFrameRate The preferred frame rate, in Hz, with range [0, infinity)
436  * @param frameInterval The resulting best matching frame interval, in 100ns
437  * @param defaultFrameRate The default frame rate which is used as a reference frame rate in case the preferred frame rate is zero, with range (0, infinity)
438  * @return The matching factor, the higher the better the match, with range [0, 1]
439  * @tparam T The data type of the descriptor
440  */
441  template <typename T>
442  static float determineFrameRateFactor(const T& frameDescriptor, const double preferredFrameRate, uint32_t& frameInterval, const double defaultFrameRate);
443 
444  protected:
445 
446  /// The descriptor of the input header interface.
448 
449  /// The uncompressed video streams.
451 
452  /// The frame-based video streams.
454 
455  /// The Mjpeg-based video streams.
457 
458  /// The index of the control interface.
459  uint8_t bInterfaceIndex_ = 0u;
460 
461  /// The endpoint of the control interface.
462  uint8_t bEndpointAddress_ = 0u;
463  };
464 
465  protected:
466 
467  /// The maximal size the memory of a sample can have, in bytes.
468  static constexpr size_t sampleMaxPayloadSize_ = 1024 * 1024 * 10;
469 
470  /**
471  * Definition of USB UVC Video Interface Subclass Codes.
472  */
473  enum VideoInterfaceSubclass : uint8_t
474  {
475  /// Undefined
476  SC_UNDEFINED = 0x00u,
477  /// This code is used for interfaces that manage control operations for video streaming devices.
478  SC_VIDEOCONTROL = 0x01u,
479  /// This code is used for interfaces that handle video streaming data.
480  SC_VIDEOSTREAMING = 0x02u,
481  /// This code is used for a collection of video interfaces that are grouped together to form a single functional entity.
482  SC_VIDEO_INTERFACE_COLLECTION = 0x03u
483  };
484 
485  enum ControlSelectors : uint8_t
486  {
487  VS_CONTROL_UNDEFINED = 0x00u,
488  /// Video Probe control.
489  VS_PROBE_CONTROL = 0x01u,
490  /// Video commit control.
491  VS_COMMIT_CONTROL = 0x02u,
492  VS_STILL_PROBE_CONTROL = 0x03u,
493  VS_STILL_COMMIT_CONTROL = 0x04u,
494  VS_STILL_IMAGE_TRIGGER_CONTROL = 0x05u,
495  VS_STREAM_ERROR_CODE_CONTROL = 0x06u,
496  VS_GENERATE_KEY_FRAME_CONTROL = 0x07u,
497  VS_UPDATE_FRAME_SEGMENT_CONTROL = 0x08u,
498  VS_SYNCH_DELAY_CONTROL = 0x09u
499  };
500 
501 #pragma pack(push)
502 #pragma pack(1)
503 
504  class OCEAN_SYSTEM_USB_VIDEO_EXPORT Control
505  {
506  public:
507 
508  /**
509  * Definition of the individual Video Class-Specific Request Codes.
510  */
511  enum RequestCode : uint8_t
512  {
513  /// Undefined request code.
514  RC_UNDEFINED = 0u,
515  /// Set current.
516  RC_SET_CUR = 0x01u,
517  /// Get current.
518  RC_GET_CUR = 0x81u,
519  /// Get minimum.
520  RC_GET_MIN = 0x82u,
521  /// Get maximum.
522  RC_GET_MAX = 0x83u,
523  /// Get resolution.
524  RC_GET_RES = 0x84u,
525  /// Get length.
526  RC_GET_LEN = 0x85u,
527  /// Get information.
528  RC_GET_INFO = 0x86u,
529  /// Get default.
530  RC_GET_DEF = 0x87u
531  };
532 
533  public:
534 
535  /**
536  * Executes a control commit or probe request.
537  * @param usbDeviceHandle The hand of the USB device to which the request will be set
538  * @param bmRequestType The request type parameter
539  * @param bRequest The request parameter
540  * @param wValue The value parameter
541  * @param wIndex The index parameter
542  * @param buffer The control buffer, must be valid
543  * @param size The size of the control buffer, in bytes, with range [1, infinity)
544  * @return True, if succeeded
545  */
546  static bool executeControl(libusb_device_handle* usbDeviceHandle, const uint8_t bmRequestType, const uint8_t bRequest, const uint16_t wValue, const uint16_t wIndex, void* buffer, const size_t size);
547  };
548 
549  /**
550  * Video Probe and Commit Controls.
551  *
552  * The streaming parameters selection process is based on a shared negotiation model between the host and the video streaming interface, taking into account the following features:
553  * - shared nature of the USB
554  * - interdependency of streaming parameters
555  * - payload independence
556  * - modification of streaming parameters during streaming
557  * This negotiation model is supported by the Video Probe and Commit controls.
558  * The Probe control allows retrieval and negotiation of streaming parameters.
559  * When an acceptable combination of streaming parameters has been obtained, the Commit control is used to configure the hardware with the negotiated parameters from the Probe control
560  *
561  * @see http://www.cajunbot.com/wiki/images/8/85/USB_Video_Class_1.1.pdf
562  */
563  class OCEAN_SYSTEM_USB_VIDEO_EXPORT VideoControl : public Control
564  {
565  public:
566 
567  /**
568  * Returns a string with the content of this object.
569  * @return The string holding the content of this object
570  */
571  std::string toString() const;
572 
573  /**
574  * Executes a video commit control request.
575  * @param usbDeviceHandle The hand of the USB device to which the request will be set
576  * @param interfaceIndex The index of the interface to which the control request will be sent
577  * @param videoControl The control to be sent, must be valid
578  * @param videoControlSize The size of the control to be sent, in bytes, either 26 or 34
579  * @param bRequest The request parameter
580  * @return True, if succeeded
581  */
582  static bool executeVideoControlCommit(libusb_device_handle* usbDeviceHandle, const uint8_t interfaceIndex, const VideoControl& videoControl, const size_t videoControlSize, const uint8_t bRequest = RC_SET_CUR);
583 
584  /**
585  * Executes a video probe control request.
586  * @param usbDeviceHandle The hand of the USB device to which the request will be set
587  * @param interfaceIndex The index of the interface to which the control request will be sent
588  * @param videoControl The resulting control
589  * @param videoControlSize The size of the control to be sent, in bytes, either 26 or 34
590  * @param bRequest The request parameter
591  * @return True, if succeeded
592  */
593  static bool executeVideoControlProbe(libusb_device_handle* usbDeviceHandle, const uint8_t interfaceIndex, VideoControl& videoControl, const size_t videoControlSize, const uint8_t bRequest = RC_GET_CUR);
594 
595  public:
596 
597  /**
598  * Bitfield control indicating to the function what fields shall be kept fixed (indicative only):
599  * D0: dwFrameInterval
600  * D1: wKeyFrameRate
601  * D2: wPFrameRate
602  * D3: wCompQuality
603  * D4: wCompWindowSize
604  * D15..5: Reserved (0)
605  *
606  * The hint bitmap indicates to the video streaming interface which fields shall be kept constant during stream parameter negotiation.
607  * For example, if the selection wants to favor frame rate over quality, the dwFrameInterval bit will be set (1).
608  *
609  * This field is set by the host, and is read-only for the video streaming interface.
610  */
611  uint16_t bmHint_ = 0u;
612 
613  /**
614  * Video format index from a format descriptor.
615  *
616  * Select a specific video stream format by setting this field to the one-based index of the associated format descriptor.
617  * To select the first format defined by a device, a value one (1) is written to this field. This field must be supported even if only one video format is supported by the device.
618  *
619  * This field is set by the host.
620  */
621  uint8_t bFormatIndex_ = 0u;
622 
623  /**
624  * Video frame index from a frame descriptor.
625  *
626  * This field selects the video frame resolution from the array of resolutions supported by the selected stream.
627  * The index value ranges from 1 to the number of Frame descriptors following a particular Format descriptor.
628  * This field must be supported even if only one video frame index is supported by the device.
629  *
630  * For video payloads with no defined frame descriptor, this field shall be set to zero (0).
631  *
632  * This field is set by the host.
633  */
634  uint8_t bFrameIndex_ = 0u;
635 
636  /**
637  * Frame interval in 100 ns units.
638  *
639  * This field sets the desired video frame interval for the selected video stream and frame index.
640  * The frame interval value is specified in 100 ns units.
641  * The device shall support the setting of all frame intervals reported in the Frame Descriptor corresponding to the selected Video Frame Index.
642  * This field must be implemented even if only one video frame interval is supported by the device.
643  *
644  * When used in conjunction with an IN endpoint, the host shall indicate its preference during the Probe phase.
645  * The value must be from the range of values supported by the device.
646  *
647  * When used in conjunction with an OUT endpoint, the host shall accept the value indicated by the device.
648  */
649  uint32_t dwFrameInterval_ = 0u;
650 
651  /**
652  * Key frame rate in key-frame per videoframe units.
653  *
654  * This field is only applicable to sources (and formats) capable of streaming video with adjustable compression parameters.
655  * Use of this control is at the discretion of the device, and is indicated in the VS Input or Output Header descriptor.
656  *
657  * The Key Frame Rate field is used to specify the compressor’s key frame rate.
658  * For example, if one of every ten encoded frames in a video stream sequence is a key frame, this control would report a value of 10.
659  * A value of 0 indicates that only the first frame is a key frame.
660  *
661  * When used in conjunction with an IN endpoint, the host shall indicate its preference during the Probe phase.
662  * The value must be from the range of values supported by the device.
663  *
664  * When used in conjunction with an OUT endpoint, the host shall accept the value indicated by the device.
665  */
666  uint16_t wKeyFrameRate_ = 0u;
667 
668  /**
669  * PFrame rate in PFrame/key frame units.
670  *
671  * This field is only applicable to sources (and formats) capable of streaming video with adjustable compression parameters.
672  * Use of this control is at the discretion of the device, and is indicated in the VS Input or Output Header descriptor.
673  *
674  * The P Frame Rate Control is used to specify the number of P frames per key frame.
675  * As an example of the relationship between the types of encoded frames, suppose a key frame occurs once in every 10 frames, and there are 3 P frames per key frame.
676  * The P frames will be spaced evenly between the key frames.
677  * The other 6 frames, which occur between the key frames and the P frames, will be bi-directional (B) frames.
678  *
679  * When used in conjunction with an IN endpoint, the host shall indicate its preference during the Probe phase.
680  * The value must be from the range of values supported by the device.
681  *
682  * When used in conjunction with an OUT endpoint, the host shall accept the value indicated by the device.
683  */
684  uint16_t wPFrameRate_ = 0u;
685 
686  /**
687  * Compression quality control in abstract units 0 (lowest) to 10000 (highest).
688  *
689  * This field is only applicable to sources (and formats) capable of streaming video with adjustable compression parameters.
690  * Use of this field is at the discretion of the device, and is indicated in the VS Input or Output Header descriptor.
691  *
692  * This field is used to specify the quality of the video compression.
693  * Values for this property range from 0 to 10000 (0 indicates the lowest quality, 10000 the highest).
694  * The resolution reported by this control will determine the number of discrete quality settings that it can support.
695  *
696  * When used in conjunction with an IN endpoint, the host shall indicate its preference during the Probe phase.
697  * The value must be from the range of values supported by the device.
698  *
699  * When used in conjunction with an OUT endpoint, the host shall accept the value indicated by the device.
700  */
701  uint16_t wCompQuality_ = 0u;
702 
703  /**
704  * Window size for average bit rate control.
705  *
706  * This field is only applicable to sources (and formats) capable of streaming video with adjustable compression parameters.
707  * Use of this control is at the discretion of the device, and is indicated in the VS Input or Output Header descriptor.
708  *
709  * The Compression Window Size Control is used to specify the number of encoded video frames over which the average size cannot exceed the specified data rate.
710  * For a window of size n, the average frame size of any consecutive n frames will not exceed the stream's specified data rate.
711  * Individual frames can be larger or smaller.
712  *
713  * For example, if the data rate has been set to 100 kilobytes per second (KBps) on a 10 frames per second (fps) movie with a compression window size of 10,
714  * the individual frames can be any size, as long as the average size of a frame in any 10-frame sequence is less than or equal to 10 kilobytes.
715  *
716  * When used in conjunction with an IN endpoint, the host shall indicate its preference during the Probe phase.
717  * The value must be from the range of values supported by the device.
718  *
719  * When used in conjunction with an OUT endpoint, the host shall accept the value indicated by the device.
720  */
721  uint16_t wCompWindowSize_ = 0u;
722 
723  /**
724  * Internal video streaming interface latency in ms from video data capture to presentation on the USB.
725  *
726  * When used in conjunction with an IN endpoint, this field is set by the device and read only from the host.
727  *
728  * When used in conjunction with an OUT endpoint, this field is set by the host and read only from the device.
729  */
730  uint16_t wDelay_ = 0u;
731 
732  /**
733  * Maximum video frame or codec-specific segment size in bytes.
734  *
735  * For frame-based formats, this field indicates the maximum size of a single video frame.
736  *
737  * For stream-based formats, and when this behavior is enabled via the bmFramingInfo field (below), this field indicates the maximum size of a single codec-specific segment.
738  * The sender is required to indicate a segment boundary via the FID bit in the payload header.
739  * This field is ignored (for stream-based formats) if the bmFramingInfo bits are not enabled.
740  *
741  * When used in conjunction with an IN endpoint, this field is set by the device and read only from the host.
742  *
743  * When used in conjunction with an OUT endpoint, this field is set by the host and read only from the device.
744  */
745  uint32_t dwMaxVideoFrameSize_ = 0u;
746 
747  /**
748  * Specifies the maximum number of bytes that the device can transmit or receive in a single payload transfer.
749  *
750  * This field is set by the device and read only from the host.
751  * Some host implementations restrict the maximum value permitted for this field.
752  */
753  uint32_t dwMaxPayloadTransferSize_ = 0u;
754 
755  /**
756  * The device clock frequency in Hz for the specified format.
757  * This will specify the units used for the time information fields in the Video Payload Headers in the data stream.
758  *
759  * This parameter is set by the device and read only from the host.
760  */
761  uint32_t dwClockFrequency_ = 0u;
762 
763  /**
764  * Bitfield control supporting the following values:
765  * D0:
766  * If set to 1, the Frame ID (FID) field is required in the Payload Header (see description of D0 in section 2.4.3.3, “Video and Still Image Payload Headers”).
767  * The sender is required to toggle the Frame ID at least every 'dwMaxVideoFrameSize' bytes (see above).
768  *
769  * D1:
770  * If set to 1, indicates that the End of Frame (EOF) field may be present in the Payload Header (see description of D1 in section 2.4.3.3, “Video and Still Image Payload Headers”).
771  * It is an error to specify this bit without also specifying D0.
772  *
773  * D7..2: Reserved (0)
774  *
775  * This control indicates to the function whether payload transfers will contain out-of-band framing information in the Video Payload Header (see section 2.4.3.3, “Video and Still Image Payload Headers”).
776  * For known frame-based formats (e.g., MJPEG, Uncompressed, DV), this control is ignored.
777  * For known stream-based formats, this control allows the sender to indicate that it will identify segment boundaries in the stream, enabling low-latency buffer handling by the receiver without the overhead of parsing the stream itself.
778  * When used in conjunction with an IN endpoint, this control is set by the device, and is read-only from the host.
779  * When used in conjunction with an OUT endpoint, this parameter is set by the host, and is read-only from the device.
780  */
781  uint8_t bmFramingInfo_ = 0u;
782 
783  /**
784  * The preferred payload format version supported by the host or device for the specified bFormatIndex value.
785  *
786  * This parameter allows the host and device to negotiate a mutually agreed version of the payload format associated with the bFormatIndex field.
787  * The host initializes this and the following bMinVersion and bMaxVersion fields to zero on the first Probe Set.
788  * Upon Probe Get, the device shall return its preferred version, plus the minimum and maximum versions supported by the device (see bMinVersion and bMaxVersion below).
789  * The host may issue a subsequent Probe Set/Get sequence to specify its preferred version (within the ranges returned in bMinVersion and bMaxVersion from the initial Probe Set/Get sequence).
790  *
791  * The host is not permitted to alter the bMinVersion and bMaxVersion values.
792  *
793  * This field will support up to 256 (0-255) versions of a single payload format.
794  * The version number is drawn from the minor version of the Payload Format specification. For example, version 1.2 of a Payload Format specification would result in a value of 2 for this parameter.
795  */
796  uint8_t bPreferedVersion_ = 0u;
797 
798  /**
799  * The minimum payload format version supported by the device for the specified bFormatIndex value.
800  *
801  * This value is initialized to zero by the host and reset to a value in the range of 0 to 255 by the device.
802  * The host is not permitted to modify this value (other than to restart the negotiation by setting bPreferredVersion, bMinVersion and bMaxVersion to zero).
803  */
804  uint8_t bMinVersion_ = 0u;
805 
806  /**
807  * The maximum payload format version supported by the device for the specified bFormatIndex value.
808  *
809  * This value is initialized to zero by the host and reset to a value in the range of 0 to 255 by the device.
810  * The host is not permitted to modify this value (other than to restart the negotiation by setting bPreferredVersion, bMinVersion and bMaxVersion to zero).
811  */
812  uint8_t bMaxVersion_ = 0u;
813  };
814 
815  /**
816  * This class implements functions for a Camera Control terminal
817  */
818  class OCEAN_SYSTEM_USB_VIDEO_EXPORT CameraTerminalControl : public Control
819  {
820  public:
821 
822  /**
823  * Executes a probe request for an auto focus interface.
824  * @param usbDeviceHandle The handle of the USB device, must be valid
825  * @param terminalId The id of the terminal to be used, must be valid
826  * @param interfaceIndex The index of the interface to be used, must be valid
827  * @param requestCode The request code to be used
828  * @param value The resulting value
829  * @return True, if succeeded
830  */
831  static bool getFocusAuto(libusb_device_handle* usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const RequestCode requestCode, bool& value);
832 
833  /**
834  * Executes a commit request for an auto focus interface.
835  * @param usbDeviceHandle The handle of the USB device, must be valid
836  * @param terminalId The id of the terminal to be used, must be valid
837  * @param interfaceIndex The index of the interface to be used, must be valid
838  * @param value The value to set
839  * @return True, if succeeded
840  */
841  static bool setFocusAuto(libusb_device_handle* usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const bool value);
842 
843  /**
844  * Executes a probe request for an absolute focus interface.
845  * @param usbDeviceHandle The handle of the USB device, must be valid
846  * @param terminalId The id of the terminal to be used, must be valid
847  * @param interfaceIndex The index of the interface to be used, must be valid
848  * @param requestCode The request code to be used
849  * @param value The resulting value
850  * @return True, if succeeded
851  */
852  static bool getFocusAbsolute(libusb_device_handle* usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const RequestCode requestCode, uint16_t& value);
853 
854  /**
855  * Executes a commit request for an auto focus interface.
856  * @param usbDeviceHandle The handle of the USB device, must be valid
857  * @param terminalId The id of the terminal to be used, must be valid
858  * @param interfaceIndex The index of the interface to be used, must be valid
859  * @param value The value to set
860  * @return True, if succeeded
861  */
862  static bool setFocusAbsolute(libusb_device_handle* usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const uint16_t value);
863 
864  /**
865  * Executes a probe request for an auto exposure interface.
866  * <pre>
867  * The setting for the attribute of the addressed Auto-Exposure Mode Control:
868  * D0: Manual Mode – manual Exposure Time, manual Iris
869  * D1: Auto Mode – auto Exposure Time, auto Iris
870  * D2: Shutter Priority Mode – manual Exposure Time, auto Iris
871  * D3: Aperture Priority Mode – auto Exposure Time, manual Iris
872  * D4..D7: Reserved, set to zero.
873  * </pre>
874  * @param usbDeviceHandle The handle of the USB device, must be valid
875  * @param terminalId The id of the terminal to be used, must be valid
876  * @param interfaceIndex The index of the interface to be used, must be valid
877  * @param requestCode The request code to be used
878  * @param value The resulting value
879  * @return True, if succeeded
880  */
881  static bool getAutoExposureMode(libusb_device_handle* usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const RequestCode requestCode, uint8_t& value);
882 
883  /**
884  * Executes a commit request for an auto exposure interface.
885  * <pre>
886  * The setting for the attribute of the addressed Auto-Exposure Mode Control:
887  * D0: Manual Mode – manual Exposure Time, manual Iris
888  * D1: Auto Mode – auto Exposure Time, auto Iris
889  * D2: Shutter Priority Mode – manual Exposure Time, auto Iris
890  * D3: Aperture Priority Mode – auto Exposure Time, manual Iris
891  * D4..D7: Reserved, set to zero.
892  * </pre>
893  * @param usbDeviceHandle The handle of the USB device, must be valid
894  * @param terminalId The id of the terminal to be used, must be valid
895  * @param interfaceIndex The index of the interface to be used, must be valid
896  * @param value The value to set
897  * @return True, if succeeded
898  */
899  static bool setAutoExposureMode(libusb_device_handle* usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const uint8_t value);
900 
901  /**
902  * Executes a probe request for an absolute exposure interface.
903  * @param usbDeviceHandle The handle of the USB device, must be valid
904  * @param terminalId The id of the terminal to be used, must be valid
905  * @param interfaceIndex The index of the interface to be used, must be valid
906  * @param requestCode The request code to be used
907  * @param value The resulting value
908  * @return True, if succeeded
909  */
910  static bool getExposureAbsolute(libusb_device_handle* usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const RequestCode requestCode, uint32_t& value);
911 
912  /**
913  * Executes a commit request for an absolute exposure interface.
914  * @param usbDeviceHandle The handle of the USB device, must be valid
915  * @param terminalId The id of the terminal to be used, must be valid
916  * @param interfaceIndex The index of the interface to be used, must be valid
917  * @param value The value to set
918  * @return True, if succeeded
919  */
920  static bool setExposureAbsolute(libusb_device_handle* usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const uint32_t value);
921  };
922 
923 #pragma pack(pop)
924 
925  static_assert(sizeof(VideoControl) == 34, "Invalid struct size!");
926 
927  /**
928  * Video and Still Image Payload Headers.
929  * Every Payload Transfer containing video or still-image sample data must start with a Payload Header.
930  *
931  * @see http://www.cajunbot.com/wiki/images/8/85/USB_Video_Class_1.1.pdf
932  */
933  class OCEAN_SYSTEM_USB_VIDEO_EXPORT PayloadHeader
934  {
935  public:
936 
937  /**
938  * Creates a new payload header object from memory.
939  * @param buffer The memory buffer in which the header is located, must be valid
940  * @param size The size of the buffer, in bytes, with range [1, infinity)
941  */
942  PayloadHeader(const uint8_t* buffer, const size_t size);
943 
944  /**
945  * Returns whether the frame id toggling between 0 and 1 every time a new video frame begins.
946  * For frame-based formats, this bit toggles between 0 and 1 every time a new video frame begins.
947  * For stream-based formats, this bit toggles between 0 and 1 at the start of each new codec-specific segment.
948  * @return The frame id, with range [0, 1]
949  */
950  inline uint8_t toggledFrameId() const;
951 
952  /**
953  * Returns whether the payload marks the end of the current video or still image frame.
954  * @return True, if so
955  */
956  inline bool isEndOfFrame() const;
957 
958  /**
959  * Returns whether the dwPresentationTime field is being sent as part of the header.
960  * @return True, if so
961  */
962  inline bool hasPresentationTime() const;
963 
964  /**
965  * Returns whether the dwSourceClock field is being sent as part of the header.
966  * @return True, if so
967  */
968  inline bool hasSourceClockReference() const;
969 
970  /**
971  * Returns whether the following data is part of a still image frame, and is only used for methods 2 and 3 of still image capture.
972  */
973  inline bool isStillImage() const;
974 
975  /**
976  * Returns whether there was an error in the video or still image transmission for this payload.
977  * @return True, if so
978  */
979  inline bool hasError() const;
980 
981  /**
982  * Returns whether the payload is the last header group in the packet.
983  * @return True, if so
984  */
985  inline bool isEndOfHeader() const;
986 
987  /**
988  * Returns the presentation time stamp (PTS).
989  * @return The presentation time stamp, if hasPresentationTime() == true
990  */
991  inline uint32_t sourceTimeClock() const;
992 
993  /**
994  * Returns the bus frame counter part of the scrSourceClock field.
995  * @return The bus frame counter, with range [0, 2^11-1], if hasPresentationTime() == true
996  */
997  inline uint16_t busFrameCounter() const;
998 
999  public:
1000 
1001  /**
1002  * Length of the payload header in bytes including this field.
1003  */
1004  uint8_t bHeaderLength_ = 0u;
1005 
1006  /**
1007  * Provides information on the sample data following the header, as well as the availability of optional header fields in this header.
1008  *
1009  * D0:
1010  * Frame ID – For frame-based formats, this bit toggles between 0 and 1 every time a new video frame begins.
1011  * For stream-based formats, this bit toggles between 0 and 1 at the start of each new codec-specific segment.
1012  * This behavior is required for frame-based payload formats (e.g., DV) and is optional for stream-based payload formats (e.g., MPEG-2 TS).
1013  * For stream-based formats, support for this bit must be indicated via the bmFramingInfo field of the Video Probe and Commit controls (see section 4.3.1.1, “Video Probe and Commit Controls”).
1014  *
1015  * D1:
1016  * End of Frame – This bit is set if the following payload data marks the end of the current video or still image frame (for frame- based formats), or to indicate the end of a codec-specific segment (for stream-based formats).
1017  * This behavior is optional for all payload formats. For stream-based formats, support for this bit must be indicated via the bmFramingInfo field of the Video Probe and Commit Controls (see section 4.3.1.1, “Video Probe and Commit Controls”).
1018  *
1019  * D2:
1020  * Presentation Time – This bit is set if the dwPresentationTime field is being sent as part of the header.
1021  *
1022  * D3:
1023  * Source Clock Reference – This bit is set if the dwSourceClock field is being sent as part of the header.
1024  *
1025  * D4:
1026  * Reserved
1027  *
1028  * D5:
1029  * Still Image – This bit is set if the following data is part of a still image frame, and is only used for methods 2 and 3 of still image capture.
1030  *
1031  * D6:
1032  * Error – This bit is set if there was an error in the video or still image transmission for this payload.
1033  * The Stream Error Code control would reflect the cause of the error.
1034  *
1035  * D7:
1036  * End of header – This bit is set if this is the last header group in the packet, where the header group refers to this field and any optional fields identified by the bits in this field (Defined for future extension).
1037  */
1038  uint8_t bmHeaderInfo_ = 0u;
1039 
1040  /**
1041  * Presentation Time Stamp (PTS).
1042  *
1043  * The source clock time in native device clock units when the raw frame capture begins.
1044  * This field may be repeated for multiple payload transfers comprising a single video frame, with the restriction that the value shall remain the same throughout that video frame.
1045  * The PTS is in the same units as specified in the dwClockFrequency field of the Video Probe Control response.
1046  */
1047  uint32_t dwPresentationTime_ = 0u;
1048 
1049  /**
1050  * A two-part Source Clock Reference (SCR) value.
1051  *
1052  * D31..D0:
1053  * Source Time Clock in native device clock units.
1054  *
1055  * D42..D32:
1056  * 1KHz SOF token counter
1057  *
1058  * D47..D43: Reserved, set to zero.
1059  *
1060  * The least-significant 32 bits (D31..D0) contain clock values sampled from the System Time Clock (STC) at the source.
1061  * The clock resolution shall be according to the dwClockFrequency field of the Probe and Commit response of the device as defined in Table 4-47 of this specification.
1062  * This value shall comply with the associated stream payload specification.
1063  *
1064  * The times at which the STC is sampled must be correlated with the USB Bus Clock.
1065  * To that end, the next most significant 11 bits of the SCR (D42..D32) contain a 1 KHz SOF counter, representing the frame number at the time the STC was sampled.
1066  * The STC is sampled at arbitrary SOF boundaries.
1067  * The SOF counter is the same size and frequency as the frame number associated with USB SOF tokens; however it is not required to match the current frame number.
1068  * This allows implementations using a chipset that can trigger on SOF tokens (but not accurately obtain the Frame number) to keep their own frame counters.
1069  *
1070  * The most-significant 5 bits (D47..D43) are reserved, and must be set to zero.
1071  * The maximum interval between Payload Headers containing SCR values is 100ms, or the video frame interval, whichever is greater. Shorter intervals are permitted.
1072  */
1073  uint8_t scrSourceClock_[6];
1074  };
1075 
1076  public:
1077 
1078  /**
1079  * Default constructor creating an invalid video device.
1080  */
1081  VideoDevice() = default;
1082 
1083  /**
1084  * Creates a new video device based on a valid device.
1085  * @param device The device which will be converted into a video device, must be valid
1086  */
1087  explicit VideoDevice(Device&& device);
1088 
1089  /**
1090  * Destructs this video device and releases all associated resources.
1091  */
1092  ~VideoDevice() override;
1093 
1094  /**
1095  * Extracts the relevant properties for a specific stream this device supports.
1096  * @param descriptorFormatIndex The video format index specifying the format descriptor of the stream delivering this sample, must be valid
1097  * @param descriptorFrameIndex The video frame index specifying the frame descriptor of the stream delivering this sample, must be valid
1098  * @param width The resulting width of the stream, in pixels, with range [1, infinity)
1099  * @param height The resulting height of the stream, in pixels, with range [1, infinity)
1100  * @param pixelFormat The resulting pixel format of the stream, valid only if stream type is DST_UNCOMPRESSED
1101  * @param encodingFormat The resulting encoding format of the stream, valid only if stream type is DST_FRAME_BASED
1102  * @return The resulting stream type, DST_INVALID if the properties could not be extracted
1103  */
1104  DeviceStreamType extractStreamProperties(const uint8_t descriptorFormatIndex, const uint8_t descriptorFrameIndex, unsigned int& width, unsigned int& height, FrameType::PixelFormat& pixelFormat, VSFrameBasedVideoFormatDescriptor::EncodingFormat& encodingFormat) const;
1105 
1106  /**
1107  * Starts the video device.
1108  * In case, the device is already started, nothing will happen.
1109  * @param preferredWidth The preferred width of the stream, in pixel, with range [1, infinity), 0 to use a default width
1110  * @param preferredHeight The preferred height of the stream, in pixel, with range [1, infinity), 0 to use a default height
1111  * @param preferredFrameRate The preferred frame rate, in Hz, with range (0, infinity), 0 to use a default frame rate
1112  * @param preferredDeviceStreamType The preferred device stream type, DST_INVALID to use a default stream type
1113  * @param preferredPixelFormat The preferred pixel format, will be ignored for streams which do not have a device stream type DST_UNCOMPRESSED
1114  * @param preferredEncodingFormat The preferred encoding format, will be ignored for streams which do not have a device stream type DST_FRAME_BASED
1115  * @return True, if the stream could be started
1116  * @see isStarted(), stopStream(), extractStreamProperties().
1117  */
1118  bool start(const unsigned int preferredWidth = 0u, const unsigned int preferredHeight = 0u, const double preferredFrameRate = 0.0, const DeviceStreamType preferredDeviceStreamType = DST_INVALID, const FrameType::PixelFormat preferredPixelFormat = FrameType::FORMAT_UNDEFINED, const VSFrameBasedVideoFormatDescriptor::EncodingFormat preferredEncodingFormat = VSFrameBasedVideoFormatDescriptor::EF_INVALID);
1119 
1120  /**
1121  * Stops the video device.
1122  * @return True, if succeeded
1123  */
1124  bool stop();
1125 
1126  /**
1127  * Returns whether auto focus is activated or deactivated.
1128  * @param value The resulting auto focus state; True, if activated; False, if deactivated
1129  * @return True, if succeeded; False, if in case of an error or if the device does not support auto focus
1130  * @see setAutoFocus().
1131  */
1132  bool autoFocus(bool &value);
1133 
1134  /**
1135  * Activate or deactivate the auto focus of this device.
1136  * @param value True, to activate the auto focus; False, to deactivate the auto focus
1137  * @return True, if succeeded; False, if in case of an error or if the device does not support auto focus
1138  * @see autoFocus().
1139  */
1140  bool setAutoFocus(const bool value);
1141 
1142  /**
1143  * Sets the absolute focus of this device.
1144  * @param value The absolute focus value to set, in the domain of the device's focus range, with range [minValue, maxValue]
1145  * @return True, if succeeded; False, if in case of an error or if the device does not support to set the absolute focus
1146  * @see absoluteFocus().
1147  */
1148  bool setAbsoluteFocus(const float value);
1149 
1150  /**
1151  * Returns the device's absolute focus.
1152  * @param minValue Optional resulting minimal focus value, in the domain of the device's focus range, nullptr if not of interest
1153  * @param currentValue Optional resulting current focus value, in the domain of the device's focus range, nullptr if not of interest
1154  * @param maxValue Optional resulting maximal focus value, in the domain of the device's focus range, nullptr if not of interest
1155  * @return True, if succeeded; False, if in case of an error or if the device does not support absolute focus
1156  * @see setAbsoluteFocus().
1157  */
1158  bool absoluteFocus(float* minValue, float* currentValue, float* maxValue);
1159 
1160  /**
1161  * Returns the supported auto exposure modes.
1162  * @param value The resulting value with supported modes
1163  * @return True, if succeeded
1164  * @see setAutoExposureMode().
1165  */
1166  bool supportedAutoExposureModes(uint8_t& value) const;
1167 
1168  /**
1169  * Returns the current auto exposure mode.
1170  * @param value The resulting value of the auto exposure mode
1171  * @return True, if succeeded; False, if in case of an error or if the device does not support the auto exposure mode
1172  * @see setAutoExposureMode(), supportedAutoExposureModes().
1173  */
1174  bool autoExposureMode(uint8_t &value) const;
1175 
1176  /**
1177  * Sets the auto exposure mode.
1178  * @param value The value of the auto exposure mode to set
1179  * @return True, if succeeded; False, if in case of an error or if the device does not support the auto exposure mode
1180  * @see autoExposureMode(), supportedAutoExposureModes().
1181  */
1182  bool setAutoExposureMode(const uint8_t value);
1183 
1184  /**
1185  * Returns the absolute exposure duration of this device.
1186  * @param minValue Optional resulting minimal exposure duration, in seconds, nullptr if not of interest
1187  * @param currentValue Optional resulting current exposure duration, in seconds, nullptr if not of interest
1188  * @param maxValue Optional resulting maximal exposure duration, in seconds, nullptr if not of interest
1189  * @return True, if succeeded
1190  * @see setAbsoluteExposure().
1191  */
1192  bool absoluteExposure(double* minValue, double* currentValue, double* maxValue);
1193 
1194  /**
1195  * Sets the absolute exposure duration of this device.
1196  * @param duration The exposure duration to be set, in seconds, with range (0, infinity), 0 for auto exposure
1197  * @return True, if succeeded
1198  * @see absoluteExposure().
1199  */
1200  bool setAbsoluteExposure(const double value);
1201 
1202  /**
1203  * Returns the next sample from this device.
1204  * Once a sample has been processed, it should be given back to the device so that it can be reused by the device.
1205  * @param pendingSamples Optional resulting number of samples which are still in the queue (not including the returned sample), nullptr if not of interest
1206  * @return The next sample, nullptr if currently no sample is available
1207  * @see giveSampleBack().
1208  */
1209  SharedSample nextSample(size_t* pendingSamples = nullptr);
1210 
1211  /**
1212  * Gives a sample back to this device so that it can be reused by the device.
1213  * @param sample The sample to give back, must be valid
1214  * @see nextSample().
1215  */
1217 
1218  /**
1219  * Returns the video streaming interface functionality of this device.
1220  * @return The device's video streaming interface
1221  */
1222  inline const VideoStreamingInterface& videoStreamingInterface() const;
1223 
1224  /**
1225  * Returns whether this video device has an active stream.
1226  * @return True, if so
1227  * @see startStream().
1228  */
1229  inline bool isStarted() const;
1230 
1231  /**
1232  * Returns whether this video device is valid and ready to be used.
1233  * @return True, if so; False, if e.g., not all devices of the USB device could be accessed
1234  */
1235  inline bool isValid() const;
1236 
1237  /**
1238  * Translates a device stream type to a string.
1239  * @param deviceStreamType The stream type to translate
1240  * @return The translated type as string, 'Invalid' if the type is invalid or unknown
1241  */
1242  static std::string translateDeviceStreamType(const DeviceStreamType deviceStreamType);
1243 
1244  protected:
1245 
1246  /**
1247  * Initializes the control interface.
1248  * @return True, if succeeded
1249  */
1251 
1252  /**
1253  * Parses all interfaces of this device.
1254  * @return True, if succeeded
1255  */
1257 
1258  /**
1259  * Parses a video interface of this device.
1260  * @param interfaceDescriptor The libusb interface descriptor of a video interface to parse, with bInterfaceClass == LIBUSB_CLASS_VIDEO
1261  * @return True, if succeeded
1262  */
1263  bool parseVideoInterface(const libusb_interface_descriptor& interfaceDescriptor);
1264 
1265  /**
1266  * Processes the payload which has been received from the device via a USB transfer.
1267  * @param bufferPointers The buffers holding the payload, at least one
1268  */
1269  void processPayload(const BufferPointers& bufferPointers);
1270 
1271  /**
1272  * Libusb status transfer callback function.
1273  * @param usbTransfer The transfer object which has been completed, failed, or canceled.
1274  * @return True, the transfer should be resubmitted
1275  */
1276  bool libStatusCallback(libusb_transfer& usbTransfer);
1277 
1278  /**
1279  * Libusb stream transfer callback function.
1280  * @param usbTransfer The transfer object which has been completed, failed, or canceled.
1281  * @return True, the transfer should be resubmitted
1282  */
1283  bool libusbStreamCallback(libusb_transfer& usbTransfer);
1284 
1285  /**
1286  * Static libusb status transfer callback function.
1287  * @param usbTransfer The transfer object which has been completed, failed, or canceled.
1288  */
1289  static void LIBUSB_CALL libStatusCallback(libusb_transfer* usbTransfer);
1290 
1291  /**
1292  * Static libusb stream transfer callback function.
1293  * @param usbTransfer The transfer object which has been completed, failed, or canceled.
1294  */
1295  static void LIBUSB_CALL libusbStreamCallback(libusb_transfer* usbTransfer);
1296 
1297  /**
1298  * Disabled copy constructor.
1299  */
1300  VideoDevice(const VideoDevice&) = delete;
1301 
1302  /**
1303  * Disabled move constructor.
1304  */
1306 
1307  /**
1308  * Disabled copy operator.
1309  * @return Reference to this object
1310  */
1311  VideoDevice& operator=(const VideoDevice&) = delete;
1312 
1313  /**
1314  * Disabled move operator.
1315  * @return Reference to this object
1316  */
1318 
1319  public:
1320 
1321  /// The video control interface of this device.
1323 
1324  /// The video streaming interface of this device.
1326 
1327  /// True, if the control interface has been initialized.
1328  bool initializeControlInterfaceInitialized_ = false;
1329 
1330  /// The libusb device configuration descriptor, nullptr if not yet initialized
1331  libusb_config_descriptor* usbConfigDescriptor_ = nullptr;
1332 
1333  /// The index of the descriptor format which is currently active/streaming.
1334  uint8_t activeDescriptorFormatIndex_ = 0u;
1335 
1336  /// The index of the descriptor frame which is currently active/streaming.
1337  uint8_t activeDescriptorFrameIndex_ = 0u;
1338 
1339  /// The clock frequency of the active stream.
1340  uint32_t activeClockFrequency_ = 0u;
1341 
1342  /// The maximal size of one sample.
1343  size_t maximalSampleSize_ = 0;
1344 
1345  /// The transfer object for interrupts.
1347 
1348  /// The streaming transfer objects.
1350 
1351  /// The map mapping transfer pointers to indices.
1353 
1354  /// The memory for the individual streaming transfer objects.
1355  std::vector<Memory> streamingTransferMemories_;
1356 
1357  /// The lock for the stream and interrupt transfers.
1359 
1360  /// True, if the video device has an active stream which has been started.
1361  bool isStarted_ = false;
1362 
1363  /// True, if the video device is currently stopping
1364  bool isStopping_ = false;
1365 
1366  /// The currently active sample which is receiving sample data from the device (but not yet filled).
1368 
1369  /// The queue with all processed samples waiting to be grabbed by an external entity.
1371 
1372  /// The reusable samples which waiting to be filled.
1374 
1375  /// The lock for the samples.
1377 
1378  /// Reusable buffer pointers to extract buffers from USB transfers.
1380 
1381  /// The subscription for the detached kernel driver (if necessary/possible).
1383 
1384  /// The subscription for the claimed video control interface.
1386 
1387  /// The subscription for the claimed video stream interface.
1389 };
1390 
1392 {
1393  constexpr uint8_t checkBit = (1u << 0u);
1394 
1395  return bmHeaderInfo_ & checkBit;
1396 }
1397 
1399 {
1400  constexpr uint8_t checkBit = (1u << 1u);
1401 
1402  return (bmHeaderInfo_ & checkBit) != 0u;
1403 }
1404 
1406 {
1407  constexpr uint8_t checkBit = (1u << 2u);
1408 
1409  return (bmHeaderInfo_ & checkBit) != 0u;
1410 }
1411 
1413 {
1414  constexpr uint8_t checkBit = (1u << 3u);
1415 
1416  return (bmHeaderInfo_ & checkBit) != 0u;
1417 }
1418 
1420 {
1421  constexpr uint8_t checkBit = (1u << 5u);
1422 
1423  return (bmHeaderInfo_ & checkBit) != 0u;
1424 }
1425 
1427 {
1428  constexpr uint8_t checkBit = (1u << 6u);
1429 
1430  return (bmHeaderInfo_ & checkBit) != 0u;
1431 }
1432 
1434 {
1435  constexpr uint8_t checkBit = (1u << 7u);
1436 
1437  return (bmHeaderInfo_ & checkBit) != 0u;
1438 }
1439 
1441 {
1442  ocean_assert(hasSourceClockReference());
1443 
1444  uint32_t result;
1445  memcpy(&result, scrSourceClock_, sizeof(uint32_t));
1446 
1447  return result;
1448 }
1449 
1451 {
1452  ocean_assert(hasSourceClockReference());
1453 
1454  uint16_t result;
1455  memcpy(&result, scrSourceClock_ + 4, sizeof(uint16_t));
1456 
1457  return result & uint16_t(0x07FFu);
1458 }
1459 
1461 {
1462  return descriptorFormatIndex_;
1463 }
1464 
1466 {
1467  return descriptorFrameIndex_;
1468 }
1469 
1470 inline const void* VideoDevice::Sample::data() const
1471 {
1472  return buffer_.data();
1473 }
1474 
1475 inline size_t VideoDevice::Sample::size() const
1476 {
1477  return position_;
1478 }
1479 
1481 {
1482  return mayContainError_;
1483 }
1484 
1486 {
1487  return isValid_;
1488 }
1489 
1491 {
1492  ocean_assert(isValid());
1493 
1494  return uncompressedVideoStreams_;
1495 }
1496 
1498 {
1499  ocean_assert(isValid());
1500 
1501  return frameBasedVideoStreams_;
1502 }
1503 
1505 {
1506  ocean_assert(isValid());
1507 
1508  return mjpegVideoStreams_;
1509 }
1510 
1512 {
1513  return vsInputHeaderDescriptor_.isValid();
1514 }
1515 
1517 {
1518  return videoStreamingInterface_;
1519 }
1520 
1521 inline bool VideoDevice::isStarted() const
1522 {
1523  const ScopedLock scopedLock(lock_);
1524 
1525  return isStarted_;
1526 }
1527 
1528 inline bool VideoDevice::isValid() const
1529 {
1530  const ScopedLock scopedLock(lock_);
1531 
1533 }
1534 
1535 }
1536 
1537 }
1538 
1539 }
1540 
1541 }
1542 
1543 #endif // META_OCEAN_SYSTEM_USB_VIDEO_VIDEO_DEVICE_H
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition: Frame.h:183
@ FORMAT_UNDEFINED
Undefined pixel format.
Definition: Frame.h:187
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
This class wraps an unmanaged object (or reference) which needs to be released after usage.
Definition: ScopedObject.h:166
This class implements a subscription object which can be used unique subscriptions to e....
Definition: ScopedSubscription.h:28
This class wraps a libusb device.
Definition: system/usb/Device.h:49
bool isValid() const
Returns whether this device is valid.
Definition: system/usb/Device.h:377
Lock lock_
The device's lock.
Definition: system/usb/Device.h:350
std::vector< BufferPointer > BufferPointers
Definition of a vector holding buffer pointers.
Definition: system/usb/Device.h:72
Class-Specific VC Interface Descriptor (VC_HEADER).
Definition: VCDescriptor.h:84
Color Matching Descriptor (VS_COLORFORMAT).
Definition: VSDescriptor.h:760
Frame Based Payload Video Format Descriptor (VS_FORMAT_FRAME_BASED).
Definition: VSDescriptor.h:491
EncodingFormat
Definition of individual encoding formats.
Definition: VSDescriptor.h:498
@ EF_INVALID
Invalid encoding format.
Definition: VSDescriptor.h:500
Input Header Descriptor (VS_INPUT_HEADER).
Definition: VSDescriptor.h:92
MJPEG Video Format Descriptor (VS_FORMAT_MJPEG).
Definition: VSDescriptor.h:863
UncompressedVideoFormatDescriptor (VS_FORMAT_UNCOMPRESSED)
Definition: VSDescriptor.h:234
This class implements functions for a Camera Control terminal.
Definition: VideoDevice.h:819
static bool getFocusAbsolute(libusb_device_handle *usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const RequestCode requestCode, uint16_t &value)
Executes a probe request for an absolute focus interface.
static bool getFocusAuto(libusb_device_handle *usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const RequestCode requestCode, bool &value)
Executes a probe request for an auto focus interface.
static bool setFocusAbsolute(libusb_device_handle *usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const uint16_t value)
Executes a commit request for an auto focus interface.
static bool getAutoExposureMode(libusb_device_handle *usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const RequestCode requestCode, uint8_t &value)
Executes a probe request for an auto exposure interface.
static bool setAutoExposureMode(libusb_device_handle *usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const uint8_t value)
Executes a commit request for an auto exposure interface.
static bool getExposureAbsolute(libusb_device_handle *usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const RequestCode requestCode, uint32_t &value)
Executes a probe request for an absolute exposure interface.
static bool setFocusAuto(libusb_device_handle *usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const bool value)
Executes a commit request for an auto focus interface.
static bool setExposureAbsolute(libusb_device_handle *usbDeviceHandle, const uint8_t terminalId, const uint8_t interfaceIndex, const uint32_t value)
Executes a commit request for an absolute exposure interface.
RequestCode
Definition of the individual Video Class-Specific Request Codes.
Definition: VideoDevice.h:512
static bool executeControl(libusb_device_handle *usbDeviceHandle, const uint8_t bmRequestType, const uint8_t bRequest, const uint16_t wValue, const uint16_t wIndex, void *buffer, const size_t size)
Executes a control commit or probe request.
Video and Still Image Payload Headers.
Definition: VideoDevice.h:934
bool hasError() const
Returns whether there was an error in the video or still image transmission for this payload.
Definition: VideoDevice.h:1426
PayloadHeader(const uint8_t *buffer, const size_t size)
Creates a new payload header object from memory.
bool isEndOfFrame() const
Returns whether the payload marks the end of the current video or still image frame.
Definition: VideoDevice.h:1398
bool hasSourceClockReference() const
Returns whether the dwSourceClock field is being sent as part of the header.
Definition: VideoDevice.h:1412
uint16_t busFrameCounter() const
Returns the bus frame counter part of the scrSourceClock field.
Definition: VideoDevice.h:1450
bool isStillImage() const
Returns whether the following data is part of a still image frame, and is only used for methods 2 and...
Definition: VideoDevice.h:1419
uint8_t toggledFrameId() const
Returns whether the frame id toggling between 0 and 1 every time a new video frame begins.
Definition: VideoDevice.h:1391
uint32_t sourceTimeClock() const
Returns the presentation time stamp (PTS).
Definition: VideoDevice.h:1440
bool hasPresentationTime() const
Returns whether the dwPresentationTime field is being sent as part of the header.
Definition: VideoDevice.h:1405
uint8_t bmHeaderInfo_
Provides information on the sample data following the header, as well as the availability of optional...
Definition: VideoDevice.h:1038
bool isEndOfHeader() const
Returns whether the payload is the last header group in the packet.
Definition: VideoDevice.h:1433
This class holds the data of a media sample.
Definition: VideoDevice.h:74
Sample()=default
Default constructor creating an invalid sample object.
std::string toString() const
Returns a string with the relevant information of this sample.
static uint64_t earlierTimestamp(const uint64_t timestampA, const uint64_t timestampB)
Returns the earlier timestamp of the two given timestamps while both timestamps are wrapped around 2^...
std::vector< uint8_t > buffer_
The payload buffer which can be appended with payload packets.
Definition: VideoDevice.h:172
bool mayContainError() const
Returns whether this sample may contain errors (e.g., because there was a transmission error).
Definition: VideoDevice.h:1480
void reset()
Resets the sample so that it can be reused.
Sample(const size_t capacity, const uint8_t descriptorFormatIndex, const uint8_t descriptorFrameIndex, const uint32_t dwClockFrequency)
Creates a new sample associated with a specific stream configuration.
bool append(const PayloadHeader &payloadHeader, const void *data, const size_t size)
Appends a payload packet to the sample.
uint8_t descriptorFormatIndex() const
Returns the video format index specifying the format descriptor of the stream delivering this sample.
Definition: VideoDevice.h:1460
size_t size() const
Returns the size of the sample.
Definition: VideoDevice.h:1475
const void * data() const
Returns the buffer of this sample.
Definition: VideoDevice.h:1470
Timestamp determineCaptureTimestamp() const
Determines the timestamp the sample was captured.
uint8_t descriptorFrameIndex() const
Returns the video frame index specifying the frame descriptor of the stream delivering this sample,...
Definition: VideoDevice.h:1465
static int unwrapTimestamps(uint64_t &timestampA, uint64_t &timestampB)
Unwraps two timestamps while both are wrapped around 2^32.
Video Probe and Commit Controls.
Definition: VideoDevice.h:564
static bool executeVideoControlProbe(libusb_device_handle *usbDeviceHandle, const uint8_t interfaceIndex, VideoControl &videoControl, const size_t videoControlSize, const uint8_t bRequest=RC_GET_CUR)
Executes a video probe control request.
std::string toString() const
Returns a string with the content of this object.
static bool executeVideoControlCommit(libusb_device_handle *usbDeviceHandle, const uint8_t interfaceIndex, const VideoControl &videoControl, const size_t videoControlSize, const uint8_t bRequest=RC_SET_CUR)
Executes a video commit control request.
This class combines the video control interface functionality.
Definition: VideoDevice.h:227
std::vector< VCSelectorUnitDescriptor > vcSelectorUnitDescriptors_
The selector unit descriptors.
Definition: VideoDevice.h:261
VideoControlInterface(const libusb_interface_descriptor &interfaceDescriptor, libusb_device_handle *usbDeviceHandle)
Creates a new video control interface based on a given interface descriptor.
bool isValid() const
Returns whether this interface object is valid.
Definition: VideoDevice.h:1485
std::vector< VCProcessingUnitDescriptor > vcProcessingUnitDescriptors_
The processing unit descriptors.
Definition: VideoDevice.h:264
std::vector< VCExtensionUnitDescriptor > vcExtensionUnitDescriptors_
The extension unit descriptors.
Definition: VideoDevice.h:267
VCHeaderDescriptor vcHeaderDescriptor_
The descriptor of the header interface.
Definition: VideoDevice.h:255
std::vector< VCInputTerminalDescriptor > vcInputTerminalDescriptors_
The input terminal descriptors.
Definition: VideoDevice.h:258
VideoControlInterface()=default
Default constructor creating an invalid interface.
This class implements a frame-based video stream.
Definition: VideoDevice.h:329
VSFrameBasedVideoFormatDescriptor vsFrameBasedVideoFormatDescriptor_
The format descriptor of the frame-based stream.
Definition: VideoDevice.h:333
std::vector< VSFrameBasedFrameDescriptor > vsFrameBasedFrameDescriptors_
The frame descriptors of the frame-based stream.
Definition: VideoDevice.h:336
This class implements a Mjpeg video stream.
Definition: VideoDevice.h:343
std::vector< VSMJPEGVideoFrameDescriptor > vsMJPEGVideoFrameDescriptors_
The frame descriptors of the Mjpeg stream.
Definition: VideoDevice.h:350
VSMJPEGVideoFormatDescriptor vsMJPEGVideoFormatDescriptor_
The format descriptor of the Mjpeg stream.
Definition: VideoDevice.h:347
This class implements an uncompressed video stream.
Definition: VideoDevice.h:315
VSUncompressedVideoFormatDescriptor vsUncompressedVideoFormatDescriptor_
The format descriptor of the uncompressed stream.
Definition: VideoDevice.h:319
std::vector< VSUncompressedVideoFrameDescriptor > vsUncompressedVideoFrameDescriptors_
The frame descriptors of the uncompressed stream.
Definition: VideoDevice.h:322
This class implements the base class for all video streams.
Definition: VideoDevice.h:304
VSColorMatchingDescriptor vsColorMatchingDescriptor_
The color matching descriptor of the stream.
Definition: VideoDevice.h:308
This class combines the video streaming interface functionality.
Definition: VideoDevice.h:283
static float determineResolutionFactor(const T &frameDescriptor, const unsigned int preferredWidth, const unsigned int preferredHeight, const unsigned int defaultPixels=1280u *720u)
Returns a factor describing how well the resolution of a descriptor matches a preferred image resolut...
std::vector< UncompressedVideoStream > UncompressedVideoStreams
Definition of a vector holding UncompressedVideoStream objects.
Definition: VideoDevice.h:356
std::vector< MJPEGVideoStream > MJPEGVideoStreams
Definition of a vector holding MJPEGVideoStream objects.
Definition: VideoDevice.h:366
VSInputHeaderDescriptor vsInputHeaderDescriptor_
The descriptor of the input header interface.
Definition: VideoDevice.h:447
const UncompressedVideoStreams & uncompressedVideoStreams() const
Returns the available uncompressed video streams.
Definition: VideoDevice.h:1490
const FrameBasedVideoStreams & frameBasedVideoStreams() const
Returns the available frame-based video streams.
Definition: VideoDevice.h:1497
bool parseAdditionalInterface(const libusb_interface_descriptor &interfaceDescriptor, libusb_device_handle *usbDeviceHandle)
Parses an additional interface and adds it to this streaming interface object.
std::map< float, PriorityTriple > PriorityMap
Definition of an ordered map mapping priorities to triples combining formats, frames,...
Definition: VideoDevice.h:296
MJPEGVideoStreams mjpegVideoStreams_
The Mjpeg-based video streams.
Definition: VideoDevice.h:456
FrameBasedVideoStreams frameBasedVideoStreams_
The frame-based video streams.
Definition: VideoDevice.h:453
VideoStreamingInterface()=default
Creates an invalid interface object.
PriorityMap findBestMatchingStream(const unsigned int preferredWidth, const unsigned int preferredHeight, const double preferredFrameRate, const DeviceStreamType deviceStreamType=DST_INVALID, const FrameType::PixelFormat pixelFormat=FrameType::FORMAT_UNDEFINED, VSFrameBasedVideoFormatDescriptor::EncodingFormat encodingFormat=VSFrameBasedVideoFormatDescriptor::EF_INVALID)
Returns a priority map with best matching stream configurations.
std::vector< FrameBasedVideoStream > FrameBasedVideoStreams
Definition of a vector holding FrameBasedVideoStream objects.
Definition: VideoDevice.h:361
UncompressedVideoStreams uncompressedVideoStreams_
The uncompressed video streams.
Definition: VideoDevice.h:450
bool isValid() const
Returns whether this interface object is valid.
Definition: VideoDevice.h:1511
static float determineFrameRateFactor(const T &frameDescriptor, const double preferredFrameRate, uint32_t &frameInterval, const double defaultFrameRate)
Returns a factor describing how well one of the frame rates of a descriptor matches a preferred frame...
const MJPEGVideoStreams & mjpegVideoStreams() const
Returns the available Mjpeg video streams.
Definition: VideoDevice.h:1504
This class implements a USB video device.
Definition: VideoDevice.h:47
VideoStreamingInterface videoStreamingInterface_
The video streaming interface of this device.
Definition: VideoDevice.h:1325
std::vector< Memory > streamingTransferMemories_
The memory for the individual streaming transfer objects.
Definition: VideoDevice.h:1355
VideoControlInterface videoControlInterface_
The video control interface of this device.
Definition: VideoDevice.h:1322
static std::string translateDeviceStreamType(const DeviceStreamType deviceStreamType)
Translates a device stream type to a string.
DeviceStreamType
Definition of individual USB UVC device stream types.
Definition: VideoDevice.h:59
@ DST_UNCOMPRESSED
A stream composed of uncompressed video frames.
Definition: VideoDevice.h:63
@ DST_MJPEG
A stream based on MJpeg.
Definition: VideoDevice.h:65
VideoInterfaceSubclass
Definition of USB UVC Video Interface Subclass Codes.
Definition: VideoDevice.h:474
Lock transferLock_
The lock for the stream and interrupt transfers.
Definition: VideoDevice.h:1358
ScopedSubscription claimedVideoStreamInterfaceSubscription_
The subscription for the claimed video stream interface.
Definition: VideoDevice.h:1388
~VideoDevice() override
Destructs this video device and releases all associated resources.
bool autoFocus(bool &value)
Returns whether auto focus is activated or deactivated.
bool parseInterfaces()
Parses all interfaces of this device.
VideoDevice & operator=(const VideoDevice &)=delete
Disabled copy operator.
VideoDevice(Device &&device)
Creates a new video device based on a valid device.
VideoDevice(VideoDevice &&)=delete
Disabled move constructor.
bool setAutoExposureMode(const uint8_t value)
Sets the auto exposure mode.
bool isStarted_
True, if the video device has an active stream which has been started.
Definition: VideoDevice.h:1361
ScopedSubscription claimedVideoControlInterfaceSubscription_
The subscription for the claimed video control interface.
Definition: VideoDevice.h:1385
bool absoluteExposure(double *minValue, double *currentValue, double *maxValue)
Returns the absolute exposure duration of this device.
bool stop()
Stops the video device.
bool setAutoFocus(const bool value)
Activate or deactivate the auto focus of this device.
BufferPointers reusableBufferPointers_
Reusable buffer pointers to extract buffers from USB transfers.
Definition: VideoDevice.h:1379
bool supportedAutoExposureModes(uint8_t &value) const
Returns the supported auto exposure modes.
Lock samplesLock_
The lock for the samples.
Definition: VideoDevice.h:1376
VideoDevice(const VideoDevice &)=delete
Disabled copy constructor.
DeviceStreamType extractStreamProperties(const uint8_t descriptorFormatIndex, const uint8_t descriptorFrameIndex, unsigned int &width, unsigned int &height, FrameType::PixelFormat &pixelFormat, VSFrameBasedVideoFormatDescriptor::EncodingFormat &encodingFormat) const
Extracts the relevant properties for a specific stream this device supports.
bool parseVideoInterface(const libusb_interface_descriptor &interfaceDescriptor)
Parses a video interface of this device.
static void LIBUSB_CALL libStatusCallback(libusb_transfer *usbTransfer)
Static libusb status transfer callback function.
void processPayload(const BufferPointers &bufferPointers)
Processes the payload which has been received from the device via a USB transfer.
bool setAbsoluteExposure(const double value)
Sets the absolute exposure duration of this device.
bool isStarted() const
Returns whether this video device has an active stream.
Definition: VideoDevice.h:1521
static void LIBUSB_CALL libusbStreamCallback(libusb_transfer *usbTransfer)
Static libusb stream transfer callback function.
ScopedSubscription detachedKernelDriverSubscription_
The subscription for the detached kernel driver (if necessary/possible).
Definition: VideoDevice.h:1382
SharedSample activeSample_
The currently active sample which is receiving sample data from the device (but not yet filled).
Definition: VideoDevice.h:1367
VideoDevice & operator=(VideoDevice &&)=delete
Disabled move operator.
bool libStatusCallback(libusb_transfer &usbTransfer)
Libusb status transfer callback function.
const VideoStreamingInterface & videoStreamingInterface() const
Returns the video streaming interface functionality of this device.
Definition: VideoDevice.h:1516
bool setAbsoluteFocus(const float value)
Sets the absolute focus of this device.
bool absoluteFocus(float *minValue, float *currentValue, float *maxValue)
Returns the device's absolute focus.
bool isValid() const
Returns whether this video device is valid and ready to be used.
Definition: VideoDevice.h:1528
bool start(const unsigned int preferredWidth=0u, const unsigned int preferredHeight=0u, const double preferredFrameRate=0.0, const DeviceStreamType preferredDeviceStreamType=DST_INVALID, const FrameType::PixelFormat preferredPixelFormat=FrameType::FORMAT_UNDEFINED, const VSFrameBasedVideoFormatDescriptor::EncodingFormat preferredEncodingFormat=VSFrameBasedVideoFormatDescriptor::EF_INVALID)
Starts the video device.
void giveSampleBack(SharedSample &&sample)
Gives a sample back to this device so that it can be reused by the device.
ControlSelectors
Definition: VideoDevice.h:486
bool initializeControlInterfaceInitialized_
True, if the control interface has been initialized.
Definition: VideoDevice.h:1328
std::queue< SharedSample > SampleQueue
Definition of a queue holding samples.
Definition: VideoDevice.h:218
VideoDevice()=default
Default constructor creating an invalid video device.
bool autoExposureMode(uint8_t &value) const
Returns the current auto exposure mode.
SampleQueue sampleQueue_
The queue with all processed samples waiting to be grabbed by an external entity.
Definition: VideoDevice.h:1370
Samples reusableSamples_
The reusable samples which waiting to be filled.
Definition: VideoDevice.h:1373
TransferIndexMap transferIndexMap_
The map mapping transfer pointers to indices.
Definition: VideoDevice.h:1352
ScopedTransfers streamingTransfers_
The streaming transfer objects.
Definition: VideoDevice.h:1349
bool libusbStreamCallback(libusb_transfer &usbTransfer)
Libusb stream transfer callback function.
ScopedTransfer interruptTransfer_
The transfer object for interrupts.
Definition: VideoDevice.h:1346
bool initializeControlInterface()
Initializes the control interface.
SharedSample nextSample(size_t *pendingSamples=nullptr)
Returns the next sample from this device.
std::vector< SharedSample > Samples
Definition of a vector holding samples.
Definition: VideoDevice.h:213
std::shared_ptr< Sample > SharedSample
Definition of a shared pointer holding a Sample.
Definition: VideoDevice.h:208
This class implements a timestamp.
Definition: Timestamp.h:36
This class implements a triple object able to hold three individual elements.
Definition: Triple.h:28
std::vector< ScopedTransfer > ScopedTransfers
Definition of a vector holding scoped libusb transfer objects.
Definition: system/usb/USB.h:68
std::shared_ptr< VideoDevice > SharedVideoDevice
Definition of a shared pointer holding a VideoDevice.
Definition: VideoDevice.h:39
std::unordered_map< libusb_transfer *, size_t > TransferIndexMap
Definition of an unordered map mapping libusb transfer points to indices.
Definition: system/usb/USB.h:73
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15