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">&lt;<a href="mailto:aldobiziak@gmail.com">aldobiziak@gmail.com</a>&gt;</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 &lt;<a href="mailto:lucaghera8@gmail.com">lucaghera8@gmail.com</a>&gt;<br>
<br><br><div>Good morning all,</div><div>I&#39;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&#39;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 &lt;gst/gst.h&gt;</div><div>#include &lt;gst/app/gstappsink.h&gt;</div><div>

#include &lt;gst/app/gstappbuffer.h&gt;</div><div>#include &lt;gst/base/gstbasesink.h&gt;</div><div><br></div><div>#include &lt;string&gt;</div><div>#include &lt;iostream&gt;</div><div>#include &lt;sstream&gt;</div><div>
#include &lt;map&gt;</div>
<div>#include &lt;vector&gt;</div><div><br></div><div>/***** section to measure performances***********/</div><div>#define DEBUG_FRAMERATE</div><div>#ifdef DEBUG_FRAMERATE</div><div>#include &lt;time.h&gt;</div><div>#include &lt;sys/time.h&gt;</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 = &quot;/dev/video0&quot;;</div>

<div><span style="white-space:pre-wrap">                </span>alwaysCopy = false;</div><div><span style="white-space:pre-wrap">                </span>encoding = &quot;video/x-raw-rgb&quot;;</div><div><span style="white-space:pre-wrap">                </span>formatSpecificEncoding = &quot;YUY2&quot;;</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 &lt;gst/gst.h&gt;</div><div>#include &lt;gst/app/gstappsink.h&gt;</div><div>#include &lt;gst/app/gstappbuffer.h&gt;</div>

<div>#include &lt;gst/base/gstbasesink.h&gt;</div><div><br></div><div>#include &lt;string&gt;</div><div>#include &lt;iostream&gt;</div><div>#include &lt;sstream&gt;</div><div>#include &lt;map&gt;</div><div>#include &lt;vector&gt;</div>

<div><br></div><div>/***** section to measure performances***********/</div><div>#define DEBUG_FRAMERATE</div><div>#ifdef DEBUG_FRAMERATE</div><div>#include &lt;time.h&gt;</div><div>#include &lt;sys/time.h&gt;</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 = &quot;/dev/video0&quot;;</div><div><span style="white-space:pre-wrap">                </span>alwaysCopy = false;</div>

<div><span style="white-space:pre-wrap">                </span>encoding = &quot;video/x-raw-rgb&quot;;</div><div><span style="white-space:pre-wrap">                </span>formatSpecificEncoding = &quot;YUY2&quot;;</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 &lt;&lt; &quot;v4l2src device=&quot; &lt;&lt; cameraDevice; // /dev/video0</div>

