tee: implement custom acceptcaps function
[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., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:element-tee
26  * @see_also: #GstIdentity
27  *
28  * Split data to multiple pads.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #  include "config.h"
33 #endif
34
35 #include "gsttee.h"
36
37 #include <string.h>
38
39 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
40     GST_PAD_SINK,
41     GST_PAD_ALWAYS,
42     GST_STATIC_CAPS_ANY);
43
44 GST_DEBUG_CATEGORY_STATIC (gst_tee_debug);
45 #define GST_CAT_DEFAULT gst_tee_debug
46
47 #define GST_TYPE_TEE_PULL_MODE (gst_tee_pull_mode_get_type())
48 static GType
49 gst_tee_pull_mode_get_type (void)
50 {
51   static GType type = 0;
52   static const GEnumValue data[] = {
53     {GST_TEE_PULL_MODE_NEVER, "Never activate in pull mode", "never"},
54     {GST_TEE_PULL_MODE_SINGLE, "Only one src pad can be active in pull mode",
55         "single"},
56     {0, NULL, NULL},
57   };
58
59   if (!type) {
60     type = g_enum_register_static ("GstTeePullMode", data);
61   }
62   return type;
63 }
64
65 /* lock to protect request pads from being removed while downstream */
66 #define GST_TEE_DYN_LOCK(tee) g_mutex_lock ((tee)->dyn_lock)
67 #define GST_TEE_DYN_UNLOCK(tee) g_mutex_unlock ((tee)->dyn_lock)
68
69 #define DEFAULT_PROP_NUM_SRC_PADS       0
70 #define DEFAULT_PROP_HAS_SINK_LOOP      FALSE
71 #define DEFAULT_PROP_HAS_CHAIN          TRUE
72 #define DEFAULT_PROP_SILENT             TRUE
73 #define DEFAULT_PROP_LAST_MESSAGE       NULL
74 #define DEFAULT_PULL_MODE               GST_TEE_PULL_MODE_NEVER
75
76 enum
77 {
78   PROP_0,
79   PROP_NUM_SRC_PADS,
80   PROP_HAS_SINK_LOOP,
81   PROP_HAS_CHAIN,
82   PROP_SILENT,
83   PROP_LAST_MESSAGE,
84   PROP_PULL_MODE,
85   PROP_ALLOC_PAD,
86 };
87
88 static GstStaticPadTemplate tee_src_template = GST_STATIC_PAD_TEMPLATE ("src%d",
89     GST_PAD_SRC,
90     GST_PAD_REQUEST,
91     GST_STATIC_CAPS_ANY);
92
93 #define _do_init(bla) \
94     GST_DEBUG_CATEGORY_INIT (gst_tee_debug, "tee", 0, "tee element");
95
96 GST_BOILERPLATE_FULL (GstTee, gst_tee, GstElement, GST_TYPE_ELEMENT, _do_init);
97
98 /* structure and quark to keep track of which pads have been pushed */
99 static GQuark push_data;
100
101 typedef struct
102 {
103   gboolean pushed;
104   GstFlowReturn result;
105   gboolean removed;
106 } PushData;
107
108 static GstPad *gst_tee_request_new_pad (GstElement * element,
109     GstPadTemplate * temp, const gchar * unused);
110 static void gst_tee_release_pad (GstElement * element, GstPad * pad);
111
112 static void gst_tee_finalize (GObject * object);
113 static void gst_tee_set_property (GObject * object, guint prop_id,
114     const GValue * value, GParamSpec * pspec);
115 static void gst_tee_get_property (GObject * object, guint prop_id,
116     GValue * value, GParamSpec * pspec);
117
118 static GstFlowReturn gst_tee_chain (GstPad * pad, GstBuffer * buffer);
119 static GstFlowReturn gst_tee_chain_list (GstPad * pad, GstBufferList * list);
120 static GstFlowReturn gst_tee_buffer_alloc (GstPad * pad, guint64 offset,
121     guint size, GstCaps * caps, GstBuffer ** buf);
122 static gboolean gst_tee_sink_acceptcaps (GstPad * pad, GstCaps * caps);
123 static gboolean gst_tee_sink_activate_push (GstPad * pad, gboolean active);
124 static gboolean gst_tee_src_check_get_range (GstPad * pad);
125 static gboolean gst_tee_src_activate_pull (GstPad * pad, gboolean active);
126 static GstFlowReturn gst_tee_src_get_range (GstPad * pad, guint64 offset,
127     guint length, GstBuffer ** buf);
128
129
130 static void
131 gst_tee_base_init (gpointer g_class)
132 {
133   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
134
135   gst_element_class_set_details_simple (gstelement_class,
136       "Tee pipe fitting",
137       "Generic",
138       "1-to-N pipe fitting",
139       "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
140   gst_element_class_add_pad_template (gstelement_class,
141       gst_static_pad_template_get (&sinktemplate));
142   gst_element_class_add_pad_template (gstelement_class,
143       gst_static_pad_template_get (&tee_src_template));
144
145   push_data = g_quark_from_static_string ("tee-push-data");
146 }
147
148 static void
149 gst_tee_finalize (GObject * object)
150 {
151   GstTee *tee;
152
153   tee = GST_TEE (object);
154
155   g_free (tee->last_message);
156
157   g_mutex_free (tee->dyn_lock);
158
159   G_OBJECT_CLASS (parent_class)->finalize (object);
160 }
161
162 static void
163 gst_tee_class_init (GstTeeClass * klass)
164 {
165   GObjectClass *gobject_class;
166   GstElementClass *gstelement_class;
167
168   gobject_class = G_OBJECT_CLASS (klass);
169   gstelement_class = GST_ELEMENT_CLASS (klass);
170
171   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_tee_finalize);
172   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_tee_set_property);
173   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_tee_get_property);
174
175   g_object_class_install_property (gobject_class, PROP_NUM_SRC_PADS,
176       g_param_spec_int ("num-src-pads", "Num Src Pads",
177           "The number of source pads", 0, G_MAXINT, DEFAULT_PROP_NUM_SRC_PADS,
178           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
179   g_object_class_install_property (gobject_class, PROP_HAS_SINK_LOOP,
180       g_param_spec_boolean ("has-sink-loop", "Has Sink Loop",
181           "If the element should spawn a thread (unimplemented and deprecated)",
182           DEFAULT_PROP_HAS_SINK_LOOP,
183           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184   g_object_class_install_property (gobject_class, PROP_HAS_CHAIN,
185       g_param_spec_boolean ("has-chain", "Has Chain",
186           "If the element can operate in push mode", DEFAULT_PROP_HAS_CHAIN,
187           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188   g_object_class_install_property (gobject_class, PROP_SILENT,
189       g_param_spec_boolean ("silent", "Silent",
190           "Don't produce last_message events", DEFAULT_PROP_SILENT,
191           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192   g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
193       g_param_spec_string ("last-message", "Last Message",
194           "The message describing current status", DEFAULT_PROP_LAST_MESSAGE,
195           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
196   g_object_class_install_property (gobject_class, PROP_PULL_MODE,
197       g_param_spec_enum ("pull-mode", "Pull mode",
198           "Behavior of tee in pull mode", GST_TYPE_TEE_PULL_MODE,
199           DEFAULT_PULL_MODE,
200           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201   g_object_class_install_property (gobject_class, PROP_ALLOC_PAD,
202       g_param_spec_object ("alloc-pad", "Allocation Src Pad",
203           "The pad used for gst_pad_alloc_buffer", GST_TYPE_PAD,
204           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
205
206   gstelement_class->request_new_pad =
207       GST_DEBUG_FUNCPTR (gst_tee_request_new_pad);
208   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_tee_release_pad);
209 }
210
211 static void
212 gst_tee_init (GstTee * tee, GstTeeClass * g_class)
213 {
214   tee->dyn_lock = g_mutex_new ();
215
216   tee->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
217   tee->sink_mode = GST_ACTIVATE_NONE;
218
219   gst_pad_set_setcaps_function (tee->sinkpad,
220       GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
221   gst_pad_set_getcaps_function (tee->sinkpad,
222       GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
223   gst_pad_set_acceptcaps_function (tee->sinkpad,
224       GST_DEBUG_FUNCPTR (gst_tee_sink_acceptcaps));
225   gst_pad_set_bufferalloc_function (tee->sinkpad,
226       GST_DEBUG_FUNCPTR (gst_tee_buffer_alloc));
227   gst_pad_set_activatepush_function (tee->sinkpad,
228       GST_DEBUG_FUNCPTR (gst_tee_sink_activate_push));
229   gst_pad_set_chain_function (tee->sinkpad, GST_DEBUG_FUNCPTR (gst_tee_chain));
230   gst_pad_set_chain_list_function (tee->sinkpad,
231       GST_DEBUG_FUNCPTR (gst_tee_chain_list));
232   gst_element_add_pad (GST_ELEMENT (tee), tee->sinkpad);
233
234   tee->last_message = NULL;
235 }
236
237 static GstPad *
238 gst_tee_request_new_pad (GstElement * element, GstPadTemplate * templ,
239     const gchar * unused)
240 {
241   gchar *name;
242   GstPad *srcpad;
243   GstTee *tee;
244   GstActivateMode mode;
245   gboolean res;
246   PushData *data;
247
248   tee = GST_TEE (element);
249
250   GST_DEBUG_OBJECT (tee, "requesting pad");
251
252   GST_OBJECT_LOCK (tee);
253   name = g_strdup_printf ("src%d", tee->pad_counter++);
254
255   srcpad = gst_pad_new_from_template (templ, name);
256   g_free (name);
257
258   mode = tee->sink_mode;
259
260   /* install the data, we automatically free it when the pad is disposed because
261    * of _release_pad or when the element goes away. */
262   data = g_new0 (PushData, 1);
263   data->pushed = FALSE;
264   data->result = GST_FLOW_NOT_LINKED;
265   data->removed = FALSE;
266   g_object_set_qdata_full (G_OBJECT (srcpad), push_data, data, g_free);
267
268   GST_OBJECT_UNLOCK (tee);
269
270   switch (mode) {
271     case GST_ACTIVATE_PULL:
272       /* we already have a src pad in pull mode, and our pull mode can only be
273          SINGLE, so fall through to activate this new pad in push mode */
274     case GST_ACTIVATE_PUSH:
275       res = gst_pad_activate_push (srcpad, TRUE);
276       break;
277     default:
278       res = TRUE;
279       break;
280   }
281
282   if (!res)
283     goto activate_failed;
284
285   gst_pad_set_setcaps_function (srcpad,
286       GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
287   gst_pad_set_getcaps_function (srcpad,
288       GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
289   gst_pad_set_activatepull_function (srcpad,
290       GST_DEBUG_FUNCPTR (gst_tee_src_activate_pull));
291   gst_pad_set_checkgetrange_function (srcpad,
292       GST_DEBUG_FUNCPTR (gst_tee_src_check_get_range));
293   gst_pad_set_getrange_function (srcpad,
294       GST_DEBUG_FUNCPTR (gst_tee_src_get_range));
295   gst_element_add_pad (GST_ELEMENT_CAST (tee), srcpad);
296
297   return srcpad;
298
299   /* ERRORS */
300 activate_failed:
301   {
302     GST_OBJECT_LOCK (tee);
303     GST_DEBUG_OBJECT (tee, "warning failed to activate request pad");
304     if (tee->allocpad == srcpad)
305       tee->allocpad = NULL;
306     gst_object_unref (srcpad);
307     GST_OBJECT_UNLOCK (tee);
308     g_object_notify (G_OBJECT (tee), "alloc-pad");
309     return NULL;
310   }
311 }
312
313 static void
314 gst_tee_release_pad (GstElement * element, GstPad * pad)
315 {
316   GstTee *tee;
317   PushData *data;
318
319   tee = GST_TEE (element);
320
321   GST_DEBUG_OBJECT (tee, "releasing pad");
322
323   GST_OBJECT_LOCK (tee);
324   if (tee->allocpad == pad)
325     tee->allocpad = NULL;
326   GST_OBJECT_UNLOCK (tee);
327   g_object_notify (G_OBJECT (tee), "alloc-pad");
328
329   /* wait for pending pad_alloc to finish */
330   GST_TEE_DYN_LOCK (tee);
331   /* mark the pad as removed so that future pad_alloc fails with NOT_LINKED. */
332   data = g_object_get_qdata (G_OBJECT (pad), push_data);
333   data->removed = TRUE;
334
335   gst_pad_set_active (pad, FALSE);
336
337   gst_element_remove_pad (GST_ELEMENT_CAST (tee), pad);
338   GST_TEE_DYN_UNLOCK (tee);
339 }
340
341 static void
342 gst_tee_set_property (GObject * object, guint prop_id, const GValue * value,
343     GParamSpec * pspec)
344 {
345   GstTee *tee = GST_TEE (object);
346
347   GST_OBJECT_LOCK (tee);
348   switch (prop_id) {
349     case PROP_HAS_SINK_LOOP:
350       tee->has_sink_loop = g_value_get_boolean (value);
351       if (tee->has_sink_loop) {
352         g_warning ("tee will never implement has-sink-loop==TRUE");
353       }
354       break;
355     case PROP_HAS_CHAIN:
356       tee->has_chain = g_value_get_boolean (value);
357       break;
358     case PROP_SILENT:
359       tee->silent = g_value_get_boolean (value);
360       break;
361     case PROP_PULL_MODE:
362       tee->pull_mode = g_value_get_enum (value);
363       break;
364     case PROP_ALLOC_PAD:
365     {
366       GstPad *pad = g_value_get_object (value);
367       GST_OBJECT_LOCK (pad);
368       if (GST_OBJECT_PARENT (pad) == GST_OBJECT_CAST (object))
369         tee->allocpad = pad;
370       else
371         GST_WARNING_OBJECT (object, "Tried to set alloc pad %s which"
372             " is not my pad", GST_OBJECT_NAME (pad));
373       GST_OBJECT_UNLOCK (pad);
374       break;
375     }
376     default:
377       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378       break;
379   }
380   GST_OBJECT_UNLOCK (tee);
381 }
382
383 static void
384 gst_tee_get_property (GObject * object, guint prop_id, GValue * value,
385     GParamSpec * pspec)
386 {
387   GstTee *tee = GST_TEE (object);
388
389   GST_OBJECT_LOCK (tee);
390   switch (prop_id) {
391     case PROP_NUM_SRC_PADS:
392       g_value_set_int (value, GST_ELEMENT (tee)->numsrcpads);
393       break;
394     case PROP_HAS_SINK_LOOP:
395       g_value_set_boolean (value, tee->has_sink_loop);
396       break;
397     case PROP_HAS_CHAIN:
398       g_value_set_boolean (value, tee->has_chain);
399       break;
400     case PROP_SILENT:
401       g_value_set_boolean (value, tee->silent);
402       break;
403     case PROP_LAST_MESSAGE:
404       g_value_set_string (value, tee->last_message);
405       break;
406     case PROP_PULL_MODE:
407       g_value_set_enum (value, tee->pull_mode);
408       break;
409     case PROP_ALLOC_PAD:
410       g_value_set_object (value, tee->allocpad);
411       break;
412     default:
413       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
414       break;
415   }
416   GST_OBJECT_UNLOCK (tee);
417 }
418
419 /* we have no previous source pad we can use to proxy the pad alloc. Loop over
420  * the source pads, try to alloc a buffer on each one of them. Keep a reference
421  * to the first pad that succeeds, we will be using it to alloc more buffers
422  * later.  must be called with the OBJECT_LOCK on tee. */
423 static GstFlowReturn
424 gst_tee_find_buffer_alloc (GstTee * tee, guint64 offset, guint size,
425     GstCaps * caps, GstBuffer ** buf)
426 {
427   GstFlowReturn res;
428   GList *pads;
429   guint32 cookie;
430
431   res = GST_FLOW_NOT_LINKED;
432
433 retry:
434   pads = GST_ELEMENT_CAST (tee)->srcpads;
435   cookie = GST_ELEMENT_CAST (tee)->pads_cookie;
436
437   while (pads) {
438     GstPad *pad;
439     PushData *data;
440
441     pad = GST_PAD_CAST (pads->data);
442     gst_object_ref (pad);
443     GST_DEBUG_OBJECT (tee, "try alloc on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
444     GST_OBJECT_UNLOCK (tee);
445
446     GST_TEE_DYN_LOCK (tee);
447     data = g_object_get_qdata (G_OBJECT (pad), push_data);
448     if (!data->removed)
449       res = gst_pad_alloc_buffer (pad, offset, size, caps, buf);
450     else
451       res = GST_FLOW_NOT_LINKED;
452     GST_TEE_DYN_UNLOCK (tee);
453
454     GST_DEBUG_OBJECT (tee, "got return value %d", res);
455
456     gst_object_unref (pad);
457
458     GST_OBJECT_LOCK (tee);
459     if (GST_ELEMENT_CAST (tee)->pads_cookie != cookie) {
460       GST_DEBUG_OBJECT (tee, "pad list changed, restart");
461       /* pad list changed, restart. If the pad alloc function returned OK we
462        * need to unref the buffer */
463       if (res == GST_FLOW_OK)
464         gst_buffer_unref (*buf);
465       *buf = NULL;
466       goto retry;
467     }
468     if (res == GST_FLOW_OK) {
469       GST_DEBUG_OBJECT (tee, "we have a buffer on pad %s:%s",
470           GST_DEBUG_PAD_NAME (pad));
471       /* we have a buffer, keep the pad for later and exit the loop. */
472       tee->allocpad = pad;
473       GST_OBJECT_UNLOCK (tee);
474       g_object_notify (G_OBJECT (tee), "alloc-pad");
475       GST_OBJECT_LOCK (tee);
476       break;
477     }
478     /* no valid buffer, try another pad */
479     pads = g_list_next (pads);
480   }
481
482   return res;
483 }
484
485 static GstFlowReturn
486 gst_tee_buffer_alloc (GstPad * pad, guint64 offset, guint size,
487     GstCaps * caps, GstBuffer ** buf)
488 {
489   GstTee *tee;
490   GstFlowReturn res;
491   GstPad *allocpad;
492
493   tee = GST_TEE (GST_PAD_PARENT (pad));
494
495   res = GST_FLOW_NOT_LINKED;
496
497   GST_OBJECT_LOCK (tee);
498   if ((allocpad = tee->allocpad)) {
499     PushData *data;
500
501     /* if we had a previous pad we used for allocating a buffer, continue using
502      * it. */
503     GST_DEBUG_OBJECT (tee, "using pad %s:%s for alloc",
504         GST_DEBUG_PAD_NAME (allocpad));
505     gst_object_ref (allocpad);
506     GST_OBJECT_UNLOCK (tee);
507
508     GST_TEE_DYN_LOCK (tee);
509     data = g_object_get_qdata (G_OBJECT (allocpad), push_data);
510     if (!data->removed)
511       res = gst_pad_alloc_buffer (allocpad, offset, size, caps, buf);
512     else
513       res = GST_FLOW_NOT_LINKED;
514     GST_TEE_DYN_UNLOCK (tee);
515
516     gst_object_unref (allocpad);
517
518     GST_OBJECT_LOCK (tee);
519   }
520   /* either we failed to alloc on the the previous pad or we did not have a
521    * previous pad. */
522   if (res == GST_FLOW_NOT_LINKED) {
523     /* find a new pad to alloc a buffer on */
524     GST_DEBUG_OBJECT (tee, "finding pad for alloc");
525     res = gst_tee_find_buffer_alloc (tee, offset, size, caps, buf);
526   }
527   GST_OBJECT_UNLOCK (tee);
528
529   return res;
530 }
531
532 /* on the sink we accept caps that are acceptable to all srcpads */
533 static gboolean
534 gst_tee_sink_acceptcaps (GstPad * pad, GstCaps * caps)
535 {
536   GstTee *tee;
537   gboolean res, done;
538   GstIterator *it;
539
540   tee = GST_TEE (GST_PAD_PARENT (pad));
541
542   it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (tee));
543
544   res = TRUE;
545   done = FALSE;
546   while (!done && res) {
547     gpointer item;
548
549     switch (gst_iterator_next (it, &item)) {
550       case GST_ITERATOR_OK:
551         res &= gst_pad_peer_accept_caps (GST_PAD_CAST (item), caps);
552         gst_object_unref (item);
553         break;
554       case GST_ITERATOR_RESYNC:
555         res = TRUE;
556         gst_iterator_resync (it);
557         break;
558       case GST_ITERATOR_ERROR:
559         res = FALSE;
560         done = TRUE;
561         break;
562       case GST_ITERATOR_DONE:
563         done = TRUE;
564         break;
565     }
566   }
567   gst_iterator_free (it);
568
569   return res;
570 }
571
572 static GstFlowReturn
573 gst_tee_do_push (GstTee * tee, GstPad * pad, gpointer data, gboolean is_list)
574 {
575   GstFlowReturn res;
576
577   if (G_UNLIKELY (!tee->silent)) {
578     GST_OBJECT_LOCK (tee);
579     g_free (tee->last_message);
580     if (is_list) {
581       tee->last_message =
582           g_strdup_printf ("chain-list   ******* (%s:%s)t %p",
583           GST_DEBUG_PAD_NAME (pad), data);
584     } else {
585       tee->last_message =
586           g_strdup_printf ("chain        ******* (%s:%s)t (%d bytes, %"
587           G_GUINT64_FORMAT ") %p", GST_DEBUG_PAD_NAME (pad),
588           GST_BUFFER_SIZE (data), GST_BUFFER_TIMESTAMP (data), data);
589     }
590     GST_OBJECT_UNLOCK (tee);
591     g_object_notify (G_OBJECT (tee), "last_message");
592   }
593
594   /* Push */
595   if (pad == tee->pull_pad) {
596     /* don't push on the pad we're pulling from */
597     res = GST_FLOW_OK;
598   } else if (is_list) {
599     res =
600         gst_pad_push_list (pad,
601         gst_buffer_list_ref (GST_BUFFER_LIST_CAST (data)));
602   } else {
603     res = gst_pad_push (pad, gst_buffer_ref (GST_BUFFER_CAST (data)));
604   }
605   return res;
606 }
607
608 static void
609 clear_pads (GstPad * pad, GstTee * tee)
610 {
611   PushData *data;
612
613   data = g_object_get_qdata (G_OBJECT (pad), push_data);
614
615   /* the data must be there or we have a screwed up internal state */
616   g_assert (data != NULL);
617
618   data->pushed = FALSE;
619   data->result = GST_FLOW_NOT_LINKED;
620 }
621
622 static GstFlowReturn
623 gst_tee_handle_data (GstTee * tee, gpointer data, gboolean is_list)
624 {
625   GList *pads;
626   guint32 cookie;
627   GstFlowReturn ret, cret;
628
629   if (!is_list) {
630     tee->offset += GST_BUFFER_SIZE (data);
631   }
632
633   GST_OBJECT_LOCK (tee);
634   /* mark all pads as 'not pushed on yet' */
635   g_list_foreach (GST_ELEMENT_CAST (tee)->srcpads, (GFunc) clear_pads, tee);
636
637 restart:
638   cret = GST_FLOW_NOT_LINKED;
639   pads = GST_ELEMENT_CAST (tee)->srcpads;
640   cookie = GST_ELEMENT_CAST (tee)->pads_cookie;
641
642   while (pads) {
643     GstPad *pad;
644     PushData *pdata;
645
646     pad = GST_PAD_CAST (pads->data);
647
648     /* get the private data, something is really wrong with the internal state
649      * when it is not there */
650     pdata = g_object_get_qdata (G_OBJECT (pad), push_data);
651     g_assert (pdata != NULL);
652
653     if (!pdata->pushed) {
654       /* not yet pushed, release lock and start pushing */
655       gst_object_ref (pad);
656       GST_OBJECT_UNLOCK (tee);
657
658       GST_LOG_OBJECT (tee, "Starting to push %s %p",
659           is_list ? "list" : "buffer", data);
660
661       ret = gst_tee_do_push (tee, pad, data, is_list);
662
663       GST_LOG_OBJECT (tee, "Pushing item %p yielded result %s", data,
664           gst_flow_get_name (ret));
665
666       GST_OBJECT_LOCK (tee);
667       /* keep track of which pad we pushed and the result value. We need to do
668        * this before we release the refcount on the pad, the PushData is
669        * destroyed when the last ref of the pad goes away. */
670       pdata->pushed = TRUE;
671       pdata->result = ret;
672       gst_object_unref (pad);
673     } else {
674       /* already pushed, use previous return value */
675       ret = pdata->result;
676       GST_LOG_OBJECT (tee, "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 (GST_ELEMENT_CAST (tee)->pads_cookie != cookie) {
684       GST_LOG_OBJECT (tee, "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 (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 (ret != GST_FLOW_NOT_LINKED) {
697       GST_LOG_OBJECT (tee, "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 error:
711   {
712     GST_DEBUG_OBJECT (tee, "received error %s", gst_flow_get_name (ret));
713     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
714     GST_OBJECT_UNLOCK (tee);
715     return ret;
716   }
717 }
718
719 static GstFlowReturn
720 gst_tee_chain (GstPad * pad, GstBuffer * buffer)
721 {
722   GstFlowReturn res;
723   GstTee *tee;
724
725   tee = GST_TEE (gst_pad_get_parent (pad));
726
727   GST_DEBUG_OBJECT (tee, "received buffer %p", buffer);
728
729   res = gst_tee_handle_data (tee, buffer, FALSE);
730
731   GST_DEBUG_OBJECT (tee, "handled buffer %s", gst_flow_get_name (res));
732
733   gst_object_unref (tee);
734
735   return res;
736 }
737
738 static GstFlowReturn
739 gst_tee_chain_list (GstPad * pad, GstBufferList * list)
740 {
741   GstFlowReturn res;
742   GstTee *tee;
743
744   tee = GST_TEE (gst_pad_get_parent (pad));
745
746   GST_DEBUG_OBJECT (tee, "received list %p", list);
747
748   res = gst_tee_handle_data (tee, list, TRUE);
749
750   GST_DEBUG_OBJECT (tee, "handled list %s", gst_flow_get_name (res));
751
752   gst_object_unref (tee);
753
754   return res;
755 }
756
757 static gboolean
758 gst_tee_sink_activate_push (GstPad * pad, gboolean active)
759 {
760   GstTee *tee;
761
762   tee = GST_TEE (GST_OBJECT_PARENT (pad));
763
764   GST_OBJECT_LOCK (tee);
765   tee->sink_mode = active && GST_ACTIVATE_PUSH;
766
767   if (active && !tee->has_chain)
768     goto no_chain;
769   GST_OBJECT_UNLOCK (tee);
770
771   return TRUE;
772
773   /* ERRORS */
774 no_chain:
775   {
776     GST_OBJECT_UNLOCK (tee);
777     GST_INFO_OBJECT (tee,
778         "Tee cannot operate in push mode with has-chain==FALSE");
779     return FALSE;
780   }
781 }
782
783 static gboolean
784 gst_tee_src_activate_pull (GstPad * pad, gboolean active)
785 {
786   GstTee *tee;
787   gboolean res;
788   GstPad *sinkpad;
789
790   tee = GST_TEE (gst_pad_get_parent (pad));
791
792   GST_OBJECT_LOCK (tee);
793
794   if (tee->pull_mode == GST_TEE_PULL_MODE_NEVER)
795     goto cannot_pull;
796
797   if (tee->pull_mode == GST_TEE_PULL_MODE_SINGLE && active && tee->pull_pad)
798     goto cannot_pull_multiple_srcs;
799
800   sinkpad = gst_object_ref (tee->sinkpad);
801
802   GST_OBJECT_UNLOCK (tee);
803
804   res = gst_pad_activate_pull (sinkpad, active);
805   gst_object_unref (sinkpad);
806
807   if (!res)
808     goto sink_activate_failed;
809
810   GST_OBJECT_LOCK (tee);
811   if (active) {
812     if (tee->pull_mode == GST_TEE_PULL_MODE_SINGLE)
813       tee->pull_pad = pad;
814   } else {
815     if (pad == tee->pull_pad)
816       tee->pull_pad = NULL;
817   }
818   tee->sink_mode = active && GST_ACTIVATE_PULL;
819   GST_OBJECT_UNLOCK (tee);
820
821   gst_object_unref (tee);
822
823   return res;
824
825   /* ERRORS */
826 cannot_pull:
827   {
828     GST_OBJECT_UNLOCK (tee);
829     GST_INFO_OBJECT (tee, "Cannot activate in pull mode, pull-mode "
830         "set to NEVER");
831     gst_object_unref (tee);
832     return FALSE;
833   }
834 cannot_pull_multiple_srcs:
835   {
836     GST_OBJECT_UNLOCK (tee);
837     GST_INFO_OBJECT (tee, "Cannot activate multiple src pads in pull mode, "
838         "pull-mode set to SINGLE");
839     gst_object_unref (tee);
840     return FALSE;
841   }
842 sink_activate_failed:
843   {
844     GST_INFO_OBJECT (tee, "Failed to %sactivate sink pad in pull mode",
845         active ? "" : "de");
846     gst_object_unref (tee);
847     return FALSE;
848   }
849 }
850
851 static gboolean
852 gst_tee_src_check_get_range (GstPad * pad)
853 {
854   GstTee *tee;
855   gboolean res;
856   GstPad *sinkpad;
857
858   tee = GST_TEE (gst_pad_get_parent (pad));
859
860   GST_OBJECT_LOCK (tee);
861
862   if (tee->pull_mode == GST_TEE_PULL_MODE_NEVER)
863     goto cannot_pull;
864
865   if (tee->pull_mode == GST_TEE_PULL_MODE_SINGLE && tee->pull_pad)
866     goto cannot_pull_multiple_srcs;
867
868   sinkpad = gst_object_ref (tee->sinkpad);
869
870   GST_OBJECT_UNLOCK (tee);
871
872   res = gst_pad_check_pull_range (sinkpad);
873   gst_object_unref (sinkpad);
874
875   gst_object_unref (tee);
876
877   return res;
878
879   /* ERRORS */
880 cannot_pull:
881   {
882     GST_OBJECT_UNLOCK (tee);
883     GST_INFO_OBJECT (tee, "Cannot activate in pull mode, pull-mode "
884         "set to NEVER");
885     gst_object_unref (tee);
886     return FALSE;
887   }
888 cannot_pull_multiple_srcs:
889   {
890     GST_OBJECT_UNLOCK (tee);
891     GST_INFO_OBJECT (tee, "Cannot activate multiple src pads in pull mode, "
892         "pull-mode set to SINGLE");
893     gst_object_unref (tee);
894     return FALSE;
895   }
896 }
897
898 static void
899 gst_tee_push_eos (GstPad * pad, GstTee * tee)
900 {
901   if (pad != tee->pull_pad)
902     gst_pad_push_event (pad, gst_event_new_eos ());
903   gst_object_unref (pad);
904 }
905
906 static void
907 gst_tee_pull_eos (GstTee * tee)
908 {
909   GstIterator *iter;
910
911   iter = gst_element_iterate_src_pads (GST_ELEMENT (tee));
912   gst_iterator_foreach (iter, (GFunc) gst_tee_push_eos, tee);
913   gst_iterator_free (iter);
914 }
915
916 static GstFlowReturn
917 gst_tee_src_get_range (GstPad * pad, guint64 offset, guint length,
918     GstBuffer ** buf)
919 {
920   GstTee *tee;
921   GstFlowReturn ret;
922
923   tee = GST_TEE (gst_pad_get_parent (pad));
924
925   ret = gst_pad_pull_range (tee->sinkpad, offset, length, buf);
926
927   if (ret == GST_FLOW_OK)
928     ret = gst_tee_handle_data (tee, gst_buffer_ref (*buf), FALSE);
929   else if (ret == GST_FLOW_UNEXPECTED)
930     gst_tee_pull_eos (tee);
931
932   gst_object_unref (tee);
933
934   return ret;
935 }