appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::CameraGst Class Reference

GStreamer implementation of the camera class. The video is streamed via a Gstreamer/GTK callback mechanism. More...

#include <CameraGst.h>

Inheritance diagram for rnr::CameraGst:
rnr::Camera

Public Member Functions

 CameraGst (const std::string &strVideoDevName="/dev/video0", const CamRes &resVideo=CamResQVGA, const CamRes &resImage=CamResVGA)
 
virtual ~CameraGst ()
 Destructor.
 
virtual int startVideo (const CamRes &resVideo=CamResDft)
 Start the camera streaming video. More...
 
virtual int stopVideo ()
 Stop the camera from streaming video. More...
 
virtual int grabFrame (cv::Mat &frame)
 Grab a image frame from the video stream. More...
 
virtual int clickImage (cv::Mat &img, const CamRes &resImage=CamResDft)
 Take a still image. More...
 
virtual void autoFocus ()
 Auto-focus camera.
 
void setXid (gulong uXid)
 Set X identifier associated with widget receiving streaming video. More...
 
void setTextOverlay (const char *sText)
 Overlay text on video stream. More...
 
void clearTextOverlay ()
 Clear text overlay on video stream.
 
- Public Member Functions inherited from rnr::Camera
 Camera (const std::string &strVideoDevName=VideoDevDft, const CamRes &resVideo=CamResQVGA, const CamRes &resImage=CamResVGA)
 
virtual ~Camera ()
 Destructor. More...
 
bool isCameraRunning () const
 Test if the camera is on and running. More...
 
bool isTakingAnImage () const
 Test if a still image is currently being taken. More...
 
bool isFatal () const
 Test if camera object is in a fatal condition. More...
 
CamRes getVideoResolution () const
 Get the current video resolution. More...
 
CamRes getImageResolution () const
 Get the current still image resolution. More...
 

Protected Member Functions

virtual CamRes setCameraResolution (const CamRes &res)
 Set the camera resolution in either video or still image mode. More...
 
void startPipeline ()
 Start the camera and the pipeline.
 
void stopPipeline ()
 Stop the camera and the pipeline.
 
int makePipeline ()
 Make gstreamer video pipeline.
 
int makeCameraSrcBin ()
 Video source bin. More...
 
int makeVideoSinkBin ()
 Video sink bin. More...
 
int makeImageSinkBin ()
 Image sink bin. More...
 
void makeTmpFile ()
 Make unique temporary file.
 

Static Protected Member Functions

static GstBusSyncReply gstBusSyncHandler (GstBus *bus, GstMessage *message, gpointer user_data)
 
static void gstBusMsgCb (GstBus *bus, GstMessage *message, gpointer user_data)
 Gstream pipeline synchronous bus handler. More...
 
static void clickCb (GstElement *element, GstBuffer *buffer, GstPad *pad, void *user_data)
 Still image capture asynchronouse callback. More...
 

Protected Attributes

GstElement * m_pPipeline
 camera pipeline
 
GstElement * m_pBinCameraSrc
 camera source bin
 
GstElement * m_pElemCamFilter
 camera properties element
 
GstElement * m_pBinVideoSink
 video sink bin
 
GstElement * m_pElemVidText
 video text overlay
 
GstElement * m_pElemVidSink
 video sink
 
GstElement * m_pBinImageSink
 still image sink bin
 
GstElement * m_pElemImgSink
 still image sink
 
int m_nSignalId
 camera still image callback signal
 
gulong m_uVidWinXid
 overlay window X-windows id
 
char m_bufTmpName [PATH_MAX]
 temporary file name buffer
 
- Protected Attributes inherited from rnr::Camera
std::string m_strVideoDevName
 video device name
 
int m_nVideoIndex
 video index
 
CamRes m_resCurrent
 current camera resolution
 
CamRes m_resVideo
 current video resolution
 
CamRes m_resImage
 current still image resolution
 
bool m_bCameraRunning
 camera is [not] on and running video
 
bool m_bTakingImage
 taking an image is [not] finished
 
bool m_bFatal
 camera instance is in a fatal state
 

Additional Inherited Members

