tee: use store_sticky events add add more logging
[platform/upstream/gstreamer.git] / plugins / elements / gsttee.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2000,2001,2002,2003,2004,2005 Wim Taymans <wim@fluendo.com>
4  *               2007 Wim Taymans <wim.taymans@gmail.com>
5  *
6  * gsttee.c: Tee element, one in N out
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 /**
25  * SECTION:element-tee
26  * @see_also: #GstIdentity
27  *
28  * Split data to multiple pads. Branching the data flow is useful when e.g.
29  * capturing a video where the video is shown on the screen and also encoded and
30  * written to a file. Another example is playing music and hooking up a
31  * visualisation module.
32  *
33  * One needs to use separate queue elements (or a multiqueue) in each branch to
34  * provide separate threads for each branch. Otherwise a blocked dataflow in one
35  * branch would stall the other branches.
36  *
37  * <refsect2>
38  * <title>Example launch line</title>
39  * |[
40  * gst-launch filesrc location=song.ogg ! decodebin2 ! tee name=t ! queue ! autoaudiosink t. ! queue ! audioconvert ! goom ! videoconvert ! autovideosink
41  * ]| Play a song.ogg from local dir and render visualisations using the goom
42  * element.
43  * </refsect2>
44  */
45
46 #ifdef HAVE_CONFIG_H
47 #  include "config.h"
48 #endif
49
50 #include "gsttee.h"
51 #include "gst/glib-compat-private.h"
52
53 #include <string.h>
54 #include <stdio.h>
55
56 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS_ANY);
60
61 GST_DEBUG_CATEGORY_STATIC (gst_tee_debug);
62 #define GST_CAT_DEFAULT gst_tee_debug
63
64 #define GST_TYPE_TEE_PULL_MODE (gst_tee_pull_mode_get_type())
65 static GType
66 gst_tee_pull_mode_get_type (void)
67 {
68   static GType type = 0;
69   static const GEnumValue data[] = {
70     {GST_TEE_PULL_MODE_NEVER, "Never activate in pull mode", "never"},
71     {GST_TEE_PULL_MODE_SINGLE, "Only one src pad can be active in pull mode",
72         "single"},
73     {0, NULL, NULL},
74   };
75
76   if (!type) {
77     type = g_enum_register_static ("GstTeePullMode", data);
78   }
79   return type;
80 }
81
82 #define DEFAULT_PROP_NUM_SRC_PADS       0
83 #define DEFAULT_PROP_HAS_CHAIN          TRUE
84 #define DEFAULT_PROP_SILENT             TRUE
85 #define DEFAULT_PROP_LAST_MESSAGE       NULL
86 #define DEFAULT_PULL_MODE               GST_TEE_PULL_MODE_NEVER
87
88 enum
89 {
90   PROP_0,
91   PROP_NUM_SRC_PADS,
92   PROP_HAS_CHAIN,
93   PROP_SILENT,
94   PROP_LAST_MESSAGE,
95   PROP_PULL_MODE,
96   PROP_ALLOC_PAD,
97 };
98
99 static GstStaticPadTemplate tee_src_template =
100 GST_STATIC_PAD_TEMPLATE ("src_%u",
101     GST_PAD_SRC,
102     GST_PAD_REQUEST,
103     GST_STATIC_CAPS_ANY);
104
105 #define _do_init \
106     GST_DEBUG_CATEGORY_INIT (gst_tee_debug, "tee", 0, "tee element");
107 #define gst_tee_parent_class parent_class
108 G_DEFINE_TYPE_WITH_CODE (GstTee, gst_tee, GST_TYPE_ELEMENT, _do_init);
109
110 static GParamSpec *pspec_last_message = NULL;
111 static GParamSpec *pspec_alloc_pad = NULL;
112
113 GType gst_tee_pad_get_type (void);
114
115 #define GST_TYPE_TEE_PAD \
116   (gst_tee_pad_get_type())
117 #define GST_TEE_PAD(obj) \
118   (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_TEE_PAD, GstTeePad))
119 #define GST_TEE_PAD_CLASS(klass) \
120   (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_TEE_PAD, GstTeePadClass))
121 #define GST_IS_TEE_PAD(obj) \
122   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_TEE_PAD))
123 #define GST_IS_TEE_PAD_CLASS(klass) \
124   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_TEE_PAD))
125 #define GST_TEE_PAD_CAST(obj) \
126   ((GstTeePad *)(obj))
127
128 typedef struct _GstTeePad GstTeePad;
129 typedef struct _GstTeePadClass GstTeePadClass;
130
131 struct _GstTeePad
132 {
133   GstPad parent;
134
135   guint index;
136   gboolean pushed;
137   GstFlowReturn result;
138   gboolean removed;
139 };
140
141 struct _GstTeePadClass
142 {
143   GstPadClass parent;
144 };
145
146 G_DEFINE_TYPE (GstTeePad, gst_tee_pad, GST_TYPE_PAD);
147
148 static void
149 gst_tee_pad_class_init (GstTeePadClass * klass)
150 {
151 }
152
153 static void
154 gst_tee_pad_reset (GstTeePad * pad)
155 {
156   pad->pushed = FALSE;
157   pad->result = GST_FLOW_NOT_LINKED;
158   pad->removed = FALSE;
159 }
160
161 static void
162 gst_tee_pad_init (GstTeePad * pad)
163 {
164   gst_tee_pad_reset (pad);
165 }
166
167 static GstPad *gst_tee_request_new_pad (GstElement * element,
168     GstPadTemplate * temp, const gchar * unused, const GstCaps * caps);
169 static void gst_tee_release_pad (GstElement * element, GstPad * pad);
170
171 static void gst_tee_finalize (GObject * object);
172 static void gst_tee_set_property (GObject * object, guint prop_id,
173     const GValue * value, GParamSpec * pspec);
174 static void gst_tee_get_property (GObject * object, guint prop_id,
175     GValue * value, GParamSpec * pspec);
176 static void gst_tee_dispose (GObject * object);
177
178 static GstFlowReturn gst_tee_chain (GstPad * pad, GstObject * parent,
179     GstBuffer * buffer);
180 static GstFlowReturn gst_tee_chain_list (GstPad * pad, GstObject * parent,
181     GstBufferList * list);
182 static gboolean gst_tee_sink_event (GstPad * pad, GstObject * parent,
183     GstEvent * event);
184 static gboolean gst_tee_sink_query (GstPad * pad, GstObject * parent,
185     GstQuery * query);
186 static gboolean gst_tee_sink_activate_mode (GstPad * pad, GstObject * parent,
187     GstPadMode mode, gboolean active);
188 static gboolean gst_tee_src_query (GstPad * pad, GstObject * parent,
189     GstQuery * query);
190 static gboolean gst_tee_src_activate_mode (GstPad * pad, GstObject * parent,
191     GstPadMode mode, gboolean active);
192 static GstFlowReturn gst_tee_src_get_range (GstPad * pad, GstObject * parent,
193     guint64 offset, guint length, GstBuffer ** buf);
194
195 static void
196 gst_tee_dispose (GObject * object)
197 {
198   GList *item;
199
200 restart:
201   for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
202     GstPad *pad = GST_PAD (item->data);
203     if (GST_PAD_IS_SRC (pad)) {
204       gst_element_release_request_pad (GST_ELEMENT (object), pad);
205       goto restart;
206     }
207   }
208
209   G_OBJECT_CLASS (parent_class)->dispose (object);
210 }
211
212 static void
213 gst_tee_finalize (GObject * object)
214 {
215   GstTee *tee;
216
217   tee = GST_TEE (object);
218
219   g_hash_table_unref (tee->pad_indexes);
220
221   g_free (tee->last_message);
222
223   G_OBJECT_CLASS (parent_class)->finalize (object);
224 }
225
226 static void
227 gst_tee_class_init (GstTeeClass * klass)
228 {
229   GObjectClass *gobject_class;
230   GstElementClass *gstelement_class;
231
232   gobject_class = G_OBJECT_CLASS (klass);
233   gstelement_class = GST_ELEMENT_CLASS (klass);
234
235   gobject_class->finalize = gst_tee_finalize;
236   gobject_class->set_property = gst_tee_set_property;
237   gobject_class->get_property = gst_tee_get_property;
238   gobject_class->dispose = gst_tee_dispose;
239
240   g_object_class_install_property (gobject_class, PROP_NUM_SRC_PADS,
241       g_param_spec_int ("num-src-pads", "Num Src Pads",
242           "The number of source pads", 0, G_MAXINT, DEFAULT_PROP_NUM_SRC_PADS,
243           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
244   g_object_class_install_property (gobject_class, PROP_HAS_CHAIN,
245       g_param_spec_boolean ("has-chain", "Has Chain",
246           "If the element can operate in push mode", DEFAULT_PROP_HAS_CHAIN,
247           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248   g_object_class_install_property (gobject_class, PROP_SILENT,
249       g_param_spec_boolean ("silent", "Silent",
250           "Don't produce last_message events", DEFAULT_PROP_SILENT,
251           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252   pspec_last_message = g_param_spec_string ("last-message", "Last Message",
253       "The message describing current status", DEFAULT_PROP_LAST_MESSAGE,
254       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
255   g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
256       pspec_last_message);
257   g_object_class_install_property (gobject_class, PROP_PULL_MODE,
258       g_param_spec_enum ("pull-mode", "Pull mode",
259           "Behavior of tee in pull mode", GST_TYPE_TEE_PULL_MODE,
260           DEFAULT_PULL_MODE,
261           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262   pspec_alloc_pad = g_param_spec_object ("alloc-pad", "Allocation Src Pad",
263       "The pad ALLOCATION queries will be proxied to (unused)", GST_TYPE_PAD,
264       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
265   g_object_class_install_property (gobject_class, PROP_ALLOC_PAD,
266       pspec_alloc_pad);
267
268   gst_element_class_set_static_metadata (gstelement_class,
269       "Tee pipe fitting",
270       "Generic",
271       "1-to-N pipe fitting",
272       "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
273   gst_element_class_add_pad_template (gstelement_class,
274       gst_static_pad_template_get (&sinktemplate));
275   gst_element_class_add_pad_template (gstelement_class,
276       gst_static_pad_template_get (&tee_src_template));
277
278   gstelement_class->request_new_pad =
279       GST_DEBUG_FUNCPTR (gst_tee_request_new_pad);
280   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_tee_release_pad);
281 }
282
283 static void
284 gst_tee_init (GstTee * tee)
285 {
286   tee->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
287   tee->sink_mode = GST_PAD_MODE_NONE;
288
289   gst_pad_set_event_function (tee->sinkpad,
290       GST_DEBUG_FUNCPTR (gst_tee_sink_event));
291   gst_pad_set_query_function (tee->sinkpad,
292       GST_DEBUG_FUNCPTR (gst_tee_sink_query));
293   gst_pad_set_activatemode_function (tee->sinkpad,
294       GST_DEBUG_FUNCPTR (gst_tee_sink_activate_mode));
295   gst_pad_set_chain_function (tee->sinkpad, GST_DEBUG_FUNCPTR (gst_tee_chain));
296   gst_pad_set_chain_list_function (tee->sinkpad,
297       GST_DEBUG_FUNCPTR (gst_tee_chain_list));
298   GST_OBJECT_FLAG_SET (tee->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
299   gst_element_add_pad (GST_ELEMENT (tee), tee->sinkpad);
300
301   tee->pad_indexes = g_hash_table_new (NULL, NULL);
302
303   tee->last_message = NULL;
304 }
305
306 static void
307 gst_tee_notify_alloc_pad (GstTee * tee)
308 {
309   g_object_notify_by_pspec ((GObject *) tee, pspec_alloc_pad);
310 }
311
312 static gboolean
313 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
314 {
315   GstPad *srcpad = GST_PAD_CAST (user_data);
316   GstFlowReturn ret;
317
318   ret = gst_pad_store_sticky_event (srcpad, *event);
319   if (ret != GST_FLOW_OK) {
320     GST_DEBUG_OBJECT (srcpad, "storing sticky event %p (%s) failed: %s", *event,
321         GST_EVENT_TYPE_NAME (*event), gst_flow_get_name (ret));
322   }
323
324   return TRUE;
325 }
326
327 static GstPad *
328 gst_tee_request_new_pad (GstElement * element, GstPadTemplate * templ,
329     const gchar * name_templ, const GstCaps * caps)
330 {
331   gchar *name;
332   GstPad *srcpad;
333   GstTee *tee;
334   GstPadMode mode;
335   gboolean res;
336   guint index = 0;
337
338   tee = GST_TEE (element);
339
340   GST_DEBUG_OBJECT (tee, "requesting pad");
341
342   GST_OBJECT_LOCK (tee);
343
344   if (name_templ) {
345     sscanf (name_templ, "src_%u", &index);
346     GST_LOG_OBJECT (element, "name: %s (index %d)", name_templ, index);
347     if (g_hash_table_contains (tee->pad_indexes, GUINT_TO_POINTER (index))) {
348       GST_ERROR_OBJECT (element, "pad name %s is not unique", name_templ);
349       GST_OBJECT_UNLOCK (tee);
350       return NULL;
351     }
352     if (index >= tee->next_pad_index)
353       tee->next_pad_index = index + 1;
354   } else {
355     index = tee->next_pad_index;
356
357     while (g_hash_table_contains (tee->pad_indexes, GUINT_TO_POINTER (index)))
358       index++;
359
360     tee->next_pad_index = index + 1;
361   }
362
363   g_hash_table_insert (tee->pad_indexes, GUINT_TO_POINTER (index), NULL);
364
365   name = g_strdup_printf ("src_%u", index);
366
367   srcpad = GST_PAD_CAST (g_object_new (GST_TYPE_TEE_PAD,
368           "name", name, "direction", templ->direction, "template", templ,
369           NULL));
370   GST_TEE_PAD_CAST (srcpad)->index = index;
371   g_free (name);
372
373   mode = tee->sink_mode;
374
375   GST_OBJECT_UNLOCK (tee);
376
377   switch (mode) {
378     case GST_PAD_MODE_PULL:
379       /* we already have a src pad in pull mode, and our pull mode can only be
380          SINGLE, so fall through to activate this new pad in push mode */
381     case GST_PAD_MODE_PUSH:
382       res = gst_pad_activate_mode (srcpad, GST_PAD_MODE_PUSH, TRUE);
383       break;
384     default:
385       res = TRUE;
386       break;
387   }
388
389   if (!res)
390     goto activate_failed;
391
392   gst_pad_set_activatemode_function (srcpad,
393       GST_DEBUG_FUNCPTR (gst_tee_src_activate_mode));
394   gst_pad_set_query_function (srcpad, GST_DEBUG_FUNCPTR (gst_tee_src_query));
395   gst_pad_set_getrange_function (srcpad,
396       GST_DEBUG_FUNCPTR (gst_tee_src_get_range));
397   /* Forward sticky events to the new srcpad */
398   gst_pad_sticky_events_foreach (tee->sinkpad, forward_sticky_events, srcpad);
399   GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PROXY_CAPS);
400   gst_element_add_pad (GST_ELEMENT_CAST (tee), srcpad);
401
402   return srcpad;
403
404   /* ERRORS */
405 activate_failed:
406   {
407     gboolean changed = FALSE;
408
409     GST_OBJECT_LOCK (tee);
410     GST_DEBUG_OBJECT (tee, "warning failed to activate request pad");
411     if (tee->allocpad == srcpad) {
412       tee->allocpad = NULL;
413       changed = TRUE;
414     }
415     GST_OBJECT_UNLOCK (tee);
416     gst_object_unref (srcpad);
417     if (changed) {
418       gst_tee_notify_alloc_pad (tee);
419     }
420     return NULL;
421   }
422 }
423
424 static void
425 gst_tee_release_pad (GstElement * element, GstPad * pad)
426 {
427   GstTee *tee;
428   gboolean changed = FALSE;
429   guint index;
430
431   tee = GST_TEE (element);
432
433   GST_DEBUG_OBJECT (tee, "releasing pad");
434
435   GST_OBJECT_LOCK (tee);
436   index = GST_TEE_PAD_CAST (pad)->index;
437   /* mark the pad as removed so that future pad_alloc fails with NOT_LINKED. */
438   GST_TEE_PAD_CAST (pad)->removed = TRUE;
439   if (tee->allocpad == pad) {
440     tee->allocpad = NULL;
441     changed = TRUE;
442   }
443   GST_OBJECT_UNLOCK (tee);
444
445   gst_object_ref (pad);
446   gst_element_remove_pad (GST_ELEMENT_CAST (tee), pad);
447
448   gst_pad_set_active (pad, FALSE);
449
450   gst_object_unref (pad);
451
452   if (changed) {
453     gst_tee_notify_alloc_pad (tee);
454   }
455
456   GST_OBJECT_LOCK (tee);
457   g_hash_table_remove (tee->pad_indexes, GUINT_TO_POINTER (index));
458   GST_OBJECT_UNLOCK (tee);
459 }
460
461 static void
462 gst_tee_set_property (GObject * object, guint prop_id, const GValue * value,
463     GParamSpec * pspec)
464 {
465   GstTee *tee = GST_TEE (object);
466
467   GST_OBJECT_LOCK (tee);
468   switch (prop_id) {
469     case PROP_HAS_CHAIN:
470       tee->has_chain = g_value_get_boolean (value);
471       break;
472     case PROP_SILENT:
473       tee->silent = g_value_get_boolean (value);
474       break;
475     case PROP_PULL_MODE:
476       tee->pull_mode = (GstTeePullMode) g_value_get_enum (value);
477       break;
478     case PROP_ALLOC_PAD:
479     {
480       GstPad *pad = g_value_get_object (value);
481       GST_OBJECT_LOCK (pad);
482       if (GST_OBJECT_PARENT (pad) == GST_OBJECT_CAST (object))
483         tee->allocpad = pad;
484       else
485         GST_WARNING_OBJECT (object, "Tried to set alloc pad %s which"
486             " is not my pad", GST_OBJECT_NAME (pad));
487       GST_OBJECT_UNLOCK (pad);
488       break;
489     }
490     default:
491       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
492       break;
493   }
494   GST_OBJECT_UNLOCK (tee);
495 }
496
497 static void
498 gst_tee_get_property (GObject * object, guint prop_id, GValue * value,
499     GParamSpec * pspec)
500 {
501   GstTee *tee = GST_TEE (object);
502
503   GST_OBJECT_LOCK (tee);
504   switch (prop_id) {
505     case PROP_NUM_SRC_PADS:
506       g_value_set_int (value, GST_ELEMENT (tee)->numsrcpads);
507       break;
508     case PROP_HAS_CHAIN:
509       g_value_set_boolean (value, tee->has_chain);
510       break;
511     case PROP_SILENT:
512       g_value_set_boolean (value, tee->silent);
513       break;
514     case PROP_LAST_MESSAGE:
515       g_value_set_string (value, tee->last_message);
516       break;
517     case PROP_PULL_MODE:
518       g_value_set_enum (value, tee->pull_mode);
519       break;
520     case PROP_ALLOC_PAD:
521       g_value_set_object (value, tee->allocpad);
522       break;
523     default:
524       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
525       break;
526   }
527   GST_OBJECT_UNLOCK (tee);
528 }
529
530 static gboolean
531 gst_tee_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
532 {
533   gboolean res;
534
535   switch (GST_EVENT_TYPE (event)) {
536     default:
537       res = gst_pad_event_default (pad, parent, event);
538       break;
539   }
540
541   return res;
542 }
543
544 static gboolean
545 gst_tee_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
546 {
547   gboolean res;
548
549   switch (GST_QUERY_TYPE (query)) {
550     default:
551       res = gst_pad_query_default (pad, parent, query);
552       break;
553   }
554   return res;
555 }
556
557 static void
558 gst_tee_do_message (GstTee * tee, GstPad * pad, gpointer data, gboolean is_list)
559 {
560   GST_OBJECT_LOCK (tee);
561   g_free (tee->last_message);
562   if (is_list) {
563     tee->last_message =
564         g_strdup_printf ("chain-list   ******* (%s:%s)t %p",
565         GST_DEBUG_PAD_NAME (pad), data);
566   } else {
567     tee->last_message =
568         g_strdup_printf ("chain        ******* (%s:%s)t (%" G_GSIZE_FORMAT
569         " bytes, %" G_GUINT64_FORMAT ") %p", GST_DEBUG_PAD_NAME (pad),
570         gst_buffer_get_size (data), GST_BUFFER_TIMESTAMP (data), data);
571   }
572   GST_OBJECT_UNLOCK (tee);
573
574   g_object_notify_by_pspec ((GObject *) tee, pspec_last_message);
575 }
576
577 static GstFlowReturn
578 gst_tee_do_push (GstTee * tee, GstPad * pad, gpointer data, gboolean is_list)
579 {
580   GstFlowReturn res;
581
582   /* Push */
583   if (pad == tee->pull_pad) {
584     /* don't push on the pad we're pulling from */
585     res = GST_FLOW_OK;
586   } else if (is_list) {
587     res =
588         gst_pad_push_list (pad,
589         gst_buffer_list_ref (GST_BUFFER_LIST_CAST (data)));
590   } else {
591     res = gst_pad_push (pad, gst_buffer_ref (GST_BUFFER_CAST (data)));
592   }
593   return res;
594 }
595
596 static void
597 clear_pads (GstPad * pad, GstTee * tee)
598 {
599   GST_TEE_PAD_CAST (pad)->pushed = FALSE;
600   GST_TEE_PAD_CAST (pad)->result = GST_FLOW_NOT_LINKED;
601 }
602
603 static GstFlowReturn
604 gst_tee_handle_data (GstTee * tee, gpointer data, gboolean is_list)
605 {
606   GList *pads;
607   guint32 cookie;
608   GstFlowReturn ret, cret;
609
610   if (G_UNLIKELY (!tee->silent))
611     gst_tee_do_message (tee, tee->sinkpad, data, is_list);
612
613   GST_OBJECT_LOCK (tee);
614   pads = GST_ELEMENT_CAST (tee)->srcpads;
615
616   /* special case for zero pads */
617   if (G_UNLIKELY (!pads))
618     goto no_pads;
619
620   /* special case for just one pad that avoids reffing the buffer */
621   if (!pads->next) {
622     GstPad *pad = GST_PAD_CAST (pads->data);
623
624     /* Keep another ref around, a pad probe
625      * might release and destroy the pad */
626     gst_object_ref (pad);
627     GST_OBJECT_UNLOCK (tee);
628
629     if (pad == tee->pull_pad) {
630       ret = GST_FLOW_OK;
631     } else if (is_list) {
632       ret = gst_pad_push_list (pad, GST_BUFFER_LIST_CAST (data));
633     } else {
634       ret = gst_pad_push (pad, GST_BUFFER_CAST (data));
635     }
636
637     gst_object_unref (pad);
638
639     return ret;
640   }
641
642   /* mark all pads as 'not pushed on yet' */
643   g_list_foreach (pads, (GFunc) clear_pads, tee);
644
645 restart:
646   cret = GST_FLOW_NOT_LINKED;
647   pads = GST_ELEMENT_CAST (tee)->srcpads;
648   cookie = GST_ELEMENT_CAST (tee)->pads_cookie;
649
650   while (pads) {
651     GstPad *pad;
652
653     pad = GST_PAD_CAST (pads->data);
654
655     if (G_LIKELY (!GST_TEE_PAD_CAST (pad)->pushed)) {
656       /* not yet pushed, release lock and start pushing */
657       gst_object_ref (pad);
658       GST_OBJECT_UNLOCK (tee);
659
660       GST_LOG_OBJECT (pad, "Starting to push %s %p",
661           is_list ? "list" : "buffer", data);
662
663       ret = gst_tee_do_push (tee, pad, data, is_list);
664
665       GST_LOG_OBJECT (pad, "Pushing item %p yielded result %s", data,
666           gst_flow_get_name (ret));
667
668       GST_OBJECT_LOCK (tee);
669       /* keep track of which pad we pushed and the result value */
670       GST_TEE_PAD_CAST (pad)->pushed = TRUE;
671       GST_TEE_PAD_CAST (pad)->result = ret;
672       gst_object_unref (pad);
673     } else {
674       /* already pushed, use previous return value */
675       ret = GST_TEE_PAD_CAST (pad)->result;
676       GST_LOG_OBJECT (pad, "pad already pushed with %s",
677           gst_flow_get_name (ret));
678     }
679
680     /* before we go combining the return value, check if the pad list is still
681      * the same. It could be possible that the pad we just pushed was removed
682      * and the return value it not valid anymore */
683     if (G_UNLIKELY (GST_ELEMENT_CAST (tee)->pads_cookie != cookie)) {
684       GST_LOG_OBJECT (pad, "pad list changed");
685       /* the list of pads changed, restart iteration. Pads that we already
686        * pushed on and are still in the new list, will not be pushed on
687        * again. */
688       goto restart;
689     }
690
691     /* stop pushing more buffers when we have a fatal error */
692     if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED))
693       goto error;
694
695     /* keep all other return values, overwriting the previous one. */
696     if (G_LIKELY (ret != GST_FLOW_NOT_LINKED)) {
697       GST_LOG_OBJECT (pad, "Replacing ret val %d with %d", cret, ret);
698       cret = ret;
699     }
700     pads = g_list_next (pads);
701   }
702   GST_OBJECT_UNLOCK (tee);
703
704   gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
705
706   /* no need to unset gvalue */
707   return cret;
708
709   /* ERRORS */
710 no_pads:
711   {
712     GST_DEBUG_OBJECT (tee, "there are no pads, return not-linked");
713     ret = GST_FLOW_NOT_LINKED;
714     goto error;
715   }
716 error:
717   {
718     GST_DEBUG_OBJECT (tee, "received error %s", gst_flow_get_name (ret));
719     GST_OBJECT_UNLOCK (tee);
720     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
721     return ret;
722   }
723 }
724
725 static GstFlowReturn
726 gst_tee_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
727 {
728   GstFlowReturn res;
729   GstTee *tee;
730
731   tee = GST_TEE_CAST (parent);
732
733   GST_DEBUG_OBJECT (tee, "received buffer %p", buffer);
734
735   res = gst_tee_handle_data (tee, buffer, FALSE);
736
737   GST_DEBUG_OBJECT (tee, "handled buffer %s", gst_flow_get_name (res));
738
739   return res;
740 }
741
742 static GstFlowReturn
743 gst_tee_chain_list (GstPad * pad, GstObject * parent, GstBufferList * list)
744 {
745   GstFlowReturn res;
746   GstTee *tee;
747
748   tee = GST_TEE_CAST (parent);
749
750   GST_DEBUG_OBJECT (tee, "received list %p", list);
751
752   res = gst_tee_handle_data (tee, list, TRUE);
753
754   GST_DEBUG_OBJECT (tee, "handled list %s", gst_flow_get_name (res));
755
756   return res;
757 }
758
759 static gboolean
760 gst_tee_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
761     gboolean active)
762 {
763   gboolean res;
764   GstTee *tee;
765
766   tee = GST_TEE (parent);
767
768   switch (mode) {
769     case GST_PAD_MODE_PUSH:
770     {
771       GST_OBJECT_LOCK (tee);
772       tee->sink_mode = active ? mode : GST_PAD_MODE_NONE;
773
774       if (active && !tee->has_chain)
775         goto no_chain;
776       GST_OBJECT_UNLOCK (tee);
777       res = TRUE;
778       break;
779     }
780     default:
781       res = FALSE;
782       break;
783   }
784   return res;
785
786   /* ERRORS */
787 no_chain:
788   {
789     GST_OBJECT_UNLOCK (tee);
790     GST_INFO_OBJECT (tee,
791         "Tee cannot operate in push mode with has-chain==FALSE");
792     return FALSE;
793   }
794 }
795
796 static gboolean
797 gst_tee_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
798     gboolean active)
799 {
800   GstTee *tee;
801   gboolean res;
802   GstPad *sinkpad;
803
804   tee = GST_TEE (parent);
805
806   switch (mode) {
807     case GST_PAD_MODE_PULL:
808     {
809       GST_OBJECT_LOCK (tee);
810
811       if (tee->pull_mode == GST_TEE_PULL_MODE_NEVER)
812         goto cannot_pull;
813
814       if (tee->pull_mode == GST_TEE_PULL_MODE_SINGLE && active && tee->pull_pad)
815         goto cannot_pull_multiple_srcs;
816
817       sinkpad = gst_object_ref (tee->sinkpad);
818
819       GST_OBJECT_UNLOCK (tee);
820
821       res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, active);
822       gst_object_unref (sinkpad);
823
824       if (!res)
825         goto sink_activate_failed;
826
827       GST_OBJECT_LOCK (tee);
828       if (active) {
829         if (tee->pull_mode == GST_TEE_PULL_MODE_SINGLE)
830           tee->pull_pad = pad;
831       } else {
832         if (pad == tee->pull_pad)
833           tee->pull_pad = NULL;
834       }
835       tee->sink_mode = (active ? GST_PAD_MODE_PULL : GST_PAD_MODE_NONE);
836       GST_OBJECT_UNLOCK (tee);
837       break;
838     }
839     default:
840       res = TRUE;
841       break;
842   }
843
844   return res;
845
846   /* ERRORS */
847 cannot_pull:
848   {
849     GST_OBJECT_UNLOCK (tee);
850     GST_INFO_OBJECT (tee, "Cannot activate in pull mode, pull-mode "
851         "set to NEVER");
852     return FALSE;
853   }
854 cannot_pull_multiple_srcs:
855   {
856     GST_OBJECT_UNLOCK (tee);
857     GST_INFO_OBJECT (tee, "Cannot activate multiple src pads in pull mode, "
858         "pull-mode set to SINGLE");
859     return FALSE;
860   }
861 sink_activate_failed:
862   {
863     GST_INFO_OBJECT (tee, "Failed to %sactivate sink pad in pull mode",
864         active ? "" : "de");
865     return FALSE;
866   }
867 }
868
869 static gboolean
870 gst_tee_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
871 {
872   GstTee *tee;
873   gboolean res;
874   GstPad *sinkpad;
875
876   tee = GST_TEE (parent);
877
878   switch (GST_QUERY_TYPE (query)) {
879     case GST_QUERY_SCHEDULING:
880     {
881       gboolean pull_mode;
882
883       GST_OBJECT_LOCK (tee);
884       pull_mode = TRUE;
885       if (tee->pull_mode == GST_TEE_PULL_MODE_NEVER) {
886         GST_INFO_OBJECT (tee, "Cannot activate in pull mode, pull-mode "
887             "set to NEVER");
888         pull_mode = FALSE;
889       } else if (tee->pull_mode == GST_TEE_PULL_MODE_SINGLE && tee->pull_pad) {
890         GST_INFO_OBJECT (tee, "Cannot activate multiple src pads in pull mode, "
891             "pull-mode set to SINGLE");
892         pull_mode = FALSE;
893       }
894
895       sinkpad = gst_object_ref (tee->sinkpad);
896       GST_OBJECT_UNLOCK (tee);
897
898       if (pull_mode) {
899         /* ask peer if we can operate in pull mode */
900         res = gst_pad_peer_query (sinkpad, query);
901       } else {
902         res = TRUE;
903       }
904       gst_object_unref (sinkpad);
905       break;
906     }
907     default:
908       res = gst_pad_query_default (pad, parent, query);
909       break;
910   }
911
912   return res;
913 }
914
915 static void
916 gst_tee_push_eos (const GValue * vpad, GstTee * tee)
917 {
918   GstPad *pad = g_value_get_object (vpad);
919
920   if (pad != tee->pull_pad)
921     gst_pad_push_event (pad, gst_event_new_eos ());
922 }
923
924 static void
925 gst_tee_pull_eos (GstTee * tee)
926 {
927   GstIterator *iter;
928
929   iter = gst_element_iterate_src_pads (GST_ELEMENT (tee));
930   gst_iterator_foreach (iter, (GstIteratorForeachFunction) gst_tee_push_eos,
931       tee);
932   gst_iterator_free (iter);
933 }
934
935 static GstFlowReturn
936 gst_tee_src_get_range (GstPad * pad, GstObject * parent, guint64 offset,
937     guint length, GstBuffer ** buf)
938 {
939   GstTee *tee;
940   GstFlowReturn ret;
941
942   tee = GST_TEE (parent);
943
944   ret = gst_pad_pull_range (tee->sinkpad, offset, length, buf);
945
946   if (ret == GST_FLOW_OK)
947     ret = gst_tee_handle_data (tee, gst_buffer_ref (*buf), FALSE);
948   else if (ret == GST_FLOW_EOS)
949     gst_tee_pull_eos (tee);
950
951   return ret;
952 }