--- a/gst_plugins_base/gst-libs/gst/audio/gstbaseaudiosrc.c Fri Mar 19 09:35:09 2010 +0200
+++ b/gst_plugins_base/gst-libs/gst/audio/gstbaseaudiosrc.c Fri Apr 16 15:15:52 2010 +0300
@@ -42,12 +42,31 @@
#include "gst/gst-i18n-plugin.h"
+GST_DEBUG_CATEGORY_STATIC (gst_base_audio_src_debug);
+#define GST_CAT_DEFAULT gst_base_audio_src_debug
+
#ifdef __SYMBIAN32__
-#include <glib_global.h>
+EXPORT_C
#endif
-GST_DEBUG_CATEGORY_STATIC (gst_base_audio_src_debug);
-#define GST_CAT_DEFAULT gst_base_audio_src_debug
+GType
+gst_base_audio_src_slave_method_get_type (void)
+{
+ static GType slave_method_type = 0;
+ static const GEnumValue slave_method[] = {
+ {GST_BASE_AUDIO_SRC_SLAVE_RESAMPLE, "Resampling slaving", "resample"},
+ {GST_BASE_AUDIO_SRC_SLAVE_RETIMESTAMP, "Re-timestamp", "re-timestamp"},
+ {GST_BASE_AUDIO_SRC_SLAVE_SKEW, "Skew", "skew"},
+ {GST_BASE_AUDIO_SRC_SLAVE_NONE, "No slaving", "none"},
+ {0, NULL, NULL},
+ };
+
+ if (!slave_method_type) {
+ slave_method_type =
+ g_enum_register_static ("GstBaseAudioSrcSlaveMethod", slave_method);
+ }
+ return slave_method_type;
+}
#define GST_BASE_AUDIO_SRC_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_AUDIO_SRC, GstBaseAudioSrcPrivate))
@@ -55,6 +74,9 @@
struct _GstBaseAudioSrcPrivate
{
gboolean provide_clock;
+
+ /* the clock slaving algorithm in use */
+ GstBaseAudioSrcSlaveMethod slave_method;
};
/* BaseAudioSrc signals and args */
@@ -66,14 +88,21 @@
#define DEFAULT_BUFFER_TIME ((200 * GST_MSECOND) / GST_USECOND)
#define DEFAULT_LATENCY_TIME ((10 * GST_MSECOND) / GST_USECOND)
+#define DEFAULT_ACTUAL_BUFFER_TIME -1
+#define DEFAULT_ACTUAL_LATENCY_TIME -1
#define DEFAULT_PROVIDE_CLOCK TRUE
+#define DEFAULT_SLAVE_METHOD GST_BASE_AUDIO_SRC_SLAVE_RETIMESTAMP
enum
{
PROP_0,
PROP_BUFFER_TIME,
PROP_LATENCY_TIME,
- PROP_PROVIDE_CLOCK
+ PROP_ACTUAL_BUFFER_TIME,
+ PROP_ACTUAL_LATENCY_TIME,
+ PROP_PROVIDE_CLOCK,
+ PROP_SLAVE_METHOD,
+ PROP_LAST
};
static void
@@ -86,6 +115,7 @@
GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
LOCALEDIR);
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+ bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif /* ENABLE_NLS */
}
@@ -147,17 +177,51 @@
g_object_class_install_property (gobject_class, PROP_BUFFER_TIME,
g_param_spec_int64 ("buffer-time", "Buffer Time",
"Size of audio buffer in microseconds", 1,
- G_MAXINT64, DEFAULT_BUFFER_TIME, G_PARAM_READWRITE));
+ G_MAXINT64, DEFAULT_BUFFER_TIME,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_LATENCY_TIME,
g_param_spec_int64 ("latency-time", "Latency Time",
"Audio latency in microseconds", 1,
- G_MAXINT64, DEFAULT_LATENCY_TIME, G_PARAM_READWRITE));
+ G_MAXINT64, DEFAULT_LATENCY_TIME,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GstBaseAudioSrc:actual-buffer-time:
+ *
+ * Actual configured size of audio buffer in microseconds.
+ *
+ * Since: 0.10.20
+ **/
+ g_object_class_install_property (gobject_class, PROP_ACTUAL_BUFFER_TIME,
+ g_param_spec_int64 ("actual-buffer-time", "Actual Buffer Time",
+ "Actual configured size of audio buffer in microseconds",
+ DEFAULT_ACTUAL_BUFFER_TIME, G_MAXINT64, DEFAULT_ACTUAL_BUFFER_TIME,
+ G_PARAM_READABLE));
+
+ /**
+ * GstBaseAudioSrc:actual-latency-time:
+ *
+ * Actual configured audio latency in microseconds.
+ *
+ * Since: 0.10.20
+ **/
+ g_object_class_install_property (gobject_class, PROP_ACTUAL_LATENCY_TIME,
+ g_param_spec_int64 ("actual-latency-time", "Actual Latency Time",
+ "Actual configured audio latency in microseconds",
+ DEFAULT_ACTUAL_LATENCY_TIME, G_MAXINT64, DEFAULT_ACTUAL_LATENCY_TIME,
+ G_PARAM_READABLE));
g_object_class_install_property (gobject_class, PROP_PROVIDE_CLOCK,
g_param_spec_boolean ("provide-clock", "Provide Clock",
"Provide a clock to be used as the global pipeline clock",
- DEFAULT_PROVIDE_CLOCK, G_PARAM_READWRITE));
+ DEFAULT_PROVIDE_CLOCK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_property (gobject_class, PROP_SLAVE_METHOD,
+ g_param_spec_enum ("slave-method", "Slave Method",
+ "Algorithm to use to match the rate of the masterclock",
+ GST_TYPE_BASE_AUDIO_SRC_SLAVE_METHOD, DEFAULT_SLAVE_METHOD,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gstelement_class->change_state =
GST_DEBUG_FUNCPTR (gst_base_audio_src_change_state);
@@ -177,6 +241,7 @@
/* ref class from a thread-safe context to work around missing bit of
* thread-safety in GObject */
g_type_class_ref (GST_TYPE_AUDIO_CLOCK);
+ g_type_class_ref (GST_TYPE_RING_BUFFER);
}
static void
@@ -188,6 +253,7 @@
baseaudiosrc->buffer_time = DEFAULT_BUFFER_TIME;
baseaudiosrc->latency_time = DEFAULT_LATENCY_TIME;
baseaudiosrc->priv->provide_clock = DEFAULT_PROVIDE_CLOCK;
+ baseaudiosrc->priv->slave_method = DEFAULT_SLAVE_METHOD;
/* reset blocksize we use latency time to calculate a more useful
* value based on negotiated format. */
GST_BASE_SRC (baseaudiosrc)->blocksize = 0;
@@ -208,6 +274,7 @@
src = GST_BASE_AUDIO_SRC (object);
+ GST_OBJECT_LOCK (src);
if (src->clock)
gst_object_unref (src->clock);
src->clock = NULL;
@@ -216,6 +283,7 @@
gst_object_unparent (GST_OBJECT_CAST (src->ringbuffer));
src->ringbuffer = NULL;
}
+ GST_OBJECT_UNLOCK (src);
G_OBJECT_CLASS (parent_class)->dispose (object);
}
@@ -350,6 +418,58 @@
return result;
}
+/**
+ * gst_base_audio_src_set_slave_method:
+ * @src: a #GstBaseAudioSrc
+ * @method: the new slave method
+ *
+ * Controls how clock slaving will be performed in @src.
+ *
+ * Since: 0.10.20
+ */
+#ifdef __SYMBIAN32__
+EXPORT_C
+#endif
+
+void
+gst_base_audio_src_set_slave_method (GstBaseAudioSrc * src,
+ GstBaseAudioSrcSlaveMethod method)
+{
+ g_return_if_fail (GST_IS_BASE_AUDIO_SRC (src));
+
+ GST_OBJECT_LOCK (src);
+ src->priv->slave_method = method;
+ GST_OBJECT_UNLOCK (src);
+}
+
+/**
+ * gst_base_audio_src_get_slave_method:
+ * @src: a #GstBaseAudioSrc
+ *
+ * Get the current slave method used by @src.
+ *
+ * Returns: The current slave method used by @src.
+ *
+ * Since: 0.10.20
+ */
+#ifdef __SYMBIAN32__
+EXPORT_C
+#endif
+
+GstBaseAudioSrcSlaveMethod
+gst_base_audio_src_get_slave_method (GstBaseAudioSrc * src)
+{
+ GstBaseAudioSrcSlaveMethod result;
+
+ g_return_val_if_fail (GST_IS_BASE_AUDIO_SRC (src), -1);
+
+ GST_OBJECT_LOCK (src);
+ result = src->priv->slave_method;
+ GST_OBJECT_UNLOCK (src);
+
+ return result;
+}
+
static void
gst_base_audio_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
@@ -368,6 +488,9 @@
case PROP_PROVIDE_CLOCK:
gst_base_audio_src_set_provide_clock (src, g_value_get_boolean (value));
break;
+ case PROP_SLAVE_METHOD:
+ gst_base_audio_src_set_slave_method (src, g_value_get_enum (value));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -389,9 +512,28 @@
case PROP_LATENCY_TIME:
g_value_set_int64 (value, src->latency_time);
break;
+ case PROP_ACTUAL_BUFFER_TIME:
+ GST_OBJECT_LOCK (src);
+ if (src->ringbuffer && src->ringbuffer->acquired)
+ g_value_set_int64 (value, src->ringbuffer->spec.buffer_time);
+ else
+ g_value_set_int64 (value, DEFAULT_ACTUAL_BUFFER_TIME);
+ GST_OBJECT_UNLOCK (src);
+ break;
+ case PROP_ACTUAL_LATENCY_TIME:
+ GST_OBJECT_LOCK (src);
+ if (src->ringbuffer && src->ringbuffer->acquired)
+ g_value_set_int64 (value, src->ringbuffer->spec.latency_time);
+ else
+ g_value_set_int64 (value, DEFAULT_ACTUAL_LATENCY_TIME);
+ GST_OBJECT_UNLOCK (src);
+ break;
case PROP_PROVIDE_CLOCK:
g_value_set_boolean (value, gst_base_audio_src_get_provide_clock (src));
break;
+ case PROP_SLAVE_METHOD:
+ g_value_set_enum (value, gst_base_audio_src_get_slave_method (src));
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -463,6 +605,9 @@
gst_ring_buffer_debug_spec_buff (spec);
+ g_object_notify (G_OBJECT (src), "actual-buffer-time");
+ g_object_notify (G_OBJECT (src), "actual-latency-time");
+
return TRUE;
/* ERRORS */
@@ -538,21 +683,31 @@
gst_base_audio_src_event (GstBaseSrc * bsrc, GstEvent * event)
{
GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
+ gboolean res;
+
+ res = TRUE;
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_FLUSH_START:
+ GST_DEBUG_OBJECT (bsrc, "flush-start");
gst_ring_buffer_pause (src->ringbuffer);
gst_ring_buffer_clear_all (src->ringbuffer);
break;
case GST_EVENT_FLUSH_STOP:
+ GST_DEBUG_OBJECT (bsrc, "flush-stop");
/* always resync on sample after a flush */
src->next_sample = -1;
gst_ring_buffer_clear_all (src->ringbuffer);
break;
+ case GST_EVENT_SEEK:
+ GST_DEBUG_OBJECT (bsrc, "refuse to seek");
+ res = FALSE;
+ break;
default:
+ GST_DEBUG_OBJECT (bsrc, "dropping event %p", event);
break;
}
- return TRUE;
+ return res;
}
/* get the next offset in the ringbuffer for reading samples.
@@ -591,7 +746,7 @@
if (diff >= segtotal) {
GST_DEBUG_OBJECT (src, "dropped, align to segment %d", segdone);
/* sample would be dropped, position to next playable position */
- sample = (segdone - segtotal + 1) * sps;
+ sample = ((guint64) (segdone)) * sps;
}
return sample;
@@ -677,7 +832,9 @@
G_GUINT64_FORMAT, sample - src->next_sample, sample);
GST_ELEMENT_WARNING (src, CORE, CLOCK,
(_("Can't record audio fast enough")),
- ("dropped %" G_GUINT64_FORMAT " samples", sample - src->next_sample));
+ ("Dropped %" G_GUINT64_FORMAT " samples. This is most likely because "
+ "downstream can't keep up and is consuming samples too slowly.",
+ sample - src->next_sample));
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
}
@@ -689,28 +846,179 @@
spec->rate) - timestamp;
GST_OBJECT_LOCK (src);
- clock = GST_ELEMENT_CLOCK (src);
- if (clock != NULL && clock != src->clock) {
- GstClockTime base_time, latency;
+ if (!(clock = GST_ELEMENT_CLOCK (src)))
+ goto no_sync;
+
+ if (clock != src->clock) {
+ /* we are slaved, check how to handle this */
+ switch (src->priv->slave_method) {
+ case GST_BASE_AUDIO_SRC_SLAVE_RESAMPLE:
+ /* not implemented, use skew algorithm. This algorithm should
+ * work on the readout pointer and produces more or less samples based
+ * on the clock drift */
+ case GST_BASE_AUDIO_SRC_SLAVE_SKEW:
+ {
+ GstClockTime running_time;
+ GstClockTime base_time;
+ GstClockTime current_time;
+ guint64 running_time_sample;
+ gint running_time_segment;
+ gint current_segment;
+ gint segment_skew;
+ gint sps;
+
+ /* samples per segment */
+ sps = ringbuffer->samples_per_seg;
+
+ /* get the current time */
+ current_time = gst_clock_get_time (clock);
+
+ /* get the basetime */
+ base_time = GST_ELEMENT_CAST (src)->base_time;
+
+ /* get the running_time */
+ running_time = current_time - base_time;
+
+ /* the running_time converted to a sample (relative to the ringbuffer) */
+ running_time_sample =
+ gst_util_uint64_scale_int (running_time, spec->rate, GST_SECOND);
+
+ /* the segmentnr corrensponding to running_time, round down */
+ running_time_segment = running_time_sample / sps;
+
+ /* the segment currently read from the ringbuffer */
+ current_segment = sample / sps;
+
+ /* the skew we have between running_time and the ringbuffertime */
+ segment_skew = running_time_segment - current_segment;
+
+ GST_DEBUG_OBJECT (bsrc, "\n running_time = %" GST_TIME_FORMAT
+ "\n timestamp = %" GST_TIME_FORMAT
+ "\n running_time_segment = %d"
+ "\n current_segment = %d"
+ "\n segment_skew = %d",
+ GST_TIME_ARGS (running_time),
+ GST_TIME_ARGS (timestamp),
+ running_time_segment, current_segment, segment_skew);
- /* We are slaved to another clock, take running time of the clock and just
- * timestamp against it. Somebody else in the pipeline should figure out the
- * clock drift, for now. We keep the duration we calculated above. */
- timestamp = gst_clock_get_time (clock);
+ /* Resync the ringbuffer if:
+ * 1. We get one segment into the future.
+ * This is clearly a lie, because we can't
+ * possibly have a buffer with timestamp 1 at
+ * time 0. (unless it has time-travelled...)
+ *
+ * 2. We are more than the length of the ringbuffer behind.
+ * The length of the ringbuffer then gets to dictate
+ * the threshold for what is concidered "too late"
+ *
+ * 3. If this is our first buffer.
+ * We know that we should catch up to running_time
+ * the first time we are ran.
+ */
+ if ((segment_skew < 0) ||
+ (segment_skew >= ringbuffer->spec.segtotal) ||
+ (current_segment == 0)) {
+ gint segments_written;
+ gint first_segment;
+ gint last_segment;
+ gint new_last_segment;
+ gint segment_diff;
+ gint new_first_segment;
+ guint64 new_sample;
+
+ /* we are going to say that the last segment was captured at the current time
+ (running_time), minus one segment of creation-latency in the ringbuffer.
+ This can be thought of as: The segment arrived in the ringbuffer at time X, and
+ that means it was created at time X - (one segment). */
+ new_last_segment = running_time_segment - 1;
+
+ /* for better readablity */
+ first_segment = current_segment;
+
+ /* get the amount of segments written from the device by now */
+ segments_written = g_atomic_int_get (&ringbuffer->segdone);
+
+ /* subtract the base to segments_written to get the number of the
+ last written segment in the ringbuffer (one segment written = segment 0) */
+ last_segment = segments_written - ringbuffer->segbase - 1;
+
+ /* we see how many segments the ringbuffer was timeshifted */
+ segment_diff = new_last_segment - last_segment;
+
+ /* we move the first segment an equal amount */
+ new_first_segment = first_segment + segment_diff;
+
+ /* and we also move the segmentbase the same amount */
+ ringbuffer->segbase -= segment_diff;
+
+ /* we calculate the new sample value */
+ new_sample = ((guint64) new_first_segment) * sps;
+
+ /* and get the relative time to this -> our new timestamp */
+ timestamp =
+ gst_util_uint64_scale_int (new_sample, GST_SECOND, spec->rate);
+
+ /* we update the next sample accordingly */
+ src->next_sample = new_sample + samples;
+
+ GST_DEBUG_OBJECT (bsrc,
+ "Timeshifted the ringbuffer with %d segments: "
+ "Updating the timestamp to %" GST_TIME_FORMAT ", "
+ "and src->next_sample to %" G_GUINT64_FORMAT, segment_diff,
+ GST_TIME_ARGS (timestamp), src->next_sample);
+ }
+ break;
+ }
+ case GST_BASE_AUDIO_SRC_SLAVE_RETIMESTAMP:
+ {
+ GstClockTime base_time, latency;
+
+ /* We are slaved to another clock, take running time of the pipeline clock and
+ * timestamp against it. Somebody else in the pipeline should figure out the
+ * clock drift. We keep the duration we calculated above. */
+ timestamp = gst_clock_get_time (clock);
+ base_time = GST_ELEMENT_CAST (src)->base_time;
+
+ if (timestamp > base_time)
+ timestamp -= base_time;
+ else
+ timestamp = 0;
+
+ /* subtract latency */
+ latency =
+ gst_util_uint64_scale_int (total_samples, GST_SECOND, spec->rate);
+ if (timestamp > latency)
+ timestamp -= latency;
+ else
+ timestamp = 0;
+ }
+ case GST_BASE_AUDIO_SRC_SLAVE_NONE:
+ break;
+ }
+ } else {
+ GstClockTime base_time;
+
+ /* to get the timestamp against the clock we also need to add our offset */
+ timestamp = gst_audio_clock_adjust (clock, timestamp);
+
+ /* we are not slaved, subtract base_time */
base_time = GST_ELEMENT_CAST (src)->base_time;
- if (timestamp > base_time)
+ if (timestamp > base_time) {
timestamp -= base_time;
- else
+ GST_LOG_OBJECT (src,
+ "buffer timestamp %" GST_TIME_FORMAT " (base_time %" GST_TIME_FORMAT
+ ")", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (base_time));
+ } else {
+ GST_LOG_OBJECT (src,
+ "buffer timestamp 0, ts %" GST_TIME_FORMAT " <= base_time %"
+ GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
+ GST_TIME_ARGS (base_time));
timestamp = 0;
+ }
+ }
- /* subtract latency */
- latency = gst_util_uint64_scale_int (total_samples, GST_SECOND, spec->rate);
- if (timestamp > latency)
- timestamp -= latency;
- else
- timestamp = 0;
- }
+no_sync:
GST_OBJECT_UNLOCK (src);
GST_BUFFER_TIMESTAMP (buf) = timestamp;
@@ -718,8 +1026,6 @@
GST_BUFFER_OFFSET (buf) = sample;
GST_BUFFER_OFFSET_END (buf) = sample + samples;
- gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (bsrc)));
-
*outbuf = buf;
return GST_FLOW_OK;
@@ -785,9 +1091,12 @@
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
GST_DEBUG_OBJECT (src, "NULL->READY");
+ GST_OBJECT_LOCK (src);
if (src->ringbuffer == NULL) {
+ gst_audio_clock_reset (GST_AUDIO_CLOCK (src->clock), 0);
src->ringbuffer = gst_base_audio_src_create_ringbuffer (src);
}
+ GST_OBJECT_UNLOCK (src);
if (!gst_ring_buffer_open_device (src->ringbuffer))
goto open_failed;
break;
@@ -824,8 +1133,10 @@
case GST_STATE_CHANGE_READY_TO_NULL:
GST_DEBUG_OBJECT (src, "READY->NULL");
gst_ring_buffer_close_device (src->ringbuffer);
+ GST_OBJECT_LOCK (src);
gst_object_unparent (GST_OBJECT_CAST (src->ringbuffer));
src->ringbuffer = NULL;
+ GST_OBJECT_UNLOCK (src);
break;
default:
break;