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