Jetson Inference
DNN Vision Library
videoSource.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __VIDEO_SOURCE_H_
24 #define __VIDEO_SOURCE_H_
25 
26 
27 #include "videoOptions.h"
28 #include "imageFormat.h"
29 #include "commandLine.h"
30 
31 
36 #define VIDEO_SOURCE_USAGE_STRING "videoSource arguments: \n" \
37  " input resource URI of the input stream, for example:\n" \
38  " * /dev/video0 (V4L2 camera #0)\n" \
39  " * csi://0 (MIPI CSI camera #0)\n" \
40  " * rtp://@:1234 (RTP stream)\n" \
41  " * rtsp://user:pass@ip:1234 (RTSP stream)\n" \
42  " * webrtc://@:1234/my_stream (WebRTC stream)\n" \
43  " * file://my_image.jpg (image file)\n" \
44  " * file://my_video.mp4 (video file)\n" \
45  " * file://my_directory/ (directory of images)\n" \
46  " --input-width=WIDTH explicitly request a width of the stream (optional)\n" \
47  " --input-height=HEIGHT explicitly request a height of the stream (optional)\n" \
48  " --input-rate=RATE explicitly request a framerate of the stream (optional)\n" \
49  " --input-save=FILE path to video file for saving the input stream to disk\n" \
50  " --input-codec=CODEC RTP requires the codec to be set, one of these:\n" \
51  " * h264, h265\n" \
52  " * vp8, vp9\n" \
53  " * mpeg2, mpeg4\n" \
54  " * mjpeg\n" \
55  " --input-decoder=TYPE the decoder engine to use, one of these:\n" \
56  " * cpu\n" \
57  " * omx (aarch64/JetPack4 only)\n" \
58  " * v4l2 (aarch64/JetPack5 only)\n" \
59  " --input-flip=FLIP flip method to apply to input:\n" \
60  " * none (default)\n" \
61  " * counterclockwise\n" \
62  " * rotate-180\n" \
63  " * clockwise\n" \
64  " * horizontal\n" \
65  " * vertical\n" \
66  " * upper-right-diagonal\n" \
67  " * upper-left-diagonal\n" \
68  " --input-loop=LOOP for file-based inputs, the number of loops to run:\n" \
69  " * -1 = loop forever\n" \
70  " * 0 = don't loop (default)\n" \
71  " * >0 = set number of loops\n\n"
72 
73 
119 {
120 public:
124  enum Status
125  {
126  ERROR = -2,
127  EOS = -1,
128  TIMEOUT = 0,
129  OK = 1
130  };
131 
136  static videoSource* Create( const videoOptions& options );
137 
142  static videoSource* Create( const char* URI, const videoOptions& options=videoOptions() );
143 
149  static videoSource* Create( const char* URI, const commandLine& cmdLine );
150 
156  static videoSource* Create( const char* URI, const int argc, char** argv );
157 
166  static videoSource* Create( const int argc, char** argv, int positionArg=-1 );
167 
176  static videoSource* Create( const commandLine& cmdLine, int positionArg=-1 );
177 
181  virtual ~videoSource();
182 
186  static inline const char* Usage() { return VIDEO_SOURCE_USAGE_STRING; }
187 
211  template<typename T> bool Capture( T** image, int* status ) { return Capture((void**)image, imageFormatFromType<T>(), DEFAULT_TIMEOUT, status); }
212 
240  template<typename T> bool Capture( T** image, uint64_t timeout=DEFAULT_TIMEOUT, int* status=NULL ) { return Capture((void**)image, imageFormatFromType<T>(), timeout); }
241 
262  virtual bool Capture( void** image, imageFormat format, uint64_t timeout=DEFAULT_TIMEOUT, int* status=NULL ) = 0;
263 
274  virtual bool Open();
275 
283  virtual void Close();
284 
291  inline bool IsStreaming() const { return mStreaming; }
292 
296  inline uint32_t GetWidth() const { return mOptions.width; }
297 
301  inline uint32_t GetHeight() const { return mOptions.height; }
302 
306  inline uint32_t GetFrameRate() const { return mOptions.frameRate; }
307 
311  inline uint64_t GetFrameCount() const { return mOptions.frameCount; }
312 
316  uint64_t GetLastTimestamp() const { return mLastTimestamp; }
317 
321  inline imageFormat GetRawFormat() const { return mRawFormat; }
322 
326  inline const URI& GetResource() const { return mOptions.resource; }
327 
331  inline const videoOptions& GetOptions() const { return mOptions; }
332 
341  virtual inline uint32_t GetType() const { return 0; }
342 
347  inline bool IsType( uint32_t type ) const { return (type == GetType()); }
348 
356  template<typename T> bool IsType() const { return IsType(T::Type); }
357 
361  inline const char* TypeToStr() const { return TypeToStr(GetType()); }
362 
366  static const char* TypeToStr( uint32_t type );
367 
371  static const uint64_t DEFAULT_TIMEOUT=1000;
372 
373 protected:
374  //videoSource();
375  videoSource( const videoOptions& options );
376 
379 
380  uint64_t mLastTimestamp;
382 };
383 
384 #endif
videoSource::Open
virtual bool Open()
Begin streaming the device.
videoSource::Close
virtual void Close()
Stop streaming the device.
videoSource::IsType
bool IsType() const
Check if a this stream is of a particular type.
Definition: videoSource.h:356
videoSource::EOS
@ EOS
end-of-stream (EOS)
Definition: videoSource.h:127
videoSource::Usage
static const char * Usage()
Usage string for command line arguments to Create()
Definition: videoSource.h:186
videoSource::GetWidth
uint32_t GetWidth() const
Return the width of the stream, in pixels.
Definition: videoSource.h:296
videoSource::GetResource
const URI & GetResource() const
Return the resource URI of the stream.
Definition: videoSource.h:326
videoSource::Capture
bool Capture(T **image, uint64_t timeout=DEFAULT_TIMEOUT, int *status=NULL)
Capture the next image from the video stream.
Definition: videoSource.h:240
videoSource::videoSource
videoSource(const videoOptions &options)
commandLine.h
videoSource::TIMEOUT
@ TIMEOUT
a timeout occurred
Definition: videoSource.h:128
videoSource::mStreaming
bool mStreaming
Definition: videoSource.h:377
videoSource::IsType
bool IsType(uint32_t type) const
Check if this stream is of a particular type.
Definition: videoSource.h:347
videoSource::IsStreaming
bool IsStreaming() const
Check if the device is actively streaming or not.
Definition: videoSource.h:291
videoSource::mLastTimestamp
uint64_t mLastTimestamp
Definition: videoSource.h:380
videoSource::Capture
bool Capture(T **image, int *status)
Capture the next image from the video stream, using the default timeout of 1000ms.
Definition: videoSource.h:211
videoSource::ERROR
@ ERROR
an error occurred
Definition: videoSource.h:126
videoSource::mRawFormat
imageFormat mRawFormat
Definition: videoSource.h:381
videoSource::GetFrameCount
uint64_t GetFrameCount() const
Return the number of frames captured.
Definition: videoSource.h:311
videoOptions::resource
URI resource
The resource URI of the device, IP stream, or file/directory.
Definition: videoOptions.h:49
videoSource::GetFrameRate
uint32_t GetFrameRate() const
Return the framerate, in Hz or FPS.
Definition: videoSource.h:306
videoSource::GetType
virtual uint32_t GetType() const
Return the interface type of the stream.
Definition: videoSource.h:341
videoSource::GetRawFormat
imageFormat GetRawFormat() const
Get raw image format.
Definition: videoSource.h:321
URI
Resource URI of a video device, IP stream, or file/directory.
Definition: URI.h:101
videoSource::GetLastTimestamp
uint64_t GetLastTimestamp() const
Get timestamp of the last captured frame, in nanoseconds.
Definition: videoSource.h:316
videoSource::mOptions
videoOptions mOptions
Definition: videoSource.h:378
videoSource::TypeToStr
const char * TypeToStr() const
Convert this stream's class type to string.
Definition: videoSource.h:361
videoSource::~videoSource
virtual ~videoSource()
Destroy interface and release all resources.
videoSource::DEFAULT_TIMEOUT
static const uint64_t DEFAULT_TIMEOUT
The default Capture timeout (1000ms)
Definition: videoSource.h:371
videoSource::OK
@ OK
frame capture successful
Definition: videoSource.h:129
videoSource::Status
Status
Stream status codes that are optionally returned from Capture()
Definition: videoSource.h:124
videoSource::GetHeight
uint32_t GetHeight() const
Return the height of the stream, in pixels.
Definition: videoSource.h:301
VIDEO_SOURCE_USAGE_STRING
#define VIDEO_SOURCE_USAGE_STRING
Standard command-line options able to be passed to videoSource::Create()
Definition: videoSource.h:36
videoOptions
The videoOptions struct contains common settings that are used to configure and query videoSource and...
Definition: videoOptions.h:37
commandLine
Command line parser for extracting flags, values, and strings.
Definition: commandLine.h:35
videoOptions::width
uint32_t width
The width of the stream (in pixels).
Definition: videoOptions.h:64
videoSource::GetOptions
const videoOptions & GetOptions() const
Return the videoOptions of the stream.
Definition: videoSource.h:331
videoOptions::frameCount
uint64_t frameCount
The number of frames that have been captured or output on this interface.
Definition: videoOptions.h:83
videoOptions::frameRate
float frameRate
The framerate of the stream (the default is 30Hz).
Definition: videoOptions.h:78
imageFormat.h
videoOptions.h
imageFormat
imageFormat
The imageFormat enum is used to identify the pixel format and colorspace of an image.
Definition: imageFormat.h:49
videoSource::Create
static videoSource * Create(const videoOptions &options)
Create videoSource interface from a videoOptions struct that's already been filled out.
videoOptions::height
uint32_t height
The height of the stream (in pixels).
Definition: videoOptions.h:71
videoSource
The videoSource API is for capturing frames from video input devices such as MIPI CSI cameras,...
Definition: videoSource.h:118