summaryrefslogtreecommitdiff
path: root/gst/debugutils
diff options
context:
space:
mode:
authorVivia Nikolaidou <vivia@ahiru.eu>2017-09-06 13:16:21 +0300
committerVivia Nikolaidou <vivia@ahiru.eu>2017-11-07 16:45:52 +0200
commit595254f21b15702d95d5a7cf6e43d29f9c179f43 (patch)
tree3dbf7906b84ae2c6b15041c31bd4cce8941e89db /gst/debugutils
parentc1294e10f99adf04a104397da735904634400464 (diff)
downloadgstreamer-plugins-bad-595254f21b15702d95d5a7cf6e43d29f9c179f43.tar.gz
debugutils: Added new jitterer element
This element can be configured to add jitter and/or drift to incoming buffers' PTS, DTS, or both. Amplitude and average of jitter and drift are configurable. https://bugzilla.gnome.org/show_bug.cgi?id=787358
Diffstat (limited to 'gst/debugutils')
-rw-r--r--gst/debugutils/Makefile.am6
-rw-r--r--gst/debugutils/debugutilsbad.c3
-rw-r--r--gst/debugutils/gstjitterer.c387
-rw-r--r--gst/debugutils/gstjitterer.h67
-rw-r--r--gst/debugutils/meson.build1
5 files changed, 462 insertions, 2 deletions
diff --git a/gst/debugutils/Makefile.am b/gst/debugutils/Makefile.am
index ee8e57dd3..5948aea08 100644
--- a/gst/debugutils/Makefile.am
+++ b/gst/debugutils/Makefile.am
@@ -8,7 +8,8 @@ libgstdebugutilsbad_la_SOURCES = \
gstchopmydata.c \
gstcompare.c \
gstwatchdog.c \
- gsterrorignore.c
+ gsterrorignore.c \
+ gstjitterer.c
libgstdebugutilsbad_la_CFLAGS = $(GST_CFLAGS) $(GST_BASE_CFLAGS) $(GST_PLUGINS_BASE_CFLAGS)
libgstdebugutilsbad_la_LIBADD = $(GST_BASE_LIBS) $(GST_PLUGINS_BASE_LIBS) \
@@ -22,4 +23,5 @@ noinst_HEADERS = fpsdisplaysink.h \
gstcompare.h \
gstdebugspy.h \
gstwatchdog.h \
- gsterrorignore.h
+ gsterrorignore.h \
+ gstjitterer.h
diff --git a/gst/debugutils/debugutilsbad.c b/gst/debugutils/debugutilsbad.c
index 68edd7829..30e4629f3 100644
--- a/gst/debugutils/debugutilsbad.c
+++ b/gst/debugutils/debugutilsbad.c
@@ -29,6 +29,7 @@ GType gst_chop_my_data_get_type (void);
GType gst_compare_get_type (void);
GType gst_debug_spy_get_type (void);
GType gst_error_ignore_get_type (void);
+GType gst_jitterer_get_type (void);
GType gst_watchdog_get_type (void);
static gboolean
@@ -48,6 +49,8 @@ plugin_init (GstPlugin * plugin)
gst_watchdog_get_type ());
gst_element_register (plugin, "errorignore", GST_RANK_NONE,
gst_error_ignore_get_type ());
+ gst_element_register (plugin, "jitterer", GST_RANK_NONE,
+ gst_jitterer_get_type ());
return TRUE;
}
diff --git a/gst/debugutils/gstjitterer.c b/gst/debugutils/gstjitterer.c
new file mode 100644
index 000000000..90b6ce269
--- /dev/null
+++ b/gst/debugutils/gstjitterer.c
@@ -0,0 +1,387 @@
+/*
+ * GStreamer
+ * Copyright (C) 2017 Vivia Nikolaidou <vivia@ahiru.eu>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+/**
+ * SECTION:element-jitterer
+ * @title: jitterer
+ *
+ * Adds jitter and/or drift to a buffer's PTS and/or DTS. Amplitude and
+ * average of jitter and drift are configurable.
+ *
+ * ## Example launch line
+ * |[
+ * gst-launch-1.0 videotestsrc ! jitterer drift-average=100 drift-amplitude=10 ! autovideosink
+ * ]|
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "gstjitterer.h"
+
+#define GST_CAT_DEFAULT gst_jitterer_debug
+GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
+
+enum
+{
+ PROP_0,
+ PROP_JITTER_AMPL,
+ PROP_JITTER_AVG,
+ PROP_DRIFT_AMPL,
+ PROP_DRIFT_AVG,
+ PROP_CHANGE_PTS,
+ PROP_CHANGE_DTS
+};
+
+static void gst_jitterer_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * pspec);
+static void gst_jitterer_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * pspec);
+static void gst_jitterer_finalize (GObject * object);
+
+static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
+ GST_PAD_SINK,
+ GST_PAD_ALWAYS,
+ GST_STATIC_CAPS_ANY);
+
+static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
+ GST_PAD_SRC,
+ GST_PAD_ALWAYS,
+ GST_STATIC_CAPS_ANY);
+
+#define parent_class gst_jitterer_parent_class
+G_DEFINE_TYPE (GstJitterer, gst_jitterer, GST_TYPE_ELEMENT);
+
+static GstFlowReturn gst_jitterer_sink_chain (GstPad * pad,
+ GstObject * parent, GstBuffer * inbuf);
+static gboolean gst_jitterer_sink_event (GstPad * pad, GstObject * parent,
+ GstEvent * event);
+
+static void
+gst_jitterer_class_init (GstJittererClass * klass)
+{
+ GstElementClass *gstelement_class;
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ GST_DEBUG_CATEGORY_INIT (gst_jitterer_debug, "jitterer", 0,
+ "Add jitter and/or drift to buffers");
+
+ gstelement_class = (GstElementClass *) klass;
+
+ gst_element_class_set_static_metadata (gstelement_class,
+ "Jitterer", "Generic", "Add jitter and/or drift to buffers",
+ "Vivia Nikolaidou <vivia@ahiru.eu>");
+
+ gst_element_class_add_static_pad_template (gstelement_class, &src_template);
+ gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
+
+ /* define virtual function pointers */
+ object_class->set_property = gst_jitterer_set_property;
+ object_class->get_property = gst_jitterer_get_property;
+ object_class->finalize = gst_jitterer_finalize;
+
+ /* define properties */
+ g_object_class_install_property (object_class, PROP_JITTER_AMPL,
+ g_param_spec_uint64 ("jitter-amplitude", "Jitter amplitude",
+ "Amplitude of the jitter to apply",
+ 0, G_MAXINT64 / 2, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_property (object_class, PROP_JITTER_AVG,
+ g_param_spec_int64 ("jitter-average", "Jitter average",
+ "Average of the jitter to apply",
+ -G_MAXINT64 / 2, G_MAXINT64 / 2, 0,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_property (object_class, PROP_DRIFT_AMPL,
+ g_param_spec_uint64 ("drift-amplitude", "Drift amplitude",
+ "Amplitude of the drift to apply",
+ 0, G_MAXINT64 / 2, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_property (object_class, PROP_DRIFT_AVG,
+ g_param_spec_int64 ("drift-average", "Drift average",
+ "Average of the drift to apply",
+ -G_MAXINT64 / 2, G_MAXINT64 / 2, 0,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_property (object_class, PROP_CHANGE_PTS,
+ g_param_spec_boolean ("change-pts", "Change PTS",
+ "Whether to change the PTS of incoming buffers",
+ TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_property (object_class, PROP_CHANGE_DTS,
+ g_param_spec_boolean ("change-dts", "Change DTS",
+ "Whether to change the DTS of incoming buffers",
+ TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+}
+
+static void
+gst_jitterer_init (GstJitterer * self)
+{
+ self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
+ gst_pad_set_chain_function (self->sinkpad,
+ GST_DEBUG_FUNCPTR (gst_jitterer_sink_chain));
+ gst_pad_set_event_function (self->sinkpad,
+ GST_DEBUG_FUNCPTR (gst_jitterer_sink_event));
+ GST_PAD_SET_PROXY_ALLOCATION (self->sinkpad);
+ GST_PAD_SET_PROXY_CAPS (self->sinkpad);
+ GST_PAD_SET_PROXY_SCHEDULING (self->sinkpad);
+ gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
+
+ self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
+ gst_pad_use_fixed_caps (self->srcpad);
+ GST_PAD_SET_PROXY_ALLOCATION (self->srcpad);
+ GST_PAD_SET_PROXY_CAPS (self->srcpad);
+ GST_PAD_SET_PROXY_SCHEDULING (self->srcpad);
+ gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
+
+ self->jitter_ampl = 0;
+ self->jitter_avg = 0;
+ self->drift_ampl = 0;
+ self->drift_avg = 0;
+ self->change_pts = TRUE;
+ self->change_dts = TRUE;
+ self->prev_pts = GST_CLOCK_TIME_NONE;
+
+ self->dts_drift_so_far = 0;
+ self->pts_drift_so_far = 0;
+ self->rand = g_rand_new ();
+}
+
+static void
+gst_jitterer_finalize (GObject * object)
+{
+ GstJitterer *self = GST_JITTERER (object);
+
+ g_rand_free (self->rand);
+
+ G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+gst_jitterer_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * pspec)
+{
+ GstJitterer *self = GST_JITTERER (object);
+
+ switch (prop_id) {
+ case PROP_JITTER_AMPL:
+ self->jitter_ampl = g_value_get_uint64 (value);
+ break;
+ case PROP_JITTER_AVG:
+ self->jitter_avg = g_value_get_int64 (value);
+ break;
+ case PROP_DRIFT_AMPL:
+ self->drift_ampl = g_value_get_uint64 (value);
+ break;
+ case PROP_DRIFT_AVG:
+ self->drift_avg = g_value_get_int64 (value);
+ break;
+ case PROP_CHANGE_PTS:
+ self->change_pts = g_value_get_boolean (value);
+ break;
+ case PROP_CHANGE_DTS:
+ self->change_dts = g_value_get_boolean (value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gst_jitterer_get_property (GObject * object, guint prop_id, GValue * value,
+ GParamSpec * pspec)
+{
+ GstJitterer *self = GST_JITTERER (object);
+
+ switch (prop_id) {
+ case PROP_JITTER_AMPL:
+ g_value_set_uint64 (value, self->jitter_ampl);
+ break;
+ case PROP_JITTER_AVG:
+ g_value_set_int64 (value, self->jitter_avg);
+ break;
+ case PROP_DRIFT_AMPL:
+ g_value_set_uint64 (value, self->drift_ampl);
+ break;
+ case PROP_DRIFT_AVG:
+ g_value_set_int64 (value, self->drift_avg);
+ break;
+ case PROP_CHANGE_PTS:
+ g_value_set_boolean (value, self->change_pts);
+ break;
+ case PROP_CHANGE_DTS:
+ g_value_set_boolean (value, self->change_dts);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static gint64
+gst_jitterer_rand_uint64_range (GRand * rand, gint64 min, gint64 max)
+{
+ gint64 dist;
+
+ g_assert (max > min);
+ dist = max - min;
+
+ if (G_LIKELY (dist < G_MAXINT32)) {
+ return g_rand_int_range (rand, 0, dist) + min;
+ } else {
+ /* This code is based on g_rand_int_range source */
+ gint64 ret = max + 1;
+ /* maxvalue is set to the predecessor of the greatest
+ * multiple of dist less or equal 2^64.
+ */
+ guint64 maxvalue;
+ if (dist <= 0x8000000000000000u) { /* 2^63 */
+ /* maxvalue = 2^64 - 1 - (2^64 % dist) */
+ guint64 leftover = (0x8000000000000000u % dist) * 2;
+ if (leftover >= dist)
+ leftover -= dist;
+ maxvalue = 0xffffffffffffffffu - leftover;
+ } else {
+ maxvalue = dist - 1;
+ }
+ do {
+ gint32 highrand, lowrand;
+
+ highrand = g_rand_int (rand);
+ lowrand = g_rand_int (rand);
+ ret = ((((gint64) highrand) << 32) | lowrand);
+ } while (ret > maxvalue);
+ ret %= dist;
+ return ret;
+ }
+}
+
+static gboolean
+gst_jitterer_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
+{
+ GstJitterer *self = GST_JITTERER (parent);
+
+ switch (GST_EVENT_TYPE (event)) {
+ case GST_EVENT_SEGMENT:
+ case GST_EVENT_FLUSH_STOP:
+ self->prev_pts = GST_CLOCK_TIME_NONE;
+ break;
+ default:
+ break;
+ }
+ return gst_pad_event_default (pad, parent, event);
+}
+
+static GstFlowReturn
+gst_jitterer_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * inbuf)
+{
+ GstJitterer *self = GST_JITTERER (parent);
+
+ if (self->jitter_ampl > 0) {
+ gint64 min, max;
+ gint64 jitter;
+
+ min = self->jitter_avg - self->jitter_ampl / 2;
+ max = self->jitter_avg + self->jitter_ampl / 2;
+
+ if (self->change_pts && GST_BUFFER_PTS (inbuf) != GST_CLOCK_TIME_NONE) {
+ jitter = gst_jitterer_rand_uint64_range (self->rand, min, max);
+ if (GST_BUFFER_PTS (inbuf) + jitter > 0)
+ GST_BUFFER_PTS (inbuf) = GST_BUFFER_PTS (inbuf) + jitter;
+ }
+ if (self->change_dts && GST_BUFFER_DTS (inbuf) != GST_CLOCK_TIME_NONE) {
+ jitter = gst_jitterer_rand_uint64_range (self->rand, min, max);
+ if (GST_BUFFER_DTS (inbuf) + jitter > 0)
+ GST_BUFFER_DTS (inbuf) = GST_BUFFER_DTS (inbuf) + jitter;
+ }
+ } else {
+ if (self->change_pts && GST_BUFFER_PTS (inbuf) != GST_CLOCK_TIME_NONE)
+ GST_BUFFER_PTS (inbuf) = GST_BUFFER_PTS (inbuf) + self->jitter_avg;
+ if (self->change_dts && GST_BUFFER_DTS (inbuf) != GST_CLOCK_TIME_NONE)
+ GST_BUFFER_DTS (inbuf) = GST_BUFFER_DTS (inbuf) + self->jitter_avg;
+ }
+
+ if (self->prev_pts != GST_CLOCK_TIME_NONE) {
+ GstClockTimeDiff drift_avg_per_frame;
+ GstClockTime pts_diff = GST_BUFFER_PTS (inbuf) - self->prev_pts;
+
+ if (self->drift_avg == 0) {
+ drift_avg_per_frame = 0;
+ } else if (self->drift_avg > 0) {
+ drift_avg_per_frame =
+ gst_util_uint64_scale (self->drift_avg, pts_diff, GST_SECOND);
+ } else {
+ drift_avg_per_frame =
+ -gst_util_uint64_scale (-self->drift_avg, pts_diff, GST_SECOND);
+ }
+
+ if (self->drift_ampl > 0) {
+ gint min, max;
+ GstClockTime drift_ampl_per_frame;
+
+ drift_ampl_per_frame =
+ gst_util_uint64_scale (self->drift_ampl, pts_diff, GST_SECOND);
+
+ min = drift_avg_per_frame - drift_ampl_per_frame / 2;
+ max = drift_avg_per_frame + drift_ampl_per_frame / 2;
+
+ if (self->change_pts && GST_BUFFER_PTS (inbuf) != GST_CLOCK_TIME_NONE) {
+ /* FIXME: if (min == max) means the amplitude is too small, make a
+ * probabilistic +1 or -1 instead */
+ if (min == max)
+ self->pts_drift_so_far += drift_avg_per_frame;
+ else
+ self->pts_drift_so_far +=
+ gst_jitterer_rand_uint64_range (self->rand, min, max);
+ if (GST_BUFFER_PTS (inbuf) + self->pts_drift_so_far > 0)
+ GST_BUFFER_PTS (inbuf) =
+ GST_BUFFER_PTS (inbuf) + self->pts_drift_so_far;
+ }
+ if (self->change_dts && GST_BUFFER_DTS (inbuf) != GST_CLOCK_TIME_NONE) {
+ if (min == max)
+ self->pts_drift_so_far += drift_avg_per_frame;
+ else
+ self->dts_drift_so_far +=
+ gst_jitterer_rand_uint64_range (self->rand, min, max);
+ if (GST_BUFFER_DTS (inbuf) + self->dts_drift_so_far > 0)
+ GST_BUFFER_DTS (inbuf) =
+ GST_BUFFER_DTS (inbuf) + self->dts_drift_so_far;
+ }
+ } else {
+ if (self->change_pts && GST_BUFFER_PTS (inbuf) != GST_CLOCK_TIME_NONE) {
+ self->pts_drift_so_far += drift_avg_per_frame;
+ GST_BUFFER_PTS (inbuf) =
+ GST_BUFFER_PTS (inbuf) + self->pts_drift_so_far;
+ }
+ if (self->change_dts && GST_BUFFER_DTS (inbuf) != GST_CLOCK_TIME_NONE) {
+ self->dts_drift_so_far += drift_avg_per_frame;
+ GST_BUFFER_DTS (inbuf) =
+ GST_BUFFER_DTS (inbuf) + self->dts_drift_so_far;
+ }
+ }
+ }
+ self->prev_pts = GST_BUFFER_PTS (inbuf);
+
+ return gst_pad_push (self->srcpad, inbuf);
+}
diff --git a/gst/debugutils/gstjitterer.h b/gst/debugutils/gstjitterer.h
new file mode 100644
index 000000000..12b472c5a
--- /dev/null
+++ b/gst/debugutils/gstjitterer.h
@@ -0,0 +1,67 @@
+/*
+ * GStreamer
+ * Copyright (C) 2017 Vivia Nikolaidou <vivia@ahiru.eu>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __GST_JITTERER_H__
+#define __GST_JITTERER_H__
+
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+
+#define GST_TYPE_JITTERER (gst_jitterer_get_type())
+#define GST_JITTERER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_JITTERER,GstJitterer))
+#define GST_IS_JITTERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_JITTERER))
+#define GST_JITTERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_JITTERER,GstJittererClass))
+#define GST_IS_JITTERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_JITTERER))
+#define GST_JITTERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_JITTERER,GstJittererClass))
+
+typedef struct _GstJitterer GstJitterer;
+typedef struct _GstJittererClass GstJittererClass;
+
+struct _GstJitterer {
+ GstElement parent;
+
+ GstClockTime jitter_ampl;
+ GstClockTimeDiff jitter_avg;
+ GstClockTime drift_ampl;
+ GstClockTimeDiff drift_avg;
+ gboolean change_pts;
+ gboolean change_dts;
+
+ gint64 dts_drift_so_far;
+ gint64 pts_drift_so_far;
+ GRand *rand;
+ GstClockTime prev_pts;
+
+ GstPad *srcpad;
+ GstPad *sinkpad;
+};
+
+struct _GstJittererClass {
+ GstElementClass parent_class;
+};
+
+GType gst_jitterer_get_type (void);
+
+gboolean gst_jitterer_plugin_init (GstPlugin * plugin);
+
+G_END_DECLS
+
+#endif /* __GST_JITTERER_H__ */
diff --git a/gst/debugutils/meson.build b/gst/debugutils/meson.build
index 648984df5..f6ed17dd0 100644
--- a/gst/debugutils/meson.build
+++ b/gst/debugutils/meson.build
@@ -1,6 +1,7 @@
debugutilsbad_sources = [
'gstdebugspy.c',
'gsterrorignore.c',
+ 'gstjitterer.c',
'debugutilsbad.c',
'fpsdisplaysink.c',
'gstchecksumsink.c',