funnel: Re-push all sticky events when buffers come from a different pad

Don't special case segment/caps, just push all sticky events when they are
received on the currently active pad or when the active pad changes.
This commit is contained in:
Olivier Crête 2013-07-01 20:35:21 -04:00
parent 176e16aa5f
commit bbb26f8756
3 changed files with 42 additions and 211 deletions

View file

@ -45,56 +45,6 @@
GST_DEBUG_CATEGORY_STATIC (gst_funnel_debug);
#define GST_CAT_DEFAULT gst_funnel_debug
GType gst_funnel_pad_get_type (void);
#define GST_TYPE_FUNNEL_PAD \
(gst_funnel_pad_get_type())
#define GST_FUNNEL_PAD(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_FUNNEL_PAD, GstFunnelPad))
#define GST_FUNNEL_PAD_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_FUNNEL_PAD, GstFunnelPadClass))
#define GST_IS_FUNNEL_PAD(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_FUNNEL_PAD))
#define GST_IS_FUNNEL_PAD_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_FUNNEL_PAD))
#define GST_FUNNEL_PAD_CAST(obj) \
((GstFunnelPad *)(obj))
typedef struct _GstFunnelPad GstFunnelPad;
typedef struct _GstFunnelPadClass GstFunnelPadClass;
struct _GstFunnelPad
{
GstPad parent;
GstSegment segment;
gboolean got_eos;
};
struct _GstFunnelPadClass
{
GstPadClass parent;
};
G_DEFINE_TYPE (GstFunnelPad, gst_funnel_pad, GST_TYPE_PAD);
static void
gst_funnel_pad_class_init (GstFunnelPadClass * klass)
{
}
static void
gst_funnel_pad_reset (GstFunnelPad * pad)
{
gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
pad->got_eos = FALSE;
}
static void
gst_funnel_pad_init (GstFunnelPad * pad)
{
gst_funnel_pad_reset (pad);
}
static GstStaticPadTemplate funnel_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink_%u",
GST_PAD_SINK,
@ -112,8 +62,6 @@ GST_STATIC_PAD_TEMPLATE ("src",
#define gst_funnel_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstFunnel, gst_funnel, GST_TYPE_ELEMENT, _do_init);
static GstStateChangeReturn gst_funnel_change_state (GstElement * element,
GstStateChange transition);
static GstPad *gst_funnel_request_new_pad (GstElement * element,
GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
static void gst_funnel_release_pad (GstElement * element, GstPad * pad);
@ -122,17 +70,15 @@ static GstFlowReturn gst_funnel_sink_chain (GstPad * pad, GstObject * parent,
GstBuffer * buffer);
static gboolean gst_funnel_sink_event (GstPad * pad, GstObject * parent,
GstEvent * event);
static gboolean gst_funnel_sink_query (GstPad * pad, GstObject * parent,
GstQuery * query);
static gboolean gst_funnel_src_event (GstPad * pad, GstObject * parent,
GstEvent * event);
static void
gst_funnel_dispose (GObject * object)
{
GstFunnel *funnel = GST_FUNNEL (object);
GList *item;
gst_object_replace ((GstObject **) & funnel->last_sinkpad, NULL);
restart:
for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
GstPad *pad = GST_PAD (item->data);
@ -166,7 +112,6 @@ gst_funnel_class_init (GstFunnelClass * klass)
gstelement_class->request_new_pad =
GST_DEBUG_FUNCPTR (gst_funnel_request_new_pad);
gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_funnel_release_pad);
gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_funnel_change_state);
}
static void
@ -174,7 +119,6 @@ gst_funnel_init (GstFunnel * funnel)
{
funnel->srcpad = gst_pad_new_from_static_template (&funnel_src_template,
"src");
gst_pad_set_event_function (funnel->srcpad, gst_funnel_src_event);
gst_pad_use_fixed_caps (funnel->srcpad);
gst_element_add_pad (GST_ELEMENT (funnel), funnel->srcpad);
}
@ -187,16 +131,12 @@ gst_funnel_request_new_pad (GstElement * element, GstPadTemplate * templ,
GST_DEBUG_OBJECT (element, "requesting pad");
sinkpad = GST_PAD_CAST (g_object_new (GST_TYPE_FUNNEL_PAD,
"name", name, "direction", templ->direction, "template", templ,
NULL));
sinkpad = gst_pad_new_from_static_template (&funnel_sink_template, name);
gst_pad_set_chain_function (sinkpad,
GST_DEBUG_FUNCPTR (gst_funnel_sink_chain));
gst_pad_set_event_function (sinkpad,
GST_DEBUG_FUNCPTR (gst_funnel_sink_event));
gst_pad_set_query_function (sinkpad,
GST_DEBUG_FUNCPTR (gst_funnel_sink_query));
gst_pad_set_active (sinkpad, TRUE);
@ -215,9 +155,14 @@ gst_funnel_all_sinkpads_eos_unlocked (GstFunnel * funnel)
return FALSE;
for (item = element->sinkpads; item != NULL; item = g_list_next (item)) {
GstFunnelPad *sinkpad = item->data;
GstPad *sinkpad = item->data;
GstEvent *eos;
if (!sinkpad->got_eos)
eos = gst_pad_get_sticky_event (sinkpad, GST_EVENT_EOS, 0);
if (eos)
gst_event_unref (eos);
if (eos == NULL)
return FALSE;
}
@ -228,20 +173,21 @@ static void
gst_funnel_release_pad (GstElement * element, GstPad * pad)
{
GstFunnel *funnel = GST_FUNNEL (element);
GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
gboolean got_eos;
GstEvent *eos;
gboolean send_eos = FALSE;
GST_DEBUG_OBJECT (funnel, "releasing pad");
gst_pad_set_active (pad, FALSE);
got_eos = fpad->got_eos;
eos = gst_pad_get_sticky_event (pad, GST_EVENT_EOS, 0);
if (eos)
gst_event_unref (eos);
gst_element_remove_pad (GST_ELEMENT_CAST (funnel), pad);
GST_OBJECT_LOCK (funnel);
if (!got_eos && gst_funnel_all_sinkpads_eos_unlocked (funnel)) {
if (eos == NULL && gst_funnel_all_sinkpads_eos_unlocked (funnel)) {
GST_DEBUG_OBJECT (funnel, "Pad removed. All others are EOS. Sending EOS");
send_eos = TRUE;
}
@ -252,78 +198,39 @@ gst_funnel_release_pad (GstElement * element, GstPad * pad)
GST_WARNING_OBJECT (funnel, "Failure pushing EOS");
}
static gboolean
forward_events (GstPad * pad, GstEvent ** event, gpointer user_data)
{
GstPad *srcpad = user_data;
if (GST_EVENT_TYPE (*event) != GST_EVENT_EOS)
gst_pad_push_event (srcpad, gst_event_ref (*event));
return TRUE;
}
static GstFlowReturn
gst_funnel_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
{
GstFlowReturn res;
GstFunnel *funnel = GST_FUNNEL (parent);
GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
GstEvent *event = NULL;
GstClockTime newts;
#if 0
GstCaps *padcaps;
#endif
GST_DEBUG_OBJECT (funnel, "received buffer %p", buffer);
GST_OBJECT_LOCK (funnel);
GST_PAD_STREAM_LOCK (funnel->srcpad);
if (fpad->got_eos) {
GST_OBJECT_UNLOCK (funnel);
GST_WARNING_OBJECT (funnel, "Got buffer on pad that received EOS");
res = GST_FLOW_EOS;
gst_buffer_unref (buffer);
goto out;
if (funnel->last_sinkpad != pad) {
gst_object_replace ((GstObject **) & funnel->last_sinkpad,
GST_OBJECT (pad));
gst_pad_sticky_events_foreach (pad, forward_events, funnel->srcpad);
}
if (fpad->segment.format == GST_FORMAT_UNDEFINED) {
GST_WARNING_OBJECT (funnel, "Got buffer without segment,"
" setting segment [0,inf[");
gst_segment_init (&fpad->segment, GST_FORMAT_TIME);
}
if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer)))
fpad->segment.position = GST_BUFFER_TIMESTAMP (buffer);
newts = gst_segment_to_running_time (&fpad->segment,
fpad->segment.format, GST_BUFFER_TIMESTAMP (buffer));
if (newts != GST_BUFFER_TIMESTAMP (buffer)) {
buffer = gst_buffer_make_writable (buffer);
GST_BUFFER_TIMESTAMP (buffer) = newts;
}
if (!funnel->has_segment) {
GstSegment segment;
gst_segment_init (&segment, GST_FORMAT_TIME);
event = gst_event_new_segment (&segment);
funnel->has_segment = TRUE;
}
GST_OBJECT_UNLOCK (funnel);
if (event) {
if (!gst_pad_push_event (funnel->srcpad, event))
GST_WARNING_OBJECT (funnel, "Could not push out newsegment event");
}
#if 0
GST_OBJECT_LOCK (pad);
padcaps = GST_PAD_CAPS (funnel->srcpad);
GST_OBJECT_UNLOCK (pad);
if (GST_BUFFER_CAPS (buffer) && GST_BUFFER_CAPS (buffer) != padcaps) {
if (!gst_pad_set_caps (funnel->srcpad, GST_BUFFER_CAPS (buffer))) {
res = GST_FLOW_NOT_NEGOTIATED;
gst_buffer_unref (buffer);
goto out;
}
}
#endif
res = gst_pad_push (funnel->srcpad, buffer);
GST_LOG_OBJECT (funnel, "handled buffer %s", gst_flow_get_name (res));
GST_PAD_STREAM_UNLOCK (funnel->srcpad);
out:
GST_LOG_OBJECT (funnel, "handled buffer %s", gst_flow_get_name (res));
return res;
}
@ -332,46 +239,16 @@ static gboolean
gst_funnel_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
{
GstFunnel *funnel = GST_FUNNEL (parent);
GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
gboolean forward = TRUE;
gboolean res = TRUE;
gboolean unlock = FALSE;
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEGMENT:
{
GST_OBJECT_LOCK (funnel);
gst_event_copy_segment (event, &fpad->segment);
GST_OBJECT_UNLOCK (funnel);
if (GST_EVENT_IS_STICKY (event)) {
unlock = TRUE;
GST_PAD_STREAM_LOCK (funnel->srcpad);
if (pad != funnel->last_sinkpad)
forward = FALSE;
break;
}
case GST_EVENT_FLUSH_STOP:
{
GST_OBJECT_LOCK (funnel);
gst_segment_init (&fpad->segment, GST_FORMAT_UNDEFINED);
funnel->has_segment = FALSE;
fpad->got_eos = FALSE;
GST_OBJECT_UNLOCK (funnel);
}
break;
case GST_EVENT_EOS:
{
GST_OBJECT_LOCK (funnel);
fpad->got_eos = TRUE;
if (!gst_funnel_all_sinkpads_eos_unlocked (funnel)) {
GST_DEBUG_OBJECT (funnel,
"Got EOS, but not from all sinkpads. Skipping");
forward = FALSE;
} else {
GST_DEBUG_OBJECT (funnel, "Got EOS from all sinkpads. Forwarding");
}
GST_OBJECT_UNLOCK (funnel);
break;
}
default:
break;
}
if (forward)
@ -384,49 +261,3 @@ gst_funnel_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
return res;
}
static void
reset_pad (const GValue * data, gpointer user_data)
{
GstPad *pad = g_value_get_object (data);
GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
GST_OBJECT_LOCK (pad);
gst_funnel_pad_reset (fpad);
GST_OBJECT_UNLOCK (pad);
}
static GstStateChangeReturn
gst_funnel_change_state (GstElement * element, GstStateChange transition)
{
GstFunnel *funnel = GST_FUNNEL (element);
GstStateChangeReturn ret;
switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:
{
GstIterator *iter = gst_element_iterate_sink_pads (element);
GstIteratorResult res;
do {
res = gst_iterator_foreach (iter, reset_pad, NULL);
} while (res == GST_ITERATOR_RESYNC);
gst_iterator_free (iter);
if (res == GST_ITERATOR_ERROR)
return GST_STATE_CHANGE_FAILURE;
GST_OBJECT_LOCK (funnel);
funnel->has_segment = FALSE;
GST_OBJECT_UNLOCK (funnel);
}
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
return ret;
}

View file

@ -55,7 +55,7 @@ struct _GstFunnel {
/*< private >*/
GstPad *srcpad;
gboolean has_segment;
GstPad *last_sinkpad;
};
struct _GstFunnelClass {

View file

@ -204,7 +204,7 @@ GST_START_TEST (test_funnel_eos)
td.funnelsink11)));
/* This will fail because everything is EOS already */
fail_if (gst_pad_push_event (td.mysrc1, gst_event_new_eos ()));
fail_unless (gst_pad_push_event (td.mysrc1, gst_event_new_eos ()));
fail_unless (num_eos == 2);
fail_unless (gst_pad_unlink (td.mysrc1, td.funnelsink11));