<div dir="ltr"><div dir="ltr">Hi Experts!!, <div><br></div><div>I am trying to read opencv images locally and push them into gstreamer pipeline. I am using appsrc element with "need-data" signal attached to call-back function.</div><div>Please look at the code below. </div><div><br></div><div>//------------------------------------------------------------------------------------------------------</div><div><div>#include <gst/gst.h></div><div>#include <opencv2/opencv.hpp></div><div>#include <opencv2/highgui.hpp></div><div><br></div><div>static GMainLoop *loop;</div><div><br></div><div>static void</div><div>cb_need_data(GstElement *appsrc,</div><div><span style="white-space:pre">     </span>guint       unused_size,</div><div><span style="white-space:pre">  </span>gpointer    user_data)</div><div>{</div><div><span style="white-space:pre">      </span>static GstClockTime timestamp = 0;</div><div><span style="white-space:pre">    </span>GstBuffer *buffer;</div><div><span style="white-space:pre">    </span>guint size, depth, height, width, step, channels;</div><div><span style="white-space:pre">     </span>GstFlowReturn ret;</div><div><span style="white-space:pre">    </span>guchar *data1;</div><div><span style="white-space:pre">        </span>GstMapInfo map;</div><div><br></div><div><span style="white-space:pre">      </span>cv::Mat img = cv::imread("C:/Users/212730892.LOGON/Desktop/lena.jpg");</div><div><span style="white-space:pre">      </span>height = img.size().height;</div><div><span style="white-space:pre">   </span>width = img.size().width;</div><div><span style="white-space:pre">     </span>//step = img.widthStep;</div><div><span style="white-space:pre">       </span>channels = img.channels();</div><div><span style="white-space:pre">    </span>//depth = img->depth;</div><div><span style="white-space:pre">      </span>data1 = (guchar *)img.data;</div><div><span style="white-space:pre">   </span>size = height * width* channels;</div><div><br></div><div><span style="white-space:pre">     </span>// Copy cv::Mat to GstBuffer</div><div><span style="white-space:pre">  </span>buffer = gst_buffer_new_allocate(NULL, size, NULL);</div><div><span style="white-space:pre">   </span>gst_buffer_map(buffer, &map, GST_MAP_WRITE);</div><div><span style="white-space:pre">      </span>memcpy((guchar *)map.data, data1, gst_buffer_get_size(buffer));</div><div><br></div><div><span style="white-space:pre">      </span>//Convert GstBuffer back to cv::Mat and write it, to check if data was correctly copied. //This is where I get to know that data is not correct.</div><div><span style="white-space:pre">      </span>cv::Mat img2(cv::Size(134, 134), CV_8UC3, (char*)(buffer));</div><div><span style="white-space:pre">   </span>cv::imwrite("C:/Users/212730892.LOGON/Desktop/dummy1.jpg", img2);</div><div><br></div><div><span style="white-space:pre">  </span>GST_BUFFER_PTS(buffer) = timestamp;</div><div><span style="white-space:pre">   </span>GST_BUFFER_DURATION(buffer) = gst_util_uint64_scale_int(1, GST_SECOND, 2);</div><div><br></div><div><span style="white-space:pre">   </span>timestamp += GST_BUFFER_DURATION(buffer);</div><div><br></div><div><span style="white-space:pre">    </span>g_signal_emit_by_name(appsrc, "push-buffer", buffer, &ret);</div><div><span style="white-space:pre">     </span>//gst_buffer_unref(buffer);</div><div><br></div><div><span style="white-space:pre">  </span>if (ret != GST_FLOW_OK) {</div><div><span style="white-space:pre">             </span>/* something wrong, stop pushing */</div><div><span style="white-space:pre">           </span>g_main_loop_quit(loop);</div><div><span style="white-space:pre">       </span>}</div><div>}</div><div><br></div><div>gint</div><div>main(gint   argc,</div><div><span style="white-space:pre">       </span>gchar *argv[])</div><div>{</div><div><span style="white-space:pre">        </span>GstElement *pipeline, *appsrc, *conv, *videosink;</div><div><br></div><div><span style="white-space:pre">    </span>/* init GStreamer */</div><div><span style="white-space:pre">  </span>gst_init(&argc, &argv);</div><div><span style="white-space:pre">       </span>loop = g_main_loop_new(NULL, FALSE);</div><div><br></div><div><span style="white-space:pre"> </span>/* setup pipeline */</div><div><span style="white-space:pre">  </span>pipeline = gst_pipeline_new("pipeline");</div><div><span style="white-space:pre">    </span>appsrc = gst_element_factory_make("appsrc", "source");</div><div><span style="white-space:pre">    </span>conv = gst_element_factory_make("videoconvert", "conv");</div><div><span style="white-space:pre">  </span>videosink = gst_element_factory_make("autovideosink", "videosink");</div><div><br></div><div><span style="white-space:pre">      </span>/* setup */</div><div><span style="white-space:pre">   </span>g_object_set(G_OBJECT(appsrc), "caps",</div><div><span style="white-space:pre">              </span>gst_caps_new_simple("video/x-raw",</div><div><span style="white-space:pre">                  </span>"format", G_TYPE_STRING, "RGB",</div><div><span style="white-space:pre">                   </span>"width", G_TYPE_INT, 134,</div><div><span style="white-space:pre">                   </span>"height", G_TYPE_INT, 134,</div><div><span style="white-space:pre">                  </span>"framerate", GST_TYPE_FRACTION, 1, 1,</div><div><span style="white-space:pre">                       </span>NULL), NULL);</div><div><span style="white-space:pre"> </span>gst_bin_add_many(GST_BIN(pipeline), appsrc, conv, videosink, NULL);</div><div><span style="white-space:pre">   </span>gst_element_link_many(appsrc, conv, videosink, NULL);</div><div><span style="white-space:pre"> </span>//g_object_set (videosink, "device", "/dev/video0", NULL);</div><div><span style="white-space:pre">        </span>/* setup appsrc */</div><div><span style="white-space:pre">    </span>g_object_set(G_OBJECT(appsrc),</div><div><span style="white-space:pre">                </span>"stream-type", 0,</div><div><span style="white-space:pre">           </span>"format", GST_FORMAT_TIME, NULL);</div><div><span style="white-space:pre">   </span>g_signal_connect(appsrc, "need-data", G_CALLBACK(cb_need_data), NULL);</div><div><br></div><div><span style="white-space:pre">     </span>/* play */</div><div><span style="white-space:pre">    </span>gst_element_set_state(pipeline, GST_STATE_PLAYING);</div><div><span style="white-space:pre">   </span>g_main_loop_run(loop);</div><div><br></div><div><span style="white-space:pre">       </span>if (gst_element_set_state(pipeline, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE)</div><div><span style="white-space:pre">      </span>{</div><div><span style="white-space:pre">             </span>g_printerr("Unable to set the pipeline to the playing state.\n");</div><div><span style="white-space:pre">   </span>}</div><div><br></div><div><span style="white-space:pre">    </span>// Free resources</div><div><span style="white-space:pre">     </span>gst_object_unref(GST_OBJECT(pipeline));</div><div><span style="white-space:pre">       </span>g_main_loop_unref(loop);</div><div><br></div><div><span style="white-space:pre">     </span>return 0;</div><div>}</div></div><div><div>//-------------------------------------------------------------------------------------------------------------------------------------</div><div><br></div><div>In cb_need_data, the part where copying I am copying to GstBuffer seems to be wrong and it does not work as expected. </div><div>Can anyone, please help me understand, how can I copy the image data perfectly and pass it downstream to other elements without losing any data?</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Thanks and Regards,</div><div dir="ltr">Saurabh Bora<br><div><br></div><div>PH NO : 7038166900</div><div>EMAIL : <a href="mailto:saurabh9bora@gmail.com" style="font-size:12.8px" target="_blank">saurabh9bora@gmail.com</a></div><div>             <a href="mailto:saurabh9bora@outlook.com" target="_blank">saurabh9bora@outlook.com</a></div></div></div></div></div></div></div></div></div></div></div></div>