- Static Public Member Functions inherited from rnr::Camera
static bool isEqResolution (const CamRes &res1, const CamRes &res2)
 Check is two camera resolutions are equal. More...
 
static int getVideoIndex (const std::string &strVideoDevName)
 Get the video index associated with the device. More...
 

Detailed Description

GStreamer implementation of the camera class. The video is streamed via a Gstreamer/GTK callback mechanism.

Definition at line 84 of file CameraGst.h.

Member Function Documentation

void CameraGst::clickCb ( GstElement *  element,
GstBuffer *  buffer,
GstPad *  pad,
void *  user_data 
)
staticprotected

Still image capture asynchronouse callback.

This function is called when an image buffer from the camera has been retrieved.

Parameters
elementGStreamer element.
bufferImage buffer.
padGStreamer pad.
user_dataUser data (this)

Definition at line 269 of file CameraGst.cxx.

References rnr::CamRes::height, m_bufTmpName, m_nSignalId, m_pElemImgSink, rnr::Camera::m_resImage, and rnr::CamRes::width.

273 {
274  CameraGst *pCam = (CameraGst *)user_data;
275 
276  GstCaps *caps;
277  const GstStructure *structure;
278  int width, height, stride;
279  GdkPixbuf *pixbuf;
280  const int bits_per_pixel = 8;
281  guchar *data;
282 
283  caps = gst_buffer_get_caps(buffer);
284  structure = gst_caps_get_structure(caps, 0);
285 
286  gst_structure_get_int(structure, "width", &width);
287  gst_structure_get_int(structure, "height", &height);
288 
289  stride = buffer->size / height;
290 
291  //
292  // Only copy the data if we're giving away a pixbuf, not if we're throwing
293  // everything away straight away
294  //
295  if (pCam->m_bufTmpName[0] != 0 )
296  {
297  data = NULL;
298  }
299  else
300  {
301  data = (guchar *)g_memdup(GST_BUFFER_DATA(buffer), buffer->size);
302  }
303 
304  pixbuf = gdk_pixbuf_new_from_data(data? data: GST_BUFFER_DATA(buffer),
305  GDK_COLORSPACE_RGB,
306  FALSE, bits_per_pixel, width, height, stride,
307  data? (GdkPixbufDestroyNotify)g_free: NULL, NULL);
308 
309  g_signal_handler_disconnect(G_OBJECT(pCam->m_pElemImgSink),
310  pCam->m_nSignalId);
311 
312  if (pCam->m_bufTmpName[0] != 0 )
313  {
314  gdk_pixbuf_save(pixbuf, pCam->m_bufTmpName, "jpeg", NULL, NULL);
315  g_object_unref(G_OBJECT(pixbuf));
316  }
317 
318  pCam->m_resImage.width = width;
319  pCam->m_resImage.height = height;
320 
321  pCam->m_nSignalId = 0;
322 }
GstElement * m_pElemImgSink
still image sink
Definition: CameraGst.h:178
char m_bufTmpName[PATH_MAX]
temporary file name buffer
Definition: CameraGst.h:181
int m_nSignalId
camera still image callback signal
Definition: CameraGst.h:179
int width
width in pixels
Definition: Camera.h:81
GStreamer implementation of the camera class. The video is streamed via a Gstreamer/GTK callback mech...
Definition: CameraGst.h:84
int height
height in pixels
Definition: Camera.h:82
CamRes m_resImage
current still image resolution
Definition: Camera.h:297
int CameraGst::clickImage ( cv::Mat &  img,
const CamRes resImage = CamResDft 
)
virtual

Take a still image.

Parameters
[in,out]imgSnap shot image taken.
resImageStill image resolution. If equal to CamResDft, then the initial/last image resolution setting is used.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Reimplemented from rnr::Camera.

Definition at line 172 of file CameraGst.cxx.

References rnr::CamResDft, rnr::Camera::isCameraRunning(), rnr::Camera::isEqResolution(), rnr::Camera::m_bTakingImage, rnr::Camera::m_resCurrent, rnr::Camera::m_resImage, rnr::Camera::m_resVideo, rnr::CameraCv::setCameraResolution(), rnr::CameraCv::startVideo(), and rnr::CameraCv::stopVideo().

