inputselector: log times in human readable form
[platform/upstream/gstreamer.git] / plugins / elements / gstinputselector.c
1 /* GStreamer
2  * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
3  * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
4  * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
5  * Copyright (C) 2007 Wim Taymans <wim.taymans@gmail.com>
6  * Copyright (C) 2007 Andy Wingo <wingo@pobox.com>
7  * Copyright (C) 2008 Nokia Corporation. (contact <stefan.kost@nokia.com>)
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /**
26  * SECTION:element-input-selector
27  * @see_also: #GstOutputSelector
28  *
29  * Direct one out of N input streams to the output pad.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <string.h>
37
38 #include "gstinputselector.h"
39 #include "gstselector-marshal.h"
40
41 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
42 #define GST_CAT_DEFAULT input_selector_debug
43
44 static GstStaticPadTemplate gst_input_selector_sink_factory =
45 GST_STATIC_PAD_TEMPLATE ("sink%d",
46     GST_PAD_SINK,
47     GST_PAD_REQUEST,
48     GST_STATIC_CAPS_ANY);
49
50 static GstStaticPadTemplate gst_input_selector_src_factory =
51 GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS_ANY);
55
56 enum
57 {
58   PROP_0,
59   PROP_N_PADS,
60   PROP_ACTIVE_PAD,
61   PROP_SELECT_ALL,
62   PROP_LAST
63 };
64
65 #define DEFAULT_PAD_ALWAYS_OK   TRUE
66
67 enum
68 {
69   PROP_PAD_0,
70   PROP_PAD_RUNNING_TIME,
71   PROP_PAD_TAGS,
72   PROP_PAD_ACTIVE,
73   PROP_PAD_ALWAYS_OK,
74   PROP_PAD_LAST
75 };
76
77 enum
78 {
79   /* methods */
80   SIGNAL_BLOCK,
81   SIGNAL_SWITCH,
82   LAST_SIGNAL
83 };
84 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
85
86 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
87     sel, GstPad * pad);
88 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
89     GstPad * pad);
90 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
91     gboolean strict);
92 static gboolean gst_input_selector_check_eos (GstElement * selector);
93
94 #define GST_TYPE_SELECTOR_PAD \
95   (gst_selector_pad_get_type())
96 #define GST_SELECTOR_PAD(obj) \
97   (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
98 #define GST_SELECTOR_PAD_CLASS(klass) \
99   (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
100 #define GST_IS_SELECTOR_PAD(obj) \
101   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
102 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
103   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
104 #define GST_SELECTOR_PAD_CAST(obj) \
105   ((GstSelectorPad *)(obj))
106
107 typedef struct _GstSelectorPad GstSelectorPad;
108 typedef struct _GstSelectorPadClass GstSelectorPadClass;
109
110 struct _GstSelectorPad
111 {
112   GstPad parent;
113
114   gboolean active;              /* when buffer have passed the pad */
115   gboolean eos;                 /* when EOS has been received */
116   gboolean discont;             /* after switching we create a discont */
117   gboolean always_ok;
118   GstSegment segment;           /* the current segment on the pad */
119   GstTagList *tags;             /* last tags received on the pad */
120
121   gboolean segment_pending;
122 };
123
124 struct _GstSelectorPadClass
125 {
126   GstPadClass parent;
127 };
128
129 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
130 static void gst_selector_pad_init (GstSelectorPad * pad);
131 static void gst_selector_pad_finalize (GObject * object);
132 static void gst_selector_pad_get_property (GObject * object,
133     guint prop_id, GValue * value, GParamSpec * pspec);
134 static void gst_selector_pad_set_property (GObject * object,
135     guint prop_id, const GValue * value, GParamSpec * pspec);
136
137 static GstPadClass *selector_pad_parent_class = NULL;
138
139 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
140 static void gst_selector_pad_reset (GstSelectorPad * pad);
141 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
142 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
143 static gboolean gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps);
144 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad);
145 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
146 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
147     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
148
149 static GType
150 gst_selector_pad_get_type (void)
151 {
152   static GType selector_pad_type = 0;
153
154   if (!selector_pad_type) {
155     static const GTypeInfo selector_pad_info = {
156       sizeof (GstSelectorPadClass),
157       NULL,
158       NULL,
159       (GClassInitFunc) gst_selector_pad_class_init,
160       NULL,
161       NULL,
162       sizeof (GstSelectorPad),
163       0,
164       (GInstanceInitFunc) gst_selector_pad_init,
165     };
166
167     selector_pad_type =
168         g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
169         &selector_pad_info, 0);
170   }
171   return selector_pad_type;
172 }
173
174 static void
175 gst_selector_pad_class_init (GstSelectorPadClass * klass)
176 {
177   GObjectClass *gobject_class;
178
179   gobject_class = (GObjectClass *) klass;
180
181   selector_pad_parent_class = g_type_class_peek_parent (klass);
182
183   gobject_class->finalize = gst_selector_pad_finalize;
184
185   gobject_class->get_property = gst_selector_pad_get_property;
186   gobject_class->set_property = gst_selector_pad_set_property;
187
188   g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
189       g_param_spec_int64 ("running-time", "Running time",
190           "Running time of stream on pad", 0, G_MAXINT64, 0,
191           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
192   g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
193       g_param_spec_boxed ("tags", "Tags",
194           "The currently active tags on the pad", GST_TYPE_TAG_LIST,
195           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
196   g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
197       g_param_spec_boolean ("active", "Active",
198           "If the pad is currently active", FALSE,
199           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
200   g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
201       g_param_spec_boolean ("always-ok", "Always OK",
202           "Make an inactive pad return OK instead of NOT_LINKED",
203           DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 }
205
206 static void
207 gst_selector_pad_init (GstSelectorPad * pad)
208 {
209   pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
210   gst_selector_pad_reset (pad);
211 }
212
213 static void
214 gst_selector_pad_finalize (GObject * object)
215 {
216   GstSelectorPad *pad;
217
218   pad = GST_SELECTOR_PAD_CAST (object);
219
220   if (pad->tags)
221     gst_tag_list_free (pad->tags);
222
223   G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
224 }
225
226 static void
227 gst_selector_pad_set_property (GObject * object, guint prop_id,
228     const GValue * value, GParamSpec * pspec)
229 {
230   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
231
232   switch (prop_id) {
233     case PROP_PAD_ALWAYS_OK:
234       GST_OBJECT_LOCK (object);
235       spad->always_ok = g_value_get_boolean (value);
236       GST_OBJECT_UNLOCK (object);
237       break;
238     default:
239       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240       break;
241   }
242 }
243
244 static void
245 gst_selector_pad_get_property (GObject * object, guint prop_id,
246     GValue * value, GParamSpec * pspec)
247 {
248   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
249
250   switch (prop_id) {
251     case PROP_PAD_RUNNING_TIME:
252       g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
253       break;
254     case PROP_PAD_TAGS:
255       GST_OBJECT_LOCK (object);
256       g_value_set_boxed (value, spad->tags);
257       GST_OBJECT_UNLOCK (object);
258       break;
259     case PROP_PAD_ACTIVE:
260     {
261       GstInputSelector *sel;
262
263       sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
264       g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
265               GST_PAD_CAST (spad)));
266       gst_object_unref (sel);
267       break;
268     }
269     case PROP_PAD_ALWAYS_OK:
270       GST_OBJECT_LOCK (object);
271       g_value_set_boolean (value, spad->always_ok);
272       GST_OBJECT_UNLOCK (object);
273       break;
274     default:
275       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276       break;
277   }
278 }
279
280 static gint64
281 gst_selector_pad_get_running_time (GstSelectorPad * pad)
282 {
283   gint64 ret = 0;
284
285   GST_OBJECT_LOCK (pad);
286   if (pad->active) {
287     gint64 last_stop = pad->segment.last_stop;
288
289     if (last_stop >= 0)
290       ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
291           last_stop);
292   }
293   GST_OBJECT_UNLOCK (pad);
294
295   GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
296       GST_TIME_ARGS (ret));
297
298   return ret;
299 }
300
301 static void
302 gst_selector_pad_reset (GstSelectorPad * pad)
303 {
304   GST_OBJECT_LOCK (pad);
305   pad->active = FALSE;
306   pad->eos = FALSE;
307   pad->segment_pending = FALSE;
308   pad->discont = FALSE;
309   gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
310   GST_OBJECT_UNLOCK (pad);
311 }
312
313 /* strictly get the linked pad from the sinkpad. If the pad is active we return
314  * the srcpad else we return NULL */
315 static GstIterator *
316 gst_selector_pad_iterate_linked_pads (GstPad * pad)
317 {
318   GstInputSelector *sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
319   GstPad *otherpad;
320   GstIterator *it;
321
322   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
323   it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
324       (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
325
326   if (otherpad)
327     gst_object_unref (otherpad);
328   gst_object_unref (sel);
329
330   return it;
331 }
332
333 static gboolean
334 gst_selector_pad_event (GstPad * pad, GstEvent * event)
335 {
336   gboolean res = TRUE;
337   gboolean forward = TRUE;
338   GstInputSelector *sel;
339   GstSelectorPad *selpad;
340   GstPad *prev_active_sinkpad;
341   GstPad *active_sinkpad;
342
343   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
344   selpad = GST_SELECTOR_PAD_CAST (pad);
345
346   GST_INPUT_SELECTOR_LOCK (sel);
347   prev_active_sinkpad = sel->active_sinkpad;
348   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
349
350   /* only forward if we are dealing with the active sinkpad or if select_all
351    * is enabled */
352   if (pad != active_sinkpad && !sel->select_all)
353     forward = FALSE;
354   GST_INPUT_SELECTOR_UNLOCK (sel);
355
356   if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
357     g_object_notify (G_OBJECT (sel), "active-pad");
358
359   switch (GST_EVENT_TYPE (event)) {
360     case GST_EVENT_FLUSH_START:
361       /* FIXME, flush out the waiter */
362       break;
363     case GST_EVENT_FLUSH_STOP:
364       GST_INPUT_SELECTOR_LOCK (sel);
365       gst_selector_pad_reset (selpad);
366       sel->pending_close = FALSE;
367       GST_INPUT_SELECTOR_UNLOCK (sel);
368       break;
369     case GST_EVENT_NEWSEGMENT:
370     {
371       gboolean update;
372       GstFormat format;
373       gdouble rate, arate;
374       gint64 start, stop, time;
375
376       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
377           &start, &stop, &time);
378
379       GST_DEBUG_OBJECT (pad,
380           "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
381           "format %d, "
382           "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
383           G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
384
385       GST_INPUT_SELECTOR_LOCK (sel);
386       GST_OBJECT_LOCK (selpad);
387       gst_segment_set_newsegment_full (&selpad->segment, update,
388           rate, arate, format, start, stop, time);
389       GST_OBJECT_UNLOCK (selpad);
390
391       /* If we aren't forwarding the event (because the pad is not the
392        * active_sinkpad, and select_all is not set, then set the flag on the
393        * that says a segment needs sending if/when that pad is activated.
394        * For all other cases, we send the event immediately, which makes
395        * sparse streams and other segment updates work correctly downstream.
396        */
397       if (!forward)
398         selpad->segment_pending = TRUE;
399
400       GST_INPUT_SELECTOR_UNLOCK (sel);
401       break;
402     }
403     case GST_EVENT_TAG:
404     {
405       GstTagList *tags, *oldtags, *newtags;
406
407       gst_event_parse_tag (event, &tags);
408
409       GST_OBJECT_LOCK (selpad);
410       oldtags = selpad->tags;
411
412       newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
413       selpad->tags = newtags;
414       if (oldtags)
415         gst_tag_list_free (oldtags);
416       GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
417       GST_OBJECT_UNLOCK (selpad);
418
419       g_object_notify (G_OBJECT (selpad), "tags");
420       break;
421     }
422     case GST_EVENT_EOS:
423       selpad->eos = TRUE;
424       GST_DEBUG_OBJECT (pad, "received EOS");
425       /* don't forward eos in select_all mode until all sink pads have eos */
426       if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
427         forward = FALSE;
428       }
429       break;
430     default:
431       break;
432   }
433   if (forward) {
434     GST_DEBUG_OBJECT (pad, "forwarding event");
435     res = gst_pad_push_event (sel->srcpad, event);
436   } else
437     gst_event_unref (event);
438
439   gst_object_unref (sel);
440
441   return res;
442 }
443
444 static GstCaps *
445 gst_selector_pad_getcaps (GstPad * pad)
446 {
447   GstInputSelector *sel;
448   GstCaps *caps;
449
450   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
451
452   GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
453   caps = gst_pad_peer_get_caps_reffed (sel->srcpad);
454   if (caps == NULL)
455     caps = gst_caps_new_any ();
456
457   gst_object_unref (sel);
458
459   return caps;
460 }
461
462 static gboolean
463 gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps)
464 {
465   GstInputSelector *sel;
466   gboolean res;
467
468   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
469
470   GST_DEBUG_OBJECT (sel, "Checking acceptcaps of srcpad peer");
471   res = gst_pad_peer_accept_caps (sel->srcpad, caps);
472   gst_object_unref (sel);
473
474   return res;
475 }
476
477 static GstFlowReturn
478 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
479     guint size, GstCaps * caps, GstBuffer ** buf)
480 {
481   GstInputSelector *sel;
482   GstFlowReturn result;
483   GstPad *active_sinkpad;
484   GstPad *prev_active_sinkpad;
485   GstSelectorPad *selpad;
486
487   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
488   selpad = GST_SELECTOR_PAD_CAST (pad);
489
490   GST_LOG_OBJECT (pad, "received alloc");
491
492   GST_INPUT_SELECTOR_LOCK (sel);
493   prev_active_sinkpad = sel->active_sinkpad;
494   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
495
496   if (pad != active_sinkpad)
497     goto not_active;
498
499   GST_INPUT_SELECTOR_UNLOCK (sel);
500
501   if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
502     g_object_notify (G_OBJECT (sel), "active-pad");
503
504   result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
505
506 done:
507   gst_object_unref (sel);
508
509   return result;
510
511   /* ERRORS */
512 not_active:
513   {
514     GST_INPUT_SELECTOR_UNLOCK (sel);
515
516     /* unselected pad, perform fallback alloc or return unlinked when
517      * asked */
518     GST_OBJECT_LOCK (selpad);
519     if (selpad->always_ok) {
520       GST_DEBUG_OBJECT (pad, "Not selected, performing fallback allocation");
521       *buf = NULL;
522       result = GST_FLOW_OK;
523     } else {
524       GST_DEBUG_OBJECT (pad, "Not selected, return NOT_LINKED");
525       result = GST_FLOW_NOT_LINKED;
526     }
527     GST_OBJECT_UNLOCK (selpad);
528
529     goto done;
530   }
531 }
532
533 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked 
534  * or return TRUE when flushing */
535 static gboolean
536 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
537 {
538   while (self->blocked && !self->flushing) {
539     /* we can be unlocked here when we are shutting down (flushing) or when we
540      * get unblocked */
541     GST_INPUT_SELECTOR_WAIT (self);
542   }
543   return self->flushing;
544 }
545
546 static GstFlowReturn
547 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
548 {
549   GstInputSelector *sel;
550   GstFlowReturn res;
551   GstPad *active_sinkpad;
552   GstPad *prev_active_sinkpad;
553   GstSelectorPad *selpad;
554   GstClockTime start_time;
555   GstSegment *seg;
556   GstEvent *close_event = NULL, *start_event = NULL;
557   GstCaps *caps;
558
559   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
560   selpad = GST_SELECTOR_PAD_CAST (pad);
561   seg = &selpad->segment;
562
563   GST_INPUT_SELECTOR_LOCK (sel);
564   /* wait or check for flushing */
565   if (gst_input_selector_wait (sel, pad))
566     goto flushing;
567
568   GST_LOG_OBJECT (pad, "getting active pad");
569
570   prev_active_sinkpad = sel->active_sinkpad;
571   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
572
573   /* update the segment on the srcpad */
574   start_time = GST_BUFFER_TIMESTAMP (buf);
575   if (GST_CLOCK_TIME_IS_VALID (start_time)) {
576     GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
577         GST_TIME_ARGS (start_time));
578     if (GST_BUFFER_DURATION_IS_VALID (buf))
579       GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
580           GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
581
582     GST_OBJECT_LOCK (pad);
583     gst_segment_set_last_stop (seg, seg->format, start_time);
584     GST_OBJECT_UNLOCK (pad);
585   }
586
587   /* Ignore buffers from pads except the selected one */
588   if (pad != active_sinkpad)
589     goto ignore;
590
591   if (G_UNLIKELY (sel->pending_close)) {
592     GstSegment *cseg = &sel->segment;
593
594     GST_DEBUG_OBJECT (sel,
595         "pushing close NEWSEGMENT update %d, rate %lf, applied rate %lf, "
596         "format %d, "
597         "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
598         G_GINT64_FORMAT, TRUE, cseg->rate, cseg->applied_rate, cseg->format,
599         cseg->start, cseg->stop, cseg->time);
600
601     /* create update segment */
602     close_event = gst_event_new_new_segment_full (TRUE, cseg->rate,
603         cseg->applied_rate, cseg->format, cseg->start, cseg->stop, cseg->time);
604
605     sel->pending_close = FALSE;
606   }
607   /* if we have a pending segment, push it out now */
608   if (G_UNLIKELY (selpad->segment_pending)) {
609     GST_DEBUG_OBJECT (pad,
610         "pushing pending NEWSEGMENT update %d, rate %lf, applied rate %lf, "
611         "format %d, "
612         "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
613         G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
614         seg->start, seg->stop, seg->time);
615
616     start_event = gst_event_new_new_segment_full (FALSE, seg->rate,
617         seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
618
619     selpad->segment_pending = FALSE;
620   }
621   GST_INPUT_SELECTOR_UNLOCK (sel);
622
623   if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
624     g_object_notify (G_OBJECT (sel), "active-pad");
625
626   if (close_event)
627     gst_pad_push_event (sel->srcpad, close_event);
628
629   if (start_event)
630     gst_pad_push_event (sel->srcpad, start_event);
631
632   if (selpad->discont) {
633     buf = gst_buffer_make_metadata_writable (buf);
634
635     GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
636     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
637     selpad->discont = FALSE;
638   }
639
640   /* forward */
641   GST_LOG_OBJECT (pad, "Forwarding buffer %p", buf);
642
643   if ((caps = GST_BUFFER_CAPS (buf))) {
644     if (GST_PAD_CAPS (sel->srcpad) != caps)
645       gst_pad_set_caps (sel->srcpad, caps);
646   }
647
648   res = gst_pad_push (sel->srcpad, buf);
649
650 done:
651   gst_object_unref (sel);
652   return res;
653
654   /* dropped buffers */
655 ignore:
656   {
657     GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
658     /* when we drop a buffer, we're creating a discont on this pad */
659     selpad->discont = TRUE;
660     GST_INPUT_SELECTOR_UNLOCK (sel);
661     gst_buffer_unref (buf);
662
663     /* figure out what to return upstream */
664     GST_OBJECT_LOCK (selpad);
665     if (selpad->always_ok)
666       res = GST_FLOW_OK;
667     else
668       res = GST_FLOW_NOT_LINKED;
669     GST_OBJECT_UNLOCK (selpad);
670
671     goto done;
672   }
673 flushing:
674   {
675     GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
676     GST_INPUT_SELECTOR_UNLOCK (sel);
677     gst_buffer_unref (buf);
678     res = GST_FLOW_WRONG_STATE;
679     goto done;
680   }
681 }
682
683 static void gst_input_selector_dispose (GObject * object);
684
685 static void gst_input_selector_set_property (GObject * object,
686     guint prop_id, const GValue * value, GParamSpec * pspec);
687 static void gst_input_selector_get_property (GObject * object,
688     guint prop_id, GValue * value, GParamSpec * pspec);
689
690 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
691     GstPadTemplate * templ, const gchar * unused);
692 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
693
694 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
695     element, GstStateChange transition);
696
697 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
698 static gboolean gst_input_selector_event (GstPad * pad, GstEvent * event);
699 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
700 static gint64 gst_input_selector_block (GstInputSelector * self);
701 static void gst_input_selector_switch (GstInputSelector * self,
702     GstPad * pad, gint64 stop_time, gint64 start_time);
703
704 #define _do_init(bla) \
705     GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
706         "input-selector", 0, "An input stream selector element");
707
708 GST_BOILERPLATE_FULL (GstInputSelector, gst_input_selector, GstElement,
709     GST_TYPE_ELEMENT, _do_init);
710
711 static void
712 gst_input_selector_base_init (gpointer g_class)
713 {
714   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
715
716   gst_element_class_set_details_simple (element_class, "Input selector",
717       "Generic", "N-to-1 input stream selectoring",
718       "Julien Moutte <julien@moutte.net>, "
719       "Jan Schmidt <thaytan@mad.scientist.com>, "
720       "Wim Taymans <wim.taymans@gmail.com>");
721   gst_element_class_add_pad_template (element_class,
722       gst_static_pad_template_get (&gst_input_selector_sink_factory));
723   gst_element_class_add_pad_template (element_class,
724       gst_static_pad_template_get (&gst_input_selector_src_factory));
725 }
726
727 static void
728 gst_input_selector_class_init (GstInputSelectorClass * klass)
729 {
730   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
731   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
732
733   parent_class = g_type_class_peek_parent (klass);
734
735   gobject_class->dispose = gst_input_selector_dispose;
736
737   gobject_class->set_property = gst_input_selector_set_property;
738   gobject_class->get_property = gst_input_selector_get_property;
739
740   g_object_class_install_property (gobject_class, PROP_N_PADS,
741       g_param_spec_uint ("n-pads", "Number of Pads",
742           "The number of sink pads", 0, G_MAXUINT, 0,
743           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
744
745   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
746       g_param_spec_object ("active-pad", "Active pad",
747           "The currently active sink pad", GST_TYPE_PAD,
748           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
749
750   g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
751       g_param_spec_boolean ("select-all", "Select all mode",
752           "Forwards data from all input pads", FALSE,
753           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
754
755   /**
756    * GstInputSelector::block:
757    * @inputselector: the #GstInputSelector
758    *
759    * Block all sink pads in preparation for a switch. Returns the stop time of
760    * the current switch segment, as a running time, or 0 if there is no current
761    * active pad or the current active pad never received data.
762    */
763   gst_input_selector_signals[SIGNAL_BLOCK] =
764       g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
765       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
766       G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
767       gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
768   /**
769    * GstInputSelector::switch:
770    * @inputselector: the #GstInputSelector
771    * @pad:            the pad to switch to
772    * @stop_time:      running time at which to close the previous segment, or -1
773    *                  to use the running time of the previously active sink pad
774    * @start_time:     running time at which to start the new segment, or -1 to
775    *                  use the running time of the newly active sink pad
776    *
777    * Switch to a new feed. The segment opened by the previously active pad, if
778    * any, will be closed, and a new segment opened before data flows again.
779    *
780    * This signal must be emitted when the element has been blocked via the <link
781    * linkend="GstInputSelector-block">block</link> signal.
782    *
783    * If you have a stream with only one switch element, such as an audio-only
784    * stream, a stream switch should be performed by first emitting the block
785    * signal, and then emitting the switch signal with -1 for the stop and start
786    * time values.
787    *
788    * The intention of the @stop_time and @start_time arguments is to allow
789    * multiple switch elements to switch and maintain stream synchronization.
790    * When switching a stream with multiple feeds, you will need as many switch
791    * elements as you have feeds. For example, a feed with audio and video will
792    * have one switch element between the audio feeds and one for video.
793    *
794    * A switch over multiple switch elements should be performed as follows:
795    * First, emit the <link linkend="GstInputSelector-block">block</link>
796    * signal, collecting the returned values. The maximum running time returned
797    * by block should then be used as the time at which to close the previous
798    * segment.
799    *
800    * Then, query the running times of the new audio and video pads that you will
801    * switch to. Naturally, these pads are on separate switch elements. Take the
802    * minimum running time for those streams and use it for the time at which to
803    * open the new segment.
804    *
805    * If @pad is the same as the current active pad, the element will cancel any
806    * previous block without adjusting segments.
807    *
808    * <note><simpara>
809    * the signal changed from accepting the pad name to the pad object.
810    * </simpara></note>
811    *
812    * Since: 0.10.7
813    */
814   gst_input_selector_signals[SIGNAL_SWITCH] =
815       g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
816       G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
817       NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
818       G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
819
820   gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
821   gstelement_class->release_pad = gst_input_selector_release_pad;
822   gstelement_class->change_state = gst_input_selector_change_state;
823
824   klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
825   /* note the underscore because switch is a keyword otherwise */
826   klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
827 }
828
829 static void
830 gst_input_selector_init (GstInputSelector * sel,
831     GstInputSelectorClass * g_class)
832 {
833   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
834   gst_pad_set_iterate_internal_links_function (sel->srcpad,
835       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
836   gst_pad_set_getcaps_function (sel->srcpad,
837       GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
838   gst_pad_set_query_function (sel->srcpad,
839       GST_DEBUG_FUNCPTR (gst_input_selector_query));
840   gst_pad_set_event_function (sel->srcpad,
841       GST_DEBUG_FUNCPTR (gst_input_selector_event));
842   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
843   /* sinkpad management */
844   sel->active_sinkpad = NULL;
845   sel->padcount = 0;
846   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
847
848   sel->lock = g_mutex_new ();
849   sel->cond = g_cond_new ();
850   sel->blocked = FALSE;
851
852   sel->select_all = FALSE;
853 }
854
855 static void
856 gst_input_selector_dispose (GObject * object)
857 {
858   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
859
860   if (sel->active_sinkpad) {
861     gst_object_unref (sel->active_sinkpad);
862     sel->active_sinkpad = NULL;
863   }
864   if (sel->lock) {
865     g_mutex_free (sel->lock);
866     sel->lock = NULL;
867   }
868   if (sel->cond) {
869     g_cond_free (sel->cond);
870     sel->cond = NULL;
871   }
872
873   G_OBJECT_CLASS (parent_class)->dispose (object);
874 }
875
876 /* Solve the following equation for B.timestamp, and set that as the segment
877  * stop:
878  * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
879  */
880 static gint64
881 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
882 {
883   if (running_time <= segment->accum)
884     return segment->start;
885   else
886     return (running_time - segment->accum) * segment->abs_rate + segment->start;
887 }
888
889 static void
890 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
891 {
892   segment->stop = gst_segment_get_timestamp (segment, running_time);
893   segment->last_stop = -1;
894 }
895
896 static void
897 gst_segment_set_start (GstSegment * segment, gint64 running_time)
898 {
899   gint64 new_start, duration;
900
901   new_start = gst_segment_get_timestamp (segment, running_time);
902
903   /* this is the duration we skipped */
904   duration = new_start - segment->start;
905   /* add the duration to the accumulated segment time */
906   segment->accum += duration;
907   /* move position in the segment */
908   segment->time += duration;
909   segment->start += duration;
910 }
911
912 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
913  * active pad changed. */
914 static gboolean
915 gst_input_selector_set_active_pad (GstInputSelector * self,
916     GstPad * pad, gint64 stop_time, gint64 start_time)
917 {
918   GstSelectorPad *old, *new;
919   GstPad **active_pad_p;
920
921   if (pad == self->active_sinkpad)
922     return FALSE;
923
924   old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
925   new = GST_SELECTOR_PAD_CAST (pad);
926
927   GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
928       GST_DEBUG_PAD_NAME (new));
929
930   if (!GST_CLOCK_TIME_IS_VALID (stop_time) && old) {
931     /* no stop time given, get the latest running_time on the active pad to 
932      * close and open the new segment */
933     stop_time = start_time = gst_selector_pad_get_running_time (old);
934     GST_DEBUG_OBJECT (self, "using start/stop of %" GST_TIME_FORMAT,
935         GST_TIME_ARGS (start_time));
936   }
937
938   if (old && old->active && !self->pending_close && stop_time >= 0) {
939     /* schedule a last_stop update if one isn't already scheduled, and a
940        segment has been pushed before. */
941     memcpy (&self->segment, &old->segment, sizeof (self->segment));
942
943     GST_DEBUG_OBJECT (self, "setting stop_time to %" GST_TIME_FORMAT,
944         GST_TIME_ARGS (stop_time));
945     gst_segment_set_stop (&self->segment, stop_time);
946     self->pending_close = TRUE;
947   }
948
949   if (new && new->active && start_time >= 0) {
950     GST_DEBUG_OBJECT (self, "setting start_time to %" GST_TIME_FORMAT,
951         GST_TIME_ARGS (start_time));
952     /* schedule a new segment push */
953     gst_segment_set_start (&new->segment, start_time);
954     new->segment_pending = TRUE;
955   }
956
957   active_pad_p = &self->active_sinkpad;
958   gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
959   GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
960       self->active_sinkpad);
961
962   return TRUE;
963 }
964
965 static void
966 gst_input_selector_set_property (GObject * object, guint prop_id,
967     const GValue * value, GParamSpec * pspec)
968 {
969   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
970
971   switch (prop_id) {
972     case PROP_ACTIVE_PAD:
973     {
974       GstPad *pad;
975
976       pad = g_value_get_object (value);
977
978       GST_INPUT_SELECTOR_LOCK (sel);
979       gst_input_selector_set_active_pad (sel, pad,
980           GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
981       GST_INPUT_SELECTOR_UNLOCK (sel);
982       break;
983     }
984     case PROP_SELECT_ALL:
985       GST_INPUT_SELECTOR_LOCK (object);
986       sel->select_all = g_value_get_boolean (value);
987       GST_INPUT_SELECTOR_UNLOCK (object);
988       break;
989     default:
990       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
991       break;
992   }
993 }
994
995 static void
996 gst_input_selector_get_property (GObject * object, guint prop_id,
997     GValue * value, GParamSpec * pspec)
998 {
999   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1000
1001   switch (prop_id) {
1002     case PROP_N_PADS:
1003       GST_INPUT_SELECTOR_LOCK (object);
1004       g_value_set_uint (value, sel->n_pads);
1005       GST_INPUT_SELECTOR_UNLOCK (object);
1006       break;
1007     case PROP_ACTIVE_PAD:
1008       GST_INPUT_SELECTOR_LOCK (object);
1009       g_value_set_object (value, sel->active_sinkpad);
1010       GST_INPUT_SELECTOR_UNLOCK (object);
1011       break;
1012     case PROP_SELECT_ALL:
1013       GST_INPUT_SELECTOR_LOCK (object);
1014       g_value_set_boolean (value, sel->select_all);
1015       GST_INPUT_SELECTOR_UNLOCK (object);
1016       break;
1017     default:
1018       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1019       break;
1020   }
1021 }
1022
1023 static GstPad *
1024 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1025 {
1026   GstInputSelector *sel;
1027   GstPad *otherpad = NULL;
1028
1029   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1030
1031   GST_INPUT_SELECTOR_LOCK (sel);
1032   if (pad == sel->srcpad)
1033     otherpad = sel->active_sinkpad;
1034   else if (pad == sel->active_sinkpad || !strict)
1035     otherpad = sel->srcpad;
1036   if (otherpad)
1037     gst_object_ref (otherpad);
1038   GST_INPUT_SELECTOR_UNLOCK (sel);
1039
1040   gst_object_unref (sel);
1041
1042   return otherpad;
1043 }
1044
1045 static gboolean
1046 gst_input_selector_event (GstPad * pad, GstEvent * event)
1047 {
1048   gboolean res = FALSE;
1049   GstPad *otherpad;
1050
1051   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1052
1053   if (otherpad) {
1054     res = gst_pad_push_event (otherpad, event);
1055
1056     gst_object_unref (otherpad);
1057   } else
1058     gst_event_unref (event);
1059   return res;
1060 }
1061
1062 /* query on the srcpad. We override this function because by default it will
1063  * only forward the query to one random sinkpad */
1064 static gboolean
1065 gst_input_selector_query (GstPad * pad, GstQuery * query)
1066 {
1067   gboolean res = TRUE;
1068   GstInputSelector *sel;
1069   GstPad *otherpad;
1070
1071   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1072
1073   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1074
1075   switch (GST_QUERY_TYPE (query)) {
1076     case GST_QUERY_LATENCY:
1077     {
1078       GList *walk;
1079       GstClockTime resmin, resmax;
1080       gboolean reslive;
1081
1082       resmin = 0;
1083       resmax = -1;
1084       reslive = FALSE;
1085
1086       /* assume FALSE, we become TRUE if one query succeeds */
1087       res = FALSE;
1088
1089       /* perform the query on all sinkpads and combine the results. We take the
1090        * max of min and the min of max for the result latency. */
1091       GST_INPUT_SELECTOR_LOCK (sel);
1092       for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1093           walk = g_list_next (walk)) {
1094         GstPad *sinkpad = GST_PAD_CAST (walk->data);
1095
1096         if (gst_pad_peer_query (sinkpad, query)) {
1097           GstClockTime min, max;
1098           gboolean live;
1099
1100           /* one query succeeded, we succeed too */
1101           res = TRUE;
1102
1103           gst_query_parse_latency (query, &live, &min, &max);
1104
1105           GST_DEBUG_OBJECT (sinkpad,
1106               "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1107               ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1108
1109           if (live) {
1110             if (min > resmin)
1111               resmin = min;
1112             if (resmax == -1)
1113               resmax = max;
1114             else if (max < resmax)
1115               resmax = max;
1116             if (reslive == FALSE)
1117               reslive = live;
1118           }
1119         }
1120       }
1121       GST_INPUT_SELECTOR_UNLOCK (sel);
1122       if (res) {
1123         gst_query_set_latency (query, reslive, resmin, resmax);
1124
1125         GST_DEBUG_OBJECT (sel,
1126             "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1127             ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1128             reslive);
1129       }
1130
1131       break;
1132     }
1133     default:
1134       if (otherpad)
1135         res = gst_pad_peer_query (otherpad, query);
1136       break;
1137   }
1138   if (otherpad)
1139     gst_object_unref (otherpad);
1140   gst_object_unref (sel);
1141
1142   return res;
1143 }
1144
1145 static GstCaps *
1146 gst_input_selector_getcaps (GstPad * pad)
1147 {
1148   GstPad *otherpad;
1149   GstObject *parent;
1150   GstCaps *caps;
1151
1152   parent = gst_object_get_parent (GST_OBJECT (pad));
1153
1154   otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1155
1156   if (!otherpad) {
1157     if (GST_INPUT_SELECTOR (parent)->select_all) {
1158       GST_DEBUG_OBJECT (parent,
1159           "Pad %s:%s not linked, returning merge of caps",
1160           GST_DEBUG_PAD_NAME (pad));
1161       caps = gst_pad_proxy_getcaps (pad);
1162     } else {
1163       GST_DEBUG_OBJECT (parent,
1164           "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1165       caps = gst_caps_new_any ();
1166     }
1167   } else {
1168     GST_DEBUG_OBJECT (parent,
1169         "Pad %s:%s is linked (to %s:%s), returning peer caps",
1170         GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1171     /* if the peer has caps, use those. If the pad is not linked, this function
1172      * returns NULL and we return ANY */
1173     if (!(caps = gst_pad_peer_get_caps_reffed (otherpad)))
1174       caps = gst_caps_new_any ();
1175     gst_object_unref (otherpad);
1176   }
1177
1178   gst_object_unref (parent);
1179   return caps;
1180 }
1181
1182 /* check if the pad is the active sinkpad */
1183 static inline gboolean
1184 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1185 {
1186   gboolean res;
1187
1188   GST_INPUT_SELECTOR_LOCK (sel);
1189   res = (pad == sel->active_sinkpad);
1190   GST_INPUT_SELECTOR_UNLOCK (sel);
1191
1192   return res;
1193 }
1194
1195 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1196 static GstPad *
1197 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1198 {
1199   GstPad *active_sinkpad;
1200   GstSelectorPad *selpad;
1201
1202   selpad = GST_SELECTOR_PAD_CAST (pad);
1203
1204   selpad->active = TRUE;
1205   active_sinkpad = sel->active_sinkpad;
1206   if (active_sinkpad == NULL || sel->select_all) {
1207     /* first pad we get activity on becomes the activated pad by default, if we
1208      * select all, we also remember the last used pad. */
1209     if (sel->active_sinkpad)
1210       gst_object_unref (sel->active_sinkpad);
1211     active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1212     GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1213   }
1214
1215   return active_sinkpad;
1216 }
1217
1218 static GstPad *
1219 gst_input_selector_request_new_pad (GstElement * element,
1220     GstPadTemplate * templ, const gchar * unused)
1221 {
1222   GstInputSelector *sel;
1223   gchar *name = NULL;
1224   GstPad *sinkpad = NULL;
1225
1226   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1227
1228   sel = GST_INPUT_SELECTOR (element);
1229
1230   GST_INPUT_SELECTOR_LOCK (sel);
1231
1232   GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1233   name = g_strdup_printf ("sink%d", sel->padcount++);
1234   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1235       "name", name, "direction", templ->direction, "template", templ, NULL);
1236   g_free (name);
1237
1238   sel->n_pads++;
1239
1240   gst_pad_set_event_function (sinkpad,
1241       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1242   gst_pad_set_getcaps_function (sinkpad,
1243       GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1244   gst_pad_set_acceptcaps_function (sinkpad,
1245       GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1246   gst_pad_set_chain_function (sinkpad,
1247       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1248   gst_pad_set_iterate_internal_links_function (sinkpad,
1249       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1250   gst_pad_set_bufferalloc_function (sinkpad,
1251       GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1252
1253   gst_pad_set_active (sinkpad, TRUE);
1254   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1255   GST_INPUT_SELECTOR_UNLOCK (sel);
1256
1257   return sinkpad;
1258 }
1259
1260 static void
1261 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1262 {
1263   GstInputSelector *sel;
1264
1265   sel = GST_INPUT_SELECTOR (element);
1266   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1267
1268   GST_INPUT_SELECTOR_LOCK (sel);
1269   /* if the pad was the active pad, makes us select a new one */
1270   if (sel->active_sinkpad == pad) {
1271     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1272     gst_object_unref (sel->active_sinkpad);
1273     sel->active_sinkpad = NULL;
1274   }
1275   sel->n_pads--;
1276
1277   gst_pad_set_active (pad, FALSE);
1278   gst_element_remove_pad (GST_ELEMENT (sel), pad);
1279   GST_INPUT_SELECTOR_UNLOCK (sel);
1280 }
1281
1282 static void
1283 gst_input_selector_reset (GstInputSelector * sel)
1284 {
1285   GList *walk;
1286
1287   GST_INPUT_SELECTOR_LOCK (sel);
1288   /* clear active pad */
1289   if (sel->active_sinkpad) {
1290     gst_object_unref (sel->active_sinkpad);
1291     sel->active_sinkpad = NULL;
1292   }
1293   /* reset segment */
1294   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1295   sel->pending_close = FALSE;
1296   /* reset each of our sinkpads state */
1297   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1298     GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1299
1300     gst_selector_pad_reset (selpad);
1301
1302     if (selpad->tags) {
1303       gst_tag_list_free (selpad->tags);
1304       selpad->tags = NULL;
1305     }
1306   }
1307   GST_INPUT_SELECTOR_UNLOCK (sel);
1308 }
1309
1310 static GstStateChangeReturn
1311 gst_input_selector_change_state (GstElement * element,
1312     GstStateChange transition)
1313 {
1314   GstInputSelector *self = GST_INPUT_SELECTOR (element);
1315   GstStateChangeReturn result;
1316
1317   switch (transition) {
1318     case GST_STATE_CHANGE_READY_TO_PAUSED:
1319       GST_INPUT_SELECTOR_LOCK (self);
1320       self->blocked = FALSE;
1321       self->flushing = FALSE;
1322       GST_INPUT_SELECTOR_UNLOCK (self);
1323       break;
1324     case GST_STATE_CHANGE_PAUSED_TO_READY:
1325       /* first unlock before we call the parent state change function, which
1326        * tries to acquire the stream lock when going to ready. */
1327       GST_INPUT_SELECTOR_LOCK (self);
1328       self->blocked = FALSE;
1329       self->flushing = TRUE;
1330       GST_INPUT_SELECTOR_BROADCAST (self);
1331       GST_INPUT_SELECTOR_UNLOCK (self);
1332       break;
1333     default:
1334       break;
1335   }
1336
1337   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1338
1339   switch (transition) {
1340     case GST_STATE_CHANGE_PAUSED_TO_READY:
1341       gst_input_selector_reset (self);
1342       break;
1343     default:
1344       break;
1345   }
1346
1347   return result;
1348 }
1349
1350 static gint64
1351 gst_input_selector_block (GstInputSelector * self)
1352 {
1353   gint64 ret = 0;
1354   GstSelectorPad *spad;
1355
1356   GST_INPUT_SELECTOR_LOCK (self);
1357
1358   if (self->blocked)
1359     GST_WARNING_OBJECT (self, "switch already blocked");
1360
1361   self->blocked = TRUE;
1362   spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1363
1364   if (spad)
1365     ret = gst_selector_pad_get_running_time (spad);
1366   else
1367     GST_DEBUG_OBJECT (self, "no active pad while blocking");
1368
1369   GST_INPUT_SELECTOR_UNLOCK (self);
1370
1371   return ret;
1372 }
1373
1374 /* stop_time and start_time are running times */
1375 static void
1376 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1377     gint64 stop_time, gint64 start_time)
1378 {
1379   gboolean changed;
1380
1381   g_return_if_fail (self->blocked == TRUE);
1382
1383   GST_INPUT_SELECTOR_LOCK (self);
1384   changed =
1385       gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1386
1387   self->blocked = FALSE;
1388   GST_INPUT_SELECTOR_BROADCAST (self);
1389   GST_INPUT_SELECTOR_UNLOCK (self);
1390
1391   if (changed)
1392     g_object_notify (G_OBJECT (self), "active-pad");
1393 }
1394
1395 static gboolean
1396 gst_input_selector_check_eos (GstElement * selector)
1397 {
1398   GstIterator *it = gst_element_iterate_sink_pads (selector);
1399   GstIteratorResult ires;
1400   gpointer item;
1401   gboolean done = FALSE, is_eos = FALSE;
1402   GstSelectorPad *pad;
1403
1404   while (!done) {
1405     ires = gst_iterator_next (it, &item);
1406     switch (ires) {
1407       case GST_ITERATOR_DONE:
1408         GST_INFO_OBJECT (selector, "all sink pads have eos");
1409         done = TRUE;
1410         is_eos = TRUE;
1411         break;
1412       case GST_ITERATOR_OK:
1413         pad = GST_SELECTOR_PAD_CAST (item);
1414         if (!pad->eos) {
1415           done = TRUE;
1416         }
1417         gst_object_unref (pad);
1418         break;
1419       case GST_ITERATOR_RESYNC:
1420         gst_iterator_resync (it);
1421         break;
1422       default:
1423         done = TRUE;
1424         break;
1425     }
1426   }
1427   gst_iterator_free (it);
1428
1429   return is_eos;
1430 }