Hi,
We try to run a gstreamer C sample and don’t observe memory increase. The sample can run overnight and exit gracefully. Here is the code:
#include <cstdlib>
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
#include <glib-unix.h>
#include <sstream>
#include <vector>
using namespace std;
static void appsink_eos(GstAppSink * appsink, gpointer user_data)
{
printf("app sink receive eos\n");
}
static GstFlowReturn new_buffer(GstAppSink *appsink, gpointer user_data)
{
GstSample *sample = NULL;
guint *p_frameCount = (guint *)user_data;
g_signal_emit_by_name (appsink, "pull-sample", &sample,NULL);
if (sample)
{
GstBuffer *buffer = NULL;
GstCaps *caps = NULL;
GstMapInfo map = {0};
caps = gst_sample_get_caps (sample);
if (!caps)
{
printf("could not get snapshot format\n");
}
gst_caps_get_structure (caps, 0);
buffer = gst_sample_get_buffer (sample);
gst_buffer_map (buffer, &map, GST_MAP_READ);
// print 60th frame to ensure the thread is alive
(*p_frameCount)+=1;
if ((*p_frameCount) == 60)
{
printf("map.size = %lu, 0x%p\n", map.size, appsink);
}
gst_buffer_unmap(buffer, &map);
gst_sample_unref (sample);
}
else
{
g_print ("could not get sample\n");
}
return GST_FLOW_OK;
}
int main_1(void) {
GMainLoop *main_loop;
main_loop = g_main_loop_new (NULL, FALSE);
GstPipeline *gst_pipeline = nullptr;
string launch_string;
guint frameCount = 0;
ostringstream launch_stream;
GstAppSinkCallbacks callbacks = {appsink_eos, NULL, new_buffer};
launch_stream
<< "rtspsrc location=rtsp://10.19.107.227:8554/test ! "
<< "rtph264depay ! h264parse ! nvv4l2decoder enable-max-performance=1 ! "
<< "nvvidconv ! video/x-raw,width=1920,height=1080,format=RGBA ! "
<< "videoconvert ! video/x-raw,format=BGR ! "
<< "appsink name=mysink ";
launch_string = launch_stream.str();
frameCount = 0;
GError *error = nullptr;
gst_pipeline = (GstPipeline*) gst_parse_launch(launch_string.c_str(), &error);
if (gst_pipeline == nullptr) {
g_print( "Failed to parse launch: %s\n", error->message);
return -1;
}
if(error) g_error_free(error);
GstElement *appsink_ = gst_bin_get_by_name(GST_BIN(gst_pipeline), "mysink");
gst_app_sink_set_callbacks (GST_APP_SINK(appsink_), &callbacks, (gpointer)&frameCount, NULL);
gst_element_set_state((GstElement*)gst_pipeline, GST_STATE_PLAYING);
sleep(3);
//send EOS
GstMessage *msg;
gst_element_send_event ((GstElement*)gst_pipeline, gst_event_new_eos ());
// Wait for EOS message
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(gst_pipeline));
msg = gst_bus_poll(bus, GST_MESSAGE_EOS, GST_CLOCK_TIME_NONE);
gst_message_unref(msg);
gst_object_unref(bus);
gst_element_set_state((GstElement*)gst_pipeline, GST_STATE_NULL);
gst_object_unref(GST_OBJECT(gst_pipeline));
g_main_loop_unref(main_loop);
return 0;
}
void *thread_function(void *arg)
{
for(guint i=0; i<5566; i++) {
g_print("loop = %d \n", i);
main_1();
sleep(1);
}
return nullptr;
}
int main(int argc, char** argv)
{
gst_init (&argc, &argv);
vector<pthread_t> pthreadIdArr;
for (int k = 0; k < 8; k++) {
pthread_t pthreadHandle;
pthread_create(&pthreadHandle, nullptr, thread_function, nullptr);
pthreadIdArr.push_back(pthreadHandle);
}
// wait all done
for (size_t i = 0; i < pthreadIdArr.size(); i++) {
pthread_join(pthreadIdArr[i], nullptr);
}
return 0;
}
So probably it is something wrong in OpenCV frameworks. Not sure if this works but may try not to use cv2.VideoCapture(), but map GstBuffer to cv::Mat like this:
gst_buffer_map (buffer, &map, GST_MAP_READ);
cv::Mat imgbuf = cv::Mat(height,
width,
CV_8UC3, map.data);