173 {
174  CamRes resVideoPrev = m_resVideo;
175  bool bCamWasRunning = isCameraRunning();
176  CamRes res = resImage;
177  uint_t usecSleep = 10000; // 10 milliseconds
178  uint_t usecMax = 10000000; // 10 seconds
179  uint_t usec;
180  int rc;
181 
182  if( m_bTakingImage || (m_nSignalId != 0) )
183  {
184  LOGDIAG3("Image capture already in progress.");
185  return RC_ERROR;
186  }
187 
188  m_bTakingImage = true;
189 
190  // default is the current image resolution
191  if( isEqResolution(res, CamResDft) )
192  {
193  res = m_resImage;
194  }
195 
196  // new resolution
197  if( !isEqResolution(res, m_resCurrent) )
198  {
199  stopPipeline();
200  setCameraResolution(res);
201  }
202 
203  // start camera
204  if( !isCameraRunning() )
205  {
206  startPipeline();
207  }
208 
209  m_resImage = res;
210 
211  // take the photo by connecting the handoff signal
212  m_nSignalId = g_signal_connect(G_OBJECT(m_pElemImgSink),
213  "handoff",
214  G_CALLBACK(clickCb),
215  this);
216 
217  //
218  // Block wait with timeout
219  //
220  for(usec = 0; (usec < usecMax) && (m_nSignalId != 0); usec += usecSleep)
221  {
222  gtk_main_iteration_do(FALSE);
223 
224  if( m_nSignalId != 0 )
225  {
226  usleep(usecSleep);
227  }
228  }
229 
230  // timed out
231  if( m_nSignalId != 0 )
232  {
233  // g_signal_handler_disconnect(G_OBJECT(m_pElemImgSink), m_nSignalId);
234  m_nSignalId = 0;
235  LOGERROR("Timedout taking still image.");
236  rc = RC_ERROR;
237  }
238 
239  // got an image
240  else
241  {
242  img = imread(m_bufTmpName, CV_LOAD_IMAGE_COLOR);
243  LOGDIAG3("Took a %dx%d still image.", img.cols, img.rows);
244  rc = OK;
245  }
246 
247  // video was running
248  if( bCamWasRunning )
249  {
250  // restart at different resolution
252  {
254  }
255  // else live running at current resolution
256  }
257 
258  // video only started to take image - stop
259  else
260  {
261  stopVideo();
262  }
263 
264  m_bTakingImage = false;
265 
266  return rc;
267 }
void stopPipeline()
Stop the camera and the pipeline.
Definition: CameraGst.cxx:373
static bool isEqResolution(const CamRes &res1, const CamRes &res2)
Check is two camera resolutions are equal.
Definition: Camera.h:257
CamRes m_resCurrent
current camera resolution
Definition: Camera.h:295
GstElement * m_pElemImgSink
still image sink
Definition: CameraGst.h:178
const CamRes CamResDft
default resolution
Definition: Camera.h:89
bool m_bTakingImage
taking an image is [not] finished
Definition: Camera.h:299
CamRes m_resVideo
current video resolution
Definition: Camera.h:296
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
Camera resolution structure.
Definition: Camera.h:79
int m_nSignalId
camera still image callback signal
Definition: CameraGst.h:179
virtual int startVideo(const CamRes &resVideo=CamResDft)
Start the camera streaming video.
Definition: CameraGst.cxx:103
CamRes m_resImage
current still image resolution
Definition: Camera.h:297
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224
void startPipeline()
Start the camera and the pipeline.
Definition: CameraGst.cxx:364
virtual int stopVideo()
Stop the camera from streaming video.
Definition: CameraGst.cxx:141
static void clickCb(GstElement *element, GstBuffer *buffer, GstPad *pad, void *user_data)
Still image capture asynchronouse callback.
Definition: CameraGst.cxx:269
int CameraGst::grabFrame ( cv::Mat &  frame)
virtual

Grab a image frame from the video stream.

Parameters
[in,out]Theimage frame matrix. May be resized.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Reimplemented from rnr::Camera.

Definition at line 157 of file CameraGst.cxx.