<div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot; always-copy=&quot;;</div><div><span style="white-space:pre-wrap">        </span>if(alwaysCopy){</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;true&quot;;</div>

<div><span style="white-space:pre-wrap">        </span>}else{</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;false&quot;;</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot; ! capsfilter caps=\&quot; &quot;;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; encoding; //&quot;video/x-raw-rgb</div>

<div><span style="white-space:pre-wrap">        </span>if(encoding.find(&quot;rgb&quot;) != string::npos){</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, bpp=(int)&quot; &lt;&lt; bpp; //24</div>

<div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, depth=(int)&quot; &lt;&lt; depth; //24</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, red_mask=(int)&quot; &lt;&lt; redMask; //16711680</div>

<div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, green_mask=(int)&quot; &lt;&lt; greenMask; //65280</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, blue_mask=(int)&quot; &lt;&lt; blueMask; //255</div>

<div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, endianness=(int)&quot; &lt;&lt; endianness; //4321</div><div><span style="white-space:pre-wrap">        </span>}else if(encoding.find(&quot;yuv&quot;) != string::npos){</div>

<div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, format=&quot; &lt;&lt; formatSpecificEncoding; //YUY2</div><div><span style="white-space:pre-wrap">        </span>}</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, width=(int)&quot; &lt;&lt; width; //640</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, height=(int)&quot; &lt;&lt; height; //480</div>

<div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, interlaced=(boolean)&quot;;</div><div><span style="white-space:pre-wrap">        </span>if(interlaced){</div><div>
<span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;true&quot;;</div><div><span style="white-space:pre-wrap">        </span>}else{</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;false&quot;;</div>

<div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, framerate=(fraction)&quot; &lt;&lt; framerate &lt;&lt; &quot;/1&quot;; //15</div>
<div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, brightness=(int)&quot; &lt;&lt; brightness; //0</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, contrast=(int)&quot; &lt;&lt; contrast; //0</div>

<div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, saturation=(int)&quot; &lt;&lt; saturation; //0</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, hue=(int)&quot; &lt;&lt; hue; //0</div>

<div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;\&quot; &quot;;</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&lt;&lt; &quot;Gstreamer Version: &quot; &lt;&lt; gst_version_string() &lt;&lt; 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(),&amp;error);</div><div><span style="white-space:pre-wrap">        </span>if (pipeline == NULL) {</div>
<div><span style="white-space:pre-wrap">                </span>cout &lt;&lt; error-&gt;message &lt;&lt; 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(&quot;appsink&quot;,NULL);</div><div><span style="white-space:pre-wrap">        </span>if(!sink){</div><div><span style="white-space:pre-wrap">                </span>cout &lt;&lt; &quot;Sink creation failed&quot; &lt;&lt; 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 &lt;&lt; &quot;gst_bin_add() failed\n&quot; &lt;&lt; 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 &lt;&lt; &quot;GStreamer: cannot link outelement(\&quot;&quot; &lt;&lt;</div>

<div><span style="white-space:pre-wrap">                                        </span>gst_element_get_name(outelement) &lt;&lt; &quot;\&quot;) -&gt; sink\n&quot; &lt;&lt; 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 &lt;&lt; &quot;GStreamer: cannot link launchpipe -&gt; sink\n&quot; &lt;&lt; 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 &lt;&lt; &quot;Failed to PAUSE.&quot; &lt;&lt; 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&lt;&lt; &quot;stream is PAUSED.&quot; &lt;&lt; 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&#39;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(&quot;../camera_parameters.txt&quot;, camera_name, camera_info)) {</div>

<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">                </span>ROS_INFO(&quot;Successfully read camera calibration.  Rerun camera calibrator if it is incorrect.&quot;);</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(&quot;No camera_parameters.txt file found.  Use default file if no other is available.&quot;);</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 &lt;&lt; &quot;Failed to PLAY.&quot; &lt;&lt; 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 &lt;&lt; &quot;stream is PLAYING.&quot; &lt;&lt; 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 &lt;&lt; &quot;Failed to PAUSE.&quot; &lt;&lt; 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&lt;&lt; &quot;stream is PAUSED.&quot; &lt;&lt; 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(&quot;gscam/image_raw&quot;, 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(&quot;gscam/set_camera_info&quot;, setCameraInfo);</div>

<div><br></div><div><span style="white-space:pre-wrap">        </span>std::cout &lt;&lt; &quot;Processing...&quot; &lt;&lt; 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, &amp;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, &quot;sink&quot;); //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,&quot;width&quot;,&amp;width);</div><div><span style="white-space:pre-wrap">        </span>gst_structure_get_int(structure,&quot;height&quot;,&amp;height);</div>

<div><span style="white-space:pre-wrap">        </span>gst_buffer_unref(buf);</div><div><span style="white-space:pre-wrap">        </span>//printf(&quot;fatto\n&quot;);</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, &amp;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(&quot;time to send one frame: %.02f ms\n&quot;,(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, &amp;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 &lt;&lt; &quot;v4l2src device=&quot; &lt;&lt; cameraDevice; // /dev/video0</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot; always-copy=&quot;;</div>

<div><span style="white-space:pre-wrap">        </span>if(alwaysCopy){</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;true&quot;;</div><div><span style="white-space:pre-wrap">        </span>}else{</div>

<div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;false&quot;;</div><div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot; ! capsfilter caps=\&quot; &quot;;</div>

<div><br></div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; encoding; //&quot;video/x-raw-rgb</div><div><span style="white-space:pre-wrap">        </span>if(encoding.find(&quot;rgb&quot;) != string::npos){</div>

<div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, bpp=(int)&quot; &lt;&lt; bpp; //24</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, depth=(int)&quot; &lt;&lt; depth; //24</div>

<div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, red_mask=(int)&quot; &lt;&lt; redMask; //16711680</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, green_mask=(int)&quot; &lt;&lt; greenMask; //65280</div>

<div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, blue_mask=(int)&quot; &lt;&lt; blueMask; //255</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, endianness=(int)&quot; &lt;&lt; endianness; //4321</div>

<div><span style="white-space:pre-wrap">        </span>}else if(encoding.find(&quot;yuv&quot;) != string::npos){</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;, format=&quot; &lt;&lt; formatSpecificEncoding; //YUY2</div>

<div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, width=(int)&quot; &lt;&lt; width; //640</div><div>
<span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, height=(int)&quot; &lt;&lt; height; //480</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, interlaced=(boolean)&quot;;</div>

<div><span style="white-space:pre-wrap">        </span>if(interlaced){</div><div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;true&quot;;</div><div><span style="white-space:pre-wrap">        </span>}else{</div>

<div><span style="white-space:pre-wrap">                </span>camConfigStream &lt;&lt; &quot;false&quot;;</div><div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, framerate=(fraction)&quot; &lt;&lt; framerate &lt;&lt; &quot;/1&quot;; //15</div>

<div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, brightness=(int)&quot; &lt;&lt; brightness; //0</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, contrast=(int)&quot; &lt;&lt; contrast; //0</div>

<div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, saturation=(int)&quot; &lt;&lt; saturation; //0</div><div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;, hue=(int)&quot; &lt;&lt; hue; //0</div>

<div><span style="white-space:pre-wrap">        </span>camConfigStream &lt;&lt; &quot;\&quot; &quot;;</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&lt;&lt; &quot;Gstreamer Version: &quot; &lt;&lt; gst_version_string() &lt;&lt; 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(),&amp;error);</div><div><span style="white-space:pre-wrap">        </span>if (pipeline == NULL) {</div>
<div><span style="white-space:pre-wrap">                </span>cout &lt;&lt; error-&gt;message &lt;&lt; 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(&quot;appsink&quot;,NULL);</div><div><span style="white-space:pre-wrap">        </span>if(!sink){</div><div><span style="white-space:pre-wrap">                </span>cout &lt;&lt; &quot;Sink creation failed&quot; &lt;&lt; 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 &lt;&lt; &quot;gst_bin_add() failed\n&quot; &lt;&lt; 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 &lt;&lt; &quot;GStreamer: cannot link outelement(\&quot;&quot; &lt;&lt;</div>

<div><span style="white-space:pre-wrap">                                        </span>gst_element_get_name(outelement) &lt;&lt; &quot;\&quot;) -&gt; sink\n&quot; &lt;&lt; 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 &lt;&lt; &quot;GStreamer: cannot link launchpipe -&gt; sink\n&quot; &lt;&lt; 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 &lt;&lt; &quot;Failed to PAUSE.&quot; &lt;&lt; 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&lt;&lt; &quot;stream is PAUSED.&quot; &lt;&lt; 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&#39;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(&quot;../camera_parameters.txt&quot;, camera_name, camera_info)) {</div>

<div><span style="white-space:pre-wrap">        </span>//<span style="white-space:pre-wrap">                </span>ROS_INFO(&quot;Successfully read camera calibration.  Rerun camera calibrator if it is incorrect.&quot;);</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(&quot;No camera_parameters.txt file found.  Use default file if no other is available.&quot;);</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 &lt;&lt; &quot;Failed to PLAY.&quot; &lt;&lt; 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 &lt;&lt; &quot;stream is PLAYING.&quot; &lt;&lt; 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 &lt;&lt; &quot;Failed to PAUSE.&quot; &lt;&lt; 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&lt;&lt; &quot;stream is PAUSED.&quot; &lt;&lt; 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(&quot;gscam/image_raw&quot;, 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(&quot;gscam/set_camera_info&quot;, setCameraInfo);</div>

<div><br></div><div><span style="white-space:pre-wrap">        </span>std::cout &lt;&lt; &quot;Processing...&quot; &lt;&lt; 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, &amp;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, &quot;sink&quot;); //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,&quot;width&quot;,&amp;width);</div><div><span style="white-space:pre-wrap">        </span>gst_structure_get_int(structure,&quot;height&quot;,&amp;height);</div>

<div><span style="white-space:pre-wrap">        </span>gst_buffer_unref(buf);</div><div><span style="white-space:pre-wrap">        </span>//printf(&quot;fatto\n&quot;);</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, &amp;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(&quot;time to send one frame: %.02f ms\n&quot;,(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, &amp;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>