22.2. Special memory

Like source elements, sink elements can sometimes provide externally allocated (such as X-provided or DMA'able) memory to elements earlier in the pipeline, and thereby prevent the need for memcpy () for incoming data. We do this by providing a pad-allocate-buffer function.


static GstBuffer *	gst_my_sink_buffer_allocate	(GstPad *pad,
							 guint64 offset,
							 guint   size);

static void
gst_my_sink_init (GstMySink *sink)
{
[..]
  gst_pad_set_bufferalloc_function (sink->sinkpad,
                                    gst_my_sink_buffer_allocate);
}

static void
gst_my_sink_buffer_free (GstBuffer *buf)
{
  GstMySink *sink = GST_MY_SINK (GST_BUFFER_PRIVATE (buf));

  /* Do whatever is needed here. */
[..]
}

static GstBuffer *
gst_my_sink_buffer_allocate (GstPad *pad,
			     guint64 offset,
			     guint   size)
{
  GstBuffer *buf = gst_buffer_new ();

  /* So here it's up to you to wrap your private buffers and
   * return that. */
  GST_BUFFER_FREE_DATA_FUNC (buf) = gst_my_sink_buffer_free;
  GST_BUFFER_PRIVATE (buf) = sink;
  GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
[..]

  return buf;
}