References rnr::CameraCv::clickImage(), rnr::Camera::isCameraRunning(), and rnr::Camera::m_resVideo.

158 {
159  if( !isCameraRunning() )
160  {
161  LOGERROR("No video capture pipeline started.");
162  return RC_ERROR;
163  }
164 
165  // grab a frame
166  else
167  {
168  return clickImage(frame, m_resVideo);
169  }
170 }
CamRes m_resVideo
current video resolution
Definition: Camera.h:296
virtual int clickImage(cv::Mat &img, const CamRes &resImage=CamResDft)
Take a still image.
Definition: CameraGst.cxx:172
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224
void CameraGst::gstBusMsgCb ( GstBus *  bus,
GstMessage *  message,
gpointer  user_data 
)
staticprotected

Gstream pipeline synchronous bus handler.

Parameters
busGStream bus.
messageBus message.
user_dataUser data.
Returns
Returns GST_BUS_PASS if message not relevant for the stream.
Returns GST_BUS_DROP otherwise.

Definition at line 886 of file CameraGst.cxx.

References m_uVidWinXid.

889 {
890  CameraGst *pCam = (CameraGst *)user_data;
891 
892  switch( GST_MESSAGE_TYPE(message) )
893  {
894  case GST_MESSAGE_EOS:
895  LOGDIAG3("GST: End of stream.");
896  break;
897 
898  case GST_MESSAGE_ERROR:
899  {
900  gchar *debug;
901  GError *error;
902 
903  gst_message_parse_error(message, &error, &debug);
904  g_free(debug);
905  LOGERROR("GST: %s.", error->message);
906  g_error_free(error);
907  }
908  break;
909 
910  default:
911  //fprintf(stderr, "DBG: GST: busmsg=%u\n", GST_MESSAGE_TYPE(message));
912  break;
913  }
914 }
GStreamer implementation of the camera class. The video is streamed via a Gstreamer/GTK callback mech...
Definition: CameraGst.h:84
int CameraGst::makeCameraSrcBin ( )
protected

Video source bin.

*           |---------------------------------|
* camera - [] v4l2src - capsfilter - identity [] -
*           |---------------------------------|
* 
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 463 of file CameraGst.cxx.

References rnr::CamRes::height, rnr::Camera::m_bFatal, rnr::Camera::m_resVideo, rnr::Camera::m_strVideoDevName, and rnr::CamRes::width.

464 {
465  const char *sFactoryName;
466  GstElement *pElemCamSrc;
467  GstElement *pElemCamIdentity;
468  GstCaps *caps;
469  GstPad *pad;
470  gboolean ok;
471 
472  m_pBinCameraSrc = gst_bin_new("camera_bin");
473 
474  if( m_pBinCameraSrc == NULL )
475  {
476  LOGERROR("Failed to create new gstreamer camera bin.");
477  m_bFatal = true;
478  return RC_ERROR;
479  }
480 
481  //
482  // Source element
483  //
484  sFactoryName = "v4l2src";
485  pElemCamSrc = gst_element_factory_make(sFactoryName, "cam_src");
486 
487  if( pElemCamSrc == NULL )
488  {
489  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
490  m_bFatal = true;
491  return RC_ERROR;
492  }
493 
494  g_object_set(G_OBJECT(pElemCamSrc),
495  "device", m_strVideoDevName.c_str(),
496  NULL);
497 
498  //
499  // Filter Element
500  //
501  sFactoryName = "capsfilter";
502  m_pElemCamFilter = gst_element_factory_make(sFactoryName, "cam_caps");
503 
504  if( m_pElemCamFilter == NULL )
505  {
506  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
507  m_bFatal = true;
508  return RC_ERROR;
509  }
510 
511  caps = gst_caps_new_simple("video/x-raw-yuv",
512  "width", G_TYPE_INT, m_resVideo.width,
513  "height", G_TYPE_INT, m_resVideo.height,
514  NULL);
515 
516  g_object_set(G_OBJECT(m_pElemCamFilter), "caps", caps, NULL);
517  gst_caps_unref(caps);
518 
520 
521  //
522  // Identity element
523  //
524  sFactoryName = "identity";
525  pElemCamIdentity = gst_element_factory_make(sFactoryName, "cam_identity");
526 
527  if( pElemCamIdentity == NULL )
528  {
529  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
530  m_bFatal = true;
531  return RC_ERROR;
532  }
533 
534  gst_bin_add_many(GST_BIN(m_pBinCameraSrc),
535  pElemCamSrc,
537  pElemCamIdentity,
538  NULL);
539 
540  ok = gst_element_link_many(
541  pElemCamSrc,
543  pElemCamIdentity,
544  NULL);
545 
546  if( !ok )
547  {
548  LOGERROR("Failed to link camera gstreamer elements.");
549  m_bFatal = true;
550  return RC_ERROR;
551  }
552 
553  //
554  // Add souce ghostpad since bins normally do not have pads.
555  //
556  pad = gst_element_get_pad(pElemCamIdentity, "src");
557  gst_element_add_pad(m_pBinCameraSrc, gst_ghost_pad_new("src", pad));
558  gst_object_unref(GST_OBJECT(pad));
559 
560  return OK;
561 }
GstElement * m_pElemCamFilter
camera properties element
Definition: CameraGst.h:173
CamRes m_resVideo
current video resolution
Definition: Camera.h:296
int width
width in pixels
Definition: Camera.h:81
int height
height in pixels
Definition: Camera.h:82
std::string m_strVideoDevName
video device name
Definition: Camera.h:293
GstElement * m_pBinCameraSrc
camera source bin
Definition: CameraGst.h:172
bool m_bFatal
camera instance is in a fatal state
Definition: Camera.h:300
int CameraGst::makeImageSinkBin ( )
protected

