Grabbing frames from GStreamer pipeline

kskill kapskill23 at gmail.com
Tue Feb 9 09:59:30 UTC 2021


Hi Kiran,

I am facing the same problem.
Instead of filesrc i am using rtpsrc...

Sharing the code 

Can you guide on me this.
I want frames of size 1280x720x3




#include <gst/gst.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <iostream>
#include <unistd.h>

#include "opencv2/opencv.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

GstElement *pipeline;
GstBus *bus;
GstMessage *msg;

using namespace cv;

static GstFlowReturn

have_frame (GstElement * appsink, gpointer app)
{
  GstBuffer *buffer;
  GstSample *sample;

  /* get the buffer, we can also wakeup the mainloop to get the subtitle
from
   * appsink in the mainloop */
  g_signal_emit_by_name (appsink, "pull-sample", &sample);
  static int framecount = 0;
  if (sample) 
  {
    GstMapInfo map;
    gint64 position;
    GstClock *clock;
    GstClockTime base_time, running_time;

    buffer = gst_sample_get_buffer (sample);
    gst_element_query_position (appsink, GST_FORMAT_TIME, &position);

    clock = gst_element_get_clock (appsink);
    base_time = gst_element_get_base_time (appsink);

    running_time = gst_clock_get_time (clock) - base_time;

    gst_object_unref (clock);

     if (gst_buffer_map(buffer, &map, GST_MAP_READ)) 
     {

          printf("Count %d\n",framecount);
          g_print("Appsink: Buffer Received: Content = %u\n", map.size);

          Mat frame(Size(1280, 720), CV_8UC3, (char*)map.data,
Mat::AUTO_STEP);  //if its a 320x240 size image

          imwrite("./Result/XYZ.jpg",frame);
          printf("Writing image file \n");
     }

//     gst_util_dump_mem (map.data, map.size);
    gst_buffer_unmap (buffer, &map);
    gst_sample_unref (sample);
    framecount++;
  }
  return GST_FLOW_OK;
}

int main(int argc, char *argv[]) 
{
     /* Initialize GStreamer */
     gst_init (&argc, &argv);
     
     /* Build the pipeline */
      pipeline = gst_parse_launch
     ("rtspsrc location=rtsp://URL !"
     "rtph264depay !"
     "h264parse !"
     "v4l2h264dec !"
     "v4l2h264enc !"
     "h264parse !"
     "queue !"
     "appsink name=appsink",
     NULL);

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

     
     g_object_set (G_OBJECT (sink), "emit-signals", TRUE, NULL);
     g_signal_connect (sink, "new-sample", G_CALLBACK (have_frame),NULL);
     
     /* Start playing */
     gst_element_set_state (pipeline, GST_STATE_PLAYING);
     /* Wait until error or EOS */
     bus = gst_element_get_bus (pipeline);
     msg = gst_bus_timed_pop_filtered (bus,
GST_CLOCK_TIME_NONE,static_cast<GstMessageType>( GST_MESSAGE_ERROR |
GST_MESSAGE_EOS));
     
     /* Parse message */
     if (msg != NULL) 
     {
          GError *err;
          gchar *debug_info;

          switch (GST_MESSAGE_TYPE (msg)) {
          case GST_MESSAGE_ERROR:
               gst_message_parse_error (msg, &err, &debug_info);
               g_printerr ("Error received from element %s: %s\n",
GST_OBJECT_NAME (msg->src), err->message);
               g_printerr ("Debugging information: %s\n", debug_info ?
debug_info : "none");
               g_clear_error (&err);
               g_free (debug_info);
               break;
          case GST_MESSAGE_EOS:
               g_print ("End-Of-Stream reached.\n");
               break;
          default:
               /* We should not reach here because we only asked for ERRORs
and EOS */
               g_printerr ("Unexpected message received.\n");
               break;
          }
          gst_message_unref (msg);
     }

     /* Free resources */
     if (msg != NULL)
          gst_message_unref (msg);
     gst_object_unref (bus);
     gst_element_set_state (pipeline, GST_STATE_NULL);
     gst_object_unref (pipeline);
     
     return 0;
}


Looking for help.
Thanks





--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/


More information about the gstreamer-devel mailing list