Jetson Inference
DNN Vision Library
gstCamera.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, 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 __GSTREAMER_CAMERA_H__
24 #define __GSTREAMER_CAMERA_H__
25 
26 #include <gst/gst.h>
27 #include <string>
28 
29 #include "Mutex.h"
30 #include "Event.h"
31 
32 
33 // Forward declarations
34 struct _GstAppSink;
35 
36 
42 {
43  GST_SOURCE_NVCAMERA, /* use nvcamerasrc element */
44  GST_SOURCE_NVARGUS, /* use nvargussrc element */
45  GST_SOURCE_V4L2 /* use v4l2src element */
46 };
47 
52 const char* gstCameraSrcToString( gstCameraSrc src );
53 
54 
70 class gstCamera
71 {
72 public:
91  static gstCamera* Create( const char* camera=NULL ); // use MIPI CSI camera by default
92 
114  static gstCamera* Create( uint32_t width, uint32_t height, const char* camera=NULL );
115 
120  ~gstCamera();
121 
132  bool Open();
133 
140  void Close();
141 
146  inline bool IsStreaming() const { return mStreaming; }
147 
171  bool Capture( void** cpu, void** cuda, uint64_t timeout=UINT64_MAX );
172 
203  bool CaptureRGBA( float** image, uint64_t timeout=UINT64_MAX, bool zeroCopy=false );
204 
229  bool ConvertRGBA( void* input, float** output, bool zeroCopy=false );
230 
234  inline uint32_t GetWidth() const { return mWidth; }
235 
239  inline uint32_t GetHeight() const { return mHeight; }
240 
246  inline uint32_t GetPixelDepth() const { return mDepth; }
247 
257  inline uint32_t GetSize() const { return mSize; }
258 
262  static const uint32_t DefaultWidth = 1280;
263 
267  static const uint32_t DefaultHeight = 720;
268 
269 private:
270  static void onEOS(_GstAppSink* sink, void* user_data);
271  static GstFlowReturn onPreroll(_GstAppSink* sink, void* user_data);
272  static GstFlowReturn onBuffer(_GstAppSink* sink, void* user_data);
273 
274  gstCamera();
275 
276  bool init( gstCameraSrc src );
277  bool buildLaunchStr( gstCameraSrc src );
278  bool parseCameraStr( const char* camera );
279 
280  void checkMsgBus();
281  void checkBuffer();
282 
283  _GstBus* mBus;
284  _GstAppSink* mAppSink;
285  _GstElement* mPipeline;
286  gstCameraSrc mSource;
287 
288  std::string mLaunchStr;
289  std::string mCameraStr;
290 
291  uint32_t mWidth;
292  uint32_t mHeight;
293  uint32_t mDepth;
294  uint32_t mSize;
295 
296  static const uint32_t NUM_RINGBUFFERS = 16;
297 
298  void* mRingbufferCPU[NUM_RINGBUFFERS];
299  void* mRingbufferGPU[NUM_RINGBUFFERS];
300 
301  Event mWaitEvent;
302  Mutex mWaitMutex;
303  Mutex mRingMutex;
304 
305  uint32_t mLatestRGBA;
306  uint32_t mLatestRingbuffer;
307  bool mLatestRetrieved;
308 
309  void* mRGBA[NUM_RINGBUFFERS];
310  bool mRGBAZeroCopy; // were the RGBA buffers allocated with zeroCopy?
311  bool mStreaming; // true if the device is currently open
312  int mSensorCSI; // -1 for V4L2, >=0 for MIPI CSI
313 
314  inline bool csiCamera() const { return (mSensorCSI >= 0); }
315 };
316 
317 #endif
Definition: gstCamera.h:43
uint32_t GetWidth() const
Return the width of the camera.
Definition: gstCamera.h:234
MIPI CSI and V4L2 camera capture using GStreamer and nvarguscamerasrc or v4l2src elements.
Definition: gstCamera.h:70
A lightweight mutual exclusion lock.
Definition: Mutex.h:35
Definition: gstCamera.h:44
bool CaptureRGBA(float **image, uint64_t timeout=UINT64_MAX, bool zeroCopy=false)
Capture the next image frame from the camera and convert it to float4 RGBA format, with pixel intensities ranging between 0.0 and 255.0.
uint32_t GetPixelDepth() const
Return the pixel bit depth of the camera (measured in bits).
Definition: gstCamera.h:246
static const uint32_t DefaultHeight
Default camera height, unless otherwise specified during Create()
Definition: gstCamera.h:267
~gstCamera()
Release the camera interface and resources.
const char * gstCameraSrcToString(gstCameraSrc src)
Stringize function to convert gstCameraSrc enum to text.
gstCameraSrc
Enumeration of camera input source methods.
Definition: gstCamera.h:41
void Close()
Stop streaming the camera.
bool IsStreaming() const
Check if the camera is streaming or not.
Definition: gstCamera.h:146
bool ConvertRGBA(void *input, float **output, bool zeroCopy=false)
Convert an image to float4 RGBA that was previously aquired with Capture().
Definition: gstCamera.h:45
static const uint32_t DefaultWidth
Default camera width, unless otherwise specified during Create()
Definition: gstCamera.h:262
uint32_t GetHeight() const
Return the height of the camera.
Definition: gstCamera.h:239
Event object for signalling other threads.
Definition: Event.h:33
uint32_t GetSize() const
Return the size (in bytes) of a camera frame from Capture().
Definition: gstCamera.h:257
static gstCamera * Create(const char *camera=NULL)
Create a MIPI CSI or V4L2 camera device.
bool Capture(void **cpu, void **cuda, uint64_t timeout=UINT64_MAX)
Capture the next image frame from the camera.
bool Open()
Begin streaming the camera.