Image sink bin.

*    |-----------------------|  ~~  |-----
* - [] colorspace - fakesink |      | callback
*    |-----------------------|  ~~  |-----
* 
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 768 of file CameraGst.cxx.

References rnr::Camera::m_bFatal.

769 {
770  const char *sFactoryName;
771  GstElement *pElemImgFfmpeg;
772  GstCaps *caps;
773  GstPad *pad;
774  gboolean ok;
775 
776  m_pBinImageSink = gst_bin_new("image_bin");
777 
778  if( m_pBinImageSink == NULL )
779  {
780  LOGERROR("Failed to create new gstreamer image bin.");
781  m_bFatal = true;
782  return RC_ERROR;
783  }
784 
785  //
786  // Colorspace element
787  //
788  sFactoryName = "ffmpegcolorspace";
789  pElemImgFfmpeg = gst_element_factory_make(sFactoryName, "img_color");
790 
791  if( pElemImgFfmpeg == NULL )
792  {
793  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
794  m_bFatal = true;
795  return RC_ERROR;
796  }
797 
798  //
799  // Sink element
800  //
801  sFactoryName = "fakesink";
802  m_pElemImgSink = gst_element_factory_make(sFactoryName, "img_sink");
803 
804  if( m_pElemImgSink == NULL )
805  {
806  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
807  m_bFatal = true;
808  return RC_ERROR;
809  }
810 
811  g_object_set(G_OBJECT(m_pElemImgSink),
812  "signal-handoffs", TRUE,
813  NULL);
814 
815  //
816  // Add elements to the pipeline
817  //
818  gst_bin_add_many(GST_BIN(m_pBinImageSink),
819  pElemImgFfmpeg,
821  NULL);
822 
823  caps = gst_caps_new_simple("video/x-raw-rgb",
824  "bpp", G_TYPE_INT, 24,
825  "depth", G_TYPE_INT, 24,
826  NULL);
827  ok = gst_element_link_filtered(pElemImgFfmpeg, m_pElemImgSink, caps);
828 
829  if( !ok )
830  {
831  LOGERROR("Failed to set filtered link.");
832  m_bFatal = true;
833  return RC_ERROR;
834  }
835 
836  gst_caps_unref(caps);
837 
838 
839  //
840  // Add sink ghostpad since bins normally do not have pads.
841  //
842  pad = gst_element_get_pad(pElemImgFfmpeg, "sink");
843  gst_element_add_pad(m_pBinImageSink, gst_ghost_pad_new("sink", pad));
844  gst_object_unref(GST_OBJECT(pad));
845 
846  return OK;
847 }
GstElement * m_pBinImageSink
still image sink bin
Definition: CameraGst.h:177
GstElement * m_pElemImgSink
still image sink
Definition: CameraGst.h:178
bool m_bFatal
camera instance is in a fatal state
Definition: Camera.h:300
int CameraGst::makeVideoSinkBin ( )
protected

