0

I want to write a C++ program that gets RTSP stream frames with gstreamer and put it in openCV mat. Below is my code. I get an error when trying to get a sample from the appsink. I highlighted the code step bold.

Do you have any idea how to get the sample from appsink?

Thanks in advance.

#include <gst/gst.h>
#include <opencv2/opencv.hpp>
#include <gstappsink.h>
#ifdef HAVE_GTK
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#endif
#include <stdlib.h>

int main (int argc, char *argv[])
{
    GstElement *pipeline;
    gint width, height;
    gchar *descr;
    GError *error = NULL;
    gint64 duration, position;
    GstStateChangeReturn ret;
    gboolean res;
    GstBus *bus;
    GstMessage *msg;

    gst_init (0, NULL);
    pipeline = gst_parse_launch ("rtspsrc location=rtsp://admin:admin@192.168.1.109:554/stream1 latency=0 ! decodebin ! appsink name=sink ",NULL);

    GstElement *sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
    if(!sink){
        printf("sink is NULL\n");
        exit(1);
    }

    GstAppSink *appsink = GST_APP_SINK(sink);
    if(!appsink){
        printf("appsink is NULL\n");
        exit(1);
    }

   **GstSample *sample = gst_app_sink_pull_sample(appsink);**
    if(!sample){
        printf("sample is NULL\n");
        exit(1);
    }

    GstBuffer *buffer = gst_sample_get_buffer(sample);
    GstMapInfo map;

    gst_buffer_map (buffer, &map, GST_MAP_READ);

    cv::Mat frame(cv::Size(320, 240), CV_8UC3, (char*)map.data, cv::Mat::AUTO_STEP);

    imwrite("XYZ.jpg",frame);
}
bsonbudak
  • 1
  • 2
  • https://stackoverflow.com/a/47045135/2286337 https://stackoverflow.com/a/46636126/2286337 – zindarod Apr 05 '18 at 06:36
  • Thanks zindarod, I already have a working pipeline. When I check with gst-launch, I can see the live feed from the camera. Below is the command I run: gst-launch-1.0 rtspsrc location=rtsp://admin:admin@192.168.1.120:554/stream1 latency=0 ! decodebin ! autovideosink – bsonbudak Apr 05 '18 at 06:46

0 Answers0