From e6798c5cce05710240e15dfc8ed803485162f5d6 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 9 Apr 2009 18:04:44 +0200 Subject: [PATCH] ringbuffer: allow for custom commit functions Allow subclasses to override the commit method. --- gst-libs/gst/audio/gstringbuffer.c | 104 ++++++++++++++++++++++--------------- gst-libs/gst/audio/gstringbuffer.h | 7 ++- 2 files changed, 69 insertions(+), 42 deletions(-) diff --git a/gst-libs/gst/audio/gstringbuffer.c b/gst-libs/gst/audio/gstringbuffer.c index 24bbda4..5aca46f 100644 --- a/gst-libs/gst/audio/gstringbuffer.c +++ b/gst-libs/gst/audio/gstringbuffer.c @@ -52,6 +52,8 @@ static void gst_ring_buffer_dispose (GObject * object); static void gst_ring_buffer_finalize (GObject * object); static gboolean gst_ring_buffer_pause_unlocked (GstRingBuffer * buf); +static guint default_commit (GstRingBuffer * buf, guint64 * sample, + guchar * data, gint in_samples, gint out_samples, gint * accum); static GstObjectClass *parent_class = NULL; @@ -89,14 +91,18 @@ gst_ring_buffer_class_init (GstRingBufferClass * klass) { GObjectClass *gobject_class; GstObjectClass *gstobject_class; + GstRingBufferClass *gstringbuffer_class; gobject_class = (GObjectClass *) klass; gstobject_class = (GstObjectClass *) klass; + gstringbuffer_class = (GstRingBufferClass *) klass; parent_class = g_type_class_peek_parent (klass); gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_ring_buffer_dispose); gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ring_buffer_finalize); + + gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (default_commit); } static void @@ -1525,43 +1531,8 @@ G_STMT_START { \ GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \ } G_STMT_END -/** - * gst_ring_buffer_commit_full: - * @buf: the #GstRingBuffer to commit - * @sample: the sample position of the data - * @data: the data to commit - * @in_samples: the number of samples in the data to commit - * @out_samples: the number of samples to write to the ringbuffer - * @accum: accumulator for rate conversion. - * - * Commit @in_samples samples pointed to by @data to the ringbuffer @buf. - * - * @in_samples and @out_samples define the rate conversion to perform on the the - * samples in @data. For negative rates, @out_samples must be negative and - * @in_samples positive. - * - * When @out_samples is positive, the first sample will be written at position @sample - * in the ringbuffer. When @out_samples is negative, the last sample will be written to - * @sample in reverse order. - * - * @out_samples does not need to be a multiple of the segment size of the ringbuffer - * although it is recommended for optimal performance. - * - * @accum will hold a temporary accumulator used in rate conversion and should be - * set to 0 when this function is first called. In case the commit operation is - * interrupted, one can resume the processing by passing the previously returned - * @accum value back to this function. - * - * MT safe. - * - * Returns: The number of samples written to the ringbuffer or -1 on error. The - * number of samples written can be less than @out_samples when @buf was interrupted - * with a flush or stop. - * - * Since: 0.10.11. - */ -guint -gst_ring_buffer_commit_full (GstRingBuffer * buf, guint64 * sample, +static guint +default_commit (GstRingBuffer * buf, guint64 * sample, guchar * data, gint in_samples, gint out_samples, gint * accum) { gint segdone; @@ -1572,10 +1543,6 @@ gst_ring_buffer_commit_full (GstRingBuffer * buf, guint64 * sample, gint inr, outr; gboolean reverse; - if (G_UNLIKELY (in_samples == 0 || out_samples == 0)) - return in_samples; - - g_return_val_if_fail (GST_IS_RING_BUFFER (buf), -1); g_return_val_if_fail (buf->data != NULL, -1); g_return_val_if_fail (data != NULL, -1); @@ -1694,6 +1661,61 @@ not_started: } /** + * gst_ring_buffer_commit_full: + * @buf: the #GstRingBuffer to commit + * @sample: the sample position of the data + * @data: the data to commit + * @in_samples: the number of samples in the data to commit + * @out_samples: the number of samples to write to the ringbuffer + * @accum: accumulator for rate conversion. + * + * Commit @in_samples samples pointed to by @data to the ringbuffer @buf. + * + * @in_samples and @out_samples define the rate conversion to perform on the the + * samples in @data. For negative rates, @out_samples must be negative and + * @in_samples positive. + * + * When @out_samples is positive, the first sample will be written at position @sample + * in the ringbuffer. When @out_samples is negative, the last sample will be written to + * @sample in reverse order. + * + * @out_samples does not need to be a multiple of the segment size of the ringbuffer + * although it is recommended for optimal performance. + * + * @accum will hold a temporary accumulator used in rate conversion and should be + * set to 0 when this function is first called. In case the commit operation is + * interrupted, one can resume the processing by passing the previously returned + * @accum value back to this function. + * + * MT safe. + * + * Returns: The number of samples written to the ringbuffer or -1 on error. The + * number of samples written can be less than @out_samples when @buf was interrupted + * with a flush or stop. + * + * Since: 0.10.11. + */ +guint +gst_ring_buffer_commit_full (GstRingBuffer * buf, guint64 * sample, + guchar * data, gint in_samples, gint out_samples, gint * accum) +{ + GstRingBufferClass *rclass; + guint res = -1; + + g_return_val_if_fail (GST_IS_RING_BUFFER (buf), -1); + + if (G_UNLIKELY (in_samples == 0 || out_samples == 0)) + return in_samples; + + rclass = GST_RING_BUFFER_GET_CLASS (buf); + + if (G_LIKELY (rclass->commit)) + res = rclass->commit (buf, sample, data, in_samples, out_samples, accum); + + return res; +} + +/** * gst_ring_buffer_commit: * @buf: the #GstRingBuffer to commit * @sample: the sample position of the data diff --git a/gst-libs/gst/audio/gstringbuffer.h b/gst-libs/gst/audio/gstringbuffer.h index f7342e2..6bfda21 100644 --- a/gst-libs/gst/audio/gstringbuffer.h +++ b/gst-libs/gst/audio/gstringbuffer.h @@ -300,6 +300,7 @@ struct _GstRingBuffer { * @delay: get number of samples queued in device * @activate: activate the thread that starts pulling and monitoring the * consumed segments in the device. Since 0.10.22 + * @commit: write samples into the ringbuffer * * The vmethods that subclasses can override to implement the ringbuffer. */ @@ -322,8 +323,12 @@ struct _GstRingBufferClass { /* ABI added */ gboolean (*activate) (GstRingBuffer *buf, gboolean active); + guint (*commit) (GstRingBuffer * buf, guint64 *sample, + guchar * data, gint in_samples, + gint out_samples, gint * accum); + /*< private >*/ - gpointer _gst_reserved[GST_PADDING - 1]; + gpointer _gst_reserved[GST_PADDING - 2]; }; GType gst_ring_buffer_get_type(void); -- 2.7.4