Video sink bin.

*    |------------------------------------------------------------------|
*    |       - queue - scale - capsfilter - colorspace - gconfvideosink |
*    |      /                                                           |
* - [] tee -                                                            |
*    |      \                                                           |
*    |       - queue (to save image)                                    []
*    |------------------------------------------------------------------|
* 
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 563 of file CameraGst.cxx.

References rnr::Camera::m_bFatal.

564 {
565  const char *sFactoryName;
566  GstElement *pElemVidIdentity;
567  GstElement *pElemVidTee;
568  GstElement *pElemVidSaveQueue;
569  GstElement *pElemVidDispQueue;
570  GstElement *pElemVidScale;
571  GstElement *pElemVidFilter;
572  GstElement *pElemVidFfmpeg;
573  GstCaps *caps;
574  GstPad *pad;
575  gboolean ok;
576 
577  m_pBinVideoSink = gst_bin_new("video_bin");
578 
579  if( m_pBinVideoSink == NULL )
580  {
581  LOGERROR("Failed to create new gstreamer video bin.");
582  m_bFatal = true;
583  return RC_ERROR;
584  }
585 
586  //
587  // Tee element
588  //
589  sFactoryName = "tee";
590  pElemVidTee = gst_element_factory_make(sFactoryName, "vid_tee");
591 
592  if( pElemVidTee == NULL )
593  {
594  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
595  m_bFatal = true;
596  return RC_ERROR;
597  }
598 
599  //
600  // Save queue element
601  //
602  sFactoryName = "queue";
603  pElemVidSaveQueue = gst_element_factory_make(sFactoryName, "vid_save_queue");
604 
605  if( pElemVidSaveQueue == NULL )
606  {
607  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
608  m_bFatal = true;
609  return RC_ERROR;
610  }
611 
612  g_object_set(G_OBJECT(pElemVidSaveQueue),
613  "max-size-buffers", 2,
614  NULL);
615 
616  //
617  // Video display queue element
618  //
619  sFactoryName = "queue";
620  pElemVidDispQueue = gst_element_factory_make(sFactoryName, "vid_disp_queue");
621 
622  if( pElemVidDispQueue == NULL )
623  {
624  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
625  m_bFatal = true;
626  return RC_ERROR;
627  }
628 
629  //
630  // Scale element
631  //
632  sFactoryName = "videoscale";
633  pElemVidScale = gst_element_factory_make(sFactoryName, "vid_scale");
634 
635  if( pElemVidScale == NULL )
636  {
637  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
638  m_bFatal = true;
639  return RC_ERROR;
640  }
641 
642  // use bilinear scaling
643  g_object_set(pElemVidScale, "method", 1, NULL);
644 
645  //
646  // Filter Element
647  //
648  sFactoryName = "capsfilter";
649  pElemVidFilter = gst_element_factory_make(sFactoryName, "vid_caps");
650 
651  if( pElemVidFilter == NULL )
652  {
653  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
654  m_bFatal = true;
655  return RC_ERROR;
656  }
657 
658  caps = gst_caps_new_simple("video/x-raw-yuv",
659  "width", G_TYPE_INT, 320,
660  "height", G_TYPE_INT, 240,
661  NULL);
662 
663  g_object_set(G_OBJECT(pElemVidFilter), "caps", caps, NULL);
664  gst_caps_unref(caps);
665 
666  //
667  // Text overlay element
668  //
669  sFactoryName = "textoverlay";
670  m_pElemVidText = gst_element_factory_make(sFactoryName, "vid_text");
671 
672  if( m_pElemVidText == NULL )
673  {
674  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
675  m_bFatal = true;
676  return RC_ERROR;
677  }
678 
679  // no text
680  g_object_set(m_pElemVidText,
681  "text", "",
682  "font-desc", "Aria 32",
683  NULL);
684 
685  //
686  // Color space element
687  //
688  sFactoryName = "ffmpegcolorspace";
689  pElemVidFfmpeg = gst_element_factory_make(sFactoryName, "vid_color");
690 
691  if( pElemVidFfmpeg == NULL )
692  {
693  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
694  m_bFatal = true;
695  return RC_ERROR;
696  }
697 
698  //
699  // Display sink element
700  //
701  sFactoryName = "gconfvideosink";
702  m_pElemVidSink = gst_element_factory_make(sFactoryName, "vid_sink");
703 
704  if( m_pElemVidSink == NULL )
705  {
706  LOGERROR("Failed to create gstreamer element: %s.", sFactoryName);
707  m_bFatal = true;
708  return RC_ERROR;
709  }
710 
711  gst_bin_add_many(GST_BIN(m_pBinVideoSink),
712  pElemVidTee,
713  pElemVidSaveQueue,
714  pElemVidDispQueue,
715  pElemVidScale,
716  pElemVidFilter,
718  pElemVidFfmpeg,
720  NULL);
721 
722  ok = gst_element_link_many(
723  pElemVidTee,
724  pElemVidSaveQueue,
725  NULL);
726 
727  if( !ok )
728  {
729  LOGERROR("Failed to link video tee save gstreamer elements.");
730  m_bFatal = true;
731  return RC_ERROR;
732  }
733 
734  ok = gst_element_link_many(
735  pElemVidTee,
736  pElemVidDispQueue,
737  pElemVidScale,
738  pElemVidFilter,
740  pElemVidFfmpeg,
742  NULL);
743 
744  if( !ok )
745  {
746  LOGERROR("Failed to link video tee display gstreamer elements.");
747  m_bFatal = true;
748  return RC_ERROR;
749  }
750 
751  //
752  // Add sink ghostpad since bins normally do not have pads.
753  //
754  pad = gst_element_get_pad(pElemVidTee, "sink");
755  gst_element_add_pad(m_pBinVideoSink, gst_ghost_pad_new("sink", pad));
756  gst_object_unref(GST_OBJECT(pad));
757 
758  //
759  // Add source ghostpad to save stream
760  //
761  pad = gst_element_get_pad(pElemVidSaveQueue, "src");
762  gst_element_add_pad(m_pBinVideoSink, gst_ghost_pad_new("src", pad));
763  gst_object_unref(GST_OBJECT(pad));
764 
765  return OK;
766 }
GstElement * m_pBinVideoSink
video sink bin
Definition: CameraGst.h:174
GstElement * m_pElemVidText
video text overlay
Definition: CameraGst.h:175
GstElement * m_pElemVidSink
video sink
Definition: CameraGst.h:176
bool m_bFatal
camera instance is in a fatal state
Definition: Camera.h:300
CamRes CameraGst::setCameraResolution ( const CamRes res)
protectedvirtual

