netsim: remove private struct and padding

There's no need for these things, since the plugin headers
are not public, and all the extra ->priv-> just clutter the
code.
This commit is contained in:
Tim-Philipp Müller 2017-11-24 22:09:25 +00:00
parent 2877aac9a5
commit 5187fdfb07
2 changed files with 86 additions and 106 deletions

View file

@ -45,24 +45,6 @@ enum
ARG_DROP_PACKETS ARG_DROP_PACKETS
}; };
struct _GstNetSimPrivate
{
GstPad *sinkpad, *srcpad;
GMutex loop_mutex;
GCond start_cond;
GMainLoop *main_loop;
gboolean running;
GRand *rand_seed;
gint min_delay;
gint max_delay;
gfloat delay_probability;
gfloat drop_probability;
gfloat duplicate_probability;
guint drop_packets;
};
/* these numbers are nothing but wild guesses and dont reflect any reality */ /* these numbers are nothing but wild guesses and dont reflect any reality */
#define DEFAULT_MIN_DELAY 200 #define DEFAULT_MIN_DELAY 200
#define DEFAULT_MAX_DELAY 400 #define DEFAULT_MAX_DELAY 400
@ -71,10 +53,6 @@ struct _GstNetSimPrivate
#define DEFAULT_DUPLICATE_PROBABILITY 0.0 #define DEFAULT_DUPLICATE_PROBABILITY 0.0
#define DEFAULT_DROP_PACKETS 0 #define DEFAULT_DROP_PACKETS 0
#define GST_NET_SIM_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_NET_SIM, \
GstNetSimPrivate))
static GstStaticPadTemplate gst_net_sim_sink_template = static GstStaticPadTemplate gst_net_sim_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink", GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK, GST_PAD_SINK,
@ -96,24 +74,24 @@ gst_net_sim_loop (GstNetSim * netsim)
GST_TRACE_OBJECT (netsim, "TASK: begin"); GST_TRACE_OBJECT (netsim, "TASK: begin");
g_mutex_lock (&netsim->priv->loop_mutex); g_mutex_lock (&netsim->loop_mutex);
loop = g_main_loop_ref (netsim->priv->main_loop); loop = g_main_loop_ref (netsim->main_loop);
netsim->priv->running = TRUE; netsim->running = TRUE;
GST_TRACE_OBJECT (netsim, "TASK: signal start"); GST_TRACE_OBJECT (netsim, "TASK: signal start");
g_cond_signal (&netsim->priv->start_cond); g_cond_signal (&netsim->start_cond);
g_mutex_unlock (&netsim->priv->loop_mutex); g_mutex_unlock (&netsim->loop_mutex);
GST_TRACE_OBJECT (netsim, "TASK: run"); GST_TRACE_OBJECT (netsim, "TASK: run");
g_main_loop_run (loop); g_main_loop_run (loop);
g_main_loop_unref (loop); g_main_loop_unref (loop);
g_mutex_lock (&netsim->priv->loop_mutex); g_mutex_lock (&netsim->loop_mutex);
GST_TRACE_OBJECT (netsim, "TASK: pause"); GST_TRACE_OBJECT (netsim, "TASK: pause");
gst_pad_pause_task (netsim->priv->srcpad); gst_pad_pause_task (netsim->srcpad);
netsim->priv->running = FALSE; netsim->running = FALSE;
GST_TRACE_OBJECT (netsim, "TASK: signal end"); GST_TRACE_OBJECT (netsim, "TASK: signal end");
g_cond_signal (&netsim->priv->start_cond); g_cond_signal (&netsim->start_cond);
g_mutex_unlock (&netsim->priv->loop_mutex); g_mutex_unlock (&netsim->loop_mutex);
GST_TRACE_OBJECT (netsim, "TASK: end"); GST_TRACE_OBJECT (netsim, "TASK: end");
} }
@ -137,25 +115,25 @@ gst_net_sim_src_activatemode (GstPad * pad, GstObject * parent,
(void) pad; (void) pad;
(void) mode; (void) mode;
g_mutex_lock (&netsim->priv->loop_mutex); g_mutex_lock (&netsim->loop_mutex);
if (active) { if (active) {
if (netsim->priv->main_loop == NULL) { if (netsim->main_loop == NULL) {
GMainContext *main_context = g_main_context_new (); GMainContext *main_context = g_main_context_new ();
netsim->priv->main_loop = g_main_loop_new (main_context, FALSE); netsim->main_loop = g_main_loop_new (main_context, FALSE);
g_main_context_unref (main_context); g_main_context_unref (main_context);
GST_TRACE_OBJECT (netsim, "ACT: Starting task on srcpad"); GST_TRACE_OBJECT (netsim, "ACT: Starting task on srcpad");
result = gst_pad_start_task (netsim->priv->srcpad, result = gst_pad_start_task (netsim->srcpad,
(GstTaskFunction) gst_net_sim_loop, netsim, NULL); (GstTaskFunction) gst_net_sim_loop, netsim, NULL);
GST_TRACE_OBJECT (netsim, "ACT: Wait for task to start"); GST_TRACE_OBJECT (netsim, "ACT: Wait for task to start");
g_assert (!netsim->priv->running); g_assert (!netsim->running);
while (!netsim->priv->running) while (!netsim->running)
g_cond_wait (&netsim->priv->start_cond, &netsim->priv->loop_mutex); g_cond_wait (&netsim->start_cond, &netsim->loop_mutex);
GST_TRACE_OBJECT (netsim, "ACT: Task on srcpad started"); GST_TRACE_OBJECT (netsim, "ACT: Task on srcpad started");
} }
} else { } else {
if (netsim->priv->main_loop != NULL) { if (netsim->main_loop != NULL) {
GSource *source; GSource *source;
guint id; guint id;
@ -164,26 +142,26 @@ gst_net_sim_src_activatemode (GstPad * pad, GstObject * parent,
GST_TRACE_OBJECT (netsim, "DEACT: Stopping main loop on deactivate"); GST_TRACE_OBJECT (netsim, "DEACT: Stopping main loop on deactivate");
source = g_idle_source_new (); source = g_idle_source_new ();
g_source_set_callback (source, _main_loop_quit_and_remove_source, g_source_set_callback (source, _main_loop_quit_and_remove_source,
g_main_loop_ref (netsim->priv->main_loop), g_main_loop_ref (netsim->main_loop),
(GDestroyNotify) g_main_loop_unref); (GDestroyNotify) g_main_loop_unref);
id = g_source_attach (source, id = g_source_attach (source,
g_main_loop_get_context (netsim->priv->main_loop)); g_main_loop_get_context (netsim->main_loop));
g_source_unref (source); g_source_unref (source);
g_assert_cmpuint (id, >, 0); g_assert_cmpuint (id, >, 0);
g_main_loop_unref (netsim->priv->main_loop); g_main_loop_unref (netsim->main_loop);
netsim->priv->main_loop = NULL; netsim->main_loop = NULL;
GST_TRACE_OBJECT (netsim, "DEACT: Wait for mainloop and task to pause"); GST_TRACE_OBJECT (netsim, "DEACT: Wait for mainloop and task to pause");
g_assert (netsim->priv->running); g_assert (netsim->running);
while (netsim->priv->running) while (netsim->running)
g_cond_wait (&netsim->priv->start_cond, &netsim->priv->loop_mutex); g_cond_wait (&netsim->start_cond, &netsim->loop_mutex);
GST_TRACE_OBJECT (netsim, "DEACT: Stopping task on srcpad"); GST_TRACE_OBJECT (netsim, "DEACT: Stopping task on srcpad");
result = gst_pad_stop_task (netsim->priv->srcpad); result = gst_pad_stop_task (netsim->srcpad);
GST_TRACE_OBJECT (netsim, "DEACT: Mainloop and GstTask stopped"); GST_TRACE_OBJECT (netsim, "DEACT: Mainloop and GstTask stopped");
} }
} }
g_mutex_unlock (&netsim->priv->loop_mutex); g_mutex_unlock (&netsim->loop_mutex);
return result; return result;
} }
@ -226,24 +204,23 @@ gst_net_sim_delay_buffer (GstNetSim * netsim, GstBuffer * buf)
{ {
GstFlowReturn ret = GST_FLOW_OK; GstFlowReturn ret = GST_FLOW_OK;
g_mutex_lock (&netsim->priv->loop_mutex); g_mutex_lock (&netsim->loop_mutex);
if (netsim->priv->main_loop != NULL && netsim->priv->delay_probability > 0 && if (netsim->main_loop != NULL && netsim->delay_probability > 0 &&
g_rand_double (netsim->priv->rand_seed) < netsim->priv->delay_probability) g_rand_double (netsim->rand_seed) < netsim->delay_probability) {
{ PushBufferCtx *ctx = push_buffer_ctx_new (netsim->srcpad, buf);
PushBufferCtx *ctx = push_buffer_ctx_new (netsim->priv->srcpad, buf); gint delay = g_rand_int_range (netsim->rand_seed,
gint delay = g_rand_int_range (netsim->priv->rand_seed, netsim->min_delay, netsim->max_delay);
netsim->priv->min_delay, netsim->priv->max_delay);
GSource *source = g_timeout_source_new (delay); GSource *source = g_timeout_source_new (delay);
GST_DEBUG_OBJECT (netsim, "Delaying packet by %d", delay); GST_DEBUG_OBJECT (netsim, "Delaying packet by %d", delay);
g_source_set_callback (source, (GSourceFunc) push_buffer_ctx_push, g_source_set_callback (source, (GSourceFunc) push_buffer_ctx_push,
ctx, (GDestroyNotify) push_buffer_ctx_free); ctx, (GDestroyNotify) push_buffer_ctx_free);
g_source_attach (source, g_main_loop_get_context (netsim->priv->main_loop)); g_source_attach (source, g_main_loop_get_context (netsim->main_loop));
g_source_unref (source); g_source_unref (source);
} else { } else {
ret = gst_pad_push (netsim->priv->srcpad, gst_buffer_ref (buf)); ret = gst_pad_push (netsim->srcpad, gst_buffer_ref (buf));
} }
g_mutex_unlock (&netsim->priv->loop_mutex); g_mutex_unlock (&netsim->loop_mutex);
return ret; return ret;
} }
@ -256,17 +233,17 @@ gst_net_sim_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
(void) pad; (void) pad;
if (netsim->priv->drop_packets > 0) { if (netsim->drop_packets > 0) {
netsim->priv->drop_packets--; netsim->drop_packets--;
GST_DEBUG_OBJECT (netsim, "Dropping packet (%d left)", GST_DEBUG_OBJECT (netsim, "Dropping packet (%d left)",
netsim->priv->drop_packets); netsim->drop_packets);
} else if (netsim->priv->drop_probability > 0 } else if (netsim->drop_probability > 0
&& g_rand_double (netsim->priv->rand_seed) < && g_rand_double (netsim->rand_seed) <
(gdouble) netsim->priv->drop_probability) { (gdouble) netsim->drop_probability) {
GST_DEBUG_OBJECT (netsim, "Dropping packet"); GST_DEBUG_OBJECT (netsim, "Dropping packet");
} else if (netsim->priv->duplicate_probability > 0 && } else if (netsim->duplicate_probability > 0 &&
g_rand_double (netsim->priv->rand_seed) < g_rand_double (netsim->rand_seed) <
(gdouble) netsim->priv->duplicate_probability) { (gdouble) netsim->duplicate_probability) {
GST_DEBUG_OBJECT (netsim, "Duplicating packet"); GST_DEBUG_OBJECT (netsim, "Duplicating packet");
gst_net_sim_delay_buffer (netsim, buf); gst_net_sim_delay_buffer (netsim, buf);
ret = gst_net_sim_delay_buffer (netsim, buf); ret = gst_net_sim_delay_buffer (netsim, buf);
@ -291,22 +268,22 @@ gst_net_sim_set_property (GObject * object,
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
case ARG_MIN_DELAY: case ARG_MIN_DELAY:
netsim->priv->min_delay = g_value_get_int (value); netsim->min_delay = g_value_get_int (value);
break; break;
case ARG_MAX_DELAY: case ARG_MAX_DELAY:
netsim->priv->max_delay = g_value_get_int (value); netsim->max_delay = g_value_get_int (value);
break; break;
case ARG_DELAY_PROBABILITY: case ARG_DELAY_PROBABILITY:
netsim->priv->delay_probability = g_value_get_float (value); netsim->delay_probability = g_value_get_float (value);
break; break;
case ARG_DROP_PROBABILITY: case ARG_DROP_PROBABILITY:
netsim->priv->drop_probability = g_value_get_float (value); netsim->drop_probability = g_value_get_float (value);
break; break;
case ARG_DUPLICATE_PROBABILITY: case ARG_DUPLICATE_PROBABILITY:
netsim->priv->duplicate_probability = g_value_get_float (value); netsim->duplicate_probability = g_value_get_float (value);
break; break;
case ARG_DROP_PACKETS: case ARG_DROP_PACKETS:
netsim->priv->drop_packets = g_value_get_uint (value); netsim->drop_packets = g_value_get_uint (value);
break; break;
} }
} }
@ -322,22 +299,22 @@ gst_net_sim_get_property (GObject * object,
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
case ARG_MIN_DELAY: case ARG_MIN_DELAY:
g_value_set_int (value, netsim->priv->min_delay); g_value_set_int (value, netsim->min_delay);
break; break;
case ARG_MAX_DELAY: case ARG_MAX_DELAY:
g_value_set_int (value, netsim->priv->max_delay); g_value_set_int (value, netsim->max_delay);
break; break;
case ARG_DELAY_PROBABILITY: case ARG_DELAY_PROBABILITY:
g_value_set_float (value, netsim->priv->delay_probability); g_value_set_float (value, netsim->delay_probability);
break; break;
case ARG_DROP_PROBABILITY: case ARG_DROP_PROBABILITY:
g_value_set_float (value, netsim->priv->drop_probability); g_value_set_float (value, netsim->drop_probability);
break; break;
case ARG_DUPLICATE_PROBABILITY: case ARG_DUPLICATE_PROBABILITY:
g_value_set_float (value, netsim->priv->duplicate_probability); g_value_set_float (value, netsim->duplicate_probability);
break; break;
case ARG_DROP_PACKETS: case ARG_DROP_PACKETS:
g_value_set_uint (value, netsim->priv->drop_packets); g_value_set_uint (value, netsim->drop_packets);
break; break;
} }
} }
@ -346,27 +323,25 @@ gst_net_sim_get_property (GObject * object,
static void static void
gst_net_sim_init (GstNetSim * netsim) gst_net_sim_init (GstNetSim * netsim)
{ {
netsim->priv = GST_NET_SIM_GET_PRIVATE (netsim); netsim->srcpad =
netsim->priv->srcpad =
gst_pad_new_from_static_template (&gst_net_sim_src_template, "src"); gst_pad_new_from_static_template (&gst_net_sim_src_template, "src");
netsim->priv->sinkpad = netsim->sinkpad =
gst_pad_new_from_static_template (&gst_net_sim_sink_template, "sink"); gst_pad_new_from_static_template (&gst_net_sim_sink_template, "sink");
gst_element_add_pad (GST_ELEMENT (netsim), netsim->priv->srcpad); gst_element_add_pad (GST_ELEMENT (netsim), netsim->srcpad);
gst_element_add_pad (GST_ELEMENT (netsim), netsim->priv->sinkpad); gst_element_add_pad (GST_ELEMENT (netsim), netsim->sinkpad);
g_mutex_init (&netsim->priv->loop_mutex); g_mutex_init (&netsim->loop_mutex);
g_cond_init (&netsim->priv->start_cond); g_cond_init (&netsim->start_cond);
netsim->priv->rand_seed = g_rand_new (); netsim->rand_seed = g_rand_new ();
netsim->priv->main_loop = NULL; netsim->main_loop = NULL;
GST_OBJECT_FLAG_SET (netsim->priv->sinkpad, GST_OBJECT_FLAG_SET (netsim->sinkpad,
GST_PAD_FLAG_PROXY_CAPS | GST_PAD_FLAG_PROXY_ALLOCATION); GST_PAD_FLAG_PROXY_CAPS | GST_PAD_FLAG_PROXY_ALLOCATION);
gst_pad_set_chain_function (netsim->priv->sinkpad, gst_pad_set_chain_function (netsim->sinkpad,
GST_DEBUG_FUNCPTR (gst_net_sim_chain)); GST_DEBUG_FUNCPTR (gst_net_sim_chain));
gst_pad_set_activatemode_function (netsim->priv->srcpad, gst_pad_set_activatemode_function (netsim->srcpad,
GST_DEBUG_FUNCPTR (gst_net_sim_src_activatemode)); GST_DEBUG_FUNCPTR (gst_net_sim_src_activatemode));
} }
@ -375,9 +350,9 @@ gst_net_sim_finalize (GObject * object)
{ {
GstNetSim *netsim = GST_NET_SIM (object); GstNetSim *netsim = GST_NET_SIM (object);
g_rand_free (netsim->priv->rand_seed); g_rand_free (netsim->rand_seed);
g_mutex_clear (&netsim->priv->loop_mutex); g_mutex_clear (&netsim->loop_mutex);
g_cond_clear (&netsim->priv->start_cond); g_cond_clear (&netsim->start_cond);
G_OBJECT_CLASS (gst_net_sim_parent_class)->finalize (object); G_OBJECT_CLASS (gst_net_sim_parent_class)->finalize (object);
} }
@ -387,7 +362,7 @@ gst_net_sim_dispose (GObject * object)
{ {
GstNetSim *netsim = GST_NET_SIM (object); GstNetSim *netsim = GST_NET_SIM (object);
g_assert (netsim->priv->main_loop == NULL); g_assert (netsim->main_loop == NULL);
G_OBJECT_CLASS (gst_net_sim_parent_class)->dispose (object); G_OBJECT_CLASS (gst_net_sim_parent_class)->dispose (object);
} }
@ -398,8 +373,6 @@ gst_net_sim_class_init (GstNetSimClass * klass)
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass); GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
g_type_class_add_private (klass, sizeof (GstNetSimPrivate));
gst_element_class_add_static_pad_template (gstelement_class, gst_element_class_add_static_pad_template (gstelement_class,
&gst_net_sim_src_template); &gst_net_sim_src_template);
gst_element_class_add_static_pad_template (gstelement_class, gst_element_class_add_static_pad_template (gstelement_class,

View file

@ -45,24 +45,31 @@ G_BEGIN_DECLS
typedef struct _GstNetSim GstNetSim; typedef struct _GstNetSim GstNetSim;
typedef struct _GstNetSimClass GstNetSimClass; typedef struct _GstNetSimClass GstNetSimClass;
typedef struct _GstNetSimPrivate GstNetSimPrivate;
struct _GstNetSim struct _GstNetSim
{ {
GstElement parent; GstElement parent;
GstNetSimPrivate *priv; GstPad *sinkpad;
GstPad *srcpad;
/*< private > */ GMutex loop_mutex;
gpointer _gst_reserved[GST_PADDING]; GCond start_cond;
GMainLoop *main_loop;
gboolean running;
GRand *rand_seed;
gint min_delay;
gint max_delay;
gfloat delay_probability;
gfloat drop_probability;
gfloat duplicate_probability;
guint drop_packets;
}; };
struct _GstNetSimClass struct _GstNetSimClass
{ {
GstElementClass parent_class; GstElementClass parent_class;
/*< private > */
gpointer _gst_reserved[GST_PADDING];
}; };
GType gst_net_sim_get_type (void); GType gst_net_sim_get_type (void);