Could anybody help me please?<div><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Aldo Biziak</b> <span dir="ltr"><<a href="mailto:aldobiziak@gmail.com">aldobiziak@gmail.com</a>></span><br>
Date: 2012/3/1<br>Subject: gst_app_sink_pull_buffer takes a long time to get data from a pipeline (about 130ms)<br>To: <a href="mailto:gstreamer-devel@lists.freedesktop.org">gstreamer-devel@lists.freedesktop.org</a><br>Cc: luca gherardi <<a href="mailto:lucaghera8@gmail.com">lucaghera8@gmail.com</a>><br>
<br><br><div>Good morning all,</div><div>I'm building a simple C application that use appsink to get buffer of data from a pipeline (this last pipeline creates data reading images from a webcam 30fps):</div><div>GstBuffer* buf = gst_app_sink_pull_buffer(GST_APP_SINK(sink));</div>
<div>this function require at least 130ms (7.6fps) of processing time, so I can't grab images in real time mode (30fps).</div><div><br></div><div>I compiled my application with -O2 optimizer option and I use a PC not bad with a dual core 2.1GHz (x86).</div>
<div>How to get a faster function to work in real time mode?</div><div><br></div><div>This is the code of my application:</div><div><div>#include <gst/gst.h></div><div>#include <gst/app/gstappsink.h></div><div>
#include <gst/app/gstappbuffer.h></div><div>#include <gst/base/gstbasesink.h></div><div><br></div><div>#include <string></div><div>#include <iostream></div><div>#include <sstream></div><div>
#include <map></div>
<div>#include <vector></div><div><br></div><div>/***** section to measure performances***********/</div><div>#define DEBUG_FRAMERATE</div><div>#ifdef DEBUG_FRAMERATE</div><div>#include <time.h></div><div>#include <sys/time.h></div>
<div>#define NUM_OF_FRAMES_TO_CATCH_TO_CALCULATE_FPS 30</div><div>int frame_counter =0;</div><div>struct timespec timestamp1;</div><div>#endif</div><div>/**************************************************/</div><div>using namespace std;</div>
<div>typedef struct</div><div>{</div><div> GMainLoop *loop;</div><div> GstElement *source;</div><div> GstElement *sink;</div><div>}ProgramData;</div><div><br></div><div>int main(char **argc, int argv[]){</div><div>string cameraDevice;</div>
<div><span style="white-space:pre-wrap">        </span>bool alwaysCopy;</div><div><span style="white-space:pre-wrap">        </span>string encoding;</div><div><span style="white-space:pre-wrap">        </span>string formatSpecificEncoding;</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>int width;</div><div><span style="white-space:pre-wrap">        </span>int height;</div><div><span style="white-space:pre-wrap">        </span>int bpp; // bit per pixel</div>
<div><span style="white-space:pre-wrap">        </span>int depth;</div><div><span style="white-space:pre-wrap">        </span>int redMask;</div><div><span style="white-space:pre-wrap">        </span>int greenMask;</div>
<div><span style="white-space:pre-wrap">        </span>int blueMask;</div><div><span style="white-space:pre-wrap">        </span>int endianness;</div><div><span style="white-space:pre-wrap">        </span>bool interlaced;</div>
<div><span style="white-space:pre-wrap">        </span>int framerate;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>int brightness;</div><div><span style="white-space:pre-wrap">        </span>int contrast;</div>
<div><span style="white-space:pre-wrap">        </span>int saturation;</div><div><span style="white-space:pre-wrap">        </span>int hue;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>bool gstreamerPad;</div>
<div><span style="white-space:pre-wrap">        </span>bool rosPad;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>int num;</div><div><span style="white-space:pre-wrap">        </span>double meanTime;</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>GstElement *pipeline;</div><div><span style="white-space:pre-wrap">        </span>GstElement *sink;</div><div><span style="white-space:pre-wrap">        </span>cameraDevice = "/dev/video0";</div>
<div><span style="white-space:pre-wrap">                </span>alwaysCopy = false;</div><div><span style="white-space:pre-wrap">                </span>encoding = "video/x-raw-rgb";</div><div><span style="white-space:pre-wrap">                </span>formatSpecificEncoding = "YUY2";</div>
<div><br></div><div><span style="white-space:pre-wrap">                </span>width = 640;</div><div><span style="white-space:pre-wrap">                </span>height = 480;</div><div><span style="white-space:pre-wrap">                </span>bpp = 24;</div>
<div><span style="white-space:pre-wrap">                </span>depth = 24;</div><div><span style="white-space:pre-wrap">                </span>redMask = 16711680;</div><div><span style="white-space:pre-wrap">                </span>greenMask = 65280;</div>
<div><span style="white-space:pre-wrap">                </span>blueMask = 255;</div><div><span style="white-space:pre-wrap">                </span>endianness = 4321;</div><div><span style="white-space:pre-wrap">                </span>interlaced = false;</div>
<div><span style="white-space:pre-wrap">                </span>framerate = 15;</div><div><br></div><div><span style="white-space:pre-wrap">                </span>brightness = 0;</div><div><span style="white-space:pre-wrap">                </span>contrast = 0;</div>
<div><span style="white-space:pre-wrap">                </span>saturation = 0;</div><div><span style="white-space:pre-wrap">                </span>hue = 0;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>meanTime = 0;</div>
<div><span style="white-space:pre-wrap">        </span>num = 0;</div><div><br></div><div>stringstream camConfigStream;#include <gst/gst.h></div><div>#include <gst/app/gstappsink.h></div><div>#include <gst/app/gstappbuffer.h></div>
<div>#include <gst/base/gstbasesink.h></div><div><br></div><div>#include <string></div><div>#include <iostream></div><div>#include <sstream></div><div>#include <map></div><div>#include <vector></div>
<div><br></div><div>/***** section to measure performances***********/</div><div>#define DEBUG_FRAMERATE</div><div>#ifdef DEBUG_FRAMERATE</div><div>#include <time.h></div><div>#include <sys/time.h></div><div>
#define NUM_OF_FRAMES_TO_CATCH_TO_CALCULATE_FPS 30</div>
<div>int frame_counter =0;</div><div>struct timespec timestamp1;</div><div>#endif</div><div>/**************************************************/</div><div>using namespace std;</div><div>typedef struct</div><div>{</div><div>
GMainLoop *loop;</div><div> GstElement *source;</div><div> GstElement *sink;</div><div>}ProgramData;</div><div><br></div><div>int main(char **argc, int argv[]){</div><div>string cameraDevice;</div><div><span style="white-space:pre-wrap">        </span>bool alwaysCopy;</div>
<div><span style="white-space:pre-wrap">        </span>string encoding;</div><div><span style="white-space:pre-wrap">        </span>string formatSpecificEncoding;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>int width;</div>
<div><span style="white-space:pre-wrap">        </span>int height;</div><div><span style="white-space:pre-wrap">        </span>int bpp; // bit per pixel</div><div><span style="white-space:pre-wrap">        </span>int depth;</div>
<div><span style="white-space:pre-wrap">        </span>int redMask;</div><div><span style="white-space:pre-wrap">        </span>int greenMask;</div><div><span style="white-space:pre-wrap">        </span>int blueMask;</div>
<div><span style="white-space:pre-wrap">        </span>int endianness;</div><div><span style="white-space:pre-wrap">        </span>bool interlaced;</div><div><span style="white-space:pre-wrap">        </span>int framerate;</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>int brightness;</div><div><span style="white-space:pre-wrap">        </span>int contrast;</div><div><span style="white-space:pre-wrap">        </span>int saturation;</div>
<div><span style="white-space:pre-wrap">        </span>int hue;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>bool gstreamerPad;</div><div><span style="white-space:pre-wrap">        </span>bool rosPad;</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>int num;</div><div><span style="white-space:pre-wrap">        </span>double meanTime;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>GstElement *pipeline;</div>
<div><span style="white-space:pre-wrap">        </span>GstElement *sink;</div><div><span style="white-space:pre-wrap">        </span>cameraDevice = "/dev/video0";</div><div><span style="white-space:pre-wrap">                </span>alwaysCopy = false;</div>
<div><span style="white-space:pre-wrap">                </span>encoding = "video/x-raw-rgb";</div><div><span style="white-space:pre-wrap">                </span>formatSpecificEncoding = "YUY2";</div>
<div><br></div><div><span style="white-space:pre-wrap">                </span>width = 640;</div><div><span style="white-space:pre-wrap">                </span>height = 480;</div><div><span style="white-space:pre-wrap">                </span>bpp = 24;</div>
<div><span style="white-space:pre-wrap">                </span>depth = 24;</div><div><span style="white-space:pre-wrap">                </span>redMask = 16711680;</div><div><span style="white-space:pre-wrap">                </span>greenMask = 65280;</div>
<div><span style="white-space:pre-wrap">                </span>blueMask = 255;</div><div><span style="white-space:pre-wrap">                </span>endianness = 4321;</div><div><span style="white-space:pre-wrap">                </span>interlaced = false;</div>
<div><span style="white-space:pre-wrap">                </span>framerate = 15;</div><div><br></div><div><span style="white-space:pre-wrap">                </span>brightness = 0;</div><div><span style="white-space:pre-wrap">                </span>contrast = 0;</div>
<div><span style="white-space:pre-wrap">                </span>saturation = 0;</div><div><span style="white-space:pre-wrap">                </span>hue = 0;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>meanTime = 0;</div>
<div><span style="white-space:pre-wrap">        </span>num = 0;</div><div><br></div><div>stringstream camConfigStream;</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << "v4l2src device=" << cameraDevice; // /dev/video0</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream << " always-copy=";</div><div><span style="white-space:pre-wrap">        </span>if(alwaysCopy){</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << "true";</div>
<div><span style="white-space:pre-wrap">        </span>}else{</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << "false";</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream << " ! capsfilter caps=\" ";</div><div><br></div><div><span style="white-space:pre-wrap">        </span>camConfigStream << encoding; //"video/x-raw-rgb</div>
<div><span style="white-space:pre-wrap">        </span>if(encoding.find("rgb") != string::npos){</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << ", bpp=(int)" << bpp; //24</div>
<div><span style="white-space:pre-wrap">                </span>camConfigStream << ", depth=(int)" << depth; //24</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << ", red_mask=(int)" << redMask; //16711680</div>
<div><span style="white-space:pre-wrap">                </span>camConfigStream << ", green_mask=(int)" << greenMask; //65280</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << ", blue_mask=(int)" << blueMask; //255</div>
<div><span style="white-space:pre-wrap">                </span>camConfigStream << ", endianness=(int)" << endianness; //4321</div><div><span style="white-space:pre-wrap">        </span>}else if(encoding.find("yuv") != string::npos){</div>
<div><span style="white-space:pre-wrap">                </span>camConfigStream << ", format=" << formatSpecificEncoding; //YUY2</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream << ", width=(int)" << width; //640</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << ", height=(int)" << height; //480</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream << ", interlaced=(boolean)";</div><div><span style="white-space:pre-wrap">        </span>if(interlaced){</div><div>
<span style="white-space:pre-wrap">                </span>camConfigStream << "true";</div><div><span style="white-space:pre-wrap">        </span>}else{</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << "false";</div>
<div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << ", framerate=(fraction)" << framerate << "/1"; //15</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream << ", brightness=(int)" << brightness; //0</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << ", contrast=(int)" << contrast; //0</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream << ", saturation=(int)" << saturation; //0</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << ", hue=(int)" << hue; //0</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream << "\" ";</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap">        </span>gst_init(0,0);</div>
<div><span style="white-space:pre-wrap">        </span>cout<< "Gstreamer Version: " << gst_version_string() << endl;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>GError *error = 0; //assignment to zero is a gst requirement</div>
<div><span style="white-space:pre-wrap">        </span>pipeline = gst_parse_launch(camConfigStream.str().c_str(),&error);</div><div><span style="white-space:pre-wrap">        </span>if (pipeline == NULL) {</div>
<div><span style="white-space:pre-wrap">                </span>cout << error->message << endl;</div><div><span style="white-space:pre-wrap">                </span>exit(-1);</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div><span style="white-space:pre-wrap">        </span>sink = gst_element_factory_make("appsink",NULL);</div><div><span style="white-space:pre-wrap">        </span>if(!sink){</div><div><span style="white-space:pre-wrap">                </span>cout << "Sink creation failed" << endl;</div>
<div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>string capFormat = encoding;</div><div><span style="white-space:pre-wrap">        </span>GstCaps * caps_alone = gst_caps_new_simple(capFormat.c_str(), NULL);</div>
<div><span style="white-space:pre-wrap">        </span>gst_app_sink_set_caps(GST_APP_SINK(sink), caps_alone);</div><div><span style="white-space:pre-wrap">        </span>gst_caps_unref(caps_alone);</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>gst_base_sink_set_sync(GST_BASE_SINK(sink), true);</div><div><br></div><div><span style="white-space:pre-wrap">        </span>if(GST_IS_PIPELINE(pipeline)) {</div>
<div><span style="white-space:pre-wrap">                </span>GstPad *outpad = gst_bin_find_unlinked_pad(GST_BIN(pipeline), GST_PAD_SRC);</div><div><span style="white-space:pre-wrap">                </span>g_assert(outpad);</div>
<div><span style="white-space:pre-wrap">                </span>GstElement *outelement = gst_pad_get_parent_element(outpad);</div><div><span style="white-space:pre-wrap">                </span>g_assert(outelement);</div>
<div><span style="white-space:pre-wrap">                </span>gst_object_unref(outpad);</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap">                </span>if(!gst_bin_add(GST_BIN(pipeline), sink)) {</div>
<div><span style="white-space:pre-wrap">                        </span>cout << "gst_bin_add() failed\n" << endl; // TODO: do some unref</div><div><span style="white-space:pre-wrap">                        </span>gst_object_unref(outelement);</div>
<div><span style="white-space:pre-wrap">                        </span>gst_object_unref(pipeline);</div><div><span style="white-space:pre-wrap">                        </span>return -1;</div><div><span style="white-space:pre-wrap">                </span>}</div>
<div><br></div><div><span style="white-space:pre-wrap">                </span>if(!gst_element_link(outelement, sink)) {</div><div><span style="white-space:pre-wrap">                        </span>cout << "GStreamer: cannot link outelement(\"" <<</div>
<div><span style="white-space:pre-wrap">                                        </span>gst_element_get_name(outelement) << "\") -> sink\n" << endl;</div><div><span style="white-space:pre-wrap">                        </span>gst_object_unref(outelement);</div>
<div><span style="white-space:pre-wrap">                        </span>gst_object_unref(pipeline);</div><div><span style="white-space:pre-wrap">                        </span>return -1;</div><div><span style="white-space:pre-wrap">                </span>}</div>
<div><br></div><div><span style="white-space:pre-wrap">                </span>gst_object_unref(outelement);</div><div><span style="white-space:pre-wrap">        </span>} else {</div><div><span style="white-space:pre-wrap">                </span>GstElement* launchpipe = pipeline;</div>
<div><span style="white-space:pre-wrap">                </span>pipeline = gst_pipeline_new(NULL);</div><div><span style="white-space:pre-wrap">                </span>g_assert(pipeline);</div><div><br></div><div><span style="white-space:pre-wrap">                </span>gst_object_unparent(GST_OBJECT(launchpipe));</div>
<div><br></div><div><span style="white-space:pre-wrap">                </span>gst_bin_add_many(GST_BIN(pipeline), launchpipe, sink, NULL);</div><div><br></div><div><span style="white-space:pre-wrap">                </span>if(!gst_element_link(launchpipe, sink)) {</div>
<div><span style="white-space:pre-wrap">                        </span>cout << "GStreamer: cannot link launchpipe -> sink\n" << endl;</div><div><span style="white-space:pre-wrap">                        </span>gst_object_unref(pipeline);</div>
<div><span style="white-space:pre-wrap">                        </span>return -1;</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>gst_element_set_state(pipeline, GST_STATE_PAUSED);</div><div><br></div><div><span style="white-space:pre-wrap">        </span>if (gst_element_get_state(pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {</div>
<div><span style="white-space:pre-wrap">                </span>cout << "Failed to PAUSE." << endl;</div><div><span style="white-space:pre-wrap">                </span>exit(-1);</div><div><span style="white-space:pre-wrap">        </span>} else {</div>
<div><span style="white-space:pre-wrap">                </span>cout<< "stream is PAUSED." << endl;</div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div>
<span style="white-space:pre-wrap">        </span>// We could probably do something with the camera name, check</div><div><span style="white-space:pre-wrap">        </span>// errors or something, but at the moment, we don't care.</div>
<div><span style="white-space:pre-wrap">        </span>std::string camera_name;</div><div><span style="white-space:pre-wrap">        </span>//TODO</div><div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>if (camera_calibration_parsers::readCalibrationIni("../camera_parameters.txt", camera_name, camera_info)) {</div>
<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">                </span>ROS_INFO("Successfully read camera calibration. Rerun camera calibrator if it is incorrect.");</div>
<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>else {</div>
<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">                </span>ROS_ERROR("No camera_parameters.txt file found. Use default file if no other is available.");</div>
<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">        </span>//TODO</div>
<div><span style="white-space:pre-wrap">        </span>bool preroll = false;</div><div><span style="white-space:pre-wrap">        </span>if (preroll) {</div><div><span style="white-space:pre-wrap">                </span>//The PAUSE, PLAY, PAUSE, PLAY cycle is to ensure proper pre-roll</div>
<div><span style="white-space:pre-wrap">                </span>//I am told this is needed and am erring on the side of caution.</div><div><span style="white-space:pre-wrap">                </span>gst_element_set_state(pipeline, GST_STATE_PLAYING);</div>
<div><br></div><div><span style="white-space:pre-wrap">                </span>if (gst_element_get_state(pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {</div><div><span style="white-space:pre-wrap">                        </span>cout << "Failed to PLAY." << endl;</div>
<div><span style="white-space:pre-wrap">                        </span>exit(-1);</div><div><span style="white-space:pre-wrap">                </span>} else {</div><div><span style="white-space:pre-wrap">                        </span>cout << "stream is PLAYING." << endl;</div>
<div><span style="white-space:pre-wrap">                </span>}</div><div><br></div><div><span style="white-space:pre-wrap">                </span>gst_element_set_state(pipeline, GST_STATE_PAUSED);</div><div><br>
</div><div><span style="white-space:pre-wrap">                </span>if (gst_element_get_state(pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {</div><div><span style="white-space:pre-wrap">                        </span>cout << "Failed to PAUSE." << endl;</div>
<div><span style="white-space:pre-wrap">                        </span>exit(-1);</div><div><span style="white-space:pre-wrap">                </span>} else {</div><div><span style="white-space:pre-wrap">                        </span>cout<< "stream is PAUSED." << endl;</div>
<div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">        </span>// TODO</div>
<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>image_transport::ImageTransport it(nh);</div><div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>image_transport::CameraPublisher pub = it.advertiseCamera("gscam/image_raw", 1);</div>
<div><span style="white-space:pre-wrap">        </span>//</div><div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>ros::ServiceServer set_camera_info = nh.advertiseService("gscam/set_camera_info", setCameraInfo);</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>std::cout << "Processing..." << std::endl;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>//processVideo</div>
<div><span style="white-space:pre-wrap">        </span>rosPad = false;</div><div><span style="white-space:pre-wrap">        </span>gstreamerPad = true;</div><div><span style="white-space:pre-wrap">        </span>gst_element_set_state(pipeline, GST_STATE_PLAYING);</div>
<div>#ifdef DEBUG_FRAMERATE</div><div><span style="white-space:pre-wrap">        </span>clock_gettime(CLOCK_REALTIME, &timestamp1);</div><div>#endif</div><div><span style="white-space:pre-wrap">        </span>while(1){</div>
<div><span style="white-space:pre-wrap">        </span>GstBuffer* buf = gst_app_sink_pull_buffer(GST_APP_SINK(sink));</div><div><span style="white-space:pre-wrap">                </span>if (!buf){</div><div>
<br></div><div><span style="white-space:pre-wrap">                        </span>return -1;</div><div><span style="white-space:pre-wrap">                </span>}</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap">        </span>GstPad* pad = gst_element_get_static_pad(sink, "sink"); //TODO spostare sink</div>
<div><span style="white-space:pre-wrap">        </span>GstCaps *caps = gst_pad_get_negotiated_caps(pad);</div><div><span style="white-space:pre-wrap">        </span>GstStructure *structure = gst_caps_get_structure(caps,0);</div>
<div><span style="white-space:pre-wrap">        </span>gst_structure_get_int(structure,"width",&width);</div><div><span style="white-space:pre-wrap">        </span>gst_structure_get_int(structure,"height",&height);</div>
<div><span style="white-space:pre-wrap">        </span>gst_buffer_unref(buf);</div><div><span style="white-space:pre-wrap">        </span>//printf("fatto\n");</div><div>#ifdef DEBUG_FRAMERATE</div>
<div><span style="white-space:pre-wrap">        </span>frame_counter++;</div><div><span style="white-space:pre-wrap">        </span>if(frame_counter%NUM_OF_FRAMES_TO_CATCH_TO_CALCULATE_FPS==0){</div>
<div><span style="white-space:pre-wrap">                </span>struct timespec timestamp2;</div><div><span style="white-space:pre-wrap">                </span>clock_gettime(CLOCK_REALTIME, &timestamp2);</div><div>
<span style="white-space:pre-wrap">        </span>long int sec=timestamp2.tv_sec - timestamp1.tv_sec;</div><div><span style="white-space:pre-wrap">        </span>long int nsec=timestamp2.tv_nsec - timestamp1.tv_nsec;</div>
<div><span style="white-space:pre-wrap">        </span>long milliseconds=(sec*1000000)+nsec/1000.0;</div><div><span style="white-space:pre-wrap">                </span>printf("time to send one frame: %.02f ms\n",(float)milliseconds/(float)NUM_OF_FRAMES_TO_CATCH_TO_CALCULATE_FPS);</div>
<div><span style="white-space:pre-wrap">                </span>frame_counter=0;</div><div><span style="white-space:pre-wrap">                </span>clock_gettime(CLOCK_REALTIME, &timestamp1);</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div>#endif</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">        </span>return 0;</div><div>
}</div><div><br></div><div><span style="white-space:pre-wrap">        </span>camConfigStream << "v4l2src device=" << cameraDevice; // /dev/video0</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << " always-copy=";</div>
<div><span style="white-space:pre-wrap">        </span>if(alwaysCopy){</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << "true";</div><div><span style="white-space:pre-wrap">        </span>}else{</div>
<div><span style="white-space:pre-wrap">                </span>camConfigStream << "false";</div><div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << " ! capsfilter caps=\" ";</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>camConfigStream << encoding; //"video/x-raw-rgb</div><div><span style="white-space:pre-wrap">        </span>if(encoding.find("rgb") != string::npos){</div>
<div><span style="white-space:pre-wrap">                </span>camConfigStream << ", bpp=(int)" << bpp; //24</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << ", depth=(int)" << depth; //24</div>
<div><span style="white-space:pre-wrap">                </span>camConfigStream << ", red_mask=(int)" << redMask; //16711680</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << ", green_mask=(int)" << greenMask; //65280</div>
<div><span style="white-space:pre-wrap">                </span>camConfigStream << ", blue_mask=(int)" << blueMask; //255</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << ", endianness=(int)" << endianness; //4321</div>
<div><span style="white-space:pre-wrap">        </span>}else if(encoding.find("yuv") != string::npos){</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << ", format=" << formatSpecificEncoding; //YUY2</div>
<div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << ", width=(int)" << width; //640</div><div>
<span style="white-space:pre-wrap">        </span>camConfigStream << ", height=(int)" << height; //480</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << ", interlaced=(boolean)";</div>
<div><span style="white-space:pre-wrap">        </span>if(interlaced){</div><div><span style="white-space:pre-wrap">                </span>camConfigStream << "true";</div><div><span style="white-space:pre-wrap">        </span>}else{</div>
<div><span style="white-space:pre-wrap">                </span>camConfigStream << "false";</div><div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << ", framerate=(fraction)" << framerate << "/1"; //15</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream << ", brightness=(int)" << brightness; //0</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << ", contrast=(int)" << contrast; //0</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream << ", saturation=(int)" << saturation; //0</div><div><span style="white-space:pre-wrap">        </span>camConfigStream << ", hue=(int)" << hue; //0</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream << "\" ";</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap">        </span>gst_init(0,0);</div>
<div><span style="white-space:pre-wrap">        </span>cout<< "Gstreamer Version: " << gst_version_string() << endl;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>GError *error = 0; //assignment to zero is a gst requirement</div>
<div><span style="white-space:pre-wrap">        </span>pipeline = gst_parse_launch(camConfigStream.str().c_str(),&error);</div><div><span style="white-space:pre-wrap">        </span>if (pipeline == NULL) {</div>
<div><span style="white-space:pre-wrap">                </span>cout << error->message << endl;</div><div><span style="white-space:pre-wrap">                </span>exit(-1);</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div><span style="white-space:pre-wrap">        </span>sink = gst_element_factory_make("appsink",NULL);</div><div><span style="white-space:pre-wrap">        </span>if(!sink){</div><div><span style="white-space:pre-wrap">                </span>cout << "Sink creation failed" << endl;</div>
<div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>string capFormat = encoding;</div><div><span style="white-space:pre-wrap">        </span>GstCaps * caps_alone = gst_caps_new_simple(capFormat.c_str(), NULL);</div>
<div><span style="white-space:pre-wrap">        </span>gst_app_sink_set_caps(GST_APP_SINK(sink), caps_alone);</div><div><span style="white-space:pre-wrap">        </span>gst_caps_unref(caps_alone);</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>gst_base_sink_set_sync(GST_BASE_SINK(sink), true);</div><div><br></div><div><span style="white-space:pre-wrap">        </span>if(GST_IS_PIPELINE(pipeline)) {</div>
<div><span style="white-space:pre-wrap">                </span>GstPad *outpad = gst_bin_find_unlinked_pad(GST_BIN(pipeline), GST_PAD_SRC);</div><div><span style="white-space:pre-wrap">                </span>g_assert(outpad);</div>
<div><span style="white-space:pre-wrap">                </span>GstElement *outelement = gst_pad_get_parent_element(outpad);</div><div><span style="white-space:pre-wrap">                </span>g_assert(outelement);</div>
<div><span style="white-space:pre-wrap">                </span>gst_object_unref(outpad);</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap">                </span>if(!gst_bin_add(GST_BIN(pipeline), sink)) {</div>
<div><span style="white-space:pre-wrap">                        </span>cout << "gst_bin_add() failed\n" << endl; // TODO: do some unref</div><div><span style="white-space:pre-wrap">                        </span>gst_object_unref(outelement);</div>
<div><span style="white-space:pre-wrap">                        </span>gst_object_unref(pipeline);</div><div><span style="white-space:pre-wrap">                        </span>return -1;</div><div><span style="white-space:pre-wrap">                </span>}</div>
<div><br></div><div><span style="white-space:pre-wrap">                </span>if(!gst_element_link(outelement, sink)) {</div><div><span style="white-space:pre-wrap">                        </span>cout << "GStreamer: cannot link outelement(\"" <<</div>
<div><span style="white-space:pre-wrap">                                        </span>gst_element_get_name(outelement) << "\") -> sink\n" << endl;</div><div><span style="white-space:pre-wrap">                        </span>gst_object_unref(outelement);</div>
<div><span style="white-space:pre-wrap">                        </span>gst_object_unref(pipeline);</div><div><span style="white-space:pre-wrap">                        </span>return -1;</div><div><span style="white-space:pre-wrap">                </span>}</div>
<div><br></div><div><span style="white-space:pre-wrap">                </span>gst_object_unref(outelement);</div><div><span style="white-space:pre-wrap">        </span>} else {</div><div><span style="white-space:pre-wrap">                </span>GstElement* launchpipe = pipeline;</div>
<div><span style="white-space:pre-wrap">                </span>pipeline = gst_pipeline_new(NULL);</div><div><span style="white-space:pre-wrap">                </span>g_assert(pipeline);</div><div><br></div><div><span style="white-space:pre-wrap">                </span>gst_object_unparent(GST_OBJECT(launchpipe));</div>
<div><br></div><div><span style="white-space:pre-wrap">                </span>gst_bin_add_many(GST_BIN(pipeline), launchpipe, sink, NULL);</div><div><br></div><div><span style="white-space:pre-wrap">                </span>if(!gst_element_link(launchpipe, sink)) {</div>
<div><span style="white-space:pre-wrap">                        </span>cout << "GStreamer: cannot link launchpipe -> sink\n" << endl;</div><div><span style="white-space:pre-wrap">                        </span>gst_object_unref(pipeline);</div>
<div><span style="white-space:pre-wrap">                        </span>return -1;</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>gst_element_set_state(pipeline, GST_STATE_PAUSED);</div><div><br></div><div><span style="white-space:pre-wrap">        </span>if (gst_element_get_state(pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {</div>
<div><span style="white-space:pre-wrap">                </span>cout << "Failed to PAUSE." << endl;</div><div><span style="white-space:pre-wrap">                </span>exit(-1);</div><div><span style="white-space:pre-wrap">        </span>} else {</div>
<div><span style="white-space:pre-wrap">                </span>cout<< "stream is PAUSED." << endl;</div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div>
<span style="white-space:pre-wrap">        </span>// We could probably do something with the camera name, check</div><div><span style="white-space:pre-wrap">        </span>// errors or something, but at the moment, we don't care.</div>
<div><span style="white-space:pre-wrap">        </span>std::string camera_name;</div><div><span style="white-space:pre-wrap">        </span>//TODO</div><div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>if (camera_calibration_parsers::readCalibrationIni("../camera_parameters.txt", camera_name, camera_info)) {</div>
<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">                </span>ROS_INFO("Successfully read camera calibration. Rerun camera calibrator if it is incorrect.");</div>
<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>else {</div>
<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">                </span>ROS_ERROR("No camera_parameters.txt file found. Use default file if no other is available.");</div>
<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">        </span>//TODO</div>
<div><span style="white-space:pre-wrap">        </span>bool preroll = false;</div><div><span style="white-space:pre-wrap">        </span>if (preroll) {</div><div><span style="white-space:pre-wrap">                </span>//The PAUSE, PLAY, PAUSE, PLAY cycle is to ensure proper pre-roll</div>
<div><span style="white-space:pre-wrap">                </span>//I am told this is needed and am erring on the side of caution.</div><div><span style="white-space:pre-wrap">                </span>gst_element_set_state(pipeline, GST_STATE_PLAYING);</div>
<div><br></div><div><span style="white-space:pre-wrap">                </span>if (gst_element_get_state(pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {</div><div><span style="white-space:pre-wrap">                        </span>cout << "Failed to PLAY." << endl;</div>
<div><span style="white-space:pre-wrap">                        </span>exit(-1);</div><div><span style="white-space:pre-wrap">                </span>} else {</div><div><span style="white-space:pre-wrap">                        </span>cout << "stream is PLAYING." << endl;</div>
<div><span style="white-space:pre-wrap">                </span>}</div><div><br></div><div><span style="white-space:pre-wrap">                </span>gst_element_set_state(pipeline, GST_STATE_PAUSED);</div><div><br>
</div><div><span style="white-space:pre-wrap">                </span>if (gst_element_get_state(pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {</div><div><span style="white-space:pre-wrap">                        </span>cout << "Failed to PAUSE." << endl;</div>
<div><span style="white-space:pre-wrap">                        </span>exit(-1);</div><div><span style="white-space:pre-wrap">                </span>} else {</div><div><span style="white-space:pre-wrap">                        </span>cout<< "stream is PAUSED." << endl;</div>
<div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">        </span>// TODO</div>
<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>image_transport::ImageTransport it(nh);</div><div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>image_transport::CameraPublisher pub = it.advertiseCamera("gscam/image_raw", 1);</div>
<div><span style="white-space:pre-wrap">        </span>//</div><div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">        </span>ros::ServiceServer set_camera_info = nh.advertiseService("gscam/set_camera_info", setCameraInfo);</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>std::cout << "Processing..." << std::endl;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>//processVideo</div>
<div><span style="white-space:pre-wrap">        </span>rosPad = false;</div><div><span style="white-space:pre-wrap">        </span>gstreamerPad = true;</div><div><span style="white-space:pre-wrap">        </span>gst_element_set_state(pipeline, GST_STATE_PLAYING);</div>
<div>#ifdef DEBUG_FRAMERATE</div><div><span style="white-space:pre-wrap">        </span>clock_gettime(CLOCK_REALTIME, &timestamp1);</div><div>#endif</div><div><span style="white-space:pre-wrap">        </span>while(1){</div>
<div><span style="white-space:pre-wrap">        </span>GstBuffer* buf = gst_app_sink_pull_buffer(GST_APP_SINK(sink));</div><div><span style="white-space:pre-wrap">                </span>if (!buf){</div><div>
<br></div><div><span style="white-space:pre-wrap">                        </span>return -1;</div><div><span style="white-space:pre-wrap">                </span>}</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap">        </span>GstPad* pad = gst_element_get_static_pad(sink, "sink"); //TODO spostare sink</div>
<div><span style="white-space:pre-wrap">        </span>GstCaps *caps = gst_pad_get_negotiated_caps(pad);</div><div><span style="white-space:pre-wrap">        </span>GstStructure *structure = gst_caps_get_structure(caps,0);</div>
<div><span style="white-space:pre-wrap">        </span>gst_structure_get_int(structure,"width",&width);</div><div><span style="white-space:pre-wrap">        </span>gst_structure_get_int(structure,"height",&height);</div>
<div><span style="white-space:pre-wrap">        </span>gst_buffer_unref(buf);</div><div><span style="white-space:pre-wrap">        </span>//printf("fatto\n");</div><div>#ifdef DEBUG_FRAMERATE</div>
<div><span style="white-space:pre-wrap">        </span>frame_counter++;</div><div><span style="white-space:pre-wrap">        </span>if(frame_counter%NUM_OF_FRAMES_TO_CATCH_TO_CALCULATE_FPS==0){</div>
<div><span style="white-space:pre-wrap">                </span>struct timespec timestamp2;</div><div><span style="white-space:pre-wrap">                </span>clock_gettime(CLOCK_REALTIME, &timestamp2);</div><div>
<span style="white-space:pre-wrap">        </span>long int sec=timestamp2.tv_sec - timestamp1.tv_sec;</div><div><span style="white-space:pre-wrap">        </span>long int nsec=timestamp2.tv_nsec - timestamp1.tv_nsec;</div>
<div><span style="white-space:pre-wrap">        </span>long milliseconds=(sec*1000000)+nsec/1000.0;</div><div><span style="white-space:pre-wrap">                </span>printf("time to send one frame: %.02f ms\n",(float)milliseconds/(float)NUM_OF_FRAMES_TO_CATCH_TO_CALCULATE_FPS);</div>
<div><span style="white-space:pre-wrap">                </span>frame_counter=0;</div><div><span style="white-space:pre-wrap">                </span>clock_gettime(CLOCK_REALTIME, &timestamp1);</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div>#endif</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap">        </span>}</div><div><br></div><div><span style="white-space:pre-wrap">        </span>return 0;</div><div>
}</div></div><div><br></div>
</div><br></div>