Set the camera resolution in either video or still image mode.

Parameters
resTarget camera resolution.
Returns
Actual resolution set.

Reimplemented from rnr::Camera.

Definition at line 328 of file CameraGst.cxx.

References rnr::CamRes::height, rnr::Camera::isCameraRunning(), rnr::Camera::m_resCurrent, and rnr::CamRes::width.

329 {
330  bool bWasRunning;
331  GstCaps *caps;
332 
333  // camera pipeline must be stopped before applying capabilities
334  if( (bWasRunning = isCameraRunning()) )
335  {
336  stopPipeline();
337  }
338 
339  caps = gst_caps_new_simple("video/x-raw-yuv",
340  "width", G_TYPE_INT, res.width,
341  "height", G_TYPE_INT, res.height,
342  NULL);
343 
344  g_object_set(G_OBJECT(m_pElemCamFilter), "caps", caps, NULL);
345  gst_caps_unref(caps);
346 
347  m_resCurrent = res;
348 
349  if( bWasRunning )
350  {
351  startPipeline();
352  }
353 
354  return m_resCurrent;
355 }
void stopPipeline()
Stop the camera and the pipeline.
Definition: CameraGst.cxx:373
GstElement * m_pElemCamFilter
camera properties element
Definition: CameraGst.h:173
CamRes m_resCurrent
current camera resolution
Definition: Camera.h:295
int width
width in pixels
Definition: Camera.h:81
int height
height in pixels
Definition: Camera.h:82
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224
void startPipeline()
Start the camera and the pipeline.
Definition: CameraGst.cxx:364
void rnr::CameraGst::setTextOverlay ( const char *  sText)
inline

