de2bbf8cba1892ba5c4f55d8d65b0d5b82f4b51a
[platform/upstream/gstreamer.git] / gst / autoconvert / gstautoconvert.c
1 /* GStreamer
2  *
3  *  Copyright 2007-2012 Collabora Ltd
4  *   @author: Olivier Crete <olivier.crete@collabora.com>
5  *  Copyright 2007-2008 Nokia
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 /**
23  * SECTION:element-autoconvert
24  * @title: autoconvert
25  *
26  * The #autoconvert element has one sink and one source pad. It will look for
27  * other elements that also have one sink and one source pad.
28  * It will then pick an element that matches the caps on both sides.
29  * If the caps change, it may change the selected element if the current one
30  * no longer matches the caps.
31  *
32  * The list of element it will look into can be specified in the
33  * #GstAutoConvert:factories property, otherwise it will look at all available
34  * elements.
35  */
36
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include "gstautoconvert.h"
43
44 #include <string.h>
45
46 GST_DEBUG_CATEGORY (autoconvert_debug);
47 #define GST_CAT_DEFAULT (autoconvert_debug)
48
49 #define GST_AUTOCONVERT_LOCK(ac) GST_OBJECT_LOCK (ac)
50 #define GST_AUTOCONVERT_UNLOCK(ac) GST_OBJECT_UNLOCK (ac)
51
52 /* elementfactory information */
53 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS_ANY);
57
58 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
59     GST_PAD_SRC,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS_ANY);
62
63 static GstStaticPadTemplate sink_internal_template =
64 GST_STATIC_PAD_TEMPLATE ("sink_internal",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS_ANY);
68
69 static GstStaticPadTemplate src_internal_template =
70 GST_STATIC_PAD_TEMPLATE ("src_internal",
71     GST_PAD_SRC,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS_ANY);
74
75 /* GstAutoConvert signals and args */
76 enum
77 {
78   /* FILL ME */
79   LAST_SIGNAL
80 };
81
82 enum
83 {
84   PROP_0,
85   PROP_FACTORIES
86 };
87
88 static void gst_auto_convert_set_property (GObject * object,
89     guint prop_id, const GValue * value, GParamSpec * pspec);
90 static void gst_auto_convert_get_property (GObject * object,
91     guint prop_id, GValue * value, GParamSpec * pspec);
92 static void gst_auto_convert_dispose (GObject * object);
93
94 static GstElement *gst_auto_convert_get_subelement (GstAutoConvert *
95     autoconvert);
96 static GstPad *gst_auto_convert_get_internal_sinkpad (GstAutoConvert *
97     autoconvert);
98 static GstPad *gst_auto_convert_get_internal_srcpad (GstAutoConvert *
99     autoconvert);
100
101 static GstIterator *gst_auto_convert_iterate_internal_links (GstPad * pad,
102     GstObject * parent);
103
104 static gboolean gst_auto_convert_sink_setcaps (GstAutoConvert * autoconvert,
105     GstCaps * caps);
106 static GstCaps *gst_auto_convert_getcaps (GstAutoConvert * autoconvert,
107     GstCaps * filter, GstPadDirection dir);
108 static GstFlowReturn gst_auto_convert_sink_chain (GstPad * pad,
109     GstObject * parent, GstBuffer * buffer);
110 static GstFlowReturn gst_auto_convert_sink_chain_list (GstPad * pad,
111     GstObject * parent, GstBufferList * list);
112 static gboolean gst_auto_convert_sink_event (GstPad * pad, GstObject * parent,
113     GstEvent * event);
114 static gboolean gst_auto_convert_sink_query (GstPad * pad, GstObject * parent,
115     GstQuery * query);
116
117 static gboolean gst_auto_convert_src_event (GstPad * pad, GstObject * parent,
118     GstEvent * event);
119 static gboolean gst_auto_convert_src_query (GstPad * pad, GstObject * parent,
120     GstQuery * query);
121
122 static GstFlowReturn gst_auto_convert_internal_sink_chain (GstPad * pad,
123     GstObject * parent, GstBuffer * buffer);
124 static GstFlowReturn gst_auto_convert_internal_sink_chain_list (GstPad * pad,
125     GstObject * parent, GstBufferList * list);
126 static gboolean gst_auto_convert_internal_sink_event (GstPad * pad,
127     GstObject * parent, GstEvent * event);
128 static gboolean gst_auto_convert_internal_sink_query (GstPad * pad,
129     GstObject * parent, GstQuery * query);
130
131 static gboolean gst_auto_convert_internal_src_event (GstPad * pad,
132     GstObject * parent, GstEvent * event);
133 static gboolean gst_auto_convert_internal_src_query (GstPad * pad,
134     GstObject * parent, GstQuery * query);
135
136 static GList *gst_auto_convert_load_factories (GstAutoConvert * autoconvert);
137 static GstElement
138     * gst_auto_convert_get_or_make_element_from_factory (GstAutoConvert *
139     autoconvert, GstElementFactory * factory);
140 static gboolean gst_auto_convert_activate_element (GstAutoConvert * autoconvert,
141     GstElement * element, GstCaps * caps);
142
143 static GQuark internal_srcpad_quark = 0;
144 static GQuark internal_sinkpad_quark = 0;
145 static GQuark parent_quark = 0;
146
147 G_DEFINE_TYPE (GstAutoConvert, gst_auto_convert, GST_TYPE_BIN);
148
149 static void
150 gst_auto_convert_class_init (GstAutoConvertClass * klass)
151 {
152   GObjectClass *gobject_class = (GObjectClass *) klass;
153   GstElementClass *gstelement_class = (GstElementClass *) klass;
154
155   GST_DEBUG_CATEGORY_INIT (autoconvert_debug, "autoconvert", 0,
156       "Auto convert based on caps");
157
158   internal_srcpad_quark = g_quark_from_static_string ("internal_srcpad");
159   internal_sinkpad_quark = g_quark_from_static_string ("internal_sinkpad");
160   parent_quark = g_quark_from_static_string ("parent");
161
162
163   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
164   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
165
166   gst_element_class_set_static_metadata (gstelement_class,
167       "Select convertor based on caps", "Generic/Bin",
168       "Selects the right transform element based on the caps",
169       "Olivier Crete <olivier.crete@collabora.com>");
170
171   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_auto_convert_dispose);
172
173   gobject_class->set_property = gst_auto_convert_set_property;
174   gobject_class->get_property = gst_auto_convert_get_property;
175
176   g_object_class_install_property (gobject_class, PROP_FACTORIES,
177       g_param_spec_pointer ("factories",
178           "GList of GstElementFactory",
179           "GList of GstElementFactory objects to pick from (the element takes"
180           " ownership of the list (NULL means it will go through all possible"
181           " elements), can only be set once",
182           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 }
184
185 static void
186 gst_auto_convert_init (GstAutoConvert * autoconvert)
187 {
188   autoconvert->sinkpad =
189       gst_pad_new_from_static_template (&sinktemplate, "sink");
190   autoconvert->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
191
192   gst_pad_set_chain_function (autoconvert->sinkpad,
193       GST_DEBUG_FUNCPTR (gst_auto_convert_sink_chain));
194   gst_pad_set_chain_list_function (autoconvert->sinkpad,
195       GST_DEBUG_FUNCPTR (gst_auto_convert_sink_chain_list));
196   gst_pad_set_event_function (autoconvert->sinkpad,
197       GST_DEBUG_FUNCPTR (gst_auto_convert_sink_event));
198   gst_pad_set_query_function (autoconvert->sinkpad,
199       GST_DEBUG_FUNCPTR (gst_auto_convert_sink_query));
200   gst_pad_set_iterate_internal_links_function (autoconvert->sinkpad,
201       GST_DEBUG_FUNCPTR (gst_auto_convert_iterate_internal_links));
202
203   gst_pad_set_event_function (autoconvert->srcpad,
204       GST_DEBUG_FUNCPTR (gst_auto_convert_src_event));
205   gst_pad_set_query_function (autoconvert->srcpad,
206       GST_DEBUG_FUNCPTR (gst_auto_convert_src_query));
207   gst_pad_set_iterate_internal_links_function (autoconvert->sinkpad,
208       GST_DEBUG_FUNCPTR (gst_auto_convert_iterate_internal_links));
209
210   gst_element_add_pad (GST_ELEMENT (autoconvert), autoconvert->sinkpad);
211   gst_element_add_pad (GST_ELEMENT (autoconvert), autoconvert->srcpad);
212 }
213
214 static void
215 gst_auto_convert_dispose (GObject * object)
216 {
217   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (object);
218
219   g_clear_object (&autoconvert->current_subelement);
220   g_clear_object (&autoconvert->current_internal_sinkpad);
221   g_clear_object (&autoconvert->current_internal_srcpad);
222
223   for (;;) {
224     GList *factories = g_atomic_pointer_get (&autoconvert->factories);
225
226     if (g_atomic_pointer_compare_and_exchange (&autoconvert->factories,
227             factories, NULL)) {
228       gst_plugin_feature_list_free (factories);
229       break;
230     }
231   }
232
233   G_OBJECT_CLASS (gst_auto_convert_parent_class)->dispose (object);
234 }
235
236 static void
237 gst_auto_convert_set_property (GObject * object,
238     guint prop_id, const GValue * value, GParamSpec * pspec)
239 {
240   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (object);
241
242   switch (prop_id) {
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246     case PROP_FACTORIES:
247       if (g_atomic_pointer_get (&autoconvert->factories) == NULL) {
248         GList *factories = g_value_get_pointer (value);
249         factories = g_list_copy (factories);
250         if (g_atomic_pointer_compare_and_exchange (&autoconvert->factories,
251                 NULL, factories))
252           g_list_foreach (factories, (GFunc) g_object_ref, NULL);
253         else
254           g_list_free (factories);
255       } else {
256         GST_WARNING_OBJECT (object, "Can not reset factories after they"
257             " have been set or auto-discovered");
258       }
259       break;
260   }
261 }
262
263 static void
264 gst_auto_convert_get_property (GObject * object,
265     guint prop_id, GValue * value, GParamSpec * pspec)
266 {
267   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (object);
268
269   switch (prop_id) {
270     default:
271       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272       break;
273     case PROP_FACTORIES:
274       g_value_set_pointer (value,
275           g_atomic_pointer_get (&autoconvert->factories));
276       break;
277   }
278 }
279
280
281 static GstElement *
282 gst_auto_convert_get_element_by_type (GstAutoConvert * autoconvert, GType type)
283 {
284   GList *item;
285   GstBin *bin = GST_BIN (autoconvert);
286   GstElement *element = NULL;
287
288   g_return_val_if_fail (type != 0, NULL);
289
290   GST_OBJECT_LOCK (autoconvert);
291
292   for (item = bin->children; item; item = item->next) {
293     if (G_TYPE_CHECK_INSTANCE_TYPE (item->data, type)) {
294       element = gst_object_ref (item->data);
295       break;
296     }
297   }
298
299   GST_OBJECT_UNLOCK (autoconvert);
300
301   return element;
302 }
303
304 /**
305  * get_pad_by_direction:
306  * @element: The Element
307  * @direction: The direction
308  *
309  * Gets a #GstPad that goes in the requested direction. I will return NULL
310  * if there is no pad or if there is more than one pad in this direction
311  */
312
313 static GstPad *
314 get_pad_by_direction (GstElement * element, GstPadDirection direction)
315 {
316   GstIterator *iter = gst_element_iterate_pads (element);
317   GstPad *selected_pad = NULL;
318   gboolean done;
319   GValue item = { 0, };
320
321   if (!iter)
322     return NULL;
323
324   done = FALSE;
325   while (!done) {
326     switch (gst_iterator_next (iter, &item)) {
327       case GST_ITERATOR_OK:
328       {
329         GstPad *pad = g_value_get_object (&item);
330
331         if (gst_pad_get_direction (pad) == direction) {
332           /* We check if there is more than one pad in this direction,
333            * if there is, we return NULL so that the element is refused
334            */
335           if (selected_pad) {
336             done = TRUE;
337             gst_object_unref (selected_pad);
338             selected_pad = NULL;
339           } else {
340             selected_pad = g_object_ref (pad);
341           }
342         }
343         g_value_unset (&item);
344       }
345         break;
346       case GST_ITERATOR_RESYNC:
347         if (selected_pad) {
348           gst_object_unref (selected_pad);
349           selected_pad = NULL;
350         }
351         gst_iterator_resync (iter);
352         break;
353       case GST_ITERATOR_ERROR:
354         GST_ERROR ("Error iterating pads of element %s",
355             GST_OBJECT_NAME (element));
356         gst_object_unref (selected_pad);
357         selected_pad = NULL;
358         done = TRUE;
359         break;
360       case GST_ITERATOR_DONE:
361         done = TRUE;
362         break;
363     }
364   }
365   g_value_unset (&item);
366   gst_iterator_free (iter);
367
368   if (!selected_pad)
369     GST_ERROR ("Did not find pad of direction %d in %s",
370         direction, GST_OBJECT_NAME (element));
371
372   return selected_pad;
373 }
374
375 static GstElement *
376 gst_auto_convert_get_subelement (GstAutoConvert * autoconvert)
377 {
378   GstElement *element = NULL;
379
380   GST_AUTOCONVERT_LOCK (autoconvert);
381   if (autoconvert->current_subelement)
382     element = gst_object_ref (autoconvert->current_subelement);
383   GST_AUTOCONVERT_UNLOCK (autoconvert);
384
385   return element;
386 }
387
388 static GstPad *
389 gst_auto_convert_get_internal_sinkpad (GstAutoConvert * autoconvert)
390 {
391   GstPad *pad = NULL;
392
393   GST_AUTOCONVERT_LOCK (autoconvert);
394   if (autoconvert->current_internal_sinkpad)
395     pad = gst_object_ref (autoconvert->current_internal_sinkpad);
396   GST_AUTOCONVERT_UNLOCK (autoconvert);
397
398   return pad;
399 }
400
401 static GstPad *
402 gst_auto_convert_get_internal_srcpad (GstAutoConvert * autoconvert)
403 {
404   GstPad *pad = NULL;
405
406   GST_AUTOCONVERT_LOCK (autoconvert);
407   if (autoconvert->current_internal_srcpad)
408     pad = gst_object_ref (autoconvert->current_internal_srcpad);
409   GST_AUTOCONVERT_UNLOCK (autoconvert);
410
411   return pad;
412 }
413
414 /*
415  * This function creates and adds an element to the GstAutoConvert
416  * it then creates the internal pads and links them
417  *
418  */
419
420 static GstElement *
421 gst_auto_convert_add_element (GstAutoConvert * autoconvert,
422     GstElementFactory * factory)
423 {
424   GstElement *element = NULL;
425   GstPad *internal_sinkpad = NULL;
426   GstPad *internal_srcpad = NULL;
427   GstPad *sinkpad = NULL;
428   GstPad *srcpad = NULL;
429   GstPadLinkReturn padlinkret;
430
431   GST_DEBUG_OBJECT (autoconvert, "Adding element %s to the autoconvert bin",
432       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
433
434   element = gst_element_factory_create (factory, NULL);
435   if (!element)
436     return NULL;
437
438   if (!gst_bin_add (GST_BIN (autoconvert), element)) {
439     GST_ERROR_OBJECT (autoconvert, "Could not add element %s to the bin",
440         GST_OBJECT_NAME (element));
441     gst_object_unref (element);
442     return NULL;
443   }
444
445   srcpad = get_pad_by_direction (element, GST_PAD_SRC);
446   if (!srcpad) {
447     GST_ERROR_OBJECT (autoconvert, "Could not find source in %s",
448         GST_OBJECT_NAME (element));
449     goto error;
450   }
451
452   sinkpad = get_pad_by_direction (element, GST_PAD_SINK);
453   if (!sinkpad) {
454     GST_ERROR_OBJECT (autoconvert, "Could not find sink in %s",
455         GST_OBJECT_NAME (element));
456     goto error;
457   }
458
459   internal_sinkpad =
460       gst_pad_new_from_static_template (&sink_internal_template,
461       "sink_internal");
462   internal_srcpad =
463       gst_pad_new_from_static_template (&src_internal_template, "src_internal");
464
465   if (!internal_sinkpad || !internal_srcpad) {
466     GST_ERROR_OBJECT (autoconvert, "Could not create internal pads");
467     if (internal_srcpad)
468       gst_object_unref (internal_srcpad);
469     if (internal_sinkpad)
470       gst_object_unref (internal_sinkpad);
471     goto error;
472   }
473
474   g_object_weak_ref (G_OBJECT (element), (GWeakNotify) gst_object_unref,
475       internal_sinkpad);
476   g_object_weak_ref (G_OBJECT (element), (GWeakNotify) gst_object_unref,
477       internal_srcpad);
478
479   gst_pad_set_active (internal_sinkpad, TRUE);
480   gst_pad_set_active (internal_srcpad, TRUE);
481
482   g_object_set_qdata (G_OBJECT (internal_srcpad), parent_quark, autoconvert);
483   g_object_set_qdata (G_OBJECT (internal_sinkpad), parent_quark, autoconvert);
484
485   gst_pad_set_chain_function (internal_sinkpad,
486       GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_chain));
487   gst_pad_set_chain_list_function (internal_sinkpad,
488       GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_chain_list));
489   gst_pad_set_event_function (internal_sinkpad,
490       GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_event));
491   gst_pad_set_query_function (internal_sinkpad,
492       GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_query));
493
494   gst_pad_set_event_function (internal_srcpad,
495       GST_DEBUG_FUNCPTR (gst_auto_convert_internal_src_event));
496   gst_pad_set_query_function (internal_srcpad,
497       GST_DEBUG_FUNCPTR (gst_auto_convert_internal_src_query));
498
499   padlinkret = gst_pad_link_full (internal_srcpad, sinkpad,
500       GST_PAD_LINK_CHECK_NOTHING);
501   if (GST_PAD_LINK_FAILED (padlinkret)) {
502     GST_WARNING_OBJECT (autoconvert, "Could not links pad %s:%s to %s:%s"
503         " for reason %d",
504         GST_DEBUG_PAD_NAME (internal_srcpad),
505         GST_DEBUG_PAD_NAME (sinkpad), padlinkret);
506     goto error;
507   }
508
509   padlinkret = gst_pad_link_full (srcpad, internal_sinkpad,
510       GST_PAD_LINK_CHECK_NOTHING);
511   if (GST_PAD_LINK_FAILED (padlinkret)) {
512     GST_WARNING_OBJECT (autoconvert, "Could not links pad %s:%s to %s:%s"
513         " for reason %d",
514         GST_DEBUG_PAD_NAME (internal_srcpad),
515         GST_DEBUG_PAD_NAME (sinkpad), padlinkret);
516     goto error;
517   }
518
519   g_object_set_qdata (G_OBJECT (element),
520       internal_srcpad_quark, internal_srcpad);
521   g_object_set_qdata (G_OBJECT (element),
522       internal_sinkpad_quark, internal_sinkpad);
523
524   /* Iffy */
525   gst_element_sync_state_with_parent (element);
526
527   /* Increment the reference count we will return to the caller */
528   gst_object_ref (element);
529
530   /* unref sink and src pad */
531   gst_object_unref (srcpad);
532   gst_object_unref (sinkpad);
533   return element;
534
535 error:
536   gst_element_set_locked_state (element, TRUE);
537   gst_element_set_state (element, GST_STATE_NULL);
538   gst_bin_remove (GST_BIN (autoconvert), element);
539
540   if (srcpad)
541     gst_object_unref (srcpad);
542   if (sinkpad)
543     gst_object_unref (sinkpad);
544
545   return NULL;
546 }
547
548 static GstElement *
549 gst_auto_convert_get_or_make_element_from_factory (GstAutoConvert * autoconvert,
550     GstElementFactory * factory)
551 {
552   GstElement *element = NULL;
553   GstElementFactory *loaded_factory =
554       GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
555           (factory)));
556
557   if (!loaded_factory)
558     return NULL;
559
560   element = gst_auto_convert_get_element_by_type (autoconvert,
561       gst_element_factory_get_element_type (loaded_factory));
562
563   if (!element) {
564     element = gst_auto_convert_add_element (autoconvert, loaded_factory);
565   }
566
567   gst_object_unref (loaded_factory);
568
569   return element;
570 }
571
572 /*
573  * This function checks if there is one and only one pad template on the
574  * factory that can accept the given caps. If there is one and only one,
575  * it returns TRUE, otherwise, its FALSE
576  */
577
578 static gboolean
579 factory_can_intersect (GstAutoConvert * autoconvert,
580     GstElementFactory * factory, GstPadDirection direction, GstCaps * caps)
581 {
582   const GList *templates;
583   gint has_direction = FALSE;
584   gboolean ret = FALSE;
585
586   g_return_val_if_fail (factory != NULL, FALSE);
587   g_return_val_if_fail (caps != NULL, FALSE);
588
589   templates = gst_element_factory_get_static_pad_templates (factory);
590
591   while (templates) {
592     GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
593
594     if (template->direction == direction) {
595       GstCaps *tmpl_caps = NULL;
596       gboolean intersect;
597
598       /* If there is more than one pad in this direction, we return FALSE
599        * Only transform elements (with one sink and one source pad)
600        * are accepted
601        */
602       if (has_direction) {
603         GST_DEBUG_OBJECT (autoconvert, "Factory %p"
604             " has more than one static template with dir %d",
605             template, direction);
606         return FALSE;
607       }
608       has_direction = TRUE;
609
610       tmpl_caps = gst_static_caps_get (&template->static_caps);
611       intersect = gst_caps_can_intersect (tmpl_caps, caps);
612       GST_DEBUG_OBJECT (autoconvert, "Factories %" GST_PTR_FORMAT
613           " static caps %" GST_PTR_FORMAT " and caps %" GST_PTR_FORMAT
614           " can%s intersect", factory, tmpl_caps, caps,
615           intersect ? "" : " not");
616       gst_caps_unref (tmpl_caps);
617
618       ret |= intersect;
619     }
620     templates = g_list_next (templates);
621   }
622
623   return ret;
624 }
625
626 static gboolean
627 sticky_event_push (GstPad * pad, GstEvent ** event, gpointer user_data)
628 {
629   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (user_data);
630
631   gst_event_ref (*event);
632   gst_pad_push_event (autoconvert->current_internal_srcpad, *event);
633
634   return TRUE;
635 }
636
637 static gboolean
638 gst_auto_convert_activate_element (GstAutoConvert * autoconvert,
639     GstElement * element, GstCaps * caps)
640 {
641   GstPad *internal_srcpad = g_object_get_qdata (G_OBJECT (element),
642       internal_srcpad_quark);
643   GstPad *internal_sinkpad = g_object_get_qdata (G_OBJECT (element),
644       internal_sinkpad_quark);
645
646   if (caps) {
647     /* check if the element can really accept said caps */
648     if (!gst_pad_peer_query_accept_caps (internal_srcpad, caps)) {
649       GST_DEBUG_OBJECT (autoconvert, "Could not set %s:%s to %"
650           GST_PTR_FORMAT, GST_DEBUG_PAD_NAME (internal_srcpad), caps);
651       return FALSE;
652     }
653   }
654
655   GST_AUTOCONVERT_LOCK (autoconvert);
656   gst_object_replace ((GstObject **) & autoconvert->current_subelement,
657       GST_OBJECT (element));
658   gst_object_replace ((GstObject **) & autoconvert->current_internal_srcpad,
659       GST_OBJECT (internal_srcpad));
660   gst_object_replace ((GstObject **) & autoconvert->current_internal_sinkpad,
661       GST_OBJECT (internal_sinkpad));
662   GST_AUTOCONVERT_UNLOCK (autoconvert);
663
664   gst_pad_sticky_events_foreach (autoconvert->sinkpad, sticky_event_push,
665       autoconvert);
666
667   gst_pad_push_event (autoconvert->sinkpad, gst_event_new_reconfigure ());
668
669   GST_INFO_OBJECT (autoconvert, "Selected element %s",
670       GST_OBJECT_NAME (GST_OBJECT (element)));
671
672   gst_object_unref (element);
673
674   return TRUE;
675 }
676
677 static GstIterator *
678 gst_auto_convert_iterate_internal_links (GstPad * pad, GstObject * parent)
679 {
680   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
681   GstIterator *it = NULL;
682   GstPad *internal;
683
684   if (pad == autoconvert->sinkpad)
685     internal = gst_auto_convert_get_internal_srcpad (autoconvert);
686   else
687     internal = gst_auto_convert_get_internal_sinkpad (autoconvert);
688
689   if (internal) {
690     GValue val = { 0, };
691
692     g_value_init (&val, GST_TYPE_PAD);
693     g_value_take_object (&val, internal);
694
695     it = gst_iterator_new_single (GST_TYPE_PAD, &val);
696     g_value_unset (&val);
697   }
698
699   return it;
700 }
701
702 /*
703  * If there is already an internal element, it will try to call set_caps on it
704  *
705  * If there isn't an internal element or if the set_caps() on the internal
706  * element failed, it will try to find another element where it would succeed
707  * and will change the internal element.
708  */
709
710 static gboolean
711 gst_auto_convert_sink_setcaps (GstAutoConvert * autoconvert, GstCaps * caps)
712 {
713   GList *elem;
714   GstCaps *other_caps = NULL;
715   GList *factories;
716   GstCaps *current_caps;
717
718   g_return_val_if_fail (autoconvert != NULL, FALSE);
719
720   current_caps = gst_pad_get_current_caps (autoconvert->sinkpad);
721   if (current_caps) {
722     if (gst_caps_is_equal_fixed (caps, current_caps)) {
723       gst_caps_unref (current_caps);
724       return TRUE;
725     }
726     gst_caps_unref (current_caps);
727   }
728
729   if (autoconvert->current_subelement) {
730     if (gst_pad_peer_query_accept_caps (autoconvert->current_internal_srcpad,
731             caps)) {
732       /* If we can set the new caps on the current element,
733        * then we just get out
734        */
735       GST_DEBUG_OBJECT (autoconvert, "Could set %s:%s to %" GST_PTR_FORMAT,
736           GST_DEBUG_PAD_NAME (autoconvert->current_internal_srcpad), caps);
737       goto get_out;
738     } else {
739       /* If the current element doesn't work,
740        * then we remove the current element before finding a new one.
741        */
742       GST_AUTOCONVERT_LOCK (autoconvert);
743       g_clear_object (&autoconvert->current_subelement);
744       g_clear_object (&autoconvert->current_internal_sinkpad);
745       g_clear_object (&autoconvert->current_internal_srcpad);
746       GST_AUTOCONVERT_UNLOCK (autoconvert);
747     }
748   }
749
750   other_caps = gst_pad_peer_query_caps (autoconvert->srcpad, NULL);
751
752   factories = g_atomic_pointer_get (&autoconvert->factories);
753
754   if (!factories)
755     factories = gst_auto_convert_load_factories (autoconvert);
756
757   for (elem = factories; elem; elem = g_list_next (elem)) {
758     GstElementFactory *factory = GST_ELEMENT_FACTORY (elem->data);
759     GstElement *element;
760
761     /* Lets first check if according to the static pad templates on the factory
762      * these caps have any chance of success
763      */
764     if (!factory_can_intersect (autoconvert, factory, GST_PAD_SINK, caps)) {
765       GST_LOG_OBJECT (autoconvert, "Factory %s does not accept sink caps %"
766           GST_PTR_FORMAT,
767           gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)), caps);
768       continue;
769     }
770     if (other_caps != NULL) {
771       if (!factory_can_intersect (autoconvert, factory, GST_PAD_SRC,
772               other_caps)) {
773         GST_LOG_OBJECT (autoconvert,
774             "Factory %s does not accept src caps %" GST_PTR_FORMAT,
775             gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
776             other_caps);
777         continue;
778       }
779     }
780
781     /* The element had a chance of success, lets make it */
782     element =
783         gst_auto_convert_get_or_make_element_from_factory (autoconvert,
784         factory);
785     if (!element)
786       continue;
787
788     /* And make it the current child */
789     if (gst_auto_convert_activate_element (autoconvert, element, caps))
790       break;
791     else
792       gst_object_unref (element);
793   }
794
795 get_out:
796   if (other_caps)
797     gst_caps_unref (other_caps);
798
799   if (autoconvert->current_subelement) {
800     return TRUE;
801   } else {
802     GST_WARNING_OBJECT (autoconvert,
803         "Could not find a matching element for caps");
804     return FALSE;
805   }
806 }
807
808 /*
809  * This function filters the pad pad templates, taking only transform element
810  * (with one sink and one src pad)
811  */
812
813 static gboolean
814 gst_auto_convert_default_filter_func (GstPluginFeature * feature,
815     gpointer user_data)
816 {
817   GstElementFactory *factory = NULL;
818   const GList *static_pad_templates, *tmp;
819   GstStaticPadTemplate *src = NULL, *sink = NULL;
820
821   if (!GST_IS_ELEMENT_FACTORY (feature))
822     return FALSE;
823
824   factory = GST_ELEMENT_FACTORY (feature);
825
826   static_pad_templates = gst_element_factory_get_static_pad_templates (factory);
827
828   for (tmp = static_pad_templates; tmp; tmp = g_list_next (tmp)) {
829     GstStaticPadTemplate *template = tmp->data;
830     GstCaps *caps;
831
832     if (template->presence == GST_PAD_SOMETIMES)
833       return FALSE;
834
835     if (template->presence != GST_PAD_ALWAYS)
836       continue;
837
838     switch (template->direction) {
839       case GST_PAD_SRC:
840         if (src)
841           return FALSE;
842         src = template;
843         break;
844       case GST_PAD_SINK:
845         if (sink)
846           return FALSE;
847         sink = template;
848         break;
849       default:
850         return FALSE;
851     }
852
853     caps = gst_static_pad_template_get_caps (template);
854
855     if (gst_caps_is_any (caps) || gst_caps_is_empty (caps))
856       return FALSE;
857   }
858
859   if (!src || !sink)
860     return FALSE;
861
862   return TRUE;
863 }
864
865 /* function used to sort element features
866  * Copy-pasted from decodebin */
867 static gint
868 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
869 {
870   gint diff;
871   const gchar *rname1, *rname2;
872
873   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
874   if (diff != 0)
875     return diff;
876
877   rname1 = gst_plugin_feature_get_name (f1);
878   rname2 = gst_plugin_feature_get_name (f2);
879
880   diff = strcmp (rname2, rname1);
881
882   return diff;
883 }
884
885 static GList *
886 gst_auto_convert_load_factories (GstAutoConvert * autoconvert)
887 {
888   GList *all_factories;
889
890   all_factories =
891       gst_registry_feature_filter (gst_registry_get (),
892       gst_auto_convert_default_filter_func, FALSE, NULL);
893
894   all_factories = g_list_sort (all_factories, (GCompareFunc) compare_ranks);
895
896   g_assert (all_factories);
897
898   if (g_atomic_pointer_compare_and_exchange (&autoconvert->factories, NULL,
899           all_factories)) {
900     gst_plugin_feature_list_free (all_factories);
901   }
902
903   return g_atomic_pointer_get (&autoconvert->factories);
904 }
905
906 /* In this case, we should almost always have an internal element, because
907  * set_caps() should have been called first
908  */
909
910 static GstFlowReturn
911 gst_auto_convert_sink_chain (GstPad * pad, GstObject * parent,
912     GstBuffer * buffer)
913 {
914   GstFlowReturn ret = GST_FLOW_NOT_NEGOTIATED;
915   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
916
917   if (autoconvert->current_internal_srcpad) {
918     ret = gst_pad_push (autoconvert->current_internal_srcpad, buffer);
919     if (ret != GST_FLOW_OK)
920       GST_DEBUG_OBJECT (autoconvert,
921           "Child element %" GST_PTR_FORMAT "returned flow %s",
922           autoconvert->current_subelement, gst_flow_get_name (ret));
923   } else {
924     GST_ERROR_OBJECT (autoconvert, "Got buffer without an negotiated element,"
925         " returning not-negotiated");
926     gst_buffer_unref (buffer);
927   }
928
929   return ret;
930 }
931
932 static GstFlowReturn
933 gst_auto_convert_sink_chain_list (GstPad * pad, GstObject * parent,
934     GstBufferList * list)
935 {
936   GstFlowReturn ret = GST_FLOW_NOT_NEGOTIATED;
937   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
938
939   if (autoconvert->current_internal_srcpad) {
940     ret = gst_pad_push_list (autoconvert->current_internal_srcpad, list);
941     if (ret != GST_FLOW_OK)
942       GST_DEBUG_OBJECT (autoconvert,
943           "Child element %" GST_PTR_FORMAT "returned flow %s",
944           autoconvert->current_subelement, gst_flow_get_name (ret));
945   } else {
946     GST_ERROR_OBJECT (autoconvert, "Got buffer without an negotiated element,"
947         " returning not-negotiated");
948     gst_buffer_list_unref (list);
949   }
950
951   return ret;
952 }
953
954 static gboolean
955 gst_auto_convert_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
956 {
957   gboolean ret = TRUE;
958   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
959   GstPad *internal_srcpad;
960
961   if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
962     GstCaps *caps;
963
964     gst_event_parse_caps (event, &caps);
965     ret = gst_auto_convert_sink_setcaps (autoconvert, caps);
966     if (!ret) {
967       gst_event_unref (event);
968       return ret;
969     }
970   }
971
972   internal_srcpad = gst_auto_convert_get_internal_srcpad (autoconvert);
973   if (internal_srcpad) {
974     ret = gst_pad_push_event (internal_srcpad, event);
975     gst_object_unref (internal_srcpad);
976   } else {
977     switch (GST_EVENT_TYPE (event)) {
978       case GST_EVENT_FLUSH_STOP:
979       case GST_EVENT_FLUSH_START:
980         ret = gst_pad_push_event (autoconvert->srcpad, event);
981         break;
982       default:
983         gst_event_unref (event);
984         ret = TRUE;
985         break;
986     }
987   }
988
989   return ret;
990 }
991
992 /* TODO Properly test that this code works well for queries */
993 static gboolean
994 gst_auto_convert_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
995 {
996   gboolean ret = TRUE;
997   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
998   GstElement *subelement;
999
1000   if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
1001     GstCaps *filter, *caps;
1002
1003     gst_query_parse_caps (query, &filter);
1004     caps = gst_auto_convert_getcaps (autoconvert, filter, GST_PAD_SINK);
1005     gst_query_set_caps_result (query, caps);
1006     gst_caps_unref (caps);
1007
1008     return TRUE;
1009   }
1010
1011   subelement = gst_auto_convert_get_subelement (autoconvert);
1012   if (subelement) {
1013     GstPad *sub_sinkpad = get_pad_by_direction (subelement, GST_PAD_SINK);
1014
1015     ret = gst_pad_query (sub_sinkpad, query);
1016
1017     gst_object_unref (sub_sinkpad);
1018     gst_object_unref (subelement);
1019
1020     if (ret && GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS) {
1021       gboolean res;
1022       gst_query_parse_accept_caps_result (query, &res);
1023
1024       if (!res)
1025         goto ignore_acceptcaps_failure;
1026     }
1027     return ret;
1028   }
1029
1030 ignore_acceptcaps_failure:
1031
1032   if (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS) {
1033     GstCaps *caps;
1034     GstCaps *accept_caps;
1035
1036     gst_query_parse_accept_caps (query, &accept_caps);
1037
1038     caps = gst_auto_convert_getcaps (autoconvert, accept_caps, GST_PAD_SINK);
1039     gst_query_set_accept_caps_result (query,
1040         gst_caps_can_intersect (caps, accept_caps));
1041     gst_caps_unref (caps);
1042
1043     return TRUE;
1044   }
1045
1046   GST_WARNING_OBJECT (autoconvert, "Got query %s while no element was"
1047       " selected, letting through",
1048       gst_query_type_get_name (GST_QUERY_TYPE (query)));
1049   return gst_pad_peer_query (autoconvert->srcpad, query);
1050 }
1051
1052 /**
1053  * gst_auto_convert_getcaps:
1054  * @pad: the sink #GstPad
1055  *
1056  * This function returns the union of the caps of all the possible element
1057  * factories, based on the static pad templates.
1058  * It also checks does a getcaps on the downstream element and ignores all
1059  * factories whose static caps can not satisfy it.
1060  *
1061  * It does not try to use each elements getcaps() function
1062  */
1063
1064 static GstCaps *
1065 gst_auto_convert_getcaps (GstAutoConvert * autoconvert, GstCaps * filter,
1066     GstPadDirection dir)
1067 {
1068   GstCaps *caps = NULL, *other_caps = NULL;
1069   GList *elem, *factories;
1070
1071   caps = gst_caps_new_empty ();
1072
1073   if (dir == GST_PAD_SINK)
1074     other_caps = gst_pad_peer_query_caps (autoconvert->srcpad, NULL);
1075   else
1076     other_caps = gst_pad_peer_query_caps (autoconvert->sinkpad, NULL);
1077
1078   GST_DEBUG_OBJECT (autoconvert,
1079       "Lets find all the element that can fit here with src caps %"
1080       GST_PTR_FORMAT, other_caps);
1081
1082   if (other_caps && gst_caps_is_empty (other_caps)) {
1083     goto out;
1084   }
1085
1086   factories = g_atomic_pointer_get (&autoconvert->factories);
1087
1088   if (!factories)
1089     factories = gst_auto_convert_load_factories (autoconvert);
1090
1091   for (elem = factories; elem; elem = g_list_next (elem)) {
1092     GstElementFactory *factory = GST_ELEMENT_FACTORY (elem->data);
1093     GstElement *element = NULL;
1094     GstCaps *element_caps;
1095     GstPad *internal_pad = NULL;
1096
1097     if (filter) {
1098       if (!factory_can_intersect (autoconvert, factory, dir, filter)) {
1099         GST_LOG_OBJECT (autoconvert,
1100             "Factory %s does not accept src caps %" GST_PTR_FORMAT,
1101             gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
1102             other_caps);
1103         continue;
1104       }
1105     }
1106
1107     if (other_caps != NULL) {
1108       if (!factory_can_intersect (autoconvert, factory,
1109               dir == GST_PAD_SINK ? GST_PAD_SRC : GST_PAD_SINK, other_caps)) {
1110         GST_LOG_OBJECT (autoconvert,
1111             "Factory %s does not accept src caps %" GST_PTR_FORMAT,
1112             gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
1113             other_caps);
1114         continue;
1115       }
1116
1117       element = gst_auto_convert_get_or_make_element_from_factory (autoconvert,
1118           factory);
1119       if (element == NULL)
1120         continue;
1121
1122       if (dir == GST_PAD_SINK)
1123         internal_pad = g_object_get_qdata (G_OBJECT (element),
1124             internal_srcpad_quark);
1125       else
1126         internal_pad = g_object_get_qdata (G_OBJECT (element),
1127             internal_sinkpad_quark);
1128
1129       element_caps = gst_pad_peer_query_caps (internal_pad, filter);
1130
1131       if (element_caps)
1132         caps = gst_caps_merge (caps, element_caps);
1133
1134       gst_object_unref (element);
1135
1136       /* Early out, any is absorbing */
1137       if (gst_caps_is_any (caps))
1138         goto out;
1139     } else {
1140       const GList *tmp;
1141
1142       for (tmp = gst_element_factory_get_static_pad_templates (factory);
1143           tmp; tmp = g_list_next (tmp)) {
1144         GstStaticPadTemplate *template = tmp->data;
1145
1146         if (GST_PAD_TEMPLATE_DIRECTION (template) == dir) {
1147           GstCaps *static_caps = gst_static_pad_template_get_caps (template);
1148
1149           if (static_caps) {
1150             caps = gst_caps_merge (caps, static_caps);
1151           }
1152
1153           /* Early out, any is absorbing */
1154           if (gst_caps_is_any (caps))
1155             goto out;
1156         }
1157       }
1158     }
1159   }
1160
1161   GST_DEBUG_OBJECT (autoconvert, "Returning unioned caps %" GST_PTR_FORMAT,
1162       caps);
1163
1164 out:
1165
1166   if (other_caps)
1167     gst_caps_unref (other_caps);
1168
1169   return caps;
1170 }
1171
1172
1173
1174 static gboolean
1175 gst_auto_convert_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1176 {
1177   gboolean ret = TRUE;
1178   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
1179   GstPad *internal_sinkpad;
1180
1181   if (GST_EVENT_TYPE (event) == GST_EVENT_RECONFIGURE)
1182     gst_pad_push_event (autoconvert->sinkpad, gst_event_ref (event));
1183
1184   internal_sinkpad = gst_auto_convert_get_internal_sinkpad (autoconvert);
1185   if (internal_sinkpad) {
1186     ret = gst_pad_push_event (internal_sinkpad, event);
1187     gst_object_unref (internal_sinkpad);
1188   } else if (GST_EVENT_TYPE (event) != GST_EVENT_RECONFIGURE) {
1189     GST_WARNING_OBJECT (autoconvert,
1190         "Got upstream event while no element was selected," "forwarding.");
1191     ret = gst_pad_push_event (autoconvert->sinkpad, event);
1192   } else
1193     gst_event_unref (event);
1194
1195   return ret;
1196 }
1197
1198 /* TODO Properly test that this code works well for queries */
1199 static gboolean
1200 gst_auto_convert_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1201 {
1202   gboolean ret = TRUE;
1203   GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
1204   GstElement *subelement;
1205
1206   if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
1207     GstCaps *filter, *caps;
1208
1209     gst_query_parse_caps (query, &filter);
1210     caps = gst_auto_convert_getcaps (autoconvert, filter, GST_PAD_SRC);
1211     gst_query_set_caps_result (query, caps);
1212     gst_caps_unref (caps);
1213
1214     return TRUE;
1215   }
1216
1217   subelement = gst_auto_convert_get_subelement (autoconvert);
1218   if (subelement) {
1219     GstPad *sub_srcpad = get_pad_by_direction (subelement, GST_PAD_SRC);
1220
1221     ret = gst_pad_query (sub_srcpad, query);
1222
1223     gst_object_unref (sub_srcpad);
1224     gst_object_unref (subelement);
1225   } else {
1226     GST_WARNING_OBJECT (autoconvert,
1227         "Got upstream query of type %s while no element was selected,"
1228         " forwarding.", gst_query_type_get_name (GST_QUERY_TYPE (query)));
1229     ret = gst_pad_peer_query (autoconvert->sinkpad, query);
1230   }
1231
1232   return ret;
1233 }
1234
1235 static GstFlowReturn
1236 gst_auto_convert_internal_sink_chain (GstPad * pad, GstObject * parent,
1237     GstBuffer * buffer)
1238 {
1239   GstAutoConvert *autoconvert =
1240       GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1241           parent_quark));
1242
1243   return gst_pad_push (autoconvert->srcpad, buffer);
1244 }
1245
1246 static GstFlowReturn
1247 gst_auto_convert_internal_sink_chain_list (GstPad * pad, GstObject * parent,
1248     GstBufferList * list)
1249 {
1250   GstAutoConvert *autoconvert =
1251       GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1252           parent_quark));
1253
1254   return gst_pad_push_list (autoconvert->srcpad, list);
1255 }
1256
1257 static gboolean
1258 gst_auto_convert_internal_sink_event (GstPad * pad, GstObject * parent,
1259     GstEvent * event)
1260 {
1261   GstAutoConvert *autoconvert =
1262       GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1263           parent_quark));
1264   gboolean drop = FALSE;
1265
1266   GST_AUTOCONVERT_LOCK (autoconvert);
1267   if (autoconvert->current_internal_sinkpad != pad) {
1268     drop = TRUE;
1269   }
1270   GST_AUTOCONVERT_UNLOCK (autoconvert);
1271
1272   if (drop) {
1273     gst_event_unref (event);
1274     return TRUE;
1275   }
1276
1277   return gst_pad_push_event (autoconvert->srcpad, event);
1278 }
1279
1280 static gboolean
1281 gst_auto_convert_internal_sink_query (GstPad * pad, GstObject * parent,
1282     GstQuery * query)
1283 {
1284   GstAutoConvert *autoconvert =
1285       GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1286           parent_quark));
1287
1288   if (!gst_pad_peer_query (autoconvert->srcpad, query)) {
1289     switch (GST_QUERY_TYPE (query)) {
1290       case GST_QUERY_CAPS:
1291       {
1292         GstCaps *filter;
1293
1294         gst_query_parse_caps (query, &filter);
1295         if (filter) {
1296           gst_query_set_caps_result (query, filter);
1297         } else {
1298           filter = gst_caps_new_any ();
1299           gst_query_set_caps_result (query, filter);
1300           gst_caps_unref (filter);
1301         }
1302         return TRUE;
1303       }
1304       case GST_QUERY_ACCEPT_CAPS:
1305         gst_query_set_accept_caps_result (query, TRUE);
1306         return TRUE;
1307       default:
1308         return FALSE;
1309     }
1310   }
1311
1312   return TRUE;
1313 }
1314
1315 static gboolean
1316 gst_auto_convert_internal_src_event (GstPad * pad, GstObject * parent,
1317     GstEvent * event)
1318 {
1319   GstAutoConvert *autoconvert =
1320       GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1321           parent_quark));
1322   gboolean drop = FALSE;
1323
1324   GST_AUTOCONVERT_LOCK (autoconvert);
1325   if (autoconvert->current_internal_srcpad != pad) {
1326     drop = TRUE;
1327   }
1328   GST_AUTOCONVERT_UNLOCK (autoconvert);
1329
1330   if (drop) {
1331     GST_DEBUG_OBJECT (autoconvert, "Dropping event %" GST_PTR_FORMAT, event);
1332     gst_event_unref (event);
1333     return TRUE;
1334   }
1335
1336   return gst_pad_push_event (autoconvert->sinkpad, event);
1337 }
1338
1339 static gboolean
1340 gst_auto_convert_internal_src_query (GstPad * pad, GstObject * parent,
1341     GstQuery * query)
1342 {
1343   GstAutoConvert *autoconvert =
1344       GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1345           parent_quark));
1346
1347   return gst_pad_peer_query (autoconvert->sinkpad, query);
1348 }