How to get a frame in memory buffer from v4l2src using appsink?

Panayiotis Mousouliotis pmousoul at gmail.com
Thu Nov 13 08:45:04 PST 2014


Hello,

I used the basic tutorial 8 in order to get a frame in memory buffer from
v4l2src using appsink, without success.

The code compiles, but I get a segmentation fault and the pipeline doesn't
even start..

Any help would be much appreciated.


Cheers,
Panos



PS: My code is the following:

#include <gst/gst.h>

/* Set V4L2 device to grab from. */
char * v4l2dev = "/dev/video0";


/* Structure to contain all our information, so we can pass it to callbacks
*/
typedef struct _CustomData {
  GstElement *pipeline;
  GstElement *source;
  GstElement *filter;
  GstElement *sink;
} CustomData;


/* The appsink has received a buffer */
static void new_sample (GstElement *sink, CustomData *data) {
  g_printerr ("In the callback function.\n");

  GstSample *sample;
  /* Retrieve the buffer */
  g_signal_emit_by_name (sink, "pull-sample", &sample);
  if (sample) {
    /* The only thing we do in this example is print a * to indicate a
received buffer */
    g_print ("*");
    gst_sample_unref (sample);
  }
}


int main(int argc, char *argv[]) {
  CustomData data;
  GstCaps *cap;
  GstCaps *app_cap;
  GstBus *bus;
  GstMessage *msg;
  GstStateChangeReturn ret;
  gboolean terminate = FALSE;

  /* Initialize GStreamer */
  gst_init (&argc, &argv);
  g_printerr ("Init done.\n");

  /* Create caps */
  cap = gst_caps_new_simple ("video/x-raw",
        "format", G_TYPE_STRING, "YUY2",
         "width", G_TYPE_INT, 640,
         "height", G_TYPE_INT, 480,
         "framerate", GST_TYPE_FRACTION, 30, 1,
         NULL);
  app_cap = cap;
  g_printerr ("Caps created.\n");

  /* Create the elements */
  data.source = gst_element_factory_make ("v4l2src", "source");
  data.filter = gst_element_factory_make ("capsfilter","filter");
  data.sink = gst_element_factory_make ("appsink", "sink");
  g_printerr ("Elements created.\n");

  /* Create the empty pipeline */
  data.pipeline = gst_pipeline_new ("test-pipeline");
  if (!data.pipeline || !data.source || !data.filter || !data.sink) {
    g_printerr ("Not all elements could be created.\n");
    return -1;
  }
  g_printerr ("Pipeline created.\n");

  /* Configure appsink */
  g_object_set (data.sink, "emit-signals", TRUE, "caps", app_cap, NULL);
  g_signal_connect (data.sink, "new-sample", G_CALLBACK (new_sample),
&data);
  gst_caps_unref (app_cap);
  g_printerr ("Appsink configured.\n");

  /* Build the pipeline. */
  gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.filter,
data.sink, NULL);
  if (!gst_element_link_many (data.source, data.filter, data.sink, NULL)) {
    g_printerr ("Elements could not be linked.\n");
    gst_object_unref (data.pipeline);
    return -1;
  }
  g_printerr ("Pipeline builded.\n");

  /* Modify the elements' properties */
  g_object_set (data.source, "device", v4l2dev, NULL);
  g_object_set (data.filter,"caps",cap,NULL);
  g_printerr ("Elements' properties set.\n");

  /* Start playing */
  ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
  if (ret == GST_STATE_CHANGE_FAILURE) {
    g_printerr ("Unable to set the pipeline to the playing state.\n");
    gst_object_unref (data.pipeline);
    return -1;
  }
  g_printerr ("Pipeline in playing mode.\n");

  /* Listen to the bus */
  bus = gst_element_get_bus (data.pipeline);
  do {
    msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
        GST_MESSAGE_STATE_CHANGED | 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);
          terminate = TRUE;
          break;
        case GST_MESSAGE_EOS:
          g_print ("End-Of-Stream reached.\n");
          terminate = TRUE;
          break;
        case GST_MESSAGE_STATE_CHANGED:
          /* We are only interested in state-changed messages from the
pipeline */
          if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {
            GstState old_state, new_state, pending_state;
            gst_message_parse_state_changed (msg, &old_state, &new_state,
&pending_state);
            g_print ("Pipeline state changed from %s to %s:\n",
                gst_element_state_get_name (old_state),
gst_element_state_get_name (new_state));
          }
          break;
        default:
          /* We should not reach here */
          g_printerr ("Unexpected message received.\n");
          break;
      }
      gst_message_unref (msg);
    }
  } while (!terminate);

  /* Free resources */
  gst_caps_unref (cap);
  gst_object_unref (bus);
  gst_element_set_state (data.pipeline, GST_STATE_NULL);
  gst_object_unref (data.pipeline);
  return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/gstreamer-devel/attachments/20141113/f23a0252/attachment.html>


More information about the gstreamer-devel mailing list