Overlay text on video stream.

Parameters
sTextNull-terminated string.

Definition at line 157 of file CameraGst.h.

References m_pElemVidText.

158  {
159  g_object_set(m_pElemVidText, "text", sText, NULL);
160  }
GstElement * m_pElemVidText
video text overlay
Definition: CameraGst.h:175
void CameraGst::setXid ( gulong  uXid)

Set X identifier associated with widget receiving streaming video.

Parameters
uXidX identifier.

Definition at line 357 of file CameraGst.cxx.

358 {
359  m_uVidWinXid = uXid;
360 
361  //g_object_set(m_pElemVidGenSrc, "xid", m_uVidWinXid, NULL);
362 }
gulong m_uVidWinXid
overlay window X-windows id
Definition: CameraGst.h:180
int CameraGst::startVideo ( const CamRes resVideo = CamResDft)
virtual

Start the camera streaming video.

Parameters
resVideoVideo resolution. If equal to CamResDft, then the initial/last video resolution setting is used.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Reimplemented from rnr::Camera.

Definition at line 103 of file CameraGst.cxx.

References rnr::CamResDft, rnr::CamRes::height, rnr::Camera::isCameraRunning(), rnr::Camera::isEqResolution(), rnr::Camera::m_resCurrent, rnr::Camera::m_resVideo, rnr::CameraCv::setCameraResolution(), and rnr::CamRes::width.

104 {
105  CamRes res = resVideo;
106 
107  // default is the current video resolution
108  if( isEqResolution(res, CamResDft) )
109  {
110  res = m_resVideo;
111  }
112 
113  // start video with the given resolution
114  if( !isCameraRunning() )
115  {
116  setCameraResolution(res);
117  startPipeline();
118  }
119 
120  // resolution is different from current setting
121  else if( !isEqResolution(res, m_resCurrent) )
122  {
123  stopPipeline();
124  setCameraResolution(res);
125  startPipeline();
126  }
127 
128  // camera already running at target resolution
129  else
130  {
131  LOGDIAG3("Camera already started with target resolution.");
132  }
133 
135 
136  LOGDIAG3("Capturing %dx%d video.", m_resVideo.width, m_resVideo.height);
137 
138  return OK;
139 }
void stopPipeline()
Stop the camera and the pipeline.
Definition: CameraGst.cxx:373
static bool isEqResolution(const CamRes &res1, const CamRes &res2)
Check is two camera resolutions are equal.
Definition: Camera.h:257
CamRes m_resCurrent
current camera resolution
Definition: Camera.h:295
const CamRes CamResDft
default resolution
Definition: Camera.h:89
CamRes m_resVideo
current video resolution
Definition: Camera.h:296
virtual CamRes setCameraResolution(const CamRes &res)
Set the camera resolution in either video or still image mode.
Definition: CameraGst.cxx:328
Camera resolution structure.
Definition: Camera.h:79
int width
width in pixels
Definition: Camera.h:81
int height
height in pixels
Definition: Camera.h:82
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224
void startPipeline()
Start the camera and the pipeline.
Definition: CameraGst.cxx:364
int CameraGst::stopVideo ( )
virtual

Stop the camera from streaming video.

Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Reimplemented from rnr::Camera.

Definition at line 141 of file CameraGst.cxx.

References rnr::Camera::isCameraRunning().

142 {
143  if( !isCameraRunning() )
144  {
145  LOGDIAG3("Video capture already stoped.");
146  }
147 
148  else
149  {
150  stopPipeline();
151  LOGDIAG3("Video capturing stopped.");
152  }
153 
154  return OK;
155 }
void stopPipeline()
Stop the camera and the pipeline.
Definition: CameraGst.cxx:373
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224

The documentation for this class was generated from the following files: