title: Push images into Gstreamer pipeline
body: <p>I have been following many examples about pushing an image into a Gstreamer pipeline but still I can't make my code work.</p>
  
  <p>Any suggestion (beside telling me to try with Gstreamer1.0 instead of 0.10) will be very appreciated. I want to understand what is wrong in the following script that feeds an <code>appsrc</code> element with a jpeg image. Later I will use the same code to feed openCv images that I obtain from my camera but first I want to understand the basics by making this simple example work.</p>
  
  <pre><code>#include &lt;gst/gst.h&gt;
  #include &lt;string.h&gt;
  #include &lt;cv.h&gt;
  #include &lt;highgui.h&gt;
  #include &lt;stdio.h&gt;
  
  
  #define VIDEO_CAPS "video/x-raw-rgb,bpp=8,depth=8,width=640,height=360,framerate=5/1,red_mask=224,green_mask=28,blue_mask=3,endianness=1234;"
  
  
  /* Structure to contain all our information, so we can pass it to callbacks */
  guint64 imagecounter=1;
  
  typedef struct _CustomData {
    GstElement *pipeline, *app_source;
    GstElement *video_convert, *video_sink;
    GstElement *image_manage;
    guint64 num_samples;   /* Number of samples generated so far (for timestamp generation) */
  
    guint sourceid;        /* To control the GSource */
  
    GMainLoop *main_loop;  /* GLib's Main Loop */
  } CustomData;
  
  /* This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
   * The idle handler is added to the mainloop when appsrc requests us to start sending data (need-data signal)
   * and is removed when appsrc has enough data (enough-data signal).
   */
  static gboolean push_data (CustomData *data) {
    GstBuffer *buffer;
    GstFlowReturn ret;
    int i;
    gint8 *raw;
    gint num_samples; 
    gfloat freq;
    int size,depth,height,width,step,channels;
    guchar *data1;
    IplImage* img;
    g_print("push_data is beginning\n");
  
  
    img=cvLoadImage("frame1.jpg",CV_LOAD_IMAGE_COLOR); 
  
    height    = img-&gt;height;  
    width     = img-&gt;width;
    step      = img-&gt;widthStep;
    channels  = img-&gt;nChannels;
    depth     = img-&gt;depth;
    data1      = (guchar *)img-&gt;imageData;
    num_samples = height*width*channels;
  
    g_print("height %d\n",height);
    g_print("width %d\n",width);
    g_print("widthStep %d\n",step); 
    g_print("nChannels %d\n",channels);
    g_print("depth %d\n",depth);
    g_print("image_number %" G_GUINT64_FORMAT "\n", imagecounter);
    g_print("sizeof guchar: %lu\n",sizeof(guchar));
  
    /* Create a new empty buffer */
    buffer = gst_buffer_new_and_alloc (num_samples); // guint size :the size in bytes of the new buffer's data.
    size = GST_BUFFER_SIZE(buffer);
    g_print("nSize %d\n",size);
    g_print("buffer initialized\n");
  
    /* Set its timestamp and duration */
    GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (imagecounter, GST_SECOND, 5 );
    imagecounter += 1;
    GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (num_samples, GST_SECOND,5);
  
    memcpy( (guchar *)GST_BUFFER_DATA( buffer ), data1, GST_BUFFER_SIZE( buffer ) );
    g_print("image data copied into buffer\n");
    data-&gt;num_samples += num_samples;
  
    /* Push the buffer into the appsrc */
    g_signal_emit_by_name (data-&gt;app_source, "push-buffer", buffer, &amp;ret);
    g_print("buffer pushed\n");
    /* Free the buffer now that we are done with it */
    gst_buffer_unref (buffer);
    g_print("buffer unreferenced\n");
    if (ret != GST_FLOW_OK) {
       g_printerr("something wrong in sending data");
      /* We got some error, stop sending data */
      return FALSE;
    }
    g_print("push_data is ending\n");
    return TRUE;
  }
  
  /* This signal callback triggers when appsrc needs data. Here, we add an idle handler
   * to the mainloop to start pushing data into the appsrc */
  static void start_feed (GstElement *source, guint size, CustomData *data) {
    if (data-&gt;sourceid == 0) {
      g_print ("Start feeding\n");
      data-&gt;sourceid = g_idle_add ((GSourceFunc) push_data, data);
      g_print ("push_data started\n");
    }
  }
  
  /* This callback triggers when appsrc has enough data and we can stop sending.
   * We remove the idle handler from the mainloop */
  static void stop_feed (GstElement *source, CustomData *data) {
    if (data-&gt;sourceid != 0) {
      g_print ("Stop feeding\n");
      g_source_remove (data-&gt;sourceid);
      data-&gt;sourceid = 0;
    }
  }
  
  
  /* This function is called when an error message is posted on the bus */
  static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
    GError *err;
    gchar *debug_info;
  
    /* Print error details on the screen */
    gst_message_parse_error (msg, &amp;err, &amp;debug_info);
    g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg-&gt;src), err-&gt;message);
    g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
    g_clear_error (&amp;err);
    g_free (debug_info);
  
    g_main_loop_quit (data-&gt;main_loop);
  }
  
  int main(int argc, char *argv[]) {
    CustomData data;
    gchar *video_caps_text;
    GstCaps *video_caps;
    GstBus *bus;
  
    /* Initialize cumstom data structure */
    memset (&amp;data, 0, sizeof (data));
  
    /* Initialize GStreamer */
    gst_init (&amp;argc, &amp;argv);
  
    /* Create the elements */
    data.app_source = gst_element_factory_make ("appsrc", "video_source");
    data.video_convert = gst_element_factory_make ("ffmpegcolorspace", "csp");
    data.video_sink = gst_element_factory_make ("v4l2sink", "video_sink");
    data.image_manage= gst_element_factory_make("imagefreeze","image_manage");
    /* Create the empty pipeline */
    data.pipeline = gst_pipeline_new ("test-pipeline");
  
    if (!data.pipeline || !data.app_source || !data.video_convert || !data.video_sink || !data.image_manage) {
      g_printerr ("Not all elements could be created.\n");
      return -1;
    }
  
    /* Configure appsrc */
    video_caps_text = g_strdup_printf (VIDEO_CAPS);
    video_caps = gst_caps_from_string (video_caps_text);
    if( !GST_IS_CAPS(video_caps) ) {
                  g_printerr("Error creating Caps for OpenCV-Source, exiting...");
                  exit( 1 );
              }
    g_object_set (data.app_source, "caps", video_caps, NULL);
    g_signal_connect (data.app_source, "need-data", G_CALLBACK (start_feed), &amp;data);
    g_signal_connect (data.app_source, "enough-data", G_CALLBACK (stop_feed), &amp;data);
  
    /* Configure v4l2loopback videosink*/
    //g_object_set (data.video_sink, "device", "/dev/video0", NULL);
  
    /* Link all elements that can be automatically linked because they have "Always" pads */
  gst_bin_add_many (GST_BIN (data.pipeline), data.app_source, data.image_manage, data.video_convert, data.video_sink, NULL);
  
  if ( gst_element_link_many ( data.app_source, data.video_convert,data.video_sink, NULL) != TRUE) {
      g_printerr ("Elements could not be linked.\n");
      gst_object_unref (data.pipeline);
      return -1;}
  /* 
  
    /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
    bus = gst_element_get_bus (data.pipeline);
    gst_bus_add_signal_watch (bus);
    g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, &amp;data);
    gst_object_unref (bus);
  
    /* Start playing the pipeline */
    gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
  
    /* Create a GLib Main Loop and set it to run */
    data.main_loop = g_main_loop_new (NULL, FALSE);
    g_main_loop_run (data.main_loop);
  
    /* Free resources */
    gst_element_set_state (data.pipeline, GST_STATE_NULL);
    gst_object_unref (data.pipeline);
    return 0;
  }
  </code></pre>
  
  <p>After a couple of buffers have been sent I receive the following output:</p>
  
  <blockquote>
    <p><code>Error received from element video_source: Internal data flow error</code>.</p>
  </blockquote>
  
  <pre><code>Debugging information: gstbasesrc.c(2625): gst_base_src_loop (): /GstPipeline:test-pipeline/GstAppSrc:video_source:
              streaming task paused, reason not-negotiated (-4)
  </code></pre>