I'm reading videoframe in appsink into opencv Mat:
GstFlowReturn GStreamPipeline::new_sample(GstElement* sink, GStreamPipeline* gsp) {
GstSample* sample;
/* Retrieve the buffer */
g_signal_emit_by_name(sink, "pull-sample", &sample);
if (sample)
{
GstBuffer* buffer = gst_sample_get_buffer(sample);
GstSegment* segment = gst_sample_get_segment(sample);
GstCaps* current_caps = gst_pad_get_current_caps(gst_element_get_static_pad(gsp->app_sink, "sink"));
GstStructure* new_pad_struct = gst_caps_get_structure(current_caps, 0);
gint width = 0, height = 0;
if (!gst_structure_get_int(new_pad_struct, "width", &width) ||
!gst_structure_get_int(new_pad_struct, "height", &height)) {
g_print("No width/height available\n");
}
GstMapInfo map;
gst_buffer_map(buffer, &map, GST_MAP_READ);
cv::Mat frame(cv::Size(width, height), CV_8UC3, (char*)map.data);
gst_buffer_unmap(buffer, &map);
if (!current_caps) gst_caps_unref(current_caps);
gst_sample_unref(sample);
return GST_FLOW_OK;
}
return GST_FLOW_ERROR;
}
What happens with memory to which frame.data points after I leave the scope of the function? How long does that buffer exists? Do I need to copy it to be safe? I want to understand its lifetime to avoid unnecessary copy.
Figured that, need to use
gst_buffer_ref(buffer)to increase ref count.