camera_usb
cam_gst.h
Go to the documentation of this file.
1 /*
2  * \file cam_gst.h
3  *
4  * \brief Allows to create a default GStreamer pipeline, which requests images using v4l2,
5  * converts them to jpegs and gives access to the image data.
6  *
7  * German Research Center for Artificial Intelligence\n
8  * Project: Rimres
9  *
10  * \date 23.11.11
11  *
12  * \author Stefan.Haase@dfki.de
13  */
14 
15 #ifndef _CAM_GST_CONFIG_H_
16 #define _CAM_GST_CONFIG_H_
17 
18 #include <glib.h>
19 #include <gst/app/gstappsink.h>
20 #include <gst/gst.h>
21 
22 #include <pthread.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/time.h>
26 #include <time.h>
27 
28 #include <iostream>
29 
30 #include "cam_config.h"
31 #include <base/samples/Frame.hpp>
32 
33 namespace camera
34 {
35 
36 class CamGstException : public std::runtime_error {
37  public:
38  CamGstException(const std::string& what_arg) : std::runtime_error(what_arg) {}
39 };
40 
47 class CamGst {
48 
49  public: // CONSTANTS
50  static const uint32_t DEFAULT_WIDTH = 640;
51  static const uint32_t DEFAULT_HEIGHT = 480;
52  static const uint32_t DEFAULT_FPS = 10;
53  static const uint32_t DEFAULT_BPP = 24;
54  static const uint32_t DEFAULT_JPEG_QUALITY = 85; // 0 to 100
55  static const uint32_t DEFAULT_PIPELINE_TIMEOUT = 4000000; // 4 sec.
56 
57  public:
64  CamGst(std::string const& device);
65 
69  ~CamGst();
70 
87  void createDefaultPipeline(bool check_for_valid_params=false,
88  uint32_t width = DEFAULT_WIDTH,
89  uint32_t height = DEFAULT_HEIGHT,
90  uint32_t fps = DEFAULT_FPS,
91  uint32_t bpp = DEFAULT_BPP,
92  base::samples::frame::frame_mode_t mode = base::samples::frame::MODE_UNDEFINED,
93  uint32_t jpeg_quality = DEFAULT_JPEG_QUALITY);
94 
98  void deletePipeline();
99 
108  bool startPipeline();
109 
114  void stopPipeline();
115 
125  bool getBuffer(std::vector<uint8_t>& buffer,
126  bool blocking_read=false, int32_t timeout=0);
127 
132  bool skipBuffer();
133 
138  bool storeImageToFile(std::vector<uint8_t> const& buffer,
139  std::string const& file_name);
140 
144  inline bool hasNewBuffer() {
145  return mNewBuffer;
146  }
147 
148  inline bool isPipelineRunning() {
149  return mPipelineRunning;
150  }
151 
157  inline int getFileDescriptor() {
158  return mFileDescriptor;
159  }
160 
161  private:
162  CamGst();
163 
169  void setCameraParameters(uint32_t* width, uint32_t* height, uint32_t* fps);
170 
171  GstElement* createDefaultSource(std::string const& device);
172 
173  GstElement* createDefaultCap(uint32_t const width, uint32_t const height, uint32_t const fps, uint32_t bpp, base::samples::frame::frame_mode_t mode );
174 
178  GstElement* createDefaultEncoder(int32_t const jpeg_quality);
179 
180  GstElement* createDefaultSink();
181 
188  bool readFileDescriptor();
189 
190  inline void rmFileDescriptor() {
191  mFileDescriptor = -1;
192  std::cout << "FD set back to -1" << std::endl;
193  }
194 
195  private: // STATIC METHODS
196  static void* mainLoop(void* ptr);
197 
201  static gboolean callbackMessagesStatic(GstBus* bus, GstMessage* msg, gpointer data);
202 
206  gboolean callbackMessages(GstBus* bus, GstMessage* msg, gpointer data);
207 
211  static void callbackNewBufferStatic(GstElement* object, CamGst* cam_gst_p);
212 
216  void callbackNewBuffer(GstElement* object, CamGst* cam_gst_p);
217 
221  void printElementFactories();
222 
223  private:
224  std::string mDevice;
225  uint32_t mJpegQuality;
226  GMainLoop* mLoop;
227  pthread_t* mMainLoopThread;
228  GstElement* mPipeline;
229  GstBus* mGstPipelineBus;
230  bool mPipelineRunning;
231 
232  pthread_mutex_t mMutexBuffer;
233  GstBuffer* mBuffer;
234  uint32_t mBufferSize;
235  bool mNewBuffer;
236 
237  GstElement* mSource; // Used to request the fd.
238  int mFileDescriptor; // File descriptor of the pipeline source. -1 if not available.
239 
240  base::samples::frame::frame_mode_t mRequestedFrameMode;
241 
246  class InitGuard {
247  public:
248  InitGuard();
249  ~InitGuard();
250  };
251 
252 };
253 
254 } // end namespace camera
255 
256 #endif
static const uint32_t DEFAULT_JPEG_QUALITY
Definition: cam_gst.h:54
Definition: cam_gst.h:36
static const uint32_t DEFAULT_WIDTH
Definition: cam_gst.h:50
static const uint32_t DEFAULT_HEIGHT
Definition: cam_gst.h:51
Definition: cam_gst.h:47
bool getBuffer(std::vector< uint8_t > &buffer, bool blocking_read=false, int32_t timeout=0)
Definition: cam_gst.cpp:234
bool skipBuffer()
Definition: cam_gst.cpp:278
bool startPipeline()
Definition: cam_gst.cpp:162
bool storeImageToFile(std::vector< uint8_t > const &buffer, std::string const &file_name)
CamGstException(const std::string &what_arg)
Definition: cam_gst.h:38
static const uint32_t DEFAULT_FPS
Definition: cam_gst.h:52
bool hasNewBuffer()
Definition: cam_gst.h:144
static const uint32_t DEFAULT_PIPELINE_TIMEOUT
Definition: cam_gst.h:55
int getFileDescriptor()
Definition: cam_gst.h:157
void deletePipeline()
Definition: cam_gst.cpp:142
bool isPipelineRunning()
Definition: cam_gst.h:148
static const uint32_t DEFAULT_BPP
Definition: cam_gst.h:53
~CamGst()
Definition: cam_gst.cpp:48
void createDefaultPipeline(bool check_for_valid_params=false, uint32_t width=DEFAULT_WIDTH, uint32_t height=DEFAULT_HEIGHT, uint32_t fps=DEFAULT_FPS, uint32_t bpp=DEFAULT_BPP, base::samples::frame::frame_mode_t mode=base::samples::frame::MODE_UNDEFINED, uint32_t jpeg_quality=DEFAULT_JPEG_QUALITY)
Definition: cam_gst.cpp:82
void stopPipeline()
Definition: cam_gst.cpp:220