appkit  1.5.1
RoadNarrows Robotics Application Kit
CameraGst.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_cam
8 //
9 // File: CameraGst.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-07-13 13:54:59 -0600 (Sat, 13 Jul 2013) $
14  * $Rev: 3122 $
15  *
16  * \brief Gstreamer video and still image camera class.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2012-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Permission is hereby granted, without written agreement and without
30  * license or royalty fees, to use, copy, modify, and distribute this
31  * software and its documentation for any purpose, provided that
32  * (1) The above copyright notice and the following two paragraphs
33  * appear in all copies of the source code and (2) redistributions
34  * including binaries reproduces these notices in the supporting
35  * documentation. Substantial modifications to this software may be
36  * copyrighted by their authors and need not follow the licensing terms
37  * described here, provided that the new terms are clearly indicated in
38  * all files where they apply.
39  *
40  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45  * THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  *
53  * @EulaEnd@
54  */
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 #ifndef _RNR_CAMERA_GST_H
58 #define _RNR_CAMERA_GST_H
59 
60 #include <limits.h>
61 
62 #include <string>
63 
64 #include "rnr/rnrconfig.h"
65 #include "rnr/log.h"
66 
67 #include "opencv/cv.h"
68 
69 #include <gst/gst.h>
70 
71 /*!
72  * \brief RoadNarrows Robotics
73  */
74 namespace rnr
75 {
76  //---------------------------------------------------------------------------
77  // GStreamer Camera Derived Class
78  //---------------------------------------------------------------------------
79 
80  /*!
81  * \brief GStreamer implementation of the camera class. The video is streamed
82  * via a Gstreamer/GTK callback mechanism.
83  */
84  class CameraGst : public Camera
85  {
86  public:
87  /*
88  * \brief Default initialization constructor.
89  *
90  * \param strVideoDevName Video camera device name.
91  * \param resVideo Video resolution.
92  * \param resImage Image resolution.
93  */
94  CameraGst(const std::string &strVideoDevName="/dev/video0",
95  const CamRes &resVideo=CamResQVGA,
96  const CamRes &resImage=CamResVGA);
97 
98  /*!
99  * \brief Destructor.
100  */
101  virtual ~CameraGst();
102 
103  /*!
104  * \brief Start the camera streaming video.
105  *
106  * \param resVideo Video resolution. If equal to CamResDft, then
107  * the initial/last video resolution setting is used.
108  *
109  * \copydoc doc_return_std
110  */
111  virtual int startVideo(const CamRes &resVideo=CamResDft);
112 
113  /*!
114  * \brief Stop the camera from streaming video.
115  *
116  * \copydoc doc_return_std
117  */
118  virtual int stopVideo();
119 
120  /*!
121  * \brief Grab a image frame from the video stream.
122  *
123  * \param [in,out] The image frame matrix. May be resized.
124  *
125  * \copydoc doc_return_std
126  */
127  virtual int grabFrame(cv::Mat &frame);
128 
129  /*!
130  * \brief Take a still image.
131  *
132  * \param [in,out] img Snap shot image taken.
133  * \param resImage Still image resolution. If equal to CamResDft, then
134  * the initial/last image resolution setting is used.
135  *
136  * \copydoc doc_return_std
137  */
138  virtual int clickImage(cv::Mat &img, const CamRes &resImage=CamResDft);
139 
140  /*!
141  * \brief Auto-focus camera.
142  */
143  virtual void autoFocus();
144 
145  /*!
146  * \brief Set X identifier associated with widget receiving streaming video.
147  *
148  * \param uXid X identifier.
149  */
150  void setXid(gulong uXid);
151 
152  /*!
153  * \brief Overlay text on video stream.
154  *
155  * \param sText Null-terminated string.
156  */
157  void setTextOverlay(const char *sText)
158  {
159  g_object_set(m_pElemVidText, "text", sText, NULL);
160  }
161 
162  /*!
163  * \brief Clear text overlay on video stream.
164  */
166  {
167  g_object_set(m_pElemVidText, "text", "", NULL);
168  }
169 
170  protected:
171  GstElement *m_pPipeline; ///< camera pipeline
172  GstElement *m_pBinCameraSrc; ///< camera source bin
173  GstElement *m_pElemCamFilter; ///< camera properties element
174  GstElement *m_pBinVideoSink; ///< video sink bin
175  GstElement *m_pElemVidText; ///< video text overlay
176  GstElement *m_pElemVidSink; ///< video sink
177  GstElement *m_pBinImageSink; ///< still image sink bin
178  GstElement *m_pElemImgSink; ///< still image sink
179  int m_nSignalId; ///< camera still image callback signal
180  gulong m_uVidWinXid; ///< overlay window X-windows id
181  char m_bufTmpName[PATH_MAX]; ///< temporary file name buffer
182 
183 
184  /*!
185  * \brief Set the camera resolution in either video or still image mode.
186  *
187  * \param res Target camera resolution.
188  *
189  * \return Actual resolution set.
190  */
191  virtual CamRes setCameraResolution(const CamRes &res);
192 
193  /*!
194  * \brief Start the camera and the pipeline.
195  */
196  void startPipeline();
197 
198  /*!
199  * \brief Stop the camera and the pipeline.
200  */
201  void stopPipeline();
202 
203  /*!
204  * \brief Make gstreamer video pipeline.
205  */
206  int makePipeline();
207 
208  /*!
209  * \brief Video source bin.
210  *
211  * \verbatim
212  * |---------------------------------|
213  * camera - [] v4l2src - capsfilter - identity [] -
214  * |---------------------------------|
215  * \endverbatim
216  *
217  * \copydoc doc_return_std
218  */
219  int makeCameraSrcBin();
220 
221  /*!
222  * \brief Video sink bin.
223  *
224  * \verbatim
225  * |------------------------------------------------------------------|
226  * | - queue - scale - capsfilter - colorspace - gconfvideosink |
227  * | / |
228  * - [] tee - |
229  * | \ |
230  * | - queue (to save image) []
231  * |------------------------------------------------------------------|
232  * \endverbatim
233  *
234  * \copydoc doc_return_std
235  */
236  int makeVideoSinkBin();
237 
238  /*!
239  * \brief Image sink bin.
240  *
241  * \verbatim
242  * |-----------------------| ~~ |-----
243  * - [] colorspace - fakesink | | callback
244  * |-----------------------| ~~ |-----
245  * \endverbatim
246  *
247  * \copydoc doc_return_std
248  */
249  int makeImageSinkBin();
250 
251  //void changeSink(GstElement *pBinNewSink);
252 
253  static GstBusSyncReply gstBusSyncHandler(GstBus *bus,
254  GstMessage *message,
255  gpointer user_data);
256 
257  /*!
258  * \brief Gstream pipeline synchronous bus handler.
259  *
260  * \param bus GStream bus.
261  * \param message Bus message.
262  * \param user_data User data.
263  *
264  * \return Returns GST_BUS_PASS if message not relevant for the stream.\n
265  * Returns GST_BUS_DROP otherwise.
266  */
267  static void gstBusMsgCb(GstBus *bus,
268  GstMessage *message,
269  gpointer user_data);
270 
271  /*!
272  * \brief Still image capture asynchronouse callback.
273  *
274  * This function is called when an image buffer from the camera has been
275  * retrieved.
276  *
277  * \param element GStreamer element.
278  * \param buffer Image buffer.
279  * \param pad GStreamer pad.
280  * \param user_data User data (this)
281  */
282  static void clickCb(GstElement *element,
283  GstBuffer *buffer,
284  GstPad *pad,
285  void *user_data);
286 
287 #if 0
288  static gint64 onBlockVideoSelector(GstElement *element,
289  gpointer user_data);
290 
291  static gint64 onSwitchVideoSelector(GstElement *element,
292  GstPad *pad,
293  gint64 stop_time,
294  gint64 start_time,
295  gpointer user_data);
296 #endif
297 
298  /*!
299  * \brief Make unique temporary file.
300  */
301  void makeTmpFile();
302 
303  };
304 
305 } // namespace rnr
306 
307 
308 #endif // _RNR_CAMERA_GST_H
void stopPipeline()
Stop the camera and the pipeline.
Definition: CameraGst.cxx:373
virtual ~CameraGst()
Destructor.
Definition: CameraGst.cxx:91
int makePipeline()
Make gstreamer video pipeline.
Definition: CameraGst.cxx:382
GstElement * m_pElemCamFilter
camera properties element
Definition: CameraGst.h:173
GstElement * m_pBinImageSink
still image sink bin
Definition: CameraGst.h:177
GstElement * m_pElemImgSink
still image sink
Definition: CameraGst.h:178
const CamRes CamResDft
default resolution
Definition: Camera.h:89
gulong m_uVidWinXid
overlay window X-windows id
Definition: CameraGst.h:180
int makeCameraSrcBin()
Video source bin.
Definition: CameraGst.cxx:463
virtual void autoFocus()
Auto-focus camera.
Definition: CameraGst.cxx:324
Camera base class.
Definition: Camera.h:125
char m_bufTmpName[PATH_MAX]
temporary file name buffer
Definition: CameraGst.h:181
virtual CamRes setCameraResolution(const CamRes &res)
Set the camera resolution in either video or still image mode.
Definition: CameraGst.cxx:328
const CamRes CamResQVGA
Quarter VGA 320 x 240 res.
Definition: Camera.h:90
GstElement * m_pBinVideoSink
video sink bin
Definition: CameraGst.h:174
Camera resolution structure.
Definition: Camera.h:79
int m_nSignalId
camera still image callback signal
Definition: CameraGst.h:179
void makeTmpFile()
Make unique temporary file.
Definition: CameraGst.cxx:985
void setTextOverlay(const char *sText)
Overlay text on video stream.
Definition: CameraGst.h:157
GstElement * m_pElemVidText
video text overlay
Definition: CameraGst.h:175
void clearTextOverlay()
Clear text overlay on video stream.
Definition: CameraGst.h:165
virtual int startVideo(const CamRes &resVideo=CamResDft)
Start the camera streaming video.
Definition: CameraGst.cxx:103
void setXid(gulong uXid)
Set X identifier associated with widget receiving streaming video.
Definition: CameraGst.cxx:357
GStreamer implementation of the camera class. The video is streamed via a Gstreamer/GTK callback mech...
Definition: CameraGst.h:84
GstElement * m_pPipeline
camera pipeline
Definition: CameraGst.h:171
virtual int clickImage(cv::Mat &img, const CamRes &resImage=CamResDft)
Take a still image.
Definition: CameraGst.cxx:172
GstElement * m_pBinCameraSrc
camera source bin
Definition: CameraGst.h:172
int makeVideoSinkBin()
Video sink bin.
Definition: CameraGst.cxx:563
void startPipeline()
Start the camera and the pipeline.
Definition: CameraGst.cxx:364
int makeImageSinkBin()
Image sink bin.
Definition: CameraGst.cxx:768
RoadNarrows Robotics.
Definition: Camera.h:74
GstElement * m_pElemVidSink
video sink
Definition: CameraGst.h:176
virtual int grabFrame(cv::Mat &frame)
Grab a image frame from the video stream.
Definition: CameraGst.cxx:157
static void gstBusMsgCb(GstBus *bus, GstMessage *message, gpointer user_data)
Gstream pipeline synchronous bus handler.
Definition: CameraGst.cxx:886
virtual int stopVideo()
Stop the camera from streaming video.
Definition: CameraGst.cxx:141
const CamRes CamResVGA
VGA 640 x 480 resolution.
Definition: Camera.h:91
static void clickCb(GstElement *element, GstBuffer *buffer, GstPad *pad, void *user_data)
Still image capture asynchronouse callback.
Definition: CameraGst.cxx:269